Graph displayer is now displaying nodes. Edges remain still undisplayed yet.
2 * src/lemon/map_utils.h - Part of LEMON, a generic C++ optimization library
4 * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
5 * (Egervary Combinatorial Optimization Research Group, EGRES).
7 * Permission to use, modify and distribute this software is granted
8 * provided that this copyright notice appears in all copies. For
9 * precise terms see the accompanying LICENSE file.
11 * This software is provided "AS IS" with no warranty of any kind,
12 * express or implied, and with no claim as to its suitability for any
20 ///\brief Map utilities.
22 #ifndef LEMON_MAP_UTILS_H
23 #define LEMON_MAP_UTILS_H
28 #include <lemon/graph_utils.h>
32 /// \addtogroup mutils
36 template <typename Map, typename Enable = void>
37 struct ReferenceMapTraits {
38 typedef typename Map::Value Value;
39 typedef typename Map::Value& Reference;
40 typedef const typename Map::Value& ConstReference;
41 typedef typename Map::Value* Pointer;
42 typedef const typename Map::Value* ConstPointer;
45 template <typename Map>
46 struct ReferenceMapTraits<
48 typename enable_if<typename Map::FullTypeTag, void>::type
50 typedef typename Map::Value Value;
51 typedef typename Map::Reference Reference;
52 typedef typename Map::ConstReference ConstReference;
53 typedef typename Map::Pointer Pointer;
54 typedef typename Map::ConstPointer ConstPointer;
57 /// \brief General inversable map type.
59 /// This type provides simple inversable map functions.
60 /// The InversableMap wraps an arbitrary ReadWriteMap
61 /// and if a key is setted to a new value then store it
62 /// in the inverse map.
63 /// \param _Graph The graph type.
64 /// \param _Map The map to extend with inversable functionality.
70 = typename ItemSetTraits<_Graph, _Item>::template Map<_Value>
72 class InversableMap : protected _Map {
78 /// The key type of InversableMap (Node, Edge, UndirEdge).
79 typedef typename _Map::Key Key;
80 /// The value type of the InversableMap.
81 typedef typename _Map::Value Value;
83 typedef std::map<Value, Key> InverseMap;
85 typedef typename _Map::ConstReference ConstReference;
87 /// \brief Constructor.
89 /// Construct a new InversableMap for the graph.
91 InversableMap(const Graph& graph) : Map(graph) {}
93 /// \brief The setter function of the map.
96 void set(const Key& key, const Value& val) {
97 Value oldval = Map::operator[](key);
98 typename InverseMap::iterator it = invMap.find(oldval);
99 if (it != invMap.end() && it->second == key) {
102 invMap.insert(make_pair(val, key));
106 /// \brief The getter function of the map.
108 /// It gives back the value associated with the key.
109 ConstReference operator[](const Key& key) const {
110 return Map::operator[](key);
113 /// \brief Add a new key to the map.
115 /// Add a new key to the map. It is called by the
116 /// \c AlterationNotifier.
117 virtual void add(const Key& key) {
121 /// \brief Erase the key from the map.
123 /// Erase the key to the map. It is called by the
124 /// \c AlterationNotifier.
125 virtual void erase(const Key& key) {
126 Value val = Map::operator[](key);
127 typename InverseMap::iterator it = invMap.find(val);
128 if (it != invMap.end() && it->second == key) {
134 /// \brief Clear the keys from the map and inverse map.
136 /// Clear the keys from the map and inverse map. It is called by the
137 /// \c AlterationNotifier.
138 virtual void clear() {
143 /// \brief It gives back the just readeable inverse map.
145 /// It gives back the just readeable inverse map.
146 const InverseMap& inverse() const {
157 /// \brief Provides a mutable, continous and unique descriptor for each
158 /// item in the graph.
160 /// The DescriptorMap class provides a mutable, continous and immutable
161 /// mapping for each item in the graph.
163 /// \param _Graph The graph class the \c DescriptorMap belongs to.
164 /// \param _Item The Item is the Key of the Map. It may be Node, Edge or
166 /// \param _Map A ReadWriteMap mapping from the item type to integer.
171 typename _Map = typename ItemSetTraits<_Graph, _Item>::template Map<int>
173 class DescriptorMap : protected _Map {
179 /// The graph class of DescriptorMap.
180 typedef _Graph Graph;
182 /// The key type of DescriptorMap (Node, Edge, UndirEdge).
183 typedef typename _Map::Key Key;
184 /// The value type of DescriptorMap.
185 typedef typename _Map::Value Value;
187 typedef std::vector<Item> InverseMap;
189 /// \brief Constructor.
191 /// Constructor for creating descriptor map.
192 DescriptorMap(const Graph& _graph) : Map(_graph) {
196 /// \brief Add a new key to the map.
198 /// Add a new key to the map. It is called by the
199 /// \c AlterationNotifier.
200 virtual void add(const Item& item) {
202 Map::set(item, invMap.size());
203 invMap.push_back(item);
206 /// \brief Erase the key from the map.
208 /// Erase the key to the map. It is called by the
209 /// \c AlterationNotifier.
210 virtual void erase(const Item& item) {
211 Map::set(invMap.back(), Map::operator[](item));
212 invMap[Map::operator[](item)] = invMap.back();
216 /// \brief Build the unique map.
218 /// Build the unique map. It is called by the
219 /// \c AlterationNotifier.
220 virtual void build() {
223 const Graph* graph = Map::getGraph();
224 for (graph->first(it); it != INVALID; graph->next(it)) {
225 Map::set(it, invMap.size());
226 invMap.push_back(it);
230 /// \brief Clear the keys from the map.
232 /// Clear the keys from the map. It is called by the
233 /// \c AlterationNotifier.
234 virtual void clear() {
239 /// \brief Gives back the \e descriptor of the item.
241 /// Gives back the mutable and unique \e descriptor of the map.
242 int operator[](const Item& item) const {
243 return Map::operator[](item);
246 /// \brief Gives back the inverse of the map.
248 /// Gives back the inverse of the map.
249 const InverseMap inverse() const {
254 std::vector<Item> invMap;
257 /// Provides an immutable and unique id for each item in the graph.
259 /// The IdMap class provides an unique and immutable mapping for each item
262 template <typename _Graph, typename _Item>
265 typedef _Graph Graph;
270 /// \brief The class represents the inverse of the map.
272 /// The class represents the inverse of the map.
276 /// \brief Constructor.
278 /// Constructor for creating an id-to-item map.
279 InverseMap(const Graph& _graph) : graph(&_graph) {}
280 /// \brief Gives back the given item from its id.
282 /// Gives back the given item from its id.
284 Item operator[](int id) const { return graph->fromId(id, Item());}
289 /// \brief Constructor.
291 /// Constructor for creating id map.
292 IdMap(const Graph& _graph) : graph(&_graph) {}
294 /// \brief Gives back the \e id of the item.
296 /// Gives back the immutable and unique \e id of the map.
297 int operator[](const Item& item) const { return graph->id(item);}
299 /// \brief Gives back the inverse of the map.
301 /// Gives back the inverse of the map.
302 InverseMap inverse() const { return InverseMap(*graph);}