lemon/bits/map_extender.h
author deba
Tue, 30 Oct 2007 20:44:53 +0000
changeset 2506 216c6bd5c18c
parent 2384 805c5a2a36dd
child 2553 bfced05fa852
permissions -rw-r--r--
Change to new union-find interface
deba@1810
     1
/* -*- C++ -*-
deba@1810
     2
 *
alpar@1956
     3
 * This file is a part of LEMON, a generic C++ optimization library
alpar@1956
     4
 *
alpar@2391
     5
 * Copyright (C) 2003-2007
alpar@1956
     6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
alpar@1956
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
deba@1810
     8
 *
deba@1810
     9
 * Permission to use, modify and distribute this software is granted
deba@1810
    10
 * provided that this copyright notice appears in all copies. For
deba@1810
    11
 * precise terms see the accompanying LICENSE file.
deba@1810
    12
 *
deba@1810
    13
 * This software is provided "AS IS" with no warranty of any kind,
deba@1810
    14
 * express or implied, and with no claim as to its suitability for any
deba@1810
    15
 * purpose.
deba@1810
    16
 *
deba@1810
    17
 */
deba@1810
    18
deba@1810
    19
#ifndef LEMON_BITS_MAP_EXTENDER_H
deba@1810
    20
#define LEMON_BITS_MAP_EXTENDER_H
deba@1810
    21
deba@1810
    22
#include <iterator>
deba@1810
    23
deba@1993
    24
#include <lemon/bits/traits.h>
deba@1810
    25
deba@2031
    26
#include <lemon/concept_check.h>
alpar@2260
    27
#include <lemon/concepts/maps.h>
deba@2031
    28
deba@1810
    29
///\file
deba@1810
    30
///\brief Extenders for iterable maps.
deba@1810
    31
deba@1810
    32
namespace lemon {
deba@1810
    33
deba@1996
    34
  /// \ingroup graphbits
deba@1999
    35
  /// 
deba@1999
    36
  /// \brief Extender for maps
deba@1810
    37
  template <typename _Map>
deba@1999
    38
  class MapExtender : public _Map {
deba@1810
    39
  public:
deba@1810
    40
deba@1810
    41
    typedef _Map Parent;
deba@1999
    42
    typedef MapExtender Map;
deba@1810
    43
deba@1810
    44
alpar@1854
    45
    typedef typename Parent::Graph Graph;
alpar@1854
    46
    typedef typename Parent::Key Item;
deba@1810
    47
alpar@1854
    48
    typedef typename Parent::Key Key;
alpar@1854
    49
    typedef typename Parent::Value Value;
deba@1810
    50
deba@1810
    51
    class MapIt;
deba@1810
    52
    class ConstMapIt;
deba@1810
    53
deba@1810
    54
    friend class MapIt;
deba@1810
    55
    friend class ConstMapIt;
deba@1810
    56
deba@1810
    57
  public:
deba@1810
    58
deba@1999
    59
    MapExtender(const Graph& graph) 
deba@1999
    60
      : Parent(graph) {}
deba@1810
    61
deba@1999
    62
    MapExtender(const Graph& graph, const Value& value) 
deba@1810
    63
      : Parent(graph, value) {}
deba@1810
    64
deba@2031
    65
    MapExtender& operator=(const MapExtender& cmap) {
deba@2031
    66
      return operator=<MapExtender>(cmap);
deba@2031
    67
    }
deba@2031
    68
deba@2031
    69
    template <typename CMap>
deba@2031
    70
    MapExtender& operator=(const CMap& cmap) {
deba@2031
    71
      Parent::operator=(cmap);
deba@2031
    72
      return *this;
deba@2031
    73
    } 
deba@1810
    74
deba@1999
    75
    class MapIt : public Item {
deba@1810
    76
    public:
deba@1810
    77
      
deba@1999
    78
      typedef Item Parent;
deba@1810
    79
      typedef typename Map::Value Value;
deba@1810
    80
      
deba@1999
    81
      MapIt() {}
deba@1999
    82
deba@1999
    83
      MapIt(Invalid i) : Parent(i) { }
deba@1999
    84
deba@1999
    85
      explicit MapIt(Map& _map) : map(_map) {
deba@2384
    86
        map.notifier()->first(*this);
deba@1999
    87
      }
deba@1999
    88
deba@1999
    89
      MapIt(const Map& _map, const Item& item) 
deba@1999
    90
	: Parent(item), map(_map) {}
deba@1999
    91
deba@1999
    92
      MapIt& operator++() { 
deba@2384
    93
	map.notifier()->next(*this);
deba@1999
    94
	return *this; 
deba@1999
    95
      }
deba@1810
    96
      
deba@1810
    97
      typename MapTraits<Map>::ConstReturnValue operator*() const {
deba@1810
    98
	return map[*this];
deba@1810
    99
      }
deba@1810
   100
deba@1810
   101
      typename MapTraits<Map>::ReturnValue operator*() {
deba@1810
   102
	return map[*this];
deba@1810
   103
      }
deba@1810
   104
      
deba@1810
   105
      void set(const Value& value) {
deba@1810
   106
	map.set(*this, value);
deba@1810
   107
      }
deba@1810
   108
      
deba@1810
   109
    protected:
deba@1810
   110
      Map& map;
deba@1810
   111
      
deba@1810
   112
    };
deba@1810
   113
deba@1999
   114
    class ConstMapIt : public Item {
deba@1810
   115
    public:
deba@1810
   116
deba@1999
   117
      typedef Item Parent;
deba@1810
   118
deba@1810
   119
      typedef typename Map::Value Value;
deba@1999
   120
      
deba@1999
   121
      ConstMapIt() {}
deba@1810
   122
deba@1999
   123
      ConstMapIt(Invalid i) : Parent(i) { }
deba@1999
   124
deba@1999
   125
      explicit ConstMapIt(Map& _map) : map(_map) {
deba@2384
   126
        map.notifier()->first(*this);
deba@1999
   127
      }
deba@1999
   128
deba@1999
   129
      ConstMapIt(const Map& _map, const Item& item) 
deba@1999
   130
	: Parent(item), map(_map) {}
deba@1999
   131
deba@1999
   132
      ConstMapIt& operator++() { 
deba@2384
   133
	map.notifier()->next(*this);
deba@1999
   134
	return *this; 
deba@1999
   135
      }
deba@1810
   136
deba@1810
   137
      typename MapTraits<Map>::ConstReturnValue operator*() const {
deba@1810
   138
	return map[*this];
deba@1810
   139
      }
deba@1999
   140
deba@1810
   141
    protected:
deba@1810
   142
      const Map& map;
deba@1810
   143
    };
deba@1810
   144
deba@2031
   145
    class ItemIt : public Item {
deba@1810
   146
    public:
deba@1810
   147
      
deba@1999
   148
      typedef Item Parent;
deba@1999
   149
      
deba@1999
   150
      ItemIt() {}
deba@1810
   151
deba@1999
   152
      ItemIt(Invalid i) : Parent(i) { }
deba@1999
   153
deba@1999
   154
      explicit ItemIt(Map& _map) : map(_map) {
deba@2384
   155
        map.notifier()->first(*this);
deba@1999
   156
      }
deba@1999
   157
deba@1999
   158
      ItemIt(const Map& _map, const Item& item) 
deba@1999
   159
	: Parent(item), map(_map) {}
deba@1999
   160
deba@1999
   161
      ItemIt& operator++() { 
deba@2384
   162
	map.notifier()->next(*this);
deba@1999
   163
	return *this; 
deba@1999
   164
      }
deba@1999
   165
deba@1999
   166
    protected:
deba@1999
   167
      const Map& map;
deba@1810
   168
      
deba@1810
   169
    };
deba@1810
   170
  };
deba@1810
   171
deba@2031
   172
  /// \ingroup graphbits
deba@2031
   173
  /// 
deba@2031
   174
  /// \brief Extender for maps which use a subset of the items.
deba@2031
   175
  template <typename _Graph, typename _Map>
deba@2031
   176
  class SubMapExtender : public _Map {
deba@2031
   177
  public:
deba@2031
   178
deba@2031
   179
    typedef _Map Parent;
deba@2031
   180
    typedef SubMapExtender Map;
deba@2031
   181
deba@2031
   182
    typedef _Graph Graph;
deba@2031
   183
deba@2031
   184
    typedef typename Parent::Key Item;
deba@2031
   185
deba@2031
   186
    typedef typename Parent::Key Key;
deba@2031
   187
    typedef typename Parent::Value Value;
deba@2031
   188
deba@2031
   189
    class MapIt;
deba@2031
   190
    class ConstMapIt;
deba@2031
   191
deba@2031
   192
    friend class MapIt;
deba@2031
   193
    friend class ConstMapIt;
deba@2031
   194
deba@2031
   195
  public:
deba@2031
   196
deba@2031
   197
    SubMapExtender(const Graph& _graph) 
deba@2031
   198
      : Parent(_graph), graph(_graph) {}
deba@2031
   199
deba@2031
   200
    SubMapExtender(const Graph& _graph, const Value& _value) 
deba@2031
   201
      : Parent(_graph, _value), graph(_graph) {}
deba@2031
   202
deba@2031
   203
    SubMapExtender& operator=(const SubMapExtender& cmap) {
deba@2031
   204
      return operator=<MapExtender>(cmap);
deba@2031
   205
    }
deba@2031
   206
deba@2031
   207
    template <typename CMap>
deba@2031
   208
    SubMapExtender& operator=(const CMap& cmap) {
alpar@2260
   209
      checkConcept<concepts::ReadMap<Key, Value>, CMap>();
deba@2031
   210
      Item it;
deba@2031
   211
      for (graph.first(it); it != INVALID; graph.next(it)) {
deba@2031
   212
        Parent::set(it, cmap[it]);
deba@2031
   213
      }
deba@2031
   214
      return *this;
deba@2031
   215
    } 
deba@2031
   216
deba@2031
   217
    class MapIt : public Item {
deba@2031
   218
    public:
deba@2031
   219
      
deba@2031
   220
      typedef Item Parent;
deba@2031
   221
      typedef typename Map::Value Value;
deba@2031
   222
      
deba@2031
   223
      MapIt() {}
deba@2031
   224
deba@2031
   225
      MapIt(Invalid i) : Parent(i) { }
deba@2031
   226
deba@2031
   227
      explicit MapIt(Map& _map) : map(_map) {
deba@2031
   228
        map.graph.first(*this);
deba@2031
   229
      }
deba@2031
   230
deba@2031
   231
      MapIt(const Map& _map, const Item& item) 
deba@2031
   232
	: Parent(item), map(_map) {}
deba@2031
   233
deba@2031
   234
      MapIt& operator++() { 
deba@2031
   235
	map.graph.next(*this);
deba@2031
   236
	return *this; 
deba@2031
   237
      }
deba@2031
   238
      
deba@2031
   239
      typename MapTraits<Map>::ConstReturnValue operator*() const {
deba@2031
   240
	return map[*this];
deba@2031
   241
      }
deba@2031
   242
deba@2031
   243
      typename MapTraits<Map>::ReturnValue operator*() {
deba@2031
   244
	return map[*this];
deba@2031
   245
      }
deba@2031
   246
      
deba@2031
   247
      void set(const Value& value) {
deba@2031
   248
	map.set(*this, value);
deba@2031
   249
      }
deba@2031
   250
      
deba@2031
   251
    protected:
deba@2031
   252
      Map& map;
deba@2031
   253
      
deba@2031
   254
    };
deba@2031
   255
deba@2031
   256
    class ConstMapIt : public Item {
deba@2031
   257
    public:
deba@2031
   258
deba@2031
   259
      typedef Item Parent;
deba@2031
   260
deba@2031
   261
      typedef typename Map::Value Value;
deba@2031
   262
      
deba@2031
   263
      ConstMapIt() {}
deba@2031
   264
deba@2031
   265
      ConstMapIt(Invalid i) : Parent(i) { }
deba@2031
   266
deba@2031
   267
      explicit ConstMapIt(Map& _map) : map(_map) {
deba@2031
   268
        map.graph.first(*this);
deba@2031
   269
      }
deba@2031
   270
deba@2031
   271
      ConstMapIt(const Map& _map, const Item& item) 
deba@2031
   272
	: Parent(item), map(_map) {}
deba@2031
   273
deba@2031
   274
      ConstMapIt& operator++() { 
deba@2031
   275
	map.graph.next(*this);
deba@2031
   276
	return *this; 
deba@2031
   277
      }
deba@2031
   278
deba@2031
   279
      typename MapTraits<Map>::ConstReturnValue operator*() const {
deba@2031
   280
	return map[*this];
deba@2031
   281
      }
deba@2031
   282
deba@2031
   283
    protected:
deba@2031
   284
      const Map& map;
deba@2031
   285
    };
deba@2031
   286
deba@2031
   287
    class ItemIt : public Item {
deba@2031
   288
    public:
deba@2031
   289
      
deba@2031
   290
      typedef Item Parent;
deba@2031
   291
      
deba@2031
   292
      ItemIt() {}
deba@2031
   293
deba@2031
   294
      ItemIt(Invalid i) : Parent(i) { }
deba@2031
   295
deba@2031
   296
      explicit ItemIt(Map& _map) : map(_map) {
deba@2031
   297
        map.graph.first(*this);
deba@2031
   298
      }
deba@2031
   299
deba@2031
   300
      ItemIt(const Map& _map, const Item& item) 
deba@2031
   301
	: Parent(item), map(_map) {}
deba@2031
   302
deba@2031
   303
      ItemIt& operator++() { 
deba@2031
   304
	map.graph.next(*this);
deba@2031
   305
	return *this; 
deba@2031
   306
      }
deba@2031
   307
deba@2031
   308
    protected:
deba@2031
   309
      const Map& map;
deba@2031
   310
      
deba@2031
   311
    };
deba@2031
   312
    
deba@2031
   313
  private:
deba@2031
   314
deba@2031
   315
    const Graph& graph;
deba@2031
   316
    
deba@2031
   317
  };
deba@2031
   318
deba@1810
   319
}
deba@1810
   320
deba@1810
   321
#endif