1 | // -*- c++ -*- |
---|
2 | #ifndef HUGO_SYM_MAP_H |
---|
3 | #define HUGO_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 | |
---|
20 | |
---|
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) { |
---|
34 | while ( (EdgeIt::n & 1) && EdgeIt::n != -1) { |
---|
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) { |
---|
48 | while ( (EdgeIt::n & 1) && EdgeIt::n != -1) { |
---|
49 | EdgeIt::operator++(); |
---|
50 | } |
---|
51 | } |
---|
52 | |
---|
53 | /** Increase operator. |
---|
54 | */ |
---|
55 | SymEdgeIt& operator++() { |
---|
56 | EdgeIt::operator++(); |
---|
57 | while ( (EdgeIt::n & 1) && EdgeIt::n != -1) { |
---|
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 | /** Graph and Registry initialized map constructor. |
---|
89 | */ |
---|
90 | SymMap(const Graph& g, MapRegistry& r) : MapImpl(g, r) {} |
---|
91 | |
---|
92 | /** Constructor to use default value to initialize the map. |
---|
93 | */ |
---|
94 | SymMap(const Graph& g, MapRegistry& r, const Value& v) |
---|
95 | : MapImpl(g, r, v) {} |
---|
96 | |
---|
97 | /** Constructor to copy a map of the same map type. |
---|
98 | */ |
---|
99 | SymMap(const SymMap& copy) |
---|
100 | : MapImpl(static_cast<const MapImpl&>(copy)) {} |
---|
101 | |
---|
102 | /** Assign operator to copy a map of the same map type. |
---|
103 | */ |
---|
104 | SymMap& operator=(const SymMap& copy) { |
---|
105 | MapImpl::operator=(static_cast<const MapImpl&>(copy)); |
---|
106 | return *this; |
---|
107 | } |
---|
108 | |
---|
109 | /** Add a new key to the map. It called by the map registry. |
---|
110 | */ |
---|
111 | void add(const KeyType& key) { |
---|
112 | int id = MapImpl::getGraph()->id(key); |
---|
113 | if (id & 1) return; |
---|
114 | MapImpl::add(key); |
---|
115 | } |
---|
116 | |
---|
117 | /** Erase a key from the map. It called by the map registry. |
---|
118 | */ |
---|
119 | void erase(const KeyType& key) { |
---|
120 | int id = MapImpl::getGraph()->id(key); |
---|
121 | if (id & 1) return; |
---|
122 | MapImpl::add(key); |
---|
123 | } |
---|
124 | }; |
---|
125 | |
---|
126 | /// @} |
---|
127 | } |
---|
128 | |
---|
129 | #endif |
---|