1.1 --- a/src/work/deba/array_map_factory.h Wed Jun 02 09:47:10 2004 +0000
1.2 +++ b/src/work/deba/array_map_factory.h Fri Jun 04 11:52:53 2004 +0000
1.3 @@ -3,121 +3,125 @@
1.4
1.5 #include <memory>
1.6
1.7 -#include "map_base.h"
1.8
1.9 #include <iostream>
1.10 using namespace std;
1.11
1.12 namespace hugo {
1.13
1.14 - template <typename G, typename K, typename KIt>
1.15 - class ArrayMapFactory {
1.16 -
1.17 -
1.18 + template <typename MapRegistry> class ArrayMapFactory {
1.19 +
1.20 + public:
1.21 +
1.22 + typedef typename MapRegistry::Graph Graph;
1.23 + typedef typename MapRegistry::Key Key;
1.24 + typedef typename MapRegistry::KeyIt KeyIt;
1.25 +
1.26 + typedef typename MapRegistry::MapBase MapBase;
1.27 +
1.28 + template <typename V, typename A = std::allocator<V> >
1.29 + class Map : public MapBase {
1.30 +
1.31 public:
1.32 -
1.33 - typedef G Graph;
1.34 - typedef K Key;
1.35 - typedef KIt KeyIt;
1.36 -
1.37 - template <typename V, typename A = allocator<V> >
1.38 - class Map : public MapBase<G, K, KIt> {
1.39 - public:
1.40 - typedef V Value;
1.41 - typedef typename _Alloc_traits<V, A>::_Alloc_type _Alloc_type;
1.42 +
1.43 + typedef V Value;
1.44 + typedef A Allocator;
1.45
1.46
1.47 - Map() : values(0), capacity(0) {}
1.48 + Map() : values(0), capacity(0) {}
1.49
1.50 - Map(Graph& g, MapRegistry<G, K, KIt>& r)
1.51 - : MapBase<G, K, KIt>(g, r) {
1.52 - int max_id = -1;
1.53 - for (KeyIt it(*graph); graph->valid(it); graph->next(it)) {
1.54 - int id = graph->id(it);
1.55 - if (id > max_id) {
1.56 - max_id = id;
1.57 - }
1.58 - }
1.59 - if (max_id == -1) {
1.60 - capacity = 0;
1.61 - values = 0;
1.62 - return;
1.63 - }
1.64 - int capacity = 1;
1.65 - while (capacity <= max_id) {
1.66 - capacity <<= 1;
1.67 - }
1.68 - Value* values = reinterpret_cast<Value*>(new char[capacity*sizeof(Value)]);
1.69 - for (KeyIt it(*graph); graph->valid(it); graph->next(it)) {
1.70 - int id = graph->id(it);
1.71 - new(&(values[id])) Value();
1.72 - }
1.73 - cerr << capacity << endl;
1.74 - }
1.75 + Map(Graph& g, MapRegistry& r) : MapBase(g, r) {
1.76 + int max_id = -1;
1.77 + for (KeyIt it(*graph); graph->valid(it); graph->next(it)) {
1.78 + int id = graph->id(it);
1.79 + if (id > max_id) {
1.80 + max_id = id;
1.81 + }
1.82 + }
1.83 + if (max_id == -1) {
1.84 + capacity = 0;
1.85 + values = 0;
1.86 + return;
1.87 + }
1.88 + capacity = 1;
1.89 + while (capacity <= max_id) {
1.90 + capacity <<= 1;
1.91 + }
1.92 + values = allocator.allocate(capacity);
1.93 + for (KeyIt it(*graph); graph->valid(it); graph->next(it)) {
1.94 + int id = graph->id(it);
1.95 + allocator.construct(&(values[id]), Value());
1.96 + }
1.97 + }
1.98 +
1.99 + Map(const Map& copy) : MapBase(*copy.graph, *copy.registry) {
1.100 + capacity = copy.capacity;
1.101 + values = allocator.allocate(capacity);
1.102 + for (KeyIt it(*graph); graph->valid(it); graph->next(it)) {
1.103 + int id = graph->id(it);
1.104 + allocator.construct(&(values[id]), copy.values[id]);
1.105 + }
1.106 + }
1.107
1.108 - virtual ~Map() {
1.109 - destroy();
1.110 - delete[] reinterpret_cast<char*>(values);
1.111 - values = 0;
1.112 - capacity = 0;
1.113 - }
1.114 + virtual ~Map() {
1.115 + destroy();
1.116 + allocator.deallocate(values, capacity);
1.117 + }
1.118
1.119
1.120 - Value& operator[](const K& key) {
1.121 - int id = graph->id(key);
1.122 - return values[id];
1.123 - }
1.124 + Value& operator[](const Key& key) {
1.125 + int id = graph->id(key);
1.126 + return values[id];
1.127 + }
1.128
1.129 - const Value& operator[](const K& key) const {
1.130 - int id = graph->id(key);
1.131 - return values[id];
1.132 - }
1.133 + const Value& operator[](const Key& key) const {
1.134 + int id = graph->id(key);
1.135 + return values[id];
1.136 + }
1.137
1.138 - const Value& get(const K& key) const {
1.139 - int id = graph->id(key);
1.140 - return values[id];
1.141 - }
1.142 + const Value& get(const Key& key) const {
1.143 + int id = graph->id(key);
1.144 + return values[id];
1.145 + }
1.146
1.147 - void set(const K& key, const Value& val) {
1.148 - int id = graph->id(key);
1.149 - values[id] = val;
1.150 - }
1.151 + void set(const Key& key, const Value& val) {
1.152 + int id = graph->id(key);
1.153 + values[id] = val;
1.154 + }
1.155
1.156 - void add(const K& key) {
1.157 - cerr << capacity << endl;
1.158 - int id = graph->id(key);
1.159 - if (id >= capacity) {
1.160 - int new_capacity = (capacity == 0 ? 1 : capacity);
1.161 - while (new_capacity <= id) {
1.162 - new_capacity <<= 1;
1.163 - }
1.164 - Value* new_values = reinterpret_cast<Value*>(new char[new_capacity*sizeof(Value)]);;
1.165 - for (KeyIt it(*graph); graph->valid(it); graph->next(it)) {
1.166 - int jd = graph->id(it);
1.167 - if (id != jd) {
1.168 - new(&(new_values[jd])) Value(values[jd]);
1.169 - }
1.170 - }
1.171 - if (capacity != 0) delete[] reinterpret_cast<char *>(values);
1.172 - values = new_values;
1.173 - capacity = new_capacity;
1.174 - }
1.175 - cerr << id << ' ' << capacity << endl;
1.176 - new(&(values[id])) Value();
1.177 - }
1.178 + void add(const Key& key) {
1.179 + int id = graph->id(key);
1.180 + if (id >= capacity) {
1.181 + int new_capacity = (capacity == 0 ? 1 : capacity);
1.182 + while (new_capacity <= id) {
1.183 + new_capacity <<= 1;
1.184 + }
1.185 + Value* new_values = allocator.allocate(new_capacity);;
1.186 + for (KeyIt it(*graph); graph->valid(it); graph->next(it)) {
1.187 + int jd = graph->id(it);
1.188 + if (id != jd) {
1.189 + allocator.construct(&(new_values[jd]), values[jd]);
1.190 + allocator.destroy(&(values[jd]));
1.191 + }
1.192 + }
1.193 + if (capacity != 0) allocator.deallocate(values, capacity);
1.194 + values = new_values;
1.195 + capacity = new_capacity;
1.196 + }
1.197 + allocator.construct(&(values[id]), Value());
1.198 + }
1.199
1.200 - void erase(const K& key) {
1.201 - int id = graph->id(key);
1.202 - values[id].~Value();
1.203 - }
1.204 + void erase(const Key& key) {
1.205 + int id = graph->id(key);
1.206 + allocator.destroy(&(values[id]));
1.207 + }
1.208
1.209 - private:
1.210 - int capacity;
1.211 - Value* values;
1.212 -
1.213 - };
1.214 -
1.215 - };
1.216 + private:
1.217 + int capacity;
1.218 + Value* values;
1.219 + Allocator allocator;
1.220 + };
1.221 + };
1.222 }
1.223
1.224 #endif