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