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