author | marci |
Wed, 21 Apr 2004 14:59:43 +0000 | |
changeset 359 | 8cc53a6b1e61 |
parent 336 | 8ff3b3e05478 |
child 377 | 33fe0ee01dc5 |
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@262 | 14 |
public: |
deba@262 | 15 |
typedef G Graph; |
deba@262 | 16 |
|
deba@262 | 17 |
typedef K KeyType; |
deba@262 | 18 |
|
deba@340 | 19 |
/** |
deba@340 | 20 |
Default constructor. |
deba@340 | 21 |
*/ |
deba@340 | 22 |
|
deba@336 | 23 |
NodeMapBase() : graph(0) {} |
deba@340 | 24 |
|
deba@340 | 25 |
/** |
deba@340 | 26 |
Simple constructor to register into a graph. |
deba@340 | 27 |
*/ |
deba@340 | 28 |
|
deba@336 | 29 |
NodeMapBase(Graph& g) : graph(&g) { |
deba@336 | 30 |
graph->node_maps.add(*this); |
deba@336 | 31 |
} |
deba@262 | 32 |
|
deba@340 | 33 |
/** |
deba@340 | 34 |
Copy constructor with registering into the map. |
deba@340 | 35 |
*/ |
deba@340 | 36 |
|
deba@340 | 37 |
NodeMapBase(const NodeMapBase& copy) : graph(copy.graph) { |
deba@340 | 38 |
if (graph) { |
deba@340 | 39 |
graph->node_maps.add(*this); |
deba@340 | 40 |
} |
deba@340 | 41 |
} |
deba@340 | 42 |
|
deba@340 | 43 |
/** |
deba@340 | 44 |
Assign operator. |
deba@340 | 45 |
*/ |
deba@340 | 46 |
|
deba@340 | 47 |
const NodeMapBase& operator=(const NodeMapBase& copy) { |
deba@340 | 48 |
if (graph) { |
deba@340 | 49 |
graph.node_maps.erase(*this); |
deba@340 | 50 |
} |
deba@340 | 51 |
graph = copy.graph; |
deba@340 | 52 |
if (graph) { |
deba@340 | 53 |
graph->node_maps.add(*this); |
deba@340 | 54 |
} |
deba@340 | 55 |
|
deba@340 | 56 |
} |
deba@340 | 57 |
|
deba@340 | 58 |
|
deba@340 | 59 |
/** |
deba@340 | 60 |
Destructor. |
deba@340 | 61 |
*/ |
deba@340 | 62 |
|
deba@336 | 63 |
virtual ~NodeMapBase() { |
deba@336 | 64 |
if (graph) { |
deba@336 | 65 |
graph.node_maps.erase(*this); |
deba@336 | 66 |
} |
deba@336 | 67 |
} |
deba@336 | 68 |
|
deba@262 | 69 |
protected: |
deba@262 | 70 |
|
deba@262 | 71 |
Graph* graph; |
deba@262 | 72 |
|
deba@262 | 73 |
int graph_index; |
deba@262 | 74 |
|
deba@340 | 75 |
/** |
deba@340 | 76 |
Helper function to implement the default constructor in the subclasses. |
deba@340 | 77 |
*/ |
deba@340 | 78 |
|
deba@336 | 79 |
void init() { |
deba@336 | 80 |
for (Graph::NodeIt it(g); g.valid(it); g.next(it)) { |
deba@336 | 81 |
add(it); |
deba@336 | 82 |
} |
deba@336 | 83 |
} |
deba@336 | 84 |
|
deba@340 | 85 |
/** |
deba@340 | 86 |
Helper function to implement the destructor in the subclasses. |
deba@340 | 87 |
*/ |
deba@340 | 88 |
|
deba@336 | 89 |
void destroy() { |
deba@336 | 90 |
for (Graph::NodeIt it(g); g.valid(it); g.next(it)) { |
deba@336 | 91 |
erase(it); |
deba@336 | 92 |
} |
deba@336 | 93 |
} |
deba@262 | 94 |
|
deba@340 | 95 |
/** |
deba@340 | 96 |
The add member function should be overloaded in the subclasses. |
deba@340 | 97 |
\e Add extends the map with the new node. |
deba@340 | 98 |
*/ |
deba@340 | 99 |
|
deba@262 | 100 |
virtual void add(const KeyType&) = 0; |
deba@340 | 101 |
|
deba@340 | 102 |
/** |
deba@340 | 103 |
The erase member function should be overloaded in the subclasses. |
deba@340 | 104 |
\e Erase removes the node from the map. |
deba@340 | 105 |
*/ |
deba@340 | 106 |
|
deba@262 | 107 |
virtual void erase(const KeyType&) = 0; |
deba@340 | 108 |
|
deba@340 | 109 |
/** |
deba@340 | 110 |
Exception class to throw at unsupported operation. |
deba@340 | 111 |
*/ |
deba@340 | 112 |
|
deba@340 | 113 |
class NotSupportedOperationException {}; |
deba@262 | 114 |
|
deba@262 | 115 |
friend class Graph; |
deba@262 | 116 |
}; |
deba@262 | 117 |
|
deba@262 | 118 |
#endif |