[822] | 1 | // -*- c++ -*- |
---|
| 2 | #ifndef SYM_MAP_H |
---|
| 3 | #define SYM_MAP_H |
---|
| 4 | |
---|
| 5 | ///\ingroup graphmaps |
---|
| 6 | ///\file |
---|
| 7 | ///\brief Graph maps that construates and destruates |
---|
| 8 | ///their elements dynamically. |
---|
| 9 | |
---|
| 10 | namespace hugo { |
---|
| 11 | |
---|
| 12 | /// \addtogroup graphmaps |
---|
| 13 | /// @{ |
---|
| 14 | |
---|
| 15 | /** The SymEdgeIt is wrapper class for the EdgeIt. It can be used to |
---|
| 16 | * iterate on the symmetric maps when all of the edge - reverse edge pair |
---|
| 17 | * has different parity. |
---|
| 18 | */ |
---|
| 19 | |
---|
[844] | 20 | |
---|
[822] | 21 | template <typename Graph, typename Edge, typename EdgeIt> |
---|
| 22 | class SymEdgeIt : public EdgeIt { |
---|
| 23 | public: |
---|
| 24 | |
---|
| 25 | /** Default constructor. |
---|
| 26 | */ |
---|
| 27 | SymEdgeIt() |
---|
| 28 | : EdgeIt() {} |
---|
| 29 | |
---|
| 30 | /** Graph initialized constructor. |
---|
| 31 | */ |
---|
| 32 | SymEdgeIt(const Graph& graph) |
---|
| 33 | : EdgeIt(graph) { |
---|
[844] | 34 | while ( (EdgeIt::n & 1) && EdgeIt::n != -1) { |
---|
[822] | 35 | EdgeIt::operator++(); |
---|
| 36 | } |
---|
| 37 | } |
---|
| 38 | |
---|
| 39 | /** Creating invelid SymEdgeIt. |
---|
| 40 | */ |
---|
| 41 | SymEdgeIt(Invalid invalid) |
---|
| 42 | : EdgeIt(invalid) {} |
---|
| 43 | |
---|
| 44 | /** SymEdgeIt from the given Edge. |
---|
| 45 | */ |
---|
| 46 | SymEdgeIt(const Graph& graph, const Edge& edge) |
---|
| 47 | : EdgeIt(graph, edge) { |
---|
[844] | 48 | while ( (EdgeIt::n & 1) && EdgeIt::n != -1) { |
---|
[822] | 49 | EdgeIt::operator++(); |
---|
| 50 | } |
---|
| 51 | } |
---|
| 52 | |
---|
| 53 | /** Increase operator. |
---|
| 54 | */ |
---|
| 55 | SymEdgeIt& operator++() { |
---|
| 56 | EdgeIt::operator++(); |
---|
[844] | 57 | while ( (EdgeIt::n & 1) && EdgeIt::n != -1) { |
---|
[822] | 58 | EdgeIt::operator++(); |
---|
| 59 | } |
---|
| 60 | return *this; |
---|
| 61 | } |
---|
| 62 | }; |
---|
| 63 | |
---|
| 64 | /** The SymMap template class is graph map structure what |
---|
| 65 | * wraps an other map structure to use as symmetric map structure. |
---|
| 66 | * |
---|
| 67 | * The template parameter is the MapRegistry that the maps |
---|
| 68 | * will belong to and the ValueType. |
---|
| 69 | */ |
---|
| 70 | template <template <typename, typename> class DynMap, |
---|
| 71 | typename MapRegistry, typename Value> |
---|
| 72 | class SymMap : public DynMap<MapRegistry, Value>{ |
---|
| 73 | |
---|
| 74 | private: |
---|
| 75 | |
---|
| 76 | typedef DynMap<MapRegistry, Value> MapImpl; |
---|
| 77 | |
---|
| 78 | public: |
---|
| 79 | |
---|
| 80 | /// The graph type of the maps. |
---|
| 81 | typedef typename MapRegistry::Graph Graph; |
---|
| 82 | |
---|
| 83 | typedef typename MapImpl::KeyType KeyType; |
---|
| 84 | |
---|
| 85 | public: |
---|
| 86 | |
---|
| 87 | |
---|
| 88 | /** Default constructor for the map. |
---|
| 89 | */ |
---|
| 90 | SymMap() : MapImpl() {} |
---|
| 91 | |
---|
| 92 | /** Graph and Registry initialized map constructor. |
---|
| 93 | */ |
---|
| 94 | SymMap(const Graph& g, MapRegistry& r) : MapImpl(g, r) {} |
---|
| 95 | |
---|
| 96 | /** Constructor to use default value to initialize the map. |
---|
| 97 | */ |
---|
| 98 | SymMap(const Graph& g, MapRegistry& r, const Value& v) |
---|
| 99 | : MapImpl(g, r, v) {} |
---|
| 100 | |
---|
| 101 | /** Constructor to copy a map of the same map type. |
---|
| 102 | */ |
---|
| 103 | SymMap(const SymMap& copy) |
---|
| 104 | : MapImpl(static_cast<const MapImpl&>(copy)) {} |
---|
| 105 | |
---|
| 106 | /** Assign operator to copy a map of the same map type. |
---|
| 107 | */ |
---|
| 108 | SymMap& operator=(const SymMap& copy) { |
---|
| 109 | MapImpl::operator=(static_cast<const MapImpl&>(copy)); |
---|
| 110 | return *this; |
---|
| 111 | } |
---|
| 112 | |
---|
| 113 | /** Add a new key to the map. It called by the map registry. |
---|
| 114 | */ |
---|
| 115 | void add(const KeyType& key) { |
---|
| 116 | int id = MapImpl::getGraph()->id(key); |
---|
| 117 | if (id & 1) return; |
---|
| 118 | MapImpl::add(key); |
---|
| 119 | } |
---|
| 120 | |
---|
| 121 | /** Erase a key from the map. It called by the map registry. |
---|
| 122 | */ |
---|
| 123 | void erase(const KeyType& key) { |
---|
| 124 | int id = MapImpl::getGraph()->id(key); |
---|
| 125 | if (id & 1) return; |
---|
| 126 | MapImpl::add(key); |
---|
| 127 | } |
---|
| 128 | }; |
---|
| 129 | |
---|
| 130 | /// @} |
---|
| 131 | } |
---|
| 132 | |
---|
| 133 | #endif |
---|