# HG changeset patch # User deba # Date 1082666181 0 # Node ID c3f93631cd24be9e4b2138820b282a4a4f1548ef # Parent 33fe0ee01dc5c5beb51bea1fd67eed885f388caa diff -r 33fe0ee01dc5 -r c3f93631cd24 src/work/deba/edge_map_base.h --- a/src/work/deba/edge_map_base.h Thu Apr 22 16:36:57 2004 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,121 +0,0 @@ -#ifndef EDGE_MAP_BASE_H -#define EDGE_MAP_BASE_H - -/** - Template base class for implementing mapping on edges. - \param The first template parameter is the Graph class. The Graph - must have an \emp edge_maps member with \emp EdgeMapRegistry class. - \param The second template parameter is the Edge type of the class. - -*/ - -template -class EdgeMapBase { - -#include "edge_map_registry.h" - -public: - typedef G Graph; - friend class EdgeMapRegistry; - - typedef K KeyType; - - /** - Default constructor. - */ - - EdgeMapBase() : graph(0) {} - - /** - Simple constructor to register into a graph. - */ - - EdgeMapBase(Graph& g) : graph(&g) { - graph->edge_maps.add(*this); - } - - /** - Copy constructor with registering into the map. - */ - - EdgeMapBase(const EdgeMapBase& copy) : graph(copy.graph) { - if (graph) { - graph->edge_maps.add(*this); - } - } - - /** - Assign operator. - */ - - const EdgeMapBase& operator=(const EdgeMapBase& copy) { - if (graph) { - graph.edge_maps.erase(*this); - } - graph = copy.graph; - if (graph) { - graph->edge_maps.add(*this); - } - - } - - - /** - Destructor. - */ - - virtual ~EdgeMapBase() { - if (graph) { - graph.edge_maps.erase(*this); - } - } - -protected: - - Graph* graph; - - int graph_index; - - /** - Helper function to implement the default constructor in the subclasses. - */ - - void init() { - for (typename Graph::EdgeIt it(g); g.valid(it); g.next(it)) { - add(it); - } - } - - /** - Helper function to implement the destructor in the subclasses. - */ - - void destroy() { - for (typename Graph::EdgeIt it(g); g.valid(it); g.next(it)) { - erase(it); - } - } - - /** - The add member function should be overloaded in the subclasses. - \e Add extends the map with the new edge. - */ - - virtual void add(const KeyType&) = 0; - - /** - The erase member function should be overloaded in the subclasses. - \e Erase removes the edge from the map. - */ - - virtual void erase(const KeyType&) = 0; - - /** - Exception class to throw at unsupported operation. - */ - - class NotSupportedOperationException {}; - -}; - -#endif diff -r 33fe0ee01dc5 -r c3f93631cd24 src/work/deba/edge_map_registry.h --- a/src/work/deba/edge_map_registry.h Thu Apr 22 16:36:57 2004 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,65 +0,0 @@ -#ifndef EDGE_MAP_REGISTRY_H -#define EDGE_MAP_REGISTRY_H - -#include - -template -class EdgeMapRegistry; - -#include "edge_map_base.h" - -template -class EdgeMapRegistry { -public: - typedef G Graph; - typedef K Edge; - - typedef EdgeMapBase MapBase; - friend class MapBase; - -protected: - - Graph* graph; - - typedef std::vector Container; - - Container container; - -public: - - EdgeMapRegistry(Graph g) : graph(&g) {} - - void add(MapBase& map_base) { - if (map_base.graph) { - map_base.graph->edge_maps.erase(map_base); - } - container.push_back(&map_base); - map_base.graph = graph; - map_base.graph_index = container.size()-1; - } - - void erase(MapBase& map_base) { - container.back()->graph_index = map_base.graph_index; - container[map_base.graph_index] = container.back(); - container.pop_back(); - map_base.graph = 0; - } - - - void add(Edge& edge) { - typename Container::iterator it; - for (it = container.begin(); it != container.end(); ++it) { - (*it)->add(edge); - } - } - - void erase(Edge& edge) { - typename Container::iterator it; - for (it = container.begin(); it != container.end(); ++it) { - (*it)->erase(edge); - } - } - -}; - -#endif diff -r 33fe0ee01dc5 -r c3f93631cd24 src/work/deba/invalid.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/work/deba/invalid.h Thu Apr 22 20:36:21 2004 +0000 @@ -0,0 +1,33 @@ +// -*- mode:C++ -*- + +#ifndef HUGO_INVALID_H +#define HUGO_INVALID_H + +///\file +///\brief Definition of INVALID. + +namespace hugo { + + /// Dummy type to make it easier to make invalid iterators. + + /// See \ref INVALID, how to use it. + + struct Invalid {}; + + /// Invalid iterators. + + /// \ref Invalid is a global type that converts to each iterator + /// in such a way that the value of the target iterator will be invalid. + + // It is also used to convert the \c INVALID constant to the + // node iterator that makes is possible to write + + //extern Invalid INVALID; + + //const Invalid &INVALID = *(Invalid *)0; + const Invalid INVALID = Invalid(); + +}; + +#endif + diff -r 33fe0ee01dc5 -r c3f93631cd24 src/work/deba/main.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/work/deba/main.cpp Thu Apr 22 20:36:21 2004 +0000 @@ -0,0 +1,15 @@ +#include +#include "test_graph.h" + +using namespace std; +using namespace hugo; + + +int main() { + ListGraph g; + ListGraph::NodeMap map(g); + ListGraph::Node node = g.addNode(); + map[node] = 12; + return 0; +} + diff -r 33fe0ee01dc5 -r c3f93631cd24 src/work/deba/map_base.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/work/deba/map_base.h Thu Apr 22 20:36:21 2004 +0000 @@ -0,0 +1,132 @@ +#ifndef MAP_BASE_H +#define MAP_BASE_H + +/** + 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. + +*/ + + +namespace hugo { + template + class MapBase; +} + +#include "map_registry.h" + +namespace hugo { + + template + class MapBase { + public: + typedef G Graph; + typedef MapRegistry Registry; + typedef K KeyType; + typedef KIt KeyIt; + + friend class Registry; + + /** + Default constructor. + */ + + MapBase() : registry(0) {} + + /** + Simple constructor to register into a graph registry. + */ + + MapBase(Registry& r) : registry(0) { + registry->add(*this); + } + + /** + Copy constructor with registering into the map. + */ + + MapBase(const MapBase& copy) : registry(0) { + if (registry) { + registry->add(*this); + } + } + + /** + Assign operator. + */ + + const MapBase& operator=(const MapBase& copy) { + if (registry) { + registry->erase(*this); + } + registry = copy.registry; + if (registry) { + registry->add(*this); + } + } + + + /** + Destructor. + */ + + virtual ~MapBase() { + if (registry) { + registry->erase(*this); + } + } + + protected: + + Registry* registry; + + int registry_index; + + /** + Helper function to implement the default constructor in the subclasses. + */ + + virtual void init(Graph& g) { + + for (KeyIt it(g); g.valid(it); g.next(it)) { + add(it); + } + } + + /** + Helper function to implement the destructor in the subclasses. + */ + + virtual void destroy(Graph& g) { + for (KeyIt it(g); g.valid(it); g.next(it)) { + erase(it); + } + } + + /** + The add member function should be overloaded in the subclasses. + \e Add extends the map with the new node. + */ + + virtual void add(const KeyType&) = 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; + + /** + Exception class to throw at unsupported operation. + */ + + class NotSupportedOperationException {}; + + }; + +} + +#endif diff -r 33fe0ee01dc5 -r c3f93631cd24 src/work/deba/map_registry.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/work/deba/map_registry.h Thu Apr 22 20:36:21 2004 +0000 @@ -0,0 +1,94 @@ +#ifndef MAP_REGISTRY_H +#define MAP_REGISTRY_H + +#include + + +namespace hugo { + template + class MapRegistry; +} + +#include "map_base.h" + +namespace hugo { + + template + class MapRegistry { + public: + typedef G Graph; + typedef K Key; + typedef KIt KeyIt; + + typedef MapBase Map; + friend class Base; + + protected: + + typedef std::vector Container; + Container container; + + Graph* graph; + + + public: + + MapRegistry(Graph& g) : container(0), graph(&g) {} + + ~MapRegistry() { + typename Container::iterator it; + for (it = container.begin(); it != container.end(); ++it) { + (*it)->destroy(*graph); + (*it)->registry = 0; + } + } + + private: + MapRegistry(const MapRegistry& ) {} + MapRegistry& operator=(const MapRegistry& ) {} + + public: + + void add(Map& map) { + if (map.registry) { + map.registry->erase(map); + } + container.push_back(&map); + map.registry = this; + map.registry_index = container.size()-1; + map.init(*graph); + } + + void erase(Map& map_base) { + map_base.destroy(*graph); + container.back()->registry_index = map_base.registry_index; + container[map_base.registry_index] = container.back(); + container.pop_back(); + map_base.registry = 0; + } + + + void add(Key& key) { + typename Container::iterator it; + for (it = container.begin(); it != container.end(); ++it) { + (*it)->add(key); + } + } + + void erase(Key& key) { + typename Container::iterator it; + for (it = container.begin(); it != container.end(); ++it) { + (*it)->erase(key); + } + } + + Graph& getGraph() { + return *graph; + } + + + }; + +} + +#endif diff -r 33fe0ee01dc5 -r c3f93631cd24 src/work/deba/node_map_base.h --- a/src/work/deba/node_map_base.h Thu Apr 22 16:36:57 2004 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,121 +0,0 @@ -#ifndef NODE_MAP_BASE_H -#define NODE_MAP_BASE_H - -/** - 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 NodeMapRegistry class. - \param The second template parameter is the Node type of the class. - -*/ - -template -class NodeMapBase { - -#include "node_map_registry.h" - -public: - typedef G Graph; - friend class NodeMapRegistry; - - typedef K KeyType; - - /** - Default constructor. - */ - - NodeMapBase() : graph(0) {} - - /** - Simple constructor to register into a graph. - */ - - NodeMapBase(Graph& g) : graph(&g) { - graph->node_maps.add(*this); - } - - /** - Copy constructor with registering into the map. - */ - - NodeMapBase(const NodeMapBase& copy) : graph(copy.graph) { - if (graph) { - graph->node_maps.add(*this); - } - } - - /** - Assign operator. - */ - - const NodeMapBase& operator=(const NodeMapBase& copy) { - if (graph) { - graph.node_maps.erase(*this); - } - graph = copy.graph; - if (graph) { - graph->node_maps.add(*this); - } - - } - - - /** - Destructor. - */ - - virtual ~NodeMapBase() { - if (graph) { - graph->node_maps.erase(*this); - } - } - -protected: - - Graph* graph; - - int graph_index; - - /** - Helper function to implement the default constructor in the subclasses. - */ - - void init() { - for (typename Graph::NodeIt it(g); g.valid(it); g.next(it)) { - add(it); - } - } - - /** - Helper function to implement the destructor in the subclasses. - */ - - void destroy() { - for (typename Graph::NodeIt it(g); g.valid(it); g.next(it)) { - erase(it); - } - } - - /** - The add member function should be overloaded in the subclasses. - \e Add extends the map with the new node. - */ - - virtual void add(const KeyType&) = 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; - - /** - Exception class to throw at unsupported operation. - */ - - class NotSupportedOperationException {}; - -}; - -#endif diff -r 33fe0ee01dc5 -r c3f93631cd24 src/work/deba/node_map_registry.h --- a/src/work/deba/node_map_registry.h Thu Apr 22 16:36:57 2004 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,65 +0,0 @@ -#ifndef NODE_MAP_REGISTRY_H -#define NODE_MAP_REGISTRY_H - -#include - -template -class NodeMapRegistry; - -#include "node_map_base.h" - -template -class NodeMapRegistry { -public: - typedef G Graph; - typedef K Node; - - typedef NodeMapBase MapBase; - friend class MapBase; - -protected: - - Graph* graph; - - typedef std::vector Container; - - Container container; - -public: - - NodeMapRegistry(Graph g) : graph(&g) {} - - void add(MapBase& map_base) { - if (map_base.graph) { - map_base.graph->node_maps.erase(map_base); - } - container.push_back(&map_base); - map_base.graph = graph; - map_base.graph_index = container.size()-1; - } - - void erase(MapBase& map_base) { - container.back()->graph_index = map_base.graph_index; - container[map_base.graph_index] = container.back(); - container.pop_back(); - map_base.graph = 0; - } - - - void add(Node& node) { - typename Container::iterator it; - for (it = container.begin(); it != container.end(); ++it) { - (*it)->add(node); - } - } - - void erase(Node& node) { - typename Container::iterator it; - for (it = container.begin(); it != container.end(); ++it) { - (*it)->erase(node); - } - } - -}; - -#endif diff -r 33fe0ee01dc5 -r c3f93631cd24 src/work/deba/test_graph.h --- a/src/work/deba/test_graph.h Thu Apr 22 16:36:57 2004 +0000 +++ b/src/work/deba/test_graph.h Thu Apr 22 20:36:21 2004 +0000 @@ -7,10 +7,6 @@ #include "invalid.h" -#include "edge_map_registry.h" -#include "node_map_registry.h" -#include "edge_map_base.h" -#include "node_map_base.h" #include "vector_map.h" namespace hugo { @@ -42,22 +38,26 @@ private: - NodeMapRegistry node_maps(*this); - EdgeMapRegistry edge_maps(*this); + typedef MapRegistry NodeMapRegistry; + NodeMapRegistry node_maps; + + typedef MapRegistry EdgeMapRegistry; + EdgeMapRegistry edge_maps; public: template - class NodeMap : public VectorMap { + class NodeMap : public VectorMap { public: - NodeMap(ListGraph& g) : VectorMap(g) {} + NodeMap(ListGraph& g) : VectorMap(g.node_maps) {} }; - EdgeMapRegistry edge_maps; - template - class EdgeMap : public VectorMap {}; + class EdgeMap : public VectorMap { + public: + EdgeMap(ListGraph& g) : VectorMap(g.edge_maps) {} + }; int node_id; @@ -215,7 +215,8 @@ /* default constructor */ - ListGraph() : node_id(0), edge_id(0), _node_num(0), _edge_num(0), _first_node(0), _last_node(0) { } + 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() { while (first().valid()) erase(first()); diff -r 33fe0ee01dc5 -r c3f93631cd24 src/work/deba/vector_edge_map.h --- a/src/work/deba/vector_edge_map.h Thu Apr 22 16:36:57 2004 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,29 +0,0 @@ -#ifndef VECTOR_EDGE_MAP_H -#define VECTOR_EDGE_MAP_H - -#include - -#include "edge_map_base.h" - -template -class VectorEdgeMap : public EdgeMapBase{ -public: - typedef V ValueType; - - VectorEdgeMap(Graph& g) : EdgeMapBase(g) {} - - void add(const E& edge) { - if (edge->id >= container.size()) { - container.resize(edge->id); - } - } - - void erase(const E&) {} - -private: - typedef vector Container; - - Container container; -} - -#endif \ No newline at end of file diff -r 33fe0ee01dc5 -r c3f93631cd24 src/work/deba/vector_map.h --- a/src/work/deba/vector_map.h Thu Apr 22 16:36:57 2004 +0000 +++ b/src/work/deba/vector_map.h Thu Apr 22 20:36:21 2004 +0000 @@ -2,49 +2,57 @@ #define VECTOR_MAP_H #include +#include -template class MapBase > -class VectorMap : public MapBase { -public: - typedef V ValueType; +#include "map_base.h" + +namespace hugo { + + template + class VectorMap : public MapBase { + public: + typedef V ValueType; - VectorMap() {} - VectorMap(G& g) : MapBase(g) { - init(); - } + VectorMap() {} + VectorMap(typename MapBase::Registry& r) : MapBase(r) {} - ~VectorMap() { -// destroy(); - } - ValueType& operator[](const K& key) { - return container[key->id]; - } + ValueType& operator[](const K& key) { + int id = registry->getGraph().id(key); + return container[id]; + } - const ValueType& operator[](const K& key) const { - return container[key->id]; - } + const ValueType& operator[](const K& key) const { + int id = registry->getGraph().id(key); + return container[id]; + } - const ValueType& get(const K& key) const { - return container[key->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) { - container[key->id] = val; - } + void set(const K& key, const ValueType& val) { + int id = registry->getGraph().id(key); + container[id] = val; + } - void add(const K& key) { - if (key->id() >= container.size()) { - container.resize(key->id() + 1); + 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) {} + void erase(const K& key) {} -private: - typedef std::vector Container; + private: + typedef std::vector Container; - Container container; -}; + Container container; + }; + +} #endif