Muster
 All Classes Namespaces Files Functions Variables Typedefs Macros
stl_utils.h
Go to the documentation of this file.
1 #ifndef STL_UTILS
2 #define STL_UTILS
3 
4 #include <utility>
5 #include <vector>
6 #include <iostream>
7 
8 // ====================================================================================
9 // stl_utils.h
10 // This file contains some utility functions for dealing with the STL.
11 // ====================================================================================
12 
13 
14 /// Functor to get the first element of a pair. Use with STL functions like transform().
15 struct get_first {
16  template <typename P>
17  typename P::first_type operator()(const P& pair) {
18  return pair.first;
19  }
20 };
21 
22 /// Functor to get the second element of a pair. Use with STL functions like transform().
23 struct get_second {
24  template <typename P>
25  typename P::second_type operator()(const P& pair) {
26  return pair.second;
27  }
28 };
29 
30 
31 template <typename Indexable>
33  const Indexable& container;
34  indexed_lt_functor(const Indexable& c) : container(c) { }
35  template <typename I>
36  bool operator()(const I& lhs, const I& rhs) {
37  return container[lhs] < container[rhs];
38  }
39 };
40 
41 template <typename Indexable>
42 indexed_lt_functor<Indexable> indexed_lt(const Indexable& container) {
43  return indexed_lt_functor<Indexable>(container);
44 }
45 
46 
47 template <typename Index>
48 void invert(std::vector<Index>& vec) {
49  std::vector<Index> inverse(vec.size());
50  for (size_t i=0; i < vec.size(); i++) {
51  inverse[vec[i]] = i;
52  }
53  inverse.swap(vec);
54 }
55 
56 
57 ///
58 /// Generator object for a strided sequence of ints.
59 ///
60 struct sequence {
61  int value, stride;
62 
63  sequence(int _start=0, int _stride=1)
64  : value(_start), stride(_stride) { }
65 
66  int operator()() {
67  int result = value;
68  value += stride;
69  return result;
70  }
71 };
72 
73 
74 
75 
76 #endif // STL_UTILS
Generator object for a strided sequence of ints.
Definition: stl_utils.h:60
bool operator()(const I &lhs, const I &rhs)
Definition: stl_utils.h:36
int value
Definition: stl_utils.h:61
Functor to get the first element of a pair. Use with STL functions like transform().
Definition: stl_utils.h:15
int operator()()
Definition: stl_utils.h:66
const Indexable & container
Definition: stl_utils.h:33
int stride
Definition: stl_utils.h:61
indexed_lt_functor< Indexable > indexed_lt(const Indexable &container)
Definition: stl_utils.h:42
P::first_type operator()(const P &pair)
Definition: stl_utils.h:17
P::second_type operator()(const P &pair)
Definition: stl_utils.h:25
void invert(std::vector< Index > &vec)
Definition: stl_utils.h:48
Functor to get the second element of a pair. Use with STL functions like transform().
Definition: stl_utils.h:23
indexed_lt_functor(const Indexable &c)
Definition: stl_utils.h:34
sequence(int _start=0, int _stride=1)
Definition: stl_utils.h:63
Muster. Copyright © 2010, Lawrence Livermore National Laboratory, LLNL-CODE-433662.
Distribution of Muster and its documentation is subject to terms of the Muster LICENSE.
Generated on Thu Sep 1 2016 using Doxygen 1.8.5