Legal files. Proposal.
12 template <typename MapRegistry> class ArrayMapFactory {
16 typedef typename MapRegistry::Graph Graph;
17 typedef typename MapRegistry::Key Key;
18 typedef typename MapRegistry::KeyIt KeyIt;
20 typedef typename MapRegistry::MapBase MapBase;
22 template <typename V, typename A = std::allocator<V> >
23 class Map : public MapBase {
31 Map() : values(0), capacity(0) {}
33 Map(Graph& g, MapRegistry& r) : MapBase(g, r) {
35 for (KeyIt it(*graph); graph->valid(it); graph->next(it)) {
36 int id = graph->id(it);
47 while (capacity <= max_id) {
50 values = allocator.allocate(capacity);
51 for (KeyIt it(*graph); graph->valid(it); graph->next(it)) {
52 int id = graph->id(it);
53 allocator.construct(&(values[id]), Value());
57 Map(const Map& copy) : MapBase(*copy.graph, *copy.registry) {
58 capacity = copy.capacity;
59 values = allocator.allocate(capacity);
60 for (KeyIt it(*graph); graph->valid(it); graph->next(it)) {
61 int id = graph->id(it);
62 allocator.construct(&(values[id]), copy.values[id]);
68 allocator.deallocate(values, capacity);
72 Value& operator[](const Key& key) {
73 int id = graph->id(key);
77 const Value& operator[](const Key& key) const {
78 int id = graph->id(key);
82 const Value& get(const Key& key) const {
83 int id = graph->id(key);
87 void set(const Key& key, const Value& val) {
88 int id = graph->id(key);
92 void add(const Key& key) {
93 int id = graph->id(key);
95 int new_capacity = (capacity == 0 ? 1 : capacity);
96 while (new_capacity <= id) {
99 Value* new_values = allocator.allocate(new_capacity);;
100 for (KeyIt it(*graph); graph->valid(it); graph->next(it)) {
101 int jd = graph->id(it);
103 allocator.construct(&(new_values[jd]), values[jd]);
104 allocator.destroy(&(values[jd]));
107 if (capacity != 0) allocator.deallocate(values, capacity);
109 capacity = new_capacity;
111 allocator.construct(&(values[id]), Value());
114 void erase(const Key& key) {
115 int id = graph->id(key);
116 allocator.destroy(&(values[id]));