lemon/bits/debug_map.h
author deba
Sat, 17 Nov 2007 20:58:11 +0000
changeset 2514 57143c09dc20
parent 2386 81b47fc5c444
child 2553 bfced05fa852
permissions -rw-r--r--
Redesign the maximum flow algorithms

Redesigned interface
Preflow changed to use elevator
Edmonds-Karp does not use the ResGraphAdaptor
Goldberg-Tarjan algorithm (Preflow with Dynamic Trees)
Dinitz-Sleator-Tarjan (Blocking flow with Dynamic Tree)
deba@2202
     1
/* -*- C++ -*-
deba@2202
     2
 *
deba@2202
     3
 * This file is a part of LEMON, a generic C++ optimization library
deba@2202
     4
 *
alpar@2391
     5
 * Copyright (C) 2003-2007
deba@2202
     6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
deba@2202
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
deba@2202
     8
 *
deba@2202
     9
 * Permission to use, modify and distribute this software is granted
deba@2202
    10
 * provided that this copyright notice appears in all copies. For
deba@2202
    11
 * precise terms see the accompanying LICENSE file.
deba@2202
    12
 *
deba@2202
    13
 * This software is provided "AS IS" with no warranty of any kind,
deba@2202
    14
 * express or implied, and with no claim as to its suitability for any
deba@2202
    15
 * purpose.
deba@2202
    16
 *
deba@2202
    17
 */
deba@2202
    18
deba@2202
    19
#ifndef LEMON_BITS_DEBUG_MAP_H
deba@2202
    20
#define LEMON_BITS_DEBUG_MAP_H
deba@2202
    21
deba@2202
    22
#include <vector>
deba@2202
    23
#include <algorithm>
deba@2202
    24
deba@2202
    25
#include <lemon/bits/traits.h>
deba@2202
    26
#include <lemon/bits/utility.h>
deba@2202
    27
#include <lemon/error.h>
deba@2202
    28
deba@2202
    29
#include <lemon/bits/alteration_notifier.h>
deba@2202
    30
deba@2202
    31
#include <lemon/concept_check.h>
alpar@2260
    32
#include <lemon/concepts/maps.h>
deba@2202
    33
deba@2202
    34
///\ingroup graphbits
deba@2202
    35
///
deba@2202
    36
///\file
deba@2202
    37
///\brief Vector based graph maps for debugging.
deba@2202
    38
namespace lemon {
deba@2202
    39
deba@2333
    40
#ifndef LEMON_STRICT_DEBUG_MAP
deba@2333
    41
#define LEMON_STRICT_DEBUG_MAP false
deba@2333
    42
#endif
deba@2333
    43
deba@2202
    44
  /// \ingroup graphbits
deba@2202
    45
  ///
deba@2202
    46
  /// \brief Graph map based on the std::vector storage.
deba@2202
    47
  ///
deba@2202
    48
  /// The DebugMap template class is graph map structure what
deba@2202
    49
  /// automatically updates the map when a key is added to or erased from
deba@2202
    50
  /// the map. This map also checks some programming failures by example
deba@2202
    51
  /// multiple addition of items, erasing of not existing item or
deba@2202
    52
  /// not erased items at the destruction of the map. It helps the
deba@2202
    53
  /// programmer to avoid segmentation faults and memory leaks.
deba@2202
    54
  ///
deba@2202
    55
  /// \param Notifier The AlterationNotifier that will notify this map.
deba@2202
    56
  /// \param Item The item type of the graph items.
deba@2202
    57
  /// \param Value The value type of the map.
deba@2202
    58
  /// 
deba@2202
    59
  /// \author Balazs Dezso  	
deba@2202
    60
  template <typename _Graph, typename _Item, typename _Value>
deba@2202
    61
  class DebugMap 
deba@2202
    62
    : public ItemSetTraits<_Graph, _Item>::ItemNotifier::ObserverBase {
deba@2202
    63
  private:
deba@2202
    64
		
deba@2202
    65
    /// The container type of the map.
deba@2202
    66
    typedef std::vector<_Value> Container;	
deba@2202
    67
deba@2202
    68
    /// The container type of the debug flags.
deba@2202
    69
    typedef std::vector<bool> Flag;	
deba@2202
    70
deba@2202
    71
  public:
deba@2202
    72
deba@2333
    73
    static const bool strictCheck = LEMON_STRICT_DEBUG_MAP;
deba@2202
    74
deba@2202
    75
    struct MapError {
deba@2202
    76
    public:
deba@2202
    77
      virtual ~MapError() {}
deba@2202
    78
      virtual const char* what() const throw() {
deba@2202
    79
        return "lemon::DebugMap::MapError";
deba@2202
    80
      }
deba@2202
    81
    };
deba@2202
    82
deba@2202
    83
    /// The graph type of the map. 
deba@2202
    84
    typedef _Graph Graph;
deba@2202
    85
    /// The item type of the map.
deba@2202
    86
    typedef _Item Item;
deba@2202
    87
    /// The reference map tag.
deba@2202
    88
    typedef True ReferenceMapTag;
deba@2202
    89
deba@2202
    90
    /// The key type of the map.
deba@2202
    91
    typedef _Item Key;
deba@2202
    92
    /// The value type of the map.
deba@2202
    93
    typedef _Value Value;
deba@2202
    94
deba@2202
    95
    /// The notifier type.
deba@2202
    96
    typedef typename ItemSetTraits<_Graph, _Item>::ItemNotifier Notifier;
deba@2202
    97
deba@2202
    98
    /// The map type.
deba@2202
    99
    typedef DebugMap Map;
deba@2202
   100
    /// The base class of the map.
deba@2202
   101
    typedef typename Notifier::ObserverBase Parent;
deba@2202
   102
deba@2202
   103
    /// The reference type of the map;
deba@2202
   104
    typedef typename Container::reference Reference;
deba@2202
   105
    /// The const reference type of the map;
deba@2202
   106
    typedef typename Container::const_reference ConstReference;
deba@2202
   107
deba@2202
   108
deba@2202
   109
    /// \brief Constructor to attach the new map into the notifier.
deba@2202
   110
    ///
deba@2202
   111
    /// It constructs a map and attachs it into the notifier.
deba@2202
   112
    /// It adds all the items of the graph to the map.
deba@2202
   113
    DebugMap(const Graph& graph) {
deba@2384
   114
      Parent::attach(graph.notifier(Item()));
deba@2384
   115
      container.resize(Parent::notifier()->maxId() + 1);
deba@2384
   116
      flag.resize(Parent::notifier()->maxId() + 1, false);
deba@2384
   117
      const typename Parent::Notifier* notifier = Parent::notifier();
deba@2202
   118
      Item it;
deba@2202
   119
      for (notifier->first(it); it != INVALID; notifier->next(it)) {
deba@2384
   120
        flag[Parent::notifier()->id(it)] = true;
deba@2202
   121
      }
deba@2202
   122
    }
deba@2202
   123
deba@2202
   124
    /// \brief Constructor uses given value to initialize the map. 
deba@2202
   125
    ///
deba@2202
   126
    /// It constructs a map uses a given value to initialize the map. 
deba@2202
   127
    /// It adds all the items of the graph to the map.
deba@2202
   128
    DebugMap(const Graph& graph, const Value& value) {
deba@2384
   129
      Parent::attach(graph.notifier(Item()));
deba@2384
   130
      container.resize(Parent::notifier()->maxId() + 1, value);
deba@2384
   131
      flag.resize(Parent::notifier()->maxId() + 1, false);
deba@2384
   132
      const typename Parent::Notifier* notifier = Parent::notifier();
deba@2202
   133
      Item it;
deba@2202
   134
      for (notifier->first(it); it != INVALID; notifier->next(it)) {
deba@2384
   135
        flag[Parent::notifier()->id(it)] = true;
deba@2202
   136
      }
deba@2202
   137
    }
deba@2202
   138
deba@2202
   139
    /// \brief Copy constructor
deba@2202
   140
    ///
deba@2202
   141
    /// Copy constructor.
deba@2202
   142
    DebugMap(const DebugMap& _copy) : Parent() {
deba@2202
   143
      if (_copy.attached()) {
deba@2384
   144
	Parent::attach(*_copy.notifier());
deba@2202
   145
	container = _copy.container;
deba@2202
   146
      }
deba@2384
   147
      flag.resize(Parent::notifier()->maxId() + 1, false);
deba@2384
   148
      const typename Parent::Notifier* notifier = Parent::notifier();
deba@2202
   149
      Item it;
deba@2202
   150
      for (notifier->first(it); it != INVALID; notifier->next(it)) {
deba@2384
   151
        flag[Parent::notifier()->id(it)] = true;
deba@2384
   152
        LEMON_ASSERT(_copy.flag[Parent::notifier()->id(it)], MapError());
deba@2202
   153
      }
deba@2202
   154
    }
deba@2202
   155
deba@2202
   156
    /// \brief Destructor
deba@2202
   157
    ///
deba@2202
   158
    /// Destructor.
deba@2202
   159
    ~DebugMap() {
deba@2384
   160
      const typename Parent::Notifier* notifier = Parent::notifier();
deba@2202
   161
      if (notifier != 0) {
deba@2202
   162
        Item it;
deba@2202
   163
        for (notifier->first(it); it != INVALID; notifier->next(it)) {
deba@2384
   164
          LEMON_ASSERT(flag[Parent::notifier()->id(it)], MapError());
deba@2384
   165
          flag[Parent::notifier()->id(it)] = false;
deba@2202
   166
        }
deba@2202
   167
      }
deba@2386
   168
      for (int i = 0; i < int(flag.size()); ++i) {
deba@2202
   169
        LEMON_ASSERT(!flag[i], MapError());
deba@2202
   170
      }
deba@2202
   171
    }
deba@2202
   172
deba@2202
   173
    /// \brief Assign operator.
deba@2202
   174
    ///
deba@2202
   175
    /// This operator assigns for each item in the map the
deba@2202
   176
    /// value mapped to the same item in the copied map.  
deba@2202
   177
    /// The parameter map should be indiced with the same
deba@2202
   178
    /// itemset because this assign operator does not change
deba@2202
   179
    /// the container of the map. 
deba@2202
   180
    DebugMap& operator=(const DebugMap& cmap) {
deba@2202
   181
      return operator=<DebugMap>(cmap);
deba@2202
   182
    }
deba@2202
   183
deba@2202
   184
deba@2202
   185
    /// \brief Template assign operator.
deba@2202
   186
    ///
deba@2202
   187
    /// The given parameter should be conform to the ReadMap
deba@2202
   188
    /// concecpt and could be indiced by the current item set of
deba@2202
   189
    /// the NodeMap. In this case the value for each item
deba@2202
   190
    /// is assigned by the value of the given ReadMap. 
deba@2202
   191
    template <typename CMap>
deba@2202
   192
    DebugMap& operator=(const CMap& cmap) {
alpar@2260
   193
      checkConcept<concepts::ReadMap<Key, _Value>, CMap>();
deba@2384
   194
      const typename Parent::Notifier* notifier = Parent::notifier();
deba@2202
   195
      Item it;
deba@2202
   196
      for (notifier->first(it); it != INVALID; notifier->next(it)) {
deba@2202
   197
        set(it, cmap[it]);
deba@2202
   198
      }
deba@2202
   199
      return *this;
deba@2202
   200
    }
deba@2202
   201
    
deba@2202
   202
  public:
deba@2202
   203
deba@2202
   204
    /// \brief The subcript operator.
deba@2202
   205
    ///
deba@2202
   206
    /// The subscript operator. The map can be subscripted by the
deba@2202
   207
    /// actual items of the graph.      
deba@2202
   208
    Reference operator[](const Key& key) {
deba@2384
   209
      LEMON_ASSERT(flag[Parent::notifier()->id(key)], MapError());
deba@2384
   210
      return container[Parent::notifier()->id(key)];
deba@2202
   211
    } 
deba@2202
   212
		
deba@2202
   213
    /// \brief The const subcript operator.
deba@2202
   214
    ///
deba@2202
   215
    /// The const subscript operator. The map can be subscripted by the
deba@2202
   216
    /// actual items of the graph. 
deba@2202
   217
    ConstReference operator[](const Key& key) const {
deba@2384
   218
      LEMON_ASSERT(flag[Parent::notifier()->id(key)], MapError());
deba@2384
   219
      return container[Parent::notifier()->id(key)];
deba@2202
   220
    }
deba@2202
   221
deba@2202
   222
deba@2202
   223
    /// \brief The setter function of the map.
deba@2202
   224
    ///
deba@2202
   225
    /// It the same as operator[](key) = value expression.
deba@2202
   226
    void set(const Key& key, const Value& value) {
deba@2202
   227
      (*this)[key] = value;
deba@2202
   228
    }
deba@2202
   229
deba@2202
   230
  protected:
deba@2202
   231
deba@2202
   232
    /// \brief Adds a new key to the map.
deba@2202
   233
    ///		
deba@2202
   234
    /// It adds a new key to the map. It called by the observer notifier
deba@2202
   235
    /// and it overrides the add() member function of the observer base.     
deba@2202
   236
    virtual void add(const Key& key) {
deba@2384
   237
      int id = Parent::notifier()->id(key);
deba@2386
   238
      if (id >= int(container.size())) {
deba@2202
   239
	container.resize(id + 1);
deba@2202
   240
        flag.resize(id + 1, false);
deba@2202
   241
      }
deba@2384
   242
      LEMON_ASSERT(!flag[Parent::notifier()->id(key)], MapError());
deba@2384
   243
      flag[Parent::notifier()->id(key)] = true;
deba@2202
   244
      if (strictCheck) {
deba@2202
   245
        std::vector<bool> fl(flag.size(), false);
deba@2384
   246
        const typename Parent::Notifier* notifier = Parent::notifier();
deba@2202
   247
        Item it;
deba@2202
   248
        for (notifier->first(it); it != INVALID; notifier->next(it)) {
deba@2386
   249
          int jd = Parent::notifier()->id(it);
deba@2386
   250
          fl[jd] = true;
deba@2202
   251
        }
deba@2202
   252
        LEMON_ASSERT(fl == flag, MapError());
deba@2202
   253
      }
deba@2202
   254
    }
deba@2202
   255
deba@2202
   256
    /// \brief Adds more new keys to the map.
deba@2202
   257
    ///		
deba@2202
   258
    /// It adds more new keys to the map. It called by the observer notifier
deba@2202
   259
    /// and it overrides the add() member function of the observer base.     
deba@2202
   260
    virtual void add(const std::vector<Key>& keys) {
deba@2202
   261
      int max = container.size() - 1;
deba@2386
   262
      for (int i = 0; i < int(keys.size()); ++i) {
deba@2384
   263
        int id = Parent::notifier()->id(keys[i]);
deba@2202
   264
        if (id >= max) {
deba@2202
   265
          max = id;
deba@2202
   266
        }
deba@2202
   267
      }
deba@2202
   268
      container.resize(max + 1);
deba@2202
   269
      flag.resize(max + 1, false);
deba@2386
   270
      for (int i = 0; i < int(keys.size()); ++i) {
deba@2384
   271
        LEMON_ASSERT(!flag[Parent::notifier()->id(keys[i])], MapError());
deba@2384
   272
        flag[Parent::notifier()->id(keys[i])] = true;
deba@2202
   273
      }
deba@2202
   274
      if (strictCheck) {
deba@2202
   275
        std::vector<bool> fl(flag.size(), false);
deba@2384
   276
        const typename Parent::Notifier* notifier = Parent::notifier();
deba@2202
   277
        Item it;
deba@2202
   278
        for (notifier->first(it); it != INVALID; notifier->next(it)) {
deba@2384
   279
          int id = Parent::notifier()->id(it);
deba@2202
   280
          fl[id] = true;
deba@2202
   281
        }
deba@2202
   282
        LEMON_ASSERT(fl == flag, MapError());
deba@2202
   283
      }
deba@2202
   284
    }
deba@2202
   285
deba@2202
   286
    /// \brief Erase a key from the map.
deba@2202
   287
    ///
deba@2202
   288
    /// Erase a key from the map. It called by the observer notifier
deba@2202
   289
    /// and it overrides the erase() member function of the observer base.     
deba@2202
   290
    virtual void erase(const Key& key) {
deba@2202
   291
      if (strictCheck) {
deba@2202
   292
        std::vector<bool> fl(flag.size(), false);
deba@2384
   293
        const typename Parent::Notifier* notifier = Parent::notifier();
deba@2202
   294
        Item it;
deba@2202
   295
        for (notifier->first(it); it != INVALID; notifier->next(it)) {
deba@2384
   296
          int id = Parent::notifier()->id(it);
deba@2202
   297
          fl[id] = true;
deba@2202
   298
        }
deba@2202
   299
        LEMON_ASSERT(fl == flag, MapError());
deba@2202
   300
      }
deba@2384
   301
      container[Parent::notifier()->id(key)] = Value();
deba@2384
   302
      LEMON_ASSERT(flag[Parent::notifier()->id(key)], MapError());
deba@2384
   303
      flag[Parent::notifier()->id(key)] = false;
deba@2202
   304
    }
deba@2202
   305
deba@2202
   306
    /// \brief Erase more keys from the map.
deba@2202
   307
    ///
deba@2202
   308
    /// Erase more keys from the map. It called by the observer notifier
deba@2202
   309
    /// and it overrides the erase() member function of the observer base.     
deba@2202
   310
    virtual void erase(const std::vector<Key>& keys) {
deba@2202
   311
      if (strictCheck) {
deba@2202
   312
        std::vector<bool> fl(flag.size(), false);
deba@2384
   313
        const typename Parent::Notifier* notifier = Parent::notifier();
deba@2202
   314
        Item it;
deba@2202
   315
        for (notifier->first(it); it != INVALID; notifier->next(it)) {
deba@2384
   316
          int id = Parent::notifier()->id(it);
deba@2202
   317
          fl[id] = true;
deba@2202
   318
        }
deba@2202
   319
        LEMON_ASSERT(fl == flag, MapError());
deba@2202
   320
      }
deba@2386
   321
      for (int i = 0; i < int(keys.size()); ++i) {
deba@2384
   322
	container[Parent::notifier()->id(keys[i])] = Value();
deba@2384
   323
        LEMON_ASSERT(flag[Parent::notifier()->id(keys[i])], MapError());
deba@2384
   324
        flag[Parent::notifier()->id(keys[i])] = false;
deba@2202
   325
      }
deba@2202
   326
    }
deba@2202
   327
    
deba@2202
   328
    /// \brief Buildes the map.
deba@2202
   329
    ///	
deba@2202
   330
    /// It buildes the map. It called by the observer notifier
deba@2202
   331
    /// and it overrides the build() member function of the observer base.
deba@2202
   332
    virtual void build() { 
deba@2202
   333
      if (strictCheck) {
deba@2386
   334
        for (int i = 0; i < int(flag.size()); ++i) {
deba@2202
   335
          LEMON_ASSERT(flag[i], MapError());
deba@2202
   336
        }
deba@2202
   337
      }
deba@2384
   338
      int size = Parent::notifier()->maxId() + 1;
deba@2202
   339
      container.reserve(size);
deba@2202
   340
      container.resize(size);
deba@2202
   341
      flag.reserve(size);
deba@2202
   342
      flag.resize(size, false);
deba@2384
   343
      const typename Parent::Notifier* notifier = Parent::notifier();
deba@2202
   344
      Item it;
deba@2202
   345
      for (notifier->first(it); it != INVALID; notifier->next(it)) {
deba@2384
   346
        int id = Parent::notifier()->id(it);
deba@2202
   347
        LEMON_ASSERT(!flag[id], MapError());
deba@2202
   348
        flag[id] = true;
deba@2202
   349
      }
deba@2202
   350
    }
deba@2202
   351
deba@2202
   352
    /// \brief Clear the map.
deba@2202
   353
    ///
deba@2202
   354
    /// It erase all items from the map. It called by the observer notifier
deba@2202
   355
    /// and it overrides the clear() member function of the observer base.     
deba@2202
   356
    virtual void clear() { 
deba@2384
   357
      const typename Parent::Notifier* notifier = Parent::notifier();
deba@2202
   358
      Item it;
deba@2202
   359
      for (notifier->first(it); it != INVALID; notifier->next(it)) {
deba@2384
   360
        int id = Parent::notifier()->id(it);
deba@2202
   361
        LEMON_ASSERT(flag[id], MapError());
deba@2202
   362
        flag[id] = false;
deba@2202
   363
      }
deba@2202
   364
      if (strictCheck) {
deba@2386
   365
        for (int i = 0; i < int(flag.size()); ++i) {
deba@2202
   366
          LEMON_ASSERT(!flag[i], MapError());
deba@2202
   367
        }
deba@2202
   368
      }
deba@2202
   369
      container.clear();
deba@2202
   370
      flag.clear();
deba@2202
   371
    }
deba@2202
   372
    
deba@2202
   373
  private:
deba@2202
   374
		
deba@2202
   375
    Container container;
deba@2202
   376
    Flag flag;
deba@2202
   377
deba@2202
   378
  };
deba@2202
   379
deba@2202
   380
}
deba@2202
   381
deba@2202
   382
#endif