(none)
authordeba
Wed, 14 Jul 2004 21:16:10 +0000
changeset 7024207f82a1778
parent 701 c03e073b8394
child 703 32f280a5ed7d
(none)
src/work/deba/array_map_factory.h
src/work/deba/extended_pair.h
src/work/deba/list_graph.h
src/work/deba/main.cpp
src/work/deba/vector_map_factory.h
     1.1 --- a/src/work/deba/array_map_factory.h	Wed Jul 14 10:06:27 2004 +0000
     1.2 +++ b/src/work/deba/array_map_factory.h	Wed Jul 14 21:16:10 2004 +0000
     1.3 @@ -19,108 +19,328 @@
     1.4  
     1.5      typedef typename MapRegistry::MapBase MapBase;
     1.6  		
     1.7 -    template <typename V, typename A = std::allocator<V> > 
     1.8 -      class Map : public MapBase {
     1.9 +    template <typename V, typename A = std::allocator<V> > class Map : public MapBase {
    1.10      
    1.11 -	public:
    1.12 +      public:
    1.13  
    1.14 -	typedef V Value;
    1.15 -	typedef A Allocator;
    1.16 +      typedef V Value;
    1.17 +      typedef A Allocator;
    1.18  
    1.19  	
    1.20 -	Map() : values(0), capacity(0) {}
    1.21 +      Map() : values(0), capacity(0) {}
    1.22  			
    1.23 -	Map(Graph& g, MapRegistry& r) : MapBase(g, r) {
    1.24 -	  int max_id = -1;
    1.25 -	  for (KeyIt it(*graph); graph->valid(it); graph->next(it)) {
    1.26 -	    int id = graph->id(it);
    1.27 -	    if (id > max_id) {
    1.28 -	      max_id = id;
    1.29 -	    }			
    1.30 -	  }
    1.31 -	  if (max_id == -1) {
    1.32 -	    capacity = 0;
    1.33 -	    values = 0;
    1.34 -	    return;
    1.35 -	  }
    1.36 -	  capacity = 1;
    1.37 -	  while (capacity <= max_id) {
    1.38 -	    capacity <<= 1;
    1.39 -	  }
    1.40 -	  values = allocator.allocate(capacity);
    1.41 -	  for (KeyIt it(*graph); graph->valid(it); graph->next(it)) {
    1.42 -	    int id = graph->id(it);
    1.43 -	    allocator.construct(&(values[id]), Value());
    1.44 -	  }								
    1.45 +      Map(const Graph& g, MapRegistry& r) : MapBase(g, r) {
    1.46 +	int max_id = -1;
    1.47 +	for (KeyIt it(*getGraph()); getGraph()->valid(it); getGraph()->next(it)) {
    1.48 +	  int id = getGraph()->id(it);
    1.49 +	  if (id > max_id) {
    1.50 +	    max_id = id;
    1.51 +	  }			
    1.52  	}
    1.53 +	if (max_id == -1) {
    1.54 +	  capacity = 0;
    1.55 +	  values = 0;
    1.56 +	  return;
    1.57 +	}
    1.58 +	capacity = 1;
    1.59 +	while (capacity <= max_id) {
    1.60 +	  capacity <<= 1;
    1.61 +	}
    1.62 +	values = allocator.allocate(capacity);
    1.63 +	for (KeyIt it(*getGraph()); getGraph()->valid(it); getGraph()->next(it)) {
    1.64 +	  int id = getGraph()->id(it);
    1.65 +	  allocator.construct(&(values[id]), Value());
    1.66 +	}								
    1.67 +      }
    1.68  
    1.69 -	Map(const Map& copy) : MapBase(*copy.graph, *copy.registry) {
    1.70 -	  capacity = copy.capacity;
    1.71 -	  values = allocator.allocate(capacity);
    1.72 -	  for (KeyIt it(*graph); graph->valid(it); graph->next(it)) {
    1.73 -	    int id = graph->id(it);
    1.74 -	    allocator.construct(&(values[id]), copy.values[id]);
    1.75 +      Map(const Graph& g, MapRegistry& r, const Value& v) : MapBase(g, r) {
    1.76 +	int max_id = -1;
    1.77 +	for (KeyIt it(*getGraph()); getGraph()->valid(it); getGraph()->next(it)) {
    1.78 +	  int id = getGraph()->id(it);
    1.79 +	  if (id > max_id) {
    1.80 +	    max_id = id;
    1.81 +	  }			
    1.82 +	}
    1.83 +	if (max_id == -1) {
    1.84 +	  capacity = 0;
    1.85 +	  values = 0;
    1.86 +	  return;
    1.87 +	}
    1.88 +	capacity = 1;
    1.89 +	while (capacity <= max_id) {
    1.90 +	  capacity <<= 1;
    1.91 +	}
    1.92 +	values = allocator.allocate(capacity);
    1.93 +	for (KeyIt it(*getGraph()); getGraph()->valid(it); getGraph()->next(it)) {
    1.94 +	  int id = getGraph()->id(it);
    1.95 +	  allocator.construct(&(values[id]), v);
    1.96 +	}								
    1.97 +      }
    1.98 +
    1.99 +      Map(const Map& copy) : MapBase(*copy.graph, *copy.registry) {
   1.100 +	capacity = copy.capacity;
   1.101 +	if (capacity == 0) return;
   1.102 +	values = allocator.allocate(capacity);
   1.103 +	for (KeyIt it(*getGraph()); getGraph()->valid(it); getGraph()->next(it)) {
   1.104 +	  int id = getGraph()->id(it);
   1.105 +	  allocator.construct(&(values[id]), copy.values[id]);
   1.106 +	}
   1.107 +      }
   1.108 +
   1.109 +      template <typename CMap> Map(const CMap& copy) : MapBase(copy) {
   1.110 +	if (getGraph()) {
   1.111 +	  init();
   1.112 +	  for (KeyIt it(*getGraph()); getGraph()->valid(it); getGraph()->next(it)) {
   1.113 +	    set(it, copy[it]);
   1.114  	  }
   1.115  	}
   1.116 -				
   1.117 -	virtual ~Map() {
   1.118 +      }
   1.119 +
   1.120 +      Map& operator=(const Map& copy) {
   1.121 +	if (&copy == this) return;
   1.122 +	if (capacity != 0) {
   1.123  	  destroy();
   1.124  	  allocator.deallocate(values, capacity);
   1.125  	}
   1.126 +	capacity = copy.capacity;
   1.127 +	if (capacity == 0) return;
   1.128 +	values = allocator.allocate(capacity);
   1.129 +	for (KeyIt it(getGraph()); getGraph()->valid(it); getGraph()->next(it)) {
   1.130 +	  int id = getGraph()->id(it);
   1.131 +	  allocator.construct(&(values[id]), copy.values[id]);
   1.132 +	}
   1.133 +      }
   1.134 +
   1.135 +      template <typename CMap> Map& operator=(const CMap& copy) {
   1.136 +	if (getGraph()) {
   1.137 +	  destroy();
   1.138 +	} 
   1.139 +	this->MapBase::operator=(copy);
   1.140 +	if (getGraph()) {
   1.141 +	  init();
   1.142 +	  for (KeyIt it(*getGraph()); getGraph()->valid(it); getGraph()->next(it)) {
   1.143 +	    set(it, copy[it]);
   1.144 +	  }
   1.145 +	}
   1.146 +      }
   1.147 +
   1.148 +       
   1.149 +				
   1.150 +      virtual ~Map() {
   1.151 +	if (capacity != 0) {
   1.152 +	  destroy();
   1.153 +	  allocator.deallocate(values, capacity);
   1.154 +	}
   1.155 +      }
   1.156  	
   1.157  	
   1.158 -	Value& operator[](const Key& key) {
   1.159 -	  int id = graph->id(key);
   1.160 -	  return values[id];
   1.161 -	} 
   1.162 +      Value& operator[](const Key& key) {
   1.163 +	int id = getGraph()->id(key);
   1.164 +	return values[id];
   1.165 +      } 
   1.166  		
   1.167 -	const Value& operator[](const Key& key) const {
   1.168 -	  int id = graph->id(key);
   1.169 -	  return values[id];
   1.170 +      const Value& operator[](const Key& key) const {
   1.171 +	int id = getGraph()->id(key);
   1.172 +	return values[id];
   1.173 +      }
   1.174 +	
   1.175 +      const Value& get(const Key& key) const {
   1.176 +	int id = getGraph()->id(key);
   1.177 +	return values[id];
   1.178 +      } 
   1.179 +		
   1.180 +      void set(const Key& key, const Value& val) {
   1.181 +	int id = getGraph()->id(key);
   1.182 +	values[id] = val;
   1.183 +      }
   1.184 +		
   1.185 +      void add(const Key& key) {
   1.186 +	int id = getGraph()->id(key);
   1.187 +	if (id >= capacity) {
   1.188 +	  int new_capacity = (capacity == 0 ? 1 : capacity);
   1.189 +	  while (new_capacity <= id) {
   1.190 +	    new_capacity <<= 1;
   1.191 +	  }
   1.192 +	  Value* new_values = allocator.allocate(new_capacity);;
   1.193 +	  for (KeyIt it(*getGraph()); getGraph()->valid(it); getGraph()->next(it)) {
   1.194 +	    int jd = getGraph()->id(it);
   1.195 +	    if (id != jd) {
   1.196 +	      allocator.construct(&(new_values[jd]), values[jd]);
   1.197 +	      allocator.destroy(&(values[jd]));
   1.198 +	    }
   1.199 +	  }
   1.200 +	  if (capacity != 0) allocator.deallocate(values, capacity);
   1.201 +	  values = new_values;
   1.202 +	  capacity = new_capacity;
   1.203 +	}
   1.204 +	allocator.construct(&(values[id]), Value());
   1.205 +      }
   1.206 +		
   1.207 +      void erase(const Key& key) {
   1.208 +	int id = getGraph()->id(key);
   1.209 +	allocator.destroy(&(values[id]));
   1.210 +      }
   1.211 +	
   1.212 +      /** Compatible iterator with the stl maps' iterators.
   1.213 +       *  It iterates on pairs of a key and a value.
   1.214 +       */
   1.215 +      class iterator {
   1.216 +	friend class Map;
   1.217 +	friend class const_iterator;
   1.218 +	private:
   1.219 +
   1.220 +	/** Private constructor to initalize the the iterators returned
   1.221 +	 *  by the begin() and end().
   1.222 +	 */
   1.223 +	iterator (Map& pmap, const KeyIt& pit) : map(&pmap), it(pit) {}
   1.224 +
   1.225 +	public:
   1.226 +
   1.227 +	/** Default constructor. 
   1.228 +	 */
   1.229 +	iterator() {}
   1.230 +
   1.231 +	/** Dereference operator for map.
   1.232 +	 */	 
   1.233 +	std::pair<const Key, Value> operator*() {
   1.234 +	  return std::pair<const Key, Value>(it, (*map)[it]);
   1.235 +	}
   1.236 +
   1.237 +	/** Arrow operator for map.
   1.238 +	 */	 
   1.239 +	std::pair<const Key, Value>* operator->() {
   1.240 +	  static std::pair<const Key, Value> tmp = operator*();
   1.241 +	  return &tmp;
   1.242 +	}
   1.243 +
   1.244 +	/** The pre increment operator of the map.
   1.245 +	 */
   1.246 +	iterator& operator++() { 
   1.247 +	  map->getGraph()->next(it); 
   1.248 +	  return *this; 
   1.249 +	}
   1.250 +
   1.251 +	/** The post increment operator of the map.
   1.252 +	 */
   1.253 +	iterator operator++(int) { 
   1.254 +	  iterator tmp(it); 
   1.255 +	  map.getGraph()->next(it); 
   1.256 +	  return tmp; 
   1.257 +	}
   1.258 +
   1.259 +	/** The equality operator of the map.
   1.260 +	 */
   1.261 +	bool operator==(const_iterator p_it) {
   1.262 +	  return p_it.it == it;
   1.263  	}
   1.264  	
   1.265 -	const Value& get(const Key& key) const {
   1.266 -	  int id = graph->id(key);
   1.267 -	  return values[id];
   1.268 -	} 
   1.269 -		
   1.270 -	void set(const Key& key, const Value& val) {
   1.271 -	  int id = graph->id(key);
   1.272 -	  values[id] = val;
   1.273 -	}
   1.274 -		
   1.275 -	void add(const Key& key) {
   1.276 -	  int id = graph->id(key);
   1.277 -	  if (id >= capacity) {
   1.278 -	    int new_capacity = (capacity == 0 ? 1 : capacity);
   1.279 -	    while (new_capacity <= id) {
   1.280 -	      new_capacity <<= 1;
   1.281 -	    }
   1.282 -	    Value* new_values = allocator.allocate(new_capacity);;
   1.283 -	    for (KeyIt it(*graph); graph->valid(it); graph->next(it)) {
   1.284 -	      int jd = graph->id(it);
   1.285 -	      if (id != jd) {
   1.286 -		allocator.construct(&(new_values[jd]), values[jd]);
   1.287 -		allocator.destroy(&(values[jd]));
   1.288 -	      }
   1.289 -	    }
   1.290 -	    if (capacity != 0) allocator.deallocate(values, capacity);
   1.291 -	    values = new_values;
   1.292 -	    capacity = new_capacity;
   1.293 -	  }
   1.294 -	  allocator.construct(&(values[id]), Value());
   1.295 -	}
   1.296 -		
   1.297 -	void erase(const Key& key) {
   1.298 -	  int id = graph->id(key);
   1.299 -	  allocator.destroy(&(values[id]));
   1.300 +	/** The not-equality operator of the map.
   1.301 +	 */
   1.302 +	bool operator!=(const_iterator p_it) {
   1.303 +	  return !(*this == p_it);
   1.304  	}
   1.305  	
   1.306  	private:
   1.307 -	int capacity;
   1.308 -	Value* values;
   1.309 -	Allocator allocator;
   1.310 -      };		
   1.311 +	Map* map;
   1.312 +	KeyIt it;
   1.313 +      };
   1.314 +
   1.315 +      /** Returns the begin iterator of the map.
   1.316 +       */
   1.317 +      iterator begin() {
   1.318 +	return iterator(*this, KeyIt(*getGraph()));
   1.319 +      }
   1.320 +
   1.321 +      /** Returns the end iterator of the map.
   1.322 +       */
   1.323 +      iterator end() {
   1.324 +	return iterator(*this, INVALID);
   1.325 +      }
   1.326 +
   1.327 +      class const_iterator {
   1.328 +	friend class Map;
   1.329 +	friend class iterator;
   1.330 +	private:
   1.331 +
   1.332 +	/** Private constructor to initalize the the iterators returned
   1.333 +	 *  by the begin() and end().
   1.334 +	 */
   1.335 +	const_iterator (Map& pmap, const KeyIt& pit) : map(&pmap), it(pit) {}
   1.336 +
   1.337 +	public:
   1.338 +
   1.339 +	/** Default constructor. 
   1.340 +	 */
   1.341 +	const_iterator() {}
   1.342 +
   1.343 +	/** Constructor to convert iterator to const_iterator.
   1.344 +	 */
   1.345 +	const_iterator(iterator p_it) {
   1.346 +	  it = p_it.it;
   1.347 +	}
   1.348 +      
   1.349 +	/** Dereference operator for map.
   1.350 +	 */	 
   1.351 +	std::pair<const Key, const Value> operator*() const {
   1.352 +	  return std::pair<const Key, const Value>(it, (*map)[it]);
   1.353 +	}
   1.354 +
   1.355 +	/** Arrow operator for map.
   1.356 +	 */	 
   1.357 +	std::pair<const Key, const Value>* operator->() const {
   1.358 +	  static std::pair<const Key, const Value> tmp = operator*();
   1.359 +	  return &tmp;
   1.360 +	}
   1.361 +
   1.362 +	/** The pre increment operator of the map.
   1.363 +	 */
   1.364 +	const_iterator& operator++() { 
   1.365 +	  map->getGraph()->next(it); 
   1.366 +	  return *this; 
   1.367 +	}
   1.368 +
   1.369 +	/** The post increment operator of the map.
   1.370 +	 */
   1.371 +	const_iterator operator++(int) { 
   1.372 +	  const_iterator tmp(it); 
   1.373 +	  map->getGraph()->next(it); 
   1.374 +	  return tmp; 
   1.375 +	}
   1.376 +
   1.377 +	/** The equality operator of the map.
   1.378 +	 */
   1.379 +	bool operator==(const_iterator p_it) {
   1.380 +	  return p_it.it == it;
   1.381 +	}
   1.382 +	
   1.383 +	/** The not-equality operator of the map.
   1.384 +	 */
   1.385 +	bool operator!=(const_iterator p_it) {
   1.386 +	  return !(*this == p_it);
   1.387 +	}
   1.388 +	
   1.389 +	private:
   1.390 +	const Map* map;
   1.391 +	KeyIt it;
   1.392 +      };
   1.393 +
   1.394 +      /** Returns the begin const_iterator of the map.
   1.395 +       */
   1.396 +      const_iterator begin() const {
   1.397 +	return const_iterator(*this, KeyIt(*getGraph()));
   1.398 +      }
   1.399 +
   1.400 +      /** Returns the end const_iterator of the map.
   1.401 +       */
   1.402 +      const_iterator end() const {
   1.403 +	return const_iterator(*this, INVALID);
   1.404 +      }
   1.405 +
   1.406 +      private:
   1.407 +      int capacity;
   1.408 +      Value* values;
   1.409 +      Allocator allocator;
   1.410 +    };		
   1.411    };
   1.412  }
   1.413  
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/src/work/deba/extended_pair.h	Wed Jul 14 21:16:10 2004 +0000
     2.3 @@ -0,0 +1,15 @@
     2.4 +#ifndef EXTENDED_PAIR_H
     2.5 +#define EXTENDED_PAIR_H
     2.6 +
     2.7 +template <typename T1, typename A1, typename T2, typename A2>
     2.8 +struct extended_pair {
     2.9 +  typedef T1 first_type;
    2.10 +  typedef T2 second_type;
    2.11 +
    2.12 +  extended_pair(A1 f, A2 s) : first(f), second(s) {}
    2.13 +
    2.14 +  T1 first;
    2.15 +  T2 second;
    2.16 +};
    2.17 +
    2.18 +#endif
     3.1 --- a/src/work/deba/list_graph.h	Wed Jul 14 10:06:27 2004 +0000
     3.2 +++ b/src/work/deba/list_graph.h	Wed Jul 14 21:16:10 2004 +0000
     3.3 @@ -77,7 +77,6 @@
     3.4      
     3.5      CREATE_MAP_REGISTRIES;
     3.6      CREATE_MAPS(VectorMapFactory);
     3.7 -
     3.8    public:
     3.9  
    3.10      ListGraph() : nodes(), first_node(-1),
     4.1 --- a/src/work/deba/main.cpp	Wed Jul 14 10:06:27 2004 +0000
     4.2 +++ b/src/work/deba/main.cpp	Wed Jul 14 21:16:10 2004 +0000
     4.3 @@ -23,6 +23,7 @@
     4.4    for (pit = map.begin(); pit != map.end(); ++pit) {
     4.5      cout << g.id(pit->first) << ' ' << pit->second << endl;
     4.6    }
     4.7 +  /*
     4.8    ListGraph::NodeMap<double> ot_map = map;
     4.9    for (ListGraph::NodeIt it(g); g.valid(it); g.next(it)) {
    4.10      ot_map[it] *= 2.1;
    4.11 @@ -32,7 +33,7 @@
    4.12    for (ListGraph::NodeIt it(g); g.valid(it); g.next(it)) {
    4.13      ot_map[it] *= 3.1;
    4.14      cout << ot_map[it] << endl;
    4.15 -  }
    4.16 +    }*/
    4.17    return 0;
    4.18  }
    4.19  
     5.1 --- a/src/work/deba/vector_map_factory.h	Wed Jul 14 10:06:27 2004 +0000
     5.2 +++ b/src/work/deba/vector_map_factory.h	Wed Jul 14 21:16:10 2004 +0000
     5.3 @@ -4,6 +4,8 @@
     5.4  #include <vector>
     5.5  #include <iostream>
     5.6  
     5.7 +#include "extended_pair.h"
     5.8 +
     5.9  namespace hugo {
    5.10  
    5.11    /** The VectorMapFactory template class is a factory class
    5.12 @@ -155,11 +157,21 @@
    5.13  	  return std::pair<const Key, Value>(it, (*map)[it]);
    5.14  	}
    5.15  
    5.16 +
    5.17 +	class Pointer {
    5.18 +	  friend class iterator;
    5.19 +	private:
    5.20 +	  typedef extended_pair<const Key&, const Key&, Value&, Value&> Pair;
    5.21 +	  Pair data;
    5.22 +	  Pointer(const Key& key, Value& val) : data(key, val) {}
    5.23 +	public:
    5.24 +	  Pair* operator->() {return &data;}
    5.25 +	};
    5.26 +
    5.27  	/** Arrow operator for map.
    5.28  	 */	 
    5.29 -	std::pair<const Key, Value>* operator->() {
    5.30 -	  static std::pair<const Key, Value> tmp = operator*();
    5.31 -	  return &tmp;
    5.32 +	Pointer operator->() {
    5.33 +	  return Pointer(it, ((*map)[it])); 
    5.34  	}
    5.35  
    5.36  	/** The pre increment operator of the map.
    5.37 @@ -188,6 +200,7 @@
    5.38  	bool operator!=(const_iterator p_it) {
    5.39  	  return !(*this == p_it);
    5.40  	}
    5.41 +
    5.42  	
    5.43        private:
    5.44  	Map* map;
    5.45 @@ -234,11 +247,19 @@
    5.46  	  return std::pair<const Key, const Value>(it, (*map)[it]);
    5.47  	}
    5.48  
    5.49 +	class Pointer {
    5.50 +	  friend class const_iterator;
    5.51 +	private:
    5.52 +	  typedef extended_pair<const Key&, const Key&, const Value&, const Value&> Pair;
    5.53 +	  Pair data;
    5.54 +	  Pointer(const Key& key, const Value& val) : data(key, val) {}
    5.55 +	public:
    5.56 +	  Pair* operator->() {return &data;}
    5.57 +	};
    5.58  	/** Arrow operator for map.
    5.59  	 */	 
    5.60 -	std::pair<const Key, const Value>* operator->() const {
    5.61 -	  static std::pair<const Key, const Value> tmp = operator*();
    5.62 -	  return &tmp;
    5.63 +	Pointer operator->() const {
    5.64 +	  return Pointer(it, (*map)[it]);
    5.65  	}
    5.66  
    5.67  	/** The pre increment operator of the map.
    5.68 @@ -268,6 +289,7 @@
    5.69  	  return !(*this == p_it);
    5.70  	}
    5.71  	
    5.72 +
    5.73        private:
    5.74  	const Map* map;
    5.75  	KeyIt it;