gravatar
deba@inf.elte.hu
deba@inf.elte.hu
Port ListDigraph and ListGraph from svn -r 3433 Details: - port Digraph and Graph concepts - port ListDigraph and ListGraph - port Basic graph constructing tools - port Digraph and Graph tests
0 3 15
default
18 files changed with 7825 insertions and 1 deletions:
↑ Collapse diff ↑
Ignore white space 6 line context
1
/* -*- C++ -*-
2
 *
3
 * This file is a part of LEMON, a generic C++ optimization library
4
 *
5
 * Copyright (C) 2003-2007
6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
8
 *
9
 * Permission to use, modify and distribute this software is granted
10
 * provided that this copyright notice appears in all copies. For
11
 * precise terms see the accompanying LICENSE file.
12
 *
13
 * This software is provided "AS IS" with no warranty of any kind,
14
 * express or implied, and with no claim as to its suitability for any
15
 * purpose.
16
 *
17
 */
18

	
19
#ifndef LEMON_BITS_ALTERATION_NOTIFIER_H
20
#define LEMON_BITS_ALTERATION_NOTIFIER_H
21

	
22
#include <vector>
23
#include <list>
24

	
25
#include <lemon/bits/utility.h>
26

	
27
///\ingroup graphbits
28
///\file
29
///\brief Observer notifier for graph alteration observers.
30

	
31
namespace lemon {
32

	
33
  /// \ingroup graphbits
34
  ///
35
  /// \brief Notifier class to notify observes about alterations in 
36
  /// a container.
37
  ///
38
  /// The simple graph's can be refered as two containers, one node container
39
  /// and one edge container. But they are not standard containers they
40
  /// does not store values directly they are just key continars for more
41
  /// value containers which are the node and edge maps.
42
  ///
43
  /// The graph's node and edge sets can be changed as we add or erase
44
  /// nodes and edges in the graph. Lemon would like to handle easily
45
  /// that the node and edge maps should contain values for all nodes or
46
  /// edges. If we want to check on every indicing if the map contains
47
  /// the current indicing key that cause a drawback in the performance
48
  /// in the library. We use another solution we notify all maps about
49
  /// an alteration in the graph, which cause only drawback on the
50
  /// alteration of the graph.
51
  ///
52
  /// This class provides an interface to the container. The \e first() and \e 
53
  /// next() member functions make possible to iterate on the keys of the
54
  /// container. The \e id() function returns an integer id for each key.
55
  /// The \e maxId() function gives back an upper bound of the ids.
56
  ///
57
  /// For the proper functonality of this class, we should notify it
58
  /// about each alteration in the container. The alterations have four type
59
  /// as \e add(), \e erase(), \e build() and \e clear(). The \e add() and
60
  /// \e erase() signals that only one or few items added or erased to or
61
  /// from the graph. If all items are erased from the graph or from an empty
62
  /// graph a new graph is builded then it can be signaled with the
63
  /// clear() and build() members. Important rule that if we erase items 
64
  /// from graph we should first signal the alteration and after that erase
65
  /// them from the container, on the other way on item addition we should
66
  /// first extend the container and just after that signal the alteration.
67
  ///
68
  /// The alteration can be observed with a class inherited from the
69
  /// \e ObserverBase nested class. The signals can be handled with
70
  /// overriding the virtual functions defined in the base class.  The
71
  /// observer base can be attached to the notifier with the 
72
  /// \e attach() member and can be detached with detach() function. The
73
  /// alteration handlers should not call any function which signals
74
  /// an other alteration in the same notifier and should not
75
  /// detach any observer from the notifier.
76
  ///
77
  /// Alteration observers try to be exception safe. If an \e add() or
78
  /// a \e clear() function throws an exception then the remaining
79
  /// observeres will not be notified and the fulfilled additions will
80
  /// be rolled back by calling the \e erase() or \e clear()
81
  /// functions. Thence the \e erase() and \e clear() should not throw
82
  /// exception. Actullay, it can be throw only 
83
  /// \ref AlterationObserver::ImmediateDetach ImmediateDetach
84
  /// exception which detach the observer from the notifier.
85
  ///
86
  /// There are some place when the alteration observing is not completly
87
  /// reliable. If we want to carry out the node degree in the graph
88
  /// as in the \ref InDegMap and we use the reverseEdge that cause 
89
  /// unreliable functionality. Because the alteration observing signals
90
  /// only erasing and adding but not the reversing it will stores bad
91
  /// degrees. The sub graph adaptors cannot signal the alterations because
92
  /// just a setting in the filter map can modify the graph and this cannot
93
  /// be watched in any way.
94
  ///
95
  /// \param _Container The container which is observed.
96
  /// \param _Item The item type which is obserbved.
97
  ///
98
  /// \author Balazs Dezso
99

	
100
  template <typename _Container, typename _Item>
101
  class AlterationNotifier {
102
  public:
103

	
104
    typedef True Notifier;
105

	
106
    typedef _Container Container;
107
    typedef _Item Item;
108

	
109
    /// \brief Exception which can be called from \e clear() and 
110
    /// \e erase().
111
    ///
112
    /// From the \e clear() and \e erase() function only this
113
    /// exception is allowed to throw. The exception immediatly
114
    /// detaches the current observer from the notifier. Because the
115
    /// \e clear() and \e erase() should not throw other exceptions
116
    /// it can be used to invalidate the observer.
117
    struct ImmediateDetach {};
118

	
119
    /// \brief ObserverBase is the base class for the observers.
120
    ///
121
    /// ObserverBase is the abstract base class for the observers.
122
    /// It will be notified about an item was inserted into or
123
    /// erased from the graph.
124
    ///
125
    /// The observer interface contains some pure virtual functions
126
    /// to override. The add() and erase() functions are
127
    /// to notify the oberver when one item is added or
128
    /// erased.
129
    ///
130
    /// The build() and clear() members are to notify the observer
131
    /// about the container is built from an empty container or
132
    /// is cleared to an empty container. 
133
    /// 
134
    /// \author Balazs Dezso
135

	
136
    class ObserverBase {
137
    protected:
138
      typedef AlterationNotifier Notifier;
139

	
140
      friend class AlterationNotifier;
141

	
142
      /// \brief Default constructor.
143
      ///
144
      /// Default constructor for ObserverBase.
145
      /// 
146
      ObserverBase() : _notifier(0) {}
147

	
148
      /// \brief Constructor which attach the observer into notifier.
149
      ///
150
      /// Constructor which attach the observer into notifier.
151
      ObserverBase(AlterationNotifier& nf) {
152
        attach(nf);
153
      }
154

	
155
      /// \brief Constructor which attach the obserever to the same notifier.
156
      ///
157
      /// Constructor which attach the obserever to the same notifier as
158
      /// the other observer is attached to. 
159
      ObserverBase(const ObserverBase& copy) {
160
	if (copy.attached()) {
161
          attach(*copy.notifier());
162
	}
163
      }
164
	
165
      /// \brief Destructor
166
      virtual ~ObserverBase() {
167
        if (attached()) {
168
          detach();
169
        }
170
      }
171

	
172
      /// \brief Attaches the observer into an AlterationNotifier.
173
      ///
174
      /// This member attaches the observer into an AlterationNotifier.
175
      ///
176
      void attach(AlterationNotifier& nf) {
177
	nf.attach(*this);
178
      }
179
      
180
      /// \brief Detaches the observer into an AlterationNotifier.
181
      ///
182
      /// This member detaches the observer from an AlterationNotifier.
183
      ///
184
      void detach() {
185
        _notifier->detach(*this);
186
      }
187
      
188
      /// \brief Gives back a pointer to the notifier which the map 
189
      /// attached into.
190
      ///
191
      /// This function gives back a pointer to the notifier which the map
192
      /// attached into.
193
      ///
194
      Notifier* notifier() const { return const_cast<Notifier*>(_notifier); }
195
      
196
      /// Gives back true when the observer is attached into a notifier.
197
      bool attached() const { return _notifier != 0; }
198

	
199
    private:
200

	
201
      ObserverBase& operator=(const ObserverBase& copy);
202

	
203
    protected:
204
      
205
      Notifier* _notifier;
206
      typename std::list<ObserverBase*>::iterator _index;
207

	
208
      /// \brief The member function to notificate the observer about an
209
      /// item is added to the container.
210
      ///
211
      /// The add() member function notificates the observer about an item
212
      /// is added to the container. It have to be overrided in the
213
      /// subclasses.
214
      virtual void add(const Item&) = 0;
215

	
216
      /// \brief The member function to notificate the observer about 
217
      /// more item is added to the container.
218
      ///
219
      /// The add() member function notificates the observer about more item
220
      /// is added to the container. It have to be overrided in the
221
      /// subclasses.
222
      virtual void add(const std::vector<Item>& items) = 0;
223

	
224
      /// \brief The member function to notificate the observer about an
225
      /// item is erased from the container.
226
      ///
227
      /// The erase() member function notificates the observer about an
228
      /// item is erased from the container. It have to be overrided in
229
      /// the subclasses.	
230
      virtual void erase(const Item&) = 0;
231

	
232
      /// \brief The member function to notificate the observer about 
233
      /// more item is erased from the container.
234
      ///
235
      /// The erase() member function notificates the observer about more item
236
      /// is erased from the container. It have to be overrided in the
237
      /// subclasses.
238
      virtual void erase(const std::vector<Item>& items) = 0;
239

	
240
      /// \brief The member function to notificate the observer about the
241
      /// container is built.
242
      ///
243
      /// The build() member function notificates the observer about the
244
      /// container is built from an empty container. It have to be
245
      /// overrided in the subclasses.
246

	
247
      virtual void build() = 0;
248

	
249
      /// \brief The member function to notificate the observer about all
250
      /// items are erased from the container.
251
      ///
252
      /// The clear() member function notificates the observer about all
253
      /// items are erased from the container. It have to be overrided in
254
      /// the subclasses.      
255
      virtual void clear() = 0;
256

	
257
    };
258
	
259
  protected:
260

	
261
    const Container* container;
262

	
263
    typedef std::list<ObserverBase*> Observers; 
264
    Observers _observers;
265

	
266
		
267
  public:
268

	
269
    /// \brief Default constructor.
270
    ///
271
    /// The default constructor of the AlterationNotifier. 
272
    /// It creates an empty notifier.
273
    AlterationNotifier() 
274
      : container(0) {}
275

	
276
    /// \brief Constructor.
277
    ///
278
    /// Constructor with the observed container parameter.
279
    AlterationNotifier(const Container& _container) 
280
      : container(&_container) {}
281

	
282
    /// \brief Copy Constructor of the AlterationNotifier. 
283
    ///
284
    /// Copy constructor of the AlterationNotifier. 
285
    /// It creates only an empty notifier because the copiable
286
    /// notifier's observers have to be registered still into that notifier.
287
    AlterationNotifier(const AlterationNotifier& _notifier) 
288
      : container(_notifier.container) {}
289

	
290
    /// \brief Destructor.
291
    ///		
292
    /// Destructor of the AlterationNotifier.
293
    ///
294
    ~AlterationNotifier() {
295
      typename Observers::iterator it;
296
      for (it = _observers.begin(); it != _observers.end(); ++it) {
297
	(*it)->_notifier = 0;
298
      }
299
    }
300

	
301
    /// \brief Sets the container.
302
    ///
303
    /// Sets the container.
304
    void setContainer(const Container& _container) {
305
      container = &_container;
306
    }
307

	
308
  protected:
309

	
310
    AlterationNotifier& operator=(const AlterationNotifier&);
311

	
312
  public:
313

	
314

	
315

	
316
    /// \brief First item in the container.
317
    ///
318
    /// Returns the first item in the container. It is
319
    /// for start the iteration on the container.
320
    void first(Item& item) const {
321
      container->first(item);
322
    }
323

	
324
    /// \brief Next item in the container.
325
    ///
326
    /// Returns the next item in the container. It is
327
    /// for iterate on the container.
328
    void next(Item& item) const {
329
      container->next(item);
330
    }
331

	
332
    /// \brief Returns the id of the item.
333
    ///
334
    /// Returns the id of the item provided by the container.
335
    int id(const Item& item) const {
336
      return container->id(item);
337
    }
338

	
339
    /// \brief Returns the maximum id of the container.
340
    ///
341
    /// Returns the maximum id of the container.
342
    int maxId() const {
343
      return container->maxId(Item());
344
    }
345
		
346
  protected:
347

	
348
    void attach(ObserverBase& observer) {
349
      observer._index = _observers.insert(_observers.begin(), &observer);
350
      observer._notifier = this;
351
    } 
352

	
353
    void detach(ObserverBase& observer) {
354
      _observers.erase(observer._index);
355
      observer._index = _observers.end();
356
      observer._notifier = 0;
357
    }
358

	
359
  public:
360
	
361
    /// \brief Notifies all the registed observers about an item added to 
362
    /// the container.
363
    ///
364
    /// It notifies all the registed observers about an item added to 
365
    /// the container.
366
    /// 
367
    void add(const Item& item) {
368
      typename Observers::reverse_iterator it;
369
      try {
370
        for (it = _observers.rbegin(); it != _observers.rend(); ++it) {
371
          (*it)->add(item);
372
        }
373
      } catch (...) {
374
        typename Observers::iterator jt;
375
        for (jt = it.base(); jt != _observers.end(); ++jt) {
376
          (*jt)->erase(item);
377
        }
378
        throw;
379
      }
380
    }	
381

	
382
    /// \brief Notifies all the registed observers about more item added to 
383
    /// the container.
384
    ///
385
    /// It notifies all the registed observers about more item added to 
386
    /// the container.
387
    /// 
388
    void add(const std::vector<Item>& items) {
389
      typename Observers::reverse_iterator it;
390
      try {
391
        for (it = _observers.rbegin(); it != _observers.rend(); ++it) {
392
          (*it)->add(items);
393
        }
394
      } catch (...) {
395
        typename Observers::iterator jt;
396
        for (jt = it.base(); jt != _observers.end(); ++jt) {
397
          (*jt)->erase(items);
398
        }
399
        throw;
400
      }
401
    }	
402

	
403
    /// \brief Notifies all the registed observers about an item erased from 
404
    /// the container.
405
    ///	
406
    /// It notifies all the registed observers about an item erased from 
407
    /// the container.
408
    /// 
409
    void erase(const Item& item) throw() {
410
      typename Observers::iterator it = _observers.begin();
411
      while (it != _observers.end()) {
412
        try {
413
          (*it)->erase(item);
414
          ++it;
415
        } catch (const ImmediateDetach&) {
416
          it = _observers.erase(it);
417
          (*it)->_index = _observers.end();
418
          (*it)->_notifier = 0;
419
        }
420
      }
421
    }
422

	
423
    /// \brief Notifies all the registed observers about more item erased  
424
    /// from the container.
425
    ///	
426
    /// It notifies all the registed observers about more item erased from 
427
    /// the container.
428
    /// 
429
    void erase(const std::vector<Item>& items) {
430
      typename Observers::iterator it = _observers.begin();
431
      while (it != _observers.end()) {
432
        try {
433
          (*it)->erase(items);
434
          ++it;
435
        } catch (const ImmediateDetach&) {
436
          it = _observers.erase(it);
437
          (*it)->_index = _observers.end();
438
          (*it)->_notifier = 0;
439
        }
440
      }
441
    }
442

	
443
    /// \brief Notifies all the registed observers about the container is 
444
    /// built.
445
    ///		
446
    /// Notifies all the registed observers about the container is built
447
    /// from an empty container.
448
    void build() {
449
      typename Observers::reverse_iterator it;
450
      try {
451
        for (it = _observers.rbegin(); it != _observers.rend(); ++it) {
452
          (*it)->build();
453
        }
454
      } catch (...) {
455
        typename Observers::iterator jt;
456
        for (jt = it.base(); jt != _observers.end(); ++jt) {
457
          (*jt)->clear();
458
        }
459
        throw;
460
      }
461
    }
462

	
463
    /// \brief Notifies all the registed observers about all items are 
464
    /// erased.
465
    ///
466
    /// Notifies all the registed observers about all items are erased
467
    /// from the container.
468
    void clear() {
469
      typename Observers::iterator it = _observers.begin();
470
      while (it != _observers.end()) {
471
        try {
472
          (*it)->clear();
473
          ++it;
474
        } catch (const ImmediateDetach&) {
475
          it = _observers.erase(it);
476
          (*it)->_index = _observers.end();
477
          (*it)->_notifier = 0;
478
        }
479
      }
480
    }
481
  };
482

	
483
}
484

	
485
#endif
Ignore white space 6 line context
1
/* -*- C++ -*-
2
 *
3
 * This file is a part of LEMON, a generic C++ optimization library
4
 *
5
 * Copyright (C) 2003-2007
6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
8
 *
9
 * Permission to use, modify and distribute this software is granted
10
 * provided that this copyright notice appears in all copies. For
11
 * precise terms see the accompanying LICENSE file.
12
 *
13
 * This software is provided "AS IS" with no warranty of any kind,
14
 * express or implied, and with no claim as to its suitability for any
15
 * purpose.
16
 *
17
 */
18

	
19
#ifndef LEMON_BITS_ARRAY_MAP_H
20
#define LEMON_BITS_ARRAY_MAP_H
21

	
22
#include <memory>
23

	
24
#include <lemon/bits/traits.h>
25
#include <lemon/bits/alteration_notifier.h>
26
#include <lemon/concept_check.h>
27
#include <lemon/concepts/maps.h>
28

	
29
/// \ingroup graphbits
30
/// \file
31
/// \brief Graph map based on the array storage.
32

	
33
namespace lemon {
34

	
35
  /// \ingroup graphbits
36
  ///
37
  /// \brief Graph map based on the array storage.
38
  ///
39
  /// The ArrayMap template class is graph map structure what
40
  /// automatically updates the map when a key is added to or erased from
41
  /// the map. This map uses the allocators to implement 
42
  /// the container functionality.
43
  ///
44
  /// The template parameters are the Graph the current Item type and
45
  /// the Value type of the map.
46
  template <typename _Graph, typename _Item, typename _Value>
47
  class ArrayMap 
48
    : public ItemSetTraits<_Graph, _Item>::ItemNotifier::ObserverBase {
49
  public:
50
    /// The graph type of the maps. 
51
    typedef _Graph Graph;
52
    /// The item type of the map.
53
    typedef _Item Item;
54
    /// The reference map tag.
55
    typedef True ReferenceMapTag;
56

	
57
    /// The key type of the maps.
58
    typedef _Item Key;
59
    /// The value type of the map.
60
    typedef _Value Value;
61

	
62
    /// The const reference type of the map.
63
    typedef const _Value& ConstReference;
64
    /// The reference type of the map.
65
    typedef _Value& Reference;
66

	
67
    /// The notifier type.
68
    typedef typename ItemSetTraits<_Graph, _Item>::ItemNotifier Notifier;
69

	
70
    /// The MapBase of the Map which imlements the core regisitry function.
71
    typedef typename Notifier::ObserverBase Parent;
72
		
73
  private:
74
    typedef std::allocator<Value> Allocator;
75

	
76
  public:
77

	
78
    /// \brief Graph initialized map constructor.
79
    ///
80
    /// Graph initialized map constructor.
81
    explicit ArrayMap(const Graph& graph) {
82
      Parent::attach(graph.notifier(Item()));
83
      allocate_memory();
84
      Notifier* nf = Parent::notifier();
85
      Item it;
86
      for (nf->first(it); it != INVALID; nf->next(it)) {
87
	int id = nf->id(it);;
88
	allocator.construct(&(values[id]), Value());
89
      }								
90
    }
91

	
92
    /// \brief Constructor to use default value to initialize the map. 
93
    ///
94
    /// It constructs a map and initialize all of the the map. 
95
    ArrayMap(const Graph& graph, const Value& value) {
96
      Parent::attach(graph.notifier(Item()));
97
      allocate_memory();
98
      Notifier* nf = Parent::notifier();
99
      Item it;
100
      for (nf->first(it); it != INVALID; nf->next(it)) {
101
	int id = nf->id(it);;
102
	allocator.construct(&(values[id]), value);
103
      }								
104
    }
105

	
106
    /// \brief Constructor to copy a map of the same map type.
107
    ///
108
    /// Constructor to copy a map of the same map type.     
109
    ArrayMap(const ArrayMap& copy) : Parent() {
110
      if (copy.attached()) {
111
	attach(*copy.notifier());
112
      }
113
      capacity = copy.capacity;
114
      if (capacity == 0) return;
115
      values = allocator.allocate(capacity);
116
      Notifier* nf = Parent::notifier();
117
      Item it;
118
      for (nf->first(it); it != INVALID; nf->next(it)) {
119
	int id = nf->id(it);;
120
	allocator.construct(&(values[id]), copy.values[id]);
121
      }
122
    }
123

	
124
    /// \brief Assign operator.
125
    ///
126
    /// This operator assigns for each item in the map the
127
    /// value mapped to the same item in the copied map.  
128
    /// The parameter map should be indiced with the same
129
    /// itemset because this assign operator does not change
130
    /// the container of the map. 
131
    ArrayMap& operator=(const ArrayMap& cmap) {
132
      return operator=<ArrayMap>(cmap);
133
    }
134

	
135

	
136
    /// \brief Template assign operator.
137
    ///
138
    /// The given parameter should be conform to the ReadMap
139
    /// concecpt and could be indiced by the current item set of
140
    /// the NodeMap. In this case the value for each item
141
    /// is assigned by the value of the given ReadMap. 
142
    template <typename CMap>
143
    ArrayMap& operator=(const CMap& cmap) {
144
      checkConcept<concepts::ReadMap<Key, _Value>, CMap>();
145
      const typename Parent::Notifier* nf = Parent::notifier();
146
      Item it;
147
      for (nf->first(it); it != INVALID; nf->next(it)) {
148
        set(it, cmap[it]);
149
      }
150
      return *this;
151
    }
152

	
153
    /// \brief The destructor of the map.
154
    ///     
155
    /// The destructor of the map.
156
    virtual ~ArrayMap() {      
157
      if (attached()) {
158
	clear();
159
	detach();
160
      }
161
    }
162
		
163
  protected:
164

	
165
    using Parent::attach;
166
    using Parent::detach;
167
    using Parent::attached;
168

	
169
  public:
170

	
171
    /// \brief The subscript operator. 
172
    ///
173
    /// The subscript operator. The map can be subscripted by the
174
    /// actual keys of the graph. 
175
    Value& operator[](const Key& key) {
176
      int id = Parent::notifier()->id(key);
177
      return values[id];
178
    } 
179
		
180
    /// \brief The const subscript operator.
181
    ///
182
    /// The const subscript operator. The map can be subscripted by the
183
    /// actual keys of the graph. 
184
    const Value& operator[](const Key& key) const {
185
      int id = Parent::notifier()->id(key);
186
      return values[id];
187
    }
188

	
189
    /// \brief Setter function of the map.
190
    ///	
191
    /// Setter function of the map. Equivalent with map[key] = val.
192
    /// This is a compatibility feature with the not dereferable maps.
193
    void set(const Key& key, const Value& val) {
194
      (*this)[key] = val;
195
    }
196

	
197
  protected:
198

	
199
    /// \brief Adds a new key to the map.
200
    ///		
201
    /// It adds a new key to the map. It called by the observer notifier
202
    /// and it overrides the add() member function of the observer base.     
203
    virtual void add(const Key& key) {
204
      Notifier* nf = Parent::notifier();
205
      int id = nf->id(key);
206
      if (id >= capacity) {
207
	int new_capacity = (capacity == 0 ? 1 : capacity);
208
	while (new_capacity <= id) {
209
	  new_capacity <<= 1;
210
	}
211
	Value* new_values = allocator.allocate(new_capacity);
212
	Item it;
213
	for (nf->first(it); it != INVALID; nf->next(it)) {
214
	  int jd = nf->id(it);;
215
	  if (id != jd) {
216
	    allocator.construct(&(new_values[jd]), values[jd]);
217
	    allocator.destroy(&(values[jd]));
218
	  }
219
	}
220
	if (capacity != 0) allocator.deallocate(values, capacity);
221
	values = new_values;
222
	capacity = new_capacity;
223
      }
224
      allocator.construct(&(values[id]), Value());
225
    }
226

	
227
    /// \brief Adds more new keys to the map.
228
    ///		
229
    /// It adds more new keys to the map. It called by the observer notifier
230
    /// and it overrides the add() member function of the observer base.     
231
    virtual void add(const std::vector<Key>& keys) {
232
      Notifier* nf = Parent::notifier();
233
      int max_id = -1;
234
      for (int i = 0; i < int(keys.size()); ++i) {
235
	int id = nf->id(keys[i]);
236
	if (id > max_id) {
237
	  max_id = id;
238
	}
239
      }
240
      if (max_id >= capacity) {
241
	int new_capacity = (capacity == 0 ? 1 : capacity);
242
	while (new_capacity <= max_id) {
243
	  new_capacity <<= 1;
244
	}
245
	Value* new_values = allocator.allocate(new_capacity);
246
	Item it;
247
	for (nf->first(it); it != INVALID; nf->next(it)) {
248
	  int id = nf->id(it);
249
	  bool found = false;
250
	  for (int i = 0; i < int(keys.size()); ++i) {
251
	    int jd = nf->id(keys[i]);
252
	    if (id == jd) {
253
	      found = true;
254
	      break;
255
	    }
256
	  }
257
	  if (found) continue;
258
	  allocator.construct(&(new_values[id]), values[id]);
259
	  allocator.destroy(&(values[id]));
260
	}
261
	if (capacity != 0) allocator.deallocate(values, capacity);
262
	values = new_values;
263
	capacity = new_capacity;
264
      }
265
      for (int i = 0; i < int(keys.size()); ++i) {
266
	int id = nf->id(keys[i]);
267
	allocator.construct(&(values[id]), Value());
268
      }
269
    }
270
		
271
    /// \brief Erase a key from the map.
272
    ///
273
    /// Erase a key from the map. It called by the observer notifier
274
    /// and it overrides the erase() member function of the observer base.     
275
    virtual void erase(const Key& key) {
276
      int id = Parent::notifier()->id(key);
277
      allocator.destroy(&(values[id]));
278
    }
279

	
280
    /// \brief Erase more keys from the map.
281
    ///
282
    /// Erase more keys from the map. It called by the observer notifier
283
    /// and it overrides the erase() member function of the observer base.     
284
    virtual void erase(const std::vector<Key>& keys) {
285
      for (int i = 0; i < int(keys.size()); ++i) {
286
	int id = Parent::notifier()->id(keys[i]);
287
	allocator.destroy(&(values[id]));
288
      }
289
    }
290

	
291
    /// \brief Buildes the map.
292
    ///	
293
    /// It buildes the map. It called by the observer notifier
294
    /// and it overrides the build() member function of the observer base. 
295
    virtual void build() {
296
      Notifier* nf = Parent::notifier();
297
      allocate_memory();
298
      Item it;
299
      for (nf->first(it); it != INVALID; nf->next(it)) {
300
	int id = nf->id(it);;
301
	allocator.construct(&(values[id]), Value());
302
      }								
303
    }
304

	
305
    /// \brief Clear the map.
306
    ///
307
    /// It erase all items from the map. It called by the observer notifier
308
    /// and it overrides the clear() member function of the observer base.     
309
    virtual void clear() {	
310
      Notifier* nf = Parent::notifier();
311
      if (capacity != 0) {
312
	Item it;
313
	for (nf->first(it); it != INVALID; nf->next(it)) {
314
	  int id = nf->id(it);
315
	  allocator.destroy(&(values[id]));
316
	}								
317
	allocator.deallocate(values, capacity);
318
	capacity = 0;
319
      }
320
    }
321

	
322
  private:
323
      
324
    void allocate_memory() {
325
      int max_id = Parent::notifier()->maxId();
326
      if (max_id == -1) {
327
	capacity = 0;
328
	values = 0;
329
	return;
330
      }
331
      capacity = 1;
332
      while (capacity <= max_id) {
333
	capacity <<= 1;
334
      }
335
      values = allocator.allocate(capacity);	
336
    }      
337

	
338
    int capacity;
339
    Value* values;
340
    Allocator allocator;
341

	
342
  };		
343

	
344
}
345

	
346
#endif 
Ignore white space 6 line context
1
/* -*- C++ -*-
2
 *
3
 * This file is a part of LEMON, a generic C++ optimization library
4
 *
5
 * Copyright (C) 2003-2007
6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
8
 *
9
 * Permission to use, modify and distribute this software is granted
10
 * provided that this copyright notice appears in all copies. For
11
 * precise terms see the accompanying LICENSE file.
12
 *
13
 * This software is provided "AS IS" with no warranty of any kind,
14
 * express or implied, and with no claim as to its suitability for any
15
 * purpose.
16
 *
17
 */
18

	
19
#ifndef LEMON_BITS_BASE_EXTENDER_H
20
#define LEMON_BITS_BASE_EXTENDER_H
21

	
22
#include <lemon/bits/invalid.h>
23
#include <lemon/error.h>
24

	
25
#include <lemon/bits/map_extender.h>
26
#include <lemon/bits/default_map.h>
27

	
28
#include <lemon/concept_check.h>
29
#include <lemon/concepts/maps.h>
30

	
31
///\ingroup digraphbits
32
///\file
33
///\brief Extenders for the digraph types
34
namespace lemon {
35

	
36
  /// \ingroup digraphbits
37
  ///
38
  /// \brief BaseDigraph to BaseGraph extender
39
  template <typename Base>
40
  class UndirDigraphExtender : public Base {
41

	
42
  public:
43

	
44
    typedef Base Parent;
45
    typedef typename Parent::Arc Edge;
46
    typedef typename Parent::Node Node;
47

	
48
    typedef True UndirectedTag;
49

	
50
    class Arc : public Edge {
51
      friend class UndirDigraphExtender;
52

	
53
    protected:
54
      bool forward;
55

	
56
      Arc(const Edge &ue, bool _forward) :
57
        Edge(ue), forward(_forward) {}
58

	
59
    public:
60
      Arc() {}
61

	
62
      /// Invalid arc constructor
63
      Arc(Invalid i) : Edge(i), forward(true) {}
64

	
65
      bool operator==(const Arc &that) const {
66
	return forward==that.forward && Edge(*this)==Edge(that);
67
      }
68
      bool operator!=(const Arc &that) const {
69
	return forward!=that.forward || Edge(*this)!=Edge(that);
70
      }
71
      bool operator<(const Arc &that) const {
72
	return forward<that.forward ||
73
	  (!(that.forward<forward) && Edge(*this)<Edge(that));
74
      }
75
    };
76

	
77

	
78

	
79
    using Parent::source;
80

	
81
    /// Source of the given Arc.
82
    Node source(const Arc &e) const {
83
      return e.forward ? Parent::source(e) : Parent::target(e);
84
    }
85

	
86
    using Parent::target;
87

	
88
    /// Target of the given Arc.
89
    Node target(const Arc &e) const {
90
      return e.forward ? Parent::target(e) : Parent::source(e);
91
    }
92

	
93
    /// \brief Directed arc from an edge.
94
    ///
95
    /// Returns a directed arc corresponding to the specified Edge.
96
    /// If the given bool is true the given edge and the
97
    /// returned arc have the same source node.
98
    static Arc direct(const Edge &ue, bool d) {
99
      return Arc(ue, d);
100
    }
101

	
102
    /// Returns whether the given directed arc is same orientation as the
103
    /// corresponding edge.
104
    ///
105
    /// \todo reference to the corresponding point of the undirected digraph
106
    /// concept. "What does the direction of an edge mean?"
107
    static bool direction(const Arc &e) { return e.forward; }
108

	
109

	
110
    using Parent::first;
111
    using Parent::next;
112

	
113
    void first(Arc &e) const {
114
      Parent::first(e);
115
      e.forward=true;
116
    }
117

	
118
    void next(Arc &e) const {
119
      if( e.forward ) {
120
	e.forward = false;
121
      }
122
      else {
123
	Parent::next(e);
124
	e.forward = true;
125
      }
126
    }
127

	
128
    void firstOut(Arc &e, const Node &n) const {
129
      Parent::firstIn(e,n);
130
      if( Edge(e) != INVALID ) {
131
	e.forward = false;
132
      }
133
      else {
134
	Parent::firstOut(e,n);
135
	e.forward = true;
136
      }
137
    }
138
    void nextOut(Arc &e) const {
139
      if( ! e.forward ) {
140
	Node n = Parent::target(e);
141
	Parent::nextIn(e);
142
	if( Edge(e) == INVALID ) {
143
	  Parent::firstOut(e, n);
144
	  e.forward = true;
145
	}
146
      }
147
      else {
148
	Parent::nextOut(e);
149
      }
150
    }
151

	
152
    void firstIn(Arc &e, const Node &n) const {
153
      Parent::firstOut(e,n);
154
      if( Edge(e) != INVALID ) {
155
	e.forward = false;
156
      }
157
      else {
158
	Parent::firstIn(e,n);
159
	e.forward = true;
160
      }
161
    }
162
    void nextIn(Arc &e) const {
163
      if( ! e.forward ) {
164
	Node n = Parent::source(e);
165
	Parent::nextOut(e);
166
	if( Edge(e) == INVALID ) {
167
	  Parent::firstIn(e, n);
168
	  e.forward = true;
169
	}
170
      }
171
      else {
172
	Parent::nextIn(e);
173
      }
174
    }
175

	
176
    void firstInc(Edge &e, bool &d, const Node &n) const {
177
      d = true;
178
      Parent::firstOut(e, n);
179
      if (e != INVALID) return;
180
      d = false;
181
      Parent::firstIn(e, n);
182
    }
183

	
184
    void nextInc(Edge &e, bool &d) const {
185
      if (d) {
186
	Node s = Parent::source(e);
187
	Parent::nextOut(e);
188
	if (e != INVALID) return;
189
	d = false;
190
	Parent::firstIn(e, s);
191
      } else {
192
	Parent::nextIn(e);
193
      }
194
    }
195

	
196
    Node nodeFromId(int ix) const {
197
      return Parent::nodeFromId(ix);
198
    }
199

	
200
    Arc arcFromId(int ix) const {
201
      return direct(Parent::arcFromId(ix >> 1), bool(ix & 1));
202
    }
203

	
204
    Edge edgeFromId(int ix) const {
205
      return Parent::arcFromId(ix);
206
    }
207

	
208
    int id(const Node &n) const {
209
      return Parent::id(n);
210
    }
211

	
212
    int id(const Edge &e) const {
213
      return Parent::id(e);
214
    }
215

	
216
    int id(const Arc &e) const {
217
      return 2 * Parent::id(e) + int(e.forward);
218
    }
219

	
220
    int maxNodeId() const {
221
      return Parent::maxNodeId();
222
    }
223

	
224
    int maxArcId() const {
225
      return 2 * Parent::maxArcId() + 1;
226
    }
227

	
228
    int maxEdgeId() const {
229
      return Parent::maxArcId();
230
    }
231

	
232

	
233
    int arcNum() const {
234
      return 2 * Parent::arcNum();
235
    }
236

	
237
    int edgeNum() const {
238
      return Parent::arcNum();
239
    }
240

	
241
    Arc findArc(Node s, Node t, Arc p = INVALID) const {
242
      if (p == INVALID) {
243
	Edge arc = Parent::findArc(s, t);
244
	if (arc != INVALID) return direct(arc, true);
245
	arc = Parent::findArc(t, s);
246
	if (arc != INVALID) return direct(arc, false);
247
      } else if (direction(p)) {
248
	Edge arc = Parent::findArc(s, t, p);
249
	if (arc != INVALID) return direct(arc, true);
250
	arc = Parent::findArc(t, s);
251
	if (arc != INVALID) return direct(arc, false);	
252
      } else {
253
	Edge arc = Parent::findArc(t, s, p);
254
	if (arc != INVALID) return direct(arc, false);	      
255
      }
256
      return INVALID;
257
    }
258

	
259
    Edge findEdge(Node s, Node t, Edge p = INVALID) const {
260
      if (s != t) {
261
        if (p == INVALID) {
262
          Edge arc = Parent::findArc(s, t);
263
          if (arc != INVALID) return arc;
264
          arc = Parent::findArc(t, s);
265
          if (arc != INVALID) return arc;
266
        } else if (Parent::s(p) == s) {
267
          Edge arc = Parent::findArc(s, t, p);
268
          if (arc != INVALID) return arc;
269
          arc = Parent::findArc(t, s);
270
          if (arc != INVALID) return arc;	
271
        } else {
272
          Edge arc = Parent::findArc(t, s, p);
273
          if (arc != INVALID) return arc;	      
274
        }
275
      } else {
276
        return Parent::findArc(s, t, p);
277
      }
278
      return INVALID;
279
    }
280
  };
281

	
282
  template <typename Base>
283
  class BidirBpGraphExtender : public Base {
284
  public:
285
    typedef Base Parent;
286
    typedef BidirBpGraphExtender Digraph;
287

	
288
    typedef typename Parent::Node Node;
289
    typedef typename Parent::Edge Edge;
290

	
291

	
292
    using Parent::first;
293
    using Parent::next;
294

	
295
    using Parent::id;
296

	
297
    class Red : public Node {
298
      friend class BidirBpGraphExtender;
299
    public:
300
      Red() {}
301
      Red(const Node& node) : Node(node) {
302
	LEMON_ASSERT(Parent::red(node) || node == INVALID, 
303
		     typename Parent::NodeSetError());
304
      }
305
      Red& operator=(const Node& node) {
306
	LEMON_ASSERT(Parent::red(node) || node == INVALID, 
307
		     typename Parent::NodeSetError());
308
        Node::operator=(node);
309
        return *this;
310
      }
311
      Red(Invalid) : Node(INVALID) {}
312
      Red& operator=(Invalid) {
313
        Node::operator=(INVALID);
314
        return *this;
315
      }
316
    };
317

	
318
    void first(Red& node) const {
319
      Parent::firstRed(static_cast<Node&>(node));
320
    }
321
    void next(Red& node) const {
322
      Parent::nextRed(static_cast<Node&>(node));
323
    }
324

	
325
    int id(const Red& node) const {
326
      return Parent::redId(node);
327
    }
328

	
329
    class Blue : public Node {
330
      friend class BidirBpGraphExtender;
331
    public:
332
      Blue() {}
333
      Blue(const Node& node) : Node(node) {
334
	LEMON_ASSERT(Parent::blue(node) || node == INVALID,
335
		     typename Parent::NodeSetError());
336
      }
337
      Blue& operator=(const Node& node) {
338
	LEMON_ASSERT(Parent::blue(node) || node == INVALID, 
339
		     typename Parent::NodeSetError());
340
        Node::operator=(node);
341
        return *this;
342
      }
343
      Blue(Invalid) : Node(INVALID) {}
344
      Blue& operator=(Invalid) {
345
        Node::operator=(INVALID);
346
        return *this;
347
      }
348
    };
349

	
350
    void first(Blue& node) const {
351
      Parent::firstBlue(static_cast<Node&>(node));
352
    }
353
    void next(Blue& node) const {
354
      Parent::nextBlue(static_cast<Node&>(node));
355
    }
356
  
357
    int id(const Blue& node) const {
358
      return Parent::redId(node);
359
    }
360

	
361
    Node source(const Edge& arc) const {
362
      return red(arc);
363
    }
364
    Node target(const Edge& arc) const {
365
      return blue(arc);
366
    }
367

	
368
    void firstInc(Edge& arc, bool& dir, const Node& node) const {
369
      if (Parent::red(node)) {
370
	Parent::firstFromRed(arc, node);
371
	dir = true;
372
      } else {
373
	Parent::firstFromBlue(arc, node);
374
	dir = static_cast<Edge&>(arc) == INVALID;
375
      }
376
    }
377
    void nextInc(Edge& arc, bool& dir) const {
378
      if (dir) {
379
	Parent::nextFromRed(arc);
380
      } else {
381
	Parent::nextFromBlue(arc);
382
	if (arc == INVALID) dir = true;
383
      }
384
    }
385

	
386
    class Arc : public Edge {
387
      friend class BidirBpGraphExtender;
388
    protected:
389
      bool forward;
390

	
391
      Arc(const Edge& arc, bool _forward)
392
	: Edge(arc), forward(_forward) {}
393

	
394
    public:
395
      Arc() {}
396
      Arc (Invalid) : Edge(INVALID), forward(true) {}
397
      bool operator==(const Arc& i) const {
398
	return Edge::operator==(i) && forward == i.forward;
399
      }
400
      bool operator!=(const Arc& i) const {
401
	return Edge::operator!=(i) || forward != i.forward;
402
      }
403
      bool operator<(const Arc& i) const {
404
	return Edge::operator<(i) || 
405
	  (!(i.forward<forward) && Edge(*this)<Edge(i));
406
      }
407
    };
408

	
409
    void first(Arc& arc) const {
410
      Parent::first(static_cast<Edge&>(arc));
411
      arc.forward = true;
412
    }
413

	
414
    void next(Arc& arc) const {
415
      if (!arc.forward) {
416
	Parent::next(static_cast<Edge&>(arc));
417
      }
418
      arc.forward = !arc.forward;
419
    }
420

	
421
    void firstOut(Arc& arc, const Node& node) const {
422
      if (Parent::red(node)) {
423
	Parent::firstFromRed(arc, node);
424
	arc.forward = true;
425
      } else {
426
	Parent::firstFromBlue(arc, node);
427
	arc.forward = static_cast<Edge&>(arc) == INVALID;
428
      }
429
    }
430
    void nextOut(Arc& arc) const {
431
      if (arc.forward) {
432
	Parent::nextFromRed(arc);
433
      } else {
434
	Parent::nextFromBlue(arc);
435
        arc.forward = static_cast<Edge&>(arc) == INVALID;
436
      }
437
    }
438

	
439
    void firstIn(Arc& arc, const Node& node) const {
440
      if (Parent::blue(node)) {
441
	Parent::firstFromBlue(arc, node);
442
	arc.forward = true;	
443
      } else {
444
	Parent::firstFromRed(arc, node);
445
	arc.forward = static_cast<Edge&>(arc) == INVALID;
446
      }
447
    }
448
    void nextIn(Arc& arc) const {
449
      if (arc.forward) {
450
	Parent::nextFromBlue(arc);
451
      } else {
452
	Parent::nextFromRed(arc);
453
	arc.forward = static_cast<Edge&>(arc) == INVALID;
454
      }
455
    }
456

	
457
    Node source(const Arc& arc) const {
458
      return arc.forward ? Parent::red(arc) : Parent::blue(arc);
459
    }
460
    Node target(const Arc& arc) const {
461
      return arc.forward ? Parent::blue(arc) : Parent::red(arc);
462
    }
463

	
464
    int id(const Arc& arc) const {
465
      return (Parent::id(static_cast<const Edge&>(arc)) << 1) + 
466
        (arc.forward ? 0 : 1);
467
    }
468
    Arc arcFromId(int ix) const {
469
      return Arc(Parent::fromEdgeId(ix >> 1), (ix & 1) == 0);
470
    }
471
    int maxArcId() const {
472
      return (Parent::maxEdgeId() << 1) + 1;
473
    }
474

	
475
    bool direction(const Arc& arc) const {
476
      return arc.forward;
477
    }
478

	
479
    Arc direct(const Edge& arc, bool dir) const {
480
      return Arc(arc, dir);
481
    }
482

	
483
    int arcNum() const {
484
      return 2 * Parent::edgeNum();
485
    }
486

	
487
    int edgeNum() const {
488
      return Parent::edgeNum();
489
    }
490

	
491

	
492
  };
493
}
494

	
495
#endif
Ignore white space 6 line context
1
/* -*- C++ -*-
2
 *
3
 * This file is a part of LEMON, a generic C++ optimization library
4
 *
5
 * Copyright (C) 2003-2007
6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
8
 *
9
 * Permission to use, modify and distribute this software is granted
10
 * provided that this copyright notice appears in all copies. For
11
 * precise terms see the accompanying LICENSE file.
12
 *
13
 * This software is provided "AS IS" with no warranty of any kind,
14
 * express or implied, and with no claim as to its suitability for any
15
 * purpose.
16
 *
17
 */
18

	
19
#ifndef LEMON_BITS_DEFAULT_MAP_H
20
#define LEMON_BITS_DEFAULT_MAP_H
21

	
22

	
23
#include <lemon/bits/array_map.h>
24
#include <lemon/bits/vector_map.h>
25
//#include <lemon/bits/debug_map.h>
26

	
27
///\ingroup graphbits
28
///\file
29
///\brief Graph maps that construct and destruct their elements dynamically.
30

	
31
namespace lemon {
32
  
33
  
34
  //#ifndef LEMON_USE_DEBUG_MAP
35

	
36
  template <typename _Graph, typename _Item, typename _Value>
37
  struct DefaultMapSelector {
38
    typedef ArrayMap<_Graph, _Item, _Value> Map;
39
  };
40

	
41
  // bool
42
  template <typename _Graph, typename _Item>
43
  struct DefaultMapSelector<_Graph, _Item, bool> {
44
    typedef VectorMap<_Graph, _Item, bool> Map;
45
  };
46

	
47
  // char
48
  template <typename _Graph, typename _Item>
49
  struct DefaultMapSelector<_Graph, _Item, char> {
50
    typedef VectorMap<_Graph, _Item, char> Map;
51
  };
52

	
53
  template <typename _Graph, typename _Item>
54
  struct DefaultMapSelector<_Graph, _Item, signed char> {
55
    typedef VectorMap<_Graph, _Item, signed char> Map;
56
  };
57

	
58
  template <typename _Graph, typename _Item>
59
  struct DefaultMapSelector<_Graph, _Item, unsigned char> {
60
    typedef VectorMap<_Graph, _Item, unsigned char> Map;
61
  };
62

	
63

	
64
  // int
65
  template <typename _Graph, typename _Item>
66
  struct DefaultMapSelector<_Graph, _Item, signed int> {
67
    typedef VectorMap<_Graph, _Item, signed int> Map;
68
  };
69

	
70
  template <typename _Graph, typename _Item>
71
  struct DefaultMapSelector<_Graph, _Item, unsigned int> {
72
    typedef VectorMap<_Graph, _Item, unsigned int> Map;
73
  };
74

	
75

	
76
  // short
77
  template <typename _Graph, typename _Item>
78
  struct DefaultMapSelector<_Graph, _Item, signed short> {
79
    typedef VectorMap<_Graph, _Item, signed short> Map;
80
  };
81

	
82
  template <typename _Graph, typename _Item>
83
  struct DefaultMapSelector<_Graph, _Item, unsigned short> {
84
    typedef VectorMap<_Graph, _Item, unsigned short> Map;
85
  };
86

	
87

	
88
  // long
89
  template <typename _Graph, typename _Item>
90
  struct DefaultMapSelector<_Graph, _Item, signed long> {
91
    typedef VectorMap<_Graph, _Item, signed long> Map;
92
  };
93

	
94
  template <typename _Graph, typename _Item>
95
  struct DefaultMapSelector<_Graph, _Item, unsigned long> {
96
    typedef VectorMap<_Graph, _Item, unsigned long> Map;
97
  };
98

	
99

	
100
#if defined __GNUC__ && !defined __STRICT_ANSI__
101

	
102
  // long long
103
  template <typename _Graph, typename _Item>
104
  struct DefaultMapSelector<_Graph, _Item, signed long long> {
105
    typedef VectorMap<_Graph, _Item, signed long long> Map;
106
  };
107

	
108
  template <typename _Graph, typename _Item>
109
  struct DefaultMapSelector<_Graph, _Item, unsigned long long> {
110
    typedef VectorMap<_Graph, _Item, unsigned long long> Map;
111
  };
112

	
113
#endif
114

	
115

	
116
  // float
117
  template <typename _Graph, typename _Item>
118
  struct DefaultMapSelector<_Graph, _Item, float> {
119
    typedef VectorMap<_Graph, _Item, float> Map;
120
  };
121

	
122

	
123
  // double
124
  template <typename _Graph, typename _Item>
125
  struct DefaultMapSelector<_Graph, _Item, double> {
126
    typedef VectorMap<_Graph, _Item,  double> Map;
127
  };
128

	
129

	
130
  // long double
131
  template <typename _Graph, typename _Item>
132
  struct DefaultMapSelector<_Graph, _Item, long double> {
133
    typedef VectorMap<_Graph, _Item, long double> Map;
134
  };
135

	
136

	
137
  // pointer
138
  template <typename _Graph, typename _Item, typename _Ptr>
139
  struct DefaultMapSelector<_Graph, _Item, _Ptr*> {
140
    typedef VectorMap<_Graph, _Item, _Ptr*> Map;
141
  };
142

	
143
// #else 
144

	
145
//   template <typename _Graph, typename _Item, typename _Value>
146
//   struct DefaultMapSelector {
147
//     typedef DebugMap<_Graph, _Item, _Value> Map;
148
//   };
149

	
150
// #endif  
151

	
152
  /// \e
153
  template <typename _Graph, typename _Item, typename _Value>
154
  class DefaultMap 
155
    : public DefaultMapSelector<_Graph, _Item, _Value>::Map {
156
  public:
157
    typedef typename DefaultMapSelector<_Graph, _Item, _Value>::Map Parent;
158
    typedef DefaultMap<_Graph, _Item, _Value> Map;
159
    
160
    typedef typename Parent::Graph Graph;
161
    typedef typename Parent::Value Value;
162

	
163
    explicit DefaultMap(const Graph& graph) : Parent(graph) {}
164
    DefaultMap(const Graph& graph, const Value& value) 
165
      : Parent(graph, value) {}
166

	
167
    DefaultMap& operator=(const DefaultMap& cmap) {
168
      return operator=<DefaultMap>(cmap);
169
    }
170

	
171
    template <typename CMap>
172
    DefaultMap& operator=(const CMap& cmap) {
173
      Parent::operator=(cmap);
174
      return *this;
175
    }
176

	
177
  };
178

	
179
}
180

	
181
#endif
Ignore white space 6 line context
1
/* -*- C++ -*-
2
 *
3
 * This file is a part of LEMON, a generic C++ optimization library
4
 *
5
 * Copyright (C) 2003-2007
6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
8
 *
9
 * Permission to use, modify and distribute this software is granted
10
 * provided that this copyright notice appears in all copies. For
11
 * precise terms see the accompanying LICENSE file.
12
 *
13
 * This software is provided "AS IS" with no warranty of any kind,
14
 * express or implied, and with no claim as to its suitability for any
15
 * purpose.
16
 *
17
 */
18

	
19
#ifndef LEMON_BITS_GRAPH_EXTENDER_H
20
#define LEMON_BITS_GRAPH_EXTENDER_H
21

	
22
#include <lemon/bits/invalid.h>
23

	
24
#include <lemon/bits/map_extender.h>
25
#include <lemon/bits/default_map.h>
26

	
27
#include <lemon/concept_check.h>
28
#include <lemon/concepts/maps.h>
29

	
30
///\ingroup graphbits
31
///\file
32
///\brief Extenders for the digraph types
33
namespace lemon {
34

	
35
  /// \ingroup graphbits
36
  ///
37
  /// \brief Extender for the Digraphs
38
  template <typename Base>
39
  class DigraphExtender : public Base {
40
  public:
41

	
42
    typedef Base Parent;
43
    typedef DigraphExtender Digraph;
44

	
45
    // Base extensions
46

	
47
    typedef typename Parent::Node Node;
48
    typedef typename Parent::Arc Arc;
49

	
50
    int maxId(Node) const {
51
      return Parent::maxNodeId();
52
    }
53

	
54
    int maxId(Arc) const {
55
      return Parent::maxArcId();
56
    }
57

	
58
    Node fromId(int id, Node) const {
59
      return Parent::nodeFromId(id);
60
    }
61

	
62
    Arc fromId(int id, Arc) const {
63
      return Parent::arcFromId(id);
64
    }
65

	
66
    Node oppositeNode(const Node &n, const Arc &e) const {
67
      if (n == Parent::source(e))
68
	return Parent::target(e);
69
      else if(n==Parent::target(e))
70
	return Parent::source(e);
71
      else
72
	return INVALID;
73
    }
74

	
75
    // Alterable extension
76

	
77
    typedef AlterationNotifier<DigraphExtender, Node> NodeNotifier;
78
    typedef AlterationNotifier<DigraphExtender, Arc> ArcNotifier;
79

	
80

	
81
  protected:
82

	
83
    mutable NodeNotifier node_notifier;
84
    mutable ArcNotifier arc_notifier;
85

	
86
  public:
87

	
88
    NodeNotifier& notifier(Node) const {
89
      return node_notifier;
90
    }
91
    
92
    ArcNotifier& notifier(Arc) const {
93
      return arc_notifier;
94
    }
95

	
96
    class NodeIt : public Node { 
97
      const Digraph* digraph;
98
    public:
99

	
100
      NodeIt() {}
101

	
102
      NodeIt(Invalid i) : Node(i) { }
103

	
104
      explicit NodeIt(const Digraph& _digraph) : digraph(&_digraph) {
105
	_digraph.first(static_cast<Node&>(*this));
106
      }
107

	
108
      NodeIt(const Digraph& _digraph, const Node& node) 
109
	: Node(node), digraph(&_digraph) {}
110

	
111
      NodeIt& operator++() { 
112
	digraph->next(*this);
113
	return *this; 
114
      }
115

	
116
    };
117

	
118

	
119
    class ArcIt : public Arc { 
120
      const Digraph* digraph;
121
    public:
122

	
123
      ArcIt() { }
124

	
125
      ArcIt(Invalid i) : Arc(i) { }
126

	
127
      explicit ArcIt(const Digraph& _digraph) : digraph(&_digraph) {
128
	_digraph.first(static_cast<Arc&>(*this));
129
      }
130

	
131
      ArcIt(const Digraph& _digraph, const Arc& e) : 
132
	Arc(e), digraph(&_digraph) { }
133

	
134
      ArcIt& operator++() { 
135
	digraph->next(*this);
136
	return *this; 
137
      }
138

	
139
    };
140

	
141

	
142
    class OutArcIt : public Arc { 
143
      const Digraph* digraph;
144
    public:
145

	
146
      OutArcIt() { }
147

	
148
      OutArcIt(Invalid i) : Arc(i) { }
149

	
150
      OutArcIt(const Digraph& _digraph, const Node& node) 
151
	: digraph(&_digraph) {
152
	_digraph.firstOut(*this, node);
153
      }
154

	
155
      OutArcIt(const Digraph& _digraph, const Arc& arc) 
156
	: Arc(arc), digraph(&_digraph) {}
157

	
158
      OutArcIt& operator++() { 
159
	digraph->nextOut(*this);
160
	return *this; 
161
      }
162

	
163
    };
164

	
165

	
166
    class InArcIt : public Arc { 
167
      const Digraph* digraph;
168
    public:
169

	
170
      InArcIt() { }
171

	
172
      InArcIt(Invalid i) : Arc(i) { }
173

	
174
      InArcIt(const Digraph& _digraph, const Node& node) 
175
	: digraph(&_digraph) {
176
	_digraph.firstIn(*this, node);
177
      }
178

	
179
      InArcIt(const Digraph& _digraph, const Arc& arc) : 
180
	Arc(arc), digraph(&_digraph) {}
181

	
182
      InArcIt& operator++() { 
183
	digraph->nextIn(*this);
184
	return *this; 
185
      }
186

	
187
    };
188

	
189
    /// \brief Base node of the iterator
190
    ///
191
    /// Returns the base node (i.e. the source in this case) of the iterator
192
    Node baseNode(const OutArcIt &e) const {
193
      return Parent::source(e);
194
    }
195
    /// \brief Running node of the iterator
196
    ///
197
    /// Returns the running node (i.e. the target in this case) of the
198
    /// iterator
199
    Node runningNode(const OutArcIt &e) const {
200
      return Parent::target(e);
201
    }
202

	
203
    /// \brief Base node of the iterator
204
    ///
205
    /// Returns the base node (i.e. the target in this case) of the iterator
206
    Node baseNode(const InArcIt &e) const {
207
      return Parent::target(e);
208
    }
209
    /// \brief Running node of the iterator
210
    ///
211
    /// Returns the running node (i.e. the source in this case) of the
212
    /// iterator
213
    Node runningNode(const InArcIt &e) const {
214
      return Parent::source(e);
215
    }
216

	
217
    
218
    template <typename _Value>
219
    class NodeMap 
220
      : public MapExtender<DefaultMap<Digraph, Node, _Value> > {
221
    public:
222
      typedef DigraphExtender Digraph;
223
      typedef MapExtender<DefaultMap<Digraph, Node, _Value> > Parent;
224

	
225
      explicit NodeMap(const Digraph& digraph) 
226
	: Parent(digraph) {}
227
      NodeMap(const Digraph& digraph, const _Value& value) 
228
	: Parent(digraph, value) {}
229

	
230
      NodeMap& operator=(const NodeMap& cmap) {
231
	return operator=<NodeMap>(cmap);
232
      }
233

	
234
      template <typename CMap>
235
      NodeMap& operator=(const CMap& cmap) {
236
        Parent::operator=(cmap);
237
	return *this;
238
      }
239

	
240
    };
241

	
242
    template <typename _Value>
243
    class ArcMap 
244
      : public MapExtender<DefaultMap<Digraph, Arc, _Value> > {
245
    public:
246
      typedef DigraphExtender Digraph;
247
      typedef MapExtender<DefaultMap<Digraph, Arc, _Value> > Parent;
248

	
249
      explicit ArcMap(const Digraph& digraph) 
250
	: Parent(digraph) {}
251
      ArcMap(const Digraph& digraph, const _Value& value) 
252
	: Parent(digraph, value) {}
253

	
254
      ArcMap& operator=(const ArcMap& cmap) {
255
	return operator=<ArcMap>(cmap);
256
      }
257

	
258
      template <typename CMap>
259
      ArcMap& operator=(const CMap& cmap) {
260
        Parent::operator=(cmap);
261
	return *this;
262
      }
263
    };
264

	
265

	
266
    Node addNode() {
267
      Node node = Parent::addNode();
268
      notifier(Node()).add(node);
269
      return node;
270
    }
271
    
272
    Arc addArc(const Node& from, const Node& to) {
273
      Arc arc = Parent::addArc(from, to);
274
      notifier(Arc()).add(arc);
275
      return arc;
276
    }
277

	
278
    void clear() {
279
      notifier(Arc()).clear();
280
      notifier(Node()).clear();
281
      Parent::clear();
282
    }
283

	
284
    template <typename Digraph, typename NodeRefMap, typename ArcRefMap>
285
    void build(const Digraph& digraph, NodeRefMap& nodeRef, ArcRefMap& arcRef) {
286
      Parent::build(digraph, nodeRef, arcRef);
287
      notifier(Node()).build();
288
      notifier(Arc()).build();
289
    }
290

	
291
    void erase(const Node& node) {
292
      Arc arc;
293
      Parent::firstOut(arc, node);
294
      while (arc != INVALID ) {
295
	erase(arc);
296
	Parent::firstOut(arc, node);
297
      } 
298

	
299
      Parent::firstIn(arc, node);
300
      while (arc != INVALID ) {
301
	erase(arc);
302
	Parent::firstIn(arc, node);
303
      }
304

	
305
      notifier(Node()).erase(node);
306
      Parent::erase(node);
307
    }
308
    
309
    void erase(const Arc& arc) {
310
      notifier(Arc()).erase(arc);
311
      Parent::erase(arc);
312
    }
313

	
314
    DigraphExtender() {
315
      node_notifier.setContainer(*this);
316
      arc_notifier.setContainer(*this);
317
    } 
318
    
319

	
320
    ~DigraphExtender() {
321
      arc_notifier.clear();
322
      node_notifier.clear();
323
    }
324
  };
325

	
326
  /// \ingroup graphbits
327
  ///
328
  /// \brief Extender for the Graphs
329
  template <typename Base> 
330
  class GraphExtender : public Base {
331
  public:
332
    
333
    typedef Base Parent;
334
    typedef GraphExtender Digraph;
335

	
336
    typedef typename Parent::Node Node;
337
    typedef typename Parent::Arc Arc;
338
    typedef typename Parent::Edge Edge;
339

	
340
    // Graph extension    
341

	
342
    int maxId(Node) const {
343
      return Parent::maxNodeId();
344
    }
345

	
346
    int maxId(Arc) const {
347
      return Parent::maxArcId();
348
    }
349

	
350
    int maxId(Edge) const {
351
      return Parent::maxEdgeId();
352
    }
353

	
354
    Node fromId(int id, Node) const {
355
      return Parent::nodeFromId(id);
356
    }
357

	
358
    Arc fromId(int id, Arc) const {
359
      return Parent::arcFromId(id);
360
    }
361

	
362
    Edge fromId(int id, Edge) const {
363
      return Parent::edgeFromId(id);
364
    }
365

	
366
    Node oppositeNode(const Node &n, const Edge &e) const {
367
      if( n == Parent::source(e))
368
	return Parent::target(e);
369
      else if( n == Parent::target(e))
370
	return Parent::source(e);
371
      else
372
	return INVALID;
373
    }
374

	
375
    Arc oppositeArc(const Arc &e) const {
376
      return Parent::direct(e, !Parent::direction(e));
377
    }
378

	
379
    using Parent::direct;
380
    Arc direct(const Edge &ue, const Node &s) const {
381
      return Parent::direct(ue, Parent::source(ue) == s);
382
    }
383

	
384
    // Alterable extension
385

	
386
    typedef AlterationNotifier<GraphExtender, Node> NodeNotifier;
387
    typedef AlterationNotifier<GraphExtender, Arc> ArcNotifier;
388
    typedef AlterationNotifier<GraphExtender, Edge> EdgeNotifier;
389

	
390

	
391
  protected:
392

	
393
    mutable NodeNotifier node_notifier;
394
    mutable ArcNotifier arc_notifier;
395
    mutable EdgeNotifier edge_notifier;
396

	
397
  public:
398

	
399
    NodeNotifier& notifier(Node) const {
400
      return node_notifier;
401
    }
402
    
403
    ArcNotifier& notifier(Arc) const {
404
      return arc_notifier;
405
    }
406

	
407
    EdgeNotifier& notifier(Edge) const {
408
      return edge_notifier;
409
    }
410

	
411

	
412

	
413
    class NodeIt : public Node { 
414
      const Digraph* digraph;
415
    public:
416

	
417
      NodeIt() {}
418

	
419
      NodeIt(Invalid i) : Node(i) { }
420

	
421
      explicit NodeIt(const Digraph& _digraph) : digraph(&_digraph) {
422
	_digraph.first(static_cast<Node&>(*this));
423
      }
424

	
425
      NodeIt(const Digraph& _digraph, const Node& node) 
426
	: Node(node), digraph(&_digraph) {}
427

	
428
      NodeIt& operator++() { 
429
	digraph->next(*this);
430
	return *this; 
431
      }
432

	
433
    };
434

	
435

	
436
    class ArcIt : public Arc { 
437
      const Digraph* digraph;
438
    public:
439

	
440
      ArcIt() { }
441

	
442
      ArcIt(Invalid i) : Arc(i) { }
443

	
444
      explicit ArcIt(const Digraph& _digraph) : digraph(&_digraph) {
445
	_digraph.first(static_cast<Arc&>(*this));
446
      }
447

	
448
      ArcIt(const Digraph& _digraph, const Arc& e) : 
449
	Arc(e), digraph(&_digraph) { }
450

	
451
      ArcIt& operator++() { 
452
	digraph->next(*this);
453
	return *this; 
454
      }
455

	
456
    };
457

	
458

	
459
    class OutArcIt : public Arc { 
460
      const Digraph* digraph;
461
    public:
462

	
463
      OutArcIt() { }
464

	
465
      OutArcIt(Invalid i) : Arc(i) { }
466

	
467
      OutArcIt(const Digraph& _digraph, const Node& node) 
468
	: digraph(&_digraph) {
469
	_digraph.firstOut(*this, node);
470
      }
471

	
472
      OutArcIt(const Digraph& _digraph, const Arc& arc) 
473
	: Arc(arc), digraph(&_digraph) {}
474

	
475
      OutArcIt& operator++() { 
476
	digraph->nextOut(*this);
477
	return *this; 
478
      }
479

	
480
    };
481

	
482

	
483
    class InArcIt : public Arc { 
484
      const Digraph* digraph;
485
    public:
486

	
487
      InArcIt() { }
488

	
489
      InArcIt(Invalid i) : Arc(i) { }
490

	
491
      InArcIt(const Digraph& _digraph, const Node& node) 
492
	: digraph(&_digraph) {
493
	_digraph.firstIn(*this, node);
494
      }
495

	
496
      InArcIt(const Digraph& _digraph, const Arc& arc) : 
497
	Arc(arc), digraph(&_digraph) {}
498

	
499
      InArcIt& operator++() { 
500
	digraph->nextIn(*this);
501
	return *this; 
502
      }
503

	
504
    };
505

	
506

	
507
    class EdgeIt : public Parent::Edge { 
508
      const Digraph* digraph;
509
    public:
510

	
511
      EdgeIt() { }
512

	
513
      EdgeIt(Invalid i) : Edge(i) { }
514

	
515
      explicit EdgeIt(const Digraph& _digraph) : digraph(&_digraph) {
516
	_digraph.first(static_cast<Edge&>(*this));
517
      }
518

	
519
      EdgeIt(const Digraph& _digraph, const Edge& e) : 
520
	Edge(e), digraph(&_digraph) { }
521

	
522
      EdgeIt& operator++() { 
523
	digraph->next(*this);
524
	return *this; 
525
      }
526

	
527
    };
528

	
529
    class IncArcIt : public Parent::Edge {
530
      friend class GraphExtender;
531
      const Digraph* digraph;
532
      bool direction;
533
    public:
534

	
535
      IncArcIt() { }
536

	
537
      IncArcIt(Invalid i) : Edge(i), direction(false) { }
538

	
539
      IncArcIt(const Digraph& _digraph, const Node &n) : digraph(&_digraph) {
540
	_digraph.firstInc(*this, direction, n);
541
      }
542

	
543
      IncArcIt(const Digraph& _digraph, const Edge &ue, const Node &n)
544
	: digraph(&_digraph), Edge(ue) {
545
	direction = (_digraph.source(ue) == n);
546
      }
547

	
548
      IncArcIt& operator++() {
549
	digraph->nextInc(*this, direction);
550
	return *this; 
551
      }
552
    };
553

	
554
    /// \brief Base node of the iterator
555
    ///
556
    /// Returns the base node (ie. the source in this case) of the iterator
557
    Node baseNode(const OutArcIt &e) const {
558
      return Parent::source(static_cast<const Arc&>(e));
559
    }
560
    /// \brief Running node of the iterator
561
    ///
562
    /// Returns the running node (ie. the target in this case) of the
563
    /// iterator
564
    Node runningNode(const OutArcIt &e) const {
565
      return Parent::target(static_cast<const Arc&>(e));
566
    }
567

	
568
    /// \brief Base node of the iterator
569
    ///
570
    /// Returns the base node (ie. the target in this case) of the iterator
571
    Node baseNode(const InArcIt &e) const {
572
      return Parent::target(static_cast<const Arc&>(e));
573
    }
574
    /// \brief Running node of the iterator
575
    ///
576
    /// Returns the running node (ie. the source in this case) of the
577
    /// iterator
578
    Node runningNode(const InArcIt &e) const {
579
      return Parent::source(static_cast<const Arc&>(e));
580
    }
581

	
582
    /// Base node of the iterator
583
    ///
584
    /// Returns the base node of the iterator
585
    Node baseNode(const IncArcIt &e) const {
586
      return e.direction ? source(e) : target(e);
587
    }
588
    /// Running node of the iterator
589
    ///
590
    /// Returns the running node of the iterator
591
    Node runningNode(const IncArcIt &e) const {
592
      return e.direction ? target(e) : source(e);
593
    }
594

	
595
    // Mappable extension
596

	
597
    template <typename _Value>
598
    class NodeMap 
599
      : public MapExtender<DefaultMap<Digraph, Node, _Value> > {
600
    public:
601
      typedef GraphExtender Digraph;
602
      typedef MapExtender<DefaultMap<Digraph, Node, _Value> > Parent;
603

	
604
      NodeMap(const Digraph& digraph) 
605
	: Parent(digraph) {}
606
      NodeMap(const Digraph& digraph, const _Value& value) 
607
	: Parent(digraph, value) {}
608

	
609
      NodeMap& operator=(const NodeMap& cmap) {
610
	return operator=<NodeMap>(cmap);
611
      }
612

	
613
      template <typename CMap>
614
      NodeMap& operator=(const CMap& cmap) {
615
        Parent::operator=(cmap);
616
	return *this;
617
      }
618

	
619
    };
620

	
621
    template <typename _Value>
622
    class ArcMap 
623
      : public MapExtender<DefaultMap<Digraph, Arc, _Value> > {
624
    public:
625
      typedef GraphExtender Digraph;
626
      typedef MapExtender<DefaultMap<Digraph, Arc, _Value> > Parent;
627

	
628
      ArcMap(const Digraph& digraph) 
629
	: Parent(digraph) {}
630
      ArcMap(const Digraph& digraph, const _Value& value) 
631
	: Parent(digraph, value) {}
632

	
633
      ArcMap& operator=(const ArcMap& cmap) {
634
	return operator=<ArcMap>(cmap);
635
      }
636

	
637
      template <typename CMap>
638
      ArcMap& operator=(const CMap& cmap) {
639
        Parent::operator=(cmap);
640
	return *this;
641
      }
642
    };
643

	
644

	
645
    template <typename _Value>
646
    class EdgeMap 
647
      : public MapExtender<DefaultMap<Digraph, Edge, _Value> > {
648
    public:
649
      typedef GraphExtender Digraph;
650
      typedef MapExtender<DefaultMap<Digraph, Edge, _Value> > Parent;
651

	
652
      EdgeMap(const Digraph& digraph) 
653
	: Parent(digraph) {}
654

	
655
      EdgeMap(const Digraph& digraph, const _Value& value) 
656
	: Parent(digraph, value) {}
657

	
658
      EdgeMap& operator=(const EdgeMap& cmap) {
659
	return operator=<EdgeMap>(cmap);
660
      }
661

	
662
      template <typename CMap>
663
      EdgeMap& operator=(const CMap& cmap) {
664
        Parent::operator=(cmap);
665
	return *this;
666
      }
667

	
668
    };
669

	
670
    // Alteration extension
671

	
672
    Node addNode() {
673
      Node node = Parent::addNode();
674
      notifier(Node()).add(node);
675
      return node;
676
    }
677

	
678
    Edge addEdge(const Node& from, const Node& to) {
679
      Edge edge = Parent::addEdge(from, to);
680
      notifier(Edge()).add(edge);
681
      std::vector<Arc> ev;
682
      ev.push_back(Parent::direct(edge, true));
683
      ev.push_back(Parent::direct(edge, false));      
684
      notifier(Arc()).add(ev);
685
      return edge;
686
    }
687
    
688
    void clear() {
689
      notifier(Arc()).clear();
690
      notifier(Edge()).clear();
691
      notifier(Node()).clear();
692
      Parent::clear();
693
    }
694

	
695
    template <typename Digraph, typename NodeRefMap, typename EdgeRefMap>
696
    void build(const Digraph& digraph, NodeRefMap& nodeRef, 
697
               EdgeRefMap& edgeRef) {
698
      Parent::build(digraph, nodeRef, edgeRef);
699
      notifier(Node()).build();
700
      notifier(Edge()).build();
701
      notifier(Arc()).build();
702
    }
703

	
704
    void erase(const Node& node) {
705
      Arc arc;
706
      Parent::firstOut(arc, node);
707
      while (arc != INVALID ) {
708
	erase(arc);
709
	Parent::firstOut(arc, node);
710
      } 
711

	
712
      Parent::firstIn(arc, node);
713
      while (arc != INVALID ) {
714
	erase(arc);
715
	Parent::firstIn(arc, node);
716
      }
717

	
718
      notifier(Node()).erase(node);
719
      Parent::erase(node);
720
    }
721

	
722
    void erase(const Edge& edge) {
723
      std::vector<Arc> ev;
724
      ev.push_back(Parent::direct(edge, true));
725
      ev.push_back(Parent::direct(edge, false));      
726
      notifier(Arc()).erase(ev);
727
      notifier(Edge()).erase(edge);
728
      Parent::erase(edge);
729
    }
730

	
731
    GraphExtender() {
732
      node_notifier.setContainer(*this); 
733
      arc_notifier.setContainer(*this);
734
      edge_notifier.setContainer(*this);
735
    } 
736

	
737
    ~GraphExtender() {
738
      edge_notifier.clear();
739
      arc_notifier.clear();
740
      node_notifier.clear(); 
741
    } 
742

	
743
  };
744

	
745
}
746

	
747
#endif
Ignore white space 6 line context
1
/* -*- C++ -*-
2
 *
3
 * This file is a part of LEMON, a generic C++ optimization library
4
 *
5
 * Copyright (C) 2003-2007
6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
8
 *
9
 * Permission to use, modify and distribute this software is granted
10
 * provided that this copyright notice appears in all copies. For
11
 * precise terms see the accompanying LICENSE file.
12
 *
13
 * This software is provided "AS IS" with no warranty of any kind,
14
 * express or implied, and with no claim as to its suitability for any
15
 * purpose.
16
 *
17
 */
18

	
19
#ifndef LEMON_BITS_MAP_EXTENDER_H
20
#define LEMON_BITS_MAP_EXTENDER_H
21

	
22
#include <iterator>
23

	
24
#include <lemon/bits/traits.h>
25

	
26
#include <lemon/concept_check.h>
27
#include <lemon/concepts/maps.h>
28

	
29
///\file
30
///\brief Extenders for iterable maps.
31

	
32
namespace lemon {
33

	
34
  /// \ingroup graphbits
35
  /// 
36
  /// \brief Extender for maps
37
  template <typename _Map>
38
  class MapExtender : public _Map {
39
  public:
40

	
41
    typedef _Map Parent;
42
    typedef MapExtender Map;
43

	
44

	
45
    typedef typename Parent::Graph Graph;
46
    typedef typename Parent::Key Item;
47

	
48
    typedef typename Parent::Key Key;
49
    typedef typename Parent::Value Value;
50

	
51
    class MapIt;
52
    class ConstMapIt;
53

	
54
    friend class MapIt;
55
    friend class ConstMapIt;
56

	
57
  public:
58

	
59
    MapExtender(const Graph& graph) 
60
      : Parent(graph) {}
61

	
62
    MapExtender(const Graph& graph, const Value& value) 
63
      : Parent(graph, value) {}
64

	
65
    MapExtender& operator=(const MapExtender& cmap) {
66
      return operator=<MapExtender>(cmap);
67
    }
68

	
69
    template <typename CMap>
70
    MapExtender& operator=(const CMap& cmap) {
71
      Parent::operator=(cmap);
72
      return *this;
73
    } 
74

	
75
    class MapIt : public Item {
76
    public:
77
      
78
      typedef Item Parent;
79
      typedef typename Map::Value Value;
80
      
81
      MapIt() {}
82

	
83
      MapIt(Invalid i) : Parent(i) { }
84

	
85
      explicit MapIt(Map& _map) : map(_map) {
86
        map.notifier()->first(*this);
87
      }
88

	
89
      MapIt(const Map& _map, const Item& item) 
90
	: Parent(item), map(_map) {}
91

	
92
      MapIt& operator++() { 
93
	map.notifier()->next(*this);
94
	return *this; 
95
      }
96
      
97
      typename MapTraits<Map>::ConstReturnValue operator*() const {
98
	return map[*this];
99
      }
100

	
101
      typename MapTraits<Map>::ReturnValue operator*() {
102
	return map[*this];
103
      }
104
      
105
      void set(const Value& value) {
106
	map.set(*this, value);
107
      }
108
      
109
    protected:
110
      Map& map;
111
      
112
    };
113

	
114
    class ConstMapIt : public Item {
115
    public:
116

	
117
      typedef Item Parent;
118

	
119
      typedef typename Map::Value Value;
120
      
121
      ConstMapIt() {}
122

	
123
      ConstMapIt(Invalid i) : Parent(i) { }
124

	
125
      explicit ConstMapIt(Map& _map) : map(_map) {
126
        map.notifier()->first(*this);
127
      }
128

	
129
      ConstMapIt(const Map& _map, const Item& item) 
130
	: Parent(item), map(_map) {}
131

	
132
      ConstMapIt& operator++() { 
133
	map.notifier()->next(*this);
134
	return *this; 
135
      }
136

	
137
      typename MapTraits<Map>::ConstReturnValue operator*() const {
138
	return map[*this];
139
      }
140

	
141
    protected:
142
      const Map& map;
143
    };
144

	
145
    class ItemIt : public Item {
146
    public:
147
      
148
      typedef Item Parent;
149
      
150
      ItemIt() {}
151

	
152
      ItemIt(Invalid i) : Parent(i) { }
153

	
154
      explicit ItemIt(Map& _map) : map(_map) {
155
        map.notifier()->first(*this);
156
      }
157

	
158
      ItemIt(const Map& _map, const Item& item) 
159
	: Parent(item), map(_map) {}
160

	
161
      ItemIt& operator++() { 
162
	map.notifier()->next(*this);
163
	return *this; 
164
      }
165

	
166
    protected:
167
      const Map& map;
168
      
169
    };
170
  };
171

	
172
  /// \ingroup graphbits
173
  /// 
174
  /// \brief Extender for maps which use a subset of the items.
175
  template <typename _Graph, typename _Map>
176
  class SubMapExtender : public _Map {
177
  public:
178

	
179
    typedef _Map Parent;
180
    typedef SubMapExtender Map;
181

	
182
    typedef _Graph Graph;
183

	
184
    typedef typename Parent::Key Item;
185

	
186
    typedef typename Parent::Key Key;
187
    typedef typename Parent::Value Value;
188

	
189
    class MapIt;
190
    class ConstMapIt;
191

	
192
    friend class MapIt;
193
    friend class ConstMapIt;
194

	
195
  public:
196

	
197
    SubMapExtender(const Graph& _graph) 
198
      : Parent(_graph), graph(_graph) {}
199

	
200
    SubMapExtender(const Graph& _graph, const Value& _value) 
201
      : Parent(_graph, _value), graph(_graph) {}
202

	
203
    SubMapExtender& operator=(const SubMapExtender& cmap) {
204
      return operator=<MapExtender>(cmap);
205
    }
206

	
207
    template <typename CMap>
208
    SubMapExtender& operator=(const CMap& cmap) {
209
      checkConcept<concepts::ReadMap<Key, Value>, CMap>();
210
      Item it;
211
      for (graph.first(it); it != INVALID; graph.next(it)) {
212
        Parent::set(it, cmap[it]);
213
      }
214
      return *this;
215
    } 
216

	
217
    class MapIt : public Item {
218
    public:
219
      
220
      typedef Item Parent;
221
      typedef typename Map::Value Value;
222
      
223
      MapIt() {}
224

	
225
      MapIt(Invalid i) : Parent(i) { }
226

	
227
      explicit MapIt(Map& _map) : map(_map) {
228
        map.graph.first(*this);
229
      }
230

	
231
      MapIt(const Map& _map, const Item& item) 
232
	: Parent(item), map(_map) {}
233

	
234
      MapIt& operator++() { 
235
	map.graph.next(*this);
236
	return *this; 
237
      }
238
      
239
      typename MapTraits<Map>::ConstReturnValue operator*() const {
240
	return map[*this];
241
      }
242

	
243
      typename MapTraits<Map>::ReturnValue operator*() {
244
	return map[*this];
245
      }
246
      
247
      void set(const Value& value) {
248
	map.set(*this, value);
249
      }
250
      
251
    protected:
252
      Map& map;
253
      
254
    };
255

	
256
    class ConstMapIt : public Item {
257
    public:
258

	
259
      typedef Item Parent;
260

	
261
      typedef typename Map::Value Value;
262
      
263
      ConstMapIt() {}
264

	
265
      ConstMapIt(Invalid i) : Parent(i) { }
266

	
267
      explicit ConstMapIt(Map& _map) : map(_map) {
268
        map.graph.first(*this);
269
      }
270

	
271
      ConstMapIt(const Map& _map, const Item& item) 
272
	: Parent(item), map(_map) {}
273

	
274
      ConstMapIt& operator++() { 
275
	map.graph.next(*this);
276
	return *this; 
277
      }
278

	
279
      typename MapTraits<Map>::ConstReturnValue operator*() const {
280
	return map[*this];
281
      }
282

	
283
    protected:
284
      const Map& map;
285
    };
286

	
287
    class ItemIt : public Item {
288
    public:
289
      
290
      typedef Item Parent;
291
      
292
      ItemIt() {}
293

	
294
      ItemIt(Invalid i) : Parent(i) { }
295

	
296
      explicit ItemIt(Map& _map) : map(_map) {
297
        map.graph.first(*this);
298
      }
299

	
300
      ItemIt(const Map& _map, const Item& item) 
301
	: Parent(item), map(_map) {}
302

	
303
      ItemIt& operator++() { 
304
	map.graph.next(*this);
305
	return *this; 
306
      }
307

	
308
    protected:
309
      const Map& map;
310
      
311
    };
312
    
313
  private:
314

	
315
    const Graph& graph;
316
    
317
  };
318

	
319
}
320

	
321
#endif
Ignore white space 6 line context
1

	
2
/* -*- C++ -*-
3
 *
4
 * This file is a part of LEMON, a generic C++ optimization library
5
 *
6
 * Copyright (C) 2003-2007
7
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
8
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
9
 *
10
 * Permission to use, modify and distribute this software is granted
11
 * provided that this copyright notice appears in all copies. For
12
 * precise terms see the accompanying LICENSE file.
13
 *
14
 * This software is provided "AS IS" with no warranty of any kind,
15
 * express or implied, and with no claim as to its suitability for any
16
 * purpose.
17
 *
18
 */
19

	
20
#ifndef LEMON_BITS_TRAITS_H
21
#define LEMON_BITS_TRAITS_H
22

	
23
#include <lemon/bits/utility.h>
24

	
25
///\file
26
///\brief Traits for graphs and maps
27
///
28

	
29
namespace lemon {
30
  template <typename _Graph, typename _Item>
31
  class ItemSetTraits {};
32
  
33

	
34
  template <typename Graph, typename Enable = void>
35
  struct NodeNotifierIndicator {
36
    typedef InvalidType Type;
37
  };
38
  template <typename Graph>
39
  struct NodeNotifierIndicator<
40
    Graph, 
41
    typename enable_if<typename Graph::NodeNotifier::Notifier, void>::type
42
  > { 
43
    typedef typename Graph::NodeNotifier Type;
44
  };
45

	
46
  template <typename _Graph>
47
  class ItemSetTraits<_Graph, typename _Graph::Node> {
48
  public:
49
    
50
    typedef _Graph Graph;
51

	
52
    typedef typename Graph::Node Item;
53
    typedef typename Graph::NodeIt ItemIt;
54

	
55
    typedef typename NodeNotifierIndicator<Graph>::Type ItemNotifier;
56

	
57
    template <typename _Value>
58
    class Map : public Graph::template NodeMap<_Value> {
59
    public:
60
      typedef typename Graph::template NodeMap<_Value> Parent; 
61
      typedef typename Graph::template NodeMap<_Value> Type; 
62
      typedef typename Parent::Value Value;
63

	
64
      Map(const Graph& _digraph) : Parent(_digraph) {}
65
      Map(const Graph& _digraph, const Value& _value) 
66
	: Parent(_digraph, _value) {}
67

	
68
     };
69

	
70
  };
71

	
72
  template <typename Graph, typename Enable = void>
73
  struct ArcNotifierIndicator {
74
    typedef InvalidType Type;
75
  };
76
  template <typename Graph>
77
  struct ArcNotifierIndicator<
78
    Graph, 
79
    typename enable_if<typename Graph::ArcNotifier::Notifier, void>::type
80
  > { 
81
    typedef typename Graph::ArcNotifier Type;
82
  };
83

	
84
  template <typename _Graph>
85
  class ItemSetTraits<_Graph, typename _Graph::Arc> {
86
  public:
87
    
88
    typedef _Graph Graph;
89

	
90
    typedef typename Graph::Arc Item;
91
    typedef typename Graph::ArcIt ItemIt;
92

	
93
    typedef typename ArcNotifierIndicator<Graph>::Type ItemNotifier;
94

	
95
    template <typename _Value>
96
    class Map : public Graph::template ArcMap<_Value> {
97
    public:
98
      typedef typename Graph::template ArcMap<_Value> Parent; 
99
      typedef typename Graph::template ArcMap<_Value> Type; 
100
      typedef typename Parent::Value Value;
101

	
102
      Map(const Graph& _digraph) : Parent(_digraph) {}
103
      Map(const Graph& _digraph, const Value& _value) 
104
	: Parent(_digraph, _value) {}
105
    };
106

	
107
  };
108

	
109
  template <typename Graph, typename Enable = void>
110
  struct EdgeNotifierIndicator {
111
    typedef InvalidType Type;
112
  };
113
  template <typename Graph>
114
  struct EdgeNotifierIndicator<
115
    Graph, 
116
    typename enable_if<typename Graph::EdgeNotifier::Notifier, void>::type
117
  > { 
118
    typedef typename Graph::EdgeNotifier Type;
119
  };
120

	
121
  template <typename _Graph>
122
  class ItemSetTraits<_Graph, typename _Graph::Edge> {
123
  public:
124
    
125
    typedef _Graph Graph;
126

	
127
    typedef typename Graph::Edge Item;
128
    typedef typename Graph::EdgeIt ItemIt;
129

	
130
    typedef typename EdgeNotifierIndicator<Graph>::Type ItemNotifier;
131

	
132
    template <typename _Value>
133
    class Map : public Graph::template EdgeMap<_Value> {
134
    public:
135
      typedef typename Graph::template EdgeMap<_Value> Parent; 
136
      typedef typename Graph::template EdgeMap<_Value> Type; 
137
      typedef typename Parent::Value Value;
138

	
139
      Map(const Graph& _digraph) : Parent(_digraph) {}
140
      Map(const Graph& _digraph, const Value& _value) 
141
	: Parent(_digraph, _value) {}
142
    };
143

	
144
  };
145

	
146
  template <typename Map, typename Enable = void>
147
  struct MapTraits {
148
    typedef False ReferenceMapTag;
149

	
150
    typedef typename Map::Key Key;
151
    typedef typename Map::Value Value;
152

	
153
    typedef const Value ConstReturnValue;
154
    typedef const Value ReturnValue;
155
  };
156

	
157
  template <typename Map>
158
  struct MapTraits<
159
    Map, typename enable_if<typename Map::ReferenceMapTag, void>::type > 
160
  {
161
    typedef True ReferenceMapTag;
162
    
163
    typedef typename Map::Key Key;
164
    typedef typename Map::Value Value;
165

	
166
    typedef typename Map::ConstReference ConstReturnValue;
167
    typedef typename Map::Reference ReturnValue;
168

	
169
    typedef typename Map::ConstReference ConstReference; 
170
    typedef typename Map::Reference Reference;
171
 };
172

	
173
  template <typename MatrixMap, typename Enable = void>
174
  struct MatrixMapTraits {
175
    typedef False ReferenceMapTag;
176

	
177
    typedef typename MatrixMap::FirstKey FirstKey;
178
    typedef typename MatrixMap::SecondKey SecondKey;
179
    typedef typename MatrixMap::Value Value;
180

	
181
    typedef const Value ConstReturnValue;
182
    typedef const Value ReturnValue;
183
  };
184

	
185
  template <typename MatrixMap>
186
  struct MatrixMapTraits<
187
    MatrixMap, typename enable_if<typename MatrixMap::ReferenceMapTag, 
188
                                  void>::type > 
189
  {
190
    typedef True ReferenceMapTag;
191
    
192
    typedef typename MatrixMap::FirstKey FirstKey;
193
    typedef typename MatrixMap::SecondKey SecondKey;
194
    typedef typename MatrixMap::Value Value;
195

	
196
    typedef typename MatrixMap::ConstReference ConstReturnValue;
197
    typedef typename MatrixMap::Reference ReturnValue;
198

	
199
    typedef typename MatrixMap::ConstReference ConstReference; 
200
    typedef typename MatrixMap::Reference Reference;
201
 };
202

	
203
  // Indicators for the tags
204

	
205
  template <typename Graph, typename Enable = void>
206
  struct NodeNumTagIndicator {
207
    static const bool value = false;
208
  };
209

	
210
  template <typename Graph>
211
  struct NodeNumTagIndicator<
212
    Graph, 
213
    typename enable_if<typename Graph::NodeNumTag, void>::type
214
  > {
215
    static const bool value = true;
216
  };
217

	
218
  template <typename Graph, typename Enable = void>
219
  struct ArcNumTagIndicator {
220
    static const bool value = false;
221
  };
222

	
223
  template <typename Graph>
224
  struct ArcNumTagIndicator<
225
    Graph, 
226
    typename enable_if<typename Graph::ArcNumTag, void>::type
227
  > {
228
    static const bool value = true;
229
  };
230

	
231
  template <typename Graph, typename Enable = void>
232
  struct FindArcTagIndicator {
233
    static const bool value = false;
234
  };
235

	
236
  template <typename Graph>
237
  struct FindArcTagIndicator<
238
    Graph, 
239
    typename enable_if<typename Graph::FindArcTag, void>::type
240
  > {
241
    static const bool value = true;
242
  };
243

	
244
  template <typename Graph, typename Enable = void>
245
  struct UndirectedTagIndicator {
246
    static const bool value = false;
247
  };
248

	
249
  template <typename Graph>
250
  struct UndirectedTagIndicator<
251
    Graph, 
252
    typename enable_if<typename Graph::UndirectedTag, void>::type
253
  > {
254
    static const bool value = true;
255
  };
256

	
257
  template <typename Graph, typename Enable = void>
258
  struct BuildTagIndicator {
259
    static const bool value = false;
260
  };
261

	
262
  template <typename Graph>
263
  struct BuildTagIndicator<
264
    Graph, 
265
    typename enable_if<typename Graph::BuildTag, void>::type
266
  > {
267
    static const bool value = true;
268
  };
269

	
270
}
271

	
272
#endif
Ignore white space 6 line context
1
/* -*- C++ -*-
2
 *
3
 * This file is a part of LEMON, a generic C++ optimization library
4
 *
5
 * Copyright (C) 2003-2007
6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
8
 *
9
 * Permission to use, modify and distribute this software is granted
10
 * provided that this copyright notice appears in all copies. For
11
 * precise terms see the accompanying LICENSE file.
12
 *
13
 * This software is provided "AS IS" with no warranty of any kind,
14
 * express or implied, and with no claim as to its suitability for any
15
 * purpose.
16
 *
17
 */
18

	
19
#ifndef LEMON_BITS_VECTOR_MAP_H
20
#define LEMON_BITS_VECTOR_MAP_H
21

	
22
#include <vector>
23
#include <algorithm>
24

	
25
#include <lemon/bits/traits.h>
26
#include <lemon/bits/utility.h>
27

	
28
#include <lemon/bits/alteration_notifier.h>
29

	
30
#include <lemon/concept_check.h>
31
#include <lemon/concepts/maps.h>
32

	
33
///\ingroup graphbits
34
///
35
///\file
36
///\brief Vector based graph maps.
37
namespace lemon {
38

	
39
  /// \ingroup graphbits
40
  ///
41
  /// \brief Graph map based on the std::vector storage.
42
  ///
43
  /// The VectorMap template class is graph map structure what
44
  /// automatically updates the map when a key is added to or erased from
45
  /// the map. This map type uses the std::vector to store the values.
46
  ///
47
  /// \param Notifier The AlterationNotifier that will notify this map.
48
  /// \param Item The item type of the graph items.
49
  /// \param Value The value type of the map.
50
  /// 
51
  /// \author Balazs Dezso  	
52
  template <typename _Graph, typename _Item, typename _Value>
53
  class VectorMap 
54
    : public ItemSetTraits<_Graph, _Item>::ItemNotifier::ObserverBase {
55
  private:
56
		
57
    /// The container type of the map.
58
    typedef std::vector<_Value> Container;	
59

	
60
  public:
61

	
62
    /// The graph type of the map. 
63
    typedef _Graph Graph;
64
    /// The item type of the map.
65
    typedef _Item Item;
66
    /// The reference map tag.
67
    typedef True ReferenceMapTag;
68

	
69
    /// The key type of the map.
70
    typedef _Item Key;
71
    /// The value type of the map.
72
    typedef _Value Value;
73

	
74
    /// The notifier type.
75
    typedef typename ItemSetTraits<_Graph, _Item>::ItemNotifier Notifier;
76

	
77
    /// The map type.
78
    typedef VectorMap Map;
79
    /// The base class of the map.
80
    typedef typename Notifier::ObserverBase Parent;
81

	
82
    /// The reference type of the map;
83
    typedef typename Container::reference Reference;
84
    /// The const reference type of the map;
85
    typedef typename Container::const_reference ConstReference;
86

	
87

	
88
    /// \brief Constructor to attach the new map into the notifier.
89
    ///
90
    /// It constructs a map and attachs it into the notifier.
91
    /// It adds all the items of the graph to the map.
92
    VectorMap(const Graph& graph) {
93
      Parent::attach(graph.notifier(Item()));
94
      container.resize(Parent::notifier()->maxId() + 1);
95
    }
96

	
97
    /// \brief Constructor uses given value to initialize the map. 
98
    ///
99
    /// It constructs a map uses a given value to initialize the map. 
100
    /// It adds all the items of the graph to the map.
101
    VectorMap(const Graph& graph, const Value& value) {
102
      Parent::attach(graph.notifier(Item()));
103
      container.resize(Parent::notifier()->maxId() + 1, value);
104
    }
105

	
106
    /// \brief Copy constructor
107
    ///
108
    /// Copy constructor.
109
    VectorMap(const VectorMap& _copy) : Parent() {
110
      if (_copy.attached()) {
111
	Parent::attach(*_copy.notifier());
112
	container = _copy.container;
113
      }
114
    }
115

	
116
    /// \brief Assign operator.
117
    ///
118
    /// This operator assigns for each item in the map the
119
    /// value mapped to the same item in the copied map.  
120
    /// The parameter map should be indiced with the same
121
    /// itemset because this assign operator does not change
122
    /// the container of the map. 
123
    VectorMap& operator=(const VectorMap& cmap) {
124
      return operator=<VectorMap>(cmap);
125
    }
126

	
127

	
128
    /// \brief Template assign operator.
129
    ///
130
    /// The given parameter should be conform to the ReadMap
131
    /// concecpt and could be indiced by the current item set of
132
    /// the NodeMap. In this case the value for each item
133
    /// is assigned by the value of the given ReadMap. 
134
    template <typename CMap>
135
    VectorMap& operator=(const CMap& cmap) {
136
      checkConcept<concepts::ReadMap<Key, _Value>, CMap>();
137
      const typename Parent::Notifier* nf = Parent::notifier();
138
      Item it;
139
      for (nf->first(it); it != INVALID; nf->next(it)) {
140
        set(it, cmap[it]);
141
      }
142
      return *this;
143
    }
144
    
145
  public:
146

	
147
    /// \brief The subcript operator.
148
    ///
149
    /// The subscript operator. The map can be subscripted by the
150
    /// actual items of the graph.      
151
    Reference operator[](const Key& key) {
152
      return container[Parent::notifier()->id(key)];
153
    } 
154
		
155
    /// \brief The const subcript operator.
156
    ///
157
    /// The const subscript operator. The map can be subscripted by the
158
    /// actual items of the graph. 
159
    ConstReference operator[](const Key& key) const {
160
      return container[Parent::notifier()->id(key)];
161
    }
162

	
163

	
164
    /// \brief The setter function of the map.
165
    ///
166
    /// It the same as operator[](key) = value expression.
167
    void set(const Key& key, const Value& value) {
168
      (*this)[key] = value;
169
    }
170

	
171
  protected:
172

	
173
    /// \brief Adds a new key to the map.
174
    ///		
175
    /// It adds a new key to the map. It called by the observer notifier
176
    /// and it overrides the add() member function of the observer base.     
177
    virtual void add(const Key& key) {
178
      int id = Parent::notifier()->id(key);
179
      if (id >= int(container.size())) {
180
	container.resize(id + 1);
181
      }
182
    }
183

	
184
    /// \brief Adds more new keys to the map.
185
    ///		
186
    /// It adds more new keys to the map. It called by the observer notifier
187
    /// and it overrides the add() member function of the observer base.     
188
    virtual void add(const std::vector<Key>& keys) {
189
      int max = container.size() - 1;
190
      for (int i = 0; i < int(keys.size()); ++i) {
191
        int id = Parent::notifier()->id(keys[i]);
192
        if (id >= max) {
193
          max = id;
194
        }
195
      }
196
      container.resize(max + 1);
197
    }
198

	
199
    /// \brief Erase a key from the map.
200
    ///
201
    /// Erase a key from the map. It called by the observer notifier
202
    /// and it overrides the erase() member function of the observer base.     
203
    virtual void erase(const Key& key) {
204
      container[Parent::notifier()->id(key)] = Value();
205
    }
206

	
207
    /// \brief Erase more keys from the map.
208
    ///
209
    /// Erase more keys from the map. It called by the observer notifier
210
    /// and it overrides the erase() member function of the observer base.     
211
    virtual void erase(const std::vector<Key>& keys) {
212
      for (int i = 0; i < int(keys.size()); ++i) {
213
	container[Parent::notifier()->id(keys[i])] = Value();
214
      }
215
    }
216
    
217
    /// \brief Buildes the map.
218
    ///	
219
    /// It buildes the map. It called by the observer notifier
220
    /// and it overrides the build() member function of the observer base.
221
    virtual void build() { 
222
      int size = Parent::notifier()->maxId() + 1;
223
      container.reserve(size);
224
      container.resize(size);
225
    }
226

	
227
    /// \brief Clear the map.
228
    ///
229
    /// It erase all items from the map. It called by the observer notifier
230
    /// and it overrides the clear() member function of the observer base.     
231
    virtual void clear() { 
232
      container.clear();
233
    }
234
    
235
  private:
236
		
237
    Container container;
238

	
239
  };
240

	
241
}
242

	
243
#endif
Ignore white space 6 line context
1
/* -*- C++ -*-
2
 *
3
 * This file is a part of LEMON, a generic C++ optimization library
4
 *
5
 * Copyright (C) 2003-2007
6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
8
 *
9
 * Permission to use, modify and distribute this software is granted
10
 * provided that this copyright notice appears in all copies. For
11
 * precise terms see the accompanying LICENSE file.
12
 *
13
 * This software is provided "AS IS" with no warranty of any kind,
14
 * express or implied, and with no claim as to its suitability for any
15
 * purpose.
16
 *
17
 */
18

	
19
#ifndef LEMON_CONCEPT_DIGRAPH_H
20
#define LEMON_CONCEPT_DIGRAPH_H
21

	
22
///\ingroup graph_concepts
23
///\file
24
///\brief The concept of directed graphs.
25

	
26
#include <lemon/bits/invalid.h>
27
#include <lemon/bits/utility.h>
28
#include <lemon/concepts/maps.h>
29
#include <lemon/concept_check.h>
30
#include <lemon/concepts/graph_components.h>
31

	
32
namespace lemon {
33
  namespace concepts {
34

	
35
    /// \ingroup graph_concepts
36
    ///
37
    /// \brief Class describing the concept of directed graphs.
38
    ///
39
    /// This class describes the \ref concept "concept" of the
40
    /// immutable directed digraphs.
41
    ///
42
    /// Note that actual digraph implementation like @ref ListDigraph or
43
    /// @ref SmartDigraph may have several additional functionality.
44
    ///
45
    /// \sa concept
46
    class Digraph {
47
    private:
48
      ///Digraphs are \e not copy constructible. Use DigraphCopy() instead.
49
      
50
      ///Digraphs are \e not copy constructible. Use DigraphCopy() instead.
51
      ///
52
      Digraph(const Digraph &) {};
53
      ///\brief Assignment of \ref Digraph "Digraph"s to another ones are
54
      ///\e not allowed. Use DigraphCopy() instead.
55
      
56
      ///Assignment of \ref Digraph "Digraph"s to another ones are
57
      ///\e not allowed.  Use DigraphCopy() instead.
58

	
59
      void operator=(const Digraph &) {}
60
    public:
61
      ///\e
62

	
63
      /// Defalult constructor.
64

	
65
      /// Defalult constructor.
66
      ///
67
      Digraph() { }
68
      /// Class for identifying a node of the digraph
69

	
70
      /// This class identifies a node of the digraph. It also serves
71
      /// as a base class of the node iterators,
72
      /// thus they will convert to this type.
73
      class Node {
74
      public:
75
        /// Default constructor
76

	
77
        /// @warning The default constructor sets the iterator
78
        /// to an undefined value.
79
        Node() { }
80
        /// Copy constructor.
81

	
82
        /// Copy constructor.
83
        ///
84
        Node(const Node&) { }
85

	
86
        /// Invalid constructor \& conversion.
87

	
88
        /// This constructor initializes the iterator to be invalid.
89
        /// \sa Invalid for more details.
90
        Node(Invalid) { }
91
        /// Equality operator
92

	
93
        /// Two iterators are equal if and only if they point to the
94
        /// same object or both are invalid.
95
        bool operator==(Node) const { return true; }
96

	
97
        /// Inequality operator
98
        
99
        /// \sa operator==(Node n)
100
        ///
101
        bool operator!=(Node) const { return true; }
102

	
103
	/// Artificial ordering operator.
104
	
105
	/// To allow the use of digraph descriptors as key type in std::map or
106
	/// similar associative container we require this.
107
	///
108
	/// \note This operator only have to define some strict ordering of
109
	/// the items; this order has nothing to do with the iteration
110
	/// ordering of the items.
111
	bool operator<(Node) const { return false; }
112

	
113
      };
114
    
115
      /// This iterator goes through each node.
116

	
117
      /// This iterator goes through each node.
118
      /// Its usage is quite simple, for example you can count the number
119
      /// of nodes in digraph \c g of type \c Digraph like this:
120
      ///\code
121
      /// int count=0;
122
      /// for (Digraph::NodeIt n(g); n!=INVALID; ++n) ++count;
123
      ///\endcode
124
      class NodeIt : public Node {
125
      public:
126
        /// Default constructor
127

	
128
        /// @warning The default constructor sets the iterator
129
        /// to an undefined value.
130
        NodeIt() { }
131
        /// Copy constructor.
132
        
133
        /// Copy constructor.
134
        ///
135
        NodeIt(const NodeIt& n) : Node(n) { }
136
        /// Invalid constructor \& conversion.
137

	
138
        /// Initialize the iterator to be invalid.
139
        /// \sa Invalid for more details.
140
        NodeIt(Invalid) { }
141
        /// Sets the iterator to the first node.
142

	
143
        /// Sets the iterator to the first node of \c g.
144
        ///
145
        NodeIt(const Digraph&) { }
146
        /// Node -> NodeIt conversion.
147

	
148
        /// Sets the iterator to the node of \c the digraph pointed by 
149
	/// the trivial iterator.
150
        /// This feature necessitates that each time we 
151
        /// iterate the arc-set, the iteration order is the same.
152
        NodeIt(const Digraph&, const Node&) { }
153
        /// Next node.
154

	
155
        /// Assign the iterator to the next node.
156
        ///
157
        NodeIt& operator++() { return *this; }
158
      };
159
    
160
    
161
      /// Class for identifying an arc of the digraph
162

	
163
      /// This class identifies an arc of the digraph. It also serves
164
      /// as a base class of the arc iterators,
165
      /// thus they will convert to this type.
166
      class Arc {
167
      public:
168
        /// Default constructor
169

	
170
        /// @warning The default constructor sets the iterator
171
        /// to an undefined value.
172
        Arc() { }
173
        /// Copy constructor.
174

	
175
        /// Copy constructor.
176
        ///
177
        Arc(const Arc&) { }
178
        /// Initialize the iterator to be invalid.
179

	
180
        /// Initialize the iterator to be invalid.
181
        ///
182
        Arc(Invalid) { }
183
        /// Equality operator
184

	
185
        /// Two iterators are equal if and only if they point to the
186
        /// same object or both are invalid.
187
        bool operator==(Arc) const { return true; }
188
        /// Inequality operator
189

	
190
        /// \sa operator==(Arc n)
191
        ///
192
        bool operator!=(Arc) const { return true; }
193

	
194
	/// Artificial ordering operator.
195
	
196
	/// To allow the use of digraph descriptors as key type in std::map or
197
	/// similar associative container we require this.
198
	///
199
	/// \note This operator only have to define some strict ordering of
200
	/// the items; this order has nothing to do with the iteration
201
	/// ordering of the items.
202
	bool operator<(Arc) const { return false; }
203
      };
204
    
205
      /// This iterator goes trough the outgoing arcs of a node.
206

	
207
      /// This iterator goes trough the \e outgoing arcs of a certain node
208
      /// of a digraph.
209
      /// Its usage is quite simple, for example you can count the number
210
      /// of outgoing arcs of a node \c n
211
      /// in digraph \c g of type \c Digraph as follows.
212
      ///\code
213
      /// int count=0;
214
      /// for (Digraph::OutArcIt e(g, n); e!=INVALID; ++e) ++count;
215
      ///\endcode
216
    
217
      class OutArcIt : public Arc {
218
      public:
219
        /// Default constructor
220

	
221
        /// @warning The default constructor sets the iterator
222
        /// to an undefined value.
223
        OutArcIt() { }
224
        /// Copy constructor.
225

	
226
        /// Copy constructor.
227
        ///
228
        OutArcIt(const OutArcIt& e) : Arc(e) { }
229
        /// Initialize the iterator to be invalid.
230

	
231
        /// Initialize the iterator to be invalid.
232
        ///
233
        OutArcIt(Invalid) { }
234
        /// This constructor sets the iterator to the first outgoing arc.
235
    
236
        /// This constructor sets the iterator to the first outgoing arc of
237
        /// the node.
238
        OutArcIt(const Digraph&, const Node&) { }
239
        /// Arc -> OutArcIt conversion
240

	
241
        /// Sets the iterator to the value of the trivial iterator.
242
	/// This feature necessitates that each time we 
243
        /// iterate the arc-set, the iteration order is the same.
244
        OutArcIt(const Digraph&, const Arc&) { }
245
        ///Next outgoing arc
246
        
247
        /// Assign the iterator to the next 
248
        /// outgoing arc of the corresponding node.
249
        OutArcIt& operator++() { return *this; }
250
      };
251

	
252
      /// This iterator goes trough the incoming arcs of a node.
253

	
254
      /// This iterator goes trough the \e incoming arcs of a certain node
255
      /// of a digraph.
256
      /// Its usage is quite simple, for example you can count the number
257
      /// of outgoing arcs of a node \c n
258
      /// in digraph \c g of type \c Digraph as follows.
259
      ///\code
260
      /// int count=0;
261
      /// for(Digraph::InArcIt e(g, n); e!=INVALID; ++e) ++count;
262
      ///\endcode
263

	
264
      class InArcIt : public Arc {
265
      public:
266
        /// Default constructor
267

	
268
        /// @warning The default constructor sets the iterator
269
        /// to an undefined value.
270
        InArcIt() { }
271
        /// Copy constructor.
272

	
273
        /// Copy constructor.
274
        ///
275
        InArcIt(const InArcIt& e) : Arc(e) { }
276
        /// Initialize the iterator to be invalid.
277

	
278
        /// Initialize the iterator to be invalid.
279
        ///
280
        InArcIt(Invalid) { }
281
        /// This constructor sets the iterator to first incoming arc.
282
    
283
        /// This constructor set the iterator to the first incoming arc of
284
        /// the node.
285
        InArcIt(const Digraph&, const Node&) { }
286
        /// Arc -> InArcIt conversion
287

	
288
        /// Sets the iterator to the value of the trivial iterator \c e.
289
        /// This feature necessitates that each time we 
290
        /// iterate the arc-set, the iteration order is the same.
291
        InArcIt(const Digraph&, const Arc&) { }
292
        /// Next incoming arc
293

	
294
        /// Assign the iterator to the next inarc of the corresponding node.
295
        ///
296
        InArcIt& operator++() { return *this; }
297
      };
298
      /// This iterator goes through each arc.
299

	
300
      /// This iterator goes through each arc of a digraph.
301
      /// Its usage is quite simple, for example you can count the number
302
      /// of arcs in a digraph \c g of type \c Digraph as follows:
303
      ///\code
304
      /// int count=0;
305
      /// for(Digraph::ArcIt e(g); e!=INVALID; ++e) ++count;
306
      ///\endcode
307
      class ArcIt : public Arc {
308
      public:
309
        /// Default constructor
310

	
311
        /// @warning The default constructor sets the iterator
312
        /// to an undefined value.
313
        ArcIt() { }
314
        /// Copy constructor.
315

	
316
        /// Copy constructor.
317
        ///
318
        ArcIt(const ArcIt& e) : Arc(e) { }
319
        /// Initialize the iterator to be invalid.
320

	
321
        /// Initialize the iterator to be invalid.
322
        ///
323
        ArcIt(Invalid) { }
324
        /// This constructor sets the iterator to the first arc.
325
    
326
        /// This constructor sets the iterator to the first arc of \c g.
327
        ///@param g the digraph
328
        ArcIt(const Digraph& g) { ignore_unused_variable_warning(g); }
329
        /// Arc -> ArcIt conversion
330

	
331
        /// Sets the iterator to the value of the trivial iterator \c e.
332
        /// This feature necessitates that each time we 
333
        /// iterate the arc-set, the iteration order is the same.
334
        ArcIt(const Digraph&, const Arc&) { } 
335
        ///Next arc
336
        
337
        /// Assign the iterator to the next arc.
338
        ArcIt& operator++() { return *this; }
339
      };
340
      ///Gives back the target node of an arc.
341

	
342
      ///Gives back the target node of an arc.
343
      ///
344
      Node target(Arc) const { return INVALID; }
345
      ///Gives back the source node of an arc.
346

	
347
      ///Gives back the source node of an arc.
348
      ///
349
      Node source(Arc) const { return INVALID; }
350

	
351
      void first(Node&) const {}
352
      void next(Node&) const {}
353

	
354
      void first(Arc&) const {}
355
      void next(Arc&) const {}
356

	
357

	
358
      void firstIn(Arc&, const Node&) const {}
359
      void nextIn(Arc&) const {}
360

	
361
      void firstOut(Arc&, const Node&) const {}
362
      void nextOut(Arc&) const {}
363

	
364
      /// \brief The base node of the iterator.
365
      ///
366
      /// Gives back the base node of the iterator.
367
      /// It is always the target of the pointed arc.
368
      Node baseNode(const InArcIt&) const { return INVALID; }
369

	
370
      /// \brief The running node of the iterator.
371
      ///
372
      /// Gives back the running node of the iterator.
373
      /// It is always the source of the pointed arc.
374
      Node runningNode(const InArcIt&) const { return INVALID; }
375

	
376
      /// \brief The base node of the iterator.
377
      ///
378
      /// Gives back the base node of the iterator.
379
      /// It is always the source of the pointed arc.
380
      Node baseNode(const OutArcIt&) const { return INVALID; }
381

	
382
      /// \brief The running node of the iterator.
383
      ///
384
      /// Gives back the running node of the iterator.
385
      /// It is always the target of the pointed arc.
386
      Node runningNode(const OutArcIt&) const { return INVALID; }
387

	
388
      /// \brief The opposite node on the given arc.
389
      ///
390
      /// Gives back the opposite node on the given arc.
391
      Node oppositeNode(const Node&, const Arc&) const { return INVALID; }
392

	
393
      /// \brief Read write map of the nodes to type \c T.
394
      /// 
395
      /// ReadWrite map of the nodes to type \c T.
396
      /// \sa Reference
397
      template<class T> 
398
      class NodeMap : public ReadWriteMap< Node, T > {
399
      public:
400

	
401
        ///\e
402
        NodeMap(const Digraph&) { }
403
        ///\e
404
        NodeMap(const Digraph&, T) { }
405

	
406
        ///Copy constructor
407
        NodeMap(const NodeMap& nm) : ReadWriteMap< Node, T >(nm) { }
408
        ///Assignment operator
409
        template <typename CMap>
410
        NodeMap& operator=(const CMap&) { 
411
          checkConcept<ReadMap<Node, T>, CMap>();
412
          return *this; 
413
        }
414
      };
415

	
416
      /// \brief Read write map of the arcs to type \c T.
417
      ///
418
      /// Reference map of the arcs to type \c T.
419
      /// \sa Reference
420
      template<class T> 
421
      class ArcMap : public ReadWriteMap<Arc,T> {
422
      public:
423

	
424
        ///\e
425
        ArcMap(const Digraph&) { }
426
        ///\e
427
        ArcMap(const Digraph&, T) { }
428
        ///Copy constructor
429
        ArcMap(const ArcMap& em) : ReadWriteMap<Arc,T>(em) { }
430
        ///Assignment operator
431
        template <typename CMap>
432
        ArcMap& operator=(const CMap&) { 
433
          checkConcept<ReadMap<Arc, T>, CMap>();
434
          return *this; 
435
        }
436
      };
437

	
438
      template <typename RDigraph>
439
      struct Constraints {
440
        void constraints() {
441
          checkConcept<IterableDigraphComponent<>, Digraph>();
442
          checkConcept<MappableDigraphComponent<>, Digraph>();
443
        }
444
      };
445

	
446
    };
447
    
448
  } //namespace concepts  
449
} //namespace lemon
450

	
451

	
452

	
453
#endif // LEMON_CONCEPT_DIGRAPH_H
Ignore white space 6 line context
1
/* -*- C++ -*-
2
 *
3
 * This file is a part of LEMON, a generic C++ optimization library
4
 *
5
 * Copyright (C) 2003-2007
6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
8
 *
9
 * Permission to use, modify and distribute this software is granted
10
 * provided that this copyright notice appears in all copies. For
11
 * precise terms see the accompanying LICENSE file.
12
 *
13
 * This software is provided "AS IS" with no warranty of any kind,
14
 * express or implied, and with no claim as to its suitability for any
15
 * purpose.
16
 *
17
 */
18

	
19
///\ingroup graph_concepts
20
///\file
21
///\brief The concept of Undirected Graphs.
22

	
23
#ifndef LEMON_CONCEPT_GRAPH_H
24
#define LEMON_CONCEPT_GRAPH_H
25

	
26
#include <lemon/concepts/graph_components.h>
27
#include <lemon/concepts/graph.h>
28
#include <lemon/bits/utility.h>
29

	
30
namespace lemon {
31
  namespace concepts {
32

	
33
    /// \ingroup graph_concepts
34
    ///
35
    /// \brief Class describing the concept of Undirected Graphs.
36
    ///
37
    /// This class describes the common interface of all Undirected
38
    /// Graphs.
39
    ///
40
    /// As all concept describing classes it provides only interface
41
    /// without any sensible implementation. So any algorithm for
42
    /// undirected graph should compile with this class, but it will not
43
    /// run properly, of course.
44
    ///
45
    /// The LEMON undirected graphs also fulfill the concept of
46
    /// directed graphs (\ref lemon::concepts::Digraph "Digraph
47
    /// Concept"). Each edges can be seen as two opposite
48
    /// directed arc and consequently the undirected graph can be
49
    /// seen as the direceted graph of these directed arcs. The
50
    /// Graph has the Edge inner class for the edges and
51
    /// the Arc type for the directed arcs. The Arc type is
52
    /// convertible to Edge or inherited from it so from a directed
53
    /// arc we can get the represented edge.
54
    ///
55
    /// In the sense of the LEMON each edge has a default
56
    /// direction (it should be in every computer implementation,
57
    /// because the order of edge's nodes defines an
58
    /// orientation). With the default orientation we can define that
59
    /// the directed arc is forward or backward directed. With the \c
60
    /// direction() and \c direct() function we can get the direction
61
    /// of the directed arc and we can direct an edge.
62
    ///
63
    /// The EdgeIt is an iterator for the edges. We can use
64
    /// the EdgeMap to map values for the edges. The InArcIt and
65
    /// OutArcIt iterates on the same edges but with opposite
66
    /// direction. The IncArcIt iterates also on the same edges
67
    /// as the OutArcIt and InArcIt but it is not convertible to Arc just
68
    /// to Edge.  
69
    class Graph {
70
    public:
71
      /// \brief The undirected graph should be tagged by the
72
      /// UndirectedTag.
73
      ///
74
      /// The undirected graph should be tagged by the UndirectedTag. This
75
      /// tag helps the enable_if technics to make compile time 
76
      /// specializations for undirected graphs.  
77
      typedef True UndirectedTag;
78

	
79
      /// \brief The base type of node iterators, 
80
      /// or in other words, the trivial node iterator.
81
      ///
82
      /// This is the base type of each node iterator,
83
      /// thus each kind of node iterator converts to this.
84
      /// More precisely each kind of node iterator should be inherited 
85
      /// from the trivial node iterator.
86
      class Node {
87
      public:
88
        /// Default constructor
89

	
90
        /// @warning The default constructor sets the iterator
91
        /// to an undefined value.
92
        Node() { }
93
        /// Copy constructor.
94

	
95
        /// Copy constructor.
96
        ///
97
        Node(const Node&) { }
98

	
99
        /// Invalid constructor \& conversion.
100

	
101
        /// This constructor initializes the iterator to be invalid.
102
        /// \sa Invalid for more details.
103
        Node(Invalid) { }
104
        /// Equality operator
105

	
106
        /// Two iterators are equal if and only if they point to the
107
        /// same object or both are invalid.
108
        bool operator==(Node) const { return true; }
109

	
110
        /// Inequality operator
111
        
112
        /// \sa operator==(Node n)
113
        ///
114
        bool operator!=(Node) const { return true; }
115

	
116
	/// Artificial ordering operator.
117
	
118
	/// To allow the use of graph descriptors as key type in std::map or
119
	/// similar associative container we require this.
120
	///
121
	/// \note This operator only have to define some strict ordering of
122
	/// the items; this order has nothing to do with the iteration
123
	/// ordering of the items.
124
	bool operator<(Node) const { return false; }
125

	
126
      };
127
    
128
      /// This iterator goes through each node.
129

	
130
      /// This iterator goes through each node.
131
      /// Its usage is quite simple, for example you can count the number
132
      /// of nodes in graph \c g of type \c Graph like this:
133
      ///\code
134
      /// int count=0;
135
      /// for (Graph::NodeIt n(g); n!=INVALID; ++n) ++count;
136
      ///\endcode
137
      class NodeIt : public Node {
138
      public:
139
        /// Default constructor
140

	
141
        /// @warning The default constructor sets the iterator
142
        /// to an undefined value.
143
        NodeIt() { }
144
        /// Copy constructor.
145
        
146
        /// Copy constructor.
147
        ///
148
        NodeIt(const NodeIt& n) : Node(n) { }
149
        /// Invalid constructor \& conversion.
150

	
151
        /// Initialize the iterator to be invalid.
152
        /// \sa Invalid for more details.
153
        NodeIt(Invalid) { }
154
        /// Sets the iterator to the first node.
155

	
156
        /// Sets the iterator to the first node of \c g.
157
        ///
158
        NodeIt(const Graph&) { }
159
        /// Node -> NodeIt conversion.
160

	
161
        /// Sets the iterator to the node of \c the graph pointed by 
162
	/// the trivial iterator.
163
        /// This feature necessitates that each time we 
164
        /// iterate the arc-set, the iteration order is the same.
165
        NodeIt(const Graph&, const Node&) { }
166
        /// Next node.
167

	
168
        /// Assign the iterator to the next node.
169
        ///
170
        NodeIt& operator++() { return *this; }
171
      };
172
    
173
    
174
      /// The base type of the edge iterators.
175

	
176
      /// The base type of the edge iterators.
177
      ///
178
      class Edge {
179
      public:
180
        /// Default constructor
181

	
182
        /// @warning The default constructor sets the iterator
183
        /// to an undefined value.
184
        Edge() { }
185
        /// Copy constructor.
186

	
187
        /// Copy constructor.
188
        ///
189
        Edge(const Edge&) { }
190
        /// Initialize the iterator to be invalid.
191

	
192
        /// Initialize the iterator to be invalid.
193
        ///
194
        Edge(Invalid) { }
195
        /// Equality operator
196

	
197
        /// Two iterators are equal if and only if they point to the
198
        /// same object or both are invalid.
199
        bool operator==(Edge) const { return true; }
200
        /// Inequality operator
201

	
202
        /// \sa operator==(Edge n)
203
        ///
204
        bool operator!=(Edge) const { return true; }
205

	
206
	/// Artificial ordering operator.
207
	
208
	/// To allow the use of graph descriptors as key type in std::map or
209
	/// similar associative container we require this.
210
	///
211
	/// \note This operator only have to define some strict ordering of
212
	/// the items; this order has nothing to do with the iteration
213
	/// ordering of the items.
214
	bool operator<(Edge) const { return false; }
215
      };
216

	
217
      /// This iterator goes through each edge.
218

	
219
      /// This iterator goes through each edge of a graph.
220
      /// Its usage is quite simple, for example you can count the number
221
      /// of edges in a graph \c g of type \c Graph as follows:
222
      ///\code
223
      /// int count=0;
224
      /// for(Graph::EdgeIt e(g); e!=INVALID; ++e) ++count;
225
      ///\endcode
226
      class EdgeIt : public Edge {
227
      public:
228
        /// Default constructor
229

	
230
        /// @warning The default constructor sets the iterator
231
        /// to an undefined value.
232
        EdgeIt() { }
233
        /// Copy constructor.
234

	
235
        /// Copy constructor.
236
        ///
237
        EdgeIt(const EdgeIt& e) : Edge(e) { }
238
        /// Initialize the iterator to be invalid.
239

	
240
        /// Initialize the iterator to be invalid.
241
        ///
242
        EdgeIt(Invalid) { }
243
        /// This constructor sets the iterator to the first edge.
244
    
245
        /// This constructor sets the iterator to the first edge.
246
        EdgeIt(const Graph&) { }
247
        /// Edge -> EdgeIt conversion
248

	
249
        /// Sets the iterator to the value of the trivial iterator.
250
        /// This feature necessitates that each time we
251
        /// iterate the edge-set, the iteration order is the 
252
	/// same.
253
        EdgeIt(const Graph&, const Edge&) { } 
254
        /// Next edge
255
        
256
        /// Assign the iterator to the next edge.
257
        EdgeIt& operator++() { return *this; }
258
      };
259

	
260
      /// \brief This iterator goes trough the incident undirected 
261
      /// arcs of a node.
262
      ///
263
      /// This iterator goes trough the incident edges
264
      /// of a certain node of a graph. You should assume that the 
265
      /// loop arcs will be iterated twice.
266
      /// 
267
      /// Its usage is quite simple, for example you can compute the
268
      /// degree (i.e. count the number of incident arcs of a node \c n
269
      /// in graph \c g of type \c Graph as follows. 
270
      ///
271
      ///\code
272
      /// int count=0;
273
      /// for(Graph::IncArcIt e(g, n); e!=INVALID; ++e) ++count;
274
      ///\endcode
275
      class IncArcIt : public Edge {
276
      public:
277
        /// Default constructor
278

	
279
        /// @warning The default constructor sets the iterator
280
        /// to an undefined value.
281
        IncArcIt() { }
282
        /// Copy constructor.
283

	
284
        /// Copy constructor.
285
        ///
286
        IncArcIt(const IncArcIt& e) : Edge(e) { }
287
        /// Initialize the iterator to be invalid.
288

	
289
        /// Initialize the iterator to be invalid.
290
        ///
291
        IncArcIt(Invalid) { }
292
        /// This constructor sets the iterator to first incident arc.
293
    
294
        /// This constructor set the iterator to the first incident arc of
295
        /// the node.
296
        IncArcIt(const Graph&, const Node&) { }
297
        /// Edge -> IncArcIt conversion
298

	
299
        /// Sets the iterator to the value of the trivial iterator \c e.
300
        /// This feature necessitates that each time we 
301
        /// iterate the arc-set, the iteration order is the same.
302
        IncArcIt(const Graph&, const Edge&) { }
303
        /// Next incident arc
304

	
305
        /// Assign the iterator to the next incident arc
306
	/// of the corresponding node.
307
        IncArcIt& operator++() { return *this; }
308
      };
309

	
310
      /// The directed arc type.
311

	
312
      /// The directed arc type. It can be converted to the
313
      /// edge or it should be inherited from the undirected
314
      /// arc.
315
      class Arc : public Edge {
316
      public:
317
        /// Default constructor
318

	
319
        /// @warning The default constructor sets the iterator
320
        /// to an undefined value.
321
        Arc() { }
322
        /// Copy constructor.
323

	
324
        /// Copy constructor.
325
        ///
326
        Arc(const Arc& e) : Edge(e) { }
327
        /// Initialize the iterator to be invalid.
328

	
329
        /// Initialize the iterator to be invalid.
330
        ///
331
        Arc(Invalid) { }
332
        /// Equality operator
333

	
334
        /// Two iterators are equal if and only if they point to the
335
        /// same object or both are invalid.
336
        bool operator==(Arc) const { return true; }
337
        /// Inequality operator
338

	
339
        /// \sa operator==(Arc n)
340
        ///
341
        bool operator!=(Arc) const { return true; }
342

	
343
	/// Artificial ordering operator.
344
	
345
	/// To allow the use of graph descriptors as key type in std::map or
346
	/// similar associative container we require this.
347
	///
348
	/// \note This operator only have to define some strict ordering of
349
	/// the items; this order has nothing to do with the iteration
350
	/// ordering of the items.
351
	bool operator<(Arc) const { return false; }
352
	
353
      }; 
354
      /// This iterator goes through each directed arc.
355

	
356
      /// This iterator goes through each arc of a graph.
357
      /// Its usage is quite simple, for example you can count the number
358
      /// of arcs in a graph \c g of type \c Graph as follows:
359
      ///\code
360
      /// int count=0;
361
      /// for(Graph::ArcIt e(g); e!=INVALID; ++e) ++count;
362
      ///\endcode
363
      class ArcIt : public Arc {
364
      public:
365
        /// Default constructor
366

	
367
        /// @warning The default constructor sets the iterator
368
        /// to an undefined value.
369
        ArcIt() { }
370
        /// Copy constructor.
371

	
372
        /// Copy constructor.
373
        ///
374
        ArcIt(const ArcIt& e) : Arc(e) { }
375
        /// Initialize the iterator to be invalid.
376

	
377
        /// Initialize the iterator to be invalid.
378
        ///
379
        ArcIt(Invalid) { }
380
        /// This constructor sets the iterator to the first arc.
381
    
382
        /// This constructor sets the iterator to the first arc of \c g.
383
        ///@param g the graph
384
        ArcIt(const Graph &g) { ignore_unused_variable_warning(g); }
385
        /// Arc -> ArcIt conversion
386

	
387
        /// Sets the iterator to the value of the trivial iterator \c e.
388
        /// This feature necessitates that each time we 
389
        /// iterate the arc-set, the iteration order is the same.
390
        ArcIt(const Graph&, const Arc&) { } 
391
        ///Next arc
392
        
393
        /// Assign the iterator to the next arc.
394
        ArcIt& operator++() { return *this; }
395
      };
396
   
397
      /// This iterator goes trough the outgoing directed arcs of a node.
398

	
399
      /// This iterator goes trough the \e outgoing arcs of a certain node
400
      /// of a graph.
401
      /// Its usage is quite simple, for example you can count the number
402
      /// of outgoing arcs of a node \c n
403
      /// in graph \c g of type \c Graph as follows.
404
      ///\code
405
      /// int count=0;
406
      /// for (Graph::OutArcIt e(g, n); e!=INVALID; ++e) ++count;
407
      ///\endcode
408
    
409
      class OutArcIt : public Arc {
410
      public:
411
        /// Default constructor
412

	
413
        /// @warning The default constructor sets the iterator
414
        /// to an undefined value.
415
        OutArcIt() { }
416
        /// Copy constructor.
417

	
418
        /// Copy constructor.
419
        ///
420
        OutArcIt(const OutArcIt& e) : Arc(e) { }
421
        /// Initialize the iterator to be invalid.
422

	
423
        /// Initialize the iterator to be invalid.
424
        ///
425
        OutArcIt(Invalid) { }
426
        /// This constructor sets the iterator to the first outgoing arc.
427
    
428
        /// This constructor sets the iterator to the first outgoing arc of
429
        /// the node.
430
        ///@param n the node
431
        ///@param g the graph
432
        OutArcIt(const Graph& n, const Node& g) {
433
	  ignore_unused_variable_warning(n);
434
	  ignore_unused_variable_warning(g);
435
	}
436
        /// Arc -> OutArcIt conversion
437

	
438
        /// Sets the iterator to the value of the trivial iterator.
439
	/// This feature necessitates that each time we 
440
        /// iterate the arc-set, the iteration order is the same.
441
        OutArcIt(const Graph&, const Arc&) { }
442
        ///Next outgoing arc
443
        
444
        /// Assign the iterator to the next 
445
        /// outgoing arc of the corresponding node.
446
        OutArcIt& operator++() { return *this; }
447
      };
448

	
449
      /// This iterator goes trough the incoming directed arcs of a node.
450

	
451
      /// This iterator goes trough the \e incoming arcs of a certain node
452
      /// of a graph.
453
      /// Its usage is quite simple, for example you can count the number
454
      /// of outgoing arcs of a node \c n
455
      /// in graph \c g of type \c Graph as follows.
456
      ///\code
457
      /// int count=0;
458
      /// for(Graph::InArcIt e(g, n); e!=INVALID; ++e) ++count;
459
      ///\endcode
460

	
461
      class InArcIt : public Arc {
462
      public:
463
        /// Default constructor
464

	
465
        /// @warning The default constructor sets the iterator
466
        /// to an undefined value.
467
        InArcIt() { }
468
        /// Copy constructor.
469

	
470
        /// Copy constructor.
471
        ///
472
        InArcIt(const InArcIt& e) : Arc(e) { }
473
        /// Initialize the iterator to be invalid.
474

	
475
        /// Initialize the iterator to be invalid.
476
        ///
477
        InArcIt(Invalid) { }
478
        /// This constructor sets the iterator to first incoming arc.
479
    
480
        /// This constructor set the iterator to the first incoming arc of
481
        /// the node.
482
        ///@param n the node
483
        ///@param g the graph
484
        InArcIt(const Graph& g, const Node& n) { 
485
	  ignore_unused_variable_warning(n);
486
	  ignore_unused_variable_warning(g);
487
	}
488
        /// Arc -> InArcIt conversion
489

	
490
        /// Sets the iterator to the value of the trivial iterator \c e.
491
        /// This feature necessitates that each time we 
492
        /// iterate the arc-set, the iteration order is the same.
493
        InArcIt(const Graph&, const Arc&) { }
494
        /// Next incoming arc
495

	
496
        /// Assign the iterator to the next inarc of the corresponding node.
497
        ///
498
        InArcIt& operator++() { return *this; }
499
      };
500

	
501
      /// \brief Read write map of the nodes to type \c T.
502
      /// 
503
      /// ReadWrite map of the nodes to type \c T.
504
      /// \sa Reference
505
      template<class T> 
506
      class NodeMap : public ReadWriteMap< Node, T >
507
      {
508
      public:
509

	
510
        ///\e
511
        NodeMap(const Graph&) { }
512
        ///\e
513
        NodeMap(const Graph&, T) { }
514

	
515
        ///Copy constructor
516
        NodeMap(const NodeMap& nm) : ReadWriteMap< Node, T >(nm) { }
517
        ///Assignment operator
518
        template <typename CMap>
519
        NodeMap& operator=(const CMap&) { 
520
          checkConcept<ReadMap<Node, T>, CMap>();
521
          return *this; 
522
        }
523
      };
524

	
525
      /// \brief Read write map of the directed arcs to type \c T.
526
      ///
527
      /// Reference map of the directed arcs to type \c T.
528
      /// \sa Reference
529
      template<class T> 
530
      class ArcMap : public ReadWriteMap<Arc,T>
531
      {
532
      public:
533

	
534
        ///\e
535
        ArcMap(const Graph&) { }
536
        ///\e
537
        ArcMap(const Graph&, T) { }
538
        ///Copy constructor
539
        ArcMap(const ArcMap& em) : ReadWriteMap<Arc,T>(em) { }
540
        ///Assignment operator
541
        template <typename CMap>
542
        ArcMap& operator=(const CMap&) { 
543
          checkConcept<ReadMap<Arc, T>, CMap>();
544
          return *this; 
545
        }
546
      };
547

	
548
      /// Read write map of the edges to type \c T.
549

	
550
      /// Reference map of the arcs to type \c T.
551
      /// \sa Reference
552
      template<class T> 
553
      class EdgeMap : public ReadWriteMap<Edge,T>
554
      {
555
      public:
556

	
557
        ///\e
558
        EdgeMap(const Graph&) { }
559
        ///\e
560
        EdgeMap(const Graph&, T) { }
561
        ///Copy constructor
562
        EdgeMap(const EdgeMap& em) : ReadWriteMap<Edge,T>(em) {}
563
        ///Assignment operator
564
        template <typename CMap>
565
        EdgeMap& operator=(const CMap&) { 
566
          checkConcept<ReadMap<Edge, T>, CMap>();
567
          return *this; 
568
        }
569
      };
570

	
571
      /// \brief Direct the given edge.
572
      ///
573
      /// Direct the given edge. The returned arc source
574
      /// will be the given node.
575
      Arc direct(const Edge&, const Node&) const {
576
	return INVALID;
577
      }
578

	
579
      /// \brief Direct the given edge.
580
      ///
581
      /// Direct the given edge. The returned arc
582
      /// represents the given edge and the direction comes
583
      /// from the bool parameter. The source of the edge and
584
      /// the directed arc is the same when the given bool is true.
585
      Arc direct(const Edge&, bool) const {
586
	return INVALID;
587
      }
588

	
589
      /// \brief Returns true if the arc has default orientation.
590
      ///
591
      /// Returns whether the given directed arc is same orientation as
592
      /// the corresponding edge's default orientation.
593
      bool direction(Arc) const { return true; }
594

	
595
      /// \brief Returns the opposite directed arc.
596
      ///
597
      /// Returns the opposite directed arc.
598
      Arc oppositeArc(Arc) const { return INVALID; }
599

	
600
      /// \brief Opposite node on an arc
601
      ///
602
      /// \return the opposite of the given Node on the given Edge
603
      Node oppositeNode(Node, Edge) const { return INVALID; }
604

	
605
      /// \brief First node of the edge.
606
      ///
607
      /// \return the first node of the given Edge.
608
      ///
609
      /// Naturally edges don't have direction and thus
610
      /// don't have source and target node. But we use these two methods
611
      /// to query the two nodes of the arc. The direction of the arc
612
      /// which arises this way is called the inherent direction of the
613
      /// edge, and is used to define the "default" direction
614
      /// of the directed versions of the arcs.
615
      /// \sa direction
616
      Node u(Edge) const { return INVALID; }
617

	
618
      /// \brief Second node of the edge.
619
      Node v(Edge) const { return INVALID; }
620

	
621
      /// \brief Source node of the directed arc.
622
      Node source(Arc) const { return INVALID; }
623

	
624
      /// \brief Target node of the directed arc.
625
      Node target(Arc) const { return INVALID; }
626

	
627
      void first(Node&) const {}
628
      void next(Node&) const {}
629

	
630
      void first(Edge&) const {}
631
      void next(Edge&) const {}
632

	
633
      void first(Arc&) const {}
634
      void next(Arc&) const {}
635

	
636
      void firstOut(Arc&, Node) const {}
637
      void nextOut(Arc&) const {}
638

	
639
      void firstIn(Arc&, Node) const {}
640
      void nextIn(Arc&) const {}
641

	
642

	
643
      void firstInc(Edge &, bool &, const Node &) const {}
644
      void nextInc(Edge &, bool &) const {}
645

	
646
      /// \brief Base node of the iterator
647
      ///
648
      /// Returns the base node (the source in this case) of the iterator
649
      Node baseNode(OutArcIt e) const {
650
	return source(e);
651
      }
652
      /// \brief Running node of the iterator
653
      ///
654
      /// Returns the running node (the target in this case) of the
655
      /// iterator
656
      Node runningNode(OutArcIt e) const {
657
	return target(e);
658
      }
659

	
660
      /// \brief Base node of the iterator
661
      ///
662
      /// Returns the base node (the target in this case) of the iterator
663
      Node baseNode(InArcIt e) const {
664
	return target(e);
665
      }
666
      /// \brief Running node of the iterator
667
      ///
668
      /// Returns the running node (the source in this case) of the
669
      /// iterator
670
      Node runningNode(InArcIt e) const {
671
	return source(e);
672
      }
673

	
674
      /// \brief Base node of the iterator
675
      ///
676
      /// Returns the base node of the iterator
677
      Node baseNode(IncArcIt) const {
678
	return INVALID;
679
      }
680
      
681
      /// \brief Running node of the iterator
682
      ///
683
      /// Returns the running node of the iterator
684
      Node runningNode(IncArcIt) const {
685
	return INVALID;
686
      }
687

	
688
      template <typename Graph>
689
      struct Constraints {
690
	void constraints() {
691
	  checkConcept<IterableGraphComponent<>, Graph>();
692
	  checkConcept<MappableGraphComponent<>, Graph>();
693
	}
694
      };
695

	
696
    };
697

	
698
  }
699

	
700
}
701

	
702
#endif
Ignore white space 6 line context
1
/* -*- C++ -*-
2
 *
3
 * This file is a part of LEMON, a generic C++ optimization library
4
 *
5
 * Copyright (C) 2003-2007
6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
8
 *
9
 * Permission to use, modify and distribute this software is granted
10
 * provided that this copyright notice appears in all copies. For
11
 * precise terms see the accompanying LICENSE file.
12
 *
13
 * This software is provided "AS IS" with no warranty of any kind,
14
 * express or implied, and with no claim as to its suitability for any
15
 * purpose.
16
 *
17
 */
18

	
19
///\ingroup graph_concepts
20
///\file
21
///\brief The concept of graph components.
22

	
23

	
24
#ifndef LEMON_CONCEPT_GRAPH_COMPONENTS_H
25
#define LEMON_CONCEPT_GRAPH_COMPONENTS_H
26

	
27
#include <lemon/bits/invalid.h>
28
#include <lemon/concepts/maps.h>
29

	
30
#include <lemon/bits/alteration_notifier.h>
31

	
32
namespace lemon {
33
  namespace concepts {
34

	
35
    /// \brief Skeleton class for graph Node and Arc types
36
    ///
37
    /// This class describes the interface of Node and Arc (and Edge
38
    /// in undirected graphs) subtypes of graph types.
39
    ///
40
    /// \note This class is a template class so that we can use it to
41
    /// create graph skeleton classes. The reason for this is than Node
42
    /// and Arc types should \em not derive from the same base class.
43
    /// For Node you should instantiate it with character 'n' and for Arc
44
    /// with 'a'.
45

	
46
#ifndef DOXYGEN
47
    template <char _selector = '0'>
48
#endif
49
    class GraphItem {
50
    public:
51
      /// \brief Default constructor.
52
      ///      
53
      /// \warning The default constructor is not required to set
54
      /// the item to some well-defined value. So you should consider it
55
      /// as uninitialized.
56
      GraphItem() {}
57
      /// \brief Copy constructor.
58
      ///
59
      /// Copy constructor.
60
      ///
61
      GraphItem(const GraphItem &) {}
62
      /// \brief Invalid constructor \& conversion.
63
      ///
64
      /// This constructor initializes the item to be invalid.
65
      /// \sa Invalid for more details.
66
      GraphItem(Invalid) {}
67
      /// \brief Assign operator for nodes.
68
      ///
69
      /// The nodes are assignable. 
70
      ///
71
      GraphItem& operator=(GraphItem const&) { return *this; }
72
      /// \brief Equality operator.
73
      ///
74
      /// Two iterators are equal if and only if they represents the
75
      /// same node in the graph or both are invalid.
76
      bool operator==(GraphItem) const { return false; }
77
      /// \brief Inequality operator.
78
      ///
79
      /// \sa operator==(const Node& n)
80
      ///
81
      bool operator!=(GraphItem) const { return false; }
82

	
83
      /// \brief Artificial ordering operator.
84
      ///
85
      /// To allow the use of graph descriptors as key type in std::map or
86
      /// similar associative container we require this.
87
      ///
88
      /// \note This operator only have to define some strict ordering of
89
      /// the items; this order has nothing to do with the iteration
90
      /// ordering of the items.
91
      bool operator<(GraphItem) const { return false; }
92

	
93
      template<typename _GraphItem>
94
      struct Constraints {
95
	void constraints() {
96
	  _GraphItem i1;
97
	  _GraphItem i2 = i1;
98
	  _GraphItem i3 = INVALID;
99
	  
100
	  i1 = i2 = i3;
101

	
102
	  bool b;
103
	  //	  b = (ia == ib) && (ia != ib) && (ia < ib);
104
	  b = (ia == ib) && (ia != ib);
105
	  b = (ia == INVALID) && (ib != INVALID);
106
          b = (ia < ib);
107
	}
108

	
109
	const _GraphItem &ia;
110
	const _GraphItem &ib;
111
      };
112
    };
113

	
114
    /// \brief An empty base directed graph class.
115
    ///  
116
    /// This class provides the minimal set of features needed for a
117
    /// directed graph structure. All digraph concepts have to be
118
    /// conform to this base directed graph. It just provides types
119
    /// for nodes and arcs and functions to get the source and the
120
    /// target of the arcs.
121
    class BaseDigraphComponent {
122
    public:
123

	
124
      typedef BaseDigraphComponent Digraph;
125
      
126
      /// \brief Node class of the digraph.
127
      ///
128
      /// This class represents the Nodes of the digraph. 
129
      ///
130
      typedef GraphItem<'n'> Node;
131

	
132
      /// \brief Arc class of the digraph.
133
      ///
134
      /// This class represents the Arcs of the digraph. 
135
      ///
136
      typedef GraphItem<'e'> Arc;
137

	
138
      /// \brief Gives back the target node of an arc.
139
      ///
140
      /// Gives back the target node of an arc.
141
      ///
142
      Node target(const Arc&) const { return INVALID;}
143

	
144
      /// \brief Gives back the source node of an arc.
145
      ///
146
      /// Gives back the source node of an arc.
147
      ///
148
      Node source(const Arc&) const { return INVALID;}
149

	
150
      /// \brief Gives back the opposite node on the given arc.
151
      ///
152
      /// Gives back the opposite node on the given arc.
153
      Node oppositeNode(const Node&, const Arc&) const {
154
        return INVALID;
155
      }
156

	
157
      template <typename _Digraph>
158
      struct Constraints {
159
	typedef typename _Digraph::Node Node;
160
	typedef typename _Digraph::Arc Arc;
161
      
162
	void constraints() {
163
	  checkConcept<GraphItem<'n'>, Node>();
164
	  checkConcept<GraphItem<'a'>, Arc>();
165
	  {
166
	    Node n;
167
	    Arc e(INVALID);
168
	    n = digraph.source(e);
169
	    n = digraph.target(e);
170
            n = digraph.oppositeNode(n, e);
171
	  }      
172
	}
173
      
174
	const _Digraph& digraph;
175
      };
176
    };
177

	
178
    /// \brief An empty base undirected graph class.
179
    ///  
180
    /// This class provides the minimal set of features needed for an
181
    /// undirected graph structure. All undirected graph concepts have
182
    /// to be conform to this base graph. It just provides types for
183
    /// nodes, arcs and edges and functions to get the
184
    /// source and the target of the arcs and edges,
185
    /// conversion from arcs to edges and function to get
186
    /// both direction of the edges.
187
    class BaseGraphComponent : public BaseDigraphComponent {
188
    public:
189
      typedef BaseDigraphComponent::Node Node;
190
      typedef BaseDigraphComponent::Arc Arc;
191
      /// \brief Undirected arc class of the graph.
192
      ///
193
      /// This class represents the edges of the graph.
194
      /// The undirected graphs can be used as a directed graph which
195
      /// for each arc contains the opposite arc too so the graph is
196
      /// bidirected. The edge represents two opposite
197
      /// directed arcs.
198
      class Edge : public GraphItem<'u'> {
199
      public:
200
        typedef GraphItem<'u'> Parent;
201
        /// \brief Default constructor.
202
        ///      
203
        /// \warning The default constructor is not required to set
204
        /// the item to some well-defined value. So you should consider it
205
        /// as uninitialized.
206
        Edge() {}
207
        /// \brief Copy constructor.
208
        ///
209
        /// Copy constructor.
210
        ///
211
        Edge(const Edge &) : Parent() {}
212
        /// \brief Invalid constructor \& conversion.
213
        ///
214
        /// This constructor initializes the item to be invalid.
215
        /// \sa Invalid for more details.
216
        Edge(Invalid) {}
217
        /// \brief Converter from arc to edge.
218
        ///
219
        /// Besides the core graph item functionality each arc should
220
        /// be convertible to the represented edge. 
221
        Edge(const Arc&) {}
222
        /// \brief Assign arc to edge.
223
        ///
224
        /// Besides the core graph item functionality each arc should
225
        /// be convertible to the represented edge. 
226
        Edge& operator=(const Arc&) { return *this; }
227
      };
228

	
229
      /// \brief Returns the direction of the arc.
230
      ///
231
      /// Returns the direction of the arc. Each arc represents an
232
      /// edge with a direction. It gives back the
233
      /// direction.
234
      bool direction(const Arc&) const { return true; }
235

	
236
      /// \brief Returns the directed arc.
237
      ///
238
      /// Returns the directed arc from its direction and the
239
      /// represented edge.
240
      Arc direct(const Edge&, bool) const { return INVALID;} 
241

	
242
      /// \brief Returns the directed arc.
243
      ///
244
      /// Returns the directed arc from its source and the
245
      /// represented edge.
246
      Arc direct(const Edge&, const Node&) const { return INVALID;} 
247

	
248
      /// \brief Returns the opposite arc.
249
      ///
250
      /// Returns the opposite arc. It is the arc representing the
251
      /// same edge and has opposite direction.
252
      Arc oppositeArc(const Arc&) const { return INVALID;}
253

	
254
      /// \brief Gives back one ending of an edge.
255
      ///
256
      /// Gives back one ending of an edge.
257
      Node u(const Edge&) const { return INVALID;}
258

	
259
      /// \brief Gives back the other ending of an edge.
260
      ///
261
      /// Gives back the other ending of an edge.
262
      Node v(const Edge&) const { return INVALID;}
263
      
264
      template <typename _Graph>
265
      struct Constraints {
266
	typedef typename _Graph::Node Node;
267
	typedef typename _Graph::Arc Arc;
268
	typedef typename _Graph::Edge Edge;
269
      
270
	void constraints() {
271
          checkConcept<BaseDigraphComponent, _Graph>();
272
	  checkConcept<GraphItem<'u'>, Edge>();
273
	  {
274
	    Node n;
275
	    Edge ue(INVALID);
276
            Arc e;
277
	    n = graph.u(ue);
278
	    n = graph.v(ue);
279
            e = graph.direct(ue, true);
280
            e = graph.direct(ue, n);
281
            e = graph.oppositeArc(e);
282
            ue = e;
283
            bool d = graph.direction(e);
284
            ignore_unused_variable_warning(d);
285
	  }      
286
	}
287
      
288
	const _Graph& graph;
289
      };
290

	
291
    };
292

	
293
    /// \brief An empty idable base digraph class.
294
    ///  
295
    /// This class provides beside the core digraph features
296
    /// core id functions for the digraph structure.
297
    /// The most of the base digraphs should be conform to this concept.
298
    /// The id's are unique and immutable.
299
    template <typename _Base = BaseDigraphComponent>
300
    class IDableDigraphComponent : public _Base {
301
    public:
302

	
303
      typedef _Base Base;
304
      typedef typename Base::Node Node;
305
      typedef typename Base::Arc Arc;
306

	
307
      /// \brief Gives back an unique integer id for the Node. 
308
      ///
309
      /// Gives back an unique integer id for the Node. 
310
      ///
311
      int id(const Node&) const { return -1;}
312

	
313
      /// \brief Gives back the node by the unique id.
314
      ///
315
      /// Gives back the node by the unique id.
316
      /// If the digraph does not contain node with the given id
317
      /// then the result of the function is undetermined. 
318
      Node nodeFromId(int) const { return INVALID;}
319

	
320
      /// \brief Gives back an unique integer id for the Arc. 
321
      ///
322
      /// Gives back an unique integer id for the Arc. 
323
      ///
324
      int id(const Arc&) const { return -1;}
325

	
326
      /// \brief Gives back the arc by the unique id.
327
      ///
328
      /// Gives back the arc by the unique id.
329
      /// If the digraph does not contain arc with the given id
330
      /// then the result of the function is undetermined. 
331
      Arc arcFromId(int) const { return INVALID;}
332

	
333
      /// \brief Gives back an integer greater or equal to the maximum
334
      /// Node id.
335
      ///
336
      /// Gives back an integer greater or equal to the maximum Node
337
      /// id.
338
      int maxNodeId() const { return -1;}
339

	
340
      /// \brief Gives back an integer greater or equal to the maximum
341
      /// Arc id.
342
      ///
343
      /// Gives back an integer greater or equal to the maximum Arc
344
      /// id.
345
      int maxArcId() const { return -1;}
346

	
347
      template <typename _Digraph>
348
      struct Constraints {
349

	
350
	void constraints() {
351
	  checkConcept<Base, _Digraph >();
352
	  typename _Digraph::Node node;
353
	  int nid = digraph.id(node);
354
	  nid = digraph.id(node);
355
	  node = digraph.nodeFromId(nid);
356
	  typename _Digraph::Arc arc;
357
	  int eid = digraph.id(arc);
358
	  eid = digraph.id(arc);
359
	  arc = digraph.arcFromId(eid);
360

	
361
	  nid = digraph.maxNodeId();
362
	  ignore_unused_variable_warning(nid);
363
	  eid = digraph.maxArcId();
364
	  ignore_unused_variable_warning(eid);
365
	}
366

	
367
	const _Digraph& digraph;
368
      };
369
    };
370

	
371
    /// \brief An empty idable base undirected graph class.
372
    ///  
373
    /// This class provides beside the core undirected graph features
374
    /// core id functions for the undirected graph structure.  The
375
    /// most of the base undirected graphs should be conform to this
376
    /// concept.  The id's are unique and immutable.
377
    template <typename _Base = BaseGraphComponent>
378
    class IDableGraphComponent : public IDableDigraphComponent<_Base> {
379
    public:
380

	
381
      typedef _Base Base;
382
      typedef typename Base::Edge Edge;
383

	
384
      using IDableDigraphComponent<_Base>::id;
385

	
386
      /// \brief Gives back an unique integer id for the Edge. 
387
      ///
388
      /// Gives back an unique integer id for the Edge. 
389
      ///
390
      int id(const Edge&) const { return -1;}
391

	
392
      /// \brief Gives back the edge by the unique id.
393
      ///
394
      /// Gives back the edge by the unique id.  If the
395
      /// graph does not contain arc with the given id then the
396
      /// result of the function is undetermined.
397
      Edge edgeFromId(int) const { return INVALID;}
398

	
399
      /// \brief Gives back an integer greater or equal to the maximum
400
      /// Edge id.
401
      ///
402
      /// Gives back an integer greater or equal to the maximum Edge
403
      /// id.
404
      int maxEdgeId() const { return -1;}
405

	
406
      template <typename _Graph>
407
      struct Constraints {
408

	
409
	void constraints() {
410
	  checkConcept<Base, _Graph >();
411
	  checkConcept<IDableDigraphComponent<Base>, _Graph >();
412
	  typename _Graph::Edge edge;
413
	  int ueid = graph.id(edge);
414
	  ueid = graph.id(edge);
415
	  edge = graph.edgeFromId(ueid);
416
	  ueid = graph.maxEdgeId();
417
	  ignore_unused_variable_warning(ueid);
418
	}
419

	
420
	const _Graph& graph;
421
      };
422
    };
423

	
424
    /// \brief Skeleton class for graph NodeIt and ArcIt
425
    ///
426
    /// Skeleton class for graph NodeIt and ArcIt.
427
    ///
428
    template <typename _Graph, typename _Item>
429
    class GraphItemIt : public _Item {
430
    public:
431
      /// \brief Default constructor.
432
      ///
433
      /// @warning The default constructor sets the iterator
434
      /// to an undefined value.
435
      GraphItemIt() {}
436
      /// \brief Copy constructor.
437
      ///
438
      /// Copy constructor.
439
      ///
440
      GraphItemIt(const GraphItemIt& ) {}
441
      /// \brief Sets the iterator to the first item.
442
      ///
443
      /// Sets the iterator to the first item of \c the graph.
444
      ///
445
      explicit GraphItemIt(const _Graph&) {}
446
      /// \brief Invalid constructor \& conversion.
447
      ///
448
      /// This constructor initializes the item to be invalid.
449
      /// \sa Invalid for more details.
450
      GraphItemIt(Invalid) {}
451
      /// \brief Assign operator for items.
452
      ///
453
      /// The items are assignable. 
454
      ///
455
      GraphItemIt& operator=(const GraphItemIt&) { return *this; }      
456
      /// \brief Next item.
457
      /// 
458
      /// Assign the iterator to the next item.
459
      ///
460
      GraphItemIt& operator++() { return *this; }
461
      /// \brief Equality operator
462
      /// 
463
      /// Two iterators are equal if and only if they point to the
464
      /// same object or both are invalid.
465
      bool operator==(const GraphItemIt&) const { return true;}
466
      /// \brief Inequality operator
467
      ///	
468
      /// \sa operator==(Node n)
469
      ///
470
      bool operator!=(const GraphItemIt&) const { return true;}
471
      
472
      template<typename _GraphItemIt>
473
      struct Constraints {
474
	void constraints() {
475
	  _GraphItemIt it1(g);	
476
	  _GraphItemIt it2;
477

	
478
	  it2 = ++it1;
479
	  ++it2 = it1;
480
	  ++(++it1);
481

	
482
	  _Item bi = it1;
483
	  bi = it2;
484
	}
485
	_Graph& g;
486
      };
487
    };
488

	
489
    /// \brief Skeleton class for graph InArcIt and OutArcIt
490
    ///
491
    /// \note Because InArcIt and OutArcIt may not inherit from the same
492
    /// base class, the _selector is a additional template parameter. For 
493
    /// InArcIt you should instantiate it with character 'i' and for 
494
    /// OutArcIt with 'o'.
495
    template <typename _Graph,
496
	      typename _Item = typename _Graph::Arc,
497
              typename _Base = typename _Graph::Node, 
498
	      char _selector = '0'>
499
    class GraphIncIt : public _Item {
500
    public:
501
      /// \brief Default constructor.
502
      ///
503
      /// @warning The default constructor sets the iterator
504
      /// to an undefined value.
505
      GraphIncIt() {}
506
      /// \brief Copy constructor.
507
      ///
508
      /// Copy constructor.
509
      ///
510
      GraphIncIt(GraphIncIt const& gi) : _Item(gi) {}
511
      /// \brief Sets the iterator to the first arc incoming into or outgoing 
512
      /// from the node.
513
      ///
514
      /// Sets the iterator to the first arc incoming into or outgoing 
515
      /// from the node.
516
      ///
517
      explicit GraphIncIt(const _Graph&, const _Base&) {}
518
      /// \brief Invalid constructor \& conversion.
519
      ///
520
      /// This constructor initializes the item to be invalid.
521
      /// \sa Invalid for more details.
522
      GraphIncIt(Invalid) {}
523
      /// \brief Assign operator for iterators.
524
      ///
525
      /// The iterators are assignable. 
526
      ///
527
      GraphIncIt& operator=(GraphIncIt const&) { return *this; }      
528
      /// \brief Next item.
529
      ///
530
      /// Assign the iterator to the next item.
531
      ///
532
      GraphIncIt& operator++() { return *this; }
533

	
534
      /// \brief Equality operator
535
      ///
536
      /// Two iterators are equal if and only if they point to the
537
      /// same object or both are invalid.
538
      bool operator==(const GraphIncIt&) const { return true;}
539

	
540
      /// \brief Inequality operator
541
      ///
542
      /// \sa operator==(Node n)
543
      ///
544
      bool operator!=(const GraphIncIt&) const { return true;}
545

	
546
      template <typename _GraphIncIt>
547
      struct Constraints {
548
	void constraints() {
549
	  checkConcept<GraphItem<_selector>, _GraphIncIt>();
550
	  _GraphIncIt it1(graph, node);
551
	  _GraphIncIt it2;
552

	
553
	  it2 = ++it1;
554
	  ++it2 = it1;
555
	  ++(++it1);
556
	  _Item e = it1;
557
	  e = it2;
558

	
559
	}
560

	
561
	_Item arc;
562
	_Base node;
563
	_Graph graph;
564
	_GraphIncIt it;
565
      };
566
    };
567

	
568

	
569
    /// \brief An empty iterable digraph class.
570
    ///
571
    /// This class provides beside the core digraph features
572
    /// iterator based iterable interface for the digraph structure.
573
    /// This concept is part of the Digraph concept.
574
    template <typename _Base = BaseDigraphComponent>
575
    class IterableDigraphComponent : public _Base {
576

	
577
    public:
578
    
579
      typedef _Base Base;
580
      typedef typename Base::Node Node;
581
      typedef typename Base::Arc Arc;
582

	
583
      typedef IterableDigraphComponent Digraph;
584

	
585
      /// \name Base iteration
586
      /// 
587
      /// This interface provides functions for iteration on digraph items
588
      ///
589
      /// @{  
590

	
591
      /// \brief Gives back the first node in the iterating order.
592
      ///      
593
      /// Gives back the first node in the iterating order.
594
      ///     
595
      void first(Node&) const {}
596

	
597
      /// \brief Gives back the next node in the iterating order.
598
      ///
599
      /// Gives back the next node in the iterating order.
600
      ///     
601
      void next(Node&) const {}
602

	
603
      /// \brief Gives back the first arc in the iterating order.
604
      ///
605
      /// Gives back the first arc in the iterating order.
606
      ///     
607
      void first(Arc&) const {}
608

	
609
      /// \brief Gives back the next arc in the iterating order.
610
      ///
611
      /// Gives back the next arc in the iterating order.
612
      ///     
613
      void next(Arc&) const {}
614

	
615

	
616
      /// \brief Gives back the first of the arcs point to the given
617
      /// node.
618
      ///
619
      /// Gives back the first of the arcs point to the given node.
620
      ///     
621
      void firstIn(Arc&, const Node&) const {}
622

	
623
      /// \brief Gives back the next of the arcs points to the given
624
      /// node.
625
      ///
626
      /// Gives back the next of the arcs points to the given node.
627
      ///
628
      void nextIn(Arc&) const {}
629

	
630
      /// \brief Gives back the first of the arcs start from the
631
      /// given node.
632
      ///      
633
      /// Gives back the first of the arcs start from the given node.
634
      ///     
635
      void firstOut(Arc&, const Node&) const {}
636

	
637
      /// \brief Gives back the next of the arcs start from the given
638
      /// node.
639
      ///
640
      /// Gives back the next of the arcs start from the given node.
641
      ///     
642
      void nextOut(Arc&) const {}
643

	
644
      /// @}
645

	
646
      /// \name Class based iteration
647
      /// 
648
      /// This interface provides functions for iteration on digraph items
649
      ///
650
      /// @{
651

	
652
      /// \brief This iterator goes through each node.
653
      ///
654
      /// This iterator goes through each node.
655
      ///
656
      typedef GraphItemIt<Digraph, Node> NodeIt;
657

	
658
      /// \brief This iterator goes through each node.
659
      ///
660
      /// This iterator goes through each node.
661
      ///
662
      typedef GraphItemIt<Digraph, Arc> ArcIt;
663

	
664
      /// \brief This iterator goes trough the incoming arcs of a node.
665
      ///
666
      /// This iterator goes trough the \e inccoming arcs of a certain node
667
      /// of a digraph.
668
      typedef GraphIncIt<Digraph, Arc, Node, 'i'> InArcIt;
669

	
670
      /// \brief This iterator goes trough the outgoing arcs of a node.
671
      ///
672
      /// This iterator goes trough the \e outgoing arcs of a certain node
673
      /// of a digraph.
674
      typedef GraphIncIt<Digraph, Arc, Node, 'o'> OutArcIt;
675

	
676
      /// \brief The base node of the iterator.
677
      ///
678
      /// Gives back the base node of the iterator.
679
      /// It is always the target of the pointed arc.
680
      Node baseNode(const InArcIt&) const { return INVALID; }
681

	
682
      /// \brief The running node of the iterator.
683
      ///
684
      /// Gives back the running node of the iterator.
685
      /// It is always the source of the pointed arc.
686
      Node runningNode(const InArcIt&) const { return INVALID; }
687

	
688
      /// \brief The base node of the iterator.
689
      ///
690
      /// Gives back the base node of the iterator.
691
      /// It is always the source of the pointed arc.
692
      Node baseNode(const OutArcIt&) const { return INVALID; }
693

	
694
      /// \brief The running node of the iterator.
695
      ///
696
      /// Gives back the running node of the iterator.
697
      /// It is always the target of the pointed arc.
698
      Node runningNode(const OutArcIt&) const { return INVALID; }
699

	
700
      /// @}
701

	
702
      template <typename _Digraph> 
703
      struct Constraints {
704
	void constraints() {
705
	  checkConcept<Base, _Digraph>();
706

	
707
          {
708
            typename _Digraph::Node node(INVALID);      
709
            typename _Digraph::Arc arc(INVALID);
710
            {
711
              digraph.first(node);
712
              digraph.next(node);
713
            }
714
            {
715
              digraph.first(arc);
716
              digraph.next(arc);
717
            }
718
            {
719
              digraph.firstIn(arc, node);
720
              digraph.nextIn(arc);
721
            }
722
            {
723
              digraph.firstOut(arc, node);
724
              digraph.nextOut(arc);
725
            }
726
          }           
727

	
728
          {
729
            checkConcept<GraphItemIt<_Digraph, typename _Digraph::Arc>,
730
              typename _Digraph::ArcIt >();
731
            checkConcept<GraphItemIt<_Digraph, typename _Digraph::Node>,
732
              typename _Digraph::NodeIt >();
733
            checkConcept<GraphIncIt<_Digraph, typename _Digraph::Arc, 
734
              typename _Digraph::Node, 'i'>, typename _Digraph::InArcIt>();
735
            checkConcept<GraphIncIt<_Digraph, typename _Digraph::Arc, 
736
              typename _Digraph::Node, 'o'>, typename _Digraph::OutArcIt>();
737

	
738
            typename _Digraph::Node n;
739
            typename _Digraph::InArcIt ieit(INVALID);
740
            typename _Digraph::OutArcIt oeit(INVALID);
741
            n = digraph.baseNode(ieit);
742
            n = digraph.runningNode(ieit);
743
            n = digraph.baseNode(oeit);
744
            n = digraph.runningNode(oeit);
745
            ignore_unused_variable_warning(n);
746
          }
747
        }
748
	
749
	const _Digraph& digraph;
750
	
751
      };
752
    };
753

	
754
    /// \brief An empty iterable undirected graph class.
755
    ///
756
    /// This class provides beside the core graph features iterator
757
    /// based iterable interface for the undirected graph structure.
758
    /// This concept is part of the Graph concept.
759
    template <typename _Base = BaseGraphComponent>
760
    class IterableGraphComponent : public IterableDigraphComponent<_Base> {
761
    public:
762

	
763
      typedef _Base Base;
764
      typedef typename Base::Node Node;
765
      typedef typename Base::Arc Arc;
766
      typedef typename Base::Edge Edge;
767

	
768
    
769
      typedef IterableGraphComponent Graph;
770

	
771
      /// \name Base iteration
772
      /// 
773
      /// This interface provides functions for iteration on graph items
774
      /// @{  
775

	
776
      using IterableDigraphComponent<_Base>::first;
777
      using IterableDigraphComponent<_Base>::next;
778

	
779
      /// \brief Gives back the first edge in the iterating
780
      /// order.
781
      ///
782
      /// Gives back the first edge in the iterating order.
783
      ///     
784
      void first(Edge&) const {}
785

	
786
      /// \brief Gives back the next edge in the iterating
787
      /// order.
788
      ///
789
      /// Gives back the next edge in the iterating order.
790
      ///     
791
      void next(Edge&) const {}
792

	
793

	
794
      /// \brief Gives back the first of the edges from the
795
      /// given node.
796
      ///
797
      /// Gives back the first of the edges from the given
798
      /// node. The bool parameter gives back that direction which
799
      /// gives a good direction of the edge so the source of the
800
      /// directed arc is the given node.
801
      void firstInc(Edge&, bool&, const Node&) const {}
802

	
803
      /// \brief Gives back the next of the edges from the
804
      /// given node.
805
      ///
806
      /// Gives back the next of the edges from the given
807
      /// node. The bool parameter should be used as the \c firstInc()
808
      /// use it.
809
      void nextInc(Edge&, bool&) const {}
810

	
811
      using IterableDigraphComponent<_Base>::baseNode;
812
      using IterableDigraphComponent<_Base>::runningNode;
813

	
814
      /// @}
815

	
816
      /// \name Class based iteration
817
      /// 
818
      /// This interface provides functions for iteration on graph items
819
      ///
820
      /// @{
821

	
822
      /// \brief This iterator goes through each node.
823
      ///
824
      /// This iterator goes through each node.
825
      typedef GraphItemIt<Graph, Edge> EdgeIt;
826
      /// \brief This iterator goes trough the incident arcs of a
827
      /// node.
828
      ///
829
      /// This iterator goes trough the incident arcs of a certain
830
      /// node of a graph.
831
      typedef GraphIncIt<Graph, Edge, Node, 'u'> IncArcIt;
832
      /// \brief The base node of the iterator.
833
      ///
834
      /// Gives back the base node of the iterator.
835
      Node baseNode(const IncArcIt&) const { return INVALID; }
836

	
837
      /// \brief The running node of the iterator.
838
      ///
839
      /// Gives back the running node of the iterator.
840
      Node runningNode(const IncArcIt&) const { return INVALID; }
841

	
842
      /// @}
843

	
844
      template <typename _Graph> 
845
      struct Constraints {
846
	void constraints() {
847
	  checkConcept<IterableDigraphComponent<Base>, _Graph>();
848

	
849
          {
850
            typename _Graph::Node node(INVALID);
851
            typename _Graph::Edge edge(INVALID);
852
            bool dir;
853
            {
854
              graph.first(edge);
855
              graph.next(edge);
856
            }
857
            {
858
              graph.firstInc(edge, dir, node);
859
              graph.nextInc(edge, dir);
860
            }
861
            
862
          }	
863
  
864
          {
865
            checkConcept<GraphItemIt<_Graph, typename _Graph::Edge>,
866
              typename _Graph::EdgeIt >();
867
            checkConcept<GraphIncIt<_Graph, typename _Graph::Edge, 
868
              typename _Graph::Node, 'u'>, typename _Graph::IncArcIt>();
869
            
870
            typename _Graph::Node n;
871
            typename _Graph::IncArcIt ueit(INVALID);
872
            n = graph.baseNode(ueit);
873
            n = graph.runningNode(ueit);
874
          }
875
        }
876
	
877
	const _Graph& graph;
878
	
879
      };
880
    };
881

	
882
    /// \brief An empty alteration notifier digraph class.
883
    ///  
884
    /// This class provides beside the core digraph features alteration
885
    /// notifier interface for the digraph structure.  This implements
886
    /// an observer-notifier pattern for each digraph item. More
887
    /// obsevers can be registered into the notifier and whenever an
888
    /// alteration occured in the digraph all the observers will
889
    /// notified about it.
890
    template <typename _Base = BaseDigraphComponent>
891
    class AlterableDigraphComponent : public _Base {
892
    public:
893

	
894
      typedef _Base Base;
895
      typedef typename Base::Node Node;
896
      typedef typename Base::Arc Arc;
897

	
898

	
899
      /// The node observer registry.
900
      typedef AlterationNotifier<AlterableDigraphComponent, Node> 
901
      NodeNotifier;
902
      /// The arc observer registry.
903
      typedef AlterationNotifier<AlterableDigraphComponent, Arc> 
904
      ArcNotifier;
905
      
906
      /// \brief Gives back the node alteration notifier.
907
      ///
908
      /// Gives back the node alteration notifier.
909
      NodeNotifier& notifier(Node) const {
910
	return NodeNotifier();
911
      }
912
      
913
      /// \brief Gives back the arc alteration notifier.
914
      ///
915
      /// Gives back the arc alteration notifier.
916
      ArcNotifier& notifier(Arc) const {
917
	return ArcNotifier();
918
      }
919

	
920
      template <typename _Digraph> 
921
      struct Constraints {
922
	void constraints() {
923
	  checkConcept<Base, _Digraph>();
924
          typename _Digraph::NodeNotifier& nn 
925
            = digraph.notifier(typename _Digraph::Node());
926

	
927
          typename _Digraph::ArcNotifier& en 
928
            = digraph.notifier(typename _Digraph::Arc());
929
          
930
          ignore_unused_variable_warning(nn);
931
          ignore_unused_variable_warning(en);
932
	}
933
	
934
	const _Digraph& digraph;
935
	
936
      };
937
      
938
    };
939

	
940
    /// \brief An empty alteration notifier undirected graph class.
941
    ///  
942
    /// This class provides beside the core graph features alteration
943
    /// notifier interface for the graph structure.  This implements
944
    /// an observer-notifier pattern for each graph item. More
945
    /// obsevers can be registered into the notifier and whenever an
946
    /// alteration occured in the graph all the observers will
947
    /// notified about it.
948
    template <typename _Base = BaseGraphComponent>
949
    class AlterableGraphComponent : public AlterableDigraphComponent<_Base> {
950
    public:
951

	
952
      typedef _Base Base;
953
      typedef typename Base::Edge Edge;
954

	
955

	
956
      /// The arc observer registry.
957
      typedef AlterationNotifier<AlterableGraphComponent, Edge> 
958
      EdgeNotifier;
959
      
960
      /// \brief Gives back the arc alteration notifier.
961
      ///
962
      /// Gives back the arc alteration notifier.
963
      EdgeNotifier& notifier(Edge) const {
964
	return EdgeNotifier();
965
      }
966

	
967
      template <typename _Graph> 
968
      struct Constraints {
969
	void constraints() {
970
	  checkConcept<AlterableGraphComponent<Base>, _Graph>();
971
          typename _Graph::EdgeNotifier& uen 
972
            = graph.notifier(typename _Graph::Edge());
973
          ignore_unused_variable_warning(uen);
974
	}
975
	
976
	const _Graph& graph;
977
	
978
      };
979
      
980
    };
981

	
982
    /// \brief Class describing the concept of graph maps
983
    /// 
984
    /// This class describes the common interface of the graph maps
985
    /// (NodeMap, ArcMap), that is \ref maps-page "maps" which can be used to
986
    /// associate data to graph descriptors (nodes or arcs).
987
    template <typename _Graph, typename _Item, typename _Value>
988
    class GraphMap : public ReadWriteMap<_Item, _Value> {
989
    public:
990

	
991
      typedef ReadWriteMap<_Item, _Value> Parent;
992

	
993
      /// The graph type of the map.
994
      typedef _Graph Graph;
995
      /// The key type of the map.
996
      typedef _Item Key;
997
      /// The value type of the map.
998
      typedef _Value Value;
999

	
1000
      /// \brief Construct a new map.
1001
      ///
1002
      /// Construct a new map for the graph.
1003
      explicit GraphMap(const Graph&) {}
1004
      /// \brief Construct a new map with default value.
1005
      ///
1006
      /// Construct a new map for the graph and initalise the values.
1007
      GraphMap(const Graph&, const Value&) {}
1008
      /// \brief Copy constructor.
1009
      ///
1010
      /// Copy Constructor.
1011
      GraphMap(const GraphMap&) : Parent() {}
1012
      
1013
      /// \brief Assign operator.
1014
      ///
1015
      /// Assign operator. It does not mofify the underlying graph,
1016
      /// it just iterates on the current item set and set the  map
1017
      /// with the value returned by the assigned map. 
1018
      template <typename CMap>
1019
      GraphMap& operator=(const CMap&) { 
1020
        checkConcept<ReadMap<Key, Value>, CMap>();
1021
        return *this;
1022
      }
1023

	
1024
      template<typename _Map>
1025
      struct Constraints {
1026
	void constraints() {
1027
	  checkConcept<ReadWriteMap<Key, Value>, _Map >();
1028
	  // Construction with a graph parameter
1029
	  _Map a(g);
1030
	  // Constructor with a graph and a default value parameter
1031
	  _Map a2(g,t);
1032
	  // Copy constructor.
1033
	  _Map b(c);
1034
          
1035
          ReadMap<Key, Value> cmap;
1036
          b = cmap;
1037

	
1038
	  ignore_unused_variable_warning(a2);
1039
	  ignore_unused_variable_warning(b);
1040
	}
1041

	
1042
	const _Map &c;
1043
	const Graph &g;
1044
	const typename GraphMap::Value &t;
1045
      };
1046

	
1047
    };
1048

	
1049
    /// \brief An empty mappable digraph class.
1050
    ///
1051
    /// This class provides beside the core digraph features
1052
    /// map interface for the digraph structure.
1053
    /// This concept is part of the Digraph concept.
1054
    template <typename _Base = BaseDigraphComponent>
1055
    class MappableDigraphComponent : public _Base  {
1056
    public:
1057

	
1058
      typedef _Base Base;
1059
      typedef typename Base::Node Node;
1060
      typedef typename Base::Arc Arc;
1061

	
1062
      typedef MappableDigraphComponent Digraph;
1063

	
1064
      /// \brief ReadWrite map of the nodes.
1065
      ///
1066
      /// ReadWrite map of the nodes.
1067
      ///
1068
      template <typename _Value>
1069
      class NodeMap : public GraphMap<Digraph, Node, _Value> {
1070
      public:
1071
        typedef GraphMap<MappableDigraphComponent, Node, _Value> Parent;
1072

	
1073
	/// \brief Construct a new map.
1074
	///
1075
	/// Construct a new map for the digraph.
1076
	explicit NodeMap(const MappableDigraphComponent& digraph) 
1077
          : Parent(digraph) {}
1078

	
1079
	/// \brief Construct a new map with default value.
1080
	///
1081
	/// Construct a new map for the digraph and initalise the values.
1082
	NodeMap(const MappableDigraphComponent& digraph, const _Value& value)
1083
          : Parent(digraph, value) {}
1084

	
1085
	/// \brief Copy constructor.
1086
	///
1087
	/// Copy Constructor.
1088
	NodeMap(const NodeMap& nm) : Parent(nm) {}
1089

	
1090
	/// \brief Assign operator.
1091
	///
1092
	/// Assign operator.
1093
        template <typename CMap>
1094
        NodeMap& operator=(const CMap&) { 
1095
          checkConcept<ReadMap<Node, _Value>, CMap>();
1096
          return *this;
1097
        }
1098

	
1099
      };
1100

	
1101
      /// \brief ReadWrite map of the arcs.
1102
      ///
1103
      /// ReadWrite map of the arcs.
1104
      ///
1105
      template <typename _Value>
1106
      class ArcMap : public GraphMap<Digraph, Arc, _Value> {
1107
      public:
1108
        typedef GraphMap<MappableDigraphComponent, Arc, _Value> Parent;
1109

	
1110
	/// \brief Construct a new map.
1111
	///
1112
	/// Construct a new map for the digraph.
1113
	explicit ArcMap(const MappableDigraphComponent& digraph) 
1114
          : Parent(digraph) {}
1115

	
1116
	/// \brief Construct a new map with default value.
1117
	///
1118
	/// Construct a new map for the digraph and initalise the values.
1119
	ArcMap(const MappableDigraphComponent& digraph, const _Value& value)
1120
          : Parent(digraph, value) {}
1121

	
1122
	/// \brief Copy constructor.
1123
	///
1124
	/// Copy Constructor.
1125
	ArcMap(const ArcMap& nm) : Parent(nm) {}
1126

	
1127
	/// \brief Assign operator.
1128
	///
1129
	/// Assign operator.
1130
        template <typename CMap>
1131
        ArcMap& operator=(const CMap&) { 
1132
          checkConcept<ReadMap<Arc, _Value>, CMap>();
1133
          return *this;
1134
        }
1135

	
1136
      };
1137

	
1138

	
1139
      template <typename _Digraph>
1140
      struct Constraints {
1141

	
1142
	struct Dummy {
1143
	  int value;
1144
	  Dummy() : value(0) {}
1145
	  Dummy(int _v) : value(_v) {}
1146
	};
1147

	
1148
	void constraints() {
1149
	  checkConcept<Base, _Digraph>();
1150
	  { // int map test
1151
	    typedef typename _Digraph::template NodeMap<int> IntNodeMap;
1152
	    checkConcept<GraphMap<_Digraph, typename _Digraph::Node, int>, 
1153
	      IntNodeMap >();
1154
	  } { // bool map test
1155
	    typedef typename _Digraph::template NodeMap<bool> BoolNodeMap;
1156
	    checkConcept<GraphMap<_Digraph, typename _Digraph::Node, bool>,
1157
	      BoolNodeMap >();
1158
	  } { // Dummy map test
1159
	    typedef typename _Digraph::template NodeMap<Dummy> DummyNodeMap;
1160
	    checkConcept<GraphMap<_Digraph, typename _Digraph::Node, Dummy>,
1161
	      DummyNodeMap >();
1162
	  } 
1163

	
1164
	  { // int map test
1165
	    typedef typename _Digraph::template ArcMap<int> IntArcMap;
1166
	    checkConcept<GraphMap<_Digraph, typename _Digraph::Arc, int>,
1167
	      IntArcMap >();
1168
	  } { // bool map test
1169
	    typedef typename _Digraph::template ArcMap<bool> BoolArcMap;
1170
	    checkConcept<GraphMap<_Digraph, typename _Digraph::Arc, bool>,
1171
	      BoolArcMap >();
1172
	  } { // Dummy map test
1173
	    typedef typename _Digraph::template ArcMap<Dummy> DummyArcMap;
1174
	    checkConcept<GraphMap<_Digraph, typename _Digraph::Arc, Dummy>, 
1175
	      DummyArcMap >();
1176
	  } 
1177
	}
1178

	
1179
	_Digraph& digraph;
1180
      };
1181
    };
1182

	
1183
    /// \brief An empty mappable base bipartite graph class.
1184
    ///
1185
    /// This class provides beside the core graph features
1186
    /// map interface for the graph structure.
1187
    /// This concept is part of the Graph concept.
1188
    template <typename _Base = BaseGraphComponent>
1189
    class MappableGraphComponent : public MappableDigraphComponent<_Base>  {
1190
    public:
1191

	
1192
      typedef _Base Base;
1193
      typedef typename Base::Edge Edge;
1194

	
1195
      typedef MappableGraphComponent Graph;
1196

	
1197
      /// \brief ReadWrite map of the edges.
1198
      ///
1199
      /// ReadWrite map of the edges.
1200
      ///
1201
      template <typename _Value>
1202
      class EdgeMap : public GraphMap<Graph, Edge, _Value> {  
1203
      public:
1204
        typedef GraphMap<MappableGraphComponent, Edge, _Value> Parent;
1205

	
1206
	/// \brief Construct a new map.
1207
	///
1208
	/// Construct a new map for the graph.
1209
	explicit EdgeMap(const MappableGraphComponent& graph) 
1210
          : Parent(graph) {}
1211

	
1212
	/// \brief Construct a new map with default value.
1213
	///
1214
	/// Construct a new map for the graph and initalise the values.
1215
	EdgeMap(const MappableGraphComponent& graph, const _Value& value)
1216
          : Parent(graph, value) {}
1217

	
1218
	/// \brief Copy constructor.
1219
	///
1220
	/// Copy Constructor.
1221
	EdgeMap(const EdgeMap& nm) : Parent(nm) {}
1222

	
1223
	/// \brief Assign operator.
1224
	///
1225
	/// Assign operator.
1226
        template <typename CMap>
1227
        EdgeMap& operator=(const CMap&) { 
1228
          checkConcept<ReadMap<Edge, _Value>, CMap>();
1229
          return *this;
1230
        }
1231

	
1232
      };
1233

	
1234

	
1235
      template <typename _Graph>
1236
      struct Constraints {
1237

	
1238
	struct Dummy {
1239
	  int value;
1240
	  Dummy() : value(0) {}
1241
	  Dummy(int _v) : value(_v) {}
1242
	};
1243

	
1244
	void constraints() {
1245
	  checkConcept<MappableGraphComponent<Base>, _Graph>();
1246

	
1247
	  { // int map test
1248
	    typedef typename _Graph::template EdgeMap<int> IntEdgeMap;
1249
	    checkConcept<GraphMap<_Graph, typename _Graph::Edge, int>,
1250
	      IntEdgeMap >();
1251
	  } { // bool map test
1252
	    typedef typename _Graph::template EdgeMap<bool> BoolEdgeMap;
1253
	    checkConcept<GraphMap<_Graph, typename _Graph::Edge, bool>,
1254
	      BoolEdgeMap >();
1255
	  } { // Dummy map test
1256
	    typedef typename _Graph::template EdgeMap<Dummy> DummyEdgeMap;
1257
	    checkConcept<GraphMap<_Graph, typename _Graph::Edge, Dummy>, 
1258
	      DummyEdgeMap >();
1259
	  } 
1260
	}
1261

	
1262
	_Graph& graph;
1263
      };
1264
    };
1265

	
1266
    /// \brief An empty extendable digraph class.
1267
    ///
1268
    /// This class provides beside the core digraph features digraph
1269
    /// extendable interface for the digraph structure.  The main
1270
    /// difference between the base and this interface is that the
1271
    /// digraph alterations should handled already on this level.
1272
    template <typename _Base = BaseDigraphComponent>
1273
    class ExtendableDigraphComponent : public _Base {
1274
    public:
1275
      typedef _Base Base;
1276

	
1277
      typedef typename _Base::Node Node;
1278
      typedef typename _Base::Arc Arc;
1279

	
1280
      /// \brief Adds a new node to the digraph.
1281
      ///
1282
      /// Adds a new node to the digraph.
1283
      ///
1284
      Node addNode() {
1285
	return INVALID;
1286
      }
1287
    
1288
      /// \brief Adds a new arc connects the given two nodes.
1289
      ///
1290
      /// Adds a new arc connects the the given two nodes.
1291
      Arc addArc(const Node&, const Node&) {
1292
	return INVALID;
1293
      }
1294

	
1295
      template <typename _Digraph>
1296
      struct Constraints {
1297
	void constraints() {
1298
          checkConcept<Base, _Digraph>();
1299
	  typename _Digraph::Node node_a, node_b;
1300
	  node_a = digraph.addNode();
1301
	  node_b = digraph.addNode();
1302
	  typename _Digraph::Arc arc;
1303
	  arc = digraph.addArc(node_a, node_b);
1304
	}
1305

	
1306
	_Digraph& digraph;
1307
      };
1308
    };
1309

	
1310
    /// \brief An empty extendable base undirected graph class.
1311
    ///
1312
    /// This class provides beside the core undirected graph features
1313
    /// core undircted graph extend interface for the graph structure.
1314
    /// The main difference between the base and this interface is
1315
    /// that the graph alterations should handled already on this
1316
    /// level.
1317
    template <typename _Base = BaseGraphComponent>
1318
    class ExtendableGraphComponent : public _Base {
1319
    public:
1320

	
1321
      typedef _Base Base;
1322
      typedef typename _Base::Node Node;
1323
      typedef typename _Base::Edge Edge;
1324

	
1325
      /// \brief Adds a new node to the graph.
1326
      ///
1327
      /// Adds a new node to the graph.
1328
      ///
1329
      Node addNode() {
1330
	return INVALID;
1331
      }
1332
    
1333
      /// \brief Adds a new arc connects the given two nodes.
1334
      ///
1335
      /// Adds a new arc connects the the given two nodes.
1336
      Edge addArc(const Node&, const Node&) {
1337
	return INVALID;
1338
      }
1339

	
1340
      template <typename _Graph>
1341
      struct Constraints {
1342
	void constraints() {
1343
	  checkConcept<Base, _Graph>();
1344
	  typename _Graph::Node node_a, node_b;
1345
	  node_a = graph.addNode();
1346
	  node_b = graph.addNode();
1347
	  typename _Graph::Edge edge;
1348
	  edge = graph.addEdge(node_a, node_b);
1349
	}
1350

	
1351
	_Graph& graph;
1352
      };
1353
    };
1354

	
1355
    /// \brief An empty erasable digraph class.
1356
    ///  
1357
    /// This class provides beside the core digraph features core erase
1358
    /// functions for the digraph structure. The main difference between
1359
    /// the base and this interface is that the digraph alterations
1360
    /// should handled already on this level.
1361
    template <typename _Base = BaseDigraphComponent>
1362
    class ErasableDigraphComponent : public _Base {
1363
    public:
1364

	
1365
      typedef _Base Base;
1366
      typedef typename Base::Node Node;
1367
      typedef typename Base::Arc Arc;
1368

	
1369
      /// \brief Erase a node from the digraph.
1370
      ///
1371
      /// Erase a node from the digraph. This function should 
1372
      /// erase all arcs connecting to the node.
1373
      void erase(const Node&) {}    
1374

	
1375
      /// \brief Erase an arc from the digraph.
1376
      ///
1377
      /// Erase an arc from the digraph.
1378
      ///
1379
      void erase(const Arc&) {}
1380

	
1381
      template <typename _Digraph>
1382
      struct Constraints {
1383
	void constraints() {
1384
          checkConcept<Base, _Digraph>();
1385
	  typename _Digraph::Node node;
1386
	  digraph.erase(node);
1387
	  typename _Digraph::Arc arc;
1388
	  digraph.erase(arc);
1389
	}
1390

	
1391
	_Digraph& digraph;
1392
      };
1393
    };
1394

	
1395
    /// \brief An empty erasable base undirected graph class.
1396
    ///  
1397
    /// This class provides beside the core undirected graph features
1398
    /// core erase functions for the undirceted graph structure. The
1399
    /// main difference between the base and this interface is that
1400
    /// the graph alterations should handled already on this level.
1401
    template <typename _Base = BaseGraphComponent>
1402
    class ErasableGraphComponent : public _Base {
1403
    public:
1404

	
1405
      typedef _Base Base;
1406
      typedef typename Base::Node Node;
1407
      typedef typename Base::Edge Edge;
1408

	
1409
      /// \brief Erase a node from the graph.
1410
      ///
1411
      /// Erase a node from the graph. This function should erase
1412
      /// arcs connecting to the node.
1413
      void erase(const Node&) {}    
1414

	
1415
      /// \brief Erase an arc from the graph.
1416
      ///
1417
      /// Erase an arc from the graph.
1418
      ///
1419
      void erase(const Edge&) {}
1420

	
1421
      template <typename _Graph>
1422
      struct Constraints {
1423
	void constraints() {
1424
          checkConcept<Base, _Graph>();
1425
	  typename _Graph::Node node;
1426
	  graph.erase(node);
1427
	  typename _Graph::Arc arc;
1428
	  graph.erase(arc);
1429
	}
1430

	
1431
	_Graph& graph;
1432
      };
1433
    };
1434

	
1435
    /// \brief An empty clearable base digraph class.
1436
    ///
1437
    /// This class provides beside the core digraph features core clear
1438
    /// functions for the digraph structure. The main difference between
1439
    /// the base and this interface is that the digraph alterations
1440
    /// should handled already on this level.
1441
    template <typename _Base = BaseDigraphComponent>
1442
    class ClearableDigraphComponent : public _Base {
1443
    public:
1444

	
1445
      typedef _Base Base;
1446

	
1447
      /// \brief Erase all nodes and arcs from the digraph.
1448
      ///
1449
      /// Erase all nodes and arcs from the digraph.
1450
      ///
1451
      void clear() {}    
1452

	
1453
      template <typename _Digraph>
1454
      struct Constraints {
1455
	void constraints() {
1456
          checkConcept<Base, _Digraph>();
1457
	  digraph.clear();
1458
	}
1459

	
1460
	_Digraph digraph;
1461
      };
1462
    };
1463

	
1464
    /// \brief An empty clearable base undirected graph class.
1465
    ///
1466
    /// This class provides beside the core undirected graph features
1467
    /// core clear functions for the undirected graph structure. The
1468
    /// main difference between the base and this interface is that
1469
    /// the graph alterations should handled already on this level.
1470
    template <typename _Base = BaseGraphComponent>
1471
    class ClearableGraphComponent : public ClearableDigraphComponent<_Base> {
1472
    public:
1473

	
1474
      typedef _Base Base;
1475

	
1476
      template <typename _Graph>
1477
      struct Constraints {
1478
	void constraints() {
1479
          checkConcept<ClearableGraphComponent<Base>, _Graph>();
1480
	}
1481

	
1482
	_Graph graph;
1483
      };
1484
    };
1485

	
1486
  }
1487

	
1488
}
1489

	
1490
#endif
Ignore white space 6 line context
1
/* -*- C++ -*-
2
 *
3
 * This file is a part of LEMON, a generic C++ optimization library
4
 *
5
 * Copyright (C) 2003-2007
6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
8
 *
9
 * Permission to use, modify and distribute this software is granted
10
 * provided that this copyright notice appears in all copies. For
11
 * precise terms see the accompanying LICENSE file.
12
 *
13
 * This software is provided "AS IS" with no warranty of any kind,
14
 * express or implied, and with no claim as to its suitability for any
15
 * purpose.
16
 *
17
 */
18

	
19
#include <iostream>
20
#include <vector>
21

	
22
#include <lemon/concepts/digraph.h>
23
#include <lemon/list_graph.h>
24
//#include <lemon/smart_graph.h>
25
//#include <lemon/full_graph.h>
26
//#include <lemon/hypercube_graph.h>
27

	
28
#include "test_tools.h"
29
#include "digraph_test.h"
30
#include "map_test.h"
31

	
32

	
33
using namespace lemon;
34
using namespace lemon::concepts;
35

	
36

	
37
int main() {
38
  { // checking digraph components
39
    checkConcept<BaseDigraphComponent, BaseDigraphComponent >();
40

	
41
    checkConcept<IDableDigraphComponent<>, 
42
      IDableDigraphComponent<> >();
43

	
44
    checkConcept<IterableDigraphComponent<>, 
45
      IterableDigraphComponent<> >();
46

	
47
    checkConcept<MappableDigraphComponent<>, 
48
      MappableDigraphComponent<> >();
49

	
50
  }
51
  { // checking skeleton digraphs
52
    checkConcept<Digraph, Digraph>();
53
  }
54
  { // checking list digraph
55
    checkConcept<Digraph, ListDigraph >();
56
    checkConcept<AlterableDigraphComponent<>, ListDigraph>();
57
    checkConcept<ExtendableDigraphComponent<>, ListDigraph>();
58
    checkConcept<ClearableDigraphComponent<>, ListDigraph>();
59
    checkConcept<ErasableDigraphComponent<>, ListDigraph>();
60

	
61
    checkDigraph<ListDigraph>();
62
    checkGraphNodeMap<ListDigraph>();
63
    checkGraphArcMap<ListDigraph>();
64
  }
65
//   { // checking smart digraph
66
//     checkConcept<Digraph, SmartDigraph >();
67

	
68
//     checkDigraph<SmartDigraph>();
69
//     checkDigraphNodeMap<SmartDigraph>();
70
//     checkDigraphArcMap<SmartDigraph>();
71
//   }
72
//   { // checking full digraph
73
//     checkConcept<Digraph, FullDigraph >();
74
//   }
75
//   { // checking full digraph
76
//     checkConcept<Digraph, HyperCubeDigraph >();
77
//   }
78

	
79
  std::cout << __FILE__ ": All tests passed.\n";
80

	
81
  return 0;
82
}
Ignore white space 6 line context
1
/* -*- C++ -*-
2
 *
3
 * This file is a part of LEMON, a generic C++ optimization library
4
 *
5
 * Copyright (C) 2003-2007
6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
8
 *
9
 * Permission to use, modify and distribute this software is granted
10
 * provided that this copyright notice appears in all copies. For
11
 * precise terms see the accompanying LICENSE file.
12
 *
13
 * This software is provided "AS IS" with no warranty of any kind,
14
 * express or implied, and with no claim as to its suitability for any
15
 * purpose.
16
 *
17
 */
18

	
19
#ifndef LEMON_TEST_GRAPH_TEST_H
20
#define LEMON_TEST_GRAPH_TEST_H
21

	
22
//#include <lemon/graph_utils.h>
23
#include "test_tools.h"
24

	
25
//! \ingroup misc
26
//! \file
27
//! \brief Some utility and test cases to test digraph classes.
28
namespace lemon {
29

	
30
  ///Structure returned by \ref addPetersen().
31

	
32
  ///Structure returned by \ref addPetersen().
33
  ///
34
  template<class Digraph> 
35
  struct PetStruct
36
  {
37
    ///Vector containing the outer nodes.
38
    std::vector<typename Digraph::Node> outer;
39
    ///Vector containing the inner nodes.
40
    std::vector<typename Digraph::Node> inner;
41
    ///Vector containing the edges of the inner circle.
42
    std::vector<typename Digraph::Arc> incir;
43
    ///Vector containing the edges of the outer circle.
44
    std::vector<typename Digraph::Arc> outcir;
45
    ///Vector containing the chord edges.
46
    std::vector<typename Digraph::Arc> chords;
47
  };
48

	
49

	
50

	
51
  ///Adds a Petersen graph to \c G.
52

	
53
  ///Adds a Petersen graph to \c G.
54
  ///\return The nodes and edges of the generated graph.
55

	
56
  template<typename Digraph>
57
  PetStruct<Digraph> addPetersen(Digraph &G,int num = 5)
58
  {
59
    PetStruct<Digraph> n;
60

	
61
    for(int i=0;i<num;i++) {
62
      n.outer.push_back(G.addNode());
63
      n.inner.push_back(G.addNode());
64
    }
65

	
66
    for(int i=0;i<num;i++) {
67
      n.chords.push_back(G.addArc(n.outer[i],n.inner[i]));
68
      n.outcir.push_back(G.addArc(n.outer[i],n.outer[(i+1) % num]));
69
      n.incir.push_back(G.addArc(n.inner[i],n.inner[(i+2) % num]));
70
    }
71
    return n;
72
  }
73

	
74
  /// \brief Adds to the digraph the reverse pair of all edges.
75
  ///
76
  /// Adds to the digraph the reverse pair of all edges.
77
  ///
78
  template<class Digraph> 
79
  void bidirDigraph(Digraph &G)
80
  {
81
    typedef typename Digraph::Arc Arc;
82
    typedef typename Digraph::ArcIt ArcIt;
83
  
84
    std::vector<Arc> ee;
85
  
86
    for(ArcIt e(G);e!=INVALID;++e) ee.push_back(e);
87

	
88
    for(typename std::vector<Arc>::iterator p=ee.begin();p!=ee.end();p++)
89
      G.addArc(G.target(*p),G.source(*p));
90
  }
91

	
92

	
93
  /// \brief Checks the bidirectioned Petersen graph.
94
  ///
95
  ///  Checks the bidirectioned Petersen graph.
96
  ///
97
  template<class Digraph> 
98
  void checkBidirPetersen(Digraph &G, int num = 5)
99
  {
100
    typedef typename Digraph::Node Node;
101

	
102
    typedef typename Digraph::ArcIt ArcIt;
103
    typedef typename Digraph::NodeIt NodeIt;
104

	
105
    checkDigraphNodeList(G, 2 * num);
106
    checkDigraphArcList(G, 6 * num);
107

	
108
    for(NodeIt n(G);n!=INVALID;++n) {
109
      checkDigraphInArcList(G, n, 3);
110
      checkDigraphOutArcList(G, n, 3);
111
    }  
112
  }
113

	
114
  template<class Digraph> void checkDigraphNodeList(Digraph &G, int nn)
115
  {
116
    typename Digraph::NodeIt n(G);
117
    for(int i=0;i<nn;i++) {
118
      check(n!=INVALID,"Wrong Node list linking.");
119
      ++n;
120
    }
121
    check(n==INVALID,"Wrong Node list linking.");
122
  }
123

	
124
  template<class Digraph>
125
  void checkDigraphArcList(Digraph &G, int nn)
126
  {
127
    typedef typename Digraph::ArcIt ArcIt;
128

	
129
    ArcIt e(G);
130
    for(int i=0;i<nn;i++) {
131
      check(e!=INVALID,"Wrong Arc list linking.");
132
      ++e;
133
    }
134
    check(e==INVALID,"Wrong Arc list linking.");
135
  }
136

	
137
  template<class Digraph> 
138
  void checkDigraphOutArcList(Digraph &G, typename Digraph::Node n, int nn)
139
  {
140
    typename Digraph::OutArcIt e(G,n);
141
    for(int i=0;i<nn;i++) {
142
      check(e!=INVALID,"Wrong OutArc list linking.");
143
      check(n==G.source(e), "Wrong OutArc list linking.");
144
      ++e;
145
    }
146
    check(e==INVALID,"Wrong OutArc list linking.");
147
  }
148

	
149
  template<class Digraph> void 
150
  checkDigraphInArcList(Digraph &G, typename Digraph::Node n, int nn)
151
  {
152
    typename Digraph::InArcIt e(G,n);
153
    for(int i=0;i<nn;i++) {
154
      check(e!=INVALID,"Wrong InArc list linking.");
155
      check(n==G.target(e), "Wrong InArc list linking.");
156
      ++e;
157
    }
158
    check(e==INVALID,"Wrong InArc list linking.");
159
  }
160

	
161
  template <class Digraph> 
162
  void checkDigraph() {
163
    const int num = 5;
164
    Digraph G;
165
    addPetersen(G, num);
166
    bidirDigraph(G);
167
    checkBidirPetersen(G, num);
168
  }
169

	
170
  template <class Digraph> 
171
  void checkDigraphIterators(const Digraph& digraph) {
172
    typedef typename Digraph::Node Node;
173
    typedef typename Digraph::NodeIt NodeIt;
174
    typedef typename Digraph::Arc Arc;
175
    typedef typename Digraph::ArcIt ArcIt;
176
    typedef typename Digraph::InArcIt InArcIt;
177
    typedef typename Digraph::OutArcIt OutArcIt;
178
    //    typedef ConArcIt<Digraph> ConArcIt;
179
  }
180

	
181
  ///\file
182
  ///\todo Check target(), source() as well;
183

	
184
  
185
} //namespace lemon
186

	
187

	
188
#endif
Ignore white space 6 line context
1
/* -*- C++ -*-
2
 *
3
 * This file is a part of LEMON, a generic C++ optimization library
4
 *
5
 * Copyright (C) 2003-2007
6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
8
 *
9
 * Permission to use, modify and distribute this software is granted
10
 * provided that this copyright notice appears in all copies. For
11
 * precise terms see the accompanying LICENSE file.
12
 *
13
 * This software is provided "AS IS" with no warranty of any kind,
14
 * express or implied, and with no claim as to its suitability for any
15
 * purpose.
16
 *
17
 */
18

	
19
#include <lemon/concepts/graph.h>
20
#include <lemon/list_graph.h>
21
// #include <lemon/smart_graph.h>
22
// #include <lemon/full_graph.h>
23
// #include <lemon/grid_graph.h>
24

	
25
//#include <lemon/graph_utils.h>
26

	
27
#include "test_tools.h"
28

	
29

	
30
using namespace lemon;
31
using namespace lemon::concepts;
32

	
33
void check_concepts() {
34

	
35
  { // checking digraph components
36
    checkConcept<BaseGraphComponent, BaseGraphComponent >();
37

	
38
    checkConcept<IDableGraphComponent<>, 
39
      IDableGraphComponent<> >();
40

	
41
    checkConcept<IterableGraphComponent<>, 
42
      IterableGraphComponent<> >();
43

	
44
    checkConcept<MappableGraphComponent<>, 
45
      MappableGraphComponent<> >();
46

	
47
  }
48
  {
49
    checkConcept<Graph, ListGraph>();    
50
//     checkConcept<Graph, SmartGraph>();    
51
//     checkConcept<Graph, FullGraph>();    
52
//     checkConcept<Graph, Graph>();    
53
//     checkConcept<Graph, GridGraph>();
54
  }
55
}
56

	
57
template <typename Graph>
58
void check_item_counts(Graph &g, int n, int e) {
59
  int nn = 0;
60
  for (typename Graph::NodeIt it(g); it != INVALID; ++it) {
61
    ++nn;
62
  }
63

	
64
  check(nn == n, "Wrong node number.");
65
  //  check(countNodes(g) == n, "Wrong node number.");
66

	
67
  int ee = 0;
68
  for (typename Graph::ArcIt it(g); it != INVALID; ++it) {
69
    ++ee;
70
  }
71

	
72
  check(ee == 2*e, "Wrong arc number.");
73
  //  check(countArcs(g) == 2*e, "Wrong arc number.");
74

	
75
  int uee = 0;
76
  for (typename Graph::EdgeIt it(g); it != INVALID; ++it) {
77
    ++uee;
78
  }
79

	
80
  check(uee == e, "Wrong edge number.");
81
  //  check(countEdges(g) == e, "Wrong edge number.");
82
}
83

	
84
template <typename Graph>
85
void print_items(Graph &g) {
86

	
87
  typedef typename Graph::NodeIt NodeIt;
88
  typedef typename Graph::EdgeIt EdgeIt;
89
  typedef typename Graph::ArcIt ArcIt;
90

	
91
  std::cout << "Nodes" << std::endl;
92
  int i=0;
93
  for(NodeIt it(g); it!=INVALID; ++it, ++i) {
94
    std::cout << "  " << i << ": " << g.id(it) << std::endl;
95
  }
96

	
97
  std::cout << "Edge" << std::endl;
98
  i=0;
99
  for(EdgeIt it(g); it!=INVALID; ++it, ++i) {
100
    std::cout << "  " << i << ": " << g.id(it) 
101
	 << " (" << g.id(g.source(it)) << ", " << g.id(g.target(it)) 
102
	 << ")" << std::endl;
103
  }
104

	
105
  std::cout << "Arc" << std::endl;
106
  i=0;
107
  for(ArcIt it(g); it!=INVALID; ++it, ++i) {
108
    std::cout << "  " << i << ": " << g.id(it)
109
	 << " (" << g.id(g.source(it)) << ", " << g.id(g.target(it)) 
110
	 << ")" << std::endl;
111
  }
112

	
113
}
114

	
115
template <typename Graph>
116
void check_graph() {
117

	
118
  typedef typename Graph::Node Node;
119
  typedef typename Graph::Edge Edge;
120
  typedef typename Graph::Arc Arc;
121
  typedef typename Graph::NodeIt NodeIt;
122
  typedef typename Graph::EdgeIt EdgeIt;
123
  typedef typename Graph::ArcIt ArcIt;
124

	
125
  Graph g;
126

	
127
  check_item_counts(g,0,0);
128

	
129
  Node
130
    n1 = g.addNode(),
131
    n2 = g.addNode(),
132
    n3 = g.addNode();
133

	
134
  Edge
135
    e1 = g.addEdge(n1, n2),
136
    e2 = g.addEdge(n2, n3);
137

	
138
  // print_items(g);
139

	
140
  check_item_counts(g,3,2);
141
}
142

	
143
// void checkGridGraph(const GridGraph& g, int w, int h) {
144
//   check(g.width() == w, "Wrong width");
145
//   check(g.height() == h, "Wrong height");
146

	
147
//   for (int i = 0; i < w; ++i) {
148
//     for (int j = 0; j < h; ++j) {
149
//       check(g.col(g(i, j)) == i, "Wrong col");
150
//       check(g.row(g(i, j)) == j, "Wrong row");
151
//     }
152
//   }
153
  
154
//   for (int i = 0; i < w; ++i) {
155
//     for (int j = 0; j < h - 1; ++j) {
156
//       check(g.source(g.down(g(i, j))) == g(i, j), "Wrong down");
157
//       check(g.target(g.down(g(i, j))) == g(i, j + 1), "Wrong down");
158
//     }
159
//     check(g.down(g(i, h - 1)) == INVALID, "Wrong down");
160
//   }
161

	
162
//   for (int i = 0; i < w; ++i) {
163
//     for (int j = 1; j < h; ++j) {
164
//       check(g.source(g.up(g(i, j))) == g(i, j), "Wrong up");
165
//       check(g.target(g.up(g(i, j))) == g(i, j - 1), "Wrong up");
166
//     }
167
//     check(g.up(g(i, 0)) == INVALID, "Wrong up");
168
//   }
169

	
170
//   for (int j = 0; j < h; ++j) {
171
//     for (int i = 0; i < w - 1; ++i) {
172
//       check(g.source(g.right(g(i, j))) == g(i, j), "Wrong right");
173
//       check(g.target(g.right(g(i, j))) == g(i + 1, j), "Wrong right");      
174
//     }
175
//     check(g.right(g(w - 1, j)) == INVALID, "Wrong right");    
176
//   }
177

	
178
//   for (int j = 0; j < h; ++j) {
179
//     for (int i = 1; i < w; ++i) {
180
//       check(g.source(g.left(g(i, j))) == g(i, j), "Wrong left");
181
//       check(g.target(g.left(g(i, j))) == g(i - 1, j), "Wrong left");      
182
//     }
183
//     check(g.left(g(0, j)) == INVALID, "Wrong left");    
184
//   }
185
// }
186

	
187
int main() {
188
  check_concepts();
189

	
190
  check_graph<ListGraph>();
191
//  check_graph<SmartGraph>();
192

	
193
//   {
194
//     FullGraph g(5);
195
//     check_item_counts(g, 5, 10);
196
//   }
197

	
198
//   {
199
//     GridGraph g(5, 6);
200
//     check_item_counts(g, 30, 49);
201
//     checkGridGraph(g, 5, 6);
202
//   }
203

	
204
  std::cout << __FILE__ ": All tests passed.\n";
205

	
206
  return 0;
207
}
Ignore white space 12 line context
1
/* -*- C++ -*-
2
 *
3
 * This file is a part of LEMON, a generic C++ optimization library
4
 *
5
 * Copyright (C) 2003-2007
6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
8
 *
9
 * Permission to use, modify and distribute this software is granted
10
 * provided that this copyright notice appears in all copies. For
11
 * precise terms see the accompanying LICENSE file.
12
 *
13
 * This software is provided "AS IS" with no warranty of any kind,
14
 * express or implied, and with no claim as to its suitability for any
15
 * purpose.
16
 *
17
 */
18

	
19
#ifndef LEMON_TEST_MAP_TEST_H
20
#define LEMON_TEST_MAP_TEST_H
21

	
22

	
23
#include <vector>
24
#include <lemon/maps.h>
25

	
26
#include "test_tools.h"
27

	
28

	
29
//! \ingroup misc
30
//! \file
31
//! \brief Some utilities to test map classes.
32

	
33
namespace lemon {
34

	
35

	
36

	
37
  template <typename Graph>
38
  void checkGraphNodeMap() {
39
    Graph graph;
40
    const int num = 16;
41
    
42
    typedef typename Graph::Node Node;
43

	
44
    std::vector<Node> nodes;
45
    for (int i = 0; i < num; ++i) {
46
      nodes.push_back(graph.addNode());      
47
    }
48
    typedef typename Graph::template NodeMap<int> IntNodeMap;
49
    IntNodeMap map(graph, 42);
50
    for (int i = 0; i < int(nodes.size()); ++i) {
51
      check(map[nodes[i]] == 42, "Wrong map constructor.");      
52
    }
53
    for (int i = 0; i < num; ++i) {
54
      nodes.push_back(graph.addNode());
55
      map[nodes.back()] = 23;
56
    }
57
    map = constMap<Node>(12);
58
    for (int i = 0; i < int(nodes.size()); ++i) {
59
      check(map[nodes[i]] == 12, "Wrong map constructor.");      
60
    }    
61
    graph.clear();
62
    nodes.clear();
63
  }
64

	
65
  template <typename Graph>
66
  void checkGraphArcMap() {
67
    Graph graph;
68
    const int num = 16;
69
    
70
    typedef typename Graph::Node Node;
71
    typedef typename Graph::Arc Arc;
72
    
73
    std::vector<Node> nodes;
74
    for (int i = 0; i < num; ++i) {
75
      nodes.push_back(graph.addNode());
76
    }
77
    
78
    std::vector<Arc> edges;
79
    for (int i = 0; i < num; ++i) {
80
      for (int j = 0; j < i; ++j) {
81
	edges.push_back(graph.addArc(nodes[i], nodes[j]));
82
      }
83
    }
84
    
85
    typedef typename Graph::template ArcMap<int> IntArcMap;
86
    IntArcMap map(graph, 42);
87
    
88
    for (int i = 0; i < int(edges.size()); ++i) {
89
      check(map[edges[i]] == 42, "Wrong map constructor.");      
90
    }
91
    
92
    for (int i = 0; i < num; ++i) {
93
      for (int j = i + 1; j < num; ++j) {
94
	edges.push_back(graph.addArc(nodes[i], nodes[j]));
95
	map[edges.back()] = 23;
96
      }
97
    }
98
    map = constMap<Arc>(12);
99
    for (int i = 0; i < int(edges.size()); ++i) {
100
      check(map[edges[i]] == 12, "Wrong map constructor.");      
101
    }    
102
    graph.clear();
103
    edges.clear();    
104
  }
105

	
106
  template <typename Graph>
107
  void checkGraphEdgeMap() {
108
    Graph graph;
109
    const int num = 16;
110
    
111
    typedef typename Graph::Node Node;
112
    typedef typename Graph::Edge Edge;
113
    
114
    std::vector<Node> nodes;
115
    for (int i = 0; i < num; ++i) {
116
      nodes.push_back(graph.addNode());
117
    }
118
    
119
    std::vector<Edge> edges;
120
    for (int i = 0; i < num; ++i) {
121
      for (int j = 0; j < i; ++j) {
122
	edges.push_back(graph.addEdge(nodes[i], nodes[j]));
123
      }
124
    }
125
    
126
    typedef typename Graph::template EdgeMap<int> IntEdgeMap;
127
    IntEdgeMap map(graph, 42);
128
    
129
    for (int i = 0; i < int(edges.size()); ++i) {
130
      check(map[edges[i]] == 42, "Wrong map constructor.");      
131
    }
132
    
133
    for (int i = 0; i < num; ++i) {
134
      for (int j = i + 1; j < num; ++j) {
135
	edges.push_back(graph.addEdge(nodes[i], nodes[j]));
136
	map[edges.back()] = 23;
137
      }
138
    }
139
    map = constMap<Edge>(12);
140
    for (int i = 0; i < int(edges.size()); ++i) {
141
      check(map[edges[i]] == 12, "Wrong map constructor.");      
142
    }    
143
    graph.clear();
144
    edges.clear();    
145
  }
146

	
147
}
148

	
149
#endif
Ignore white space 6 line context
... ...
@@ -18,10 +18,20 @@
18 18
        lemon/dim2.h \
19 19
        lemon/random.h \
20 20
	lemon/list_graph.h \
21 21
        lemon/tolerance.h
22 22

	
23 23
bits_HEADERS += \
24
	lemon/bits/alteration_notifier.h \
25
	lemon/bits/array_map.h \
26
	lemon/bits/base_extender.h \
27
	lemon/bits/default_map.h \
24 28
        lemon/bits/invalid.h \
25
        lemon/bits/utility.h
29
	lemon/bits/map_extender.h \
30
        lemon/bits/utility.h \
31
	lemon/bits/vector_map.h
26 32

	
27 33
concept_HEADERS +=
34
	lemon/concept_check.h \
35
	lemon/concepts/digraph.h \
36
	lemon/concepts/graph.h \
37
	lemon/concepts/graph_components.h
Ignore white space 6 line context
... ...
@@ -13,6 +13,1455 @@
13 13
 * This software is provided "AS IS" with no warranty of any kind,
14 14
 * express or implied, and with no claim as to its suitability for any
15 15
 * purpose.
16 16
 *
17 17
 */
18 18

	
19
#ifndef LEMON_LIST_GRAPH_H
20
#define LEMON_LIST_GRAPH_H
21

	
22
///\ingroup graphs
23
///\file
24
///\brief ListDigraph, ListGraph classes.
25

	
26
#include <lemon/bits/graph_extender.h>
27

	
28
#include <vector>
29
#include <list>
30

	
31
namespace lemon {
32

	
33
  class ListDigraphBase {
34

	
35
  protected:
36
    struct NodeT {
37
      int first_in, first_out;
38
      int prev, next;
39
    };
40
 
41
    struct ArcT {
42
      int target, source;
43
      int prev_in, prev_out;
44
      int next_in, next_out;
45
    };
46

	
47
    std::vector<NodeT> nodes;
48

	
49
    int first_node;
50

	
51
    int first_free_node;
52

	
53
    std::vector<ArcT> arcs;
54

	
55
    int first_free_arc;
56
    
57
  public:
58
    
59
    typedef ListDigraphBase Digraph;
60
    
61
    class Node {
62
      friend class ListDigraphBase;
63
    protected:
64

	
65
      int id;
66
      explicit Node(int pid) { id = pid;}
67

	
68
    public:
69
      Node() {}
70
      Node (Invalid) { id = -1; }
71
      bool operator==(const Node& node) const {return id == node.id;}
72
      bool operator!=(const Node& node) const {return id != node.id;}
73
      bool operator<(const Node& node) const {return id < node.id;}
74
    };
75

	
76
    class Arc {
77
      friend class ListDigraphBase;
78
    protected:
79

	
80
      int id;
81
      explicit Arc(int pid) { id = pid;}
82

	
83
    public:
84
      Arc() {}
85
      Arc (Invalid) { id = -1; }
86
      bool operator==(const Arc& arc) const {return id == arc.id;}
87
      bool operator!=(const Arc& arc) const {return id != arc.id;}
88
      bool operator<(const Arc& arc) const {return id < arc.id;}
89
    };
90

	
91

	
92

	
93
    ListDigraphBase()
94
      : nodes(), first_node(-1),
95
	first_free_node(-1), arcs(), first_free_arc(-1) {}
96

	
97
    
98
    int maxNodeId() const { return nodes.size()-1; } 
99
    int maxArcId() const { return arcs.size()-1; }
100

	
101
    Node source(Arc e) const { return Node(arcs[e.id].source); }
102
    Node target(Arc e) const { return Node(arcs[e.id].target); }
103

	
104

	
105
    void first(Node& node) const { 
106
      node.id = first_node;
107
    }
108

	
109
    void next(Node& node) const {
110
      node.id = nodes[node.id].next;
111
    }
112

	
113

	
114
    void first(Arc& e) const { 
115
      int n;
116
      for(n = first_node; 
117
	  n!=-1 && nodes[n].first_in == -1; 
118
	  n = nodes[n].next);
119
      e.id = (n == -1) ? -1 : nodes[n].first_in;
120
    }
121

	
122
    void next(Arc& arc) const {
123
      if (arcs[arc.id].next_in != -1) {
124
	arc.id = arcs[arc.id].next_in;
125
      } else {
126
	int n;
127
	for(n = nodes[arcs[arc.id].target].next;
128
	  n!=-1 && nodes[n].first_in == -1; 
129
	  n = nodes[n].next);
130
	arc.id = (n == -1) ? -1 : nodes[n].first_in;
131
      }      
132
    }
133

	
134
    void firstOut(Arc &e, const Node& v) const {
135
      e.id = nodes[v.id].first_out;
136
    }
137
    void nextOut(Arc &e) const {
138
      e.id=arcs[e.id].next_out;
139
    }
140

	
141
    void firstIn(Arc &e, const Node& v) const {
142
      e.id = nodes[v.id].first_in;
143
    }
144
    void nextIn(Arc &e) const {
145
      e.id=arcs[e.id].next_in;
146
    }
147

	
148
    
149
    static int id(Node v) { return v.id; }
150
    static int id(Arc e) { return e.id; }
151

	
152
    static Node nodeFromId(int id) { return Node(id);}
153
    static Arc arcFromId(int id) { return Arc(id);}
154

	
155
    Node addNode() {     
156
      int n;
157
      
158
      if(first_free_node==-1) {
159
	n = nodes.size();
160
	nodes.push_back(NodeT());
161
      } else {
162
	n = first_free_node;
163
	first_free_node = nodes[n].next;
164
      }
165
      
166
      nodes[n].next = first_node;
167
      if(first_node != -1) nodes[first_node].prev = n;
168
      first_node = n;
169
      nodes[n].prev = -1;
170
      
171
      nodes[n].first_in = nodes[n].first_out = -1;
172
      
173
      return Node(n);
174
    }
175
    
176
    Arc addArc(Node u, Node v) {
177
      int n;      
178

	
179
      if (first_free_arc == -1) {
180
	n = arcs.size();
181
	arcs.push_back(ArcT());
182
      } else {
183
	n = first_free_arc;
184
	first_free_arc = arcs[n].next_in;
185
      }
186
      
187
      arcs[n].source = u.id; 
188
      arcs[n].target = v.id;
189

	
190
      arcs[n].next_out = nodes[u.id].first_out;
191
      if(nodes[u.id].first_out != -1) {
192
	arcs[nodes[u.id].first_out].prev_out = n;
193
      }
194
      
195
      arcs[n].next_in = nodes[v.id].first_in;
196
      if(nodes[v.id].first_in != -1) {
197
	arcs[nodes[v.id].first_in].prev_in = n;
198
      }
199
      
200
      arcs[n].prev_in = arcs[n].prev_out = -1;
201
	
202
      nodes[u.id].first_out = nodes[v.id].first_in = n;
203

	
204
      return Arc(n);
205
    }
206
    
207
    void erase(const Node& node) {
208
      int n = node.id;
209
      
210
      if(nodes[n].next != -1) {
211
	nodes[nodes[n].next].prev = nodes[n].prev;
212
      }
213
      
214
      if(nodes[n].prev != -1) {
215
	nodes[nodes[n].prev].next = nodes[n].next;
216
      } else {
217
	first_node = nodes[n].next;
218
      }
219
      
220
      nodes[n].next = first_free_node;
221
      first_free_node = n;
222

	
223
    }
224
    
225
    void erase(const Arc& arc) {
226
      int n = arc.id;
227
      
228
      if(arcs[n].next_in!=-1) {
229
	arcs[arcs[n].next_in].prev_in = arcs[n].prev_in;
230
      }
231

	
232
      if(arcs[n].prev_in!=-1) {
233
	arcs[arcs[n].prev_in].next_in = arcs[n].next_in;
234
      } else {
235
	nodes[arcs[n].target].first_in = arcs[n].next_in;
236
      }
237

	
238
      
239
      if(arcs[n].next_out!=-1) {
240
	arcs[arcs[n].next_out].prev_out = arcs[n].prev_out;
241
      } 
242

	
243
      if(arcs[n].prev_out!=-1) {
244
	arcs[arcs[n].prev_out].next_out = arcs[n].next_out;
245
      } else {
246
	nodes[arcs[n].source].first_out = arcs[n].next_out;
247
      }
248
      
249
      arcs[n].next_in = first_free_arc;
250
      first_free_arc = n;      
251

	
252
    }
253

	
254
    void clear() {
255
      arcs.clear();
256
      nodes.clear();
257
      first_node = first_free_node = first_free_arc = -1;
258
    }
259

	
260
  protected:
261
    void changeTarget(Arc e, Node n) 
262
    {
263
      if(arcs[e.id].next_in != -1)
264
	arcs[arcs[e.id].next_in].prev_in = arcs[e.id].prev_in;
265
      if(arcs[e.id].prev_in != -1)
266
	arcs[arcs[e.id].prev_in].next_in = arcs[e.id].next_in;
267
      else nodes[arcs[e.id].target].first_in = arcs[e.id].next_in;
268
      if (nodes[n.id].first_in != -1) {
269
	arcs[nodes[n.id].first_in].prev_in = e.id;
270
      }
271
      arcs[e.id].target = n.id;
272
      arcs[e.id].prev_in = -1;
273
      arcs[e.id].next_in = nodes[n.id].first_in;
274
      nodes[n.id].first_in = e.id;
275
    }
276
    void changeSource(Arc e, Node n) 
277
    {
278
      if(arcs[e.id].next_out != -1)
279
	arcs[arcs[e.id].next_out].prev_out = arcs[e.id].prev_out;
280
      if(arcs[e.id].prev_out != -1)
281
	arcs[arcs[e.id].prev_out].next_out = arcs[e.id].next_out;
282
      else nodes[arcs[e.id].source].first_out = arcs[e.id].next_out;
283
      if (nodes[n.id].first_out != -1) {
284
	arcs[nodes[n.id].first_out].prev_out = e.id;
285
      }
286
      arcs[e.id].source = n.id;
287
      arcs[e.id].prev_out = -1;
288
      arcs[e.id].next_out = nodes[n.id].first_out;
289
      nodes[n.id].first_out = e.id;
290
    }
291

	
292
  };
293

	
294
  typedef DigraphExtender<ListDigraphBase> ExtendedListDigraphBase;
295

	
296
  /// \addtogroup digraphs
297
  /// @{
298

	
299
  ///A list digraph class.
300

	
301
  ///This is a simple and fast digraph implementation.
302
  ///
303
  ///It conforms to the \ref concepts::Digraph "Digraph concept" and it
304
  ///also provides several additional useful extra functionalities.
305
  ///The most of the member functions and nested classes are
306
  ///documented only in the concept class.
307
  ///
308
  ///An important extra feature of this digraph implementation is that
309
  ///its maps are real \ref concepts::ReferenceMap "reference map"s.
310
  ///
311
  ///\sa concepts::Digraph.
312

	
313
  class ListDigraph : public ExtendedListDigraphBase {
314
  private:
315
    ///ListDigraph is \e not copy constructible. Use DigraphCopy() instead.
316
    
317
    ///ListDigraph is \e not copy constructible. Use DigraphCopy() instead.
318
    ///
319
    ListDigraph(const ListDigraph &) :ExtendedListDigraphBase() {};
320
    ///\brief Assignment of ListDigraph to another one is \e not allowed.
321
    ///Use DigraphCopy() instead.
322

	
323
    ///Assignment of ListDigraph to another one is \e not allowed.
324
    ///Use DigraphCopy() instead.
325
    void operator=(const ListDigraph &) {}
326
  public:
327

	
328
    typedef ExtendedListDigraphBase Parent;
329

	
330
    /// Constructor
331
    
332
    /// Constructor.
333
    ///
334
    ListDigraph() {}
335

	
336
    ///Add a new node to the digraph.
337
    
338
    /// \return the new node.
339
    ///
340
    Node addNode() { return Parent::addNode(); }
341

	
342
    ///Add a new arc to the digraph.
343
    
344
    ///Add a new arc to the digraph with source node \c s
345
    ///and target node \c t.
346
    ///\return the new arc.
347
    Arc addArc(const Node& s, const Node& t) { 
348
      return Parent::addArc(s, t); 
349
    }
350

	
351
    /// Changes the target of \c e to \c n
352

	
353
    /// Changes the target of \c e to \c n
354
    ///
355
    ///\note The <tt>ArcIt</tt>s and <tt>OutArcIt</tt>s referencing
356
    ///the changed arc remain valid. However <tt>InArcIt</tt>s are
357
    ///invalidated.
358
    ///\warning This functionality cannot be used together with the Snapshot
359
    ///feature.
360
    void changeTarget(Arc e, Node n) { 
361
      Parent::changeTarget(e,n); 
362
    }
363
    /// Changes the source of \c e to \c n
364

	
365
    /// Changes the source of \c e to \c n
366
    ///
367
    ///\note The <tt>ArcIt</tt>s and <tt>InArcIt</tt>s referencing
368
    ///the changed arc remain valid. However <tt>OutArcIt</tt>s are
369
    ///invalidated.
370
    ///\warning This functionality cannot be used together with the Snapshot
371
    ///feature.
372
    void changeSource(Arc e, Node n) { 
373
      Parent::changeSource(e,n);
374
    }
375

	
376
    /// Invert the direction of an arc.
377

	
378
    ///\note The <tt>ArcIt</tt>s referencing the changed arc remain
379
    ///valid. However <tt>OutArcIt</tt>s and <tt>InArcIt</tt>s are
380
    ///invalidated.
381
    ///\warning This functionality cannot be used together with the Snapshot
382
    ///feature.
383
    void reverseArc(Arc e) {
384
      Node t=target(e);
385
      changeTarget(e,source(e));
386
      changeSource(e,t);
387
    }
388

	
389
    /// Using this it is possible to avoid the superfluous memory
390
    /// allocation: if you know that the digraph you want to build will
391
    /// be very large (e.g. it will contain millions of nodes and/or arcs)
392
    /// then it is worth reserving space for this amount before starting
393
    /// to build the digraph.
394
    /// \sa reserveArc
395
    void reserveNode(int n) { nodes.reserve(n); };
396

	
397
    /// \brief Using this it is possible to avoid the superfluous memory
398
    /// allocation.
399

	
400
    /// Using this it is possible to avoid the superfluous memory
401
    /// allocation: if you know that the digraph you want to build will
402
    /// be very large (e.g. it will contain millions of nodes and/or arcs)
403
    /// then it is worth reserving space for this amount before starting
404
    /// to build the digraph.
405
    /// \sa reserveNode
406
    void reserveArc(int m) { arcs.reserve(m); };
407

	
408
    ///Contract two nodes.
409

	
410
    ///This function contracts two nodes.
411
    ///
412
    ///Node \p b will be removed but instead of deleting
413
    ///incident arcs, they will be joined to \p a.
414
    ///The last parameter \p r controls whether to remove loops. \c true
415
    ///means that loops will be removed.
416
    ///
417
    ///\note The <tt>ArcIt</tt>s
418
    ///referencing a moved arc remain
419
    ///valid. However <tt>InArcIt</tt>s and <tt>OutArcIt</tt>s
420
    ///may be invalidated.
421
    ///\warning This functionality cannot be used together with the Snapshot
422
    ///feature.
423
    void contract(Node a, Node b, bool r = true) 
424
    {
425
      for(OutArcIt e(*this,b);e!=INVALID;) {
426
	OutArcIt f=e;
427
	++f;
428
	if(r && target(e)==a) erase(e);
429
	else changeSource(e,a);
430
	e=f;
431
      }
432
      for(InArcIt e(*this,b);e!=INVALID;) {
433
	InArcIt f=e;
434
	++f;
435
	if(r && source(e)==a) erase(e);
436
	else changeTarget(e,a);
437
	e=f;
438
      }
439
      erase(b);
440
    }
441

	
442
    ///Split a node.
443

	
444
    ///This function splits a node. First a new node is added to the digraph,
445
    ///then the source of each outgoing arc of \c n is moved to this new node.
446
    ///If \c connect is \c true (this is the default value), then a new arc
447
    ///from \c n to the newly created node is also added.
448
    ///\return The newly created node.
449
    ///
450
    ///\note The <tt>ArcIt</tt>s referencing a moved arc remain
451
    ///valid. However <tt>InArcIt</tt>s and <tt>OutArcIt</tt>s may
452
    ///be invalidated.  
453
    ///
454
    ///\warning This functionality cannot be used together with the
455
    ///Snapshot feature.  \todo It could be implemented in a bit
456
    ///faster way.
457
    Node split(Node n, bool connect = true) {
458
      Node b = addNode();
459
      for(OutArcIt e(*this,n);e!=INVALID;) {
460
 	OutArcIt f=e;
461
	++f;
462
	changeSource(e,b);
463
	e=f;
464
      }
465
      if (connect) addArc(n,b);
466
      return b;
467
    }
468
      
469
    ///Split an arc.
470

	
471
    ///This function splits an arc. First a new node \c b is added to
472
    ///the digraph, then the original arc is re-targeted to \c
473
    ///b. Finally an arc from \c b to the original target is added.
474
    ///\return The newly created node.  
475
    ///\warning This functionality
476
    ///cannot be used together with the Snapshot feature.
477
    Node split(Arc e) {
478
      Node b = addNode();
479
      addArc(b,target(e));
480
      changeTarget(e,b);
481
      return b;
482
    }
483
      
484
    /// \brief Class to make a snapshot of the digraph and restore
485
    /// to it later.
486
    ///
487
    /// Class to make a snapshot of the digraph and to restore it
488
    /// later.
489
    ///
490
    /// The newly added nodes and arcs can be removed using the
491
    /// restore() function.
492
    ///
493
    /// \warning Arc and node deletions cannot be restored. This
494
    /// events invalidate the snapshot. 
495
    class Snapshot {
496
    protected:
497

	
498
      typedef Parent::NodeNotifier NodeNotifier;
499

	
500
      class NodeObserverProxy : public NodeNotifier::ObserverBase {
501
      public:
502

	
503
        NodeObserverProxy(Snapshot& _snapshot)
504
          : snapshot(_snapshot) {}
505

	
506
        using NodeNotifier::ObserverBase::attach;
507
        using NodeNotifier::ObserverBase::detach;
508
        using NodeNotifier::ObserverBase::attached;
509
        
510
      protected:
511
        
512
        virtual void add(const Node& node) {
513
          snapshot.addNode(node);
514
        }
515
        virtual void add(const std::vector<Node>& nodes) {
516
          for (int i = nodes.size() - 1; i >= 0; ++i) {
517
            snapshot.addNode(nodes[i]);
518
          }
519
        }
520
        virtual void erase(const Node& node) {
521
          snapshot.eraseNode(node);
522
        }
523
        virtual void erase(const std::vector<Node>& nodes) {
524
          for (int i = 0; i < int(nodes.size()); ++i) {
525
            snapshot.eraseNode(nodes[i]);
526
          }
527
        }
528
        virtual void build() {
529
          Node node;
530
          std::vector<Node> nodes;
531
          for (notifier()->first(node); node != INVALID; 
532
               notifier()->next(node)) {
533
            nodes.push_back(node);
534
          }
535
          for (int i = nodes.size() - 1; i >= 0; --i) {
536
            snapshot.addNode(nodes[i]);
537
          }
538
        }
539
        virtual void clear() {
540
          Node node;
541
          for (notifier()->first(node); node != INVALID; 
542
               notifier()->next(node)) {
543
            snapshot.eraseNode(node);
544
          }
545
        }
546

	
547
        Snapshot& snapshot;
548
      };
549

	
550
      class ArcObserverProxy : public ArcNotifier::ObserverBase {
551
      public:
552

	
553
        ArcObserverProxy(Snapshot& _snapshot)
554
          : snapshot(_snapshot) {}
555

	
556
        using ArcNotifier::ObserverBase::attach;
557
        using ArcNotifier::ObserverBase::detach;
558
        using ArcNotifier::ObserverBase::attached;
559
        
560
      protected:
561

	
562
        virtual void add(const Arc& arc) {
563
          snapshot.addArc(arc);
564
        }
565
        virtual void add(const std::vector<Arc>& arcs) {
566
          for (int i = arcs.size() - 1; i >= 0; ++i) {
567
            snapshot.addArc(arcs[i]);
568
          }
569
        }
570
        virtual void erase(const Arc& arc) {
571
          snapshot.eraseArc(arc);
572
        }
573
        virtual void erase(const std::vector<Arc>& arcs) {
574
          for (int i = 0; i < int(arcs.size()); ++i) {
575
            snapshot.eraseArc(arcs[i]);
576
          }
577
        }
578
        virtual void build() {
579
          Arc arc;
580
          std::vector<Arc> arcs;
581
          for (notifier()->first(arc); arc != INVALID; 
582
               notifier()->next(arc)) {
583
            arcs.push_back(arc);
584
          }
585
          for (int i = arcs.size() - 1; i >= 0; --i) {
586
            snapshot.addArc(arcs[i]);
587
          }
588
        }
589
        virtual void clear() {
590
          Arc arc;
591
          for (notifier()->first(arc); arc != INVALID; 
592
               notifier()->next(arc)) {
593
            snapshot.eraseArc(arc);
594
          }
595
        }
596

	
597
        Snapshot& snapshot;
598
      };
599
      
600
      ListDigraph *digraph;
601

	
602
      NodeObserverProxy node_observer_proxy;
603
      ArcObserverProxy arc_observer_proxy;
604

	
605
      std::list<Node> added_nodes;
606
      std::list<Arc> added_arcs;
607

	
608

	
609
      void addNode(const Node& node) {
610
        added_nodes.push_front(node);        
611
      }
612
      void eraseNode(const Node& node) {
613
        std::list<Node>::iterator it = 
614
          std::find(added_nodes.begin(), added_nodes.end(), node);
615
        if (it == added_nodes.end()) {
616
          clear();
617
          arc_observer_proxy.detach();
618
          throw NodeNotifier::ImmediateDetach();
619
        } else {
620
          added_nodes.erase(it);
621
        }
622
      }
623

	
624
      void addArc(const Arc& arc) {
625
        added_arcs.push_front(arc);        
626
      }
627
      void eraseArc(const Arc& arc) {
628
        std::list<Arc>::iterator it = 
629
          std::find(added_arcs.begin(), added_arcs.end(), arc);
630
        if (it == added_arcs.end()) {
631
          clear();
632
          node_observer_proxy.detach(); 
633
          throw ArcNotifier::ImmediateDetach();
634
        } else {
635
          added_arcs.erase(it);
636
        }        
637
      }
638

	
639
      void attach(ListDigraph &_digraph) {
640
	digraph = &_digraph;
641
	node_observer_proxy.attach(digraph->notifier(Node()));
642
        arc_observer_proxy.attach(digraph->notifier(Arc()));
643
      }
644
            
645
      void detach() {
646
	node_observer_proxy.detach();
647
	arc_observer_proxy.detach();
648
      }
649

	
650
      bool attached() const {
651
        return node_observer_proxy.attached();
652
      }
653

	
654
      void clear() {
655
        added_nodes.clear();
656
        added_arcs.clear();        
657
      }
658

	
659
    public:
660

	
661
      /// \brief Default constructor.
662
      ///
663
      /// Default constructor.
664
      /// To actually make a snapshot you must call save().
665
      Snapshot() 
666
        : digraph(0), node_observer_proxy(*this), 
667
          arc_observer_proxy(*this) {}
668
      
669
      /// \brief Constructor that immediately makes a snapshot.
670
      ///      
671
      /// This constructor immediately makes a snapshot of the digraph.
672
      /// \param _digraph The digraph we make a snapshot of.
673
      Snapshot(ListDigraph &_digraph) 
674
        : node_observer_proxy(*this), 
675
          arc_observer_proxy(*this) {
676
	attach(_digraph);
677
      }
678
      
679
      /// \brief Make a snapshot.
680
      ///
681
      /// Make a snapshot of the digraph.
682
      ///
683
      /// This function can be called more than once. In case of a repeated
684
      /// call, the previous snapshot gets lost.
685
      /// \param _digraph The digraph we make the snapshot of.
686
      void save(ListDigraph &_digraph) {
687
        if (attached()) {
688
          detach();
689
          clear();
690
        }
691
        attach(_digraph);
692
      }
693
      
694
      /// \brief Undo the changes until the last snapshot.
695
      // 
696
      /// Undo the changes until the last snapshot created by save().
697
      void restore() {
698
	detach();
699
	for(std::list<Arc>::iterator it = added_arcs.begin(); 
700
            it != added_arcs.end(); ++it) {
701
	  digraph->erase(*it);
702
	}
703
	for(std::list<Node>::iterator it = added_nodes.begin(); 
704
            it != added_nodes.end(); ++it) {
705
	  digraph->erase(*it);
706
	}
707
        clear();
708
      }
709

	
710
      /// \brief Gives back true when the snapshot is valid.
711
      ///
712
      /// Gives back true when the snapshot is valid.
713
      bool valid() const {
714
        return attached();
715
      }
716
    };
717
    
718
  };
719

	
720
  ///@}
721

	
722
  class ListGraphBase {
723

	
724
  protected:
725

	
726
    struct NodeT {
727
      int first_out;
728
      int prev, next;
729
    };
730
 
731
    struct ArcT {
732
      int target;
733
      int prev_out, next_out;
734
    };
735

	
736
    std::vector<NodeT> nodes;
737

	
738
    int first_node;
739

	
740
    int first_free_node;
741

	
742
    std::vector<ArcT> arcs;
743

	
744
    int first_free_arc;
745
    
746
  public:
747
    
748
    typedef ListGraphBase Digraph;
749

	
750
    class Node;
751
    class Arc;
752
    class Edge;
753
    
754
    class Node {
755
      friend class ListGraphBase;
756
    protected:
757

	
758
      int id;
759
      explicit Node(int pid) { id = pid;}
760

	
761
    public:
762
      Node() {}
763
      Node (Invalid) { id = -1; }
764
      bool operator==(const Node& node) const {return id == node.id;}
765
      bool operator!=(const Node& node) const {return id != node.id;}
766
      bool operator<(const Node& node) const {return id < node.id;}
767
    };
768

	
769
    class Edge {
770
      friend class ListGraphBase;
771
    protected:
772

	
773
      int id;
774
      explicit Edge(int pid) { id = pid;}
775

	
776
    public:
777
      Edge() {}
778
      Edge (Invalid) { id = -1; }
779
      bool operator==(const Edge& arc) const {return id == arc.id;}
780
      bool operator!=(const Edge& arc) const {return id != arc.id;}
781
      bool operator<(const Edge& arc) const {return id < arc.id;}
782
    };
783

	
784
    class Arc {
785
      friend class ListGraphBase;
786
    protected:
787

	
788
      int id;
789
      explicit Arc(int pid) { id = pid;}
790

	
791
    public:
792
      operator Edge() const { return edgeFromId(id / 2); }
793

	
794
      Arc() {}
795
      Arc (Invalid) { id = -1; }
796
      bool operator==(const Arc& arc) const {return id == arc.id;}
797
      bool operator!=(const Arc& arc) const {return id != arc.id;}
798
      bool operator<(const Arc& arc) const {return id < arc.id;}
799
    };
800

	
801

	
802

	
803
    ListGraphBase()
804
      : nodes(), first_node(-1),
805
	first_free_node(-1), arcs(), first_free_arc(-1) {}
806

	
807
    
808
    int maxNodeId() const { return nodes.size()-1; } 
809
    int maxEdgeId() const { return arcs.size() / 2 - 1; }
810
    int maxArcId() const { return arcs.size()-1; }
811

	
812
    Node source(Arc e) const { return Node(arcs[e.id ^ 1].target); }
813
    Node target(Arc e) const { return Node(arcs[e.id].target); }
814

	
815
    Node u(Edge e) const { return Node(arcs[2 * e.id].target); }
816
    Node v(Edge e) const { return Node(arcs[2 * e.id + 1].target); }
817

	
818
    static bool direction(Arc e) {
819
      return (e.id & 1) == 1;
820
    }
821

	
822
    static Arc direct(Edge e, bool d) {
823
      return Arc(e.id * 2 + (d ? 1 : 0));
824
    }
825

	
826
    void first(Node& node) const { 
827
      node.id = first_node;
828
    }
829

	
830
    void next(Node& node) const {
831
      node.id = nodes[node.id].next;
832
    }
833

	
834
    void first(Arc& e) const { 
835
      int n = first_node;
836
      while (n != -1 && nodes[n].first_out == -1) {
837
        n = nodes[n].next;
838
      }
839
      e.id = (n == -1) ? -1 : nodes[n].first_out;
840
    }
841

	
842
    void next(Arc& e) const {
843
      if (arcs[e.id].next_out != -1) {
844
	e.id = arcs[e.id].next_out;
845
      } else {
846
	int n = nodes[arcs[e.id ^ 1].target].next;
847
        while(n != -1 && nodes[n].first_out == -1) {
848
          n = nodes[n].next;
849
        }
850
	e.id = (n == -1) ? -1 : nodes[n].first_out;
851
      }      
852
    }
853

	
854
    void first(Edge& e) const { 
855
      int n = first_node;
856
      while (n != -1) {
857
        e.id = nodes[n].first_out;
858
        while ((e.id & 1) != 1) {
859
          e.id = arcs[e.id].next_out;
860
        }
861
        if (e.id != -1) {
862
          e.id /= 2;
863
          return;
864
        } 
865
        n = nodes[n].next;
866
      }
867
      e.id = -1;
868
    }
869

	
870
    void next(Edge& e) const {
871
      int n = arcs[e.id * 2].target;
872
      e.id = arcs[(e.id * 2) | 1].next_out;
873
      while ((e.id & 1) != 1) {
874
        e.id = arcs[e.id].next_out;
875
      }
876
      if (e.id != -1) {
877
        e.id /= 2;
878
        return;
879
      } 
880
      n = nodes[n].next;
881
      while (n != -1) {
882
        e.id = nodes[n].first_out;
883
        while ((e.id & 1) != 1) {
884
          e.id = arcs[e.id].next_out;
885
        }
886
        if (e.id != -1) {
887
          e.id /= 2;
888
          return;
889
        } 
890
        n = nodes[n].next;
891
      }
892
      e.id = -1;
893
    }
894

	
895
    void firstOut(Arc &e, const Node& v) const {
896
      e.id = nodes[v.id].first_out;
897
    }
898
    void nextOut(Arc &e) const {
899
      e.id = arcs[e.id].next_out;
900
    }
901

	
902
    void firstIn(Arc &e, const Node& v) const {
903
      e.id = ((nodes[v.id].first_out) ^ 1);
904
      if (e.id == -2) e.id = -1;
905
    }
906
    void nextIn(Arc &e) const {
907
      e.id = ((arcs[e.id ^ 1].next_out) ^ 1);
908
      if (e.id == -2) e.id = -1;
909
    }
910

	
911
    void firstInc(Edge &e, bool& d, const Node& v) const {
912
      int de = nodes[v.id].first_out;
913
      if (de != -1 ) {
914
        e.id = de / 2;
915
        d = ((de & 1) == 1);
916
      } else {
917
        e.id = -1;
918
        d = true;
919
      }
920
    }
921
    void nextInc(Edge &e, bool& d) const {
922
      int de = (arcs[(e.id * 2) | (d ? 1 : 0)].next_out);
923
      if (de != -1 ) {
924
        e.id = de / 2;
925
        d = ((de & 1) == 1);
926
      } else {
927
        e.id = -1;
928
        d = true;
929
      }
930
    }
931
    
932
    static int id(Node v) { return v.id; }
933
    static int id(Arc e) { return e.id; }
934
    static int id(Edge e) { return e.id; }
935

	
936
    static Node nodeFromId(int id) { return Node(id);}
937
    static Arc arcFromId(int id) { return Arc(id);}
938
    static Edge edgeFromId(int id) { return Edge(id);}
939

	
940
    Node addNode() {     
941
      int n;
942
      
943
      if(first_free_node==-1) {
944
	n = nodes.size();
945
	nodes.push_back(NodeT());
946
      } else {
947
	n = first_free_node;
948
	first_free_node = nodes[n].next;
949
      }
950
      
951
      nodes[n].next = first_node;
952
      if (first_node != -1) nodes[first_node].prev = n;
953
      first_node = n;
954
      nodes[n].prev = -1;
955
      
956
      nodes[n].first_out = -1;
957
      
958
      return Node(n);
959
    }
960
    
961
    Edge addEdge(Node u, Node v) {
962
      int n;      
963

	
964
      if (first_free_arc == -1) {
965
	n = arcs.size();
966
	arcs.push_back(ArcT());
967
	arcs.push_back(ArcT());
968
      } else {
969
	n = first_free_arc;
970
	first_free_arc = arcs[n].next_out;
971
      }
972
      
973
      arcs[n].target = u.id;
974
      arcs[n | 1].target = v.id;
975

	
976
      arcs[n].next_out = nodes[v.id].first_out;
977
      if (nodes[v.id].first_out != -1) {
978
	arcs[nodes[v.id].first_out].prev_out = n;
979
      }      
980
      arcs[n].prev_out = -1;
981
      nodes[v.id].first_out = n;
982
      
983
      arcs[n | 1].next_out = nodes[u.id].first_out;
984
      if (nodes[u.id].first_out != -1) {
985
	arcs[nodes[u.id].first_out].prev_out = (n | 1);
986
      }
987
      arcs[n | 1].prev_out = -1;      
988
      nodes[u.id].first_out = (n | 1);
989

	
990
      return Edge(n / 2);
991
    }
992
    
993
    void erase(const Node& node) {
994
      int n = node.id;
995
      
996
      if(nodes[n].next != -1) {
997
	nodes[nodes[n].next].prev = nodes[n].prev;
998
      }
999
      
1000
      if(nodes[n].prev != -1) {
1001
	nodes[nodes[n].prev].next = nodes[n].next;
1002
      } else {
1003
	first_node = nodes[n].next;
1004
      }
1005
      
1006
      nodes[n].next = first_free_node;
1007
      first_free_node = n;
1008

	
1009
    }
1010
    
1011
    void erase(const Edge& arc) {
1012
      int n = arc.id * 2;
1013
      
1014
      if (arcs[n].next_out != -1) {
1015
	arcs[arcs[n].next_out].prev_out = arcs[n].prev_out;
1016
      } 
1017

	
1018
      if (arcs[n].prev_out != -1) {
1019
	arcs[arcs[n].prev_out].next_out = arcs[n].next_out;
1020
      } else {
1021
	nodes[arcs[n | 1].target].first_out = arcs[n].next_out;
1022
      }
1023

	
1024
      if (arcs[n | 1].next_out != -1) {
1025
	arcs[arcs[n | 1].next_out].prev_out = arcs[n | 1].prev_out;
1026
      } 
1027

	
1028
      if (arcs[n | 1].prev_out != -1) {
1029
	arcs[arcs[n | 1].prev_out].next_out = arcs[n | 1].next_out;
1030
      } else {
1031
	nodes[arcs[n].target].first_out = arcs[n | 1].next_out;
1032
      }
1033
      
1034
      arcs[n].next_out = first_free_arc;
1035
      first_free_arc = n;      
1036

	
1037
    }
1038

	
1039
    void clear() {
1040
      arcs.clear();
1041
      nodes.clear();
1042
      first_node = first_free_node = first_free_arc = -1;
1043
    }
1044

	
1045
  protected:
1046

	
1047
    void changeTarget(Edge e, Node n) {
1048
      if(arcs[2 * e.id].next_out != -1) {
1049
	arcs[arcs[2 * e.id].next_out].prev_out = arcs[2 * e.id].prev_out;
1050
      }
1051
      if(arcs[2 * e.id].prev_out != -1) {
1052
	arcs[arcs[2 * e.id].prev_out].next_out = 
1053
          arcs[2 * e.id].next_out;
1054
      } else {
1055
        nodes[arcs[(2 * e.id) | 1].target].first_out = 
1056
          arcs[2 * e.id].next_out;
1057
      }
1058

	
1059
      if (nodes[n.id].first_out != -1) {
1060
	arcs[nodes[n.id].first_out].prev_out = 2 * e.id;
1061
      }
1062
      arcs[(2 * e.id) | 1].target = n.id;
1063
      arcs[2 * e.id].prev_out = -1;
1064
      arcs[2 * e.id].next_out = nodes[n.id].first_out;
1065
      nodes[n.id].first_out = 2 * e.id;
1066
    }
1067

	
1068
    void changeSource(Edge e, Node n) {
1069
      if(arcs[(2 * e.id) | 1].next_out != -1) {
1070
	arcs[arcs[(2 * e.id) | 1].next_out].prev_out = 
1071
          arcs[(2 * e.id) | 1].prev_out;
1072
      }
1073
      if(arcs[(2 * e.id) | 1].prev_out != -1) {
1074
	arcs[arcs[(2 * e.id) | 1].prev_out].next_out = 
1075
          arcs[(2 * e.id) | 1].next_out;
1076
      } else {
1077
        nodes[arcs[2 * e.id].target].first_out = 
1078
          arcs[(2 * e.id) | 1].next_out;
1079
      }
1080

	
1081
      if (nodes[n.id].first_out != -1) {
1082
	arcs[nodes[n.id].first_out].prev_out = ((2 * e.id) | 1);
1083
      }
1084
      arcs[2 * e.id].target = n.id;
1085
      arcs[(2 * e.id) | 1].prev_out = -1;
1086
      arcs[(2 * e.id) | 1].next_out = nodes[n.id].first_out;
1087
      nodes[n.id].first_out = ((2 * e.id) | 1);
1088
    }
1089

	
1090
  };
1091

	
1092
//   typedef GraphExtender<UndirDigraphExtender<ListDigraphBase> > 
1093
//   ExtendedListGraphBase;
1094

	
1095
  typedef GraphExtender<ListGraphBase> ExtendedListGraphBase;
1096

	
1097

	
1098

	
1099
  /// \addtogroup digraphs
1100
  /// @{
1101

	
1102
  ///An undirected list digraph class.
1103

	
1104
  ///This is a simple and fast undirected digraph implementation.
1105
  ///
1106
  ///An important extra feature of this digraph implementation is that
1107
  ///its maps are real \ref concepts::ReferenceMap "reference map"s.
1108
  ///
1109
  ///It conforms to the
1110
  ///\ref concepts::Graph "Graph concept".
1111
  ///
1112
  ///\sa concepts::Graph.
1113
  ///
1114
  class ListGraph : public ExtendedListGraphBase {
1115
  private:
1116
    ///ListGraph is \e not copy constructible. Use GraphCopy() instead.
1117

	
1118
    ///ListGraph is \e not copy constructible. Use GraphCopy() instead.
1119
    ///
1120
    ListGraph(const ListGraph &) :ExtendedListGraphBase()  {};
1121
    ///\brief Assignment of ListGraph to another one is \e not allowed.
1122
    ///Use GraphCopy() instead.
1123

	
1124
    ///Assignment of ListGraph to another one is \e not allowed.
1125
    ///Use GraphCopy() instead.
1126
    void operator=(const ListGraph &) {}
1127
  public:
1128
    /// Constructor
1129
    
1130
    /// Constructor.
1131
    ///
1132
    ListGraph() {}
1133

	
1134
    typedef ExtendedListGraphBase Parent;
1135

	
1136
    typedef Parent::OutArcIt IncArcIt;
1137

	
1138
    /// \brief Add a new node to the digraph.
1139
    ///
1140
    /// \return the new node.
1141
    ///
1142
    Node addNode() { return Parent::addNode(); }
1143

	
1144
    /// \brief Add a new edge to the digraph.
1145
    ///
1146
    /// Add a new arc to the digraph with source node \c s
1147
    /// and target node \c t.
1148
    /// \return the new edge.
1149
    Edge addEdge(const Node& s, const Node& t) { 
1150
      return Parent::addEdge(s, t); 
1151
    }
1152
    /// \brief Changes the source of \c e to \c n
1153
    ///
1154
    /// Changes the source of \c e to \c n
1155
    ///
1156
    ///\note The <tt>ArcIt</tt>s and <tt>InArcIt</tt>s
1157
    ///referencing the changed arc remain
1158
    ///valid. However <tt>OutArcIt</tt>s are invalidated.
1159
    void changeSource(Edge e, Node n) { 
1160
      Parent::changeSource(e,n); 
1161
    }    
1162
    /// \brief Changes the target of \c e to \c n
1163
    ///
1164
    /// Changes the target of \c e to \c n
1165
    ///
1166
    /// \note The <tt>ArcIt</tt>s referencing the changed arc remain
1167
    /// valid. However the other iterators may be invalidated.
1168
    void changeTarget(Edge e, Node n) { 
1169
      Parent::changeTarget(e,n); 
1170
    }
1171
    /// \brief Changes the source of \c e to \c n
1172
    ///
1173
    /// Changes the source of \c e to \c n. It changes the proper
1174
    /// node of the represented edge.
1175
    ///
1176
    ///\note The <tt>ArcIt</tt>s and <tt>InArcIt</tt>s
1177
    ///referencing the changed arc remain
1178
    ///valid. However <tt>OutArcIt</tt>s are invalidated.
1179
    void changeSource(Arc e, Node n) { 
1180
      if (Parent::direction(e)) {
1181
        Parent::changeSource(e,n);
1182
      } else {
1183
        Parent::changeTarget(e,n);
1184
      } 
1185
    }
1186
    /// \brief Changes the target of \c e to \c n
1187
    ///
1188
    /// Changes the target of \c e to \c n. It changes the proper
1189
    /// node of the represented edge.
1190
    ///
1191
    ///\note The <tt>ArcIt</tt>s and <tt>OutArcIt</tt>s
1192
    ///referencing the changed arc remain
1193
    ///valid. However <tt>InArcIt</tt>s are invalidated.
1194
    void changeTarget(Arc e, Node n) { 
1195
      if (Parent::direction(e)) {
1196
        Parent::changeTarget(e,n);
1197
      } else {
1198
        Parent::changeSource(e,n);
1199
      } 
1200
    }
1201
    /// \brief Contract two nodes.
1202
    ///
1203
    /// This function contracts two nodes.
1204
    ///
1205
    /// Node \p b will be removed but instead of deleting
1206
    /// its neighboring arcs, they will be joined to \p a.
1207
    /// The last parameter \p r controls whether to remove loops. \c true
1208
    /// means that loops will be removed.
1209
    ///
1210
    /// \note The <tt>ArcIt</tt>s referencing a moved arc remain
1211
    /// valid.
1212
    void contract(Node a, Node b, bool r = true) {
1213
      for(IncArcIt e(*this, b); e!=INVALID;) {
1214
	IncArcIt f = e; ++f;
1215
	if (r && runningNode(e) == a) {
1216
	  erase(e);
1217
	} else if (source(e) == b) {
1218
	  changeSource(e, a);
1219
	} else {
1220
	  changeTarget(e, a);
1221
	}
1222
	e = f;
1223
      }
1224
      erase(b);
1225
    }
1226

	
1227

	
1228
    /// \brief Class to make a snapshot of the digraph and restore
1229
    /// to it later.
1230
    ///
1231
    /// Class to make a snapshot of the digraph and to restore it
1232
    /// later.
1233
    ///
1234
    /// The newly added nodes and edges can be removed
1235
    /// using the restore() function.
1236
    ///
1237
    /// \warning Arc and node deletions cannot be restored. This
1238
    /// events invalidate the snapshot. 
1239
    class Snapshot {
1240
    protected:
1241

	
1242
      typedef Parent::NodeNotifier NodeNotifier;
1243

	
1244
      class NodeObserverProxy : public NodeNotifier::ObserverBase {
1245
      public:
1246

	
1247
        NodeObserverProxy(Snapshot& _snapshot)
1248
          : snapshot(_snapshot) {}
1249

	
1250
        using NodeNotifier::ObserverBase::attach;
1251
        using NodeNotifier::ObserverBase::detach;
1252
        using NodeNotifier::ObserverBase::attached;
1253
        
1254
      protected:
1255
        
1256
        virtual void add(const Node& node) {
1257
          snapshot.addNode(node);
1258
        }
1259
        virtual void add(const std::vector<Node>& nodes) {
1260
          for (int i = nodes.size() - 1; i >= 0; ++i) {
1261
            snapshot.addNode(nodes[i]);
1262
          }
1263
        }
1264
        virtual void erase(const Node& node) {
1265
          snapshot.eraseNode(node);
1266
        }
1267
        virtual void erase(const std::vector<Node>& nodes) {
1268
          for (int i = 0; i < int(nodes.size()); ++i) {
1269
            snapshot.eraseNode(nodes[i]);
1270
          }
1271
        }
1272
        virtual void build() {
1273
          Node node;
1274
          std::vector<Node> nodes;
1275
          for (notifier()->first(node); node != INVALID; 
1276
               notifier()->next(node)) {
1277
            nodes.push_back(node);
1278
          }
1279
          for (int i = nodes.size() - 1; i >= 0; --i) {
1280
            snapshot.addNode(nodes[i]);
1281
          }
1282
        }
1283
        virtual void clear() {
1284
          Node node;
1285
          for (notifier()->first(node); node != INVALID; 
1286
               notifier()->next(node)) {
1287
            snapshot.eraseNode(node);
1288
          }
1289
        }
1290

	
1291
        Snapshot& snapshot;
1292
      };
1293

	
1294
      class EdgeObserverProxy : public EdgeNotifier::ObserverBase {
1295
      public:
1296

	
1297
        EdgeObserverProxy(Snapshot& _snapshot)
1298
          : snapshot(_snapshot) {}
1299

	
1300
        using EdgeNotifier::ObserverBase::attach;
1301
        using EdgeNotifier::ObserverBase::detach;
1302
        using EdgeNotifier::ObserverBase::attached;
1303
        
1304
      protected:
1305

	
1306
        virtual void add(const Edge& arc) {
1307
          snapshot.addEdge(arc);
1308
        }
1309
        virtual void add(const std::vector<Edge>& arcs) {
1310
          for (int i = arcs.size() - 1; i >= 0; ++i) {
1311
            snapshot.addEdge(arcs[i]);
1312
          }
1313
        }
1314
        virtual void erase(const Edge& arc) {
1315
          snapshot.eraseEdge(arc);
1316
        }
1317
        virtual void erase(const std::vector<Edge>& arcs) {
1318
          for (int i = 0; i < int(arcs.size()); ++i) {
1319
            snapshot.eraseEdge(arcs[i]);
1320
          }
1321
        }
1322
        virtual void build() {
1323
          Edge arc;
1324
          std::vector<Edge> arcs;
1325
          for (notifier()->first(arc); arc != INVALID; 
1326
               notifier()->next(arc)) {
1327
            arcs.push_back(arc);
1328
          }
1329
          for (int i = arcs.size() - 1; i >= 0; --i) {
1330
            snapshot.addEdge(arcs[i]);
1331
          }
1332
        }
1333
        virtual void clear() {
1334
          Edge arc;
1335
          for (notifier()->first(arc); arc != INVALID; 
1336
               notifier()->next(arc)) {
1337
            snapshot.eraseEdge(arc);
1338
          }
1339
        }
1340

	
1341
        Snapshot& snapshot;
1342
      };
1343
      
1344
      ListGraph *digraph;
1345

	
1346
      NodeObserverProxy node_observer_proxy;
1347
      EdgeObserverProxy arc_observer_proxy;
1348

	
1349
      std::list<Node> added_nodes;
1350
      std::list<Edge> added_arcs;
1351

	
1352

	
1353
      void addNode(const Node& node) {
1354
        added_nodes.push_front(node);        
1355
      }
1356
      void eraseNode(const Node& node) {
1357
        std::list<Node>::iterator it = 
1358
          std::find(added_nodes.begin(), added_nodes.end(), node);
1359
        if (it == added_nodes.end()) {
1360
          clear();
1361
          arc_observer_proxy.detach();
1362
          throw NodeNotifier::ImmediateDetach();
1363
        } else {
1364
          added_nodes.erase(it);
1365
        }
1366
      }
1367

	
1368
      void addEdge(const Edge& arc) {
1369
        added_arcs.push_front(arc);        
1370
      }
1371
      void eraseEdge(const Edge& arc) {
1372
        std::list<Edge>::iterator it = 
1373
          std::find(added_arcs.begin(), added_arcs.end(), arc);
1374
        if (it == added_arcs.end()) {
1375
          clear();
1376
          node_observer_proxy.detach();
1377
          throw EdgeNotifier::ImmediateDetach();
1378
        } else {
1379
          added_arcs.erase(it);
1380
        }        
1381
      }
1382

	
1383
      void attach(ListGraph &_digraph) {
1384
	digraph = &_digraph;
1385
	node_observer_proxy.attach(digraph->notifier(Node()));
1386
        arc_observer_proxy.attach(digraph->notifier(Edge()));
1387
      }
1388
            
1389
      void detach() {
1390
	node_observer_proxy.detach();
1391
	arc_observer_proxy.detach();
1392
      }
1393

	
1394
      bool attached() const {
1395
        return node_observer_proxy.attached();
1396
      }
1397

	
1398
      void clear() {
1399
        added_nodes.clear();
1400
        added_arcs.clear();        
1401
      }
1402

	
1403
    public:
1404

	
1405
      /// \brief Default constructor.
1406
      ///
1407
      /// Default constructor.
1408
      /// To actually make a snapshot you must call save().
1409
      Snapshot() 
1410
        : digraph(0), node_observer_proxy(*this), 
1411
          arc_observer_proxy(*this) {}
1412
      
1413
      /// \brief Constructor that immediately makes a snapshot.
1414
      ///      
1415
      /// This constructor immediately makes a snapshot of the digraph.
1416
      /// \param _digraph The digraph we make a snapshot of.
1417
      Snapshot(ListGraph &_digraph) 
1418
        : node_observer_proxy(*this), 
1419
          arc_observer_proxy(*this) {
1420
	attach(_digraph);
1421
      }
1422
      
1423
      /// \brief Make a snapshot.
1424
      ///
1425
      /// Make a snapshot of the digraph.
1426
      ///
1427
      /// This function can be called more than once. In case of a repeated
1428
      /// call, the previous snapshot gets lost.
1429
      /// \param _digraph The digraph we make the snapshot of.
1430
      void save(ListGraph &_digraph) {
1431
        if (attached()) {
1432
          detach();
1433
          clear();
1434
        }
1435
        attach(_digraph);
1436
      }
1437
      
1438
      /// \brief Undo the changes until the last snapshot.
1439
      // 
1440
      /// Undo the changes until the last snapshot created by save().
1441
      void restore() {
1442
	detach();
1443
	for(std::list<Edge>::iterator it = added_arcs.begin(); 
1444
            it != added_arcs.end(); ++it) {
1445
	  digraph->erase(*it);
1446
	}
1447
	for(std::list<Node>::iterator it = added_nodes.begin(); 
1448
            it != added_nodes.end(); ++it) {
1449
	  digraph->erase(*it);
1450
	}
1451
        clear();
1452
      }
1453

	
1454
      /// \brief Gives back true when the snapshot is valid.
1455
      ///
1456
      /// Gives back true when the snapshot is valid.
1457
      bool valid() const {
1458
        return attached();
1459
      }
1460
    };
1461
  };
1462
  
1463
  /// @}  
1464
} //namespace lemon
1465
  
1466

	
1467
#endif
Ignore white space 6 line context
... ...
@@ -2,18 +2,22 @@
2 2
	test/Makefile
3 3

	
4 4
noinst_HEADERS += \
5 5
        test/test_tools.h
6 6

	
7 7
check_PROGRAMS += \
8
	test/digraph_test \
8 9
        test/dim_test \
10
	test/graph_test \
9 11
        test/random_test \
10 12
        test/test_tools_fail \
11 13
        test/test_tools_pass
12 14

	
13 15
TESTS += $(check_PROGRAMS)
14 16
XFAIL_TESTS += test/test_tools_fail$(EXEEXT)
15 17

	
18
test_digraph_test_SOURCES = test/digraph_test.cc
16 19
test_dim_test_SOURCES = test/dim_test.cc
20
test_graph_test_SOURCES = test/graph_test.cc
17 21
test_random_test_SOURCES = test/random_test.cc
18 22
test_test_tools_fail_SOURCES = test/test_tools_fail.cc
19 23
test_test_tools_pass_SOURCES = test/test_tools_pass.cc
0 comments (0 inline)