CMS 3D CMS Logo

associationMapFilterValues.h
Go to the documentation of this file.
1 #ifndef CommonTools_Utils_associationMapFilterValues_h
2 #define CommonTools_Utils_associationMapFilterValues_h
3 
8 
10 
12  // Common implementation
13  template <typename T_AssociationMap, typename T_Key,
14  typename T_ValueIndex, typename T_Value,
15  typename T_ValueIndices>
16  void findInsert(T_AssociationMap& ret, const T_Key& key,
17  const T_ValueIndex& valueIndex, const T_Value& value,
18  const T_ValueIndices& value_indices) {
19  if(value_indices.has(valueIndex)) {
20  ret.insert(key, value);
21  }
22  }
23 
24  // By default no implementation, as it turns out to be very specific for the types
25  template <typename ValueTag>
26  struct IfFound;
27 
28  // Specialize for Ref and RefToBase, implementation is the same
29  template <typename C, typename T, typename F>
30  struct IfFound<edm::Ref<C, T, F>> {
31  template <typename T_AssociationMap, typename T_KeyValue, typename T_ValueIndices>
32  static void insert(T_AssociationMap& ret, const T_KeyValue& keyValue, const T_ValueIndices& value_indices) {
33  findInsert(ret, keyValue.key, keyValue.val.key(), keyValue.val, value_indices);
34  }
35  };
36 
37  template <typename T>
38  struct IfFound<edm::RefToBase<T>> {
39  template <typename T_AssociationMap, typename T_KeyValue, typename T_ValueIndices>
40  static void insert(T_AssociationMap& ret, const T_KeyValue& keyValue, const T_ValueIndices& value_indices) {
41  findInsert(ret, keyValue.key, keyValue.val.key(), keyValue.val, value_indices);
42  }
43  };
44 
45  // Specialize for RefVector
46  template <typename C, typename T, typename F>
47  struct IfFound<edm::RefVector<C, T, F>> {
48  template <typename T_AssociationMap, typename T_KeyValue, typename T_ValueIndices>
49  static void insert(T_AssociationMap& ret, const T_KeyValue& keyValue, const T_ValueIndices& value_indices) {
50  for(const auto& value: keyValue.val) {
51  findInsert(ret, keyValue.key, value.key(), value, value_indices);
52  }
53  }
54  };
55 
56  // Specialize for vector<pair<Ref, Q>> for OneToManyWithQuality
57  template <typename C, typename T, typename F, typename Q>
58  struct IfFound<std::vector<std::pair<edm::Ref<C, T, F>, Q> > > {
59  template <typename T_AssociationMap, typename T_KeyValue, typename T_ValueIndices>
60  static void insert(T_AssociationMap& ret, const T_KeyValue& keyValue, const T_ValueIndices& value_indices) {
61  for(const auto& value: keyValue.val) {
62  findInsert(ret, keyValue.key, value.first.key(), value, value_indices);
63  }
64  }
65  };
66 
67  // Specialize for vector<pair<RefToBase, Q>> for OneToManyWithQuality
68  template <typename T, typename Q>
69  struct IfFound<std::vector<std::pair<edm::RefToBase<T>, Q> > > {
70  template <typename T_AssociationMap, typename T_KeyValue, typename T_ValueIndices>
71  static void insert(T_AssociationMap& ret, const T_KeyValue& keyValue, const T_ValueIndices& value_indices) {
72  for(const auto& value: keyValue.val) {
73  findInsert(ret, keyValue.key, value.first.key(), value, value_indices);
74  }
75  }
76  };
77 
78  // Default implementation for RefVector or vector<Ref>
79  template <typename T_RefVector>
80  struct FillIndices {
81  template <typename T_Set, typename T_RefProd>
82  static
83  void fill(T_Set& set, const T_RefVector& valueRefs, const T_RefProd& refProd) {
84  for(const auto& ref: valueRefs) {
85  edm::helpers::checkRef(refProd.val, ref);
86  set.insert(ref.key());
87  }
88  }
89  };
90 
91  // Specialize for View
92  template <typename T>
93  struct FillIndices<edm::View<T> > {
94  template <typename T_Set, typename T_RefProd>
95  static
96  void fill(T_Set& set, const edm::View<T>& valueView, const T_RefProd& refProd) {
97  for(size_t i=0; i<valueView.size(); ++i) {
98  const auto& ref = valueView.refAt(i);
99  edm::helpers::checkRef(refProd.val, ref);
100  set.insert(ref.key());
101  }
102  }
103  };
104 }
105 
126 template <typename T_AssociationMap, typename T_RefVector>
127 T_AssociationMap associationMapFilterValues(const T_AssociationMap& map, const T_RefVector& valueRefs) {
128  // If the input map is empty, just return it in order to avoid an
129  // exception from failing edm::helpers::checkRef() (in this case the
130  // refProd points to (0,0) that will fail the check).
131  if(map.empty())
132  return map;
133 
134  T_AssociationMap ret(map.refProd());
135 
136  // First copy the keys of values to a set for faster lookup of their existence in the map
137  edm::IndexSet value_indices;
138  value_indices.reserve(valueRefs.size()); // hopefully a good guess of the last ref index without having to do the gymnastics for genericity
139  associationMapFilterValuesHelpers::FillIndices<T_RefVector>::fill(value_indices, valueRefs, map.refProd());
140 
141  for(const auto& keyValue: map) {
143  }
144 
145 
146  return ret;
147 }
148 
149 #endif
150 
static void insert(T_AssociationMap &ret, const T_KeyValue &keyValue, const T_ValueIndices &value_indices)
static void insert(T_AssociationMap &ret, const T_KeyValue &keyValue, const T_ValueIndices &value_indices)
T_AssociationMap associationMapFilterValues(const T_AssociationMap &map, const T_RefVector &valueRefs)
static void fill(T_Set &set, const edm::View< T > &valueView, const T_RefProd &refProd)
Definition: value.py:1
bool insert(Storage &iStorage, ItemType *iItem, const IdTag &iIdTag)
Definition: HCMethods.h:50
static void insert(T_AssociationMap &ret, const T_KeyValue &keyValue, const T_ValueIndices &value_indices)
void findInsert(T_AssociationMap &ret, const T_Key &key, const T_ValueIndex &valueIndex, const T_Value &value, const T_ValueIndices &value_indices)
static void insert(T_AssociationMap &ret, const T_KeyValue &keyValue, const T_ValueIndices &value_indices)
HLT enums.
static void insert(T_AssociationMap &ret, const T_KeyValue &keyValue, const T_ValueIndices &value_indices)
void checkRef(const RP &rp, const R &r)
throw if r hasn&#39;t the same id as rp
void reserve(unsigned int size)
Reserve memory for the set.
Definition: IndexSet.h:34
edm::RefVector< Container > RefVector
static void fill(T_Set &set, const T_RefVector &valueRefs, const T_RefProd &refProd)