Changeset 703:32f280a5ed7d in lemon-0.x for src/work/deba/array_map_factory.h
- Timestamp:
- 07/15/04 14:15:58 (20 years ago)
- Branch:
- default
- Phase:
- public
- Convert:
- svn:c9d7d8f5-90d6-0310-b91f-818b3a526b0e/lemon/trunk@954
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/work/deba/array_map_factory.h
r702 r703 1 // -*- c++ -*- 1 2 #ifndef ARRAY_MAP_H 2 3 #define ARRAY_MAP_H … … 4 5 #include <memory> 5 6 6 7 #include <iostream> 8 using namespace std; 7 #include "extended_pair.h" 9 8 10 9 namespace hugo { … … 20 19 typedef typename MapRegistry::MapBase MapBase; 21 20 22 template <typename V, typename A = std::allocator<V> > class Map : public MapBase { 21 template <typename V, typename A = std::allocator<V> > 22 class Map : public MapBase { 23 23 24 24 public: … … 31 31 32 32 Map(const Graph& g, MapRegistry& r) : MapBase(g, r) { 33 int max_id = -1; 34 for (KeyIt it(*getGraph()); getGraph()->valid(it); getGraph()->next(it)) { 35 int id = getGraph()->id(it); 36 if (id > max_id) { 37 max_id = id; 38 } 39 } 40 if (max_id == -1) { 41 capacity = 0; 42 values = 0; 43 return; 44 } 45 capacity = 1; 46 while (capacity <= max_id) { 47 capacity <<= 1; 48 } 49 values = allocator.allocate(capacity); 33 allocate_memory(); 50 34 for (KeyIt it(*getGraph()); getGraph()->valid(it); getGraph()->next(it)) { 51 35 int id = getGraph()->id(it); … … 55 39 56 40 Map(const Graph& g, MapRegistry& r, const Value& v) : MapBase(g, r) { 57 int max_id = -1; 58 for (KeyIt it(*getGraph()); getGraph()->valid(it); getGraph()->next(it)) { 59 int id = getGraph()->id(it); 60 if (id > max_id) { 61 max_id = id; 62 } 63 } 64 if (max_id == -1) { 65 capacity = 0; 66 values = 0; 67 return; 68 } 69 capacity = 1; 70 while (capacity <= max_id) { 71 capacity <<= 1; 72 } 73 values = allocator.allocate(capacity); 41 allocate_memory(); 74 42 for (KeyIt it(*getGraph()); getGraph()->valid(it); getGraph()->next(it)) { 75 43 int id = getGraph()->id(it); … … 88 56 } 89 57 90 template <typename CMap> Map(const CMap& copy) : MapBase(copy) { 58 template <typename CMap> Map(const CMap& copy) 59 : capacity(0), values(0), MapBase(copy) { 91 60 if (getGraph()) { 92 init();61 allocate_memory(); 93 62 for (KeyIt it(*getGraph()); getGraph()->valid(it); getGraph()->next(it)) { 94 63 set(it, copy[it]); … … 118 87 this->MapBase::operator=(copy); 119 88 if (getGraph()) { 120 init();89 allocate_memory(); 121 90 for (KeyIt it(*getGraph()); getGraph()->valid(it); getGraph()->next(it)) { 122 91 set(it, copy[it]); … … 124 93 } 125 94 } 126 127 128 95 129 96 virtual ~Map() { … … 182 149 } 183 150 184 /** Compatible iterator with the stl maps' iterators.185 * It iterates on pairs of a key and a value.186 */187 151 class iterator { 188 152 friend class Map; 189 153 friend class const_iterator; 190 154 private: 191 155 192 156 /** Private constructor to initalize the the iterators returned … … 195 159 iterator (Map& pmap, const KeyIt& pit) : map(&pmap), it(pit) {} 196 160 197 161 public: 198 162 199 163 /** Default constructor. 200 164 */ 201 165 iterator() {} 166 167 typedef extended_pair<const Key&, const Key&, 168 Value&, Value&> Reference; 202 169 203 170 /** Dereference operator for map. 204 171 */ 205 std::pair<const Key, Value> operator*() { 206 return std::pair<const Key, Value>(it, (*map)[it]); 207 } 172 Reference operator*() { 173 return Reference(it, (*map)[it]); 174 } 175 176 class Pointer { 177 friend class iterator; 178 private: 179 Reference data; 180 Pointer(const Key& key, Value& val) : data(key, val) {} 181 public: 182 Reference* operator->() {return &data;} 183 }; 208 184 209 185 /** Arrow operator for map. 210 186 */ 211 std::pair<const Key, Value>* operator->() { 212 static std::pair<const Key, Value> tmp = operator*(); 213 return &tmp; 187 Pointer operator->() { 188 return Pointer(it, ((*map)[it])); 214 189 } 215 190 … … 240 215 return !(*this == p_it); 241 216 } 242 243 private: 217 218 219 private: 244 220 Map* map; 245 221 KeyIt it; … … 261 237 friend class Map; 262 238 friend class iterator; 263 239 private: 264 240 265 241 /** Private constructor to initalize the the iterators returned 266 242 * by the begin() and end(). 267 243 */ 268 const_iterator (Map& pmap, const KeyIt& pit) : map(&pmap), it(pit) {} 269 270 public: 244 const_iterator (const Map& pmap, const KeyIt& pit) 245 : map(&pmap), it(pit) {} 246 247 public: 271 248 272 249 /** Default constructor. … … 276 253 /** Constructor to convert iterator to const_iterator. 277 254 */ 278 const_iterator(iterator p_it) { 279 it = p_it.it; 280 } 255 const_iterator(iterator p_it) : map(p_it.map), it(p_it.it) {} 281 256 257 typedef extended_pair<const Key&, const Key&, 258 const Value&, const Value&> Reference; 259 282 260 /** Dereference operator for map. 283 261 */ 284 std::pair<const Key, const Value> operator*() const { 285 return std::pair<const Key, const Value>(it, (*map)[it]); 286 } 262 Reference operator*() { 263 return Reference(it, (*map)[it]); 264 } 265 266 267 class Pointer { 268 friend class const_iterator; 269 private: 270 Reference data; 271 Pointer(const Key& key, const Value& val) : data(key, val) {} 272 public: 273 Reference* operator->() {return &data;} 274 }; 287 275 288 276 /** Arrow operator for map. 289 277 */ 290 std::pair<const Key, const Value>* operator->() const { 291 static std::pair<const Key, const Value> tmp = operator*(); 292 return &tmp; 278 Pointer operator->() { 279 return Pointer(it, ((*map)[it])); 293 280 } 294 281 … … 320 307 } 321 308 322 private: 309 310 private: 323 311 const Map* map; 324 312 KeyIt it; … … 337 325 } 338 326 339 private: 327 private: 328 329 void allocate_memory() { 330 int max_id = -1; 331 for (KeyIt it(*getGraph()); getGraph()->valid(it); getGraph()->next(it)) { 332 int id = getGraph()->id(it); 333 if (id > max_id) { 334 max_id = id; 335 } 336 } 337 if (max_id == -1) { 338 capacity = 0; 339 values = 0; 340 return; 341 } 342 capacity = 1; 343 while (capacity <= max_id) { 344 capacity <<= 1; 345 } 346 values = allocator.allocate(capacity); 347 } 340 348 int capacity; 341 349 Value* values;
Note: See TracChangeset
for help on using the changeset viewer.