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 10.5.p1 ] [ 10.6 ] [ 10.6.p1 ] [ 10.6.p2 ] [ 10.6.p3 ] [ 10.7 ] [ 10.7.p1 ] [ 10.7.p2 ] [ 10.7.p3 ] [ 10.7.p4 ] [ 11.0 ] [ 11.0.p1 ] [ 11.0.p2 ] [ 11.0.p3, ] [ 11.0.p4 ] [ 11.1 ] [ 11.1.1 ] [ 11.1.2 ] [ 11.1.3 ] [ 11.2 ] [ 11.2.1 ] [ 11.2.2 ] [ 11.3.0 ]

  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