[Lemon-commits] [lemon_svn] deba: r1882 - hugo/trunk/src/lemon

Lemon SVN svn at lemon.cs.elte.hu
Mon Nov 6 20:48:26 CET 2006


Author: deba
Date: Wed May 11 19:36:25 2005
New Revision: 1882

Modified:
   hugo/trunk/src/lemon/graph_utils.h

Log:
Fixing invertable maps:
    InvertableMap
    DescriptorMap
    IdMap



Modified: hugo/trunk/src/lemon/graph_utils.h
==============================================================================
--- hugo/trunk/src/lemon/graph_utils.h	(original)
+++ hugo/trunk/src/lemon/graph_utils.h	Wed May 11 19:36:25 2005
@@ -22,6 +22,7 @@
 
 #include <lemon/invalid.h>
 #include <lemon/utility.h>
+#include <lemon/maps.h>
 
 ///\ingroup gutils
 ///\file
@@ -339,6 +340,28 @@
   /// \addtogroup graph_maps
   /// @{
 
+  template <typename Map, typename Enable = void>
+  struct ReferenceMapTraits {
+    typedef typename Map::Value Value;
+    typedef typename Map::Value& Reference;
+    typedef const typename Map::Value& ConstReference;
+    typedef typename Map::Value* Pointer;
+    typedef const typename Map::Value* ConstPointer;
+  };
+
+  template <typename Map>
+  struct ReferenceMapTraits<
+    Map, 
+    typename enable_if<typename Map::FullTypeTag, void>::type
+  > {
+    typedef typename Map::Value Value;
+    typedef typename Map::Reference Reference;
+    typedef typename Map::ConstReference ConstReference;
+    typedef typename Map::Pointer Pointer;
+    typedef typename Map::ConstPointer ConstPointer;
+  };
+
+
   /// Provides an immutable and unique id for each item in the graph.
 
   /// The IdMap class provides an unique and immutable mapping for each item
@@ -352,6 +375,22 @@
     typedef _Item Item;
     typedef _Item Key;
 
+    /// \brief Constructor.
+    ///
+    /// Constructor for creating id map.
+    IdMap(const Graph& _graph) : graph(&_graph) {}
+
+    /// \brief Gives back the \e id of the item.
+    ///
+    /// Gives back the immutable and unique \e id of the map.
+    int operator[](const Item& item) const { return graph->id(item);}
+
+
+  private:
+    const Graph* graph;
+
+  public:
+
     /// \brief The class represents the inverse of the map.
     ///
     /// The class represents the inverse of the map.
@@ -362,6 +401,12 @@
       ///
       /// Constructor for creating an id-to-item map.
       InverseMap(const Graph& _graph) : graph(&_graph) {}
+
+      /// \brief Constructor.
+      ///
+      /// Constructor for creating an id-to-item map.
+      InverseMap(const IdMap& idMap) : graph(idMap.graph) {}
+
       /// \brief Gives back the given item from its id.
       ///
       /// Gives back the given item from its id.
@@ -371,48 +416,14 @@
       const Graph* graph;
     };
 
-    /// \brief Constructor.
-    ///
-    /// Constructor for creating id map.
-    IdMap(const Graph& _graph) : graph(&_graph) {}
-
-    /// \brief Gives back the \e id of the item.
-    ///
-    /// Gives back the immutable and unique \e id of the map.
-    int operator[](const Item& item) const { return graph->id(item);}
-
     /// \brief Gives back the inverse of the map.
     ///
     /// Gives back the inverse of the map.
     InverseMap inverse() const { return InverseMap(*graph);} 
 
-  private:
-    const Graph* graph;
-
   };
 
   
-  template <typename Map, typename Enable = void>
-  struct ReferenceMapTraits {
-    typedef typename Map::Value Value;
-    typedef typename Map::Value& Reference;
-    typedef const typename Map::Value& ConstReference;
-    typedef typename Map::Value* Pointer;
-    typedef const typename Map::Value* ConstPointer;
-  };
-
-  template <typename Map>
-  struct ReferenceMapTraits<
-    Map, 
-    typename enable_if<typename Map::FullTypeTag, void>::type
-  > {
-    typedef typename Map::Value Value;
-    typedef typename Map::Reference Reference;
-    typedef typename Map::ConstReference ConstReference;
-    typedef typename Map::Pointer Pointer;
-    typedef typename Map::ConstPointer ConstPointer;
-  };
-
   /// \brief General inversable graph-map type.
 
   /// This type provides simple inversable map functions. 
@@ -426,35 +437,32 @@
     typename _Item, 
     typename _Value,
     typename _Map 
-    = typename ItemSetTraits<_Graph, _Item>::template Map<_Value> 
+    = typename ItemSetTraits<_Graph, _Item>::template Map<_Value>::Parent 
   >
-  class InversableMap : protected _Map {
+  class InvertableMap : protected _Map {
 
   public:
  
     typedef _Map Map;
     typedef _Graph Graph;
-   /// The key type of InversableMap (Node, Edge, UndirEdge).
+
+    /// The key type of InvertableMap (Node, Edge, UndirEdge).
     typedef typename _Map::Key Key;
-    /// The value type of the InversableMap.
+    /// The value type of the InvertableMap.
     typedef typename _Map::Value Value;
 
-    typedef std::map<Value, Key> InverseMap;
-    
-    typedef typename _Map::ConstReference ConstReference;
-
     /// \brief Constructor.
     ///
-    /// Construct a new InversableMap for the graph.
+    /// Construct a new InvertableMap for the graph.
     ///
-    InversableMap(const Graph& graph) : Map(graph) {} 
+    InvertableMap(const Graph& graph) : Map(graph) {} 
     
     /// \brief The setter function of the map.
     ///
-
+    /// Sets the mapped value.
     void set(const Key& key, const Value& val) {
       Value oldval = Map::operator[](key);
-      typename InverseMap::iterator it = invMap.find(oldval);
+      typename Container::iterator it = invMap.find(oldval);
       if (it != invMap.end() && it->second == key) {
 	invMap.erase(it);
       }      
@@ -465,7 +473,7 @@
     /// \brief The getter function of the map.
     ///
     /// It gives back the value associated with the key.
-    ConstReference operator[](const Key& key) const {
+    const Value operator[](const Key& key) const {
       return Map::operator[](key);
     }
 
@@ -483,7 +491,7 @@
     /// \c AlterationNotifier.
     virtual void erase(const Key& key) {
       Value val = Map::operator[](key);
-      typename InverseMap::iterator it = invMap.find(val);
+      typename Container::iterator it = invMap.find(val);
       if (it != invMap.end() && it->second == key) {
 	invMap.erase(it);
       }
@@ -499,33 +507,69 @@
       Map::clear();
     }
 
+  private:
+    
+    typedef std::map<Value, Key> Container;
+    Container invMap;    
+
+  public:
+
+    /// \brief The inverse map type.
+    ///
+    /// The inverse of this map. The subscript operator of the map
+    /// gives back always the item what was last assigned to the value. 
+    class InverseMap {
+    public:
+      /// \brief Constructor of the InverseMap.
+      ///
+      /// Constructor of the InverseMap.
+      InverseMap(const InvertableMap& _inverted) : inverted(_inverted) {}
+
+      /// The value type of the InverseMap.
+      typedef typename InvertableMap::Key Value;
+      /// The key type of the InverseMap.
+      typedef typename InvertableMap::Value Key; 
+
+      /// \brief Subscript operator. 
+      ///
+      /// Subscript operator. It gives back always the item 
+      /// what was last assigned to the value.
+      Value operator[](const Key& key) const {
+	typename Container::const_iterator it = inverted.invMap.find(key);
+	return it->second;
+      }
+      
+    private:
+      const InvertableMap& inverted;
+    };
+
     /// \brief It gives back the just readeable inverse map.
     ///
     /// It gives back the just readeable inverse map.
-    const InverseMap& inverse() const {
-      return invMap;
+    InverseMap inverse() const {
+      return InverseMap(*this);
     } 
 
 
-  private:
-    InverseMap invMap;    
+    
   };
 
   /// \brief Provides a mutable, continuous and unique descriptor for each 
   /// item in the graph.
   ///
   /// The DescriptorMap class provides a mutable, continuous and immutable
-  /// mapping for each item in the graph.
+  /// mapping for each item in the graph. The value for an item may mutated
+  /// on each operation when the an item erased or added to graph.
   ///
   /// \param _Graph The graph class the \c DescriptorMap belongs to.
   /// \param _Item The Item is the Key of the Map. It may be Node, Edge or 
   /// UndirEdge.
   /// \param _Map A ReadWriteMap mapping from the item type to integer.
-
   template <
     typename _Graph,   
     typename _Item,
-    typename _Map = typename ItemSetTraits<_Graph, _Item>::template Map<int>
+    typename _Map 
+    = typename ItemSetTraits<_Graph, _Item>::template Map<int>::Parent
   >
   class DescriptorMap : protected _Map {
 
@@ -541,11 +585,9 @@
     /// The value type of DescriptorMap.
     typedef typename _Map::Value Value;
 
-    typedef std::vector<Item> InverseMap;
-
     /// \brief Constructor.
     ///
-    /// Constructor for creating descriptor map.
+    /// Constructor for descriptor map.
     DescriptorMap(const Graph& _graph) : Map(_graph) {
       build();
     }
@@ -567,6 +609,7 @@
     virtual void erase(const Item& item) {
       Map::set(invMap.back(), Map::operator[](item));
       invMap[Map::operator[](item)] = invMap.back();
+      invMap.pop_back();
       Map::erase(item);
     }
 
@@ -600,15 +643,47 @@
       return Map::operator[](item);
     }
     
+  private:
+
+    typedef std::vector<Item> Container;
+    Container invMap;
+
+  public:
+    /// \brief The inverse map type.
+    ///
+    /// The inverse map type.
+    class InverseMap {
+    public:
+      /// \brief Constructor of the InverseMap.
+      ///
+      /// Constructor of the InverseMap.
+      InverseMap(const DescriptorMap& _inverted) 
+	: inverted(_inverted) {}
+
+
+      /// The value type of the InverseMap.
+      typedef typename DescriptorMap::Key Value;
+      /// The key type of the InverseMap.
+      typedef typename DescriptorMap::Value Key; 
+
+      /// \brief Subscript operator. 
+      ///
+      /// Subscript operator. It gives back the item 
+      /// that the descriptor belongs to currently.
+      Value operator[](const Key& key) const {
+	return inverted.invMap[key];
+      }
+      
+    private:
+      const DescriptorMap& inverted;
+    };
+
     /// \brief Gives back the inverse of the map.
     ///
     /// Gives back the inverse of the map.
     const InverseMap inverse() const {
-      return invMap;
+      return InverseMap(*this);
     }
-
-  private:
-    std::vector<Item> invMap;
   };
 
   /// \brief Returns the source of the given edge.



More information about the Lemon-commits mailing list