CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros 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
17  Value&
18  findOrInsert(std::map<Key, Value>& m, Key const& k) {
19  return m[k];
20  }
21 
22  // This function will not insert into the map.
23  // If the element is not found, it returns the supplied default value
24  // Comes in const and non-const versions
25  template <typename Key, typename Value>
26  inline
27  Value const&
28  findOrDefault(std::map<Key, Value> const& m, Key const& k, Value const& defaultValue) {
29  typename std::map<Key, Value>::const_iterator it = m.find(k);
30  return (it == m.end() ? defaultValue : it->second);
31  }
32 
33  template <typename Key, typename Value>
34  inline
35  Value&
36  findOrDefault(std::map<Key, Value>& m, Key const& k, Value& defaultValue) {
37  typename std::map<Key, Value>::const_iterator it = m.find(k);
38  return (it == m.end() ? defaultValue : it->second);
39  }
40 
41  // This function will not insert into the map.
42  // If the element is not found, it returns a default constructed value
43  // Note that the return is by value, so if the element is found, it is copied.
44  template <typename Key, typename Value>
45  inline
46  Value
47  findOrDefault(std::map<Key, Value> const& m, Key const& k) {
48  typename std::map<Key, Value>::const_iterator it = m.find(k);
49  return (it == m.end() ? Value() : it->second);
50  }
51 
52  // This function will not insert into the map.
53  // If the element is not found, it asserts.
54  // Comes in const and non-const versions
55  template <typename Key, typename Value>
56  inline
57  Value const&
58  findOrAssert(std::map<Key, Value> const& m, Key const& k) {
59  typename std::map<Key, Value>::const_iterator it = m.find(k);
60  if (it == m.end()) assert("findOrAssert" && 0);
61  return it->second;
62  }
63 
64  template <typename Key, typename Value>
65  inline
66  Value&
67  findOrAssert(std::map<Key, Value>& m, Key const& k) {
68  typename std::map<Key, Value>::const_iterator it = m.find(k);
69  if (it == m.end()) assert("findOrAssert" && 0);
70  return it->second;
71  }
72 }
73 #endif
Value const & findOrAssert(std::map< Key, Value > const &m, Key const &k)
Definition: Map.h:58
assert(m_qm.get())
Value & findOrInsert(std::map< Key, Value > &m, Key const &k)
Definition: Map.h:18
reco::JetExtendedAssociation::JetExtendedData Value
Value const & findOrDefault(std::map< Key, Value > const &m, Key const &k, Value const &defaultValue)
Definition: Map.h:28