Files
geant4/source/externals/g4tools/include/tools/mapmanip
T
2025-12-05 08:54:02 +01:00

54 lines
1.1 KiB
Plaintext

// Copyright (C) 2010, Guy Barrand. All rights reserved.
// See the file tools.license for terms.
#ifndef tools_mapmanip
#define tools_mapmanip
#include <map>
namespace tools {
template <class K,class V>
inline void safe_clear(std::map<K,V*>& a_m){
// the below takes into account the case in
// which "delete entry" could modify a_m.
typedef typename std::map<K,V*>::iterator it_t;
while(!a_m.empty()) {
it_t it = a_m.begin();
V* entry = (*it).second;
a_m.erase(it);
delete entry;
}
}
template <class K,class V>
inline void find_and_remove_value(std::map<K,V*>& a_m,V* a_value){
typedef typename std::map<K,V*>::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 <class K,class V>
inline bool delete_key(std::map<K,V*>& a_m,const K& a_key){
typedef typename std::map<K,V*>::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