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
2.1 --- a/src/work/deba/main.cpp Wed Jun 02 09:47:10 2004 +0000
2.2 +++ b/src/work/deba/main.cpp Fri Jun 04 11:52:53 2004 +0000
2.3 @@ -11,7 +11,7 @@
2.4 for (int i = 0; i < 10; ++i) {
2.5 ListGraph::Node node = g.addNode();
2.6 }
2.7 - ListGraph::NodeMapFactory::Map<int> map(g, g.node_maps);
2.8 + ListGraph::NodeMap<int> map(g);
2.9 for (int i = 0; i < 10; ++i) {
2.10 ListGraph::Node node = g.addNode();
2.11 map[node] = rand()%100;
3.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
3.2 +++ b/src/work/deba/map_defines.h Fri Jun 04 11:52:53 2004 +0000
3.3 @@ -0,0 +1,47 @@
3.4 +#ifndef MAP_DEFINES_H
3.5 +#define MAP_DEFINES_H
3.6 +
3.7 +#define CREATE_EDGE_MAP_REGISTRY \
3.8 +typedef MapRegistry<Graph, Edge, EdgeIt> EdgeMapRegistry; \
3.9 +EdgeMapRegistry edge_maps;
3.10 +
3.11 +#define CREATE_NODE_MAP_REGISTRY \
3.12 +typedef MapRegistry<Graph, Node, NodeIt> NodeMapRegistry; \
3.13 +NodeMapRegistry node_maps;
3.14 +
3.15 +#define CREATE_MAP_REGISTRIES \
3.16 +CREATE_NODE_MAP_REGISTRY \
3.17 +CREATE_EDGE_MAP_REGISTRY
3.18 +
3.19 +#define CREATE_NODE_MAP_FACTORY(TemplateFactory) \
3.20 +typedef TemplateFactory<NodeMapRegistry> NodeMapFactory;
3.21 +
3.22 +#define CREATE_EDGE_MAP_FACTORY(TemplateFactory) \
3.23 +typedef TemplateFactory<EdgeMapRegistry> EdgeMapFactory;
3.24 +
3.25 +#define CREATE_MAP_FACTORIES(TemplateFactory) \
3.26 +CREATE_NODE_MAP_FACTORY(TemplateFactory) \
3.27 +CREATE_EDGE_MAP_FACTORY(TemplateFactory)
3.28 +
3.29 +#define IMPORT_NODE_MAP(Factory) \
3.30 +template <typename V> \
3.31 +class NodeMap : public Factory::Map<V> { \
3.32 +public: \
3.33 +NodeMap() {} \
3.34 +NodeMap(Graph& g) : Factory::Map<V>(g, g.node_maps) {} \
3.35 +};
3.36 +
3.37 +#define IMPORT_EDGE_MAP(Factory) \
3.38 +template <typename V> \
3.39 +class EdgeMap : public Factory::Map<V> { \
3.40 +public: \
3.41 +EdgeMap() {} \
3.42 +EdgeMap(Graph& g) : Factory::Map<V>(g, g.edge_maps) {} \
3.43 +};
3.44 +
3.45 +#define CREATE_MAPS(TemplateFactory) \
3.46 +CREATE_MAP_FACTORIES(TemplateFactory) \
3.47 +IMPORT_NODE_MAP(NodeMapFactory) \
3.48 +IMPORT_EDGE_MAP(EdgeMapFactory)
3.49 +
3.50 +#endif
4.1 --- a/src/work/deba/test_graph.h Wed Jun 02 09:47:10 2004 +0000
4.2 +++ b/src/work/deba/test_graph.h Fri Jun 04 11:52:53 2004 +0000
4.3 @@ -7,7 +7,10 @@
4.4
4.5 #include "invalid.h"
4.6
4.7 -#include "vector_map_factory.h"
4.8 +#include "map_registry.h"
4.9 +#include "array_map_factory.h"
4.10 +
4.11 +#include "map_defines.h"
4.12
4.13 namespace hugo {
4.14
4.15 @@ -30,27 +33,12 @@
4.16 class InEdgeIt;
4.17 class SymEdgeIt;
4.18
4.19 - // template <typename T> class NodeMap;
4.20 - // template <typename T> class EdgeMap;
4.21 + typedef ListGraph Graph;
4.22 +
4.23 + CREATE_MAP_REGISTRIES
4.24 + CREATE_MAPS(ArrayMapFactory)
4.25 +
4.26 private:
4.27 - // template <typename T> friend class NodeMap;
4.28 - // template <typename T> friend class EdgeMap;
4.29 -
4.30 - private:
4.31 -
4.32 -
4.33 - public:
4.34 -
4.35 - typedef MapRegistry<ListGraph, Node, NodeIt> NodeMapRegistry;
4.36 - typedef VectorMapFactory<NodeMapRegistry> NodeMapFactory;
4.37 - NodeMapRegistry node_maps;
4.38 -
4.39 -
4.40 -
4.41 - typedef MapRegistry<ListGraph, Edge, EdgeIt> EdgeMapRegistry;
4.42 - typedef VectorMapFactory<EdgeMapRegistry> EdgeMapFactory;
4.43 - EdgeMapRegistry edge_maps;
4.44 -
4.45
4.46 int node_id;
4.47 int edge_id;