lemon/bits/map_extender.h
author Peter Kovacs <kpeter@inf.elte.hu>
Sun, 26 Apr 2009 16:44:53 +0200
changeset 625 029a48052c67
parent 580 2313edd0db0b
child 718 703ebf476a1d
permissions -rw-r--r--
Modify the interface of MinCostArborescence + improvements (#267)

- Rename arborescenceValue() to arborescenceCost().
- Rename DefXyz template named paramaters to SetXyz.
- Rearrange public functions (for better doc).
- Doc improvements.
- Extend the test file with interface checking.
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
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@617
    60
    MapExtender(const GraphType& graph)
deba@57
    61
      : Parent(graph) {}
deba@57
    62
kpeter@617
    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@617
    79
      typedef Item Parent;
kpeter@617
    80
deba@57
    81
    public:
alpar@209
    82
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 {
kpeter@617
   119
      typedef Item Parent;
kpeter@617
   120
deba@57
   121
    public:
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 {
kpeter@617
   150
      typedef Item Parent;
kpeter@617
   151
deba@57
   152
    public:
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 {
kpeter@617
   181
    typedef _Map Parent;
kpeter@617
   182
    typedef _Graph GraphType;
kpeter@617
   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@580
   191
    typedef typename Parent::Reference Reference;
kpeter@580
   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@617
   202
    SubMapExtender(const GraphType& _graph)
deba@57
   203
      : Parent(_graph), graph(_graph) {}
deba@57
   204
kpeter@617
   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@617
   225
      typedef Item Parent;
kpeter@617
   226
deba@57
   227
    public:
deba@57
   228
      typedef typename Map::Value Value;
alpar@209
   229
deba@57
   230
      MapIt() {}
deba@57
   231
deba@57
   232
      MapIt(Invalid i) : Parent(i) { }
deba@57
   233
deba@57
   234
      explicit MapIt(Map& _map) : map(_map) {
deba@57
   235
        map.graph.first(*this);
deba@57
   236
      }
deba@57
   237
alpar@209
   238
      MapIt(const Map& _map, const Item& item)
alpar@209
   239
        : Parent(item), map(_map) {}
deba@57
   240
alpar@209
   241
      MapIt& operator++() {
alpar@209
   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 {
alpar@209
   247
        return map[*this];
deba@57
   248
      }
deba@57
   249
deba@57
   250
      typename MapTraits<Map>::ReturnValue operator*() {
alpar@209
   251
        return map[*this];
deba@57
   252
      }
alpar@209
   253
deba@57
   254
      void set(const Value& value) {
alpar@209
   255
        map.set(*this, value);
deba@57
   256
      }
alpar@209
   257
deba@57
   258
    protected:
deba@57
   259
      Map& map;
alpar@209
   260
deba@57
   261
    };
deba@57
   262
deba@57
   263
    class ConstMapIt : public Item {
kpeter@617
   264
      typedef Item Parent;
kpeter@617
   265
deba@57
   266
    public:
deba@57
   267
deba@57
   268
      typedef typename Map::Value Value;
alpar@209
   269
deba@57
   270
      ConstMapIt() {}
deba@57
   271
deba@57
   272
      ConstMapIt(Invalid i) : Parent(i) { }
deba@57
   273
deba@57
   274
      explicit ConstMapIt(Map& _map) : map(_map) {
deba@57
   275
        map.graph.first(*this);
deba@57
   276
      }
deba@57
   277
alpar@209
   278
      ConstMapIt(const Map& _map, const Item& item)
alpar@209
   279
        : Parent(item), map(_map) {}
deba@57
   280
alpar@209
   281
      ConstMapIt& operator++() {
alpar@209
   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 {
alpar@209
   287
        return map[*this];
deba@57
   288
      }
deba@57
   289
deba@57
   290
    protected:
deba@57
   291
      const Map& map;
deba@57
   292
    };
deba@57
   293
deba@57
   294
    class ItemIt : public Item {
kpeter@617
   295
      typedef Item Parent;
kpeter@617
   296
deba@57
   297
    public:
alpar@209
   298
deba@57
   299
      ItemIt() {}
deba@57
   300
deba@57
   301
      ItemIt(Invalid i) : Parent(i) { }
deba@57
   302
deba@57
   303
      explicit ItemIt(Map& _map) : map(_map) {
deba@57
   304
        map.graph.first(*this);
deba@57
   305
      }
deba@57
   306
alpar@209
   307
      ItemIt(const Map& _map, const Item& item)
alpar@209
   308
        : Parent(item), map(_map) {}
deba@57
   309
alpar@209
   310
      ItemIt& operator++() {
alpar@209
   311
        map.graph.next(*this);
alpar@209
   312
        return *this;
deba@57
   313
      }
deba@57
   314
deba@57
   315
    protected:
deba@57
   316
      const Map& map;
alpar@209
   317
deba@57
   318
    };
alpar@209
   319
deba@57
   320
  private:
deba@57
   321
kpeter@617
   322
    const GraphType& graph;
alpar@209
   323
deba@57
   324
  };
deba@57
   325
deba@57
   326
}
deba@57
   327
deba@57
   328
#endif