| 1 | #ifndef EDGE_MAP_BASE_H |
|---|
| 2 | #define EDGE_MAP_BASE_H |
|---|
| 3 | |
|---|
| 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> |
|---|
| 13 | class EdgeMapBase { |
|---|
| 14 | |
|---|
| 15 | #include "edge_map_registry.h" |
|---|
| 16 | |
|---|
| 17 | public: |
|---|
| 18 | typedef G Graph; |
|---|
| 19 | friend class EdgeMapRegistry<G, K>; |
|---|
| 20 | |
|---|
| 21 | typedef K KeyType; |
|---|
| 22 | |
|---|
| 23 | /** |
|---|
| 24 | Default constructor. |
|---|
| 25 | */ |
|---|
| 26 | |
|---|
| 27 | EdgeMapBase() : graph(0) {} |
|---|
| 28 | |
|---|
| 29 | /** |
|---|
| 30 | Simple constructor to register into a graph. |
|---|
| 31 | */ |
|---|
| 32 | |
|---|
| 33 | EdgeMapBase(Graph& g) : graph(&g) { |
|---|
| 34 | graph->edge_maps.add(*this); |
|---|
| 35 | } |
|---|
| 36 | |
|---|
| 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 | |
|---|
| 67 | virtual ~EdgeMapBase() { |
|---|
| 68 | if (graph) { |
|---|
| 69 | graph.edge_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::EdgeIt 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::EdgeIt 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 edge. |
|---|
| 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 edge 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 |
|---|