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.
39 template <typename, typename> class _InvMap = std::Map
41 class InversableMap : protected _Map {
46 typename _InvMap<Map::Value, Map::Key> InverseMap;
48 typename _Map::Key Key;
49 typename _Map::Value Value;
50 typename _Map::ConstReference ConstReference;
54 /// Construct a new InversableMap for the graph.
56 InversableMap(const Graph& graph) : Map(graph) {}
58 /// The setter function of the map.
60 /// It sets the map and the inverse map
61 void set(const Key& key, const Value& val) {
62 Value oldval = Map::operator[](key);
63 InverseMap::iterator it = invMap.find(oldval);
64 if (it != invMap.end() && it->second == key) {
67 invMap.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 InverseMap::iterator it = invMap.find(val);
82 if (it != invMap.end() && it->second == key) {
88 const InverseMap& inverse() const {