alpar@906: /* -*- C++ -*- ladanyi@1435: * lemon/bits/array_map.h - Part of LEMON, a generic C++ optimization library alpar@906: * alpar@1164: * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport alpar@1359: * (Egervary Research Group on Combinatorial Optimization, EGRES). alpar@906: * alpar@906: * Permission to use, modify and distribute this software is granted alpar@906: * provided that this copyright notice appears in all copies. For alpar@906: * precise terms see the accompanying LICENSE file. alpar@906: * alpar@906: * This software is provided "AS IS" with no warranty of any kind, alpar@906: * express or implied, and with no claim as to its suitability for any alpar@906: * purpose. alpar@906: * alpar@906: */ alpar@906: alpar@921: #ifndef LEMON_ARRAY_MAP_H alpar@921: #define LEMON_ARRAY_MAP_H deba@822: deba@822: #include deba@1307: #include deba@822: deba@822: ///\ingroup graphmaps deba@822: ///\file deba@822: ///\brief Graph maps that construates and destruates deba@822: ///their elements dynamically. deba@822: alpar@921: namespace lemon { deba@822: deba@822: deba@822: /// \addtogroup graphmaps deba@822: /// @{ deba@822: deba@1414: /// The ArrayMap template class is graph map structure what deba@1414: /// automatically updates the map when a key is added to or erased from deba@1414: /// the map. This map factory uses the allocators to implement deba@1414: /// the container functionality. deba@1414: /// deba@1414: /// The template parameter is the AlterationNotifier that the maps deba@1414: /// will belong to and the Value. deba@1414: deba@822: klao@946: template deba@1039: class ArrayMap : public AlterationNotifier<_Item>::ObserverBase { deba@897: deba@1267: typedef _Item Item; deba@822: public: deba@822: deba@822: /// The graph type of the maps. klao@946: typedef _Graph Graph; deba@822: /// The key type of the maps. alpar@987: typedef _Item Key; klao@946: deba@1039: typedef AlterationNotifier<_Item> Registry; klao@946: deba@822: /// The MapBase of the Map which imlements the core regisitry function. klao@946: typedef typename Registry::ObserverBase Parent; deba@822: deba@822: /// The value type of the map. alpar@987: typedef _Value Value; deba@822: deba@822: klao@946: private: deba@822: typedef std::allocator Allocator; deba@822: klao@946: klao@946: public: klao@946: deba@1414: /// Graph and Registry initialized map constructor. deba@1414: deba@980: ArrayMap(const Graph& _g) : graph(&_g) { deba@1267: Item it; deba@1267: attach(_g.getNotifier(Item())); deba@822: allocate_memory(); deba@1267: for (graph->first(it); it != INVALID; graph->next(it)) { deba@980: int id = graph->id(it);; deba@822: allocator.construct(&(values[id]), Value()); deba@822: } deba@822: } deba@822: klao@946: /// Constructor to use default value to initialize the map. klao@946: klao@946: /// It constrates a map and initialize all of the the map. klao@946: deba@980: ArrayMap(const Graph& _g, const Value& _v) : graph(&_g) { deba@1267: Item it; deba@1039: attach(_g.getNotifier(_Item())); deba@822: allocate_memory(); deba@1267: for (graph->first(it); it != INVALID; graph->next(it)) { deba@980: int id = graph->id(it);; klao@946: allocator.construct(&(values[id]), _v); deba@822: } deba@822: } deba@822: deba@1414: /// Constructor to copy a map of the same map type. deba@1414: deba@1374: ArrayMap(const ArrayMap& copy) : Parent() { klao@946: if (copy.attached()) { klao@946: attach(*copy.getRegistry()); klao@946: } deba@822: capacity = copy.capacity; deba@822: if (capacity == 0) return; deba@822: values = allocator.allocate(capacity); deba@1267: Item it; deba@1267: for (graph->first(it); it != INVALID; graph->next(it)) { deba@980: int id = graph->id(it);; deba@891: allocator.construct(&(values[id]), copy.values[id]); deba@822: } deba@822: } deba@822: klao@978: using Parent::attach; klao@978: using Parent::detach; klao@978: using Parent::attached; klao@978: deba@1414: /// Assign operator to copy a map of the same map type. deba@1414: deba@822: ArrayMap& operator=(const ArrayMap& copy) { deba@822: if (© == this) return *this; deba@897: klao@946: if (graph != copy.graph) { klao@946: if (attached()) { klao@946: clear(); klao@946: detach(); deba@897: } klao@946: if (copy.attached()) { klao@946: attach(*copy.getRegistry()); klao@946: } deba@897: capacity = copy.capacity; deba@897: if (capacity == 0) return *this; deba@897: values = allocator.allocate(capacity); deba@822: } deba@891: deba@1267: Item it; deba@1267: for (graph->first(it); it != INVALID; graph->next(it)) { deba@980: int id = graph->id(it);; deba@822: allocator.construct(&(values[id]), copy.values[id]); deba@822: } deba@891: deba@822: return *this; deba@822: } deba@822: deba@1414: /// The destructor of the map. deba@1414: klao@946: virtual ~ArrayMap() { klao@946: if (attached()) { klao@946: clear(); klao@946: detach(); deba@822: } deba@822: } deba@822: deba@822: deba@1414: ///The subscript operator. The map can be subscripted by the deba@1414: ///actual keys of the graph. deba@1414: deba@1267: Value& operator[](const Key& key) { deba@980: int id = graph->id(key); deba@822: return values[id]; deba@822: } deba@822: deba@1414: deba@1414: ///The const subscript operator. The map can be subscripted by the deba@1414: ///actual keys of the graph. deba@1414: deba@1267: const Value& operator[](const Key& key) const { deba@980: int id = graph->id(key); deba@822: return values[id]; deba@822: } deba@822: deba@1414: /// Setter function of the map. Equivalent with map[key] = val. deba@1414: /// This is a compatibility feature with the not dereferable maps. deba@1414: alpar@987: void set(const Key& key, const Value& val) { klao@946: (*this)[key] = val; deba@822: } deba@822: deba@1414: /// Add a new key to the map. It called by the map registry. deba@1414: alpar@987: void add(const Key& key) { deba@980: int id = graph->id(key); deba@822: if (id >= capacity) { deba@822: int new_capacity = (capacity == 0 ? 1 : capacity); deba@822: while (new_capacity <= id) { deba@822: new_capacity <<= 1; deba@822: } klao@946: Value* new_values = allocator.allocate(new_capacity); deba@1267: Item it; deba@1267: for (graph->first(it); it != INVALID; graph->next(it)) { deba@980: int jd = graph->id(it);; deba@822: if (id != jd) { deba@822: allocator.construct(&(new_values[jd]), values[jd]); deba@822: allocator.destroy(&(values[jd])); deba@822: } deba@822: } deba@822: if (capacity != 0) allocator.deallocate(values, capacity); deba@822: values = new_values; deba@822: capacity = new_capacity; deba@822: } deba@822: allocator.construct(&(values[id]), Value()); deba@822: } deba@1414: deba@1414: void add(const std::vector& keys) { deba@1414: int max_id = -1; deba@1414: for (int i = 0; i < (int)keys.size(); ++i) { deba@1414: int id = graph->id(keys[i]); deba@1414: if (id > max_id) { deba@1414: max_id = id; deba@1414: } deba@1414: } deba@1414: if (max_id >= capacity) { deba@1414: int new_capacity = (capacity == 0 ? 1 : capacity); deba@1414: while (new_capacity <= max_id) { deba@1414: new_capacity <<= 1; deba@1414: } deba@1414: Value* new_values = allocator.allocate(new_capacity); deba@1414: Item it; deba@1414: for (graph->first(it); it != INVALID; graph->next(it)) { deba@1414: int id = graph->id(it); deba@1414: bool found = false; deba@1414: for (int i = 0; i < (int)keys.size(); ++i) { deba@1414: int jd = graph->id(keys[i]); deba@1414: if (id == jd) { deba@1414: found = true; deba@1414: break; deba@1414: } deba@1414: } deba@1414: if (found) continue; deba@1414: allocator.construct(&(new_values[id]), values[id]); deba@1414: allocator.destroy(&(values[id])); deba@1414: } deba@1414: if (capacity != 0) allocator.deallocate(values, capacity); deba@1414: values = new_values; deba@1414: capacity = new_capacity; deba@1414: } deba@1414: for (int i = 0; i < (int)keys.size(); ++i) { deba@1414: int id = graph->id(keys[i]); deba@1414: allocator.construct(&(values[id]), Value()); deba@1414: } deba@1414: } deba@822: deba@1414: /// Erase a key from the map. It called by the map registry. deba@1414: alpar@987: void erase(const Key& key) { deba@980: int id = graph->id(key); deba@822: allocator.destroy(&(values[id])); deba@822: } deba@822: deba@1414: void erase(const std::vector& keys) { deba@1414: for (int i = 0; i < (int)keys.size(); ++i) { deba@1414: int id = graph->id(keys[i]); deba@1414: allocator.destroy(&(values[id])); deba@1414: } deba@1414: } deba@1414: klao@946: void build() { klao@946: allocate_memory(); deba@1267: Item it; deba@1267: for (graph->first(it); it != INVALID; graph->next(it)) { deba@980: int id = graph->id(it);; klao@946: allocator.construct(&(values[id]), Value()); klao@946: } klao@946: } klao@946: deba@822: void clear() { deba@822: if (capacity != 0) { deba@1267: Item it; deba@1267: for (graph->first(it); it != INVALID; graph->next(it)) { deba@1414: int id = graph->id(it); klao@946: allocator.destroy(&(values[id])); klao@946: } deba@822: allocator.deallocate(values, capacity); deba@822: capacity = 0; deba@822: } deba@822: } deba@822: deba@1271: const Graph* getGraph() { deba@1271: return graph; deba@1271: } deba@1271: deba@822: private: deba@822: deba@822: void allocate_memory() { deba@980: int max_id = graph->maxId(_Item()); deba@822: if (max_id == -1) { deba@822: capacity = 0; deba@822: values = 0; deba@822: return; deba@822: } deba@822: capacity = 1; deba@822: while (capacity <= max_id) { deba@822: capacity <<= 1; deba@822: } deba@822: values = allocator.allocate(capacity); deba@822: } deba@822: klao@946: const Graph* graph; deba@822: int capacity; deba@822: Value* values; deba@822: Allocator allocator; deba@844: deba@822: }; deba@822: klao@946: template klao@946: class ArrayMappableGraphExtender : public _Base { klao@946: public: klao@946: klao@946: typedef ArrayMappableGraphExtender<_Base> Graph; klao@946: typedef _Base Parent; klao@946: klao@946: typedef typename Parent::Node Node; klao@946: typedef typename Parent::NodeIt NodeIt; deba@1039: typedef typename Parent::NodeNotifier NodeObserverRegistry; klao@946: klao@946: typedef typename Parent::Edge Edge; klao@946: typedef typename Parent::EdgeIt EdgeIt; deba@1039: typedef typename Parent::EdgeNotifier EdgeObserverRegistry; klao@946: klao@946: klao@946: klao@946: template deba@1267: class NodeMap deba@1267: : public IterableMapExtender > { klao@946: public: klao@946: typedef ArrayMappableGraphExtender<_Base> Graph; klao@946: klao@946: typedef typename Graph::Node Node; klao@946: typedef typename Graph::NodeIt NodeIt; klao@946: deba@1267: typedef IterableMapExtender > Parent; klao@946: klao@979: //typedef typename Parent::Graph Graph; klao@946: typedef typename Parent::Value Value; klao@946: klao@946: NodeMap(const Graph& g) deba@980: : Parent(g) {} klao@946: NodeMap(const Graph& g, const Value& v) deba@980: : Parent(g, v) {} klao@946: klao@946: }; klao@946: klao@946: template deba@1267: class EdgeMap deba@1267: : public IterableMapExtender > { klao@946: public: klao@946: typedef ArrayMappableGraphExtender<_Base> Graph; klao@946: klao@946: typedef typename Graph::Edge Edge; klao@946: typedef typename Graph::EdgeIt EdgeIt; klao@946: deba@1267: typedef IterableMapExtender > Parent; klao@946: klao@979: //typedef typename Parent::Graph Graph; klao@946: typedef typename Parent::Value Value; klao@946: klao@946: EdgeMap(const Graph& g) deba@980: : Parent(g) {} klao@946: EdgeMap(const Graph& g, const Value& v) deba@980: : Parent(g, v) {} klao@946: klao@946: }; klao@946: klao@946: }; klao@946: deba@822: /// @} deba@822: deba@822: } deba@822: alpar@921: #endif //LEMON_ARRAY_MAP_H