Changeset 340:a2ce3c4780b7 in lemon-0.x for src
- Timestamp:
- 04/16/04 15:42:03 (20 years ago)
- Branch:
- default
- Phase:
- public
- Convert:
- svn:c9d7d8f5-90d6-0310-b91f-818b3a526b0e/lemon/trunk@459
- Location:
- src/work/deba
- Files:
-
- 1 added
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
src/work/deba/edge_map_base.h
r336 r340 2 2 #define EDGE_MAP_BASE_H 3 3 4 template <class G, class K> 4 /** 5 Template base class for implementing mapping on edges. 6 \param The first template parameter is the Graph class. The Graph 7 must have an \emp edge_maps member with \emp EdgeMapRegistry class. 8 \param The second template parameter is the Edge type of the class. 9 10 */ 11 12 template <typename G, typename K> 5 13 class EdgeMapBase { 6 14 public: … … 9 17 typedef K KeyType; 10 18 19 /** 20 Default constructor. 21 */ 22 11 23 EdgeMapBase() : graph(0) {} 24 25 /** 26 Simple constructor to register into a graph. 27 */ 28 12 29 EdgeMapBase(Graph& g) : graph(&g) { 13 30 graph->edge_maps.add(*this); 14 31 } 32 33 /** 34 Copy constructor with registering into the map. 35 */ 36 37 EdgeMapBase(const EdgeMapBase& copy) : graph(copy.graph) { 38 if (graph) { 39 graph->edge_maps.add(*this); 40 } 41 } 42 43 /** 44 Assign operator. 45 */ 46 47 const EdgeMapBase& operator=(const EdgeMapBase& copy) { 48 if (graph) { 49 graph.edge_maps.erase(*this); 50 } 51 graph = copy.graph; 52 if (graph) { 53 graph->edge_maps.add(*this); 54 } 55 56 } 57 58 59 /** 60 Destructor. 61 */ 15 62 16 63 virtual ~EdgeMapBase() { … … 26 73 int graph_index; 27 74 75 /** 76 Helper function to implement the default constructor in the subclasses. 77 */ 78 28 79 void init() { 29 80 for (Graph::EdgeIt it(g); g.valid(it); g.next(it)) { … … 31 82 } 32 83 } 84 85 /** 86 Helper function to implement the destructor in the subclasses. 87 */ 33 88 34 89 void destroy() { … … 38 93 } 39 94 95 /** 96 The add member function should be overloaded in the subclasses. 97 \e Add extends the map with the new edge. 98 */ 99 40 100 virtual void add(const KeyType&) = 0; 101 102 /** 103 The erase member function should be overloaded in the subclasses. 104 \e Erase removes the edge from the map. 105 */ 106 41 107 virtual void erase(const KeyType&) = 0; 108 109 /** 110 Exception class to throw at unsupported operation. 111 */ 112 113 class NotSupportedOperationException {}; 42 114 43 115 friend class Graph; -
src/work/deba/edge_map_registry.h
r337 r340 3 3 4 4 #include <vector> 5 6 #include "edge_map_base.h" 5 7 6 8 template <typename G, typename E> … … 10 12 typedef E Edge 11 13 12 typedef EdgeMapBase<Graph, Edge> EdgeMapBase;14 typedef EdgeMapBase<Graph, Edge> MapBase; 13 15 14 16 protected: … … 17 19 Container container; 18 20 19 void add( EdgeMapBase& map_base) {21 void add(MapBase& map_base) { 20 22 if (map_base.graph) { 21 23 map_base.graph->edge_maps.erase(map_base); … … 26 28 } 27 29 28 void erase( EdgeMapBase& map_base) {30 void erase(MapBase& map_base) { 29 31 if (map_base.graph != this) return; 30 32 container.back()->graph_index = map_base.graph_index; … … 34 36 } 35 37 36 void add Edge(Edge& edge) {38 void add(Edge& edge) { 37 39 typename Container::iterator it; 38 40 for (it = container.begin(); it != container.end(); ++it) { … … 41 43 } 42 44 43 void erase Edge(Edge& edge) {45 void erase(Edge& edge) { 44 46 typename Container::iterator it; 45 47 for (it = container.begin(); it != container.end(); ++it) { … … 48 50 } 49 51 50 friend class EdgeMapBase;52 friend class MapBase; 51 53 }; 52 54 -
src/work/deba/node_map_base.h
r336 r340 2 2 #define NODE_MAP_BASE_H 3 3 4 template <class G, class K> 4 /** 5 Template base class for implementing mapping on nodes. 6 \param The first template parameter is the Graph class. The Graph 7 must have an \emp node_maps member with \emp NodeMapRegistry class. 8 \param The second template parameter is the Node type of the class. 9 10 */ 11 12 template <typename G, typename K> 5 13 class NodeMapBase { 6 14 public: … … 9 17 typedef K KeyType; 10 18 19 /** 20 Default constructor. 21 */ 22 11 23 NodeMapBase() : graph(0) {} 24 25 /** 26 Simple constructor to register into a graph. 27 */ 28 12 29 NodeMapBase(Graph& g) : graph(&g) { 13 30 graph->node_maps.add(*this); 14 31 } 32 33 /** 34 Copy constructor with registering into the map. 35 */ 36 37 NodeMapBase(const NodeMapBase& copy) : graph(copy.graph) { 38 if (graph) { 39 graph->node_maps.add(*this); 40 } 41 } 42 43 /** 44 Assign operator. 45 */ 46 47 const NodeMapBase& operator=(const NodeMapBase& copy) { 48 if (graph) { 49 graph.node_maps.erase(*this); 50 } 51 graph = copy.graph; 52 if (graph) { 53 graph->node_maps.add(*this); 54 } 55 56 } 57 58 59 /** 60 Destructor. 61 */ 15 62 16 63 virtual ~NodeMapBase() { … … 26 73 int graph_index; 27 74 75 /** 76 Helper function to implement the default constructor in the subclasses. 77 */ 78 28 79 void init() { 29 80 for (Graph::NodeIt it(g); g.valid(it); g.next(it)) { … … 31 82 } 32 83 } 84 85 /** 86 Helper function to implement the destructor in the subclasses. 87 */ 33 88 34 89 void destroy() { … … 38 93 } 39 94 95 /** 96 The add member function should be overloaded in the subclasses. 97 \e Add extends the map with the new node. 98 */ 99 40 100 virtual void add(const KeyType&) = 0; 101 102 /** 103 The erase member function should be overloaded in the subclasses. 104 \e Erase removes the node from the map. 105 */ 106 41 107 virtual void erase(const KeyType&) = 0; 108 109 /** 110 Exception class to throw at unsupported operation. 111 */ 112 113 class NotSupportedOperationException {}; 42 114 43 115 friend class Graph; -
src/work/deba/node_map_registry.h
r337 r340 3 3 4 4 #include <vector> 5 6 #include "node_map_base.h" 5 7 6 8 template <typename G, typename E> … … 34 36 } 35 37 36 void add Node(Node& node) {38 void add(Node& node) { 37 39 typename Container::iterator it; 38 40 for (it = container.begin(); it != container.end(); ++it) { … … 41 43 } 42 44 43 void erase Node(Node& node) {45 void erase(Node& node) { 44 46 typename Container::iterator it; 45 47 for (it = container.begin(); it != container.end(); ++it) { -
src/work/deba/test_graph.h
r262 r340 7 7 8 8 #include <invalid.h> 9 10 #include "vector_map.h" 11 #include "edge_map_registry.h" 12 #include "node_map_registry.h" 13 #include "edge_map_base.h" 14 #include "node_map_base.h" 9 15 10 16 namespace hugo { … … 29 35 class SymEdgeIt; 30 36 31 template <typename T> class NodeMap;32 template <typename T> class EdgeMap;37 // template <typename T> class NodeMap; 38 // template <typename T> class EdgeMap; 33 39 private: 34 template <typename T> friend class NodeMap;35 template <typename T> friend class EdgeMap;40 // template <typename T> friend class NodeMap; 41 // template <typename T> friend class EdgeMap; 36 42 43 NodeMapRegistry<ListGraph, Node> node_maps; 44 37 45 template <typename T> 38 class NodeMap { 39 const ListGraph& G; 40 std::vector<T> container; 41 public: 42 typedef T ValueType; 43 typedef Node KeyType; 44 NodeMap(const ListGraph& _G) : G(_G), container(G.node_id) { } 45 NodeMap(const ListGraph& _G, T a) : 46 G(_G), container(G.node_id, a) { } 47 void set(Node n, T a) { container[/*G.id(n)*/n.node->id]=a; } 48 T get(Node n) const { return container[/*G.id(n)*/n.node->id]; } 49 typename std::vector<T>::reference operator[](Node n) { 50 return container[/*G.id(n)*/n.node->id]; } 51 typename std::vector<T>::const_reference operator[](Node n) const { 52 return container[/*G.id(n)*/n.node->id]; 53 } 54 void update() { container.resize(G.node_id); } 55 void update(T a) { container.resize(G.node_id, a); } 56 }; 46 class NodeMap : public VectorMap<ListGraph, Edge, T, EdgeMapBase> {}; 47 48 EdgeMapRegistry<ListGraph, Edge> edge_maps; 57 49 58 50 template <typename T> 59 class EdgeMap { 60 const ListGraph& G; 61 std::vector<T> container; 62 public: 63 typedef T ValueType; 64 typedef Edge KeyType; 65 EdgeMap(const ListGraph& _G) : G(_G), container(G.edge_id) { } 66 EdgeMap(const ListGraph& _G, T a) : 67 G(_G), container(G.edge_id, a) { } 68 void set(Edge e, T a) { container[/*G.id(e)*/e.edge->id]=a; } 69 T get(Edge e) const { return container[/*G.id(e)*/e.edge->id]; } 70 typename std::vector<T>::reference operator[](Edge e) { 71 return container[/*G.id(e)*/e.edge->id]; } 72 typename std::vector<T>::const_reference operator[](Edge e) const { 73 return container[/*G.id(e)*/e.edge->id]; 74 } 75 void update() { container.resize(G.edge_id); } 76 void update(T a) { container.resize(G.edge_id, a); } 77 }; 51 class EdgeMap : public VectorMap<Graph, Node, T, NodeMapBase> {}; 52 78 53 79 54 int node_id; … … 324 299 /* adding nodes and edges */ 325 300 326 Node addNode() { return Node(_add_node()); } 301 Node addNode() { 302 Node n = _add_node(); 303 node_maps.add(n); 304 return n; 305 } 327 306 Edge addEdge(Node u, Node v) { 328 return Edge(_add_edge(u.node, v.node)); 307 Edge e = _add_edge(u.node, v.node); 308 edge_maps.add(e); 309 return e; 329 310 } 330 311 331 312 void erase(Node i) { 313 node_map.erase(i); 332 314 while (first<OutEdgeIt>(i).valid()) erase(first<OutEdgeIt>(i)); 333 315 while (first<InEdgeIt>(i).valid()) erase(first<InEdgeIt>(i)); … … 335 317 } 336 318 337 void erase(Edge e) { _delete_edge(e.edge); } 319 void erase(Edge e) { 320 edge_maps.erase(e); 321 _delete_edge(e.edge); 322 } 338 323 339 324 void clear() {
Note: See TracChangeset
for help on using the changeset viewer.