// Copyright (C) 2010, Guy Barrand. All rights reserved. // See the file tools.license for terms. #ifndef tools_mapmanip #define tools_mapmanip #include namespace tools { template inline void safe_clear(std::map& a_m){ // the below takes into account the case in // which "delete entry" could modify a_m. typedef typename std::map::iterator it_t; while(!a_m.empty()) { it_t it = a_m.begin(); V* entry = (*it).second; a_m.erase(it); delete entry; } } template inline void find_and_remove_value(std::map& a_m,V* a_value){ typedef typename std::map::iterator it_t; while(true) { bool found = false; for(it_t it=a_m.begin();it!=a_m.end();++it) { if((*it).second==a_value) { a_m.erase(it); found = true; break; } } if(!found) break; } } template inline bool delete_key(std::map& a_m,const K& a_key){ typedef typename std::map::iterator it_t; it_t it = a_m.find(a_key); if(it==a_m.end()) return false; V* obj = (*it).second; a_m.erase(it); delete obj; return true; } } #endif