alpar@906: /* -*- C++ -*- alpar@906: * alpar@1956: * This file is a part of LEMON, a generic C++ optimization library alpar@1956: * alpar@2553: * Copyright (C) 2003-2008 alpar@1956: * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport alpar@1956: * (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: deba@1996: #ifndef LEMON_BITS_ARRAY_MAP_H deba@1996: #define LEMON_BITS_ARRAY_MAP_H deba@822: deba@822: #include deba@1999: deba@1999: #include deba@1813: #include deba@2031: #include alpar@2260: #include deba@822: deba@1996: /// \ingroup graphbits deba@1669: /// \file deba@1999: /// \brief Graph map based on the array storage. deba@822: alpar@921: namespace lemon { deba@822: deba@1996: /// \ingroup graphbits deba@1669: /// deba@1669: /// \brief Graph map based on the array storage. deba@1669: /// deba@1414: /// The ArrayMap template class is graph map structure what deba@1999: /// automatically updates the map when a key is added to or erased from deba@1669: /// the map. This map uses the allocators to implement deba@1414: /// the container functionality. deba@1414: /// deba@2202: /// The template parameters are the Graph the current Item type and deba@1999: /// the Value type of the map. deba@1999: template deba@1999: class ArrayMap deba@1999: : public ItemSetTraits<_Graph, _Item>::ItemNotifier::ObserverBase { deba@822: public: deba@822: /// The graph type of the maps. klao@946: typedef _Graph Graph; deba@1999: /// The item type of the map. deba@1999: typedef _Item Item; deba@1719: /// The reference map tag. deba@1719: typedef True ReferenceMapTag; deba@1719: deba@822: /// The key type of the maps. alpar@987: typedef _Item Key; deba@1719: /// The value type of the map. deba@1719: typedef _Value Value; deba@1999: deba@1719: /// The const reference type of the map. deba@1719: typedef const _Value& ConstReference; deba@1719: /// The reference type of the map. deba@1719: typedef _Value& Reference; deba@1719: deba@1999: /// The notifier type. deba@1999: typedef typename ItemSetTraits<_Graph, _Item>::ItemNotifier Notifier; klao@946: deba@822: /// The MapBase of the Map which imlements the core regisitry function. deba@1999: typedef typename Notifier::ObserverBase Parent; deba@822: klao@946: private: deba@822: typedef std::allocator Allocator; deba@822: klao@946: public: klao@946: deba@1719: /// \brief Graph initialized map constructor. deba@1703: /// deba@1719: /// Graph initialized map constructor. klao@2046: explicit ArrayMap(const Graph& graph) { deba@2384: Parent::attach(graph.notifier(Item())); deba@1999: allocate_memory(); deba@2386: Notifier* nf = Parent::notifier(); deba@1267: Item it; deba@2386: for (nf->first(it); it != INVALID; nf->next(it)) { deba@2386: int id = nf->id(it);; deba@822: allocator.construct(&(values[id]), Value()); deba@822: } deba@822: } deba@822: deba@1703: /// \brief Constructor to use default value to initialize the map. deba@1703: /// deba@1703: /// It constructs a map and initialize all of the the map. deba@1999: ArrayMap(const Graph& graph, const Value& value) { deba@2384: Parent::attach(graph.notifier(Item())); deba@1999: allocate_memory(); deba@2386: Notifier* nf = Parent::notifier(); deba@1267: Item it; deba@2386: for (nf->first(it); it != INVALID; nf->next(it)) { deba@2386: int id = nf->id(it);; deba@1999: allocator.construct(&(values[id]), value); deba@822: } deba@822: } deba@822: deba@1703: /// \brief Constructor to copy a map of the same map type. deba@1703: /// deba@1703: /// Constructor to copy a map of the same map type. deba@1999: ArrayMap(const ArrayMap& copy) : Parent() { klao@946: if (copy.attached()) { deba@2384: attach(*copy.notifier()); klao@946: } deba@822: capacity = copy.capacity; deba@822: if (capacity == 0) return; deba@822: values = allocator.allocate(capacity); deba@2386: Notifier* nf = Parent::notifier(); deba@1267: Item it; deba@2386: for (nf->first(it); it != INVALID; nf->next(it)) { deba@2386: int id = nf->id(it);; deba@891: allocator.construct(&(values[id]), copy.values[id]); deba@822: } deba@822: } deba@822: deba@2031: /// \brief Assign operator. deba@2031: /// deba@2031: /// This operator assigns for each item in the map the deba@2031: /// value mapped to the same item in the copied map. deba@2031: /// The parameter map should be indiced with the same deba@2031: /// itemset because this assign operator does not change deba@2031: /// the container of the map. deba@2031: ArrayMap& operator=(const ArrayMap& cmap) { deba@2031: return operator=(cmap); deba@2031: } deba@2031: deba@2031: deba@2031: /// \brief Template assign operator. deba@2031: /// deba@2031: /// The given parameter should be conform to the ReadMap deba@2031: /// concecpt and could be indiced by the current item set of deba@2031: /// the NodeMap. In this case the value for each item deba@2031: /// is assigned by the value of the given ReadMap. deba@2031: template deba@2031: ArrayMap& operator=(const CMap& cmap) { alpar@2260: checkConcept, CMap>(); deba@2386: const typename Parent::Notifier* nf = Parent::notifier(); deba@2031: Item it; deba@2386: for (nf->first(it); it != INVALID; nf->next(it)) { deba@2031: set(it, cmap[it]); deba@2031: } deba@2031: return *this; deba@2031: } deba@2031: deba@1669: /// \brief The destructor of the map. deba@1669: /// deba@1414: /// The destructor of the map. klao@946: virtual ~ArrayMap() { klao@946: if (attached()) { klao@946: clear(); klao@946: detach(); deba@822: } deba@822: } deba@1669: deba@1669: protected: deba@1669: deba@1669: using Parent::attach; deba@1669: using Parent::detach; deba@1669: using Parent::attached; deba@1669: deba@1669: public: deba@1669: deba@1703: /// \brief The subscript operator. deba@1703: /// deba@1703: /// The subscript operator. The map can be subscripted by the deba@1703: /// actual keys of the graph. deba@1267: Value& operator[](const Key& key) { deba@2384: int id = Parent::notifier()->id(key); deba@822: return values[id]; deba@822: } deba@822: deba@1703: /// \brief The const subscript operator. deba@1703: /// deba@1703: /// The const subscript operator. The map can be subscripted by the deba@1703: /// actual keys of the graph. deba@1267: const Value& operator[](const Key& key) const { deba@2384: int id = Parent::notifier()->id(key); deba@822: return values[id]; deba@822: } deba@1703: deba@1703: /// \brief Setter function of the map. deba@1703: /// deba@1414: /// Setter function of the map. Equivalent with map[key] = val. deba@1414: /// This is a compatibility feature with the not dereferable maps. alpar@987: void set(const Key& key, const Value& val) { klao@946: (*this)[key] = val; deba@822: } deba@1669: deba@1669: protected: deba@1703: deba@1999: /// \brief Adds a new key to the map. deba@1999: /// deba@1999: /// It adds a new key to the map. It called by the observer notifier deba@1999: /// and it overrides the add() member function of the observer base. deba@1703: virtual void add(const Key& key) { deba@2386: Notifier* nf = Parent::notifier(); deba@2386: int id = nf->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@2386: for (nf->first(it); it != INVALID; nf->next(it)) { deba@2386: int jd = nf->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@1999: /// \brief Adds more new keys to the map. deba@1999: /// deba@1999: /// It adds more new keys to the map. It called by the observer notifier deba@1999: /// and it overrides the add() member function of the observer base. deba@1703: virtual void add(const std::vector& keys) { deba@2386: Notifier* nf = Parent::notifier(); deba@1414: int max_id = -1; deba@2386: for (int i = 0; i < int(keys.size()); ++i) { deba@2386: int id = nf->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@2386: for (nf->first(it); it != INVALID; nf->next(it)) { deba@2386: int id = nf->id(it); deba@1414: bool found = false; deba@2386: for (int i = 0; i < int(keys.size()); ++i) { deba@2386: int jd = nf->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@2386: for (int i = 0; i < int(keys.size()); ++i) { deba@2386: int id = nf->id(keys[i]); deba@1414: allocator.construct(&(values[id]), Value()); deba@1414: } deba@1414: } deba@822: deba@1999: /// \brief Erase a key from the map. deba@1999: /// deba@1999: /// Erase a key from the map. It called by the observer notifier deba@1999: /// and it overrides the erase() member function of the observer base. deba@1703: virtual void erase(const Key& key) { deba@2384: int id = Parent::notifier()->id(key); deba@822: allocator.destroy(&(values[id])); deba@822: } deba@822: deba@1999: /// \brief Erase more keys from the map. deba@1999: /// deba@1999: /// Erase more keys from the map. It called by the observer notifier deba@1999: /// and it overrides the erase() member function of the observer base. deba@1703: virtual void erase(const std::vector& keys) { deba@2386: for (int i = 0; i < int(keys.size()); ++i) { deba@2384: int id = Parent::notifier()->id(keys[i]); deba@1414: allocator.destroy(&(values[id])); deba@1414: } deba@1414: } deba@1414: deba@1999: /// \brief Buildes the map. deba@1999: /// deba@1999: /// It buildes the map. It called by the observer notifier deba@1999: /// and it overrides the build() member function of the observer base. deba@1703: virtual void build() { deba@2386: Notifier* nf = Parent::notifier(); klao@946: allocate_memory(); deba@1267: Item it; deba@2386: for (nf->first(it); it != INVALID; nf->next(it)) { deba@2386: int id = nf->id(it);; klao@946: allocator.construct(&(values[id]), Value()); klao@946: } klao@946: } klao@946: deba@1999: /// \brief Clear the map. deba@1999: /// deba@1999: /// It erase all items from the map. It called by the observer notifier deba@1999: /// and it overrides the clear() member function of the observer base. deba@1703: virtual void clear() { deba@2386: Notifier* nf = Parent::notifier(); deba@822: if (capacity != 0) { deba@1267: Item it; deba@2386: for (nf->first(it); it != INVALID; nf->next(it)) { deba@2386: int id = nf->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@822: private: deba@822: deba@822: void allocate_memory() { deba@2384: int max_id = Parent::notifier()->maxId(); 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@822: }; deba@822: deba@822: } deba@822: deba@1996: #endif