src/hugo/vector_map.h
author deba
Tue, 28 Sep 2004 16:40:55 +0000
changeset 916 c0734a8c282c
parent 901 69a8e672acb1
permissions -rw-r--r--
Fix InEdgeIt and OutEdgeIt in the symmetric graphs.
     1 /* -*- C++ -*-
     2  * src/hugo/vector_map.h - Part of HUGOlib, a generic C++ optimization library
     3  *
     4  * Copyright (C) 2004 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
     5  * (Egervary Combinatorial Optimization Research Group, EGRES).
     6  *
     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.
    10  *
    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
    13  * purpose.
    14  *
    15  */
    16 
    17 #ifndef HUGO_VECTOR_MAP_H
    18 #define HUGO_VECTOR_MAP_H
    19 
    20 #include <vector>
    21 
    22 #include <hugo/map_iterator.h>
    23 #include <hugo/map_bits.h>
    24 
    25 ///\ingroup graphmaps
    26 ///\file
    27 ///\brief Vector based graph maps.
    28 
    29 namespace hugo {
    30   
    31   /// \addtogroup graphmaps
    32   /// @{
    33   
    34   /** The ArrayMap template class is graph map structure what
    35    *  automatically updates the map when a key is added to or erased from
    36    *  the map. This map factory uses the allocators to implement 
    37    *  the container functionality. This map factory
    38    *  uses the std::vector to implement the container function.
    39    *
    40    *  The template parameter is the MapRegistry that the maps
    41    *  will belong to and the ValueType.
    42    * 
    43    * \todo It should use a faster initialization using the maxNodeId() or
    44    * maxEdgeId() function of the graph instead of iterating through each
    45    * edge/node.
    46    */
    47 	
    48   template <typename MapRegistry, typename Value>
    49   class VectorMap : public MapRegistry::MapBase {
    50     template <typename MR, typename T> friend class VectorMap;
    51   public:
    52 		
    53     /// The graph type of the maps. 
    54     typedef typename MapRegistry::Graph Graph;
    55     /// The key type of the maps.
    56     typedef typename MapRegistry::KeyType KeyType;
    57     /// The iterator to iterate on the keys.
    58     typedef typename MapRegistry::KeyIt KeyIt;
    59 
    60     /// The map type.
    61     typedef VectorMap Map;
    62     /// The MapBase of the Map which implements the core regisitry function.
    63     typedef typename MapRegistry::MapBase MapBase;
    64 
    65   private:
    66 		
    67     /// The container type of the map.
    68     typedef std::vector<Value> Container;	
    69 
    70   public:
    71 
    72 
    73     /// The value type of the map.
    74     typedef Value ValueType;
    75     /// The reference type of the map;
    76     typedef typename Container::reference ReferenceType;
    77     /// The pointer type of the map;
    78     typedef typename Container::pointer PointerType;
    79 
    80     /// The const value type of the map.
    81     typedef const Value ConstValueType;
    82     /// The const reference type of the map;
    83     typedef typename Container::const_reference ConstReferenceType;
    84     /// The pointer type of the map;
    85     typedef typename Container::const_pointer ConstPointerType;
    86 
    87     /** Graph and Registry initialized map constructor.
    88      */
    89     VectorMap(const Graph& g, MapRegistry& r) 
    90       : MapBase(g, r), container(KeyInfo<Graph, KeyIt>::maxId(g)+1) {}
    91 
    92     /** Constructor to use default value to initialize the map. 
    93      */
    94     VectorMap(const Graph& g, MapRegistry& r, const Value& v) 
    95       : MapBase(g, r), container(KeyInfo<Graph, KeyIt>::maxId(g)+1, v) {}
    96 
    97     /** Assign operator to copy a map of an other map type.
    98      */
    99     template <typename TT>
   100     VectorMap(const VectorMap<MapRegistry, TT>& c) 
   101       : MapBase(c), container(c.container.size()) {
   102       for (KeyIt it(*MapBase::getGraph()); it != INVALID; ++it) {
   103 	int id = KeyInfo<Graph, KeyIt>::id(*MapBase::getGraph(), it);
   104 	container[id] = c.container[id];
   105       }
   106     }
   107 
   108     /** Assign operator to copy a map of an other map type.
   109      */
   110     template <typename TT>
   111     VectorMap& operator=(const VectorMap<MapRegistry, TT>& c) {
   112       if (MapBase::getGraph() != c.getGraph()) {
   113 	MapBase::operator=(c);
   114 	container.resize(c.container.size());
   115       }
   116       for (KeyIt it(*MapBase::getGraph()); it != INVALID; ++it) {
   117 	int id = KeyInfo<Graph, KeyIt>::id(*MapBase::getGraph(), it);
   118 	container[id] = c.container[id];
   119       }
   120       return *this;
   121     }
   122     /**
   123      * The subscript operator. The map can be subscripted by the
   124      * actual keys of the graph. 
   125      */
   126     ReferenceType operator[](const KeyType& key) {
   127       int id = KeyInfo<Graph, KeyIt>::id(*MapBase::getGraph(), key);
   128       return container[id];
   129     } 
   130 		
   131     /**
   132      * The const subscript operator. The map can be subscripted by the
   133      * actual keys of the graph. 
   134      */
   135     ConstReferenceType operator[](const KeyType& key) const {
   136       int id = KeyInfo<Graph, KeyIt>::id(*MapBase::getGraph(), key);
   137       return container[id];
   138     }
   139 
   140     /** Setter function of the map. Equivalent with map[key] = val.
   141      *  This is a compatibility feature with the not dereferable maps.
   142      */
   143     void set(const KeyType& key, const ValueType& val) {
   144       int id = KeyInfo<Graph, KeyIt>::id(*MapBase::getGraph(), key);
   145       container[id] = val;
   146     }
   147 		
   148     /** Add a new key to the map. It called by the map registry.
   149      */
   150     void add(const KeyType& key) {
   151       int id = KeyInfo<Graph, KeyIt>::id(*MapBase::getGraph(), key);
   152       if (id >= (int)container.size()) {
   153 	container.resize(id + 1);
   154       }
   155     }
   156 		
   157     /** Erase a key from the map. It called by the map registry.
   158      */
   159     void erase(const KeyType& key) {}
   160 
   161     /** Clear the data structure.
   162      */
   163     void clear() { 
   164       container.clear();
   165     }
   166 
   167     /// The stl compatible pair iterator of the map.
   168     typedef MapIterator<VectorMap> Iterator;
   169     /// The stl compatible const pair iterator of the map.
   170     typedef MapConstIterator<VectorMap> ConstIterator;
   171 
   172     /** Returns the begin iterator of the map.
   173      */
   174     Iterator begin() {
   175       return Iterator(*this, KeyIt(*MapBase::getGraph()));
   176     }
   177 
   178     /** Returns the end iterator of the map.
   179      */
   180     Iterator end() {
   181       return Iterator(*this, INVALID);
   182     }
   183 
   184     /** Returns the begin ConstIterator of the map.
   185      */
   186     ConstIterator begin() const {
   187       return ConstIterator(*this, KeyIt(*MapBase::getGraph()));
   188     }
   189 
   190     /** Returns the end const_iterator of the map.
   191      */
   192     ConstIterator end() const {
   193       return ConstIterator(*this, INVALID);
   194     }
   195 
   196     /// The KeySet of the Map.
   197     typedef MapConstKeySet<VectorMap> ConstKeySet;
   198 
   199     /// KeySet getter function.
   200     ConstKeySet keySet() const {
   201       return ConstKeySet(*this); 
   202     }
   203 
   204     /// The ConstValueSet of the Map.
   205     typedef MapConstValueSet<VectorMap> ConstValueSet;
   206 
   207     /// ConstValueSet getter function.
   208     ConstValueSet valueSet() const {
   209       return ConstValueSet(*this);
   210     }
   211 
   212     /// The ValueSet of the Map.
   213     typedef MapValueSet<VectorMap> ValueSet;
   214 
   215     /// ValueSet getter function.
   216     ValueSet valueSet() {
   217       return ValueSet(*this);
   218     }
   219 
   220 
   221   private:
   222 		
   223     Container container;
   224 
   225   public:
   226     // STL  compatibility typedefs.
   227     typedef Iterator iterator;
   228     typedef ConstIterator const_iterator;
   229     typedef typename Iterator::PairValueType value_type;
   230     typedef typename Iterator::KeyType key_type;
   231     typedef typename Iterator::ValueType data_type;
   232     typedef typename Iterator::PairReferenceType reference;
   233     typedef typename Iterator::PairPointerType pointer;
   234     typedef typename ConstIterator::PairReferenceType const_reference;
   235     typedef typename ConstIterator::PairPointerType const_pointer;
   236     typedef int difference_type;		
   237   };
   238   
   239   /// @}
   240   
   241 }
   242 
   243 #endif