47 typedef typename _Map::Value Value; |
47 typedef typename _Map::Value Value; |
48 typedef std::map<Value, Key> InverseMap; |
48 typedef std::map<Value, Key> InverseMap; |
49 |
49 |
50 typedef typename _Map::ConstReference ConstReference; |
50 typedef typename _Map::ConstReference ConstReference; |
51 |
51 |
52 /// Constructor. |
52 /// \brief Constructor. |
53 |
53 /// |
54 /// Construct a new InversableMap for the graph. |
54 /// Construct a new InversableMap for the graph. |
55 /// |
55 /// |
56 InversableMap(const Graph& graph) : Map(graph) {} |
56 InversableMap(const Graph& graph) : Map(graph) {} |
57 |
57 |
58 /// The setter function of the map. |
58 /// \brief The setter function of the map. |
59 |
59 /// |
60 /// It sets the map and the inverse map |
60 /// It sets the map and the inverse map to given key-value pair. |
61 void set(const Key& key, const Value& val) { |
61 void set(const Key& key, const Value& val) { |
62 Value oldval = Map::operator[](key); |
62 Value oldval = Map::operator[](key); |
63 typename InverseMap::iterator it = inv_map.find(oldval); |
63 typename InverseMap::iterator it = inv_map.find(oldval); |
64 if (it != inv_map.end() && it->second == key) { |
64 if (it != inv_map.end() && it->second == key) { |
65 inv_map.erase(it); |
65 inv_map.erase(it); |
93 private: |
93 private: |
94 InverseMap inv_map; |
94 InverseMap inv_map; |
95 }; |
95 }; |
96 |
96 |
97 |
97 |
98 // unique, continous, mutable |
98 |
|
99 /// \brief Provides a mutable, continous and unique descriptor for each |
|
100 /// item in the graph. |
|
101 /// |
|
102 /// The DescriptorMap class provides a mutable, continous and immutable |
|
103 /// mapping for each item in the graph. |
|
104 /// |
|
105 /// \param _Graph The graph class the \c DescriptorMap belongs to. |
|
106 /// \param _Item The Item is the Key of the Map. It may be Node, Edge or |
|
107 /// UndirEdge. |
|
108 /// \param _Map A ReadWriteMap mapping from the item type to integer. |
99 |
109 |
100 template < |
110 template < |
101 typename _Graph, |
111 typename _Graph, |
102 typename _Item, |
112 typename _Item, |
103 typename _ItemIt, |
|
104 typename _Map |
113 typename _Map |
105 > |
114 > |
106 class DescriptorMap : protected _Map { |
115 class DescriptorMap : protected _Map { |
|
116 |
|
117 typedef _Item Item; |
|
118 typedef _Map Map; |
|
119 |
107 public: |
120 public: |
|
121 /// The graph class of DescriptorMap. |
108 typedef _Graph Graph; |
122 typedef _Graph Graph; |
109 typedef _Item Item; |
123 |
110 typedef _ItemIt ItemIt; |
124 /// The key type of DescriptorMap (Node, Edge, UndirEdge). |
111 typedef _Map Map; |
|
112 |
|
113 |
|
114 typedef typename _Map::Key Key; |
125 typedef typename _Map::Key Key; |
|
126 /// The value type of DescriptorMap. |
115 typedef typename _Map::Value Value; |
127 typedef typename _Map::Value Value; |
116 |
128 |
117 typedef vector<Item> InverseMap; |
129 typedef std::vector<Item> InverseMap; |
118 |
130 |
|
131 /// \brief Constructor. |
|
132 /// |
|
133 /// Constructor for creating descriptor map. |
119 DescriptorMap(const Graph& _graph) : Map(_graph) { |
134 DescriptorMap(const Graph& _graph) : Map(_graph) { |
120 build(); |
135 build(); |
121 } |
136 } |
122 |
137 |
123 virtual void add(const Item& item) { |
138 virtual void add(const Item& item) { |
132 Map::erase(item); |
147 Map::erase(item); |
133 } |
148 } |
134 |
149 |
135 virtual void build() { |
150 virtual void build() { |
136 Map::build(); |
151 Map::build(); |
137 for (ItemIt it(*Map::getGraph()); it != INVALID; ++it) { |
152 Item it; |
|
153 for (getGraph()->first(it); it != INVALID; getGraph()->next(it)) { |
138 Map::set(it, inv_map.size()); |
154 Map::set(it, inv_map.size()); |
139 inv_map.push_back(it); |
155 inv_map.push_back(it); |
140 } |
156 } |
141 } |
157 } |
142 |
158 |
143 virtual void clear() { |
159 virtual void clear() { |
144 inv_map.clear(); |
160 inv_map.clear(); |
145 Map::clear(); |
161 Map::clear(); |
146 } |
162 } |
147 |
163 |
|
164 /// \brief Gives back the \e descriptor of the item. |
|
165 /// |
|
166 /// Gives back the mutable and unique \e descriptor of the map. |
148 int operator[](const Item& item) const { |
167 int operator[](const Item& item) const { |
149 return Map::operator[](item); |
168 return Map::operator[](item); |
150 } |
169 } |
151 |
170 |
152 |
171 /// \brief Gives back the inverse of the map. |
|
172 /// |
|
173 /// Gives back the inverse of the map. |
153 const InverseMap inverse() const { |
174 const InverseMap inverse() const { |
154 return inv_map; |
175 return inv_map; |
155 } |
176 } |
156 |
177 |
157 private: |
178 private: |
158 vector<Item> inv_map; |
179 vector<Item> inv_map; |
159 }; |
180 }; |
160 |
|
161 // unique, immutable => IDMap |
|
162 |
181 |
163 |
182 /// Provides an immutable and unique id for each item in the graph. |
|
183 |
|
184 /// The IdMap class provides an unique and immutable mapping for each item |
|
185 /// in the graph. |
|
186 /// |
|
187 template <typename _Graph, typename _Item> |
|
188 class IdMap { |
|
189 public: |
|
190 typedef _Graph Graph; |
|
191 typedef int Value; |
|
192 typedef _Item Item; |
|
193 |
|
194 /// \brief The class represents the inverse of the map. |
|
195 /// |
|
196 /// The class represents the inverse of the map. |
|
197 /// \see inverse() |
|
198 class InverseMap { |
|
199 protected: |
|
200 InverseMap(const Graph& _graph) : graph(_graph) {} |
|
201 public: |
|
202 /// \brief Gives back the given item by its id. |
|
203 /// |
|
204 /// Gives back the given item by its id. |
|
205 /// |
|
206 Item operator[](int id) const { return graph->fromId(id, Item());} |
|
207 private: |
|
208 Graph* graph; |
|
209 }; |
|
210 |
|
211 /// \brief Constructor. |
|
212 /// |
|
213 /// Constructor for creating id map. |
|
214 IdMap(const Graph& _graph) : graph(&_graph) {} |
|
215 |
|
216 /// \brief Gives back the \e id of the item. |
|
217 /// |
|
218 /// Gives back the immutable and unique \e id of the map. |
|
219 int operator[](const Item& item) const { return graph->id(item);} |
|
220 |
|
221 /// \brief Gives back the inverse of the map. |
|
222 /// |
|
223 /// Gives back the inverse of the map. |
|
224 InverseMap inverse() const { return InverseMap(*graph);} |
|
225 |
|
226 private: |
|
227 const Graph* graph; |
|
228 |
|
229 }; |
|
230 |
|
231 |
164 |
232 |
165 } |
233 } |
166 |
234 |