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