# HG changeset patch # User deba # Date 1083917910 0 # Node ID 9632ea8be6cac834339d8a3aec727ff55bfd9d9b # Parent eec0a62979c973ebdb6ec819b9bf54c09f5787bb diff -r eec0a62979c9 -r 9632ea8be6ca src/work/deba/main.cpp --- a/src/work/deba/main.cpp Fri May 07 08:02:17 2004 +0000 +++ b/src/work/deba/main.cpp Fri May 07 08:18:30 2004 +0000 @@ -7,7 +7,7 @@ int main() { ListGraph g; - ListGraph::NodeMap map(g); + ListGraph::NodeMapFactory::VectorMap map(g, g.node_maps); ListGraph::Node node = g.addNode(); map[node] = 12; return 0; diff -r eec0a62979c9 -r 9632ea8be6ca src/work/deba/map_base.h --- a/src/work/deba/map_base.h Fri May 07 08:02:17 2004 +0000 +++ b/src/work/deba/map_base.h Fri May 07 08:18:30 2004 +0000 @@ -39,17 +39,17 @@ Simple constructor to register into a graph registry. */ - MapBase(Registry& r) : registry(0) { - registry->add(*this); + MapBase(Graph& g, Registry& r) : graph(&g), registry(0) { + registry->attach(*this); } /** Copy constructor with registering into the map. */ - MapBase(const MapBase& copy) : registry(0) { - if (registry) { - registry->add(*this); + MapBase(const MapBase& copy) : registry(0), graph(copy.graph) { + if (copy.registry) { + copy.registry->attach(*this); } } @@ -59,11 +59,11 @@ const MapBase& operator=(const MapBase& copy) { if (registry) { - registry->erase(*this); + registry->detach(*this); } - registry = copy.registry; - if (registry) { - registry->add(*this); + graph = copy.graph; + if (copy.registry) { + copy.registry->attach(*this); } } @@ -74,13 +74,14 @@ virtual ~MapBase() { if (registry) { - registry->erase(*this); + registry->detach(*this); } } protected: Registry* registry; + Graph* graph; int registry_index; @@ -88,9 +89,9 @@ Helper function to implement the default constructor in the subclasses. */ - virtual void init(Graph& g) { + virtual void init() { - for (KeyIt it(g); g.valid(it); g.next(it)) { + for (KeyIt it(*graph); graph->valid(it); graph->next(it)) { add(it); } } @@ -99,8 +100,8 @@ Helper function to implement the destructor in the subclasses. */ - virtual void destroy(Graph& g) { - for (KeyIt it(g); g.valid(it); g.next(it)) { + virtual void destroy() { + for (KeyIt it(*graph); graph->valid(it); graph->next(it)) { erase(it); } } diff -r eec0a62979c9 -r 9632ea8be6ca src/work/deba/map_registry.h --- a/src/work/deba/map_registry.h Fri May 07 08:02:17 2004 +0000 +++ b/src/work/deba/map_registry.h Fri May 07 08:18:30 2004 +0000 @@ -28,43 +28,50 @@ typedef std::vector Container; Container container; - Graph* graph; - public: - MapRegistry(Graph& g) : container(0), graph(&g) {} + MapRegistry() {} + + MapRegistry(const MapRegistry&) {} + + MapRegistry& operator=(const MapRegistry&) { + for (it = container.begin(); it != container.end(); ++it) { + (*it)->destroy(); + (*it)->graph = 0; + (*it)->registry = 0; + } + } ~MapRegistry() { typename Container::iterator it; for (it = container.begin(); it != container.end(); ++it) { - (*it)->destroy(*graph); + (*it)->destroy(); (*it)->registry = 0; + (*it)->graph = 0; } } - private: - MapRegistry(const MapRegistry& ) {} - MapRegistry& operator=(const MapRegistry& ) {} public: - void add(Map& map) { + void attach(Map& map) { if (map.registry) { - map.registry->erase(map); + map.registry->detach(map); } container.push_back(&map); map.registry = this; map.registry_index = container.size()-1; - map.init(*graph); + map.init(); } - void erase(Map& map_base) { - map_base.destroy(*graph); + void detach(Map& map_base) { + map_base.destroy(); container.back()->registry_index = map_base.registry_index; container[map_base.registry_index] = container.back(); container.pop_back(); map_base.registry = 0; + map_base.graph = 0; } @@ -82,11 +89,6 @@ } } - Graph& getGraph() { - return *graph; - } - - }; } diff -r eec0a62979c9 -r 9632ea8be6ca src/work/deba/test_graph.h --- a/src/work/deba/test_graph.h Fri May 07 08:02:17 2004 +0000 +++ b/src/work/deba/test_graph.h Fri May 07 08:18:30 2004 +0000 @@ -7,7 +7,7 @@ #include "invalid.h" -#include "vector_map.h" +#include "vector_map_factory.h" namespace hugo { @@ -38,27 +38,19 @@ private: + + public: + typedef MapRegistry NodeMapRegistry; NodeMapRegistry node_maps; + typedef MapRegistry EdgeMapRegistry; EdgeMapRegistry edge_maps; + + typedef VectorMapFactory EdgeMapFactory; + typedef VectorMapFactory NodeMapFactory; - public: - - - template - class NodeMap : public VectorMap { - public: - NodeMap(ListGraph& g) : VectorMap(g.node_maps) {} - }; - - template - class EdgeMap : public VectorMap { - public: - EdgeMap(ListGraph& g) : VectorMap(g.edge_maps) {} - }; - int node_id; int edge_id; @@ -215,8 +207,7 @@ /* default constructor */ - ListGraph() : node_id(0), edge_id(0), _node_num(0), _edge_num(0), _first_node(0), _last_node(0), - edge_maps(*this), node_maps(*this) { } + ListGraph() : node_id(0), edge_id(0), _node_num(0), _edge_num(0), _first_node(0), _last_node(0){ } ~ListGraph() { while (first().valid()) erase(first()); diff -r eec0a62979c9 -r 9632ea8be6ca src/work/deba/vector_map.h --- a/src/work/deba/vector_map.h Fri May 07 08:02:17 2004 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,58 +0,0 @@ -#ifndef VECTOR_MAP_H -#define VECTOR_MAP_H - -#include -#include - -#include "map_base.h" - -namespace hugo { - - template - class VectorMap : public MapBase { - public: - typedef V ValueType; - - VectorMap() {} - VectorMap(typename MapBase::Registry& r) : MapBase(r) {} - - - ValueType& operator[](const K& key) { - int id = registry->getGraph().id(key); - return container[id]; - } - - const ValueType& operator[](const K& key) const { - int id = registry->getGraph().id(key); - return container[id]; - } - - const ValueType& get(const K& key) const { - int id = registry->getGraph().id(key); - return container[id]; - } - - void set(const K& key, const ValueType& val) { - int id = registry->getGraph().id(key); - container[id] = val; - } - - void add(const K& key) { - int id = registry->getGraph().id(key); - std::cerr << id << std::endl; - if (id >= container.size()) { - container.resize(id + 1); - } - } - - void erase(const K& key) {} - - private: - typedef std::vector Container; - - Container container; - }; - -} - -#endif diff -r eec0a62979c9 -r 9632ea8be6ca src/work/deba/vector_map_factory.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/work/deba/vector_map_factory.h Fri May 07 08:18:30 2004 +0000 @@ -0,0 +1,71 @@ +#ifndef VECTOR_MAP_H +#define VECTOR_MAP_H + +#include +#include + +#include "map_base.h" + +namespace hugo { + + template + class VectorMapFactory { + + + public: + + typedef G Graph; + typedef K Key; + typedef KIt KeyIt; + + template + class VectorMap : public MapBase { + public: + typedef V ValueType; + + VectorMap() {} + + VectorMap(Graph& g, MapRegistry& r) + : MapBase(g, r) {} + + + ValueType& operator[](const K& key) { + int id = graph->id(key); + return container[id]; + } + + const ValueType& operator[](const K& key) const { + int id = graph->id(key); + return container[id]; + } + + const ValueType& get(const K& key) const { + int id = graph->id(key); + return container[id]; + } + + void set(const K& key, const ValueType& val) { + int id = graph->id(key); + container[id] = val; + } + + void add(const K& key) { + int id = graph->id(key); + std::cerr << id << std::endl; + if (id >= container.size()) { + container.resize(id + 1); + } + } + + void erase(const K& key) {} + + private: + typedef std::vector Container; + + Container container; + }; + + }; +} + +#endif