PSCF v1.4.0
Sort.cpp
1/*
2* PSCF - Polymer Self-Consistent Field
3*
4* Copyright 2015 - 2025, The Regents of the University of Minnesota
5* Distributed under the terms of the GNU General Public License.
6*/
7
8#include "Sort.h"
9#include <util/global.h>
10#include <vector>
11#include <algorithm>
12
13namespace Pscf {
14namespace Sort {
15
16 using namespace Util;
17
18 /*
19 * Sort a std::vector of items.
20 */
21 template <typename T>
22 void sort(std::vector< Item<T> >& items)
23 { std::sort(items.begin(), items.end()); }
24
25 /*
26 * Identify slices of equal values within a sorted vector.
27 */
28 template <typename T>
29 void findBunches(std::vector< Item<T> > const & items,
30 GArray< Bunch >& bunches,
31 T epsilon)
32 {
33 int size = items.size();
34 UTIL_CHECK(size > 0);
35 bunches.clear();
36 Bunch bunch;
37 bunch[0] = 0;
38 bunch[1] = 0;
39 T newVal = items[0].value;
40 T oldVal = newVal;
41 if (size > 1) {
42 for (int i = 1; i < size; ++i) {
43 newVal = items[i].value;
44 UTIL_CHECK(newVal > oldVal - epsilon);
45 if (newVal > oldVal + epsilon) {
46 bunch[1] = i;
47 UTIL_CHECK(bunch[1] > bunch[0]);
48 bunches.append(bunch);
49 bunch[0] = i;
50 bunch[1] = i;
51 oldVal = newVal;
52 }
53 }
54 }
55 UTIL_CHECK(bunch[0] < size);
56 bunch[1] = size;
57 UTIL_CHECK(bunch[1] > bunch[0]);
58 bunches.append(bunch);
59 }
60
61 // Explicit instantiation definitions
62
63 template void sort<double>(std::vector< Item<double> >& );
64 template void sort<float>(std::vector< Item<float> >& );
65
66 template
67 void findBunches<double>(std::vector< Item<double> > const &,
68 GArray< Bunch >&, double);
69
70 template
71 void findBunches<float>(std::vector< Item<float> > const &,
72 GArray< Bunch >&, float);
73
74} // namespace Sort
75} // namespace Pscf
An automatically growable array, analogous to a std::vector.
Definition GArray.h:34
void clear()
Reset to empty state.
Definition GArray.h:296
void append(Data const &data)
Append an element to the end of the sequence.
Definition GArray.h:303
File containing preprocessor macros for error handling.
#define UTIL_CHECK(condition)
Assertion macro suitable for serial or parallel production code.
Definition global.h:68
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