equal
deleted
inserted
replaced
1 #ifndef VECTOR_MAP_H |
1 #ifndef VECTOR_MAP_H |
2 #define VECTOR_MAP_H |
2 #define VECTOR_MAP_H |
3 |
3 |
4 #include <vector> |
4 #include <vector> |
5 |
5 |
6 #include "map_registry.h" |
|
7 |
|
8 namespace hugo { |
6 namespace hugo { |
9 |
7 |
10 template <typename MapRegistry> |
8 template <typename MapRegistry> |
11 class VectorMapFactory { |
9 class VectorMapFactory { |
12 public: |
10 public: |
13 |
11 |
14 typedef typename MapRegistry::Graph Graph; |
12 typedef typename MapRegistry::Graph Graph; |
15 typedef typename MapRegistry::Key Key; |
13 typedef typename MapRegistry::Key Key; |
16 typedef typename MapRegistry::KeyIt KeyIt; |
14 typedef typename MapRegistry::KeyIt KeyIt; |
17 |
15 |
18 typedef typename MapRegistry::MapBase MapBase; |
16 typedef typename MapRegistry::MapBase MapBase; |
|
17 |
19 |
18 |
20 template <typename V> |
19 template <typename V> |
21 class Map : public MapBase { |
20 class Map : public MapBase { |
22 public: |
21 public: |
23 typedef V Value; |
22 typedef V Value; |
24 |
23 |
|
24 typedef std::vector<Value> Container; |
25 Map() {} |
25 Map() {} |
26 |
26 |
27 Map(Graph& g, MapRegistry& r) : MapBase(g, r) { |
27 Map(Graph& g, MapRegistry& r) : MapBase(g, r) { |
28 init(); |
28 init(); |
29 } |
29 } |
32 virtual ~Map() { |
32 virtual ~Map() { |
33 destroy(); |
33 destroy(); |
34 } |
34 } |
35 |
35 |
36 |
36 |
37 Value& operator[](const Key& key) { |
37 typename Container::reference operator[](const Key& key) { |
38 int id = graph->id(key); |
38 int id = graph->id(key); |
39 return container[id]; |
39 return container[id]; |
40 } |
40 } |
41 |
41 |
42 const Value& operator[](const Key& key) const { |
42 typename Container::const_reference operator[](const Key& key) const { |
43 int id = graph->id(key); |
43 int id = graph->id(key); |
44 return container[id]; |
44 return container[id]; |
45 } |
45 } |
46 |
46 |
47 const Value& get(const Key& key) const { |
|
48 int id = graph->id(key); |
|
49 return container[id]; |
|
50 } |
|
51 |
|
52 void set(const Key& key, const Value& val) { |
47 void set(const Key& key, const Value& val) { |
53 int id = graph->id(key); |
48 int id = graph->id(key); |
54 container[id] = val; |
49 container[id] = val; |
55 } |
50 } |
56 |
51 |
61 } |
56 } |
62 } |
57 } |
63 |
58 |
64 void erase(const Key& key) {} |
59 void erase(const Key& key) {} |
65 |
60 |
|
61 class const_iterator { |
|
62 |
|
63 private: |
|
64 |
|
65 }; |
|
66 |
|
67 class iterator { |
|
68 public: |
|
69 iterator() {} |
|
70 |
|
71 std::pair<const Key&, Value&> operator*() { |
|
72 return std::pair<const Key&, Value&>(static_cast<Key&>(it), map[it]); |
|
73 } |
|
74 |
|
75 iterator& operator++() { ++it; return *this; } |
|
76 iterator operator++(int) { iterator tmp(it); ++it; return tmp; } |
|
77 private: |
|
78 Map& map; |
|
79 KeyIt it; |
|
80 }; |
|
81 |
66 private: |
82 private: |
67 typedef std::vector<Value> Container; |
83 typedef std::vector<Value> Container; |
68 |
84 |
69 Container container; |
85 Container container; |
|
86 |
|
87 |
70 }; |
88 }; |
|
89 |
|
90 |
|
91 |
71 |
92 |
72 }; |
93 }; |
73 } |
94 } |
74 |
95 |
75 #endif |
96 #endif |