CARMA C++
stlContainerUtils.h
Go to the documentation of this file.
1 #ifndef CARMA_UTIL_STLCONTAINERUTILS_H
2 #define CARMA_UTIL_STLCONTAINERUTILS_H
3 
6 
7 #include <algorithm>
8 #include <iterator>
9 #include <map>
10 #include <set>
11 
12 
13 namespace carma {
14 namespace util {
15 
16 
22 template < typename C >
23 bool sizeIsExactlyOne( const C & );
24 
25 
31 template < typename C >
32 bool sizeIsOneOrLess( const C & );
33 
34 
35 template < typename K, typename V >
36 ::std::multimap< V, K > invertToMultimap( const ::std::map< K, V > & in );
37 
38 
39 template < typename K, typename V >
40 ::std::multimap< V, K > invertToMultimap( const ::std::multimap< K, V > & in );
41 
42 
45 template < typename K, typename V >
46 ::std::set< K > keys( const ::std::map< K, V > & in );
47 
50 template < typename K, typename V >
51 ::std::set< K > keys( const ::std::multimap< K, V > & in );
52 
54 //@see carma::util::keys()
55 struct RetrieveKey {
56  template< typename T >
57  typename T::first_type operator()(T keyValuePair ) const {
58  return keyValuePair.first;
59  }
60 };
61 } // namespace carma::util
62 } // namespace carma
63 
64 
66 
67 template < typename C >
68 bool
70 {
71  if ( c.empty() ) // constant time
72  return false;
73 
74  typename C::const_iterator i = c.begin(); // constant time
75 
76  ++i; // constant time
77 
78  return (i == c.end()); // constant time
79 }
80 
81 
82 template < typename C >
83 bool
85 {
86  if ( c.empty() ) // constant time
87  return true;
88 
89  typename C::const_iterator i = c.begin(); // constant time
90 
91  ++i; // constant time
92 
93  return (i == c.end()); // constant time
94 }
95 
96 
97 template < typename K, typename V >
98 ::std::multimap< V, K >
99 carma::util::invertToMultimap( const ::std::map< K, V > & in )
100 {
101  ::std::multimap< V, K > out;
102 
103  typename ::std::map< K, V >::const_iterator i = in.begin();
104  const typename ::std::map< K, V >::const_iterator iEnd = in.end();
105 
106  for ( ; i != iEnd; ++i )
107  out.insert( ::std::make_pair( i->second, i->first ) );
108 
109  return out;
110 }
111 
112 
113 template < typename K, typename V >
114 ::std::multimap< V, K >
115 carma::util::invertToMultimap( const ::std::multimap< K, V > & in )
116 {
117  ::std::multimap< V, K > out;
118 
119  typename ::std::multimap< K, V >::const_iterator i = in.begin();
120  const typename ::std::multimap< K, V >::const_iterator iEnd = in.end();
121 
122  for ( ; i != iEnd; ++i )
123  out.insert( ::std::make_pair( i->second, i->first ) );
124 
125  return out;
126 }
127 
128 template < typename K, typename V >
129 ::std::set< K > carma::util::keys( const ::std::map< K, V > & in )
130 {
131  ::std::set< K > out;
132  transform( in.begin(), in.end(),
133  inserter( out, out.begin() ), RetrieveKey()
134  );
135  return out;
136 }
137 
138 template < typename K, typename V >
139 ::std::set< K > carma::util::keys( const ::std::multimap< K, V > & in )
140 {
141  ::std::set< K > out;
142  // this is somewhat inefficient since identical keys are multiply
143  // retrieved.
144  transform( in.begin(), in.end(),
145  inserter( out, out.begin() ), RetrieveKey()
146  );
147  return out;
148 }
149 
150 
151 #endif
bool sizeIsOneOrLess(const C &)
Constant time check that a container has no more than one element.
bool sizeIsExactlyOne(const C &)
Constant time check that a container has exactly one element.
Struct which operators on a container key-value pair to retrieve the key.
::std::set< K > keys(const ::std::map< K, V > &in)
Return the keys of a map as a set.