Rev | Line | |
---|
[571] | 1 | #ifndef VECTOR_MAP_H |
---|
| 2 | #define VECTOR_MAP_H |
---|
| 3 | |
---|
| 4 | #include <vector> |
---|
| 5 | |
---|
| 6 | namespace hugo { |
---|
| 7 | |
---|
[627] | 8 | template <typename MapRegistry> |
---|
[698] | 9 | class VectorMapFactory { |
---|
| 10 | public: |
---|
[627] | 11 | |
---|
| 12 | typedef typename MapRegistry::Graph Graph; |
---|
| 13 | typedef typename MapRegistry::Key Key; |
---|
| 14 | typedef typename MapRegistry::KeyIt KeyIt; |
---|
| 15 | |
---|
| 16 | typedef typename MapRegistry::MapBase MapBase; |
---|
[698] | 17 | |
---|
[627] | 18 | |
---|
| 19 | template <typename V> |
---|
| 20 | class Map : public MapBase { |
---|
| 21 | public: |
---|
| 22 | typedef V Value; |
---|
[571] | 23 | |
---|
[698] | 24 | typedef std::vector<Value> Container; |
---|
[627] | 25 | Map() {} |
---|
| 26 | |
---|
| 27 | Map(Graph& g, MapRegistry& r) : MapBase(g, r) { |
---|
| 28 | init(); |
---|
| 29 | } |
---|
| 30 | |
---|
| 31 | |
---|
| 32 | virtual ~Map() { |
---|
| 33 | destroy(); |
---|
| 34 | } |
---|
[571] | 35 | |
---|
[627] | 36 | |
---|
[698] | 37 | typename Container::reference operator[](const Key& key) { |
---|
[627] | 38 | int id = graph->id(key); |
---|
| 39 | return container[id]; |
---|
| 40 | } |
---|
[571] | 41 | |
---|
[698] | 42 | typename Container::const_reference operator[](const Key& key) const { |
---|
[627] | 43 | int id = graph->id(key); |
---|
| 44 | return container[id]; |
---|
| 45 | } |
---|
| 46 | |
---|
| 47 | void set(const Key& key, const Value& val) { |
---|
| 48 | int id = graph->id(key); |
---|
| 49 | container[id] = val; |
---|
| 50 | } |
---|
| 51 | |
---|
| 52 | void add(const Key& key) { |
---|
| 53 | int id = graph->id(key); |
---|
| 54 | if (id >= container.size()) { |
---|
| 55 | container.resize(id + 1); |
---|
| 56 | } |
---|
| 57 | } |
---|
| 58 | |
---|
| 59 | void erase(const Key& key) {} |
---|
[571] | 60 | |
---|
[698] | 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 | |
---|
[627] | 82 | private: |
---|
| 83 | typedef std::vector<Value> Container; |
---|
[571] | 84 | |
---|
[627] | 85 | Container container; |
---|
[698] | 86 | |
---|
| 87 | |
---|
[627] | 88 | }; |
---|
[698] | 89 | |
---|
| 90 | |
---|
| 91 | |
---|
[571] | 92 | |
---|
[627] | 93 | }; |
---|
[571] | 94 | } |
---|
| 95 | |
---|
| 96 | #endif |
---|
Note: See
TracBrowser
for help on using the repository browser.