Changeset 703:32f280a5ed7d in lemon-0.x
- Timestamp:
- 07/15/04 14:15:58 (20 years ago)
- Branch:
- default
- Phase:
- public
- Convert:
- svn:c9d7d8f5-90d6-0310-b91f-818b3a526b0e/lemon/trunk@954
- Location:
- src/work/deba
- Files:
-
- 7 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; -
src/work/deba/extended_pair.h
r702 r703 1 // -*- c++ -*- 1 2 #ifndef EXTENDED_PAIR_H 2 3 #define EXTENDED_PAIR_H … … 7 8 typedef T2 second_type; 8 9 10 extended_pair() : first(), second() {} 11 9 12 extended_pair(A1 f, A2 s) : first(f), second(s) {} 13 14 template <class Pair> 15 extended_pair(const Pair& pair) : first(pair.first), second(pair.second) {} 10 16 11 17 T1 first; … … 13 19 }; 14 20 21 template <typename T1, typename T2, 22 typename LA1, typename LA2, typename RA1, typename RA2> 23 bool operator==(const extended_pair<T1, LA1, T2, LA2>& left, 24 const extended_pair<T1, RA1, T2, RA2>& right) { 25 return left.first == right.first && left.second == right.second; 26 } 27 28 template <typename T1, typename T2, 29 typename LA1, typename LA2, typename RA1, typename RA2> 30 bool operator!=(const extended_pair<T1, LA1, T2, LA2>& left, 31 const extended_pair<T1, RA1, T2, RA2>& right) { 32 return !(left == right); 33 } 34 35 template <typename T1, typename T2, 36 typename LA1, typename LA2, typename RA1, typename RA2> 37 bool operator<(const extended_pair<T1, LA1, T2, LA2>& left, 38 const extended_pair<T1, RA1, T2, RA2>& right) { 39 if (left.first == right.first) return left.second == right.second; 40 return left.first < right.first; 41 } 42 43 template <typename T1, typename T2, 44 typename LA1, typename LA2, typename RA1, typename RA2> 45 bool operator>(const extended_pair<T1, LA1, T2, LA2>& left, 46 const extended_pair<T1, RA1, T2, RA2>& right) { 47 return right < left; 48 } 49 50 template <typename T1, typename T2, 51 typename LA1, typename LA2, typename RA1, typename RA2> 52 bool operator<=(const extended_pair<T1, LA1, T2, LA2>& left, 53 const extended_pair<T1, RA1, T2, RA2>& right) { 54 return !(right > left); 55 } 56 57 template <typename T1, typename T2, 58 typename LA1, typename LA2, typename RA1, typename RA2> 59 bool operator>=(const extended_pair<T1, LA1, T2, LA2>& left, 60 const extended_pair<T1, RA1, T2, RA2>& right) { 61 return !(right < left); 62 } 63 64 15 65 #endif -
src/work/deba/list_graph.h
r702 r703 13 13 #include "invalid.h" 14 14 15 #include " vector_map_factory.h"15 #include "array_map_factory.h" 16 16 #include "map_registry.h" 17 17 … … 77 77 78 78 CREATE_MAP_REGISTRIES; 79 CREATE_MAPS( VectorMapFactory);79 CREATE_MAPS(ArrayMapFactory); 80 80 public: 81 81 -
src/work/deba/main.cpp
r702 r703 1 // -*- c++ -*- 1 2 #include <iostream> 2 3 #include <cstdlib> … … 5 6 using namespace std; 6 7 using namespace hugo; 8 7 9 8 10 … … 23 25 for (pit = map.begin(); pit != map.end(); ++pit) { 24 26 cout << g.id(pit->first) << ' ' << pit->second << endl; 25 } 26 /* 27 ListGraph::NodeMap<double> ot_map = map; 28 for (ListGraph::NodeIt it(g); g.valid(it); g.next(it)) { 29 ot_map[it] *= 2.1; 30 cout << ot_map[it] << endl; 31 } 32 ot_map = map; 33 for (ListGraph::NodeIt it(g); g.valid(it); g.next(it)) { 34 ot_map[it] *= 3.1; 35 cout << ot_map[it] << endl; 36 }*/ 27 (*pit).second = g.id(pit->first); 28 cout << g.id((*pit).first) << ' ' << (*pit).second << endl; 29 } 30 const ListGraph::NodeMap<int> const_map = map; 31 ListGraph::NodeMap<int>::const_iterator cit; 32 for (cit = const_map.begin(); cit != const_map.end(); ++cit) { 33 cerr << g.id(cit->first) << ' ' << cit->second << endl; 34 cerr << g.id((*cit).first) << ' ' << (*cit).second << endl; 35 } 37 36 return 0; 38 37 } -
src/work/deba/map_defines.h
r701 r703 54 54 NodeMap(const Graph& g) : Factory::Map<V>(&g, &(g.node_maps)) {} \ 55 55 NodeMap(const Graph& g, const V& v) : Factory::Map<V>(g, g.node_maps, v) {} \ 56 NodeMap(const NodeMap& copy) : Factory::Map<V>(copy) {} \ 56 NodeMap(const NodeMap& copy) \ 57 : Factory::Map<V>(static_cast<const Factory::Map<V>&>(copy)) {} \ 57 58 template <typename CMap> NodeMap(const CMap& copy) : Factory::Map<V>(copy) {} \ 58 59 NodeMap& operator=(const NodeMap& copy) { \ 59 this->Factory::Map<V>::operator=( copy); \60 this->Factory::Map<V>::operator=(static_cast<Factory::Map<V>&>(copy)); \ 60 61 return *this; \ 61 62 } \ … … 79 80 EdgeMap(const Graph& g) : Factory::Map<V>(g, g.edge_maps) {} \ 80 81 EdgeMap(const Graph& g, const V& v) : Factory::Map<V>(g, g.node_maps, v) {} \ 81 EdgeMap(const EdgeMap& copy) : Factory::Map<V>(copy) {} \ 82 EdgeMap(const EdgeMap& copy) \ 83 : Factory::Map<V>(static_cast<Factory::Map<V>&>(copy)) {} \ 82 84 template <typename CMap> EdgeMap(const CMap& copy) : Factory::Map<V>(copy) {} \ 83 85 EdgeMap& operator=(const EdgeMap& copy) { \ 84 this->Factory::Map<V>::operator=( copy); \86 this->Factory::Map<V>::operator=(static_cast<Factory::Map<V>&>(copy)); \ 85 87 return *this; \ 86 88 } \ -
src/work/deba/map_registry.h
r701 r703 92 92 const Graph* getGraph() const { return graph; } 93 93 94 pr ivate:95 96 const Graph* graph; 94 protected: 95 96 const Graph* graph; 97 97 Registry* registry; 98 98 -
src/work/deba/vector_map_factory.h
r702 r703 1 // -*- c++ -*- 1 2 #ifndef VECTOR_MAP_H 2 3 #define VECTOR_MAP_H 3 4 4 5 #include <vector> 5 #include <iostream>6 6 7 7 #include "extended_pair.h" … … 56 56 */ 57 57 Map(const Graph& g, MapRegistry& r, const Value& v) : MapBase(g, r) { 58 init();59 58 for (KeyIt it(*getGraph()); getGraph()->valid(it); getGraph()->next(it)) { 60 set(it, v); 59 int id = getGraph->id(it); 60 if (id >= container.size) { 61 container.resize(id + 1); 62 } 63 set(it, v); 61 64 } 62 65 } … … 66 69 template <typename CMap> Map(const CMap& copy) : MapBase(copy) { 67 70 if (getGraph()) { 68 init();69 71 for (KeyIt it(*getGraph()); getGraph()->valid(it); getGraph()->next(it)) { 72 int id = getGraph->id(it); 73 if (id >= container.size) { 74 container.resize(id + 1); 75 } 70 76 set(it, copy[it]); 71 77 } … … 81 87 this->MapBase::operator=(copy); 82 88 if (getGraph()) { 83 init();84 89 for (KeyIt it(*getGraph()); getGraph()->valid(it); getGraph()->next(it)) { 90 int id = getGraph->id(it); 91 if (id >= container.size) { 92 container.resize(id + 1); 93 } 85 94 set(it, copy[it]); 86 95 } … … 91 100 */ 92 101 virtual ~Map() { 93 destroy();94 102 } 95 103 … … 152 160 iterator() {} 153 161 162 typedef extended_pair<const Key&, const Key&, 163 Value&, Value&> Reference; 164 154 165 /** Dereference operator for map. 155 166 */ 156 std::pair<const Key, Value> operator*() { 157 return std::pair<const Key, Value>(it, (*map)[it]); 158 } 159 167 Reference operator*() { 168 return Reference(it, (*map)[it]); 169 } 160 170 161 171 class Pointer { 162 172 friend class iterator; 163 173 private: 164 typedef extended_pair<const Key&, const Key&, Value&, Value&> Pair; 165 Pair data; 174 Reference data; 166 175 Pointer(const Key& key, Value& val) : data(key, val) {} 167 176 public: 168 Pair* operator->() {return &data;}177 Reference* operator->() {return &data;} 169 178 }; 170 179 … … 228 237 * by the begin() and end(). 229 238 */ 230 const_iterator (Map& pmap, const KeyIt& pit) : map(&pmap), it(pit) {} 239 const_iterator (const Map& pmap, const KeyIt& pit) 240 : map(&pmap), it(pit) {} 231 241 232 242 public: … … 238 248 /** Constructor to convert iterator to const_iterator. 239 249 */ 240 const_iterator(iterator p_it) { 241 it = p_it.it; 242 } 250 const_iterator(iterator p_it) : map(p_it.map), it(p_it.it) {} 243 251 252 typedef extended_pair<const Key&, const Key&, 253 const Value&, const Value&> Reference; 254 244 255 /** Dereference operator for map. 245 256 */ 246 std::pair<const Key, const Value> operator*() const { 247 return std::pair<const Key, const Value>(it, (*map)[it]); 248 } 257 Reference operator*() { 258 return Reference(it, (*map)[it]); 259 } 260 249 261 250 262 class Pointer { 251 263 friend class const_iterator; 252 264 private: 253 typedef extended_pair<const Key&, const Key&, const Value&, const Value&> Pair; 254 Pair data; 265 Reference data; 255 266 Pointer(const Key& key, const Value& val) : data(key, val) {} 256 267 public: 257 Pair* operator->() {return &data;}268 Reference* operator->() {return &data;} 258 269 }; 270 259 271 /** Arrow operator for map. 260 272 */ 261 Pointer operator->() const{262 return Pointer(it, ( *map)[it]);273 Pointer operator->() { 274 return Pointer(it, ((*map)[it])); 263 275 } 264 276
Note: See TracChangeset
for help on using the changeset viewer.