deba@627
|
1 |
#ifndef ARRAY_MAP_H
|
deba@627
|
2 |
#define ARRAY_MAP_H
|
deba@627
|
3 |
|
deba@627
|
4 |
#include <memory>
|
deba@627
|
5 |
|
deba@627
|
6 |
#include "map_base.h"
|
deba@627
|
7 |
|
deba@627
|
8 |
#include <iostream>
|
deba@627
|
9 |
using namespace std;
|
deba@627
|
10 |
|
deba@627
|
11 |
namespace hugo {
|
deba@627
|
12 |
|
deba@627
|
13 |
template <typename G, typename K, typename KIt>
|
deba@627
|
14 |
class ArrayMapFactory {
|
deba@627
|
15 |
|
deba@627
|
16 |
|
deba@627
|
17 |
public:
|
deba@627
|
18 |
|
deba@627
|
19 |
typedef G Graph;
|
deba@627
|
20 |
typedef K Key;
|
deba@627
|
21 |
typedef KIt KeyIt;
|
deba@627
|
22 |
|
deba@627
|
23 |
template <typename V, typename A = allocator<V> >
|
deba@627
|
24 |
class Map : public MapBase<G, K, KIt> {
|
deba@627
|
25 |
public:
|
deba@627
|
26 |
typedef V Value;
|
deba@627
|
27 |
typedef typename _Alloc_traits<V, A>::_Alloc_type _Alloc_type;
|
deba@627
|
28 |
|
deba@627
|
29 |
|
deba@627
|
30 |
Map() : values(0), capacity(0) {}
|
deba@627
|
31 |
|
deba@627
|
32 |
Map(Graph& g, MapRegistry<G, K, KIt>& r)
|
deba@627
|
33 |
: MapBase<G, K, KIt>(g, r) {
|
deba@627
|
34 |
int max_id = -1;
|
deba@627
|
35 |
for (KeyIt it(*graph); graph->valid(it); graph->next(it)) {
|
deba@627
|
36 |
int id = graph->id(it);
|
deba@627
|
37 |
if (id > max_id) {
|
deba@627
|
38 |
max_id = id;
|
deba@627
|
39 |
}
|
deba@627
|
40 |
}
|
deba@627
|
41 |
if (max_id == -1) {
|
deba@627
|
42 |
capacity = 0;
|
deba@627
|
43 |
values = 0;
|
deba@627
|
44 |
return;
|
deba@627
|
45 |
}
|
deba@627
|
46 |
int capacity = 1;
|
deba@627
|
47 |
while (capacity <= max_id) {
|
deba@627
|
48 |
capacity <<= 1;
|
deba@627
|
49 |
}
|
deba@627
|
50 |
Value* values = reinterpret_cast<Value*>(new char[capacity*sizeof(Value)]);
|
deba@627
|
51 |
for (KeyIt it(*graph); graph->valid(it); graph->next(it)) {
|
deba@627
|
52 |
int id = graph->id(it);
|
deba@627
|
53 |
new(&(values[id])) Value();
|
deba@627
|
54 |
}
|
deba@627
|
55 |
cerr << capacity << endl;
|
deba@627
|
56 |
}
|
deba@627
|
57 |
|
deba@627
|
58 |
virtual ~Map() {
|
deba@627
|
59 |
destroy();
|
deba@627
|
60 |
delete[] reinterpret_cast<char*>(values);
|
deba@627
|
61 |
values = 0;
|
deba@627
|
62 |
capacity = 0;
|
deba@627
|
63 |
}
|
deba@627
|
64 |
|
deba@627
|
65 |
|
deba@627
|
66 |
Value& operator[](const K& key) {
|
deba@627
|
67 |
int id = graph->id(key);
|
deba@627
|
68 |
return values[id];
|
deba@627
|
69 |
}
|
deba@627
|
70 |
|
deba@627
|
71 |
const Value& operator[](const K& key) const {
|
deba@627
|
72 |
int id = graph->id(key);
|
deba@627
|
73 |
return values[id];
|
deba@627
|
74 |
}
|
deba@627
|
75 |
|
deba@627
|
76 |
const Value& get(const K& key) const {
|
deba@627
|
77 |
int id = graph->id(key);
|
deba@627
|
78 |
return values[id];
|
deba@627
|
79 |
}
|
deba@627
|
80 |
|
deba@627
|
81 |
void set(const K& key, const Value& val) {
|
deba@627
|
82 |
int id = graph->id(key);
|
deba@627
|
83 |
values[id] = val;
|
deba@627
|
84 |
}
|
deba@627
|
85 |
|
deba@627
|
86 |
void add(const K& key) {
|
deba@627
|
87 |
cerr << capacity << endl;
|
deba@627
|
88 |
int id = graph->id(key);
|
deba@627
|
89 |
if (id >= capacity) {
|
deba@627
|
90 |
int new_capacity = (capacity == 0 ? 1 : capacity);
|
deba@627
|
91 |
while (new_capacity <= id) {
|
deba@627
|
92 |
new_capacity <<= 1;
|
deba@627
|
93 |
}
|
deba@627
|
94 |
Value* new_values = reinterpret_cast<Value*>(new char[new_capacity*sizeof(Value)]);;
|
deba@627
|
95 |
for (KeyIt it(*graph); graph->valid(it); graph->next(it)) {
|
deba@627
|
96 |
int jd = graph->id(it);
|
deba@627
|
97 |
if (id != jd) {
|
deba@627
|
98 |
new(&(new_values[jd])) Value(values[jd]);
|
deba@627
|
99 |
}
|
deba@627
|
100 |
}
|
deba@627
|
101 |
if (capacity != 0) delete[] reinterpret_cast<char *>(values);
|
deba@627
|
102 |
values = new_values;
|
deba@627
|
103 |
capacity = new_capacity;
|
deba@627
|
104 |
}
|
deba@627
|
105 |
cerr << id << ' ' << capacity << endl;
|
deba@627
|
106 |
new(&(values[id])) Value();
|
deba@627
|
107 |
}
|
deba@627
|
108 |
|
deba@627
|
109 |
void erase(const K& key) {
|
deba@627
|
110 |
int id = graph->id(key);
|
deba@627
|
111 |
values[id].~Value();
|
deba@627
|
112 |
}
|
deba@627
|
113 |
|
deba@627
|
114 |
private:
|
deba@627
|
115 |
int capacity;
|
deba@627
|
116 |
Value* values;
|
deba@627
|
117 |
|
deba@627
|
118 |
};
|
deba@627
|
119 |
|
deba@627
|
120 |
};
|
deba@627
|
121 |
}
|
deba@627
|
122 |
|
deba@627
|
123 |
#endif
|