↑ Collapse diff ↑
Ignore white space 6 line context
1 1
LEMON code without an explicit copyright is covered by the following
2 2
copyright/license.
3 3

	
4
Copyright (C) 2003-2007 Egervary Jeno Kombinatorikus Optimalizalasi
4
Copyright (C) 2003-2008 Egervary Jeno Kombinatorikus Optimalizalasi
5 5
Kutatocsoport (Egervary Combinatorial Optimization Research Group,
6 6
EGRES).
7 7

	
8 8
Permission is hereby granted, free of charge, to any person or organization
9 9
obtaining a copy of the software and accompanying documentation covered by
10 10
this license (the "Software") to use, reproduce, display, distribute,
11 11
execute, and transmit the Software, and to prepare derivative works of the
12 12
Software, and to permit third-parties to whom the Software is furnished to
13 13
do so, all subject to the following:
14 14

	
15 15
The copyright notices in the Software and this entire statement, including
16 16
the above license grant, this restriction and the following disclaimer,
17 17
must be included in all copies of the Software, in whole or in part, and
18 18
all derivative works of the Software, unless such copies or derivative
19 19
works are solely in the form of machine-executable object code generated by
20 20
a source language processor.
21 21

	
22 22
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 23
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 24
FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
25 25
SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
26 26
FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
27 27
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
28 28
DEALINGS IN THE SOFTWARE.
29 29

	
30 30
===========================================================================
31 31
This license is a verbatim copy of the Boost Software License, Version 1.0.
32 32

	
33 33

	
Ignore white space 6 line context
1 1
/* -*- C++ -*-
2 2
 *
3 3
 * This file is a part of LEMON, a generic C++ optimization library
4 4
 *
5
 * Copyright (C) 2003-2007
5
 * Copyright (C) 2003-2008
6 6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
8 8
 *
9 9
 * Permission to use, modify and distribute this software is granted
10 10
 * provided that this copyright notice appears in all copies. For
11 11
 * precise terms see the accompanying LICENSE file.
12 12
 *
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 19
#ifndef LEMON_BITS_ALTERATION_NOTIFIER_H
20 20
#define LEMON_BITS_ALTERATION_NOTIFIER_H
21 21

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

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

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

	
31 31
namespace lemon {
32 32

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

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

	
104 104
    typedef True Notifier;
105 105

	
106 106
    typedef _Container Container;
107 107
    typedef _Item Item;
108 108

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

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

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

	
140 140
      friend class AlterationNotifier;
141 141

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

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

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

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

	
199 199
    private:
200 200

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

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

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

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

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

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

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

	
247 247
      virtual void build() = 0;
248 248

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

	
257 257
    };
258 258
	
259 259
  protected:
260 260

	
261 261
    const Container* container;
262 262

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

	
266 266
		
267 267
  public:
268 268

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

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

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

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

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

	
308 308
  protected:
309 309

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

	
312 312
  public:
313 313

	
314 314

	
315 315

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

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

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

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

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

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

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

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

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

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

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

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

	
483 483
}
484 484

	
485 485
#endif
Ignore white space 6 line context
1 1
/* -*- C++ -*-
2 2
 *
3 3
 * This file is a part of LEMON, a generic C++ optimization library
4 4
 *
5
 * Copyright (C) 2003-2007
5
 * Copyright (C) 2003-2008
6 6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
8 8
 *
9 9
 * Permission to use, modify and distribute this software is granted
10 10
 * provided that this copyright notice appears in all copies. For
11 11
 * precise terms see the accompanying LICENSE file.
12 12
 *
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 19
#ifndef LEMON_BITS_ARRAY_MAP_H
20 20
#define LEMON_BITS_ARRAY_MAP_H
21 21

	
22 22
#include <memory>
23 23

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

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

	
33 33
namespace lemon {
34 34

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

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

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

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

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

	
76 76
  public:
77 77

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

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

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

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

	
135 135

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

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

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

	
169 169
  public:
170 170

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

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

	
197 197
  protected:
198 198

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

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

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

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

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

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

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

	
342 342
  };		
343 343

	
344 344
}
345 345

	
346 346
#endif 
Ignore white space 6 line context
1 1
/* -*- C++ -*-
2 2
 *
3 3
 * This file is a part of LEMON, a generic C++ optimization library
4 4
 *
5
 * Copyright (C) 2003-2007
5
 * Copyright (C) 2003-2008
6 6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
8 8
 *
9 9
 * Permission to use, modify and distribute this software is granted
10 10
 * provided that this copyright notice appears in all copies. For
11 11
 * precise terms see the accompanying LICENSE file.
12 12
 *
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 19
#ifndef LEMON_BITS_BASE_EXTENDER_H
20 20
#define LEMON_BITS_BASE_EXTENDER_H
21 21

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

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

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

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

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

	
42 42
  public:
43 43

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

	
48 48
    typedef True UndirectedTag;
49 49

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

	
53 53
    protected:
54 54
      bool forward;
55 55

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

	
59 59
    public:
60 60
      Arc() {}
61 61

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

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

	
77 77

	
78 78

	
79 79
    using Parent::source;
80 80

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

	
86 86
    using Parent::target;
87 87

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

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

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

	
109 109

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

	
232 232

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

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

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

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

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

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

	
291 291

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

	
295 295
    using Parent::id;
296 296

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

	
491 491

	
492 492
  };
493 493
}
494 494

	
495 495
#endif
Ignore white space 6 line context
1 1
/* -*- C++ -*-
2 2
 *
3 3
 * This file is a part of LEMON, a generic C++ optimization library
4 4
 *
5
 * Copyright (C) 2003-2007
5
 * Copyright (C) 2003-2008
6 6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
8 8
 *
9 9
 * Permission to use, modify and distribute this software is granted
10 10
 * provided that this copyright notice appears in all copies. For
11 11
 * precise terms see the accompanying LICENSE file.
12 12
 *
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 19
#ifndef LEMON_BITS_DEFAULT_MAP_H
20 20
#define LEMON_BITS_DEFAULT_MAP_H
21 21

	
22 22

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

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

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

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

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

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

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

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

	
63 63

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

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

	
75 75

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

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

	
87 87

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

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

	
99 99

	
100 100
#if defined __GNUC__ && !defined __STRICT_ANSI__
101 101

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

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

	
113 113
#endif
114 114

	
115 115

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

	
122 122

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

	
129 129

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

	
136 136

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

	
143 143
// #else 
144 144

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

	
150 150
// #endif  
151 151

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

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

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

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

	
177 177
  };
178 178

	
179 179
}
180 180

	
181 181
#endif
Ignore white space 6 line context
1 1
/* -*- C++ -*-
2 2
 *
3 3
 * This file is a part of LEMON, a generic C++ optimization library
4 4
 *
5
 * Copyright (C) 2003-2007
5
 * Copyright (C) 2003-2008
6 6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
8 8
 *
9 9
 * Permission to use, modify and distribute this software is granted
10 10
 * provided that this copyright notice appears in all copies. For
11 11
 * precise terms see the accompanying LICENSE file.
12 12
 *
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 19
#ifndef LEMON_BITS_GRAPH_EXTENDER_H
20 20
#define LEMON_BITS_GRAPH_EXTENDER_H
21 21

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

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

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

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

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

	
43 43
    typedef Base Parent;
44 44
    typedef DigraphExtender Digraph;
45 45

	
46 46
    // Base extensions
47 47

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

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

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

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

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

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

	
76 76
    // Alterable extension
77 77

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

	
81 81

	
82 82
  protected:
83 83

	
84 84
    mutable NodeNotifier node_notifier;
85 85
    mutable ArcNotifier arc_notifier;
86 86

	
87 87
  public:
88 88

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

	
97 97
    class NodeIt : public Node { 
98 98
      const Digraph* _digraph;
99 99
    public:
100 100

	
101 101
      NodeIt() {}
102 102

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

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

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

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

	
117 117
    };
118 118

	
119 119

	
120 120
    class ArcIt : public Arc { 
121 121
      const Digraph* _digraph;
122 122
    public:
123 123

	
124 124
      ArcIt() { }
125 125

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

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

	
132 132
      ArcIt(const Digraph& digraph, const Arc& arc) : 
133 133
	Arc(arc), _digraph(&digraph) { }
134 134

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

	
140 140
    };
141 141

	
142 142

	
143 143
    class OutArcIt : public Arc { 
144 144
      const Digraph* _digraph;
145 145
    public:
146 146

	
147 147
      OutArcIt() { }
148 148

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

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

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

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

	
164 164
    };
165 165

	
166 166

	
167 167
    class InArcIt : public Arc { 
168 168
      const Digraph* _digraph;
169 169
    public:
170 170

	
171 171
      InArcIt() { }
172 172

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

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

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

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

	
188 188
    };
189 189

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

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

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

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

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

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

	
241 241
    };
242 242

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

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

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

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

	
266 266

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

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

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

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

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

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

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

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

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

	
337 337
    typedef True UndirectedTag;
338 338

	
339 339
    typedef typename Parent::Node Node;
340 340
    typedef typename Parent::Arc Arc;
341 341
    typedef typename Parent::Edge Edge;
342 342

	
343 343
    // Graph extension    
344 344

	
345 345
    int maxId(Node) const {
346 346
      return Parent::maxNodeId();
347 347
    }
348 348

	
349 349
    int maxId(Arc) const {
350 350
      return Parent::maxArcId();
351 351
    }
352 352

	
353 353
    int maxId(Edge) const {
354 354
      return Parent::maxEdgeId();
355 355
    }
356 356

	
357 357
    Node fromId(int id, Node) const {
358 358
      return Parent::nodeFromId(id);
359 359
    }
360 360

	
361 361
    Arc fromId(int id, Arc) const {
362 362
      return Parent::arcFromId(id);
363 363
    }
364 364

	
365 365
    Edge fromId(int id, Edge) const {
366 366
      return Parent::edgeFromId(id);
367 367
    }
368 368

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

	
378 378
    Arc oppositeArc(const Arc &arc) const {
379 379
      return Parent::direct(arc, !Parent::direction(arc));
380 380
    }
381 381

	
382 382
    using Parent::direct;
383 383
    Arc direct(const Edge &edge, const Node &node) const {
384 384
      return Parent::direct(edge, Parent::source(edge) == node);
385 385
    }
386 386

	
387 387
    // Alterable extension
388 388

	
389 389
    typedef AlterationNotifier<GraphExtender, Node> NodeNotifier;
390 390
    typedef AlterationNotifier<GraphExtender, Arc> ArcNotifier;
391 391
    typedef AlterationNotifier<GraphExtender, Edge> EdgeNotifier;
392 392

	
393 393

	
394 394
  protected:
395 395

	
396 396
    mutable NodeNotifier node_notifier;
397 397
    mutable ArcNotifier arc_notifier;
398 398
    mutable EdgeNotifier edge_notifier;
399 399

	
400 400
  public:
401 401

	
402 402
    NodeNotifier& notifier(Node) const {
403 403
      return node_notifier;
404 404
    }
405 405
    
406 406
    ArcNotifier& notifier(Arc) const {
407 407
      return arc_notifier;
408 408
    }
409 409

	
410 410
    EdgeNotifier& notifier(Edge) const {
411 411
      return edge_notifier;
412 412
    }
413 413

	
414 414

	
415 415

	
416 416
    class NodeIt : public Node { 
417 417
      const Graph* _graph;
418 418
    public:
419 419

	
420 420
      NodeIt() {}
421 421

	
422 422
      NodeIt(Invalid i) : Node(i) { }
423 423

	
424 424
      explicit NodeIt(const Graph& graph) : _graph(&graph) {
425 425
	_graph->first(static_cast<Node&>(*this));
426 426
      }
427 427

	
428 428
      NodeIt(const Graph& graph, const Node& node) 
429 429
	: Node(node), _graph(&graph) {}
430 430

	
431 431
      NodeIt& operator++() { 
432 432
	_graph->next(*this);
433 433
	return *this; 
434 434
      }
435 435

	
436 436
    };
437 437

	
438 438

	
439 439
    class ArcIt : public Arc { 
440 440
      const Graph* _graph;
441 441
    public:
442 442

	
443 443
      ArcIt() { }
444 444

	
445 445
      ArcIt(Invalid i) : Arc(i) { }
446 446

	
447 447
      explicit ArcIt(const Graph& graph) : _graph(&graph) {
448 448
	_graph->first(static_cast<Arc&>(*this));
449 449
      }
450 450

	
451 451
      ArcIt(const Graph& graph, const Arc& arc) : 
452 452
	Arc(arc), _graph(&graph) { }
453 453

	
454 454
      ArcIt& operator++() { 
455 455
	_graph->next(*this);
456 456
	return *this; 
457 457
      }
458 458

	
459 459
    };
460 460

	
461 461

	
462 462
    class OutArcIt : public Arc { 
463 463
      const Graph* _graph;
464 464
    public:
465 465

	
466 466
      OutArcIt() { }
467 467

	
468 468
      OutArcIt(Invalid i) : Arc(i) { }
469 469

	
470 470
      OutArcIt(const Graph& graph, const Node& node) 
471 471
	: _graph(&graph) {
472 472
	_graph->firstOut(*this, node);
473 473
      }
474 474

	
475 475
      OutArcIt(const Graph& graph, const Arc& arc) 
476 476
	: Arc(arc), _graph(&graph) {}
477 477

	
478 478
      OutArcIt& operator++() { 
479 479
	_graph->nextOut(*this);
480 480
	return *this; 
481 481
      }
482 482

	
483 483
    };
484 484

	
485 485

	
486 486
    class InArcIt : public Arc { 
487 487
      const Graph* _graph;
488 488
    public:
489 489

	
490 490
      InArcIt() { }
491 491

	
492 492
      InArcIt(Invalid i) : Arc(i) { }
493 493

	
494 494
      InArcIt(const Graph& graph, const Node& node) 
495 495
	: _graph(&graph) {
496 496
	_graph->firstIn(*this, node);
497 497
      }
498 498

	
499 499
      InArcIt(const Graph& graph, const Arc& arc) : 
500 500
	Arc(arc), _graph(&graph) {}
501 501

	
502 502
      InArcIt& operator++() { 
503 503
	_graph->nextIn(*this);
504 504
	return *this; 
505 505
      }
506 506

	
507 507
    };
508 508

	
509 509

	
510 510
    class EdgeIt : public Parent::Edge { 
511 511
      const Graph* _graph;
512 512
    public:
513 513

	
514 514
      EdgeIt() { }
515 515

	
516 516
      EdgeIt(Invalid i) : Edge(i) { }
517 517

	
518 518
      explicit EdgeIt(const Graph& graph) : _graph(&graph) {
519 519
	_graph->first(static_cast<Edge&>(*this));
520 520
      }
521 521

	
522 522
      EdgeIt(const Graph& graph, const Edge& edge) : 
523 523
	Edge(edge), _graph(&graph) { }
524 524

	
525 525
      EdgeIt& operator++() { 
526 526
	_graph->next(*this);
527 527
	return *this; 
528 528
      }
529 529

	
530 530
    };
531 531

	
532 532
    class IncEdgeIt : public Parent::Edge {
533 533
      friend class GraphExtender;
534 534
      const Graph* _graph;
535 535
      bool _direction;
536 536
    public:
537 537

	
538 538
      IncEdgeIt() { }
539 539

	
540 540
      IncEdgeIt(Invalid i) : Edge(i), _direction(false) { }
541 541

	
542 542
      IncEdgeIt(const Graph& graph, const Node &node) : _graph(&graph) {
543 543
	_graph->firstInc(*this, _direction, node);
544 544
      }
545 545

	
546 546
      IncEdgeIt(const Graph& graph, const Edge &edge, const Node &node)
547 547
	: _graph(&graph), Edge(edge) {
548 548
	_direction = (_graph->source(edge) == node);
549 549
      }
550 550

	
551 551
      IncEdgeIt& operator++() {
552 552
	_graph->nextInc(*this, _direction);
553 553
	return *this; 
554 554
      }
555 555
    };
556 556

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

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

	
585 585
    /// Base node of the iterator
586 586
    ///
587 587
    /// Returns the base node of the iterator
588 588
    Node baseNode(const IncEdgeIt &edge) const {
589 589
      return edge._direction ? source(edge) : target(edge);
590 590
    }
591 591
    /// Running node of the iterator
592 592
    ///
593 593
    /// Returns the running node of the iterator
594 594
    Node runningNode(const IncEdgeIt &edge) const {
595 595
      return edge._direction ? target(edge) : source(edge);
596 596
    }
597 597

	
598 598
    // Mappable extension
599 599

	
600 600
    template <typename _Value>
601 601
    class NodeMap 
602 602
      : public MapExtender<DefaultMap<Graph, Node, _Value> > {
603 603
    public:
604 604
      typedef GraphExtender Graph;
605 605
      typedef MapExtender<DefaultMap<Graph, Node, _Value> > Parent;
606 606

	
607 607
      NodeMap(const Graph& graph) 
608 608
	: Parent(graph) {}
609 609
      NodeMap(const Graph& graph, const _Value& value) 
610 610
	: Parent(graph, value) {}
611 611

	
612 612
      NodeMap& operator=(const NodeMap& cmap) {
613 613
	return operator=<NodeMap>(cmap);
614 614
      }
615 615

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

	
622 622
    };
623 623

	
624 624
    template <typename _Value>
625 625
    class ArcMap 
626 626
      : public MapExtender<DefaultMap<Graph, Arc, _Value> > {
627 627
    public:
628 628
      typedef GraphExtender Graph;
629 629
      typedef MapExtender<DefaultMap<Graph, Arc, _Value> > Parent;
630 630

	
631 631
      ArcMap(const Graph& graph) 
632 632
	: Parent(graph) {}
633 633
      ArcMap(const Graph& graph, const _Value& value) 
634 634
	: Parent(graph, value) {}
635 635

	
636 636
      ArcMap& operator=(const ArcMap& cmap) {
637 637
	return operator=<ArcMap>(cmap);
638 638
      }
639 639

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

	
647 647

	
648 648
    template <typename _Value>
649 649
    class EdgeMap 
650 650
      : public MapExtender<DefaultMap<Graph, Edge, _Value> > {
651 651
    public:
652 652
      typedef GraphExtender Graph;
653 653
      typedef MapExtender<DefaultMap<Graph, Edge, _Value> > Parent;
654 654

	
655 655
      EdgeMap(const Graph& graph) 
656 656
	: Parent(graph) {}
657 657

	
658 658
      EdgeMap(const Graph& graph, const _Value& value) 
659 659
	: Parent(graph, value) {}
660 660

	
661 661
      EdgeMap& operator=(const EdgeMap& cmap) {
662 662
	return operator=<EdgeMap>(cmap);
663 663
      }
664 664

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

	
671 671
    };
672 672

	
673 673
    // Alteration extension
674 674

	
675 675
    Node addNode() {
676 676
      Node node = Parent::addNode();
677 677
      notifier(Node()).add(node);
678 678
      return node;
679 679
    }
680 680

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

	
698 698
    template <typename Graph, typename NodeRefMap, typename EdgeRefMap>
699 699
    void build(const Graph& graph, NodeRefMap& nodeRef, 
700 700
               EdgeRefMap& edgeRef) {
701 701
      Parent::build(graph, nodeRef, edgeRef);
702 702
      notifier(Node()).build();
703 703
      notifier(Edge()).build();
704 704
      notifier(Arc()).build();
705 705
    }
706 706

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

	
715 715
      Parent::firstIn(arc, node);
716 716
      while (arc != INVALID ) {
717 717
	erase(arc);
718 718
	Parent::firstIn(arc, node);
719 719
      }
720 720

	
721 721
      notifier(Node()).erase(node);
722 722
      Parent::erase(node);
723 723
    }
724 724

	
725 725
    void erase(const Edge& edge) {
726 726
      std::vector<Arc> av;
727 727
      av.push_back(Parent::direct(edge, true));
728 728
      av.push_back(Parent::direct(edge, false));      
729 729
      notifier(Arc()).erase(av);
730 730
      notifier(Edge()).erase(edge);
731 731
      Parent::erase(edge);
732 732
    }
733 733

	
734 734
    GraphExtender() {
735 735
      node_notifier.setContainer(*this); 
736 736
      arc_notifier.setContainer(*this);
737 737
      edge_notifier.setContainer(*this);
738 738
    } 
739 739

	
740 740
    ~GraphExtender() {
741 741
      edge_notifier.clear();
742 742
      arc_notifier.clear();
743 743
      node_notifier.clear(); 
744 744
    } 
745 745

	
746 746
  };
747 747

	
748 748
}
749 749

	
750 750
#endif
Ignore white space 6 line context
1 1
/* -*- C++ -*-
2 2
 *
3 3
 * This file is a part of LEMON, a generic C++ optimization library
4 4
 *
5
 * Copyright (C) 2003-2007
5
 * Copyright (C) 2003-2008
6 6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
8 8
 *
9 9
 * Permission to use, modify and distribute this software is granted
10 10
 * provided that this copyright notice appears in all copies. For
11 11
 * precise terms see the accompanying LICENSE file.
12 12
 *
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 19
#ifndef LEMON_BITS_MAP_EXTENDER_H
20 20
#define LEMON_BITS_MAP_EXTENDER_H
21 21

	
22 22
#include <iterator>
23 23

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

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

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

	
32 32
namespace lemon {
33 33

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

	
41 41
    typedef _Map Parent;
42 42
    typedef MapExtender Map;
43 43

	
44 44

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

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

	
51 51
    class MapIt;
52 52
    class ConstMapIt;
53 53

	
54 54
    friend class MapIt;
55 55
    friend class ConstMapIt;
56 56

	
57 57
  public:
58 58

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

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

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

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

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

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

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

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

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

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

	
114 114
    class ConstMapIt : public Item {
115 115
    public:
116 116

	
117 117
      typedef Item Parent;
118 118

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

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

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

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

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

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

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

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

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

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

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

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

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

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

	
179 179
    typedef _Map Parent;
180 180
    typedef SubMapExtender Map;
181 181

	
182 182
    typedef _Graph Graph;
183 183

	
184 184
    typedef typename Parent::Key Item;
185 185

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

	
189 189
    class MapIt;
190 190
    class ConstMapIt;
191 191

	
192 192
    friend class MapIt;
193 193
    friend class ConstMapIt;
194 194

	
195 195
  public:
196 196

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

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

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

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

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

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

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

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

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

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

	
256 256
    class ConstMapIt : public Item {
257 257
    public:
258 258

	
259 259
      typedef Item Parent;
260 260

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

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

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

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

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

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

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

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

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

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

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

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

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

	
315 315
    const Graph& graph;
316 316
    
317 317
  };
318 318

	
319 319
}
320 320

	
321 321
#endif
Ignore white space 1536 line context
1 1

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

	
20 20
#ifndef LEMON_BITS_TRAITS_H
21 21
#define LEMON_BITS_TRAITS_H
22 22

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

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

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

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

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

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

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

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

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

	
68 68
     };
69 69

	
70 70
  };
71 71

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

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

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

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

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

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

	
107 107
  };
108 108

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

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

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

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

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

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

	
144 144
  };
145 145

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

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

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

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

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

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

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

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

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

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

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

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

	
203 203
  // Indicators for the tags
204 204

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

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

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

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

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

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

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

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

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

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

	
270 270
}
271 271

	
272 272
#endif
Ignore white space 6 line context
1 1
/* -*- C++ -*-
2 2
 *
3 3
 * This file is a part of LEMON, a generic C++ optimization library
4 4
 *
5
 * Copyright (C) 2003-2007
5
 * Copyright (C) 2003-2008
6 6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
8 8
 *
9 9
 * Permission to use, modify and distribute this software is granted
10 10
 * provided that this copyright notice appears in all copies. For
11 11
 * precise terms see the accompanying LICENSE file.
12 12
 *
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 19
#ifndef LEMON_BITS_VECTOR_MAP_H
20 20
#define LEMON_BITS_VECTOR_MAP_H
21 21

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

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

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

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

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

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

	
60 60
  public:
61 61

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

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

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

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

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

	
87 87

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

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

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

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

	
127 127

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

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

	
163 163

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

	
171 171
  protected:
172 172

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

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

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

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

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

	
239 239
  };
240 240

	
241 241
}
242 242

	
243 243
#endif
Ignore white space 6 line context
1 1
/* -*- C++ -*-
2 2
 *
3 3
 * This file is a part of LEMON, a generic C++ optimization library
4 4
 *
5
 * Copyright (C) 2003-2007
5
 * Copyright (C) 2003-2008
6 6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
8 8
 *
9 9
 * Permission to use, modify and distribute this software is granted
10 10
 * provided that this copyright notice appears in all copies. For
11 11
 * precise terms see the accompanying LICENSE file.
12 12
 *
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 19
#ifndef LEMON_CONCEPT_DIGRAPH_H
20 20
#define LEMON_CONCEPT_DIGRAPH_H
21 21

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

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

	
32 32
namespace lemon {
33 33
  namespace concepts {
34 34

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

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

	
63 63
      /// Defalult constructor.
64 64

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

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

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

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

	
86 86
        /// Invalid constructor \& conversion.
87 87

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

	
351 351
      /// \brief Returns the ID of the node.
352 352
      int id(Node) const { return -1; } 
353 353

	
354 354
      /// \brief Returns the ID of the arc.
355 355
      int id(Arc) const { return -1; } 
356 356

	
357 357
      /// \brief Returns the node with the given ID.
358 358
      ///
359 359
      /// \pre The argument should be a valid node ID in the graph.
360 360
      Node nodeFromId(int) const { return INVALID; } 
361 361

	
362 362
      /// \brief Returns the arc with the given ID.
363 363
      ///
364 364
      /// \pre The argument should be a valid arc ID in the graph.
365 365
      Arc arcFromId(int) const { return INVALID; } 
366 366

	
367 367
      /// \brief Returns an upper bound on the node IDs.
368 368
      int maxNodeId() const { return -1; } 
369 369

	
370 370
      /// \brief Returns an upper bound on the arc IDs.
371 371
      int maxArcId() const { return -1; } 
372 372

	
373 373
      void first(Node&) const {}
374 374
      void next(Node&) const {}
375 375

	
376 376
      void first(Arc&) const {}
377 377
      void next(Arc&) const {}
378 378

	
379 379

	
380 380
      void firstIn(Arc&, const Node&) const {}
381 381
      void nextIn(Arc&) const {}
382 382

	
383 383
      void firstOut(Arc&, const Node&) const {}
384 384
      void nextOut(Arc&) const {}
385 385

	
386 386
      // The second parameter is dummy.
387 387
      Node fromId(int, Node) const { return INVALID; }
388 388
      // The second parameter is dummy.
389 389
      Arc fromId(int, Arc) const { return INVALID; }
390 390

	
391 391
      // Dummy parameter.
392 392
      int maxId(Node) const { return -1; } 
393 393
      // Dummy parameter.
394 394
      int maxId(Arc) const { return -1; } 
395 395

	
396 396
      /// \brief The base node of the iterator.
397 397
      ///
398 398
      /// Gives back the base node of the iterator.
399 399
      /// It is always the target of the pointed arc.
400 400
      Node baseNode(const InArcIt&) const { return INVALID; }
401 401

	
402 402
      /// \brief The running node of the iterator.
403 403
      ///
404 404
      /// Gives back the running node of the iterator.
405 405
      /// It is always the source of the pointed arc.
406 406
      Node runningNode(const InArcIt&) const { return INVALID; }
407 407

	
408 408
      /// \brief The base node of the iterator.
409 409
      ///
410 410
      /// Gives back the base node of the iterator.
411 411
      /// It is always the source of the pointed arc.
412 412
      Node baseNode(const OutArcIt&) const { return INVALID; }
413 413

	
414 414
      /// \brief The running node of the iterator.
415 415
      ///
416 416
      /// Gives back the running node of the iterator.
417 417
      /// It is always the target of the pointed arc.
418 418
      Node runningNode(const OutArcIt&) const { return INVALID; }
419 419

	
420 420
      /// \brief The opposite node on the given arc.
421 421
      ///
422 422
      /// Gives back the opposite node on the given arc.
423 423
      Node oppositeNode(const Node&, const Arc&) const { return INVALID; }
424 424

	
425 425
      /// \brief Read write map of the nodes to type \c T.
426 426
      /// 
427 427
      /// ReadWrite map of the nodes to type \c T.
428 428
      /// \sa Reference
429 429
      template<class T> 
430 430
      class NodeMap : public ReadWriteMap< Node, T > {
431 431
      public:
432 432

	
433 433
        ///\e
434 434
        NodeMap(const Digraph&) { }
435 435
        ///\e
436 436
        NodeMap(const Digraph&, T) { }
437 437

	
438 438
        ///Copy constructor
439 439
        NodeMap(const NodeMap& nm) : ReadWriteMap< Node, T >(nm) { }
440 440
        ///Assignment operator
441 441
        template <typename CMap>
442 442
        NodeMap& operator=(const CMap&) { 
443 443
          checkConcept<ReadMap<Node, T>, CMap>();
444 444
          return *this; 
445 445
        }
446 446
      };
447 447

	
448 448
      /// \brief Read write map of the arcs to type \c T.
449 449
      ///
450 450
      /// Reference map of the arcs to type \c T.
451 451
      /// \sa Reference
452 452
      template<class T> 
453 453
      class ArcMap : public ReadWriteMap<Arc,T> {
454 454
      public:
455 455

	
456 456
        ///\e
457 457
        ArcMap(const Digraph&) { }
458 458
        ///\e
459 459
        ArcMap(const Digraph&, T) { }
460 460
        ///Copy constructor
461 461
        ArcMap(const ArcMap& em) : ReadWriteMap<Arc,T>(em) { }
462 462
        ///Assignment operator
463 463
        template <typename CMap>
464 464
        ArcMap& operator=(const CMap&) { 
465 465
          checkConcept<ReadMap<Arc, T>, CMap>();
466 466
          return *this; 
467 467
        }
468 468
      };
469 469

	
470 470
      template <typename RDigraph>
471 471
      struct Constraints {
472 472
        void constraints() {
473 473
          checkConcept<IterableDigraphComponent<>, Digraph>();
474 474
	  checkConcept<IDableDigraphComponent<>, Digraph>();
475 475
          checkConcept<MappableDigraphComponent<>, Digraph>();
476 476
        }
477 477
      };
478 478

	
479 479
    };
480 480
    
481 481
  } //namespace concepts  
482 482
} //namespace lemon
483 483

	
484 484

	
485 485

	
486 486
#endif // LEMON_CONCEPT_DIGRAPH_H
Ignore white space 6 line context
1 1
/* -*- C++ -*-
2 2
 *
3 3
 * This file is a part of LEMON, a generic C++ optimization library
4 4
 *
5
 * Copyright (C) 2003-2007
5
 * Copyright (C) 2003-2008
6 6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
8 8
 *
9 9
 * Permission to use, modify and distribute this software is granted
10 10
 * provided that this copyright notice appears in all copies. For
11 11
 * precise terms see the accompanying LICENSE file.
12 12
 *
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 19
///\ingroup graph_concepts
20 20
///\file
21 21
///\brief The concept of Undirected Graphs.
22 22

	
23 23
#ifndef LEMON_CONCEPT_GRAPH_H
24 24
#define LEMON_CONCEPT_GRAPH_H
25 25

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

	
30 30
namespace lemon {
31 31
  namespace concepts {
32 32

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

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

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

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

	
99 99
        /// Invalid constructor \& conversion.
100 100

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

	
310 310
      /// The directed arc type.
311 311

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

	
627 627
      /// \brief Returns the id of the node.
628 628
      int id(Node) const { return -1; } 
629 629

	
630 630
      /// \brief Returns the id of the edge.
631 631
      int id(Edge) const { return -1; } 
632 632

	
633 633
      /// \brief Returns the id of the arc.
634 634
      int id(Arc) const { return -1; } 
635 635

	
636 636
      /// \brief Returns the node with the given id.
637 637
      ///
638 638
      /// \pre The argument should be a valid node id in the graph.
639 639
      Node nodeFromId(int) const { return INVALID; } 
640 640

	
641 641
      /// \brief Returns the edge with the given id.
642 642
      ///
643 643
      /// \pre The argument should be a valid edge id in the graph.
644 644
      Edge edgeFromId(int) const { return INVALID; } 
645 645

	
646 646
      /// \brief Returns the arc with the given id.
647 647
      ///
648 648
      /// \pre The argument should be a valid arc id in the graph.
649 649
      Arc arcFromId(int) const { return INVALID; } 
650 650

	
651 651
      /// \brief Returns an upper bound on the node IDs.
652 652
      int maxNodeId() const { return -1; } 
653 653

	
654 654
      /// \brief Returns an upper bound on the edge IDs.
655 655
      int maxEdgeId() const { return -1; } 
656 656

	
657 657
      /// \brief Returns an upper bound on the arc IDs.
658 658
      int maxArcId() const { return -1; } 
659 659

	
660 660
      void first(Node&) const {}
661 661
      void next(Node&) const {}
662 662

	
663 663
      void first(Edge&) const {}
664 664
      void next(Edge&) const {}
665 665

	
666 666
      void first(Arc&) const {}
667 667
      void next(Arc&) const {}
668 668

	
669 669
      void firstOut(Arc&, Node) const {}
670 670
      void nextOut(Arc&) const {}
671 671

	
672 672
      void firstIn(Arc&, Node) const {}
673 673
      void nextIn(Arc&) const {}
674 674

	
675 675
      void firstInc(Edge &, bool &, const Node &) const {}
676 676
      void nextInc(Edge &, bool &) const {}
677 677

	
678 678
      // The second parameter is dummy.
679 679
      Node fromId(int, Node) const { return INVALID; }
680 680
      // The second parameter is dummy.
681 681
      Edge fromId(int, Edge) const { return INVALID; }
682 682
      // The second parameter is dummy.
683 683
      Arc fromId(int, Arc) const { return INVALID; }
684 684

	
685 685
      // Dummy parameter.
686 686
      int maxId(Node) const { return -1; } 
687 687
      // Dummy parameter.
688 688
      int maxId(Edge) const { return -1; } 
689 689
      // Dummy parameter.
690 690
      int maxId(Arc) const { return -1; } 
691 691

	
692 692
      /// \brief Base node of the iterator
693 693
      ///
694 694
      /// Returns the base node (the source in this case) of the iterator
695 695
      Node baseNode(OutArcIt e) const {
696 696
	return source(e);
697 697
      }
698 698
      /// \brief Running node of the iterator
699 699
      ///
700 700
      /// Returns the running node (the target in this case) of the
701 701
      /// iterator
702 702
      Node runningNode(OutArcIt e) const {
703 703
	return target(e);
704 704
      }
705 705

	
706 706
      /// \brief Base node of the iterator
707 707
      ///
708 708
      /// Returns the base node (the target in this case) of the iterator
709 709
      Node baseNode(InArcIt e) const {
710 710
	return target(e);
711 711
      }
712 712
      /// \brief Running node of the iterator
713 713
      ///
714 714
      /// Returns the running node (the source in this case) of the
715 715
      /// iterator
716 716
      Node runningNode(InArcIt e) const {
717 717
	return source(e);
718 718
      }
719 719

	
720 720
      /// \brief Base node of the iterator
721 721
      ///
722 722
      /// Returns the base node of the iterator
723 723
      Node baseNode(IncEdgeIt) const {
724 724
	return INVALID;
725 725
      }
726 726
      
727 727
      /// \brief Running node of the iterator
728 728
      ///
729 729
      /// Returns the running node of the iterator
730 730
      Node runningNode(IncEdgeIt) const {
731 731
	return INVALID;
732 732
      }
733 733

	
734 734
      template <typename Graph>
735 735
      struct Constraints {
736 736
	void constraints() {
737 737
	  checkConcept<IterableGraphComponent<>, Graph>();
738 738
	  checkConcept<IDableGraphComponent<>, Graph>();
739 739
	  checkConcept<MappableGraphComponent<>, Graph>();
740 740
	}
741 741
      };
742 742

	
743 743
    };
744 744

	
745 745
  }
746 746

	
747 747
}
748 748

	
749 749
#endif
Ignore white space 6 line context
1 1
/* -*- C++ -*-
2 2
 *
3 3
 * This file is a part of LEMON, a generic C++ optimization library
4 4
 *
5
 * Copyright (C) 2003-2007
5
 * Copyright (C) 2003-2008
6 6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
8 8
 *
9 9
 * Permission to use, modify and distribute this software is granted
10 10
 * provided that this copyright notice appears in all copies. For
11 11
 * precise terms see the accompanying LICENSE file.
12 12
 *
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 19
///\ingroup graph_concepts
20 20
///\file
21 21
///\brief The concept of graph components.
22 22

	
23 23

	
24 24
#ifndef LEMON_CONCEPT_GRAPH_COMPONENTS_H
25 25
#define LEMON_CONCEPT_GRAPH_COMPONENTS_H
26 26

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

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

	
32 32
namespace lemon {
33 33
  namespace concepts {
34 34

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

	
291 291
    };
292 292

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

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

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

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

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

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

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

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

	
347 347
      template <typename _Digraph>
348 348
      struct Constraints {
349 349

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

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

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

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

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

	
384 384
      using IDableDigraphComponent<_Base>::id;
385 385

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

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

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

	
406 406
      template <typename _Graph>
407 407
      struct Constraints {
408 408

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

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

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

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

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

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

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

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

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

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

	
559 559
	}
560 560

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

	
568 568

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

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

	
583 583
      typedef IterableDigraphComponent Digraph;
584 584

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

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

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

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

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

	
615 615

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

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

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

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

	
644 644
      /// @}
645 645

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

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

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

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

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

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

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

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

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

	
700 700
      /// @}
701 701

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

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

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

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

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

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

	
768 768
    
769 769
      typedef IterableGraphComponent Graph;
770 770

	
771 771
      /// \name Base iteration
772 772
      /// 
773 773
      /// This interface provides functions for iteration on graph items
Ignore white space 6 line context
1 1
/* -*- C++ -*-
2 2
 *
3 3
 * This file is a part of LEMON, a generic C++ optimization library
4 4
 *
5
 * Copyright (C) 2003-2007
5
 * Copyright (C) 2003-2008
6 6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
8 8
 *
9 9
 * Permission to use, modify and distribute this software is granted
10 10
 * provided that this copyright notice appears in all copies. For
11 11
 * precise terms see the accompanying LICENSE file.
12 12
 *
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 19
#include <iostream>
20 20
#include <vector>
21 21

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

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

	
32 32

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

	
36 36

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

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

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

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

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

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

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

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

	
81 81
  return 0;
82 82
}
Ignore white space 6 line context
1 1
/* -*- C++ -*-
2 2
 *
3 3
 * This file is a part of LEMON, a generic C++ optimization library
4 4
 *
5
 * Copyright (C) 2003-2007
5
 * Copyright (C) 2003-2008
6 6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
8 8
 *
9 9
 * Permission to use, modify and distribute this software is granted
10 10
 * provided that this copyright notice appears in all copies. For
11 11
 * precise terms see the accompanying LICENSE file.
12 12
 *
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 19
#ifndef LEMON_TEST_GRAPH_TEST_H
20 20
#define LEMON_TEST_GRAPH_TEST_H
21 21

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

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

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

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

	
49 49

	
50 50

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

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

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

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

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

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

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

	
92 92

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

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

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

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

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

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

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

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

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

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

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

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

	
184 184
  
185 185
} //namespace lemon
186 186

	
187 187

	
188 188
#endif
Ignore white space 6 line context
1 1
/* -*- C++ -*-
2 2
 *
3 3
 * This file is a part of LEMON, a generic C++ optimization library
4 4
 *
5
 * Copyright (C) 2003-2007
5
 * Copyright (C) 2003-2008
6 6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
8 8
 *
9 9
 * Permission to use, modify and distribute this software is granted
10 10
 * provided that this copyright notice appears in all copies. For
11 11
 * precise terms see the accompanying LICENSE file.
12 12
 *
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 19
#include <lemon/concepts/graph.h>
20 20
#include <lemon/list_graph.h>
21 21
// #include <lemon/smart_graph.h>
22 22
// #include <lemon/full_graph.h>
23 23
// #include <lemon/grid_graph.h>
24 24

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

	
27 27
#include "test_tools.h"
28 28

	
29 29

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

	
33 33
void check_concepts() {
34 34

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

	
113 113
}
114 114

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

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

	
125 125
  Graph g;
126 126

	
127 127
  check_item_counts(g,0,0);
128 128

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

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

	
138 138
  // print_items(g);
139 139

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

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

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

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

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

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

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

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

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

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

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

	
206 206
  return 0;
207 207
}
Ignore white space 6 line context
1 1
/* -*- C++ -*-
2 2
 *
3 3
 * This file is a part of LEMON, a generic C++ optimization library
4 4
 *
5
 * Copyright (C) 2003-2007
5
 * Copyright (C) 2003-2008
6 6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
8 8
 *
9 9
 * Permission to use, modify and distribute this software is granted
10 10
 * provided that this copyright notice appears in all copies. For
11 11
 * precise terms see the accompanying LICENSE file.
12 12
 *
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 19
#ifndef LEMON_TEST_MAP_TEST_H
20 20
#define LEMON_TEST_MAP_TEST_H
21 21

	
22 22

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

	
26 26
#include "test_tools.h"
27 27

	
28 28

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

	
33 33
namespace lemon {
34 34

	
35 35

	
36 36

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

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

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

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

	
147 147
}
148 148

	
149 149
#endif
0 comments (0 inline)