src/hugo/array_map_factory.h
changeset 786 d7b3b13b9df6
parent 785 a9b0863c2265
child 799 3393abe30678
equal deleted inserted replaced
2:24182881d4d0 3:d816b9dbb169
    20   template <typename MapRegistry> class ArrayMapFactory {
    20   template <typename MapRegistry> class ArrayMapFactory {
    21 		
    21 		
    22   public:
    22   public:
    23 		
    23 		
    24     typedef typename MapRegistry::Graph Graph;
    24     typedef typename MapRegistry::Graph Graph;
    25     typedef typename MapRegistry::Key Key;
    25     typedef typename MapRegistry::KeyType KeyType;
    26     typedef typename MapRegistry::KeyIt KeyIt;
    26     typedef typename MapRegistry::KeyIt KeyIt;
    27 
    27 
    28     typedef typename MapRegistry::MapBase MapBase;
    28     typedef typename MapRegistry::MapBase MapBase;
    29 		
    29 		
    30     template <typename V, typename A = std::allocator<V> > 
    30     template <typename V, typename A = std::allocator<V> > 
   111 	  allocator.deallocate(values, capacity);
   111 	  allocator.deallocate(values, capacity);
   112 	}
   112 	}
   113       }
   113       }
   114 	
   114 	
   115 	
   115 	
   116       Value& operator[](const Key& key) {
   116       Value& operator[](const KeyType& key) {
   117 	int id = MapBase::getGraph()->id(key);
   117 	int id = MapBase::getGraph()->id(key);
   118 	return values[id];
   118 	return values[id];
   119       } 
   119       } 
   120 		
   120 		
   121       const Value& operator[](const Key& key) const {
   121       const Value& operator[](const KeyType& key) const {
   122 	int id = MapBase::getGraph()->id(key);
   122 	int id = MapBase::getGraph()->id(key);
   123 	return values[id];
   123 	return values[id];
   124       }
   124       }
   125 	
   125 	
   126       const Value& get(const Key& key) const {
   126       const Value& get(const KeyType& key) const {
   127 	int id = MapBase::getGraph()->id(key);
   127 	int id = MapBase::getGraph()->id(key);
   128 	return values[id];
   128 	return values[id];
   129       } 
   129       } 
   130 		
   130 		
   131       void set(const Key& key, const Value& val) {
   131       void set(const KeyType& key, const Value& val) {
   132 	int id = MapBase::getGraph()->id(key);
   132 	int id = MapBase::getGraph()->id(key);
   133 	values[id] = val;
   133 	values[id] = val;
   134       }
   134       }
   135 		
   135 		
   136       void add(const Key& key) {
   136       void add(const KeyType& key) {
   137 	int id = MapBase::getGraph()->id(key);
   137 	int id = MapBase::getGraph()->id(key);
   138 	if (id >= capacity) {
   138 	if (id >= capacity) {
   139 	  int new_capacity = (capacity == 0 ? 1 : capacity);
   139 	  int new_capacity = (capacity == 0 ? 1 : capacity);
   140 	  while (new_capacity <= id) {
   140 	  while (new_capacity <= id) {
   141 	    new_capacity <<= 1;
   141 	    new_capacity <<= 1;
   153 	  capacity = new_capacity;
   153 	  capacity = new_capacity;
   154 	}
   154 	}
   155 	allocator.construct(&(values[id]), Value());
   155 	allocator.construct(&(values[id]), Value());
   156       }
   156       }
   157 		
   157 		
   158       void erase(const Key& key) {
   158       void erase(const KeyType& key) {
   159 	int id = MapBase::getGraph()->id(key);
   159 	int id = MapBase::getGraph()->id(key);
   160 	allocator.destroy(&(values[id]));
   160 	allocator.destroy(&(values[id]));
   161       }
   161       }
   162 
   162 
   163       void clear() {	
   163       void clear() {	
   182 
   182 
   183 	/** Default constructor. 
   183 	/** Default constructor. 
   184 	 */
   184 	 */
   185 	iterator() {}
   185 	iterator() {}
   186 
   186 
   187 	typedef extended_pair<const Key&, const Key&, 
   187 	typedef extended_pair<const KeyType&, const KeyType&, 
   188 			      Value&, Value&> Reference;
   188 			      Value&, Value&> Reference;
   189 
   189 
   190 	/** Dereference operator for map.
   190 	/** Dereference operator for map.
   191 	 */	 
   191 	 */	 
   192 	Reference operator*() {
   192 	Reference operator*() {
   195 
   195 
   196 	class Pointer {
   196 	class Pointer {
   197 	  friend class iterator;
   197 	  friend class iterator;
   198 	private:
   198 	private:
   199 	  Reference data;
   199 	  Reference data;
   200 	  Pointer(const Key& key, Value& val) : data(key, val) {}
   200 	  Pointer(const KeyType& key, Value& val) : data(key, val) {}
   201 	public:
   201 	public:
   202 	  Reference* operator->() {return &data;}
   202 	  Reference* operator->() {return &data;}
   203 	};
   203 	};
   204 
   204 
   205 	/** Arrow operator for map.
   205 	/** Arrow operator for map.
   272 
   272 
   273 	/** Constructor to convert iterator to const_iterator.
   273 	/** Constructor to convert iterator to const_iterator.
   274 	 */
   274 	 */
   275 	const_iterator(iterator p_it) : map(p_it.map), it(p_it.it) {}
   275 	const_iterator(iterator p_it) : map(p_it.map), it(p_it.it) {}
   276       
   276       
   277 	typedef extended_pair<const Key&, const Key&, 
   277 	typedef extended_pair<const KeyType&, const KeyType&, 
   278 	  const Value&, const Value&> Reference;
   278 	  const Value&, const Value&> Reference;
   279 
   279 
   280 	/** Dereference operator for map.
   280 	/** Dereference operator for map.
   281 	 */	 
   281 	 */	 
   282 	Reference operator*() {
   282 	Reference operator*() {
   286 
   286 
   287 	class Pointer {
   287 	class Pointer {
   288 	  friend class const_iterator;
   288 	  friend class const_iterator;
   289 	private:
   289 	private:
   290 	  Reference data;
   290 	  Reference data;
   291 	  Pointer(const Key& key, const Value& val) : data(key, val) {}
   291 	  Pointer(const KeyType& key, const Value& val) : data(key, val) {}
   292 	public:
   292 	public:
   293 	  Reference* operator->() {return &data;}
   293 	  Reference* operator->() {return &data;}
   294 	};
   294 	};
   295 
   295 
   296 	/** Arrow operator for map.
   296 	/** Arrow operator for map.