2  * lemon/vector_map.h - Part of LEMON, a generic C++ optimization library
 
     4  * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
 
     5  * (Egervary Research Group on Combinatorial Optimization, EGRES).
 
     7  * Permission to use, modify and distribute this software is granted
 
     8  * provided that this copyright notice appears in all copies. For
 
     9  * precise terms see the accompanying LICENSE file.
 
    11  * This software is provided "AS IS" with no warranty of any kind,
 
    12  * express or implied, and with no claim as to its suitability for any
 
    17 #ifndef LEMON_VECTOR_MAP_H
 
    18 #define LEMON_VECTOR_MAP_H
 
    23 #include <lemon/utility.h>
 
    24 #include <lemon/bits/map_iterator.h>
 
    25 #include <lemon/bits/alteration_notifier.h>
 
    26 #include <lemon/concept_check.h>
 
    27 #include <lemon/concept/maps.h>
 
    29 /// \ingroup graphmapfactory
 
    32 ///\brief Vector based graph maps.
 
    36   /// \ingroup graphmapfactory
 
    38   /// \brief Graph map based on the std::vector storage.
 
    40   /// The VectorMap template class is graph map structure what
 
    41   /// automatically updates the map when a key is added to or erased from
 
    42   /// the map. This map factory uses the allocators to implement 
 
    43   /// the container functionality. This map factory
 
    44   /// uses the std::vector to implement the container function.
 
    46   /// \param Registry The AlterationNotifier that will notify this map.
 
    47   /// \param IdMap The IdMap type of the graph items.
 
    48   /// \param Value The value type of the map.
 
    50   /// \author Balazs Dezso
 
    58   class VectorMap : public AlterationNotifier<_Item>::ObserverBase {
 
    61     /// The graph type of the map. 
 
    63     /// The key type of the map.
 
    65     /// The id map type of the map.
 
    66     typedef AlterationNotifier<_Item> Registry;
 
    67     /// The value type of the map.
 
    71     typedef VectorMap Map;
 
    72     /// The base class of the map.
 
    73     typedef typename Registry::ObserverBase Parent;
 
    77     /// The container type of the map.
 
    78     typedef std::vector<Value> Container;	
 
    82     /// The reference type of the map;
 
    83     typedef typename Container::reference Reference;
 
    84     /// The pointer type of the map;
 
    85     typedef typename Container::pointer Pointer;
 
    87     /// The const value type of the map.
 
    88     typedef const Value ConstValue;
 
    89     /// The const reference type of the map;
 
    90     typedef typename Container::const_reference ConstReference;
 
    91     /// The pointer type of the map;
 
    92     typedef typename Container::const_pointer ConstPointer;
 
    94     typedef True FullTypeTag;
 
    96     /// Constructor to attach the new map into the registry.
 
    98     /// It construates a map and attachs it into the registry.
 
    99     /// It adds all the items of the graph to the map.
 
   101     VectorMap(const Graph& _g) : graph(&_g) {
 
   102       attach(_g.getNotifier(_Item()));
 
   106     /// Constructor uses given value to initialize the map. 
 
   108     /// It construates a map uses a given value to initialize the map. 
 
   109     /// It adds all the items of the graph to the map.
 
   111     VectorMap(const Graph& _g, const Value& _v) : graph(&_g) { 
 
   112       attach(_g.getNotifier(_Item()));
 
   113       container.resize(graph->maxId(_Item()) + 1, _v);
 
   116     VectorMap(const VectorMap& _copy) 
 
   117       : Parent(), graph(_copy.getGraph()) {
 
   118       if (_copy.attached()) {
 
   119 	attach(*_copy.getRegistry());
 
   120 	container = _copy.container;
 
   124     virtual ~VectorMap() {
 
   133     VectorMap& operator=(const VectorMap&);
 
   137     using Parent::attach;
 
   138     using Parent::detach;
 
   139     using Parent::attached;
 
   141     const Graph* getGraph() const {
 
   147     /// The subcript operator.
 
   149     /// The subscript operator. The map can be subscripted by the
 
   150     /// actual items of the graph. 
 
   152     Reference operator[](const Key& key) {
 
   153       return container[graph->id(key)];
 
   156     /// The const subcript operator.
 
   158     /// The const subscript operator. The map can be subscripted by the
 
   159     /// actual items of the graph. 
 
   161     ConstReference operator[](const Key& key) const {
 
   162       return container[graph->id(key)];
 
   166     /// The setter function of the map.
 
   168     /// It the same as operator[](key) = value expression.
 
   170     void set(const Key& key, const Value& value) {
 
   171       (*this)[key] = value;
 
   176     /// \brief Adds a new key to the map.
 
   178     /// It adds a new key to the map. It called by the observer registry
 
   179     /// and it overrides the add() member function of the observer base.
 
   181     void add(const Key& key) {
 
   182       int id = graph->id(key);
 
   183       if (id >= (int)container.size()) {
 
   184 	container.resize(id + 1);
 
   188     /// Erases a key from the map.
 
   190     /// Erase a key from the map. It called by the observer registry
 
   191     /// and it overrides the erase() member function of the observer base.     
 
   192     void erase(const Key&) {}
 
   196     /// It buildes the map. It called by the observer registry
 
   197     /// and it overrides the build() member function of the observer base.
 
   200       container.resize(graph->maxId(_Item()) + 1);
 
   205     /// It erase all items from the map. It called by the observer registry
 
   206     /// and it overrides the clear() member function of the observer base.