lemon/bits/map_extender.h
author Peter Kovacs <kpeter@inf.elte.hu>
Mon, 01 Sep 2008 22:00:40 +0200
changeset 257 8d76a7bf9961
parent 107 31a2e6d28f61
child 263 be8a861d3bb7
permissions -rw-r--r--
Rename Def* to Set* in Bfs, Dfs, Dijkstra (ticket #134)
- DefXyzMap --> SetXyzMap
- DefHeap --> SetHeap
- DefStandardHeap --> SetStandardHeap
- DefOperationTraits --> SetOperationTraits
- DefProcessedMapToBeDefaultMap --> SetStandardProcessedMap
- Bug fix: SetStandardProcessedMap shouldn't be template
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@107
     5
 * Copyright (C) 2003-2008
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_MAP_EXTENDER_H
deba@57
    20
#define LEMON_BITS_MAP_EXTENDER_H
deba@57
    21
deba@57
    22
#include <iterator>
deba@57
    23
deba@57
    24
#include <lemon/bits/traits.h>
deba@57
    25
deba@57
    26
#include <lemon/concept_check.h>
deba@57
    27
#include <lemon/concepts/maps.h>
deba@57
    28
deba@57
    29
///\file
deba@57
    30
///\brief Extenders for iterable maps.
deba@57
    31
deba@57
    32
namespace lemon {
deba@57
    33
deba@57
    34
  /// \ingroup graphbits
alpar@209
    35
  ///
deba@57
    36
  /// \brief Extender for maps
deba@57
    37
  template <typename _Map>
deba@57
    38
  class MapExtender : public _Map {
deba@57
    39
  public:
deba@57
    40
deba@57
    41
    typedef _Map Parent;
deba@57
    42
    typedef MapExtender Map;
deba@57
    43
deba@57
    44
deba@57
    45
    typedef typename Parent::Graph Graph;
deba@57
    46
    typedef typename Parent::Key Item;
deba@57
    47
deba@57
    48
    typedef typename Parent::Key Key;
deba@57
    49
    typedef typename Parent::Value Value;
deba@57
    50
deba@57
    51
    class MapIt;
deba@57
    52
    class ConstMapIt;
deba@57
    53
deba@57
    54
    friend class MapIt;
deba@57
    55
    friend class ConstMapIt;
deba@57
    56
deba@57
    57
  public:
deba@57
    58
alpar@209
    59
    MapExtender(const Graph& graph)
deba@57
    60
      : Parent(graph) {}
deba@57
    61
alpar@209
    62
    MapExtender(const Graph& graph, const Value& value)
deba@57
    63
      : Parent(graph, value) {}
deba@57
    64
deba@57
    65
    MapExtender& operator=(const MapExtender& cmap) {
deba@57
    66
      return operator=<MapExtender>(cmap);
deba@57
    67
    }
deba@57
    68
deba@57
    69
    template <typename CMap>
deba@57
    70
    MapExtender& operator=(const CMap& cmap) {
deba@57
    71
      Parent::operator=(cmap);
deba@57
    72
      return *this;
alpar@209
    73
    }
deba@57
    74
deba@57
    75
    class MapIt : public Item {
deba@57
    76
    public:
alpar@209
    77
deba@57
    78
      typedef Item Parent;
deba@57
    79
      typedef typename Map::Value Value;
alpar@209
    80
deba@57
    81
      MapIt() {}
deba@57
    82
deba@57
    83
      MapIt(Invalid i) : Parent(i) { }
deba@57
    84
deba@57
    85
      explicit MapIt(Map& _map) : map(_map) {
deba@57
    86
        map.notifier()->first(*this);
deba@57
    87
      }
deba@57
    88
alpar@209
    89
      MapIt(const Map& _map, const Item& item)
alpar@209
    90
        : Parent(item), map(_map) {}
deba@57
    91
alpar@209
    92
      MapIt& operator++() {
alpar@209
    93
        map.notifier()->next(*this);
alpar@209
    94
        return *this;
deba@57
    95
      }
alpar@209
    96
deba@57
    97
      typename MapTraits<Map>::ConstReturnValue operator*() const {
alpar@209
    98
        return map[*this];
deba@57
    99
      }
deba@57
   100
deba@57
   101
      typename MapTraits<Map>::ReturnValue operator*() {
alpar@209
   102
        return map[*this];
deba@57
   103
      }
alpar@209
   104
deba@57
   105
      void set(const Value& value) {
alpar@209
   106
        map.set(*this, value);
deba@57
   107
      }
alpar@209
   108
deba@57
   109
    protected:
deba@57
   110
      Map& map;
alpar@209
   111
deba@57
   112
    };
deba@57
   113
deba@57
   114
    class ConstMapIt : public Item {
deba@57
   115
    public:
deba@57
   116
deba@57
   117
      typedef Item Parent;
deba@57
   118
deba@57
   119
      typedef typename Map::Value Value;
alpar@209
   120
deba@57
   121
      ConstMapIt() {}
deba@57
   122
deba@57
   123
      ConstMapIt(Invalid i) : Parent(i) { }
deba@57
   124
deba@57
   125
      explicit ConstMapIt(Map& _map) : map(_map) {
deba@57
   126
        map.notifier()->first(*this);
deba@57
   127
      }
deba@57
   128
alpar@209
   129
      ConstMapIt(const Map& _map, const Item& item)
alpar@209
   130
        : Parent(item), map(_map) {}
deba@57
   131
alpar@209
   132
      ConstMapIt& operator++() {
alpar@209
   133
        map.notifier()->next(*this);
alpar@209
   134
        return *this;
deba@57
   135
      }
deba@57
   136
deba@57
   137
      typename MapTraits<Map>::ConstReturnValue operator*() const {
alpar@209
   138
        return map[*this];
deba@57
   139
      }
deba@57
   140
deba@57
   141
    protected:
deba@57
   142
      const Map& map;
deba@57
   143
    };
deba@57
   144
deba@57
   145
    class ItemIt : public Item {
deba@57
   146
    public:
alpar@209
   147
deba@57
   148
      typedef Item Parent;
alpar@209
   149
deba@57
   150
      ItemIt() {}
deba@57
   151
deba@57
   152
      ItemIt(Invalid i) : Parent(i) { }
deba@57
   153
deba@57
   154
      explicit ItemIt(Map& _map) : map(_map) {
deba@57
   155
        map.notifier()->first(*this);
deba@57
   156
      }
deba@57
   157
alpar@209
   158
      ItemIt(const Map& _map, const Item& item)
alpar@209
   159
        : Parent(item), map(_map) {}
deba@57
   160
alpar@209
   161
      ItemIt& operator++() {
alpar@209
   162
        map.notifier()->next(*this);
alpar@209
   163
        return *this;
deba@57
   164
      }
deba@57
   165
deba@57
   166
    protected:
deba@57
   167
      const Map& map;
alpar@209
   168
deba@57
   169
    };
deba@57
   170
  };
deba@57
   171
deba@57
   172
  /// \ingroup graphbits
alpar@209
   173
  ///
deba@57
   174
  /// \brief Extender for maps which use a subset of the items.
deba@57
   175
  template <typename _Graph, typename _Map>
deba@57
   176
  class SubMapExtender : public _Map {
deba@57
   177
  public:
deba@57
   178
deba@57
   179
    typedef _Map Parent;
deba@57
   180
    typedef SubMapExtender Map;
deba@57
   181
deba@57
   182
    typedef _Graph Graph;
deba@57
   183
deba@57
   184
    typedef typename Parent::Key Item;
deba@57
   185
deba@57
   186
    typedef typename Parent::Key Key;
deba@57
   187
    typedef typename Parent::Value Value;
deba@57
   188
deba@57
   189
    class MapIt;
deba@57
   190
    class ConstMapIt;
deba@57
   191
deba@57
   192
    friend class MapIt;
deba@57
   193
    friend class ConstMapIt;
deba@57
   194
deba@57
   195
  public:
deba@57
   196
alpar@209
   197
    SubMapExtender(const Graph& _graph)
deba@57
   198
      : Parent(_graph), graph(_graph) {}
deba@57
   199
alpar@209
   200
    SubMapExtender(const Graph& _graph, const Value& _value)
deba@57
   201
      : Parent(_graph, _value), graph(_graph) {}
deba@57
   202
deba@57
   203
    SubMapExtender& operator=(const SubMapExtender& cmap) {
deba@57
   204
      return operator=<MapExtender>(cmap);
deba@57
   205
    }
deba@57
   206
deba@57
   207
    template <typename CMap>
deba@57
   208
    SubMapExtender& operator=(const CMap& cmap) {
deba@57
   209
      checkConcept<concepts::ReadMap<Key, Value>, CMap>();
deba@57
   210
      Item it;
deba@57
   211
      for (graph.first(it); it != INVALID; graph.next(it)) {
deba@57
   212
        Parent::set(it, cmap[it]);
deba@57
   213
      }
deba@57
   214
      return *this;
alpar@209
   215
    }
deba@57
   216
deba@57
   217
    class MapIt : public Item {
deba@57
   218
    public:
alpar@209
   219
deba@57
   220
      typedef Item Parent;
deba@57
   221
      typedef typename Map::Value Value;
alpar@209
   222
deba@57
   223
      MapIt() {}
deba@57
   224
deba@57
   225
      MapIt(Invalid i) : Parent(i) { }
deba@57
   226
deba@57
   227
      explicit MapIt(Map& _map) : map(_map) {
deba@57
   228
        map.graph.first(*this);
deba@57
   229
      }
deba@57
   230
alpar@209
   231
      MapIt(const Map& _map, const Item& item)
alpar@209
   232
        : Parent(item), map(_map) {}
deba@57
   233
alpar@209
   234
      MapIt& operator++() {
alpar@209
   235
        map.graph.next(*this);
alpar@209
   236
        return *this;
deba@57
   237
      }
alpar@209
   238
deba@57
   239
      typename MapTraits<Map>::ConstReturnValue operator*() const {
alpar@209
   240
        return map[*this];
deba@57
   241
      }
deba@57
   242
deba@57
   243
      typename MapTraits<Map>::ReturnValue operator*() {
alpar@209
   244
        return map[*this];
deba@57
   245
      }
alpar@209
   246
deba@57
   247
      void set(const Value& value) {
alpar@209
   248
        map.set(*this, value);
deba@57
   249
      }
alpar@209
   250
deba@57
   251
    protected:
deba@57
   252
      Map& map;
alpar@209
   253
deba@57
   254
    };
deba@57
   255
deba@57
   256
    class ConstMapIt : public Item {
deba@57
   257
    public:
deba@57
   258
deba@57
   259
      typedef Item Parent;
deba@57
   260
deba@57
   261
      typedef typename Map::Value Value;
alpar@209
   262
deba@57
   263
      ConstMapIt() {}
deba@57
   264
deba@57
   265
      ConstMapIt(Invalid i) : Parent(i) { }
deba@57
   266
deba@57
   267
      explicit ConstMapIt(Map& _map) : map(_map) {
deba@57
   268
        map.graph.first(*this);
deba@57
   269
      }
deba@57
   270
alpar@209
   271
      ConstMapIt(const Map& _map, const Item& item)
alpar@209
   272
        : Parent(item), map(_map) {}
deba@57
   273
alpar@209
   274
      ConstMapIt& operator++() {
alpar@209
   275
        map.graph.next(*this);
alpar@209
   276
        return *this;
deba@57
   277
      }
deba@57
   278
deba@57
   279
      typename MapTraits<Map>::ConstReturnValue operator*() const {
alpar@209
   280
        return map[*this];
deba@57
   281
      }
deba@57
   282
deba@57
   283
    protected:
deba@57
   284
      const Map& map;
deba@57
   285
    };
deba@57
   286
deba@57
   287
    class ItemIt : public Item {
deba@57
   288
    public:
alpar@209
   289
deba@57
   290
      typedef Item Parent;
alpar@209
   291
deba@57
   292
      ItemIt() {}
deba@57
   293
deba@57
   294
      ItemIt(Invalid i) : Parent(i) { }
deba@57
   295
deba@57
   296
      explicit ItemIt(Map& _map) : map(_map) {
deba@57
   297
        map.graph.first(*this);
deba@57
   298
      }
deba@57
   299
alpar@209
   300
      ItemIt(const Map& _map, const Item& item)
alpar@209
   301
        : Parent(item), map(_map) {}
deba@57
   302
alpar@209
   303
      ItemIt& operator++() {
alpar@209
   304
        map.graph.next(*this);
alpar@209
   305
        return *this;
deba@57
   306
      }
deba@57
   307
deba@57
   308
    protected:
deba@57
   309
      const Map& map;
alpar@209
   310
deba@57
   311
    };
alpar@209
   312
deba@57
   313
  private:
deba@57
   314
deba@57
   315
    const Graph& graph;
alpar@209
   316
deba@57
   317
  };
deba@57
   318
deba@57
   319
}
deba@57
   320
deba@57
   321
#endif