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