# HG changeset patch # User deba # Date 1086349973 0 # Node ID 7733d18de0e887668d053e07cb942be5c0401c2a # Parent b387504959a27df96422673088a1339965a96287 diff -r b387504959a2 -r 7733d18de0e8 src/work/deba/array_map_factory.h --- a/src/work/deba/array_map_factory.h Wed Jun 02 09:47:10 2004 +0000 +++ b/src/work/deba/array_map_factory.h Fri Jun 04 11:52:53 2004 +0000 @@ -3,121 +3,125 @@ #include -#include "map_base.h" #include using namespace std; namespace hugo { - template - class ArrayMapFactory { - - + template class ArrayMapFactory { + + public: + + typedef typename MapRegistry::Graph Graph; + typedef typename MapRegistry::Key Key; + typedef typename MapRegistry::KeyIt KeyIt; + + typedef typename MapRegistry::MapBase MapBase; + + template > + class Map : public MapBase { + public: - - typedef G Graph; - typedef K Key; - typedef KIt KeyIt; - - template > - class Map : public MapBase { - public: - typedef V Value; - typedef typename _Alloc_traits::_Alloc_type _Alloc_type; + + typedef V Value; + typedef A Allocator; - Map() : values(0), capacity(0) {} + Map() : values(0), capacity(0) {} - Map(Graph& g, MapRegistry& r) - : MapBase(g, r) { - int max_id = -1; - for (KeyIt it(*graph); graph->valid(it); graph->next(it)) { - int id = graph->id(it); - if (id > max_id) { - max_id = id; - } - } - if (max_id == -1) { - capacity = 0; - values = 0; - return; - } - int capacity = 1; - while (capacity <= max_id) { - capacity <<= 1; - } - Value* values = reinterpret_cast(new char[capacity*sizeof(Value)]); - for (KeyIt it(*graph); graph->valid(it); graph->next(it)) { - int id = graph->id(it); - new(&(values[id])) Value(); - } - cerr << capacity << endl; - } + Map(Graph& g, MapRegistry& r) : MapBase(g, r) { + int max_id = -1; + for (KeyIt it(*graph); graph->valid(it); graph->next(it)) { + int id = graph->id(it); + if (id > max_id) { + max_id = id; + } + } + if (max_id == -1) { + capacity = 0; + values = 0; + return; + } + capacity = 1; + while (capacity <= max_id) { + capacity <<= 1; + } + values = allocator.allocate(capacity); + for (KeyIt it(*graph); graph->valid(it); graph->next(it)) { + int id = graph->id(it); + allocator.construct(&(values[id]), Value()); + } + } + + Map(const Map& copy) : MapBase(*copy.graph, *copy.registry) { + capacity = copy.capacity; + values = allocator.allocate(capacity); + for (KeyIt it(*graph); graph->valid(it); graph->next(it)) { + int id = graph->id(it); + allocator.construct(&(values[id]), copy.values[id]); + } + } - virtual ~Map() { - destroy(); - delete[] reinterpret_cast(values); - values = 0; - capacity = 0; - } + virtual ~Map() { + destroy(); + allocator.deallocate(values, capacity); + } - Value& operator[](const K& key) { - int id = graph->id(key); - return values[id]; - } + Value& operator[](const Key& key) { + int id = graph->id(key); + return values[id]; + } - const Value& operator[](const K& key) const { - int id = graph->id(key); - return values[id]; - } + const Value& operator[](const Key& key) const { + int id = graph->id(key); + return values[id]; + } - const Value& get(const K& key) const { - int id = graph->id(key); - return values[id]; - } + const Value& get(const Key& key) const { + int id = graph->id(key); + return values[id]; + } - void set(const K& key, const Value& val) { - int id = graph->id(key); - values[id] = val; - } + void set(const Key& key, const Value& val) { + int id = graph->id(key); + values[id] = val; + } - void add(const K& key) { - cerr << capacity << endl; - int id = graph->id(key); - if (id >= capacity) { - int new_capacity = (capacity == 0 ? 1 : capacity); - while (new_capacity <= id) { - new_capacity <<= 1; - } - Value* new_values = reinterpret_cast(new char[new_capacity*sizeof(Value)]);; - for (KeyIt it(*graph); graph->valid(it); graph->next(it)) { - int jd = graph->id(it); - if (id != jd) { - new(&(new_values[jd])) Value(values[jd]); - } - } - if (capacity != 0) delete[] reinterpret_cast(values); - values = new_values; - capacity = new_capacity; - } - cerr << id << ' ' << capacity << endl; - new(&(values[id])) Value(); - } + void add(const Key& key) { + int id = graph->id(key); + if (id >= capacity) { + int new_capacity = (capacity == 0 ? 1 : capacity); + while (new_capacity <= id) { + new_capacity <<= 1; + } + Value* new_values = allocator.allocate(new_capacity);; + for (KeyIt it(*graph); graph->valid(it); graph->next(it)) { + int jd = graph->id(it); + if (id != jd) { + allocator.construct(&(new_values[jd]), values[jd]); + allocator.destroy(&(values[jd])); + } + } + if (capacity != 0) allocator.deallocate(values, capacity); + values = new_values; + capacity = new_capacity; + } + allocator.construct(&(values[id]), Value()); + } - void erase(const K& key) { - int id = graph->id(key); - values[id].~Value(); - } + void erase(const Key& key) { + int id = graph->id(key); + allocator.destroy(&(values[id])); + } - private: - int capacity; - Value* values; - - }; - - }; + private: + int capacity; + Value* values; + Allocator allocator; + }; + }; } #endif diff -r b387504959a2 -r 7733d18de0e8 src/work/deba/main.cpp --- a/src/work/deba/main.cpp Wed Jun 02 09:47:10 2004 +0000 +++ b/src/work/deba/main.cpp Fri Jun 04 11:52:53 2004 +0000 @@ -11,7 +11,7 @@ for (int i = 0; i < 10; ++i) { ListGraph::Node node = g.addNode(); } - ListGraph::NodeMapFactory::Map map(g, g.node_maps); + ListGraph::NodeMap map(g); for (int i = 0; i < 10; ++i) { ListGraph::Node node = g.addNode(); map[node] = rand()%100; diff -r b387504959a2 -r 7733d18de0e8 src/work/deba/map_defines.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/work/deba/map_defines.h Fri Jun 04 11:52:53 2004 +0000 @@ -0,0 +1,47 @@ +#ifndef MAP_DEFINES_H +#define MAP_DEFINES_H + +#define CREATE_EDGE_MAP_REGISTRY \ +typedef MapRegistry EdgeMapRegistry; \ +EdgeMapRegistry edge_maps; + +#define CREATE_NODE_MAP_REGISTRY \ +typedef MapRegistry NodeMapRegistry; \ +NodeMapRegistry node_maps; + +#define CREATE_MAP_REGISTRIES \ +CREATE_NODE_MAP_REGISTRY \ +CREATE_EDGE_MAP_REGISTRY + +#define CREATE_NODE_MAP_FACTORY(TemplateFactory) \ +typedef TemplateFactory NodeMapFactory; + +#define CREATE_EDGE_MAP_FACTORY(TemplateFactory) \ +typedef TemplateFactory EdgeMapFactory; + +#define CREATE_MAP_FACTORIES(TemplateFactory) \ +CREATE_NODE_MAP_FACTORY(TemplateFactory) \ +CREATE_EDGE_MAP_FACTORY(TemplateFactory) + +#define IMPORT_NODE_MAP(Factory) \ +template \ +class NodeMap : public Factory::Map { \ +public: \ +NodeMap() {} \ +NodeMap(Graph& g) : Factory::Map(g, g.node_maps) {} \ +}; + +#define IMPORT_EDGE_MAP(Factory) \ +template \ +class EdgeMap : public Factory::Map { \ +public: \ +EdgeMap() {} \ +EdgeMap(Graph& g) : Factory::Map(g, g.edge_maps) {} \ +}; + +#define CREATE_MAPS(TemplateFactory) \ +CREATE_MAP_FACTORIES(TemplateFactory) \ +IMPORT_NODE_MAP(NodeMapFactory) \ +IMPORT_EDGE_MAP(EdgeMapFactory) + +#endif diff -r b387504959a2 -r 7733d18de0e8 src/work/deba/test_graph.h --- a/src/work/deba/test_graph.h Wed Jun 02 09:47:10 2004 +0000 +++ b/src/work/deba/test_graph.h Fri Jun 04 11:52:53 2004 +0000 @@ -7,7 +7,10 @@ #include "invalid.h" -#include "vector_map_factory.h" +#include "map_registry.h" +#include "array_map_factory.h" + +#include "map_defines.h" namespace hugo { @@ -30,27 +33,12 @@ class InEdgeIt; class SymEdgeIt; - // template class NodeMap; - // template class EdgeMap; + typedef ListGraph Graph; + + CREATE_MAP_REGISTRIES + CREATE_MAPS(ArrayMapFactory) + private: - // template friend class NodeMap; - // template friend class EdgeMap; - - private: - - - public: - - typedef MapRegistry NodeMapRegistry; - typedef VectorMapFactory NodeMapFactory; - NodeMapRegistry node_maps; - - - - typedef MapRegistry EdgeMapRegistry; - typedef VectorMapFactory EdgeMapFactory; - EdgeMapRegistry edge_maps; - int node_id; int edge_id;