COIN-OR::LEMON - Graph Library

source: lemon-1.2/lemon/bits/vector_map.h @ 503:41bdb4d6c8c3

Last change on this file since 503:41bdb4d6c8c3 was 314:2cc60866a0c9, checked in by Peter Kovacs <kpeter@…>, 15 years ago

Doc reorganization + improvements

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