Geant4 Cross Reference |
1 // Copyright (C) 2010, Guy Barrand. All rights reserved. 2 // See the file tools.license for terms. 3 4 #ifndef tools_vpair 5 #define tools_vpair 6 7 #include <vector> 8 9 namespace tools { 10 11 template <class K,class V> 12 inline void add(std::vector< std::pair<K,V> >& a_vec,const K& a_key,const V& a_value) { 13 typedef typename std::vector< std::pair<K,V> >::iterator it_t; 14 it_t it; 15 for(it=a_vec.begin();it!=a_vec.end();++it) { 16 if((*it).first==a_key) { 17 (*it).second = a_value; //override. 18 return; 19 } 20 } 21 //not found, add a new pair : 22 a_vec.push_back(std::pair<K,V>(a_key,a_value)); 23 } 24 25 template <class K,class V> 26 inline bool find(const std::vector< std::pair<K,V> >& a_vec,const K& a_key,V& a_value) { 27 typedef typename std::vector< std::pair<K,V> >::const_iterator it_t; 28 it_t it; 29 for(it=a_vec.begin();it!=a_vec.end();++it) { 30 if((*it).first==a_key) { 31 a_value = (*it).second; 32 return true; 33 } 34 } 35 a_value = V(); 36 return false; 37 } 38 39 template <class K,class V> 40 inline bool rfind(const std::vector< std::pair<K,V> >& a_vec,const K& a_key,V& a_value) { 41 typedef typename std::vector< std::pair<K,V> >::const_reverse_iterator it_t; 42 it_t it; 43 for(it=a_vec.rbegin();it!=a_vec.rend();++it) { 44 if((*it).first==a_key) { 45 a_value = (*it).second; 46 return true; 47 } 48 } 49 a_value = V(); 50 return false; 51 } 52 53 template <class K,class V> 54 inline bool is_key(const std::vector< std::pair<K,V> >& a_vec,const K& a_key) { 55 typedef typename std::vector< std::pair<K,V> >::const_iterator it_t; 56 it_t it; 57 for(it=a_vec.begin();it!=a_vec.end();++it) { 58 if((*it).first==a_key) return true; 59 } 60 return false; 61 } 62 63 template <class K,class V> 64 inline bool find_key(const std::vector< std::pair<K,V> >& a_vec,const V& a_value,K& a_key) { 65 typedef typename std::vector< std::pair<K,V> >::const_iterator it_t; 66 it_t it; 67 for(it=a_vec.begin();it!=a_vec.end();++it) { 68 if((*it).second==a_value) { 69 a_key = (*it).first; 70 return true; 71 } 72 } 73 a_key = K(); 74 return false; 75 } 76 77 template <class K,class V> 78 inline void sort_by_second(std::vector< std::pair<K,V> >& a_vec){ 79 //sort according V 80 81 //brute force. 82 std::vector< std::pair<K,V> > v; 83 typedef typename std::vector< std::pair<K,V> >::iterator it_t; 84 85 it_t it; 86 for(it=a_vec.begin();it!=a_vec.end();++it) { 87 const V& val = (*it).second; 88 89 bool done = false; 90 it_t it2; 91 for(it2=v.begin();it2!=v.end();++it2) { 92 if(val<(*it2).second) { 93 v.insert(it2,*it); 94 done = true; 95 break; 96 } 97 } 98 if(!done) { 99 v.push_back(*it); 100 } 101 } 102 103 a_vec = v; 104 } 105 106 template <class K,class V> 107 inline bool remove(std::vector< std::pair<K,V> >& a_vec,const K& a_key) { 108 typedef typename std::vector< std::pair<K,V> >::iterator it_t; 109 it_t it; 110 for(it=a_vec.begin();it!=a_vec.end();++it) { 111 if((*it).first==a_key) { 112 a_vec.erase(it); 113 return true; 114 } 115 } 116 return false; 117 } 118 119 template <class K,class V> 120 inline bool remove(std::vector< std::pair<K,V> >& a_vec,const K& a_key,bool a_delete) { 121 typedef typename std::vector< std::pair<K,V> >::iterator it_t; 122 it_t it; 123 for(it=a_vec.begin();it!=a_vec.end();++it) { 124 if((*it).first==a_key) { 125 V val = (*it).second; 126 a_vec.erase(it); 127 if(a_delete) delete val; 128 return true; 129 } 130 } 131 return false; 132 } 133 134 } 135 136 #endif