Changeset 1999:2ff283124dfc in lemon-0.x for lemon/bits/array_map.h
- Timestamp:
- 03/06/06 11:28:37 (18 years ago)
- Branch:
- default
- Phase:
- public
- Convert:
- svn:c9d7d8f5-90d6-0310-b91f-818b3a526b0e/lemon/trunk@2609
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
lemon/bits/array_map.h
r1996 r1999 21 21 22 22 #include <memory> 23 #include <lemon/bits/map_extender.h> 23 24 #include <lemon/bits/traits.h> 24 25 #include <lemon/bits/alteration_notifier.h> 25 #include <lemon/concept_check.h>26 #include <lemon/concept/maps.h>27 26 28 27 /// \ingroup graphbits 29 28 /// \file 30 /// \brief Graph maps that construct and destruct 31 /// their elements dynamically. 29 /// \brief Graph map based on the array storage. 32 30 33 31 namespace lemon { … … 38 36 /// 39 37 /// The ArrayMap template class is graph map structure what 40 /// automatically indates the map when a key is added to or erased from38 /// automatically updates the map when a key is added to or erased from 41 39 /// the map. This map uses the allocators to implement 42 40 /// the container functionality. 43 41 /// 44 /// The template parameter is the AlterationNotifier that the maps 45 /// will belong to and the Value. 46 47 template <typename _Graph, 48 typename _Item, 49 typename _Value> 50 class ArrayMap : public AlterationNotifier<_Item>::ObserverBase { 51 52 typedef _Item Item; 42 /// The template parameter is the Graph the current Item type and 43 /// the Value type of the map. 44 template <typename _Graph, typename _Item, typename _Value> 45 class ArrayMap 46 : public ItemSetTraits<_Graph, _Item>::ItemNotifier::ObserverBase { 53 47 public: 54 48 /// The graph type of the maps. 55 49 typedef _Graph Graph; 50 /// The item type of the map. 51 typedef _Item Item; 56 52 /// The reference map tag. 57 53 typedef True ReferenceMapTag; … … 61 57 /// The value type of the map. 62 58 typedef _Value Value; 59 63 60 /// The const reference type of the map. 64 61 typedef const _Value& ConstReference; … … 66 63 typedef _Value& Reference; 67 64 68 typedef const Value ConstValue; 69 typedef Value* Pointer; 70 typedef const Value* ConstPointer; 71 72 typedef AlterationNotifier<_Item> Registry; 65 /// The notifier type. 66 typedef typename ItemSetTraits<_Graph, _Item>::ItemNotifier Notifier; 73 67 74 68 /// The MapBase of the Map which imlements the core regisitry function. 75 typedef typename Registry::ObserverBase Parent;69 typedef typename Notifier::ObserverBase Parent; 76 70 77 78 79 71 private: 80 72 typedef std::allocator<Value> Allocator; 81 73 82 83 74 public: 84 75 … … 86 77 /// 87 78 /// Graph initialized map constructor. 88 ArrayMap(const Graph& _g) : graph(&_g) { 79 ArrayMap(const Graph& graph) { 80 Parent::attach(graph.getNotifier(Item())); 81 allocate_memory(); 82 Notifier* notifier = Parent::getNotifier(); 89 83 Item it; 90 attach(_g.getNotifier(Item())); 91 allocate_memory(); 92 for (graph->first(it); it != INVALID; graph->next(it)) { 93 int id = graph->id(it);; 84 for (notifier->first(it); it != INVALID; notifier->next(it)) { 85 int id = notifier->id(it);; 94 86 allocator.construct(&(values[id]), Value()); 95 87 } … … 99 91 /// 100 92 /// It constructs a map and initialize all of the the map. 101 ArrayMap(const Graph& _g, const Value& _v) : graph(&_g) { 93 ArrayMap(const Graph& graph, const Value& value) { 94 Parent::attach(graph.getNotifier(Item())); 95 allocate_memory(); 96 Notifier* notifier = Parent::getNotifier(); 102 97 Item it; 103 attach(_g.getNotifier(_Item())); 104 allocate_memory(); 105 for (graph->first(it); it != INVALID; graph->next(it)) { 106 int id = graph->id(it);; 107 allocator.construct(&(values[id]), _v); 98 for (notifier->first(it); it != INVALID; notifier->next(it)) { 99 int id = notifier->id(it);; 100 allocator.construct(&(values[id]), value); 108 101 } 109 102 } … … 112 105 /// 113 106 /// Constructor to copy a map of the same map type. 114 ArrayMap(const ArrayMap& copy) : Parent() , graph(copy.graph){107 ArrayMap(const ArrayMap& copy) : Parent() { 115 108 if (copy.attached()) { 116 attach(*copy.get Registry());109 attach(*copy.getNotifier()); 117 110 } 118 111 capacity = copy.capacity; 119 112 if (capacity == 0) return; 120 113 values = allocator.allocate(capacity); 114 Notifier* notifier = Parent::getNotifier(); 121 115 Item it; 122 for ( graph->first(it); it != INVALID; graph->next(it)) {123 int id = graph->id(it);;116 for (notifier->first(it); it != INVALID; notifier->next(it)) { 117 int id = notifier->id(it);; 124 118 allocator.construct(&(values[id]), copy.values[id]); 125 119 } … … 146 140 using Parent::attached; 147 141 148 const Graph* getGraph() {149 return graph;150 }151 152 153 142 public: 154 143 … … 158 147 /// actual keys of the graph. 159 148 Value& operator[](const Key& key) { 160 int id = graph->id(key);149 int id = Parent::getNotifier()->id(key); 161 150 return values[id]; 162 151 } … … 167 156 /// actual keys of the graph. 168 157 const Value& operator[](const Key& key) const { 169 int id = graph->id(key);158 int id = Parent::getNotifier()->id(key); 170 159 return values[id]; 171 160 } … … 181 170 protected: 182 171 183 /// Add a new key to the map. It called by the map registry. 184 172 /// \brief Adds a new key to the map. 173 /// 174 /// It adds a new key to the map. It called by the observer notifier 175 /// and it overrides the add() member function of the observer base. 185 176 virtual void add(const Key& key) { 186 int id = graph->id(key); 177 Notifier* notifier = Parent::getNotifier(); 178 int id = notifier->id(key); 187 179 if (id >= capacity) { 188 180 int new_capacity = (capacity == 0 ? 1 : capacity); … … 192 184 Value* new_values = allocator.allocate(new_capacity); 193 185 Item it; 194 for ( graph->first(it); it != INVALID; graph->next(it)) {195 int jd = graph->id(it);;186 for (notifier->first(it); it != INVALID; notifier->next(it)) { 187 int jd = notifier->id(it);; 196 188 if (id != jd) { 197 189 allocator.construct(&(new_values[jd]), values[jd]); … … 206 198 } 207 199 200 /// \brief Adds more new keys to the map. 201 /// 202 /// It adds more new keys to the map. It called by the observer notifier 203 /// and it overrides the add() member function of the observer base. 208 204 virtual void add(const std::vector<Key>& keys) { 205 Notifier* notifier = Parent::getNotifier(); 209 206 int max_id = -1; 210 207 for (int i = 0; i < (int)keys.size(); ++i) { 211 int id = graph->id(keys[i]);208 int id = notifier->id(keys[i]); 212 209 if (id > max_id) { 213 210 max_id = id; … … 221 218 Value* new_values = allocator.allocate(new_capacity); 222 219 Item it; 223 for ( graph->first(it); it != INVALID; graph->next(it)) {224 int id = graph->id(it);220 for (notifier->first(it); it != INVALID; notifier->next(it)) { 221 int id = notifier->id(it); 225 222 bool found = false; 226 223 for (int i = 0; i < (int)keys.size(); ++i) { 227 int jd = graph->id(keys[i]);224 int jd = notifier->id(keys[i]); 228 225 if (id == jd) { 229 226 found = true; … … 240 237 } 241 238 for (int i = 0; i < (int)keys.size(); ++i) { 242 int id = graph->id(keys[i]);239 int id = notifier->id(keys[i]); 243 240 allocator.construct(&(values[id]), Value()); 244 241 } 245 242 } 246 243 247 /// Erase a key from the map. It called by the map registry. 248 244 /// \brief Erase a key from the map. 245 /// 246 /// Erase a key from the map. It called by the observer notifier 247 /// and it overrides the erase() member function of the observer base. 249 248 virtual void erase(const Key& key) { 250 int id = graph->id(key);249 int id = Parent::getNotifier()->id(key); 251 250 allocator.destroy(&(values[id])); 252 251 } 253 252 253 /// \brief Erase more keys from the map. 254 /// 255 /// Erase more keys from the map. It called by the observer notifier 256 /// and it overrides the erase() member function of the observer base. 254 257 virtual void erase(const std::vector<Key>& keys) { 255 258 for (int i = 0; i < (int)keys.size(); ++i) { 256 int id = graph->id(keys[i]);259 int id = Parent::getNotifier()->id(keys[i]); 257 260 allocator.destroy(&(values[id])); 258 261 } 259 262 } 260 263 264 /// \brief Buildes the map. 265 /// 266 /// It buildes the map. It called by the observer notifier 267 /// and it overrides the build() member function of the observer base. 261 268 virtual void build() { 269 Notifier* notifier = Parent::getNotifier(); 262 270 allocate_memory(); 263 271 Item it; 264 for ( graph->first(it); it != INVALID; graph->next(it)) {265 int id = graph->id(it);;272 for (notifier->first(it); it != INVALID; notifier->next(it)) { 273 int id = notifier->id(it);; 266 274 allocator.construct(&(values[id]), Value()); 267 275 } 268 276 } 269 277 278 /// \brief Clear the map. 279 /// 280 /// It erase all items from the map. It called by the observer notifier 281 /// and it overrides the clear() member function of the observer base. 270 282 virtual void clear() { 283 Notifier* notifier = Parent::getNotifier(); 271 284 if (capacity != 0) { 272 285 Item it; 273 for ( graph->first(it); it != INVALID; graph->next(it)) {274 int id = graph->id(it);286 for (notifier->first(it); it != INVALID; notifier->next(it)) { 287 int id = notifier->id(it); 275 288 allocator.destroy(&(values[id])); 276 289 } … … 283 296 284 297 void allocate_memory() { 285 int max_id = graph->maxId(_Item());298 int max_id = Parent::getNotifier()->maxId(); 286 299 if (max_id == -1) { 287 300 capacity = 0; … … 296 309 } 297 310 298 const Graph* graph;299 311 int capacity; 300 312 Value* values;
Note: See TracChangeset
for help on using the changeset viewer.