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