LArSoft  v06_85_00
Liquid Argon Software toolkit - http://larsoft.org/
SortByPointers.h
Go to the documentation of this file.
1 
10 #ifndef LARCOREALG_COREUTILS_SORTBYPOINTER_H
11 #define LARCOREALG_COREUTILS_SORTBYPOINTER_H
12 
13 // C/C++ standard libraries
14 #include <vector>
15 #include <algorithm> // std::transform(), std::sort()
16 #include <memory> // std::addressof()
17 #include <iterator> // std::back_inserter()
18 #include <utility> // std::move()
19 #include <type_traits> // std::add_pointer_t
20 
21 
22 namespace util {
23 
24  namespace details {
25 
26  template <typename Coll, typename PtrColl>
28 
29  } // namespace details
30 
31 
32  //----------------------------------------------------------------------------
39  template <typename Coll>
40  auto makePointerVector(Coll& coll);
41 
42 
43  //----------------------------------------------------------------------------
56  template <typename Coll, typename PtrColl>
57  void MoveFromPointers(Coll& dest, PtrColl& src)
59 
60 
61  //----------------------------------------------------------------------------
90  template <typename Coll, typename Sorter>
91  void SortByPointers(Coll& coll, Sorter sorter);
92 
93 
94  //----------------------------------------------------------------------------
95 
96 } // namespace util
97 
98 
99 //------------------------------------------------------------------------------
100 //--- Template implementation
101 //------------------------------------------------------------------------------
102 template <typename Coll>
103 auto util::makePointerVector(Coll& coll) {
104 
105  using coll_t = Coll;
106  using value_type = typename coll_t::value_type;
107  using pointer_type = std::add_pointer_t<value_type>;
108  using ptr_coll_t = std::vector<pointer_type>;
109 
110  auto const n = coll.size();
111 
112  //
113  // create the collection of pointers to data
114  //
115  ptr_coll_t ptrs;
116  ptrs.reserve(n);
117  std::transform(coll.begin(), coll.end(), std::back_inserter(ptrs),
118  [](auto& obj){ return &obj; });
119 
120  return ptrs;
121 
122 } // util::makePointerVector()
123 
124 
125 //------------------------------------------------------------------------------
126 template <typename Coll, typename Sorter>
127 void util::SortByPointers(Coll& coll, Sorter sorter) {
128 
129  using coll_t = Coll;
130 
131  //
132  // create the collection of pointers to data
133  //
134  auto ptrs = makePointerVector(coll);
135 
136  //
137  // delegate the sorting by pointers
138  //
139  sorter(ptrs);
140 
141  //
142  // create a sorted collection moving the content from the original one
143  //
144  coll_t sorted;
145  MoveFromPointers(sorted, ptrs);
146 
147  //
148  // replace the old container with the new one
149  //
150  coll = std::move(sorted);
151 
152 } // util::SortByPointers()
153 
154 
155 //------------------------------------------------------------------------------
156 namespace util {
157  namespace details {
158 
159  template <typename Coll, typename PtrColl>
160  void moveFromPointersImplBase(Coll& dest, PtrColl& src)
161  { for (auto&& ptr: src) dest.push_back(std::move(*ptr)); }
162 
163 
164  template <typename Coll, typename PtrColl>
165  struct MoveFromPointersImpl {
166  static void move(Coll& dest, PtrColl& src)
167  {
168  dest.clear();
169  moveFromPointersImplBase(dest, src);
170  }
171  }; // struct MoveFromPointersImpl
172 
173 
174  template <typename Data, typename PtrColl>
175  struct MoveFromPointersImpl<std::vector<Data>, PtrColl> {
176  static void move(std::vector<Data>& dest, PtrColl& src)
177  {
178  dest.clear();
179  dest.reserve(src.size());
180  moveFromPointersImplBase(dest, src);
181  }
182  }; // struct MoveFromPointersImpl
183 
184  } // namespace details
185 } // namespace util
186 
187 
188 //------------------------------------------------------------------------------
189 
190 #endif // LARCOREALG_COREUTILS_SORTBYPOINTER_H
static void move(std::vector< Data > &dest, PtrColl &src)
static void move(Coll &dest, PtrColl &src)
Namespace for general, non-LArSoft-specific utilities.
Definition: PIDAAlg.h:17
auto makePointerVector(Coll &coll)
Creates a STL vector with pointers to data from another collection.
STL namespace.
auto vector(Vector const &v)
Returns a manipulator which will print the specified array.
Definition: DumpUtils.h:265
void SortByPointers(Coll &coll, Sorter sorter)
Applies sorting indirectly, minimizing data copy.
void moveFromPointersImplBase(Coll &dest, PtrColl &src)
void MoveFromPointers(Coll &dest, PtrColl &src)
Moves the content from a collection of pointers to one of data.
Char_t n[5]