src/work/deba/map_utils.h
changeset 1115 444f69240539
parent 1037 3eaff8d04171
child 1133 9fd485470fee
     1.1 --- a/src/work/deba/map_utils.h	Tue Feb 01 15:51:22 2005 +0000
     1.2 +++ b/src/work/deba/map_utils.h	Tue Feb 01 15:56:37 2005 +0000
     1.3 @@ -14,7 +14,7 @@
     1.4   *
     1.5   */
     1.6  
     1.7 -///\ingroup gutils
     1.8 +///\ingroup mutils
     1.9  ///\file
    1.10  ///\brief Map utilities.
    1.11  
    1.12 @@ -49,15 +49,15 @@
    1.13      
    1.14      typedef typename _Map::ConstReference ConstReference;
    1.15  
    1.16 -    /// Constructor.
    1.17 -
    1.18 +    /// \brief Constructor.
    1.19 +    ///
    1.20      /// Construct a new InversableMap for the graph.
    1.21      ///
    1.22      InversableMap(const Graph& graph) : Map(graph) {} 
    1.23      
    1.24 -    /// The setter function of the map.
    1.25 -
    1.26 -    /// It sets the map and the inverse map 
    1.27 +    /// \brief The setter function of the map.
    1.28 +    ///
    1.29 +    /// It sets the map and the inverse map to given key-value pair.
    1.30      void set(const Key& key, const Value& val) {
    1.31        Value oldval = Map::operator[](key);
    1.32        typename InverseMap::iterator it = inv_map.find(oldval);
    1.33 @@ -95,27 +95,42 @@
    1.34    };
    1.35  
    1.36  
    1.37 -  // unique, continous, mutable
    1.38 +  
    1.39 +  /// \brief Provides a mutable, continous and unique descriptor for each 
    1.40 +  /// item in the graph.
    1.41 +  ///
    1.42 +  /// The DescriptorMap class provides a mutable, continous and immutable
    1.43 +  /// mapping for each item in the graph.
    1.44 +  ///
    1.45 +  /// \param _Graph The graph class the \c DescriptorMap belongs to.
    1.46 +  /// \param _Item The Item is the Key of the Map. It may be Node, Edge or 
    1.47 +  /// UndirEdge.
    1.48 +  /// \param _Map A ReadWriteMap mapping from the item type to integer.
    1.49  
    1.50    template <
    1.51      typename _Graph,   
    1.52      typename _Item,
    1.53 -    typename _ItemIt,
    1.54      typename _Map
    1.55    >
    1.56    class DescriptorMap : protected _Map {
    1.57 -  public:
    1.58 -    typedef _Graph Graph;
    1.59 +
    1.60      typedef _Item Item;
    1.61 -    typedef _ItemIt ItemIt;
    1.62      typedef _Map Map;
    1.63  
    1.64 +  public:
    1.65 +    /// The graph class of DescriptorMap.
    1.66 +    typedef _Graph Graph;
    1.67  
    1.68 +    /// The key type of DescriptorMap (Node, Edge, UndirEdge).
    1.69      typedef typename _Map::Key Key;
    1.70 +    /// The value type of DescriptorMap.
    1.71      typedef typename _Map::Value Value;
    1.72  
    1.73 -    typedef vector<Item> InverseMap;
    1.74 +    typedef std::vector<Item> InverseMap;
    1.75  
    1.76 +    /// \brief Constructor.
    1.77 +    ///
    1.78 +    /// Constructor for creating descriptor map.
    1.79      DescriptorMap(const Graph& _graph) : Map(_graph) {
    1.80        build();
    1.81      }
    1.82 @@ -134,7 +149,8 @@
    1.83  
    1.84      virtual void build() {
    1.85        Map::build();
    1.86 -      for (ItemIt it(*Map::getGraph()); it != INVALID; ++it) {
    1.87 +      Item it;
    1.88 +      for (getGraph()->first(it); it != INVALID; getGraph()->next(it)) {
    1.89  	Map::set(it, inv_map.size());
    1.90  	inv_map.push_back(it);	
    1.91        }      
    1.92 @@ -145,11 +161,16 @@
    1.93        Map::clear();
    1.94      }
    1.95  
    1.96 +    /// \brief Gives back the \e descriptor of the item.
    1.97 +    ///
    1.98 +    /// Gives back the mutable and unique \e descriptor of the map.
    1.99      int operator[](const Item& item) const {
   1.100        return Map::operator[](item);
   1.101      }
   1.102 -
   1.103      
   1.104 +    /// \brief Gives back the inverse of the map.
   1.105 +    ///
   1.106 +    /// Gives back the inverse of the map.
   1.107      const InverseMap inverse() const {
   1.108        return inv_map;
   1.109      }
   1.110 @@ -157,10 +178,57 @@
   1.111    private:
   1.112      vector<Item> inv_map;
   1.113    };
   1.114 +  
   1.115 +  /// Provides an immutable and unique id for each item in the graph.
   1.116  
   1.117 -  // unique, immutable => IDMap
   1.118 -  
   1.119 -  
   1.120 +  /// The IdMap class provides an unique and immutable mapping for each item
   1.121 +  /// in the graph.
   1.122 +  ///
   1.123 +  template <typename _Graph, typename _Item>
   1.124 +  class IdMap {
   1.125 +  public:
   1.126 +    typedef _Graph Graph;
   1.127 +    typedef int Value;
   1.128 +    typedef _Item Item;
   1.129 +
   1.130 +    /// \brief The class represents the inverse of the map.
   1.131 +    ///
   1.132 +    /// The class represents the inverse of the map.
   1.133 +    /// \see inverse()
   1.134 +    class InverseMap {
   1.135 +    protected:
   1.136 +      InverseMap(const Graph& _graph) : graph(_graph) {}
   1.137 +    public:
   1.138 +      /// \brief Gives back the given item by its id.
   1.139 +      ///
   1.140 +      /// Gives back the given item by its id.
   1.141 +      /// 
   1.142 +      Item operator[](int id) const { return graph->fromId(id, Item());}
   1.143 +    private:
   1.144 +      Graph* graph;
   1.145 +    };
   1.146 +
   1.147 +    /// \brief Constructor.
   1.148 +    ///
   1.149 +    /// Constructor for creating id map.
   1.150 +    IdMap(const Graph& _graph) : graph(&_graph) {}
   1.151 +
   1.152 +    /// \brief Gives back the \e id of the item.
   1.153 +    ///
   1.154 +    /// Gives back the immutable and unique \e id of the map.
   1.155 +    int operator[](const Item& item) const { return graph->id(item);}
   1.156 +
   1.157 +    /// \brief Gives back the inverse of the map.
   1.158 +    ///
   1.159 +    /// Gives back the inverse of the map.
   1.160 +    InverseMap inverse() const { return InverseMap(*graph);} 
   1.161 +
   1.162 +  private:
   1.163 +    const Graph* graph;
   1.164 +
   1.165 +  };
   1.166 +
   1.167 +
   1.168  
   1.169  }
   1.170