1.1 --- a/lemon/Makefile.am Sat Jan 12 23:30:44 2008 +0000
1.2 +++ b/lemon/Makefile.am Sun Jan 20 20:43:48 2008 +0100
1.3 @@ -21,7 +21,17 @@
1.4 lemon/tolerance.h
1.5
1.6 bits_HEADERS += \
1.7 + lemon/bits/alteration_notifier.h \
1.8 + lemon/bits/array_map.h \
1.9 + lemon/bits/base_extender.h \
1.10 + lemon/bits/default_map.h \
1.11 lemon/bits/invalid.h \
1.12 - lemon/bits/utility.h
1.13 + lemon/bits/map_extender.h \
1.14 + lemon/bits/utility.h \
1.15 + lemon/bits/vector_map.h
1.16
1.17 concept_HEADERS +=
1.18 + lemon/concept_check.h \
1.19 + lemon/concepts/digraph.h \
1.20 + lemon/concepts/graph.h \
1.21 + lemon/concepts/graph_components.h
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
2.2 +++ b/lemon/bits/alteration_notifier.h Sun Jan 20 20:43:48 2008 +0100
2.3 @@ -0,0 +1,485 @@
2.4 +/* -*- C++ -*-
2.5 + *
2.6 + * This file is a part of LEMON, a generic C++ optimization library
2.7 + *
2.8 + * Copyright (C) 2003-2007
2.9 + * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
2.10 + * (Egervary Research Group on Combinatorial Optimization, EGRES).
2.11 + *
2.12 + * Permission to use, modify and distribute this software is granted
2.13 + * provided that this copyright notice appears in all copies. For
2.14 + * precise terms see the accompanying LICENSE file.
2.15 + *
2.16 + * This software is provided "AS IS" with no warranty of any kind,
2.17 + * express or implied, and with no claim as to its suitability for any
2.18 + * purpose.
2.19 + *
2.20 + */
2.21 +
2.22 +#ifndef LEMON_BITS_ALTERATION_NOTIFIER_H
2.23 +#define LEMON_BITS_ALTERATION_NOTIFIER_H
2.24 +
2.25 +#include <vector>
2.26 +#include <list>
2.27 +
2.28 +#include <lemon/bits/utility.h>
2.29 +
2.30 +///\ingroup graphbits
2.31 +///\file
2.32 +///\brief Observer notifier for graph alteration observers.
2.33 +
2.34 +namespace lemon {
2.35 +
2.36 + /// \ingroup graphbits
2.37 + ///
2.38 + /// \brief Notifier class to notify observes about alterations in
2.39 + /// a container.
2.40 + ///
2.41 + /// The simple graph's can be refered as two containers, one node container
2.42 + /// and one edge container. But they are not standard containers they
2.43 + /// does not store values directly they are just key continars for more
2.44 + /// value containers which are the node and edge maps.
2.45 + ///
2.46 + /// The graph's node and edge sets can be changed as we add or erase
2.47 + /// nodes and edges in the graph. Lemon would like to handle easily
2.48 + /// that the node and edge maps should contain values for all nodes or
2.49 + /// edges. If we want to check on every indicing if the map contains
2.50 + /// the current indicing key that cause a drawback in the performance
2.51 + /// in the library. We use another solution we notify all maps about
2.52 + /// an alteration in the graph, which cause only drawback on the
2.53 + /// alteration of the graph.
2.54 + ///
2.55 + /// This class provides an interface to the container. The \e first() and \e
2.56 + /// next() member functions make possible to iterate on the keys of the
2.57 + /// container. The \e id() function returns an integer id for each key.
2.58 + /// The \e maxId() function gives back an upper bound of the ids.
2.59 + ///
2.60 + /// For the proper functonality of this class, we should notify it
2.61 + /// about each alteration in the container. The alterations have four type
2.62 + /// as \e add(), \e erase(), \e build() and \e clear(). The \e add() and
2.63 + /// \e erase() signals that only one or few items added or erased to or
2.64 + /// from the graph. If all items are erased from the graph or from an empty
2.65 + /// graph a new graph is builded then it can be signaled with the
2.66 + /// clear() and build() members. Important rule that if we erase items
2.67 + /// from graph we should first signal the alteration and after that erase
2.68 + /// them from the container, on the other way on item addition we should
2.69 + /// first extend the container and just after that signal the alteration.
2.70 + ///
2.71 + /// The alteration can be observed with a class inherited from the
2.72 + /// \e ObserverBase nested class. The signals can be handled with
2.73 + /// overriding the virtual functions defined in the base class. The
2.74 + /// observer base can be attached to the notifier with the
2.75 + /// \e attach() member and can be detached with detach() function. The
2.76 + /// alteration handlers should not call any function which signals
2.77 + /// an other alteration in the same notifier and should not
2.78 + /// detach any observer from the notifier.
2.79 + ///
2.80 + /// Alteration observers try to be exception safe. If an \e add() or
2.81 + /// a \e clear() function throws an exception then the remaining
2.82 + /// observeres will not be notified and the fulfilled additions will
2.83 + /// be rolled back by calling the \e erase() or \e clear()
2.84 + /// functions. Thence the \e erase() and \e clear() should not throw
2.85 + /// exception. Actullay, it can be throw only
2.86 + /// \ref AlterationObserver::ImmediateDetach ImmediateDetach
2.87 + /// exception which detach the observer from the notifier.
2.88 + ///
2.89 + /// There are some place when the alteration observing is not completly
2.90 + /// reliable. If we want to carry out the node degree in the graph
2.91 + /// as in the \ref InDegMap and we use the reverseEdge that cause
2.92 + /// unreliable functionality. Because the alteration observing signals
2.93 + /// only erasing and adding but not the reversing it will stores bad
2.94 + /// degrees. The sub graph adaptors cannot signal the alterations because
2.95 + /// just a setting in the filter map can modify the graph and this cannot
2.96 + /// be watched in any way.
2.97 + ///
2.98 + /// \param _Container The container which is observed.
2.99 + /// \param _Item The item type which is obserbved.
2.100 + ///
2.101 + /// \author Balazs Dezso
2.102 +
2.103 + template <typename _Container, typename _Item>
2.104 + class AlterationNotifier {
2.105 + public:
2.106 +
2.107 + typedef True Notifier;
2.108 +
2.109 + typedef _Container Container;
2.110 + typedef _Item Item;
2.111 +
2.112 + /// \brief Exception which can be called from \e clear() and
2.113 + /// \e erase().
2.114 + ///
2.115 + /// From the \e clear() and \e erase() function only this
2.116 + /// exception is allowed to throw. The exception immediatly
2.117 + /// detaches the current observer from the notifier. Because the
2.118 + /// \e clear() and \e erase() should not throw other exceptions
2.119 + /// it can be used to invalidate the observer.
2.120 + struct ImmediateDetach {};
2.121 +
2.122 + /// \brief ObserverBase is the base class for the observers.
2.123 + ///
2.124 + /// ObserverBase is the abstract base class for the observers.
2.125 + /// It will be notified about an item was inserted into or
2.126 + /// erased from the graph.
2.127 + ///
2.128 + /// The observer interface contains some pure virtual functions
2.129 + /// to override. The add() and erase() functions are
2.130 + /// to notify the oberver when one item is added or
2.131 + /// erased.
2.132 + ///
2.133 + /// The build() and clear() members are to notify the observer
2.134 + /// about the container is built from an empty container or
2.135 + /// is cleared to an empty container.
2.136 + ///
2.137 + /// \author Balazs Dezso
2.138 +
2.139 + class ObserverBase {
2.140 + protected:
2.141 + typedef AlterationNotifier Notifier;
2.142 +
2.143 + friend class AlterationNotifier;
2.144 +
2.145 + /// \brief Default constructor.
2.146 + ///
2.147 + /// Default constructor for ObserverBase.
2.148 + ///
2.149 + ObserverBase() : _notifier(0) {}
2.150 +
2.151 + /// \brief Constructor which attach the observer into notifier.
2.152 + ///
2.153 + /// Constructor which attach the observer into notifier.
2.154 + ObserverBase(AlterationNotifier& nf) {
2.155 + attach(nf);
2.156 + }
2.157 +
2.158 + /// \brief Constructor which attach the obserever to the same notifier.
2.159 + ///
2.160 + /// Constructor which attach the obserever to the same notifier as
2.161 + /// the other observer is attached to.
2.162 + ObserverBase(const ObserverBase& copy) {
2.163 + if (copy.attached()) {
2.164 + attach(*copy.notifier());
2.165 + }
2.166 + }
2.167 +
2.168 + /// \brief Destructor
2.169 + virtual ~ObserverBase() {
2.170 + if (attached()) {
2.171 + detach();
2.172 + }
2.173 + }
2.174 +
2.175 + /// \brief Attaches the observer into an AlterationNotifier.
2.176 + ///
2.177 + /// This member attaches the observer into an AlterationNotifier.
2.178 + ///
2.179 + void attach(AlterationNotifier& nf) {
2.180 + nf.attach(*this);
2.181 + }
2.182 +
2.183 + /// \brief Detaches the observer into an AlterationNotifier.
2.184 + ///
2.185 + /// This member detaches the observer from an AlterationNotifier.
2.186 + ///
2.187 + void detach() {
2.188 + _notifier->detach(*this);
2.189 + }
2.190 +
2.191 + /// \brief Gives back a pointer to the notifier which the map
2.192 + /// attached into.
2.193 + ///
2.194 + /// This function gives back a pointer to the notifier which the map
2.195 + /// attached into.
2.196 + ///
2.197 + Notifier* notifier() const { return const_cast<Notifier*>(_notifier); }
2.198 +
2.199 + /// Gives back true when the observer is attached into a notifier.
2.200 + bool attached() const { return _notifier != 0; }
2.201 +
2.202 + private:
2.203 +
2.204 + ObserverBase& operator=(const ObserverBase& copy);
2.205 +
2.206 + protected:
2.207 +
2.208 + Notifier* _notifier;
2.209 + typename std::list<ObserverBase*>::iterator _index;
2.210 +
2.211 + /// \brief The member function to notificate the observer about an
2.212 + /// item is added to the container.
2.213 + ///
2.214 + /// The add() member function notificates the observer about an item
2.215 + /// is added to the container. It have to be overrided in the
2.216 + /// subclasses.
2.217 + virtual void add(const Item&) = 0;
2.218 +
2.219 + /// \brief The member function to notificate the observer about
2.220 + /// more item is added to the container.
2.221 + ///
2.222 + /// The add() member function notificates the observer about more item
2.223 + /// is added to the container. It have to be overrided in the
2.224 + /// subclasses.
2.225 + virtual void add(const std::vector<Item>& items) = 0;
2.226 +
2.227 + /// \brief The member function to notificate the observer about an
2.228 + /// item is erased from the container.
2.229 + ///
2.230 + /// The erase() member function notificates the observer about an
2.231 + /// item is erased from the container. It have to be overrided in
2.232 + /// the subclasses.
2.233 + virtual void erase(const Item&) = 0;
2.234 +
2.235 + /// \brief The member function to notificate the observer about
2.236 + /// more item is erased from the container.
2.237 + ///
2.238 + /// The erase() member function notificates the observer about more item
2.239 + /// is erased from the container. It have to be overrided in the
2.240 + /// subclasses.
2.241 + virtual void erase(const std::vector<Item>& items) = 0;
2.242 +
2.243 + /// \brief The member function to notificate the observer about the
2.244 + /// container is built.
2.245 + ///
2.246 + /// The build() member function notificates the observer about the
2.247 + /// container is built from an empty container. It have to be
2.248 + /// overrided in the subclasses.
2.249 +
2.250 + virtual void build() = 0;
2.251 +
2.252 + /// \brief The member function to notificate the observer about all
2.253 + /// items are erased from the container.
2.254 + ///
2.255 + /// The clear() member function notificates the observer about all
2.256 + /// items are erased from the container. It have to be overrided in
2.257 + /// the subclasses.
2.258 + virtual void clear() = 0;
2.259 +
2.260 + };
2.261 +
2.262 + protected:
2.263 +
2.264 + const Container* container;
2.265 +
2.266 + typedef std::list<ObserverBase*> Observers;
2.267 + Observers _observers;
2.268 +
2.269 +
2.270 + public:
2.271 +
2.272 + /// \brief Default constructor.
2.273 + ///
2.274 + /// The default constructor of the AlterationNotifier.
2.275 + /// It creates an empty notifier.
2.276 + AlterationNotifier()
2.277 + : container(0) {}
2.278 +
2.279 + /// \brief Constructor.
2.280 + ///
2.281 + /// Constructor with the observed container parameter.
2.282 + AlterationNotifier(const Container& _container)
2.283 + : container(&_container) {}
2.284 +
2.285 + /// \brief Copy Constructor of the AlterationNotifier.
2.286 + ///
2.287 + /// Copy constructor of the AlterationNotifier.
2.288 + /// It creates only an empty notifier because the copiable
2.289 + /// notifier's observers have to be registered still into that notifier.
2.290 + AlterationNotifier(const AlterationNotifier& _notifier)
2.291 + : container(_notifier.container) {}
2.292 +
2.293 + /// \brief Destructor.
2.294 + ///
2.295 + /// Destructor of the AlterationNotifier.
2.296 + ///
2.297 + ~AlterationNotifier() {
2.298 + typename Observers::iterator it;
2.299 + for (it = _observers.begin(); it != _observers.end(); ++it) {
2.300 + (*it)->_notifier = 0;
2.301 + }
2.302 + }
2.303 +
2.304 + /// \brief Sets the container.
2.305 + ///
2.306 + /// Sets the container.
2.307 + void setContainer(const Container& _container) {
2.308 + container = &_container;
2.309 + }
2.310 +
2.311 + protected:
2.312 +
2.313 + AlterationNotifier& operator=(const AlterationNotifier&);
2.314 +
2.315 + public:
2.316 +
2.317 +
2.318 +
2.319 + /// \brief First item in the container.
2.320 + ///
2.321 + /// Returns the first item in the container. It is
2.322 + /// for start the iteration on the container.
2.323 + void first(Item& item) const {
2.324 + container->first(item);
2.325 + }
2.326 +
2.327 + /// \brief Next item in the container.
2.328 + ///
2.329 + /// Returns the next item in the container. It is
2.330 + /// for iterate on the container.
2.331 + void next(Item& item) const {
2.332 + container->next(item);
2.333 + }
2.334 +
2.335 + /// \brief Returns the id of the item.
2.336 + ///
2.337 + /// Returns the id of the item provided by the container.
2.338 + int id(const Item& item) const {
2.339 + return container->id(item);
2.340 + }
2.341 +
2.342 + /// \brief Returns the maximum id of the container.
2.343 + ///
2.344 + /// Returns the maximum id of the container.
2.345 + int maxId() const {
2.346 + return container->maxId(Item());
2.347 + }
2.348 +
2.349 + protected:
2.350 +
2.351 + void attach(ObserverBase& observer) {
2.352 + observer._index = _observers.insert(_observers.begin(), &observer);
2.353 + observer._notifier = this;
2.354 + }
2.355 +
2.356 + void detach(ObserverBase& observer) {
2.357 + _observers.erase(observer._index);
2.358 + observer._index = _observers.end();
2.359 + observer._notifier = 0;
2.360 + }
2.361 +
2.362 + public:
2.363 +
2.364 + /// \brief Notifies all the registed observers about an item added to
2.365 + /// the container.
2.366 + ///
2.367 + /// It notifies all the registed observers about an item added to
2.368 + /// the container.
2.369 + ///
2.370 + void add(const Item& item) {
2.371 + typename Observers::reverse_iterator it;
2.372 + try {
2.373 + for (it = _observers.rbegin(); it != _observers.rend(); ++it) {
2.374 + (*it)->add(item);
2.375 + }
2.376 + } catch (...) {
2.377 + typename Observers::iterator jt;
2.378 + for (jt = it.base(); jt != _observers.end(); ++jt) {
2.379 + (*jt)->erase(item);
2.380 + }
2.381 + throw;
2.382 + }
2.383 + }
2.384 +
2.385 + /// \brief Notifies all the registed observers about more item added to
2.386 + /// the container.
2.387 + ///
2.388 + /// It notifies all the registed observers about more item added to
2.389 + /// the container.
2.390 + ///
2.391 + void add(const std::vector<Item>& items) {
2.392 + typename Observers::reverse_iterator it;
2.393 + try {
2.394 + for (it = _observers.rbegin(); it != _observers.rend(); ++it) {
2.395 + (*it)->add(items);
2.396 + }
2.397 + } catch (...) {
2.398 + typename Observers::iterator jt;
2.399 + for (jt = it.base(); jt != _observers.end(); ++jt) {
2.400 + (*jt)->erase(items);
2.401 + }
2.402 + throw;
2.403 + }
2.404 + }
2.405 +
2.406 + /// \brief Notifies all the registed observers about an item erased from
2.407 + /// the container.
2.408 + ///
2.409 + /// It notifies all the registed observers about an item erased from
2.410 + /// the container.
2.411 + ///
2.412 + void erase(const Item& item) throw() {
2.413 + typename Observers::iterator it = _observers.begin();
2.414 + while (it != _observers.end()) {
2.415 + try {
2.416 + (*it)->erase(item);
2.417 + ++it;
2.418 + } catch (const ImmediateDetach&) {
2.419 + it = _observers.erase(it);
2.420 + (*it)->_index = _observers.end();
2.421 + (*it)->_notifier = 0;
2.422 + }
2.423 + }
2.424 + }
2.425 +
2.426 + /// \brief Notifies all the registed observers about more item erased
2.427 + /// from the container.
2.428 + ///
2.429 + /// It notifies all the registed observers about more item erased from
2.430 + /// the container.
2.431 + ///
2.432 + void erase(const std::vector<Item>& items) {
2.433 + typename Observers::iterator it = _observers.begin();
2.434 + while (it != _observers.end()) {
2.435 + try {
2.436 + (*it)->erase(items);
2.437 + ++it;
2.438 + } catch (const ImmediateDetach&) {
2.439 + it = _observers.erase(it);
2.440 + (*it)->_index = _observers.end();
2.441 + (*it)->_notifier = 0;
2.442 + }
2.443 + }
2.444 + }
2.445 +
2.446 + /// \brief Notifies all the registed observers about the container is
2.447 + /// built.
2.448 + ///
2.449 + /// Notifies all the registed observers about the container is built
2.450 + /// from an empty container.
2.451 + void build() {
2.452 + typename Observers::reverse_iterator it;
2.453 + try {
2.454 + for (it = _observers.rbegin(); it != _observers.rend(); ++it) {
2.455 + (*it)->build();
2.456 + }
2.457 + } catch (...) {
2.458 + typename Observers::iterator jt;
2.459 + for (jt = it.base(); jt != _observers.end(); ++jt) {
2.460 + (*jt)->clear();
2.461 + }
2.462 + throw;
2.463 + }
2.464 + }
2.465 +
2.466 + /// \brief Notifies all the registed observers about all items are
2.467 + /// erased.
2.468 + ///
2.469 + /// Notifies all the registed observers about all items are erased
2.470 + /// from the container.
2.471 + void clear() {
2.472 + typename Observers::iterator it = _observers.begin();
2.473 + while (it != _observers.end()) {
2.474 + try {
2.475 + (*it)->clear();
2.476 + ++it;
2.477 + } catch (const ImmediateDetach&) {
2.478 + it = _observers.erase(it);
2.479 + (*it)->_index = _observers.end();
2.480 + (*it)->_notifier = 0;
2.481 + }
2.482 + }
2.483 + }
2.484 + };
2.485 +
2.486 +}
2.487 +
2.488 +#endif
3.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
3.2 +++ b/lemon/bits/array_map.h Sun Jan 20 20:43:48 2008 +0100
3.3 @@ -0,0 +1,346 @@
3.4 +/* -*- C++ -*-
3.5 + *
3.6 + * This file is a part of LEMON, a generic C++ optimization library
3.7 + *
3.8 + * Copyright (C) 2003-2007
3.9 + * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
3.10 + * (Egervary Research Group on Combinatorial Optimization, EGRES).
3.11 + *
3.12 + * Permission to use, modify and distribute this software is granted
3.13 + * provided that this copyright notice appears in all copies. For
3.14 + * precise terms see the accompanying LICENSE file.
3.15 + *
3.16 + * This software is provided "AS IS" with no warranty of any kind,
3.17 + * express or implied, and with no claim as to its suitability for any
3.18 + * purpose.
3.19 + *
3.20 + */
3.21 +
3.22 +#ifndef LEMON_BITS_ARRAY_MAP_H
3.23 +#define LEMON_BITS_ARRAY_MAP_H
3.24 +
3.25 +#include <memory>
3.26 +
3.27 +#include <lemon/bits/traits.h>
3.28 +#include <lemon/bits/alteration_notifier.h>
3.29 +#include <lemon/concept_check.h>
3.30 +#include <lemon/concepts/maps.h>
3.31 +
3.32 +/// \ingroup graphbits
3.33 +/// \file
3.34 +/// \brief Graph map based on the array storage.
3.35 +
3.36 +namespace lemon {
3.37 +
3.38 + /// \ingroup graphbits
3.39 + ///
3.40 + /// \brief Graph map based on the array storage.
3.41 + ///
3.42 + /// The ArrayMap template class is graph map structure what
3.43 + /// automatically updates the map when a key is added to or erased from
3.44 + /// the map. This map uses the allocators to implement
3.45 + /// the container functionality.
3.46 + ///
3.47 + /// The template parameters are the Graph the current Item type and
3.48 + /// the Value type of the map.
3.49 + template <typename _Graph, typename _Item, typename _Value>
3.50 + class ArrayMap
3.51 + : public ItemSetTraits<_Graph, _Item>::ItemNotifier::ObserverBase {
3.52 + public:
3.53 + /// The graph type of the maps.
3.54 + typedef _Graph Graph;
3.55 + /// The item type of the map.
3.56 + typedef _Item Item;
3.57 + /// The reference map tag.
3.58 + typedef True ReferenceMapTag;
3.59 +
3.60 + /// The key type of the maps.
3.61 + typedef _Item Key;
3.62 + /// The value type of the map.
3.63 + typedef _Value Value;
3.64 +
3.65 + /// The const reference type of the map.
3.66 + typedef const _Value& ConstReference;
3.67 + /// The reference type of the map.
3.68 + typedef _Value& Reference;
3.69 +
3.70 + /// The notifier type.
3.71 + typedef typename ItemSetTraits<_Graph, _Item>::ItemNotifier Notifier;
3.72 +
3.73 + /// The MapBase of the Map which imlements the core regisitry function.
3.74 + typedef typename Notifier::ObserverBase Parent;
3.75 +
3.76 + private:
3.77 + typedef std::allocator<Value> Allocator;
3.78 +
3.79 + public:
3.80 +
3.81 + /// \brief Graph initialized map constructor.
3.82 + ///
3.83 + /// Graph initialized map constructor.
3.84 + explicit ArrayMap(const Graph& graph) {
3.85 + Parent::attach(graph.notifier(Item()));
3.86 + allocate_memory();
3.87 + Notifier* nf = Parent::notifier();
3.88 + Item it;
3.89 + for (nf->first(it); it != INVALID; nf->next(it)) {
3.90 + int id = nf->id(it);;
3.91 + allocator.construct(&(values[id]), Value());
3.92 + }
3.93 + }
3.94 +
3.95 + /// \brief Constructor to use default value to initialize the map.
3.96 + ///
3.97 + /// It constructs a map and initialize all of the the map.
3.98 + ArrayMap(const Graph& graph, const Value& value) {
3.99 + Parent::attach(graph.notifier(Item()));
3.100 + allocate_memory();
3.101 + Notifier* nf = Parent::notifier();
3.102 + Item it;
3.103 + for (nf->first(it); it != INVALID; nf->next(it)) {
3.104 + int id = nf->id(it);;
3.105 + allocator.construct(&(values[id]), value);
3.106 + }
3.107 + }
3.108 +
3.109 + /// \brief Constructor to copy a map of the same map type.
3.110 + ///
3.111 + /// Constructor to copy a map of the same map type.
3.112 + ArrayMap(const ArrayMap& copy) : Parent() {
3.113 + if (copy.attached()) {
3.114 + attach(*copy.notifier());
3.115 + }
3.116 + capacity = copy.capacity;
3.117 + if (capacity == 0) return;
3.118 + values = allocator.allocate(capacity);
3.119 + Notifier* nf = Parent::notifier();
3.120 + Item it;
3.121 + for (nf->first(it); it != INVALID; nf->next(it)) {
3.122 + int id = nf->id(it);;
3.123 + allocator.construct(&(values[id]), copy.values[id]);
3.124 + }
3.125 + }
3.126 +
3.127 + /// \brief Assign operator.
3.128 + ///
3.129 + /// This operator assigns for each item in the map the
3.130 + /// value mapped to the same item in the copied map.
3.131 + /// The parameter map should be indiced with the same
3.132 + /// itemset because this assign operator does not change
3.133 + /// the container of the map.
3.134 + ArrayMap& operator=(const ArrayMap& cmap) {
3.135 + return operator=<ArrayMap>(cmap);
3.136 + }
3.137 +
3.138 +
3.139 + /// \brief Template assign operator.
3.140 + ///
3.141 + /// The given parameter should be conform to the ReadMap
3.142 + /// concecpt and could be indiced by the current item set of
3.143 + /// the NodeMap. In this case the value for each item
3.144 + /// is assigned by the value of the given ReadMap.
3.145 + template <typename CMap>
3.146 + ArrayMap& operator=(const CMap& cmap) {
3.147 + checkConcept<concepts::ReadMap<Key, _Value>, CMap>();
3.148 + const typename Parent::Notifier* nf = Parent::notifier();
3.149 + Item it;
3.150 + for (nf->first(it); it != INVALID; nf->next(it)) {
3.151 + set(it, cmap[it]);
3.152 + }
3.153 + return *this;
3.154 + }
3.155 +
3.156 + /// \brief The destructor of the map.
3.157 + ///
3.158 + /// The destructor of the map.
3.159 + virtual ~ArrayMap() {
3.160 + if (attached()) {
3.161 + clear();
3.162 + detach();
3.163 + }
3.164 + }
3.165 +
3.166 + protected:
3.167 +
3.168 + using Parent::attach;
3.169 + using Parent::detach;
3.170 + using Parent::attached;
3.171 +
3.172 + public:
3.173 +
3.174 + /// \brief The subscript operator.
3.175 + ///
3.176 + /// The subscript operator. The map can be subscripted by the
3.177 + /// actual keys of the graph.
3.178 + Value& operator[](const Key& key) {
3.179 + int id = Parent::notifier()->id(key);
3.180 + return values[id];
3.181 + }
3.182 +
3.183 + /// \brief The const subscript operator.
3.184 + ///
3.185 + /// The const subscript operator. The map can be subscripted by the
3.186 + /// actual keys of the graph.
3.187 + const Value& operator[](const Key& key) const {
3.188 + int id = Parent::notifier()->id(key);
3.189 + return values[id];
3.190 + }
3.191 +
3.192 + /// \brief Setter function of the map.
3.193 + ///
3.194 + /// Setter function of the map. Equivalent with map[key] = val.
3.195 + /// This is a compatibility feature with the not dereferable maps.
3.196 + void set(const Key& key, const Value& val) {
3.197 + (*this)[key] = val;
3.198 + }
3.199 +
3.200 + protected:
3.201 +
3.202 + /// \brief Adds a new key to the map.
3.203 + ///
3.204 + /// It adds a new key to the map. It called by the observer notifier
3.205 + /// and it overrides the add() member function of the observer base.
3.206 + virtual void add(const Key& key) {
3.207 + Notifier* nf = Parent::notifier();
3.208 + int id = nf->id(key);
3.209 + if (id >= capacity) {
3.210 + int new_capacity = (capacity == 0 ? 1 : capacity);
3.211 + while (new_capacity <= id) {
3.212 + new_capacity <<= 1;
3.213 + }
3.214 + Value* new_values = allocator.allocate(new_capacity);
3.215 + Item it;
3.216 + for (nf->first(it); it != INVALID; nf->next(it)) {
3.217 + int jd = nf->id(it);;
3.218 + if (id != jd) {
3.219 + allocator.construct(&(new_values[jd]), values[jd]);
3.220 + allocator.destroy(&(values[jd]));
3.221 + }
3.222 + }
3.223 + if (capacity != 0) allocator.deallocate(values, capacity);
3.224 + values = new_values;
3.225 + capacity = new_capacity;
3.226 + }
3.227 + allocator.construct(&(values[id]), Value());
3.228 + }
3.229 +
3.230 + /// \brief Adds more new keys to the map.
3.231 + ///
3.232 + /// It adds more new keys to the map. It called by the observer notifier
3.233 + /// and it overrides the add() member function of the observer base.
3.234 + virtual void add(const std::vector<Key>& keys) {
3.235 + Notifier* nf = Parent::notifier();
3.236 + int max_id = -1;
3.237 + for (int i = 0; i < int(keys.size()); ++i) {
3.238 + int id = nf->id(keys[i]);
3.239 + if (id > max_id) {
3.240 + max_id = id;
3.241 + }
3.242 + }
3.243 + if (max_id >= capacity) {
3.244 + int new_capacity = (capacity == 0 ? 1 : capacity);
3.245 + while (new_capacity <= max_id) {
3.246 + new_capacity <<= 1;
3.247 + }
3.248 + Value* new_values = allocator.allocate(new_capacity);
3.249 + Item it;
3.250 + for (nf->first(it); it != INVALID; nf->next(it)) {
3.251 + int id = nf->id(it);
3.252 + bool found = false;
3.253 + for (int i = 0; i < int(keys.size()); ++i) {
3.254 + int jd = nf->id(keys[i]);
3.255 + if (id == jd) {
3.256 + found = true;
3.257 + break;
3.258 + }
3.259 + }
3.260 + if (found) continue;
3.261 + allocator.construct(&(new_values[id]), values[id]);
3.262 + allocator.destroy(&(values[id]));
3.263 + }
3.264 + if (capacity != 0) allocator.deallocate(values, capacity);
3.265 + values = new_values;
3.266 + capacity = new_capacity;
3.267 + }
3.268 + for (int i = 0; i < int(keys.size()); ++i) {
3.269 + int id = nf->id(keys[i]);
3.270 + allocator.construct(&(values[id]), Value());
3.271 + }
3.272 + }
3.273 +
3.274 + /// \brief Erase a key from the map.
3.275 + ///
3.276 + /// Erase a key from the map. It called by the observer notifier
3.277 + /// and it overrides the erase() member function of the observer base.
3.278 + virtual void erase(const Key& key) {
3.279 + int id = Parent::notifier()->id(key);
3.280 + allocator.destroy(&(values[id]));
3.281 + }
3.282 +
3.283 + /// \brief Erase more keys from the map.
3.284 + ///
3.285 + /// Erase more keys from the map. It called by the observer notifier
3.286 + /// and it overrides the erase() member function of the observer base.
3.287 + virtual void erase(const std::vector<Key>& keys) {
3.288 + for (int i = 0; i < int(keys.size()); ++i) {
3.289 + int id = Parent::notifier()->id(keys[i]);
3.290 + allocator.destroy(&(values[id]));
3.291 + }
3.292 + }
3.293 +
3.294 + /// \brief Buildes the map.
3.295 + ///
3.296 + /// It buildes the map. It called by the observer notifier
3.297 + /// and it overrides the build() member function of the observer base.
3.298 + virtual void build() {
3.299 + Notifier* nf = Parent::notifier();
3.300 + allocate_memory();
3.301 + Item it;
3.302 + for (nf->first(it); it != INVALID; nf->next(it)) {
3.303 + int id = nf->id(it);;
3.304 + allocator.construct(&(values[id]), Value());
3.305 + }
3.306 + }
3.307 +
3.308 + /// \brief Clear the map.
3.309 + ///
3.310 + /// It erase all items from the map. It called by the observer notifier
3.311 + /// and it overrides the clear() member function of the observer base.
3.312 + virtual void clear() {
3.313 + Notifier* nf = Parent::notifier();
3.314 + if (capacity != 0) {
3.315 + Item it;
3.316 + for (nf->first(it); it != INVALID; nf->next(it)) {
3.317 + int id = nf->id(it);
3.318 + allocator.destroy(&(values[id]));
3.319 + }
3.320 + allocator.deallocate(values, capacity);
3.321 + capacity = 0;
3.322 + }
3.323 + }
3.324 +
3.325 + private:
3.326 +
3.327 + void allocate_memory() {
3.328 + int max_id = Parent::notifier()->maxId();
3.329 + if (max_id == -1) {
3.330 + capacity = 0;
3.331 + values = 0;
3.332 + return;
3.333 + }
3.334 + capacity = 1;
3.335 + while (capacity <= max_id) {
3.336 + capacity <<= 1;
3.337 + }
3.338 + values = allocator.allocate(capacity);
3.339 + }
3.340 +
3.341 + int capacity;
3.342 + Value* values;
3.343 + Allocator allocator;
3.344 +
3.345 + };
3.346 +
3.347 +}
3.348 +
3.349 +#endif
4.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
4.2 +++ b/lemon/bits/base_extender.h Sun Jan 20 20:43:48 2008 +0100
4.3 @@ -0,0 +1,495 @@
4.4 +/* -*- C++ -*-
4.5 + *
4.6 + * This file is a part of LEMON, a generic C++ optimization library
4.7 + *
4.8 + * Copyright (C) 2003-2007
4.9 + * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
4.10 + * (Egervary Research Group on Combinatorial Optimization, EGRES).
4.11 + *
4.12 + * Permission to use, modify and distribute this software is granted
4.13 + * provided that this copyright notice appears in all copies. For
4.14 + * precise terms see the accompanying LICENSE file.
4.15 + *
4.16 + * This software is provided "AS IS" with no warranty of any kind,
4.17 + * express or implied, and with no claim as to its suitability for any
4.18 + * purpose.
4.19 + *
4.20 + */
4.21 +
4.22 +#ifndef LEMON_BITS_BASE_EXTENDER_H
4.23 +#define LEMON_BITS_BASE_EXTENDER_H
4.24 +
4.25 +#include <lemon/bits/invalid.h>
4.26 +#include <lemon/error.h>
4.27 +
4.28 +#include <lemon/bits/map_extender.h>
4.29 +#include <lemon/bits/default_map.h>
4.30 +
4.31 +#include <lemon/concept_check.h>
4.32 +#include <lemon/concepts/maps.h>
4.33 +
4.34 +///\ingroup digraphbits
4.35 +///\file
4.36 +///\brief Extenders for the digraph types
4.37 +namespace lemon {
4.38 +
4.39 + /// \ingroup digraphbits
4.40 + ///
4.41 + /// \brief BaseDigraph to BaseGraph extender
4.42 + template <typename Base>
4.43 + class UndirDigraphExtender : public Base {
4.44 +
4.45 + public:
4.46 +
4.47 + typedef Base Parent;
4.48 + typedef typename Parent::Arc Edge;
4.49 + typedef typename Parent::Node Node;
4.50 +
4.51 + typedef True UndirectedTag;
4.52 +
4.53 + class Arc : public Edge {
4.54 + friend class UndirDigraphExtender;
4.55 +
4.56 + protected:
4.57 + bool forward;
4.58 +
4.59 + Arc(const Edge &ue, bool _forward) :
4.60 + Edge(ue), forward(_forward) {}
4.61 +
4.62 + public:
4.63 + Arc() {}
4.64 +
4.65 + /// Invalid arc constructor
4.66 + Arc(Invalid i) : Edge(i), forward(true) {}
4.67 +
4.68 + bool operator==(const Arc &that) const {
4.69 + return forward==that.forward && Edge(*this)==Edge(that);
4.70 + }
4.71 + bool operator!=(const Arc &that) const {
4.72 + return forward!=that.forward || Edge(*this)!=Edge(that);
4.73 + }
4.74 + bool operator<(const Arc &that) const {
4.75 + return forward<that.forward ||
4.76 + (!(that.forward<forward) && Edge(*this)<Edge(that));
4.77 + }
4.78 + };
4.79 +
4.80 +
4.81 +
4.82 + using Parent::source;
4.83 +
4.84 + /// Source of the given Arc.
4.85 + Node source(const Arc &e) const {
4.86 + return e.forward ? Parent::source(e) : Parent::target(e);
4.87 + }
4.88 +
4.89 + using Parent::target;
4.90 +
4.91 + /// Target of the given Arc.
4.92 + Node target(const Arc &e) const {
4.93 + return e.forward ? Parent::target(e) : Parent::source(e);
4.94 + }
4.95 +
4.96 + /// \brief Directed arc from an edge.
4.97 + ///
4.98 + /// Returns a directed arc corresponding to the specified Edge.
4.99 + /// If the given bool is true the given edge and the
4.100 + /// returned arc have the same source node.
4.101 + static Arc direct(const Edge &ue, bool d) {
4.102 + return Arc(ue, d);
4.103 + }
4.104 +
4.105 + /// Returns whether the given directed arc is same orientation as the
4.106 + /// corresponding edge.
4.107 + ///
4.108 + /// \todo reference to the corresponding point of the undirected digraph
4.109 + /// concept. "What does the direction of an edge mean?"
4.110 + static bool direction(const Arc &e) { return e.forward; }
4.111 +
4.112 +
4.113 + using Parent::first;
4.114 + using Parent::next;
4.115 +
4.116 + void first(Arc &e) const {
4.117 + Parent::first(e);
4.118 + e.forward=true;
4.119 + }
4.120 +
4.121 + void next(Arc &e) const {
4.122 + if( e.forward ) {
4.123 + e.forward = false;
4.124 + }
4.125 + else {
4.126 + Parent::next(e);
4.127 + e.forward = true;
4.128 + }
4.129 + }
4.130 +
4.131 + void firstOut(Arc &e, const Node &n) const {
4.132 + Parent::firstIn(e,n);
4.133 + if( Edge(e) != INVALID ) {
4.134 + e.forward = false;
4.135 + }
4.136 + else {
4.137 + Parent::firstOut(e,n);
4.138 + e.forward = true;
4.139 + }
4.140 + }
4.141 + void nextOut(Arc &e) const {
4.142 + if( ! e.forward ) {
4.143 + Node n = Parent::target(e);
4.144 + Parent::nextIn(e);
4.145 + if( Edge(e) == INVALID ) {
4.146 + Parent::firstOut(e, n);
4.147 + e.forward = true;
4.148 + }
4.149 + }
4.150 + else {
4.151 + Parent::nextOut(e);
4.152 + }
4.153 + }
4.154 +
4.155 + void firstIn(Arc &e, const Node &n) const {
4.156 + Parent::firstOut(e,n);
4.157 + if( Edge(e) != INVALID ) {
4.158 + e.forward = false;
4.159 + }
4.160 + else {
4.161 + Parent::firstIn(e,n);
4.162 + e.forward = true;
4.163 + }
4.164 + }
4.165 + void nextIn(Arc &e) const {
4.166 + if( ! e.forward ) {
4.167 + Node n = Parent::source(e);
4.168 + Parent::nextOut(e);
4.169 + if( Edge(e) == INVALID ) {
4.170 + Parent::firstIn(e, n);
4.171 + e.forward = true;
4.172 + }
4.173 + }
4.174 + else {
4.175 + Parent::nextIn(e);
4.176 + }
4.177 + }
4.178 +
4.179 + void firstInc(Edge &e, bool &d, const Node &n) const {
4.180 + d = true;
4.181 + Parent::firstOut(e, n);
4.182 + if (e != INVALID) return;
4.183 + d = false;
4.184 + Parent::firstIn(e, n);
4.185 + }
4.186 +
4.187 + void nextInc(Edge &e, bool &d) const {
4.188 + if (d) {
4.189 + Node s = Parent::source(e);
4.190 + Parent::nextOut(e);
4.191 + if (e != INVALID) return;
4.192 + d = false;
4.193 + Parent::firstIn(e, s);
4.194 + } else {
4.195 + Parent::nextIn(e);
4.196 + }
4.197 + }
4.198 +
4.199 + Node nodeFromId(int ix) const {
4.200 + return Parent::nodeFromId(ix);
4.201 + }
4.202 +
4.203 + Arc arcFromId(int ix) const {
4.204 + return direct(Parent::arcFromId(ix >> 1), bool(ix & 1));
4.205 + }
4.206 +
4.207 + Edge edgeFromId(int ix) const {
4.208 + return Parent::arcFromId(ix);
4.209 + }
4.210 +
4.211 + int id(const Node &n) const {
4.212 + return Parent::id(n);
4.213 + }
4.214 +
4.215 + int id(const Edge &e) const {
4.216 + return Parent::id(e);
4.217 + }
4.218 +
4.219 + int id(const Arc &e) const {
4.220 + return 2 * Parent::id(e) + int(e.forward);
4.221 + }
4.222 +
4.223 + int maxNodeId() const {
4.224 + return Parent::maxNodeId();
4.225 + }
4.226 +
4.227 + int maxArcId() const {
4.228 + return 2 * Parent::maxArcId() + 1;
4.229 + }
4.230 +
4.231 + int maxEdgeId() const {
4.232 + return Parent::maxArcId();
4.233 + }
4.234 +
4.235 +
4.236 + int arcNum() const {
4.237 + return 2 * Parent::arcNum();
4.238 + }
4.239 +
4.240 + int edgeNum() const {
4.241 + return Parent::arcNum();
4.242 + }
4.243 +
4.244 + Arc findArc(Node s, Node t, Arc p = INVALID) const {
4.245 + if (p == INVALID) {
4.246 + Edge arc = Parent::findArc(s, t);
4.247 + if (arc != INVALID) return direct(arc, true);
4.248 + arc = Parent::findArc(t, s);
4.249 + if (arc != INVALID) return direct(arc, false);
4.250 + } else if (direction(p)) {
4.251 + Edge arc = Parent::findArc(s, t, p);
4.252 + if (arc != INVALID) return direct(arc, true);
4.253 + arc = Parent::findArc(t, s);
4.254 + if (arc != INVALID) return direct(arc, false);
4.255 + } else {
4.256 + Edge arc = Parent::findArc(t, s, p);
4.257 + if (arc != INVALID) return direct(arc, false);
4.258 + }
4.259 + return INVALID;
4.260 + }
4.261 +
4.262 + Edge findEdge(Node s, Node t, Edge p = INVALID) const {
4.263 + if (s != t) {
4.264 + if (p == INVALID) {
4.265 + Edge arc = Parent::findArc(s, t);
4.266 + if (arc != INVALID) return arc;
4.267 + arc = Parent::findArc(t, s);
4.268 + if (arc != INVALID) return arc;
4.269 + } else if (Parent::s(p) == s) {
4.270 + Edge arc = Parent::findArc(s, t, p);
4.271 + if (arc != INVALID) return arc;
4.272 + arc = Parent::findArc(t, s);
4.273 + if (arc != INVALID) return arc;
4.274 + } else {
4.275 + Edge arc = Parent::findArc(t, s, p);
4.276 + if (arc != INVALID) return arc;
4.277 + }
4.278 + } else {
4.279 + return Parent::findArc(s, t, p);
4.280 + }
4.281 + return INVALID;
4.282 + }
4.283 + };
4.284 +
4.285 + template <typename Base>
4.286 + class BidirBpGraphExtender : public Base {
4.287 + public:
4.288 + typedef Base Parent;
4.289 + typedef BidirBpGraphExtender Digraph;
4.290 +
4.291 + typedef typename Parent::Node Node;
4.292 + typedef typename Parent::Edge Edge;
4.293 +
4.294 +
4.295 + using Parent::first;
4.296 + using Parent::next;
4.297 +
4.298 + using Parent::id;
4.299 +
4.300 + class Red : public Node {
4.301 + friend class BidirBpGraphExtender;
4.302 + public:
4.303 + Red() {}
4.304 + Red(const Node& node) : Node(node) {
4.305 + LEMON_ASSERT(Parent::red(node) || node == INVALID,
4.306 + typename Parent::NodeSetError());
4.307 + }
4.308 + Red& operator=(const Node& node) {
4.309 + LEMON_ASSERT(Parent::red(node) || node == INVALID,
4.310 + typename Parent::NodeSetError());
4.311 + Node::operator=(node);
4.312 + return *this;
4.313 + }
4.314 + Red(Invalid) : Node(INVALID) {}
4.315 + Red& operator=(Invalid) {
4.316 + Node::operator=(INVALID);
4.317 + return *this;
4.318 + }
4.319 + };
4.320 +
4.321 + void first(Red& node) const {
4.322 + Parent::firstRed(static_cast<Node&>(node));
4.323 + }
4.324 + void next(Red& node) const {
4.325 + Parent::nextRed(static_cast<Node&>(node));
4.326 + }
4.327 +
4.328 + int id(const Red& node) const {
4.329 + return Parent::redId(node);
4.330 + }
4.331 +
4.332 + class Blue : public Node {
4.333 + friend class BidirBpGraphExtender;
4.334 + public:
4.335 + Blue() {}
4.336 + Blue(const Node& node) : Node(node) {
4.337 + LEMON_ASSERT(Parent::blue(node) || node == INVALID,
4.338 + typename Parent::NodeSetError());
4.339 + }
4.340 + Blue& operator=(const Node& node) {
4.341 + LEMON_ASSERT(Parent::blue(node) || node == INVALID,
4.342 + typename Parent::NodeSetError());
4.343 + Node::operator=(node);
4.344 + return *this;
4.345 + }
4.346 + Blue(Invalid) : Node(INVALID) {}
4.347 + Blue& operator=(Invalid) {
4.348 + Node::operator=(INVALID);
4.349 + return *this;
4.350 + }
4.351 + };
4.352 +
4.353 + void first(Blue& node) const {
4.354 + Parent::firstBlue(static_cast<Node&>(node));
4.355 + }
4.356 + void next(Blue& node) const {
4.357 + Parent::nextBlue(static_cast<Node&>(node));
4.358 + }
4.359 +
4.360 + int id(const Blue& node) const {
4.361 + return Parent::redId(node);
4.362 + }
4.363 +
4.364 + Node source(const Edge& arc) const {
4.365 + return red(arc);
4.366 + }
4.367 + Node target(const Edge& arc) const {
4.368 + return blue(arc);
4.369 + }
4.370 +
4.371 + void firstInc(Edge& arc, bool& dir, const Node& node) const {
4.372 + if (Parent::red(node)) {
4.373 + Parent::firstFromRed(arc, node);
4.374 + dir = true;
4.375 + } else {
4.376 + Parent::firstFromBlue(arc, node);
4.377 + dir = static_cast<Edge&>(arc) == INVALID;
4.378 + }
4.379 + }
4.380 + void nextInc(Edge& arc, bool& dir) const {
4.381 + if (dir) {
4.382 + Parent::nextFromRed(arc);
4.383 + } else {
4.384 + Parent::nextFromBlue(arc);
4.385 + if (arc == INVALID) dir = true;
4.386 + }
4.387 + }
4.388 +
4.389 + class Arc : public Edge {
4.390 + friend class BidirBpGraphExtender;
4.391 + protected:
4.392 + bool forward;
4.393 +
4.394 + Arc(const Edge& arc, bool _forward)
4.395 + : Edge(arc), forward(_forward) {}
4.396 +
4.397 + public:
4.398 + Arc() {}
4.399 + Arc (Invalid) : Edge(INVALID), forward(true) {}
4.400 + bool operator==(const Arc& i) const {
4.401 + return Edge::operator==(i) && forward == i.forward;
4.402 + }
4.403 + bool operator!=(const Arc& i) const {
4.404 + return Edge::operator!=(i) || forward != i.forward;
4.405 + }
4.406 + bool operator<(const Arc& i) const {
4.407 + return Edge::operator<(i) ||
4.408 + (!(i.forward<forward) && Edge(*this)<Edge(i));
4.409 + }
4.410 + };
4.411 +
4.412 + void first(Arc& arc) const {
4.413 + Parent::first(static_cast<Edge&>(arc));
4.414 + arc.forward = true;
4.415 + }
4.416 +
4.417 + void next(Arc& arc) const {
4.418 + if (!arc.forward) {
4.419 + Parent::next(static_cast<Edge&>(arc));
4.420 + }
4.421 + arc.forward = !arc.forward;
4.422 + }
4.423 +
4.424 + void firstOut(Arc& arc, const Node& node) const {
4.425 + if (Parent::red(node)) {
4.426 + Parent::firstFromRed(arc, node);
4.427 + arc.forward = true;
4.428 + } else {
4.429 + Parent::firstFromBlue(arc, node);
4.430 + arc.forward = static_cast<Edge&>(arc) == INVALID;
4.431 + }
4.432 + }
4.433 + void nextOut(Arc& arc) const {
4.434 + if (arc.forward) {
4.435 + Parent::nextFromRed(arc);
4.436 + } else {
4.437 + Parent::nextFromBlue(arc);
4.438 + arc.forward = static_cast<Edge&>(arc) == INVALID;
4.439 + }
4.440 + }
4.441 +
4.442 + void firstIn(Arc& arc, const Node& node) const {
4.443 + if (Parent::blue(node)) {
4.444 + Parent::firstFromBlue(arc, node);
4.445 + arc.forward = true;
4.446 + } else {
4.447 + Parent::firstFromRed(arc, node);
4.448 + arc.forward = static_cast<Edge&>(arc) == INVALID;
4.449 + }
4.450 + }
4.451 + void nextIn(Arc& arc) const {
4.452 + if (arc.forward) {
4.453 + Parent::nextFromBlue(arc);
4.454 + } else {
4.455 + Parent::nextFromRed(arc);
4.456 + arc.forward = static_cast<Edge&>(arc) == INVALID;
4.457 + }
4.458 + }
4.459 +
4.460 + Node source(const Arc& arc) const {
4.461 + return arc.forward ? Parent::red(arc) : Parent::blue(arc);
4.462 + }
4.463 + Node target(const Arc& arc) const {
4.464 + return arc.forward ? Parent::blue(arc) : Parent::red(arc);
4.465 + }
4.466 +
4.467 + int id(const Arc& arc) const {
4.468 + return (Parent::id(static_cast<const Edge&>(arc)) << 1) +
4.469 + (arc.forward ? 0 : 1);
4.470 + }
4.471 + Arc arcFromId(int ix) const {
4.472 + return Arc(Parent::fromEdgeId(ix >> 1), (ix & 1) == 0);
4.473 + }
4.474 + int maxArcId() const {
4.475 + return (Parent::maxEdgeId() << 1) + 1;
4.476 + }
4.477 +
4.478 + bool direction(const Arc& arc) const {
4.479 + return arc.forward;
4.480 + }
4.481 +
4.482 + Arc direct(const Edge& arc, bool dir) const {
4.483 + return Arc(arc, dir);
4.484 + }
4.485 +
4.486 + int arcNum() const {
4.487 + return 2 * Parent::edgeNum();
4.488 + }
4.489 +
4.490 + int edgeNum() const {
4.491 + return Parent::edgeNum();
4.492 + }
4.493 +
4.494 +
4.495 + };
4.496 +}
4.497 +
4.498 +#endif
5.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
5.2 +++ b/lemon/bits/default_map.h Sun Jan 20 20:43:48 2008 +0100
5.3 @@ -0,0 +1,181 @@
5.4 +/* -*- C++ -*-
5.5 + *
5.6 + * This file is a part of LEMON, a generic C++ optimization library
5.7 + *
5.8 + * Copyright (C) 2003-2007
5.9 + * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
5.10 + * (Egervary Research Group on Combinatorial Optimization, EGRES).
5.11 + *
5.12 + * Permission to use, modify and distribute this software is granted
5.13 + * provided that this copyright notice appears in all copies. For
5.14 + * precise terms see the accompanying LICENSE file.
5.15 + *
5.16 + * This software is provided "AS IS" with no warranty of any kind,
5.17 + * express or implied, and with no claim as to its suitability for any
5.18 + * purpose.
5.19 + *
5.20 + */
5.21 +
5.22 +#ifndef LEMON_BITS_DEFAULT_MAP_H
5.23 +#define LEMON_BITS_DEFAULT_MAP_H
5.24 +
5.25 +
5.26 +#include <lemon/bits/array_map.h>
5.27 +#include <lemon/bits/vector_map.h>
5.28 +//#include <lemon/bits/debug_map.h>
5.29 +
5.30 +///\ingroup graphbits
5.31 +///\file
5.32 +///\brief Graph maps that construct and destruct their elements dynamically.
5.33 +
5.34 +namespace lemon {
5.35 +
5.36 +
5.37 + //#ifndef LEMON_USE_DEBUG_MAP
5.38 +
5.39 + template <typename _Graph, typename _Item, typename _Value>
5.40 + struct DefaultMapSelector {
5.41 + typedef ArrayMap<_Graph, _Item, _Value> Map;
5.42 + };
5.43 +
5.44 + // bool
5.45 + template <typename _Graph, typename _Item>
5.46 + struct DefaultMapSelector<_Graph, _Item, bool> {
5.47 + typedef VectorMap<_Graph, _Item, bool> Map;
5.48 + };
5.49 +
5.50 + // char
5.51 + template <typename _Graph, typename _Item>
5.52 + struct DefaultMapSelector<_Graph, _Item, char> {
5.53 + typedef VectorMap<_Graph, _Item, char> Map;
5.54 + };
5.55 +
5.56 + template <typename _Graph, typename _Item>
5.57 + struct DefaultMapSelector<_Graph, _Item, signed char> {
5.58 + typedef VectorMap<_Graph, _Item, signed char> Map;
5.59 + };
5.60 +
5.61 + template <typename _Graph, typename _Item>
5.62 + struct DefaultMapSelector<_Graph, _Item, unsigned char> {
5.63 + typedef VectorMap<_Graph, _Item, unsigned char> Map;
5.64 + };
5.65 +
5.66 +
5.67 + // int
5.68 + template <typename _Graph, typename _Item>
5.69 + struct DefaultMapSelector<_Graph, _Item, signed int> {
5.70 + typedef VectorMap<_Graph, _Item, signed int> Map;
5.71 + };
5.72 +
5.73 + template <typename _Graph, typename _Item>
5.74 + struct DefaultMapSelector<_Graph, _Item, unsigned int> {
5.75 + typedef VectorMap<_Graph, _Item, unsigned int> Map;
5.76 + };
5.77 +
5.78 +
5.79 + // short
5.80 + template <typename _Graph, typename _Item>
5.81 + struct DefaultMapSelector<_Graph, _Item, signed short> {
5.82 + typedef VectorMap<_Graph, _Item, signed short> Map;
5.83 + };
5.84 +
5.85 + template <typename _Graph, typename _Item>
5.86 + struct DefaultMapSelector<_Graph, _Item, unsigned short> {
5.87 + typedef VectorMap<_Graph, _Item, unsigned short> Map;
5.88 + };
5.89 +
5.90 +
5.91 + // long
5.92 + template <typename _Graph, typename _Item>
5.93 + struct DefaultMapSelector<_Graph, _Item, signed long> {
5.94 + typedef VectorMap<_Graph, _Item, signed long> Map;
5.95 + };
5.96 +
5.97 + template <typename _Graph, typename _Item>
5.98 + struct DefaultMapSelector<_Graph, _Item, unsigned long> {
5.99 + typedef VectorMap<_Graph, _Item, unsigned long> Map;
5.100 + };
5.101 +
5.102 +
5.103 +#if defined __GNUC__ && !defined __STRICT_ANSI__
5.104 +
5.105 + // long long
5.106 + template <typename _Graph, typename _Item>
5.107 + struct DefaultMapSelector<_Graph, _Item, signed long long> {
5.108 + typedef VectorMap<_Graph, _Item, signed long long> Map;
5.109 + };
5.110 +
5.111 + template <typename _Graph, typename _Item>
5.112 + struct DefaultMapSelector<_Graph, _Item, unsigned long long> {
5.113 + typedef VectorMap<_Graph, _Item, unsigned long long> Map;
5.114 + };
5.115 +
5.116 +#endif
5.117 +
5.118 +
5.119 + // float
5.120 + template <typename _Graph, typename _Item>
5.121 + struct DefaultMapSelector<_Graph, _Item, float> {
5.122 + typedef VectorMap<_Graph, _Item, float> Map;
5.123 + };
5.124 +
5.125 +
5.126 + // double
5.127 + template <typename _Graph, typename _Item>
5.128 + struct DefaultMapSelector<_Graph, _Item, double> {
5.129 + typedef VectorMap<_Graph, _Item, double> Map;
5.130 + };
5.131 +
5.132 +
5.133 + // long double
5.134 + template <typename _Graph, typename _Item>
5.135 + struct DefaultMapSelector<_Graph, _Item, long double> {
5.136 + typedef VectorMap<_Graph, _Item, long double> Map;
5.137 + };
5.138 +
5.139 +
5.140 + // pointer
5.141 + template <typename _Graph, typename _Item, typename _Ptr>
5.142 + struct DefaultMapSelector<_Graph, _Item, _Ptr*> {
5.143 + typedef VectorMap<_Graph, _Item, _Ptr*> Map;
5.144 + };
5.145 +
5.146 +// #else
5.147 +
5.148 +// template <typename _Graph, typename _Item, typename _Value>
5.149 +// struct DefaultMapSelector {
5.150 +// typedef DebugMap<_Graph, _Item, _Value> Map;
5.151 +// };
5.152 +
5.153 +// #endif
5.154 +
5.155 + /// \e
5.156 + template <typename _Graph, typename _Item, typename _Value>
5.157 + class DefaultMap
5.158 + : public DefaultMapSelector<_Graph, _Item, _Value>::Map {
5.159 + public:
5.160 + typedef typename DefaultMapSelector<_Graph, _Item, _Value>::Map Parent;
5.161 + typedef DefaultMap<_Graph, _Item, _Value> Map;
5.162 +
5.163 + typedef typename Parent::Graph Graph;
5.164 + typedef typename Parent::Value Value;
5.165 +
5.166 + explicit DefaultMap(const Graph& graph) : Parent(graph) {}
5.167 + DefaultMap(const Graph& graph, const Value& value)
5.168 + : Parent(graph, value) {}
5.169 +
5.170 + DefaultMap& operator=(const DefaultMap& cmap) {
5.171 + return operator=<DefaultMap>(cmap);
5.172 + }
5.173 +
5.174 + template <typename CMap>
5.175 + DefaultMap& operator=(const CMap& cmap) {
5.176 + Parent::operator=(cmap);
5.177 + return *this;
5.178 + }
5.179 +
5.180 + };
5.181 +
5.182 +}
5.183 +
5.184 +#endif
6.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
6.2 +++ b/lemon/bits/graph_extender.h Sun Jan 20 20:43:48 2008 +0100
6.3 @@ -0,0 +1,747 @@
6.4 +/* -*- C++ -*-
6.5 + *
6.6 + * This file is a part of LEMON, a generic C++ optimization library
6.7 + *
6.8 + * Copyright (C) 2003-2007
6.9 + * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
6.10 + * (Egervary Research Group on Combinatorial Optimization, EGRES).
6.11 + *
6.12 + * Permission to use, modify and distribute this software is granted
6.13 + * provided that this copyright notice appears in all copies. For
6.14 + * precise terms see the accompanying LICENSE file.
6.15 + *
6.16 + * This software is provided "AS IS" with no warranty of any kind,
6.17 + * express or implied, and with no claim as to its suitability for any
6.18 + * purpose.
6.19 + *
6.20 + */
6.21 +
6.22 +#ifndef LEMON_BITS_GRAPH_EXTENDER_H
6.23 +#define LEMON_BITS_GRAPH_EXTENDER_H
6.24 +
6.25 +#include <lemon/bits/invalid.h>
6.26 +
6.27 +#include <lemon/bits/map_extender.h>
6.28 +#include <lemon/bits/default_map.h>
6.29 +
6.30 +#include <lemon/concept_check.h>
6.31 +#include <lemon/concepts/maps.h>
6.32 +
6.33 +///\ingroup graphbits
6.34 +///\file
6.35 +///\brief Extenders for the digraph types
6.36 +namespace lemon {
6.37 +
6.38 + /// \ingroup graphbits
6.39 + ///
6.40 + /// \brief Extender for the Digraphs
6.41 + template <typename Base>
6.42 + class DigraphExtender : public Base {
6.43 + public:
6.44 +
6.45 + typedef Base Parent;
6.46 + typedef DigraphExtender Digraph;
6.47 +
6.48 + // Base extensions
6.49 +
6.50 + typedef typename Parent::Node Node;
6.51 + typedef typename Parent::Arc Arc;
6.52 +
6.53 + int maxId(Node) const {
6.54 + return Parent::maxNodeId();
6.55 + }
6.56 +
6.57 + int maxId(Arc) const {
6.58 + return Parent::maxArcId();
6.59 + }
6.60 +
6.61 + Node fromId(int id, Node) const {
6.62 + return Parent::nodeFromId(id);
6.63 + }
6.64 +
6.65 + Arc fromId(int id, Arc) const {
6.66 + return Parent::arcFromId(id);
6.67 + }
6.68 +
6.69 + Node oppositeNode(const Node &n, const Arc &e) const {
6.70 + if (n == Parent::source(e))
6.71 + return Parent::target(e);
6.72 + else if(n==Parent::target(e))
6.73 + return Parent::source(e);
6.74 + else
6.75 + return INVALID;
6.76 + }
6.77 +
6.78 + // Alterable extension
6.79 +
6.80 + typedef AlterationNotifier<DigraphExtender, Node> NodeNotifier;
6.81 + typedef AlterationNotifier<DigraphExtender, Arc> ArcNotifier;
6.82 +
6.83 +
6.84 + protected:
6.85 +
6.86 + mutable NodeNotifier node_notifier;
6.87 + mutable ArcNotifier arc_notifier;
6.88 +
6.89 + public:
6.90 +
6.91 + NodeNotifier& notifier(Node) const {
6.92 + return node_notifier;
6.93 + }
6.94 +
6.95 + ArcNotifier& notifier(Arc) const {
6.96 + return arc_notifier;
6.97 + }
6.98 +
6.99 + class NodeIt : public Node {
6.100 + const Digraph* digraph;
6.101 + public:
6.102 +
6.103 + NodeIt() {}
6.104 +
6.105 + NodeIt(Invalid i) : Node(i) { }
6.106 +
6.107 + explicit NodeIt(const Digraph& _digraph) : digraph(&_digraph) {
6.108 + _digraph.first(static_cast<Node&>(*this));
6.109 + }
6.110 +
6.111 + NodeIt(const Digraph& _digraph, const Node& node)
6.112 + : Node(node), digraph(&_digraph) {}
6.113 +
6.114 + NodeIt& operator++() {
6.115 + digraph->next(*this);
6.116 + return *this;
6.117 + }
6.118 +
6.119 + };
6.120 +
6.121 +
6.122 + class ArcIt : public Arc {
6.123 + const Digraph* digraph;
6.124 + public:
6.125 +
6.126 + ArcIt() { }
6.127 +
6.128 + ArcIt(Invalid i) : Arc(i) { }
6.129 +
6.130 + explicit ArcIt(const Digraph& _digraph) : digraph(&_digraph) {
6.131 + _digraph.first(static_cast<Arc&>(*this));
6.132 + }
6.133 +
6.134 + ArcIt(const Digraph& _digraph, const Arc& e) :
6.135 + Arc(e), digraph(&_digraph) { }
6.136 +
6.137 + ArcIt& operator++() {
6.138 + digraph->next(*this);
6.139 + return *this;
6.140 + }
6.141 +
6.142 + };
6.143 +
6.144 +
6.145 + class OutArcIt : public Arc {
6.146 + const Digraph* digraph;
6.147 + public:
6.148 +
6.149 + OutArcIt() { }
6.150 +
6.151 + OutArcIt(Invalid i) : Arc(i) { }
6.152 +
6.153 + OutArcIt(const Digraph& _digraph, const Node& node)
6.154 + : digraph(&_digraph) {
6.155 + _digraph.firstOut(*this, node);
6.156 + }
6.157 +
6.158 + OutArcIt(const Digraph& _digraph, const Arc& arc)
6.159 + : Arc(arc), digraph(&_digraph) {}
6.160 +
6.161 + OutArcIt& operator++() {
6.162 + digraph->nextOut(*this);
6.163 + return *this;
6.164 + }
6.165 +
6.166 + };
6.167 +
6.168 +
6.169 + class InArcIt : public Arc {
6.170 + const Digraph* digraph;
6.171 + public:
6.172 +
6.173 + InArcIt() { }
6.174 +
6.175 + InArcIt(Invalid i) : Arc(i) { }
6.176 +
6.177 + InArcIt(const Digraph& _digraph, const Node& node)
6.178 + : digraph(&_digraph) {
6.179 + _digraph.firstIn(*this, node);
6.180 + }
6.181 +
6.182 + InArcIt(const Digraph& _digraph, const Arc& arc) :
6.183 + Arc(arc), digraph(&_digraph) {}
6.184 +
6.185 + InArcIt& operator++() {
6.186 + digraph->nextIn(*this);
6.187 + return *this;
6.188 + }
6.189 +
6.190 + };
6.191 +
6.192 + /// \brief Base node of the iterator
6.193 + ///
6.194 + /// Returns the base node (i.e. the source in this case) of the iterator
6.195 + Node baseNode(const OutArcIt &e) const {
6.196 + return Parent::source(e);
6.197 + }
6.198 + /// \brief Running node of the iterator
6.199 + ///
6.200 + /// Returns the running node (i.e. the target in this case) of the
6.201 + /// iterator
6.202 + Node runningNode(const OutArcIt &e) const {
6.203 + return Parent::target(e);
6.204 + }
6.205 +
6.206 + /// \brief Base node of the iterator
6.207 + ///
6.208 + /// Returns the base node (i.e. the target in this case) of the iterator
6.209 + Node baseNode(const InArcIt &e) const {
6.210 + return Parent::target(e);
6.211 + }
6.212 + /// \brief Running node of the iterator
6.213 + ///
6.214 + /// Returns the running node (i.e. the source in this case) of the
6.215 + /// iterator
6.216 + Node runningNode(const InArcIt &e) const {
6.217 + return Parent::source(e);
6.218 + }
6.219 +
6.220 +
6.221 + template <typename _Value>
6.222 + class NodeMap
6.223 + : public MapExtender<DefaultMap<Digraph, Node, _Value> > {
6.224 + public:
6.225 + typedef DigraphExtender Digraph;
6.226 + typedef MapExtender<DefaultMap<Digraph, Node, _Value> > Parent;
6.227 +
6.228 + explicit NodeMap(const Digraph& digraph)
6.229 + : Parent(digraph) {}
6.230 + NodeMap(const Digraph& digraph, const _Value& value)
6.231 + : Parent(digraph, value) {}
6.232 +
6.233 + NodeMap& operator=(const NodeMap& cmap) {
6.234 + return operator=<NodeMap>(cmap);
6.235 + }
6.236 +
6.237 + template <typename CMap>
6.238 + NodeMap& operator=(const CMap& cmap) {
6.239 + Parent::operator=(cmap);
6.240 + return *this;
6.241 + }
6.242 +
6.243 + };
6.244 +
6.245 + template <typename _Value>
6.246 + class ArcMap
6.247 + : public MapExtender<DefaultMap<Digraph, Arc, _Value> > {
6.248 + public:
6.249 + typedef DigraphExtender Digraph;
6.250 + typedef MapExtender<DefaultMap<Digraph, Arc, _Value> > Parent;
6.251 +
6.252 + explicit ArcMap(const Digraph& digraph)
6.253 + : Parent(digraph) {}
6.254 + ArcMap(const Digraph& digraph, const _Value& value)
6.255 + : Parent(digraph, value) {}
6.256 +
6.257 + ArcMap& operator=(const ArcMap& cmap) {
6.258 + return operator=<ArcMap>(cmap);
6.259 + }
6.260 +
6.261 + template <typename CMap>
6.262 + ArcMap& operator=(const CMap& cmap) {
6.263 + Parent::operator=(cmap);
6.264 + return *this;
6.265 + }
6.266 + };
6.267 +
6.268 +
6.269 + Node addNode() {
6.270 + Node node = Parent::addNode();
6.271 + notifier(Node()).add(node);
6.272 + return node;
6.273 + }
6.274 +
6.275 + Arc addArc(const Node& from, const Node& to) {
6.276 + Arc arc = Parent::addArc(from, to);
6.277 + notifier(Arc()).add(arc);
6.278 + return arc;
6.279 + }
6.280 +
6.281 + void clear() {
6.282 + notifier(Arc()).clear();
6.283 + notifier(Node()).clear();
6.284 + Parent::clear();
6.285 + }
6.286 +
6.287 + template <typename Digraph, typename NodeRefMap, typename ArcRefMap>
6.288 + void build(const Digraph& digraph, NodeRefMap& nodeRef, ArcRefMap& arcRef) {
6.289 + Parent::build(digraph, nodeRef, arcRef);
6.290 + notifier(Node()).build();
6.291 + notifier(Arc()).build();
6.292 + }
6.293 +
6.294 + void erase(const Node& node) {
6.295 + Arc arc;
6.296 + Parent::firstOut(arc, node);
6.297 + while (arc != INVALID ) {
6.298 + erase(arc);
6.299 + Parent::firstOut(arc, node);
6.300 + }
6.301 +
6.302 + Parent::firstIn(arc, node);
6.303 + while (arc != INVALID ) {
6.304 + erase(arc);
6.305 + Parent::firstIn(arc, node);
6.306 + }
6.307 +
6.308 + notifier(Node()).erase(node);
6.309 + Parent::erase(node);
6.310 + }
6.311 +
6.312 + void erase(const Arc& arc) {
6.313 + notifier(Arc()).erase(arc);
6.314 + Parent::erase(arc);
6.315 + }
6.316 +
6.317 + DigraphExtender() {
6.318 + node_notifier.setContainer(*this);
6.319 + arc_notifier.setContainer(*this);
6.320 + }
6.321 +
6.322 +
6.323 + ~DigraphExtender() {
6.324 + arc_notifier.clear();
6.325 + node_notifier.clear();
6.326 + }
6.327 + };
6.328 +
6.329 + /// \ingroup graphbits
6.330 + ///
6.331 + /// \brief Extender for the Graphs
6.332 + template <typename Base>
6.333 + class GraphExtender : public Base {
6.334 + public:
6.335 +
6.336 + typedef Base Parent;
6.337 + typedef GraphExtender Digraph;
6.338 +
6.339 + typedef typename Parent::Node Node;
6.340 + typedef typename Parent::Arc Arc;
6.341 + typedef typename Parent::Edge Edge;
6.342 +
6.343 + // Graph extension
6.344 +
6.345 + int maxId(Node) const {
6.346 + return Parent::maxNodeId();
6.347 + }
6.348 +
6.349 + int maxId(Arc) const {
6.350 + return Parent::maxArcId();
6.351 + }
6.352 +
6.353 + int maxId(Edge) const {
6.354 + return Parent::maxEdgeId();
6.355 + }
6.356 +
6.357 + Node fromId(int id, Node) const {
6.358 + return Parent::nodeFromId(id);
6.359 + }
6.360 +
6.361 + Arc fromId(int id, Arc) const {
6.362 + return Parent::arcFromId(id);
6.363 + }
6.364 +
6.365 + Edge fromId(int id, Edge) const {
6.366 + return Parent::edgeFromId(id);
6.367 + }
6.368 +
6.369 + Node oppositeNode(const Node &n, const Edge &e) const {
6.370 + if( n == Parent::source(e))
6.371 + return Parent::target(e);
6.372 + else if( n == Parent::target(e))
6.373 + return Parent::source(e);
6.374 + else
6.375 + return INVALID;
6.376 + }
6.377 +
6.378 + Arc oppositeArc(const Arc &e) const {
6.379 + return Parent::direct(e, !Parent::direction(e));
6.380 + }
6.381 +
6.382 + using Parent::direct;
6.383 + Arc direct(const Edge &ue, const Node &s) const {
6.384 + return Parent::direct(ue, Parent::source(ue) == s);
6.385 + }
6.386 +
6.387 + // Alterable extension
6.388 +
6.389 + typedef AlterationNotifier<GraphExtender, Node> NodeNotifier;
6.390 + typedef AlterationNotifier<GraphExtender, Arc> ArcNotifier;
6.391 + typedef AlterationNotifier<GraphExtender, Edge> EdgeNotifier;
6.392 +
6.393 +
6.394 + protected:
6.395 +
6.396 + mutable NodeNotifier node_notifier;
6.397 + mutable ArcNotifier arc_notifier;
6.398 + mutable EdgeNotifier edge_notifier;
6.399 +
6.400 + public:
6.401 +
6.402 + NodeNotifier& notifier(Node) const {
6.403 + return node_notifier;
6.404 + }
6.405 +
6.406 + ArcNotifier& notifier(Arc) const {
6.407 + return arc_notifier;
6.408 + }
6.409 +
6.410 + EdgeNotifier& notifier(Edge) const {
6.411 + return edge_notifier;
6.412 + }
6.413 +
6.414 +
6.415 +
6.416 + class NodeIt : public Node {
6.417 + const Digraph* digraph;
6.418 + public:
6.419 +
6.420 + NodeIt() {}
6.421 +
6.422 + NodeIt(Invalid i) : Node(i) { }
6.423 +
6.424 + explicit NodeIt(const Digraph& _digraph) : digraph(&_digraph) {
6.425 + _digraph.first(static_cast<Node&>(*this));
6.426 + }
6.427 +
6.428 + NodeIt(const Digraph& _digraph, const Node& node)
6.429 + : Node(node), digraph(&_digraph) {}
6.430 +
6.431 + NodeIt& operator++() {
6.432 + digraph->next(*this);
6.433 + return *this;
6.434 + }
6.435 +
6.436 + };
6.437 +
6.438 +
6.439 + class ArcIt : public Arc {
6.440 + const Digraph* digraph;
6.441 + public:
6.442 +
6.443 + ArcIt() { }
6.444 +
6.445 + ArcIt(Invalid i) : Arc(i) { }
6.446 +
6.447 + explicit ArcIt(const Digraph& _digraph) : digraph(&_digraph) {
6.448 + _digraph.first(static_cast<Arc&>(*this));
6.449 + }
6.450 +
6.451 + ArcIt(const Digraph& _digraph, const Arc& e) :
6.452 + Arc(e), digraph(&_digraph) { }
6.453 +
6.454 + ArcIt& operator++() {
6.455 + digraph->next(*this);
6.456 + return *this;
6.457 + }
6.458 +
6.459 + };
6.460 +
6.461 +
6.462 + class OutArcIt : public Arc {
6.463 + const Digraph* digraph;
6.464 + public:
6.465 +
6.466 + OutArcIt() { }
6.467 +
6.468 + OutArcIt(Invalid i) : Arc(i) { }
6.469 +
6.470 + OutArcIt(const Digraph& _digraph, const Node& node)
6.471 + : digraph(&_digraph) {
6.472 + _digraph.firstOut(*this, node);
6.473 + }
6.474 +
6.475 + OutArcIt(const Digraph& _digraph, const Arc& arc)
6.476 + : Arc(arc), digraph(&_digraph) {}
6.477 +
6.478 + OutArcIt& operator++() {
6.479 + digraph->nextOut(*this);
6.480 + return *this;
6.481 + }
6.482 +
6.483 + };
6.484 +
6.485 +
6.486 + class InArcIt : public Arc {
6.487 + const Digraph* digraph;
6.488 + public:
6.489 +
6.490 + InArcIt() { }
6.491 +
6.492 + InArcIt(Invalid i) : Arc(i) { }
6.493 +
6.494 + InArcIt(const Digraph& _digraph, const Node& node)
6.495 + : digraph(&_digraph) {
6.496 + _digraph.firstIn(*this, node);
6.497 + }
6.498 +
6.499 + InArcIt(const Digraph& _digraph, const Arc& arc) :
6.500 + Arc(arc), digraph(&_digraph) {}
6.501 +
6.502 + InArcIt& operator++() {
6.503 + digraph->nextIn(*this);
6.504 + return *this;
6.505 + }
6.506 +
6.507 + };
6.508 +
6.509 +
6.510 + class EdgeIt : public Parent::Edge {
6.511 + const Digraph* digraph;
6.512 + public:
6.513 +
6.514 + EdgeIt() { }
6.515 +
6.516 + EdgeIt(Invalid i) : Edge(i) { }
6.517 +
6.518 + explicit EdgeIt(const Digraph& _digraph) : digraph(&_digraph) {
6.519 + _digraph.first(static_cast<Edge&>(*this));
6.520 + }
6.521 +
6.522 + EdgeIt(const Digraph& _digraph, const Edge& e) :
6.523 + Edge(e), digraph(&_digraph) { }
6.524 +
6.525 + EdgeIt& operator++() {
6.526 + digraph->next(*this);
6.527 + return *this;
6.528 + }
6.529 +
6.530 + };
6.531 +
6.532 + class IncArcIt : public Parent::Edge {
6.533 + friend class GraphExtender;
6.534 + const Digraph* digraph;
6.535 + bool direction;
6.536 + public:
6.537 +
6.538 + IncArcIt() { }
6.539 +
6.540 + IncArcIt(Invalid i) : Edge(i), direction(false) { }
6.541 +
6.542 + IncArcIt(const Digraph& _digraph, const Node &n) : digraph(&_digraph) {
6.543 + _digraph.firstInc(*this, direction, n);
6.544 + }
6.545 +
6.546 + IncArcIt(const Digraph& _digraph, const Edge &ue, const Node &n)
6.547 + : digraph(&_digraph), Edge(ue) {
6.548 + direction = (_digraph.source(ue) == n);
6.549 + }
6.550 +
6.551 + IncArcIt& operator++() {
6.552 + digraph->nextInc(*this, direction);
6.553 + return *this;
6.554 + }
6.555 + };
6.556 +
6.557 + /// \brief Base node of the iterator
6.558 + ///
6.559 + /// Returns the base node (ie. the source in this case) of the iterator
6.560 + Node baseNode(const OutArcIt &e) const {
6.561 + return Parent::source(static_cast<const Arc&>(e));
6.562 + }
6.563 + /// \brief Running node of the iterator
6.564 + ///
6.565 + /// Returns the running node (ie. the target in this case) of the
6.566 + /// iterator
6.567 + Node runningNode(const OutArcIt &e) const {
6.568 + return Parent::target(static_cast<const Arc&>(e));
6.569 + }
6.570 +
6.571 + /// \brief Base node of the iterator
6.572 + ///
6.573 + /// Returns the base node (ie. the target in this case) of the iterator
6.574 + Node baseNode(const InArcIt &e) const {
6.575 + return Parent::target(static_cast<const Arc&>(e));
6.576 + }
6.577 + /// \brief Running node of the iterator
6.578 + ///
6.579 + /// Returns the running node (ie. the source in this case) of the
6.580 + /// iterator
6.581 + Node runningNode(const InArcIt &e) const {
6.582 + return Parent::source(static_cast<const Arc&>(e));
6.583 + }
6.584 +
6.585 + /// Base node of the iterator
6.586 + ///
6.587 + /// Returns the base node of the iterator
6.588 + Node baseNode(const IncArcIt &e) const {
6.589 + return e.direction ? source(e) : target(e);
6.590 + }
6.591 + /// Running node of the iterator
6.592 + ///
6.593 + /// Returns the running node of the iterator
6.594 + Node runningNode(const IncArcIt &e) const {
6.595 + return e.direction ? target(e) : source(e);
6.596 + }
6.597 +
6.598 + // Mappable extension
6.599 +
6.600 + template <typename _Value>
6.601 + class NodeMap
6.602 + : public MapExtender<DefaultMap<Digraph, Node, _Value> > {
6.603 + public:
6.604 + typedef GraphExtender Digraph;
6.605 + typedef MapExtender<DefaultMap<Digraph, Node, _Value> > Parent;
6.606 +
6.607 + NodeMap(const Digraph& digraph)
6.608 + : Parent(digraph) {}
6.609 + NodeMap(const Digraph& digraph, const _Value& value)
6.610 + : Parent(digraph, value) {}
6.611 +
6.612 + NodeMap& operator=(const NodeMap& cmap) {
6.613 + return operator=<NodeMap>(cmap);
6.614 + }
6.615 +
6.616 + template <typename CMap>
6.617 + NodeMap& operator=(const CMap& cmap) {
6.618 + Parent::operator=(cmap);
6.619 + return *this;
6.620 + }
6.621 +
6.622 + };
6.623 +
6.624 + template <typename _Value>
6.625 + class ArcMap
6.626 + : public MapExtender<DefaultMap<Digraph, Arc, _Value> > {
6.627 + public:
6.628 + typedef GraphExtender Digraph;
6.629 + typedef MapExtender<DefaultMap<Digraph, Arc, _Value> > Parent;
6.630 +
6.631 + ArcMap(const Digraph& digraph)
6.632 + : Parent(digraph) {}
6.633 + ArcMap(const Digraph& digraph, const _Value& value)
6.634 + : Parent(digraph, value) {}
6.635 +
6.636 + ArcMap& operator=(const ArcMap& cmap) {
6.637 + return operator=<ArcMap>(cmap);
6.638 + }
6.639 +
6.640 + template <typename CMap>
6.641 + ArcMap& operator=(const CMap& cmap) {
6.642 + Parent::operator=(cmap);
6.643 + return *this;
6.644 + }
6.645 + };
6.646 +
6.647 +
6.648 + template <typename _Value>
6.649 + class EdgeMap
6.650 + : public MapExtender<DefaultMap<Digraph, Edge, _Value> > {
6.651 + public:
6.652 + typedef GraphExtender Digraph;
6.653 + typedef MapExtender<DefaultMap<Digraph, Edge, _Value> > Parent;
6.654 +
6.655 + EdgeMap(const Digraph& digraph)
6.656 + : Parent(digraph) {}
6.657 +
6.658 + EdgeMap(const Digraph& digraph, const _Value& value)
6.659 + : Parent(digraph, value) {}
6.660 +
6.661 + EdgeMap& operator=(const EdgeMap& cmap) {
6.662 + return operator=<EdgeMap>(cmap);
6.663 + }
6.664 +
6.665 + template <typename CMap>
6.666 + EdgeMap& operator=(const CMap& cmap) {
6.667 + Parent::operator=(cmap);
6.668 + return *this;
6.669 + }
6.670 +
6.671 + };
6.672 +
6.673 + // Alteration extension
6.674 +
6.675 + Node addNode() {
6.676 + Node node = Parent::addNode();
6.677 + notifier(Node()).add(node);
6.678 + return node;
6.679 + }
6.680 +
6.681 + Edge addEdge(const Node& from, const Node& to) {
6.682 + Edge edge = Parent::addEdge(from, to);
6.683 + notifier(Edge()).add(edge);
6.684 + std::vector<Arc> ev;
6.685 + ev.push_back(Parent::direct(edge, true));
6.686 + ev.push_back(Parent::direct(edge, false));
6.687 + notifier(Arc()).add(ev);
6.688 + return edge;
6.689 + }
6.690 +
6.691 + void clear() {
6.692 + notifier(Arc()).clear();
6.693 + notifier(Edge()).clear();
6.694 + notifier(Node()).clear();
6.695 + Parent::clear();
6.696 + }
6.697 +
6.698 + template <typename Digraph, typename NodeRefMap, typename EdgeRefMap>
6.699 + void build(const Digraph& digraph, NodeRefMap& nodeRef,
6.700 + EdgeRefMap& edgeRef) {
6.701 + Parent::build(digraph, nodeRef, edgeRef);
6.702 + notifier(Node()).build();
6.703 + notifier(Edge()).build();
6.704 + notifier(Arc()).build();
6.705 + }
6.706 +
6.707 + void erase(const Node& node) {
6.708 + Arc arc;
6.709 + Parent::firstOut(arc, node);
6.710 + while (arc != INVALID ) {
6.711 + erase(arc);
6.712 + Parent::firstOut(arc, node);
6.713 + }
6.714 +
6.715 + Parent::firstIn(arc, node);
6.716 + while (arc != INVALID ) {
6.717 + erase(arc);
6.718 + Parent::firstIn(arc, node);
6.719 + }
6.720 +
6.721 + notifier(Node()).erase(node);
6.722 + Parent::erase(node);
6.723 + }
6.724 +
6.725 + void erase(const Edge& edge) {
6.726 + std::vector<Arc> ev;
6.727 + ev.push_back(Parent::direct(edge, true));
6.728 + ev.push_back(Parent::direct(edge, false));
6.729 + notifier(Arc()).erase(ev);
6.730 + notifier(Edge()).erase(edge);
6.731 + Parent::erase(edge);
6.732 + }
6.733 +
6.734 + GraphExtender() {
6.735 + node_notifier.setContainer(*this);
6.736 + arc_notifier.setContainer(*this);
6.737 + edge_notifier.setContainer(*this);
6.738 + }
6.739 +
6.740 + ~GraphExtender() {
6.741 + edge_notifier.clear();
6.742 + arc_notifier.clear();
6.743 + node_notifier.clear();
6.744 + }
6.745 +
6.746 + };
6.747 +
6.748 +}
6.749 +
6.750 +#endif
7.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
7.2 +++ b/lemon/bits/map_extender.h Sun Jan 20 20:43:48 2008 +0100
7.3 @@ -0,0 +1,321 @@
7.4 +/* -*- C++ -*-
7.5 + *
7.6 + * This file is a part of LEMON, a generic C++ optimization library
7.7 + *
7.8 + * Copyright (C) 2003-2007
7.9 + * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7.10 + * (Egervary Research Group on Combinatorial Optimization, EGRES).
7.11 + *
7.12 + * Permission to use, modify and distribute this software is granted
7.13 + * provided that this copyright notice appears in all copies. For
7.14 + * precise terms see the accompanying LICENSE file.
7.15 + *
7.16 + * This software is provided "AS IS" with no warranty of any kind,
7.17 + * express or implied, and with no claim as to its suitability for any
7.18 + * purpose.
7.19 + *
7.20 + */
7.21 +
7.22 +#ifndef LEMON_BITS_MAP_EXTENDER_H
7.23 +#define LEMON_BITS_MAP_EXTENDER_H
7.24 +
7.25 +#include <iterator>
7.26 +
7.27 +#include <lemon/bits/traits.h>
7.28 +
7.29 +#include <lemon/concept_check.h>
7.30 +#include <lemon/concepts/maps.h>
7.31 +
7.32 +///\file
7.33 +///\brief Extenders for iterable maps.
7.34 +
7.35 +namespace lemon {
7.36 +
7.37 + /// \ingroup graphbits
7.38 + ///
7.39 + /// \brief Extender for maps
7.40 + template <typename _Map>
7.41 + class MapExtender : public _Map {
7.42 + public:
7.43 +
7.44 + typedef _Map Parent;
7.45 + typedef MapExtender Map;
7.46 +
7.47 +
7.48 + typedef typename Parent::Graph Graph;
7.49 + typedef typename Parent::Key Item;
7.50 +
7.51 + typedef typename Parent::Key Key;
7.52 + typedef typename Parent::Value Value;
7.53 +
7.54 + class MapIt;
7.55 + class ConstMapIt;
7.56 +
7.57 + friend class MapIt;
7.58 + friend class ConstMapIt;
7.59 +
7.60 + public:
7.61 +
7.62 + MapExtender(const Graph& graph)
7.63 + : Parent(graph) {}
7.64 +
7.65 + MapExtender(const Graph& graph, const Value& value)
7.66 + : Parent(graph, value) {}
7.67 +
7.68 + MapExtender& operator=(const MapExtender& cmap) {
7.69 + return operator=<MapExtender>(cmap);
7.70 + }
7.71 +
7.72 + template <typename CMap>
7.73 + MapExtender& operator=(const CMap& cmap) {
7.74 + Parent::operator=(cmap);
7.75 + return *this;
7.76 + }
7.77 +
7.78 + class MapIt : public Item {
7.79 + public:
7.80 +
7.81 + typedef Item Parent;
7.82 + typedef typename Map::Value Value;
7.83 +
7.84 + MapIt() {}
7.85 +
7.86 + MapIt(Invalid i) : Parent(i) { }
7.87 +
7.88 + explicit MapIt(Map& _map) : map(_map) {
7.89 + map.notifier()->first(*this);
7.90 + }
7.91 +
7.92 + MapIt(const Map& _map, const Item& item)
7.93 + : Parent(item), map(_map) {}
7.94 +
7.95 + MapIt& operator++() {
7.96 + map.notifier()->next(*this);
7.97 + return *this;
7.98 + }
7.99 +
7.100 + typename MapTraits<Map>::ConstReturnValue operator*() const {
7.101 + return map[*this];
7.102 + }
7.103 +
7.104 + typename MapTraits<Map>::ReturnValue operator*() {
7.105 + return map[*this];
7.106 + }
7.107 +
7.108 + void set(const Value& value) {
7.109 + map.set(*this, value);
7.110 + }
7.111 +
7.112 + protected:
7.113 + Map& map;
7.114 +
7.115 + };
7.116 +
7.117 + class ConstMapIt : public Item {
7.118 + public:
7.119 +
7.120 + typedef Item Parent;
7.121 +
7.122 + typedef typename Map::Value Value;
7.123 +
7.124 + ConstMapIt() {}
7.125 +
7.126 + ConstMapIt(Invalid i) : Parent(i) { }
7.127 +
7.128 + explicit ConstMapIt(Map& _map) : map(_map) {
7.129 + map.notifier()->first(*this);
7.130 + }
7.131 +
7.132 + ConstMapIt(const Map& _map, const Item& item)
7.133 + : Parent(item), map(_map) {}
7.134 +
7.135 + ConstMapIt& operator++() {
7.136 + map.notifier()->next(*this);
7.137 + return *this;
7.138 + }
7.139 +
7.140 + typename MapTraits<Map>::ConstReturnValue operator*() const {
7.141 + return map[*this];
7.142 + }
7.143 +
7.144 + protected:
7.145 + const Map& map;
7.146 + };
7.147 +
7.148 + class ItemIt : public Item {
7.149 + public:
7.150 +
7.151 + typedef Item Parent;
7.152 +
7.153 + ItemIt() {}
7.154 +
7.155 + ItemIt(Invalid i) : Parent(i) { }
7.156 +
7.157 + explicit ItemIt(Map& _map) : map(_map) {
7.158 + map.notifier()->first(*this);
7.159 + }
7.160 +
7.161 + ItemIt(const Map& _map, const Item& item)
7.162 + : Parent(item), map(_map) {}
7.163 +
7.164 + ItemIt& operator++() {
7.165 + map.notifier()->next(*this);
7.166 + return *this;
7.167 + }
7.168 +
7.169 + protected:
7.170 + const Map& map;
7.171 +
7.172 + };
7.173 + };
7.174 +
7.175 + /// \ingroup graphbits
7.176 + ///
7.177 + /// \brief Extender for maps which use a subset of the items.
7.178 + template <typename _Graph, typename _Map>
7.179 + class SubMapExtender : public _Map {
7.180 + public:
7.181 +
7.182 + typedef _Map Parent;
7.183 + typedef SubMapExtender Map;
7.184 +
7.185 + typedef _Graph Graph;
7.186 +
7.187 + typedef typename Parent::Key Item;
7.188 +
7.189 + typedef typename Parent::Key Key;
7.190 + typedef typename Parent::Value Value;
7.191 +
7.192 + class MapIt;
7.193 + class ConstMapIt;
7.194 +
7.195 + friend class MapIt;
7.196 + friend class ConstMapIt;
7.197 +
7.198 + public:
7.199 +
7.200 + SubMapExtender(const Graph& _graph)
7.201 + : Parent(_graph), graph(_graph) {}
7.202 +
7.203 + SubMapExtender(const Graph& _graph, const Value& _value)
7.204 + : Parent(_graph, _value), graph(_graph) {}
7.205 +
7.206 + SubMapExtender& operator=(const SubMapExtender& cmap) {
7.207 + return operator=<MapExtender>(cmap);
7.208 + }
7.209 +
7.210 + template <typename CMap>
7.211 + SubMapExtender& operator=(const CMap& cmap) {
7.212 + checkConcept<concepts::ReadMap<Key, Value>, CMap>();
7.213 + Item it;
7.214 + for (graph.first(it); it != INVALID; graph.next(it)) {
7.215 + Parent::set(it, cmap[it]);
7.216 + }
7.217 + return *this;
7.218 + }
7.219 +
7.220 + class MapIt : public Item {
7.221 + public:
7.222 +
7.223 + typedef Item Parent;
7.224 + typedef typename Map::Value Value;
7.225 +
7.226 + MapIt() {}
7.227 +
7.228 + MapIt(Invalid i) : Parent(i) { }
7.229 +
7.230 + explicit MapIt(Map& _map) : map(_map) {
7.231 + map.graph.first(*this);
7.232 + }
7.233 +
7.234 + MapIt(const Map& _map, const Item& item)
7.235 + : Parent(item), map(_map) {}
7.236 +
7.237 + MapIt& operator++() {
7.238 + map.graph.next(*this);
7.239 + return *this;
7.240 + }
7.241 +
7.242 + typename MapTraits<Map>::ConstReturnValue operator*() const {
7.243 + return map[*this];
7.244 + }
7.245 +
7.246 + typename MapTraits<Map>::ReturnValue operator*() {
7.247 + return map[*this];
7.248 + }
7.249 +
7.250 + void set(const Value& value) {
7.251 + map.set(*this, value);
7.252 + }
7.253 +
7.254 + protected:
7.255 + Map& map;
7.256 +
7.257 + };
7.258 +
7.259 + class ConstMapIt : public Item {
7.260 + public:
7.261 +
7.262 + typedef Item Parent;
7.263 +
7.264 + typedef typename Map::Value Value;
7.265 +
7.266 + ConstMapIt() {}
7.267 +
7.268 + ConstMapIt(Invalid i) : Parent(i) { }
7.269 +
7.270 + explicit ConstMapIt(Map& _map) : map(_map) {
7.271 + map.graph.first(*this);
7.272 + }
7.273 +
7.274 + ConstMapIt(const Map& _map, const Item& item)
7.275 + : Parent(item), map(_map) {}
7.276 +
7.277 + ConstMapIt& operator++() {
7.278 + map.graph.next(*this);
7.279 + return *this;
7.280 + }
7.281 +
7.282 + typename MapTraits<Map>::ConstReturnValue operator*() const {
7.283 + return map[*this];
7.284 + }
7.285 +
7.286 + protected:
7.287 + const Map& map;
7.288 + };
7.289 +
7.290 + class ItemIt : public Item {
7.291 + public:
7.292 +
7.293 + typedef Item Parent;
7.294 +
7.295 + ItemIt() {}
7.296 +
7.297 + ItemIt(Invalid i) : Parent(i) { }
7.298 +
7.299 + explicit ItemIt(Map& _map) : map(_map) {
7.300 + map.graph.first(*this);
7.301 + }
7.302 +
7.303 + ItemIt(const Map& _map, const Item& item)
7.304 + : Parent(item), map(_map) {}
7.305 +
7.306 + ItemIt& operator++() {
7.307 + map.graph.next(*this);
7.308 + return *this;
7.309 + }
7.310 +
7.311 + protected:
7.312 + const Map& map;
7.313 +
7.314 + };
7.315 +
7.316 + private:
7.317 +
7.318 + const Graph& graph;
7.319 +
7.320 + };
7.321 +
7.322 +}
7.323 +
7.324 +#endif
8.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
8.2 +++ b/lemon/bits/traits.h Sun Jan 20 20:43:48 2008 +0100
8.3 @@ -0,0 +1,272 @@
8.4 +
8.5 +/* -*- C++ -*-
8.6 + *
8.7 + * This file is a part of LEMON, a generic C++ optimization library
8.8 + *
8.9 + * Copyright (C) 2003-2007
8.10 + * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
8.11 + * (Egervary Research Group on Combinatorial Optimization, EGRES).
8.12 + *
8.13 + * Permission to use, modify and distribute this software is granted
8.14 + * provided that this copyright notice appears in all copies. For
8.15 + * precise terms see the accompanying LICENSE file.
8.16 + *
8.17 + * This software is provided "AS IS" with no warranty of any kind,
8.18 + * express or implied, and with no claim as to its suitability for any
8.19 + * purpose.
8.20 + *
8.21 + */
8.22 +
8.23 +#ifndef LEMON_BITS_TRAITS_H
8.24 +#define LEMON_BITS_TRAITS_H
8.25 +
8.26 +#include <lemon/bits/utility.h>
8.27 +
8.28 +///\file
8.29 +///\brief Traits for graphs and maps
8.30 +///
8.31 +
8.32 +namespace lemon {
8.33 + template <typename _Graph, typename _Item>
8.34 + class ItemSetTraits {};
8.35 +
8.36 +
8.37 + template <typename Graph, typename Enable = void>
8.38 + struct NodeNotifierIndicator {
8.39 + typedef InvalidType Type;
8.40 + };
8.41 + template <typename Graph>
8.42 + struct NodeNotifierIndicator<
8.43 + Graph,
8.44 + typename enable_if<typename Graph::NodeNotifier::Notifier, void>::type
8.45 + > {
8.46 + typedef typename Graph::NodeNotifier Type;
8.47 + };
8.48 +
8.49 + template <typename _Graph>
8.50 + class ItemSetTraits<_Graph, typename _Graph::Node> {
8.51 + public:
8.52 +
8.53 + typedef _Graph Graph;
8.54 +
8.55 + typedef typename Graph::Node Item;
8.56 + typedef typename Graph::NodeIt ItemIt;
8.57 +
8.58 + typedef typename NodeNotifierIndicator<Graph>::Type ItemNotifier;
8.59 +
8.60 + template <typename _Value>
8.61 + class Map : public Graph::template NodeMap<_Value> {
8.62 + public:
8.63 + typedef typename Graph::template NodeMap<_Value> Parent;
8.64 + typedef typename Graph::template NodeMap<_Value> Type;
8.65 + typedef typename Parent::Value Value;
8.66 +
8.67 + Map(const Graph& _digraph) : Parent(_digraph) {}
8.68 + Map(const Graph& _digraph, const Value& _value)
8.69 + : Parent(_digraph, _value) {}
8.70 +
8.71 + };
8.72 +
8.73 + };
8.74 +
8.75 + template <typename Graph, typename Enable = void>
8.76 + struct ArcNotifierIndicator {
8.77 + typedef InvalidType Type;
8.78 + };
8.79 + template <typename Graph>
8.80 + struct ArcNotifierIndicator<
8.81 + Graph,
8.82 + typename enable_if<typename Graph::ArcNotifier::Notifier, void>::type
8.83 + > {
8.84 + typedef typename Graph::ArcNotifier Type;
8.85 + };
8.86 +
8.87 + template <typename _Graph>
8.88 + class ItemSetTraits<_Graph, typename _Graph::Arc> {
8.89 + public:
8.90 +
8.91 + typedef _Graph Graph;
8.92 +
8.93 + typedef typename Graph::Arc Item;
8.94 + typedef typename Graph::ArcIt ItemIt;
8.95 +
8.96 + typedef typename ArcNotifierIndicator<Graph>::Type ItemNotifier;
8.97 +
8.98 + template <typename _Value>
8.99 + class Map : public Graph::template ArcMap<_Value> {
8.100 + public:
8.101 + typedef typename Graph::template ArcMap<_Value> Parent;
8.102 + typedef typename Graph::template ArcMap<_Value> Type;
8.103 + typedef typename Parent::Value Value;
8.104 +
8.105 + Map(const Graph& _digraph) : Parent(_digraph) {}
8.106 + Map(const Graph& _digraph, const Value& _value)
8.107 + : Parent(_digraph, _value) {}
8.108 + };
8.109 +
8.110 + };
8.111 +
8.112 + template <typename Graph, typename Enable = void>
8.113 + struct EdgeNotifierIndicator {
8.114 + typedef InvalidType Type;
8.115 + };
8.116 + template <typename Graph>
8.117 + struct EdgeNotifierIndicator<
8.118 + Graph,
8.119 + typename enable_if<typename Graph::EdgeNotifier::Notifier, void>::type
8.120 + > {
8.121 + typedef typename Graph::EdgeNotifier Type;
8.122 + };
8.123 +
8.124 + template <typename _Graph>
8.125 + class ItemSetTraits<_Graph, typename _Graph::Edge> {
8.126 + public:
8.127 +
8.128 + typedef _Graph Graph;
8.129 +
8.130 + typedef typename Graph::Edge Item;
8.131 + typedef typename Graph::EdgeIt ItemIt;
8.132 +
8.133 + typedef typename EdgeNotifierIndicator<Graph>::Type ItemNotifier;
8.134 +
8.135 + template <typename _Value>
8.136 + class Map : public Graph::template EdgeMap<_Value> {
8.137 + public:
8.138 + typedef typename Graph::template EdgeMap<_Value> Parent;
8.139 + typedef typename Graph::template EdgeMap<_Value> Type;
8.140 + typedef typename Parent::Value Value;
8.141 +
8.142 + Map(const Graph& _digraph) : Parent(_digraph) {}
8.143 + Map(const Graph& _digraph, const Value& _value)
8.144 + : Parent(_digraph, _value) {}
8.145 + };
8.146 +
8.147 + };
8.148 +
8.149 + template <typename Map, typename Enable = void>
8.150 + struct MapTraits {
8.151 + typedef False ReferenceMapTag;
8.152 +
8.153 + typedef typename Map::Key Key;
8.154 + typedef typename Map::Value Value;
8.155 +
8.156 + typedef const Value ConstReturnValue;
8.157 + typedef const Value ReturnValue;
8.158 + };
8.159 +
8.160 + template <typename Map>
8.161 + struct MapTraits<
8.162 + Map, typename enable_if<typename Map::ReferenceMapTag, void>::type >
8.163 + {
8.164 + typedef True ReferenceMapTag;
8.165 +
8.166 + typedef typename Map::Key Key;
8.167 + typedef typename Map::Value Value;
8.168 +
8.169 + typedef typename Map::ConstReference ConstReturnValue;
8.170 + typedef typename Map::Reference ReturnValue;
8.171 +
8.172 + typedef typename Map::ConstReference ConstReference;
8.173 + typedef typename Map::Reference Reference;
8.174 + };
8.175 +
8.176 + template <typename MatrixMap, typename Enable = void>
8.177 + struct MatrixMapTraits {
8.178 + typedef False ReferenceMapTag;
8.179 +
8.180 + typedef typename MatrixMap::FirstKey FirstKey;
8.181 + typedef typename MatrixMap::SecondKey SecondKey;
8.182 + typedef typename MatrixMap::Value Value;
8.183 +
8.184 + typedef const Value ConstReturnValue;
8.185 + typedef const Value ReturnValue;
8.186 + };
8.187 +
8.188 + template <typename MatrixMap>
8.189 + struct MatrixMapTraits<
8.190 + MatrixMap, typename enable_if<typename MatrixMap::ReferenceMapTag,
8.191 + void>::type >
8.192 + {
8.193 + typedef True ReferenceMapTag;
8.194 +
8.195 + typedef typename MatrixMap::FirstKey FirstKey;
8.196 + typedef typename MatrixMap::SecondKey SecondKey;
8.197 + typedef typename MatrixMap::Value Value;
8.198 +
8.199 + typedef typename MatrixMap::ConstReference ConstReturnValue;
8.200 + typedef typename MatrixMap::Reference ReturnValue;
8.201 +
8.202 + typedef typename MatrixMap::ConstReference ConstReference;
8.203 + typedef typename MatrixMap::Reference Reference;
8.204 + };
8.205 +
8.206 + // Indicators for the tags
8.207 +
8.208 + template <typename Graph, typename Enable = void>
8.209 + struct NodeNumTagIndicator {
8.210 + static const bool value = false;
8.211 + };
8.212 +
8.213 + template <typename Graph>
8.214 + struct NodeNumTagIndicator<
8.215 + Graph,
8.216 + typename enable_if<typename Graph::NodeNumTag, void>::type
8.217 + > {
8.218 + static const bool value = true;
8.219 + };
8.220 +
8.221 + template <typename Graph, typename Enable = void>
8.222 + struct ArcNumTagIndicator {
8.223 + static const bool value = false;
8.224 + };
8.225 +
8.226 + template <typename Graph>
8.227 + struct ArcNumTagIndicator<
8.228 + Graph,
8.229 + typename enable_if<typename Graph::ArcNumTag, void>::type
8.230 + > {
8.231 + static const bool value = true;
8.232 + };
8.233 +
8.234 + template <typename Graph, typename Enable = void>
8.235 + struct FindArcTagIndicator {
8.236 + static const bool value = false;
8.237 + };
8.238 +
8.239 + template <typename Graph>
8.240 + struct FindArcTagIndicator<
8.241 + Graph,
8.242 + typename enable_if<typename Graph::FindArcTag, void>::type
8.243 + > {
8.244 + static const bool value = true;
8.245 + };
8.246 +
8.247 + template <typename Graph, typename Enable = void>
8.248 + struct UndirectedTagIndicator {
8.249 + static const bool value = false;
8.250 + };
8.251 +
8.252 + template <typename Graph>
8.253 + struct UndirectedTagIndicator<
8.254 + Graph,
8.255 + typename enable_if<typename Graph::UndirectedTag, void>::type
8.256 + > {
8.257 + static const bool value = true;
8.258 + };
8.259 +
8.260 + template <typename Graph, typename Enable = void>
8.261 + struct BuildTagIndicator {
8.262 + static const bool value = false;
8.263 + };
8.264 +
8.265 + template <typename Graph>
8.266 + struct BuildTagIndicator<
8.267 + Graph,
8.268 + typename enable_if<typename Graph::BuildTag, void>::type
8.269 + > {
8.270 + static const bool value = true;
8.271 + };
8.272 +
8.273 +}
8.274 +
8.275 +#endif
9.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
9.2 +++ b/lemon/bits/vector_map.h Sun Jan 20 20:43:48 2008 +0100
9.3 @@ -0,0 +1,243 @@
9.4 +/* -*- C++ -*-
9.5 + *
9.6 + * This file is a part of LEMON, a generic C++ optimization library
9.7 + *
9.8 + * Copyright (C) 2003-2007
9.9 + * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
9.10 + * (Egervary Research Group on Combinatorial Optimization, EGRES).
9.11 + *
9.12 + * Permission to use, modify and distribute this software is granted
9.13 + * provided that this copyright notice appears in all copies. For
9.14 + * precise terms see the accompanying LICENSE file.
9.15 + *
9.16 + * This software is provided "AS IS" with no warranty of any kind,
9.17 + * express or implied, and with no claim as to its suitability for any
9.18 + * purpose.
9.19 + *
9.20 + */
9.21 +
9.22 +#ifndef LEMON_BITS_VECTOR_MAP_H
9.23 +#define LEMON_BITS_VECTOR_MAP_H
9.24 +
9.25 +#include <vector>
9.26 +#include <algorithm>
9.27 +
9.28 +#include <lemon/bits/traits.h>
9.29 +#include <lemon/bits/utility.h>
9.30 +
9.31 +#include <lemon/bits/alteration_notifier.h>
9.32 +
9.33 +#include <lemon/concept_check.h>
9.34 +#include <lemon/concepts/maps.h>
9.35 +
9.36 +///\ingroup graphbits
9.37 +///
9.38 +///\file
9.39 +///\brief Vector based graph maps.
9.40 +namespace lemon {
9.41 +
9.42 + /// \ingroup graphbits
9.43 + ///
9.44 + /// \brief Graph map based on the std::vector storage.
9.45 + ///
9.46 + /// The VectorMap template class is graph map structure what
9.47 + /// automatically updates the map when a key is added to or erased from
9.48 + /// the map. This map type uses the std::vector to store the values.
9.49 + ///
9.50 + /// \param Notifier The AlterationNotifier that will notify this map.
9.51 + /// \param Item The item type of the graph items.
9.52 + /// \param Value The value type of the map.
9.53 + ///
9.54 + /// \author Balazs Dezso
9.55 + template <typename _Graph, typename _Item, typename _Value>
9.56 + class VectorMap
9.57 + : public ItemSetTraits<_Graph, _Item>::ItemNotifier::ObserverBase {
9.58 + private:
9.59 +
9.60 + /// The container type of the map.
9.61 + typedef std::vector<_Value> Container;
9.62 +
9.63 + public:
9.64 +
9.65 + /// The graph type of the map.
9.66 + typedef _Graph Graph;
9.67 + /// The item type of the map.
9.68 + typedef _Item Item;
9.69 + /// The reference map tag.
9.70 + typedef True ReferenceMapTag;
9.71 +
9.72 + /// The key type of the map.
9.73 + typedef _Item Key;
9.74 + /// The value type of the map.
9.75 + typedef _Value Value;
9.76 +
9.77 + /// The notifier type.
9.78 + typedef typename ItemSetTraits<_Graph, _Item>::ItemNotifier Notifier;
9.79 +
9.80 + /// The map type.
9.81 + typedef VectorMap Map;
9.82 + /// The base class of the map.
9.83 + typedef typename Notifier::ObserverBase Parent;
9.84 +
9.85 + /// The reference type of the map;
9.86 + typedef typename Container::reference Reference;
9.87 + /// The const reference type of the map;
9.88 + typedef typename Container::const_reference ConstReference;
9.89 +
9.90 +
9.91 + /// \brief Constructor to attach the new map into the notifier.
9.92 + ///
9.93 + /// It constructs a map and attachs it into the notifier.
9.94 + /// It adds all the items of the graph to the map.
9.95 + VectorMap(const Graph& graph) {
9.96 + Parent::attach(graph.notifier(Item()));
9.97 + container.resize(Parent::notifier()->maxId() + 1);
9.98 + }
9.99 +
9.100 + /// \brief Constructor uses given value to initialize the map.
9.101 + ///
9.102 + /// It constructs a map uses a given value to initialize the map.
9.103 + /// It adds all the items of the graph to the map.
9.104 + VectorMap(const Graph& graph, const Value& value) {
9.105 + Parent::attach(graph.notifier(Item()));
9.106 + container.resize(Parent::notifier()->maxId() + 1, value);
9.107 + }
9.108 +
9.109 + /// \brief Copy constructor
9.110 + ///
9.111 + /// Copy constructor.
9.112 + VectorMap(const VectorMap& _copy) : Parent() {
9.113 + if (_copy.attached()) {
9.114 + Parent::attach(*_copy.notifier());
9.115 + container = _copy.container;
9.116 + }
9.117 + }
9.118 +
9.119 + /// \brief Assign operator.
9.120 + ///
9.121 + /// This operator assigns for each item in the map the
9.122 + /// value mapped to the same item in the copied map.
9.123 + /// The parameter map should be indiced with the same
9.124 + /// itemset because this assign operator does not change
9.125 + /// the container of the map.
9.126 + VectorMap& operator=(const VectorMap& cmap) {
9.127 + return operator=<VectorMap>(cmap);
9.128 + }
9.129 +
9.130 +
9.131 + /// \brief Template assign operator.
9.132 + ///
9.133 + /// The given parameter should be conform to the ReadMap
9.134 + /// concecpt and could be indiced by the current item set of
9.135 + /// the NodeMap. In this case the value for each item
9.136 + /// is assigned by the value of the given ReadMap.
9.137 + template <typename CMap>
9.138 + VectorMap& operator=(const CMap& cmap) {
9.139 + checkConcept<concepts::ReadMap<Key, _Value>, CMap>();
9.140 + const typename Parent::Notifier* nf = Parent::notifier();
9.141 + Item it;
9.142 + for (nf->first(it); it != INVALID; nf->next(it)) {
9.143 + set(it, cmap[it]);
9.144 + }
9.145 + return *this;
9.146 + }
9.147 +
9.148 + public:
9.149 +
9.150 + /// \brief The subcript operator.
9.151 + ///
9.152 + /// The subscript operator. The map can be subscripted by the
9.153 + /// actual items of the graph.
9.154 + Reference operator[](const Key& key) {
9.155 + return container[Parent::notifier()->id(key)];
9.156 + }
9.157 +
9.158 + /// \brief The const subcript operator.
9.159 + ///
9.160 + /// The const subscript operator. The map can be subscripted by the
9.161 + /// actual items of the graph.
9.162 + ConstReference operator[](const Key& key) const {
9.163 + return container[Parent::notifier()->id(key)];
9.164 + }
9.165 +
9.166 +
9.167 + /// \brief The setter function of the map.
9.168 + ///
9.169 + /// It the same as operator[](key) = value expression.
9.170 + void set(const Key& key, const Value& value) {
9.171 + (*this)[key] = value;
9.172 + }
9.173 +
9.174 + protected:
9.175 +
9.176 + /// \brief Adds a new key to the map.
9.177 + ///
9.178 + /// It adds a new key to the map. It called by the observer notifier
9.179 + /// and it overrides the add() member function of the observer base.
9.180 + virtual void add(const Key& key) {
9.181 + int id = Parent::notifier()->id(key);
9.182 + if (id >= int(container.size())) {
9.183 + container.resize(id + 1);
9.184 + }
9.185 + }
9.186 +
9.187 + /// \brief Adds more new keys to the map.
9.188 + ///
9.189 + /// It adds more new keys to the map. It called by the observer notifier
9.190 + /// and it overrides the add() member function of the observer base.
9.191 + virtual void add(const std::vector<Key>& keys) {
9.192 + int max = container.size() - 1;
9.193 + for (int i = 0; i < int(keys.size()); ++i) {
9.194 + int id = Parent::notifier()->id(keys[i]);
9.195 + if (id >= max) {
9.196 + max = id;
9.197 + }
9.198 + }
9.199 + container.resize(max + 1);
9.200 + }
9.201 +
9.202 + /// \brief Erase a key from the map.
9.203 + ///
9.204 + /// Erase a key from the map. It called by the observer notifier
9.205 + /// and it overrides the erase() member function of the observer base.
9.206 + virtual void erase(const Key& key) {
9.207 + container[Parent::notifier()->id(key)] = Value();
9.208 + }
9.209 +
9.210 + /// \brief Erase more keys from the map.
9.211 + ///
9.212 + /// Erase more keys from the map. It called by the observer notifier
9.213 + /// and it overrides the erase() member function of the observer base.
9.214 + virtual void erase(const std::vector<Key>& keys) {
9.215 + for (int i = 0; i < int(keys.size()); ++i) {
9.216 + container[Parent::notifier()->id(keys[i])] = Value();
9.217 + }
9.218 + }
9.219 +
9.220 + /// \brief Buildes the map.
9.221 + ///
9.222 + /// It buildes the map. It called by the observer notifier
9.223 + /// and it overrides the build() member function of the observer base.
9.224 + virtual void build() {
9.225 + int size = Parent::notifier()->maxId() + 1;
9.226 + container.reserve(size);
9.227 + container.resize(size);
9.228 + }
9.229 +
9.230 + /// \brief Clear the map.
9.231 + ///
9.232 + /// It erase all items from the map. It called by the observer notifier
9.233 + /// and it overrides the clear() member function of the observer base.
9.234 + virtual void clear() {
9.235 + container.clear();
9.236 + }
9.237 +
9.238 + private:
9.239 +
9.240 + Container container;
9.241 +
9.242 + };
9.243 +
9.244 +}
9.245 +
9.246 +#endif
10.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
10.2 +++ b/lemon/concepts/digraph.h Sun Jan 20 20:43:48 2008 +0100
10.3 @@ -0,0 +1,453 @@
10.4 +/* -*- C++ -*-
10.5 + *
10.6 + * This file is a part of LEMON, a generic C++ optimization library
10.7 + *
10.8 + * Copyright (C) 2003-2007
10.9 + * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
10.10 + * (Egervary Research Group on Combinatorial Optimization, EGRES).
10.11 + *
10.12 + * Permission to use, modify and distribute this software is granted
10.13 + * provided that this copyright notice appears in all copies. For
10.14 + * precise terms see the accompanying LICENSE file.
10.15 + *
10.16 + * This software is provided "AS IS" with no warranty of any kind,
10.17 + * express or implied, and with no claim as to its suitability for any
10.18 + * purpose.
10.19 + *
10.20 + */
10.21 +
10.22 +#ifndef LEMON_CONCEPT_DIGRAPH_H
10.23 +#define LEMON_CONCEPT_DIGRAPH_H
10.24 +
10.25 +///\ingroup graph_concepts
10.26 +///\file
10.27 +///\brief The concept of directed graphs.
10.28 +
10.29 +#include <lemon/bits/invalid.h>
10.30 +#include <lemon/bits/utility.h>
10.31 +#include <lemon/concepts/maps.h>
10.32 +#include <lemon/concept_check.h>
10.33 +#include <lemon/concepts/graph_components.h>
10.34 +
10.35 +namespace lemon {
10.36 + namespace concepts {
10.37 +
10.38 + /// \ingroup graph_concepts
10.39 + ///
10.40 + /// \brief Class describing the concept of directed graphs.
10.41 + ///
10.42 + /// This class describes the \ref concept "concept" of the
10.43 + /// immutable directed digraphs.
10.44 + ///
10.45 + /// Note that actual digraph implementation like @ref ListDigraph or
10.46 + /// @ref SmartDigraph may have several additional functionality.
10.47 + ///
10.48 + /// \sa concept
10.49 + class Digraph {
10.50 + private:
10.51 + ///Digraphs are \e not copy constructible. Use DigraphCopy() instead.
10.52 +
10.53 + ///Digraphs are \e not copy constructible. Use DigraphCopy() instead.
10.54 + ///
10.55 + Digraph(const Digraph &) {};
10.56 + ///\brief Assignment of \ref Digraph "Digraph"s to another ones are
10.57 + ///\e not allowed. Use DigraphCopy() instead.
10.58 +
10.59 + ///Assignment of \ref Digraph "Digraph"s to another ones are
10.60 + ///\e not allowed. Use DigraphCopy() instead.
10.61 +
10.62 + void operator=(const Digraph &) {}
10.63 + public:
10.64 + ///\e
10.65 +
10.66 + /// Defalult constructor.
10.67 +
10.68 + /// Defalult constructor.
10.69 + ///
10.70 + Digraph() { }
10.71 + /// Class for identifying a node of the digraph
10.72 +
10.73 + /// This class identifies a node of the digraph. It also serves
10.74 + /// as a base class of the node iterators,
10.75 + /// thus they will convert to this type.
10.76 + class Node {
10.77 + public:
10.78 + /// Default constructor
10.79 +
10.80 + /// @warning The default constructor sets the iterator
10.81 + /// to an undefined value.
10.82 + Node() { }
10.83 + /// Copy constructor.
10.84 +
10.85 + /// Copy constructor.
10.86 + ///
10.87 + Node(const Node&) { }
10.88 +
10.89 + /// Invalid constructor \& conversion.
10.90 +
10.91 + /// This constructor initializes the iterator to be invalid.
10.92 + /// \sa Invalid for more details.
10.93 + Node(Invalid) { }
10.94 + /// Equality operator
10.95 +
10.96 + /// Two iterators are equal if and only if they point to the
10.97 + /// same object or both are invalid.
10.98 + bool operator==(Node) const { return true; }
10.99 +
10.100 + /// Inequality operator
10.101 +
10.102 + /// \sa operator==(Node n)
10.103 + ///
10.104 + bool operator!=(Node) const { return true; }
10.105 +
10.106 + /// Artificial ordering operator.
10.107 +
10.108 + /// To allow the use of digraph descriptors as key type in std::map or
10.109 + /// similar associative container we require this.
10.110 + ///
10.111 + /// \note This operator only have to define some strict ordering of
10.112 + /// the items; this order has nothing to do with the iteration
10.113 + /// ordering of the items.
10.114 + bool operator<(Node) const { return false; }
10.115 +
10.116 + };
10.117 +
10.118 + /// This iterator goes through each node.
10.119 +
10.120 + /// This iterator goes through each node.
10.121 + /// Its usage is quite simple, for example you can count the number
10.122 + /// of nodes in digraph \c g of type \c Digraph like this:
10.123 + ///\code
10.124 + /// int count=0;
10.125 + /// for (Digraph::NodeIt n(g); n!=INVALID; ++n) ++count;
10.126 + ///\endcode
10.127 + class NodeIt : public Node {
10.128 + public:
10.129 + /// Default constructor
10.130 +
10.131 + /// @warning The default constructor sets the iterator
10.132 + /// to an undefined value.
10.133 + NodeIt() { }
10.134 + /// Copy constructor.
10.135 +
10.136 + /// Copy constructor.
10.137 + ///
10.138 + NodeIt(const NodeIt& n) : Node(n) { }
10.139 + /// Invalid constructor \& conversion.
10.140 +
10.141 + /// Initialize the iterator to be invalid.
10.142 + /// \sa Invalid for more details.
10.143 + NodeIt(Invalid) { }
10.144 + /// Sets the iterator to the first node.
10.145 +
10.146 + /// Sets the iterator to the first node of \c g.
10.147 + ///
10.148 + NodeIt(const Digraph&) { }
10.149 + /// Node -> NodeIt conversion.
10.150 +
10.151 + /// Sets the iterator to the node of \c the digraph pointed by
10.152 + /// the trivial iterator.
10.153 + /// This feature necessitates that each time we
10.154 + /// iterate the arc-set, the iteration order is the same.
10.155 + NodeIt(const Digraph&, const Node&) { }
10.156 + /// Next node.
10.157 +
10.158 + /// Assign the iterator to the next node.
10.159 + ///
10.160 + NodeIt& operator++() { return *this; }
10.161 + };
10.162 +
10.163 +
10.164 + /// Class for identifying an arc of the digraph
10.165 +
10.166 + /// This class identifies an arc of the digraph. It also serves
10.167 + /// as a base class of the arc iterators,
10.168 + /// thus they will convert to this type.
10.169 + class Arc {
10.170 + public:
10.171 + /// Default constructor
10.172 +
10.173 + /// @warning The default constructor sets the iterator
10.174 + /// to an undefined value.
10.175 + Arc() { }
10.176 + /// Copy constructor.
10.177 +
10.178 + /// Copy constructor.
10.179 + ///
10.180 + Arc(const Arc&) { }
10.181 + /// Initialize the iterator to be invalid.
10.182 +
10.183 + /// Initialize the iterator to be invalid.
10.184 + ///
10.185 + Arc(Invalid) { }
10.186 + /// Equality operator
10.187 +
10.188 + /// Two iterators are equal if and only if they point to the
10.189 + /// same object or both are invalid.
10.190 + bool operator==(Arc) const { return true; }
10.191 + /// Inequality operator
10.192 +
10.193 + /// \sa operator==(Arc n)
10.194 + ///
10.195 + bool operator!=(Arc) const { return true; }
10.196 +
10.197 + /// Artificial ordering operator.
10.198 +
10.199 + /// To allow the use of digraph descriptors as key type in std::map or
10.200 + /// similar associative container we require this.
10.201 + ///
10.202 + /// \note This operator only have to define some strict ordering of
10.203 + /// the items; this order has nothing to do with the iteration
10.204 + /// ordering of the items.
10.205 + bool operator<(Arc) const { return false; }
10.206 + };
10.207 +
10.208 + /// This iterator goes trough the outgoing arcs of a node.
10.209 +
10.210 + /// This iterator goes trough the \e outgoing arcs of a certain node
10.211 + /// of a digraph.
10.212 + /// Its usage is quite simple, for example you can count the number
10.213 + /// of outgoing arcs of a node \c n
10.214 + /// in digraph \c g of type \c Digraph as follows.
10.215 + ///\code
10.216 + /// int count=0;
10.217 + /// for (Digraph::OutArcIt e(g, n); e!=INVALID; ++e) ++count;
10.218 + ///\endcode
10.219 +
10.220 + class OutArcIt : public Arc {
10.221 + public:
10.222 + /// Default constructor
10.223 +
10.224 + /// @warning The default constructor sets the iterator
10.225 + /// to an undefined value.
10.226 + OutArcIt() { }
10.227 + /// Copy constructor.
10.228 +
10.229 + /// Copy constructor.
10.230 + ///
10.231 + OutArcIt(const OutArcIt& e) : Arc(e) { }
10.232 + /// Initialize the iterator to be invalid.
10.233 +
10.234 + /// Initialize the iterator to be invalid.
10.235 + ///
10.236 + OutArcIt(Invalid) { }
10.237 + /// This constructor sets the iterator to the first outgoing arc.
10.238 +
10.239 + /// This constructor sets the iterator to the first outgoing arc of
10.240 + /// the node.
10.241 + OutArcIt(const Digraph&, const Node&) { }
10.242 + /// Arc -> OutArcIt conversion
10.243 +
10.244 + /// Sets the iterator to the value of the trivial iterator.
10.245 + /// This feature necessitates that each time we
10.246 + /// iterate the arc-set, the iteration order is the same.
10.247 + OutArcIt(const Digraph&, const Arc&) { }
10.248 + ///Next outgoing arc
10.249 +
10.250 + /// Assign the iterator to the next
10.251 + /// outgoing arc of the corresponding node.
10.252 + OutArcIt& operator++() { return *this; }
10.253 + };
10.254 +
10.255 + /// This iterator goes trough the incoming arcs of a node.
10.256 +
10.257 + /// This iterator goes trough the \e incoming arcs of a certain node
10.258 + /// of a digraph.
10.259 + /// Its usage is quite simple, for example you can count the number
10.260 + /// of outgoing arcs of a node \c n
10.261 + /// in digraph \c g of type \c Digraph as follows.
10.262 + ///\code
10.263 + /// int count=0;
10.264 + /// for(Digraph::InArcIt e(g, n); e!=INVALID; ++e) ++count;
10.265 + ///\endcode
10.266 +
10.267 + class InArcIt : public Arc {
10.268 + public:
10.269 + /// Default constructor
10.270 +
10.271 + /// @warning The default constructor sets the iterator
10.272 + /// to an undefined value.
10.273 + InArcIt() { }
10.274 + /// Copy constructor.
10.275 +
10.276 + /// Copy constructor.
10.277 + ///
10.278 + InArcIt(const InArcIt& e) : Arc(e) { }
10.279 + /// Initialize the iterator to be invalid.
10.280 +
10.281 + /// Initialize the iterator to be invalid.
10.282 + ///
10.283 + InArcIt(Invalid) { }
10.284 + /// This constructor sets the iterator to first incoming arc.
10.285 +
10.286 + /// This constructor set the iterator to the first incoming arc of
10.287 + /// the node.
10.288 + InArcIt(const Digraph&, const Node&) { }
10.289 + /// Arc -> InArcIt conversion
10.290 +
10.291 + /// Sets the iterator to the value of the trivial iterator \c e.
10.292 + /// This feature necessitates that each time we
10.293 + /// iterate the arc-set, the iteration order is the same.
10.294 + InArcIt(const Digraph&, const Arc&) { }
10.295 + /// Next incoming arc
10.296 +
10.297 + /// Assign the iterator to the next inarc of the corresponding node.
10.298 + ///
10.299 + InArcIt& operator++() { return *this; }
10.300 + };
10.301 + /// This iterator goes through each arc.
10.302 +
10.303 + /// This iterator goes through each arc of a digraph.
10.304 + /// Its usage is quite simple, for example you can count the number
10.305 + /// of arcs in a digraph \c g of type \c Digraph as follows:
10.306 + ///\code
10.307 + /// int count=0;
10.308 + /// for(Digraph::ArcIt e(g); e!=INVALID; ++e) ++count;
10.309 + ///\endcode
10.310 + class ArcIt : public Arc {
10.311 + public:
10.312 + /// Default constructor
10.313 +
10.314 + /// @warning The default constructor sets the iterator
10.315 + /// to an undefined value.
10.316 + ArcIt() { }
10.317 + /// Copy constructor.
10.318 +
10.319 + /// Copy constructor.
10.320 + ///
10.321 + ArcIt(const ArcIt& e) : Arc(e) { }
10.322 + /// Initialize the iterator to be invalid.
10.323 +
10.324 + /// Initialize the iterator to be invalid.
10.325 + ///
10.326 + ArcIt(Invalid) { }
10.327 + /// This constructor sets the iterator to the first arc.
10.328 +
10.329 + /// This constructor sets the iterator to the first arc of \c g.
10.330 + ///@param g the digraph
10.331 + ArcIt(const Digraph& g) { ignore_unused_variable_warning(g); }
10.332 + /// Arc -> ArcIt conversion
10.333 +
10.334 + /// Sets the iterator to the value of the trivial iterator \c e.
10.335 + /// This feature necessitates that each time we
10.336 + /// iterate the arc-set, the iteration order is the same.
10.337 + ArcIt(const Digraph&, const Arc&) { }
10.338 + ///Next arc
10.339 +
10.340 + /// Assign the iterator to the next arc.
10.341 + ArcIt& operator++() { return *this; }
10.342 + };
10.343 + ///Gives back the target node of an arc.
10.344 +
10.345 + ///Gives back the target node of an arc.
10.346 + ///
10.347 + Node target(Arc) const { return INVALID; }
10.348 + ///Gives back the source node of an arc.
10.349 +
10.350 + ///Gives back the source node of an arc.
10.351 + ///
10.352 + Node source(Arc) const { return INVALID; }
10.353 +
10.354 + void first(Node&) const {}
10.355 + void next(Node&) const {}
10.356 +
10.357 + void first(Arc&) const {}
10.358 + void next(Arc&) const {}
10.359 +
10.360 +
10.361 + void firstIn(Arc&, const Node&) const {}
10.362 + void nextIn(Arc&) const {}
10.363 +
10.364 + void firstOut(Arc&, const Node&) const {}
10.365 + void nextOut(Arc&) const {}
10.366 +
10.367 + /// \brief The base node of the iterator.
10.368 + ///
10.369 + /// Gives back the base node of the iterator.
10.370 + /// It is always the target of the pointed arc.
10.371 + Node baseNode(const InArcIt&) const { return INVALID; }
10.372 +
10.373 + /// \brief The running node of the iterator.
10.374 + ///
10.375 + /// Gives back the running node of the iterator.
10.376 + /// It is always the source of the pointed arc.
10.377 + Node runningNode(const InArcIt&) const { return INVALID; }
10.378 +
10.379 + /// \brief The base node of the iterator.
10.380 + ///
10.381 + /// Gives back the base node of the iterator.
10.382 + /// It is always the source of the pointed arc.
10.383 + Node baseNode(const OutArcIt&) const { return INVALID; }
10.384 +
10.385 + /// \brief The running node of the iterator.
10.386 + ///
10.387 + /// Gives back the running node of the iterator.
10.388 + /// It is always the target of the pointed arc.
10.389 + Node runningNode(const OutArcIt&) const { return INVALID; }
10.390 +
10.391 + /// \brief The opposite node on the given arc.
10.392 + ///
10.393 + /// Gives back the opposite node on the given arc.
10.394 + Node oppositeNode(const Node&, const Arc&) const { return INVALID; }
10.395 +
10.396 + /// \brief Read write map of the nodes to type \c T.
10.397 + ///
10.398 + /// ReadWrite map of the nodes to type \c T.
10.399 + /// \sa Reference
10.400 + template<class T>
10.401 + class NodeMap : public ReadWriteMap< Node, T > {
10.402 + public:
10.403 +
10.404 + ///\e
10.405 + NodeMap(const Digraph&) { }
10.406 + ///\e
10.407 + NodeMap(const Digraph&, T) { }
10.408 +
10.409 + ///Copy constructor
10.410 + NodeMap(const NodeMap& nm) : ReadWriteMap< Node, T >(nm) { }
10.411 + ///Assignment operator
10.412 + template <typename CMap>
10.413 + NodeMap& operator=(const CMap&) {
10.414 + checkConcept<ReadMap<Node, T>, CMap>();
10.415 + return *this;
10.416 + }
10.417 + };
10.418 +
10.419 + /// \brief Read write map of the arcs to type \c T.
10.420 + ///
10.421 + /// Reference map of the arcs to type \c T.
10.422 + /// \sa Reference
10.423 + template<class T>
10.424 + class ArcMap : public ReadWriteMap<Arc,T> {
10.425 + public:
10.426 +
10.427 + ///\e
10.428 + ArcMap(const Digraph&) { }
10.429 + ///\e
10.430 + ArcMap(const Digraph&, T) { }
10.431 + ///Copy constructor
10.432 + ArcMap(const ArcMap& em) : ReadWriteMap<Arc,T>(em) { }
10.433 + ///Assignment operator
10.434 + template <typename CMap>
10.435 + ArcMap& operator=(const CMap&) {
10.436 + checkConcept<ReadMap<Arc, T>, CMap>();
10.437 + return *this;
10.438 + }
10.439 + };
10.440 +
10.441 + template <typename RDigraph>
10.442 + struct Constraints {
10.443 + void constraints() {
10.444 + checkConcept<IterableDigraphComponent<>, Digraph>();
10.445 + checkConcept<MappableDigraphComponent<>, Digraph>();
10.446 + }
10.447 + };
10.448 +
10.449 + };
10.450 +
10.451 + } //namespace concepts
10.452 +} //namespace lemon
10.453 +
10.454 +
10.455 +
10.456 +#endif // LEMON_CONCEPT_DIGRAPH_H
11.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
11.2 +++ b/lemon/concepts/graph.h Sun Jan 20 20:43:48 2008 +0100
11.3 @@ -0,0 +1,702 @@
11.4 +/* -*- C++ -*-
11.5 + *
11.6 + * This file is a part of LEMON, a generic C++ optimization library
11.7 + *
11.8 + * Copyright (C) 2003-2007
11.9 + * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
11.10 + * (Egervary Research Group on Combinatorial Optimization, EGRES).
11.11 + *
11.12 + * Permission to use, modify and distribute this software is granted
11.13 + * provided that this copyright notice appears in all copies. For
11.14 + * precise terms see the accompanying LICENSE file.
11.15 + *
11.16 + * This software is provided "AS IS" with no warranty of any kind,
11.17 + * express or implied, and with no claim as to its suitability for any
11.18 + * purpose.
11.19 + *
11.20 + */
11.21 +
11.22 +///\ingroup graph_concepts
11.23 +///\file
11.24 +///\brief The concept of Undirected Graphs.
11.25 +
11.26 +#ifndef LEMON_CONCEPT_GRAPH_H
11.27 +#define LEMON_CONCEPT_GRAPH_H
11.28 +
11.29 +#include <lemon/concepts/graph_components.h>
11.30 +#include <lemon/concepts/graph.h>
11.31 +#include <lemon/bits/utility.h>
11.32 +
11.33 +namespace lemon {
11.34 + namespace concepts {
11.35 +
11.36 + /// \ingroup graph_concepts
11.37 + ///
11.38 + /// \brief Class describing the concept of Undirected Graphs.
11.39 + ///
11.40 + /// This class describes the common interface of all Undirected
11.41 + /// Graphs.
11.42 + ///
11.43 + /// As all concept describing classes it provides only interface
11.44 + /// without any sensible implementation. So any algorithm for
11.45 + /// undirected graph should compile with this class, but it will not
11.46 + /// run properly, of course.
11.47 + ///
11.48 + /// The LEMON undirected graphs also fulfill the concept of
11.49 + /// directed graphs (\ref lemon::concepts::Digraph "Digraph
11.50 + /// Concept"). Each edges can be seen as two opposite
11.51 + /// directed arc and consequently the undirected graph can be
11.52 + /// seen as the direceted graph of these directed arcs. The
11.53 + /// Graph has the Edge inner class for the edges and
11.54 + /// the Arc type for the directed arcs. The Arc type is
11.55 + /// convertible to Edge or inherited from it so from a directed
11.56 + /// arc we can get the represented edge.
11.57 + ///
11.58 + /// In the sense of the LEMON each edge has a default
11.59 + /// direction (it should be in every computer implementation,
11.60 + /// because the order of edge's nodes defines an
11.61 + /// orientation). With the default orientation we can define that
11.62 + /// the directed arc is forward or backward directed. With the \c
11.63 + /// direction() and \c direct() function we can get the direction
11.64 + /// of the directed arc and we can direct an edge.
11.65 + ///
11.66 + /// The EdgeIt is an iterator for the edges. We can use
11.67 + /// the EdgeMap to map values for the edges. The InArcIt and
11.68 + /// OutArcIt iterates on the same edges but with opposite
11.69 + /// direction. The IncArcIt iterates also on the same edges
11.70 + /// as the OutArcIt and InArcIt but it is not convertible to Arc just
11.71 + /// to Edge.
11.72 + class Graph {
11.73 + public:
11.74 + /// \brief The undirected graph should be tagged by the
11.75 + /// UndirectedTag.
11.76 + ///
11.77 + /// The undirected graph should be tagged by the UndirectedTag. This
11.78 + /// tag helps the enable_if technics to make compile time
11.79 + /// specializations for undirected graphs.
11.80 + typedef True UndirectedTag;
11.81 +
11.82 + /// \brief The base type of node iterators,
11.83 + /// or in other words, the trivial node iterator.
11.84 + ///
11.85 + /// This is the base type of each node iterator,
11.86 + /// thus each kind of node iterator converts to this.
11.87 + /// More precisely each kind of node iterator should be inherited
11.88 + /// from the trivial node iterator.
11.89 + class Node {
11.90 + public:
11.91 + /// Default constructor
11.92 +
11.93 + /// @warning The default constructor sets the iterator
11.94 + /// to an undefined value.
11.95 + Node() { }
11.96 + /// Copy constructor.
11.97 +
11.98 + /// Copy constructor.
11.99 + ///
11.100 + Node(const Node&) { }
11.101 +
11.102 + /// Invalid constructor \& conversion.
11.103 +
11.104 + /// This constructor initializes the iterator to be invalid.
11.105 + /// \sa Invalid for more details.
11.106 + Node(Invalid) { }
11.107 + /// Equality operator
11.108 +
11.109 + /// Two iterators are equal if and only if they point to the
11.110 + /// same object or both are invalid.
11.111 + bool operator==(Node) const { return true; }
11.112 +
11.113 + /// Inequality operator
11.114 +
11.115 + /// \sa operator==(Node n)
11.116 + ///
11.117 + bool operator!=(Node) const { return true; }
11.118 +
11.119 + /// Artificial ordering operator.
11.120 +
11.121 + /// To allow the use of graph descriptors as key type in std::map or
11.122 + /// similar associative container we require this.
11.123 + ///
11.124 + /// \note This operator only have to define some strict ordering of
11.125 + /// the items; this order has nothing to do with the iteration
11.126 + /// ordering of the items.
11.127 + bool operator<(Node) const { return false; }
11.128 +
11.129 + };
11.130 +
11.131 + /// This iterator goes through each node.
11.132 +
11.133 + /// This iterator goes through each node.
11.134 + /// Its usage is quite simple, for example you can count the number
11.135 + /// of nodes in graph \c g of type \c Graph like this:
11.136 + ///\code
11.137 + /// int count=0;
11.138 + /// for (Graph::NodeIt n(g); n!=INVALID; ++n) ++count;
11.139 + ///\endcode
11.140 + class NodeIt : public Node {
11.141 + public:
11.142 + /// Default constructor
11.143 +
11.144 + /// @warning The default constructor sets the iterator
11.145 + /// to an undefined value.
11.146 + NodeIt() { }
11.147 + /// Copy constructor.
11.148 +
11.149 + /// Copy constructor.
11.150 + ///
11.151 + NodeIt(const NodeIt& n) : Node(n) { }
11.152 + /// Invalid constructor \& conversion.
11.153 +
11.154 + /// Initialize the iterator to be invalid.
11.155 + /// \sa Invalid for more details.
11.156 + NodeIt(Invalid) { }
11.157 + /// Sets the iterator to the first node.
11.158 +
11.159 + /// Sets the iterator to the first node of \c g.
11.160 + ///
11.161 + NodeIt(const Graph&) { }
11.162 + /// Node -> NodeIt conversion.
11.163 +
11.164 + /// Sets the iterator to the node of \c the graph pointed by
11.165 + /// the trivial iterator.
11.166 + /// This feature necessitates that each time we
11.167 + /// iterate the arc-set, the iteration order is the same.
11.168 + NodeIt(const Graph&, const Node&) { }
11.169 + /// Next node.
11.170 +
11.171 + /// Assign the iterator to the next node.
11.172 + ///
11.173 + NodeIt& operator++() { return *this; }
11.174 + };
11.175 +
11.176 +
11.177 + /// The base type of the edge iterators.
11.178 +
11.179 + /// The base type of the edge iterators.
11.180 + ///
11.181 + class Edge {
11.182 + public:
11.183 + /// Default constructor
11.184 +
11.185 + /// @warning The default constructor sets the iterator
11.186 + /// to an undefined value.
11.187 + Edge() { }
11.188 + /// Copy constructor.
11.189 +
11.190 + /// Copy constructor.
11.191 + ///
11.192 + Edge(const Edge&) { }
11.193 + /// Initialize the iterator to be invalid.
11.194 +
11.195 + /// Initialize the iterator to be invalid.
11.196 + ///
11.197 + Edge(Invalid) { }
11.198 + /// Equality operator
11.199 +
11.200 + /// Two iterators are equal if and only if they point to the
11.201 + /// same object or both are invalid.
11.202 + bool operator==(Edge) const { return true; }
11.203 + /// Inequality operator
11.204 +
11.205 + /// \sa operator==(Edge n)
11.206 + ///
11.207 + bool operator!=(Edge) const { return true; }
11.208 +
11.209 + /// Artificial ordering operator.
11.210 +
11.211 + /// To allow the use of graph descriptors as key type in std::map or
11.212 + /// similar associative container we require this.
11.213 + ///
11.214 + /// \note This operator only have to define some strict ordering of
11.215 + /// the items; this order has nothing to do with the iteration
11.216 + /// ordering of the items.
11.217 + bool operator<(Edge) const { return false; }
11.218 + };
11.219 +
11.220 + /// This iterator goes through each edge.
11.221 +
11.222 + /// This iterator goes through each edge of a graph.
11.223 + /// Its usage is quite simple, for example you can count the number
11.224 + /// of edges in a graph \c g of type \c Graph as follows:
11.225 + ///\code
11.226 + /// int count=0;
11.227 + /// for(Graph::EdgeIt e(g); e!=INVALID; ++e) ++count;
11.228 + ///\endcode
11.229 + class EdgeIt : public Edge {
11.230 + public:
11.231 + /// Default constructor
11.232 +
11.233 + /// @warning The default constructor sets the iterator
11.234 + /// to an undefined value.
11.235 + EdgeIt() { }
11.236 + /// Copy constructor.
11.237 +
11.238 + /// Copy constructor.
11.239 + ///
11.240 + EdgeIt(const EdgeIt& e) : Edge(e) { }
11.241 + /// Initialize the iterator to be invalid.
11.242 +
11.243 + /// Initialize the iterator to be invalid.
11.244 + ///
11.245 + EdgeIt(Invalid) { }
11.246 + /// This constructor sets the iterator to the first edge.
11.247 +
11.248 + /// This constructor sets the iterator to the first edge.
11.249 + EdgeIt(const Graph&) { }
11.250 + /// Edge -> EdgeIt conversion
11.251 +
11.252 + /// Sets the iterator to the value of the trivial iterator.
11.253 + /// This feature necessitates that each time we
11.254 + /// iterate the edge-set, the iteration order is the
11.255 + /// same.
11.256 + EdgeIt(const Graph&, const Edge&) { }
11.257 + /// Next edge
11.258 +
11.259 + /// Assign the iterator to the next edge.
11.260 + EdgeIt& operator++() { return *this; }
11.261 + };
11.262 +
11.263 + /// \brief This iterator goes trough the incident undirected
11.264 + /// arcs of a node.
11.265 + ///
11.266 + /// This iterator goes trough the incident edges
11.267 + /// of a certain node of a graph. You should assume that the
11.268 + /// loop arcs will be iterated twice.
11.269 + ///
11.270 + /// Its usage is quite simple, for example you can compute the
11.271 + /// degree (i.e. count the number of incident arcs of a node \c n
11.272 + /// in graph \c g of type \c Graph as follows.
11.273 + ///
11.274 + ///\code
11.275 + /// int count=0;
11.276 + /// for(Graph::IncArcIt e(g, n); e!=INVALID; ++e) ++count;
11.277 + ///\endcode
11.278 + class IncArcIt : public Edge {
11.279 + public:
11.280 + /// Default constructor
11.281 +
11.282 + /// @warning The default constructor sets the iterator
11.283 + /// to an undefined value.
11.284 + IncArcIt() { }
11.285 + /// Copy constructor.
11.286 +
11.287 + /// Copy constructor.
11.288 + ///
11.289 + IncArcIt(const IncArcIt& e) : Edge(e) { }
11.290 + /// Initialize the iterator to be invalid.
11.291 +
11.292 + /// Initialize the iterator to be invalid.
11.293 + ///
11.294 + IncArcIt(Invalid) { }
11.295 + /// This constructor sets the iterator to first incident arc.
11.296 +
11.297 + /// This constructor set the iterator to the first incident arc of
11.298 + /// the node.
11.299 + IncArcIt(const Graph&, const Node&) { }
11.300 + /// Edge -> IncArcIt conversion
11.301 +
11.302 + /// Sets the iterator to the value of the trivial iterator \c e.
11.303 + /// This feature necessitates that each time we
11.304 + /// iterate the arc-set, the iteration order is the same.
11.305 + IncArcIt(const Graph&, const Edge&) { }
11.306 + /// Next incident arc
11.307 +
11.308 + /// Assign the iterator to the next incident arc
11.309 + /// of the corresponding node.
11.310 + IncArcIt& operator++() { return *this; }
11.311 + };
11.312 +
11.313 + /// The directed arc type.
11.314 +
11.315 + /// The directed arc type. It can be converted to the
11.316 + /// edge or it should be inherited from the undirected
11.317 + /// arc.
11.318 + class Arc : public Edge {
11.319 + public:
11.320 + /// Default constructor
11.321 +
11.322 + /// @warning The default constructor sets the iterator
11.323 + /// to an undefined value.
11.324 + Arc() { }
11.325 + /// Copy constructor.
11.326 +
11.327 + /// Copy constructor.
11.328 + ///
11.329 + Arc(const Arc& e) : Edge(e) { }
11.330 + /// Initialize the iterator to be invalid.
11.331 +
11.332 + /// Initialize the iterator to be invalid.
11.333 + ///
11.334 + Arc(Invalid) { }
11.335 + /// Equality operator
11.336 +
11.337 + /// Two iterators are equal if and only if they point to the
11.338 + /// same object or both are invalid.
11.339 + bool operator==(Arc) const { return true; }
11.340 + /// Inequality operator
11.341 +
11.342 + /// \sa operator==(Arc n)
11.343 + ///
11.344 + bool operator!=(Arc) const { return true; }
11.345 +
11.346 + /// Artificial ordering operator.
11.347 +
11.348 + /// To allow the use of graph descriptors as key type in std::map or
11.349 + /// similar associative container we require this.
11.350 + ///
11.351 + /// \note This operator only have to define some strict ordering of
11.352 + /// the items; this order has nothing to do with the iteration
11.353 + /// ordering of the items.
11.354 + bool operator<(Arc) const { return false; }
11.355 +
11.356 + };
11.357 + /// This iterator goes through each directed arc.
11.358 +
11.359 + /// This iterator goes through each arc of a graph.
11.360 + /// Its usage is quite simple, for example you can count the number
11.361 + /// of arcs in a graph \c g of type \c Graph as follows:
11.362 + ///\code
11.363 + /// int count=0;
11.364 + /// for(Graph::ArcIt e(g); e!=INVALID; ++e) ++count;
11.365 + ///\endcode
11.366 + class ArcIt : public Arc {
11.367 + public:
11.368 + /// Default constructor
11.369 +
11.370 + /// @warning The default constructor sets the iterator
11.371 + /// to an undefined value.
11.372 + ArcIt() { }
11.373 + /// Copy constructor.
11.374 +
11.375 + /// Copy constructor.
11.376 + ///
11.377 + ArcIt(const ArcIt& e) : Arc(e) { }
11.378 + /// Initialize the iterator to be invalid.
11.379 +
11.380 + /// Initialize the iterator to be invalid.
11.381 + ///
11.382 + ArcIt(Invalid) { }
11.383 + /// This constructor sets the iterator to the first arc.
11.384 +
11.385 + /// This constructor sets the iterator to the first arc of \c g.
11.386 + ///@param g the graph
11.387 + ArcIt(const Graph &g) { ignore_unused_variable_warning(g); }
11.388 + /// Arc -> ArcIt conversion
11.389 +
11.390 + /// Sets the iterator to the value of the trivial iterator \c e.
11.391 + /// This feature necessitates that each time we
11.392 + /// iterate the arc-set, the iteration order is the same.
11.393 + ArcIt(const Graph&, const Arc&) { }
11.394 + ///Next arc
11.395 +
11.396 + /// Assign the iterator to the next arc.
11.397 + ArcIt& operator++() { return *this; }
11.398 + };
11.399 +
11.400 + /// This iterator goes trough the outgoing directed arcs of a node.
11.401 +
11.402 + /// This iterator goes trough the \e outgoing arcs of a certain node
11.403 + /// of a graph.
11.404 + /// Its usage is quite simple, for example you can count the number
11.405 + /// of outgoing arcs of a node \c n
11.406 + /// in graph \c g of type \c Graph as follows.
11.407 + ///\code
11.408 + /// int count=0;
11.409 + /// for (Graph::OutArcIt e(g, n); e!=INVALID; ++e) ++count;
11.410 + ///\endcode
11.411 +
11.412 + class OutArcIt : public Arc {
11.413 + public:
11.414 + /// Default constructor
11.415 +
11.416 + /// @warning The default constructor sets the iterator
11.417 + /// to an undefined value.
11.418 + OutArcIt() { }
11.419 + /// Copy constructor.
11.420 +
11.421 + /// Copy constructor.
11.422 + ///
11.423 + OutArcIt(const OutArcIt& e) : Arc(e) { }
11.424 + /// Initialize the iterator to be invalid.
11.425 +
11.426 + /// Initialize the iterator to be invalid.
11.427 + ///
11.428 + OutArcIt(Invalid) { }
11.429 + /// This constructor sets the iterator to the first outgoing arc.
11.430 +
11.431 + /// This constructor sets the iterator to the first outgoing arc of
11.432 + /// the node.
11.433 + ///@param n the node
11.434 + ///@param g the graph
11.435 + OutArcIt(const Graph& n, const Node& g) {
11.436 + ignore_unused_variable_warning(n);
11.437 + ignore_unused_variable_warning(g);
11.438 + }
11.439 + /// Arc -> OutArcIt conversion
11.440 +
11.441 + /// Sets the iterator to the value of the trivial iterator.
11.442 + /// This feature necessitates that each time we
11.443 + /// iterate the arc-set, the iteration order is the same.
11.444 + OutArcIt(const Graph&, const Arc&) { }
11.445 + ///Next outgoing arc
11.446 +
11.447 + /// Assign the iterator to the next
11.448 + /// outgoing arc of the corresponding node.
11.449 + OutArcIt& operator++() { return *this; }
11.450 + };
11.451 +
11.452 + /// This iterator goes trough the incoming directed arcs of a node.
11.453 +
11.454 + /// This iterator goes trough the \e incoming arcs of a certain node
11.455 + /// of a graph.
11.456 + /// Its usage is quite simple, for example you can count the number
11.457 + /// of outgoing arcs of a node \c n
11.458 + /// in graph \c g of type \c Graph as follows.
11.459 + ///\code
11.460 + /// int count=0;
11.461 + /// for(Graph::InArcIt e(g, n); e!=INVALID; ++e) ++count;
11.462 + ///\endcode
11.463 +
11.464 + class InArcIt : public Arc {
11.465 + public:
11.466 + /// Default constructor
11.467 +
11.468 + /// @warning The default constructor sets the iterator
11.469 + /// to an undefined value.
11.470 + InArcIt() { }
11.471 + /// Copy constructor.
11.472 +
11.473 + /// Copy constructor.
11.474 + ///
11.475 + InArcIt(const InArcIt& e) : Arc(e) { }
11.476 + /// Initialize the iterator to be invalid.
11.477 +
11.478 + /// Initialize the iterator to be invalid.
11.479 + ///
11.480 + InArcIt(Invalid) { }
11.481 + /// This constructor sets the iterator to first incoming arc.
11.482 +
11.483 + /// This constructor set the iterator to the first incoming arc of
11.484 + /// the node.
11.485 + ///@param n the node
11.486 + ///@param g the graph
11.487 + InArcIt(const Graph& g, const Node& n) {
11.488 + ignore_unused_variable_warning(n);
11.489 + ignore_unused_variable_warning(g);
11.490 + }
11.491 + /// Arc -> InArcIt conversion
11.492 +
11.493 + /// Sets the iterator to the value of the trivial iterator \c e.
11.494 + /// This feature necessitates that each time we
11.495 + /// iterate the arc-set, the iteration order is the same.
11.496 + InArcIt(const Graph&, const Arc&) { }
11.497 + /// Next incoming arc
11.498 +
11.499 + /// Assign the iterator to the next inarc of the corresponding node.
11.500 + ///
11.501 + InArcIt& operator++() { return *this; }
11.502 + };
11.503 +
11.504 + /// \brief Read write map of the nodes to type \c T.
11.505 + ///
11.506 + /// ReadWrite map of the nodes to type \c T.
11.507 + /// \sa Reference
11.508 + template<class T>
11.509 + class NodeMap : public ReadWriteMap< Node, T >
11.510 + {
11.511 + public:
11.512 +
11.513 + ///\e
11.514 + NodeMap(const Graph&) { }
11.515 + ///\e
11.516 + NodeMap(const Graph&, T) { }
11.517 +
11.518 + ///Copy constructor
11.519 + NodeMap(const NodeMap& nm) : ReadWriteMap< Node, T >(nm) { }
11.520 + ///Assignment operator
11.521 + template <typename CMap>
11.522 + NodeMap& operator=(const CMap&) {
11.523 + checkConcept<ReadMap<Node, T>, CMap>();
11.524 + return *this;
11.525 + }
11.526 + };
11.527 +
11.528 + /// \brief Read write map of the directed arcs to type \c T.
11.529 + ///
11.530 + /// Reference map of the directed arcs to type \c T.
11.531 + /// \sa Reference
11.532 + template<class T>
11.533 + class ArcMap : public ReadWriteMap<Arc,T>
11.534 + {
11.535 + public:
11.536 +
11.537 + ///\e
11.538 + ArcMap(const Graph&) { }
11.539 + ///\e
11.540 + ArcMap(const Graph&, T) { }
11.541 + ///Copy constructor
11.542 + ArcMap(const ArcMap& em) : ReadWriteMap<Arc,T>(em) { }
11.543 + ///Assignment operator
11.544 + template <typename CMap>
11.545 + ArcMap& operator=(const CMap&) {
11.546 + checkConcept<ReadMap<Arc, T>, CMap>();
11.547 + return *this;
11.548 + }
11.549 + };
11.550 +
11.551 + /// Read write map of the edges to type \c T.
11.552 +
11.553 + /// Reference map of the arcs to type \c T.
11.554 + /// \sa Reference
11.555 + template<class T>
11.556 + class EdgeMap : public ReadWriteMap<Edge,T>
11.557 + {
11.558 + public:
11.559 +
11.560 + ///\e
11.561 + EdgeMap(const Graph&) { }
11.562 + ///\e
11.563 + EdgeMap(const Graph&, T) { }
11.564 + ///Copy constructor
11.565 + EdgeMap(const EdgeMap& em) : ReadWriteMap<Edge,T>(em) {}
11.566 + ///Assignment operator
11.567 + template <typename CMap>
11.568 + EdgeMap& operator=(const CMap&) {
11.569 + checkConcept<ReadMap<Edge, T>, CMap>();
11.570 + return *this;
11.571 + }
11.572 + };
11.573 +
11.574 + /// \brief Direct the given edge.
11.575 + ///
11.576 + /// Direct the given edge. The returned arc source
11.577 + /// will be the given node.
11.578 + Arc direct(const Edge&, const Node&) const {
11.579 + return INVALID;
11.580 + }
11.581 +
11.582 + /// \brief Direct the given edge.
11.583 + ///
11.584 + /// Direct the given edge. The returned arc
11.585 + /// represents the given edge and the direction comes
11.586 + /// from the bool parameter. The source of the edge and
11.587 + /// the directed arc is the same when the given bool is true.
11.588 + Arc direct(const Edge&, bool) const {
11.589 + return INVALID;
11.590 + }
11.591 +
11.592 + /// \brief Returns true if the arc has default orientation.
11.593 + ///
11.594 + /// Returns whether the given directed arc is same orientation as
11.595 + /// the corresponding edge's default orientation.
11.596 + bool direction(Arc) const { return true; }
11.597 +
11.598 + /// \brief Returns the opposite directed arc.
11.599 + ///
11.600 + /// Returns the opposite directed arc.
11.601 + Arc oppositeArc(Arc) const { return INVALID; }
11.602 +
11.603 + /// \brief Opposite node on an arc
11.604 + ///
11.605 + /// \return the opposite of the given Node on the given Edge
11.606 + Node oppositeNode(Node, Edge) const { return INVALID; }
11.607 +
11.608 + /// \brief First node of the edge.
11.609 + ///
11.610 + /// \return the first node of the given Edge.
11.611 + ///
11.612 + /// Naturally edges don't have direction and thus
11.613 + /// don't have source and target node. But we use these two methods
11.614 + /// to query the two nodes of the arc. The direction of the arc
11.615 + /// which arises this way is called the inherent direction of the
11.616 + /// edge, and is used to define the "default" direction
11.617 + /// of the directed versions of the arcs.
11.618 + /// \sa direction
11.619 + Node u(Edge) const { return INVALID; }
11.620 +
11.621 + /// \brief Second node of the edge.
11.622 + Node v(Edge) const { return INVALID; }
11.623 +
11.624 + /// \brief Source node of the directed arc.
11.625 + Node source(Arc) const { return INVALID; }
11.626 +
11.627 + /// \brief Target node of the directed arc.
11.628 + Node target(Arc) const { return INVALID; }
11.629 +
11.630 + void first(Node&) const {}
11.631 + void next(Node&) const {}
11.632 +
11.633 + void first(Edge&) const {}
11.634 + void next(Edge&) const {}
11.635 +
11.636 + void first(Arc&) const {}
11.637 + void next(Arc&) const {}
11.638 +
11.639 + void firstOut(Arc&, Node) const {}
11.640 + void nextOut(Arc&) const {}
11.641 +
11.642 + void firstIn(Arc&, Node) const {}
11.643 + void nextIn(Arc&) const {}
11.644 +
11.645 +
11.646 + void firstInc(Edge &, bool &, const Node &) const {}
11.647 + void nextInc(Edge &, bool &) const {}
11.648 +
11.649 + /// \brief Base node of the iterator
11.650 + ///
11.651 + /// Returns the base node (the source in this case) of the iterator
11.652 + Node baseNode(OutArcIt e) const {
11.653 + return source(e);
11.654 + }
11.655 + /// \brief Running node of the iterator
11.656 + ///
11.657 + /// Returns the running node (the target in this case) of the
11.658 + /// iterator
11.659 + Node runningNode(OutArcIt e) const {
11.660 + return target(e);
11.661 + }
11.662 +
11.663 + /// \brief Base node of the iterator
11.664 + ///
11.665 + /// Returns the base node (the target in this case) of the iterator
11.666 + Node baseNode(InArcIt e) const {
11.667 + return target(e);
11.668 + }
11.669 + /// \brief Running node of the iterator
11.670 + ///
11.671 + /// Returns the running node (the source in this case) of the
11.672 + /// iterator
11.673 + Node runningNode(InArcIt e) const {
11.674 + return source(e);
11.675 + }
11.676 +
11.677 + /// \brief Base node of the iterator
11.678 + ///
11.679 + /// Returns the base node of the iterator
11.680 + Node baseNode(IncArcIt) const {
11.681 + return INVALID;
11.682 + }
11.683 +
11.684 + /// \brief Running node of the iterator
11.685 + ///
11.686 + /// Returns the running node of the iterator
11.687 + Node runningNode(IncArcIt) const {
11.688 + return INVALID;
11.689 + }
11.690 +
11.691 + template <typename Graph>
11.692 + struct Constraints {
11.693 + void constraints() {
11.694 + checkConcept<IterableGraphComponent<>, Graph>();
11.695 + checkConcept<MappableGraphComponent<>, Graph>();
11.696 + }
11.697 + };
11.698 +
11.699 + };
11.700 +
11.701 + }
11.702 +
11.703 +}
11.704 +
11.705 +#endif
12.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
12.2 +++ b/lemon/concepts/graph_components.h Sun Jan 20 20:43:48 2008 +0100
12.3 @@ -0,0 +1,1490 @@
12.4 +/* -*- C++ -*-
12.5 + *
12.6 + * This file is a part of LEMON, a generic C++ optimization library
12.7 + *
12.8 + * Copyright (C) 2003-2007
12.9 + * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
12.10 + * (Egervary Research Group on Combinatorial Optimization, EGRES).
12.11 + *
12.12 + * Permission to use, modify and distribute this software is granted
12.13 + * provided that this copyright notice appears in all copies. For
12.14 + * precise terms see the accompanying LICENSE file.
12.15 + *
12.16 + * This software is provided "AS IS" with no warranty of any kind,
12.17 + * express or implied, and with no claim as to its suitability for any
12.18 + * purpose.
12.19 + *
12.20 + */
12.21 +
12.22 +///\ingroup graph_concepts
12.23 +///\file
12.24 +///\brief The concept of graph components.
12.25 +
12.26 +
12.27 +#ifndef LEMON_CONCEPT_GRAPH_COMPONENTS_H
12.28 +#define LEMON_CONCEPT_GRAPH_COMPONENTS_H
12.29 +
12.30 +#include <lemon/bits/invalid.h>
12.31 +#include <lemon/concepts/maps.h>
12.32 +
12.33 +#include <lemon/bits/alteration_notifier.h>
12.34 +
12.35 +namespace lemon {
12.36 + namespace concepts {
12.37 +
12.38 + /// \brief Skeleton class for graph Node and Arc types
12.39 + ///
12.40 + /// This class describes the interface of Node and Arc (and Edge
12.41 + /// in undirected graphs) subtypes of graph types.
12.42 + ///
12.43 + /// \note This class is a template class so that we can use it to
12.44 + /// create graph skeleton classes. The reason for this is than Node
12.45 + /// and Arc types should \em not derive from the same base class.
12.46 + /// For Node you should instantiate it with character 'n' and for Arc
12.47 + /// with 'a'.
12.48 +
12.49 +#ifndef DOXYGEN
12.50 + template <char _selector = '0'>
12.51 +#endif
12.52 + class GraphItem {
12.53 + public:
12.54 + /// \brief Default constructor.
12.55 + ///
12.56 + /// \warning The default constructor is not required to set
12.57 + /// the item to some well-defined value. So you should consider it
12.58 + /// as uninitialized.
12.59 + GraphItem() {}
12.60 + /// \brief Copy constructor.
12.61 + ///
12.62 + /// Copy constructor.
12.63 + ///
12.64 + GraphItem(const GraphItem &) {}
12.65 + /// \brief Invalid constructor \& conversion.
12.66 + ///
12.67 + /// This constructor initializes the item to be invalid.
12.68 + /// \sa Invalid for more details.
12.69 + GraphItem(Invalid) {}
12.70 + /// \brief Assign operator for nodes.
12.71 + ///
12.72 + /// The nodes are assignable.
12.73 + ///
12.74 + GraphItem& operator=(GraphItem const&) { return *this; }
12.75 + /// \brief Equality operator.
12.76 + ///
12.77 + /// Two iterators are equal if and only if they represents the
12.78 + /// same node in the graph or both are invalid.
12.79 + bool operator==(GraphItem) const { return false; }
12.80 + /// \brief Inequality operator.
12.81 + ///
12.82 + /// \sa operator==(const Node& n)
12.83 + ///
12.84 + bool operator!=(GraphItem) const { return false; }
12.85 +
12.86 + /// \brief Artificial ordering operator.
12.87 + ///
12.88 + /// To allow the use of graph descriptors as key type in std::map or
12.89 + /// similar associative container we require this.
12.90 + ///
12.91 + /// \note This operator only have to define some strict ordering of
12.92 + /// the items; this order has nothing to do with the iteration
12.93 + /// ordering of the items.
12.94 + bool operator<(GraphItem) const { return false; }
12.95 +
12.96 + template<typename _GraphItem>
12.97 + struct Constraints {
12.98 + void constraints() {
12.99 + _GraphItem i1;
12.100 + _GraphItem i2 = i1;
12.101 + _GraphItem i3 = INVALID;
12.102 +
12.103 + i1 = i2 = i3;
12.104 +
12.105 + bool b;
12.106 + // b = (ia == ib) && (ia != ib) && (ia < ib);
12.107 + b = (ia == ib) && (ia != ib);
12.108 + b = (ia == INVALID) && (ib != INVALID);
12.109 + b = (ia < ib);
12.110 + }
12.111 +
12.112 + const _GraphItem &ia;
12.113 + const _GraphItem &ib;
12.114 + };
12.115 + };
12.116 +
12.117 + /// \brief An empty base directed graph class.
12.118 + ///
12.119 + /// This class provides the minimal set of features needed for a
12.120 + /// directed graph structure. All digraph concepts have to be
12.121 + /// conform to this base directed graph. It just provides types
12.122 + /// for nodes and arcs and functions to get the source and the
12.123 + /// target of the arcs.
12.124 + class BaseDigraphComponent {
12.125 + public:
12.126 +
12.127 + typedef BaseDigraphComponent Digraph;
12.128 +
12.129 + /// \brief Node class of the digraph.
12.130 + ///
12.131 + /// This class represents the Nodes of the digraph.
12.132 + ///
12.133 + typedef GraphItem<'n'> Node;
12.134 +
12.135 + /// \brief Arc class of the digraph.
12.136 + ///
12.137 + /// This class represents the Arcs of the digraph.
12.138 + ///
12.139 + typedef GraphItem<'e'> Arc;
12.140 +
12.141 + /// \brief Gives back the target node of an arc.
12.142 + ///
12.143 + /// Gives back the target node of an arc.
12.144 + ///
12.145 + Node target(const Arc&) const { return INVALID;}
12.146 +
12.147 + /// \brief Gives back the source node of an arc.
12.148 + ///
12.149 + /// Gives back the source node of an arc.
12.150 + ///
12.151 + Node source(const Arc&) const { return INVALID;}
12.152 +
12.153 + /// \brief Gives back the opposite node on the given arc.
12.154 + ///
12.155 + /// Gives back the opposite node on the given arc.
12.156 + Node oppositeNode(const Node&, const Arc&) const {
12.157 + return INVALID;
12.158 + }
12.159 +
12.160 + template <typename _Digraph>
12.161 + struct Constraints {
12.162 + typedef typename _Digraph::Node Node;
12.163 + typedef typename _Digraph::Arc Arc;
12.164 +
12.165 + void constraints() {
12.166 + checkConcept<GraphItem<'n'>, Node>();
12.167 + checkConcept<GraphItem<'a'>, Arc>();
12.168 + {
12.169 + Node n;
12.170 + Arc e(INVALID);
12.171 + n = digraph.source(e);
12.172 + n = digraph.target(e);
12.173 + n = digraph.oppositeNode(n, e);
12.174 + }
12.175 + }
12.176 +
12.177 + const _Digraph& digraph;
12.178 + };
12.179 + };
12.180 +
12.181 + /// \brief An empty base undirected graph class.
12.182 + ///
12.183 + /// This class provides the minimal set of features needed for an
12.184 + /// undirected graph structure. All undirected graph concepts have
12.185 + /// to be conform to this base graph. It just provides types for
12.186 + /// nodes, arcs and edges and functions to get the
12.187 + /// source and the target of the arcs and edges,
12.188 + /// conversion from arcs to edges and function to get
12.189 + /// both direction of the edges.
12.190 + class BaseGraphComponent : public BaseDigraphComponent {
12.191 + public:
12.192 + typedef BaseDigraphComponent::Node Node;
12.193 + typedef BaseDigraphComponent::Arc Arc;
12.194 + /// \brief Undirected arc class of the graph.
12.195 + ///
12.196 + /// This class represents the edges of the graph.
12.197 + /// The undirected graphs can be used as a directed graph which
12.198 + /// for each arc contains the opposite arc too so the graph is
12.199 + /// bidirected. The edge represents two opposite
12.200 + /// directed arcs.
12.201 + class Edge : public GraphItem<'u'> {
12.202 + public:
12.203 + typedef GraphItem<'u'> Parent;
12.204 + /// \brief Default constructor.
12.205 + ///
12.206 + /// \warning The default constructor is not required to set
12.207 + /// the item to some well-defined value. So you should consider it
12.208 + /// as uninitialized.
12.209 + Edge() {}
12.210 + /// \brief Copy constructor.
12.211 + ///
12.212 + /// Copy constructor.
12.213 + ///
12.214 + Edge(const Edge &) : Parent() {}
12.215 + /// \brief Invalid constructor \& conversion.
12.216 + ///
12.217 + /// This constructor initializes the item to be invalid.
12.218 + /// \sa Invalid for more details.
12.219 + Edge(Invalid) {}
12.220 + /// \brief Converter from arc to edge.
12.221 + ///
12.222 + /// Besides the core graph item functionality each arc should
12.223 + /// be convertible to the represented edge.
12.224 + Edge(const Arc&) {}
12.225 + /// \brief Assign arc to edge.
12.226 + ///
12.227 + /// Besides the core graph item functionality each arc should
12.228 + /// be convertible to the represented edge.
12.229 + Edge& operator=(const Arc&) { return *this; }
12.230 + };
12.231 +
12.232 + /// \brief Returns the direction of the arc.
12.233 + ///
12.234 + /// Returns the direction of the arc. Each arc represents an
12.235 + /// edge with a direction. It gives back the
12.236 + /// direction.
12.237 + bool direction(const Arc&) const { return true; }
12.238 +
12.239 + /// \brief Returns the directed arc.
12.240 + ///
12.241 + /// Returns the directed arc from its direction and the
12.242 + /// represented edge.
12.243 + Arc direct(const Edge&, bool) const { return INVALID;}
12.244 +
12.245 + /// \brief Returns the directed arc.
12.246 + ///
12.247 + /// Returns the directed arc from its source and the
12.248 + /// represented edge.
12.249 + Arc direct(const Edge&, const Node&) const { return INVALID;}
12.250 +
12.251 + /// \brief Returns the opposite arc.
12.252 + ///
12.253 + /// Returns the opposite arc. It is the arc representing the
12.254 + /// same edge and has opposite direction.
12.255 + Arc oppositeArc(const Arc&) const { return INVALID;}
12.256 +
12.257 + /// \brief Gives back one ending of an edge.
12.258 + ///
12.259 + /// Gives back one ending of an edge.
12.260 + Node u(const Edge&) const { return INVALID;}
12.261 +
12.262 + /// \brief Gives back the other ending of an edge.
12.263 + ///
12.264 + /// Gives back the other ending of an edge.
12.265 + Node v(const Edge&) const { return INVALID;}
12.266 +
12.267 + template <typename _Graph>
12.268 + struct Constraints {
12.269 + typedef typename _Graph::Node Node;
12.270 + typedef typename _Graph::Arc Arc;
12.271 + typedef typename _Graph::Edge Edge;
12.272 +
12.273 + void constraints() {
12.274 + checkConcept<BaseDigraphComponent, _Graph>();
12.275 + checkConcept<GraphItem<'u'>, Edge>();
12.276 + {
12.277 + Node n;
12.278 + Edge ue(INVALID);
12.279 + Arc e;
12.280 + n = graph.u(ue);
12.281 + n = graph.v(ue);
12.282 + e = graph.direct(ue, true);
12.283 + e = graph.direct(ue, n);
12.284 + e = graph.oppositeArc(e);
12.285 + ue = e;
12.286 + bool d = graph.direction(e);
12.287 + ignore_unused_variable_warning(d);
12.288 + }
12.289 + }
12.290 +
12.291 + const _Graph& graph;
12.292 + };
12.293 +
12.294 + };
12.295 +
12.296 + /// \brief An empty idable base digraph class.
12.297 + ///
12.298 + /// This class provides beside the core digraph features
12.299 + /// core id functions for the digraph structure.
12.300 + /// The most of the base digraphs should be conform to this concept.
12.301 + /// The id's are unique and immutable.
12.302 + template <typename _Base = BaseDigraphComponent>
12.303 + class IDableDigraphComponent : public _Base {
12.304 + public:
12.305 +
12.306 + typedef _Base Base;
12.307 + typedef typename Base::Node Node;
12.308 + typedef typename Base::Arc Arc;
12.309 +
12.310 + /// \brief Gives back an unique integer id for the Node.
12.311 + ///
12.312 + /// Gives back an unique integer id for the Node.
12.313 + ///
12.314 + int id(const Node&) const { return -1;}
12.315 +
12.316 + /// \brief Gives back the node by the unique id.
12.317 + ///
12.318 + /// Gives back the node by the unique id.
12.319 + /// If the digraph does not contain node with the given id
12.320 + /// then the result of the function is undetermined.
12.321 + Node nodeFromId(int) const { return INVALID;}
12.322 +
12.323 + /// \brief Gives back an unique integer id for the Arc.
12.324 + ///
12.325 + /// Gives back an unique integer id for the Arc.
12.326 + ///
12.327 + int id(const Arc&) const { return -1;}
12.328 +
12.329 + /// \brief Gives back the arc by the unique id.
12.330 + ///
12.331 + /// Gives back the arc by the unique id.
12.332 + /// If the digraph does not contain arc with the given id
12.333 + /// then the result of the function is undetermined.
12.334 + Arc arcFromId(int) const { return INVALID;}
12.335 +
12.336 + /// \brief Gives back an integer greater or equal to the maximum
12.337 + /// Node id.
12.338 + ///
12.339 + /// Gives back an integer greater or equal to the maximum Node
12.340 + /// id.
12.341 + int maxNodeId() const { return -1;}
12.342 +
12.343 + /// \brief Gives back an integer greater or equal to the maximum
12.344 + /// Arc id.
12.345 + ///
12.346 + /// Gives back an integer greater or equal to the maximum Arc
12.347 + /// id.
12.348 + int maxArcId() const { return -1;}
12.349 +
12.350 + template <typename _Digraph>
12.351 + struct Constraints {
12.352 +
12.353 + void constraints() {
12.354 + checkConcept<Base, _Digraph >();
12.355 + typename _Digraph::Node node;
12.356 + int nid = digraph.id(node);
12.357 + nid = digraph.id(node);
12.358 + node = digraph.nodeFromId(nid);
12.359 + typename _Digraph::Arc arc;
12.360 + int eid = digraph.id(arc);
12.361 + eid = digraph.id(arc);
12.362 + arc = digraph.arcFromId(eid);
12.363 +
12.364 + nid = digraph.maxNodeId();
12.365 + ignore_unused_variable_warning(nid);
12.366 + eid = digraph.maxArcId();
12.367 + ignore_unused_variable_warning(eid);
12.368 + }
12.369 +
12.370 + const _Digraph& digraph;
12.371 + };
12.372 + };
12.373 +
12.374 + /// \brief An empty idable base undirected graph class.
12.375 + ///
12.376 + /// This class provides beside the core undirected graph features
12.377 + /// core id functions for the undirected graph structure. The
12.378 + /// most of the base undirected graphs should be conform to this
12.379 + /// concept. The id's are unique and immutable.
12.380 + template <typename _Base = BaseGraphComponent>
12.381 + class IDableGraphComponent : public IDableDigraphComponent<_Base> {
12.382 + public:
12.383 +
12.384 + typedef _Base Base;
12.385 + typedef typename Base::Edge Edge;
12.386 +
12.387 + using IDableDigraphComponent<_Base>::id;
12.388 +
12.389 + /// \brief Gives back an unique integer id for the Edge.
12.390 + ///
12.391 + /// Gives back an unique integer id for the Edge.
12.392 + ///
12.393 + int id(const Edge&) const { return -1;}
12.394 +
12.395 + /// \brief Gives back the edge by the unique id.
12.396 + ///
12.397 + /// Gives back the edge by the unique id. If the
12.398 + /// graph does not contain arc with the given id then the
12.399 + /// result of the function is undetermined.
12.400 + Edge edgeFromId(int) const { return INVALID;}
12.401 +
12.402 + /// \brief Gives back an integer greater or equal to the maximum
12.403 + /// Edge id.
12.404 + ///
12.405 + /// Gives back an integer greater or equal to the maximum Edge
12.406 + /// id.
12.407 + int maxEdgeId() const { return -1;}
12.408 +
12.409 + template <typename _Graph>
12.410 + struct Constraints {
12.411 +
12.412 + void constraints() {
12.413 + checkConcept<Base, _Graph >();
12.414 + checkConcept<IDableDigraphComponent<Base>, _Graph >();
12.415 + typename _Graph::Edge edge;
12.416 + int ueid = graph.id(edge);
12.417 + ueid = graph.id(edge);
12.418 + edge = graph.edgeFromId(ueid);
12.419 + ueid = graph.maxEdgeId();
12.420 + ignore_unused_variable_warning(ueid);
12.421 + }
12.422 +
12.423 + const _Graph& graph;
12.424 + };
12.425 + };
12.426 +
12.427 + /// \brief Skeleton class for graph NodeIt and ArcIt
12.428 + ///
12.429 + /// Skeleton class for graph NodeIt and ArcIt.
12.430 + ///
12.431 + template <typename _Graph, typename _Item>
12.432 + class GraphItemIt : public _Item {
12.433 + public:
12.434 + /// \brief Default constructor.
12.435 + ///
12.436 + /// @warning The default constructor sets the iterator
12.437 + /// to an undefined value.
12.438 + GraphItemIt() {}
12.439 + /// \brief Copy constructor.
12.440 + ///
12.441 + /// Copy constructor.
12.442 + ///
12.443 + GraphItemIt(const GraphItemIt& ) {}
12.444 + /// \brief Sets the iterator to the first item.
12.445 + ///
12.446 + /// Sets the iterator to the first item of \c the graph.
12.447 + ///
12.448 + explicit GraphItemIt(const _Graph&) {}
12.449 + /// \brief Invalid constructor \& conversion.
12.450 + ///
12.451 + /// This constructor initializes the item to be invalid.
12.452 + /// \sa Invalid for more details.
12.453 + GraphItemIt(Invalid) {}
12.454 + /// \brief Assign operator for items.
12.455 + ///
12.456 + /// The items are assignable.
12.457 + ///
12.458 + GraphItemIt& operator=(const GraphItemIt&) { return *this; }
12.459 + /// \brief Next item.
12.460 + ///
12.461 + /// Assign the iterator to the next item.
12.462 + ///
12.463 + GraphItemIt& operator++() { return *this; }
12.464 + /// \brief Equality operator
12.465 + ///
12.466 + /// Two iterators are equal if and only if they point to the
12.467 + /// same object or both are invalid.
12.468 + bool operator==(const GraphItemIt&) const { return true;}
12.469 + /// \brief Inequality operator
12.470 + ///
12.471 + /// \sa operator==(Node n)
12.472 + ///
12.473 + bool operator!=(const GraphItemIt&) const { return true;}
12.474 +
12.475 + template<typename _GraphItemIt>
12.476 + struct Constraints {
12.477 + void constraints() {
12.478 + _GraphItemIt it1(g);
12.479 + _GraphItemIt it2;
12.480 +
12.481 + it2 = ++it1;
12.482 + ++it2 = it1;
12.483 + ++(++it1);
12.484 +
12.485 + _Item bi = it1;
12.486 + bi = it2;
12.487 + }
12.488 + _Graph& g;
12.489 + };
12.490 + };
12.491 +
12.492 + /// \brief Skeleton class for graph InArcIt and OutArcIt
12.493 + ///
12.494 + /// \note Because InArcIt and OutArcIt may not inherit from the same
12.495 + /// base class, the _selector is a additional template parameter. For
12.496 + /// InArcIt you should instantiate it with character 'i' and for
12.497 + /// OutArcIt with 'o'.
12.498 + template <typename _Graph,
12.499 + typename _Item = typename _Graph::Arc,
12.500 + typename _Base = typename _Graph::Node,
12.501 + char _selector = '0'>
12.502 + class GraphIncIt : public _Item {
12.503 + public:
12.504 + /// \brief Default constructor.
12.505 + ///
12.506 + /// @warning The default constructor sets the iterator
12.507 + /// to an undefined value.
12.508 + GraphIncIt() {}
12.509 + /// \brief Copy constructor.
12.510 + ///
12.511 + /// Copy constructor.
12.512 + ///
12.513 + GraphIncIt(GraphIncIt const& gi) : _Item(gi) {}
12.514 + /// \brief Sets the iterator to the first arc incoming into or outgoing
12.515 + /// from the node.
12.516 + ///
12.517 + /// Sets the iterator to the first arc incoming into or outgoing
12.518 + /// from the node.
12.519 + ///
12.520 + explicit GraphIncIt(const _Graph&, const _Base&) {}
12.521 + /// \brief Invalid constructor \& conversion.
12.522 + ///
12.523 + /// This constructor initializes the item to be invalid.
12.524 + /// \sa Invalid for more details.
12.525 + GraphIncIt(Invalid) {}
12.526 + /// \brief Assign operator for iterators.
12.527 + ///
12.528 + /// The iterators are assignable.
12.529 + ///
12.530 + GraphIncIt& operator=(GraphIncIt const&) { return *this; }
12.531 + /// \brief Next item.
12.532 + ///
12.533 + /// Assign the iterator to the next item.
12.534 + ///
12.535 + GraphIncIt& operator++() { return *this; }
12.536 +
12.537 + /// \brief Equality operator
12.538 + ///
12.539 + /// Two iterators are equal if and only if they point to the
12.540 + /// same object or both are invalid.
12.541 + bool operator==(const GraphIncIt&) const { return true;}
12.542 +
12.543 + /// \brief Inequality operator
12.544 + ///
12.545 + /// \sa operator==(Node n)
12.546 + ///
12.547 + bool operator!=(const GraphIncIt&) const { return true;}
12.548 +
12.549 + template <typename _GraphIncIt>
12.550 + struct Constraints {
12.551 + void constraints() {
12.552 + checkConcept<GraphItem<_selector>, _GraphIncIt>();
12.553 + _GraphIncIt it1(graph, node);
12.554 + _GraphIncIt it2;
12.555 +
12.556 + it2 = ++it1;
12.557 + ++it2 = it1;
12.558 + ++(++it1);
12.559 + _Item e = it1;
12.560 + e = it2;
12.561 +
12.562 + }
12.563 +
12.564 + _Item arc;
12.565 + _Base node;
12.566 + _Graph graph;
12.567 + _GraphIncIt it;
12.568 + };
12.569 + };
12.570 +
12.571 +
12.572 + /// \brief An empty iterable digraph class.
12.573 + ///
12.574 + /// This class provides beside the core digraph features
12.575 + /// iterator based iterable interface for the digraph structure.
12.576 + /// This concept is part of the Digraph concept.
12.577 + template <typename _Base = BaseDigraphComponent>
12.578 + class IterableDigraphComponent : public _Base {
12.579 +
12.580 + public:
12.581 +
12.582 + typedef _Base Base;
12.583 + typedef typename Base::Node Node;
12.584 + typedef typename Base::Arc Arc;
12.585 +
12.586 + typedef IterableDigraphComponent Digraph;
12.587 +
12.588 + /// \name Base iteration
12.589 + ///
12.590 + /// This interface provides functions for iteration on digraph items
12.591 + ///
12.592 + /// @{
12.593 +
12.594 + /// \brief Gives back the first node in the iterating order.
12.595 + ///
12.596 + /// Gives back the first node in the iterating order.
12.597 + ///
12.598 + void first(Node&) const {}
12.599 +
12.600 + /// \brief Gives back the next node in the iterating order.
12.601 + ///
12.602 + /// Gives back the next node in the iterating order.
12.603 + ///
12.604 + void next(Node&) const {}
12.605 +
12.606 + /// \brief Gives back the first arc in the iterating order.
12.607 + ///
12.608 + /// Gives back the first arc in the iterating order.
12.609 + ///
12.610 + void first(Arc&) const {}
12.611 +
12.612 + /// \brief Gives back the next arc in the iterating order.
12.613 + ///
12.614 + /// Gives back the next arc in the iterating order.
12.615 + ///
12.616 + void next(Arc&) const {}
12.617 +
12.618 +
12.619 + /// \brief Gives back the first of the arcs point to the given
12.620 + /// node.
12.621 + ///
12.622 + /// Gives back the first of the arcs point to the given node.
12.623 + ///
12.624 + void firstIn(Arc&, const Node&) const {}
12.625 +
12.626 + /// \brief Gives back the next of the arcs points to the given
12.627 + /// node.
12.628 + ///
12.629 + /// Gives back the next of the arcs points to the given node.
12.630 + ///
12.631 + void nextIn(Arc&) const {}
12.632 +
12.633 + /// \brief Gives back the first of the arcs start from the
12.634 + /// given node.
12.635 + ///
12.636 + /// Gives back the first of the arcs start from the given node.
12.637 + ///
12.638 + void firstOut(Arc&, const Node&) const {}
12.639 +
12.640 + /// \brief Gives back the next of the arcs start from the given
12.641 + /// node.
12.642 + ///
12.643 + /// Gives back the next of the arcs start from the given node.
12.644 + ///
12.645 + void nextOut(Arc&) const {}
12.646 +
12.647 + /// @}
12.648 +
12.649 + /// \name Class based iteration
12.650 + ///
12.651 + /// This interface provides functions for iteration on digraph items
12.652 + ///
12.653 + /// @{
12.654 +
12.655 + /// \brief This iterator goes through each node.
12.656 + ///
12.657 + /// This iterator goes through each node.
12.658 + ///
12.659 + typedef GraphItemIt<Digraph, Node> NodeIt;
12.660 +
12.661 + /// \brief This iterator goes through each node.
12.662 + ///
12.663 + /// This iterator goes through each node.
12.664 + ///
12.665 + typedef GraphItemIt<Digraph, Arc> ArcIt;
12.666 +
12.667 + /// \brief This iterator goes trough the incoming arcs of a node.
12.668 + ///
12.669 + /// This iterator goes trough the \e inccoming arcs of a certain node
12.670 + /// of a digraph.
12.671 + typedef GraphIncIt<Digraph, Arc, Node, 'i'> InArcIt;
12.672 +
12.673 + /// \brief This iterator goes trough the outgoing arcs of a node.
12.674 + ///
12.675 + /// This iterator goes trough the \e outgoing arcs of a certain node
12.676 + /// of a digraph.
12.677 + typedef GraphIncIt<Digraph, Arc, Node, 'o'> OutArcIt;
12.678 +
12.679 + /// \brief The base node of the iterator.
12.680 + ///
12.681 + /// Gives back the base node of the iterator.
12.682 + /// It is always the target of the pointed arc.
12.683 + Node baseNode(const InArcIt&) const { return INVALID; }
12.684 +
12.685 + /// \brief The running node of the iterator.
12.686 + ///
12.687 + /// Gives back the running node of the iterator.
12.688 + /// It is always the source of the pointed arc.
12.689 + Node runningNode(const InArcIt&) const { return INVALID; }
12.690 +
12.691 + /// \brief The base node of the iterator.
12.692 + ///
12.693 + /// Gives back the base node of the iterator.
12.694 + /// It is always the source of the pointed arc.
12.695 + Node baseNode(const OutArcIt&) const { return INVALID; }
12.696 +
12.697 + /// \brief The running node of the iterator.
12.698 + ///
12.699 + /// Gives back the running node of the iterator.
12.700 + /// It is always the target of the pointed arc.
12.701 + Node runningNode(const OutArcIt&) const { return INVALID; }
12.702 +
12.703 + /// @}
12.704 +
12.705 + template <typename _Digraph>
12.706 + struct Constraints {
12.707 + void constraints() {
12.708 + checkConcept<Base, _Digraph>();
12.709 +
12.710 + {
12.711 + typename _Digraph::Node node(INVALID);
12.712 + typename _Digraph::Arc arc(INVALID);
12.713 + {
12.714 + digraph.first(node);
12.715 + digraph.next(node);
12.716 + }
12.717 + {
12.718 + digraph.first(arc);
12.719 + digraph.next(arc);
12.720 + }
12.721 + {
12.722 + digraph.firstIn(arc, node);
12.723 + digraph.nextIn(arc);
12.724 + }
12.725 + {
12.726 + digraph.firstOut(arc, node);
12.727 + digraph.nextOut(arc);
12.728 + }
12.729 + }
12.730 +
12.731 + {
12.732 + checkConcept<GraphItemIt<_Digraph, typename _Digraph::Arc>,
12.733 + typename _Digraph::ArcIt >();
12.734 + checkConcept<GraphItemIt<_Digraph, typename _Digraph::Node>,
12.735 + typename _Digraph::NodeIt >();
12.736 + checkConcept<GraphIncIt<_Digraph, typename _Digraph::Arc,
12.737 + typename _Digraph::Node, 'i'>, typename _Digraph::InArcIt>();
12.738 + checkConcept<GraphIncIt<_Digraph, typename _Digraph::Arc,
12.739 + typename _Digraph::Node, 'o'>, typename _Digraph::OutArcIt>();
12.740 +
12.741 + typename _Digraph::Node n;
12.742 + typename _Digraph::InArcIt ieit(INVALID);
12.743 + typename _Digraph::OutArcIt oeit(INVALID);
12.744 + n = digraph.baseNode(ieit);
12.745 + n = digraph.runningNode(ieit);
12.746 + n = digraph.baseNode(oeit);
12.747 + n = digraph.runningNode(oeit);
12.748 + ignore_unused_variable_warning(n);
12.749 + }
12.750 + }
12.751 +
12.752 + const _Digraph& digraph;
12.753 +
12.754 + };
12.755 + };
12.756 +
12.757 + /// \brief An empty iterable undirected graph class.
12.758 + ///
12.759 + /// This class provides beside the core graph features iterator
12.760 + /// based iterable interface for the undirected graph structure.
12.761 + /// This concept is part of the Graph concept.
12.762 + template <typename _Base = BaseGraphComponent>
12.763 + class IterableGraphComponent : public IterableDigraphComponent<_Base> {
12.764 + public:
12.765 +
12.766 + typedef _Base Base;
12.767 + typedef typename Base::Node Node;
12.768 + typedef typename Base::Arc Arc;
12.769 + typedef typename Base::Edge Edge;
12.770 +
12.771 +
12.772 + typedef IterableGraphComponent Graph;
12.773 +
12.774 + /// \name Base iteration
12.775 + ///
12.776 + /// This interface provides functions for iteration on graph items
12.777 + /// @{
12.778 +
12.779 + using IterableDigraphComponent<_Base>::first;
12.780 + using IterableDigraphComponent<_Base>::next;
12.781 +
12.782 + /// \brief Gives back the first edge in the iterating
12.783 + /// order.
12.784 + ///
12.785 + /// Gives back the first edge in the iterating order.
12.786 + ///
12.787 + void first(Edge&) const {}
12.788 +
12.789 + /// \brief Gives back the next edge in the iterating
12.790 + /// order.
12.791 + ///
12.792 + /// Gives back the next edge in the iterating order.
12.793 + ///
12.794 + void next(Edge&) const {}
12.795 +
12.796 +
12.797 + /// \brief Gives back the first of the edges from the
12.798 + /// given node.
12.799 + ///
12.800 + /// Gives back the first of the edges from the given
12.801 + /// node. The bool parameter gives back that direction which
12.802 + /// gives a good direction of the edge so the source of the
12.803 + /// directed arc is the given node.
12.804 + void firstInc(Edge&, bool&, const Node&) const {}
12.805 +
12.806 + /// \brief Gives back the next of the edges from the
12.807 + /// given node.
12.808 + ///
12.809 + /// Gives back the next of the edges from the given
12.810 + /// node. The bool parameter should be used as the \c firstInc()
12.811 + /// use it.
12.812 + void nextInc(Edge&, bool&) const {}
12.813 +
12.814 + using IterableDigraphComponent<_Base>::baseNode;
12.815 + using IterableDigraphComponent<_Base>::runningNode;
12.816 +
12.817 + /// @}
12.818 +
12.819 + /// \name Class based iteration
12.820 + ///
12.821 + /// This interface provides functions for iteration on graph items
12.822 + ///
12.823 + /// @{
12.824 +
12.825 + /// \brief This iterator goes through each node.
12.826 + ///
12.827 + /// This iterator goes through each node.
12.828 + typedef GraphItemIt<Graph, Edge> EdgeIt;
12.829 + /// \brief This iterator goes trough the incident arcs of a
12.830 + /// node.
12.831 + ///
12.832 + /// This iterator goes trough the incident arcs of a certain
12.833 + /// node of a graph.
12.834 + typedef GraphIncIt<Graph, Edge, Node, 'u'> IncArcIt;
12.835 + /// \brief The base node of the iterator.
12.836 + ///
12.837 + /// Gives back the base node of the iterator.
12.838 + Node baseNode(const IncArcIt&) const { return INVALID; }
12.839 +
12.840 + /// \brief The running node of the iterator.
12.841 + ///
12.842 + /// Gives back the running node of the iterator.
12.843 + Node runningNode(const IncArcIt&) const { return INVALID; }
12.844 +
12.845 + /// @}
12.846 +
12.847 + template <typename _Graph>
12.848 + struct Constraints {
12.849 + void constraints() {
12.850 + checkConcept<IterableDigraphComponent<Base>, _Graph>();
12.851 +
12.852 + {
12.853 + typename _Graph::Node node(INVALID);
12.854 + typename _Graph::Edge edge(INVALID);
12.855 + bool dir;
12.856 + {
12.857 + graph.first(edge);
12.858 + graph.next(edge);
12.859 + }
12.860 + {
12.861 + graph.firstInc(edge, dir, node);
12.862 + graph.nextInc(edge, dir);
12.863 + }
12.864 +
12.865 + }
12.866 +
12.867 + {
12.868 + checkConcept<GraphItemIt<_Graph, typename _Graph::Edge>,
12.869 + typename _Graph::EdgeIt >();
12.870 + checkConcept<GraphIncIt<_Graph, typename _Graph::Edge,
12.871 + typename _Graph::Node, 'u'>, typename _Graph::IncArcIt>();
12.872 +
12.873 + typename _Graph::Node n;
12.874 + typename _Graph::IncArcIt ueit(INVALID);
12.875 + n = graph.baseNode(ueit);
12.876 + n = graph.runningNode(ueit);
12.877 + }
12.878 + }
12.879 +
12.880 + const _Graph& graph;
12.881 +
12.882 + };
12.883 + };
12.884 +
12.885 + /// \brief An empty alteration notifier digraph class.
12.886 + ///
12.887 + /// This class provides beside the core digraph features alteration
12.888 + /// notifier interface for the digraph structure. This implements
12.889 + /// an observer-notifier pattern for each digraph item. More
12.890 + /// obsevers can be registered into the notifier and whenever an
12.891 + /// alteration occured in the digraph all the observers will
12.892 + /// notified about it.
12.893 + template <typename _Base = BaseDigraphComponent>
12.894 + class AlterableDigraphComponent : public _Base {
12.895 + public:
12.896 +
12.897 + typedef _Base Base;
12.898 + typedef typename Base::Node Node;
12.899 + typedef typename Base::Arc Arc;
12.900 +
12.901 +
12.902 + /// The node observer registry.
12.903 + typedef AlterationNotifier<AlterableDigraphComponent, Node>
12.904 + NodeNotifier;
12.905 + /// The arc observer registry.
12.906 + typedef AlterationNotifier<AlterableDigraphComponent, Arc>
12.907 + ArcNotifier;
12.908 +
12.909 + /// \brief Gives back the node alteration notifier.
12.910 + ///
12.911 + /// Gives back the node alteration notifier.
12.912 + NodeNotifier& notifier(Node) const {
12.913 + return NodeNotifier();
12.914 + }
12.915 +
12.916 + /// \brief Gives back the arc alteration notifier.
12.917 + ///
12.918 + /// Gives back the arc alteration notifier.
12.919 + ArcNotifier& notifier(Arc) const {
12.920 + return ArcNotifier();
12.921 + }
12.922 +
12.923 + template <typename _Digraph>
12.924 + struct Constraints {
12.925 + void constraints() {
12.926 + checkConcept<Base, _Digraph>();
12.927 + typename _Digraph::NodeNotifier& nn
12.928 + = digraph.notifier(typename _Digraph::Node());
12.929 +
12.930 + typename _Digraph::ArcNotifier& en
12.931 + = digraph.notifier(typename _Digraph::Arc());
12.932 +
12.933 + ignore_unused_variable_warning(nn);
12.934 + ignore_unused_variable_warning(en);
12.935 + }
12.936 +
12.937 + const _Digraph& digraph;
12.938 +
12.939 + };
12.940 +
12.941 + };
12.942 +
12.943 + /// \brief An empty alteration notifier undirected graph class.
12.944 + ///
12.945 + /// This class provides beside the core graph features alteration
12.946 + /// notifier interface for the graph structure. This implements
12.947 + /// an observer-notifier pattern for each graph item. More
12.948 + /// obsevers can be registered into the notifier and whenever an
12.949 + /// alteration occured in the graph all the observers will
12.950 + /// notified about it.
12.951 + template <typename _Base = BaseGraphComponent>
12.952 + class AlterableGraphComponent : public AlterableDigraphComponent<_Base> {
12.953 + public:
12.954 +
12.955 + typedef _Base Base;
12.956 + typedef typename Base::Edge Edge;
12.957 +
12.958 +
12.959 + /// The arc observer registry.
12.960 + typedef AlterationNotifier<AlterableGraphComponent, Edge>
12.961 + EdgeNotifier;
12.962 +
12.963 + /// \brief Gives back the arc alteration notifier.
12.964 + ///
12.965 + /// Gives back the arc alteration notifier.
12.966 + EdgeNotifier& notifier(Edge) const {
12.967 + return EdgeNotifier();
12.968 + }
12.969 +
12.970 + template <typename _Graph>
12.971 + struct Constraints {
12.972 + void constraints() {
12.973 + checkConcept<AlterableGraphComponent<Base>, _Graph>();
12.974 + typename _Graph::EdgeNotifier& uen
12.975 + = graph.notifier(typename _Graph::Edge());
12.976 + ignore_unused_variable_warning(uen);
12.977 + }
12.978 +
12.979 + const _Graph& graph;
12.980 +
12.981 + };
12.982 +
12.983 + };
12.984 +
12.985 + /// \brief Class describing the concept of graph maps
12.986 + ///
12.987 + /// This class describes the common interface of the graph maps
12.988 + /// (NodeMap, ArcMap), that is \ref maps-page "maps" which can be used to
12.989 + /// associate data to graph descriptors (nodes or arcs).
12.990 + template <typename _Graph, typename _Item, typename _Value>
12.991 + class GraphMap : public ReadWriteMap<_Item, _Value> {
12.992 + public:
12.993 +
12.994 + typedef ReadWriteMap<_Item, _Value> Parent;
12.995 +
12.996 + /// The graph type of the map.
12.997 + typedef _Graph Graph;
12.998 + /// The key type of the map.
12.999 + typedef _Item Key;
12.1000 + /// The value type of the map.
12.1001 + typedef _Value Value;
12.1002 +
12.1003 + /// \brief Construct a new map.
12.1004 + ///
12.1005 + /// Construct a new map for the graph.
12.1006 + explicit GraphMap(const Graph&) {}
12.1007 + /// \brief Construct a new map with default value.
12.1008 + ///
12.1009 + /// Construct a new map for the graph and initalise the values.
12.1010 + GraphMap(const Graph&, const Value&) {}
12.1011 + /// \brief Copy constructor.
12.1012 + ///
12.1013 + /// Copy Constructor.
12.1014 + GraphMap(const GraphMap&) : Parent() {}
12.1015 +
12.1016 + /// \brief Assign operator.
12.1017 + ///
12.1018 + /// Assign operator. It does not mofify the underlying graph,
12.1019 + /// it just iterates on the current item set and set the map
12.1020 + /// with the value returned by the assigned map.
12.1021 + template <typename CMap>
12.1022 + GraphMap& operator=(const CMap&) {
12.1023 + checkConcept<ReadMap<Key, Value>, CMap>();
12.1024 + return *this;
12.1025 + }
12.1026 +
12.1027 + template<typename _Map>
12.1028 + struct Constraints {
12.1029 + void constraints() {
12.1030 + checkConcept<ReadWriteMap<Key, Value>, _Map >();
12.1031 + // Construction with a graph parameter
12.1032 + _Map a(g);
12.1033 + // Constructor with a graph and a default value parameter
12.1034 + _Map a2(g,t);
12.1035 + // Copy constructor.
12.1036 + _Map b(c);
12.1037 +
12.1038 + ReadMap<Key, Value> cmap;
12.1039 + b = cmap;
12.1040 +
12.1041 + ignore_unused_variable_warning(a2);
12.1042 + ignore_unused_variable_warning(b);
12.1043 + }
12.1044 +
12.1045 + const _Map &c;
12.1046 + const Graph &g;
12.1047 + const typename GraphMap::Value &t;
12.1048 + };
12.1049 +
12.1050 + };
12.1051 +
12.1052 + /// \brief An empty mappable digraph class.
12.1053 + ///
12.1054 + /// This class provides beside the core digraph features
12.1055 + /// map interface for the digraph structure.
12.1056 + /// This concept is part of the Digraph concept.
12.1057 + template <typename _Base = BaseDigraphComponent>
12.1058 + class MappableDigraphComponent : public _Base {
12.1059 + public:
12.1060 +
12.1061 + typedef _Base Base;
12.1062 + typedef typename Base::Node Node;
12.1063 + typedef typename Base::Arc Arc;
12.1064 +
12.1065 + typedef MappableDigraphComponent Digraph;
12.1066 +
12.1067 + /// \brief ReadWrite map of the nodes.
12.1068 + ///
12.1069 + /// ReadWrite map of the nodes.
12.1070 + ///
12.1071 + template <typename _Value>
12.1072 + class NodeMap : public GraphMap<Digraph, Node, _Value> {
12.1073 + public:
12.1074 + typedef GraphMap<MappableDigraphComponent, Node, _Value> Parent;
12.1075 +
12.1076 + /// \brief Construct a new map.
12.1077 + ///
12.1078 + /// Construct a new map for the digraph.
12.1079 + explicit NodeMap(const MappableDigraphComponent& digraph)
12.1080 + : Parent(digraph) {}
12.1081 +
12.1082 + /// \brief Construct a new map with default value.
12.1083 + ///
12.1084 + /// Construct a new map for the digraph and initalise the values.
12.1085 + NodeMap(const MappableDigraphComponent& digraph, const _Value& value)
12.1086 + : Parent(digraph, value) {}
12.1087 +
12.1088 + /// \brief Copy constructor.
12.1089 + ///
12.1090 + /// Copy Constructor.
12.1091 + NodeMap(const NodeMap& nm) : Parent(nm) {}
12.1092 +
12.1093 + /// \brief Assign operator.
12.1094 + ///
12.1095 + /// Assign operator.
12.1096 + template <typename CMap>
12.1097 + NodeMap& operator=(const CMap&) {
12.1098 + checkConcept<ReadMap<Node, _Value>, CMap>();
12.1099 + return *this;
12.1100 + }
12.1101 +
12.1102 + };
12.1103 +
12.1104 + /// \brief ReadWrite map of the arcs.
12.1105 + ///
12.1106 + /// ReadWrite map of the arcs.
12.1107 + ///
12.1108 + template <typename _Value>
12.1109 + class ArcMap : public GraphMap<Digraph, Arc, _Value> {
12.1110 + public:
12.1111 + typedef GraphMap<MappableDigraphComponent, Arc, _Value> Parent;
12.1112 +
12.1113 + /// \brief Construct a new map.
12.1114 + ///
12.1115 + /// Construct a new map for the digraph.
12.1116 + explicit ArcMap(const MappableDigraphComponent& digraph)
12.1117 + : Parent(digraph) {}
12.1118 +
12.1119 + /// \brief Construct a new map with default value.
12.1120 + ///
12.1121 + /// Construct a new map for the digraph and initalise the values.
12.1122 + ArcMap(const MappableDigraphComponent& digraph, const _Value& value)
12.1123 + : Parent(digraph, value) {}
12.1124 +
12.1125 + /// \brief Copy constructor.
12.1126 + ///
12.1127 + /// Copy Constructor.
12.1128 + ArcMap(const ArcMap& nm) : Parent(nm) {}
12.1129 +
12.1130 + /// \brief Assign operator.
12.1131 + ///
12.1132 + /// Assign operator.
12.1133 + template <typename CMap>
12.1134 + ArcMap& operator=(const CMap&) {
12.1135 + checkConcept<ReadMap<Arc, _Value>, CMap>();
12.1136 + return *this;
12.1137 + }
12.1138 +
12.1139 + };
12.1140 +
12.1141 +
12.1142 + template <typename _Digraph>
12.1143 + struct Constraints {
12.1144 +
12.1145 + struct Dummy {
12.1146 + int value;
12.1147 + Dummy() : value(0) {}
12.1148 + Dummy(int _v) : value(_v) {}
12.1149 + };
12.1150 +
12.1151 + void constraints() {
12.1152 + checkConcept<Base, _Digraph>();
12.1153 + { // int map test
12.1154 + typedef typename _Digraph::template NodeMap<int> IntNodeMap;
12.1155 + checkConcept<GraphMap<_Digraph, typename _Digraph::Node, int>,
12.1156 + IntNodeMap >();
12.1157 + } { // bool map test
12.1158 + typedef typename _Digraph::template NodeMap<bool> BoolNodeMap;
12.1159 + checkConcept<GraphMap<_Digraph, typename _Digraph::Node, bool>,
12.1160 + BoolNodeMap >();
12.1161 + } { // Dummy map test
12.1162 + typedef typename _Digraph::template NodeMap<Dummy> DummyNodeMap;
12.1163 + checkConcept<GraphMap<_Digraph, typename _Digraph::Node, Dummy>,
12.1164 + DummyNodeMap >();
12.1165 + }
12.1166 +
12.1167 + { // int map test
12.1168 + typedef typename _Digraph::template ArcMap<int> IntArcMap;
12.1169 + checkConcept<GraphMap<_Digraph, typename _Digraph::Arc, int>,
12.1170 + IntArcMap >();
12.1171 + } { // bool map test
12.1172 + typedef typename _Digraph::template ArcMap<bool> BoolArcMap;
12.1173 + checkConcept<GraphMap<_Digraph, typename _Digraph::Arc, bool>,
12.1174 + BoolArcMap >();
12.1175 + } { // Dummy map test
12.1176 + typedef typename _Digraph::template ArcMap<Dummy> DummyArcMap;
12.1177 + checkConcept<GraphMap<_Digraph, typename _Digraph::Arc, Dummy>,
12.1178 + DummyArcMap >();
12.1179 + }
12.1180 + }
12.1181 +
12.1182 + _Digraph& digraph;
12.1183 + };
12.1184 + };
12.1185 +
12.1186 + /// \brief An empty mappable base bipartite graph class.
12.1187 + ///
12.1188 + /// This class provides beside the core graph features
12.1189 + /// map interface for the graph structure.
12.1190 + /// This concept is part of the Graph concept.
12.1191 + template <typename _Base = BaseGraphComponent>
12.1192 + class MappableGraphComponent : public MappableDigraphComponent<_Base> {
12.1193 + public:
12.1194 +
12.1195 + typedef _Base Base;
12.1196 + typedef typename Base::Edge Edge;
12.1197 +
12.1198 + typedef MappableGraphComponent Graph;
12.1199 +
12.1200 + /// \brief ReadWrite map of the edges.
12.1201 + ///
12.1202 + /// ReadWrite map of the edges.
12.1203 + ///
12.1204 + template <typename _Value>
12.1205 + class EdgeMap : public GraphMap<Graph, Edge, _Value> {
12.1206 + public:
12.1207 + typedef GraphMap<MappableGraphComponent, Edge, _Value> Parent;
12.1208 +
12.1209 + /// \brief Construct a new map.
12.1210 + ///
12.1211 + /// Construct a new map for the graph.
12.1212 + explicit EdgeMap(const MappableGraphComponent& graph)
12.1213 + : Parent(graph) {}
12.1214 +
12.1215 + /// \brief Construct a new map with default value.
12.1216 + ///
12.1217 + /// Construct a new map for the graph and initalise the values.
12.1218 + EdgeMap(const MappableGraphComponent& graph, const _Value& value)
12.1219 + : Parent(graph, value) {}
12.1220 +
12.1221 + /// \brief Copy constructor.
12.1222 + ///
12.1223 + /// Copy Constructor.
12.1224 + EdgeMap(const EdgeMap& nm) : Parent(nm) {}
12.1225 +
12.1226 + /// \brief Assign operator.
12.1227 + ///
12.1228 + /// Assign operator.
12.1229 + template <typename CMap>
12.1230 + EdgeMap& operator=(const CMap&) {
12.1231 + checkConcept<ReadMap<Edge, _Value>, CMap>();
12.1232 + return *this;
12.1233 + }
12.1234 +
12.1235 + };
12.1236 +
12.1237 +
12.1238 + template <typename _Graph>
12.1239 + struct Constraints {
12.1240 +
12.1241 + struct Dummy {
12.1242 + int value;
12.1243 + Dummy() : value(0) {}
12.1244 + Dummy(int _v) : value(_v) {}
12.1245 + };
12.1246 +
12.1247 + void constraints() {
12.1248 + checkConcept<MappableGraphComponent<Base>, _Graph>();
12.1249 +
12.1250 + { // int map test
12.1251 + typedef typename _Graph::template EdgeMap<int> IntEdgeMap;
12.1252 + checkConcept<GraphMap<_Graph, typename _Graph::Edge, int>,
12.1253 + IntEdgeMap >();
12.1254 + } { // bool map test
12.1255 + typedef typename _Graph::template EdgeMap<bool> BoolEdgeMap;
12.1256 + checkConcept<GraphMap<_Graph, typename _Graph::Edge, bool>,
12.1257 + BoolEdgeMap >();
12.1258 + } { // Dummy map test
12.1259 + typedef typename _Graph::template EdgeMap<Dummy> DummyEdgeMap;
12.1260 + checkConcept<GraphMap<_Graph, typename _Graph::Edge, Dummy>,
12.1261 + DummyEdgeMap >();
12.1262 + }
12.1263 + }
12.1264 +
12.1265 + _Graph& graph;
12.1266 + };
12.1267 + };
12.1268 +
12.1269 + /// \brief An empty extendable digraph class.
12.1270 + ///
12.1271 + /// This class provides beside the core digraph features digraph
12.1272 + /// extendable interface for the digraph structure. The main
12.1273 + /// difference between the base and this interface is that the
12.1274 + /// digraph alterations should handled already on this level.
12.1275 + template <typename _Base = BaseDigraphComponent>
12.1276 + class ExtendableDigraphComponent : public _Base {
12.1277 + public:
12.1278 + typedef _Base Base;
12.1279 +
12.1280 + typedef typename _Base::Node Node;
12.1281 + typedef typename _Base::Arc Arc;
12.1282 +
12.1283 + /// \brief Adds a new node to the digraph.
12.1284 + ///
12.1285 + /// Adds a new node to the digraph.
12.1286 + ///
12.1287 + Node addNode() {
12.1288 + return INVALID;
12.1289 + }
12.1290 +
12.1291 + /// \brief Adds a new arc connects the given two nodes.
12.1292 + ///
12.1293 + /// Adds a new arc connects the the given two nodes.
12.1294 + Arc addArc(const Node&, const Node&) {
12.1295 + return INVALID;
12.1296 + }
12.1297 +
12.1298 + template <typename _Digraph>
12.1299 + struct Constraints {
12.1300 + void constraints() {
12.1301 + checkConcept<Base, _Digraph>();
12.1302 + typename _Digraph::Node node_a, node_b;
12.1303 + node_a = digraph.addNode();
12.1304 + node_b = digraph.addNode();
12.1305 + typename _Digraph::Arc arc;
12.1306 + arc = digraph.addArc(node_a, node_b);
12.1307 + }
12.1308 +
12.1309 + _Digraph& digraph;
12.1310 + };
12.1311 + };
12.1312 +
12.1313 + /// \brief An empty extendable base undirected graph class.
12.1314 + ///
12.1315 + /// This class provides beside the core undirected graph features
12.1316 + /// core undircted graph extend interface for the graph structure.
12.1317 + /// The main difference between the base and this interface is
12.1318 + /// that the graph alterations should handled already on this
12.1319 + /// level.
12.1320 + template <typename _Base = BaseGraphComponent>
12.1321 + class ExtendableGraphComponent : public _Base {
12.1322 + public:
12.1323 +
12.1324 + typedef _Base Base;
12.1325 + typedef typename _Base::Node Node;
12.1326 + typedef typename _Base::Edge Edge;
12.1327 +
12.1328 + /// \brief Adds a new node to the graph.
12.1329 + ///
12.1330 + /// Adds a new node to the graph.
12.1331 + ///
12.1332 + Node addNode() {
12.1333 + return INVALID;
12.1334 + }
12.1335 +
12.1336 + /// \brief Adds a new arc connects the given two nodes.
12.1337 + ///
12.1338 + /// Adds a new arc connects the the given two nodes.
12.1339 + Edge addArc(const Node&, const Node&) {
12.1340 + return INVALID;
12.1341 + }
12.1342 +
12.1343 + template <typename _Graph>
12.1344 + struct Constraints {
12.1345 + void constraints() {
12.1346 + checkConcept<Base, _Graph>();
12.1347 + typename _Graph::Node node_a, node_b;
12.1348 + node_a = graph.addNode();
12.1349 + node_b = graph.addNode();
12.1350 + typename _Graph::Edge edge;
12.1351 + edge = graph.addEdge(node_a, node_b);
12.1352 + }
12.1353 +
12.1354 + _Graph& graph;
12.1355 + };
12.1356 + };
12.1357 +
12.1358 + /// \brief An empty erasable digraph class.
12.1359 + ///
12.1360 + /// This class provides beside the core digraph features core erase
12.1361 + /// functions for the digraph structure. The main difference between
12.1362 + /// the base and this interface is that the digraph alterations
12.1363 + /// should handled already on this level.
12.1364 + template <typename _Base = BaseDigraphComponent>
12.1365 + class ErasableDigraphComponent : public _Base {
12.1366 + public:
12.1367 +
12.1368 + typedef _Base Base;
12.1369 + typedef typename Base::Node Node;
12.1370 + typedef typename Base::Arc Arc;
12.1371 +
12.1372 + /// \brief Erase a node from the digraph.
12.1373 + ///
12.1374 + /// Erase a node from the digraph. This function should
12.1375 + /// erase all arcs connecting to the node.
12.1376 + void erase(const Node&) {}
12.1377 +
12.1378 + /// \brief Erase an arc from the digraph.
12.1379 + ///
12.1380 + /// Erase an arc from the digraph.
12.1381 + ///
12.1382 + void erase(const Arc&) {}
12.1383 +
12.1384 + template <typename _Digraph>
12.1385 + struct Constraints {
12.1386 + void constraints() {
12.1387 + checkConcept<Base, _Digraph>();
12.1388 + typename _Digraph::Node node;
12.1389 + digraph.erase(node);
12.1390 + typename _Digraph::Arc arc;
12.1391 + digraph.erase(arc);
12.1392 + }
12.1393 +
12.1394 + _Digraph& digraph;
12.1395 + };
12.1396 + };
12.1397 +
12.1398 + /// \brief An empty erasable base undirected graph class.
12.1399 + ///
12.1400 + /// This class provides beside the core undirected graph features
12.1401 + /// core erase functions for the undirceted graph structure. The
12.1402 + /// main difference between the base and this interface is that
12.1403 + /// the graph alterations should handled already on this level.
12.1404 + template <typename _Base = BaseGraphComponent>
12.1405 + class ErasableGraphComponent : public _Base {
12.1406 + public:
12.1407 +
12.1408 + typedef _Base Base;
12.1409 + typedef typename Base::Node Node;
12.1410 + typedef typename Base::Edge Edge;
12.1411 +
12.1412 + /// \brief Erase a node from the graph.
12.1413 + ///
12.1414 + /// Erase a node from the graph. This function should erase
12.1415 + /// arcs connecting to the node.
12.1416 + void erase(const Node&) {}
12.1417 +
12.1418 + /// \brief Erase an arc from the graph.
12.1419 + ///
12.1420 + /// Erase an arc from the graph.
12.1421 + ///
12.1422 + void erase(const Edge&) {}
12.1423 +
12.1424 + template <typename _Graph>
12.1425 + struct Constraints {
12.1426 + void constraints() {
12.1427 + checkConcept<Base, _Graph>();
12.1428 + typename _Graph::Node node;
12.1429 + graph.erase(node);
12.1430 + typename _Graph::Arc arc;
12.1431 + graph.erase(arc);
12.1432 + }
12.1433 +
12.1434 + _Graph& graph;
12.1435 + };
12.1436 + };
12.1437 +
12.1438 + /// \brief An empty clearable base digraph class.
12.1439 + ///
12.1440 + /// This class provides beside the core digraph features core clear
12.1441 + /// functions for the digraph structure. The main difference between
12.1442 + /// the base and this interface is that the digraph alterations
12.1443 + /// should handled already on this level.
12.1444 + template <typename _Base = BaseDigraphComponent>
12.1445 + class ClearableDigraphComponent : public _Base {
12.1446 + public:
12.1447 +
12.1448 + typedef _Base Base;
12.1449 +
12.1450 + /// \brief Erase all nodes and arcs from the digraph.
12.1451 + ///
12.1452 + /// Erase all nodes and arcs from the digraph.
12.1453 + ///
12.1454 + void clear() {}
12.1455 +
12.1456 + template <typename _Digraph>
12.1457 + struct Constraints {
12.1458 + void constraints() {
12.1459 + checkConcept<Base, _Digraph>();
12.1460 + digraph.clear();
12.1461 + }
12.1462 +
12.1463 + _Digraph digraph;
12.1464 + };
12.1465 + };
12.1466 +
12.1467 + /// \brief An empty clearable base undirected graph class.
12.1468 + ///
12.1469 + /// This class provides beside the core undirected graph features
12.1470 + /// core clear functions for the undirected graph structure. The
12.1471 + /// main difference between the base and this interface is that
12.1472 + /// the graph alterations should handled already on this level.
12.1473 + template <typename _Base = BaseGraphComponent>
12.1474 + class ClearableGraphComponent : public ClearableDigraphComponent<_Base> {
12.1475 + public:
12.1476 +
12.1477 + typedef _Base Base;
12.1478 +
12.1479 + template <typename _Graph>
12.1480 + struct Constraints {
12.1481 + void constraints() {
12.1482 + checkConcept<ClearableGraphComponent<Base>, _Graph>();
12.1483 + }
12.1484 +
12.1485 + _Graph graph;
12.1486 + };
12.1487 + };
12.1488 +
12.1489 + }
12.1490 +
12.1491 +}
12.1492 +
12.1493 +#endif
13.1 --- a/lemon/list_graph.h Sat Jan 12 23:30:44 2008 +0000
13.2 +++ b/lemon/list_graph.h Sun Jan 20 20:43:48 2008 +0100
13.3 @@ -16,3 +16,1452 @@
13.4 *
13.5 */
13.6
13.7 +#ifndef LEMON_LIST_GRAPH_H
13.8 +#define LEMON_LIST_GRAPH_H
13.9 +
13.10 +///\ingroup graphs
13.11 +///\file
13.12 +///\brief ListDigraph, ListGraph classes.
13.13 +
13.14 +#include <lemon/bits/graph_extender.h>
13.15 +
13.16 +#include <vector>
13.17 +#include <list>
13.18 +
13.19 +namespace lemon {
13.20 +
13.21 + class ListDigraphBase {
13.22 +
13.23 + protected:
13.24 + struct NodeT {
13.25 + int first_in, first_out;
13.26 + int prev, next;
13.27 + };
13.28 +
13.29 + struct ArcT {
13.30 + int target, source;
13.31 + int prev_in, prev_out;
13.32 + int next_in, next_out;
13.33 + };
13.34 +
13.35 + std::vector<NodeT> nodes;
13.36 +
13.37 + int first_node;
13.38 +
13.39 + int first_free_node;
13.40 +
13.41 + std::vector<ArcT> arcs;
13.42 +
13.43 + int first_free_arc;
13.44 +
13.45 + public:
13.46 +
13.47 + typedef ListDigraphBase Digraph;
13.48 +
13.49 + class Node {
13.50 + friend class ListDigraphBase;
13.51 + protected:
13.52 +
13.53 + int id;
13.54 + explicit Node(int pid) { id = pid;}
13.55 +
13.56 + public:
13.57 + Node() {}
13.58 + Node (Invalid) { id = -1; }
13.59 + bool operator==(const Node& node) const {return id == node.id;}
13.60 + bool operator!=(const Node& node) const {return id != node.id;}
13.61 + bool operator<(const Node& node) const {return id < node.id;}
13.62 + };
13.63 +
13.64 + class Arc {
13.65 + friend class ListDigraphBase;
13.66 + protected:
13.67 +
13.68 + int id;
13.69 + explicit Arc(int pid) { id = pid;}
13.70 +
13.71 + public:
13.72 + Arc() {}
13.73 + Arc (Invalid) { id = -1; }
13.74 + bool operator==(const Arc& arc) const {return id == arc.id;}
13.75 + bool operator!=(const Arc& arc) const {return id != arc.id;}
13.76 + bool operator<(const Arc& arc) const {return id < arc.id;}
13.77 + };
13.78 +
13.79 +
13.80 +
13.81 + ListDigraphBase()
13.82 + : nodes(), first_node(-1),
13.83 + first_free_node(-1), arcs(), first_free_arc(-1) {}
13.84 +
13.85 +
13.86 + int maxNodeId() const { return nodes.size()-1; }
13.87 + int maxArcId() const { return arcs.size()-1; }
13.88 +
13.89 + Node source(Arc e) const { return Node(arcs[e.id].source); }
13.90 + Node target(Arc e) const { return Node(arcs[e.id].target); }
13.91 +
13.92 +
13.93 + void first(Node& node) const {
13.94 + node.id = first_node;
13.95 + }
13.96 +
13.97 + void next(Node& node) const {
13.98 + node.id = nodes[node.id].next;
13.99 + }
13.100 +
13.101 +
13.102 + void first(Arc& e) const {
13.103 + int n;
13.104 + for(n = first_node;
13.105 + n!=-1 && nodes[n].first_in == -1;
13.106 + n = nodes[n].next);
13.107 + e.id = (n == -1) ? -1 : nodes[n].first_in;
13.108 + }
13.109 +
13.110 + void next(Arc& arc) const {
13.111 + if (arcs[arc.id].next_in != -1) {
13.112 + arc.id = arcs[arc.id].next_in;
13.113 + } else {
13.114 + int n;
13.115 + for(n = nodes[arcs[arc.id].target].next;
13.116 + n!=-1 && nodes[n].first_in == -1;
13.117 + n = nodes[n].next);
13.118 + arc.id = (n == -1) ? -1 : nodes[n].first_in;
13.119 + }
13.120 + }
13.121 +
13.122 + void firstOut(Arc &e, const Node& v) const {
13.123 + e.id = nodes[v.id].first_out;
13.124 + }
13.125 + void nextOut(Arc &e) const {
13.126 + e.id=arcs[e.id].next_out;
13.127 + }
13.128 +
13.129 + void firstIn(Arc &e, const Node& v) const {
13.130 + e.id = nodes[v.id].first_in;
13.131 + }
13.132 + void nextIn(Arc &e) const {
13.133 + e.id=arcs[e.id].next_in;
13.134 + }
13.135 +
13.136 +
13.137 + static int id(Node v) { return v.id; }
13.138 + static int id(Arc e) { return e.id; }
13.139 +
13.140 + static Node nodeFromId(int id) { return Node(id);}
13.141 + static Arc arcFromId(int id) { return Arc(id);}
13.142 +
13.143 + Node addNode() {
13.144 + int n;
13.145 +
13.146 + if(first_free_node==-1) {
13.147 + n = nodes.size();
13.148 + nodes.push_back(NodeT());
13.149 + } else {
13.150 + n = first_free_node;
13.151 + first_free_node = nodes[n].next;
13.152 + }
13.153 +
13.154 + nodes[n].next = first_node;
13.155 + if(first_node != -1) nodes[first_node].prev = n;
13.156 + first_node = n;
13.157 + nodes[n].prev = -1;
13.158 +
13.159 + nodes[n].first_in = nodes[n].first_out = -1;
13.160 +
13.161 + return Node(n);
13.162 + }
13.163 +
13.164 + Arc addArc(Node u, Node v) {
13.165 + int n;
13.166 +
13.167 + if (first_free_arc == -1) {
13.168 + n = arcs.size();
13.169 + arcs.push_back(ArcT());
13.170 + } else {
13.171 + n = first_free_arc;
13.172 + first_free_arc = arcs[n].next_in;
13.173 + }
13.174 +
13.175 + arcs[n].source = u.id;
13.176 + arcs[n].target = v.id;
13.177 +
13.178 + arcs[n].next_out = nodes[u.id].first_out;
13.179 + if(nodes[u.id].first_out != -1) {
13.180 + arcs[nodes[u.id].first_out].prev_out = n;
13.181 + }
13.182 +
13.183 + arcs[n].next_in = nodes[v.id].first_in;
13.184 + if(nodes[v.id].first_in != -1) {
13.185 + arcs[nodes[v.id].first_in].prev_in = n;
13.186 + }
13.187 +
13.188 + arcs[n].prev_in = arcs[n].prev_out = -1;
13.189 +
13.190 + nodes[u.id].first_out = nodes[v.id].first_in = n;
13.191 +
13.192 + return Arc(n);
13.193 + }
13.194 +
13.195 + void erase(const Node& node) {
13.196 + int n = node.id;
13.197 +
13.198 + if(nodes[n].next != -1) {
13.199 + nodes[nodes[n].next].prev = nodes[n].prev;
13.200 + }
13.201 +
13.202 + if(nodes[n].prev != -1) {
13.203 + nodes[nodes[n].prev].next = nodes[n].next;
13.204 + } else {
13.205 + first_node = nodes[n].next;
13.206 + }
13.207 +
13.208 + nodes[n].next = first_free_node;
13.209 + first_free_node = n;
13.210 +
13.211 + }
13.212 +
13.213 + void erase(const Arc& arc) {
13.214 + int n = arc.id;
13.215 +
13.216 + if(arcs[n].next_in!=-1) {
13.217 + arcs[arcs[n].next_in].prev_in = arcs[n].prev_in;
13.218 + }
13.219 +
13.220 + if(arcs[n].prev_in!=-1) {
13.221 + arcs[arcs[n].prev_in].next_in = arcs[n].next_in;
13.222 + } else {
13.223 + nodes[arcs[n].target].first_in = arcs[n].next_in;
13.224 + }
13.225 +
13.226 +
13.227 + if(arcs[n].next_out!=-1) {
13.228 + arcs[arcs[n].next_out].prev_out = arcs[n].prev_out;
13.229 + }
13.230 +
13.231 + if(arcs[n].prev_out!=-1) {
13.232 + arcs[arcs[n].prev_out].next_out = arcs[n].next_out;
13.233 + } else {
13.234 + nodes[arcs[n].source].first_out = arcs[n].next_out;
13.235 + }
13.236 +
13.237 + arcs[n].next_in = first_free_arc;
13.238 + first_free_arc = n;
13.239 +
13.240 + }
13.241 +
13.242 + void clear() {
13.243 + arcs.clear();
13.244 + nodes.clear();
13.245 + first_node = first_free_node = first_free_arc = -1;
13.246 + }
13.247 +
13.248 + protected:
13.249 + void changeTarget(Arc e, Node n)
13.250 + {
13.251 + if(arcs[e.id].next_in != -1)
13.252 + arcs[arcs[e.id].next_in].prev_in = arcs[e.id].prev_in;
13.253 + if(arcs[e.id].prev_in != -1)
13.254 + arcs[arcs[e.id].prev_in].next_in = arcs[e.id].next_in;
13.255 + else nodes[arcs[e.id].target].first_in = arcs[e.id].next_in;
13.256 + if (nodes[n.id].first_in != -1) {
13.257 + arcs[nodes[n.id].first_in].prev_in = e.id;
13.258 + }
13.259 + arcs[e.id].target = n.id;
13.260 + arcs[e.id].prev_in = -1;
13.261 + arcs[e.id].next_in = nodes[n.id].first_in;
13.262 + nodes[n.id].first_in = e.id;
13.263 + }
13.264 + void changeSource(Arc e, Node n)
13.265 + {
13.266 + if(arcs[e.id].next_out != -1)
13.267 + arcs[arcs[e.id].next_out].prev_out = arcs[e.id].prev_out;
13.268 + if(arcs[e.id].prev_out != -1)
13.269 + arcs[arcs[e.id].prev_out].next_out = arcs[e.id].next_out;
13.270 + else nodes[arcs[e.id].source].first_out = arcs[e.id].next_out;
13.271 + if (nodes[n.id].first_out != -1) {
13.272 + arcs[nodes[n.id].first_out].prev_out = e.id;
13.273 + }
13.274 + arcs[e.id].source = n.id;
13.275 + arcs[e.id].prev_out = -1;
13.276 + arcs[e.id].next_out = nodes[n.id].first_out;
13.277 + nodes[n.id].first_out = e.id;
13.278 + }
13.279 +
13.280 + };
13.281 +
13.282 + typedef DigraphExtender<ListDigraphBase> ExtendedListDigraphBase;
13.283 +
13.284 + /// \addtogroup digraphs
13.285 + /// @{
13.286 +
13.287 + ///A list digraph class.
13.288 +
13.289 + ///This is a simple and fast digraph implementation.
13.290 + ///
13.291 + ///It conforms to the \ref concepts::Digraph "Digraph concept" and it
13.292 + ///also provides several additional useful extra functionalities.
13.293 + ///The most of the member functions and nested classes are
13.294 + ///documented only in the concept class.
13.295 + ///
13.296 + ///An important extra feature of this digraph implementation is that
13.297 + ///its maps are real \ref concepts::ReferenceMap "reference map"s.
13.298 + ///
13.299 + ///\sa concepts::Digraph.
13.300 +
13.301 + class ListDigraph : public ExtendedListDigraphBase {
13.302 + private:
13.303 + ///ListDigraph is \e not copy constructible. Use DigraphCopy() instead.
13.304 +
13.305 + ///ListDigraph is \e not copy constructible. Use DigraphCopy() instead.
13.306 + ///
13.307 + ListDigraph(const ListDigraph &) :ExtendedListDigraphBase() {};
13.308 + ///\brief Assignment of ListDigraph to another one is \e not allowed.
13.309 + ///Use DigraphCopy() instead.
13.310 +
13.311 + ///Assignment of ListDigraph to another one is \e not allowed.
13.312 + ///Use DigraphCopy() instead.
13.313 + void operator=(const ListDigraph &) {}
13.314 + public:
13.315 +
13.316 + typedef ExtendedListDigraphBase Parent;
13.317 +
13.318 + /// Constructor
13.319 +
13.320 + /// Constructor.
13.321 + ///
13.322 + ListDigraph() {}
13.323 +
13.324 + ///Add a new node to the digraph.
13.325 +
13.326 + /// \return the new node.
13.327 + ///
13.328 + Node addNode() { return Parent::addNode(); }
13.329 +
13.330 + ///Add a new arc to the digraph.
13.331 +
13.332 + ///Add a new arc to the digraph with source node \c s
13.333 + ///and target node \c t.
13.334 + ///\return the new arc.
13.335 + Arc addArc(const Node& s, const Node& t) {
13.336 + return Parent::addArc(s, t);
13.337 + }
13.338 +
13.339 + /// Changes the target of \c e to \c n
13.340 +
13.341 + /// Changes the target of \c e to \c n
13.342 + ///
13.343 + ///\note The <tt>ArcIt</tt>s and <tt>OutArcIt</tt>s referencing
13.344 + ///the changed arc remain valid. However <tt>InArcIt</tt>s are
13.345 + ///invalidated.
13.346 + ///\warning This functionality cannot be used together with the Snapshot
13.347 + ///feature.
13.348 + void changeTarget(Arc e, Node n) {
13.349 + Parent::changeTarget(e,n);
13.350 + }
13.351 + /// Changes the source of \c e to \c n
13.352 +
13.353 + /// Changes the source of \c e to \c n
13.354 + ///
13.355 + ///\note The <tt>ArcIt</tt>s and <tt>InArcIt</tt>s referencing
13.356 + ///the changed arc remain valid. However <tt>OutArcIt</tt>s are
13.357 + ///invalidated.
13.358 + ///\warning This functionality cannot be used together with the Snapshot
13.359 + ///feature.
13.360 + void changeSource(Arc e, Node n) {
13.361 + Parent::changeSource(e,n);
13.362 + }
13.363 +
13.364 + /// Invert the direction of an arc.
13.365 +
13.366 + ///\note The <tt>ArcIt</tt>s referencing the changed arc remain
13.367 + ///valid. However <tt>OutArcIt</tt>s and <tt>InArcIt</tt>s are
13.368 + ///invalidated.
13.369 + ///\warning This functionality cannot be used together with the Snapshot
13.370 + ///feature.
13.371 + void reverseArc(Arc e) {
13.372 + Node t=target(e);
13.373 + changeTarget(e,source(e));
13.374 + changeSource(e,t);
13.375 + }
13.376 +
13.377 + /// Using this it is possible to avoid the superfluous memory
13.378 + /// allocation: if you know that the digraph you want to build will
13.379 + /// be very large (e.g. it will contain millions of nodes and/or arcs)
13.380 + /// then it is worth reserving space for this amount before starting
13.381 + /// to build the digraph.
13.382 + /// \sa reserveArc
13.383 + void reserveNode(int n) { nodes.reserve(n); };
13.384 +
13.385 + /// \brief Using this it is possible to avoid the superfluous memory
13.386 + /// allocation.
13.387 +
13.388 + /// Using this it is possible to avoid the superfluous memory
13.389 + /// allocation: if you know that the digraph you want to build will
13.390 + /// be very large (e.g. it will contain millions of nodes and/or arcs)
13.391 + /// then it is worth reserving space for this amount before starting
13.392 + /// to build the digraph.
13.393 + /// \sa reserveNode
13.394 + void reserveArc(int m) { arcs.reserve(m); };
13.395 +
13.396 + ///Contract two nodes.
13.397 +
13.398 + ///This function contracts two nodes.
13.399 + ///
13.400 + ///Node \p b will be removed but instead of deleting
13.401 + ///incident arcs, they will be joined to \p a.
13.402 + ///The last parameter \p r controls whether to remove loops. \c true
13.403 + ///means that loops will be removed.
13.404 + ///
13.405 + ///\note The <tt>ArcIt</tt>s
13.406 + ///referencing a moved arc remain
13.407 + ///valid. However <tt>InArcIt</tt>s and <tt>OutArcIt</tt>s
13.408 + ///may be invalidated.
13.409 + ///\warning This functionality cannot be used together with the Snapshot
13.410 + ///feature.
13.411 + void contract(Node a, Node b, bool r = true)
13.412 + {
13.413 + for(OutArcIt e(*this,b);e!=INVALID;) {
13.414 + OutArcIt f=e;
13.415 + ++f;
13.416 + if(r && target(e)==a) erase(e);
13.417 + else changeSource(e,a);
13.418 + e=f;
13.419 + }
13.420 + for(InArcIt e(*this,b);e!=INVALID;) {
13.421 + InArcIt f=e;
13.422 + ++f;
13.423 + if(r && source(e)==a) erase(e);
13.424 + else changeTarget(e,a);
13.425 + e=f;
13.426 + }
13.427 + erase(b);
13.428 + }
13.429 +
13.430 + ///Split a node.
13.431 +
13.432 + ///This function splits a node. First a new node is added to the digraph,
13.433 + ///then the source of each outgoing arc of \c n is moved to this new node.
13.434 + ///If \c connect is \c true (this is the default value), then a new arc
13.435 + ///from \c n to the newly created node is also added.
13.436 + ///\return The newly created node.
13.437 + ///
13.438 + ///\note The <tt>ArcIt</tt>s referencing a moved arc remain
13.439 + ///valid. However <tt>InArcIt</tt>s and <tt>OutArcIt</tt>s may
13.440 + ///be invalidated.
13.441 + ///
13.442 + ///\warning This functionality cannot be used together with the
13.443 + ///Snapshot feature. \todo It could be implemented in a bit
13.444 + ///faster way.
13.445 + Node split(Node n, bool connect = true) {
13.446 + Node b = addNode();
13.447 + for(OutArcIt e(*this,n);e!=INVALID;) {
13.448 + OutArcIt f=e;
13.449 + ++f;
13.450 + changeSource(e,b);
13.451 + e=f;
13.452 + }
13.453 + if (connect) addArc(n,b);
13.454 + return b;
13.455 + }
13.456 +
13.457 + ///Split an arc.
13.458 +
13.459 + ///This function splits an arc. First a new node \c b is added to
13.460 + ///the digraph, then the original arc is re-targeted to \c
13.461 + ///b. Finally an arc from \c b to the original target is added.
13.462 + ///\return The newly created node.
13.463 + ///\warning This functionality
13.464 + ///cannot be used together with the Snapshot feature.
13.465 + Node split(Arc e) {
13.466 + Node b = addNode();
13.467 + addArc(b,target(e));
13.468 + changeTarget(e,b);
13.469 + return b;
13.470 + }
13.471 +
13.472 + /// \brief Class to make a snapshot of the digraph and restore
13.473 + /// to it later.
13.474 + ///
13.475 + /// Class to make a snapshot of the digraph and to restore it
13.476 + /// later.
13.477 + ///
13.478 + /// The newly added nodes and arcs can be removed using the
13.479 + /// restore() function.
13.480 + ///
13.481 + /// \warning Arc and node deletions cannot be restored. This
13.482 + /// events invalidate the snapshot.
13.483 + class Snapshot {
13.484 + protected:
13.485 +
13.486 + typedef Parent::NodeNotifier NodeNotifier;
13.487 +
13.488 + class NodeObserverProxy : public NodeNotifier::ObserverBase {
13.489 + public:
13.490 +
13.491 + NodeObserverProxy(Snapshot& _snapshot)
13.492 + : snapshot(_snapshot) {}
13.493 +
13.494 + using NodeNotifier::ObserverBase::attach;
13.495 + using NodeNotifier::ObserverBase::detach;
13.496 + using NodeNotifier::ObserverBase::attached;
13.497 +
13.498 + protected:
13.499 +
13.500 + virtual void add(const Node& node) {
13.501 + snapshot.addNode(node);
13.502 + }
13.503 + virtual void add(const std::vector<Node>& nodes) {
13.504 + for (int i = nodes.size() - 1; i >= 0; ++i) {
13.505 + snapshot.addNode(nodes[i]);
13.506 + }
13.507 + }
13.508 + virtual void erase(const Node& node) {
13.509 + snapshot.eraseNode(node);
13.510 + }
13.511 + virtual void erase(const std::vector<Node>& nodes) {
13.512 + for (int i = 0; i < int(nodes.size()); ++i) {
13.513 + snapshot.eraseNode(nodes[i]);
13.514 + }
13.515 + }
13.516 + virtual void build() {
13.517 + Node node;
13.518 + std::vector<Node> nodes;
13.519 + for (notifier()->first(node); node != INVALID;
13.520 + notifier()->next(node)) {
13.521 + nodes.push_back(node);
13.522 + }
13.523 + for (int i = nodes.size() - 1; i >= 0; --i) {
13.524 + snapshot.addNode(nodes[i]);
13.525 + }
13.526 + }
13.527 + virtual void clear() {
13.528 + Node node;
13.529 + for (notifier()->first(node); node != INVALID;
13.530 + notifier()->next(node)) {
13.531 + snapshot.eraseNode(node);
13.532 + }
13.533 + }
13.534 +
13.535 + Snapshot& snapshot;
13.536 + };
13.537 +
13.538 + class ArcObserverProxy : public ArcNotifier::ObserverBase {
13.539 + public:
13.540 +
13.541 + ArcObserverProxy(Snapshot& _snapshot)
13.542 + : snapshot(_snapshot) {}
13.543 +
13.544 + using ArcNotifier::ObserverBase::attach;
13.545 + using ArcNotifier::ObserverBase::detach;
13.546 + using ArcNotifier::ObserverBase::attached;
13.547 +
13.548 + protected:
13.549 +
13.550 + virtual void add(const Arc& arc) {
13.551 + snapshot.addArc(arc);
13.552 + }
13.553 + virtual void add(const std::vector<Arc>& arcs) {
13.554 + for (int i = arcs.size() - 1; i >= 0; ++i) {
13.555 + snapshot.addArc(arcs[i]);
13.556 + }
13.557 + }
13.558 + virtual void erase(const Arc& arc) {
13.559 + snapshot.eraseArc(arc);
13.560 + }
13.561 + virtual void erase(const std::vector<Arc>& arcs) {
13.562 + for (int i = 0; i < int(arcs.size()); ++i) {
13.563 + snapshot.eraseArc(arcs[i]);
13.564 + }
13.565 + }
13.566 + virtual void build() {
13.567 + Arc arc;
13.568 + std::vector<Arc> arcs;
13.569 + for (notifier()->first(arc); arc != INVALID;
13.570 + notifier()->next(arc)) {
13.571 + arcs.push_back(arc);
13.572 + }
13.573 + for (int i = arcs.size() - 1; i >= 0; --i) {
13.574 + snapshot.addArc(arcs[i]);
13.575 + }
13.576 + }
13.577 + virtual void clear() {
13.578 + Arc arc;
13.579 + for (notifier()->first(arc); arc != INVALID;
13.580 + notifier()->next(arc)) {
13.581 + snapshot.eraseArc(arc);
13.582 + }
13.583 + }
13.584 +
13.585 + Snapshot& snapshot;
13.586 + };
13.587 +
13.588 + ListDigraph *digraph;
13.589 +
13.590 + NodeObserverProxy node_observer_proxy;
13.591 + ArcObserverProxy arc_observer_proxy;
13.592 +
13.593 + std::list<Node> added_nodes;
13.594 + std::list<Arc> added_arcs;
13.595 +
13.596 +
13.597 + void addNode(const Node& node) {
13.598 + added_nodes.push_front(node);
13.599 + }
13.600 + void eraseNode(const Node& node) {
13.601 + std::list<Node>::iterator it =
13.602 + std::find(added_nodes.begin(), added_nodes.end(), node);
13.603 + if (it == added_nodes.end()) {
13.604 + clear();
13.605 + arc_observer_proxy.detach();
13.606 + throw NodeNotifier::ImmediateDetach();
13.607 + } else {
13.608 + added_nodes.erase(it);
13.609 + }
13.610 + }
13.611 +
13.612 + void addArc(const Arc& arc) {
13.613 + added_arcs.push_front(arc);
13.614 + }
13.615 + void eraseArc(const Arc& arc) {
13.616 + std::list<Arc>::iterator it =
13.617 + std::find(added_arcs.begin(), added_arcs.end(), arc);
13.618 + if (it == added_arcs.end()) {
13.619 + clear();
13.620 + node_observer_proxy.detach();
13.621 + throw ArcNotifier::ImmediateDetach();
13.622 + } else {
13.623 + added_arcs.erase(it);
13.624 + }
13.625 + }
13.626 +
13.627 + void attach(ListDigraph &_digraph) {
13.628 + digraph = &_digraph;
13.629 + node_observer_proxy.attach(digraph->notifier(Node()));
13.630 + arc_observer_proxy.attach(digraph->notifier(Arc()));
13.631 + }
13.632 +
13.633 + void detach() {
13.634 + node_observer_proxy.detach();
13.635 + arc_observer_proxy.detach();
13.636 + }
13.637 +
13.638 + bool attached() const {
13.639 + return node_observer_proxy.attached();
13.640 + }
13.641 +
13.642 + void clear() {
13.643 + added_nodes.clear();
13.644 + added_arcs.clear();
13.645 + }
13.646 +
13.647 + public:
13.648 +
13.649 + /// \brief Default constructor.
13.650 + ///
13.651 + /// Default constructor.
13.652 + /// To actually make a snapshot you must call save().
13.653 + Snapshot()
13.654 + : digraph(0), node_observer_proxy(*this),
13.655 + arc_observer_proxy(*this) {}
13.656 +
13.657 + /// \brief Constructor that immediately makes a snapshot.
13.658 + ///
13.659 + /// This constructor immediately makes a snapshot of the digraph.
13.660 + /// \param _digraph The digraph we make a snapshot of.
13.661 + Snapshot(ListDigraph &_digraph)
13.662 + : node_observer_proxy(*this),
13.663 + arc_observer_proxy(*this) {
13.664 + attach(_digraph);
13.665 + }
13.666 +
13.667 + /// \brief Make a snapshot.
13.668 + ///
13.669 + /// Make a snapshot of the digraph.
13.670 + ///
13.671 + /// This function can be called more than once. In case of a repeated
13.672 + /// call, the previous snapshot gets lost.
13.673 + /// \param _digraph The digraph we make the snapshot of.
13.674 + void save(ListDigraph &_digraph) {
13.675 + if (attached()) {
13.676 + detach();
13.677 + clear();
13.678 + }
13.679 + attach(_digraph);
13.680 + }
13.681 +
13.682 + /// \brief Undo the changes until the last snapshot.
13.683 + //
13.684 + /// Undo the changes until the last snapshot created by save().
13.685 + void restore() {
13.686 + detach();
13.687 + for(std::list<Arc>::iterator it = added_arcs.begin();
13.688 + it != added_arcs.end(); ++it) {
13.689 + digraph->erase(*it);
13.690 + }
13.691 + for(std::list<Node>::iterator it = added_nodes.begin();
13.692 + it != added_nodes.end(); ++it) {
13.693 + digraph->erase(*it);
13.694 + }
13.695 + clear();
13.696 + }
13.697 +
13.698 + /// \brief Gives back true when the snapshot is valid.
13.699 + ///
13.700 + /// Gives back true when the snapshot is valid.
13.701 + bool valid() const {
13.702 + return attached();
13.703 + }
13.704 + };
13.705 +
13.706 + };
13.707 +
13.708 + ///@}
13.709 +
13.710 + class ListGraphBase {
13.711 +
13.712 + protected:
13.713 +
13.714 + struct NodeT {
13.715 + int first_out;
13.716 + int prev, next;
13.717 + };
13.718 +
13.719 + struct ArcT {
13.720 + int target;
13.721 + int prev_out, next_out;
13.722 + };
13.723 +
13.724 + std::vector<NodeT> nodes;
13.725 +
13.726 + int first_node;
13.727 +
13.728 + int first_free_node;
13.729 +
13.730 + std::vector<ArcT> arcs;
13.731 +
13.732 + int first_free_arc;
13.733 +
13.734 + public:
13.735 +
13.736 + typedef ListGraphBase Digraph;
13.737 +
13.738 + class Node;
13.739 + class Arc;
13.740 + class Edge;
13.741 +
13.742 + class Node {
13.743 + friend class ListGraphBase;
13.744 + protected:
13.745 +
13.746 + int id;
13.747 + explicit Node(int pid) { id = pid;}
13.748 +
13.749 + public:
13.750 + Node() {}
13.751 + Node (Invalid) { id = -1; }
13.752 + bool operator==(const Node& node) const {return id == node.id;}
13.753 + bool operator!=(const Node& node) const {return id != node.id;}
13.754 + bool operator<(const Node& node) const {return id < node.id;}
13.755 + };
13.756 +
13.757 + class Edge {
13.758 + friend class ListGraphBase;
13.759 + protected:
13.760 +
13.761 + int id;
13.762 + explicit Edge(int pid) { id = pid;}
13.763 +
13.764 + public:
13.765 + Edge() {}
13.766 + Edge (Invalid) { id = -1; }
13.767 + bool operator==(const Edge& arc) const {return id == arc.id;}
13.768 + bool operator!=(const Edge& arc) const {return id != arc.id;}
13.769 + bool operator<(const Edge& arc) const {return id < arc.id;}
13.770 + };
13.771 +
13.772 + class Arc {
13.773 + friend class ListGraphBase;
13.774 + protected:
13.775 +
13.776 + int id;
13.777 + explicit Arc(int pid) { id = pid;}
13.778 +
13.779 + public:
13.780 + operator Edge() const { return edgeFromId(id / 2); }
13.781 +
13.782 + Arc() {}
13.783 + Arc (Invalid) { id = -1; }
13.784 + bool operator==(const Arc& arc) const {return id == arc.id;}
13.785 + bool operator!=(const Arc& arc) const {return id != arc.id;}
13.786 + bool operator<(const Arc& arc) const {return id < arc.id;}
13.787 + };
13.788 +
13.789 +
13.790 +
13.791 + ListGraphBase()
13.792 + : nodes(), first_node(-1),
13.793 + first_free_node(-1), arcs(), first_free_arc(-1) {}
13.794 +
13.795 +
13.796 + int maxNodeId() const { return nodes.size()-1; }
13.797 + int maxEdgeId() const { return arcs.size() / 2 - 1; }
13.798 + int maxArcId() const { return arcs.size()-1; }
13.799 +
13.800 + Node source(Arc e) const { return Node(arcs[e.id ^ 1].target); }
13.801 + Node target(Arc e) const { return Node(arcs[e.id].target); }
13.802 +
13.803 + Node u(Edge e) const { return Node(arcs[2 * e.id].target); }
13.804 + Node v(Edge e) const { return Node(arcs[2 * e.id + 1].target); }
13.805 +
13.806 + static bool direction(Arc e) {
13.807 + return (e.id & 1) == 1;
13.808 + }
13.809 +
13.810 + static Arc direct(Edge e, bool d) {
13.811 + return Arc(e.id * 2 + (d ? 1 : 0));
13.812 + }
13.813 +
13.814 + void first(Node& node) const {
13.815 + node.id = first_node;
13.816 + }
13.817 +
13.818 + void next(Node& node) const {
13.819 + node.id = nodes[node.id].next;
13.820 + }
13.821 +
13.822 + void first(Arc& e) const {
13.823 + int n = first_node;
13.824 + while (n != -1 && nodes[n].first_out == -1) {
13.825 + n = nodes[n].next;
13.826 + }
13.827 + e.id = (n == -1) ? -1 : nodes[n].first_out;
13.828 + }
13.829 +
13.830 + void next(Arc& e) const {
13.831 + if (arcs[e.id].next_out != -1) {
13.832 + e.id = arcs[e.id].next_out;
13.833 + } else {
13.834 + int n = nodes[arcs[e.id ^ 1].target].next;
13.835 + while(n != -1 && nodes[n].first_out == -1) {
13.836 + n = nodes[n].next;
13.837 + }
13.838 + e.id = (n == -1) ? -1 : nodes[n].first_out;
13.839 + }
13.840 + }
13.841 +
13.842 + void first(Edge& e) const {
13.843 + int n = first_node;
13.844 + while (n != -1) {
13.845 + e.id = nodes[n].first_out;
13.846 + while ((e.id & 1) != 1) {
13.847 + e.id = arcs[e.id].next_out;
13.848 + }
13.849 + if (e.id != -1) {
13.850 + e.id /= 2;
13.851 + return;
13.852 + }
13.853 + n = nodes[n].next;
13.854 + }
13.855 + e.id = -1;
13.856 + }
13.857 +
13.858 + void next(Edge& e) const {
13.859 + int n = arcs[e.id * 2].target;
13.860 + e.id = arcs[(e.id * 2) | 1].next_out;
13.861 + while ((e.id & 1) != 1) {
13.862 + e.id = arcs[e.id].next_out;
13.863 + }
13.864 + if (e.id != -1) {
13.865 + e.id /= 2;
13.866 + return;
13.867 + }
13.868 + n = nodes[n].next;
13.869 + while (n != -1) {
13.870 + e.id = nodes[n].first_out;
13.871 + while ((e.id & 1) != 1) {
13.872 + e.id = arcs[e.id].next_out;
13.873 + }
13.874 + if (e.id != -1) {
13.875 + e.id /= 2;
13.876 + return;
13.877 + }
13.878 + n = nodes[n].next;
13.879 + }
13.880 + e.id = -1;
13.881 + }
13.882 +
13.883 + void firstOut(Arc &e, const Node& v) const {
13.884 + e.id = nodes[v.id].first_out;
13.885 + }
13.886 + void nextOut(Arc &e) const {
13.887 + e.id = arcs[e.id].next_out;
13.888 + }
13.889 +
13.890 + void firstIn(Arc &e, const Node& v) const {
13.891 + e.id = ((nodes[v.id].first_out) ^ 1);
13.892 + if (e.id == -2) e.id = -1;
13.893 + }
13.894 + void nextIn(Arc &e) const {
13.895 + e.id = ((arcs[e.id ^ 1].next_out) ^ 1);
13.896 + if (e.id == -2) e.id = -1;
13.897 + }
13.898 +
13.899 + void firstInc(Edge &e, bool& d, const Node& v) const {
13.900 + int de = nodes[v.id].first_out;
13.901 + if (de != -1 ) {
13.902 + e.id = de / 2;
13.903 + d = ((de & 1) == 1);
13.904 + } else {
13.905 + e.id = -1;
13.906 + d = true;
13.907 + }
13.908 + }
13.909 + void nextInc(Edge &e, bool& d) const {
13.910 + int de = (arcs[(e.id * 2) | (d ? 1 : 0)].next_out);
13.911 + if (de != -1 ) {
13.912 + e.id = de / 2;
13.913 + d = ((de & 1) == 1);
13.914 + } else {
13.915 + e.id = -1;
13.916 + d = true;
13.917 + }
13.918 + }
13.919 +
13.920 + static int id(Node v) { return v.id; }
13.921 + static int id(Arc e) { return e.id; }
13.922 + static int id(Edge e) { return e.id; }
13.923 +
13.924 + static Node nodeFromId(int id) { return Node(id);}
13.925 + static Arc arcFromId(int id) { return Arc(id);}
13.926 + static Edge edgeFromId(int id) { return Edge(id);}
13.927 +
13.928 + Node addNode() {
13.929 + int n;
13.930 +
13.931 + if(first_free_node==-1) {
13.932 + n = nodes.size();
13.933 + nodes.push_back(NodeT());
13.934 + } else {
13.935 + n = first_free_node;
13.936 + first_free_node = nodes[n].next;
13.937 + }
13.938 +
13.939 + nodes[n].next = first_node;
13.940 + if (first_node != -1) nodes[first_node].prev = n;
13.941 + first_node = n;
13.942 + nodes[n].prev = -1;
13.943 +
13.944 + nodes[n].first_out = -1;
13.945 +
13.946 + return Node(n);
13.947 + }
13.948 +
13.949 + Edge addEdge(Node u, Node v) {
13.950 + int n;
13.951 +
13.952 + if (first_free_arc == -1) {
13.953 + n = arcs.size();
13.954 + arcs.push_back(ArcT());
13.955 + arcs.push_back(ArcT());
13.956 + } else {
13.957 + n = first_free_arc;
13.958 + first_free_arc = arcs[n].next_out;
13.959 + }
13.960 +
13.961 + arcs[n].target = u.id;
13.962 + arcs[n | 1].target = v.id;
13.963 +
13.964 + arcs[n].next_out = nodes[v.id].first_out;
13.965 + if (nodes[v.id].first_out != -1) {
13.966 + arcs[nodes[v.id].first_out].prev_out = n;
13.967 + }
13.968 + arcs[n].prev_out = -1;
13.969 + nodes[v.id].first_out = n;
13.970 +
13.971 + arcs[n | 1].next_out = nodes[u.id].first_out;
13.972 + if (nodes[u.id].first_out != -1) {
13.973 + arcs[nodes[u.id].first_out].prev_out = (n | 1);
13.974 + }
13.975 + arcs[n | 1].prev_out = -1;
13.976 + nodes[u.id].first_out = (n | 1);
13.977 +
13.978 + return Edge(n / 2);
13.979 + }
13.980 +
13.981 + void erase(const Node& node) {
13.982 + int n = node.id;
13.983 +
13.984 + if(nodes[n].next != -1) {
13.985 + nodes[nodes[n].next].prev = nodes[n].prev;
13.986 + }
13.987 +
13.988 + if(nodes[n].prev != -1) {
13.989 + nodes[nodes[n].prev].next = nodes[n].next;
13.990 + } else {
13.991 + first_node = nodes[n].next;
13.992 + }
13.993 +
13.994 + nodes[n].next = first_free_node;
13.995 + first_free_node = n;
13.996 +
13.997 + }
13.998 +
13.999 + void erase(const Edge& arc) {
13.1000 + int n = arc.id * 2;
13.1001 +
13.1002 + if (arcs[n].next_out != -1) {
13.1003 + arcs[arcs[n].next_out].prev_out = arcs[n].prev_out;
13.1004 + }
13.1005 +
13.1006 + if (arcs[n].prev_out != -1) {
13.1007 + arcs[arcs[n].prev_out].next_out = arcs[n].next_out;
13.1008 + } else {
13.1009 + nodes[arcs[n | 1].target].first_out = arcs[n].next_out;
13.1010 + }
13.1011 +
13.1012 + if (arcs[n | 1].next_out != -1) {
13.1013 + arcs[arcs[n | 1].next_out].prev_out = arcs[n | 1].prev_out;
13.1014 + }
13.1015 +
13.1016 + if (arcs[n | 1].prev_out != -1) {
13.1017 + arcs[arcs[n | 1].prev_out].next_out = arcs[n | 1].next_out;
13.1018 + } else {
13.1019 + nodes[arcs[n].target].first_out = arcs[n | 1].next_out;
13.1020 + }
13.1021 +
13.1022 + arcs[n].next_out = first_free_arc;
13.1023 + first_free_arc = n;
13.1024 +
13.1025 + }
13.1026 +
13.1027 + void clear() {
13.1028 + arcs.clear();
13.1029 + nodes.clear();
13.1030 + first_node = first_free_node = first_free_arc = -1;
13.1031 + }
13.1032 +
13.1033 + protected:
13.1034 +
13.1035 + void changeTarget(Edge e, Node n) {
13.1036 + if(arcs[2 * e.id].next_out != -1) {
13.1037 + arcs[arcs[2 * e.id].next_out].prev_out = arcs[2 * e.id].prev_out;
13.1038 + }
13.1039 + if(arcs[2 * e.id].prev_out != -1) {
13.1040 + arcs[arcs[2 * e.id].prev_out].next_out =
13.1041 + arcs[2 * e.id].next_out;
13.1042 + } else {
13.1043 + nodes[arcs[(2 * e.id) | 1].target].first_out =
13.1044 + arcs[2 * e.id].next_out;
13.1045 + }
13.1046 +
13.1047 + if (nodes[n.id].first_out != -1) {
13.1048 + arcs[nodes[n.id].first_out].prev_out = 2 * e.id;
13.1049 + }
13.1050 + arcs[(2 * e.id) | 1].target = n.id;
13.1051 + arcs[2 * e.id].prev_out = -1;
13.1052 + arcs[2 * e.id].next_out = nodes[n.id].first_out;
13.1053 + nodes[n.id].first_out = 2 * e.id;
13.1054 + }
13.1055 +
13.1056 + void changeSource(Edge e, Node n) {
13.1057 + if(arcs[(2 * e.id) | 1].next_out != -1) {
13.1058 + arcs[arcs[(2 * e.id) | 1].next_out].prev_out =
13.1059 + arcs[(2 * e.id) | 1].prev_out;
13.1060 + }
13.1061 + if(arcs[(2 * e.id) | 1].prev_out != -1) {
13.1062 + arcs[arcs[(2 * e.id) | 1].prev_out].next_out =
13.1063 + arcs[(2 * e.id) | 1].next_out;
13.1064 + } else {
13.1065 + nodes[arcs[2 * e.id].target].first_out =
13.1066 + arcs[(2 * e.id) | 1].next_out;
13.1067 + }
13.1068 +
13.1069 + if (nodes[n.id].first_out != -1) {
13.1070 + arcs[nodes[n.id].first_out].prev_out = ((2 * e.id) | 1);
13.1071 + }
13.1072 + arcs[2 * e.id].target = n.id;
13.1073 + arcs[(2 * e.id) | 1].prev_out = -1;
13.1074 + arcs[(2 * e.id) | 1].next_out = nodes[n.id].first_out;
13.1075 + nodes[n.id].first_out = ((2 * e.id) | 1);
13.1076 + }
13.1077 +
13.1078 + };
13.1079 +
13.1080 +// typedef GraphExtender<UndirDigraphExtender<ListDigraphBase> >
13.1081 +// ExtendedListGraphBase;
13.1082 +
13.1083 + typedef GraphExtender<ListGraphBase> ExtendedListGraphBase;
13.1084 +
13.1085 +
13.1086 +
13.1087 + /// \addtogroup digraphs
13.1088 + /// @{
13.1089 +
13.1090 + ///An undirected list digraph class.
13.1091 +
13.1092 + ///This is a simple and fast undirected digraph implementation.
13.1093 + ///
13.1094 + ///An important extra feature of this digraph implementation is that
13.1095 + ///its maps are real \ref concepts::ReferenceMap "reference map"s.
13.1096 + ///
13.1097 + ///It conforms to the
13.1098 + ///\ref concepts::Graph "Graph concept".
13.1099 + ///
13.1100 + ///\sa concepts::Graph.
13.1101 + ///
13.1102 + class ListGraph : public ExtendedListGraphBase {
13.1103 + private:
13.1104 + ///ListGraph is \e not copy constructible. Use GraphCopy() instead.
13.1105 +
13.1106 + ///ListGraph is \e not copy constructible. Use GraphCopy() instead.
13.1107 + ///
13.1108 + ListGraph(const ListGraph &) :ExtendedListGraphBase() {};
13.1109 + ///\brief Assignment of ListGraph to another one is \e not allowed.
13.1110 + ///Use GraphCopy() instead.
13.1111 +
13.1112 + ///Assignment of ListGraph to another one is \e not allowed.
13.1113 + ///Use GraphCopy() instead.
13.1114 + void operator=(const ListGraph &) {}
13.1115 + public:
13.1116 + /// Constructor
13.1117 +
13.1118 + /// Constructor.
13.1119 + ///
13.1120 + ListGraph() {}
13.1121 +
13.1122 + typedef ExtendedListGraphBase Parent;
13.1123 +
13.1124 + typedef Parent::OutArcIt IncArcIt;
13.1125 +
13.1126 + /// \brief Add a new node to the digraph.
13.1127 + ///
13.1128 + /// \return the new node.
13.1129 + ///
13.1130 + Node addNode() { return Parent::addNode(); }
13.1131 +
13.1132 + /// \brief Add a new edge to the digraph.
13.1133 + ///
13.1134 + /// Add a new arc to the digraph with source node \c s
13.1135 + /// and target node \c t.
13.1136 + /// \return the new edge.
13.1137 + Edge addEdge(const Node& s, const Node& t) {
13.1138 + return Parent::addEdge(s, t);
13.1139 + }
13.1140 + /// \brief Changes the source of \c e to \c n
13.1141 + ///
13.1142 + /// Changes the source of \c e to \c n
13.1143 + ///
13.1144 + ///\note The <tt>ArcIt</tt>s and <tt>InArcIt</tt>s
13.1145 + ///referencing the changed arc remain
13.1146 + ///valid. However <tt>OutArcIt</tt>s are invalidated.
13.1147 + void changeSource(Edge e, Node n) {
13.1148 + Parent::changeSource(e,n);
13.1149 + }
13.1150 + /// \brief Changes the target of \c e to \c n
13.1151 + ///
13.1152 + /// Changes the target of \c e to \c n
13.1153 + ///
13.1154 + /// \note The <tt>ArcIt</tt>s referencing the changed arc remain
13.1155 + /// valid. However the other iterators may be invalidated.
13.1156 + void changeTarget(Edge e, Node n) {
13.1157 + Parent::changeTarget(e,n);
13.1158 + }
13.1159 + /// \brief Changes the source of \c e to \c n
13.1160 + ///
13.1161 + /// Changes the source of \c e to \c n. It changes the proper
13.1162 + /// node of the represented edge.
13.1163 + ///
13.1164 + ///\note The <tt>ArcIt</tt>s and <tt>InArcIt</tt>s
13.1165 + ///referencing the changed arc remain
13.1166 + ///valid. However <tt>OutArcIt</tt>s are invalidated.
13.1167 + void changeSource(Arc e, Node n) {
13.1168 + if (Parent::direction(e)) {
13.1169 + Parent::changeSource(e,n);
13.1170 + } else {
13.1171 + Parent::changeTarget(e,n);
13.1172 + }
13.1173 + }
13.1174 + /// \brief Changes the target of \c e to \c n
13.1175 + ///
13.1176 + /// Changes the target of \c e to \c n. It changes the proper
13.1177 + /// node of the represented edge.
13.1178 + ///
13.1179 + ///\note The <tt>ArcIt</tt>s and <tt>OutArcIt</tt>s
13.1180 + ///referencing the changed arc remain
13.1181 + ///valid. However <tt>InArcIt</tt>s are invalidated.
13.1182 + void changeTarget(Arc e, Node n) {
13.1183 + if (Parent::direction(e)) {
13.1184 + Parent::changeTarget(e,n);
13.1185 + } else {
13.1186 + Parent::changeSource(e,n);
13.1187 + }
13.1188 + }
13.1189 + /// \brief Contract two nodes.
13.1190 + ///
13.1191 + /// This function contracts two nodes.
13.1192 + ///
13.1193 + /// Node \p b will be removed but instead of deleting
13.1194 + /// its neighboring arcs, they will be joined to \p a.
13.1195 + /// The last parameter \p r controls whether to remove loops. \c true
13.1196 + /// means that loops will be removed.
13.1197 + ///
13.1198 + /// \note The <tt>ArcIt</tt>s referencing a moved arc remain
13.1199 + /// valid.
13.1200 + void contract(Node a, Node b, bool r = true) {
13.1201 + for(IncArcIt e(*this, b); e!=INVALID;) {
13.1202 + IncArcIt f = e; ++f;
13.1203 + if (r && runningNode(e) == a) {
13.1204 + erase(e);
13.1205 + } else if (source(e) == b) {
13.1206 + changeSource(e, a);
13.1207 + } else {
13.1208 + changeTarget(e, a);
13.1209 + }
13.1210 + e = f;
13.1211 + }
13.1212 + erase(b);
13.1213 + }
13.1214 +
13.1215 +
13.1216 + /// \brief Class to make a snapshot of the digraph and restore
13.1217 + /// to it later.
13.1218 + ///
13.1219 + /// Class to make a snapshot of the digraph and to restore it
13.1220 + /// later.
13.1221 + ///
13.1222 + /// The newly added nodes and edges can be removed
13.1223 + /// using the restore() function.
13.1224 + ///
13.1225 + /// \warning Arc and node deletions cannot be restored. This
13.1226 + /// events invalidate the snapshot.
13.1227 + class Snapshot {
13.1228 + protected:
13.1229 +
13.1230 + typedef Parent::NodeNotifier NodeNotifier;
13.1231 +
13.1232 + class NodeObserverProxy : public NodeNotifier::ObserverBase {
13.1233 + public:
13.1234 +
13.1235 + NodeObserverProxy(Snapshot& _snapshot)
13.1236 + : snapshot(_snapshot) {}
13.1237 +
13.1238 + using NodeNotifier::ObserverBase::attach;
13.1239 + using NodeNotifier::ObserverBase::detach;
13.1240 + using NodeNotifier::ObserverBase::attached;
13.1241 +
13.1242 + protected:
13.1243 +
13.1244 + virtual void add(const Node& node) {
13.1245 + snapshot.addNode(node);
13.1246 + }
13.1247 + virtual void add(const std::vector<Node>& nodes) {
13.1248 + for (int i = nodes.size() - 1; i >= 0; ++i) {
13.1249 + snapshot.addNode(nodes[i]);
13.1250 + }
13.1251 + }
13.1252 + virtual void erase(const Node& node) {
13.1253 + snapshot.eraseNode(node);
13.1254 + }
13.1255 + virtual void erase(const std::vector<Node>& nodes) {
13.1256 + for (int i = 0; i < int(nodes.size()); ++i) {
13.1257 + snapshot.eraseNode(nodes[i]);
13.1258 + }
13.1259 + }
13.1260 + virtual void build() {
13.1261 + Node node;
13.1262 + std::vector<Node> nodes;
13.1263 + for (notifier()->first(node); node != INVALID;
13.1264 + notifier()->next(node)) {
13.1265 + nodes.push_back(node);
13.1266 + }
13.1267 + for (int i = nodes.size() - 1; i >= 0; --i) {
13.1268 + snapshot.addNode(nodes[i]);
13.1269 + }
13.1270 + }
13.1271 + virtual void clear() {
13.1272 + Node node;
13.1273 + for (notifier()->first(node); node != INVALID;
13.1274 + notifier()->next(node)) {
13.1275 + snapshot.eraseNode(node);
13.1276 + }
13.1277 + }
13.1278 +
13.1279 + Snapshot& snapshot;
13.1280 + };
13.1281 +
13.1282 + class EdgeObserverProxy : public EdgeNotifier::ObserverBase {
13.1283 + public:
13.1284 +
13.1285 + EdgeObserverProxy(Snapshot& _snapshot)
13.1286 + : snapshot(_snapshot) {}
13.1287 +
13.1288 + using EdgeNotifier::ObserverBase::attach;
13.1289 + using EdgeNotifier::ObserverBase::detach;
13.1290 + using EdgeNotifier::ObserverBase::attached;
13.1291 +
13.1292 + protected:
13.1293 +
13.1294 + virtual void add(const Edge& arc) {
13.1295 + snapshot.addEdge(arc);
13.1296 + }
13.1297 + virtual void add(const std::vector<Edge>& arcs) {
13.1298 + for (int i = arcs.size() - 1; i >= 0; ++i) {
13.1299 + snapshot.addEdge(arcs[i]);
13.1300 + }
13.1301 + }
13.1302 + virtual void erase(const Edge& arc) {
13.1303 + snapshot.eraseEdge(arc);
13.1304 + }
13.1305 + virtual void erase(const std::vector<Edge>& arcs) {
13.1306 + for (int i = 0; i < int(arcs.size()); ++i) {
13.1307 + snapshot.eraseEdge(arcs[i]);
13.1308 + }
13.1309 + }
13.1310 + virtual void build() {
13.1311 + Edge arc;
13.1312 + std::vector<Edge> arcs;
13.1313 + for (notifier()->first(arc); arc != INVALID;
13.1314 + notifier()->next(arc)) {
13.1315 + arcs.push_back(arc);
13.1316 + }
13.1317 + for (int i = arcs.size() - 1; i >= 0; --i) {
13.1318 + snapshot.addEdge(arcs[i]);
13.1319 + }
13.1320 + }
13.1321 + virtual void clear() {
13.1322 + Edge arc;
13.1323 + for (notifier()->first(arc); arc != INVALID;
13.1324 + notifier()->next(arc)) {
13.1325 + snapshot.eraseEdge(arc);
13.1326 + }
13.1327 + }
13.1328 +
13.1329 + Snapshot& snapshot;
13.1330 + };
13.1331 +
13.1332 + ListGraph *digraph;
13.1333 +
13.1334 + NodeObserverProxy node_observer_proxy;
13.1335 + EdgeObserverProxy arc_observer_proxy;
13.1336 +
13.1337 + std::list<Node> added_nodes;
13.1338 + std::list<Edge> added_arcs;
13.1339 +
13.1340 +
13.1341 + void addNode(const Node& node) {
13.1342 + added_nodes.push_front(node);
13.1343 + }
13.1344 + void eraseNode(const Node& node) {
13.1345 + std::list<Node>::iterator it =
13.1346 + std::find(added_nodes.begin(), added_nodes.end(), node);
13.1347 + if (it == added_nodes.end()) {
13.1348 + clear();
13.1349 + arc_observer_proxy.detach();
13.1350 + throw NodeNotifier::ImmediateDetach();
13.1351 + } else {
13.1352 + added_nodes.erase(it);
13.1353 + }
13.1354 + }
13.1355 +
13.1356 + void addEdge(const Edge& arc) {
13.1357 + added_arcs.push_front(arc);
13.1358 + }
13.1359 + void eraseEdge(const Edge& arc) {
13.1360 + std::list<Edge>::iterator it =
13.1361 + std::find(added_arcs.begin(), added_arcs.end(), arc);
13.1362 + if (it == added_arcs.end()) {
13.1363 + clear();
13.1364 + node_observer_proxy.detach();
13.1365 + throw EdgeNotifier::ImmediateDetach();
13.1366 + } else {
13.1367 + added_arcs.erase(it);
13.1368 + }
13.1369 + }
13.1370 +
13.1371 + void attach(ListGraph &_digraph) {
13.1372 + digraph = &_digraph;
13.1373 + node_observer_proxy.attach(digraph->notifier(Node()));
13.1374 + arc_observer_proxy.attach(digraph->notifier(Edge()));
13.1375 + }
13.1376 +
13.1377 + void detach() {
13.1378 + node_observer_proxy.detach();
13.1379 + arc_observer_proxy.detach();
13.1380 + }
13.1381 +
13.1382 + bool attached() const {
13.1383 + return node_observer_proxy.attached();
13.1384 + }
13.1385 +
13.1386 + void clear() {
13.1387 + added_nodes.clear();
13.1388 + added_arcs.clear();
13.1389 + }
13.1390 +
13.1391 + public:
13.1392 +
13.1393 + /// \brief Default constructor.
13.1394 + ///
13.1395 + /// Default constructor.
13.1396 + /// To actually make a snapshot you must call save().
13.1397 + Snapshot()
13.1398 + : digraph(0), node_observer_proxy(*this),
13.1399 + arc_observer_proxy(*this) {}
13.1400 +
13.1401 + /// \brief Constructor that immediately makes a snapshot.
13.1402 + ///
13.1403 + /// This constructor immediately makes a snapshot of the digraph.
13.1404 + /// \param _digraph The digraph we make a snapshot of.
13.1405 + Snapshot(ListGraph &_digraph)
13.1406 + : node_observer_proxy(*this),
13.1407 + arc_observer_proxy(*this) {
13.1408 + attach(_digraph);
13.1409 + }
13.1410 +
13.1411 + /// \brief Make a snapshot.
13.1412 + ///
13.1413 + /// Make a snapshot of the digraph.
13.1414 + ///
13.1415 + /// This function can be called more than once. In case of a repeated
13.1416 + /// call, the previous snapshot gets lost.
13.1417 + /// \param _digraph The digraph we make the snapshot of.
13.1418 + void save(ListGraph &_digraph) {
13.1419 + if (attached()) {
13.1420 + detach();
13.1421 + clear();
13.1422 + }
13.1423 + attach(_digraph);
13.1424 + }
13.1425 +
13.1426 + /// \brief Undo the changes until the last snapshot.
13.1427 + //
13.1428 + /// Undo the changes until the last snapshot created by save().
13.1429 + void restore() {
13.1430 + detach();
13.1431 + for(std::list<Edge>::iterator it = added_arcs.begin();
13.1432 + it != added_arcs.end(); ++it) {
13.1433 + digraph->erase(*it);
13.1434 + }
13.1435 + for(std::list<Node>::iterator it = added_nodes.begin();
13.1436 + it != added_nodes.end(); ++it) {
13.1437 + digraph->erase(*it);
13.1438 + }
13.1439 + clear();
13.1440 + }
13.1441 +
13.1442 + /// \brief Gives back true when the snapshot is valid.
13.1443 + ///
13.1444 + /// Gives back true when the snapshot is valid.
13.1445 + bool valid() const {
13.1446 + return attached();
13.1447 + }
13.1448 + };
13.1449 + };
13.1450 +
13.1451 + /// @}
13.1452 +} //namespace lemon
13.1453 +
13.1454 +
13.1455 +#endif
14.1 --- a/test/Makefile.am Sat Jan 12 23:30:44 2008 +0000
14.2 +++ b/test/Makefile.am Sun Jan 20 20:43:48 2008 +0100
14.3 @@ -5,7 +5,9 @@
14.4 test/test_tools.h
14.5
14.6 check_PROGRAMS += \
14.7 + test/digraph_test \
14.8 test/dim_test \
14.9 + test/graph_test \
14.10 test/random_test \
14.11 test/test_tools_fail \
14.12 test/test_tools_pass
14.13 @@ -13,7 +15,9 @@
14.14 TESTS += $(check_PROGRAMS)
14.15 XFAIL_TESTS += test/test_tools_fail$(EXEEXT)
14.16
14.17 +test_digraph_test_SOURCES = test/digraph_test.cc
14.18 test_dim_test_SOURCES = test/dim_test.cc
14.19 +test_graph_test_SOURCES = test/graph_test.cc
14.20 test_random_test_SOURCES = test/random_test.cc
14.21 test_test_tools_fail_SOURCES = test/test_tools_fail.cc
14.22 test_test_tools_pass_SOURCES = test/test_tools_pass.cc
15.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
15.2 +++ b/test/digraph_test.cc Sun Jan 20 20:43:48 2008 +0100
15.3 @@ -0,0 +1,82 @@
15.4 +/* -*- C++ -*-
15.5 + *
15.6 + * This file is a part of LEMON, a generic C++ optimization library
15.7 + *
15.8 + * Copyright (C) 2003-2007
15.9 + * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
15.10 + * (Egervary Research Group on Combinatorial Optimization, EGRES).
15.11 + *
15.12 + * Permission to use, modify and distribute this software is granted
15.13 + * provided that this copyright notice appears in all copies. For
15.14 + * precise terms see the accompanying LICENSE file.
15.15 + *
15.16 + * This software is provided "AS IS" with no warranty of any kind,
15.17 + * express or implied, and with no claim as to its suitability for any
15.18 + * purpose.
15.19 + *
15.20 + */
15.21 +
15.22 +#include <iostream>
15.23 +#include <vector>
15.24 +
15.25 +#include <lemon/concepts/digraph.h>
15.26 +#include <lemon/list_graph.h>
15.27 +//#include <lemon/smart_graph.h>
15.28 +//#include <lemon/full_graph.h>
15.29 +//#include <lemon/hypercube_graph.h>
15.30 +
15.31 +#include "test_tools.h"
15.32 +#include "digraph_test.h"
15.33 +#include "map_test.h"
15.34 +
15.35 +
15.36 +using namespace lemon;
15.37 +using namespace lemon::concepts;
15.38 +
15.39 +
15.40 +int main() {
15.41 + { // checking digraph components
15.42 + checkConcept<BaseDigraphComponent, BaseDigraphComponent >();
15.43 +
15.44 + checkConcept<IDableDigraphComponent<>,
15.45 + IDableDigraphComponent<> >();
15.46 +
15.47 + checkConcept<IterableDigraphComponent<>,
15.48 + IterableDigraphComponent<> >();
15.49 +
15.50 + checkConcept<MappableDigraphComponent<>,
15.51 + MappableDigraphComponent<> >();
15.52 +
15.53 + }
15.54 + { // checking skeleton digraphs
15.55 + checkConcept<Digraph, Digraph>();
15.56 + }
15.57 + { // checking list digraph
15.58 + checkConcept<Digraph, ListDigraph >();
15.59 + checkConcept<AlterableDigraphComponent<>, ListDigraph>();
15.60 + checkConcept<ExtendableDigraphComponent<>, ListDigraph>();
15.61 + checkConcept<ClearableDigraphComponent<>, ListDigraph>();
15.62 + checkConcept<ErasableDigraphComponent<>, ListDigraph>();
15.63 +
15.64 + checkDigraph<ListDigraph>();
15.65 + checkGraphNodeMap<ListDigraph>();
15.66 + checkGraphArcMap<ListDigraph>();
15.67 + }
15.68 +// { // checking smart digraph
15.69 +// checkConcept<Digraph, SmartDigraph >();
15.70 +
15.71 +// checkDigraph<SmartDigraph>();
15.72 +// checkDigraphNodeMap<SmartDigraph>();
15.73 +// checkDigraphArcMap<SmartDigraph>();
15.74 +// }
15.75 +// { // checking full digraph
15.76 +// checkConcept<Digraph, FullDigraph >();
15.77 +// }
15.78 +// { // checking full digraph
15.79 +// checkConcept<Digraph, HyperCubeDigraph >();
15.80 +// }
15.81 +
15.82 + std::cout << __FILE__ ": All tests passed.\n";
15.83 +
15.84 + return 0;
15.85 +}
16.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
16.2 +++ b/test/digraph_test.h Sun Jan 20 20:43:48 2008 +0100
16.3 @@ -0,0 +1,188 @@
16.4 +/* -*- C++ -*-
16.5 + *
16.6 + * This file is a part of LEMON, a generic C++ optimization library
16.7 + *
16.8 + * Copyright (C) 2003-2007
16.9 + * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
16.10 + * (Egervary Research Group on Combinatorial Optimization, EGRES).
16.11 + *
16.12 + * Permission to use, modify and distribute this software is granted
16.13 + * provided that this copyright notice appears in all copies. For
16.14 + * precise terms see the accompanying LICENSE file.
16.15 + *
16.16 + * This software is provided "AS IS" with no warranty of any kind,
16.17 + * express or implied, and with no claim as to its suitability for any
16.18 + * purpose.
16.19 + *
16.20 + */
16.21 +
16.22 +#ifndef LEMON_TEST_GRAPH_TEST_H
16.23 +#define LEMON_TEST_GRAPH_TEST_H
16.24 +
16.25 +//#include <lemon/graph_utils.h>
16.26 +#include "test_tools.h"
16.27 +
16.28 +//! \ingroup misc
16.29 +//! \file
16.30 +//! \brief Some utility and test cases to test digraph classes.
16.31 +namespace lemon {
16.32 +
16.33 + ///Structure returned by \ref addPetersen().
16.34 +
16.35 + ///Structure returned by \ref addPetersen().
16.36 + ///
16.37 + template<class Digraph>
16.38 + struct PetStruct
16.39 + {
16.40 + ///Vector containing the outer nodes.
16.41 + std::vector<typename Digraph::Node> outer;
16.42 + ///Vector containing the inner nodes.
16.43 + std::vector<typename Digraph::Node> inner;
16.44 + ///Vector containing the edges of the inner circle.
16.45 + std::vector<typename Digraph::Arc> incir;
16.46 + ///Vector containing the edges of the outer circle.
16.47 + std::vector<typename Digraph::Arc> outcir;
16.48 + ///Vector containing the chord edges.
16.49 + std::vector<typename Digraph::Arc> chords;
16.50 + };
16.51 +
16.52 +
16.53 +
16.54 + ///Adds a Petersen graph to \c G.
16.55 +
16.56 + ///Adds a Petersen graph to \c G.
16.57 + ///\return The nodes and edges of the generated graph.
16.58 +
16.59 + template<typename Digraph>
16.60 + PetStruct<Digraph> addPetersen(Digraph &G,int num = 5)
16.61 + {
16.62 + PetStruct<Digraph> n;
16.63 +
16.64 + for(int i=0;i<num;i++) {
16.65 + n.outer.push_back(G.addNode());
16.66 + n.inner.push_back(G.addNode());
16.67 + }
16.68 +
16.69 + for(int i=0;i<num;i++) {
16.70 + n.chords.push_back(G.addArc(n.outer[i],n.inner[i]));
16.71 + n.outcir.push_back(G.addArc(n.outer[i],n.outer[(i+1) % num]));
16.72 + n.incir.push_back(G.addArc(n.inner[i],n.inner[(i+2) % num]));
16.73 + }
16.74 + return n;
16.75 + }
16.76 +
16.77 + /// \brief Adds to the digraph the reverse pair of all edges.
16.78 + ///
16.79 + /// Adds to the digraph the reverse pair of all edges.
16.80 + ///
16.81 + template<class Digraph>
16.82 + void bidirDigraph(Digraph &G)
16.83 + {
16.84 + typedef typename Digraph::Arc Arc;
16.85 + typedef typename Digraph::ArcIt ArcIt;
16.86 +
16.87 + std::vector<Arc> ee;
16.88 +
16.89 + for(ArcIt e(G);e!=INVALID;++e) ee.push_back(e);
16.90 +
16.91 + for(typename std::vector<Arc>::iterator p=ee.begin();p!=ee.end();p++)
16.92 + G.addArc(G.target(*p),G.source(*p));
16.93 + }
16.94 +
16.95 +
16.96 + /// \brief Checks the bidirectioned Petersen graph.
16.97 + ///
16.98 + /// Checks the bidirectioned Petersen graph.
16.99 + ///
16.100 + template<class Digraph>
16.101 + void checkBidirPetersen(Digraph &G, int num = 5)
16.102 + {
16.103 + typedef typename Digraph::Node Node;
16.104 +
16.105 + typedef typename Digraph::ArcIt ArcIt;
16.106 + typedef typename Digraph::NodeIt NodeIt;
16.107 +
16.108 + checkDigraphNodeList(G, 2 * num);
16.109 + checkDigraphArcList(G, 6 * num);
16.110 +
16.111 + for(NodeIt n(G);n!=INVALID;++n) {
16.112 + checkDigraphInArcList(G, n, 3);
16.113 + checkDigraphOutArcList(G, n, 3);
16.114 + }
16.115 + }
16.116 +
16.117 + template<class Digraph> void checkDigraphNodeList(Digraph &G, int nn)
16.118 + {
16.119 + typename Digraph::NodeIt n(G);
16.120 + for(int i=0;i<nn;i++) {
16.121 + check(n!=INVALID,"Wrong Node list linking.");
16.122 + ++n;
16.123 + }
16.124 + check(n==INVALID,"Wrong Node list linking.");
16.125 + }
16.126 +
16.127 + template<class Digraph>
16.128 + void checkDigraphArcList(Digraph &G, int nn)
16.129 + {
16.130 + typedef typename Digraph::ArcIt ArcIt;
16.131 +
16.132 + ArcIt e(G);
16.133 + for(int i=0;i<nn;i++) {
16.134 + check(e!=INVALID,"Wrong Arc list linking.");
16.135 + ++e;
16.136 + }
16.137 + check(e==INVALID,"Wrong Arc list linking.");
16.138 + }
16.139 +
16.140 + template<class Digraph>
16.141 + void checkDigraphOutArcList(Digraph &G, typename Digraph::Node n, int nn)
16.142 + {
16.143 + typename Digraph::OutArcIt e(G,n);
16.144 + for(int i=0;i<nn;i++) {
16.145 + check(e!=INVALID,"Wrong OutArc list linking.");
16.146 + check(n==G.source(e), "Wrong OutArc list linking.");
16.147 + ++e;
16.148 + }
16.149 + check(e==INVALID,"Wrong OutArc list linking.");
16.150 + }
16.151 +
16.152 + template<class Digraph> void
16.153 + checkDigraphInArcList(Digraph &G, typename Digraph::Node n, int nn)
16.154 + {
16.155 + typename Digraph::InArcIt e(G,n);
16.156 + for(int i=0;i<nn;i++) {
16.157 + check(e!=INVALID,"Wrong InArc list linking.");
16.158 + check(n==G.target(e), "Wrong InArc list linking.");
16.159 + ++e;
16.160 + }
16.161 + check(e==INVALID,"Wrong InArc list linking.");
16.162 + }
16.163 +
16.164 + template <class Digraph>
16.165 + void checkDigraph() {
16.166 + const int num = 5;
16.167 + Digraph G;
16.168 + addPetersen(G, num);
16.169 + bidirDigraph(G);
16.170 + checkBidirPetersen(G, num);
16.171 + }
16.172 +
16.173 + template <class Digraph>
16.174 + void checkDigraphIterators(const Digraph& digraph) {
16.175 + typedef typename Digraph::Node Node;
16.176 + typedef typename Digraph::NodeIt NodeIt;
16.177 + typedef typename Digraph::Arc Arc;
16.178 + typedef typename Digraph::ArcIt ArcIt;
16.179 + typedef typename Digraph::InArcIt InArcIt;
16.180 + typedef typename Digraph::OutArcIt OutArcIt;
16.181 + // typedef ConArcIt<Digraph> ConArcIt;
16.182 + }
16.183 +
16.184 + ///\file
16.185 + ///\todo Check target(), source() as well;
16.186 +
16.187 +
16.188 +} //namespace lemon
16.189 +
16.190 +
16.191 +#endif
17.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
17.2 +++ b/test/graph_test.cc Sun Jan 20 20:43:48 2008 +0100
17.3 @@ -0,0 +1,207 @@
17.4 +/* -*- C++ -*-
17.5 + *
17.6 + * This file is a part of LEMON, a generic C++ optimization library
17.7 + *
17.8 + * Copyright (C) 2003-2007
17.9 + * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
17.10 + * (Egervary Research Group on Combinatorial Optimization, EGRES).
17.11 + *
17.12 + * Permission to use, modify and distribute this software is granted
17.13 + * provided that this copyright notice appears in all copies. For
17.14 + * precise terms see the accompanying LICENSE file.
17.15 + *
17.16 + * This software is provided "AS IS" with no warranty of any kind,
17.17 + * express or implied, and with no claim as to its suitability for any
17.18 + * purpose.
17.19 + *
17.20 + */
17.21 +
17.22 +#include <lemon/concepts/graph.h>
17.23 +#include <lemon/list_graph.h>
17.24 +// #include <lemon/smart_graph.h>
17.25 +// #include <lemon/full_graph.h>
17.26 +// #include <lemon/grid_graph.h>
17.27 +
17.28 +//#include <lemon/graph_utils.h>
17.29 +
17.30 +#include "test_tools.h"
17.31 +
17.32 +
17.33 +using namespace lemon;
17.34 +using namespace lemon::concepts;
17.35 +
17.36 +void check_concepts() {
17.37 +
17.38 + { // checking digraph components
17.39 + checkConcept<BaseGraphComponent, BaseGraphComponent >();
17.40 +
17.41 + checkConcept<IDableGraphComponent<>,
17.42 + IDableGraphComponent<> >();
17.43 +
17.44 + checkConcept<IterableGraphComponent<>,
17.45 + IterableGraphComponent<> >();
17.46 +
17.47 + checkConcept<MappableGraphComponent<>,
17.48 + MappableGraphComponent<> >();
17.49 +
17.50 + }
17.51 + {
17.52 + checkConcept<Graph, ListGraph>();
17.53 +// checkConcept<Graph, SmartGraph>();
17.54 +// checkConcept<Graph, FullGraph>();
17.55 +// checkConcept<Graph, Graph>();
17.56 +// checkConcept<Graph, GridGraph>();
17.57 + }
17.58 +}
17.59 +
17.60 +template <typename Graph>
17.61 +void check_item_counts(Graph &g, int n, int e) {
17.62 + int nn = 0;
17.63 + for (typename Graph::NodeIt it(g); it != INVALID; ++it) {
17.64 + ++nn;
17.65 + }
17.66 +
17.67 + check(nn == n, "Wrong node number.");
17.68 + // check(countNodes(g) == n, "Wrong node number.");
17.69 +
17.70 + int ee = 0;
17.71 + for (typename Graph::ArcIt it(g); it != INVALID; ++it) {
17.72 + ++ee;
17.73 + }
17.74 +
17.75 + check(ee == 2*e, "Wrong arc number.");
17.76 + // check(countArcs(g) == 2*e, "Wrong arc number.");
17.77 +
17.78 + int uee = 0;
17.79 + for (typename Graph::EdgeIt it(g); it != INVALID; ++it) {
17.80 + ++uee;
17.81 + }
17.82 +
17.83 + check(uee == e, "Wrong edge number.");
17.84 + // check(countEdges(g) == e, "Wrong edge number.");
17.85 +}
17.86 +
17.87 +template <typename Graph>
17.88 +void print_items(Graph &g) {
17.89 +
17.90 + typedef typename Graph::NodeIt NodeIt;
17.91 + typedef typename Graph::EdgeIt EdgeIt;
17.92 + typedef typename Graph::ArcIt ArcIt;
17.93 +
17.94 + std::cout << "Nodes" << std::endl;
17.95 + int i=0;
17.96 + for(NodeIt it(g); it!=INVALID; ++it, ++i) {
17.97 + std::cout << " " << i << ": " << g.id(it) << std::endl;
17.98 + }
17.99 +
17.100 + std::cout << "Edge" << std::endl;
17.101 + i=0;
17.102 + for(EdgeIt it(g); it!=INVALID; ++it, ++i) {
17.103 + std::cout << " " << i << ": " << g.id(it)
17.104 + << " (" << g.id(g.source(it)) << ", " << g.id(g.target(it))
17.105 + << ")" << std::endl;
17.106 + }
17.107 +
17.108 + std::cout << "Arc" << std::endl;
17.109 + i=0;
17.110 + for(ArcIt it(g); it!=INVALID; ++it, ++i) {
17.111 + std::cout << " " << i << ": " << g.id(it)
17.112 + << " (" << g.id(g.source(it)) << ", " << g.id(g.target(it))
17.113 + << ")" << std::endl;
17.114 + }
17.115 +
17.116 +}
17.117 +
17.118 +template <typename Graph>
17.119 +void check_graph() {
17.120 +
17.121 + typedef typename Graph::Node Node;
17.122 + typedef typename Graph::Edge Edge;
17.123 + typedef typename Graph::Arc Arc;
17.124 + typedef typename Graph::NodeIt NodeIt;
17.125 + typedef typename Graph::EdgeIt EdgeIt;
17.126 + typedef typename Graph::ArcIt ArcIt;
17.127 +
17.128 + Graph g;
17.129 +
17.130 + check_item_counts(g,0,0);
17.131 +
17.132 + Node
17.133 + n1 = g.addNode(),
17.134 + n2 = g.addNode(),
17.135 + n3 = g.addNode();
17.136 +
17.137 + Edge
17.138 + e1 = g.addEdge(n1, n2),
17.139 + e2 = g.addEdge(n2, n3);
17.140 +
17.141 + // print_items(g);
17.142 +
17.143 + check_item_counts(g,3,2);
17.144 +}
17.145 +
17.146 +// void checkGridGraph(const GridGraph& g, int w, int h) {
17.147 +// check(g.width() == w, "Wrong width");
17.148 +// check(g.height() == h, "Wrong height");
17.149 +
17.150 +// for (int i = 0; i < w; ++i) {
17.151 +// for (int j = 0; j < h; ++j) {
17.152 +// check(g.col(g(i, j)) == i, "Wrong col");
17.153 +// check(g.row(g(i, j)) == j, "Wrong row");
17.154 +// }
17.155 +// }
17.156 +
17.157 +// for (int i = 0; i < w; ++i) {
17.158 +// for (int j = 0; j < h - 1; ++j) {
17.159 +// check(g.source(g.down(g(i, j))) == g(i, j), "Wrong down");
17.160 +// check(g.target(g.down(g(i, j))) == g(i, j + 1), "Wrong down");
17.161 +// }
17.162 +// check(g.down(g(i, h - 1)) == INVALID, "Wrong down");
17.163 +// }
17.164 +
17.165 +// for (int i = 0; i < w; ++i) {
17.166 +// for (int j = 1; j < h; ++j) {
17.167 +// check(g.source(g.up(g(i, j))) == g(i, j), "Wrong up");
17.168 +// check(g.target(g.up(g(i, j))) == g(i, j - 1), "Wrong up");
17.169 +// }
17.170 +// check(g.up(g(i, 0)) == INVALID, "Wrong up");
17.171 +// }
17.172 +
17.173 +// for (int j = 0; j < h; ++j) {
17.174 +// for (int i = 0; i < w - 1; ++i) {
17.175 +// check(g.source(g.right(g(i, j))) == g(i, j), "Wrong right");
17.176 +// check(g.target(g.right(g(i, j))) == g(i + 1, j), "Wrong right");
17.177 +// }
17.178 +// check(g.right(g(w - 1, j)) == INVALID, "Wrong right");
17.179 +// }
17.180 +
17.181 +// for (int j = 0; j < h; ++j) {
17.182 +// for (int i = 1; i < w; ++i) {
17.183 +// check(g.source(g.left(g(i, j))) == g(i, j), "Wrong left");
17.184 +// check(g.target(g.left(g(i, j))) == g(i - 1, j), "Wrong left");
17.185 +// }
17.186 +// check(g.left(g(0, j)) == INVALID, "Wrong left");
17.187 +// }
17.188 +// }
17.189 +
17.190 +int main() {
17.191 + check_concepts();
17.192 +
17.193 + check_graph<ListGraph>();
17.194 +// check_graph<SmartGraph>();
17.195 +
17.196 +// {
17.197 +// FullGraph g(5);
17.198 +// check_item_counts(g, 5, 10);
17.199 +// }
17.200 +
17.201 +// {
17.202 +// GridGraph g(5, 6);
17.203 +// check_item_counts(g, 30, 49);
17.204 +// checkGridGraph(g, 5, 6);
17.205 +// }
17.206 +
17.207 + std::cout << __FILE__ ": All tests passed.\n";
17.208 +
17.209 + return 0;
17.210 +}
18.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
18.2 +++ b/test/map_test.h Sun Jan 20 20:43:48 2008 +0100
18.3 @@ -0,0 +1,149 @@
18.4 +/* -*- C++ -*-
18.5 + *
18.6 + * This file is a part of LEMON, a generic C++ optimization library
18.7 + *
18.8 + * Copyright (C) 2003-2007
18.9 + * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
18.10 + * (Egervary Research Group on Combinatorial Optimization, EGRES).
18.11 + *
18.12 + * Permission to use, modify and distribute this software is granted
18.13 + * provided that this copyright notice appears in all copies. For
18.14 + * precise terms see the accompanying LICENSE file.
18.15 + *
18.16 + * This software is provided "AS IS" with no warranty of any kind,
18.17 + * express or implied, and with no claim as to its suitability for any
18.18 + * purpose.
18.19 + *
18.20 + */
18.21 +
18.22 +#ifndef LEMON_TEST_MAP_TEST_H
18.23 +#define LEMON_TEST_MAP_TEST_H
18.24 +
18.25 +
18.26 +#include <vector>
18.27 +#include <lemon/maps.h>
18.28 +
18.29 +#include "test_tools.h"
18.30 +
18.31 +
18.32 +//! \ingroup misc
18.33 +//! \file
18.34 +//! \brief Some utilities to test map classes.
18.35 +
18.36 +namespace lemon {
18.37 +
18.38 +
18.39 +
18.40 + template <typename Graph>
18.41 + void checkGraphNodeMap() {
18.42 + Graph graph;
18.43 + const int num = 16;
18.44 +
18.45 + typedef typename Graph::Node Node;
18.46 +
18.47 + std::vector<Node> nodes;
18.48 + for (int i = 0; i < num; ++i) {
18.49 + nodes.push_back(graph.addNode());
18.50 + }
18.51 + typedef typename Graph::template NodeMap<int> IntNodeMap;
18.52 + IntNodeMap map(graph, 42);
18.53 + for (int i = 0; i < int(nodes.size()); ++i) {
18.54 + check(map[nodes[i]] == 42, "Wrong map constructor.");
18.55 + }
18.56 + for (int i = 0; i < num; ++i) {
18.57 + nodes.push_back(graph.addNode());
18.58 + map[nodes.back()] = 23;
18.59 + }
18.60 + map = constMap<Node>(12);
18.61 + for (int i = 0; i < int(nodes.size()); ++i) {
18.62 + check(map[nodes[i]] == 12, "Wrong map constructor.");
18.63 + }
18.64 + graph.clear();
18.65 + nodes.clear();
18.66 + }
18.67 +
18.68 + template <typename Graph>
18.69 + void checkGraphArcMap() {
18.70 + Graph graph;
18.71 + const int num = 16;
18.72 +
18.73 + typedef typename Graph::Node Node;
18.74 + typedef typename Graph::Arc Arc;
18.75 +
18.76 + std::vector<Node> nodes;
18.77 + for (int i = 0; i < num; ++i) {
18.78 + nodes.push_back(graph.addNode());
18.79 + }
18.80 +
18.81 + std::vector<Arc> edges;
18.82 + for (int i = 0; i < num; ++i) {
18.83 + for (int j = 0; j < i; ++j) {
18.84 + edges.push_back(graph.addArc(nodes[i], nodes[j]));
18.85 + }
18.86 + }
18.87 +
18.88 + typedef typename Graph::template ArcMap<int> IntArcMap;
18.89 + IntArcMap map(graph, 42);
18.90 +
18.91 + for (int i = 0; i < int(edges.size()); ++i) {
18.92 + check(map[edges[i]] == 42, "Wrong map constructor.");
18.93 + }
18.94 +
18.95 + for (int i = 0; i < num; ++i) {
18.96 + for (int j = i + 1; j < num; ++j) {
18.97 + edges.push_back(graph.addArc(nodes[i], nodes[j]));
18.98 + map[edges.back()] = 23;
18.99 + }
18.100 + }
18.101 + map = constMap<Arc>(12);
18.102 + for (int i = 0; i < int(edges.size()); ++i) {
18.103 + check(map[edges[i]] == 12, "Wrong map constructor.");
18.104 + }
18.105 + graph.clear();
18.106 + edges.clear();
18.107 + }
18.108 +
18.109 + template <typename Graph>
18.110 + void checkGraphEdgeMap() {
18.111 + Graph graph;
18.112 + const int num = 16;
18.113 +
18.114 + typedef typename Graph::Node Node;
18.115 + typedef typename Graph::Edge Edge;
18.116 +
18.117 + std::vector<Node> nodes;
18.118 + for (int i = 0; i < num; ++i) {
18.119 + nodes.push_back(graph.addNode());
18.120 + }
18.121 +
18.122 + std::vector<Edge> edges;
18.123 + for (int i = 0; i < num; ++i) {
18.124 + for (int j = 0; j < i; ++j) {
18.125 + edges.push_back(graph.addEdge(nodes[i], nodes[j]));
18.126 + }
18.127 + }
18.128 +
18.129 + typedef typename Graph::template EdgeMap<int> IntEdgeMap;
18.130 + IntEdgeMap map(graph, 42);
18.131 +
18.132 + for (int i = 0; i < int(edges.size()); ++i) {
18.133 + check(map[edges[i]] == 42, "Wrong map constructor.");
18.134 + }
18.135 +
18.136 + for (int i = 0; i < num; ++i) {
18.137 + for (int j = i + 1; j < num; ++j) {
18.138 + edges.push_back(graph.addEdge(nodes[i], nodes[j]));
18.139 + map[edges.back()] = 23;
18.140 + }
18.141 + }
18.142 + map = constMap<Edge>(12);
18.143 + for (int i = 0; i < int(edges.size()); ++i) {
18.144 + check(map[edges[i]] == 12, "Wrong map constructor.");
18.145 + }
18.146 + graph.clear();
18.147 + edges.clear();
18.148 + }
18.149 +
18.150 +}
18.151 +
18.152 +#endif