1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/lemon/concepts/digraph.h Sun Jan 20 20:43:48 2008 +0100
1.3 @@ -0,0 +1,453 @@
1.4 +/* -*- C++ -*-
1.5 + *
1.6 + * This file is a part of LEMON, a generic C++ optimization library
1.7 + *
1.8 + * Copyright (C) 2003-2007
1.9 + * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
1.10 + * (Egervary Research Group on Combinatorial Optimization, EGRES).
1.11 + *
1.12 + * Permission to use, modify and distribute this software is granted
1.13 + * provided that this copyright notice appears in all copies. For
1.14 + * precise terms see the accompanying LICENSE file.
1.15 + *
1.16 + * This software is provided "AS IS" with no warranty of any kind,
1.17 + * express or implied, and with no claim as to its suitability for any
1.18 + * purpose.
1.19 + *
1.20 + */
1.21 +
1.22 +#ifndef LEMON_CONCEPT_DIGRAPH_H
1.23 +#define LEMON_CONCEPT_DIGRAPH_H
1.24 +
1.25 +///\ingroup graph_concepts
1.26 +///\file
1.27 +///\brief The concept of directed graphs.
1.28 +
1.29 +#include <lemon/bits/invalid.h>
1.30 +#include <lemon/bits/utility.h>
1.31 +#include <lemon/concepts/maps.h>
1.32 +#include <lemon/concept_check.h>
1.33 +#include <lemon/concepts/graph_components.h>
1.34 +
1.35 +namespace lemon {
1.36 + namespace concepts {
1.37 +
1.38 + /// \ingroup graph_concepts
1.39 + ///
1.40 + /// \brief Class describing the concept of directed graphs.
1.41 + ///
1.42 + /// This class describes the \ref concept "concept" of the
1.43 + /// immutable directed digraphs.
1.44 + ///
1.45 + /// Note that actual digraph implementation like @ref ListDigraph or
1.46 + /// @ref SmartDigraph may have several additional functionality.
1.47 + ///
1.48 + /// \sa concept
1.49 + class Digraph {
1.50 + private:
1.51 + ///Digraphs are \e not copy constructible. Use DigraphCopy() instead.
1.52 +
1.53 + ///Digraphs are \e not copy constructible. Use DigraphCopy() instead.
1.54 + ///
1.55 + Digraph(const Digraph &) {};
1.56 + ///\brief Assignment of \ref Digraph "Digraph"s to another ones are
1.57 + ///\e not allowed. Use DigraphCopy() instead.
1.58 +
1.59 + ///Assignment of \ref Digraph "Digraph"s to another ones are
1.60 + ///\e not allowed. Use DigraphCopy() instead.
1.61 +
1.62 + void operator=(const Digraph &) {}
1.63 + public:
1.64 + ///\e
1.65 +
1.66 + /// Defalult constructor.
1.67 +
1.68 + /// Defalult constructor.
1.69 + ///
1.70 + Digraph() { }
1.71 + /// Class for identifying a node of the digraph
1.72 +
1.73 + /// This class identifies a node of the digraph. It also serves
1.74 + /// as a base class of the node iterators,
1.75 + /// thus they will convert to this type.
1.76 + class Node {
1.77 + public:
1.78 + /// Default constructor
1.79 +
1.80 + /// @warning The default constructor sets the iterator
1.81 + /// to an undefined value.
1.82 + Node() { }
1.83 + /// Copy constructor.
1.84 +
1.85 + /// Copy constructor.
1.86 + ///
1.87 + Node(const Node&) { }
1.88 +
1.89 + /// Invalid constructor \& conversion.
1.90 +
1.91 + /// This constructor initializes the iterator to be invalid.
1.92 + /// \sa Invalid for more details.
1.93 + Node(Invalid) { }
1.94 + /// Equality operator
1.95 +
1.96 + /// Two iterators are equal if and only if they point to the
1.97 + /// same object or both are invalid.
1.98 + bool operator==(Node) const { return true; }
1.99 +
1.100 + /// Inequality operator
1.101 +
1.102 + /// \sa operator==(Node n)
1.103 + ///
1.104 + bool operator!=(Node) const { return true; }
1.105 +
1.106 + /// Artificial ordering operator.
1.107 +
1.108 + /// To allow the use of digraph descriptors as key type in std::map or
1.109 + /// similar associative container we require this.
1.110 + ///
1.111 + /// \note This operator only have to define some strict ordering of
1.112 + /// the items; this order has nothing to do with the iteration
1.113 + /// ordering of the items.
1.114 + bool operator<(Node) const { return false; }
1.115 +
1.116 + };
1.117 +
1.118 + /// This iterator goes through each node.
1.119 +
1.120 + /// This iterator goes through each node.
1.121 + /// Its usage is quite simple, for example you can count the number
1.122 + /// of nodes in digraph \c g of type \c Digraph like this:
1.123 + ///\code
1.124 + /// int count=0;
1.125 + /// for (Digraph::NodeIt n(g); n!=INVALID; ++n) ++count;
1.126 + ///\endcode
1.127 + class NodeIt : public Node {
1.128 + public:
1.129 + /// Default constructor
1.130 +
1.131 + /// @warning The default constructor sets the iterator
1.132 + /// to an undefined value.
1.133 + NodeIt() { }
1.134 + /// Copy constructor.
1.135 +
1.136 + /// Copy constructor.
1.137 + ///
1.138 + NodeIt(const NodeIt& n) : Node(n) { }
1.139 + /// Invalid constructor \& conversion.
1.140 +
1.141 + /// Initialize the iterator to be invalid.
1.142 + /// \sa Invalid for more details.
1.143 + NodeIt(Invalid) { }
1.144 + /// Sets the iterator to the first node.
1.145 +
1.146 + /// Sets the iterator to the first node of \c g.
1.147 + ///
1.148 + NodeIt(const Digraph&) { }
1.149 + /// Node -> NodeIt conversion.
1.150 +
1.151 + /// Sets the iterator to the node of \c the digraph pointed by
1.152 + /// the trivial iterator.
1.153 + /// This feature necessitates that each time we
1.154 + /// iterate the arc-set, the iteration order is the same.
1.155 + NodeIt(const Digraph&, const Node&) { }
1.156 + /// Next node.
1.157 +
1.158 + /// Assign the iterator to the next node.
1.159 + ///
1.160 + NodeIt& operator++() { return *this; }
1.161 + };
1.162 +
1.163 +
1.164 + /// Class for identifying an arc of the digraph
1.165 +
1.166 + /// This class identifies an arc of the digraph. It also serves
1.167 + /// as a base class of the arc iterators,
1.168 + /// thus they will convert to this type.
1.169 + class Arc {
1.170 + public:
1.171 + /// Default constructor
1.172 +
1.173 + /// @warning The default constructor sets the iterator
1.174 + /// to an undefined value.
1.175 + Arc() { }
1.176 + /// Copy constructor.
1.177 +
1.178 + /// Copy constructor.
1.179 + ///
1.180 + Arc(const Arc&) { }
1.181 + /// Initialize the iterator to be invalid.
1.182 +
1.183 + /// Initialize the iterator to be invalid.
1.184 + ///
1.185 + Arc(Invalid) { }
1.186 + /// Equality operator
1.187 +
1.188 + /// Two iterators are equal if and only if they point to the
1.189 + /// same object or both are invalid.
1.190 + bool operator==(Arc) const { return true; }
1.191 + /// Inequality operator
1.192 +
1.193 + /// \sa operator==(Arc n)
1.194 + ///
1.195 + bool operator!=(Arc) const { return true; }
1.196 +
1.197 + /// Artificial ordering operator.
1.198 +
1.199 + /// To allow the use of digraph descriptors as key type in std::map or
1.200 + /// similar associative container we require this.
1.201 + ///
1.202 + /// \note This operator only have to define some strict ordering of
1.203 + /// the items; this order has nothing to do with the iteration
1.204 + /// ordering of the items.
1.205 + bool operator<(Arc) const { return false; }
1.206 + };
1.207 +
1.208 + /// This iterator goes trough the outgoing arcs of a node.
1.209 +
1.210 + /// This iterator goes trough the \e outgoing arcs of a certain node
1.211 + /// of a digraph.
1.212 + /// Its usage is quite simple, for example you can count the number
1.213 + /// of outgoing arcs of a node \c n
1.214 + /// in digraph \c g of type \c Digraph as follows.
1.215 + ///\code
1.216 + /// int count=0;
1.217 + /// for (Digraph::OutArcIt e(g, n); e!=INVALID; ++e) ++count;
1.218 + ///\endcode
1.219 +
1.220 + class OutArcIt : public Arc {
1.221 + public:
1.222 + /// Default constructor
1.223 +
1.224 + /// @warning The default constructor sets the iterator
1.225 + /// to an undefined value.
1.226 + OutArcIt() { }
1.227 + /// Copy constructor.
1.228 +
1.229 + /// Copy constructor.
1.230 + ///
1.231 + OutArcIt(const OutArcIt& e) : Arc(e) { }
1.232 + /// Initialize the iterator to be invalid.
1.233 +
1.234 + /// Initialize the iterator to be invalid.
1.235 + ///
1.236 + OutArcIt(Invalid) { }
1.237 + /// This constructor sets the iterator to the first outgoing arc.
1.238 +
1.239 + /// This constructor sets the iterator to the first outgoing arc of
1.240 + /// the node.
1.241 + OutArcIt(const Digraph&, const Node&) { }
1.242 + /// Arc -> OutArcIt conversion
1.243 +
1.244 + /// Sets the iterator to the value of the trivial iterator.
1.245 + /// This feature necessitates that each time we
1.246 + /// iterate the arc-set, the iteration order is the same.
1.247 + OutArcIt(const Digraph&, const Arc&) { }
1.248 + ///Next outgoing arc
1.249 +
1.250 + /// Assign the iterator to the next
1.251 + /// outgoing arc of the corresponding node.
1.252 + OutArcIt& operator++() { return *this; }
1.253 + };
1.254 +
1.255 + /// This iterator goes trough the incoming arcs of a node.
1.256 +
1.257 + /// This iterator goes trough the \e incoming arcs of a certain node
1.258 + /// of a digraph.
1.259 + /// Its usage is quite simple, for example you can count the number
1.260 + /// of outgoing arcs of a node \c n
1.261 + /// in digraph \c g of type \c Digraph as follows.
1.262 + ///\code
1.263 + /// int count=0;
1.264 + /// for(Digraph::InArcIt e(g, n); e!=INVALID; ++e) ++count;
1.265 + ///\endcode
1.266 +
1.267 + class InArcIt : public Arc {
1.268 + public:
1.269 + /// Default constructor
1.270 +
1.271 + /// @warning The default constructor sets the iterator
1.272 + /// to an undefined value.
1.273 + InArcIt() { }
1.274 + /// Copy constructor.
1.275 +
1.276 + /// Copy constructor.
1.277 + ///
1.278 + InArcIt(const InArcIt& e) : Arc(e) { }
1.279 + /// Initialize the iterator to be invalid.
1.280 +
1.281 + /// Initialize the iterator to be invalid.
1.282 + ///
1.283 + InArcIt(Invalid) { }
1.284 + /// This constructor sets the iterator to first incoming arc.
1.285 +
1.286 + /// This constructor set the iterator to the first incoming arc of
1.287 + /// the node.
1.288 + InArcIt(const Digraph&, const Node&) { }
1.289 + /// Arc -> InArcIt conversion
1.290 +
1.291 + /// Sets the iterator to the value of the trivial iterator \c e.
1.292 + /// This feature necessitates that each time we
1.293 + /// iterate the arc-set, the iteration order is the same.
1.294 + InArcIt(const Digraph&, const Arc&) { }
1.295 + /// Next incoming arc
1.296 +
1.297 + /// Assign the iterator to the next inarc of the corresponding node.
1.298 + ///
1.299 + InArcIt& operator++() { return *this; }
1.300 + };
1.301 + /// This iterator goes through each arc.
1.302 +
1.303 + /// This iterator goes through each arc of a digraph.
1.304 + /// Its usage is quite simple, for example you can count the number
1.305 + /// of arcs in a digraph \c g of type \c Digraph as follows:
1.306 + ///\code
1.307 + /// int count=0;
1.308 + /// for(Digraph::ArcIt e(g); e!=INVALID; ++e) ++count;
1.309 + ///\endcode
1.310 + class ArcIt : public Arc {
1.311 + public:
1.312 + /// Default constructor
1.313 +
1.314 + /// @warning The default constructor sets the iterator
1.315 + /// to an undefined value.
1.316 + ArcIt() { }
1.317 + /// Copy constructor.
1.318 +
1.319 + /// Copy constructor.
1.320 + ///
1.321 + ArcIt(const ArcIt& e) : Arc(e) { }
1.322 + /// Initialize the iterator to be invalid.
1.323 +
1.324 + /// Initialize the iterator to be invalid.
1.325 + ///
1.326 + ArcIt(Invalid) { }
1.327 + /// This constructor sets the iterator to the first arc.
1.328 +
1.329 + /// This constructor sets the iterator to the first arc of \c g.
1.330 + ///@param g the digraph
1.331 + ArcIt(const Digraph& g) { ignore_unused_variable_warning(g); }
1.332 + /// Arc -> ArcIt conversion
1.333 +
1.334 + /// Sets the iterator to the value of the trivial iterator \c e.
1.335 + /// This feature necessitates that each time we
1.336 + /// iterate the arc-set, the iteration order is the same.
1.337 + ArcIt(const Digraph&, const Arc&) { }
1.338 + ///Next arc
1.339 +
1.340 + /// Assign the iterator to the next arc.
1.341 + ArcIt& operator++() { return *this; }
1.342 + };
1.343 + ///Gives back the target node of an arc.
1.344 +
1.345 + ///Gives back the target node of an arc.
1.346 + ///
1.347 + Node target(Arc) const { return INVALID; }
1.348 + ///Gives back the source node of an arc.
1.349 +
1.350 + ///Gives back the source node of an arc.
1.351 + ///
1.352 + Node source(Arc) const { return INVALID; }
1.353 +
1.354 + void first(Node&) const {}
1.355 + void next(Node&) const {}
1.356 +
1.357 + void first(Arc&) const {}
1.358 + void next(Arc&) const {}
1.359 +
1.360 +
1.361 + void firstIn(Arc&, const Node&) const {}
1.362 + void nextIn(Arc&) const {}
1.363 +
1.364 + void firstOut(Arc&, const Node&) const {}
1.365 + void nextOut(Arc&) const {}
1.366 +
1.367 + /// \brief The base node of the iterator.
1.368 + ///
1.369 + /// Gives back the base node of the iterator.
1.370 + /// It is always the target of the pointed arc.
1.371 + Node baseNode(const InArcIt&) const { return INVALID; }
1.372 +
1.373 + /// \brief The running node of the iterator.
1.374 + ///
1.375 + /// Gives back the running node of the iterator.
1.376 + /// It is always the source of the pointed arc.
1.377 + Node runningNode(const InArcIt&) const { return INVALID; }
1.378 +
1.379 + /// \brief The base node of the iterator.
1.380 + ///
1.381 + /// Gives back the base node of the iterator.
1.382 + /// It is always the source of the pointed arc.
1.383 + Node baseNode(const OutArcIt&) const { return INVALID; }
1.384 +
1.385 + /// \brief The running node of the iterator.
1.386 + ///
1.387 + /// Gives back the running node of the iterator.
1.388 + /// It is always the target of the pointed arc.
1.389 + Node runningNode(const OutArcIt&) const { return INVALID; }
1.390 +
1.391 + /// \brief The opposite node on the given arc.
1.392 + ///
1.393 + /// Gives back the opposite node on the given arc.
1.394 + Node oppositeNode(const Node&, const Arc&) const { return INVALID; }
1.395 +
1.396 + /// \brief Read write map of the nodes to type \c T.
1.397 + ///
1.398 + /// ReadWrite map of the nodes to type \c T.
1.399 + /// \sa Reference
1.400 + template<class T>
1.401 + class NodeMap : public ReadWriteMap< Node, T > {
1.402 + public:
1.403 +
1.404 + ///\e
1.405 + NodeMap(const Digraph&) { }
1.406 + ///\e
1.407 + NodeMap(const Digraph&, T) { }
1.408 +
1.409 + ///Copy constructor
1.410 + NodeMap(const NodeMap& nm) : ReadWriteMap< Node, T >(nm) { }
1.411 + ///Assignment operator
1.412 + template <typename CMap>
1.413 + NodeMap& operator=(const CMap&) {
1.414 + checkConcept<ReadMap<Node, T>, CMap>();
1.415 + return *this;
1.416 + }
1.417 + };
1.418 +
1.419 + /// \brief Read write map of the arcs to type \c T.
1.420 + ///
1.421 + /// Reference map of the arcs to type \c T.
1.422 + /// \sa Reference
1.423 + template<class T>
1.424 + class ArcMap : public ReadWriteMap<Arc,T> {
1.425 + public:
1.426 +
1.427 + ///\e
1.428 + ArcMap(const Digraph&) { }
1.429 + ///\e
1.430 + ArcMap(const Digraph&, T) { }
1.431 + ///Copy constructor
1.432 + ArcMap(const ArcMap& em) : ReadWriteMap<Arc,T>(em) { }
1.433 + ///Assignment operator
1.434 + template <typename CMap>
1.435 + ArcMap& operator=(const CMap&) {
1.436 + checkConcept<ReadMap<Arc, T>, CMap>();
1.437 + return *this;
1.438 + }
1.439 + };
1.440 +
1.441 + template <typename RDigraph>
1.442 + struct Constraints {
1.443 + void constraints() {
1.444 + checkConcept<IterableDigraphComponent<>, Digraph>();
1.445 + checkConcept<MappableDigraphComponent<>, Digraph>();
1.446 + }
1.447 + };
1.448 +
1.449 + };
1.450 +
1.451 + } //namespace concepts
1.452 +} //namespace lemon
1.453 +
1.454 +
1.455 +
1.456 +#endif // LEMON_CONCEPT_DIGRAPH_H