deba@822: // -*- c++ -*- deba@822: #ifndef ARRAY_MAP_H deba@822: #define ARRAY_MAP_H deba@822: deba@822: #include deba@822: deba@822: #include deba@844: #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: deba@822: namespace hugo { 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 deba@822: * will belong to and the ValueType. deba@822: */ deba@822: deba@822: template deba@822: class ArrayMap : public MapRegistry::MapBase { deba@822: deba@822: public: deba@822: deba@822: /// The graph type of the maps. deba@822: typedef typename MapRegistry::Graph Graph; deba@822: /// The key type of the maps. deba@822: typedef typename MapRegistry::KeyType KeyType; deba@822: /// The iterator to iterate on the keys. deba@822: typedef typename MapRegistry::KeyIt KeyIt; deba@822: deba@822: /// The MapBase of the Map which imlements the core regisitry function. deba@822: typedef typename MapRegistry::MapBase MapBase; deba@822: deba@822: deba@822: public: deba@822: deba@822: /// The value type of the map. deba@822: typedef Value ValueType; deba@822: /// The reference type of the map; deba@822: typedef Value& ReferenceType; deba@822: /// The pointer type of the map; deba@822: typedef Value* PointerType; deba@822: deba@822: /// The const value type of the map. deba@822: typedef const Value ConstValueType; deba@822: /// The const reference type of the map; deba@822: typedef const Value& ConstReferenceType; deba@822: /// The pointer type of the map; deba@822: typedef const Value* ConstPointerType; deba@822: deba@822: deba@822: typedef std::allocator Allocator; deba@822: deba@822: deba@822: /** Default constructor for the map. deba@822: */ deba@822: ArrayMap() : values(0), capacity(0) {} deba@822: deba@822: /** Graph and Registry initialized map constructor. deba@822: */ deba@822: ArrayMap(const Graph& g, MapRegistry& r) : MapBase(g, r) { deba@822: allocate_memory(); deba@822: for (KeyIt it(*MapBase::getGraph()); it != INVALID; ++it) { deba@844: int id = KeyInfo::id(*MapBase::getGraph(), it); deba@822: allocator.construct(&(values[id]), Value()); deba@822: } deba@822: } deba@822: deba@822: /** Constructor to use default value to initialize the map. deba@822: */ deba@822: ArrayMap(const Graph& g, MapRegistry& r, const Value& v) deba@822: : MapBase(g, r) { deba@822: allocate_memory(); deba@822: for (KeyIt it(*MapBase::getGraph()); it != INVALID; ++it) { deba@844: int id = KeyInfo::id(*MapBase::getGraph(), it); deba@822: 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@822: ArrayMap(const ArrayMap& copy) : MapBase(*copy.graph, *copy.registry) { deba@822: capacity = copy.capacity; deba@822: if (capacity == 0) return; deba@822: values = allocator.allocate(capacity); deba@822: for (KeyIt it(*MapBase::getGraph()); it != INVALID; ++it) { deba@844: int id = KeyInfo::id(*MapBase::getGraph(), it); deba@822: allocator.construct(&(values[id]), copy.values[id]); deba@822: } deba@822: } deba@822: deba@822: /** Constructor to copy a map of an other map type. deba@822: */ deba@822: template ArrayMap(const CMap& copy) deba@822: : MapBase(copy), capacity(0), values(0) { deba@822: if (MapBase::getGraph()) { deba@822: allocate_memory(); deba@822: for (KeyIt it(*MapBase::getGraph()); it != INVALID; ++it) { deba@822: set(it, copy[it]); deba@822: } deba@822: } deba@822: } deba@822: 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@822: if (capacity != 0) { deba@822: MapBase::destroy(); deba@822: allocator.deallocate(values, capacity); deba@822: } deba@822: capacity = copy.capacity; deba@822: if (capacity == 0) return *this; deba@822: values = allocator.allocate(capacity); deba@822: for (KeyIt it(*MapBase::getGraph()); it != INVALID; ++it) { deba@844: int id = KeyInfo::id(*MapBase::getGraph(), it); deba@822: allocator.construct(&(values[id]), copy.values[id]); deba@822: } deba@822: return *this; deba@822: } deba@822: deba@822: /** Assign operator to copy a map an other map type. deba@822: */ deba@822: template ArrayMap& operator=(const CMap& copy) { deba@844: if (capacity != 0) { deba@822: MapBase::destroy(); deba@844: allocator.deallocate(values, capacity); deba@844: } deba@822: MapBase::operator=(copy); deba@822: if (MapBase::getGraph()) { deba@822: allocate_memory(); deba@822: for (KeyIt it(*MapBase::getGraph()); it != INVALID; ++it) { deba@822: set(it, copy[it]); deba@822: } deba@822: } deba@822: return *this; deba@822: } deba@822: deba@822: /** The destructor of the map. deba@822: */ deba@822: virtual ~ArrayMap() { deba@822: if (capacity != 0) { deba@822: MapBase::destroy(); deba@822: allocator.deallocate(values, capacity); 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@822: ReferenceType operator[](const KeyType& key) { deba@844: int id = KeyInfo::id(*MapBase::getGraph(), 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@822: ConstReferenceType operator[](const KeyType& key) const { deba@844: int id = KeyInfo::id(*MapBase::getGraph(), 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: */ deba@822: void set(const KeyType& key, const ValueType& val) { deba@844: int id = KeyInfo::id(*MapBase::getGraph(), key); deba@822: values[id] = val; deba@822: } deba@822: deba@822: /** Add a new key to the map. It called by the map registry. deba@822: */ deba@822: void add(const KeyType& key) { deba@844: int id = KeyInfo::id(*MapBase::getGraph(), 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: } deba@822: Value* new_values = allocator.allocate(new_capacity);; deba@822: for (KeyIt it(*MapBase::getGraph()); it != INVALID; ++it) { deba@844: int jd = KeyInfo::id(*MapBase::getGraph(), 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: */ deba@822: void erase(const KeyType& key) { deba@844: int id = KeyInfo::id(*MapBase::getGraph(), key); deba@822: allocator.destroy(&(values[id])); deba@822: } deba@822: deba@822: /** Clear the data structure. deba@822: */ deba@822: void clear() { deba@822: if (capacity != 0) { deba@822: MapBase::destroy(); deba@822: allocator.deallocate(values, capacity); deba@822: capacity = 0; deba@822: } deba@822: } deba@822: deba@822: /// The stl compatible pair iterator of the map. deba@822: typedef MapIterator Iterator; deba@822: /// The stl compatible const pair iterator of the map. deba@822: typedef MapConstIterator ConstIterator; deba@822: deba@822: /** Returns the begin iterator of the map. deba@822: */ deba@822: Iterator begin() { deba@822: return Iterator(*this, KeyIt(*MapBase::getGraph())); deba@822: } deba@822: deba@822: /** Returns the end iterator of the map. deba@822: */ deba@822: Iterator end() { deba@822: return Iterator(*this, INVALID); deba@822: } deba@822: deba@822: /** Returns the begin ConstIterator of the map. deba@822: */ deba@822: ConstIterator begin() const { deba@822: return ConstIterator(*this, KeyIt(*MapBase::getGraph())); deba@822: } deba@822: deba@822: /** Returns the end const_iterator of the map. deba@822: */ deba@822: ConstIterator end() const { deba@822: return ConstIterator(*this, INVALID); deba@822: } deba@822: deba@830: /// The KeySet of the Map. deba@830: typedef MapConstKeySet ConstKeySet; deba@830: deba@830: /// KeySet getter function. deba@830: ConstKeySet keySet() const { deba@830: return ConstKeySet(*this); deba@830: } deba@830: deba@830: /// The ConstValueSet of the Map. deba@830: typedef MapConstValueSet ConstValueSet; deba@830: deba@830: /// ConstValueSet getter function. deba@830: ConstValueSet valueSet() const { deba@830: return ConstValueSet(*this); deba@830: } deba@830: deba@830: /// The ValueSet of the Map. deba@830: typedef MapValueSet ValueSet; deba@830: deba@830: /// ValueSet getter function. deba@830: ValueSet valueSet() { deba@830: return ValueSet(*this); deba@830: } deba@830: deba@822: private: deba@822: deba@822: void allocate_memory() { deba@844: int max_id = KeyInfo::maxId(*MapBase::getGraph()); 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: deba@822: int capacity; deba@822: Value* values; deba@822: Allocator allocator; deba@844: deba@844: public: deba@844: // STL compatibility typedefs. deba@844: typedef Iterator iterator; deba@844: typedef ConstIterator const_iterator; deba@844: typedef typename Iterator::PairValueType value_type; deba@844: typedef typename Iterator::KeyType key_type; deba@844: typedef typename Iterator::ValueType data_type; deba@844: typedef typename Iterator::PairReferenceType reference; deba@844: typedef typename Iterator::PairPointerType pointer; deba@844: typedef typename ConstIterator::PairReferenceType const_reference; deba@844: typedef typename ConstIterator::PairPointerType const_pointer; deba@844: typedef int difference_type; deba@822: }; deba@822: deba@822: /// @} deba@822: deba@822: } deba@822: deba@822: #endif