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