g4tools  5.4.0
mapmanip
Go to the documentation of this file.
1 // Copyright (C) 2010, Guy Barrand. All rights reserved.
2 // See the file tools.license for terms.
3 
4 #ifndef tools_mapmanip
5 #define tools_mapmanip
6 
7 #include <map>
8 
9 namespace tools {
10 
11 template <class K,class V>
12 inline void safe_clear(std::map<K,V*>& a_m){
13  // the below takes into account the case in
14  // which "delete entry" could modify a_m.
15  typedef typename std::map<K,V*>::iterator it_t;
16  while(!a_m.empty()) {
17  it_t it = a_m.begin();
18  V* entry = (*it).second;
19  a_m.erase(it);
20  delete entry;
21  }
22 }
23 
24 template <class K,class V>
25 inline void find_and_remove_value(std::map<K,V*>& a_m,V* a_value){
26  typedef typename std::map<K,V*>::iterator it_t;
27  while(true) {
28  bool found = false;
29  for(it_t it=a_m.begin();it!=a_m.end();++it) {
30  if((*it).second==a_value) {
31  a_m.erase(it);
32  found = true;
33  break;
34  }
35  }
36  if(!found) break;
37  }
38 }
39 
40 #ifdef TOOLS_DEPRECATED
41 template <class K,class V> inline void clear(std::map<K,V*>& a_m){safe_clear<K,V>(a_m);}
42 #endif
43 
44 template <class K,class V>
45 inline bool delete_key(std::map<K,V*>& a_m,const K& a_key){
46  typedef typename std::map<K,V*>::iterator it_t;
47  it_t it = a_m.find(a_key);
48  if(it==a_m.end()) return false;
49  V* obj = (*it).second;
50  a_m.erase(it);
51  delete obj;
52  return true;
53 }
54 
55 template <class K,class V>
56 inline void raw_clear(std::map<K,V*>& a_m){
57  typedef typename std::map<K,V*>::iterator it_t;
58  for(it_t it=a_m.begin();it!=a_m.end();++it) delete (*it).second;
59  a_m.clear();
60 }
61 
62 template <class K,class V>
63 inline void copy(std::map<K,V*>& a_to,const std::map<K,V*>& a_from){
64  raw_clear<K,V>(a_to);
65  typedef typename std::map<K,V*>::const_iterator it_t;
66  for(it_t it=a_from.begin();it!=a_from.end();++it) {
67  a_to[(*it).first] = (*it).second->copy();
68  }
69 }
70 
71 template <class K,class V>
72 inline bool add_unique(std::map<K,V*>& a_map,const K& a_key,V* a_value,bool a_delete) {
73  typedef typename std::map<K,V*>::iterator it_t;
74  it_t it = a_map.find(a_key);
75  if(it==a_map.end()) {a_map[a_key] = a_value;return false;} //false=was not found.
76  if(a_delete) {
77  V* entry = (*it).second;
78  a_map.erase(it);
79  delete entry;
80  }
81  a_map[a_key] = a_value;
82  return true;
83 }
84 
85 template <class K,class V>
86 inline bool find(const std::map<K,V>& a_map,const K& a_key,V& a_value) {
87  typedef typename std::map<K,V>::const_iterator it_t;
88  it_t it = a_map.find(a_key);
89  if(it==a_map.end()) {a_value = V();return false;}
90  a_value = (*it).second;
91  return true;
92 }
93 
94 template <class K,class V>
95 inline bool is_key(const std::map<K,V>& a_map,const K& a_key) {
96  typedef typename std::map<K,V>::const_iterator it_t;
97  it_t it = a_map.find(a_key);
98  if(it==a_map.end()) return false;
99  return true;
100 }
101 
102 }
103 
104 #endif
tools::find
bool find(const std::map< K, V > &a_map, const K &a_key, V &a_value)
Definition: mapmanip:86
tools::is_key
bool is_key(const std::map< K, V > &a_map, const K &a_key)
Definition: mapmanip:95
tools::delete_key
bool delete_key(std::map< K, V * > &a_m, const K &a_key)
Definition: mapmanip:45
tools::raw_clear
void raw_clear(std::map< K, V * > &a_m)
Definition: mapmanip:56
tools::copy
bool copy(T *&a_v, I a_n, const T *a_from)
Definition: carray:30
tools::safe_clear
void safe_clear(std::map< K, V * > &a_m)
Definition: mapmanip:12
tools
inlined C code : ///////////////////////////////////
Definition: aida_ntuple:26
tools::file::found
bool found(const std::string &a_file, const std::string &a_what, std::vector< std::string > &a_found)
Definition: file:507
tools::find_and_remove_value
void find_and_remove_value(std::map< K, V * > &a_m, V *a_value)
Definition: mapmanip:25
tools::add_unique
bool add_unique(std::map< K, V * > &a_map, const K &a_key, V *a_value, bool a_delete)
Definition: mapmanip:72