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