# HG changeset patch # User deba # Date 1084196975 0 # Node ID e10b5e9419efbe69318659f4cdf40df8b18f0a8a # Parent 23a608ba40ab2ce888d56a580c89f45bd4b4f8c4 diff -r 23a608ba40ab -r e10b5e9419ef src/work/deba/main.cpp --- a/src/work/deba/main.cpp Mon May 10 09:12:28 2004 +0000 +++ b/src/work/deba/main.cpp Mon May 10 13:49:35 2004 +0000 @@ -1,4 +1,5 @@ #include +#include #include "test_graph.h" using namespace std; @@ -7,9 +8,17 @@ int main() { ListGraph g; - ListGraph::NodeMapFactory::VectorMap map(g, g.node_maps); - ListGraph::Node node = g.addNode(); - map[node] = 12; + for (int i = 0; i < 3; ++i) { + ListGraph::Node node = g.addNode(); + } + ListGraph::NodeMapFactory::Map map(g, g.node_maps); + for (int i = 0; i < 10; ++i) { + ListGraph::Node node = g.addNode(); + map[node] = rand()%100; + } + for (ListGraph::NodeIt it(g); g.valid(it); g.next(it)) { + cout << map[it] << endl; + } return 0; } diff -r 23a608ba40ab -r e10b5e9419ef src/work/deba/map_base.h --- a/src/work/deba/map_base.h Mon May 10 09:12:28 2004 +0000 +++ b/src/work/deba/map_base.h Mon May 10 13:49:35 2004 +0000 @@ -1,12 +1,14 @@ #ifndef MAP_BASE_H #define MAP_BASE_H +using namespace std; + /** - Template base class for implementing mapping on nodes. - \param The first template parameter is the Graph class. The Graph - must have an \emp node_maps member with \emp MapRegistry class. - \param The second template parameter is the type of the class. - + Template base class for implementing mapping on nodes and edges. + \param The first template parameter is the Graph class. + \param The second template parameter is the key type. + \param The third template parameter is an iterator on + the keys. */ @@ -24,7 +26,7 @@ public: typedef G Graph; typedef MapRegistry Registry; - typedef K KeyType; + typedef K Key; typedef KIt KeyIt; friend class Registry; @@ -33,14 +35,14 @@ Default constructor. */ - MapBase() : registry(0) {} + MapBase() : graph(0), registry(0) {} /** Simple constructor to register into a graph registry. */ MapBase(Graph& g, Registry& r) : graph(&g), registry(0) { - registry->attach(*this); + r.attach(*this); } /** @@ -80,17 +82,16 @@ protected: + Graph* graph; Registry* registry; - Graph* graph; int registry_index; /** - Helper function to implement the default constructor in the subclasses. + Helper function to implement constructors in the subclasses. */ virtual void init() { - for (KeyIt it(*graph); graph->valid(it); graph->next(it)) { add(it); } @@ -111,14 +112,13 @@ \e Add extends the map with the new node. */ - virtual void add(const KeyType&) = 0; - + virtual void add(const Key&) = 0; /** The erase member function should be overloaded in the subclasses. \e Erase removes the node from the map. */ - virtual void erase(const KeyType&) = 0; + virtual void erase(const Key&) = 0; /** Exception class to throw at unsupported operation. diff -r 23a608ba40ab -r e10b5e9419ef src/work/deba/map_registry.h --- a/src/work/deba/map_registry.h Mon May 10 09:12:28 2004 +0000 +++ b/src/work/deba/map_registry.h Mon May 10 13:49:35 2004 +0000 @@ -3,6 +3,8 @@ #include +using namespace std; + namespace hugo { template @@ -62,27 +64,25 @@ container.push_back(&map); map.registry = this; map.registry_index = container.size()-1; - map.init(); } - void detach(Map& map_base) { - map_base.destroy(); - container.back()->registry_index = map_base.registry_index; - container[map_base.registry_index] = container.back(); + void detach(Map& map) { + container.back()->registry_index = map.registry_index; + container[map.registry_index] = container.back(); container.pop_back(); - map_base.registry = 0; - map_base.graph = 0; + map.registry = 0; + map.graph = 0; } - void add(Key& key) { + virtual void add(Key& key) { typename Container::iterator it; for (it = container.begin(); it != container.end(); ++it) { (*it)->add(key); } } - void erase(Key& key) { + virtual void erase(Key& key) { typename Container::iterator it; for (it = container.begin(); it != container.end(); ++it) { (*it)->erase(key); diff -r 23a608ba40ab -r e10b5e9419ef src/work/deba/pac_map_factory.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/work/deba/pac_map_factory.h Mon May 10 13:49:35 2004 +0000 @@ -0,0 +1,73 @@ +#ifndef PAC_MAP_FACTORY_H +#define PAC_MAP_FACTORY_H + +#include "map_base.h" + +/** + Converter class to use the standard template + libary's pair associative containers as a graph map. +*/ + +namespace hugo { + + template class PAC> + class PacMapFactory { + + + public: + + typedef G Graph; + typedef K Key; + typedef KIt KeyIt; + + template + class Map : public MapBase { + public: + typedef V Value; + + Map() {} + + Map(Graph& g, MapRegistry& r) + : MapBase(g, r) { + init(); + } + + virtual ~Map() { + destroy(); + } + + + V& operator[](const K& key) { + return container.find(key)->second; + } + + const V& operator[](const K& key) const { + return container.find(key)->second; + } + + const V& get(const K& key) const { + return container.find(key)->second; + } + + void set(const K& key, const V& value) { + container.find(key)->second = value; + } + + void add(const K& key) { + container.insert(key); + } + + void erase(const K& key) { + container.erase(key); + } + + private: + typedef PAC Container; + + Container container; + }; + + }; +} + +#endif \ No newline at end of file diff -r 23a608ba40ab -r e10b5e9419ef src/work/deba/test_graph.h --- a/src/work/deba/test_graph.h Mon May 10 09:12:28 2004 +0000 +++ b/src/work/deba/test_graph.h Mon May 10 13:49:35 2004 +0000 @@ -41,15 +41,17 @@ public: + typedef MapBase NodeMapBase; typedef MapRegistry NodeMapRegistry; + typedef VectorMapFactory NodeMapFactory; NodeMapRegistry node_maps; - - + + + + typedef MapBase EdgeMapBase; typedef MapRegistry EdgeMapRegistry; + typedef VectorMapFactory EdgeMapFactory; EdgeMapRegistry edge_maps; - - typedef VectorMapFactory EdgeMapFactory; - typedef VectorMapFactory NodeMapFactory; int node_id; diff -r 23a608ba40ab -r e10b5e9419ef src/work/deba/vector_map_factory.h --- a/src/work/deba/vector_map_factory.h Mon May 10 09:12:28 2004 +0000 +++ b/src/work/deba/vector_map_factory.h Mon May 10 13:49:35 2004 +0000 @@ -2,7 +2,6 @@ #define VECTOR_MAP_H #include -#include #include "map_base.h" @@ -19,39 +18,44 @@ typedef KIt KeyIt; template - class VectorMap : public MapBase { + class Map : public MapBase { public: - typedef V ValueType; + typedef V Value; - VectorMap() {} + Map() {} - VectorMap(Graph& g, MapRegistry& r) - : MapBase(g, r) {} + Map(Graph& g, MapRegistry& r) + : MapBase(g, r) { + init(); + } + + virtual ~Map() { + destroy(); + } - ValueType& operator[](const K& key) { + Value& operator[](const K& key) { int id = graph->id(key); return container[id]; } - const ValueType& operator[](const K& key) const { + const Value& operator[](const K& key) const { int id = graph->id(key); return container[id]; } - const ValueType& get(const K& key) const { + const Value& get(const K& key) const { int id = graph->id(key); return container[id]; } - void set(const K& key, const ValueType& val) { + void set(const K& key, const Value& 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); } @@ -60,7 +64,7 @@ void erase(const K& key) {} private: - typedef std::vector Container; + typedef std::vector Container; Container container; };