src/work/deba/map_utils.h
changeset 1032 9e903d3a1ef6
child 1037 3eaff8d04171
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/work/deba/map_utils.h	Wed Dec 08 20:54:26 2004 +0000
     1.3 @@ -0,0 +1,98 @@
     1.4 +/* -*- C++ -*-
     1.5 + * src/lemon/map_utils.h - Part of LEMON, a generic C++ optimization library
     1.6 + *
     1.7 + * Copyright (C) 2004 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
     1.8 + * (Egervary Combinatorial Optimization Research Group, EGRES).
     1.9 + *
    1.10 + * Permission to use, modify and distribute this software is granted
    1.11 + * provided that this copyright notice appears in all copies. For
    1.12 + * precise terms see the accompanying LICENSE file.
    1.13 + *
    1.14 + * This software is provided "AS IS" with no warranty of any kind,
    1.15 + * express or implied, and with no claim as to its suitability for any
    1.16 + * purpose.
    1.17 + *
    1.18 + */
    1.19 +
    1.20 +///\ingroup gutils
    1.21 +///\file
    1.22 +///\brief Map utilities.
    1.23 +
    1.24 +#include <map>
    1.25 +
    1.26 +
    1.27 +namespace lemon {
    1.28 +
    1.29 +  /// \addtogroup gutils
    1.30 +  /// @{
    1.31 +
    1.32 +
    1.33 +  /// \brief General inversable map type.
    1.34 +
    1.35 +  /// This type provides simple inversable map functions. 
    1.36 +  /// The InversableMap wraps an arbitrary ReadWriteMap 
    1.37 +  /// and if a key is setted to a new value then store it
    1.38 +  /// in the inverse map.
    1.39 +  template <
    1.40 +    typename _Graph, 
    1.41 +    typename _Map, 
    1.42 +    template <typename, typename> class _InvMap = std::Map
    1.43 +  >
    1.44 +  class InversableMap : protected _Map {
    1.45 +
    1.46 +  public:
    1.47 +
    1.48 +    typename _Map Map;
    1.49 +    typename _InvMap<Map::Value, Map::Key> InverseMap;
    1.50 +    
    1.51 +    typename _Map::Key Key;
    1.52 +    typename _Map::Value Value;
    1.53 +    typename _Map::ConstReference ConstReference;
    1.54 +
    1.55 +    /// Constructor.
    1.56 +
    1.57 +    /// Construct a new InversableMap for the graph.
    1.58 +    ///
    1.59 +    InversableMap(const Graph& graph) : Map(graph) {} 
    1.60 +    
    1.61 +    /// The setter function of the map.
    1.62 +
    1.63 +    /// It sets the map and the inverse map 
    1.64 +    void set(const Key& key, const Value& val) {
    1.65 +      Value oldval = Map::operator[](key);
    1.66 +      InverseMap::iterator it = invMap.find(oldval);
    1.67 +      if (it != invMap.end() && it->second == key) {
    1.68 +	invMap.erase(it);
    1.69 +      }      
    1.70 +      invMap.insert(make_pair(val, key));
    1.71 +      Map::set(key, val);
    1.72 +    }
    1.73 +
    1.74 +    ConstReference operator[](const Key&) const {
    1.75 +      return Map::operator[](key);
    1.76 +    }
    1.77 +
    1.78 +    virtual void add(const Key&) {
    1.79 +      Map::add(key);
    1.80 +    }
    1.81 +
    1.82 +    virtual void erase(const Key&) {
    1.83 +      Value val = Map::operator[](key);
    1.84 +      InverseMap::iterator it = invMap.find(val);
    1.85 +      if (it != invMap.end() && it->second == key) {
    1.86 +	invMap.erase(it);
    1.87 +      }
    1.88 +      Map::erase(key);
    1.89 +    }
    1.90 +
    1.91 +    const InverseMap& inverse() const {
    1.92 +      return invMap;
    1.93 +    } 
    1.94 +
    1.95 +
    1.96 +  private:
    1.97 +    InverseMap invMap;    
    1.98 +  };
    1.99 +
   1.100 +}
   1.101 +