lemon/bits/map_extender.h
author Alpar Juttner <alpar@cs.elte.hu>
Fri, 26 Feb 2010 17:08:30 +0100
branch1.1
changeset 913 2f9d9bcc1867
parent 867 994c7df296c9
parent 912 37f440367057
child 1064 40bbb450143e
permissions -rw-r--r--
Merge 4 backouts (#50, #312)
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@463
     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@664
    39
    typedef _Map Parent;
kpeter@664
    40
    typedef typename Parent::GraphType GraphType;
kpeter@664
    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@627
    49
    typedef typename Parent::Reference Reference;
kpeter@627
    50
    typedef typename Parent::ConstReference ConstReference;
deba@57
    51
deba@57
    52
    class MapIt;
deba@57
    53
    class ConstMapIt;
deba@57
    54
deba@57
    55
    friend class MapIt;
deba@57
    56
    friend class ConstMapIt;
deba@57
    57
deba@57
    58
  public:
deba@57
    59
kpeter@664
    60
    MapExtender(const GraphType& graph)
deba@57
    61
      : Parent(graph) {}
deba@57
    62
kpeter@664
    63
    MapExtender(const GraphType& graph, const Value& value)
deba@57
    64
      : Parent(graph, value) {}
deba@57
    65
kpeter@263
    66
  private:
deba@57
    67
    MapExtender& operator=(const MapExtender& cmap) {
deba@57
    68
      return operator=<MapExtender>(cmap);
deba@57
    69
    }
deba@57
    70
deba@57
    71
    template <typename CMap>
deba@57
    72
    MapExtender& operator=(const CMap& cmap) {
deba@57
    73
      Parent::operator=(cmap);
deba@57
    74
      return *this;
alpar@209
    75
    }
deba@57
    76
kpeter@263
    77
  public:
deba@57
    78
    class MapIt : public Item {
kpeter@664
    79
      typedef Item Parent;
kpeter@664
    80
deba@57
    81
    public:
alpar@209
    82
deba@57
    83
      typedef typename Map::Value Value;
alpar@209
    84
kpeter@865
    85
      MapIt() : map(NULL) {}
deba@57
    86
kpeter@865
    87
      MapIt(Invalid i) : Parent(i), map(NULL) {}
deba@57
    88
kpeter@865
    89
      explicit MapIt(Map& _map) : map(&_map) {
kpeter@865
    90
        map->notifier()->first(*this);
deba@57
    91
      }
deba@57
    92
alpar@209
    93
      MapIt(const Map& _map, const Item& item)
kpeter@865
    94
        : Parent(item), map(&_map) {}
deba@57
    95
alpar@209
    96
      MapIt& operator++() {
kpeter@865
    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 {
kpeter@865
   102
        return (*map)[*this];
deba@57
   103
      }
deba@57
   104
deba@57
   105
      typename MapTraits<Map>::ReturnValue operator*() {
kpeter@865
   106
        return (*map)[*this];
deba@57
   107
      }
alpar@209
   108
deba@57
   109
      void set(const Value& value) {
kpeter@865
   110
        map->set(*this, value);
deba@57
   111
      }
alpar@209
   112
deba@57
   113
    protected:
kpeter@865
   114
      Map* map;
alpar@209
   115
deba@57
   116
    };
deba@57
   117
deba@57
   118
    class ConstMapIt : public Item {
kpeter@664
   119
      typedef Item Parent;
kpeter@664
   120
deba@57
   121
    public:
deba@57
   122
deba@57
   123
      typedef typename Map::Value Value;
alpar@209
   124
kpeter@865
   125
      ConstMapIt() : map(NULL) {}
deba@57
   126
kpeter@865
   127
      ConstMapIt(Invalid i) : Parent(i), map(NULL) {}
deba@57
   128
kpeter@865
   129
      explicit ConstMapIt(Map& _map) : map(&_map) {
kpeter@865
   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++() {
kpeter@865
   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:
kpeter@865
   146
      const Map* map;
deba@57
   147
    };
deba@57
   148
deba@57
   149
    class ItemIt : public Item {
kpeter@664
   150
      typedef Item Parent;
kpeter@664
   151
deba@57
   152
    public:
kpeter@865
   153
      ItemIt() : map(NULL) {}
alpar@209
   154
deba@57
   155
kpeter@865
   156
      ItemIt(Invalid i) : Parent(i), map(NULL) {}
deba@57
   157
kpeter@865
   158
      explicit ItemIt(Map& _map) : map(&_map) {
kpeter@865
   159
        map->notifier()->first(*this);
deba@57
   160
      }
deba@57
   161
alpar@209
   162
      ItemIt(const Map& _map, const Item& item)
kpeter@865
   163
        : Parent(item), map(&_map) {}
deba@57
   164
alpar@209
   165
      ItemIt& operator++() {
kpeter@865
   166
        map->notifier()->next(*this);
alpar@209
   167
        return *this;
deba@57
   168
      }
deba@57
   169
deba@57
   170
    protected:
kpeter@865
   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 {
kpeter@664
   181
    typedef _Map Parent;
kpeter@664
   182
    typedef _Graph GraphType;
kpeter@664
   183
deba@57
   184
  public:
deba@57
   185
deba@57
   186
    typedef SubMapExtender Map;
deba@57
   187
    typedef typename Parent::Key Item;
deba@57
   188
deba@57
   189
    typedef typename Parent::Key Key;
deba@57
   190
    typedef typename Parent::Value Value;
kpeter@627
   191
    typedef typename Parent::Reference Reference;
kpeter@627
   192
    typedef typename Parent::ConstReference ConstReference;
deba@57
   193
deba@57
   194
    class MapIt;
deba@57
   195
    class ConstMapIt;
deba@57
   196
deba@57
   197
    friend class MapIt;
deba@57
   198
    friend class ConstMapIt;
deba@57
   199
deba@57
   200
  public:
deba@57
   201
kpeter@664
   202
    SubMapExtender(const GraphType& _graph)
deba@57
   203
      : Parent(_graph), graph(_graph) {}
deba@57
   204
kpeter@664
   205
    SubMapExtender(const GraphType& _graph, const Value& _value)
deba@57
   206
      : Parent(_graph, _value), graph(_graph) {}
deba@57
   207
kpeter@263
   208
  private:
deba@57
   209
    SubMapExtender& operator=(const SubMapExtender& cmap) {
deba@57
   210
      return operator=<MapExtender>(cmap);
deba@57
   211
    }
deba@57
   212
deba@57
   213
    template <typename CMap>
deba@57
   214
    SubMapExtender& operator=(const CMap& cmap) {
deba@57
   215
      checkConcept<concepts::ReadMap<Key, Value>, CMap>();
deba@57
   216
      Item it;
deba@57
   217
      for (graph.first(it); it != INVALID; graph.next(it)) {
deba@57
   218
        Parent::set(it, cmap[it]);
deba@57
   219
      }
deba@57
   220
      return *this;
alpar@209
   221
    }
deba@57
   222
kpeter@263
   223
  public:
deba@57
   224
    class MapIt : public Item {
kpeter@664
   225
      typedef Item Parent;
kpeter@664
   226
deba@57
   227
    public:
deba@57
   228
      typedef typename Map::Value Value;
alpar@209
   229
kpeter@865
   230
      MapIt() : map(NULL) {}
deba@57
   231
kpeter@865
   232
      MapIt(Invalid i) : Parent(i), map(NULL) { }
deba@57
   233
kpeter@865
   234
      explicit MapIt(Map& _map) : map(&_map) {
kpeter@865
   235
        map->graph.first(*this);
deba@57
   236
      }
deba@57
   237
alpar@209
   238
      MapIt(const Map& _map, const Item& item)
kpeter@865
   239
        : Parent(item), map(&_map) {}
deba@57
   240
alpar@209
   241
      MapIt& operator++() {
kpeter@865
   242
        map->graph.next(*this);
alpar@209
   243
        return *this;
deba@57
   244
      }
alpar@209
   245
deba@57
   246
      typename MapTraits<Map>::ConstReturnValue operator*() const {
kpeter@865
   247
        return (*map)[*this];
deba@57
   248
      }
deba@57
   249
deba@57
   250
      typename MapTraits<Map>::ReturnValue operator*() {
kpeter@865
   251
        return (*map)[*this];
deba@57
   252
      }
alpar@209
   253
deba@57
   254
      void set(const Value& value) {
kpeter@865
   255
        map->set(*this, value);
deba@57
   256
      }
alpar@209
   257
deba@57
   258
    protected:
kpeter@865
   259
      Map* map;
alpar@209
   260
deba@57
   261
    };
deba@57
   262
deba@57
   263
    class ConstMapIt : public Item {
kpeter@664
   264
      typedef Item Parent;
kpeter@664
   265
deba@57
   266
    public:
deba@57
   267
deba@57
   268
      typedef typename Map::Value Value;
alpar@209
   269
kpeter@865
   270
      ConstMapIt() : map(NULL) {}
deba@57
   271
kpeter@865
   272
      ConstMapIt(Invalid i) : Parent(i), map(NULL) { }
deba@57
   273
kpeter@865
   274
      explicit ConstMapIt(Map& _map) : map(&_map) {
kpeter@865
   275
        map->graph.first(*this);
deba@57
   276
      }
deba@57
   277
alpar@209
   278
      ConstMapIt(const Map& _map, const Item& item)
kpeter@865
   279
        : Parent(item), map(&_map) {}
deba@57
   280
alpar@209
   281
      ConstMapIt& operator++() {
kpeter@865
   282
        map->graph.next(*this);
alpar@209
   283
        return *this;
deba@57
   284
      }
deba@57
   285
deba@57
   286
      typename MapTraits<Map>::ConstReturnValue operator*() const {
kpeter@865
   287
        return (*map)[*this];
deba@57
   288
      }
deba@57
   289
deba@57
   290
    protected:
kpeter@865
   291
      const Map* map;
deba@57
   292
    };
deba@57
   293
deba@57
   294
    class ItemIt : public Item {
kpeter@664
   295
      typedef Item Parent;
kpeter@664
   296
deba@57
   297
    public:
kpeter@865
   298
      ItemIt() : map(NULL) {}
alpar@209
   299
deba@57
   300
kpeter@865
   301
      ItemIt(Invalid i) : Parent(i), map(NULL) { }
deba@57
   302
kpeter@865
   303
      explicit ItemIt(Map& _map) : map(&_map) {
kpeter@865
   304
        map->graph.first(*this);
deba@57
   305
      }
deba@57
   306
alpar@209
   307
      ItemIt(const Map& _map, const Item& item)
kpeter@865
   308
        : Parent(item), map(&_map) {}
deba@57
   309
alpar@209
   310
      ItemIt& operator++() {
kpeter@865
   311
        map->graph.next(*this);
alpar@209
   312
        return *this;
deba@57
   313
      }
deba@57
   314
deba@57
   315
    protected:
kpeter@865
   316
      const Map* map;
alpar@209
   317
deba@57
   318
    };
alpar@209
   319
deba@57
   320
  private:
deba@57
   321
kpeter@664
   322
    const GraphType& graph;
alpar@209
   323
deba@57
   324
  };
deba@57
   325
deba@57
   326
}
deba@57
   327
deba@57
   328
#endif