Back to home page

EIC code displayed by LXR

 
 

    


Warning, /include/Geant4/tools/mapmanip is written in an unsupported language. File is not indexed.

0001 // Copyright (C) 2010, Guy Barrand. All rights reserved.
0002 // See the file tools.license for terms.
0003 
0004 #ifndef tools_mapmanip
0005 #define tools_mapmanip
0006 
0007 #include <map>
0008 
0009 namespace tools {
0010 
0011 template <class K,class V>
0012 inline void safe_clear(std::map<K,V*>& a_m){
0013   // the below takes into account the case in
0014   // which "delete entry" could modify a_m.
0015   typedef typename std::map<K,V*>::iterator it_t;
0016   while(!a_m.empty()) {
0017     it_t it = a_m.begin();
0018     V* entry  = (*it).second;
0019     a_m.erase(it);
0020     delete entry;
0021   }
0022 }
0023 
0024 template <class K,class V>
0025 inline void find_and_remove_value(std::map<K,V*>& a_m,V* a_value){
0026   typedef typename std::map<K,V*>::iterator it_t;
0027   while(true) {
0028     bool found = false;
0029     for(it_t it=a_m.begin();it!=a_m.end();++it) {
0030       if((*it).second==a_value) {
0031         a_m.erase(it);
0032         found = true;
0033         break;
0034       }
0035     }
0036     if(!found) break;
0037   }
0038 }
0039 
0040 #ifdef TOOLS_DEPRECATED
0041 template <class K,class V> inline void clear(std::map<K,V*>& a_m){safe_clear<K,V>(a_m);}
0042 #endif
0043 
0044 template <class K,class V>
0045 inline bool delete_key(std::map<K,V*>& a_m,const K& a_key){
0046   typedef typename std::map<K,V*>::iterator it_t;
0047   it_t it = a_m.find(a_key);
0048   if(it==a_m.end()) return false;
0049   V* obj = (*it).second;
0050   a_m.erase(it);
0051   delete obj;
0052   return true;
0053 }
0054 
0055 template <class K,class V>
0056 inline void raw_clear(std::map<K,V*>& a_m){
0057   typedef typename std::map<K,V*>::iterator it_t;
0058   for(it_t it=a_m.begin();it!=a_m.end();++it) delete (*it).second;
0059   a_m.clear();
0060 }
0061 
0062 template <class K,class V>
0063 inline void copy(std::map<K,V*>& a_to,const std::map<K,V*>& a_from){
0064   raw_clear<K,V>(a_to);
0065   typedef typename std::map<K,V*>::const_iterator it_t;
0066   for(it_t it=a_from.begin();it!=a_from.end();++it) {
0067     a_to[(*it).first] = (*it).second->copy();
0068   }
0069 }
0070 
0071 template <class K,class V>
0072 inline bool add_unique(std::map<K,V*>& a_map,const K& a_key,V* a_value,bool a_delete) {
0073   typedef typename std::map<K,V*>::iterator it_t;
0074   it_t it = a_map.find(a_key);
0075   if(it==a_map.end()) {a_map[a_key] = a_value;return false;} //false=was not found.
0076   if(a_delete) {
0077     V* entry  = (*it).second;
0078     a_map.erase(it);
0079     delete entry;
0080   }
0081   a_map[a_key] = a_value;
0082   return true;
0083 }
0084 
0085 template <class K,class V>
0086 inline bool find(const std::map<K,V>& a_map,const K& a_key,V& a_value) {
0087   typedef typename std::map<K,V>::const_iterator it_t;
0088   it_t it = a_map.find(a_key);
0089   if(it==a_map.end()) {a_value = V();return false;}
0090   a_value = (*it).second;
0091   return true;
0092 }
0093 
0094 template <class K,class V>
0095 inline bool is_key(const std::map<K,V>& a_map,const K& a_key) {
0096   typedef typename std::map<K,V>::const_iterator it_t;
0097   it_t it = a_map.find(a_key);
0098   if(it==a_map.end()) return false;
0099   return true;
0100 }
0101 
0102 }
0103 
0104 #endif