CMS 3D CMS Logo

DDMapper.h
Go to the documentation of this file.
1 #ifndef DD_DDMapper
2 #define DD_DDMapper
3 
4 #include <map>
5 #include <string>
6 #include <vector>
7 
8 #include <iostream>
9 
11 
12 template <class KeyType, class ValueType>
13 class DDMapper {
14 public:
16  typedef std::pair<KeyType, ValueType> Pair;
17 
19  typedef std::vector<Pair> Vector;
20 
22 
23  void insert(const KeyType &, const ValueType &);
24 
26 
27  //void remove(const KeyType &);
28 
30 
31  //void remove(const ValueType &);
32 
34 
35  bool value(const KeyType &key, ValueType &result);
36 
38  //bool keys(const ValueType & value, std::vector<KeyType> & result);
39 
41  unsigned int noSpecifics(const KeyType &key, const std::string &name) const;
42 
44 
49  unsigned int toDouble(const std::string &name, const KeyType &key, double &value, unsigned int pos = 0) const;
50 
52  unsigned int toString(const std::string &name, const KeyType &key, std::string &value, unsigned int pos = 0) const;
53 
54  unsigned int toDouble(const std::string &name, const ValueType &key, double &value, unsigned int pos = 0) const;
55 
57  unsigned int toString(const std::string &name, const ValueType &key, std::string &value, unsigned int pos = 0) const;
58 
60  Vector all(const std::string &name, const std::string &value) const;
61 
63  Vector all(const std::string &name, const double &value) const;
64 
66  Vector all(const std::string &name) const;
67 
68 private:
69  std::map<KeyType, ValueType> keyToValue_;
70  std::multimap<ValueType, KeyType> valueToKey_;
71 };
72 
73 template <class K, class V>
74 void DDMapper<K, V>::insert(const K &key, const V &value) {
76  valueToKey_.insert(std::make_pair(value, key));
77  // valueToKey_[value] = key;
78 }
79 
80 template <class K, class V>
81 bool DDMapper<K, V>::value(const K &key, V &value) {
82  bool result = false;
83  typename std::map<K, V>::const_iterator it = keyToValue_.find(key);
84  if (it != keyToValue_.end()) {
85  value = it->second;
86  result = true;
87  }
88  return result;
89 }
90 
91 template <class K, class V>
92 unsigned int DDMapper<K, V>::noSpecifics(const K &key, const std::string &name) const {
93  typedef std::vector<const DDsvalues_type *> sv_type;
94  unsigned int result = 0;
95  typename std::map<K, V>::const_iterator it = keyToValue_.find(key);
96  if (it != keyToValue_.end()) {
97  sv_type sv = it->second.specifics();
98  sv_type::const_iterator it = sv.begin();
99  DDValue v(name);
100  for (; it != sv.end(); ++it) {
101  if (DDfetch(*it, v)) {
102  result += v.size();
103  }
104  }
105  }
106  return result;
107 }
108 
109 // to be fast, we will only return the first specific found matching the given name
110 template <class K, class V>
111 unsigned int DDMapper<K, V>::toDouble(const std::string &name, const K &key, double &value, unsigned int pos) const {
112  typedef std::vector<const DDsvalues_type *> sv_type;
113  unsigned int result = 0;
114  typename std::map<K, V>::const_iterator it = keyToValue_.find(key);
115  if (it != keyToValue_.end()) {
116  sv_type sv = it->second.specifics();
117  sv_type::const_iterator svIt = sv.begin();
118  sv_type::const_iterator svEd = sv.end();
119  DDValue v(name);
120  for (; svIt != svEd; ++svIt) {
121  if (DDfetch(*svIt, v)) {
122  result = v.size();
123  value = v.doubles()[pos];
124  break;
125  }
126  }
127  }
128  return result;
129 }
130 
131 template <class K, class V>
132 unsigned int DDMapper<K, V>::toDouble(const std::string &name, const V &val, double &value, unsigned int pos) const {
133  typedef std::vector<const DDsvalues_type *> sv_type;
134  unsigned int result = 0;
135  sv_type sv = val.specifics();
136  sv_type::const_iterator svIt = sv.begin();
137  sv_type::const_iterator svEd = sv.end();
138  DDValue v(name);
139  for (; svIt != svEd; ++svIt) {
140  if (DDfetch(*svIt, v)) {
141  result = v.size();
142  value = v.doubles()[pos];
143  break;
144  }
145  }
146  return result;
147 }
148 
149 template <class K, class V>
151  const V &val,
153  unsigned int pos) const {
154  typedef std::vector<const DDsvalues_type *> sv_type;
155  unsigned int result = 0;
156  sv_type sv = val.specifics();
157  sv_type::const_iterator svIt = sv.begin();
158  sv_type::const_iterator svEd = sv.end();
159  DDValue v(name);
160  for (; svIt != svEd; ++svIt) {
161  if (DDfetch(*svIt, v)) {
162  result = v.size();
163  value = v.strings()[pos];
164  break;
165  }
166  }
167  return result;
168 }
169 
170 // to be fast, we will only return the first specific found matcing the given name
171 template <class K, class V>
173  const K &key,
175  unsigned int pos) const {
176  typedef std::vector<const DDsvalues_type *> sv_type;
177  unsigned int result = 0;
178  typename std::map<K, V>::const_iterator it = keyToValue_.find(key);
179  if (it != keyToValue_.end()) {
180  sv_type sv = it->second.specifics();
181  sv_type::const_iterator svIt = sv.begin();
182  sv_type::const_iterator svEd = sv.end();
183  DDValue v(name);
184  //std::cout << "DDValue=" << name << std::endl;
185  for (; svIt != svEd; ++svIt) {
186  //std::cout << "looping..." << **svIt << std::endl;
187  if (DDfetch(*svIt, v)) {
188  result = v.size();
189  //std::cout << "found!" << std::endl;
190  value = v.strings()[pos];
191  break;
192  }
193  }
194  }
195  return result;
196 }
197 
198 template <class K, class V>
199 std::vector<std::pair<K, V> > DDMapper<K, V>::all(const std::string &name, const std::string &value) const {
200  std::vector<std::pair<K, V> > result;
201  typedef std::vector<const DDsvalues_type *> sv_type;
202  typename std::map<V, K>::const_iterator it = valueToKey_.begin();
203  typename std::map<V, K>::const_iterator ed = valueToKey_.end();
204 
205  // loop over all registered ValueTypes
206  for (; it != ed; ++it) {
207  sv_type sv = it->first.specifics();
208  //std::cout << "now at: " << it->first.name() << std::endl;
209  sv_type::const_iterator svIt = sv.begin();
210  sv_type::const_iterator svEd = sv.end();
211  DDValue v(name);
212  for (; svIt != svEd; ++svIt) {
213  if (DDfetch(*svIt, v)) {
214  //std::cout << "found: ";
215  const std::vector<std::string> &s = v.strings();
216  if (!s.empty()) {
217  //std::cout << s[0];
218  if (s[0] == value) {
219  result.emplace_back(std::make_pair(it->second, it->first));
220  break;
221  }
222  }
223  //std::cout << std::endl;
224  }
225  }
226  }
227  return result;
228 }
229 
230 template <class K, class V>
231 std::vector<std::pair<K, V> > DDMapper<K, V>::all(const std::string &name, const double &value) const {
232  std::vector<std::pair<K, V> > result;
233  typedef std::vector<const DDsvalues_type *> sv_type;
234  typename std::map<V, K>::const_iterator it = valueToKey_.begin();
235  typename std::map<V, K>::const_iterator ed = valueToKey_.end();
236 
237  // loop over all registered ValueTypes
238  for (; it != ed; ++it) {
239  sv_type sv = it->first.specifics();
240  //std::cout << "now at: " << it->first.name() << std::endl;
241  sv_type::const_iterator svIt = sv.begin();
242  sv_type::const_iterator svEd = sv.end();
243  DDValue v(name);
244  for (; svIt != svEd; ++svIt) {
245  if (DDfetch(*svIt, v)) {
246  //std::cout << "found: ";
247  const std::vector<double> &s = v.doubles();
248  if (!s.empty()) {
249  //std::cout << s[0];
250  if (s[0] == value) {
251  result.emplace_back(std::make_pair(it->second, it->first));
252  break;
253  }
254  }
255  //std::cout << std::endl;
256  }
257  }
258  }
259  return result;
260 }
261 
262 template <class K, class V>
263 std::vector<std::pair<K, V> > DDMapper<K, V>::all(const std::string &name) const {
264  std::vector<std::pair<K, V> > result;
265  typedef std::vector<const DDsvalues_type *> sv_type;
266  typename std::map<V, K>::const_iterator it = valueToKey_.begin();
267  typename std::map<V, K>::const_iterator ed = valueToKey_.end();
268 
269  // loop over all registered ValueTypes
270  for (; it != ed; ++it) {
271  sv_type sv = it->first.specifics();
272  //std::cout << "now at: " << it->first.name() << std::endl;
273  sv_type::const_iterator svIt = sv.begin();
274  sv_type::const_iterator svEd = sv.end();
275  DDValue v(name);
276  for (; svIt != svEd; ++svIt) {
277  if (DDfetch(*svIt, v)) {
278  result.emplace_back(std::make_pair(it->second, it->first));
279  break;
280  }
281  }
282  }
283  return result;
284 }
285 
286 #endif
unsigned int noSpecifics(const KeyType &key, const std::string &name) const
fetch a key given a value
Definition: DDMapper.h:92
std::pair< KeyType, ValueType > Pair
usefull typedef
Definition: DDMapper.h:16
const std::vector< double > & doubles() const
a reference to the double-valued values stored in the given instance of DDValue
Definition: DDValue.cc:111
bool value(const KeyType &key, ValueType &result)
removes a key-value pair
Definition: DDMapper.h:81
unsigned int toDouble(const std::string &name, const KeyType &key, double &value, unsigned int pos=0) const
returns the number specific parameters named &#39;name&#39; and the corrsponding double
Definition: DDMapper.h:111
bool DDfetch(const DDsvalues_type *, DDValue &)
helper for retrieving DDValues from DDsvalues_type *.
Definition: DDsvalues.cc:79
std::map< KeyType, ValueType > keyToValue_
Definition: DDMapper.h:69
ValueType
Type of the value held by a Value object.
Definition: value.h:23
Definition: value.py:1
std::vector< Pair > Vector
usefull typedef
Definition: DDMapper.h:19
const std::vector< std::string > & strings() const
a reference to the std::string-valued values stored in the given instance of DDValue ...
Definition: DDValue.h:60
unsigned int toString(const std::string &name, const KeyType &key, std::string &value, unsigned int pos=0) const
same as toDouble but for std::string-valued values of named parameters
Definition: DDMapper.h:172
unsigned int size() const
the size of the stored value-pairs (std::string,double)
Definition: DDValue.h:67
void insert(const KeyType &, const ValueType &)
insert a new key-value-pair
Definition: DDMapper.h:74
Vector all(const std::string &name, const std::string &value) const
get all std::mapped instances which have a specific &#39;name&#39; with value &#39;value&#39;
Definition: DDMapper.h:199
std::multimap< ValueType, KeyType > valueToKey_
Definition: DDMapper.h:70