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