alpar@906: /* -*- C++ -*- alpar@921: * src/lemon/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@822: /** The ArrayMap template class is graph map structure what deba@822: * automatically updates the map when a key is added to or erased from deba@822: * the map. This map factory uses the allocators to implement deba@822: * the container functionality. deba@822: * deba@1267: * The template parameter is the AlterationNotifier that the maps alpar@987: * will belong to and the Value. deba@822: */ 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@822: /** Graph and Registry initialized map constructor. deba@822: */ 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@822: /** Constructor to copy a map of the same map type. deba@822: */ 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@822: /** Assign operator to copy a map of the same map type. deba@822: */ 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@822: /** The destructor of the map. deba@822: */ klao@946: virtual ~ArrayMap() { klao@946: if (attached()) { klao@946: clear(); klao@946: detach(); deba@822: } deba@822: } deba@822: deba@822: deba@822: /** deba@822: * The subscript operator. The map can be subscripted by the deba@822: * actual keys of the graph. deba@822: */ deba@1267: Value& operator[](const Key& key) { deba@980: int id = graph->id(key); deba@822: return values[id]; deba@822: } deba@822: deba@822: /** deba@822: * The const subscript operator. The map can be subscripted by the deba@822: * actual keys of the graph. deba@822: */ 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@822: /** Setter function of the map. Equivalent with map[key] = val. deba@822: * This is a compatibility feature with the not dereferable maps. deba@822: */ alpar@987: void set(const Key& key, const Value& val) { klao@946: (*this)[key] = val; deba@822: } deba@822: deba@822: /** Add a new key to the map. It called by the map registry. deba@822: */ 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@822: deba@822: /** Erase a key from the map. It called by the map registry. deba@822: */ alpar@987: void erase(const Key& key) { deba@980: int id = graph->id(key); deba@822: allocator.destroy(&(values[id])); deba@822: } deba@822: 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@980: 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: klao@946: // /// The stl compatible pair iterator of the map. klao@946: // typedef MapIterator Iterator; klao@946: // /// The stl compatible const pair iterator of the map. klao@946: // typedef MapConstIterator ConstIterator; deba@822: klao@946: // /** Returns the begin iterator of the map. klao@946: // */ klao@946: // Iterator begin() { klao@946: // return Iterator(*this, KeyIt(*MapBase::getGraph())); klao@946: // } deba@822: klao@946: // /** Returns the end iterator of the map. klao@946: // */ klao@946: // Iterator end() { klao@946: // return Iterator(*this, INVALID); klao@946: // } deba@822: klao@946: // /** Returns the begin ConstIterator of the map. klao@946: // */ klao@946: // ConstIterator begin() const { klao@946: // return ConstIterator(*this, KeyIt(*MapBase::getGraph())); klao@946: // } deba@822: klao@946: // /** Returns the end const_iterator of the map. klao@946: // */ klao@946: // ConstIterator end() const { klao@946: // return ConstIterator(*this, INVALID); klao@946: // } deba@822: klao@946: // /// The KeySet of the Map. klao@946: // typedef MapConstKeySet ConstKeySet; deba@830: klao@946: // /// KeySet getter function. klao@946: // ConstKeySet keySet() const { klao@946: // return ConstKeySet(*this); klao@946: // } deba@830: klao@946: // /// The ConstValueSet of the Map. klao@946: // typedef MapConstValueSet ConstValueSet; deba@830: klao@946: // /// ConstValueSet getter function. klao@946: // ConstValueSet valueSet() const { klao@946: // return ConstValueSet(*this); klao@946: // } deba@830: klao@946: // /// The ValueSet of the Map. klao@946: // typedef MapValueSet ValueSet; deba@830: klao@946: // /// ValueSet getter function. klao@946: // ValueSet valueSet() { klao@946: // return ValueSet(*this); klao@946: // } deba@830: 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