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