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
19 ///\brief Map utilities.
21 #ifndef LEMON_MAP_UTILS_H
22 #define LEMON_MAP_UTILS_H
29 /// \addtogroup mutils
32 /// \brief General inversable map type.
34 /// This type provides simple inversable map functions.
35 /// The InversableMap wraps an arbitrary ReadWriteMap
36 /// and if a key is setted to a new value then store it
37 /// in the inverse map.
38 /// \param _Graph The graph type.
39 /// \param _Map The map to extend with inversable functionality.
44 class InversableMap : protected _Map {
50 /// The key type of InversableMap (Node, Edge, UndirEdge).
51 typedef typename _Map::Key Key;
52 /// The value type of the InversableMap.
53 typedef typename _Map::Value Value;
54 typedef std::map<Value, Key> InverseMap;
56 typedef typename _Map::ConstReference ConstReference;
58 /// \brief Constructor.
60 /// Construct a new InversableMap for the graph.
62 InversableMap(const Graph& graph) : Map(graph) {}
64 /// \brief The setter function of the map.
66 /// It sets the map and the inverse map to given key-value pair.
67 void set(const Key& key, const Value& val) {
68 Value oldval = Map::operator[](key);
69 typename InverseMap::iterator it = invMap.find(oldval);
70 if (it != invMap.end() && it->second == key) {
73 invMap.insert(make_pair(val, key));
77 /// \brief The getter function of the map.
79 /// It gives back the value associated with the key.
80 ConstReference operator[](const Key& key) const {
81 return Map::operator[](key);
84 /// \brief Add a new key to the map.
86 /// Add a new key to the map. It is called by the
87 /// \c AlterationNotifier.
88 virtual void add(const Key& key) {
92 /// \brief Erase the key from the map.
94 /// Erase the key to the map. It is called by the
95 /// \c AlterationNotifier.
96 virtual void erase(const Key& key) {
97 Value val = Map::operator[](key);
98 typename InverseMap::iterator it = invMap.find(val);
99 if (it != invMap.end() && it->second == key) {
105 /// \brief Clear the keys from the map and inverse map.
107 /// Clear the keys from the map and inverse map. It is called by the
108 /// \c AlterationNotifier.
109 virtual void clear() {
114 /// \brief It gives back the just readeable inverse map.
116 /// It gives back the just readeable inverse map.
117 const InverseMap& inverse() const {
128 /// \brief Provides a mutable, continous and unique descriptor for each
129 /// item in the graph.
131 /// The DescriptorMap class provides a mutable, continous and immutable
132 /// mapping for each item in the graph.
134 /// \param _Graph The graph class the \c DescriptorMap belongs to.
135 /// \param _Item The Item is the Key of the Map. It may be Node, Edge or
137 /// \param _Map A ReadWriteMap mapping from the item type to integer.
144 class DescriptorMap : protected _Map {
150 /// The graph class of DescriptorMap.
151 typedef _Graph Graph;
153 /// The key type of DescriptorMap (Node, Edge, UndirEdge).
154 typedef typename _Map::Key Key;
155 /// The value type of DescriptorMap.
156 typedef typename _Map::Value Value;
158 typedef std::vector<Item> InverseMap;
160 /// \brief Constructor.
162 /// Constructor for creating descriptor map.
163 DescriptorMap(const Graph& _graph) : Map(_graph) {
167 /// \brief Add a new key to the map.
169 /// Add a new key to the map. It is called by the
170 /// \c AlterationNotifier.
171 virtual void add(const Item& item) {
173 Map::set(item, invMap.size());
174 invMap.push_back(item);
177 /// \brief Erase the key from the map.
179 /// Erase the key to the map. It is called by the
180 /// \c AlterationNotifier.
181 virtual void erase(const Item& item) {
182 Map::set(invMap.back(), Map::operator[](item));
183 invMap[Map::operator[](item)] = invMap.back();
187 /// \brief Build the unique map.
189 /// Build the unique map. It is called by the
190 /// \c AlterationNotifier.
191 virtual void build() {
194 for (getGraph()->first(it); it != INVALID; getGraph()->next(it)) {
195 Map::set(it, invMap.size());
196 invMap.push_back(it);
200 /// \brief Clear the keys from the map.
202 /// Clear the keys from the map. It is called by the
203 /// \c AlterationNotifier.
204 virtual void clear() {
209 /// \brief Gives back the \e descriptor of the item.
211 /// Gives back the mutable and unique \e descriptor of the map.
212 int operator[](const Item& item) const {
213 return Map::operator[](item);
216 /// \brief Gives back the inverse of the map.
218 /// Gives back the inverse of the map.
219 const InverseMap inverse() const {
224 std::vector<Item> invMap;
227 /// Provides an immutable and unique id for each item in the graph.
229 /// The IdMap class provides an unique and immutable mapping for each item
232 template <typename _Graph, typename _Item>
235 typedef _Graph Graph;
239 /// \brief The class represents the inverse of the map.
241 /// The class represents the inverse of the map.
245 InverseMap(const Graph& _graph) : graph(_graph) {}
247 /// \brief Gives back the given item by its id.
249 /// Gives back the given item by its id.
251 Item operator[](int id) const { return graph->fromId(id, Item());}
256 /// \brief Constructor.
258 /// Constructor for creating id map.
259 IdMap(const Graph& _graph) : graph(&_graph) {}
261 /// \brief Gives back the \e id of the item.
263 /// Gives back the immutable and unique \e id of the map.
264 int operator[](const Item& item) const { return graph->id(item);}
266 /// \brief Gives back the inverse of the map.
268 /// Gives back the inverse of the map.
269 InverseMap inverse() const { return InverseMap(*graph);}