src/work/deba/array_map_factory.h
changeset 627 6cc21a9c9fda
child 674 7733d18de0e8
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/work/deba/array_map_factory.h	Thu May 13 08:20:39 2004 +0000
     1.3 @@ -0,0 +1,123 @@
     1.4 +#ifndef ARRAY_MAP_H
     1.5 +#define ARRAY_MAP_H
     1.6 +
     1.7 +#include <memory>
     1.8 +
     1.9 +#include "map_base.h"
    1.10 +
    1.11 +#include <iostream>
    1.12 +using namespace std;
    1.13 +
    1.14 +namespace hugo {
    1.15 +	
    1.16 +	template <typename G, typename K, typename KIt>
    1.17 +	class ArrayMapFactory {
    1.18 +	
    1.19 +	
    1.20 +	public:
    1.21 +		
    1.22 +		typedef G Graph;
    1.23 +		typedef K Key;
    1.24 +		typedef KIt KeyIt;
    1.25 +		
    1.26 +		template <typename V, typename A = allocator<V> > 
    1.27 +		class Map : public MapBase<G, K, KIt> {
    1.28 +		public:
    1.29 +			typedef V Value;
    1.30 +			typedef typename _Alloc_traits<V, A>::_Alloc_type _Alloc_type;
    1.31 +
    1.32 +	
    1.33 +			Map() : values(0), capacity(0) {}
    1.34 +			
    1.35 +			Map(Graph& g, MapRegistry<G, K, KIt>& r) 
    1.36 +				: MapBase<G, K, KIt>(g, r) {
    1.37 +				int max_id = -1;
    1.38 +				for (KeyIt it(*graph); graph->valid(it); graph->next(it)) {
    1.39 +					int id = graph->id(it);
    1.40 +					if (id > max_id) {
    1.41 +						max_id = id;
    1.42 +					}				
    1.43 +				}
    1.44 +				if (max_id == -1) {
    1.45 +					capacity = 0;
    1.46 +					values = 0;
    1.47 +					return;
    1.48 +				}
    1.49 +				int capacity = 1;
    1.50 +				while (capacity <= max_id) {
    1.51 +					capacity <<= 1;
    1.52 +				}
    1.53 +				Value* values = reinterpret_cast<Value*>(new char[capacity*sizeof(Value)]);
    1.54 +				for (KeyIt it(*graph); graph->valid(it); graph->next(it)) {
    1.55 +					int id = graph->id(it);
    1.56 +					new(&(values[id])) Value();
    1.57 +				}								
    1.58 +				cerr << capacity << endl;	
    1.59 +			}
    1.60 +				
    1.61 +			virtual ~Map() {
    1.62 +				destroy();
    1.63 +				delete[] reinterpret_cast<char*>(values);
    1.64 +				values = 0;
    1.65 +				capacity = 0;
    1.66 +			}
    1.67 +	
    1.68 +	
    1.69 +			Value& operator[](const K& key) {
    1.70 +				int id = graph->id(key);
    1.71 +				return values[id];
    1.72 +			} 
    1.73 +		
    1.74 +			const Value& operator[](const K& key) const {
    1.75 +				int id = graph->id(key);
    1.76 +				return values[id];
    1.77 +			}
    1.78 +	
    1.79 +			const Value& get(const K& key) const {
    1.80 +				int id = graph->id(key);
    1.81 +				return values[id];
    1.82 +			} 
    1.83 +		
    1.84 +			void set(const K& key, const Value& val) {
    1.85 +				int id = graph->id(key);
    1.86 +				values[id] = val;
    1.87 +			}
    1.88 +		
    1.89 +			void add(const K& key) {
    1.90 +				cerr << capacity << endl;
    1.91 +				int id = graph->id(key);
    1.92 +				if (id >= capacity) {
    1.93 +					int new_capacity = (capacity == 0 ? 1 : capacity);
    1.94 +					while (new_capacity <= id) {
    1.95 +						new_capacity <<= 1;
    1.96 +					}
    1.97 +					Value* new_values = reinterpret_cast<Value*>(new char[new_capacity*sizeof(Value)]);;
    1.98 +					for (KeyIt it(*graph); graph->valid(it); graph->next(it)) {
    1.99 +						int jd = graph->id(it);
   1.100 +						if (id != jd) {
   1.101 +							new(&(new_values[jd])) Value(values[jd]);
   1.102 +						}
   1.103 +					}
   1.104 +					if (capacity != 0) delete[] reinterpret_cast<char *>(values);
   1.105 +					values = new_values;
   1.106 +					capacity = new_capacity;
   1.107 +				}
   1.108 +				cerr << id << ' ' << capacity << endl;
   1.109 +				new(&(values[id])) Value();
   1.110 +			}
   1.111 +		
   1.112 +			void erase(const K& key) {
   1.113 +				int id = graph->id(key);
   1.114 +				values[id].~Value();
   1.115 +			}
   1.116 +	
   1.117 +		private:
   1.118 +			int capacity;
   1.119 +			Value* values;
   1.120 +								
   1.121 +		};
   1.122 +		
   1.123 +	};
   1.124 +}
   1.125 +
   1.126 +#endif