COIN-OR::LEMON - Graph Library

source: lemon-0.x/lemon/bits/vector_map.h @ 2333:8070a099ffb6

Last change on this file since 2333:8070a099ffb6 was 2260:4274224f8a7d, checked in by Alpar Juttner, 17 years ago

concept -> concepts (namespace & directory)

File size: 7.3 KB
RevLine 
[906]1/* -*- C++ -*-
2 *
[1956]3 * This file is a part of LEMON, a generic C++ optimization library
4 *
5 * Copyright (C) 2003-2006
6 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 * (Egervary Research Group on Combinatorial Optimization, EGRES).
[906]8 *
9 * Permission to use, modify and distribute this software is granted
10 * provided that this copyright notice appears in all copies. For
11 * precise terms see the accompanying LICENSE file.
12 *
13 * This software is provided "AS IS" with no warranty of any kind,
14 * express or implied, and with no claim as to its suitability for any
15 * purpose.
16 *
17 */
18
[1999]19#ifndef LEMON_BITS_VECTOR_MAP_H
20#define LEMON_BITS_VECTOR_MAP_H
[822]21
22#include <vector>
[946]23#include <algorithm>
[822]24
[1999]25#include <lemon/bits/traits.h>
[1993]26#include <lemon/bits/utility.h>
[1999]27
[1307]28#include <lemon/bits/alteration_notifier.h>
[822]29
[2031]30#include <lemon/concept_check.h>
[2260]31#include <lemon/concepts/maps.h>
[2031]32
[1999]33///\ingroup graphbits
[1669]34///
[822]35///\file
36///\brief Vector based graph maps.
[921]37namespace lemon {
[1669]38
[1996]39  /// \ingroup graphbits
[1669]40  ///
41  /// \brief Graph map based on the std::vector storage.
42  ///
[946]43  /// The VectorMap template class is graph map structure what
[1999]44  /// automatically updates the map when a key is added to or erased from
[2202]45  /// the map. This map type uses the std::vector to store the values.
[946]46  ///
[1999]47  /// \param Notifier The AlterationNotifier that will notify this map.
[1703]48  /// \param Item The item type of the graph items.
[946]49  /// \param Value The value type of the map.
50  ///
[1999]51  /// \author Balazs Dezso     
52  template <typename _Graph, typename _Item, typename _Value>
53  class VectorMap
54    : public ItemSetTraits<_Graph, _Item>::ItemNotifier::ObserverBase {
[1719]55  private:
56               
57    /// The container type of the map.
58    typedef std::vector<_Value> Container;     
59
[822]60  public:
[1703]61
[946]62    /// The graph type of the map.
63    typedef _Graph Graph;
[1999]64    /// The item type of the map.
65    typedef _Item Item;
[1719]66    /// The reference map tag.
67    typedef True ReferenceMapTag;
68
[946]69    /// The key type of the map.
[987]70    typedef _Item Key;
[946]71    /// The value type of the map.
72    typedef _Value Value;
[1719]73
[1999]74    /// The notifier type.
75    typedef typename ItemSetTraits<_Graph, _Item>::ItemNotifier Notifier;
[822]76
77    /// The map type.
78    typedef VectorMap Map;
[946]79    /// The base class of the map.
[1999]80    typedef typename Notifier::ObserverBase Parent;
[822]81
82    /// The reference type of the map;
[987]83    typedef typename Container::reference Reference;
[822]84    /// The const reference type of the map;
[987]85    typedef typename Container::const_reference ConstReference;
[822]86
[1267]87
[1999]88    /// \brief Constructor to attach the new map into the notifier.
[1703]89    ///
[1999]90    /// It constructs a map and attachs it into the notifier.
[946]91    /// It adds all the items of the graph to the map.
[1999]92    VectorMap(const Graph& graph) {
93      Parent::attach(graph.getNotifier(Item()));
94      container.resize(Parent::getNotifier()->maxId() + 1);
[946]95    }
[822]96
[1703]97    /// \brief Constructor uses given value to initialize the map.
98    ///
99    /// It constructs a map uses a given value to initialize the map.
[946]100    /// It adds all the items of the graph to the map.
[1999]101    VectorMap(const Graph& graph, const Value& value) {
102      Parent::attach(graph.getNotifier(Item()));
103      container.resize(Parent::getNotifier()->maxId() + 1, value);
[946]104    }
[822]105
[1703]106    /// \brief Copy constructor
107    ///
108    /// Copy constructor.
[1999]109    VectorMap(const VectorMap& _copy) : Parent() {
[980]110      if (_copy.attached()) {
[1999]111        Parent::attach(*_copy.getNotifier());
[980]112        container = _copy.container;
[822]113      }
114    }
115
[2031]116    /// \brief Assign operator.
117    ///
118    /// This operator assigns for each item in the map the
119    /// value mapped to the same item in the copied map. 
120    /// The parameter map should be indiced with the same
121    /// itemset because this assign operator does not change
122    /// the container of the map.
123    VectorMap& operator=(const VectorMap& cmap) {
124      return operator=<VectorMap>(cmap);
125    }
[1669]126
127
[2031]128    /// \brief Template assign operator.
129    ///
130    /// The given parameter should be conform to the ReadMap
131    /// concecpt and could be indiced by the current item set of
132    /// the NodeMap. In this case the value for each item
133    /// is assigned by the value of the given ReadMap.
134    template <typename CMap>
135    VectorMap& operator=(const CMap& cmap) {
[2260]136      checkConcept<concepts::ReadMap<Key, _Value>, CMap>();
[2031]137      const typename Parent::Notifier* notifier = Parent::getNotifier();
138      Item it;
139      for (notifier->first(it); it != INVALID; notifier->next(it)) {
140        set(it, cmap[it]);
141      }
142      return *this;
143    }
144   
[1669]145  public:
146
[1703]147    /// \brief The subcript operator.
148    ///
[946]149    /// The subscript operator. The map can be subscripted by the
[1703]150    /// actual items of the graph.     
[987]151    Reference operator[](const Key& key) {
[1999]152      return container[Parent::getNotifier()->id(key)];
[822]153    }
154               
[1703]155    /// \brief The const subcript operator.
156    ///
[946]157    /// The const subscript operator. The map can be subscripted by the
158    /// actual items of the graph.
[987]159    ConstReference operator[](const Key& key) const {
[1999]160      return container[Parent::getNotifier()->id(key)];
[822]161    }
162
[937]163
[1703]164    /// \brief The setter function of the map.
165    ///
[946]166    /// It the same as operator[](key) = value expression.
[987]167    void set(const Key& key, const Value& value) {
[946]168      (*this)[key] = value;
[822]169    }
[946]170
[1669]171  protected:
172
173    /// \brief Adds a new key to the map.
174    ///         
[1999]175    /// It adds a new key to the map. It called by the observer notifier
[1703]176    /// and it overrides the add() member function of the observer base.     
177    virtual void add(const Key& key) {
[1999]178      int id = Parent::getNotifier()->id(key);
[822]179      if (id >= (int)container.size()) {
180        container.resize(id + 1);
181      }
182    }
[937]183
[1832]184    /// \brief Adds more new keys to the map.
185    ///         
[1999]186    /// It adds more new keys to the map. It called by the observer notifier
[1832]187    /// and it overrides the add() member function of the observer base.     
188    virtual void add(const std::vector<Key>& keys) {
[1999]189      int max = container.size() - 1;
[1832]190      for (int i = 0; i < (int)keys.size(); ++i) {
[1999]191        int id = Parent::getNotifier()->id(keys[i]);
192        if (id >= max) {
193          max = id;
194        }
[1832]195      }
[1999]196      container.resize(max + 1);
[1832]197    }
198
[1703]199    /// \brief Erase a key from the map.
200    ///
[1999]201    /// Erase a key from the map. It called by the observer notifier
[946]202    /// and it overrides the erase() member function of the observer base.     
[1999]203    virtual void erase(const Key& key) {
204      container[Parent::getNotifier()->id(key)] = Value();
205    }
[822]206
[1832]207    /// \brief Erase more keys from the map.
208    ///
[1999]209    /// Erase more keys from the map. It called by the observer notifier
[1832]210    /// and it overrides the erase() member function of the observer base.     
[1999]211    virtual void erase(const std::vector<Key>& keys) {
212      for (int i = 0; i < (int)keys.size(); ++i) {
213        container[Parent::getNotifier()->id(keys[i])] = Value();
214      }
215    }
[1832]216   
[1703]217    /// \brief Buildes the map.
218    ///
[1999]219    /// It buildes the map. It called by the observer notifier
[946]220    /// and it overrides the build() member function of the observer base.
[1703]221    virtual void build() {
[1999]222      int size = Parent::getNotifier()->maxId() + 1;
223      container.reserve(size);
224      container.resize(size);
[946]225    }
[937]226
[1703]227    /// \brief Clear the map.
228    ///
[1999]229    /// It erase all items from the map. It called by the observer notifier
[946]230    /// and it overrides the clear() member function of the observer base.     
[1703]231    virtual void clear() {
[822]232      container.clear();
233    }
[1267]234   
[822]235  private:
236               
237    Container container;
238
[946]239  };
240
[822]241}
242
243#endif
Note: See TracBrowser for help on using the repository browser.