*** empty log message ***
authordeba
Wed, 14 Jul 2004 10:05:31 +0000
changeset 700236117f60eee
parent 699 59f8d173968e
child 701 c03e073b8394
*** empty log message ***
src/work/deba/vector_map_factory.h
     1.1 --- a/src/work/deba/vector_map_factory.h	Tue Jul 13 07:19:34 2004 +0000
     1.2 +++ b/src/work/deba/vector_map_factory.h	Wed Jul 14 10:05:31 2004 +0000
     1.3 @@ -2,95 +2,297 @@
     1.4  #define VECTOR_MAP_H
     1.5  
     1.6  #include <vector>
     1.7 +#include <iostream>
     1.8  
     1.9  namespace hugo {
    1.10 +
    1.11 +  /** The VectorMapFactory template class is a factory class
    1.12 +   *  to create maps for the edge and nodes. This map factory
    1.13 +   *  use the std::vector to implement the container function.
    1.14 +   *
    1.15 +   *  The template parameter is the MapRegistry that the maps
    1.16 +   *  will belong to.
    1.17 +   */
    1.18  	
    1.19    template <typename MapRegistry>
    1.20 -    class VectorMapFactory {
    1.21 -    public:
    1.22 +  class VectorMapFactory {
    1.23 +  public:
    1.24  		
    1.25 +    /// The graph type of the maps. 
    1.26      typedef typename MapRegistry::Graph Graph;
    1.27 +    /// The key type of the maps.
    1.28      typedef typename MapRegistry::Key Key;
    1.29 +    /// The iterator to iterate on the keys.
    1.30      typedef typename MapRegistry::KeyIt KeyIt;
    1.31  
    1.32 +    /// The MapBase of the Map which imlements the core regisitry function.
    1.33      typedef typename MapRegistry::MapBase MapBase;
    1.34  
    1.35  		
    1.36 +    /** The template Map type.
    1.37 +     */
    1.38      template <typename V> 
    1.39 -      class Map : public MapBase {
    1.40 -      public:
    1.41 +    class Map : public MapBase {
    1.42 +    public:
    1.43 +
    1.44 +      /// The value type of the map.
    1.45        typedef V Value;
    1.46 -	
    1.47 -      typedef std::vector<Value> Container;
    1.48 +
    1.49 +      typedef std::vector<Value> Container;	
    1.50 +
    1.51 +      /** Default constructor for the map.
    1.52 +       */
    1.53        Map() {}
    1.54 -			
    1.55 -      Map(Graph& g, MapRegistry& r) : MapBase(g, r) {
    1.56 +		
    1.57 +      /** Graph and Registry initialized map constructor.
    1.58 +       */
    1.59 +      Map(const Graph& g, MapRegistry& r) : MapBase(g, r) {
    1.60  	init();
    1.61        }
    1.62 -			
    1.63 -						
    1.64 +
    1.65 +      /** Constructor to use default value to initialize the map. 
    1.66 +       */
    1.67 +      Map(const Graph& g, MapRegistry& r, const Value& v) : MapBase(g, r) {
    1.68 +	init();
    1.69 +	for (KeyIt it(*getGraph()); getGraph()->valid(it); getGraph()->next(it)) {
    1.70 +          set(it, v);
    1.71 +        }
    1.72 +      }
    1.73 +
    1.74 +      /** Constructor to copy a map of an other map type.
    1.75 +       */
    1.76 +      template <typename CMap> Map(const CMap& copy) : MapBase(copy) {
    1.77 +	if (getGraph()) {
    1.78 +	  init();
    1.79 +	  for (KeyIt it(*getGraph()); getGraph()->valid(it); getGraph()->next(it)) {
    1.80 +	    set(it, copy[it]);
    1.81 +	  }
    1.82 +	}
    1.83 +      }
    1.84 +
    1.85 +      /** Assign operator to copy a map an other map type.
    1.86 +       */
    1.87 +      template <typename CMap> Map& operator=(const CMap& copy) {
    1.88 +	if (getGraph()) {
    1.89 +	  destroy();
    1.90 +	} 
    1.91 +	this->MapBase::operator=(copy);
    1.92 +	if (getGraph()) {
    1.93 +	  init();
    1.94 +	  for (KeyIt it(*getGraph()); getGraph()->valid(it); getGraph()->next(it)) {
    1.95 +	    set(it, copy[it]);
    1.96 +	  }
    1.97 +	}
    1.98 +      }
    1.99 +
   1.100 +      /** The destructor of the map.
   1.101 +       */
   1.102        virtual ~Map() {
   1.103  	destroy();
   1.104        }
   1.105 -	
   1.106 -	
   1.107 +		
   1.108 +      /**
   1.109 +       * The subscript operator. The map can be subscripted by the
   1.110 +       * actual keys of the graph. 
   1.111 +       */
   1.112        typename Container::reference operator[](const Key& key) {
   1.113 -	int id = graph->id(key);
   1.114 +	int id = getGraph()->id(key);
   1.115  	return container[id];
   1.116        } 
   1.117  		
   1.118 +      /**
   1.119 +       * The const subscript operator. The map can be subscripted by the
   1.120 +       * actual keys of the graph. 
   1.121 +       */
   1.122        typename Container::const_reference operator[](const Key& key) const {
   1.123 -	int id = graph->id(key);
   1.124 +	int id = getGraph()->id(key);
   1.125  	return container[id];
   1.126        }
   1.127 -	
   1.128 +
   1.129 +      /** Setter function of the map. Equivalent with map[key] = val.
   1.130 +       *  This is a compatibility feature with the not dereferable maps.
   1.131 +       */
   1.132        void set(const Key& key, const Value& val) {
   1.133 -	int id = graph->id(key);
   1.134 +	int id = getGraph()->id(key);
   1.135  	container[id] = val;
   1.136        }
   1.137  		
   1.138 +      /** Add a new key to the map. It called by the map registry.
   1.139 +       */
   1.140        void add(const Key& key) {
   1.141 -	int id = graph->id(key);
   1.142 +	int id = getGraph()->id(key);
   1.143  	if (id >= container.size()) {
   1.144  	  container.resize(id + 1);
   1.145  	}
   1.146        }
   1.147  		
   1.148 +      /** Erease a key from the map. It called by the map registry.
   1.149 +       */
   1.150        void erase(const Key& key) {}
   1.151 -	
   1.152 -      class const_iterator {
   1.153  
   1.154 +      /** Compatible iterator with the stl maps' iterators.
   1.155 +       *  It iterates on pairs of a key and a value.
   1.156 +       */
   1.157 +      class iterator {
   1.158 +	friend class Map;
   1.159 +	friend class const_iterator;
   1.160        private:
   1.161 -      
   1.162 -      };
   1.163  
   1.164 -      class iterator {
   1.165 +	/** Private constructor to initalize the the iterators returned
   1.166 +	 *  by the begin() and end().
   1.167 +	 */
   1.168 +	iterator (Map& pmap, const KeyIt& pit) : map(&pmap), it(pit) {}
   1.169 +
   1.170        public:
   1.171 +
   1.172 +	/** Default constructor. 
   1.173 +	 */
   1.174  	iterator() {}
   1.175 -      
   1.176 -	std::pair<const Key&, Value&> operator*() {
   1.177 -	  return std::pair<const Key&, Value&>(static_cast<Key&>(it), map[it]);
   1.178 +
   1.179 +	/** Dereference operator for map.
   1.180 +	 */	 
   1.181 +	std::pair<const Key, Value> operator*() {
   1.182 +	  return std::pair<const Key, Value>(it, (*map)[it]);
   1.183  	}
   1.184  
   1.185 -	iterator& operator++() { ++it; return *this; }
   1.186 -	iterator operator++(int) { iterator tmp(it); ++it; return tmp; }
   1.187 +	/** Arrow operator for map.
   1.188 +	 */	 
   1.189 +	std::pair<const Key, Value>* operator->() {
   1.190 +	  static std::pair<const Key, Value> tmp = operator*();
   1.191 +	  return &tmp;
   1.192 +	}
   1.193 +
   1.194 +	/** The pre increment operator of the map.
   1.195 +	 */
   1.196 +	iterator& operator++() { 
   1.197 +	  map->getGraph()->next(it); 
   1.198 +	  return *this; 
   1.199 +	}
   1.200 +
   1.201 +	/** The post increment operator of the map.
   1.202 +	 */
   1.203 +	iterator operator++(int) { 
   1.204 +	  iterator tmp(it); 
   1.205 +	  map.getGraph()->next(it); 
   1.206 +	  return tmp; 
   1.207 +	}
   1.208 +
   1.209 +	/** The equality operator of the map.
   1.210 +	 */
   1.211 +	bool operator==(const_iterator p_it) {
   1.212 +	  return p_it.it == it;
   1.213 +	}
   1.214 +	
   1.215 +	/** The not-equality operator of the map.
   1.216 +	 */
   1.217 +	bool operator!=(const_iterator p_it) {
   1.218 +	  return !(*this == p_it);
   1.219 +	}
   1.220 +	
   1.221        private:
   1.222 -	Map& map;
   1.223 +	Map* map;
   1.224  	KeyIt it;
   1.225        };
   1.226  
   1.227 +      /** Returns the begin iterator of the map.
   1.228 +       */
   1.229 +      iterator begin() {
   1.230 +	return iterator(*this, KeyIt(*getGraph()));
   1.231 +      }
   1.232 +
   1.233 +      /** Returns the end iterator of the map.
   1.234 +       */
   1.235 +      iterator end() {
   1.236 +	return iterator(*this, INVALID);
   1.237 +      }
   1.238 +
   1.239 +      class const_iterator {
   1.240 +	friend class Map;
   1.241 +	friend class iterator;
   1.242        private:
   1.243 -      typedef std::vector<Value> Container;
   1.244 +
   1.245 +	/** Private constructor to initalize the the iterators returned
   1.246 +	 *  by the begin() and end().
   1.247 +	 */
   1.248 +	const_iterator (Map& pmap, const KeyIt& pit) : map(&pmap), it(pit) {}
   1.249 +
   1.250 +      public:
   1.251 +
   1.252 +	/** Default constructor. 
   1.253 +	 */
   1.254 +	const_iterator() {}
   1.255 +
   1.256 +	/** Constructor to convert iterator to const_iterator.
   1.257 +	 */
   1.258 +	const_iterator(iterator p_it) {
   1.259 +	  it = p_it.it;
   1.260 +	}
   1.261 +      
   1.262 +	/** Dereference operator for map.
   1.263 +	 */	 
   1.264 +	std::pair<const Key, const Value> operator*() const {
   1.265 +	  return std::pair<const Key, const Value>(it, (*map)[it]);
   1.266 +	}
   1.267 +
   1.268 +	/** Arrow operator for map.
   1.269 +	 */	 
   1.270 +	std::pair<const Key, const Value>* operator->() const {
   1.271 +	  static std::pair<const Key, const Value> tmp = operator*();
   1.272 +	  return &tmp;
   1.273 +	}
   1.274 +
   1.275 +	/** The pre increment operator of the map.
   1.276 +	 */
   1.277 +	const_iterator& operator++() { 
   1.278 +	  map->getGraph()->next(it); 
   1.279 +	  return *this; 
   1.280 +	}
   1.281 +
   1.282 +	/** The post increment operator of the map.
   1.283 +	 */
   1.284 +	const_iterator operator++(int) { 
   1.285 +	  const_iterator tmp(it); 
   1.286 +	  map->getGraph()->next(it); 
   1.287 +	  return tmp; 
   1.288 +	}
   1.289 +
   1.290 +	/** The equality operator of the map.
   1.291 +	 */
   1.292 +	bool operator==(const_iterator p_it) {
   1.293 +	  return p_it.it == it;
   1.294 +	}
   1.295 +	
   1.296 +	/** The not-equality operator of the map.
   1.297 +	 */
   1.298 +	bool operator!=(const_iterator p_it) {
   1.299 +	  return !(*this == p_it);
   1.300 +	}
   1.301 +	
   1.302 +      private:
   1.303 +	const Map* map;
   1.304 +	KeyIt it;
   1.305 +      };
   1.306 +
   1.307 +      /** Returns the begin const_iterator of the map.
   1.308 +       */
   1.309 +      const_iterator begin() const {
   1.310 +	return const_iterator(*this, KeyIt(*getGraph()));
   1.311 +      }
   1.312 +
   1.313 +      /** Returns the end const_iterator of the map.
   1.314 +       */
   1.315 +      const_iterator end() const {
   1.316 +	return const_iterator(*this, INVALID);
   1.317 +      }
   1.318 +
   1.319 +      private:
   1.320  		
   1.321        Container container;
   1.322  
   1.323 -
   1.324      };
   1.325 -
   1.326 -    
   1.327 -
   1.328  		
   1.329    };
   1.330 +
   1.331  }
   1.332  
   1.333  #endif