src/lemon/array_map.h
changeset 980 0f1044b7a3af
parent 979 b5fb023cdb7b
child 987 87f7c54892df
equal deleted inserted replaced
3:94642a1bf8cc 4:80430ba62ad4
    42    */
    42    */
    43 
    43 
    44   template <typename _Graph, 
    44   template <typename _Graph, 
    45 	    typename _Item,
    45 	    typename _Item,
    46 	    typename _ItemIt,
    46 	    typename _ItemIt,
    47 	    typename _IdMap,
       
    48 	    typename _Value>
    47 	    typename _Value>
    49   class ArrayMap : public AlterationObserverRegistry<_Item>::ObserverBase {
    48   class ArrayMap : public AlterationObserverRegistry<_Item>::ObserverBase {
    50 
    49 
    51   public:
    50   public:
    52 		
    51 		
    58     typedef AlterationObserverRegistry<_Item> Registry;
    57     typedef AlterationObserverRegistry<_Item> Registry;
    59 
    58 
    60   private:
    59   private:
    61     /// The iterator to iterate on the keys.
    60     /// The iterator to iterate on the keys.
    62     typedef _ItemIt KeyIt;
    61     typedef _ItemIt KeyIt;
    63 
       
    64     typedef _IdMap IdMap;
       
    65 
    62 
    66     typedef _Value Value;
    63     typedef _Value Value;
    67 
    64 
    68     /// The MapBase of the Map which imlements the core regisitry function.
    65     /// The MapBase of the Map which imlements the core regisitry function.
    69     typedef typename Registry::ObserverBase Parent;
    66     typedef typename Registry::ObserverBase Parent;
    92 
    89 
    93   public:
    90   public:
    94 
    91 
    95     /** Graph and Registry initialized map constructor.
    92     /** Graph and Registry initialized map constructor.
    96      */
    93      */
    97     ArrayMap(const Graph& _g, Registry& _r) : graph(&_g) {
    94     ArrayMap(const Graph& _g) : graph(&_g) {
    98       attach(_r);
    95       attach(_g.getObserverRegistry(_Item()));
    99       allocate_memory();
    96       allocate_memory();
   100       for (KeyIt it(*graph); it != INVALID; ++it) {
    97       for (KeyIt it(*graph); it != INVALID; ++it) {
   101 	int id = IdMap(*graph)[it];
    98 	int id = graph->id(it);;
   102 	allocator.construct(&(values[id]), Value());
    99 	allocator.construct(&(values[id]), Value());
   103       }								
   100       }								
   104     }
   101     }
   105 
   102 
   106     /// Constructor to use default value to initialize the map. 
   103     /// Constructor to use default value to initialize the map. 
   107 
   104 
   108     /// It constrates a map and initialize all of the the map. 
   105     /// It constrates a map and initialize all of the the map. 
   109 
   106 
   110     ArrayMap(const Graph& _g, Registry& _r, const Value& _v) : graph(&_g) {
   107     ArrayMap(const Graph& _g, const Value& _v) : graph(&_g) {
   111       attach(_r);
   108       attach(_g.getObserverRegistry(_Item()));
   112       allocate_memory();
   109       allocate_memory();
   113       for (KeyIt it(*graph); it != INVALID; ++it) {
   110       for (KeyIt it(*graph); it != INVALID; ++it) {
   114 	int id = IdMap(*graph)[it];
   111 	int id = graph->id(it);;
   115 	allocator.construct(&(values[id]), _v);
   112 	allocator.construct(&(values[id]), _v);
   116       }								
   113       }								
   117     }
   114     }
   118 
   115 
   119     /** Constructor to copy a map of the same map type.
   116     /** Constructor to copy a map of the same map type.
   124       }
   121       }
   125       capacity = copy.capacity;
   122       capacity = copy.capacity;
   126       if (capacity == 0) return;
   123       if (capacity == 0) return;
   127       values = allocator.allocate(capacity);
   124       values = allocator.allocate(capacity);
   128       for (KeyIt it(*graph); it != INVALID; ++it) {
   125       for (KeyIt it(*graph); it != INVALID; ++it) {
   129 	int id = IdMap(*graph)[it];
   126 	int id = graph->id(it);;
   130 	allocator.construct(&(values[id]), copy.values[id]);
   127 	allocator.construct(&(values[id]), copy.values[id]);
   131       }
   128       }
   132     }
   129     }
   133 
   130 
   134     using Parent::attach;
   131     using Parent::attach;
   152 	if (capacity == 0) return *this;
   149 	if (capacity == 0) return *this;
   153 	values = allocator.allocate(capacity);      
   150 	values = allocator.allocate(capacity);      
   154       }
   151       }
   155 
   152 
   156       for (KeyIt it(*graph); it != INVALID; ++it) {
   153       for (KeyIt it(*graph); it != INVALID; ++it) {
   157 	int id = IdMap(*graph)[it];
   154 	int id = graph->id(it);;
   158 	allocator.construct(&(values[id]), copy.values[id]);
   155 	allocator.construct(&(values[id]), copy.values[id]);
   159       }
   156       }
   160 
   157 
   161       return *this;
   158       return *this;
   162     }
   159     }
   174     /**
   171     /**
   175      * The subscript operator. The map can be subscripted by the
   172      * The subscript operator. The map can be subscripted by the
   176      * actual keys of the graph. 
   173      * actual keys of the graph. 
   177      */
   174      */
   178     ReferenceType operator[](const KeyType& key) {
   175     ReferenceType operator[](const KeyType& key) {
   179       int id = IdMap(*graph)[key];
   176       int id = graph->id(key);
   180       return values[id];
   177       return values[id];
   181     } 
   178     } 
   182 		
   179 		
   183     /**
   180     /**
   184      * The const subscript operator. The map can be subscripted by the
   181      * The const subscript operator. The map can be subscripted by the
   185      * actual keys of the graph. 
   182      * actual keys of the graph. 
   186      */
   183      */
   187     ConstReferenceType operator[](const KeyType& key) const {
   184     ConstReferenceType operator[](const KeyType& key) const {
   188       int id = IdMap(*graph)[key];
   185       int id = graph->id(key);
   189       return values[id];
   186       return values[id];
   190     }
   187     }
   191 	
   188 	
   192     /** Setter function of the map. Equivalent with map[key] = val.
   189     /** Setter function of the map. Equivalent with map[key] = val.
   193      *  This is a compatibility feature with the not dereferable maps.
   190      *  This is a compatibility feature with the not dereferable maps.
   197     }
   194     }
   198 		
   195 		
   199     /** Add a new key to the map. It called by the map registry.
   196     /** Add a new key to the map. It called by the map registry.
   200      */
   197      */
   201     void add(const KeyType& key) {
   198     void add(const KeyType& key) {
   202       int id = IdMap(*graph)[key];
   199       int id = graph->id(key);
   203       if (id >= capacity) {
   200       if (id >= capacity) {
   204 	int new_capacity = (capacity == 0 ? 1 : capacity);
   201 	int new_capacity = (capacity == 0 ? 1 : capacity);
   205 	while (new_capacity <= id) {
   202 	while (new_capacity <= id) {
   206 	  new_capacity <<= 1;
   203 	  new_capacity <<= 1;
   207 	}
   204 	}
   208 	Value* new_values = allocator.allocate(new_capacity);
   205 	Value* new_values = allocator.allocate(new_capacity);
   209 	for (KeyIt it(*graph); it != INVALID; ++it) {
   206 	for (KeyIt it(*graph); it != INVALID; ++it) {
   210 	  int jd = IdMap(*graph)[it];
   207 	  int jd = graph->id(it);;
   211 	  if (id != jd) {
   208 	  if (id != jd) {
   212 	    allocator.construct(&(new_values[jd]), values[jd]);
   209 	    allocator.construct(&(new_values[jd]), values[jd]);
   213 	    allocator.destroy(&(values[jd]));
   210 	    allocator.destroy(&(values[jd]));
   214 	  }
   211 	  }
   215 	}
   212 	}
   221     }
   218     }
   222 		
   219 		
   223     /** Erase a key from the map. It called by the map registry.
   220     /** Erase a key from the map. It called by the map registry.
   224      */
   221      */
   225     void erase(const KeyType& key) {
   222     void erase(const KeyType& key) {
   226       int id = IdMap(*graph)[key];
   223       int id = graph->id(key);
   227       allocator.destroy(&(values[id]));
   224       allocator.destroy(&(values[id]));
   228     }
   225     }
   229 
   226 
   230     void build() {
   227     void build() {
   231       allocate_memory();
   228       allocate_memory();
   232       for (KeyIt it(*graph); it != INVALID; ++it) {
   229       for (KeyIt it(*graph); it != INVALID; ++it) {
   233 	int id = IdMap(*graph)[it];
   230 	int id = graph->id(it);;
   234 	allocator.construct(&(values[id]), Value());
   231 	allocator.construct(&(values[id]), Value());
   235       }								
   232       }								
   236     }
   233     }
   237 
   234 
   238     void clear() {	
   235     void clear() {	
   239       if (capacity != 0) {
   236       if (capacity != 0) {
   240 	for (KeyIt it(*graph); it != INVALID; ++it) {
   237 	for (KeyIt it(*graph); it != INVALID; ++it) {
   241 	  int id = IdMap(*graph)[it];
   238 	  int id = graph->id(it);;
   242 	  allocator.destroy(&(values[id]));
   239 	  allocator.destroy(&(values[id]));
   243 	}								
   240 	}								
   244 	allocator.deallocate(values, capacity);
   241 	allocator.deallocate(values, capacity);
   245 	capacity = 0;
   242 	capacity = 0;
   246       }
   243       }
   300 //     }
   297 //     }
   301 
   298 
   302   private:
   299   private:
   303       
   300       
   304     void allocate_memory() {
   301     void allocate_memory() {
   305       int max_id = IdMap(*graph).maxId();
   302       int max_id = graph->maxId(_Item());
   306       if (max_id == -1) {
   303       if (max_id == -1) {
   307 	capacity = 0;
   304 	capacity = 0;
   308 	values = 0;
   305 	values = 0;
   309 	return;
   306 	return;
   310       }
   307       }
   341     typedef ArrayMappableGraphExtender<_Base> Graph;
   338     typedef ArrayMappableGraphExtender<_Base> Graph;
   342     typedef _Base Parent;
   339     typedef _Base Parent;
   343 
   340 
   344     typedef typename Parent::Node Node;
   341     typedef typename Parent::Node Node;
   345     typedef typename Parent::NodeIt NodeIt;
   342     typedef typename Parent::NodeIt NodeIt;
   346     typedef typename Parent::NodeIdMap NodeIdMap;
       
   347     typedef typename Parent::NodeObserverRegistry NodeObserverRegistry;
   343     typedef typename Parent::NodeObserverRegistry NodeObserverRegistry;
   348 
   344 
   349     typedef typename Parent::Edge Edge;
   345     typedef typename Parent::Edge Edge;
   350     typedef typename Parent::EdgeIt EdgeIt;
   346     typedef typename Parent::EdgeIt EdgeIt;
   351     typedef typename Parent::EdgeIdMap EdgeIdMap;
       
   352     typedef typename Parent::EdgeObserverRegistry EdgeObserverRegistry;
   347     typedef typename Parent::EdgeObserverRegistry EdgeObserverRegistry;
   353 
   348 
   354     
   349     
   355 
   350 
   356     template <typename _Value>
   351     template <typename _Value>
   357     class NodeMap : public ArrayMap<Graph, Node, NodeIt, NodeIdMap, _Value> {
   352     class NodeMap : public ArrayMap<Graph, Node, NodeIt, _Value> {
   358     public:
   353     public:
   359       typedef ArrayMappableGraphExtender<_Base> Graph;
   354       typedef ArrayMappableGraphExtender<_Base> Graph;
   360 
   355 
   361       typedef typename Graph::Node Node;
   356       typedef typename Graph::Node Node;
   362       typedef typename Graph::NodeIt NodeIt;
   357       typedef typename Graph::NodeIt NodeIt;
   363       typedef typename Graph::NodeIdMap NodeIdMap;
   358 
   364 
   359       typedef ArrayMap<Graph, Node, NodeIt, _Value> Parent;
   365       typedef ArrayMap<Graph, Node, NodeIt, NodeIdMap, _Value> Parent;
       
   366 
   360 
   367       //typedef typename Parent::Graph Graph;
   361       //typedef typename Parent::Graph Graph;
   368       typedef typename Parent::Value Value;
   362       typedef typename Parent::Value Value;
   369 
   363 
   370       NodeMap(const Graph& g) 
   364       NodeMap(const Graph& g) 
   371 	: Parent(g, g.getNodeObserverRegistry()) {}
   365 	: Parent(g) {}
   372       NodeMap(const Graph& g, const Value& v) 
   366       NodeMap(const Graph& g, const Value& v) 
   373 	: Parent(g, g.getNodeObserverRegistry(), v) {}
   367 	: Parent(g, v) {}
   374 
   368 
   375     };
   369     };
   376 
   370 
   377     template <typename _Value>
   371     template <typename _Value>
   378     class EdgeMap : public ArrayMap<Graph, Edge, EdgeIt, EdgeIdMap, _Value> {
   372     class EdgeMap : public ArrayMap<Graph, Edge, EdgeIt, _Value> {
   379     public:
   373     public:
   380       typedef ArrayMappableGraphExtender<_Base> Graph;
   374       typedef ArrayMappableGraphExtender<_Base> Graph;
   381 
   375 
   382       typedef typename Graph::Edge Edge;
   376       typedef typename Graph::Edge Edge;
   383       typedef typename Graph::EdgeIt EdgeIt;
   377       typedef typename Graph::EdgeIt EdgeIt;
   384       typedef typename Graph::EdgeIdMap EdgeIdMap;
   378 
   385 
   379       typedef ArrayMap<Graph, Edge, EdgeIt, _Value> Parent;
   386       typedef ArrayMap<Graph, Edge, EdgeIt, EdgeIdMap, _Value> Parent;
       
   387 
   380 
   388       //typedef typename Parent::Graph Graph;
   381       //typedef typename Parent::Graph Graph;
   389       typedef typename Parent::Value Value;
   382       typedef typename Parent::Value Value;
   390 
   383 
   391       EdgeMap(const Graph& g) 
   384       EdgeMap(const Graph& g) 
   392 	: Parent(g, g.getEdgeObserverRegistry()) {}
   385 	: Parent(g) {}
   393       EdgeMap(const Graph& g, const Value& v) 
   386       EdgeMap(const Graph& g, const Value& v) 
   394 	: Parent(g, g.getEdgeObserverRegistry(), v) {}
   387 	: Parent(g, v) {}
   395 
   388 
   396     };
   389     };
   397     
   390     
   398   };
   391   };
   399 
   392