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@1999: #ifndef LEMON_BITS_VECTOR_MAP_H deba@1999: #define LEMON_BITS_VECTOR_MAP_H deba@822: deba@822: #include klao@946: #include deba@822: deba@1999: #include deba@1993: #include deba@1999: deba@1307: #include deba@822: deba@2031: #include alpar@2260: #include deba@2031: deba@1999: ///\ingroup graphbits deba@1669: /// deba@822: ///\file deba@822: ///\brief Vector based graph maps. alpar@921: namespace lemon { deba@1669: deba@1996: /// \ingroup graphbits deba@1669: /// deba@1669: /// \brief Graph map based on the std::vector storage. deba@1669: /// klao@946: /// The VectorMap template class is graph map structure what deba@1999: /// automatically updates the map when a key is added to or erased from deba@2202: /// the map. This map type uses the std::vector to store the values. klao@946: /// deba@1999: /// \param Notifier The AlterationNotifier that will notify this map. deba@1703: /// \param Item The item type of the graph items. klao@946: /// \param Value The value type of the map. klao@946: /// deba@1999: /// \author Balazs Dezso deba@1999: template deba@1999: class VectorMap deba@1999: : public ItemSetTraits<_Graph, _Item>::ItemNotifier::ObserverBase { deba@1719: private: deba@1719: deba@1719: /// The container type of the map. deba@1719: typedef std::vector<_Value> Container; deba@1719: deba@822: public: deba@1703: klao@946: /// The graph type of the map. 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: klao@946: /// The key type of the map. alpar@987: typedef _Item Key; klao@946: /// The value type of the map. klao@946: typedef _Value Value; deba@1719: deba@1999: /// The notifier type. deba@1999: typedef typename ItemSetTraits<_Graph, _Item>::ItemNotifier Notifier; deba@822: deba@822: /// The map type. deba@822: typedef VectorMap Map; klao@946: /// The base class of the map. deba@1999: typedef typename Notifier::ObserverBase Parent; deba@822: deba@822: /// The reference type of the map; alpar@987: typedef typename Container::reference Reference; deba@822: /// The const reference type of the map; alpar@987: typedef typename Container::const_reference ConstReference; deba@822: deba@1267: deba@1999: /// \brief Constructor to attach the new map into the notifier. deba@1703: /// deba@1999: /// It constructs a map and attachs it into the notifier. klao@946: /// It adds all the items of the graph to the map. deba@1999: VectorMap(const Graph& graph) { deba@2384: Parent::attach(graph.notifier(Item())); deba@2384: container.resize(Parent::notifier()->maxId() + 1); klao@946: } deba@822: deba@1703: /// \brief Constructor uses given value to initialize the map. deba@1703: /// deba@1703: /// It constructs a map uses a given value to initialize the map. klao@946: /// It adds all the items of the graph to the map. deba@1999: VectorMap(const Graph& graph, const Value& value) { deba@2384: Parent::attach(graph.notifier(Item())); deba@2384: container.resize(Parent::notifier()->maxId() + 1, value); klao@946: } deba@822: deba@1703: /// \brief Copy constructor deba@1703: /// deba@1703: /// Copy constructor. deba@1999: VectorMap(const VectorMap& _copy) : Parent() { deba@980: if (_copy.attached()) { deba@2384: Parent::attach(*_copy.notifier()); deba@980: container = _copy.container; 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: VectorMap& operator=(const VectorMap& cmap) { deba@2031: return operator=(cmap); deba@2031: } deba@1669: deba@1669: 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: VectorMap& 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: public: deba@1669: deba@1703: /// \brief The subcript operator. deba@1703: /// klao@946: /// The subscript operator. The map can be subscripted by the deba@1703: /// actual items of the graph. alpar@987: Reference operator[](const Key& key) { deba@2384: return container[Parent::notifier()->id(key)]; deba@822: } deba@822: deba@1703: /// \brief The const subcript operator. deba@1703: /// klao@946: /// The const subscript operator. The map can be subscripted by the klao@946: /// actual items of the graph. alpar@987: ConstReference operator[](const Key& key) const { deba@2384: return container[Parent::notifier()->id(key)]; deba@822: } deba@822: deba@937: deba@1703: /// \brief The setter function of the map. deba@1703: /// klao@946: /// It the same as operator[](key) = value expression. alpar@987: void set(const Key& key, const Value& value) { klao@946: (*this)[key] = value; deba@822: } klao@946: deba@1669: protected: deba@1669: deba@1669: /// \brief Adds a new key to the map. deba@1669: /// deba@1999: /// It adds a new key to the map. It called by the observer notifier deba@1703: /// and it overrides the add() member function of the observer base. deba@1703: virtual void add(const Key& key) { deba@2384: int id = Parent::notifier()->id(key); deba@2386: if (id >= int(container.size())) { deba@822: container.resize(id + 1); deba@822: } deba@822: } deba@937: deba@1832: /// \brief Adds more new keys to the map. deba@1832: /// deba@1999: /// It adds more new keys to the map. It called by the observer notifier deba@1832: /// and it overrides the add() member function of the observer base. deba@1832: virtual void add(const std::vector& keys) { deba@1999: int max = container.size() - 1; deba@2386: for (int i = 0; i < int(keys.size()); ++i) { deba@2384: int id = Parent::notifier()->id(keys[i]); deba@1999: if (id >= max) { deba@1999: max = id; deba@1999: } deba@1832: } deba@1999: container.resize(max + 1); deba@1832: } deba@1832: deba@1703: /// \brief Erase a key from the map. deba@1703: /// deba@1999: /// Erase a key from the map. It called by the observer notifier klao@946: /// and it overrides the erase() member function of the observer base. deba@1999: virtual void erase(const Key& key) { deba@2384: container[Parent::notifier()->id(key)] = Value(); deba@1999: } deba@822: deba@1832: /// \brief Erase more keys from the map. deba@1832: /// deba@1999: /// Erase more keys from the map. It called by the observer notifier deba@1832: /// and it overrides the erase() member function of the observer base. deba@1999: virtual void erase(const std::vector& keys) { deba@2386: for (int i = 0; i < int(keys.size()); ++i) { deba@2384: container[Parent::notifier()->id(keys[i])] = Value(); deba@1999: } deba@1999: } deba@1832: deba@1703: /// \brief Buildes the map. deba@1703: /// deba@1999: /// It buildes the map. It called by the observer notifier klao@946: /// and it overrides the build() member function of the observer base. deba@1703: virtual void build() { deba@2384: int size = Parent::notifier()->maxId() + 1; deba@1999: container.reserve(size); deba@1999: container.resize(size); klao@946: } deba@937: deba@1703: /// \brief Clear the map. deba@1703: /// deba@1999: /// It erase all items from the map. It called by the observer notifier klao@946: /// and it overrides the clear() member function of the observer base. deba@1703: virtual void clear() { deba@822: container.clear(); deba@822: } deba@1267: deba@822: private: deba@822: deba@822: Container container; deba@822: klao@946: }; klao@946: deba@822: } deba@822: deba@822: #endif