www.mooseframework.org
IndirectSort.h
Go to the documentation of this file.
1 //* This file is part of the MOOSE framework
2 //* https://www.mooseframework.org
3 //*
4 //* All rights reserved, see COPYRIGHT for full restrictions
5 //* https://github.com/idaholab/moose/blob/master/COPYRIGHT
6 //*
7 //* Licensed under LGPL 2.1, please see LICENSE for details
8 //* https://www.gnu.org/licenses/lgpl-2.1.html
9 
10 #pragma once
11 
12 #include <functional>
13 #include <vector>
14 #include <algorithm>
15 #include <iterator> // std::iterator_traits
16 
17 namespace Moose
18 {
19 
20 // The indirect (or index) comparison functor is templated on a random
21 // access iterator type, and a user comparison function. This class
22 // is to be constructed with a random access iterator which points to
23 // the beginning of the container whose values are to be indirectly
24 // sorted. This class is not to be used directly by the user.
25 template <class RandomAccessIterator, class UserComparisonFunctor>
27 {
28  // ctor
29  indirect_comparator(RandomAccessIterator r, UserComparisonFunctor c)
31  {
32  }
33 
34  // comparison operator - calls the user's comparison function on
35  // v[lhs] and v[rhs]
36  bool operator()(size_t lhs, size_t rhs)
37  {
38  // Note: operator[] is defined for random access iterators!
40  }
41 
42 private:
43  // data
44  RandomAccessIterator _random_access_iterator;
45  UserComparisonFunctor _user_comp;
46 };
47 
48 // This is a common initialization function called by the indirect_sort's.
49 // Should not be called directly by users...
50 template <class RandomAccessIterator>
51 void
52 initialize_indirect_sort(RandomAccessIterator beg,
53  RandomAccessIterator end,
54  std::vector<size_t> & b)
55 {
56  // enough storage for all the indices
57  b.resize(std::distance(beg, end));
58 
59  // iota
60  for (size_t i = 0; i < b.size(); ++i)
61  b[i] = i;
62 }
63 
64 // A generic indirect sort function templated on the iterator type. Uses
65 // std::less<T> for the comparisons.
66 template <class RandomAccessIterator>
67 void
68 indirectSort(RandomAccessIterator beg, RandomAccessIterator end, std::vector<size_t> & b)
69 {
70  // Space in b
71  initialize_indirect_sort(beg, end, b);
72 
73  // Typedef for less typing. Note: use of std::iterator_traits means this should work with
74  // naked pointers too...
75  typedef std::less<typename std::iterator_traits<RandomAccessIterator>::value_type>
76  LessThanComparator;
77 
78  // Construct comparator object
80 
81  // Sort the indices, based on the data
82  std::sort(b.begin(), b.end(), ic);
83 }
84 
85 // A generic indirect sort function templated on the iterator type *and* the comparison functor
86 // to be used for the ordering.
87 template <class RandomAccessIterator, class UserComparisonFunctor>
88 void
89 indirectSort(RandomAccessIterator beg,
90  RandomAccessIterator end,
91  std::vector<size_t> & b,
92  UserComparisonFunctor user_comp)
93 {
94  // Space in b
95  initialize_indirect_sort(beg, end, b);
96 
97  // Construct comparator object
99 
100  // Sort the indices, based on the data
101  std::sort(b.begin(), b.end(), ic);
102 }
103 
106 template <typename T>
107 void
108 applyIndices(T & container, const std::vector<size_t> & indices)
109 {
110  T tmp;
111  tmp.resize(container.size());
112  for (size_t i = 0; i < indices.size(); i++)
113  tmp[i] = container[indices[i]];
114  std::swap(tmp, container);
115 }
116 
117 } // namespace Moose
void swap(std::vector< T > &data, const std::size_t idx0, const std::size_t idx1, const libMesh::Parallel::Communicator &comm)
Swap function for serial or distributed vector of data.
Definition: Shuffle.h:494
RandomAccessIterator _random_access_iterator
Definition: IndirectSort.h:44
void indirectSort(RandomAccessIterator beg, RandomAccessIterator end, std::vector< size_t > &b)
Definition: IndirectSort.h:68
bool operator()(size_t lhs, size_t rhs)
Definition: IndirectSort.h:36
void initialize_indirect_sort(RandomAccessIterator beg, RandomAccessIterator end, std::vector< size_t > &b)
Definition: IndirectSort.h:52
void applyIndices(T &container, const std::vector< size_t > &indices)
Uses indices created by the indirectSort function to sort the given container (which must support ran...
Definition: IndirectSort.h:108
indirect_comparator(RandomAccessIterator r, UserComparisonFunctor c)
Definition: IndirectSort.h:29
UserComparisonFunctor _user_comp
Definition: IndirectSort.h:45
MOOSE now contains C++17 code, so give a reasonable error message stating what the user can do to add...