alpar@906: /* -*- C++ -*- alpar@921: * src/lemon/array_map.h - Part of LEMON, a generic C++ optimization library alpar@906: * alpar@906: * Copyright (C) 2004 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport alpar@906: * (Egervary Combinatorial Optimization Research Group, 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@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@822: * The template parameter is the MapRegistry that the maps alpar@987: * will belong to and the Value. deba@822: */ deba@822: klao@946: template klao@946: class ArrayMap : public AlterationObserverRegistry<_Item>::ObserverBase { deba@897: 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: klao@946: typedef AlterationObserverRegistry<_Item> Registry; klao@946: klao@946: private: deba@822: /// The iterator to iterate on the keys. klao@946: typedef _ItemIt KeyIt; 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: deba@822: public: deba@822: deba@822: /// The value type of the map. alpar@987: typedef _Value Value; deba@822: /// The reference type of the map; alpar@987: typedef Value& Reference; deba@822: /// The pointer type of the map; alpar@987: typedef Value* Pointer; deba@822: deba@822: /// The const value type of the map. alpar@987: typedef const Value ConstValue; deba@822: /// The const reference type of the map; alpar@987: typedef const Value& ConstReference; deba@822: /// The pointer type of the map; alpar@987: typedef const Value* ConstPointer; 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@980: attach(_g.getObserverRegistry(_Item())); deba@822: allocate_memory(); klao@946: for (KeyIt it(*graph); it != INVALID; ++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@980: attach(_g.getObserverRegistry(_Item())); deba@822: allocate_memory(); klao@946: for (KeyIt it(*graph); it != INVALID; ++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: */ klao@946: ArrayMap(const ArrayMap& copy) { 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); klao@946: for (KeyIt it(*graph); it != INVALID; ++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: klao@946: for (KeyIt it(*graph); it != INVALID; ++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: */ alpar@987: Reference 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: */ alpar@987: ConstReference 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); klao@946: for (KeyIt it(*graph); it != INVALID; ++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(); klao@946: for (KeyIt it(*graph); it != INVALID; ++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) { klao@946: for (KeyIt it(*graph); it != INVALID; ++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: 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@844: public: klao@946: // // STL compatibility typedefs. klao@946: // typedef Iterator iterator; klao@946: // typedef ConstIterator const_iterator; alpar@987: // typedef typename Iterator::PairValue value_type; alpar@987: // typedef typename Iterator::Key key_type; alpar@987: // typedef typename Iterator::Value data_type; alpar@987: // typedef typename Iterator::PairReference reference; alpar@987: // typedef typename Iterator::PairPointer pointer; alpar@987: // typedef typename ConstIterator::PairReference const_reference; alpar@987: // typedef typename ConstIterator::PairPointer const_pointer; klao@946: // typedef int difference_type; 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; klao@946: typedef typename Parent::NodeObserverRegistry NodeObserverRegistry; klao@946: klao@946: typedef typename Parent::Edge Edge; klao@946: typedef typename Parent::EdgeIt EdgeIt; klao@946: typedef typename Parent::EdgeObserverRegistry EdgeObserverRegistry; klao@946: klao@946: klao@946: klao@946: template deba@980: class NodeMap : public ArrayMap { 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@980: typedef ArrayMap 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@980: class EdgeMap : public ArrayMap { 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@980: typedef ArrayMap 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