Changeset 1133:9fd485470fee in lemon-0.x for src/work/deba/map_utils.h
- Timestamp:
- 02/07/05 11:48:14 (19 years ago)
- Branch:
- default
- Phase:
- public
- Convert:
- svn:c9d7d8f5-90d6-0310-b91f-818b3a526b0e/lemon/trunk@1532
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/work/deba/map_utils.h
r1115 r1133 24 24 namespace lemon { 25 25 26 /// \addtogroup gutils26 /// \addtogroup mutils 27 27 /// @{ 28 29 28 30 29 /// \brief General inversable map type. … … 34 33 /// and if a key is setted to a new value then store it 35 34 /// in the inverse map. 35 /// \param _Graph The graph type. 36 /// \param _Map The map to extend with inversable functionality. 36 37 template < 37 38 typename _Graph, … … 44 45 45 46 typedef _Map Map; 47 /// The key type of InversableMap (Node, Edge, UndirEdge). 46 48 typedef typename _Map::Key Key; 49 /// The value type of the InversableMap. 47 50 typedef typename _Map::Value Value; 48 51 typedef std::map<Value, Key> InverseMap; … … 61 64 void set(const Key& key, const Value& val) { 62 65 Value oldval = Map::operator[](key); 63 typename InverseMap::iterator it = inv _map.find(oldval);64 if (it != inv _map.end() && it->second == key) {65 inv _map.erase(it);66 typename InverseMap::iterator it = invMap.find(oldval); 67 if (it != invMap.end() && it->second == key) { 68 invMap.erase(it); 66 69 } 67 inv _map.insert(make_pair(val, key));70 invMap.insert(make_pair(val, key)); 68 71 Map::set(key, val); 69 72 } 70 73 71 ConstReference operator[](const Key&) const { 74 /// \brief The getter function of the map. 75 /// 76 /// It gives back the value associated with the key. 77 ConstReference operator[](const Key& key) const { 72 78 return Map::operator[](key); 73 79 } 74 80 75 virtual void add(const Key&) { 81 /// \brief Add a new key to the map. 82 /// 83 /// Add a new key to the map. It is called by the 84 /// \c AlterationNotifier. 85 virtual void add(const Key& key) { 76 86 Map::add(key); 77 87 } 78 88 79 virtual void erase(const Key&) { 89 /// \brief Erase the key from the map. 90 /// 91 /// Erase the key to the map. It is called by the 92 /// \c AlterationNotifier. 93 virtual void erase(const Key& key) { 80 94 Value val = Map::operator[](key); 81 typename InverseMap::iterator it = inv _map.find(val);82 if (it != inv _map.end() && it->second == key) {95 typename InverseMap::iterator it = invMap.find(val); 96 if (it != invMap.end() && it->second == key) { 83 97 invMap.erase(it); 84 98 } … … 86 100 } 87 101 102 /// \brief Clear the keys from the map and inverse map. 103 /// 104 /// Clear the keys from the map and inverse map. It is called by the 105 /// \c AlterationNotifier. 106 virtual void clear() { 107 invMap.clear(); 108 Map::clear(); 109 } 110 111 /// \brief It gives back the just readeable inverse map. 112 /// 113 /// It gives back the just readeable inverse map. 88 114 const InverseMap& inverse() const { 89 return inv _map;115 return invMap; 90 116 } 91 117 92 118 93 119 private: 94 InverseMap inv _map;120 InverseMap invMap; 95 121 }; 96 122 … … 136 162 } 137 163 164 /// \brief Add a new key to the map. 165 /// 166 /// Add a new key to the map. It is called by the 167 /// \c AlterationNotifier. 138 168 virtual void add(const Item& item) { 139 169 Map::add(item); 140 Map::set(item, inv_map.size()); 141 inv_map.push_back(item); 142 } 143 170 Map::set(item, invMap.size()); 171 invMap.push_back(item); 172 } 173 174 /// \brief Erase the key from the map. 175 /// 176 /// Erase the key to the map. It is called by the 177 /// \c AlterationNotifier. 144 178 virtual void erase(const Item& item) { 145 Map::set(inv _map.back(), Map::operator[](item));146 inv _map[Map::operator[](item)] = inv_map.back();179 Map::set(invMap.back(), Map::operator[](item)); 180 invMap[Map::operator[](item)] = invMap.back(); 147 181 Map::erase(item); 148 182 } 149 183 184 /// \brief Build the unique map. 185 /// 186 /// Build the unique map. It is called by the 187 /// \c AlterationNotifier. 150 188 virtual void build() { 151 189 Map::build(); 152 190 Item it; 153 191 for (getGraph()->first(it); it != INVALID; getGraph()->next(it)) { 154 Map::set(it, inv _map.size());155 inv _map.push_back(it);192 Map::set(it, invMap.size()); 193 invMap.push_back(it); 156 194 } 157 195 } 158 196 197 /// \brief Clear the keys from the map. 198 /// 199 /// Clear the keys from the map. It is called by the 200 /// \c AlterationNotifier. 159 201 virtual void clear() { 160 inv _map.clear();202 invMap.clear(); 161 203 Map::clear(); 162 204 } … … 173 215 /// Gives back the inverse of the map. 174 216 const InverseMap inverse() const { 175 return inv _map;217 return invMap; 176 218 } 177 219 178 220 private: 179 vector<Item> inv _map;221 vector<Item> invMap; 180 222 }; 181 223
Note: See TracChangeset
for help on using the changeset viewer.