/* -*- C++ -*- * * This file is a part of LEMON, a generic C++ optimization library * * Copyright (C) 2003-2006 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport * (Egervary Research Group on Combinatorial Optimization, EGRES). * * Permission to use, modify and distribute this software is granted * provided that this copyright notice appears in all copies. For * precise terms see the accompanying LICENSE file. * * This software is provided "AS IS" with no warranty of any kind, * express or implied, and with no claim as to its suitability for any * purpose. * */ #ifndef LEMON_VECTOR_MAP_H #define LEMON_VECTOR_MAP_H #include #include #include #include #include #include #include /// \ingroup graphmapfactory /// ///\file ///\brief Vector based graph maps. namespace lemon { /// \ingroup graphmapfactory /// /// \brief Graph map based on the std::vector storage. /// /// The VectorMap template class is graph map structure what /// automatically indates the map when a key is added to or erased from /// the map. This map factory uses the allocators to implement /// the container functionality. This map factory /// uses the std::vector to implement the container function. /// /// \param Registry The AlterationNotifier that will notify this map. /// \param Item The item type of the graph items. /// \param Value The value type of the map. /// /// \author Balazs Dezso template < typename _Graph, typename _Item, typename _Value > class VectorMap : public AlterationNotifier<_Item>::ObserverBase { private: /// The container type of the map. typedef std::vector<_Value> Container; public: /// The graph type of the map. typedef _Graph Graph; /// The reference map tag. typedef True ReferenceMapTag; /// The key type of the map. typedef _Item Key; /// The value type of the map. typedef _Value Value; typedef AlterationNotifier<_Item> Registry; /// The map type. typedef VectorMap Map; /// The base class of the map. typedef typename Registry::ObserverBase Parent; /// The reference type of the map; typedef typename Container::reference Reference; /// The pointer type of the map; typedef typename Container::pointer Pointer; /// The const value type of the map. typedef const Value ConstValue; /// The const reference type of the map; typedef typename Container::const_reference ConstReference; /// The pointer type of the map; typedef typename Container::const_pointer ConstPointer; /// \brief Constructor to attach the new map into the registry. /// /// It constructs a map and attachs it into the registry. /// It adds all the items of the graph to the map. VectorMap(const Graph& _g) : graph(&_g) { attach(_g.getNotifier(_Item())); build(); } /// \brief Constructor uses given value to initialize the map. /// /// It constructs a map uses a given value to initialize the map. /// It adds all the items of the graph to the map. VectorMap(const Graph& _g, const Value& _v) : graph(&_g) { attach(_g.getNotifier(_Item())); container.resize(graph->maxId(_Item()) + 1, _v); } /// \brief Copy constructor /// /// Copy constructor. VectorMap(const VectorMap& _copy) : Parent(), graph(_copy.getGraph()) { if (_copy.attached()) { attach(*_copy.getRegistry()); container = _copy.container; } } /// \brief Destrcutor /// /// Destructor. virtual ~VectorMap() { if (attached()) { detach(); } } private: VectorMap& operator=(const VectorMap&); protected: using Parent::attach; using Parent::detach; using Parent::attached; const Graph* getGraph() const { return graph; } public: /// \brief The subcript operator. /// /// The subscript operator. The map can be subscripted by the /// actual items of the graph. Reference operator[](const Key& key) { return container[graph->id(key)]; } /// \brief The const subcript operator. /// /// The const subscript operator. The map can be subscripted by the /// actual items of the graph. ConstReference operator[](const Key& key) const { return container[graph->id(key)]; } /// \brief The setter function of the map. /// /// It the same as operator[](key) = value expression. void set(const Key& key, const Value& value) { (*this)[key] = value; } protected: /// \brief Adds a new key to the map. /// /// It adds a new key to the map. It called by the observer registry /// and it overrides the add() member function of the observer base. virtual void add(const Key& key) { int id = graph->id(key); if (id >= (int)container.size()) { container.resize(id + 1); } } /// \brief Adds more new keys to the map. /// /// It adds more new keys to the map. It called by the observer registry /// and it overrides the add() member function of the observer base. virtual void add(const std::vector& keys) { for (int i = 0; i < (int)keys.size(); ++i) { add(keys[i]); } } /// \brief Erase a key from the map. /// /// Erase a key from the map. It called by the observer registry /// and it overrides the erase() member function of the observer base. virtual void erase(const Key&) {} /// \brief Erase more keys from the map. /// /// Erase more keys from the map. It called by the observer registry /// and it overrides the erase() member function of the observer base. virtual void erase(const std::vector&) {} /// \brief Buildes the map. /// /// It buildes the map. It called by the observer registry /// and it overrides the build() member function of the observer base. virtual void build() { container.resize(graph->maxId(_Item()) + 1); } /// \brief Clear the map. /// /// It erase all items from the map. It called by the observer registry /// and it overrides the clear() member function of the observer base. virtual void clear() { container.clear(); } private: Container container; const Graph *graph; }; } #endif