Geant4 Cross Reference |
1 // Copyright (C) 2010, Guy Barrand. All rights 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 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 26 typedef typename std::map<K,V*>::iterator it 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(s 42 #endif 43 44 template <class K,class V> 45 inline bool delete_key(std::map<K,V*>& a_m,con 46 typedef typename std::map<K,V*>::iterator it 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 58 for(it_t it=a_m.begin();it!=a_m.end();++it) 59 a_m.clear(); 60 } 61 62 template <class K,class V> 63 inline void copy(std::map<K,V*>& a_to,const st 64 raw_clear<K,V>(a_to); 65 typedef typename std::map<K,V*>::const_itera 66 for(it_t it=a_from.begin();it!=a_from.end(); 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,c 73 typedef typename std::map<K,V*>::iterator it 74 it_t it = a_map.find(a_key); 75 if(it==a_map.end()) {a_map[a_key] = a_value; 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,co 87 typedef typename std::map<K,V>::const_iterat 88 it_t it = a_map.find(a_key); 89 if(it==a_map.end()) {a_value = V();return fa 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, 96 typedef typename std::map<K,V>::const_iterat 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