PSCF v1.4.0
Sort.h
1#ifndef PSCF_SORT_H
2#define PSCF_SORT_H
3
4/*
5* PSCF - Polymer Self-Consistent Field
6*
7* Copyright 2015 - 2025, The Regents of the University of Minnesota
8* Distributed under the terms of the GNU General Public License.
9*/
10
11#include <util/containers/Pair.h>
12#include <util/containers/GArray.h>
13#include <vector>
14
15namespace Pscf {
16
17 using namespace Util;
18
25 namespace Sort {
26
36 template <typename T>
37 struct Item {
38 T value;
39 int id;
40 };
41
42 /*
43 * A Bunch represents a slice of the sorted array of items.
44 *
45 * A Bunch is used to represent a contiguous slice of the sorted
46 * array of items produced by the Sort::sort function within which
47 * values are equal to within some tolerance. If x is a Bunch, then
48 * x[0] is the array index of the first element in a slice, and
49 * x[1] an integer is one greater than the index of the last element
50 * in that slice.
51 *
52 * \ingroup Pscf_Math_Sort_Module
53 */
54 using Bunch = Pair<int>;
55
65 template <typename T>
66 bool operator < (Item<T> const & a, Item<T> const & b)
67 { return (a.value < b.value); }
68
77 template <typename T>
78 void sort(std::vector< Item<T> >& items);
79
98 template <typename T>
99 void findBunches(std::vector< Item<T> > const & items,
100 GArray< Bunch >& bunches,
101 T epsilon);
102
103
104 // Explicit instantiation declarations
105
106 extern template void sort<double>(std::vector< Item<double> >& );
107 extern template void sort<float>(std::vector< Item<float> >& );
108
109 extern template
110 void findBunches<double>(std::vector< Item<double> > const &,
111 GArray< Bunch >&, double);
112
113 extern template
114 void findBunches<float>(std::vector< Item<float> > const &,
115 GArray< Bunch >&, float);
116
117 } // namespace Sort
118
119} // namespace Pscf
120#endif
An automatically growable array, analogous to a std::vector.
Definition GArray.h:34
An array of exactly 2 objects.
Definition Pair.h:24
bool operator<(Item< T > const &a, Item< T > const &b)
Less than comparator for Item<T> objects.
Definition Sort.h:66
void findBunches(std::vector< Item< T > > const &items, GArray< Bunch > &bunches, T epsilon)
Identify "bunches" of equal values within a sorted vector.
Definition Sort.cpp:29
void sort(std::vector< Item< T > > &items)
Sort a std::vector< Item<T> > by ascending item value.
Definition Sort.cpp:22
PSCF package top-level namespace.
Struct with value and index, to keep track of permutation.
Definition Sort.h:37