src/work/deba/array_map_factory.h
changeset 703 32f280a5ed7d
parent 702 4207f82a1778
child 921 818510fa3d99
     1.1 --- a/src/work/deba/array_map_factory.h	Wed Jul 14 21:16:10 2004 +0000
     1.2 +++ b/src/work/deba/array_map_factory.h	Thu Jul 15 12:15:58 2004 +0000
     1.3 @@ -1,11 +1,10 @@
     1.4 +// -*- c++ -*-
     1.5  #ifndef ARRAY_MAP_H
     1.6  #define ARRAY_MAP_H
     1.7  
     1.8  #include <memory>
     1.9  
    1.10 -
    1.11 -#include <iostream>
    1.12 -using namespace std;
    1.13 +#include "extended_pair.h"
    1.14  
    1.15  namespace hugo {
    1.16  	
    1.17 @@ -19,7 +18,8 @@
    1.18  
    1.19      typedef typename MapRegistry::MapBase MapBase;
    1.20  		
    1.21 -    template <typename V, typename A = std::allocator<V> > class Map : public MapBase {
    1.22 +    template <typename V, typename A = std::allocator<V> > 
    1.23 +    class Map : public MapBase {
    1.24      
    1.25        public:
    1.26  
    1.27 @@ -30,23 +30,7 @@
    1.28        Map() : values(0), capacity(0) {}
    1.29  			
    1.30        Map(const Graph& g, MapRegistry& r) : MapBase(g, r) {
    1.31 -	int max_id = -1;
    1.32 -	for (KeyIt it(*getGraph()); getGraph()->valid(it); getGraph()->next(it)) {
    1.33 -	  int id = getGraph()->id(it);
    1.34 -	  if (id > max_id) {
    1.35 -	    max_id = id;
    1.36 -	  }			
    1.37 -	}
    1.38 -	if (max_id == -1) {
    1.39 -	  capacity = 0;
    1.40 -	  values = 0;
    1.41 -	  return;
    1.42 -	}
    1.43 -	capacity = 1;
    1.44 -	while (capacity <= max_id) {
    1.45 -	  capacity <<= 1;
    1.46 -	}
    1.47 -	values = allocator.allocate(capacity);
    1.48 +	allocate_memory();
    1.49  	for (KeyIt it(*getGraph()); getGraph()->valid(it); getGraph()->next(it)) {
    1.50  	  int id = getGraph()->id(it);
    1.51  	  allocator.construct(&(values[id]), Value());
    1.52 @@ -54,23 +38,7 @@
    1.53        }
    1.54  
    1.55        Map(const Graph& g, MapRegistry& r, const Value& v) : MapBase(g, r) {
    1.56 -	int max_id = -1;
    1.57 -	for (KeyIt it(*getGraph()); getGraph()->valid(it); getGraph()->next(it)) {
    1.58 -	  int id = getGraph()->id(it);
    1.59 -	  if (id > max_id) {
    1.60 -	    max_id = id;
    1.61 -	  }			
    1.62 -	}
    1.63 -	if (max_id == -1) {
    1.64 -	  capacity = 0;
    1.65 -	  values = 0;
    1.66 -	  return;
    1.67 -	}
    1.68 -	capacity = 1;
    1.69 -	while (capacity <= max_id) {
    1.70 -	  capacity <<= 1;
    1.71 -	}
    1.72 -	values = allocator.allocate(capacity);
    1.73 +	allocate_memory();
    1.74  	for (KeyIt it(*getGraph()); getGraph()->valid(it); getGraph()->next(it)) {
    1.75  	  int id = getGraph()->id(it);
    1.76  	  allocator.construct(&(values[id]), v);
    1.77 @@ -87,9 +55,10 @@
    1.78  	}
    1.79        }
    1.80  
    1.81 -      template <typename CMap> Map(const CMap& copy) : MapBase(copy) {
    1.82 +      template <typename CMap> Map(const CMap& copy) 
    1.83 +	: capacity(0), values(0), MapBase(copy) {
    1.84  	if (getGraph()) {
    1.85 -	  init();
    1.86 +	  allocate_memory();
    1.87  	  for (KeyIt it(*getGraph()); getGraph()->valid(it); getGraph()->next(it)) {
    1.88  	    set(it, copy[it]);
    1.89  	  }
    1.90 @@ -117,14 +86,12 @@
    1.91  	} 
    1.92  	this->MapBase::operator=(copy);
    1.93  	if (getGraph()) {
    1.94 -	  init();
    1.95 +	  allocate_memory();
    1.96  	  for (KeyIt it(*getGraph()); getGraph()->valid(it); getGraph()->next(it)) {
    1.97  	    set(it, copy[it]);
    1.98  	  }
    1.99  	}
   1.100        }
   1.101 -
   1.102 -       
   1.103  				
   1.104        virtual ~Map() {
   1.105  	if (capacity != 0) {
   1.106 @@ -181,36 +148,44 @@
   1.107  	allocator.destroy(&(values[id]));
   1.108        }
   1.109  	
   1.110 -      /** Compatible iterator with the stl maps' iterators.
   1.111 -       *  It iterates on pairs of a key and a value.
   1.112 -       */
   1.113        class iterator {
   1.114  	friend class Map;
   1.115  	friend class const_iterator;
   1.116 -	private:
   1.117 +      private:
   1.118  
   1.119  	/** Private constructor to initalize the the iterators returned
   1.120  	 *  by the begin() and end().
   1.121  	 */
   1.122  	iterator (Map& pmap, const KeyIt& pit) : map(&pmap), it(pit) {}
   1.123  
   1.124 -	public:
   1.125 +      public:
   1.126  
   1.127  	/** Default constructor. 
   1.128  	 */
   1.129  	iterator() {}
   1.130  
   1.131 +	typedef extended_pair<const Key&, const Key&, 
   1.132 +			      Value&, Value&> Reference;
   1.133 +
   1.134  	/** Dereference operator for map.
   1.135  	 */	 
   1.136 -	std::pair<const Key, Value> operator*() {
   1.137 -	  return std::pair<const Key, Value>(it, (*map)[it]);
   1.138 +	Reference operator*() {
   1.139 +	  return Reference(it, (*map)[it]);
   1.140  	}
   1.141  
   1.142 +	class Pointer {
   1.143 +	  friend class iterator;
   1.144 +	private:
   1.145 +	  Reference data;
   1.146 +	  Pointer(const Key& key, Value& val) : data(key, val) {}
   1.147 +	public:
   1.148 +	  Reference* operator->() {return &data;}
   1.149 +	};
   1.150 +
   1.151  	/** Arrow operator for map.
   1.152  	 */	 
   1.153 -	std::pair<const Key, Value>* operator->() {
   1.154 -	  static std::pair<const Key, Value> tmp = operator*();
   1.155 -	  return &tmp;
   1.156 +	Pointer operator->() {
   1.157 +	  return Pointer(it, ((*map)[it])); 
   1.158  	}
   1.159  
   1.160  	/** The pre increment operator of the map.
   1.161 @@ -239,8 +214,9 @@
   1.162  	bool operator!=(const_iterator p_it) {
   1.163  	  return !(*this == p_it);
   1.164  	}
   1.165 +
   1.166  	
   1.167 -	private:
   1.168 +      private:
   1.169  	Map* map;
   1.170  	KeyIt it;
   1.171        };
   1.172 @@ -260,14 +236,15 @@
   1.173        class const_iterator {
   1.174  	friend class Map;
   1.175  	friend class iterator;
   1.176 -	private:
   1.177 +      private:
   1.178  
   1.179  	/** Private constructor to initalize the the iterators returned
   1.180  	 *  by the begin() and end().
   1.181  	 */
   1.182 -	const_iterator (Map& pmap, const KeyIt& pit) : map(&pmap), it(pit) {}
   1.183 +	const_iterator (const Map& pmap, const KeyIt& pit) 
   1.184 +	  : map(&pmap), it(pit) {}
   1.185  
   1.186 -	public:
   1.187 +      public:
   1.188  
   1.189  	/** Default constructor. 
   1.190  	 */
   1.191 @@ -275,21 +252,31 @@
   1.192  
   1.193  	/** Constructor to convert iterator to const_iterator.
   1.194  	 */
   1.195 -	const_iterator(iterator p_it) {
   1.196 -	  it = p_it.it;
   1.197 -	}
   1.198 +	const_iterator(iterator p_it) : map(p_it.map), it(p_it.it) {}
   1.199        
   1.200 +	typedef extended_pair<const Key&, const Key&, 
   1.201 +	  const Value&, const Value&> Reference;
   1.202 +
   1.203  	/** Dereference operator for map.
   1.204  	 */	 
   1.205 -	std::pair<const Key, const Value> operator*() const {
   1.206 -	  return std::pair<const Key, const Value>(it, (*map)[it]);
   1.207 +	Reference operator*() {
   1.208 +	  return Reference(it, (*map)[it]);
   1.209  	}
   1.210  
   1.211 +
   1.212 +	class Pointer {
   1.213 +	  friend class const_iterator;
   1.214 +	private:
   1.215 +	  Reference data;
   1.216 +	  Pointer(const Key& key, const Value& val) : data(key, val) {}
   1.217 +	public:
   1.218 +	  Reference* operator->() {return &data;}
   1.219 +	};
   1.220 +
   1.221  	/** Arrow operator for map.
   1.222  	 */	 
   1.223 -	std::pair<const Key, const Value>* operator->() const {
   1.224 -	  static std::pair<const Key, const Value> tmp = operator*();
   1.225 -	  return &tmp;
   1.226 +	Pointer operator->() {
   1.227 +	  return Pointer(it, ((*map)[it])); 
   1.228  	}
   1.229  
   1.230  	/** The pre increment operator of the map.
   1.231 @@ -319,7 +306,8 @@
   1.232  	  return !(*this == p_it);
   1.233  	}
   1.234  	
   1.235 -	private:
   1.236 +
   1.237 +      private:
   1.238  	const Map* map;
   1.239  	KeyIt it;
   1.240        };
   1.241 @@ -336,7 +324,27 @@
   1.242  	return const_iterator(*this, INVALID);
   1.243        }
   1.244  
   1.245 -      private:
   1.246 +    private:
   1.247 +      
   1.248 +      void allocate_memory() {
   1.249 +	int max_id = -1;
   1.250 +	for (KeyIt it(*getGraph()); getGraph()->valid(it); getGraph()->next(it)) {
   1.251 +	  int id = getGraph()->id(it);
   1.252 +	  if (id > max_id) {
   1.253 +	    max_id = id;
   1.254 +	  }			
   1.255 +	}
   1.256 +	if (max_id == -1) {
   1.257 +	  capacity = 0;
   1.258 +	  values = 0;
   1.259 +	  return;
   1.260 +	}
   1.261 +	capacity = 1;
   1.262 +	while (capacity <= max_id) {
   1.263 +	  capacity <<= 1;
   1.264 +	}
   1.265 +	values = allocator.allocate(capacity);	
   1.266 +      }      
   1.267        int capacity;
   1.268        Value* values;
   1.269        Allocator allocator;