2 * src/lemon/map_utils.h - Part of LEMON, a generic C++ optimization library
4 * Copyright (C) 2004 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.
26 /// \addtogroup gutils
30 /// \brief General inversable map type.
32 /// This type provides simple inversable map functions.
33 /// The InversableMap wraps an arbitrary ReadWriteMap
34 /// and if a key is setted to a new value then store it
35 /// in the inverse map.
40 class InversableMap : protected _Map {
46 typedef typename _Map::Key Key;
47 typedef typename _Map::Value Value;
48 typedef std::map<Value, Key> InverseMap;
50 typedef typename _Map::ConstReference ConstReference;
52 /// \brief Constructor.
54 /// Construct a new InversableMap for the graph.
56 InversableMap(const Graph& graph) : Map(graph) {}
58 /// \brief The setter function of the map.
60 /// It sets the map and the inverse map to given key-value pair.
61 void set(const Key& key, const Value& val) {
62 Value oldval = Map::operator[](key);
63 typename InverseMap::iterator it = inv_map.find(oldval);
64 if (it != inv_map.end() && it->second == key) {
67 inv_map.insert(make_pair(val, key));
71 ConstReference operator[](const Key&) const {
72 return Map::operator[](key);
75 virtual void add(const Key&) {
79 virtual void erase(const Key&) {
80 Value val = Map::operator[](key);
81 typename InverseMap::iterator it = inv_map.find(val);
82 if (it != inv_map.end() && it->second == key) {
88 const InverseMap& inverse() const {
99 /// \brief Provides a mutable, continous and unique descriptor for each
100 /// item in the graph.
102 /// The DescriptorMap class provides a mutable, continous and immutable
103 /// mapping for each item in the graph.
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
108 /// \param _Map A ReadWriteMap mapping from the item type to integer.
115 class DescriptorMap : protected _Map {
121 /// The graph class of DescriptorMap.
122 typedef _Graph Graph;
124 /// The key type of DescriptorMap (Node, Edge, UndirEdge).
125 typedef typename _Map::Key Key;
126 /// The value type of DescriptorMap.
127 typedef typename _Map::Value Value;
129 typedef std::vector<Item> InverseMap;
131 /// \brief Constructor.
133 /// Constructor for creating descriptor map.
134 DescriptorMap(const Graph& _graph) : Map(_graph) {
138 virtual void add(const Item& item) {
140 Map::set(item, inv_map.size());
141 inv_map.push_back(item);
144 virtual void erase(const Item& item) {
145 Map::set(inv_map.back(), Map::operator[](item));
146 inv_map[Map::operator[](item)] = inv_map.back();
150 virtual void build() {
153 for (getGraph()->first(it); it != INVALID; getGraph()->next(it)) {
154 Map::set(it, inv_map.size());
155 inv_map.push_back(it);
159 virtual void clear() {
164 /// \brief Gives back the \e descriptor of the item.
166 /// Gives back the mutable and unique \e descriptor of the map.
167 int operator[](const Item& item) const {
168 return Map::operator[](item);
171 /// \brief Gives back the inverse of the map.
173 /// Gives back the inverse of the map.
174 const InverseMap inverse() const {
179 vector<Item> inv_map;
182 /// Provides an immutable and unique id for each item in the graph.
184 /// The IdMap class provides an unique and immutable mapping for each item
187 template <typename _Graph, typename _Item>
190 typedef _Graph Graph;
194 /// \brief The class represents the inverse of the map.
196 /// The class represents the inverse of the map.
200 InverseMap(const Graph& _graph) : graph(_graph) {}
202 /// \brief Gives back the given item by its id.
204 /// Gives back the given item by its id.
206 Item operator[](int id) const { return graph->fromId(id, Item());}
211 /// \brief Constructor.
213 /// Constructor for creating id map.
214 IdMap(const Graph& _graph) : graph(&_graph) {}
216 /// \brief Gives back the \e id of the item.
218 /// Gives back the immutable and unique \e id of the map.
219 int operator[](const Item& item) const { return graph->id(item);}
221 /// \brief Gives back the inverse of the map.
223 /// Gives back the inverse of the map.
224 InverseMap inverse() const { return InverseMap(*graph);}