diff -r f2ea4aac9ada -r c94ef40a22ce src/lemon/array_map.h --- a/src/lemon/array_map.h Mon Oct 25 13:29:46 2004 +0000 +++ b/src/lemon/array_map.h Wed Oct 27 22:38:50 2004 +0000 @@ -20,7 +20,6 @@ #include #include -#include ///\ingroup graphmaps ///\file @@ -42,22 +41,32 @@ * will belong to and the ValueType. */ - template - class ArrayMap : public MapRegistry::MapBase { + template + class ArrayMap : public AlterationObserverRegistry<_Item>::ObserverBase { - template friend class ArrayMap; - public: /// The graph type of the maps. - typedef typename MapRegistry::Graph Graph; + typedef _Graph Graph; /// The key type of the maps. - typedef typename MapRegistry::KeyType KeyType; + typedef _Item KeyType; + + typedef AlterationObserverRegistry<_Item> Registry; + + private: /// The iterator to iterate on the keys. - typedef typename MapRegistry::KeyIt KeyIt; + typedef _ItemIt KeyIt; + + typedef _IdMap IdMap; + + typedef _Value Value; /// The MapBase of the Map which imlements the core regisitry function. - typedef typename MapRegistry::MapBase MapBase; + typedef typename Registry::ObserverBase Parent; public: @@ -77,52 +86,47 @@ typedef const Value* ConstPointerType; + private: typedef std::allocator Allocator; - + + public: + /** Graph and Registry initialized map constructor. */ - ArrayMap(const Graph& g, MapRegistry& r) : MapBase(g, r) { + ArrayMap(const Graph& _g, Registry& _r) : graph(&_g) { + attach(_r); allocate_memory(); - for (KeyIt it(*MapBase::getGraph()); it != INVALID; ++it) { - int id = KeyInfo::id(*MapBase::getGraph(), it); + for (KeyIt it(*graph); it != INVALID; ++it) { + int id = IdMap(*graph)[it]; allocator.construct(&(values[id]), Value()); } } - /** Constructor to use default value to initialize the map. - */ - ArrayMap(const Graph& g, MapRegistry& r, const Value& v) - : MapBase(g, r) { + /// Constructor to use default value to initialize the map. + + /// It constrates a map and initialize all of the the map. + + ArrayMap(const Graph& _g, Registry& _r, const Value& _v) : graph(&_g) { + attach(_r); allocate_memory(); - for (KeyIt it(*MapBase::getGraph()); it != INVALID; ++it) { - int id = KeyInfo::id(*MapBase::getGraph(), it); - allocator.construct(&(values[id]), v); + for (KeyIt it(*graph); it != INVALID; ++it) { + int id = IdMap(*graph)[it]; + allocator.construct(&(values[id]), _v); } } /** Constructor to copy a map of the same map type. */ - ArrayMap(const ArrayMap& copy) : MapBase(copy) { + ArrayMap(const ArrayMap& copy) { + if (copy.attached()) { + attach(*copy.getRegistry()); + } capacity = copy.capacity; if (capacity == 0) return; values = allocator.allocate(capacity); - for (KeyIt it(*MapBase::getGraph()); it != INVALID; ++it) { - int id = KeyInfo::id(*MapBase::getGraph(), it); - allocator.construct(&(values[id]), copy.values[id]); - } - } - - /** Constructor to copy a map of an other map type. - */ - template - ArrayMap(const ArrayMap& copy) - : MapBase(copy) { - capacity = copy.capacity; - if (capacity == 0) return; - values = allocator.allocate(capacity); - for (KeyIt it(*MapBase::getGraph()); it != INVALID; ++it) { - int id = KeyInfo::id(*MapBase::getGraph(), it); + for (KeyIt it(*graph); it != INVALID; ++it) { + int id = IdMap(*graph)[it]; allocator.construct(&(values[id]), copy.values[id]); } } @@ -132,58 +136,33 @@ ArrayMap& operator=(const ArrayMap& copy) { if (© == this) return *this; - if (MapBase::getGraph() != copy.getGraph()) { - if (capacity != 0) { - MapBase::destroy(); - allocator.deallocate(values, capacity); + if (graph != copy.graph) { + if (attached()) { + clear(); + detach(); } - - MapBase::operator=(copy); + if (copy.attached()) { + attach(*copy.getRegistry()); + } capacity = copy.capacity; if (capacity == 0) return *this; values = allocator.allocate(capacity); } - for (KeyIt it(*MapBase::getGraph()); it != INVALID; ++it) { - int id = KeyInfo::id(*MapBase::getGraph(), it); + for (KeyIt it(*graph); it != INVALID; ++it) { + int id = IdMap(*graph)[it]; allocator.construct(&(values[id]), copy.values[id]); } return *this; } - /** Assign operator to copy a map of an other map type. - */ - template - ArrayMap& operator=(const ArrayMap& copy) { - - if (MapBase::getGraph() != copy.getGraph()) { - if (capacity != 0) { - MapBase::destroy(); - allocator.deallocate(values, capacity); - } - - MapBase::operator=(copy); - - capacity = copy.capacity; - if (capacity == 0) return *this; - values = allocator.allocate(capacity); - } - - for (KeyIt it(*MapBase::getGraph()); it != INVALID; ++it) { - int id = KeyInfo::id(*MapBase::getGraph(), it); - allocator.construct(&(values[id]), copy.values[id]); - } - - return *this; - } - /** The destructor of the map. */ - virtual ~ArrayMap() { - if (capacity != 0) { - MapBase::destroy(); - allocator.deallocate(values, capacity); + virtual ~ArrayMap() { + if (attached()) { + clear(); + detach(); } } @@ -193,7 +172,7 @@ * actual keys of the graph. */ ReferenceType operator[](const KeyType& key) { - int id = KeyInfo::id(*MapBase::getGraph(), key); + int id = IdMap(*graph)[key]; return values[id]; } @@ -202,7 +181,7 @@ * actual keys of the graph. */ ConstReferenceType operator[](const KeyType& key) const { - int id = KeyInfo::id(*MapBase::getGraph(), key); + int id = IdMap(*graph)[key]; return values[id]; } @@ -210,22 +189,21 @@ * This is a compatibility feature with the not dereferable maps. */ void set(const KeyType& key, const ValueType& val) { - int id = KeyInfo::id(*MapBase::getGraph(), key); - values[id] = val; + (*this)[key] = val; } /** Add a new key to the map. It called by the map registry. */ void add(const KeyType& key) { - int id = KeyInfo::id(*MapBase::getGraph(), key); + int id = IdMap(*graph)[key]; if (id >= capacity) { int new_capacity = (capacity == 0 ? 1 : capacity); while (new_capacity <= id) { new_capacity <<= 1; } - Value* new_values = allocator.allocate(new_capacity);; - for (KeyIt it(*MapBase::getGraph()); it != INVALID; ++it) { - int jd = KeyInfo::id(*MapBase::getGraph(), it); + Value* new_values = allocator.allocate(new_capacity); + for (KeyIt it(*graph); it != INVALID; ++it) { + int jd = IdMap(*graph)[it]; if (id != jd) { allocator.construct(&(new_values[jd]), values[jd]); allocator.destroy(&(values[jd])); @@ -241,77 +219,86 @@ /** Erase a key from the map. It called by the map registry. */ void erase(const KeyType& key) { - int id = KeyInfo::id(*MapBase::getGraph(), key); + int id = IdMap(*graph)[key]; allocator.destroy(&(values[id])); } - /** Clear the data structure. - */ + void build() { + allocate_memory(); + for (KeyIt it(*graph); it != INVALID; ++it) { + int id = IdMap(*graph)[it]; + allocator.construct(&(values[id]), Value()); + } + } + void clear() { if (capacity != 0) { - MapBase::destroy(); + for (KeyIt it(*graph); it != INVALID; ++it) { + int id = IdMap(*graph)[it]; + allocator.destroy(&(values[id])); + } allocator.deallocate(values, capacity); capacity = 0; } } - /// The stl compatible pair iterator of the map. - typedef MapIterator Iterator; - /// The stl compatible const pair iterator of the map. - typedef MapConstIterator ConstIterator; +// /// The stl compatible pair iterator of the map. +// typedef MapIterator Iterator; +// /// The stl compatible const pair iterator of the map. +// typedef MapConstIterator ConstIterator; - /** Returns the begin iterator of the map. - */ - Iterator begin() { - return Iterator(*this, KeyIt(*MapBase::getGraph())); - } +// /** Returns the begin iterator of the map. +// */ +// Iterator begin() { +// return Iterator(*this, KeyIt(*MapBase::getGraph())); +// } - /** Returns the end iterator of the map. - */ - Iterator end() { - return Iterator(*this, INVALID); - } +// /** Returns the end iterator of the map. +// */ +// Iterator end() { +// return Iterator(*this, INVALID); +// } - /** Returns the begin ConstIterator of the map. - */ - ConstIterator begin() const { - return ConstIterator(*this, KeyIt(*MapBase::getGraph())); - } +// /** Returns the begin ConstIterator of the map. +// */ +// ConstIterator begin() const { +// return ConstIterator(*this, KeyIt(*MapBase::getGraph())); +// } - /** Returns the end const_iterator of the map. - */ - ConstIterator end() const { - return ConstIterator(*this, INVALID); - } +// /** Returns the end const_iterator of the map. +// */ +// ConstIterator end() const { +// return ConstIterator(*this, INVALID); +// } - /// The KeySet of the Map. - typedef MapConstKeySet ConstKeySet; +// /// The KeySet of the Map. +// typedef MapConstKeySet ConstKeySet; - /// KeySet getter function. - ConstKeySet keySet() const { - return ConstKeySet(*this); - } +// /// KeySet getter function. +// ConstKeySet keySet() const { +// return ConstKeySet(*this); +// } - /// The ConstValueSet of the Map. - typedef MapConstValueSet ConstValueSet; +// /// The ConstValueSet of the Map. +// typedef MapConstValueSet ConstValueSet; - /// ConstValueSet getter function. - ConstValueSet valueSet() const { - return ConstValueSet(*this); - } +// /// ConstValueSet getter function. +// ConstValueSet valueSet() const { +// return ConstValueSet(*this); +// } - /// The ValueSet of the Map. - typedef MapValueSet ValueSet; +// /// The ValueSet of the Map. +// typedef MapValueSet ValueSet; - /// ValueSet getter function. - ValueSet valueSet() { - return ValueSet(*this); - } +// /// ValueSet getter function. +// ValueSet valueSet() { +// return ValueSet(*this); +// } private: void allocate_memory() { - int max_id = KeyInfo::maxId(*MapBase::getGraph()); + int max_id = IdMap(*graph).maxId(); if (max_id == -1) { capacity = 0; values = 0; @@ -324,24 +311,88 @@ values = allocator.allocate(capacity); } + const Graph* graph; int capacity; Value* values; Allocator allocator; public: - // STL compatibility typedefs. - typedef Iterator iterator; - typedef ConstIterator const_iterator; - typedef typename Iterator::PairValueType value_type; - typedef typename Iterator::KeyType key_type; - typedef typename Iterator::ValueType data_type; - typedef typename Iterator::PairReferenceType reference; - typedef typename Iterator::PairPointerType pointer; - typedef typename ConstIterator::PairReferenceType const_reference; - typedef typename ConstIterator::PairPointerType const_pointer; - typedef int difference_type; +// // STL compatibility typedefs. +// typedef Iterator iterator; +// typedef ConstIterator const_iterator; +// typedef typename Iterator::PairValueType value_type; +// typedef typename Iterator::KeyType key_type; +// typedef typename Iterator::ValueType data_type; +// typedef typename Iterator::PairReferenceType reference; +// typedef typename Iterator::PairPointerType pointer; +// typedef typename ConstIterator::PairReferenceType const_reference; +// typedef typename ConstIterator::PairPointerType const_pointer; +// typedef int difference_type; }; + template + class ArrayMappableGraphExtender : public _Base { + public: + + typedef ArrayMappableGraphExtender<_Base> Graph; + typedef _Base Parent; + + typedef typename Parent::Node Node; + typedef typename Parent::NodeIt NodeIt; + typedef typename Parent::NodeIdMap NodeIdMap; + typedef typename Parent::NodeObserverRegistry NodeObserverRegistry; + + typedef typename Parent::Edge Edge; + typedef typename Parent::EdgeIt EdgeIt; + typedef typename Parent::EdgeIdMap EdgeIdMap; + typedef typename Parent::EdgeObserverRegistry EdgeObserverRegistry; + + + + template + class NodeMap : public ArrayMap { + public: + typedef ArrayMappableGraphExtender<_Base> Graph; + + typedef typename Graph::Node Node; + typedef typename Graph::NodeIt NodeIt; + typedef typename Graph::NodeIdMap NodeIdMap; + + typedef ArrayMap Parent; + + typedef typename Parent::Graph Graph; + typedef typename Parent::Value Value; + + NodeMap(const Graph& g) + : Parent(g, g.getNodeObserverRegistry()) {} + NodeMap(const Graph& g, const Value& v) + : Parent(g, g.getNodeObserverRegistry(), v) {} + + }; + + template + class EdgeMap : public ArrayMap { + public: + typedef ArrayMappableGraphExtender<_Base> Graph; + + typedef typename Graph::Edge Edge; + typedef typename Graph::EdgeIt EdgeIt; + typedef typename Graph::EdgeIdMap EdgeIdMap; + + typedef ArrayMap Parent; + + typedef typename Parent::Graph Graph; + typedef typename Parent::Value Value; + + EdgeMap(const Graph& g) + : Parent(g, g.getEdgeObserverRegistry()) {} + EdgeMap(const Graph& g, const Value& v) + : Parent(g, g.getEdgeObserverRegistry(), v) {} + + }; + + }; + /// @} }