src/hugo/vector_map.h
changeset 865 2f3f87afb1d2
parent 830 89dfa3bece81
child 891 74589d20dbc3
equal deleted inserted replaced
1:8cd488b774b7 2:eea5f7431087
     3 #define VECTOR_MAP_H
     3 #define VECTOR_MAP_H
     4 
     4 
     5 #include <vector>
     5 #include <vector>
     6 
     6 
     7 #include <hugo/map_iterator.h>
     7 #include <hugo/map_iterator.h>
       
     8 #include <hugo/map_bits.h>
     8 
     9 
     9 ///\ingroup graphmaps
    10 ///\ingroup graphmaps
    10 ///\file
    11 ///\file
    11 ///\brief Vector based graph maps.
    12 ///\brief Vector based graph maps.
    12 
    13 
    71      */
    72      */
    72     VectorMap() {}
    73     VectorMap() {}
    73 		
    74 		
    74     /** Graph and Registry initialized map constructor.
    75     /** Graph and Registry initialized map constructor.
    75      */
    76      */
    76     VectorMap(const Graph& g, MapRegistry& r) : MapBase(g, r) {
    77     VectorMap(const Graph& g, MapRegistry& r) 
    77       init();
    78       : MapBase(g, r), container(KeyInfo<Graph, KeyIt>::maxId(g)+1) {}
    78     }
       
    79 
    79 
    80     /** Constructor to use default value to initialize the map. 
    80     /** Constructor to use default value to initialize the map. 
    81      */
    81      */
    82     VectorMap(const Graph& g, MapRegistry& r, const Value& v) 
    82     VectorMap(const Graph& g, MapRegistry& r, const Value& v) 
    83       : MapBase(g, r) {
    83       : MapBase(g, r), container(KeyInfo<Graph, KeyIt>::maxId(g)+1, v) {}
    84       for (KeyIt it(*getGraph()); it != INVALID; ++it) {
       
    85 	int id = getGraph()->id(it);
       
    86 	if (id >= (int)container.size()) {
       
    87 	  container.resize(id + 1);
       
    88 	}
       
    89 	set(it, v);
       
    90       }
       
    91     }
       
    92 
    84 
    93     /** Constructor to copy a map of an other map type.
    85     /** Constructor to copy a map of an other map type.
    94      */
    86      */
    95     template <typename CMap> VectorMap(const CMap& copy) : MapBase(copy) {
    87     template <typename CMap> VectorMap(const CMap& copy) : MapBase(copy) {
    96       if (getGraph()) {
    88       if (MapBase::getGraph()) {
    97 	for (KeyIt it(*getGraph()); it != INVALID; ++it) {
    89 	container.resize(KeyInfo<Graph, KeyIt>::maxId(*MapBase::getGraph())+1);
    98 	  int id = getGraph()->id(it);
    90 	for (KeyIt it(*MapBase::getGraph()); it != INVALID; ++it) {
    99 	  if (id >= (int)container.size()) {
       
   100 	    container.resize(id + 1);
       
   101 	  }
       
   102 	  set(it, copy[it]);
    91 	  set(it, copy[it]);
   103 	}
    92 	}
   104       }
    93       }
   105     }
    94     }
   106 
    95 
   107     /** Assign operator to copy a map an other map type.
    96     /** Assign operator to copy a map an other map type.
   108      */
    97      */
   109     template <typename CMap> VectorMap& operator=(const CMap& copy) {
    98     template <typename CMap> VectorMap& operator=(const CMap& copy) {
   110       if (getGraph()) {
    99       container.clear();
   111 	destroy();
       
   112       } 
       
   113       this->MapBase::operator=(copy);
   100       this->MapBase::operator=(copy);
   114       if (getGraph()) {
   101       if (MapBase::getGraph()) {
   115 	for (KeyIt it(*getGraph()); it != INVALID; ++it) {
   102 	container.resize(KeyInfo<Graph, KeyIt>::maxId(*MapBase::getGraph())+1);
   116 	  int id = getGraph()->id(it);
   103 	for (KeyIt it(*MapBase::getGraph()); it != INVALID; ++it) {
   117 	  if (id >= (int)container.size()) {
       
   118 	    container.resize(id + 1);
       
   119 	  }
       
   120 	  set(it, copy[it]);
   104 	  set(it, copy[it]);
   121 	}
   105 	}
   122       }
   106       }
   123       return *this;
   107       return *this;
   124     }
   108     }
   131     /**
   115     /**
   132      * The subscript operator. The map can be subscripted by the
   116      * The subscript operator. The map can be subscripted by the
   133      * actual keys of the graph. 
   117      * actual keys of the graph. 
   134      */
   118      */
   135     ReferenceType operator[](const KeyType& key) {
   119     ReferenceType operator[](const KeyType& key) {
   136       int id = getGraph()->id(key);
   120       int id = KeyInfo<Graph, KeyIt>::id(*MapBase::getGraph(), key);
   137       return container[id];
   121       return container[id];
   138     } 
   122     } 
   139 		
   123 		
   140     /**
   124     /**
   141      * The const subscript operator. The map can be subscripted by the
   125      * The const subscript operator. The map can be subscripted by the
   142      * actual keys of the graph. 
   126      * actual keys of the graph. 
   143      */
   127      */
   144     ConstReferenceType operator[](const KeyType& key) const {
   128     ConstReferenceType operator[](const KeyType& key) const {
   145       int id = getGraph()->id(key);
   129       int id = KeyInfo<Graph, KeyIt>::id(*MapBase::getGraph(), key);
   146       return container[id];
   130       return container[id];
   147     }
   131     }
   148 
   132 
   149     /** Setter function of the map. Equivalent with map[key] = val.
   133     /** Setter function of the map. Equivalent with map[key] = val.
   150      *  This is a compatibility feature with the not dereferable maps.
   134      *  This is a compatibility feature with the not dereferable maps.
   151      */
   135      */
   152     void set(const KeyType& key, const ValueType& val) {
   136     void set(const KeyType& key, const ValueType& val) {
   153       int id = getGraph()->id(key);
   137       int id = KeyInfo<Graph, KeyIt>::id(*MapBase::getGraph(), key);
   154       container[id] = val;
   138       container[id] = val;
   155     }
   139     }
   156 		
   140 		
   157     /** Add a new key to the map. It called by the map registry.
   141     /** Add a new key to the map. It called by the map registry.
   158      */
   142      */
   159     void add(const KeyType& key) {
   143     void add(const KeyType& key) {
   160       int id = getGraph()->id(key);
   144       int id = KeyInfo<Graph, KeyIt>::id(*MapBase::getGraph(), key);
   161       if (id >= (int)container.size()) {
   145       if (id >= (int)container.size()) {
   162 	container.resize(id + 1);
   146 	container.resize(id + 1);
   163       }
   147       }
   164     }
   148     }
   165 		
   149 		
   229 
   213 
   230   private:
   214   private:
   231 		
   215 		
   232     Container container;
   216     Container container;
   233 
   217 
   234 		
   218   public:
       
   219     // STL  compatibility typedefs.
       
   220     typedef Iterator iterator;
       
   221     typedef ConstIterator const_iterator;
       
   222     typedef typename Iterator::PairValueType value_type;
       
   223     typedef typename Iterator::KeyType key_type;
       
   224     typedef typename Iterator::ValueType data_type;
       
   225     typedef typename Iterator::PairReferenceType reference;
       
   226     typedef typename Iterator::PairPointerType pointer;
       
   227     typedef typename ConstIterator::PairReferenceType const_reference;
       
   228     typedef typename ConstIterator::PairPointerType const_pointer;
       
   229     typedef int difference_type;		
   235   };
   230   };
   236   
   231   
   237   /// @}
   232   /// @}
   238   
   233   
   239 }
   234 }