author | deba |
Thu, 22 Apr 2004 16:36:57 +0000 | |
changeset 377 | 33fe0ee01dc5 |
parent 340 | a2ce3c4780b7 |
permissions | -rw-r--r-- |
deba@262 | 1 |
#ifndef NODE_MAP_BASE_H |
deba@262 | 2 |
#define NODE_MAP_BASE_H |
deba@262 | 3 |
|
deba@340 | 4 |
/** |
deba@340 | 5 |
Template base class for implementing mapping on nodes. |
deba@340 | 6 |
\param The first template parameter is the Graph class. The Graph |
deba@340 | 7 |
must have an \emp node_maps member with \emp NodeMapRegistry class. |
deba@340 | 8 |
\param The second template parameter is the Node type of the class. |
deba@340 | 9 |
|
deba@340 | 10 |
*/ |
deba@340 | 11 |
|
deba@340 | 12 |
template <typename G, typename K> |
deba@262 | 13 |
class NodeMapBase { |
deba@377 | 14 |
|
deba@377 | 15 |
#include "node_map_registry.h" |
deba@377 | 16 |
|
deba@262 | 17 |
public: |
deba@262 | 18 |
typedef G Graph; |
deba@377 | 19 |
friend class NodeMapRegistry<G, K>; |
deba@262 | 20 |
|
deba@262 | 21 |
typedef K KeyType; |
deba@262 | 22 |
|
deba@340 | 23 |
/** |
deba@340 | 24 |
Default constructor. |
deba@340 | 25 |
*/ |
deba@340 | 26 |
|
deba@336 | 27 |
NodeMapBase() : graph(0) {} |
deba@340 | 28 |
|
deba@340 | 29 |
/** |
deba@340 | 30 |
Simple constructor to register into a graph. |
deba@340 | 31 |
*/ |
deba@340 | 32 |
|
deba@336 | 33 |
NodeMapBase(Graph& g) : graph(&g) { |
deba@336 | 34 |
graph->node_maps.add(*this); |
deba@336 | 35 |
} |
deba@262 | 36 |
|
deba@340 | 37 |
/** |
deba@340 | 38 |
Copy constructor with registering into the map. |
deba@340 | 39 |
*/ |
deba@340 | 40 |
|
deba@340 | 41 |
NodeMapBase(const NodeMapBase& copy) : graph(copy.graph) { |
deba@340 | 42 |
if (graph) { |
deba@340 | 43 |
graph->node_maps.add(*this); |
deba@340 | 44 |
} |
deba@340 | 45 |
} |
deba@340 | 46 |
|
deba@340 | 47 |
/** |
deba@340 | 48 |
Assign operator. |
deba@340 | 49 |
*/ |
deba@340 | 50 |
|
deba@340 | 51 |
const NodeMapBase& operator=(const NodeMapBase& copy) { |
deba@340 | 52 |
if (graph) { |
deba@340 | 53 |
graph.node_maps.erase(*this); |
deba@340 | 54 |
} |
deba@340 | 55 |
graph = copy.graph; |
deba@340 | 56 |
if (graph) { |
deba@340 | 57 |
graph->node_maps.add(*this); |
deba@340 | 58 |
} |
deba@340 | 59 |
|
deba@340 | 60 |
} |
deba@340 | 61 |
|
deba@340 | 62 |
|
deba@340 | 63 |
/** |
deba@340 | 64 |
Destructor. |
deba@340 | 65 |
*/ |
deba@340 | 66 |
|
deba@336 | 67 |
virtual ~NodeMapBase() { |
deba@336 | 68 |
if (graph) { |
deba@377 | 69 |
graph->node_maps.erase(*this); |
deba@336 | 70 |
} |
deba@336 | 71 |
} |
deba@336 | 72 |
|
deba@262 | 73 |
protected: |
deba@262 | 74 |
|
deba@262 | 75 |
Graph* graph; |
deba@262 | 76 |
|
deba@262 | 77 |
int graph_index; |
deba@262 | 78 |
|
deba@340 | 79 |
/** |
deba@340 | 80 |
Helper function to implement the default constructor in the subclasses. |
deba@340 | 81 |
*/ |
deba@340 | 82 |
|
deba@336 | 83 |
void init() { |
deba@377 | 84 |
for (typename Graph::NodeIt it(g); g.valid(it); g.next(it)) { |
deba@336 | 85 |
add(it); |
deba@336 | 86 |
} |
deba@336 | 87 |
} |
deba@336 | 88 |
|
deba@340 | 89 |
/** |
deba@340 | 90 |
Helper function to implement the destructor in the subclasses. |
deba@340 | 91 |
*/ |
deba@340 | 92 |
|
deba@336 | 93 |
void destroy() { |
deba@377 | 94 |
for (typename Graph::NodeIt it(g); g.valid(it); g.next(it)) { |
deba@336 | 95 |
erase(it); |
deba@336 | 96 |
} |
deba@336 | 97 |
} |
deba@262 | 98 |
|
deba@340 | 99 |
/** |
deba@340 | 100 |
The add member function should be overloaded in the subclasses. |
deba@340 | 101 |
\e Add extends the map with the new node. |
deba@340 | 102 |
*/ |
deba@340 | 103 |
|
deba@262 | 104 |
virtual void add(const KeyType&) = 0; |
deba@340 | 105 |
|
deba@340 | 106 |
/** |
deba@340 | 107 |
The erase member function should be overloaded in the subclasses. |
deba@340 | 108 |
\e Erase removes the node from the map. |
deba@340 | 109 |
*/ |
deba@340 | 110 |
|
deba@262 | 111 |
virtual void erase(const KeyType&) = 0; |
deba@340 | 112 |
|
deba@340 | 113 |
/** |
deba@340 | 114 |
Exception class to throw at unsupported operation. |
deba@340 | 115 |
*/ |
deba@340 | 116 |
|
deba@340 | 117 |
class NotSupportedOperationException {}; |
deba@262 | 118 |
|
deba@262 | 119 |
}; |
deba@262 | 120 |
|
deba@262 | 121 |
#endif |