container_algorithms.h
Go to the documentation of this file.
1 #ifndef cetlib_container_algorithms_h
2 #define cetlib_container_algorithms_h
3 
4 // ======================================================================
5 //
6 // container_algorithms: Function templates wrapping standard algorithms
7 //
8 // Avoids incompatible iterators and reduces some duplication
9 //
10 // ======================================================================
11 
12 #include <algorithm>
13 #include <iterator>
14 
15 namespace cet {
16 
17  // Wrappers for std::for_each().
18  template <class FwdCont, class Func>
19  auto for_all(FwdCont&, Func);
20  template <class FwdCont, class Func>
21  auto for_all(FwdCont const&, Func);
22 
23  // Wrappers for std::copy().
24  template <class FwdCont, class FwdIter>
25  auto copy_all(FwdCont&, FwdIter);
26  template <class FwdCont, class FwdIter>
27  auto copy_all(FwdCont const&, FwdIter);
28 
29  // Wrappers for std::copy_if().
30  template <class FwdCont, class FwdIter, class Pred>
31  auto copy_if_all(FwdCont&, FwdIter, Pred);
32  template <class FwdCont, class FwdIter, class Pred>
33  auto copy_if_all(FwdCont const&, FwdIter, Pred);
34 
35  // Wrappers for std::find().
36  template <class FwdCont, class Datum>
37  auto find_in_all(FwdCont&, Datum const&);
38  template <class FwdCont, class Datum>
39  auto find_in_all(FwdCont const&, Datum const&);
40  template <class FwdCont, class Datum>
41  bool search_all(FwdCont const&, Datum const&);
42 
43  // Wrapper for std::binary_search().
44  template <class FwdCont, class Datum>
45  bool binary_search_all(FwdCont const&, Datum const&);
46 
47  // Wrappers for std::lower_bound().
48  template <class FwdCont, class Datum>
49  auto lower_bound_all(FwdCont&, Datum const&);
50  template <class FwdCont, class Datum>
51  auto lower_bound_all(FwdCont const&, Datum const&);
52  template <class FwdCont, class Datum, class Pred>
53  auto lower_bound_all(FwdCont&, Datum const&, Pred);
54  template <class FwdCont, class Datum, class Pred>
55  auto lower_bound_all(FwdCont const&, Datum const&, Pred);
56 
57  // Wrappers for std::sort().
58  template <class RandCont>
59  void sort_all(RandCont&);
60  template <class RandCont, class Pred>
61  void sort_all(RandCont&, Pred);
62 
63  // Wrappers for std::stable_sort().
64  template <class RandCont>
65  void stable_sort_all(RandCont&);
66  template <class RandCont, class Pred>
67  void stable_sort_all(RandCont&, Pred);
68 
69  // Wrappers for std::transform().
70  // One-container input.
71  template <class Container, class OutputIt, class UnaryOp>
72  auto transform_all(Container&, OutputIt, UnaryOp);
73  template <class Container, class OutputIt, class UnaryOp>
74  auto transform_all(Container const&, OutputIt, UnaryOp);
75  // Two-container input.
76  template <class Container1, class Container2, class OutputIt, class BinaryOp>
77  auto transform_all(Container1&, Container2&, OutputIt, BinaryOp);
78  template <class Container1, class Container2, class OutputIt, class BinaryOp>
79  auto transform_all(Container1 const&, Container2 const&, OutputIt, BinaryOp);
80 
81  template <class FwdCont, class Func>
82  void for_all_with_index(FwdCont&, Func);
83  template <class FwdCont, class Func>
84  void for_all_with_index(FwdCont const&, Func);
85 }
86 
87 // for_all().
88 template <class FwdCont, class Func>
89 inline auto
90 cet::for_all(FwdCont& s, Func f)
91 {
92  using std::begin;
93  using std::end;
94  return std::for_each(begin(s), end(s), f);
95 }
96 
97 template <class FwdCont, class Func>
98 inline auto
99 cet::for_all(FwdCont const& s, Func f)
100 {
101  using std::cbegin;
102  using std::cend;
103  return std::for_each(cbegin(s), cend(s), f);
104 }
105 
106 // for_all_with_index().
107 template <class FwdCont, class Func>
108 inline void
109 cet::for_all_with_index(FwdCont& s, Func f)
110 {
111  std::size_t i{};
112  for (auto& e : s) {
113  f(i, e);
114  ++i;
115  }
116 }
117 
118 template <class FwdCont, class Func>
119 inline void
120 cet::for_all_with_index(FwdCont const& s, Func f)
121 {
122  std::size_t i{};
123  for (auto const& e : s) {
124  f(i, e);
125  ++i;
126  }
127 }
128 
129 // copy_all().
130 template <class FwdCont, class FwdIter>
131 inline auto
132 cet::copy_all(FwdCont& s, FwdIter it)
133 {
134  using std::begin;
135  using std::end;
136  return std::copy(begin(s), end(s), it);
137 }
138 
139 template <class FwdCont, class FwdIter>
140 inline auto
141 cet::copy_all(FwdCont const& s, FwdIter it)
142 {
143  using std::cbegin;
144  using std::cend;
145  return std::copy(cbegin(s), cend(s), it);
146 }
147 
148 // copy_if_all().
149 template <class FwdCont, class FwdIter, class Pred>
150 inline auto
151 cet::copy_if_all(FwdCont& s, FwdIter it, Pred p)
152 {
153  using std::cbegin;
154  using std::cend;
155  return std::copy_if(begin(s), end(s), it, p);
156 }
157 
158 template <class FwdCont, class FwdIter, class Pred>
159 inline auto
160 cet::copy_if_all(FwdCont const& s, FwdIter it, Pred p)
161 {
162  using std::cbegin;
163  using std::cend;
164  return std::copy_if(cbegin(s), cend(s), it, p);
165 }
166 
167 // Find_in_all().
168 template <class FwdCont, class Datum>
169 inline auto
170 cet::find_in_all(FwdCont& s, Datum const& d)
171 {
172  using std::begin;
173  using std::end;
174  return std::find(begin(s), end(s), d);
175 }
176 
177 template <class FwdCont, class Datum>
178 inline auto
179 cet::find_in_all(FwdCont const& s, Datum const& d)
180 {
181  using std::cbegin;
182  using std::cend;
183  return std::find(cbegin(s), cend(s), d);
184 }
185 
186 // search_all().
187 template <class FwdCont, class Datum>
188 inline bool
189 cet::search_all(FwdCont const& s, Datum const& d)
190 {
191  using std::cbegin;
192  using std::cend;
193  return std::find(cbegin(s), cend(s), d) != s.end();
194 }
195 
196 // binary_search_all().
197 template <class FwdCont, class Datum>
198 inline bool
199 cet::binary_search_all(FwdCont const& s, Datum const& d)
200 {
201  using std::cbegin;
202  using std::cend;
203  return std::binary_search(cbegin(s), cend(s), d);
204 }
205 
206 // lower_bound_all().
207 template <class FwdCont, class Datum>
208 inline auto
209 cet::lower_bound_all(FwdCont& s, Datum const& d)
210 {
211  using std::begin;
212  using std::end;
213  return std::lower_bound(begin(s), end(s), d);
214 }
215 
216 template <class FwdCont, class Datum>
217 inline auto
218 cet::lower_bound_all(FwdCont const& s, Datum const& d)
219 {
220  using std::cbegin;
221  using std::cend;
222  return std::lower_bound(cbegin(s), cend(s), d);
223 }
224 
225 template <class FwdCont, class Datum, class Pred>
226 inline auto
227 cet::lower_bound_all(FwdCont& s, Datum const& d, Pred p)
228 {
229  using std::begin;
230  using std::end;
231  return std::lower_bound(begin(s), end(s), d, p);
232 }
233 
234 template <class FwdCont, class Datum, class Pred>
235 inline auto
236 cet::lower_bound_all(FwdCont const& s, Datum const& d, Pred p)
237 {
238  using std::cbegin;
239  using std::cend;
240  return std::lower_bound(cbegin(s), cend(s), d, p);
241 }
242 
243 // sort_all().
244 template <class RandCont>
245 inline void
246 cet::sort_all(RandCont& s)
247 {
248  using std::begin;
249  using std::end;
250  std::sort(begin(s), end(s));
251 }
252 
253 template <class RandCont, class Pred>
254 inline void
255 cet::sort_all(RandCont& s, Pred p)
256 {
257  using std::begin;
258  using std::end;
259  std::sort(begin(s), end(s), p);
260 }
261 
262 // stable_sort_all().
263 template <class RandCont>
264 inline void
266 {
267  using std::begin;
268  using std::end;
269  std::stable_sort(begin(s), end(s));
270 }
271 
272 template <class RandCont, class Pred>
273 inline void
274 cet::stable_sort_all(RandCont& s, Pred p)
275 {
276  using std::begin;
277  using std::end;
278  std::stable_sort(begin(s), end(s), p);
279 }
280 
281 // transform_all().
282 template <class Container, class OutputIt, class UnaryOp>
283 inline auto
284 cet::transform_all(Container& in, OutputIt out, UnaryOp unary_op)
285 {
286  using std::begin;
287  using std::end;
288  return std::transform(begin(in), end(in), out, unary_op);
289 }
290 
291 template <class Container, class OutputIt, class UnaryOp>
292 inline auto
293 cet::transform_all(Container const& in, OutputIt out, UnaryOp unary_op)
294 {
295  using std::cbegin;
296  using std::cend;
297  return std::transform(cbegin(in), cend(in), out, unary_op);
298 }
299 
300 template <class Container1, class Container2, class OutputIt, class BinaryOp>
301 inline auto
302 cet::transform_all(Container1& in1,
303  Container2& in2,
304  OutputIt out,
305  BinaryOp binary_op)
306 {
307  using std::begin;
308  using std::end;
309  return std::transform(begin(in1), end(in1), begin(in2), out, binary_op);
310 }
311 
312 template <class Container1, class Container2, class OutputIt, class BinaryOp>
313 inline auto
314 cet::transform_all(Container1 const& in1,
315  Container2 const& in2,
316  OutputIt out,
317  BinaryOp binary_op)
318 {
319  using std::cbegin;
320  using std::cend;
321  return std::transform(cbegin(in1), cend(in1), cbegin(in2), out, binary_op);
322 }
323 
324 #endif /* cetlib_container_algorithms_h */
325 
326 // Local variables:
327 // mode: c++
328 // End:
void stable_sort_all(RandCont &)
auto lower_bound_all(FwdCont &, Datum const &)
set< int >::iterator it
auto copy_if_all(FwdCont &, FwdIter, Pred)
void for_all_with_index(FwdCont &, Func)
const char * p
Definition: xmltok.h:285
void sort_all(RandCont &)
const XML_Char * s
Definition: expat.h:262
bool search_all(FwdCont const &, Datum const &)
Float_t d
Definition: plot.C:236
auto transform_all(Container &, OutputIt, UnaryOp)
auto copy_all(FwdCont &, FwdIter)
ifstream in
Definition: comparison.C:7
bool binary_search_all(FwdCont const &, Datum const &)
auto find_in_all(FwdCont &, Datum const &)
auto for_all(FwdCont &, Func)
Float_t e
Definition: plot.C:35