src/work/deba/array_map_factory.h
changeset 755 a8c2e828ce0b
parent 702 4207f82a1778
child 921 818510fa3d99
equal deleted inserted replaced
2:89a38e4d06ea 3:bcf19ace03ff
       
     1 // -*- c++ -*-
     1 #ifndef ARRAY_MAP_H
     2 #ifndef ARRAY_MAP_H
     2 #define ARRAY_MAP_H
     3 #define ARRAY_MAP_H
     3 
     4 
     4 #include <memory>
     5 #include <memory>
     5 
     6 
     6 
     7 #include "extended_pair.h"
     7 #include <iostream>
       
     8 using namespace std;
       
     9 
     8 
    10 namespace hugo {
     9 namespace hugo {
    11 	
    10 	
    12   template <typename MapRegistry> class ArrayMapFactory {
    11   template <typename MapRegistry> class ArrayMapFactory {
    13 		
    12 		
    17     typedef typename MapRegistry::Key Key;
    16     typedef typename MapRegistry::Key Key;
    18     typedef typename MapRegistry::KeyIt KeyIt;
    17     typedef typename MapRegistry::KeyIt KeyIt;
    19 
    18 
    20     typedef typename MapRegistry::MapBase MapBase;
    19     typedef typename MapRegistry::MapBase MapBase;
    21 		
    20 		
    22     template <typename V, typename A = std::allocator<V> > class Map : public MapBase {
    21     template <typename V, typename A = std::allocator<V> > 
       
    22     class Map : public MapBase {
    23     
    23     
    24       public:
    24       public:
    25 
    25 
    26       typedef V Value;
    26       typedef V Value;
    27       typedef A Allocator;
    27       typedef A Allocator;
    28 
    28 
    29 	
    29 	
    30       Map() : values(0), capacity(0) {}
    30       Map() : values(0), capacity(0) {}
    31 			
    31 			
    32       Map(const Graph& g, MapRegistry& r) : MapBase(g, r) {
    32       Map(const Graph& g, MapRegistry& r) : MapBase(g, r) {
    33 	int max_id = -1;
    33 	allocate_memory();
    34 	for (KeyIt it(*getGraph()); getGraph()->valid(it); getGraph()->next(it)) {
       
    35 	  int id = getGraph()->id(it);
       
    36 	  if (id > max_id) {
       
    37 	    max_id = id;
       
    38 	  }			
       
    39 	}
       
    40 	if (max_id == -1) {
       
    41 	  capacity = 0;
       
    42 	  values = 0;
       
    43 	  return;
       
    44 	}
       
    45 	capacity = 1;
       
    46 	while (capacity <= max_id) {
       
    47 	  capacity <<= 1;
       
    48 	}
       
    49 	values = allocator.allocate(capacity);
       
    50 	for (KeyIt it(*getGraph()); getGraph()->valid(it); getGraph()->next(it)) {
    34 	for (KeyIt it(*getGraph()); getGraph()->valid(it); getGraph()->next(it)) {
    51 	  int id = getGraph()->id(it);
    35 	  int id = getGraph()->id(it);
    52 	  allocator.construct(&(values[id]), Value());
    36 	  allocator.construct(&(values[id]), Value());
    53 	}								
    37 	}								
    54       }
    38       }
    55 
    39 
    56       Map(const Graph& g, MapRegistry& r, const Value& v) : MapBase(g, r) {
    40       Map(const Graph& g, MapRegistry& r, const Value& v) : MapBase(g, r) {
    57 	int max_id = -1;
    41 	allocate_memory();
    58 	for (KeyIt it(*getGraph()); getGraph()->valid(it); getGraph()->next(it)) {
       
    59 	  int id = getGraph()->id(it);
       
    60 	  if (id > max_id) {
       
    61 	    max_id = id;
       
    62 	  }			
       
    63 	}
       
    64 	if (max_id == -1) {
       
    65 	  capacity = 0;
       
    66 	  values = 0;
       
    67 	  return;
       
    68 	}
       
    69 	capacity = 1;
       
    70 	while (capacity <= max_id) {
       
    71 	  capacity <<= 1;
       
    72 	}
       
    73 	values = allocator.allocate(capacity);
       
    74 	for (KeyIt it(*getGraph()); getGraph()->valid(it); getGraph()->next(it)) {
    42 	for (KeyIt it(*getGraph()); getGraph()->valid(it); getGraph()->next(it)) {
    75 	  int id = getGraph()->id(it);
    43 	  int id = getGraph()->id(it);
    76 	  allocator.construct(&(values[id]), v);
    44 	  allocator.construct(&(values[id]), v);
    77 	}								
    45 	}								
    78       }
    46       }
    85 	  int id = getGraph()->id(it);
    53 	  int id = getGraph()->id(it);
    86 	  allocator.construct(&(values[id]), copy.values[id]);
    54 	  allocator.construct(&(values[id]), copy.values[id]);
    87 	}
    55 	}
    88       }
    56       }
    89 
    57 
    90       template <typename CMap> Map(const CMap& copy) : MapBase(copy) {
    58       template <typename CMap> Map(const CMap& copy) 
       
    59 	: capacity(0), values(0), MapBase(copy) {
    91 	if (getGraph()) {
    60 	if (getGraph()) {
    92 	  init();
    61 	  allocate_memory();
    93 	  for (KeyIt it(*getGraph()); getGraph()->valid(it); getGraph()->next(it)) {
    62 	  for (KeyIt it(*getGraph()); getGraph()->valid(it); getGraph()->next(it)) {
    94 	    set(it, copy[it]);
    63 	    set(it, copy[it]);
    95 	  }
    64 	  }
    96 	}
    65 	}
    97       }
    66       }
   115 	if (getGraph()) {
    84 	if (getGraph()) {
   116 	  destroy();
    85 	  destroy();
   117 	} 
    86 	} 
   118 	this->MapBase::operator=(copy);
    87 	this->MapBase::operator=(copy);
   119 	if (getGraph()) {
    88 	if (getGraph()) {
   120 	  init();
    89 	  allocate_memory();
   121 	  for (KeyIt it(*getGraph()); getGraph()->valid(it); getGraph()->next(it)) {
    90 	  for (KeyIt it(*getGraph()); getGraph()->valid(it); getGraph()->next(it)) {
   122 	    set(it, copy[it]);
    91 	    set(it, copy[it]);
   123 	  }
    92 	  }
   124 	}
    93 	}
   125       }
    94       }
   126 
       
   127        
       
   128 				
    95 				
   129       virtual ~Map() {
    96       virtual ~Map() {
   130 	if (capacity != 0) {
    97 	if (capacity != 0) {
   131 	  destroy();
    98 	  destroy();
   132 	  allocator.deallocate(values, capacity);
    99 	  allocator.deallocate(values, capacity);
   179       void erase(const Key& key) {
   146       void erase(const Key& key) {
   180 	int id = getGraph()->id(key);
   147 	int id = getGraph()->id(key);
   181 	allocator.destroy(&(values[id]));
   148 	allocator.destroy(&(values[id]));
   182       }
   149       }
   183 	
   150 	
   184       /** Compatible iterator with the stl maps' iterators.
       
   185        *  It iterates on pairs of a key and a value.
       
   186        */
       
   187       class iterator {
   151       class iterator {
   188 	friend class Map;
   152 	friend class Map;
   189 	friend class const_iterator;
   153 	friend class const_iterator;
   190 	private:
   154       private:
   191 
   155 
   192 	/** Private constructor to initalize the the iterators returned
   156 	/** Private constructor to initalize the the iterators returned
   193 	 *  by the begin() and end().
   157 	 *  by the begin() and end().
   194 	 */
   158 	 */
   195 	iterator (Map& pmap, const KeyIt& pit) : map(&pmap), it(pit) {}
   159 	iterator (Map& pmap, const KeyIt& pit) : map(&pmap), it(pit) {}
   196 
   160 
   197 	public:
   161       public:
   198 
   162 
   199 	/** Default constructor. 
   163 	/** Default constructor. 
   200 	 */
   164 	 */
   201 	iterator() {}
   165 	iterator() {}
       
   166 
       
   167 	typedef extended_pair<const Key&, const Key&, 
       
   168 			      Value&, Value&> Reference;
   202 
   169 
   203 	/** Dereference operator for map.
   170 	/** Dereference operator for map.
   204 	 */	 
   171 	 */	 
   205 	std::pair<const Key, Value> operator*() {
   172 	Reference operator*() {
   206 	  return std::pair<const Key, Value>(it, (*map)[it]);
   173 	  return Reference(it, (*map)[it]);
   207 	}
   174 	}
       
   175 
       
   176 	class Pointer {
       
   177 	  friend class iterator;
       
   178 	private:
       
   179 	  Reference data;
       
   180 	  Pointer(const Key& key, Value& val) : data(key, val) {}
       
   181 	public:
       
   182 	  Reference* operator->() {return &data;}
       
   183 	};
   208 
   184 
   209 	/** Arrow operator for map.
   185 	/** Arrow operator for map.
   210 	 */	 
   186 	 */	 
   211 	std::pair<const Key, Value>* operator->() {
   187 	Pointer operator->() {
   212 	  static std::pair<const Key, Value> tmp = operator*();
   188 	  return Pointer(it, ((*map)[it])); 
   213 	  return &tmp;
       
   214 	}
   189 	}
   215 
   190 
   216 	/** The pre increment operator of the map.
   191 	/** The pre increment operator of the map.
   217 	 */
   192 	 */
   218 	iterator& operator++() { 
   193 	iterator& operator++() { 
   237 	/** The not-equality operator of the map.
   212 	/** The not-equality operator of the map.
   238 	 */
   213 	 */
   239 	bool operator!=(const_iterator p_it) {
   214 	bool operator!=(const_iterator p_it) {
   240 	  return !(*this == p_it);
   215 	  return !(*this == p_it);
   241 	}
   216 	}
   242 	
   217 
   243 	private:
   218 	
       
   219       private:
   244 	Map* map;
   220 	Map* map;
   245 	KeyIt it;
   221 	KeyIt it;
   246       };
   222       };
   247 
   223 
   248       /** Returns the begin iterator of the map.
   224       /** Returns the begin iterator of the map.
   258       }
   234       }
   259 
   235 
   260       class const_iterator {
   236       class const_iterator {
   261 	friend class Map;
   237 	friend class Map;
   262 	friend class iterator;
   238 	friend class iterator;
   263 	private:
   239       private:
   264 
   240 
   265 	/** Private constructor to initalize the the iterators returned
   241 	/** Private constructor to initalize the the iterators returned
   266 	 *  by the begin() and end().
   242 	 *  by the begin() and end().
   267 	 */
   243 	 */
   268 	const_iterator (Map& pmap, const KeyIt& pit) : map(&pmap), it(pit) {}
   244 	const_iterator (const Map& pmap, const KeyIt& pit) 
   269 
   245 	  : map(&pmap), it(pit) {}
   270 	public:
   246 
       
   247       public:
   271 
   248 
   272 	/** Default constructor. 
   249 	/** Default constructor. 
   273 	 */
   250 	 */
   274 	const_iterator() {}
   251 	const_iterator() {}
   275 
   252 
   276 	/** Constructor to convert iterator to const_iterator.
   253 	/** Constructor to convert iterator to const_iterator.
   277 	 */
   254 	 */
   278 	const_iterator(iterator p_it) {
   255 	const_iterator(iterator p_it) : map(p_it.map), it(p_it.it) {}
   279 	  it = p_it.it;
       
   280 	}
       
   281       
   256       
       
   257 	typedef extended_pair<const Key&, const Key&, 
       
   258 	  const Value&, const Value&> Reference;
       
   259 
   282 	/** Dereference operator for map.
   260 	/** Dereference operator for map.
   283 	 */	 
   261 	 */	 
   284 	std::pair<const Key, const Value> operator*() const {
   262 	Reference operator*() {
   285 	  return std::pair<const Key, const Value>(it, (*map)[it]);
   263 	  return Reference(it, (*map)[it]);
   286 	}
   264 	}
       
   265 
       
   266 
       
   267 	class Pointer {
       
   268 	  friend class const_iterator;
       
   269 	private:
       
   270 	  Reference data;
       
   271 	  Pointer(const Key& key, const Value& val) : data(key, val) {}
       
   272 	public:
       
   273 	  Reference* operator->() {return &data;}
       
   274 	};
   287 
   275 
   288 	/** Arrow operator for map.
   276 	/** Arrow operator for map.
   289 	 */	 
   277 	 */	 
   290 	std::pair<const Key, const Value>* operator->() const {
   278 	Pointer operator->() {
   291 	  static std::pair<const Key, const Value> tmp = operator*();
   279 	  return Pointer(it, ((*map)[it])); 
   292 	  return &tmp;
       
   293 	}
   280 	}
   294 
   281 
   295 	/** The pre increment operator of the map.
   282 	/** The pre increment operator of the map.
   296 	 */
   283 	 */
   297 	const_iterator& operator++() { 
   284 	const_iterator& operator++() { 
   317 	 */
   304 	 */
   318 	bool operator!=(const_iterator p_it) {
   305 	bool operator!=(const_iterator p_it) {
   319 	  return !(*this == p_it);
   306 	  return !(*this == p_it);
   320 	}
   307 	}
   321 	
   308 	
   322 	private:
   309 
       
   310       private:
   323 	const Map* map;
   311 	const Map* map;
   324 	KeyIt it;
   312 	KeyIt it;
   325       };
   313       };
   326 
   314 
   327       /** Returns the begin const_iterator of the map.
   315       /** Returns the begin const_iterator of the map.
   334        */
   322        */
   335       const_iterator end() const {
   323       const_iterator end() const {
   336 	return const_iterator(*this, INVALID);
   324 	return const_iterator(*this, INVALID);
   337       }
   325       }
   338 
   326 
   339       private:
   327     private:
       
   328       
       
   329       void allocate_memory() {
       
   330 	int max_id = -1;
       
   331 	for (KeyIt it(*getGraph()); getGraph()->valid(it); getGraph()->next(it)) {
       
   332 	  int id = getGraph()->id(it);
       
   333 	  if (id > max_id) {
       
   334 	    max_id = id;
       
   335 	  }			
       
   336 	}
       
   337 	if (max_id == -1) {
       
   338 	  capacity = 0;
       
   339 	  values = 0;
       
   340 	  return;
       
   341 	}
       
   342 	capacity = 1;
       
   343 	while (capacity <= max_id) {
       
   344 	  capacity <<= 1;
       
   345 	}
       
   346 	values = allocator.allocate(capacity);	
       
   347       }      
   340       int capacity;
   348       int capacity;
   341       Value* values;
   349       Value* values;
   342       Allocator allocator;
   350       Allocator allocator;
   343     };		
   351     };		
   344   };
   352   };