CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
Map.h
Go to the documentation of this file.
1 #ifndef FWCore_Utilities_Map_h
2 #define FWCore_Utilities_Map_h
3 
4 #include <cassert>
5 #include <map>
6 
7 // Alternatives to std::map::operator[]
8 // They differ on what happens if the element is not in the map.
9 // Those that do not insert into the map will also work on a const map.
10 
11 namespace edm {
12 
13  // This silly little function documents the fact
14  // a possible insert into the map is intentional.
15  template <typename Key, typename Value>
16  inline Value& findOrInsert(std::map<Key, Value>& m, Key const& k) {
17  return m[k];
18  }
19 
20  // This function will not insert into the map.
21  // If the element is not found, it returns the supplied default value
22  // Comes in const and non-const versions
23  template <typename Key, typename Value>
24  inline Value const& findOrDefault(std::map<Key, Value> const& m, Key const& k, Value const& defaultValue) {
25  typename std::map<Key, Value>::const_iterator it = m.find(k);
26  return (it == m.end() ? defaultValue : it->second);
27  }
28 
29  template <typename Key, typename Value>
30  inline Value& findOrDefault(std::map<Key, Value>& m, Key const& k, Value& defaultValue) {
31  typename std::map<Key, Value>::const_iterator it = m.find(k);
32  return (it == m.end() ? defaultValue : it->second);
33  }
34 
35  // This function will not insert into the map.
36  // If the element is not found, it returns a default constructed value
37  // Note that the return is by value, so if the element is found, it is copied.
38  template <typename Key, typename Value>
39  inline Value findOrDefault(std::map<Key, Value> const& m, Key const& k) {
40  typename std::map<Key, Value>::const_iterator it = m.find(k);
41  return (it == m.end() ? Value() : it->second);
42  }
43 
44  // This function will not insert into the map.
45  // If the element is not found, it asserts.
46  // Comes in const and non-const versions
47  template <typename Key, typename Value>
48  inline Value const& findOrAssert(std::map<Key, Value> const& m, Key const& k) {
49  typename std::map<Key, Value>::const_iterator it = m.find(k);
50  if (it == m.end())
51  assert("findOrAssert" && 0);
52  return it->second;
53  }
54 
55  template <typename Key, typename Value>
56  inline Value& findOrAssert(std::map<Key, Value>& m, Key const& k) {
57  typename std::map<Key, Value>::const_iterator it = m.find(k);
58  if (it == m.end())
59  assert("findOrAssert" && 0);
60  return it->second;
61  }
62 } // namespace edm
63 #endif
Value const & findOrAssert(std::map< Key, Value > const &m, Key const &k)
Definition: Map.h:48
assert(be >=bs)
Value & findOrInsert(std::map< Key, Value > &m, Key const &k)
Definition: Map.h:16
reco::JetExtendedAssociation::JetExtendedData Value
Value const & findOrDefault(std::map< Key, Value > const &m, Key const &k, Value const &defaultValue)
Definition: Map.h:24