[med-svn] [cufflinks] 01/12: Imported Upstream version 2.2.1+dfsg
Andreas Tille
tille at debian.org
Mon Aug 15 06:42:43 UTC 2016
This is an automated email from the git hooks/post-receive script.
tille pushed a commit to branch master
in repository cufflinks.
commit ecb58db53e356f42dd8e4873c41dd757ac024b3f
Author: Andreas Tille <tille at debian.org>
Date: Fri Aug 5 16:21:34 2016 +0200
Imported Upstream version 2.2.1+dfsg
---
src/lemon/bfs.h | 1597 ----------------
src/lemon/bin_heap.h | 346 ----
src/lemon/bipartite_matching.h | 1732 -----------------
src/lemon/bits/alteration_notifier.h | 485 -----
src/lemon/bits/array_map.h | 346 ----
src/lemon/bits/base_extender.h | 495 -----
src/lemon/bits/debug_map.h | 382 ----
src/lemon/bits/default_map.h | 181 --
src/lemon/bits/graph_adaptor_extender.h | 742 --------
src/lemon/bits/graph_extender.h | 1397 --------------
src/lemon/bits/invalid.h | 54 -
src/lemon/bits/map_extender.h | 321 ----
src/lemon/bits/path_dump.h | 174 --
src/lemon/bits/traits.h | 346 ----
src/lemon/bits/utility.h | 140 --
src/lemon/bits/variant.h | 508 -----
src/lemon/bits/vector_map.h | 243 ---
src/lemon/bucket_heap.h | 831 --------
src/lemon/concept_check.h | 105 -
src/lemon/concepts/bpugraph.h | 1004 ----------
src/lemon/concepts/graph.h | 453 -----
src/lemon/concepts/graph_components.h | 2093 --------------------
src/lemon/concepts/heap.h | 226 ---
src/lemon/concepts/maps.h | 208 --
src/lemon/concepts/matrix_maps.h | 207 --
src/lemon/concepts/path.h | 307 ---
src/lemon/concepts/ugraph.h | 702 -------
src/lemon/dfs.h | 1543 ---------------
src/lemon/error.h | 683 -------
src/lemon/fib_heap.h | 464 -----
src/lemon/graph_adaptor.h | 2720 --------------------------
src/lemon/graph_utils.h | 3179 -------------------------------
src/lemon/list_graph.h | 2249 ----------------------
src/lemon/maps.h | 1633 ----------------
src/lemon/math.h | 63 -
src/lemon/smart_graph.h | 1163 -----------
src/lemon/tolerance.h | 454 -----
src/lemon/topology.h | 1590 ----------------
38 files changed, 31366 deletions(-)
diff --git a/src/lemon/bfs.h b/src/lemon/bfs.h
deleted file mode 100644
index 8fda28b..0000000
--- a/src/lemon/bfs.h
+++ /dev/null
@@ -1,1597 +0,0 @@
-/* -*- C++ -*-
- *
- * This file is a part of LEMON, a generic C++ optimization library
- *
- * Copyright (C) 2003-2008
- * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
- * (Egervary Research Group on Combinatorial Optimization, EGRES).
- *
- * Permission to use, modify and distribute this software is granted
- * provided that this copyright notice appears in all copies. For
- * precise terms see the accompanying LICENSE file.
- *
- * This software is provided "AS IS" with no warranty of any kind,
- * express or implied, and with no claim as to its suitability for any
- * purpose.
- *
- */
-
-#ifndef LEMON_BFS_H
-#define LEMON_BFS_H
-
-///\ingroup search
-///\file
-///\brief Bfs algorithm.
-
-#include <lemon/list_graph.h>
-#include <lemon/graph_utils.h>
-#include <lemon/bits/path_dump.h>
-#include <lemon/bits/invalid.h>
-#include <lemon/error.h>
-#include <lemon/maps.h>
-
-namespace lemon {
-
-
-
- ///Default traits class of Bfs class.
-
- ///Default traits class of Bfs class.
- ///\param GR Graph type.
- template<class GR>
- struct BfsDefaultTraits
- {
- ///The graph type the algorithm runs on.
- typedef GR Graph;
- ///\brief The type of the map that stores the last
- ///edges of the shortest paths.
- ///
- ///The type of the map that stores the last
- ///edges of the shortest paths.
- ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
- ///
- typedef typename Graph::template NodeMap<typename GR::Edge> PredMap;
- ///Instantiates a PredMap.
-
- ///This function instantiates a \ref PredMap.
- ///\param G is the graph, to which we would like to define the PredMap.
- ///\todo The graph alone may be insufficient to initialize
- static PredMap *createPredMap(const GR &G)
- {
- return new PredMap(G);
- }
- ///The type of the map that indicates which nodes are processed.
-
- ///The type of the map that indicates which nodes are processed.
- ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
- ///\todo named parameter to set this type, function to read and write.
- typedef NullMap<typename Graph::Node,bool> ProcessedMap;
- ///Instantiates a ProcessedMap.
-
- ///This function instantiates a \ref ProcessedMap.
- ///\param g is the graph, to which
- ///we would like to define the \ref ProcessedMap
-#ifdef DOXYGEN
- static ProcessedMap *createProcessedMap(const GR &g)
-#else
- static ProcessedMap *createProcessedMap(const GR &)
-#endif
- {
- return new ProcessedMap();
- }
- ///The type of the map that indicates which nodes are reached.
-
- ///The type of the map that indicates which nodes are reached.
- ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
- ///\todo named parameter to set this type, function to read and write.
- typedef typename Graph::template NodeMap<bool> ReachedMap;
- ///Instantiates a ReachedMap.
-
- ///This function instantiates a \ref ReachedMap.
- ///\param G is the graph, to which
- ///we would like to define the \ref ReachedMap.
- static ReachedMap *createReachedMap(const GR &G)
- {
- return new ReachedMap(G);
- }
- ///The type of the map that stores the dists of the nodes.
-
- ///The type of the map that stores the dists of the nodes.
- ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
- ///
- typedef typename Graph::template NodeMap<int> DistMap;
- ///Instantiates a DistMap.
-
- ///This function instantiates a \ref DistMap.
- ///\param G is the graph, to which we would like to define the \ref DistMap
- static DistMap *createDistMap(const GR &G)
- {
- return new DistMap(G);
- }
- };
-
- ///%BFS algorithm class.
-
- ///\ingroup search
- ///This class provides an efficient implementation of the %BFS algorithm.
- ///
- ///\param GR The graph type the algorithm runs on. The default value is
- ///\ref ListGraph. The value of GR is not used directly by Bfs, it
- ///is only passed to \ref BfsDefaultTraits.
- ///\param TR Traits class to set various data types used by the algorithm.
- ///The default traits class is
- ///\ref BfsDefaultTraits "BfsDefaultTraits<GR>".
- ///See \ref BfsDefaultTraits for the documentation of
- ///a Bfs traits class.
- ///
- ///\author Alpar Juttner
-
-#ifdef DOXYGEN
- template <typename GR,
- typename TR>
-#else
- template <typename GR=ListGraph,
- typename TR=BfsDefaultTraits<GR> >
-#endif
- class Bfs {
- public:
- /**
- * \brief \ref Exception for uninitialized parameters.
- *
- * This error represents problems in the initialization
- * of the parameters of the algorithms.
- */
- class UninitializedParameter : public lemon::UninitializedParameter {
- public:
- virtual const char* what() const throw() {
- return "lemon::Bfs::UninitializedParameter";
- }
- };
-
- typedef TR Traits;
- ///The type of the underlying graph.
- typedef typename TR::Graph Graph;
-
- ///\brief The type of the map that stores the last
- ///edges of the shortest paths.
- typedef typename TR::PredMap PredMap;
- ///The type of the map indicating which nodes are reached.
- typedef typename TR::ReachedMap ReachedMap;
- ///The type of the map indicating which nodes are processed.
- typedef typename TR::ProcessedMap ProcessedMap;
- ///The type of the map that stores the dists of the nodes.
- typedef typename TR::DistMap DistMap;
- private:
-
- typedef typename Graph::Node Node;
- typedef typename Graph::NodeIt NodeIt;
- typedef typename Graph::Edge Edge;
- typedef typename Graph::OutEdgeIt OutEdgeIt;
-
- /// Pointer to the underlying graph.
- const Graph *G;
- ///Pointer to the map of predecessors edges.
- PredMap *_pred;
- ///Indicates if \ref _pred is locally allocated (\c true) or not.
- bool local_pred;
- ///Pointer to the map of distances.
- DistMap *_dist;
- ///Indicates if \ref _dist is locally allocated (\c true) or not.
- bool local_dist;
- ///Pointer to the map of reached status of the nodes.
- ReachedMap *_reached;
- ///Indicates if \ref _reached is locally allocated (\c true) or not.
- bool local_reached;
- ///Pointer to the map of processed status of the nodes.
- ProcessedMap *_processed;
- ///Indicates if \ref _processed is locally allocated (\c true) or not.
- bool local_processed;
-
- std::vector<typename Graph::Node> _queue;
- int _queue_head,_queue_tail,_queue_next_dist;
- int _curr_dist;
-
- ///Creates the maps if necessary.
-
- ///\todo Better memory allocation (instead of new).
- void create_maps()
- {
- if(!_pred) {
- local_pred = true;
- _pred = Traits::createPredMap(*G);
- }
- if(!_dist) {
- local_dist = true;
- _dist = Traits::createDistMap(*G);
- }
- if(!_reached) {
- local_reached = true;
- _reached = Traits::createReachedMap(*G);
- }
- if(!_processed) {
- local_processed = true;
- _processed = Traits::createProcessedMap(*G);
- }
- }
-
- protected:
-
- Bfs() {}
-
- public:
-
- typedef Bfs Create;
-
- ///\name Named template parameters
-
- ///@{
-
- template <class T>
- struct DefPredMapTraits : public Traits {
- typedef T PredMap;
- static PredMap *createPredMap(const Graph &)
- {
- throw UninitializedParameter();
- }
- };
- ///\brief \ref named-templ-param "Named parameter" for setting
- ///PredMap type
- ///
- ///\ref named-templ-param "Named parameter" for setting PredMap type
- ///
- template <class T>
- struct DefPredMap : public Bfs< Graph, DefPredMapTraits<T> > {
- typedef Bfs< Graph, DefPredMapTraits<T> > Create;
- };
-
- template <class T>
- struct DefDistMapTraits : public Traits {
- typedef T DistMap;
- static DistMap *createDistMap(const Graph &)
- {
- throw UninitializedParameter();
- }
- };
- ///\brief \ref named-templ-param "Named parameter" for setting
- ///DistMap type
- ///
- ///\ref named-templ-param "Named parameter" for setting DistMap type
- ///
- template <class T>
- struct DefDistMap : public Bfs< Graph, DefDistMapTraits<T> > {
- typedef Bfs< Graph, DefDistMapTraits<T> > Create;
- };
-
- template <class T>
- struct DefReachedMapTraits : public Traits {
- typedef T ReachedMap;
- static ReachedMap *createReachedMap(const Graph &)
- {
- throw UninitializedParameter();
- }
- };
- ///\brief \ref named-templ-param "Named parameter" for setting
- ///ReachedMap type
- ///
- ///\ref named-templ-param "Named parameter" for setting ReachedMap type
- ///
- template <class T>
- struct DefReachedMap : public Bfs< Graph, DefReachedMapTraits<T> > {
- typedef Bfs< Graph, DefReachedMapTraits<T> > Create;
- };
-
- template <class T>
- struct DefProcessedMapTraits : public Traits {
- typedef T ProcessedMap;
- static ProcessedMap *createProcessedMap(const Graph &)
- {
- throw UninitializedParameter();
- }
- };
- ///\brief \ref named-templ-param "Named parameter" for setting
- ///ProcessedMap type
- ///
- ///\ref named-templ-param "Named parameter" for setting ProcessedMap type
- ///
- template <class T>
- struct DefProcessedMap : public Bfs< Graph, DefProcessedMapTraits<T> > {
- typedef Bfs< Graph, DefProcessedMapTraits<T> > Create;
- };
-
- struct DefGraphProcessedMapTraits : public Traits {
- typedef typename Graph::template NodeMap<bool> ProcessedMap;
- static ProcessedMap *createProcessedMap(const Graph &G)
- {
- return new ProcessedMap(G);
- }
- };
- ///\brief \ref named-templ-param "Named parameter"
- ///for setting the ProcessedMap type to be Graph::NodeMap<bool>.
- ///
- ///\ref named-templ-param "Named parameter"
- ///for setting the ProcessedMap type to be Graph::NodeMap<bool>.
- ///If you don't set it explicitly, it will be automatically allocated.
- template <class T>
- struct DefProcessedMapToBeDefaultMap :
- public Bfs< Graph, DefGraphProcessedMapTraits> {
- typedef Bfs< Graph, DefGraphProcessedMapTraits> Create;
- };
-
- ///@}
-
- public:
-
- ///Constructor.
-
- ///\param _G the graph the algorithm will run on.
- ///
- Bfs(const Graph& _G) :
- G(&_G),
- _pred(NULL), local_pred(false),
- _dist(NULL), local_dist(false),
- _reached(NULL), local_reached(false),
- _processed(NULL), local_processed(false)
- { }
-
- ///Destructor.
- ~Bfs()
- {
- if(local_pred) delete _pred;
- if(local_dist) delete _dist;
- if(local_reached) delete _reached;
- if(local_processed) delete _processed;
- }
-
- ///Sets the map storing the predecessor edges.
-
- ///Sets the map storing the predecessor edges.
- ///If you don't use this function before calling \ref run(),
- ///it will allocate one. The destructor deallocates this
- ///automatically allocated map, of course.
- ///\return <tt> (*this) </tt>
- Bfs &predMap(PredMap &m)
- {
- if(local_pred) {
- delete _pred;
- local_pred=false;
- }
- _pred = &m;
- return *this;
- }
-
- ///Sets the map indicating the reached nodes.
-
- ///Sets the map indicating the reached nodes.
- ///If you don't use this function before calling \ref run(),
- ///it will allocate one. The destructor deallocates this
- ///automatically allocated map, of course.
- ///\return <tt> (*this) </tt>
- Bfs &reachedMap(ReachedMap &m)
- {
- if(local_reached) {
- delete _reached;
- local_reached=false;
- }
- _reached = &m;
- return *this;
- }
-
- ///Sets the map indicating the processed nodes.
-
- ///Sets the map indicating the processed nodes.
- ///If you don't use this function before calling \ref run(),
- ///it will allocate one. The destructor deallocates this
- ///automatically allocated map, of course.
- ///\return <tt> (*this) </tt>
- Bfs &processedMap(ProcessedMap &m)
- {
- if(local_processed) {
- delete _processed;
- local_processed=false;
- }
- _processed = &m;
- return *this;
- }
-
- ///Sets the map storing the distances calculated by the algorithm.
-
- ///Sets the map storing the distances calculated by the algorithm.
- ///If you don't use this function before calling \ref run(),
- ///it will allocate one. The destructor deallocates this
- ///automatically allocated map, of course.
- ///\return <tt> (*this) </tt>
- Bfs &distMap(DistMap &m)
- {
- if(local_dist) {
- delete _dist;
- local_dist=false;
- }
- _dist = &m;
- return *this;
- }
-
- public:
- ///\name Execution control
- ///The simplest way to execute the algorithm is to use
- ///one of the member functions called \c run(...).
- ///\n
- ///If you need more control on the execution,
- ///first you must call \ref init(), then you can add several source nodes
- ///with \ref addSource().
- ///Finally \ref start() will perform the actual path
- ///computation.
-
- ///@{
-
- ///\brief Initializes the internal data structures.
- ///
- ///Initializes the internal data structures.
- ///
- void init()
- {
- create_maps();
- _queue.resize(countNodes(*G));
- _queue_head=_queue_tail=0;
- _curr_dist=1;
- for ( NodeIt u(*G) ; u!=INVALID ; ++u ) {
- _pred->set(u,INVALID);
- _reached->set(u,false);
- _processed->set(u,false);
- }
- }
-
- ///Adds a new source node.
-
- ///Adds a new source node to the set of nodes to be processed.
- ///
- void addSource(Node s)
- {
- if(!(*_reached)[s])
- {
- _reached->set(s,true);
- _pred->set(s,INVALID);
- _dist->set(s,0);
- _queue[_queue_head++]=s;
- _queue_next_dist=_queue_head;
- }
- }
-
- ///Processes the next node.
-
- ///Processes the next node.
- ///
- ///\return The processed node.
- ///
- ///\warning The queue must not be empty!
- Node processNextNode()
- {
- if(_queue_tail==_queue_next_dist) {
- _curr_dist++;
- _queue_next_dist=_queue_head;
- }
- Node n=_queue[_queue_tail++];
- _processed->set(n,true);
- Node m;
- for(OutEdgeIt e(*G,n);e!=INVALID;++e)
- if(!(*_reached)[m=G->target(e)]) {
- _queue[_queue_head++]=m;
- _reached->set(m,true);
- _pred->set(m,e);
- _dist->set(m,_curr_dist);
- }
- return n;
- }
-
- ///Processes the next node.
-
- ///Processes the next node. And checks that the given target node
- ///is reached. If the target node is reachable from the processed
- ///node then the reached parameter will be set true. The reached
- ///parameter should be initially false.
- ///
- ///\param target The target node.
- ///\retval reach Indicates that the target node is reached.
- ///\return The processed node.
- ///
- ///\warning The queue must not be empty!
- Node processNextNode(Node target, bool& reach)
- {
- if(_queue_tail==_queue_next_dist) {
- _curr_dist++;
- _queue_next_dist=_queue_head;
- }
- Node n=_queue[_queue_tail++];
- _processed->set(n,true);
- Node m;
- for(OutEdgeIt e(*G,n);e!=INVALID;++e)
- if(!(*_reached)[m=G->target(e)]) {
- _queue[_queue_head++]=m;
- _reached->set(m,true);
- _pred->set(m,e);
- _dist->set(m,_curr_dist);
- reach = reach || (target == m);
- }
- return n;
- }
-
- ///Processes the next node.
-
- ///Processes the next node. And checks that at least one of
- ///reached node has true value in the \c nm node map. If one node
- ///with true value is reachable from the processed node then the
- ///rnode parameter will be set to the first of such nodes.
- ///
- ///\param nm The node map of possible targets.
- ///\retval rnode The reached target node.
- ///\return The processed node.
- ///
- ///\warning The queue must not be empty!
- template<class NM>
- Node processNextNode(const NM& nm, Node& rnode)
- {
- if(_queue_tail==_queue_next_dist) {
- _curr_dist++;
- _queue_next_dist=_queue_head;
- }
- Node n=_queue[_queue_tail++];
- _processed->set(n,true);
- Node m;
- for(OutEdgeIt e(*G,n);e!=INVALID;++e)
- if(!(*_reached)[m=G->target(e)]) {
- _queue[_queue_head++]=m;
- _reached->set(m,true);
- _pred->set(m,e);
- _dist->set(m,_curr_dist);
- if (nm[m] && rnode == INVALID) rnode = m;
- }
- return n;
- }
-
- ///Next node to be processed.
-
- ///Next node to be processed.
- ///
- ///\return The next node to be processed or INVALID if the queue is
- /// empty.
- Node nextNode()
- {
- return _queue_tail<_queue_head?_queue[_queue_tail]:INVALID;
- }
-
- ///\brief Returns \c false if there are nodes
- ///to be processed in the queue
- ///
- ///Returns \c false if there are nodes
- ///to be processed in the queue
- bool emptyQueue() { return _queue_tail==_queue_head; }
- ///Returns the number of the nodes to be processed.
-
- ///Returns the number of the nodes to be processed in the queue.
- int queueSize() { return _queue_head-_queue_tail; }
-
- ///Executes the algorithm.
-
- ///Executes the algorithm.
- ///
- ///\pre init() must be called and at least one node should be added
- ///with addSource() before using this function.
- ///
- ///This method runs the %BFS algorithm from the root node(s)
- ///in order to
- ///compute the
- ///shortest path to each node. The algorithm computes
- ///- The shortest path tree.
- ///- The distance of each node from the root(s).
- void start()
- {
- while ( !emptyQueue() ) processNextNode();
- }
-
- ///Executes the algorithm until \c dest is reached.
-
- ///Executes the algorithm until \c dest is reached.
- ///
- ///\pre init() must be called and at least one node should be added
- ///with addSource() before using this function.
- ///
- ///This method runs the %BFS algorithm from the root node(s)
- ///in order to compute the shortest path to \c dest.
- ///The algorithm computes
- ///- The shortest path to \c dest.
- ///- The distance of \c dest from the root(s).
- void start(Node dest)
- {
- bool reach = false;
- while ( !emptyQueue() && !reach ) processNextNode(dest, reach);
- }
-
- ///Executes the algorithm until a condition is met.
-
- ///Executes the algorithm until a condition is met.
- ///
- ///\pre init() must be called and at least one node should be added
- ///with addSource() before using this function.
- ///
- ///\param nm must be a bool (or convertible) node map. The
- ///algorithm will stop when it reaches a node \c v with
- /// <tt>nm[v]</tt> true.
- ///
- ///\return The reached node \c v with <tt>nm[v]</tt> true or
- ///\c INVALID if no such node was found.
- template<class NM>
- Node start(const NM &nm)
- {
- Node rnode = INVALID;
- while ( !emptyQueue() && rnode == INVALID ) {
- processNextNode(nm, rnode);
- }
- return rnode;
- }
-
- ///Runs %BFS algorithm from node \c s.
-
- ///This method runs the %BFS algorithm from a root node \c s
- ///in order to
- ///compute the
- ///shortest path to each node. The algorithm computes
- ///- The shortest path tree.
- ///- The distance of each node from the root.
- ///
- ///\note b.run(s) is just a shortcut of the following code.
- ///\code
- /// b.init();
- /// b.addSource(s);
- /// b.start();
- ///\endcode
- void run(Node s) {
- init();
- addSource(s);
- start();
- }
-
- ///Finds the shortest path between \c s and \c t.
-
- ///Finds the shortest path between \c s and \c t.
- ///
- ///\return The length of the shortest s---t path if there exists one,
- ///0 otherwise.
- ///\note Apart from the return value, b.run(s) is
- ///just a shortcut of the following code.
- ///\code
- /// b.init();
- /// b.addSource(s);
- /// b.start(t);
- ///\endcode
- int run(Node s,Node t) {
- init();
- addSource(s);
- start(t);
- return reached(t) ? _curr_dist : 0;
- }
-
- ///@}
-
- ///\name Query Functions
- ///The result of the %BFS algorithm can be obtained using these
- ///functions.\n
- ///Before the use of these functions,
- ///either run() or start() must be calleb.
-
- ///@{
-
- typedef PredMapPath<Graph, PredMap> Path;
-
- ///Gives back the shortest path.
-
- ///Gives back the shortest path.
- ///\pre The \c t should be reachable from the source.
- Path path(Node t)
- {
- return Path(*G, *_pred, t);
- }
-
- ///The distance of a node from the root(s).
-
- ///Returns the distance of a node from the root(s).
- ///\pre \ref run() must be called before using this function.
- ///\warning If node \c v in unreachable from the root(s) the return value
- ///of this function is undefined.
- int dist(Node v) const { return (*_dist)[v]; }
-
- ///Returns the 'previous edge' of the shortest path tree.
-
- ///For a node \c v it returns the 'previous edge'
- ///of the shortest path tree,
- ///i.e. it returns the last edge of a shortest path from the root(s) to \c
- ///v. It is \ref INVALID
- ///if \c v is unreachable from the root(s) or \c v is a root. The
- ///shortest path tree used here is equal to the shortest path tree used in
- ///\ref predNode().
- ///\pre Either \ref run() or \ref start() must be called before using
- ///this function.
- Edge predEdge(Node v) const { return (*_pred)[v];}
-
- ///Returns the 'previous node' of the shortest path tree.
-
- ///For a node \c v it returns the 'previous node'
- ///of the shortest path tree,
- ///i.e. it returns the last but one node from a shortest path from the
- ///root(a) to \c /v.
- ///It is INVALID if \c v is unreachable from the root(s) or
- ///if \c v itself a root.
- ///The shortest path tree used here is equal to the shortest path
- ///tree used in \ref predEdge().
- ///\pre Either \ref run() or \ref start() must be called before
- ///using this function.
- Node predNode(Node v) const { return (*_pred)[v]==INVALID ? INVALID:
- G->source((*_pred)[v]); }
-
- ///Returns a reference to the NodeMap of distances.
-
- ///Returns a reference to the NodeMap of distances.
- ///\pre Either \ref run() or \ref init() must
- ///be called before using this function.
- const DistMap &distMap() const { return *_dist;}
-
- ///Returns a reference to the shortest path tree map.
-
- ///Returns a reference to the NodeMap of the edges of the
- ///shortest path tree.
- ///\pre Either \ref run() or \ref init()
- ///must be called before using this function.
- const PredMap &predMap() const { return *_pred;}
-
- ///Checks if a node is reachable from the root.
-
- ///Returns \c true if \c v is reachable from the root.
- ///\warning The source nodes are indicated as unreached.
- ///\pre Either \ref run() or \ref start()
- ///must be called before using this function.
- ///
- bool reached(Node v) { return (*_reached)[v]; }
-
- ///@}
- };
-
- ///Default traits class of Bfs function.
-
- ///Default traits class of Bfs function.
- ///\param GR Graph type.
- template<class GR>
- struct BfsWizardDefaultTraits
- {
- ///The graph type the algorithm runs on.
- typedef GR Graph;
- ///\brief The type of the map that stores the last
- ///edges of the shortest paths.
- ///
- ///The type of the map that stores the last
- ///edges of the shortest paths.
- ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
- ///
- typedef NullMap<typename Graph::Node,typename GR::Edge> PredMap;
- ///Instantiates a PredMap.
-
- ///This function instantiates a \ref PredMap.
- ///\param g is the graph, to which we would like to define the PredMap.
- ///\todo The graph alone may be insufficient to initialize
-#ifdef DOXYGEN
- static PredMap *createPredMap(const GR &g)
-#else
- static PredMap *createPredMap(const GR &)
-#endif
- {
- return new PredMap();
- }
-
- ///The type of the map that indicates which nodes are processed.
-
- ///The type of the map that indicates which nodes are processed.
- ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
- ///\todo named parameter to set this type, function to read and write.
- typedef NullMap<typename Graph::Node,bool> ProcessedMap;
- ///Instantiates a ProcessedMap.
-
- ///This function instantiates a \ref ProcessedMap.
- ///\param g is the graph, to which
- ///we would like to define the \ref ProcessedMap
-#ifdef DOXYGEN
- static ProcessedMap *createProcessedMap(const GR &g)
-#else
- static ProcessedMap *createProcessedMap(const GR &)
-#endif
- {
- return new ProcessedMap();
- }
- ///The type of the map that indicates which nodes are reached.
-
- ///The type of the map that indicates which nodes are reached.
- ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
- ///\todo named parameter to set this type, function to read and write.
- typedef typename Graph::template NodeMap<bool> ReachedMap;
- ///Instantiates a ReachedMap.
-
- ///This function instantiates a \ref ReachedMap.
- ///\param G is the graph, to which
- ///we would like to define the \ref ReachedMap.
- static ReachedMap *createReachedMap(const GR &G)
- {
- return new ReachedMap(G);
- }
- ///The type of the map that stores the dists of the nodes.
-
- ///The type of the map that stores the dists of the nodes.
- ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
- ///
- typedef NullMap<typename Graph::Node,int> DistMap;
- ///Instantiates a DistMap.
-
- ///This function instantiates a \ref DistMap.
- ///\param g is the graph, to which we would like to define the \ref DistMap
-#ifdef DOXYGEN
- static DistMap *createDistMap(const GR &g)
-#else
- static DistMap *createDistMap(const GR &)
-#endif
- {
- return new DistMap();
- }
- };
-
- /// Default traits used by \ref BfsWizard
-
- /// To make it easier to use Bfs algorithm
- ///we have created a wizard class.
- /// This \ref BfsWizard class needs default traits,
- ///as well as the \ref Bfs class.
- /// The \ref BfsWizardBase is a class to be the default traits of the
- /// \ref BfsWizard class.
- template<class GR>
- class BfsWizardBase : public BfsWizardDefaultTraits<GR>
- {
-
- typedef BfsWizardDefaultTraits<GR> Base;
- protected:
- /// Type of the nodes in the graph.
- typedef typename Base::Graph::Node Node;
-
- /// Pointer to the underlying graph.
- void *_g;
- ///Pointer to the map of reached nodes.
- void *_reached;
- ///Pointer to the map of processed nodes.
- void *_processed;
- ///Pointer to the map of predecessors edges.
- void *_pred;
- ///Pointer to the map of distances.
- void *_dist;
- ///Pointer to the source node.
- Node _source;
-
- public:
- /// Constructor.
-
- /// This constructor does not require parameters, therefore it initiates
- /// all of the attributes to default values (0, INVALID).
- BfsWizardBase() : _g(0), _reached(0), _processed(0), _pred(0),
- _dist(0), _source(INVALID) {}
-
- /// Constructor.
-
- /// This constructor requires some parameters,
- /// listed in the parameters list.
- /// Others are initiated to 0.
- /// \param g is the initial value of \ref _g
- /// \param s is the initial value of \ref _source
- BfsWizardBase(const GR &g, Node s=INVALID) :
- _g(reinterpret_cast<void*>(const_cast<GR*>(&g))),
- _reached(0), _processed(0), _pred(0), _dist(0), _source(s) {}
-
- };
-
- /// A class to make the usage of Bfs algorithm easier
-
- /// This class is created to make it easier to use Bfs algorithm.
- /// It uses the functions and features of the plain \ref Bfs,
- /// but it is much simpler to use it.
- ///
- /// Simplicity means that the way to change the types defined
- /// in the traits class is based on functions that returns the new class
- /// and not on templatable built-in classes.
- /// When using the plain \ref Bfs
- /// the new class with the modified type comes from
- /// the original class by using the ::
- /// operator. In the case of \ref BfsWizard only
- /// a function have to be called and it will
- /// return the needed class.
- ///
- /// It does not have own \ref run method. When its \ref run method is called
- /// it initiates a plain \ref Bfs class, and calls the \ref Bfs::run
- /// method of it.
- template<class TR>
- class BfsWizard : public TR
- {
- typedef TR Base;
-
- ///The type of the underlying graph.
- typedef typename TR::Graph Graph;
- //\e
- typedef typename Graph::Node Node;
- //\e
- typedef typename Graph::NodeIt NodeIt;
- //\e
- typedef typename Graph::Edge Edge;
- //\e
- typedef typename Graph::OutEdgeIt OutEdgeIt;
-
- ///\brief The type of the map that stores
- ///the reached nodes
- typedef typename TR::ReachedMap ReachedMap;
- ///\brief The type of the map that stores
- ///the processed nodes
- typedef typename TR::ProcessedMap ProcessedMap;
- ///\brief The type of the map that stores the last
- ///edges of the shortest paths.
- typedef typename TR::PredMap PredMap;
- ///The type of the map that stores the dists of the nodes.
- typedef typename TR::DistMap DistMap;
-
- public:
- /// Constructor.
- BfsWizard() : TR() {}
-
- /// Constructor that requires parameters.
-
- /// Constructor that requires parameters.
- /// These parameters will be the default values for the traits class.
- BfsWizard(const Graph &g, Node s=INVALID) :
- TR(g,s) {}
-
- ///Copy constructor
- BfsWizard(const TR &b) : TR(b) {}
-
- ~BfsWizard() {}
-
- ///Runs Bfs algorithm from a given node.
-
- ///Runs Bfs algorithm from a given node.
- ///The node can be given by the \ref source function.
- void run()
- {
- if(Base::_source==INVALID) throw UninitializedParameter();
- Bfs<Graph,TR> alg(*reinterpret_cast<const Graph*>(Base::_g));
- if(Base::_reached)
- alg.reachedMap(*reinterpret_cast<ReachedMap*>(Base::_reached));
- if(Base::_processed)
- alg.processedMap(*reinterpret_cast<ProcessedMap*>(Base::_processed));
- if(Base::_pred)
- alg.predMap(*reinterpret_cast<PredMap*>(Base::_pred));
- if(Base::_dist)
- alg.distMap(*reinterpret_cast<DistMap*>(Base::_dist));
- alg.run(Base::_source);
- }
-
- ///Runs Bfs algorithm from the given node.
-
- ///Runs Bfs algorithm from the given node.
- ///\param s is the given source.
- void run(Node s)
- {
- Base::_source=s;
- run();
- }
-
- template<class T>
- struct DefPredMapBase : public Base {
- typedef T PredMap;
- static PredMap *createPredMap(const Graph &) { return 0; };
- DefPredMapBase(const TR &b) : TR(b) {}
- };
-
- ///\brief \ref named-templ-param "Named parameter"
- ///function for setting PredMap
- ///
- /// \ref named-templ-param "Named parameter"
- ///function for setting PredMap
- ///
- template<class T>
- BfsWizard<DefPredMapBase<T> > predMap(const T &t)
- {
- Base::_pred=reinterpret_cast<void*>(const_cast<T*>(&t));
- return BfsWizard<DefPredMapBase<T> >(*this);
- }
-
-
- template<class T>
- struct DefReachedMapBase : public Base {
- typedef T ReachedMap;
- static ReachedMap *createReachedMap(const Graph &) { return 0; };
- DefReachedMapBase(const TR &b) : TR(b) {}
- };
-
- ///\brief \ref named-templ-param "Named parameter"
- ///function for setting ReachedMap
- ///
- /// \ref named-templ-param "Named parameter"
- ///function for setting ReachedMap
- ///
- template<class T>
- BfsWizard<DefReachedMapBase<T> > reachedMap(const T &t)
- {
- Base::_pred=reinterpret_cast<void*>(const_cast<T*>(&t));
- return BfsWizard<DefReachedMapBase<T> >(*this);
- }
-
-
- template<class T>
- struct DefProcessedMapBase : public Base {
- typedef T ProcessedMap;
- static ProcessedMap *createProcessedMap(const Graph &) { return 0; };
- DefProcessedMapBase(const TR &b) : TR(b) {}
- };
-
- ///\brief \ref named-templ-param "Named parameter"
- ///function for setting ProcessedMap
- ///
- /// \ref named-templ-param "Named parameter"
- ///function for setting ProcessedMap
- ///
- template<class T>
- BfsWizard<DefProcessedMapBase<T> > processedMap(const T &t)
- {
- Base::_pred=reinterpret_cast<void*>(const_cast<T*>(&t));
- return BfsWizard<DefProcessedMapBase<T> >(*this);
- }
-
-
- template<class T>
- struct DefDistMapBase : public Base {
- typedef T DistMap;
- static DistMap *createDistMap(const Graph &) { return 0; };
- DefDistMapBase(const TR &b) : TR(b) {}
- };
-
- ///\brief \ref named-templ-param "Named parameter"
- ///function for setting DistMap type
- ///
- /// \ref named-templ-param "Named parameter"
- ///function for setting DistMap type
- ///
- template<class T>
- BfsWizard<DefDistMapBase<T> > distMap(const T &t)
- {
- Base::_dist=reinterpret_cast<void*>(const_cast<T*>(&t));
- return BfsWizard<DefDistMapBase<T> >(*this);
- }
-
- /// Sets the source node, from which the Bfs algorithm runs.
-
- /// Sets the source node, from which the Bfs algorithm runs.
- /// \param s is the source node.
- BfsWizard<TR> &source(Node s)
- {
- Base::_source=s;
- return *this;
- }
-
- };
-
- ///Function type interface for Bfs algorithm.
-
- /// \ingroup search
- ///Function type interface for Bfs algorithm.
- ///
- ///This function also has several
- ///\ref named-templ-func-param "named parameters",
- ///they are declared as the members of class \ref BfsWizard.
- ///The following
- ///example shows how to use these parameters.
- ///\code
- /// bfs(g,source).predMap(preds).run();
- ///\endcode
- ///\warning Don't forget to put the \ref BfsWizard::run() "run()"
- ///to the end of the parameter list.
- ///\sa BfsWizard
- ///\sa Bfs
- template<class GR>
- BfsWizard<BfsWizardBase<GR> >
- bfs(const GR &g,typename GR::Node s=INVALID)
- {
- return BfsWizard<BfsWizardBase<GR> >(g,s);
- }
-
-#ifdef DOXYGEN
- /// \brief Visitor class for bfs.
- ///
- /// This class defines the interface of the BfsVisit events, and
- /// it could be the base of a real Visitor class.
- template <typename _Graph>
- struct BfsVisitor {
- typedef _Graph Graph;
- typedef typename Graph::Edge Edge;
- typedef typename Graph::Node Node;
- /// \brief Called when the edge reach a node.
- ///
- /// It is called when the bfs find an edge which target is not
- /// reached yet.
- void discover(const Edge& edge) {}
- /// \brief Called when the node reached first time.
- ///
- /// It is Called when the node reached first time.
- void reach(const Node& node) {}
- /// \brief Called when the edge examined but target of the edge
- /// already discovered.
- ///
- /// It called when the edge examined but the target of the edge
- /// already discovered.
- void examine(const Edge& edge) {}
- /// \brief Called for the source node of the bfs.
- ///
- /// It is called for the source node of the bfs.
- void start(const Node& node) {}
- /// \brief Called when the node processed.
- ///
- /// It is Called when the node processed.
- void process(const Node& node) {}
- };
-#else
- template <typename _Graph>
- struct BfsVisitor {
- typedef _Graph Graph;
- typedef typename Graph::Edge Edge;
- typedef typename Graph::Node Node;
- void discover(const Edge&) {}
- void reach(const Node&) {}
- void examine(const Edge&) {}
- void start(const Node&) {}
- void process(const Node&) {}
-
- template <typename _Visitor>
- struct Constraints {
- void constraints() {
- Edge edge;
- Node node;
- visitor.discover(edge);
- visitor.reach(node);
- visitor.examine(edge);
- visitor.start(node);
- visitor.process(node);
- }
- _Visitor& visitor;
- };
- };
-#endif
-
- /// \brief Default traits class of BfsVisit class.
- ///
- /// Default traits class of BfsVisit class.
- /// \param _Graph Graph type.
- template<class _Graph>
- struct BfsVisitDefaultTraits {
-
- /// \brief The graph type the algorithm runs on.
- typedef _Graph Graph;
-
- /// \brief The type of the map that indicates which nodes are reached.
- ///
- /// The type of the map that indicates which nodes are reached.
- /// It must meet the \ref concepts::WriteMap "WriteMap" concept.
- /// \todo named parameter to set this type, function to read and write.
- typedef typename Graph::template NodeMap<bool> ReachedMap;
-
- /// \brief Instantiates a ReachedMap.
- ///
- /// This function instantiates a \ref ReachedMap.
- /// \param graph is the graph, to which
- /// we would like to define the \ref ReachedMap.
- static ReachedMap *createReachedMap(const Graph &graph) {
- return new ReachedMap(graph);
- }
-
- };
-
- /// \ingroup search
- ///
- /// \brief %BFS Visit algorithm class.
- ///
- /// This class provides an efficient implementation of the %BFS algorithm
- /// with visitor interface.
- ///
- /// The %BfsVisit class provides an alternative interface to the Bfs
- /// class. It works with callback mechanism, the BfsVisit object calls
- /// on every bfs event the \c Visitor class member functions.
- ///
- /// \param _Graph The graph type the algorithm runs on. The default value is
- /// \ref ListGraph. The value of _Graph is not used directly by Bfs, it
- /// is only passed to \ref BfsDefaultTraits.
- /// \param _Visitor The Visitor object for the algorithm. The
- /// \ref BfsVisitor "BfsVisitor<_Graph>" is an empty Visitor which
- /// does not observe the Bfs events. If you want to observe the bfs
- /// events you should implement your own Visitor class.
- /// \param _Traits Traits class to set various data types used by the
- /// algorithm. The default traits class is
- /// \ref BfsVisitDefaultTraits "BfsVisitDefaultTraits<_Graph>".
- /// See \ref BfsVisitDefaultTraits for the documentation of
- /// a Bfs visit traits class.
- ///
- /// \author Jacint Szabo, Alpar Juttner and Balazs Dezso
-#ifdef DOXYGEN
- template <typename _Graph, typename _Visitor, typename _Traits>
-#else
- template <typename _Graph = ListGraph,
- typename _Visitor = BfsVisitor<_Graph>,
- typename _Traits = BfsDefaultTraits<_Graph> >
-#endif
- class BfsVisit {
- public:
-
- /// \brief \ref Exception for uninitialized parameters.
- ///
- /// This error represents problems in the initialization
- /// of the parameters of the algorithms.
- class UninitializedParameter : public lemon::UninitializedParameter {
- public:
- virtual const char* what() const throw()
- {
- return "lemon::BfsVisit::UninitializedParameter";
- }
- };
-
- typedef _Traits Traits;
-
- typedef typename Traits::Graph Graph;
-
- typedef _Visitor Visitor;
-
- ///The type of the map indicating which nodes are reached.
- typedef typename Traits::ReachedMap ReachedMap;
-
- private:
-
- typedef typename Graph::Node Node;
- typedef typename Graph::NodeIt NodeIt;
- typedef typename Graph::Edge Edge;
- typedef typename Graph::OutEdgeIt OutEdgeIt;
-
- /// Pointer to the underlying graph.
- const Graph *_graph;
- /// Pointer to the visitor object.
- Visitor *_visitor;
- ///Pointer to the map of reached status of the nodes.
- ReachedMap *_reached;
- ///Indicates if \ref _reached is locally allocated (\c true) or not.
- bool local_reached;
-
- std::vector<typename Graph::Node> _list;
- int _list_front, _list_back;
-
- /// \brief Creates the maps if necessary.
- ///
- /// Creates the maps if necessary.
- void create_maps() {
- if(!_reached) {
- local_reached = true;
- _reached = Traits::createReachedMap(*_graph);
- }
- }
-
- protected:
-
- BfsVisit() {}
-
- public:
-
- typedef BfsVisit Create;
-
- /// \name Named template parameters
-
- ///@{
- template <class T>
- struct DefReachedMapTraits : public Traits {
- typedef T ReachedMap;
- static ReachedMap *createReachedMap(const Graph &graph) {
- throw UninitializedParameter();
- }
- };
- /// \brief \ref named-templ-param "Named parameter" for setting
- /// ReachedMap type
- ///
- /// \ref named-templ-param "Named parameter" for setting ReachedMap type
- template <class T>
- struct DefReachedMap : public BfsVisit< Graph, Visitor,
- DefReachedMapTraits<T> > {
- typedef BfsVisit< Graph, Visitor, DefReachedMapTraits<T> > Create;
- };
- ///@}
-
- public:
-
- /// \brief Constructor.
- ///
- /// Constructor.
- ///
- /// \param graph the graph the algorithm will run on.
- /// \param visitor The visitor of the algorithm.
- ///
- BfsVisit(const Graph& graph, Visitor& visitor)
- : _graph(&graph), _visitor(&visitor),
- _reached(0), local_reached(false) {}
-
- /// \brief Destructor.
- ///
- /// Destructor.
- ~BfsVisit() {
- if(local_reached) delete _reached;
- }
-
- /// \brief Sets the map indicating if a node is reached.
- ///
- /// Sets the map indicating if a node is reached.
- /// If you don't use this function before calling \ref run(),
- /// it will allocate one. The destuctor deallocates this
- /// automatically allocated map, of course.
- /// \return <tt> (*this) </tt>
- BfsVisit &reachedMap(ReachedMap &m) {
- if(local_reached) {
- delete _reached;
- local_reached = false;
- }
- _reached = &m;
- return *this;
- }
-
- public:
- /// \name Execution control
- /// The simplest way to execute the algorithm is to use
- /// one of the member functions called \c run(...).
- /// \n
- /// If you need more control on the execution,
- /// first you must call \ref init(), then you can adda source node
- /// with \ref addSource().
- /// Finally \ref start() will perform the actual path
- /// computation.
-
- /// @{
- /// \brief Initializes the internal data structures.
- ///
- /// Initializes the internal data structures.
- ///
- void init() {
- create_maps();
- _list.resize(countNodes(*_graph));
- _list_front = _list_back = -1;
- for (NodeIt u(*_graph) ; u != INVALID ; ++u) {
- _reached->set(u, false);
- }
- }
-
- /// \brief Adds a new source node.
- ///
- /// Adds a new source node to the set of nodes to be processed.
- void addSource(Node s) {
- if(!(*_reached)[s]) {
- _reached->set(s,true);
- _visitor->start(s);
- _visitor->reach(s);
- _list[++_list_back] = s;
- }
- }
-
- /// \brief Processes the next node.
- ///
- /// Processes the next node.
- ///
- /// \return The processed node.
- ///
- /// \pre The queue must not be empty!
- Node processNextNode() {
- Node n = _list[++_list_front];
- _visitor->process(n);
- Edge e;
- for (_graph->firstOut(e, n); e != INVALID; _graph->nextOut(e)) {
- Node m = _graph->target(e);
- if (!(*_reached)[m]) {
- _visitor->discover(e);
- _visitor->reach(m);
- _reached->set(m, true);
- _list[++_list_back] = m;
- } else {
- _visitor->examine(e);
- }
- }
- return n;
- }
-
- /// \brief Processes the next node.
- ///
- /// Processes the next node. And checks that the given target node
- /// is reached. If the target node is reachable from the processed
- /// node then the reached parameter will be set true. The reached
- /// parameter should be initially false.
- ///
- /// \param target The target node.
- /// \retval reach Indicates that the target node is reached.
- /// \return The processed node.
- ///
- /// \warning The queue must not be empty!
- Node processNextNode(Node target, bool& reach) {
- Node n = _list[++_list_front];
- _visitor->process(n);
- Edge e;
- for (_graph->firstOut(e, n); e != INVALID; _graph->nextOut(e)) {
- Node m = _graph->target(e);
- if (!(*_reached)[m]) {
- _visitor->discover(e);
- _visitor->reach(m);
- _reached->set(m, true);
- _list[++_list_back] = m;
- reach = reach || (target == m);
- } else {
- _visitor->examine(e);
- }
- }
- return n;
- }
-
- /// \brief Processes the next node.
- ///
- /// Processes the next node. And checks that at least one of
- /// reached node has true value in the \c nm node map. If one node
- /// with true value is reachable from the processed node then the
- /// rnode parameter will be set to the first of such nodes.
- ///
- /// \param nm The node map of possible targets.
- /// \retval rnode The reached target node.
- /// \return The processed node.
- ///
- /// \warning The queue must not be empty!
- template <typename NM>
- Node processNextNode(const NM& nm, Node& rnode) {
- Node n = _list[++_list_front];
- _visitor->process(n);
- Edge e;
- for (_graph->firstOut(e, n); e != INVALID; _graph->nextOut(e)) {
- Node m = _graph->target(e);
- if (!(*_reached)[m]) {
- _visitor->discover(e);
- _visitor->reach(m);
- _reached->set(m, true);
- _list[++_list_back] = m;
- if (nm[m] && rnode == INVALID) rnode = m;
- } else {
- _visitor->examine(e);
- }
- }
- return n;
- }
-
- /// \brief Next node to be processed.
- ///
- /// Next node to be processed.
- ///
- /// \return The next node to be processed or INVALID if the stack is
- /// empty.
- Node nextNode() {
- return _list_front != _list_back ? _list[_list_front + 1] : INVALID;
- }
-
- /// \brief Returns \c false if there are nodes
- /// to be processed in the queue
- ///
- /// Returns \c false if there are nodes
- /// to be processed in the queue
- bool emptyQueue() { return _list_front == _list_back; }
-
- /// \brief Returns the number of the nodes to be processed.
- ///
- /// Returns the number of the nodes to be processed in the queue.
- int queueSize() { return _list_back - _list_front; }
-
- /// \brief Executes the algorithm.
- ///
- /// Executes the algorithm.
- ///
- /// \pre init() must be called and at least one node should be added
- /// with addSource() before using this function.
- void start() {
- while ( !emptyQueue() ) processNextNode();
- }
-
- /// \brief Executes the algorithm until \c dest is reached.
- ///
- /// Executes the algorithm until \c dest is reached.
- ///
- /// \pre init() must be called and at least one node should be added
- /// with addSource() before using this function.
- void start(Node dest) {
- bool reach = false;
- while ( !emptyQueue() && !reach ) processNextNode(dest, reach);
- }
-
- /// \brief Executes the algorithm until a condition is met.
- ///
- /// Executes the algorithm until a condition is met.
- ///
- /// \pre init() must be called and at least one node should be added
- /// with addSource() before using this function.
- ///
- ///\param nm must be a bool (or convertible) node map. The
- ///algorithm will stop when it reaches a node \c v with
- /// <tt>nm[v]</tt> true.
- ///
- ///\return The reached node \c v with <tt>nm[v]</tt> true or
- ///\c INVALID if no such node was found.
- template <typename NM>
- Node start(const NM &nm) {
- Node rnode = INVALID;
- while ( !emptyQueue() && rnode == INVALID ) {
- processNextNode(nm, rnode);
- }
- return rnode;
- }
-
- /// \brief Runs %BFSVisit algorithm from node \c s.
- ///
- /// This method runs the %BFS algorithm from a root node \c s.
- /// \note b.run(s) is just a shortcut of the following code.
- ///\code
- /// b.init();
- /// b.addSource(s);
- /// b.start();
- ///\endcode
- void run(Node s) {
- init();
- addSource(s);
- start();
- }
-
- /// \brief Runs %BFSVisit algorithm to visit all nodes in the graph.
- ///
- /// This method runs the %BFS algorithm in order to
- /// compute the %BFS path to each node. The algorithm computes
- /// - The %BFS tree.
- /// - The distance of each node from the root in the %BFS tree.
- ///
- ///\note b.run() is just a shortcut of the following code.
- ///\code
- /// b.init();
- /// for (NodeIt it(graph); it != INVALID; ++it) {
- /// if (!b.reached(it)) {
- /// b.addSource(it);
- /// b.start();
- /// }
- /// }
- ///\endcode
- void run() {
- init();
- for (NodeIt it(*_graph); it != INVALID; ++it) {
- if (!reached(it)) {
- addSource(it);
- start();
- }
- }
- }
- ///@}
-
- /// \name Query Functions
- /// The result of the %BFS algorithm can be obtained using these
- /// functions.\n
- /// Before the use of these functions,
- /// either run() or start() must be called.
- ///@{
-
- /// \brief Checks if a node is reachable from the root.
- ///
- /// Returns \c true if \c v is reachable from the root(s).
- /// \warning The source nodes are inditated as unreachable.
- /// \pre Either \ref run() or \ref start()
- /// must be called before using this function.
- ///
- bool reached(Node v) { return (*_reached)[v]; }
- ///@}
- };
-
-} //END OF NAMESPACE LEMON
-
-#endif
-
diff --git a/src/lemon/bin_heap.h b/src/lemon/bin_heap.h
deleted file mode 100644
index 63e0c08..0000000
--- a/src/lemon/bin_heap.h
+++ /dev/null
@@ -1,346 +0,0 @@
-/* -*- C++ -*-
- *
- * This file is a part of LEMON, a generic C++ optimization library
- *
- * Copyright (C) 2003-2008
- * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
- * (Egervary Research Group on Combinatorial Optimization, EGRES).
- *
- * Permission to use, modify and distribute this software is granted
- * provided that this copyright notice appears in all copies. For
- * precise terms see the accompanying LICENSE file.
- *
- * This software is provided "AS IS" with no warranty of any kind,
- * express or implied, and with no claim as to its suitability for any
- * purpose.
- *
- */
-
-#ifndef LEMON_BIN_HEAP_H
-#define LEMON_BIN_HEAP_H
-
-///\ingroup auxdat
-///\file
-///\brief Binary Heap implementation.
-
-#include <vector>
-#include <utility>
-#include <functional>
-
-namespace lemon {
-
- ///\ingroup auxdat
- ///
- ///\brief A Binary Heap implementation.
- ///
- ///This class implements the \e binary \e heap data structure. A \e heap
- ///is a data structure for storing items with specified values called \e
- ///priorities in such a way that finding the item with minimum priority is
- ///efficient. \c Compare specifies the ordering of the priorities. In a heap
- ///one can change the priority of an item, add or erase an item, etc.
- ///
- ///\param _Prio Type of the priority of the items.
- ///\param _ItemIntMap A read and writable Item int map, used internally
- ///to handle the cross references.
- ///\param _Compare A class for the ordering of the priorities. The
- ///default is \c std::less<_Prio>.
- ///
- ///\sa FibHeap
- ///\sa Dijkstra
- template <typename _Prio, typename _ItemIntMap,
- typename _Compare = std::less<_Prio> >
- class BinHeap {
-
- public:
- ///\e
- typedef _ItemIntMap ItemIntMap;
- ///\e
- typedef _Prio Prio;
- ///\e
- typedef typename ItemIntMap::Key Item;
- ///\e
- typedef std::pair<Item,Prio> Pair;
- ///\e
- typedef _Compare Compare;
-
- /// \brief Type to represent the items states.
- ///
- /// Each Item element have a state associated to it. It may be "in heap",
- /// "pre heap" or "post heap". The latter two are indifferent from the
- /// heap's point of view, but may be useful to the user.
- ///
- /// The ItemIntMap \e should be initialized in such way that it maps
- /// PRE_HEAP (-1) to any element to be put in the heap...
- enum State {
- IN_HEAP = 0,
- PRE_HEAP = -1,
- POST_HEAP = -2
- };
-
- private:
- std::vector<Pair> data;
- Compare comp;
- ItemIntMap &iim;
-
- public:
- /// \brief The constructor.
- ///
- /// The constructor.
- /// \param _iim should be given to the constructor, since it is used
- /// internally to handle the cross references. The value of the map
- /// should be PRE_HEAP (-1) for each element.
- explicit BinHeap(ItemIntMap &_iim) : iim(_iim) {}
-
- /// \brief The constructor.
- ///
- /// The constructor.
- /// \param _iim should be given to the constructor, since it is used
- /// internally to handle the cross references. The value of the map
- /// should be PRE_HEAP (-1) for each element.
- ///
- /// \param _comp The comparator function object.
- BinHeap(ItemIntMap &_iim, const Compare &_comp)
- : iim(_iim), comp(_comp) {}
-
-
- /// The number of items stored in the heap.
- ///
- /// \brief Returns the number of items stored in the heap.
- int size() const { return data.size(); }
-
- /// \brief Checks if the heap stores no items.
- ///
- /// Returns \c true if and only if the heap stores no items.
- bool empty() const { return data.empty(); }
-
- /// \brief Make empty this heap.
- ///
- /// Make empty this heap. It does not change the cross reference map.
- /// If you want to reuse what is not surely empty you should first clear
- /// the heap and after that you should set the cross reference map for
- /// each item to \c PRE_HEAP.
- void clear() {
- data.clear();
- }
-
- private:
- static int parent(int i) { return (i-1)/2; }
-
- static int second_child(int i) { return 2*i+2; }
- bool less(const Pair &p1, const Pair &p2) const {
- return comp(p1.second, p2.second);
- }
-
- int bubble_up(int hole, Pair p) {
- int par = parent(hole);
- while( hole>0 && less(p,data[par]) ) {
- move(data[par],hole);
- hole = par;
- par = parent(hole);
- }
- move(p, hole);
- return hole;
- }
-
- int bubble_down(int hole, Pair p, int length) {
- int child = second_child(hole);
- while(child < length) {
- if( less(data[child-1], data[child]) ) {
- --child;
- }
- if( !less(data[child], p) )
- goto ok;
- move(data[child], hole);
- hole = child;
- child = second_child(hole);
- }
- child--;
- if( child<length && less(data[child], p) ) {
- move(data[child], hole);
- hole=child;
- }
- ok:
- move(p, hole);
- return hole;
- }
-
- void move(const Pair &p, int i) {
- data[i] = p;
- iim.set(p.first, i);
- }
-
- public:
- /// \brief Insert a pair of item and priority into the heap.
- ///
- /// Adds \c p.first to the heap with priority \c p.second.
- /// \param p The pair to insert.
- void push(const Pair &p) {
- int n = data.size();
- data.resize(n+1);
- bubble_up(n, p);
- }
-
- /// \brief Insert an item into the heap with the given heap.
- ///
- /// Adds \c i to the heap with priority \c p.
- /// \param i The item to insert.
- /// \param p The priority of the item.
- void push(const Item &i, const Prio &p) { push(Pair(i,p)); }
-
- /// \brief Returns the item with minimum priority relative to \c Compare.
- ///
- /// This method returns the item with minimum priority relative to \c
- /// Compare.
- /// \pre The heap must be nonempty.
- Item top() const {
- return data[0].first;
- }
-
- /// \brief Returns the minimum priority relative to \c Compare.
- ///
- /// It returns the minimum priority relative to \c Compare.
- /// \pre The heap must be nonempty.
- Prio prio() const {
- return data[0].second;
- }
-
- /// \brief Deletes the item with minimum priority relative to \c Compare.
- ///
- /// This method deletes the item with minimum priority relative to \c
- /// Compare from the heap.
- /// \pre The heap must be non-empty.
- void pop() {
- int n = data.size()-1;
- iim.set(data[0].first, POST_HEAP);
- if (n > 0) {
- bubble_down(0, data[n], n);
- }
- data.pop_back();
- }
-
- /// \brief Deletes \c i from the heap.
- ///
- /// This method deletes item \c i from the heap.
- /// \param i The item to erase.
- /// \pre The item should be in the heap.
- void erase(const Item &i) {
- int h = iim[i];
- int n = data.size()-1;
- iim.set(data[h].first, POST_HEAP);
- if( h < n ) {
- if ( bubble_up(h, data[n]) == h) {
- bubble_down(h, data[n], n);
- }
- }
- data.pop_back();
- }
-
-
- /// \brief Returns the priority of \c i.
- ///
- /// This function returns the priority of item \c i.
- /// \pre \c i must be in the heap.
- /// \param i The item.
- Prio operator[](const Item &i) const {
- int idx = iim[i];
- return data[idx].second;
- }
-
- /// \brief \c i gets to the heap with priority \c p independently
- /// if \c i was already there.
- ///
- /// This method calls \ref push(\c i, \c p) if \c i is not stored
- /// in the heap and sets the priority of \c i to \c p otherwise.
- /// \param i The item.
- /// \param p The priority.
- void set(const Item &i, const Prio &p) {
- int idx = iim[i];
- if( idx < 0 ) {
- push(i,p);
- }
- else if( comp(p, data[idx].second) ) {
- bubble_up(idx, Pair(i,p));
- }
- else {
- bubble_down(idx, Pair(i,p), data.size());
- }
- }
-
- /// \brief Decreases the priority of \c i to \c p.
- ///
- /// This method decreases the priority of item \c i to \c p.
- /// \pre \c i must be stored in the heap with priority at least \c
- /// p relative to \c Compare.
- /// \param i The item.
- /// \param p The priority.
- void decrease(const Item &i, const Prio &p) {
- int idx = iim[i];
- bubble_up(idx, Pair(i,p));
- }
-
- /// \brief Increases the priority of \c i to \c p.
- ///
- /// This method sets the priority of item \c i to \c p.
- /// \pre \c i must be stored in the heap with priority at most \c
- /// p relative to \c Compare.
- /// \param i The item.
- /// \param p The priority.
- void increase(const Item &i, const Prio &p) {
- int idx = iim[i];
- bubble_down(idx, Pair(i,p), data.size());
- }
-
- /// \brief Returns if \c item is in, has already been in, or has
- /// never been in the heap.
- ///
- /// This method returns PRE_HEAP if \c item has never been in the
- /// heap, IN_HEAP if it is in the heap at the moment, and POST_HEAP
- /// otherwise. In the latter case it is possible that \c item will
- /// get back to the heap again.
- /// \param i The item.
- State state(const Item &i) const {
- int s = iim[i];
- if( s>=0 )
- s=0;
- return State(s);
- }
-
- /// \brief Sets the state of the \c item in the heap.
- ///
- /// Sets the state of the \c item in the heap. It can be used to
- /// manually clear the heap when it is important to achive the
- /// better time complexity.
- /// \param i The item.
- /// \param st The state. It should not be \c IN_HEAP.
- void state(const Item& i, State st) {
- switch (st) {
- case POST_HEAP:
- case PRE_HEAP:
- if (state(i) == IN_HEAP) {
- erase(i);
- }
- iim[i] = st;
- break;
- case IN_HEAP:
- break;
- }
- }
-
- /// \brief Replaces an item in the heap.
- ///
- /// The \c i item is replaced with \c j item. The \c i item should
- /// be in the heap, while the \c j should be out of the heap. The
- /// \c i item will out of the heap and \c j will be in the heap
- /// with the same prioriority as prevoiusly the \c i item.
- void replace(const Item& i, const Item& j) {
- int idx = iim[i];
- iim.set(i, iim[j]);
- iim.set(j, idx);
- data[idx].first = j;
- }
-
- }; // class BinHeap
-
-} // namespace lemon
-
-#endif // LEMON_BIN_HEAP_H
diff --git a/src/lemon/bipartite_matching.h b/src/lemon/bipartite_matching.h
deleted file mode 100644
index ce3cb4f..0000000
--- a/src/lemon/bipartite_matching.h
+++ /dev/null
@@ -1,1732 +0,0 @@
-/* -*- C++ -*-
- *
- * This file is a part of LEMON, a generic C++ optimization library
- *
- * Copyright (C) 2003-2008
- * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
- * (Egervary Research Group on Combinatorial Optimization, EGRES).
- *
- * Permission to use, modify and distribute this software is granted
- * provided that this copyright notice appears in all copies. For
- * precise terms see the accompanying LICENSE file.
- *
- * This software is provided "AS IS" with no warranty of any kind,
- * express or implied, and with no claim as to its suitability for any
- * purpose.
- *
- */
-
-#ifndef LEMON_BIPARTITE_MATCHING
-#define LEMON_BIPARTITE_MATCHING
-
-#include <functional>
-
-#include <lemon/bin_heap.h>
-#include <lemon/fib_heap.h>
-#include <lemon/maps.h>
-
-#include <iostream>
-
-///\ingroup matching
-///\file
-///\brief Maximum matching algorithms in bipartite graphs.
-///
-///\note The pr_bipartite_matching.h file also contains algorithms to
-///solve maximum cardinality bipartite matching problems.
-
-namespace lemon {
-
- /// \ingroup matching
- ///
- /// \brief Bipartite Max Cardinality Matching algorithm
- ///
- /// Bipartite Max Cardinality Matching algorithm. This class implements
- /// the Hopcroft-Karp algorithm which has \f$ O(e\sqrt{n}) \f$ time
- /// complexity.
- ///
- /// \note In several cases the push-relabel based algorithms have
- /// better runtime performance than the augmenting path based ones.
- ///
- /// \see PrBipartiteMatching
- template <typename BpUGraph>
- class MaxBipartiteMatching {
- protected:
-
- typedef BpUGraph Graph;
-
- typedef typename Graph::Node Node;
- typedef typename Graph::ANodeIt ANodeIt;
- typedef typename Graph::BNodeIt BNodeIt;
- typedef typename Graph::UEdge UEdge;
- typedef typename Graph::UEdgeIt UEdgeIt;
- typedef typename Graph::IncEdgeIt IncEdgeIt;
-
- typedef typename BpUGraph::template ANodeMap<UEdge> ANodeMatchingMap;
- typedef typename BpUGraph::template BNodeMap<UEdge> BNodeMatchingMap;
-
-
- public:
-
- /// \brief Constructor.
- ///
- /// Constructor of the algorithm.
- MaxBipartiteMatching(const BpUGraph& graph)
- : _matching(graph), _rmatching(graph), _reached(graph), _graph(&graph) {}
-
- /// \name Execution control
- /// The simplest way to execute the algorithm is to use
- /// one of the member functions called \c run().
- /// \n
- /// If you need more control on the execution,
- /// first you must call \ref init() or one alternative for it.
- /// Finally \ref start() will perform the matching computation or
- /// with step-by-step execution you can augment the solution.
-
- /// @{
-
- /// \brief Initalize the data structures.
- ///
- /// It initalizes the data structures and creates an empty matching.
- void init() {
- for (ANodeIt it(*_graph); it != INVALID; ++it) {
- _matching.set(it, INVALID);
- }
- for (BNodeIt it(*_graph); it != INVALID; ++it) {
- _rmatching.set(it, INVALID);
- _reached.set(it, -1);
- }
- _size = 0;
- _phase = -1;
- }
-
- /// \brief Initalize the data structures.
- ///
- /// It initalizes the data structures and creates a greedy
- /// matching. From this matching sometimes it is faster to get
- /// the matching than from the initial empty matching.
- void greedyInit() {
- _size = 0;
- for (BNodeIt it(*_graph); it != INVALID; ++it) {
- _rmatching.set(it, INVALID);
- _reached.set(it, 0);
- }
- for (ANodeIt it(*_graph); it != INVALID; ++it) {
- _matching[it] = INVALID;
- for (IncEdgeIt jt(*_graph, it); jt != INVALID; ++jt) {
- if (_rmatching[_graph->bNode(jt)] == INVALID) {
- _matching.set(it, jt);
- _rmatching.set(_graph->bNode(jt), jt);
- _reached.set(_graph->bNode(jt), -1);
- ++_size;
- break;
- }
- }
- }
- _phase = 0;
- }
-
- /// \brief Initalize the data structures with an initial matching.
- ///
- /// It initalizes the data structures with an initial matching.
- template <typename MatchingMap>
- void matchingInit(const MatchingMap& mm) {
- for (ANodeIt it(*_graph); it != INVALID; ++it) {
- _matching.set(it, INVALID);
- }
- for (BNodeIt it(*_graph); it != INVALID; ++it) {
- _rmatching.set(it, INVALID);
- _reached.set(it, 0);
- }
- _size = 0;
- for (UEdgeIt it(*_graph); it != INVALID; ++it) {
- if (mm[it]) {
- ++_size;
- _matching.set(_graph->aNode(it), it);
- _rmatching.set(_graph->bNode(it), it);
- _reached.set(_graph->bNode(it), 0);
- }
- }
- _phase = 0;
- }
-
- /// \brief Initalize the data structures with an initial matching.
- ///
- /// It initalizes the data structures with an initial matching.
- /// \return %True when the given map contains really a matching.
- template <typename MatchingMap>
- bool checkedMatchingInit(const MatchingMap& mm) {
- for (ANodeIt it(*_graph); it != INVALID; ++it) {
- _matching.set(it, INVALID);
- }
- for (BNodeIt it(*_graph); it != INVALID; ++it) {
- _rmatching.set(it, INVALID);
- _reached.set(it, 0);
- }
- _size = 0;
- for (UEdgeIt it(*_graph); it != INVALID; ++it) {
- if (mm[it]) {
- ++_size;
- if (_matching[_graph->aNode(it)] != INVALID) {
- return false;
- }
- _matching.set(_graph->aNode(it), it);
- if (_matching[_graph->bNode(it)] != INVALID) {
- return false;
- }
- _matching.set(_graph->bNode(it), it);
- _reached.set(_graph->bNode(it), -1);
- }
- }
- _phase = 0;
- return true;
- }
-
- private:
-
- bool _find_path(Node anode, int maxlevel,
- typename Graph::template BNodeMap<int>& level) {
- for (IncEdgeIt it(*_graph, anode); it != INVALID; ++it) {
- Node bnode = _graph->bNode(it);
- if (level[bnode] == maxlevel) {
- level.set(bnode, -1);
- if (maxlevel == 0) {
- _matching.set(anode, it);
- _rmatching.set(bnode, it);
- return true;
- } else {
- Node nnode = _graph->aNode(_rmatching[bnode]);
- if (_find_path(nnode, maxlevel - 1, level)) {
- _matching.set(anode, it);
- _rmatching.set(bnode, it);
- return true;
- }
- }
- }
- }
- return false;
- }
-
- public:
-
- /// \brief An augmenting phase of the Hopcroft-Karp algorithm
- ///
- /// It runs an augmenting phase of the Hopcroft-Karp
- /// algorithm. This phase finds maximal edge disjoint augmenting
- /// paths and augments on these paths. The algorithm consists at
- /// most of \f$ O(\sqrt{n}) \f$ phase and one phase is \f$ O(e)
- /// \f$ long.
- bool augment() {
-
- ++_phase;
-
- typename Graph::template BNodeMap<int> _level(*_graph, -1);
- //typename Graph::template ANodeMap<int> _found(*_graph, false);
- typename Graph::template ANodeMap<bool> _found(*_graph, false);
- std::vector<Node> queue, aqueue;
- for (BNodeIt it(*_graph); it != INVALID; ++it) {
- if (_rmatching[it] == INVALID) {
- queue.push_back(it);
- _reached.set(it, _phase);
- _level.set(it, 0);
- }
- }
-
- bool success = false;
-
- int level = 0;
- //std::vector<Node> nqueue;
- while (!success && !queue.empty()) {
- //nqueue.clear();
- std::vector<Node> nqueue;
- for (int i = 0; i < int(queue.size()); ++i) {
- Node bnode = queue[i];
- for (IncEdgeIt jt(*_graph, bnode); jt != INVALID; ++jt) {
- Node anode = _graph->aNode(jt);
- if (_matching[anode] == INVALID) {
-
- if (!_found[anode]) {
- if (_find_path(anode, level, _level)) {
- ++_size;
- }
- _found.set(anode, true);
- }
- success = true;
- } else {
- Node nnode = _graph->bNode(_matching[anode]);
- if (_reached[nnode] != _phase) {
- _reached.set(nnode, _phase);
- nqueue.push_back(nnode);
- _level.set(nnode, level + 1);
- }
- }
- }
- }
- ++level;
- queue.swap(nqueue);
- }
-
- return success;
- }
- private:
-
- void _find_path_bfs(Node anode,
- typename Graph::template ANodeMap<UEdge>& pred) {
- while (true) {
- UEdge uedge = pred[anode];
- Node bnode = _graph->bNode(uedge);
-
- UEdge nedge = _rmatching[bnode];
-
- _matching.set(anode, uedge);
- _rmatching.set(bnode, uedge);
-
- if (nedge == INVALID) break;
- anode = _graph->aNode(nedge);
- }
- }
-
- public:
-
- /// \brief An augmenting phase with single path augementing
- ///
- /// This phase finds only one augmenting paths and augments on
- /// these paths. The algorithm consists at most of \f$ O(n) \f$
- /// phase and one phase is \f$ O(e) \f$ long.
- bool simpleAugment() {
- ++_phase;
-
- typename Graph::template ANodeMap<UEdge> _pred(*_graph);
-
- std::vector<Node> queue, aqueue;
- for (BNodeIt it(*_graph); it != INVALID; ++it) {
- if (_rmatching[it] == INVALID) {
- queue.push_back(it);
- _reached.set(it, _phase);
- }
- }
-
- bool success = false;
-
- int level = 0;
- while (!success && !queue.empty()) {
- std::vector<Node> nqueue;
- for (int i = 0; i < int(queue.size()); ++i) {
- Node bnode = queue[i];
- for (IncEdgeIt jt(*_graph, bnode); jt != INVALID; ++jt) {
- Node anode = _graph->aNode(jt);
- if (_matching[anode] == INVALID) {
- _pred.set(anode, jt);
- _find_path_bfs(anode, _pred);
- ++_size;
- return true;
- } else {
- Node nnode = _graph->bNode(_matching[anode]);
- if (_reached[nnode] != _phase) {
- _pred.set(anode, jt);
- _reached.set(nnode, _phase);
- nqueue.push_back(nnode);
- }
- }
- }
- }
- ++level;
- queue.swap(nqueue);
- }
-
- return success;
- }
-
-
-
- /// \brief Starts the algorithm.
- ///
- /// Starts the algorithm. It runs augmenting phases until the optimal
- /// solution reached.
- void start() {
- while (augment()) {}
- }
-
- /// \brief Runs the algorithm.
- ///
- /// It just initalize the algorithm and then start it.
- void run() {
- greedyInit();
- start();
- }
-
- /// @}
-
- /// \name Query Functions
- /// The result of the %Matching algorithm can be obtained using these
- /// functions.\n
- /// Before the use of these functions,
- /// either run() or start() must be called.
-
- ///@{
-
- /// \brief Return true if the given uedge is in the matching.
- ///
- /// It returns true if the given uedge is in the matching.
- bool matchingEdge(const UEdge& edge) const {
- return _matching[_graph->aNode(edge)] == edge;
- }
-
- /// \brief Returns the matching edge from the node.
- ///
- /// Returns the matching edge from the node. If there is not such
- /// edge it gives back \c INVALID.
- /// \note If the parameter node is a B-node then the running time is
- /// propotional to the degree of the node.
- UEdge matchingEdge(const Node& node) const {
- if (_graph->aNode(node)) {
- return _matching[node];
- } else {
- return _rmatching[node];
- }
- }
-
- /// \brief Set true all matching uedge in the map.
- ///
- /// Set true all matching uedge in the map. It does not change the
- /// value mapped to the other uedges.
- /// \return The number of the matching edges.
- template <typename MatchingMap>
- int quickMatching(MatchingMap& mm) const {
- for (ANodeIt it(*_graph); it != INVALID; ++it) {
- if (_matching[it] != INVALID) {
- mm.set(_matching[it], true);
- }
- }
- return _size;
- }
-
- /// \brief Set true all matching uedge in the map and the others to false.
- ///
- /// Set true all matching uedge in the map and the others to false.
- /// \return The number of the matching edges.
- template <typename MatchingMap>
- int matching(MatchingMap& mm) const {
- for (UEdgeIt it(*_graph); it != INVALID; ++it) {
- mm.set(it, it == _matching[_graph->aNode(it)]);
- }
- return _size;
- }
-
- ///Gives back the matching in an ANodeMap.
-
- ///Gives back the matching in an ANodeMap. The parameter should
- ///be a write ANodeMap of UEdge values.
- ///\return The number of the matching edges.
- template<class MatchingMap>
- int aMatching(MatchingMap& mm) const {
- for (ANodeIt it(*_graph); it != INVALID; ++it) {
- mm.set(it, _matching[it]);
- }
- return _size;
- }
-
- ///Gives back the matching in a BNodeMap.
-
- ///Gives back the matching in a BNodeMap. The parameter should
- ///be a write BNodeMap of UEdge values.
- ///\return The number of the matching edges.
- template<class MatchingMap>
- int bMatching(MatchingMap& mm) const {
- for (BNodeIt it(*_graph); it != INVALID; ++it) {
- mm.set(it, _rmatching[it]);
- }
- return _size;
- }
-
- /// \brief Returns a minimum covering of the nodes.
- ///
- /// The minimum covering set problem is the dual solution of the
- /// maximum bipartite matching. It provides a solution for this
- /// problem what is proof of the optimality of the matching.
- /// \return The size of the cover set.
- template <typename CoverMap>
- int coverSet(CoverMap& covering) const {
-
- int size = 0;
- for (ANodeIt it(*_graph); it != INVALID; ++it) {
- bool cn = _matching[it] != INVALID &&
- _reached[_graph->bNode(_matching[it])] == _phase;
- covering.set(it, cn);
- if (cn) ++size;
- }
- for (BNodeIt it(*_graph); it != INVALID; ++it) {
- bool cn = _reached[it] != _phase;
- covering.set(it, cn);
- if (cn) ++size;
- }
- return size;
- }
-
- /// \brief Gives back a barrier on the A-nodes
- ///
- /// The barrier is s subset of the nodes on the same side of the
- /// graph, which size minus its neighbours is exactly the
- /// unmatched nodes on the A-side.
- /// \retval barrier A WriteMap on the ANodes with bool value.
- template <typename BarrierMap>
- void aBarrier(BarrierMap& barrier) const {
-
- for (ANodeIt it(*_graph); it != INVALID; ++it) {
- barrier.set(it, _matching[it] == INVALID ||
- _reached[_graph->bNode(_matching[it])] != _phase);
- }
- }
-
- /// \brief Gives back a barrier on the B-nodes
- ///
- /// The barrier is s subset of the nodes on the same side of the
- /// graph, which size minus its neighbours is exactly the
- /// unmatched nodes on the B-side.
- /// \retval barrier A WriteMap on the BNodes with bool value.
- template <typename BarrierMap>
- void bBarrier(BarrierMap& barrier) const {
-
- for (BNodeIt it(*_graph); it != INVALID; ++it) {
- barrier.set(it, _reached[it] == _phase);
- }
- }
-
- /// \brief Gives back the number of the matching edges.
- ///
- /// Gives back the number of the matching edges.
- int matchingSize() const {
- return _size;
- }
-
- /// @}
-
- private:
-
- typename BpUGraph::template ANodeMap<UEdge> _matching;
- typename BpUGraph::template BNodeMap<UEdge> _rmatching;
-
- typename BpUGraph::template BNodeMap<int> _reached;
-
- int _phase;
- const Graph *_graph;
-
- int _size;
-
- };
-
- /// \ingroup matching
- ///
- /// \brief Maximum cardinality bipartite matching
- ///
- /// This function calculates the maximum cardinality matching
- /// in a bipartite graph. It gives back the matching in an undirected
- /// edge map.
- ///
- /// \param graph The bipartite graph.
- /// \return The size of the matching.
- template <typename BpUGraph>
- int maxBipartiteMatching(const BpUGraph& graph) {
- MaxBipartiteMatching<BpUGraph> bpmatching(graph);
- bpmatching.run();
- return bpmatching.matchingSize();
- }
-
- /// \ingroup matching
- ///
- /// \brief Maximum cardinality bipartite matching
- ///
- /// This function calculates the maximum cardinality matching
- /// in a bipartite graph. It gives back the matching in an undirected
- /// edge map.
- ///
- /// \param graph The bipartite graph.
- /// \retval matching The ANodeMap of UEdges which will be set to covered
- /// matching undirected edge.
- /// \return The size of the matching.
- template <typename BpUGraph, typename MatchingMap>
- int maxBipartiteMatching(const BpUGraph& graph, MatchingMap& matching) {
- MaxBipartiteMatching<BpUGraph> bpmatching(graph);
- bpmatching.run();
- bpmatching.aMatching(matching);
- return bpmatching.matchingSize();
- }
-
- /// \ingroup matching
- ///
- /// \brief Maximum cardinality bipartite matching
- ///
- /// This function calculates the maximum cardinality matching
- /// in a bipartite graph. It gives back the matching in an undirected
- /// edge map.
- ///
- /// \param graph The bipartite graph.
- /// \retval matching The ANodeMap of UEdges which will be set to covered
- /// matching undirected edge.
- /// \retval barrier The BNodeMap of bools which will be set to a barrier
- /// of the BNode-set.
- /// \return The size of the matching.
- template <typename BpUGraph, typename MatchingMap, typename BarrierMap>
- int maxBipartiteMatching(const BpUGraph& graph,
- MatchingMap& matching, BarrierMap& barrier) {
- MaxBipartiteMatching<BpUGraph> bpmatching(graph);
- bpmatching.run();
- bpmatching.aMatching(matching);
- bpmatching.bBarrier(barrier);
- return bpmatching.matchingSize();
- }
-
- /// \brief Default traits class for weighted bipartite matching algoritms.
- ///
- /// Default traits class for weighted bipartite matching algoritms.
- /// \param _BpUGraph The bipartite undirected graph type.
- /// \param _WeightMap Type of weight map.
- template <typename _BpUGraph, typename _WeightMap>
- struct MaxWeightedBipartiteMatchingDefaultTraits {
- /// \brief The type of the weight of the undirected edges.
- typedef typename _WeightMap::Value Value;
-
- /// The undirected bipartite graph type the algorithm runs on.
- typedef _BpUGraph BpUGraph;
-
- /// The map of the edges weights
- typedef _WeightMap WeightMap;
-
- /// \brief The cross reference type used by heap.
- ///
- /// The cross reference type used by heap.
- /// Usually it is \c Graph::ANodeMap<int>.
- typedef typename BpUGraph::template ANodeMap<int> HeapCrossRef;
-
- /// \brief Instantiates a HeapCrossRef.
- ///
- /// This function instantiates a \ref HeapCrossRef.
- /// \param graph is the graph, to which we would like to define the
- /// HeapCrossRef.
- static HeapCrossRef *createHeapCrossRef(const BpUGraph &graph) {
- return new HeapCrossRef(graph);
- }
-
- /// \brief The heap type used by weighted matching algorithms.
- ///
- /// The heap type used by weighted matching algorithms. It should
- /// minimize the priorities and the heap's key type is the graph's
- /// anode graph's node.
- ///
- /// \sa BinHeap
- //typedef BinHeap<Value, HeapCrossRef> Heap;
- typedef FibHeap<Value, HeapCrossRef> Heap;
-
- /// \brief Instantiates a Heap.
- ///
- /// This function instantiates a \ref Heap.
- /// \param crossref The cross reference of the heap.
- static Heap *createHeap(HeapCrossRef& crossref) {
- return new Heap(crossref);
- }
-
- };
-
-
- /// \ingroup matching
- ///
- /// \brief Bipartite Max Weighted Matching algorithm
- ///
- /// This class implements the bipartite Max Weighted Matching
- /// algorithm. It uses the successive shortest path algorithm to
- /// calculate the maximum weighted matching in the bipartite
- /// graph. The algorithm can be used also to calculate the maximum
- /// cardinality maximum weighted matching. The time complexity
- /// of the algorithm is \f$ O(ne\log(n)) \f$ with the default binary
- /// heap implementation but this can be improved to
- /// \f$ O(n^2\log(n)+ne) \f$ if we use fibonacci heaps.
- ///
- /// The algorithm also provides a potential function on the nodes
- /// which a dual solution of the matching algorithm and it can be
- /// used to proof the optimality of the given pimal solution.
-#ifdef DOXYGEN
- template <typename _BpUGraph, typename _WeightMap, typename _Traits>
-#else
- template <typename _BpUGraph,
- typename _WeightMap = typename _BpUGraph::template UEdgeMap<int>,
- typename _Traits =
- MaxWeightedBipartiteMatchingDefaultTraits<_BpUGraph, _WeightMap> >
-#endif
- class MaxWeightedBipartiteMatching {
-public:
-
- typedef _Traits Traits;
- typedef typename Traits::BpUGraph BpUGraph;
- typedef typename Traits::WeightMap WeightMap;
- typedef typename Traits::Value Value;
-
-protected:
-
- typedef typename Traits::HeapCrossRef HeapCrossRef;
- typedef typename Traits::Heap Heap;
-
-
- typedef typename BpUGraph::Node Node;
- typedef typename BpUGraph::ANodeIt ANodeIt;
- typedef typename BpUGraph::BNodeIt BNodeIt;
- typedef typename BpUGraph::UEdge UEdge;
- typedef typename BpUGraph::UEdgeIt UEdgeIt;
- typedef typename BpUGraph::IncEdgeIt IncEdgeIt;
-
- typedef typename BpUGraph::template ANodeMap<UEdge> ANodeMatchingMap;
- typedef typename BpUGraph::template BNodeMap<UEdge> BNodeMatchingMap;
-
- typedef typename BpUGraph::template ANodeMap<Value> ANodePotentialMap;
- typedef typename BpUGraph::template BNodeMap<Value> BNodePotentialMap;
-
-
-public:
-
- /// \brief \ref Exception for uninitialized parameters.
- ///
- /// This error represents problems in the initialization
- /// of the parameters of the algorithms.
- class UninitializedParameter : public lemon::UninitializedParameter {
- public:
- virtual const char* what() const throw() {
- return "lemon::MaxWeightedBipartiteMatching::UninitializedParameter";
- }
- };
-
- ///\name Named template parameters
-
- ///@{
-
- template <class H, class CR>
- struct DefHeapTraits : public Traits {
- typedef CR HeapCrossRef;
- typedef H Heap;
- static HeapCrossRef *createHeapCrossRef(const BpUGraph &) {
- throw UninitializedParameter();
- }
- static Heap *createHeap(HeapCrossRef &) {
- throw UninitializedParameter();
- }
- };
-
- /// \brief \ref named-templ-param "Named parameter" for setting heap
- /// and cross reference type
- ///
- /// \ref named-templ-param "Named parameter" for setting heap and cross
- /// reference type
- template <class H, class CR = typename BpUGraph::template NodeMap<int> >
- struct DefHeap
- : public MaxWeightedBipartiteMatching<BpUGraph, WeightMap,
- DefHeapTraits<H, CR> > {
- typedef MaxWeightedBipartiteMatching<BpUGraph, WeightMap,
- DefHeapTraits<H, CR> > Create;
-};
-
-template <class H, class CR>
-struct DefStandardHeapTraits : public Traits {
- typedef CR HeapCrossRef;
- typedef H Heap;
- static HeapCrossRef *createHeapCrossRef(const BpUGraph &graph) {
- return new HeapCrossRef(graph);
- }
- static Heap *createHeap(HeapCrossRef &crossref) {
- return new Heap(crossref);
- }
-};
-
-/// \brief \ref named-templ-param "Named parameter" for setting heap and
-/// cross reference type with automatic allocation
-///
-/// \ref named-templ-param "Named parameter" for setting heap and cross
-/// reference type. It can allocate the heap and the cross reference
-/// object if the cross reference's constructor waits for the graph as
-/// parameter and the heap's constructor waits for the cross reference.
-template <class H, class CR = typename BpUGraph::template NodeMap<int> >
-struct DefStandardHeap
-: public MaxWeightedBipartiteMatching<BpUGraph, WeightMap,
-DefStandardHeapTraits<H, CR> > {
-typedef MaxWeightedBipartiteMatching<BpUGraph, WeightMap,
-DefStandardHeapTraits<H, CR> >
-Create;
-};
-
-///@}
-
-
-/// \brief Constructor.
-///
-/// Constructor of the algorithm.
-MaxWeightedBipartiteMatching(const BpUGraph& _graph,
- const WeightMap& _weight)
-: graph(&_graph), weight(&_weight),
-anode_matching(_graph), bnode_matching(_graph),
-anode_potential(_graph), bnode_potential(_graph),
-_heap_cross_ref(0), local_heap_cross_ref(false),
-_heap(0), local_heap(0) {}
-
-/// \brief Destructor.
-///
-/// Destructor of the algorithm.
-~MaxWeightedBipartiteMatching() {
- destroyStructures();
-}
-
-/// \brief Sets the heap and the cross reference used by algorithm.
-///
-/// Sets the heap and the cross reference used by algorithm.
-/// If you don't use this function before calling \ref run(),
-/// it will allocate one. The destuctor deallocates this
-/// automatically allocated map, of course.
-/// \return \c (*this)
-MaxWeightedBipartiteMatching& heap(Heap& hp, HeapCrossRef &cr) {
- if(local_heap_cross_ref) {
- delete _heap_cross_ref;
- local_heap_cross_ref = false;
- }
- _heap_cross_ref = &cr;
- if(local_heap) {
- delete _heap;
- local_heap = false;
- }
- _heap = &hp;
- return *this;
-}
-
-/// \name Execution control
-/// The simplest way to execute the algorithm is to use
-/// one of the member functions called \c run().
-/// \n
-/// If you need more control on the execution,
-/// first you must call \ref init() or one alternative for it.
-/// Finally \ref start() will perform the matching computation or
-/// with step-by-step execution you can augment the solution.
-
-/// @{
-
-/// \brief Initalize the data structures.
-///
-/// It initalizes the data structures and creates an empty matching.
-void init() {
- initStructures();
- for (ANodeIt it(*graph); it != INVALID; ++it) {
- anode_matching[it] = INVALID;
- anode_potential[it] = 0;
- }
- for (BNodeIt it(*graph); it != INVALID; ++it) {
- bnode_matching[it] = INVALID;
- bnode_potential[it] = 0;
- for (IncEdgeIt jt(*graph, it); jt != INVALID; ++jt) {
- if ((*weight)[jt] > bnode_potential[it]) {
- bnode_potential[it] = (*weight)[jt];
- }
- }
- }
- matching_value = 0;
- matching_size = 0;
-}
-
-
-/// \brief An augmenting phase of the weighted matching algorithm
-///
-/// It runs an augmenting phase of the weighted matching
-/// algorithm. This phase finds the best augmenting path and
-/// augments only on this paths.
-///
-/// The algorithm consists at most
-/// of \f$ O(n) \f$ phase and one phase is \f$ O(n\log(n)+e) \f$
-/// long with Fibonacci heap or \f$ O((n+e)\log(n)) \f$ long
-/// with binary heap.
-/// \param decrease If the given parameter true the matching value
-/// can be decreased in the augmenting phase. If we would like
-/// to calculate the maximum cardinality maximum weighted matching
-/// then we should let the algorithm to decrease the matching
-/// value in order to increase the number of the matching edges.
-bool augment(bool decrease = false) {
-
- typename BpUGraph::template BNodeMap<Value> bdist(*graph);
- typename BpUGraph::template BNodeMap<UEdge> bpred(*graph, INVALID);
-
- Node bestNode = INVALID;
- Value bestValue = 0;
-
- _heap->clear();
- for (ANodeIt it(*graph); it != INVALID; ++it) {
- (*_heap_cross_ref)[it] = Heap::PRE_HEAP;
- }
-
- for (ANodeIt it(*graph); it != INVALID; ++it) {
- if (anode_matching[it] == INVALID) {
- _heap->push(it, 0);
- }
- }
-
- Value bdistMax = 0;
- while (!_heap->empty()) {
- Node anode = _heap->top();
- Value avalue = _heap->prio();
- _heap->pop();
- for (IncEdgeIt jt(*graph, anode); jt != INVALID; ++jt) {
- if (jt == anode_matching[anode]) continue;
- Node bnode = graph->bNode(jt);
- Value bvalue = avalue - (*weight)[jt] +
- anode_potential[anode] + bnode_potential[bnode];
- if (bvalue > bdistMax) {
- bdistMax = bvalue;
- }
- if (bpred[bnode] == INVALID || bvalue < bdist[bnode]) {
- bdist[bnode] = bvalue;
- bpred[bnode] = jt;
- } else continue;
- if (bnode_matching[bnode] != INVALID) {
- Node newanode = graph->aNode(bnode_matching[bnode]);
- switch (_heap->state(newanode)) {
- case Heap::PRE_HEAP:
- _heap->push(newanode, bvalue);
- break;
- case Heap::IN_HEAP:
- if (bvalue < (*_heap)[newanode]) {
- _heap->decrease(newanode, bvalue);
- }
- break;
- case Heap::POST_HEAP:
- break;
- }
- } else {
- if (bestNode == INVALID ||
- bnode_potential[bnode] - bvalue > bestValue) {
- bestValue = bnode_potential[bnode] - bvalue;
- bestNode = bnode;
- }
- }
- }
- }
-
- if (bestNode == INVALID || (!decrease && bestValue < 0)) {
- return false;
- }
-
- matching_value += bestValue;
- ++matching_size;
-
- for (BNodeIt it(*graph); it != INVALID; ++it) {
- if (bpred[it] != INVALID) {
- bnode_potential[it] -= bdist[it];
- } else {
- bnode_potential[it] -= bdistMax;
- }
- }
- for (ANodeIt it(*graph); it != INVALID; ++it) {
- if (anode_matching[it] != INVALID) {
- Node bnode = graph->bNode(anode_matching[it]);
- if (bpred[bnode] != INVALID) {
- anode_potential[it] += bdist[bnode];
- } else {
- anode_potential[it] += bdistMax;
- }
- }
- }
-
- while (bestNode != INVALID) {
- UEdge uedge = bpred[bestNode];
- Node anode = graph->aNode(uedge);
-
- bnode_matching[bestNode] = uedge;
- if (anode_matching[anode] != INVALID) {
- bestNode = graph->bNode(anode_matching[anode]);
- } else {
- bestNode = INVALID;
- }
- anode_matching[anode] = uedge;
- }
-
-
- return true;
-}
-
-/// \brief Starts the algorithm.
-///
-/// Starts the algorithm. It runs augmenting phases until the
-/// optimal solution reached.
-///
-/// \param maxCardinality If the given value is true it will
-/// calculate the maximum cardinality maximum matching instead of
-/// the maximum matching.
-void start(bool maxCardinality = false) {
- while (augment(maxCardinality)) {}
-}
-
-/// \brief Runs the algorithm.
-///
-/// It just initalize the algorithm and then start it.
-///
-/// \param maxCardinality If the given value is true it will
-/// calculate the maximum cardinality maximum matching instead of
-/// the maximum matching.
-void run(bool maxCardinality = false) {
- init();
- start(maxCardinality);
-}
-
-/// @}
-
-/// \name Query Functions
-/// The result of the %Matching algorithm can be obtained using these
-/// functions.\n
-/// Before the use of these functions,
-/// either run() or start() must be called.
-
-///@{
-
-/// \brief Gives back the potential in the NodeMap
-///
-/// Gives back the potential in the NodeMap. The matching is optimal
-/// with the current number of edges if \f$ \pi(a) + \pi(b) - w(ab) = 0 \f$
-/// for each matching edges and \f$ \pi(a) + \pi(b) - w(ab) \ge 0 \f$
-/// for each edges.
-template <typename PotentialMap>
-void potential(PotentialMap& pt) const {
- for (ANodeIt it(*graph); it != INVALID; ++it) {
- pt.set(it, anode_potential[it]);
- }
- for (BNodeIt it(*graph); it != INVALID; ++it) {
- pt.set(it, bnode_potential[it]);
- }
-}
-
-/// \brief Set true all matching uedge in the map.
-///
-/// Set true all matching uedge in the map. It does not change the
-/// value mapped to the other uedges.
-/// \return The number of the matching edges.
-template <typename MatchingMap>
-int quickMatching(MatchingMap& mm) const {
- for (ANodeIt it(*graph); it != INVALID; ++it) {
- if (anode_matching[it] != INVALID) {
- mm.set(anode_matching[it], true);
- }
- }
- return matching_size;
-}
-
-/// \brief Set true all matching uedge in the map and the others to false.
-///
-/// Set true all matching uedge in the map and the others to false.
-/// \return The number of the matching edges.
-template <typename MatchingMap>
-int matching(MatchingMap& mm) const {
- for (UEdgeIt it(*graph); it != INVALID; ++it) {
- mm.set(it, it == anode_matching[graph->aNode(it)]);
- }
- return matching_size;
-}
-
-///Gives back the matching in an ANodeMap.
-
-///Gives back the matching in an ANodeMap. The parameter should
-///be a write ANodeMap of UEdge values.
-///\return The number of the matching edges.
-template<class MatchingMap>
-int aMatching(MatchingMap& mm) const {
- for (ANodeIt it(*graph); it != INVALID; ++it) {
- mm.set(it, anode_matching[it]);
- }
- return matching_size;
-}
-
-///Gives back the matching in a BNodeMap.
-
-///Gives back the matching in a BNodeMap. The parameter should
-///be a write BNodeMap of UEdge values.
-///\return The number of the matching edges.
-template<class MatchingMap>
-int bMatching(MatchingMap& mm) const {
- for (BNodeIt it(*graph); it != INVALID; ++it) {
- mm.set(it, bnode_matching[it]);
- }
- return matching_size;
-}
-
-
-/// \brief Return true if the given uedge is in the matching.
-///
-/// It returns true if the given uedge is in the matching.
-bool matchingEdge(const UEdge& edge) const {
- return anode_matching[graph->aNode(edge)] == edge;
-}
-
-/// \brief Returns the matching edge from the node.
-///
-/// Returns the matching edge from the node. If there is not such
-/// edge it gives back \c INVALID.
-UEdge matchingEdge(const Node& node) const {
- if (graph->aNode(node)) {
- return anode_matching[node];
- } else {
- return bnode_matching[node];
- }
-}
-
-/// \brief Gives back the sum of weights of the matching edges.
-///
-/// Gives back the sum of weights of the matching edges.
-Value matchingValue() const {
- return matching_value;
-}
-
-/// \brief Gives back the number of the matching edges.
-///
-/// Gives back the number of the matching edges.
-int matchingSize() const {
- return matching_size;
-}
-
-/// @}
-
-private:
-
-void initStructures() {
- if (!_heap_cross_ref) {
- local_heap_cross_ref = true;
- _heap_cross_ref = Traits::createHeapCrossRef(*graph);
- }
- if (!_heap) {
- local_heap = true;
- _heap = Traits::createHeap(*_heap_cross_ref);
- }
-}
-
-void destroyStructures() {
- if (local_heap_cross_ref) delete _heap_cross_ref;
- if (local_heap) delete _heap;
-}
-
-
-private:
-
-const BpUGraph *graph;
-const WeightMap* weight;
-
-ANodeMatchingMap anode_matching;
-BNodeMatchingMap bnode_matching;
-
-ANodePotentialMap anode_potential;
-BNodePotentialMap bnode_potential;
-
-Value matching_value;
-int matching_size;
-
-HeapCrossRef *_heap_cross_ref;
-bool local_heap_cross_ref;
-
-Heap *_heap;
-bool local_heap;
-
-};
-
-/// \ingroup matching
-///
-/// \brief Maximum weighted bipartite matching
-///
-/// This function calculates the maximum weighted matching
-/// in a bipartite graph. It gives back the matching in an undirected
-/// edge map.
-///
-/// \param graph The bipartite graph.
-/// \param weight The undirected edge map which contains the weights.
-/// \retval matching The undirected edge map which will be set to
-/// the matching.
-/// \return The value of the matching.
-template <typename BpUGraph, typename WeightMap, typename MatchingMap>
-typename WeightMap::Value
-maxWeightedBipartiteMatching(const BpUGraph& graph, const WeightMap& weight,
- MatchingMap& matching) {
- MaxWeightedBipartiteMatching<BpUGraph, WeightMap>
- bpmatching(graph, weight);
- bpmatching.run();
- bpmatching.matching(matching);
- return bpmatching.matchingValue();
-}
-
-/// \ingroup matching
-///
-/// \brief Maximum weighted maximum cardinality bipartite matching
-///
-/// This function calculates the maximum weighted of the maximum cardinality
-/// matchings of a bipartite graph. It gives back the matching in an
-/// undirected edge map.
-///
-/// \param graph The bipartite graph.
-/// \param weight The undirected edge map which contains the weights.
-/// \retval matching The undirected edge map which will be set to
-/// the matching.
-/// \return The value of the matching.
-template <typename BpUGraph, typename WeightMap, typename MatchingMap>
-typename WeightMap::Value
-maxWeightedMaxBipartiteMatching(const BpUGraph& graph,
- const WeightMap& weight,
- MatchingMap& matching) {
- MaxWeightedBipartiteMatching<BpUGraph, WeightMap>
- bpmatching(graph, weight);
- bpmatching.run(true);
- bpmatching.matching(matching);
- return bpmatching.matchingValue();
-}
-
-/// \brief Default traits class for minimum cost bipartite matching
-/// algoritms.
-///
-/// Default traits class for minimum cost bipartite matching
-/// algoritms.
-///
-/// \param _BpUGraph The bipartite undirected graph
-/// type.
-///
-/// \param _CostMap Type of cost map.
-template <typename _BpUGraph, typename _CostMap>
-struct MinCostMaxBipartiteMatchingDefaultTraits {
- /// \brief The type of the cost of the undirected edges.
- typedef typename _CostMap::Value Value;
-
- /// The undirected bipartite graph type the algorithm runs on.
- typedef _BpUGraph BpUGraph;
-
- /// The map of the edges costs
- typedef _CostMap CostMap;
-
- /// \brief The cross reference type used by heap.
- ///
- /// The cross reference type used by heap.
- /// Usually it is \c Graph::NodeMap<int>.
- typedef typename BpUGraph::template NodeMap<int> HeapCrossRef;
-
- /// \brief Instantiates a HeapCrossRef.
- ///
- /// This function instantiates a \ref HeapCrossRef.
- /// \param graph is the graph, to which we would like to define the
- /// HeapCrossRef.
- static HeapCrossRef *createHeapCrossRef(const BpUGraph &graph) {
- return new HeapCrossRef(graph);
- }
-
- /// \brief The heap type used by costed matching algorithms.
- ///
- /// The heap type used by costed matching algorithms. It should
- /// minimize the priorities and the heap's key type is the graph's
- /// anode graph's node.
- ///
- /// \sa BinHeap
- //typedef BinHeap<Value, HeapCrossRef> Heap;
- typedef FibHeap<Value, HeapCrossRef> Heap;
-
- /// \brief Instantiates a Heap.
- ///
- /// This function instantiates a \ref Heap.
- /// \param crossref The cross reference of the heap.
- static Heap *createHeap(HeapCrossRef& crossref) {
- return new Heap(crossref);
- }
-
-};
-
-
-/// \ingroup matching
-///
-/// \brief Bipartite Min Cost Matching algorithm
-///
-/// This class implements the bipartite Min Cost Matching algorithm.
-/// It uses the successive shortest path algorithm to calculate the
-/// minimum cost maximum matching in the bipartite graph. The time
-/// complexity of the algorithm is \f$ O(ne\log(n)) \f$ with the
-/// default binary heap implementation but this can be improved to
-/// \f$ O(n^2\log(n)+ne) \f$ if we use fibonacci heaps.
-///
-/// The algorithm also provides a potential function on the nodes
-/// which a dual solution of the matching algorithm and it can be
-/// used to proof the optimality of the given pimal solution.
-#ifdef DOXYGEN
-template <typename _BpUGraph, typename _CostMap, typename _Traits>
-#else
-template <typename _BpUGraph,
-typename _CostMap = typename _BpUGraph::template UEdgeMap<int>,
-typename _Traits =
-MinCostMaxBipartiteMatchingDefaultTraits<_BpUGraph, _CostMap> >
-#endif
-class MinCostMaxBipartiteMatching {
-public:
-
-typedef _Traits Traits;
-typedef typename Traits::BpUGraph BpUGraph;
-typedef typename Traits::CostMap CostMap;
-typedef typename Traits::Value Value;
-
-protected:
-
-typedef typename Traits::HeapCrossRef HeapCrossRef;
-typedef typename Traits::Heap Heap;
-
-
-typedef typename BpUGraph::Node Node;
-typedef typename BpUGraph::ANodeIt ANodeIt;
-typedef typename BpUGraph::BNodeIt BNodeIt;
-typedef typename BpUGraph::UEdge UEdge;
-typedef typename BpUGraph::UEdgeIt UEdgeIt;
-typedef typename BpUGraph::IncEdgeIt IncEdgeIt;
-
-typedef typename BpUGraph::template ANodeMap<UEdge> ANodeMatchingMap;
-typedef typename BpUGraph::template BNodeMap<UEdge> BNodeMatchingMap;
-
-typedef typename BpUGraph::template ANodeMap<Value> ANodePotentialMap;
-typedef typename BpUGraph::template BNodeMap<Value> BNodePotentialMap;
-
-
-public:
-
-/// \brief \ref Exception for uninitialized parameters.
-///
-/// This error represents problems in the initialization
-/// of the parameters of the algorithms.
-class UninitializedParameter : public lemon::UninitializedParameter {
-public:
- virtual const char* what() const throw() {
- return "lemon::MinCostMaxBipartiteMatching::UninitializedParameter";
- }
-};
-
-///\name Named template parameters
-
-///@{
-
-template <class H, class CR>
-struct DefHeapTraits : public Traits {
- typedef CR HeapCrossRef;
- typedef H Heap;
- static HeapCrossRef *createHeapCrossRef(const BpUGraph &) {
- throw UninitializedParameter();
- }
- static Heap *createHeap(HeapCrossRef &) {
- throw UninitializedParameter();
- }
-};
-
-/// \brief \ref named-templ-param "Named parameter" for setting heap
-/// and cross reference type
-///
-/// \ref named-templ-param "Named parameter" for setting heap and cross
-/// reference type
-template <class H, class CR = typename BpUGraph::template NodeMap<int> >
-struct DefHeap
-: public MinCostMaxBipartiteMatching<BpUGraph, CostMap,
-DefHeapTraits<H, CR> > {
-typedef MinCostMaxBipartiteMatching<BpUGraph, CostMap,
-DefHeapTraits<H, CR> > Create;
-};
-
-template <class H, class CR>
-struct DefStandardHeapTraits : public Traits {
- typedef CR HeapCrossRef;
- typedef H Heap;
- static HeapCrossRef *createHeapCrossRef(const BpUGraph &graph) {
- return new HeapCrossRef(graph);
- }
- static Heap *createHeap(HeapCrossRef &crossref) {
- return new Heap(crossref);
- }
-};
-
-/// \brief \ref named-templ-param "Named parameter" for setting heap and
-/// cross reference type with automatic allocation
-///
-/// \ref named-templ-param "Named parameter" for setting heap and cross
-/// reference type. It can allocate the heap and the cross reference
-/// object if the cross reference's constructor waits for the graph as
-/// parameter and the heap's constructor waits for the cross reference.
-template <class H, class CR = typename BpUGraph::template NodeMap<int> >
-struct DefStandardHeap
-: public MinCostMaxBipartiteMatching<BpUGraph, CostMap,
-DefStandardHeapTraits<H, CR> > {
-typedef MinCostMaxBipartiteMatching<BpUGraph, CostMap,
-DefStandardHeapTraits<H, CR> >
-Create;
-};
-
-///@}
-
-
-/// \brief Constructor.
-///
-/// Constructor of the algorithm.
-MinCostMaxBipartiteMatching(const BpUGraph& _graph,
- const CostMap& _cost)
-: graph(&_graph), cost(&_cost),
-anode_matching(_graph), bnode_matching(_graph),
-anode_potential(_graph), bnode_potential(_graph),
-_heap_cross_ref(0), local_heap_cross_ref(false),
-_heap(0), local_heap(0) {}
-
-/// \brief Destructor.
-///
-/// Destructor of the algorithm.
-~MinCostMaxBipartiteMatching() {
- destroyStructures();
-}
-
-/// \brief Sets the heap and the cross reference used by algorithm.
-///
-/// Sets the heap and the cross reference used by algorithm.
-/// If you don't use this function before calling \ref run(),
-/// it will allocate one. The destuctor deallocates this
-/// automatically allocated map, of course.
-/// \return \c (*this)
-MinCostMaxBipartiteMatching& heap(Heap& hp, HeapCrossRef &cr) {
- if(local_heap_cross_ref) {
- delete _heap_cross_ref;
- local_heap_cross_ref = false;
- }
- _heap_cross_ref = &cr;
- if(local_heap) {
- delete _heap;
- local_heap = false;
- }
- _heap = &hp;
- return *this;
-}
-
-/// \name Execution control
-/// The simplest way to execute the algorithm is to use
-/// one of the member functions called \c run().
-/// \n
-/// If you need more control on the execution,
-/// first you must call \ref init() or one alternative for it.
-/// Finally \ref start() will perform the matching computation or
-/// with step-by-step execution you can augment the solution.
-
-/// @{
-
-/// \brief Initalize the data structures.
-///
-/// It initalizes the data structures and creates an empty matching.
-void init() {
- initStructures();
- for (ANodeIt it(*graph); it != INVALID; ++it) {
- anode_matching[it] = INVALID;
- anode_potential[it] = 0;
- }
- for (BNodeIt it(*graph); it != INVALID; ++it) {
- bnode_matching[it] = INVALID;
- bnode_potential[it] = 0;
- }
- matching_cost = 0;
- matching_size = 0;
-}
-
-
-/// \brief An augmenting phase of the costed matching algorithm
-///
-/// It runs an augmenting phase of the matching algorithm. The
-/// phase finds the best augmenting path and augments only on this
-/// paths.
-///
-/// The algorithm consists at most
-/// of \f$ O(n) \f$ phase and one phase is \f$ O(n\log(n)+e) \f$
-/// long with Fibonacci heap or \f$ O((n+e)\log(n)) \f$ long
-/// with binary heap.
-bool augment() {
-
- typename BpUGraph::template BNodeMap<Value> bdist(*graph);
- typename BpUGraph::template BNodeMap<UEdge> bpred(*graph, INVALID);
-
- Node bestNode = INVALID;
- Value bestValue = 0;
-
- _heap->clear();
- for (ANodeIt it(*graph); it != INVALID; ++it) {
- (*_heap_cross_ref)[it] = Heap::PRE_HEAP;
- }
-
- for (ANodeIt it(*graph); it != INVALID; ++it) {
- if (anode_matching[it] == INVALID) {
- _heap->push(it, 0);
- }
- }
- Value bdistMax = 0;
-
- while (!_heap->empty()) {
- Node anode = _heap->top();
- Value avalue = _heap->prio();
- _heap->pop();
- for (IncEdgeIt jt(*graph, anode); jt != INVALID; ++jt) {
- if (jt == anode_matching[anode]) continue;
- Node bnode = graph->bNode(jt);
- Value bvalue = avalue + (*cost)[jt] +
- anode_potential[anode] - bnode_potential[bnode];
- if (bvalue > bdistMax) {
- bdistMax = bvalue;
- }
- if (bpred[bnode] == INVALID || bvalue < bdist[bnode]) {
- bdist[bnode] = bvalue;
- bpred[bnode] = jt;
- } else continue;
- if (bnode_matching[bnode] != INVALID) {
- Node newanode = graph->aNode(bnode_matching[bnode]);
- switch (_heap->state(newanode)) {
- case Heap::PRE_HEAP:
- _heap->push(newanode, bvalue);
- break;
- case Heap::IN_HEAP:
- if (bvalue < (*_heap)[newanode]) {
- _heap->decrease(newanode, bvalue);
- }
- break;
- case Heap::POST_HEAP:
- break;
- }
- } else {
- if (bestNode == INVALID ||
- bvalue + bnode_potential[bnode] < bestValue) {
- bestValue = bvalue + bnode_potential[bnode];
- bestNode = bnode;
- }
- }
- }
- }
-
- if (bestNode == INVALID) {
- return false;
- }
-
- matching_cost += bestValue;
- ++matching_size;
-
- for (BNodeIt it(*graph); it != INVALID; ++it) {
- if (bpred[it] != INVALID) {
- bnode_potential[it] += bdist[it];
- } else {
- bnode_potential[it] += bdistMax;
- }
- }
- for (ANodeIt it(*graph); it != INVALID; ++it) {
- if (anode_matching[it] != INVALID) {
- Node bnode = graph->bNode(anode_matching[it]);
- if (bpred[bnode] != INVALID) {
- anode_potential[it] += bdist[bnode];
- } else {
- anode_potential[it] += bdistMax;
- }
- }
- }
-
- while (bestNode != INVALID) {
- UEdge uedge = bpred[bestNode];
- Node anode = graph->aNode(uedge);
-
- bnode_matching[bestNode] = uedge;
- if (anode_matching[anode] != INVALID) {
- bestNode = graph->bNode(anode_matching[anode]);
- } else {
- bestNode = INVALID;
- }
- anode_matching[anode] = uedge;
- }
-
-
- return true;
-}
-
-/// \brief Starts the algorithm.
-///
-/// Starts the algorithm. It runs augmenting phases until the
-/// optimal solution reached.
-void start() {
- while (augment()) {}
-}
-
-/// \brief Runs the algorithm.
-///
-/// It just initalize the algorithm and then start it.
-void run() {
- init();
- start();
-}
-
-/// @}
-
-/// \name Query Functions
-/// The result of the %Matching algorithm can be obtained using these
-/// functions.\n
-/// Before the use of these functions,
-/// either run() or start() must be called.
-
-///@{
-
-/// \brief Gives back the potential in the NodeMap
-///
-/// Gives back the potential in the NodeMap. The matching is optimal
-/// with the current number of edges if \f$ \pi(a) + \pi(b) - w(ab) = 0 \f$
-/// for each matching edges and \f$ \pi(a) + \pi(b) - w(ab) \ge 0 \f$
-/// for each edges.
-template <typename PotentialMap>
-void potential(PotentialMap& pt) const {
- for (ANodeIt it(*graph); it != INVALID; ++it) {
- pt.set(it, anode_potential[it]);
- }
- for (BNodeIt it(*graph); it != INVALID; ++it) {
- pt.set(it, bnode_potential[it]);
- }
-}
-
-/// \brief Set true all matching uedge in the map.
-///
-/// Set true all matching uedge in the map. It does not change the
-/// value mapped to the other uedges.
-/// \return The number of the matching edges.
-template <typename MatchingMap>
-int quickMatching(MatchingMap& mm) const {
- for (ANodeIt it(*graph); it != INVALID; ++it) {
- if (anode_matching[it] != INVALID) {
- mm.set(anode_matching[it], true);
- }
- }
- return matching_size;
-}
-
-/// \brief Set true all matching uedge in the map and the others to false.
-///
-/// Set true all matching uedge in the map and the others to false.
-/// \return The number of the matching edges.
-template <typename MatchingMap>
-int matching(MatchingMap& mm) const {
- for (UEdgeIt it(*graph); it != INVALID; ++it) {
- mm.set(it, it == anode_matching[graph->aNode(it)]);
- }
- return matching_size;
-}
-
-/// \brief Gives back the matching in an ANodeMap.
-///
-/// Gives back the matching in an ANodeMap. The parameter should
-/// be a write ANodeMap of UEdge values.
-/// \return The number of the matching edges.
-template<class MatchingMap>
-int aMatching(MatchingMap& mm) const {
- for (ANodeIt it(*graph); it != INVALID; ++it) {
- mm.set(it, anode_matching[it]);
- }
- return matching_size;
-}
-
-/// \brief Gives back the matching in a BNodeMap.
-///
-/// Gives back the matching in a BNodeMap. The parameter should
-/// be a write BNodeMap of UEdge values.
-/// \return The number of the matching edges.
-template<class MatchingMap>
-int bMatching(MatchingMap& mm) const {
- for (BNodeIt it(*graph); it != INVALID; ++it) {
- mm.set(it, bnode_matching[it]);
- }
- return matching_size;
-}
-
-/// \brief Return true if the given uedge is in the matching.
-///
-/// It returns true if the given uedge is in the matching.
-bool matchingEdge(const UEdge& edge) const {
- return anode_matching[graph->aNode(edge)] == edge;
-}
-
-/// \brief Returns the matching edge from the node.
-///
-/// Returns the matching edge from the node. If there is not such
-/// edge it gives back \c INVALID.
-UEdge matchingEdge(const Node& node) const {
- if (graph->aNode(node)) {
- return anode_matching[node];
- } else {
- return bnode_matching[node];
- }
-}
-
-/// \brief Gives back the sum of costs of the matching edges.
-///
-/// Gives back the sum of costs of the matching edges.
-Value matchingCost() const {
- return matching_cost;
-}
-
-/// \brief Gives back the number of the matching edges.
-///
-/// Gives back the number of the matching edges.
-int matchingSize() const {
- return matching_size;
-}
-
-/// @}
-
-private:
-
-void initStructures() {
- if (!_heap_cross_ref) {
- local_heap_cross_ref = true;
- _heap_cross_ref = Traits::createHeapCrossRef(*graph);
- }
- if (!_heap) {
- local_heap = true;
- _heap = Traits::createHeap(*_heap_cross_ref);
- }
-}
-
-void destroyStructures() {
- if (local_heap_cross_ref) delete _heap_cross_ref;
- if (local_heap) delete _heap;
-}
-
-
-private:
-
-const BpUGraph *graph;
-const CostMap* cost;
-
-ANodeMatchingMap anode_matching;
-BNodeMatchingMap bnode_matching;
-
-ANodePotentialMap anode_potential;
-BNodePotentialMap bnode_potential;
-
-Value matching_cost;
-int matching_size;
-
-HeapCrossRef *_heap_cross_ref;
-bool local_heap_cross_ref;
-
-Heap *_heap;
-bool local_heap;
-
-};
-
-/// \ingroup matching
-///
-/// \brief Minimum cost maximum cardinality bipartite matching
-///
-/// This function calculates the maximum cardinality matching with
-/// minimum cost of a bipartite graph. It gives back the matching in
-/// an undirected edge map.
-///
-/// \param graph The bipartite graph.
-/// \param cost The undirected edge map which contains the costs.
-/// \retval matching The undirected edge map which will be set to
-/// the matching.
-/// \return The cost of the matching.
-template <typename BpUGraph, typename CostMap, typename MatchingMap>
-typename CostMap::Value
-minCostMaxBipartiteMatching(const BpUGraph& graph,
- const CostMap& cost,
- MatchingMap& matching) {
- MinCostMaxBipartiteMatching<BpUGraph, CostMap>
- bpmatching(graph, cost);
- bpmatching.run();
- bpmatching.matching(matching);
- return bpmatching.matchingCost();
-}
-
-}
-
-#endif
diff --git a/src/lemon/bits/alteration_notifier.h b/src/lemon/bits/alteration_notifier.h
deleted file mode 100644
index 9a164bf..0000000
--- a/src/lemon/bits/alteration_notifier.h
+++ /dev/null
@@ -1,485 +0,0 @@
-/* -*- C++ -*-
- *
- * This file is a part of LEMON, a generic C++ optimization library
- *
- * Copyright (C) 2003-2008
- * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
- * (Egervary Research Group on Combinatorial Optimization, EGRES).
- *
- * Permission to use, modify and distribute this software is granted
- * provided that this copyright notice appears in all copies. For
- * precise terms see the accompanying LICENSE file.
- *
- * This software is provided "AS IS" with no warranty of any kind,
- * express or implied, and with no claim as to its suitability for any
- * purpose.
- *
- */
-
-#ifndef LEMON_BITS_ALTERATION_NOTIFIER_H
-#define LEMON_BITS_ALTERATION_NOTIFIER_H
-
-#include <vector>
-#include <list>
-
-#include <lemon/bits/utility.h>
-
-///\ingroup graphbits
-///\file
-///\brief Observer notifier for graph alteration observers.
-
-namespace lemon {
-
- /// \ingroup graphbits
- ///
- /// \brief Notifier class to notify observes about alterations in
- /// a container.
- ///
- /// The simple graph's can be refered as two containers, one node container
- /// and one edge container. But they are not standard containers they
- /// does not store values directly they are just key continars for more
- /// value containers which are the node and edge maps.
- ///
- /// The graph's node and edge sets can be changed as we add or erase
- /// nodes and edges in the graph. Lemon would like to handle easily
- /// that the node and edge maps should contain values for all nodes or
- /// edges. If we want to check on every indicing if the map contains
- /// the current indicing key that cause a drawback in the performance
- /// in the library. We use another solution we notify all maps about
- /// an alteration in the graph, which cause only drawback on the
- /// alteration of the graph.
- ///
- /// This class provides an interface to the container. The \e first() and \e
- /// next() member functions make possible to iterate on the keys of the
- /// container. The \e id() function returns an integer id for each key.
- /// The \e maxId() function gives back an upper bound of the ids.
- ///
- /// For the proper functonality of this class, we should notify it
- /// about each alteration in the container. The alterations have four type
- /// as \e add(), \e erase(), \e build() and \e clear(). The \e add() and
- /// \e erase() signals that only one or few items added or erased to or
- /// from the graph. If all items are erased from the graph or from an empty
- /// graph a new graph is builded then it can be signaled with the
- /// clear() and build() members. Important rule that if we erase items
- /// from graph we should first signal the alteration and after that erase
- /// them from the container, on the other way on item addition we should
- /// first extend the container and just after that signal the alteration.
- ///
- /// The alteration can be observed with a class inherited from the
- /// \e ObserverBase nested class. The signals can be handled with
- /// overriding the virtual functions defined in the base class. The
- /// observer base can be attached to the notifier with the
- /// \e attach() member and can be detached with detach() function. The
- /// alteration handlers should not call any function which signals
- /// an other alteration in the same notifier and should not
- /// detach any observer from the notifier.
- ///
- /// Alteration observers try to be exception safe. If an \e add() or
- /// a \e clear() function throws an exception then the remaining
- /// observeres will not be notified and the fulfilled additions will
- /// be rolled back by calling the \e erase() or \e clear()
- /// functions. Thence the \e erase() and \e clear() should not throw
- /// exception. Actullay, it can be throw only
- /// \ref AlterationObserver::ImmediateDetach ImmediateDetach
- /// exception which detach the observer from the notifier.
- ///
- /// There are some place when the alteration observing is not completly
- /// reliable. If we want to carry out the node degree in the graph
- /// as in the \ref InDegMap and we use the reverseEdge that cause
- /// unreliable functionality. Because the alteration observing signals
- /// only erasing and adding but not the reversing it will stores bad
- /// degrees. The sub graph adaptors cannot signal the alterations because
- /// just a setting in the filter map can modify the graph and this cannot
- /// be watched in any way.
- ///
- /// \param _Container The container which is observed.
- /// \param _Item The item type which is obserbved.
- ///
- /// \author Balazs Dezso
-
- template <typename _Container, typename _Item>
- class AlterationNotifier {
- public:
-
- typedef True Notifier;
-
- typedef _Container Container;
- typedef _Item Item;
-
- /// \brief Exception which can be called from \e clear() and
- /// \e erase().
- ///
- /// From the \e clear() and \e erase() function only this
- /// exception is allowed to throw. The exception immediatly
- /// detaches the current observer from the notifier. Because the
- /// \e clear() and \e erase() should not throw other exceptions
- /// it can be used to invalidate the observer.
- struct ImmediateDetach {};
-
- /// \brief ObserverBase is the base class for the observers.
- ///
- /// ObserverBase is the abstract base class for the observers.
- /// It will be notified about an item was inserted into or
- /// erased from the graph.
- ///
- /// The observer interface contains some pure virtual functions
- /// to override. The add() and erase() functions are
- /// to notify the oberver when one item is added or
- /// erased.
- ///
- /// The build() and clear() members are to notify the observer
- /// about the container is built from an empty container or
- /// is cleared to an empty container.
- ///
- /// \author Balazs Dezso
-
- class ObserverBase {
- protected:
- typedef AlterationNotifier Notifier;
-
- friend class AlterationNotifier;
-
- /// \brief Default constructor.
- ///
- /// Default constructor for ObserverBase.
- ///
- ObserverBase() : _notifier(0) {}
-
- /// \brief Constructor which attach the observer into notifier.
- ///
- /// Constructor which attach the observer into notifier.
- ObserverBase(AlterationNotifier& nf) {
- attach(nf);
- }
-
- /// \brief Constructor which attach the obserever to the same notifier.
- ///
- /// Constructor which attach the obserever to the same notifier as
- /// the other observer is attached to.
- ObserverBase(const ObserverBase& copy) {
- if (copy.attached()) {
- attach(*copy.notifier());
- }
- }
-
- /// \brief Destructor
- virtual ~ObserverBase() {
- if (attached()) {
- detach();
- }
- }
-
- /// \brief Attaches the observer into an AlterationNotifier.
- ///
- /// This member attaches the observer into an AlterationNotifier.
- ///
- void attach(AlterationNotifier& nf) {
- nf.attach(*this);
- }
-
- /// \brief Detaches the observer into an AlterationNotifier.
- ///
- /// This member detaches the observer from an AlterationNotifier.
- ///
- void detach() {
- _notifier->detach(*this);
- }
-
- /// \brief Gives back a pointer to the notifier which the map
- /// attached into.
- ///
- /// This function gives back a pointer to the notifier which the map
- /// attached into.
- ///
- Notifier* notifier() const { return const_cast<Notifier*>(_notifier); }
-
- /// Gives back true when the observer is attached into a notifier.
- bool attached() const { return _notifier != 0; }
-
- private:
-
- ObserverBase& operator=(const ObserverBase& copy);
-
- protected:
-
- Notifier* _notifier;
- typename std::list<ObserverBase*>::iterator _index;
-
- /// \brief The member function to notificate the observer about an
- /// item is added to the container.
- ///
- /// The add() member function notificates the observer about an item
- /// is added to the container. It have to be overrided in the
- /// subclasses.
- virtual void add(const Item&) = 0;
-
- /// \brief The member function to notificate the observer about
- /// more item is added to the container.
- ///
- /// The add() member function notificates the observer about more item
- /// is added to the container. It have to be overrided in the
- /// subclasses.
- virtual void add(const std::vector<Item>& items) = 0;
-
- /// \brief The member function to notificate the observer about an
- /// item is erased from the container.
- ///
- /// The erase() member function notificates the observer about an
- /// item is erased from the container. It have to be overrided in
- /// the subclasses.
- virtual void erase(const Item&) = 0;
-
- /// \brief The member function to notificate the observer about
- /// more item is erased from the container.
- ///
- /// The erase() member function notificates the observer about more item
- /// is erased from the container. It have to be overrided in the
- /// subclasses.
- virtual void erase(const std::vector<Item>& items) = 0;
-
- /// \brief The member function to notificate the observer about the
- /// container is built.
- ///
- /// The build() member function notificates the observer about the
- /// container is built from an empty container. It have to be
- /// overrided in the subclasses.
-
- virtual void build() = 0;
-
- /// \brief The member function to notificate the observer about all
- /// items are erased from the container.
- ///
- /// The clear() member function notificates the observer about all
- /// items are erased from the container. It have to be overrided in
- /// the subclasses.
- virtual void clear() = 0;
-
- };
-
- protected:
-
- const Container* container;
-
- typedef std::list<ObserverBase*> Observers;
- Observers _observers;
-
-
- public:
-
- /// \brief Default constructor.
- ///
- /// The default constructor of the AlterationNotifier.
- /// It creates an empty notifier.
- AlterationNotifier()
- : container(0) {}
-
- /// \brief Constructor.
- ///
- /// Constructor with the observed container parameter.
- AlterationNotifier(const Container& _container)
- : container(&_container) {}
-
- /// \brief Copy Constructor of the AlterationNotifier.
- ///
- /// Copy constructor of the AlterationNotifier.
- /// It creates only an empty notifier because the copiable
- /// notifier's observers have to be registered still into that notifier.
- AlterationNotifier(const AlterationNotifier& _notifier)
- : container(_notifier.container) {}
-
- /// \brief Destructor.
- ///
- /// Destructor of the AlterationNotifier.
- ///
- ~AlterationNotifier() {
- typename Observers::iterator it;
- for (it = _observers.begin(); it != _observers.end(); ++it) {
- (*it)->_notifier = 0;
- }
- }
-
- /// \brief Sets the container.
- ///
- /// Sets the container.
- void setContainer(const Container& _container) {
- container = &_container;
- }
-
- protected:
-
- AlterationNotifier& operator=(const AlterationNotifier&);
-
- public:
-
-
-
- /// \brief First item in the container.
- ///
- /// Returns the first item in the container. It is
- /// for start the iteration on the container.
- void first(Item& item) const {
- container->first(item);
- }
-
- /// \brief Next item in the container.
- ///
- /// Returns the next item in the container. It is
- /// for iterate on the container.
- void next(Item& item) const {
- container->next(item);
- }
-
- /// \brief Returns the id of the item.
- ///
- /// Returns the id of the item provided by the container.
- int id(const Item& item) const {
- return container->id(item);
- }
-
- /// \brief Returns the maximum id of the container.
- ///
- /// Returns the maximum id of the container.
- int maxId() const {
- return container->maxId(Item());
- }
-
- protected:
-
- void attach(ObserverBase& observer) {
- observer._index = _observers.insert(_observers.begin(), &observer);
- observer._notifier = this;
- }
-
- void detach(ObserverBase& observer) {
- _observers.erase(observer._index);
- observer._index = _observers.end();
- observer._notifier = 0;
- }
-
- public:
-
- /// \brief Notifies all the registed observers about an item added to
- /// the container.
- ///
- /// It notifies all the registed observers about an item added to
- /// the container.
- ///
- void add(const Item& item) {
- typename Observers::reverse_iterator it;
- try {
- for (it = _observers.rbegin(); it != _observers.rend(); ++it) {
- (*it)->add(item);
- }
- } catch (...) {
- typename Observers::iterator jt;
- for (jt = it.base(); jt != _observers.end(); ++jt) {
- (*jt)->erase(item);
- }
- throw;
- }
- }
-
- /// \brief Notifies all the registed observers about more item added to
- /// the container.
- ///
- /// It notifies all the registed observers about more item added to
- /// the container.
- ///
- void add(const std::vector<Item>& items) {
- typename Observers::reverse_iterator it;
- try {
- for (it = _observers.rbegin(); it != _observers.rend(); ++it) {
- (*it)->add(items);
- }
- } catch (...) {
- typename Observers::iterator jt;
- for (jt = it.base(); jt != _observers.end(); ++jt) {
- (*jt)->erase(items);
- }
- throw;
- }
- }
-
- /// \brief Notifies all the registed observers about an item erased from
- /// the container.
- ///
- /// It notifies all the registed observers about an item erased from
- /// the container.
- ///
- void erase(const Item& item) throw() {
- typename Observers::iterator it = _observers.begin();
- while (it != _observers.end()) {
- try {
- (*it)->erase(item);
- ++it;
- } catch (const ImmediateDetach&) {
- it = _observers.erase(it);
- (*it)->_index = _observers.end();
- (*it)->_notifier = 0;
- }
- }
- }
-
- /// \brief Notifies all the registed observers about more item erased
- /// from the container.
- ///
- /// It notifies all the registed observers about more item erased from
- /// the container.
- ///
- void erase(const std::vector<Item>& items) {
- typename Observers::iterator it = _observers.begin();
- while (it != _observers.end()) {
- try {
- (*it)->erase(items);
- ++it;
- } catch (const ImmediateDetach&) {
- it = _observers.erase(it);
- (*it)->_index = _observers.end();
- (*it)->_notifier = 0;
- }
- }
- }
-
- /// \brief Notifies all the registed observers about the container is
- /// built.
- ///
- /// Notifies all the registed observers about the container is built
- /// from an empty container.
- void build() {
- typename Observers::reverse_iterator it;
- try {
- for (it = _observers.rbegin(); it != _observers.rend(); ++it) {
- (*it)->build();
- }
- } catch (...) {
- typename Observers::iterator jt;
- for (jt = it.base(); jt != _observers.end(); ++jt) {
- (*jt)->clear();
- }
- throw;
- }
- }
-
- /// \brief Notifies all the registed observers about all items are
- /// erased.
- ///
- /// Notifies all the registed observers about all items are erased
- /// from the container.
- void clear() {
- typename Observers::iterator it = _observers.begin();
- while (it != _observers.end()) {
- try {
- (*it)->clear();
- ++it;
- } catch (const ImmediateDetach&) {
- it = _observers.erase(it);
- (*it)->_index = _observers.end();
- (*it)->_notifier = 0;
- }
- }
- }
- };
-
-}
-
-#endif
diff --git a/src/lemon/bits/array_map.h b/src/lemon/bits/array_map.h
deleted file mode 100644
index 08ab218..0000000
--- a/src/lemon/bits/array_map.h
+++ /dev/null
@@ -1,346 +0,0 @@
-/* -*- C++ -*-
- *
- * This file is a part of LEMON, a generic C++ optimization library
- *
- * Copyright (C) 2003-2008
- * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
- * (Egervary Research Group on Combinatorial Optimization, EGRES).
- *
- * Permission to use, modify and distribute this software is granted
- * provided that this copyright notice appears in all copies. For
- * precise terms see the accompanying LICENSE file.
- *
- * This software is provided "AS IS" with no warranty of any kind,
- * express or implied, and with no claim as to its suitability for any
- * purpose.
- *
- */
-
-#ifndef LEMON_BITS_ARRAY_MAP_H
-#define LEMON_BITS_ARRAY_MAP_H
-
-#include <memory>
-
-#include <lemon/bits/traits.h>
-#include <lemon/bits/alteration_notifier.h>
-#include <lemon/concept_check.h>
-#include <lemon/concepts/maps.h>
-
-/// \ingroup graphbits
-/// \file
-/// \brief Graph map based on the array storage.
-
-namespace lemon {
-
- /// \ingroup graphbits
- ///
- /// \brief Graph map based on the array storage.
- ///
- /// The ArrayMap template class is graph map structure what
- /// automatically updates the map when a key is added to or erased from
- /// the map. This map uses the allocators to implement
- /// the container functionality.
- ///
- /// The template parameters are the Graph the current Item type and
- /// the Value type of the map.
- template <typename _Graph, typename _Item, typename _Value>
- class ArrayMap
- : public ItemSetTraits<_Graph, _Item>::ItemNotifier::ObserverBase {
- public:
- /// The graph type of the maps.
- typedef _Graph Graph;
- /// The item type of the map.
- typedef _Item Item;
- /// The reference map tag.
- typedef True ReferenceMapTag;
-
- /// The key type of the maps.
- typedef _Item Key;
- /// The value type of the map.
- typedef _Value Value;
-
- /// The const reference type of the map.
- typedef const _Value& ConstReference;
- /// The reference type of the map.
- typedef _Value& Reference;
-
- /// The notifier type.
- typedef typename ItemSetTraits<_Graph, _Item>::ItemNotifier Notifier;
-
- /// The MapBase of the Map which imlements the core regisitry function.
- typedef typename Notifier::ObserverBase Parent;
-
- private:
- typedef std::allocator<Value> Allocator;
-
- public:
-
- /// \brief Graph initialized map constructor.
- ///
- /// Graph initialized map constructor.
- explicit ArrayMap(const Graph& graph) {
- Parent::attach(graph.notifier(Item()));
- allocate_memory();
- Notifier* nf = Parent::notifier();
- Item it;
- for (nf->first(it); it != INVALID; nf->next(it)) {
- int id = nf->id(it);;
- allocator.construct(&(values[id]), Value());
- }
- }
-
- /// \brief Constructor to use default value to initialize the map.
- ///
- /// It constructs a map and initialize all of the the map.
- ArrayMap(const Graph& graph, const Value& value) {
- Parent::attach(graph.notifier(Item()));
- allocate_memory();
- Notifier* nf = Parent::notifier();
- Item it;
- for (nf->first(it); it != INVALID; nf->next(it)) {
- int id = nf->id(it);;
- allocator.construct(&(values[id]), value);
- }
- }
-
- /// \brief Constructor to copy a map of the same map type.
- ///
- /// Constructor to copy a map of the same map type.
- ArrayMap(const ArrayMap& copy) : Parent() {
- if (copy.attached()) {
- attach(*copy.notifier());
- }
- capacity = copy.capacity;
- if (capacity == 0) return;
- values = allocator.allocate(capacity);
- Notifier* nf = Parent::notifier();
- Item it;
- for (nf->first(it); it != INVALID; nf->next(it)) {
- int id = nf->id(it);;
- allocator.construct(&(values[id]), copy.values[id]);
- }
- }
-
- /// \brief Assign operator.
- ///
- /// This operator assigns for each item in the map the
- /// value mapped to the same item in the copied map.
- /// The parameter map should be indiced with the same
- /// itemset because this assign operator does not change
- /// the container of the map.
- ArrayMap& operator=(const ArrayMap& cmap) {
- return operator=<ArrayMap>(cmap);
- }
-
-
- /// \brief Template assign operator.
- ///
- /// The given parameter should be conform to the ReadMap
- /// concecpt and could be indiced by the current item set of
- /// the NodeMap. In this case the value for each item
- /// is assigned by the value of the given ReadMap.
- template <typename CMap>
- ArrayMap& operator=(const CMap& cmap) {
- checkConcept<concepts::ReadMap<Key, _Value>, CMap>();
- const typename Parent::Notifier* nf = Parent::notifier();
- Item it;
- for (nf->first(it); it != INVALID; nf->next(it)) {
- set(it, cmap[it]);
- }
- return *this;
- }
-
- /// \brief The destructor of the map.
- ///
- /// The destructor of the map.
- virtual ~ArrayMap() {
- if (attached()) {
- clear();
- detach();
- }
- }
-
- protected:
-
- using Parent::attach;
- using Parent::detach;
- using Parent::attached;
-
- public:
-
- /// \brief The subscript operator.
- ///
- /// The subscript operator. The map can be subscripted by the
- /// actual keys of the graph.
- Value& operator[](const Key& key) {
- int id = Parent::notifier()->id(key);
- return values[id];
- }
-
- /// \brief The const subscript operator.
- ///
- /// The const subscript operator. The map can be subscripted by the
- /// actual keys of the graph.
- const Value& operator[](const Key& key) const {
- int id = Parent::notifier()->id(key);
- return values[id];
- }
-
- /// \brief Setter function of the map.
- ///
- /// Setter function of the map. Equivalent with map[key] = val.
- /// This is a compatibility feature with the not dereferable maps.
- void set(const Key& key, const Value& val) {
- (*this)[key] = val;
- }
-
- protected:
-
- /// \brief Adds a new key to the map.
- ///
- /// It adds a new key to the map. It called by the observer notifier
- /// and it overrides the add() member function of the observer base.
- virtual void add(const Key& key) {
- Notifier* nf = Parent::notifier();
- int id = nf->id(key);
- if (id >= capacity) {
- int new_capacity = (capacity == 0 ? 1 : capacity);
- while (new_capacity <= id) {
- new_capacity <<= 1;
- }
- Value* new_values = allocator.allocate(new_capacity);
- Item it;
- for (nf->first(it); it != INVALID; nf->next(it)) {
- int jd = nf->id(it);;
- if (id != jd) {
- allocator.construct(&(new_values[jd]), values[jd]);
- allocator.destroy(&(values[jd]));
- }
- }
- if (capacity != 0) allocator.deallocate(values, capacity);
- values = new_values;
- capacity = new_capacity;
- }
- allocator.construct(&(values[id]), Value());
- }
-
- /// \brief Adds more new keys to the map.
- ///
- /// It adds more new keys to the map. It called by the observer notifier
- /// and it overrides the add() member function of the observer base.
- virtual void add(const std::vector<Key>& keys) {
- Notifier* nf = Parent::notifier();
- int max_id = -1;
- for (int i = 0; i < int(keys.size()); ++i) {
- int id = nf->id(keys[i]);
- if (id > max_id) {
- max_id = id;
- }
- }
- if (max_id >= capacity) {
- int new_capacity = (capacity == 0 ? 1 : capacity);
- while (new_capacity <= max_id) {
- new_capacity <<= 1;
- }
- Value* new_values = allocator.allocate(new_capacity);
- Item it;
- for (nf->first(it); it != INVALID; nf->next(it)) {
- int id = nf->id(it);
- bool found = false;
- for (int i = 0; i < int(keys.size()); ++i) {
- int jd = nf->id(keys[i]);
- if (id == jd) {
- found = true;
- break;
- }
- }
- if (found) continue;
- allocator.construct(&(new_values[id]), values[id]);
- allocator.destroy(&(values[id]));
- }
- if (capacity != 0) allocator.deallocate(values, capacity);
- values = new_values;
- capacity = new_capacity;
- }
- for (int i = 0; i < int(keys.size()); ++i) {
- int id = nf->id(keys[i]);
- allocator.construct(&(values[id]), Value());
- }
- }
-
- /// \brief Erase a key from the map.
- ///
- /// Erase a key from the map. It called by the observer notifier
- /// and it overrides the erase() member function of the observer base.
- virtual void erase(const Key& key) {
- int id = Parent::notifier()->id(key);
- allocator.destroy(&(values[id]));
- }
-
- /// \brief Erase more keys from the map.
- ///
- /// Erase more keys from the map. It called by the observer notifier
- /// and it overrides the erase() member function of the observer base.
- virtual void erase(const std::vector<Key>& keys) {
- for (int i = 0; i < int(keys.size()); ++i) {
- int id = Parent::notifier()->id(keys[i]);
- allocator.destroy(&(values[id]));
- }
- }
-
- /// \brief Buildes the map.
- ///
- /// It buildes the map. It called by the observer notifier
- /// and it overrides the build() member function of the observer base.
- virtual void build() {
- Notifier* nf = Parent::notifier();
- allocate_memory();
- Item it;
- for (nf->first(it); it != INVALID; nf->next(it)) {
- int id = nf->id(it);;
- allocator.construct(&(values[id]), Value());
- }
- }
-
- /// \brief Clear the map.
- ///
- /// It erase all items from the map. It called by the observer notifier
- /// and it overrides the clear() member function of the observer base.
- virtual void clear() {
- Notifier* nf = Parent::notifier();
- if (capacity != 0) {
- Item it;
- for (nf->first(it); it != INVALID; nf->next(it)) {
- int id = nf->id(it);
- allocator.destroy(&(values[id]));
- }
- allocator.deallocate(values, capacity);
- capacity = 0;
- }
- }
-
- private:
-
- void allocate_memory() {
- int max_id = Parent::notifier()->maxId();
- if (max_id == -1) {
- capacity = 0;
- values = 0;
- return;
- }
- capacity = 1;
- while (capacity <= max_id) {
- capacity <<= 1;
- }
- values = allocator.allocate(capacity);
- }
-
- int capacity;
- Value* values;
- Allocator allocator;
-
- };
-
-}
-
-#endif
diff --git a/src/lemon/bits/base_extender.h b/src/lemon/bits/base_extender.h
deleted file mode 100644
index b812247..0000000
--- a/src/lemon/bits/base_extender.h
+++ /dev/null
@@ -1,495 +0,0 @@
-/* -*- C++ -*-
- *
- * This file is a part of LEMON, a generic C++ optimization library
- *
- * Copyright (C) 2003-2008
- * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
- * (Egervary Research Group on Combinatorial Optimization, EGRES).
- *
- * Permission to use, modify and distribute this software is granted
- * provided that this copyright notice appears in all copies. For
- * precise terms see the accompanying LICENSE file.
- *
- * This software is provided "AS IS" with no warranty of any kind,
- * express or implied, and with no claim as to its suitability for any
- * purpose.
- *
- */
-
-#ifndef LEMON_BITS_BASE_EXTENDER_H
-#define LEMON_BITS_BASE_EXTENDER_H
-
-#include <lemon/bits/invalid.h>
-#include <lemon/error.h>
-
-#include <lemon/bits/map_extender.h>
-#include <lemon/bits/default_map.h>
-
-#include <lemon/concept_check.h>
-#include <lemon/concepts/maps.h>
-
-///\ingroup graphbits
-///\file
-///\brief Extenders for the graph types
-namespace lemon {
-
- /// \ingroup graphbits
- ///
- /// \brief BaseGraph to BaseUGraph extender
- template <typename Base>
- class UndirGraphExtender : public Base {
-
- public:
-
- typedef Base Parent;
- typedef typename Parent::Edge UEdge;
- typedef typename Parent::Node Node;
-
- typedef True UndirectedTag;
-
- class Edge : public UEdge {
- friend class UndirGraphExtender;
-
- protected:
- bool forward;
-
- Edge(const UEdge &ue, bool _forward) :
- UEdge(ue), forward(_forward) {}
-
- public:
- Edge() {}
-
- /// Invalid edge constructor
- Edge(Invalid i) : UEdge(i), forward(true) {}
-
- bool operator==(const Edge &that) const {
- return forward==that.forward && UEdge(*this)==UEdge(that);
- }
- bool operator!=(const Edge &that) const {
- return forward!=that.forward || UEdge(*this)!=UEdge(that);
- }
- bool operator<(const Edge &that) const {
- return forward<that.forward ||
- (!(that.forward<forward) && UEdge(*this)<UEdge(that));
- }
- };
-
-
-
- using Parent::source;
-
- /// Source of the given Edge.
- Node source(const Edge &e) const {
- return e.forward ? Parent::source(e) : Parent::target(e);
- }
-
- using Parent::target;
-
- /// Target of the given Edge.
- Node target(const Edge &e) const {
- return e.forward ? Parent::target(e) : Parent::source(e);
- }
-
- /// \brief Directed edge from an undirected edge.
- ///
- /// Returns a directed edge corresponding to the specified UEdge.
- /// If the given bool is true the given undirected edge and the
- /// returned edge have the same source node.
- static Edge direct(const UEdge &ue, bool d) {
- return Edge(ue, d);
- }
-
- /// Returns whether the given directed edge is same orientation as the
- /// corresponding undirected edge.
- ///
- /// \todo reference to the corresponding point of the undirected graph
- /// concept. "What does the direction of an undirected edge mean?"
- static bool direction(const Edge &e) { return e.forward; }
-
-
- using Parent::first;
- using Parent::next;
-
- void first(Edge &e) const {
- Parent::first(e);
- e.forward=true;
- }
-
- void next(Edge &e) const {
- if( e.forward ) {
- e.forward = false;
- }
- else {
- Parent::next(e);
- e.forward = true;
- }
- }
-
- void firstOut(Edge &e, const Node &n) const {
- Parent::firstIn(e,n);
- if( UEdge(e) != INVALID ) {
- e.forward = false;
- }
- else {
- Parent::firstOut(e,n);
- e.forward = true;
- }
- }
- void nextOut(Edge &e) const {
- if( ! e.forward ) {
- Node n = Parent::target(e);
- Parent::nextIn(e);
- if( UEdge(e) == INVALID ) {
- Parent::firstOut(e, n);
- e.forward = true;
- }
- }
- else {
- Parent::nextOut(e);
- }
- }
-
- void firstIn(Edge &e, const Node &n) const {
- Parent::firstOut(e,n);
- if( UEdge(e) != INVALID ) {
- e.forward = false;
- }
- else {
- Parent::firstIn(e,n);
- e.forward = true;
- }
- }
- void nextIn(Edge &e) const {
- if( ! e.forward ) {
- Node n = Parent::source(e);
- Parent::nextOut(e);
- if( UEdge(e) == INVALID ) {
- Parent::firstIn(e, n);
- e.forward = true;
- }
- }
- else {
- Parent::nextIn(e);
- }
- }
-
- void firstInc(UEdge &e, bool &d, const Node &n) const {
- d = true;
- Parent::firstOut(e, n);
- if (e != INVALID) return;
- d = false;
- Parent::firstIn(e, n);
- }
-
- void nextInc(UEdge &e, bool &d) const {
- if (d) {
- Node s = Parent::source(e);
- Parent::nextOut(e);
- if (e != INVALID) return;
- d = false;
- Parent::firstIn(e, s);
- } else {
- Parent::nextIn(e);
- }
- }
-
- Node nodeFromId(int ix) const {
- return Parent::nodeFromId(ix);
- }
-
- Edge edgeFromId(int ix) const {
- return direct(Parent::edgeFromId(ix >> 1), bool(ix & 1));
- }
-
- UEdge uEdgeFromId(int ix) const {
- return Parent::edgeFromId(ix);
- }
-
- int id(const Node &n) const {
- return Parent::id(n);
- }
-
- int id(const UEdge &e) const {
- return Parent::id(e);
- }
-
- int id(const Edge &e) const {
- return 2 * Parent::id(e) + int(e.forward);
- }
-
- int maxNodeId() const {
- return Parent::maxNodeId();
- }
-
- int maxEdgeId() const {
- return 2 * Parent::maxEdgeId() + 1;
- }
-
- int maxUEdgeId() const {
- return Parent::maxEdgeId();
- }
-
-
- int edgeNum() const {
- return 2 * Parent::edgeNum();
- }
-
- int uEdgeNum() const {
- return Parent::edgeNum();
- }
-
- Edge findEdge(Node s, Node t, Edge p = INVALID) const {
- if (p == INVALID) {
- UEdge edge = Parent::findEdge(s, t);
- if (edge != INVALID) return direct(edge, true);
- edge = Parent::findEdge(t, s);
- if (edge != INVALID) return direct(edge, false);
- } else if (direction(p)) {
- UEdge edge = Parent::findEdge(s, t, p);
- if (edge != INVALID) return direct(edge, true);
- edge = Parent::findEdge(t, s);
- if (edge != INVALID) return direct(edge, false);
- } else {
- UEdge edge = Parent::findEdge(t, s, p);
- if (edge != INVALID) return direct(edge, false);
- }
- return INVALID;
- }
-
- UEdge findUEdge(Node s, Node t, UEdge p = INVALID) const {
- if (s != t) {
- if (p == INVALID) {
- UEdge edge = Parent::findEdge(s, t);
- if (edge != INVALID) return edge;
- edge = Parent::findEdge(t, s);
- if (edge != INVALID) return edge;
- } else if (Parent::s(p) == s) {
- UEdge edge = Parent::findEdge(s, t, p);
- if (edge != INVALID) return edge;
- edge = Parent::findEdge(t, s);
- if (edge != INVALID) return edge;
- } else {
- UEdge edge = Parent::findEdge(t, s, p);
- if (edge != INVALID) return edge;
- }
- } else {
- return Parent::findEdge(s, t, p);
- }
- return INVALID;
- }
- };
-
- template <typename Base>
- class BidirBpUGraphExtender : public Base {
- public:
- typedef Base Parent;
- typedef BidirBpUGraphExtender Graph;
-
- typedef typename Parent::Node Node;
- typedef typename Parent::UEdge UEdge;
-
-
- using Parent::first;
- using Parent::next;
-
- using Parent::id;
-
- class ANode : public Node {
- friend class BidirBpUGraphExtender;
- public:
- ANode() {}
- ANode(const Node& node) : Node(node) {
- LEMON_ASSERT(Parent::aNode(node) || node == INVALID,
- typename Parent::NodeSetError());
- }
- ANode& operator=(const Node& node) {
- LEMON_ASSERT(Parent::aNode(node) || node == INVALID,
- typename Parent::NodeSetError());
- Node::operator=(node);
- return *this;
- }
- ANode(Invalid) : Node(INVALID) {}
- ANode& operator=(Invalid) {
- Node::operator=(INVALID);
- return *this;
- }
- };
-
- void first(ANode& node) const {
- Parent::firstANode(static_cast<Node&>(node));
- }
- void next(ANode& node) const {
- Parent::nextANode(static_cast<Node&>(node));
- }
-
- int id(const ANode& node) const {
- return Parent::aNodeId(node);
- }
-
- class BNode : public Node {
- friend class BidirBpUGraphExtender;
- public:
- BNode() {}
- BNode(const Node& node) : Node(node) {
- LEMON_ASSERT(Parent::bNode(node) || node == INVALID,
- typename Parent::NodeSetError());
- }
- BNode& operator=(const Node& node) {
- LEMON_ASSERT(Parent::bNode(node) || node == INVALID,
- typename Parent::NodeSetError());
- Node::operator=(node);
- return *this;
- }
- BNode(Invalid) : Node(INVALID) {}
- BNode& operator=(Invalid) {
- Node::operator=(INVALID);
- return *this;
- }
- };
-
- void first(BNode& node) const {
- Parent::firstBNode(static_cast<Node&>(node));
- }
- void next(BNode& node) const {
- Parent::nextBNode(static_cast<Node&>(node));
- }
-
- int id(const BNode& node) const {
- return Parent::aNodeId(node);
- }
-
- Node source(const UEdge& edge) const {
- return this->aNode(edge);
- }
- Node target(const UEdge& edge) const {
- return this->bNode(edge);
- }
-
- void firstInc(UEdge& edge, bool& dir, const Node& node) const {
- if (Parent::aNode(node)) {
- Parent::firstFromANode(edge, node);
- dir = true;
- } else {
- Parent::firstFromBNode(edge, node);
- dir = static_cast<UEdge&>(edge) == INVALID;
- }
- }
- void nextInc(UEdge& edge, bool& dir) const {
- if (dir) {
- Parent::nextFromANode(edge);
- } else {
- Parent::nextFromBNode(edge);
- if (edge == INVALID) dir = true;
- }
- }
-
- class Edge : public UEdge {
- friend class BidirBpUGraphExtender;
- protected:
- bool forward;
-
- Edge(const UEdge& edge, bool _forward)
- : UEdge(edge), forward(_forward) {}
-
- public:
- Edge() {}
- Edge (Invalid) : UEdge(INVALID), forward(true) {}
- bool operator==(const Edge& i) const {
- return UEdge::operator==(i) && forward == i.forward;
- }
- bool operator!=(const Edge& i) const {
- return UEdge::operator!=(i) || forward != i.forward;
- }
- bool operator<(const Edge& i) const {
- return UEdge::operator<(i) ||
- (!(i.forward<forward) && UEdge(*this)<UEdge(i));
- }
- };
-
- void first(Edge& edge) const {
- Parent::first(static_cast<UEdge&>(edge));
- edge.forward = true;
- }
-
- void next(Edge& edge) const {
- if (!edge.forward) {
- Parent::next(static_cast<UEdge&>(edge));
- }
- edge.forward = !edge.forward;
- }
-
- void firstOut(Edge& edge, const Node& node) const {
- if (Parent::aNode(node)) {
- Parent::firstFromANode(edge, node);
- edge.forward = true;
- } else {
- Parent::firstFromBNode(edge, node);
- edge.forward = static_cast<UEdge&>(edge) == INVALID;
- }
- }
- void nextOut(Edge& edge) const {
- if (edge.forward) {
- Parent::nextFromANode(edge);
- } else {
- Parent::nextFromBNode(edge);
- edge.forward = static_cast<UEdge&>(edge) == INVALID;
- }
- }
-
- void firstIn(Edge& edge, const Node& node) const {
- if (Parent::bNode(node)) {
- Parent::firstFromBNode(edge, node);
- edge.forward = true;
- } else {
- Parent::firstFromANode(edge, node);
- edge.forward = static_cast<UEdge&>(edge) == INVALID;
- }
- }
- void nextIn(Edge& edge) const {
- if (edge.forward) {
- Parent::nextFromBNode(edge);
- } else {
- Parent::nextFromANode(edge);
- edge.forward = static_cast<UEdge&>(edge) == INVALID;
- }
- }
-
- Node source(const Edge& edge) const {
- return edge.forward ? Parent::aNode(edge) : Parent::bNode(edge);
- }
- Node target(const Edge& edge) const {
- return edge.forward ? Parent::bNode(edge) : Parent::aNode(edge);
- }
-
- int id(const Edge& edge) const {
- return (Parent::id(static_cast<const UEdge&>(edge)) << 1) +
- (edge.forward ? 0 : 1);
- }
- Edge edgeFromId(int ix) const {
- return Edge(Parent::fromUEdgeId(ix >> 1), (ix & 1) == 0);
- }
- int maxEdgeId() const {
- return (Parent::maxUEdgeId() << 1) + 1;
- }
-
- bool direction(const Edge& edge) const {
- return edge.forward;
- }
-
- Edge direct(const UEdge& edge, bool dir) const {
- return Edge(edge, dir);
- }
-
- int edgeNum() const {
- return 2 * Parent::uEdgeNum();
- }
-
- int uEdgeNum() const {
- return Parent::uEdgeNum();
- }
-
-
- };
-}
-
-#endif
diff --git a/src/lemon/bits/debug_map.h b/src/lemon/bits/debug_map.h
deleted file mode 100644
index 9447822..0000000
--- a/src/lemon/bits/debug_map.h
+++ /dev/null
@@ -1,382 +0,0 @@
-/* -*- C++ -*-
- *
- * This file is a part of LEMON, a generic C++ optimization library
- *
- * Copyright (C) 2003-2008
- * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
- * (Egervary Research Group on Combinatorial Optimization, EGRES).
- *
- * Permission to use, modify and distribute this software is granted
- * provided that this copyright notice appears in all copies. For
- * precise terms see the accompanying LICENSE file.
- *
- * This software is provided "AS IS" with no warranty of any kind,
- * express or implied, and with no claim as to its suitability for any
- * purpose.
- *
- */
-
-#ifndef LEMON_BITS_DEBUG_MAP_H
-#define LEMON_BITS_DEBUG_MAP_H
-
-#include <vector>
-#include <algorithm>
-
-#include <lemon/bits/traits.h>
-#include <lemon/bits/utility.h>
-#include <lemon/error.h>
-
-#include <lemon/bits/alteration_notifier.h>
-
-#include <lemon/concept_check.h>
-#include <lemon/concepts/maps.h>
-
-///\ingroup graphbits
-///
-///\file
-///\brief Vector based graph maps for debugging.
-namespace lemon {
-
-#ifndef LEMON_STRICT_DEBUG_MAP
-#define LEMON_STRICT_DEBUG_MAP false
-#endif
-
- /// \ingroup graphbits
- ///
- /// \brief Graph map based on the std::vector storage.
- ///
- /// The DebugMap template class is graph map structure what
- /// automatically updates the map when a key is added to or erased from
- /// the map. This map also checks some programming failures by example
- /// multiple addition of items, erasing of not existing item or
- /// not erased items at the destruction of the map. It helps the
- /// programmer to avoid segmentation faults and memory leaks.
- ///
- /// \param Notifier The AlterationNotifier that will notify this map.
- /// \param Item The item type of the graph items.
- /// \param Value The value type of the map.
- ///
- /// \author Balazs Dezso
- template <typename _Graph, typename _Item, typename _Value>
- class DebugMap
- : public ItemSetTraits<_Graph, _Item>::ItemNotifier::ObserverBase {
- private:
-
- /// The container type of the map.
- typedef std::vector<_Value> Container;
-
- /// The container type of the debug flags.
- typedef std::vector<bool> Flag;
-
- public:
-
- static const bool strictCheck = LEMON_STRICT_DEBUG_MAP;
-
- struct MapError {
- public:
- virtual ~MapError() {}
- virtual const char* what() const throw() {
- return "lemon::DebugMap::MapError";
- }
- };
-
- /// The graph type of the map.
- typedef _Graph Graph;
- /// The item type of the map.
- typedef _Item Item;
- /// The reference map tag.
- typedef True ReferenceMapTag;
-
- /// The key type of the map.
- typedef _Item Key;
- /// The value type of the map.
- typedef _Value Value;
-
- /// The notifier type.
- typedef typename ItemSetTraits<_Graph, _Item>::ItemNotifier Notifier;
-
- /// The map type.
- typedef DebugMap Map;
- /// The base class of the map.
- typedef typename Notifier::ObserverBase Parent;
-
- /// The reference type of the map;
- typedef typename Container::reference Reference;
- /// The const reference type of the map;
- typedef typename Container::const_reference ConstReference;
-
-
- /// \brief Constructor to attach the new map into the notifier.
- ///
- /// It constructs a map and attachs it into the notifier.
- /// It adds all the items of the graph to the map.
- DebugMap(const Graph& graph) {
- Parent::attach(graph.notifier(Item()));
- container.resize(Parent::notifier()->maxId() + 1);
- flag.resize(Parent::notifier()->maxId() + 1, false);
- const typename Parent::Notifier* notifier = Parent::notifier();
- Item it;
- for (notifier->first(it); it != INVALID; notifier->next(it)) {
- flag[Parent::notifier()->id(it)] = true;
- }
- }
-
- /// \brief Constructor uses given value to initialize the map.
- ///
- /// It constructs a map uses a given value to initialize the map.
- /// It adds all the items of the graph to the map.
- DebugMap(const Graph& graph, const Value& value) {
- Parent::attach(graph.notifier(Item()));
- container.resize(Parent::notifier()->maxId() + 1, value);
- flag.resize(Parent::notifier()->maxId() + 1, false);
- const typename Parent::Notifier* notifier = Parent::notifier();
- Item it;
- for (notifier->first(it); it != INVALID; notifier->next(it)) {
- flag[Parent::notifier()->id(it)] = true;
- }
- }
-
- /// \brief Copy constructor
- ///
- /// Copy constructor.
- DebugMap(const DebugMap& _copy) : Parent() {
- if (_copy.attached()) {
- Parent::attach(*_copy.notifier());
- container = _copy.container;
- }
- flag.resize(Parent::notifier()->maxId() + 1, false);
- const typename Parent::Notifier* notifier = Parent::notifier();
- Item it;
- for (notifier->first(it); it != INVALID; notifier->next(it)) {
- flag[Parent::notifier()->id(it)] = true;
- LEMON_ASSERT(_copy.flag[Parent::notifier()->id(it)], MapError());
- }
- }
-
- /// \brief Destructor
- ///
- /// Destructor.
- ~DebugMap() {
- const typename Parent::Notifier* notifier = Parent::notifier();
- if (notifier != 0) {
- Item it;
- for (notifier->first(it); it != INVALID; notifier->next(it)) {
- LEMON_ASSERT(flag[Parent::notifier()->id(it)], MapError());
- flag[Parent::notifier()->id(it)] = false;
- }
- }
- for (int i = 0; i < int(flag.size()); ++i) {
- LEMON_ASSERT(!flag[i], MapError());
- }
- }
-
- /// \brief Assign operator.
- ///
- /// This operator assigns for each item in the map the
- /// value mapped to the same item in the copied map.
- /// The parameter map should be indiced with the same
- /// itemset because this assign operator does not change
- /// the container of the map.
- DebugMap& operator=(const DebugMap& cmap) {
- return operator=<DebugMap>(cmap);
- }
-
-
- /// \brief Template assign operator.
- ///
- /// The given parameter should be conform to the ReadMap
- /// concecpt and could be indiced by the current item set of
- /// the NodeMap. In this case the value for each item
- /// is assigned by the value of the given ReadMap.
- template <typename CMap>
- DebugMap& operator=(const CMap& cmap) {
- checkConcept<concepts::ReadMap<Key, _Value>, CMap>();
- const typename Parent::Notifier* notifier = Parent::notifier();
- Item it;
- for (notifier->first(it); it != INVALID; notifier->next(it)) {
- set(it, cmap[it]);
- }
- return *this;
- }
-
- public:
-
- /// \brief The subcript operator.
- ///
- /// The subscript operator. The map can be subscripted by the
- /// actual items of the graph.
- Reference operator[](const Key& key) {
- LEMON_ASSERT(flag[Parent::notifier()->id(key)], MapError());
- return container[Parent::notifier()->id(key)];
- }
-
- /// \brief The const subcript operator.
- ///
- /// The const subscript operator. The map can be subscripted by the
- /// actual items of the graph.
- ConstReference operator[](const Key& key) const {
- LEMON_ASSERT(flag[Parent::notifier()->id(key)], MapError());
- return container[Parent::notifier()->id(key)];
- }
-
-
- /// \brief The setter function of the map.
- ///
- /// It the same as operator[](key) = value expression.
- void set(const Key& key, const Value& value) {
- (*this)[key] = value;
- }
-
- protected:
-
- /// \brief Adds a new key to the map.
- ///
- /// It adds a new key to the map. It called by the observer notifier
- /// and it overrides the add() member function of the observer base.
- virtual void add(const Key& key) {
- int id = Parent::notifier()->id(key);
- if (id >= int(container.size())) {
- container.resize(id + 1);
- flag.resize(id + 1, false);
- }
- LEMON_ASSERT(!flag[Parent::notifier()->id(key)], MapError());
- flag[Parent::notifier()->id(key)] = true;
- if (strictCheck) {
- std::vector<bool> fl(flag.size(), false);
- const typename Parent::Notifier* notifier = Parent::notifier();
- Item it;
- for (notifier->first(it); it != INVALID; notifier->next(it)) {
- int jd = Parent::notifier()->id(it);
- fl[jd] = true;
- }
- LEMON_ASSERT(fl == flag, MapError());
- }
- }
-
- /// \brief Adds more new keys to the map.
- ///
- /// It adds more new keys to the map. It called by the observer notifier
- /// and it overrides the add() member function of the observer base.
- virtual void add(const std::vector<Key>& keys) {
- int max = container.size() - 1;
- for (int i = 0; i < int(keys.size()); ++i) {
- int id = Parent::notifier()->id(keys[i]);
- if (id >= max) {
- max = id;
- }
- }
- container.resize(max + 1);
- flag.resize(max + 1, false);
- for (int i = 0; i < int(keys.size()); ++i) {
- LEMON_ASSERT(!flag[Parent::notifier()->id(keys[i])], MapError());
- flag[Parent::notifier()->id(keys[i])] = true;
- }
- if (strictCheck) {
- std::vector<bool> fl(flag.size(), false);
- const typename Parent::Notifier* notifier = Parent::notifier();
- Item it;
- for (notifier->first(it); it != INVALID; notifier->next(it)) {
- int id = Parent::notifier()->id(it);
- fl[id] = true;
- }
- LEMON_ASSERT(fl == flag, MapError());
- }
- }
-
- /// \brief Erase a key from the map.
- ///
- /// Erase a key from the map. It called by the observer notifier
- /// and it overrides the erase() member function of the observer base.
- virtual void erase(const Key& key) {
- if (strictCheck) {
- std::vector<bool> fl(flag.size(), false);
- const typename Parent::Notifier* notifier = Parent::notifier();
- Item it;
- for (notifier->first(it); it != INVALID; notifier->next(it)) {
- int id = Parent::notifier()->id(it);
- fl[id] = true;
- }
- LEMON_ASSERT(fl == flag, MapError());
- }
- container[Parent::notifier()->id(key)] = Value();
- LEMON_ASSERT(flag[Parent::notifier()->id(key)], MapError());
- flag[Parent::notifier()->id(key)] = false;
- }
-
- /// \brief Erase more keys from the map.
- ///
- /// Erase more keys from the map. It called by the observer notifier
- /// and it overrides the erase() member function of the observer base.
- virtual void erase(const std::vector<Key>& keys) {
- if (strictCheck) {
- std::vector<bool> fl(flag.size(), false);
- const typename Parent::Notifier* notifier = Parent::notifier();
- Item it;
- for (notifier->first(it); it != INVALID; notifier->next(it)) {
- int id = Parent::notifier()->id(it);
- fl[id] = true;
- }
- LEMON_ASSERT(fl == flag, MapError());
- }
- for (int i = 0; i < int(keys.size()); ++i) {
- container[Parent::notifier()->id(keys[i])] = Value();
- LEMON_ASSERT(flag[Parent::notifier()->id(keys[i])], MapError());
- flag[Parent::notifier()->id(keys[i])] = false;
- }
- }
-
- /// \brief Buildes the map.
- ///
- /// It buildes the map. It called by the observer notifier
- /// and it overrides the build() member function of the observer base.
- virtual void build() {
- if (strictCheck) {
- for (int i = 0; i < int(flag.size()); ++i) {
- LEMON_ASSERT(flag[i], MapError());
- }
- }
- int size = Parent::notifier()->maxId() + 1;
- container.reserve(size);
- container.resize(size);
- flag.reserve(size);
- flag.resize(size, false);
- const typename Parent::Notifier* notifier = Parent::notifier();
- Item it;
- for (notifier->first(it); it != INVALID; notifier->next(it)) {
- int id = Parent::notifier()->id(it);
- LEMON_ASSERT(!flag[id], MapError());
- flag[id] = true;
- }
- }
-
- /// \brief Clear the map.
- ///
- /// It erase all items from the map. It called by the observer notifier
- /// and it overrides the clear() member function of the observer base.
- virtual void clear() {
- const typename Parent::Notifier* notifier = Parent::notifier();
- Item it;
- for (notifier->first(it); it != INVALID; notifier->next(it)) {
- int id = Parent::notifier()->id(it);
- LEMON_ASSERT(flag[id], MapError());
- flag[id] = false;
- }
- if (strictCheck) {
- for (int i = 0; i < int(flag.size()); ++i) {
- LEMON_ASSERT(!flag[i], MapError());
- }
- }
- container.clear();
- flag.clear();
- }
-
- private:
-
- Container container;
- Flag flag;
-
- };
-
-}
-
-#endif
diff --git a/src/lemon/bits/default_map.h b/src/lemon/bits/default_map.h
deleted file mode 100644
index 2a8689d..0000000
--- a/src/lemon/bits/default_map.h
+++ /dev/null
@@ -1,181 +0,0 @@
-/* -*- C++ -*-
- *
- * This file is a part of LEMON, a generic C++ optimization library
- *
- * Copyright (C) 2003-2008
- * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
- * (Egervary Research Group on Combinatorial Optimization, EGRES).
- *
- * Permission to use, modify and distribute this software is granted
- * provided that this copyright notice appears in all copies. For
- * precise terms see the accompanying LICENSE file.
- *
- * This software is provided "AS IS" with no warranty of any kind,
- * express or implied, and with no claim as to its suitability for any
- * purpose.
- *
- */
-
-#ifndef LEMON_BITS_DEFAULT_MAP_H
-#define LEMON_BITS_DEFAULT_MAP_H
-
-
-#include <lemon/bits/array_map.h>
-#include <lemon/bits/vector_map.h>
-#include <lemon/bits/debug_map.h>
-
-///\ingroup graphbits
-///\file
-///\brief Graph maps that construct and destruct their elements dynamically.
-
-namespace lemon {
-
-
-#ifndef LEMON_USE_DEBUG_MAP
-
- template <typename _Graph, typename _Item, typename _Value>
- struct DefaultMapSelector {
- typedef ArrayMap<_Graph, _Item, _Value> Map;
- };
-
- // bool
- template <typename _Graph, typename _Item>
- struct DefaultMapSelector<_Graph, _Item, bool> {
- typedef VectorMap<_Graph, _Item, bool> Map;
- };
-
- // char
- template <typename _Graph, typename _Item>
- struct DefaultMapSelector<_Graph, _Item, char> {
- typedef VectorMap<_Graph, _Item, char> Map;
- };
-
- template <typename _Graph, typename _Item>
- struct DefaultMapSelector<_Graph, _Item, signed char> {
- typedef VectorMap<_Graph, _Item, signed char> Map;
- };
-
- template <typename _Graph, typename _Item>
- struct DefaultMapSelector<_Graph, _Item, unsigned char> {
- typedef VectorMap<_Graph, _Item, unsigned char> Map;
- };
-
-
- // int
- template <typename _Graph, typename _Item>
- struct DefaultMapSelector<_Graph, _Item, signed int> {
- typedef VectorMap<_Graph, _Item, signed int> Map;
- };
-
- template <typename _Graph, typename _Item>
- struct DefaultMapSelector<_Graph, _Item, unsigned int> {
- typedef VectorMap<_Graph, _Item, unsigned int> Map;
- };
-
-
- // short
- template <typename _Graph, typename _Item>
- struct DefaultMapSelector<_Graph, _Item, signed short> {
- typedef VectorMap<_Graph, _Item, signed short> Map;
- };
-
- template <typename _Graph, typename _Item>
- struct DefaultMapSelector<_Graph, _Item, unsigned short> {
- typedef VectorMap<_Graph, _Item, unsigned short> Map;
- };
-
-
- // long
- template <typename _Graph, typename _Item>
- struct DefaultMapSelector<_Graph, _Item, signed long> {
- typedef VectorMap<_Graph, _Item, signed long> Map;
- };
-
- template <typename _Graph, typename _Item>
- struct DefaultMapSelector<_Graph, _Item, unsigned long> {
- typedef VectorMap<_Graph, _Item, unsigned long> Map;
- };
-
-
-#if defined __GNUC__ && !defined __STRICT_ANSI__
-
- // long long
- template <typename _Graph, typename _Item>
- struct DefaultMapSelector<_Graph, _Item, signed long long> {
- typedef VectorMap<_Graph, _Item, signed long long> Map;
- };
-
- template <typename _Graph, typename _Item>
- struct DefaultMapSelector<_Graph, _Item, unsigned long long> {
- typedef VectorMap<_Graph, _Item, unsigned long long> Map;
- };
-
-#endif
-
-
- // float
- template <typename _Graph, typename _Item>
- struct DefaultMapSelector<_Graph, _Item, float> {
- typedef VectorMap<_Graph, _Item, float> Map;
- };
-
-
- // double
- template <typename _Graph, typename _Item>
- struct DefaultMapSelector<_Graph, _Item, double> {
- typedef VectorMap<_Graph, _Item, double> Map;
- };
-
-
- // long double
- template <typename _Graph, typename _Item>
- struct DefaultMapSelector<_Graph, _Item, long double> {
- typedef VectorMap<_Graph, _Item, long double> Map;
- };
-
-
- // pointer
- template <typename _Graph, typename _Item, typename _Ptr>
- struct DefaultMapSelector<_Graph, _Item, _Ptr*> {
- typedef VectorMap<_Graph, _Item, _Ptr*> Map;
- };
-
-#else
-
- template <typename _Graph, typename _Item, typename _Value>
- struct DefaultMapSelector {
- typedef DebugMap<_Graph, _Item, _Value> Map;
- };
-
-#endif
-
- /// \e
- template <typename _Graph, typename _Item, typename _Value>
- class DefaultMap
- : public DefaultMapSelector<_Graph, _Item, _Value>::Map {
- public:
- typedef typename DefaultMapSelector<_Graph, _Item, _Value>::Map Parent;
- typedef DefaultMap<_Graph, _Item, _Value> Map;
-
- typedef typename Parent::Graph Graph;
- typedef typename Parent::Value Value;
-
- explicit DefaultMap(const Graph& graph) : Parent(graph) {}
- DefaultMap(const Graph& graph, const Value& value)
- : Parent(graph, value) {}
-
- DefaultMap& operator=(const DefaultMap& cmap) {
- return operator=<DefaultMap>(cmap);
- }
-
- template <typename CMap>
- DefaultMap& operator=(const CMap& cmap) {
- Parent::operator=(cmap);
- return *this;
- }
-
- };
-
-}
-
-#endif
diff --git a/src/lemon/bits/graph_adaptor_extender.h b/src/lemon/bits/graph_adaptor_extender.h
deleted file mode 100644
index 3619264..0000000
--- a/src/lemon/bits/graph_adaptor_extender.h
+++ /dev/null
@@ -1,742 +0,0 @@
-/* -*- C++ -*-
- *
- * This file is a part of LEMON, a generic C++ optimization library
- *
- * Copyright (C) 2003-2008
- * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
- * (Egervary Research Group on Combinatorial Optimization, EGRES).
- *
- * Permission to use, modify and distribute this software is granted
- * provided that this copyright notice appears in all copies. For
- * precise terms see the accompanying LICENSE file.
- *
- * This software is provided "AS IS" with no warranty of any kind,
- * express or implied, and with no claim as to its suitability for any
- * purpose.
- *
- */
-
-#ifndef LEMON_BITS_GRAPH_ADAPTOR_EXTENDER_H
-#define LEMON_BITS_GRAPH_ADAPTOR_EXTENDER_H
-
-#include <lemon/bits/invalid.h>
-#include <lemon/error.h>
-
-#include <lemon/bits/default_map.h>
-
-
-///\ingroup graphbits
-///\file
-///\brief Extenders for the graph adaptor types
-namespace lemon {
-
- /// \ingroup graphbits
- ///
- /// \brief Extender for the GraphAdaptors
- template <typename _Graph>
- class GraphAdaptorExtender : public _Graph {
- public:
-
- typedef _Graph Parent;
- typedef _Graph Graph;
- typedef GraphAdaptorExtender Adaptor;
-
- // Base extensions
-
- typedef typename Parent::Node Node;
- typedef typename Parent::Edge Edge;
-
- int maxId(Node) const {
- return Parent::maxNodeId();
- }
-
- int maxId(Edge) const {
- return Parent::maxEdgeId();
- }
-
- Node fromId(int id, Node) const {
- return Parent::nodeFromId(id);
- }
-
- Edge fromId(int id, Edge) const {
- return Parent::edgeFromId(id);
- }
-
- Node oppositeNode(const Node &n, const Edge &e) const {
- if (n == Parent::source(e))
- return Parent::target(e);
- else if(n==Parent::target(e))
- return Parent::source(e);
- else
- return INVALID;
- }
-
- class NodeIt : public Node {
- const Adaptor* graph;
- public:
-
- NodeIt() {}
-
- NodeIt(Invalid i) : Node(i) { }
-
- explicit NodeIt(const Adaptor& _graph) : graph(&_graph) {
- _graph.first(static_cast<Node&>(*this));
- }
-
- NodeIt(const Adaptor& _graph, const Node& node)
- : Node(node), graph(&_graph) {}
-
- NodeIt& operator++() {
- graph->next(*this);
- return *this;
- }
-
- };
-
-
- class EdgeIt : public Edge {
- const Adaptor* graph;
- public:
-
- EdgeIt() { }
-
- EdgeIt(Invalid i) : Edge(i) { }
-
- explicit EdgeIt(const Adaptor& _graph) : graph(&_graph) {
- _graph.first(static_cast<Edge&>(*this));
- }
-
- EdgeIt(const Adaptor& _graph, const Edge& e) :
- Edge(e), graph(&_graph) { }
-
- EdgeIt& operator++() {
- graph->next(*this);
- return *this;
- }
-
- };
-
-
- class OutEdgeIt : public Edge {
- const Adaptor* graph;
- public:
-
- OutEdgeIt() { }
-
- OutEdgeIt(Invalid i) : Edge(i) { }
-
- OutEdgeIt(const Adaptor& _graph, const Node& node)
- : graph(&_graph) {
- _graph.firstOut(*this, node);
- }
-
- OutEdgeIt(const Adaptor& _graph, const Edge& edge)
- : Edge(edge), graph(&_graph) {}
-
- OutEdgeIt& operator++() {
- graph->nextOut(*this);
- return *this;
- }
-
- };
-
-
- class InEdgeIt : public Edge {
- const Adaptor* graph;
- public:
-
- InEdgeIt() { }
-
- InEdgeIt(Invalid i) : Edge(i) { }
-
- InEdgeIt(const Adaptor& _graph, const Node& node)
- : graph(&_graph) {
- _graph.firstIn(*this, node);
- }
-
- InEdgeIt(const Adaptor& _graph, const Edge& edge) :
- Edge(edge), graph(&_graph) {}
-
- InEdgeIt& operator++() {
- graph->nextIn(*this);
- return *this;
- }
-
- };
-
- /// \brief Base node of the iterator
- ///
- /// Returns the base node (ie. the source in this case) of the iterator
- Node baseNode(const OutEdgeIt &e) const {
- return Parent::source(e);
- }
- /// \brief Running node of the iterator
- ///
- /// Returns the running node (ie. the target in this case) of the
- /// iterator
- Node runningNode(const OutEdgeIt &e) const {
- return Parent::target(e);
- }
-
- /// \brief Base node of the iterator
- ///
- /// Returns the base node (ie. the target in this case) of the iterator
- Node baseNode(const InEdgeIt &e) const {
- return Parent::target(e);
- }
- /// \brief Running node of the iterator
- ///
- /// Returns the running node (ie. the source in this case) of the
- /// iterator
- Node runningNode(const InEdgeIt &e) const {
- return Parent::source(e);
- }
-
- };
-
-
- /// \ingroup graphbits
- ///
- /// \brief Extender for the UGraphAdaptors
- template <typename _UGraph>
- class UGraphAdaptorExtender : public _UGraph {
- public:
-
- typedef _UGraph Parent;
- typedef _UGraph UGraph;
- typedef UGraphAdaptorExtender Adaptor;
-
- typedef typename Parent::Node Node;
- typedef typename Parent::Edge Edge;
- typedef typename Parent::UEdge UEdge;
-
- // UGraph extension
-
- int maxId(Node) const {
- return Parent::maxNodeId();
- }
-
- int maxId(Edge) const {
- return Parent::maxEdgeId();
- }
-
- int maxId(UEdge) const {
- return Parent::maxUEdgeId();
- }
-
- Node fromId(int id, Node) const {
- return Parent::nodeFromId(id);
- }
-
- Edge fromId(int id, Edge) const {
- return Parent::edgeFromId(id);
- }
-
- UEdge fromId(int id, UEdge) const {
- return Parent::uEdgeFromId(id);
- }
-
- Node oppositeNode(const Node &n, const UEdge &e) const {
- if( n == Parent::source(e))
- return Parent::target(e);
- else if( n == Parent::target(e))
- return Parent::source(e);
- else
- return INVALID;
- }
-
- Edge oppositeEdge(const Edge &e) const {
- return Parent::direct(e, !Parent::direction(e));
- }
-
- using Parent::direct;
- Edge direct(const UEdge &ue, const Node &s) const {
- return Parent::direct(ue, Parent::source(ue) == s);
- }
-
-
- class NodeIt : public Node {
- const Adaptor* graph;
- public:
-
- NodeIt() {}
-
- NodeIt(Invalid i) : Node(i) { }
-
- explicit NodeIt(const Adaptor& _graph) : graph(&_graph) {
- _graph.first(static_cast<Node&>(*this));
- }
-
- NodeIt(const Adaptor& _graph, const Node& node)
- : Node(node), graph(&_graph) {}
-
- NodeIt& operator++() {
- graph->next(*this);
- return *this;
- }
-
- };
-
-
- class EdgeIt : public Edge {
- const Adaptor* graph;
- public:
-
- EdgeIt() { }
-
- EdgeIt(Invalid i) : Edge(i) { }
-
- explicit EdgeIt(const Adaptor& _graph) : graph(&_graph) {
- _graph.first(static_cast<Edge&>(*this));
- }
-
- EdgeIt(const Adaptor& _graph, const Edge& e) :
- Edge(e), graph(&_graph) { }
-
- EdgeIt& operator++() {
- graph->next(*this);
- return *this;
- }
-
- };
-
-
- class OutEdgeIt : public Edge {
- const Adaptor* graph;
- public:
-
- OutEdgeIt() { }
-
- OutEdgeIt(Invalid i) : Edge(i) { }
-
- OutEdgeIt(const Adaptor& _graph, const Node& node)
- : graph(&_graph) {
- _graph.firstOut(*this, node);
- }
-
- OutEdgeIt(const Adaptor& _graph, const Edge& edge)
- : Edge(edge), graph(&_graph) {}
-
- OutEdgeIt& operator++() {
- graph->nextOut(*this);
- return *this;
- }
-
- };
-
-
- class InEdgeIt : public Edge {
- const Adaptor* graph;
- public:
-
- InEdgeIt() { }
-
- InEdgeIt(Invalid i) : Edge(i) { }
-
- InEdgeIt(const Adaptor& _graph, const Node& node)
- : graph(&_graph) {
- _graph.firstIn(*this, node);
- }
-
- InEdgeIt(const Adaptor& _graph, const Edge& edge) :
- Edge(edge), graph(&_graph) {}
-
- InEdgeIt& operator++() {
- graph->nextIn(*this);
- return *this;
- }
-
- };
-
- class UEdgeIt : public Parent::UEdge {
- const Adaptor* graph;
- public:
-
- UEdgeIt() { }
-
- UEdgeIt(Invalid i) : UEdge(i) { }
-
- explicit UEdgeIt(const Adaptor& _graph) : graph(&_graph) {
- _graph.first(static_cast<UEdge&>(*this));
- }
-
- UEdgeIt(const Adaptor& _graph, const UEdge& e) :
- UEdge(e), graph(&_graph) { }
-
- UEdgeIt& operator++() {
- graph->next(*this);
- return *this;
- }
-
- };
-
- class IncEdgeIt : public Parent::UEdge {
- friend class UGraphAdaptorExtender;
- const Adaptor* graph;
- bool direction;
- public:
-
- IncEdgeIt() { }
-
- IncEdgeIt(Invalid i) : UEdge(i), direction(false) { }
-
- IncEdgeIt(const Adaptor& _graph, const Node &n) : graph(&_graph) {
- _graph.firstInc(static_cast<UEdge&>(*this), direction, n);
- }
-
- IncEdgeIt(const Adaptor& _graph, const UEdge &ue, const Node &n)
- : graph(&_graph), UEdge(ue) {
- direction = (_graph.source(ue) == n);
- }
-
- IncEdgeIt& operator++() {
- graph->nextInc(*this, direction);
- return *this;
- }
- };
-
- /// \brief Base node of the iterator
- ///
- /// Returns the base node (ie. the source in this case) of the iterator
- Node baseNode(const OutEdgeIt &e) const {
- return Parent::source(static_cast<const Edge&>(e));
- }
- /// \brief Running node of the iterator
- ///
- /// Returns the running node (ie. the target in this case) of the
- /// iterator
- Node runningNode(const OutEdgeIt &e) const {
- return Parent::target(static_cast<const Edge&>(e));
- }
-
- /// \brief Base node of the iterator
- ///
- /// Returns the base node (ie. the target in this case) of the iterator
- Node baseNode(const InEdgeIt &e) const {
- return Parent::target(static_cast<const Edge&>(e));
- }
- /// \brief Running node of the iterator
- ///
- /// Returns the running node (ie. the source in this case) of the
- /// iterator
- Node runningNode(const InEdgeIt &e) const {
- return Parent::source(static_cast<const Edge&>(e));
- }
-
- /// Base node of the iterator
- ///
- /// Returns the base node of the iterator
- Node baseNode(const IncEdgeIt &e) const {
- return e.direction ? source(e) : target(e);
- }
- /// Running node of the iterator
- ///
- /// Returns the running node of the iterator
- Node runningNode(const IncEdgeIt &e) const {
- return e.direction ? target(e) : source(e);
- }
-
- };
-
- /// \ingroup graphbits
- ///
- /// \brief Extender for the BpUGraphAdaptors
- template <typename Base>
- class BpUGraphAdaptorExtender : public Base {
- public:
- typedef Base Parent;
- typedef BpUGraphAdaptorExtender Graph;
-
- typedef typename Parent::Node Node;
- typedef typename Parent::BNode BNode;
- typedef typename Parent::ANode ANode;
- typedef typename Parent::Edge Edge;
- typedef typename Parent::UEdge UEdge;
-
-
- int maxId(Node) const {
- return Parent::maxNodeId();
- }
- int maxId(BNode) const {
- return Parent::maxBNodeId();
- }
- int maxId(ANode) const {
- return Parent::maxANodeId();
- }
- int maxId(Edge) const {
- return Parent::maxEdgeId();
- }
- int maxId(UEdge) const {
- return Parent::maxUEdgeId();
- }
-
-
- Node fromId(int id, Node) const {
- return Parent::nodeFromId(id);
- }
- ANode fromId(int id, ANode) const {
- return Parent::nodeFromANodeId(id);
- }
- BNode fromId(int id, BNode) const {
- return Parent::nodeFromBNodeId(id);
- }
- Edge fromId(int id, Edge) const {
- return Parent::edgeFromId(id);
- }
- UEdge fromId(int id, UEdge) const {
- return Parent::uEdgeFromId(id);
- }
-
- class NodeIt : public Node {
- const Graph* graph;
- public:
-
- NodeIt() { }
-
- NodeIt(Invalid i) : Node(INVALID) { }
-
- explicit NodeIt(const Graph& _graph) : graph(&_graph) {
- graph->first(static_cast<Node&>(*this));
- }
-
- NodeIt(const Graph& _graph, const Node& node)
- : Node(node), graph(&_graph) { }
-
- NodeIt& operator++() {
- graph->next(*this);
- return *this;
- }
-
- };
-
- class ANodeIt : public Node {
- friend class BpUGraphAdaptorExtender;
- const Graph* graph;
- public:
-
- ANodeIt() { }
-
- ANodeIt(Invalid i) : Node(INVALID) { }
-
- explicit ANodeIt(const Graph& _graph) : graph(&_graph) {
- graph->firstANode(static_cast<Node&>(*this));
- }
-
- ANodeIt(const Graph& _graph, const Node& node)
- : Node(node), graph(&_graph) {}
-
- ANodeIt& operator++() {
- graph->nextANode(*this);
- return *this;
- }
- };
-
- class BNodeIt : public Node {
- friend class BpUGraphAdaptorExtender;
- const Graph* graph;
- public:
-
- BNodeIt() { }
-
- BNodeIt(Invalid i) : Node(INVALID) { }
-
- explicit BNodeIt(const Graph& _graph) : graph(&_graph) {
- graph->firstBNode(static_cast<Node&>(*this));
- }
-
- BNodeIt(const Graph& _graph, const Node& node)
- : Node(node), graph(&_graph) {}
-
- BNodeIt& operator++() {
- graph->nextBNode(*this);
- return *this;
- }
- };
-
- class EdgeIt : public Edge {
- friend class BpUGraphAdaptorExtender;
- const Graph* graph;
- public:
-
- EdgeIt() { }
-
- EdgeIt(Invalid i) : Edge(INVALID) { }
-
- explicit EdgeIt(const Graph& _graph) : graph(&_graph) {
- graph->first(static_cast<Edge&>(*this));
- }
-
- EdgeIt(const Graph& _graph, const Edge& edge)
- : Edge(edge), graph(&_graph) { }
-
- EdgeIt& operator++() {
- graph->next(*this);
- return *this;
- }
-
- };
-
- class UEdgeIt : public UEdge {
- friend class BpUGraphAdaptorExtender;
- const Graph* graph;
- public:
-
- UEdgeIt() { }
-
- UEdgeIt(Invalid i) : UEdge(INVALID) { }
-
- explicit UEdgeIt(const Graph& _graph) : graph(&_graph) {
- graph->first(static_cast<UEdge&>(*this));
- }
-
- UEdgeIt(const Graph& _graph, const UEdge& edge)
- : UEdge(edge), graph(&_graph) { }
-
- UEdgeIt& operator++() {
- graph->next(*this);
- return *this;
- }
- };
-
- class OutEdgeIt : public Edge {
- friend class BpUGraphAdaptorExtender;
- const Graph* graph;
- public:
-
- OutEdgeIt() { }
-
- OutEdgeIt(Invalid i) : Edge(i) { }
-
- OutEdgeIt(const Graph& _graph, const Node& node)
- : graph(&_graph) {
- graph->firstOut(*this, node);
- }
-
- OutEdgeIt(const Graph& _graph, const Edge& edge)
- : Edge(edge), graph(&_graph) {}
-
- OutEdgeIt& operator++() {
- graph->nextOut(*this);
- return *this;
- }
-
- };
-
-
- class InEdgeIt : public Edge {
- friend class BpUGraphAdaptorExtender;
- const Graph* graph;
- public:
-
- InEdgeIt() { }
-
- InEdgeIt(Invalid i) : Edge(i) { }
-
- InEdgeIt(const Graph& _graph, const Node& node)
- : graph(&_graph) {
- graph->firstIn(*this, node);
- }
-
- InEdgeIt(const Graph& _graph, const Edge& edge) :
- Edge(edge), graph(&_graph) {}
-
- InEdgeIt& operator++() {
- graph->nextIn(*this);
- return *this;
- }
-
- };
-
- /// \brief Base node of the iterator
- ///
- /// Returns the base node (ie. the source in this case) of the iterator
- Node baseNode(const OutEdgeIt &e) const {
- return Parent::source(static_cast<const Edge&>(e));
- }
- /// \brief Running node of the iterator
- ///
- /// Returns the running node (ie. the target in this case) of the
- /// iterator
- Node runningNode(const OutEdgeIt &e) const {
- return Parent::target(static_cast<const Edge&>(e));
- }
-
- /// \brief Base node of the iterator
- ///
- /// Returns the base node (ie. the target in this case) of the iterator
- Node baseNode(const InEdgeIt &e) const {
- return Parent::target(static_cast<const Edge&>(e));
- }
- /// \brief Running node of the iterator
- ///
- /// Returns the running node (ie. the source in this case) of the
- /// iterator
- Node runningNode(const InEdgeIt &e) const {
- return Parent::source(static_cast<const Edge&>(e));
- }
-
- class IncEdgeIt : public Parent::UEdge {
- friend class BpUGraphAdaptorExtender;
- const Graph* graph;
- bool direction;
- public:
-
- IncEdgeIt() { }
-
- IncEdgeIt(Invalid i) : UEdge(i), direction(true) { }
-
- IncEdgeIt(const Graph& _graph, const Node &n) : graph(&_graph) {
- graph->firstInc(*this, direction, n);
- }
-
- IncEdgeIt(const Graph& _graph, const UEdge &ue, const Node &n)
- : graph(&_graph), UEdge(ue) {
- direction = (graph->source(ue) == n);
- }
-
- IncEdgeIt& operator++() {
- graph->nextInc(*this, direction);
- return *this;
- }
- };
-
-
- /// Base node of the iterator
- ///
- /// Returns the base node of the iterator
- Node baseNode(const IncEdgeIt &e) const {
- return e.direction ? source(e) : target(e);
- }
-
- /// Running node of the iterator
- ///
- /// Returns the running node of the iterator
- Node runningNode(const IncEdgeIt &e) const {
- return e.direction ? target(e) : source(e);
- }
-
- Node oppositeNode(const Node &n, const UEdge &e) const {
- if( n == Parent::source(e))
- return Parent::target(e);
- else if( n == Parent::target(e))
- return Parent::source(e);
- else
- return INVALID;
- }
-
- Edge oppositeEdge(const Edge &e) const {
- return Parent::direct(e, !Parent::direction(e));
- }
-
- using Parent::direct;
- Edge direct(const UEdge &ue, const Node &s) const {
- return Parent::direct(ue, Parent::source(ue) == s);
- }
-
- };
-
-
-}
-
-
-#endif
diff --git a/src/lemon/bits/graph_extender.h b/src/lemon/bits/graph_extender.h
deleted file mode 100644
index 464594d..0000000
--- a/src/lemon/bits/graph_extender.h
+++ /dev/null
@@ -1,1397 +0,0 @@
-/* -*- C++ -*-
- *
- * This file is a part of LEMON, a generic C++ optimization library
- *
- * Copyright (C) 2003-2008
- * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
- * (Egervary Research Group on Combinatorial Optimization, EGRES).
- *
- * Permission to use, modify and distribute this software is granted
- * provided that this copyright notice appears in all copies. For
- * precise terms see the accompanying LICENSE file.
- *
- * This software is provided "AS IS" with no warranty of any kind,
- * express or implied, and with no claim as to its suitability for any
- * purpose.
- *
- */
-
-#ifndef LEMON_BITS_GRAPH_EXTENDER_H
-#define LEMON_BITS_GRAPH_EXTENDER_H
-
-#include <lemon/bits/invalid.h>
-#include <lemon/bits/utility.h>
-#include <lemon/error.h>
-
-#include <lemon/bits/map_extender.h>
-#include <lemon/bits/default_map.h>
-
-#include <lemon/concept_check.h>
-#include <lemon/concepts/maps.h>
-
-///\ingroup graphbits
-///\file
-///\brief Extenders for the graph types
-namespace lemon {
-
- /// \ingroup graphbits
- ///
- /// \brief Extender for the Graphs
- template <typename Base>
- class GraphExtender : public Base {
- public:
-
- typedef Base Parent;
- typedef GraphExtender Graph;
-
- // Base extensions
-
- typedef typename Parent::Node Node;
- typedef typename Parent::Edge Edge;
-
- int maxId(Node) const {
- return Parent::maxNodeId();
- }
-
- int maxId(Edge) const {
- return Parent::maxEdgeId();
- }
-
- Node fromId(int id, Node) const {
- return Parent::nodeFromId(id);
- }
-
- Edge fromId(int id, Edge) const {
- return Parent::edgeFromId(id);
- }
-
- Node oppositeNode(const Node &n, const Edge &e) const {
- if (n == Parent::source(e))
- return Parent::target(e);
- else if(n==Parent::target(e))
- return Parent::source(e);
- else
- return INVALID;
- }
-
- // Alterable extension
-
- typedef AlterationNotifier<GraphExtender, Node> NodeNotifier;
- typedef AlterationNotifier<GraphExtender, Edge> EdgeNotifier;
-
-
- protected:
-
- mutable NodeNotifier node_notifier;
- mutable EdgeNotifier edge_notifier;
-
- public:
-
- NodeNotifier& notifier(Node) const {
- return node_notifier;
- }
-
- EdgeNotifier& notifier(Edge) const {
- return edge_notifier;
- }
-
- class NodeIt : public Node {
- const Graph* graph;
- public:
-
- NodeIt() {}
-
- NodeIt(Invalid i) : Node(i) { }
-
- explicit NodeIt(const Graph& _graph) : graph(&_graph) {
- _graph.first(static_cast<Node&>(*this));
- }
-
- NodeIt(const Graph& _graph, const Node& node)
- : Node(node), graph(&_graph) {}
-
- NodeIt& operator++() {
- graph->next(*this);
- return *this;
- }
-
- };
-
-
- class EdgeIt : public Edge {
- const Graph* graph;
- public:
-
- EdgeIt() { }
-
- EdgeIt(Invalid i) : Edge(i) { }
-
- explicit EdgeIt(const Graph& _graph) : graph(&_graph) {
- _graph.first(static_cast<Edge&>(*this));
- }
-
- EdgeIt(const Graph& _graph, const Edge& e) :
- Edge(e), graph(&_graph) { }
-
- EdgeIt& operator++() {
- graph->next(*this);
- return *this;
- }
-
- };
-
-
- class OutEdgeIt : public Edge {
- const Graph* graph;
- public:
-
- OutEdgeIt() { }
-
- OutEdgeIt(Invalid i) : Edge(i) { }
-
- OutEdgeIt(const Graph& _graph, const Node& node)
- : graph(&_graph) {
- _graph.firstOut(*this, node);
- }
-
- OutEdgeIt(const Graph& _graph, const Edge& edge)
- : Edge(edge), graph(&_graph) {}
-
- OutEdgeIt& operator++() {
- graph->nextOut(*this);
- return *this;
- }
-
- };
-
-
- class InEdgeIt : public Edge {
- const Graph* graph;
- public:
-
- InEdgeIt() { }
-
- InEdgeIt(Invalid i) : Edge(i) { }
-
- InEdgeIt(const Graph& _graph, const Node& node)
- : graph(&_graph) {
- _graph.firstIn(*this, node);
- }
-
- InEdgeIt(const Graph& _graph, const Edge& edge) :
- Edge(edge), graph(&_graph) {}
-
- InEdgeIt& operator++() {
- graph->nextIn(*this);
- return *this;
- }
-
- };
-
- /// \brief Base node of the iterator
- ///
- /// Returns the base node (i.e. the source in this case) of the iterator
- Node baseNode(const OutEdgeIt &e) const {
- return Parent::source(e);
- }
- /// \brief Running node of the iterator
- ///
- /// Returns the running node (i.e. the target in this case) of the
- /// iterator
- Node runningNode(const OutEdgeIt &e) const {
- return Parent::target(e);
- }
-
- /// \brief Base node of the iterator
- ///
- /// Returns the base node (i.e. the target in this case) of the iterator
- Node baseNode(const InEdgeIt &e) const {
- return Parent::target(e);
- }
- /// \brief Running node of the iterator
- ///
- /// Returns the running node (i.e. the source in this case) of the
- /// iterator
- Node runningNode(const InEdgeIt &e) const {
- return Parent::source(e);
- }
-
-
- template <typename _Value>
- class NodeMap
- : public MapExtender<DefaultMap<Graph, Node, _Value> > {
- public:
- typedef GraphExtender Graph;
- typedef MapExtender<DefaultMap<Graph, Node, _Value> > Parent;
-
- explicit NodeMap(const Graph& graph)
- : Parent(graph) {}
- NodeMap(const Graph& graph, const _Value& value)
- : Parent(graph, value) {}
-
- NodeMap& operator=(const NodeMap& cmap) {
- return operator=<NodeMap>(cmap);
- }
-
- template <typename CMap>
- NodeMap& operator=(const CMap& cmap) {
- Parent::operator=(cmap);
- return *this;
- }
-
- };
-
- template <typename _Value>
- class EdgeMap
- : public MapExtender<DefaultMap<Graph, Edge, _Value> > {
- public:
- typedef GraphExtender Graph;
- typedef MapExtender<DefaultMap<Graph, Edge, _Value> > Parent;
-
- explicit EdgeMap(const Graph& graph)
- : Parent(graph) {}
- EdgeMap(const Graph& graph, const _Value& value)
- : Parent(graph, value) {}
-
- EdgeMap& operator=(const EdgeMap& cmap) {
- return operator=<EdgeMap>(cmap);
- }
-
- template <typename CMap>
- EdgeMap& operator=(const CMap& cmap) {
- Parent::operator=(cmap);
- return *this;
- }
- };
-
-
- Node addNode() {
- Node node = Parent::addNode();
- notifier(Node()).add(node);
- return node;
- }
-
- Edge addEdge(const Node& from, const Node& to) {
- Edge edge = Parent::addEdge(from, to);
- notifier(Edge()).add(edge);
- return edge;
- }
-
- void clear() {
- notifier(Edge()).clear();
- notifier(Node()).clear();
- Parent::clear();
- }
-
- template <typename Graph, typename NodeRefMap, typename EdgeRefMap>
- void build(const Graph& graph, NodeRefMap& nodeRef, EdgeRefMap& edgeRef) {
- Parent::build(graph, nodeRef, edgeRef);
- notifier(Node()).build();
- notifier(Edge()).build();
- }
-
- void erase(const Node& node) {
- Edge edge;
- Parent::firstOut(edge, node);
- while (edge != INVALID ) {
- erase(edge);
- Parent::firstOut(edge, node);
- }
-
- Parent::firstIn(edge, node);
- while (edge != INVALID ) {
- erase(edge);
- Parent::firstIn(edge, node);
- }
-
- notifier(Node()).erase(node);
- Parent::erase(node);
- }
-
- void erase(const Edge& edge) {
- notifier(Edge()).erase(edge);
- Parent::erase(edge);
- }
-
- GraphExtender() {
- node_notifier.setContainer(*this);
- edge_notifier.setContainer(*this);
- }
-
-
- ~GraphExtender() {
- edge_notifier.clear();
- node_notifier.clear();
- }
- };
-
- /// \ingroup graphbits
- ///
- /// \brief Extender for the UGraphs
- template <typename Base>
- class UGraphExtender : public Base {
- public:
-
- typedef Base Parent;
- typedef UGraphExtender Graph;
-
- typedef True UndirectedTag;
-
- typedef typename Parent::Node Node;
- typedef typename Parent::Edge Edge;
- typedef typename Parent::UEdge UEdge;
-
- // UGraph extension
-
- int maxId(Node) const {
- return Parent::maxNodeId();
- }
-
- int maxId(Edge) const {
- return Parent::maxEdgeId();
- }
-
- int maxId(UEdge) const {
- return Parent::maxUEdgeId();
- }
-
- Node fromId(int id, Node) const {
- return Parent::nodeFromId(id);
- }
-
- Edge fromId(int id, Edge) const {
- return Parent::edgeFromId(id);
- }
-
- UEdge fromId(int id, UEdge) const {
- return Parent::uEdgeFromId(id);
- }
-
- Node oppositeNode(const Node &n, const UEdge &e) const {
- if( n == Parent::source(e))
- return Parent::target(e);
- else if( n == Parent::target(e))
- return Parent::source(e);
- else
- return INVALID;
- }
-
- Edge oppositeEdge(const Edge &e) const {
- return Parent::direct(e, !Parent::direction(e));
- }
-
- using Parent::direct;
- Edge direct(const UEdge &ue, const Node &s) const {
- return Parent::direct(ue, Parent::source(ue) == s);
- }
-
- // Alterable extension
-
- typedef AlterationNotifier<UGraphExtender, Node> NodeNotifier;
- typedef AlterationNotifier<UGraphExtender, Edge> EdgeNotifier;
- typedef AlterationNotifier<UGraphExtender, UEdge> UEdgeNotifier;
-
-
- protected:
-
- mutable NodeNotifier node_notifier;
- mutable EdgeNotifier edge_notifier;
- mutable UEdgeNotifier uedge_notifier;
-
- public:
-
- NodeNotifier& notifier(Node) const {
- return node_notifier;
- }
-
- EdgeNotifier& notifier(Edge) const {
- return edge_notifier;
- }
-
- UEdgeNotifier& notifier(UEdge) const {
- return uedge_notifier;
- }
-
-
-
- class NodeIt : public Node {
- const Graph* graph;
- public:
-
- NodeIt() {}
-
- NodeIt(Invalid i) : Node(i) { }
-
- explicit NodeIt(const Graph& _graph) : graph(&_graph) {
- _graph.first(static_cast<Node&>(*this));
- }
-
- NodeIt(const Graph& _graph, const Node& node)
- : Node(node), graph(&_graph) {}
-
- NodeIt& operator++() {
- graph->next(*this);
- return *this;
- }
-
- };
-
-
- class EdgeIt : public Edge {
- const Graph* graph;
- public:
-
- EdgeIt() { }
-
- EdgeIt(Invalid i) : Edge(i) { }
-
- explicit EdgeIt(const Graph& _graph) : graph(&_graph) {
- _graph.first(static_cast<Edge&>(*this));
- }
-
- EdgeIt(const Graph& _graph, const Edge& e) :
- Edge(e), graph(&_graph) { }
-
- EdgeIt& operator++() {
- graph->next(*this);
- return *this;
- }
-
- };
-
-
- class OutEdgeIt : public Edge {
- const Graph* graph;
- public:
-
- OutEdgeIt() { }
-
- OutEdgeIt(Invalid i) : Edge(i) { }
-
- OutEdgeIt(const Graph& _graph, const Node& node)
- : graph(&_graph) {
- _graph.firstOut(*this, node);
- }
-
- OutEdgeIt(const Graph& _graph, const Edge& edge)
- : Edge(edge), graph(&_graph) {}
-
- OutEdgeIt& operator++() {
- graph->nextOut(*this);
- return *this;
- }
-
- };
-
-
- class InEdgeIt : public Edge {
- const Graph* graph;
- public:
-
- InEdgeIt() { }
-
- InEdgeIt(Invalid i) : Edge(i) { }
-
- InEdgeIt(const Graph& _graph, const Node& node)
- : graph(&_graph) {
- _graph.firstIn(*this, node);
- }
-
- InEdgeIt(const Graph& _graph, const Edge& edge) :
- Edge(edge), graph(&_graph) {}
-
- InEdgeIt& operator++() {
- graph->nextIn(*this);
- return *this;
- }
-
- };
-
-
- class UEdgeIt : public Parent::UEdge {
- const Graph* graph;
- public:
-
- UEdgeIt() { }
-
- UEdgeIt(Invalid i) : UEdge(i) { }
-
- explicit UEdgeIt(const Graph& _graph) : graph(&_graph) {
- _graph.first(static_cast<UEdge&>(*this));
- }
-
- UEdgeIt(const Graph& _graph, const UEdge& e) :
- UEdge(e), graph(&_graph) { }
-
- UEdgeIt& operator++() {
- graph->next(*this);
- return *this;
- }
-
- };
-
- class IncEdgeIt : public Parent::UEdge {
- friend class UGraphExtender;
- const Graph* graph;
- bool direction;
- public:
-
- IncEdgeIt() { }
-
- IncEdgeIt(Invalid i) : UEdge(i), direction(false) { }
-
- IncEdgeIt(const Graph& _graph, const Node &n) : graph(&_graph) {
- _graph.firstInc(*this, direction, n);
- }
-
- IncEdgeIt(const Graph& _graph, const UEdge &ue, const Node &n)
- : graph(&_graph), UEdge(ue) {
- direction = (_graph.source(ue) == n);
- }
-
- IncEdgeIt& operator++() {
- graph->nextInc(*this, direction);
- return *this;
- }
- };
-
- /// \brief Base node of the iterator
- ///
- /// Returns the base node (ie. the source in this case) of the iterator
- Node baseNode(const OutEdgeIt &e) const {
- return Parent::source(static_cast<const Edge&>(e));
- }
- /// \brief Running node of the iterator
- ///
- /// Returns the running node (ie. the target in this case) of the
- /// iterator
- Node runningNode(const OutEdgeIt &e) const {
- return Parent::target(static_cast<const Edge&>(e));
- }
-
- /// \brief Base node of the iterator
- ///
- /// Returns the base node (ie. the target in this case) of the iterator
- Node baseNode(const InEdgeIt &e) const {
- return Parent::target(static_cast<const Edge&>(e));
- }
- /// \brief Running node of the iterator
- ///
- /// Returns the running node (ie. the source in this case) of the
- /// iterator
- Node runningNode(const InEdgeIt &e) const {
- return Parent::source(static_cast<const Edge&>(e));
- }
-
- /// Base node of the iterator
- ///
- /// Returns the base node of the iterator
- Node baseNode(const IncEdgeIt &e) const {
- return e.direction ? source(e) : target(e);
- }
- /// Running node of the iterator
- ///
- /// Returns the running node of the iterator
- Node runningNode(const IncEdgeIt &e) const {
- return e.direction ? target(e) : source(e);
- }
-
- // Mappable extension
-
- template <typename _Value>
- class NodeMap
- : public MapExtender<DefaultMap<Graph, Node, _Value> > {
- public:
- typedef UGraphExtender Graph;
- typedef MapExtender<DefaultMap<Graph, Node, _Value> > Parent;
-
- NodeMap(const Graph& graph)
- : Parent(graph) {}
- NodeMap(const Graph& graph, const _Value& value)
- : Parent(graph, value) {}
-
- NodeMap& operator=(const NodeMap& cmap) {
- return operator=<NodeMap>(cmap);
- }
-
- template <typename CMap>
- NodeMap& operator=(const CMap& cmap) {
- Parent::operator=(cmap);
- return *this;
- }
-
- };
-
- template <typename _Value>
- class EdgeMap
- : public MapExtender<DefaultMap<Graph, Edge, _Value> > {
- public:
- typedef UGraphExtender Graph;
- typedef MapExtender<DefaultMap<Graph, Edge, _Value> > Parent;
-
- EdgeMap(const Graph& graph)
- : Parent(graph) {}
- EdgeMap(const Graph& graph, const _Value& value)
- : Parent(graph, value) {}
-
- EdgeMap& operator=(const EdgeMap& cmap) {
- return operator=<EdgeMap>(cmap);
- }
-
- template <typename CMap>
- EdgeMap& operator=(const CMap& cmap) {
- Parent::operator=(cmap);
- return *this;
- }
- };
-
-
- template <typename _Value>
- class UEdgeMap
- : public MapExtender<DefaultMap<Graph, UEdge, _Value> > {
- public:
- typedef UGraphExtender Graph;
- typedef MapExtender<DefaultMap<Graph, UEdge, _Value> > Parent;
-
- UEdgeMap(const Graph& graph)
- : Parent(graph) {}
-
- UEdgeMap(const Graph& graph, const _Value& value)
- : Parent(graph, value) {}
-
- UEdgeMap& operator=(const UEdgeMap& cmap) {
- return operator=<UEdgeMap>(cmap);
- }
-
- template <typename CMap>
- UEdgeMap& operator=(const CMap& cmap) {
- Parent::operator=(cmap);
- return *this;
- }
-
- };
-
- // Alteration extension
-
- Node addNode() {
- Node node = Parent::addNode();
- notifier(Node()).add(node);
- return node;
- }
-
- UEdge addEdge(const Node& from, const Node& to) {
- UEdge uedge = Parent::addEdge(from, to);
- notifier(UEdge()).add(uedge);
- std::vector<Edge> ev;
- ev.push_back(Parent::direct(uedge, true));
- ev.push_back(Parent::direct(uedge, false));
- notifier(Edge()).add(ev);
- return uedge;
- }
-
- void clear() {
- notifier(Edge()).clear();
- notifier(UEdge()).clear();
- notifier(Node()).clear();
- Parent::clear();
- }
-
- template <typename Graph, typename NodeRefMap, typename UEdgeRefMap>
- void build(const Graph& graph, NodeRefMap& nodeRef,
- UEdgeRefMap& uEdgeRef) {
- Parent::build(graph, nodeRef, uEdgeRef);
- notifier(Node()).build();
- notifier(UEdge()).build();
- notifier(Edge()).build();
- }
-
- void erase(const Node& node) {
- Edge edge;
- Parent::firstOut(edge, node);
- while (edge != INVALID ) {
- erase(edge);
- Parent::firstOut(edge, node);
- }
-
- Parent::firstIn(edge, node);
- while (edge != INVALID ) {
- erase(edge);
- Parent::firstIn(edge, node);
- }
-
- notifier(Node()).erase(node);
- Parent::erase(node);
- }
-
- void erase(const UEdge& uedge) {
- std::vector<Edge> ev;
- ev.push_back(Parent::direct(uedge, true));
- ev.push_back(Parent::direct(uedge, false));
- notifier(Edge()).erase(ev);
- notifier(UEdge()).erase(uedge);
- Parent::erase(uedge);
- }
-
- UGraphExtender() {
- node_notifier.setContainer(*this);
- edge_notifier.setContainer(*this);
- uedge_notifier.setContainer(*this);
- }
-
- ~UGraphExtender() {
- uedge_notifier.clear();
- edge_notifier.clear();
- node_notifier.clear();
- }
-
- };
-
- /// \ingroup graphbits
- ///
- /// \brief Extender for the BpUGraphs
- template <typename Base>
- class BpUGraphExtender : public Base {
- public:
-
- typedef Base Parent;
- typedef BpUGraphExtender Graph;
-
- typedef True UndirectedTag;
-
- typedef typename Parent::Node Node;
- typedef typename Parent::ANode ANode;
- typedef typename Parent::BNode BNode;
- typedef typename Parent::Edge Edge;
- typedef typename Parent::UEdge UEdge;
-
-
- Node oppositeNode(const Node& node, const UEdge& edge) const {
- return Parent::aNode(edge) == node ?
- Parent::bNode(edge) : Parent::aNode(edge);
- }
-
- using Parent::direct;
- Edge direct(const UEdge& edge, const Node& node) const {
- return Parent::direct(edge, node == Parent::source(edge));
- }
-
- Edge oppositeEdge(const Edge& edge) const {
- return direct(edge, !Parent::direction(edge));
- }
-
- int maxId(Node) const {
- return Parent::maxNodeId();
- }
- int maxId(BNode) const {
- return Parent::maxBNodeId();
- }
- int maxId(ANode) const {
- return Parent::maxANodeId();
- }
- int maxId(Edge) const {
- return Parent::maxEdgeId();
- }
- int maxId(UEdge) const {
- return Parent::maxUEdgeId();
- }
-
-
- Node fromId(int id, Node) const {
- return Parent::nodeFromId(id);
- }
- ANode fromId(int id, ANode) const {
- return Parent::nodeFromANodeId(id);
- }
- BNode fromId(int id, BNode) const {
- return Parent::nodeFromBNodeId(id);
- }
- Edge fromId(int id, Edge) const {
- return Parent::edgeFromId(id);
- }
- UEdge fromId(int id, UEdge) const {
- return Parent::uEdgeFromId(id);
- }
-
- typedef AlterationNotifier<BpUGraphExtender, ANode> ANodeNotifier;
- typedef AlterationNotifier<BpUGraphExtender, BNode> BNodeNotifier;
- typedef AlterationNotifier<BpUGraphExtender, Node> NodeNotifier;
- typedef AlterationNotifier<BpUGraphExtender, Edge> EdgeNotifier;
- typedef AlterationNotifier<BpUGraphExtender, UEdge> UEdgeNotifier;
-
- protected:
-
- mutable ANodeNotifier anode_notifier;
- mutable BNodeNotifier bnode_notifier;
- mutable NodeNotifier node_notifier;
- mutable EdgeNotifier edge_notifier;
- mutable UEdgeNotifier uedge_notifier;
-
- public:
-
- NodeNotifier& notifier(Node) const {
- return node_notifier;
- }
-
- ANodeNotifier& notifier(ANode) const {
- return anode_notifier;
- }
-
- BNodeNotifier& notifier(BNode) const {
- return bnode_notifier;
- }
-
- EdgeNotifier& notifier(Edge) const {
- return edge_notifier;
- }
-
- UEdgeNotifier& notifier(UEdge) const {
- return uedge_notifier;
- }
-
- class NodeIt : public Node {
- const Graph* graph;
- public:
-
- NodeIt() { }
-
- NodeIt(Invalid i) : Node(INVALID) { }
-
- explicit NodeIt(const Graph& _graph) : graph(&_graph) {
- graph->first(static_cast<Node&>(*this));
- }
-
- NodeIt(const Graph& _graph, const Node& node)
- : Node(node), graph(&_graph) { }
-
- NodeIt& operator++() {
- graph->next(*this);
- return *this;
- }
-
- };
-
- class ANodeIt : public Node {
- friend class BpUGraphExtender;
- const Graph* graph;
- public:
-
- ANodeIt() { }
-
- ANodeIt(Invalid i) : Node(INVALID) { }
-
- explicit ANodeIt(const Graph& _graph) : graph(&_graph) {
- graph->firstANode(static_cast<Node&>(*this));
- }
-
- ANodeIt(const Graph& _graph, const Node& node)
- : Node(node), graph(&_graph) {}
-
- ANodeIt& operator++() {
- graph->nextANode(*this);
- return *this;
- }
- };
-
- class BNodeIt : public Node {
- friend class BpUGraphExtender;
- const Graph* graph;
- public:
-
- BNodeIt() { }
-
- BNodeIt(Invalid i) : Node(INVALID) { }
-
- explicit BNodeIt(const Graph& _graph) : graph(&_graph) {
- graph->firstBNode(static_cast<Node&>(*this));
- }
-
- BNodeIt(const Graph& _graph, const Node& node)
- : Node(node), graph(&_graph) {}
-
- BNodeIt& operator++() {
- graph->nextBNode(*this);
- return *this;
- }
- };
-
- class EdgeIt : public Edge {
- friend class BpUGraphExtender;
- const Graph* graph;
- public:
-
- EdgeIt() { }
-
- EdgeIt(Invalid i) : Edge(INVALID) { }
-
- explicit EdgeIt(const Graph& _graph) : graph(&_graph) {
- graph->first(static_cast<Edge&>(*this));
- }
-
- EdgeIt(const Graph& _graph, const Edge& edge)
- : Edge(edge), graph(&_graph) { }
-
- EdgeIt& operator++() {
- graph->next(*this);
- return *this;
- }
-
- };
-
- class UEdgeIt : public UEdge {
- friend class BpUGraphExtender;
- const Graph* graph;
- public:
-
- UEdgeIt() { }
-
- UEdgeIt(Invalid i) : UEdge(INVALID) { }
-
- explicit UEdgeIt(const Graph& _graph) : graph(&_graph) {
- graph->first(static_cast<UEdge&>(*this));
- }
-
- UEdgeIt(const Graph& _graph, const UEdge& edge)
- : UEdge(edge), graph(&_graph) { }
-
- UEdgeIt& operator++() {
- graph->next(*this);
- return *this;
- }
- };
-
- class OutEdgeIt : public Edge {
- friend class BpUGraphExtender;
- const Graph* graph;
- public:
-
- OutEdgeIt() { }
-
- OutEdgeIt(Invalid i) : Edge(i) { }
-
- OutEdgeIt(const Graph& _graph, const Node& node)
- : graph(&_graph) {
- graph->firstOut(*this, node);
- }
-
- OutEdgeIt(const Graph& _graph, const Edge& edge)
- : Edge(edge), graph(&_graph) {}
-
- OutEdgeIt& operator++() {
- graph->nextOut(*this);
- return *this;
- }
-
- };
-
-
- class InEdgeIt : public Edge {
- friend class BpUGraphExtender;
- const Graph* graph;
- public:
-
- InEdgeIt() { }
-
- InEdgeIt(Invalid i) : Edge(i) { }
-
- InEdgeIt(const Graph& _graph, const Node& node)
- : graph(&_graph) {
- graph->firstIn(*this, node);
- }
-
- InEdgeIt(const Graph& _graph, const Edge& edge) :
- Edge(edge), graph(&_graph) {}
-
- InEdgeIt& operator++() {
- graph->nextIn(*this);
- return *this;
- }
-
- };
-
- /// \brief Base node of the iterator
- ///
- /// Returns the base node (ie. the source in this case) of the iterator
- Node baseNode(const OutEdgeIt &e) const {
- return Parent::source(static_cast<const Edge&>(e));
- }
- /// \brief Running node of the iterator
- ///
- /// Returns the running node (ie. the target in this case) of the
- /// iterator
- Node runningNode(const OutEdgeIt &e) const {
- return Parent::target(static_cast<const Edge&>(e));
- }
-
- /// \brief Base node of the iterator
- ///
- /// Returns the base node (ie. the target in this case) of the iterator
- Node baseNode(const InEdgeIt &e) const {
- return Parent::target(static_cast<const Edge&>(e));
- }
- /// \brief Running node of the iterator
- ///
- /// Returns the running node (ie. the source in this case) of the
- /// iterator
- Node runningNode(const InEdgeIt &e) const {
- return Parent::source(static_cast<const Edge&>(e));
- }
-
- class IncEdgeIt : public Parent::UEdge {
- friend class BpUGraphExtender;
- const Graph* graph;
- bool direction;
- public:
-
- IncEdgeIt() { }
-
- IncEdgeIt(Invalid i) : UEdge(i), direction(true) { }
-
- IncEdgeIt(const Graph& _graph, const Node &n) : graph(&_graph) {
- graph->firstInc(*this, direction, n);
- }
-
- IncEdgeIt(const Graph& _graph, const UEdge &ue, const Node &n)
- : graph(&_graph), UEdge(ue) {
- direction = (graph->source(ue) == n);
- }
-
- IncEdgeIt& operator++() {
- graph->nextInc(*this, direction);
- return *this;
- }
- };
-
-
- /// Base node of the iterator
- ///
- /// Returns the base node of the iterator
- Node baseNode(const IncEdgeIt &e) const {
- return e.direction ? source(e) : target(e);
- }
-
- /// Running node of the iterator
- ///
- /// Returns the running node of the iterator
- Node runningNode(const IncEdgeIt &e) const {
- return e.direction ? target(e) : source(e);
- }
-
- template <typename _Value>
- class ANodeMap
- : public MapExtender<DefaultMap<Graph, ANode, _Value> > {
- public:
- typedef BpUGraphExtender Graph;
- typedef MapExtender<DefaultMap<Graph, ANode, _Value> > Parent;
-
- ANodeMap(const Graph& graph)
- : Parent(graph) {}
- ANodeMap(const Graph& graph, const _Value& value)
- : Parent(graph, value) {}
-
- ANodeMap& operator=(const ANodeMap& cmap) {
- return operator=<ANodeMap>(cmap);
- }
-
- template <typename CMap>
- ANodeMap& operator=(const CMap& cmap) {
- Parent::operator=(cmap);
- return *this;
- }
-
- };
-
- template <typename _Value>
- class BNodeMap
- : public MapExtender<DefaultMap<Graph, BNode, _Value> > {
- public:
- typedef BpUGraphExtender Graph;
- typedef MapExtender<DefaultMap<Graph, BNode, _Value> > Parent;
-
- BNodeMap(const Graph& graph)
- : Parent(graph) {}
- BNodeMap(const Graph& graph, const _Value& value)
- : Parent(graph, value) {}
-
- BNodeMap& operator=(const BNodeMap& cmap) {
- return operator=<BNodeMap>(cmap);
- }
-
- template <typename CMap>
- BNodeMap& operator=(const CMap& cmap) {
- Parent::operator=(cmap);
- return *this;
- }
-
- };
-
- public:
-
- template <typename _Value>
- class NodeMap {
- public:
- typedef BpUGraphExtender Graph;
-
- typedef Node Key;
- typedef _Value Value;
-
- /// The reference type of the map;
- typedef typename ANodeMap<_Value>::Reference Reference;
- /// The const reference type of the map;
- typedef typename ANodeMap<_Value>::ConstReference ConstReference;
-
- typedef True ReferenceMapTag;
-
- NodeMap(const Graph& _graph)
- : graph(_graph), aNodeMap(_graph), bNodeMap(_graph) {}
- NodeMap(const Graph& _graph, const _Value& _value)
- : graph(_graph), aNodeMap(_graph, _value), bNodeMap(_graph, _value) {}
-
- NodeMap& operator=(const NodeMap& cmap) {
- return operator=<NodeMap>(cmap);
- }
-
- template <typename CMap>
- NodeMap& operator=(const CMap& cmap) {
- checkConcept<concepts::ReadMap<Node, _Value>, CMap>();
- aNodeMap = cmap;
- bNodeMap = cmap;
- return *this;
- }
-
- ConstReference operator[](const Key& node) const {
- if (Parent::aNode(node)) {
- return aNodeMap[node];
- } else {
- return bNodeMap[node];
- }
- }
-
- Reference operator[](const Key& node) {
- if (Parent::aNode(node)) {
- return aNodeMap[node];
- } else {
- return bNodeMap[node];
- }
- }
-
- void set(const Key& node, const Value& value) {
- if (Parent::aNode(node)) {
- aNodeMap.set(node, value);
- } else {
- bNodeMap.set(node, value);
- }
- }
-
- class MapIt : public NodeIt {
- public:
-
- typedef NodeIt Parent;
-
- explicit MapIt(NodeMap& _map)
- : Parent(_map.graph), map(_map) {}
-
- typename MapTraits<NodeMap>::ConstReturnValue operator*() const {
- return map[*this];
- }
-
- typename MapTraits<NodeMap>::ReturnValue operator*() {
- return map[*this];
- }
-
- void set(const Value& value) {
- map.set(*this, value);
- }
-
- private:
- NodeMap& map;
- };
-
- class ConstMapIt : public NodeIt {
- public:
-
- typedef NodeIt Parent;
-
- explicit ConstMapIt(const NodeMap& _map)
- : Parent(_map.graph), map(_map) {}
-
- typename MapTraits<NodeMap>::ConstReturnValue operator*() const {
- return map[*this];
- }
-
- private:
- const NodeMap& map;
- };
-
- class ItemIt : public NodeIt {
- public:
-
- typedef NodeIt Parent;
-
- explicit ItemIt(const NodeMap& _map)
- : Parent(_map.graph) {}
-
- };
-
- private:
- const Graph& graph;
- ANodeMap<_Value> aNodeMap;
- BNodeMap<_Value> bNodeMap;
- };
-
-
- template <typename _Value>
- class EdgeMap
- : public MapExtender<DefaultMap<Graph, Edge, _Value> > {
- public:
- typedef BpUGraphExtender Graph;
- typedef MapExtender<DefaultMap<Graph, Edge, _Value> > Parent;
-
- EdgeMap(const Graph& graph)
- : Parent(graph) {}
- EdgeMap(const Graph& graph, const _Value& value)
- : Parent(graph, value) {}
-
- EdgeMap& operator=(const EdgeMap& cmap) {
- return operator=<EdgeMap>(cmap);
- }
-
- template <typename CMap>
- EdgeMap& operator=(const CMap& cmap) {
- Parent::operator=(cmap);
- return *this;
- }
- };
-
- template <typename _Value>
- class UEdgeMap
- : public MapExtender<DefaultMap<Graph, UEdge, _Value> > {
- public:
- typedef BpUGraphExtender Graph;
- typedef MapExtender<DefaultMap<Graph, UEdge, _Value> > Parent;
-
- UEdgeMap(const Graph& graph)
- : Parent(graph) {}
- UEdgeMap(const Graph& graph, const _Value& value)
- : Parent(graph, value) {}
-
- UEdgeMap& operator=(const UEdgeMap& cmap) {
- return operator=<UEdgeMap>(cmap);
- }
-
- template <typename CMap>
- UEdgeMap& operator=(const CMap& cmap) {
- Parent::operator=(cmap);
- return *this;
- }
- };
-
-
- Node addANode() {
- Node node = Parent::addANode();
- notifier(ANode()).add(node);
- notifier(Node()).add(node);
- return node;
- }
-
- Node addBNode() {
- Node node = Parent::addBNode();
- notifier(BNode()).add(node);
- notifier(Node()).add(node);
- return node;
- }
-
- UEdge addEdge(const Node& s, const Node& t) {
- UEdge uedge = Parent::addEdge(s, t);
- notifier(UEdge()).add(uedge);
-
- std::vector<Edge> ev;
- ev.push_back(Parent::direct(uedge, true));
- ev.push_back(Parent::direct(uedge, false));
- notifier(Edge()).add(ev);
-
- return uedge;
- }
-
- void clear() {
- notifier(Edge()).clear();
- notifier(UEdge()).clear();
- notifier(Node()).clear();
- notifier(BNode()).clear();
- notifier(ANode()).clear();
- Parent::clear();
- }
-
- template <typename Graph, typename ANodeRefMap,
- typename BNodeRefMap, typename UEdgeRefMap>
- void build(const Graph& graph, ANodeRefMap& aNodeRef,
- BNodeRefMap& bNodeRef, UEdgeRefMap& uEdgeRef) {
- Parent::build(graph, aNodeRef, bNodeRef, uEdgeRef);
- notifier(ANode()).build();
- notifier(BNode()).build();
- notifier(Node()).build();
- notifier(UEdge()).build();
- notifier(Edge()).build();
- }
-
- void erase(const Node& node) {
- UEdge uedge;
- if (Parent::aNode(node)) {
- Parent::firstFromANode(uedge, node);
- while (uedge != INVALID) {
- erase(uedge);
- Parent::firstFromANode(uedge, node);
- }
- notifier(ANode()).erase(node);
- } else {
- Parent::firstFromBNode(uedge, node);
- while (uedge != INVALID) {
- erase(uedge);
- Parent::firstFromBNode(uedge, node);
- }
- notifier(BNode()).erase(node);
- }
-
- notifier(Node()).erase(node);
- Parent::erase(node);
- }
-
- void erase(const UEdge& uedge) {
- std::vector<Edge> ev;
- ev.push_back(Parent::direct(uedge, true));
- ev.push_back(Parent::direct(uedge, false));
- notifier(Edge()).erase(ev);
- notifier(UEdge()).erase(uedge);
- Parent::erase(uedge);
- }
-
-
- BpUGraphExtender() {
- anode_notifier.setContainer(*this);
- bnode_notifier.setContainer(*this);
- node_notifier.setContainer(*this);
- edge_notifier.setContainer(*this);
- uedge_notifier.setContainer(*this);
- }
-
- ~BpUGraphExtender() {
- uedge_notifier.clear();
- edge_notifier.clear();
- node_notifier.clear();
- anode_notifier.clear();
- bnode_notifier.clear();
- }
-
- Edge findEdge(Node u, Node v, Edge prev = INVALID) const {
- UEdge uedge = Parent::findUEdge(u, v, prev);
- if (uedge != INVALID) {
- return Parent::direct(uedge, Parent::aNode(u));
- } else {
- return INVALID;
- }
- }
-
- };
-
-}
-
-#endif
diff --git a/src/lemon/bits/invalid.h b/src/lemon/bits/invalid.h
deleted file mode 100644
index 164e5c3..0000000
--- a/src/lemon/bits/invalid.h
+++ /dev/null
@@ -1,54 +0,0 @@
-/* -*- C++ -*-
- *
- * This file is a part of LEMON, a generic C++ optimization library
- *
- * Copyright (C) 2003-2008
- * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
- * (Egervary Research Group on Combinatorial Optimization, EGRES).
- *
- * Permission to use, modify and distribute this software is granted
- * provided that this copyright notice appears in all copies. For
- * precise terms see the accompanying LICENSE file.
- *
- * This software is provided "AS IS" with no warranty of any kind,
- * express or implied, and with no claim as to its suitability for any
- * purpose.
- *
- */
-
-#ifndef LEMON_BITS_INVALID_H
-#define LEMON_BITS_INVALID_H
-
-///\file
-///\brief Definition of INVALID.
-
-namespace lemon {
-
- /// \brief Dummy type to make it easier to make invalid iterators.
- ///
- /// See \ref INVALID for the usage.
- struct Invalid {
- public:
- bool operator==(Invalid) { return true; }
- bool operator!=(Invalid) { return false; }
- bool operator< (Invalid) { return false; }
- };
-
- /// Invalid iterators.
-
- /// \ref Invalid is a global type that converts to each iterator
- /// in such a way that the value of the target iterator will be invalid.
-
- //Some people didn't like this:
- //const Invalid &INVALID = *(Invalid *)0;
-
-#ifdef LEMON_ONLY_TEMPLATES
- const Invalid INVALID = Invalid();
-#else
- extern const Invalid INVALID;
-#endif
-
-} //namespace lemon
-
-#endif
-
diff --git a/src/lemon/bits/map_extender.h b/src/lemon/bits/map_extender.h
deleted file mode 100644
index 68e8608..0000000
--- a/src/lemon/bits/map_extender.h
+++ /dev/null
@@ -1,321 +0,0 @@
-/* -*- C++ -*-
- *
- * This file is a part of LEMON, a generic C++ optimization library
- *
- * Copyright (C) 2003-2008
- * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
- * (Egervary Research Group on Combinatorial Optimization, EGRES).
- *
- * Permission to use, modify and distribute this software is granted
- * provided that this copyright notice appears in all copies. For
- * precise terms see the accompanying LICENSE file.
- *
- * This software is provided "AS IS" with no warranty of any kind,
- * express or implied, and with no claim as to its suitability for any
- * purpose.
- *
- */
-
-#ifndef LEMON_BITS_MAP_EXTENDER_H
-#define LEMON_BITS_MAP_EXTENDER_H
-
-#include <iterator>
-
-#include <lemon/bits/traits.h>
-
-#include <lemon/concept_check.h>
-#include <lemon/concepts/maps.h>
-
-///\file
-///\brief Extenders for iterable maps.
-
-namespace lemon {
-
- /// \ingroup graphbits
- ///
- /// \brief Extender for maps
- template <typename _Map>
- class MapExtender : public _Map {
- public:
-
- typedef _Map Parent;
- typedef MapExtender Map;
-
-
- typedef typename Parent::Graph Graph;
- typedef typename Parent::Key Item;
-
- typedef typename Parent::Key Key;
- typedef typename Parent::Value Value;
-
- class MapIt;
- class ConstMapIt;
-
- friend class MapIt;
- friend class ConstMapIt;
-
- public:
-
- MapExtender(const Graph& graph)
- : Parent(graph) {}
-
- MapExtender(const Graph& graph, const Value& value)
- : Parent(graph, value) {}
-
- MapExtender& operator=(const MapExtender& cmap) {
- return operator=<MapExtender>(cmap);
- }
-
- template <typename CMap>
- MapExtender& operator=(const CMap& cmap) {
- Parent::operator=(cmap);
- return *this;
- }
-
- class MapIt : public Item {
- public:
-
- typedef Item Parent;
- typedef typename Map::Value Value;
-
- MapIt() {}
-
- MapIt(Invalid i) : Parent(i) { }
-
- explicit MapIt(Map& _map) : map(_map) {
- map.notifier()->first(*this);
- }
-
- MapIt(const Map& _map, const Item& item)
- : Parent(item), map(_map) {}
-
- MapIt& operator++() {
- map.notifier()->next(*this);
- return *this;
- }
-
- typename MapTraits<Map>::ConstReturnValue operator*() const {
- return map[*this];
- }
-
- typename MapTraits<Map>::ReturnValue operator*() {
- return map[*this];
- }
-
- void set(const Value& value) {
- map.set(*this, value);
- }
-
- protected:
- Map& map;
-
- };
-
- class ConstMapIt : public Item {
- public:
-
- typedef Item Parent;
-
- typedef typename Map::Value Value;
-
- ConstMapIt() {}
-
- ConstMapIt(Invalid i) : Parent(i) { }
-
- explicit ConstMapIt(Map& _map) : map(_map) {
- map.notifier()->first(*this);
- }
-
- ConstMapIt(const Map& _map, const Item& item)
- : Parent(item), map(_map) {}
-
- ConstMapIt& operator++() {
- map.notifier()->next(*this);
- return *this;
- }
-
- typename MapTraits<Map>::ConstReturnValue operator*() const {
- return map[*this];
- }
-
- protected:
- const Map& map;
- };
-
- class ItemIt : public Item {
- public:
-
- typedef Item Parent;
-
- ItemIt() {}
-
- ItemIt(Invalid i) : Parent(i) { }
-
- explicit ItemIt(Map& _map) : map(_map) {
- map.notifier()->first(*this);
- }
-
- ItemIt(const Map& _map, const Item& item)
- : Parent(item), map(_map) {}
-
- ItemIt& operator++() {
- map.notifier()->next(*this);
- return *this;
- }
-
- protected:
- const Map& map;
-
- };
- };
-
- /// \ingroup graphbits
- ///
- /// \brief Extender for maps which use a subset of the items.
- template <typename _Graph, typename _Map>
- class SubMapExtender : public _Map {
- public:
-
- typedef _Map Parent;
- typedef SubMapExtender Map;
-
- typedef _Graph Graph;
-
- typedef typename Parent::Key Item;
-
- typedef typename Parent::Key Key;
- typedef typename Parent::Value Value;
-
- class MapIt;
- class ConstMapIt;
-
- friend class MapIt;
- friend class ConstMapIt;
-
- public:
-
- SubMapExtender(const Graph& _graph)
- : Parent(_graph), graph(_graph) {}
-
- SubMapExtender(const Graph& _graph, const Value& _value)
- : Parent(_graph, _value), graph(_graph) {}
-
- SubMapExtender& operator=(const SubMapExtender& cmap) {
- return operator=<MapExtender>(cmap);
- }
-
- template <typename CMap>
- SubMapExtender& operator=(const CMap& cmap) {
- checkConcept<concepts::ReadMap<Key, Value>, CMap>();
- Item it;
- for (graph.first(it); it != INVALID; graph.next(it)) {
- Parent::set(it, cmap[it]);
- }
- return *this;
- }
-
- class MapIt : public Item {
- public:
-
- typedef Item Parent;
- typedef typename Map::Value Value;
-
- MapIt() {}
-
- MapIt(Invalid i) : Parent(i) { }
-
- explicit MapIt(Map& _map) : map(_map) {
- map.graph.first(*this);
- }
-
- MapIt(const Map& _map, const Item& item)
- : Parent(item), map(_map) {}
-
- MapIt& operator++() {
- map.graph.next(*this);
- return *this;
- }
-
- typename MapTraits<Map>::ConstReturnValue operator*() const {
- return map[*this];
- }
-
- typename MapTraits<Map>::ReturnValue operator*() {
- return map[*this];
- }
-
- void set(const Value& value) {
- map.set(*this, value);
- }
-
- protected:
- Map& map;
-
- };
-
- class ConstMapIt : public Item {
- public:
-
- typedef Item Parent;
-
- typedef typename Map::Value Value;
-
- ConstMapIt() {}
-
- ConstMapIt(Invalid i) : Parent(i) { }
-
- explicit ConstMapIt(Map& _map) : map(_map) {
- map.graph.first(*this);
- }
-
- ConstMapIt(const Map& _map, const Item& item)
- : Parent(item), map(_map) {}
-
- ConstMapIt& operator++() {
- map.graph.next(*this);
- return *this;
- }
-
- typename MapTraits<Map>::ConstReturnValue operator*() const {
- return map[*this];
- }
-
- protected:
- const Map& map;
- };
-
- class ItemIt : public Item {
- public:
-
- typedef Item Parent;
-
- ItemIt() {}
-
- ItemIt(Invalid i) : Parent(i) { }
-
- explicit ItemIt(Map& _map) : map(_map) {
- map.graph.first(*this);
- }
-
- ItemIt(const Map& _map, const Item& item)
- : Parent(item), map(_map) {}
-
- ItemIt& operator++() {
- map.graph.next(*this);
- return *this;
- }
-
- protected:
- const Map& map;
-
- };
-
- private:
-
- const Graph& graph;
-
- };
-
-}
-
-#endif
diff --git a/src/lemon/bits/path_dump.h b/src/lemon/bits/path_dump.h
deleted file mode 100644
index ccef70c..0000000
--- a/src/lemon/bits/path_dump.h
+++ /dev/null
@@ -1,174 +0,0 @@
-/* -*- C++ -*-
- *
- * This file is a part of LEMON, a generic C++ optimization library
- *
- * Copyright (C) 2003-2008
- * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
- * (Egervary Research Group on Combinatorial Optimization, EGRES).
- *
- * Permission to use, modify and distribute this software is granted
- * provided that this copyright notice appears in all copies. For
- * precise terms see the accompanying LICENSE file.
- *
- * This software is provided "AS IS" with no warranty of any kind,
- * express or implied, and with no claim as to its suitability for any
- * purpose.
- *
- */
-
-#ifndef LEMON_BITS_PRED_MAP_PATH_H
-#define LEMON_BITS_PRED_MAP_PATH_H
-
-namespace lemon {
-
- template <typename _Graph, typename _PredMap>
- class PredMapPath {
- public:
- typedef True RevPathTag;
-
- typedef _Graph Graph;
- typedef typename Graph::Edge Edge;
- typedef _PredMap PredMap;
-
- PredMapPath(const Graph& _graph, const PredMap& _predMap,
- typename Graph::Node _target)
- : graph(_graph), predMap(_predMap), target(_target) {}
-
- int length() const {
- int len = 0;
- typename Graph::Node node = target;
- typename Graph::Edge edge;
- while ((edge = predMap[node]) != INVALID) {
- node = graph.source(edge);
- ++len;
- }
- return len;
- }
-
- bool empty() const {
- return predMap[target] != INVALID;
- }
-
- class RevEdgeIt {
- public:
- RevEdgeIt() {}
- RevEdgeIt(Invalid) : path(0), current(INVALID) {}
- RevEdgeIt(const PredMapPath& _path)
- : path(&_path), current(_path.target) {
- if (path->predMap[current] == INVALID) current = INVALID;
- }
-
- operator const typename Graph::Edge() const {
- return path->predMap[current];
- }
-
- RevEdgeIt& operator++() {
- current = path->graph.source(path->predMap[current]);
- if (path->predMap[current] == INVALID) current = INVALID;
- return *this;
- }
-
- bool operator==(const RevEdgeIt& e) const {
- return current == e.current;
- }
-
- bool operator!=(const RevEdgeIt& e) const {
- return current != e.current;
- }
-
- bool operator<(const RevEdgeIt& e) const {
- return current < e.current;
- }
-
- private:
- const PredMapPath* path;
- typename Graph::Node current;
- };
-
- private:
- const Graph& graph;
- const PredMap& predMap;
- typename Graph::Node target;
- };
-
-
- template <typename _Graph, typename _PredMatrixMap>
- class PredMatrixMapPath {
- public:
- typedef True RevPathTag;
-
- typedef _Graph Graph;
- typedef typename Graph::Edge Edge;
- typedef _PredMatrixMap PredMatrixMap;
-
- PredMatrixMapPath(const Graph& _graph,
- const PredMatrixMap& _predMatrixMap,
- typename Graph::Node _source,
- typename Graph::Node _target)
- : graph(_graph), predMatrixMap(_predMatrixMap),
- source(_source), target(_target) {}
-
- int length() const {
- int len = 0;
- typename Graph::Node node = target;
- typename Graph::Edge edge;
- while ((edge = predMatrixMap(source, node)) != INVALID) {
- node = graph.source(edge);
- ++len;
- }
- return len;
- }
-
- bool empty() const {
- return source != target;
- }
-
- class RevEdgeIt {
- public:
- RevEdgeIt() {}
- RevEdgeIt(Invalid) : path(0), current(INVALID) {}
- RevEdgeIt(const PredMatrixMapPath& _path)
- : path(&_path), current(_path.target) {
- if (path->predMatrixMap(path->source, current) == INVALID)
- current = INVALID;
- }
-
- operator const typename Graph::Edge() const {
- return path->predMatrixMap(path->source, current);
- }
-
- RevEdgeIt& operator++() {
- current =
- path->graph.source(path->predMatrixMap(path->source, current));
- if (path->predMatrixMap(path->source, current) == INVALID)
- current = INVALID;
- return *this;
- }
-
- bool operator==(const RevEdgeIt& e) const {
- return current == e.current;
- }
-
- bool operator!=(const RevEdgeIt& e) const {
- return current != e.current;
- }
-
- bool operator<(const RevEdgeIt& e) const {
- return current < e.current;
- }
-
- private:
- const PredMatrixMapPath* path;
- typename Graph::Node current;
- };
-
- private:
- const Graph& graph;
- const PredMatrixMap& predMatrixMap;
- typename Graph::Node source;
- typename Graph::Node target;
- };
-
-}
-
-#endif
diff --git a/src/lemon/bits/traits.h b/src/lemon/bits/traits.h
deleted file mode 100644
index 6eb5b75..0000000
--- a/src/lemon/bits/traits.h
+++ /dev/null
@@ -1,346 +0,0 @@
-/* -*- C++ -*-
- *
- * This file is a part of LEMON, a generic C++ optimization library
- *
- * Copyright (C) 2003-2008
- * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
- * (Egervary Research Group on Combinatorial Optimization, EGRES).
- *
- * Permission to use, modify and distribute this software is granted
- * provided that this copyright notice appears in all copies. For
- * precise terms see the accompanying LICENSE file.
- *
- * This software is provided "AS IS" with no warranty of any kind,
- * express or implied, and with no claim as to its suitability for any
- * purpose.
- *
- */
-
-#ifndef LEMON_BITS_TRAITS_H
-#define LEMON_BITS_TRAITS_H
-
-#include <lemon/bits/utility.h>
-
-///\file
-///\brief Traits for graphs and maps
-///
-
-namespace lemon {
- template <typename _Graph, typename _Item>
- class ItemSetTraits {};
-
-
- template <typename Graph, typename Enable = void>
- struct NodeNotifierIndicator {
- typedef InvalidType Type;
- };
- template <typename Graph>
- struct NodeNotifierIndicator<
- Graph,
- typename enable_if<typename Graph::NodeNotifier::Notifier, void>::type
- > {
- typedef typename Graph::NodeNotifier Type;
- };
-
- template <typename _Graph>
- class ItemSetTraits<_Graph, typename _Graph::Node> {
- public:
-
- typedef _Graph Graph;
-
- typedef typename Graph::Node Item;
- typedef typename Graph::NodeIt ItemIt;
-
- typedef typename NodeNotifierIndicator<Graph>::Type ItemNotifier;
-
- template <typename _Value>
- class Map : public Graph::template NodeMap<_Value> {
- public:
- typedef typename Graph::template NodeMap<_Value> Parent;
- typedef typename Graph::template NodeMap<_Value> Type;
- typedef typename Parent::Value Value;
-
- Map(const Graph& _graph) : Parent(_graph) {}
- Map(const Graph& _graph, const Value& _value)
- : Parent(_graph, _value) {}
-
- };
-
- };
-
- template <typename Graph, typename Enable = void>
- struct EdgeNotifierIndicator {
- typedef InvalidType Type;
- };
- template <typename Graph>
- struct EdgeNotifierIndicator<
- Graph,
- typename enable_if<typename Graph::EdgeNotifier::Notifier, void>::type
- > {
- typedef typename Graph::EdgeNotifier Type;
- };
-
- template <typename _Graph>
- class ItemSetTraits<_Graph, typename _Graph::Edge> {
- public:
-
- typedef _Graph Graph;
-
- typedef typename Graph::Edge Item;
- typedef typename Graph::EdgeIt ItemIt;
-
- typedef typename EdgeNotifierIndicator<Graph>::Type ItemNotifier;
-
- template <typename _Value>
- class Map : public Graph::template EdgeMap<_Value> {
- public:
- typedef typename Graph::template EdgeMap<_Value> Parent;
- typedef typename Graph::template EdgeMap<_Value> Type;
- typedef typename Parent::Value Value;
-
- Map(const Graph& _graph) : Parent(_graph) {}
- Map(const Graph& _graph, const Value& _value)
- : Parent(_graph, _value) {}
- };
-
- };
-
- template <typename Graph, typename Enable = void>
- struct UEdgeNotifierIndicator {
- typedef InvalidType Type;
- };
- template <typename Graph>
- struct UEdgeNotifierIndicator<
- Graph,
- typename enable_if<typename Graph::UEdgeNotifier::Notifier, void>::type
- > {
- typedef typename Graph::UEdgeNotifier Type;
- };
-
- template <typename _Graph>
- class ItemSetTraits<_Graph, typename _Graph::UEdge> {
- public:
-
- typedef _Graph Graph;
-
- typedef typename Graph::UEdge Item;
- typedef typename Graph::UEdgeIt ItemIt;
-
- typedef typename UEdgeNotifierIndicator<Graph>::Type ItemNotifier;
-
- template <typename _Value>
- class Map : public Graph::template UEdgeMap<_Value> {
- public:
- typedef typename Graph::template UEdgeMap<_Value> Parent;
- typedef typename Graph::template UEdgeMap<_Value> Type;
- typedef typename Parent::Value Value;
-
- Map(const Graph& _graph) : Parent(_graph) {}
- Map(const Graph& _graph, const Value& _value)
- : Parent(_graph, _value) {}
- };
-
- };
-
- template <typename Graph, typename Enable = void>
- struct ANodeNotifierIndicator {
- typedef InvalidType Type;
- };
- template <typename Graph>
- struct ANodeNotifierIndicator<
- Graph,
- typename enable_if<typename Graph::ANodeNotifier::Notifier, void>::type
- > {
- typedef typename Graph::ANodeNotifier Type;
- };
-
- template <typename _Graph>
- class ItemSetTraits<_Graph, typename _Graph::ANode> {
- public:
-
- typedef _Graph Graph;
-
- typedef typename Graph::ANode Item;
- typedef typename Graph::ANodeIt ItemIt;
-
- typedef typename ANodeNotifierIndicator<Graph>::Type ItemNotifier;
-
- template <typename _Value>
- class Map : public Graph::template ANodeMap<_Value> {
- public:
- typedef typename Graph::template ANodeMap<_Value> Parent;
- typedef typename Graph::template ANodeMap<_Value> Type;
- typedef typename Parent::Value Value;
-
- Map(const Graph& _graph) : Parent(_graph) {}
- Map(const Graph& _graph, const Value& _value)
- : Parent(_graph, _value) {}
- };
-
- };
-
- template <typename Graph, typename Enable = void>
- struct BNodeNotifierIndicator {
- typedef InvalidType Type;
- };
- template <typename Graph>
- struct BNodeNotifierIndicator<
- Graph,
- typename enable_if<typename Graph::BNodeNotifier::Notifier, void>::type
- > {
- typedef typename Graph::BNodeNotifier Type;
- };
-
- template <typename _Graph>
- class ItemSetTraits<_Graph, typename _Graph::BNode> {
- public:
-
- typedef _Graph Graph;
-
- typedef typename Graph::BNode Item;
- typedef typename Graph::BNodeIt ItemIt;
-
- typedef typename BNodeNotifierIndicator<Graph>::Type ItemNotifier;
-
- template <typename _Value>
- class Map : public Graph::template BNodeMap<_Value> {
- public:
- typedef typename Graph::template BNodeMap<_Value> Parent;
- typedef typename Graph::template BNodeMap<_Value> Type;
- typedef typename Parent::Value Value;
-
- Map(const Graph& _graph) : Parent(_graph) {}
- Map(const Graph& _graph, const Value& _value)
- : Parent(_graph, _value) {}
- };
-
- };
-
-
- template <typename Map, typename Enable = void>
- struct MapTraits {
- typedef False ReferenceMapTag;
-
- typedef typename Map::Key Key;
- typedef typename Map::Value Value;
-
- typedef const Value ConstReturnValue;
- typedef const Value ReturnValue;
- };
-
- template <typename Map>
- struct MapTraits<
- Map, typename enable_if<typename Map::ReferenceMapTag, void>::type >
- {
- typedef True ReferenceMapTag;
-
- typedef typename Map::Key Key;
- typedef typename Map::Value Value;
-
- typedef typename Map::ConstReference ConstReturnValue;
- typedef typename Map::Reference ReturnValue;
-
- typedef typename Map::ConstReference ConstReference;
- typedef typename Map::Reference Reference;
- };
-
- template <typename MatrixMap, typename Enable = void>
- struct MatrixMapTraits {
- typedef False ReferenceMapTag;
-
- typedef typename MatrixMap::FirstKey FirstKey;
- typedef typename MatrixMap::SecondKey SecondKey;
- typedef typename MatrixMap::Value Value;
-
- typedef const Value ConstReturnValue;
- typedef const Value ReturnValue;
- };
-
- template <typename MatrixMap>
- struct MatrixMapTraits<
- MatrixMap, typename enable_if<typename MatrixMap::ReferenceMapTag,
- void>::type >
- {
- typedef True ReferenceMapTag;
-
- typedef typename MatrixMap::FirstKey FirstKey;
- typedef typename MatrixMap::SecondKey SecondKey;
- typedef typename MatrixMap::Value Value;
-
- typedef typename MatrixMap::ConstReference ConstReturnValue;
- typedef typename MatrixMap::Reference ReturnValue;
-
- typedef typename MatrixMap::ConstReference ConstReference;
- typedef typename MatrixMap::Reference Reference;
- };
-
- // Indicators for the tags
-
- template <typename Graph, typename Enable = void>
- struct NodeNumTagIndicator {
- static const bool value = false;
- };
-
- template <typename Graph>
- struct NodeNumTagIndicator<
- Graph,
- typename enable_if<typename Graph::NodeNumTag, void>::type
- > {
- static const bool value = true;
- };
-
- template <typename Graph, typename Enable = void>
- struct EdgeNumTagIndicator {
- static const bool value = false;
- };
-
- template <typename Graph>
- struct EdgeNumTagIndicator<
- Graph,
- typename enable_if<typename Graph::EdgeNumTag, void>::type
- > {
- static const bool value = true;
- };
-
- template <typename Graph, typename Enable = void>
- struct FindEdgeTagIndicator {
- static const bool value = false;
- };
-
- template <typename Graph>
- struct FindEdgeTagIndicator<
- Graph,
- typename enable_if<typename Graph::FindEdgeTag, void>::type
- > {
- static const bool value = true;
- };
-
- template <typename Graph, typename Enable = void>
- struct UndirectedTagIndicator {
- static const bool value = false;
- };
-
- template <typename Graph>
- struct UndirectedTagIndicator<
- Graph,
- typename enable_if<typename Graph::UndirectedTag, void>::type
- > {
- static const bool value = true;
- };
-
- template <typename Graph, typename Enable = void>
- struct BuildTagIndicator {
- static const bool value = false;
- };
-
- template <typename Graph>
- struct BuildTagIndicator<
- Graph,
- typename enable_if<typename Graph::BuildTag, void>::type
- > {
- static const bool value = true;
- };
-
-}
-
-#endif
diff --git a/src/lemon/bits/utility.h b/src/lemon/bits/utility.h
deleted file mode 100644
index 34fa159..0000000
--- a/src/lemon/bits/utility.h
+++ /dev/null
@@ -1,140 +0,0 @@
-/* -*- C++ -*-
- *
- * This file is a part of LEMON, a generic C++ optimization library
- *
- * Copyright (C) 2003-2008
- * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
- * (Egervary Research Group on Combinatorial Optimization, EGRES).
- *
- * Permission to use, modify and distribute this software is granted
- * provided that this copyright notice appears in all copies. For
- * precise terms see the accompanying LICENSE file.
- *
- * This software is provided "AS IS" with no warranty of any kind,
- * express or implied, and with no claim as to its suitability for any
- * purpose.
- *
- */
-
-// This file contains a modified version of the enable_if library from BOOST.
-// See the appropriate copyright notice below.
-
-// Boost enable_if library
-
-// Copyright 2003 � The Trustees of Indiana University.
-
-// Use, modification, and distribution is subject to the Boost Software
-// License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
-// http://www.boost.org/LICENSE_1_0.txt)
-
-// Authors: Jaakko J�rvi (jajarvi at osl.iu.edu)
-// Jeremiah Willcock (jewillco at osl.iu.edu)
-// Andrew Lumsdaine (lums at osl.iu.edu)
-
-
-#ifndef LEMON_BITS_UTILITY_H
-#define LEMON_BITS_UTILITY_H
-
-///\file
-///\brief Miscellaneous basic utilities
-///
-///\todo Please rethink the organisation of the basic files like this.
-///E.g. this file might be merged with invalid.h.
-
-
-namespace lemon
-{
-
- /// Basic type for defining "tags". A "YES" condition for \c enable_if.
-
- /// Basic type for defining "tags". A "YES" condition for \c enable_if.
- ///
- ///\sa False
- ///
- /// \todo This should go to a separate "basic_types.h" (or something)
- /// file.
- struct True {
- ///\e
- static const bool value = true;
- };
-
- /// Basic type for defining "tags". A "NO" condition for \c enable_if.
-
- /// Basic type for defining "tags". A "NO" condition for \c enable_if.
- ///
- ///\sa True
- struct False {
- ///\e
- static const bool value = false;
- };
-
-
- struct InvalidType {
- };
-
- template <typename T>
- struct Wrap {
- const T &value;
- Wrap(const T &t) : value(t) {}
- };
-
- /**************** dummy class to avoid ambiguity ****************/
-
- template<int T> struct dummy { dummy(int) {} };
-
- /**************** enable_if from BOOST ****************/
-
- template <typename Type, typename T = void>
- struct exists {
- typedef T type;
- };
-
-
- template <bool B, class T = void>
- struct enable_if_c {
- typedef T type;
- };
-
- template <class T>
- struct enable_if_c<false, T> {};
-
- template <class Cond, class T = void>
- struct enable_if : public enable_if_c<Cond::value, T> {};
-
- template <bool B, class T>
- struct lazy_enable_if_c {
- typedef typename T::type type;
- };
-
- template <class T>
- struct lazy_enable_if_c<false, T> {};
-
- template <class Cond, class T>
- struct lazy_enable_if : public lazy_enable_if_c<Cond::value, T> {};
-
-
- template <bool B, class T = void>
- struct disable_if_c {
- typedef T type;
- };
-
- template <class T>
- struct disable_if_c<true, T> {};
-
- template <class Cond, class T = void>
- struct disable_if : public disable_if_c<Cond::value, T> {};
-
- template <bool B, class T>
- struct lazy_disable_if_c {
- typedef typename T::type type;
- };
-
- template <class T>
- struct lazy_disable_if_c<true, T> {};
-
- template <class Cond, class T>
- struct lazy_disable_if : public lazy_disable_if_c<Cond::value, T> {};
-
-} // namespace lemon
-
-#endif
diff --git a/src/lemon/bits/variant.h b/src/lemon/bits/variant.h
deleted file mode 100644
index 1f71484..0000000
--- a/src/lemon/bits/variant.h
+++ /dev/null
@@ -1,508 +0,0 @@
-/* -*- C++ -*-
- *
- * This file is a part of LEMON, a generic C++ optimization library
- *
- * Copyright (C) 2003-2008
- * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
- * (Egervary Research Group on Combinatorial Optimization, EGRES).
- *
- * Permission to use, modify and distribute this software is granted
- * provided that this copyright notice appears in all copies. For
- * precise terms see the accompanying LICENSE file.
- *
- * This software is provided "AS IS" with no warranty of any kind,
- * express or implied, and with no claim as to its suitability for any
- * purpose.
- *
- */
-
-#ifndef LEMON_BITS_VARIANT_H
-#define LEMON_BITS_VARIANT_H
-
-#include <lemon/error.h>
-
-/// \file
-/// \brief Variant types
-
-namespace lemon {
-
- namespace _variant_bits {
-
- template <int left, int right>
- struct CTMax {
- static const int value = left < right ? right : left;
- };
-
- }
-
-
- /// \brief Simple Variant type for two types
- ///
- /// Simple Variant type for two types. The Variant type is a type
- /// safe union. The C++ has strong limitations for using unions, by
- /// example we can not store type with non default constructor or
- /// destructor in an union. This class always knowns the current
- /// state of the variant and it cares for the proper construction
- /// and destruction.
- template <typename _First, typename _Second>
- class BiVariant {
- public:
-
- /// \brief The \c First type.
- typedef _First First;
- /// \brief The \c Second type.
- typedef _Second Second;
-
- struct WrongStateError : public lemon::LogicError {
- public:
- virtual const char* what() const throw() {
- return "lemon::BiVariant::WrongStateError";
- }
- };
-
- /// \brief Constructor
- ///
- /// This constructor initalizes to the default value of the \c First
- /// type.
- BiVariant() {
- flag = true;
- new(reinterpret_cast<First*>(data)) First();
- }
-
- /// \brief Constructor
- ///
- /// This constructor initalizes to the given value of the \c First
- /// type.
- BiVariant(const First& f) {
- flag = true;
- new(reinterpret_cast<First*>(data)) First(f);
- }
-
- /// \brief Constructor
- ///
- /// This constructor initalizes to the given value of the \c
- /// Second type.
- BiVariant(const Second& s) {
- flag = false;
- new(reinterpret_cast<Second*>(data)) Second(s);
- }
-
- /// \brief Copy constructor
- ///
- /// Copy constructor
- BiVariant(const BiVariant& bivariant) {
- flag = bivariant.flag;
- if (flag) {
- new(reinterpret_cast<First*>(data)) First(bivariant.first());
- } else {
- new(reinterpret_cast<Second*>(data)) Second(bivariant.second());
- }
- }
-
- /// \brief Destrcutor
- ///
- /// Destructor
- ~BiVariant() {
- destroy();
- }
-
- /// \brief Set to the default value of the \c First type.
- ///
- /// This function sets the variant to the default value of the \c
- /// First type.
- BiVariant& setFirst() {
- destroy();
- flag = true;
- new(reinterpret_cast<First*>(data)) First();
- return *this;
- }
-
- /// \brief Set to the given value of the \c First type.
- ///
- /// This function sets the variant to the given value of the \c
- /// First type.
- BiVariant& setFirst(const First& f) {
- destroy();
- flag = true;
- new(reinterpret_cast<First*>(data)) First(f);
- return *this;
- }
-
- /// \brief Set to the default value of the \c Second type.
- ///
- /// This function sets the variant to the default value of the \c
- /// Second type.
- BiVariant& setSecond() {
- destroy();
- flag = false;
- new(reinterpret_cast<Second*>(data)) Second();
- return *this;
- }
-
- /// \brief Set to the given value of the \c Second type.
- ///
- /// This function sets the variant to the given value of the \c
- /// Second type.
- BiVariant& setSecond(const Second& s) {
- destroy();
- flag = false;
- new(reinterpret_cast<Second*>(data)) Second(s);
- return *this;
- }
-
- /// \brief Operator form of the \c setFirst()
- BiVariant& operator=(const First& f) {
- return setFirst(f);
- }
-
- /// \brief Operator form of the \c setSecond()
- BiVariant& operator=(const Second& s) {
- return setSecond(s);
- }
-
- /// \brief Assign operator
- BiVariant& operator=(const BiVariant& bivariant) {
- if (this == &bivariant) return *this;
- destroy();
- flag = bivariant.flag;
- if (flag) {
- new(reinterpret_cast<First*>(data)) First(bivariant.first());
- } else {
- new(reinterpret_cast<Second*>(data)) Second(bivariant.second());
- }
- return *this;
- }
-
- /// \brief Reference to the value
- ///
- /// Reference to the value of the \c First type.
- /// \pre The BiVariant should store value of \c First type.
- First& first() {
- LEMON_ASSERT(flag, WrongStateError());
- return *reinterpret_cast<First*>(data);
- }
-
- /// \brief Const reference to the value
- ///
- /// Const reference to the value of the \c First type.
- /// \pre The BiVariant should store value of \c First type.
- const First& first() const {
- LEMON_ASSERT(flag, WrongStateError());
- return *reinterpret_cast<const First*>(data);
- }
-
- /// \brief Operator form of the \c first()
- operator First&() { return first(); }
- /// \brief Operator form of the const \c first()
- operator const First&() const { return first(); }
-
- /// \brief Reference to the value
- ///
- /// Reference to the value of the \c Second type.
- /// \pre The BiVariant should store value of \c Second type.
- Second& second() {
- LEMON_ASSERT(!flag, WrongStateError());
- return *reinterpret_cast<Second*>(data);
- }
-
- /// \brief Const reference to the value
- ///
- /// Const reference to the value of the \c Second type.
- /// \pre The BiVariant should store value of \c Second type.
- const Second& second() const {
- LEMON_ASSERT(!flag, WrongStateError());
- return *reinterpret_cast<const Second*>(data);
- }
-
- /// \brief Operator form of the \c second()
- operator Second&() { return second(); }
- /// \brief Operator form of the const \c second()
- operator const Second&() const { return second(); }
-
- /// \brief %True when the variant is in the first state
- ///
- /// %True when the variant stores value of the \c First type.
- bool firstState() const { return flag; }
-
- /// \brief %True when the variant is in the second state
- ///
- /// %True when the variant stores value of the \c Second type.
- bool secondState() const { return !flag; }
-
- private:
-
- void destroy() {
- if (flag) {
- reinterpret_cast<First*>(data)->~First();
- } else {
- reinterpret_cast<Second*>(data)->~Second();
- }
- }
-
- char data[_variant_bits::CTMax<sizeof(First), sizeof(Second)>::value];
- bool flag;
- };
-
- namespace _variant_bits {
-
- template <int _idx, typename _TypeMap>
- struct Memory {
-
- typedef typename _TypeMap::template Map<_idx>::Type Current;
-
- static void destroy(int index, char* place) {
- if (index == _idx) {
- reinterpret_cast<Current*>(place)->~Current();
- } else {
- Memory<_idx - 1, _TypeMap>::destroy(index, place);
- }
- }
-
- static void copy(int index, char* to, const char* from) {
- if (index == _idx) {
- new (reinterpret_cast<Current*>(to))
- Current(reinterpret_cast<const Current*>(from));
- } else {
- Memory<_idx - 1, _TypeMap>::copy(index, to, from);
- }
- }
-
- };
-
- template <typename _TypeMap>
- struct Memory<-1, _TypeMap> {
-
- static void destroy(int, char*) {
- LEMON_ASSERT(false, "Wrong Variant Index.");
- }
-
- static void copy(int, char*, const char*) {
- LEMON_ASSERT(false, "Wrong Variant Index.");
- }
- };
-
- template <int _idx, typename _TypeMap>
- struct Size {
- static const int value =
- CTMax<sizeof(typename _TypeMap::template Map<_idx>::Type),
- Size<_idx - 1, _TypeMap>::value>::value;
- };
-
- template <typename _TypeMap>
- struct Size<0, _TypeMap> {
- static const int value =
- sizeof(typename _TypeMap::template Map<0>::Type);
- };
-
- }
-
- /// \brief Variant type
- ///
- /// Simple Variant type. The Variant type is a type safe union. The
- /// C++ has strong limitations for using unions, by example we
- /// cannot store type with non default constructor or destructor in
- /// a union. This class always knowns the current state of the
- /// variant and it cares for the proper construction and
- /// destruction.
- ///
- /// \param _num The number of the types which can be stored in the
- /// variant type.
- /// \param _TypeMap This class describes the types of the Variant. The
- /// _TypeMap::Map<index>::Type should be a valid type for each index
- /// in the range {0, 1, ..., _num - 1}. The \c VariantTypeMap is helper
- /// class to define such type mappings up to 10 types.
- ///
- /// And the usage of the class:
- ///\code
- /// typedef Variant<3, VariantTypeMap<int, std::string, double> > MyVariant;
- /// MyVariant var;
- /// var.set<0>(12);
- /// std::cout << var.get<0>() << std::endl;
- /// var.set<1>("alpha");
- /// std::cout << var.get<1>() << std::endl;
- /// var.set<2>(0.75);
- /// std::cout << var.get<2>() << std::endl;
- ///\endcode
- ///
- /// The result of course:
- ///\code
- /// 12
- /// alpha
- /// 0.75
- ///\endcode
- template <int _num, typename _TypeMap>
- class Variant {
- public:
-
- static const int num = _num;
-
- typedef _TypeMap TypeMap;
-
- struct WrongStateError : public lemon::LogicError {
- public:
- virtual const char* what() const throw() {
- return "lemon::Variant::WrongStateError";
- }
- };
-
- /// \brief Constructor
- ///
- /// This constructor initalizes to the default value of the \c type
- /// with 0 index.
- Variant() {
- flag = 0;
- new(reinterpret_cast<typename TypeMap::template Map<0>::Type*>(data))
- typename TypeMap::template Map<0>::Type();
- }
-
-
- /// \brief Copy constructor
- ///
- /// Copy constructor
- Variant(const Variant& variant) {
- flag = variant.flag;
- _variant_bits::Memory<num - 1, TypeMap>::copy(flag, data, variant.data);
- }
-
- /// \brief Assign operator
- ///
- /// Assign operator
- Variant& operator=(const Variant& variant) {
- if (this == &variant) return *this;
- _variant_bits::Memory<num - 1, TypeMap>::
- destroy(flag, data);
- flag = variant.flag;
- _variant_bits::Memory<num - 1, TypeMap>::
- copy(flag, data, variant.data);
- return *this;
- }
-
- /// \brief Destrcutor
- ///
- /// Destructor
- ~Variant() {
- _variant_bits::Memory<num - 1, TypeMap>::destroy(flag, data);
- }
-
- /// \brief Set to the default value of the type with \c _idx index.
- ///
- /// This function sets the variant to the default value of the
- /// type with \c _idx index.
- template <int _idx>
- Variant& set() {
- _variant_bits::Memory<num - 1, TypeMap>::destroy(flag, data);
- flag = _idx;
- new(reinterpret_cast<typename TypeMap::template Map<_idx>::Type*>(data))
- typename TypeMap::template Map<_idx>::Type();
- return *this;
- }
-
- /// \brief Set to the given value of the type with \c _idx index.
- ///
- /// This function sets the variant to the given value of the type
- /// with \c _idx index.
- template <int _idx>
- Variant& set(const typename _TypeMap::template Map<_idx>::Type& init) {
- _variant_bits::Memory<num - 1, TypeMap>::destroy(flag, data);
- flag = _idx;
- new(reinterpret_cast<typename TypeMap::template Map<_idx>::Type*>(data))
- typename TypeMap::template Map<_idx>::Type(init);
- return *this;
- }
-
- /// \brief Gets the current value of the type with \c _idx index.
- ///
- /// Gets the current value of the type with \c _idx index.
- template <int _idx>
- const typename TypeMap::template Map<_idx>::Type& get() const {
- LEMON_ASSERT(_idx == flag, "Wrong Variant Index.");
- return *reinterpret_cast<const typename TypeMap::
- template Map<_idx>::Type*>(data);
- }
-
- /// \brief Gets the current value of the type with \c _idx index.
- ///
- /// Gets the current value of the type with \c _idx index.
- template <int _idx>
- typename _TypeMap::template Map<_idx>::Type& get() {
- LEMON_ASSERT(_idx == flag, "Wrong Variant Index.");
- return *reinterpret_cast<typename TypeMap::template Map<_idx>::Type*>
- (data);
- }
-
- /// \brief Returns the current state of the variant.
- ///
- /// Returns the current state of the variant.
- int state() const {
- return flag;
- }
-
- private:
-
- char data[_variant_bits::Size<num - 1, TypeMap>::value];
- int flag;
- };
-
- namespace _variant_bits {
-
- template <int _index, typename _List>
- struct Get {
- typedef typename Get<_index - 1, typename _List::Next>::Type Type;
- };
-
- template <typename _List>
- struct Get<0, _List> {
- typedef typename _List::Type Type;
- };
-
- struct List {};
-
- template <typename _Type, typename _List>
- struct Insert {
- typedef _List Next;
- typedef _Type Type;
- };
-
- template <int _idx, typename _T0, typename _T1, typename _T2,
- typename _T3, typename _T5, typename _T4, typename _T6,
- typename _T7, typename _T8, typename _T9>
- struct Mapper {
- typedef List L10;
- typedef Insert<_T9, L10> L9;
- typedef Insert<_T8, L9> L8;
- typedef Insert<_T7, L8> L7;
- typedef Insert<_T6, L7> L6;
- typedef Insert<_T5, L6> L5;
- typedef Insert<_T4, L5> L4;
- typedef Insert<_T3, L4> L3;
- typedef Insert<_T2, L3> L2;
- typedef Insert<_T1, L2> L1;
- typedef Insert<_T0, L1> L0;
- typedef typename Get<_idx, L0>::Type Type;
- };
-
- }
-
- /// \brief Helper class for Variant
- ///
- /// Helper class to define type mappings for Variant. This class
- /// converts the template parameters to be mappable by integer.
- /// \see Variant
- template <
- typename _T0,
- typename _T1 = void, typename _T2 = void, typename _T3 = void,
- typename _T5 = void, typename _T4 = void, typename _T6 = void,
- typename _T7 = void, typename _T8 = void, typename _T9 = void>
- struct VariantTypeMap {
- template <int _idx>
- struct Map {
- typedef typename _variant_bits::
- Mapper<_idx, _T0, _T1, _T2, _T3, _T4, _T5, _T6, _T7, _T8, _T9>::Type
- Type;
- };
- };
-
-}
-
-
-#endif
diff --git a/src/lemon/bits/vector_map.h b/src/lemon/bits/vector_map.h
deleted file mode 100644
index 9299671..0000000
--- a/src/lemon/bits/vector_map.h
+++ /dev/null
@@ -1,243 +0,0 @@
-/* -*- C++ -*-
- *
- * This file is a part of LEMON, a generic C++ optimization library
- *
- * Copyright (C) 2003-2008
- * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
- * (Egervary Research Group on Combinatorial Optimization, EGRES).
- *
- * Permission to use, modify and distribute this software is granted
- * provided that this copyright notice appears in all copies. For
- * precise terms see the accompanying LICENSE file.
- *
- * This software is provided "AS IS" with no warranty of any kind,
- * express or implied, and with no claim as to its suitability for any
- * purpose.
- *
- */
-
-#ifndef LEMON_BITS_VECTOR_MAP_H
-#define LEMON_BITS_VECTOR_MAP_H
-
-#include <vector>
-#include <algorithm>
-
-#include <lemon/bits/traits.h>
-#include <lemon/bits/utility.h>
-
-#include <lemon/bits/alteration_notifier.h>
-
-#include <lemon/concept_check.h>
-#include <lemon/concepts/maps.h>
-
-///\ingroup graphbits
-///
-///\file
-///\brief Vector based graph maps.
-namespace lemon {
-
- /// \ingroup graphbits
- ///
- /// \brief Graph map based on the std::vector storage.
- ///
- /// The VectorMap template class is graph map structure what
- /// automatically updates the map when a key is added to or erased from
- /// the map. This map type uses the std::vector to store the values.
- ///
- /// \param Notifier The AlterationNotifier that will notify this map.
- /// \param Item The item type of the graph items.
- /// \param Value The value type of the map.
- ///
- /// \author Balazs Dezso
- template <typename _Graph, typename _Item, typename _Value>
- class VectorMap
- : public ItemSetTraits<_Graph, _Item>::ItemNotifier::ObserverBase {
- private:
-
- /// The container type of the map.
- typedef std::vector<_Value> Container;
-
- public:
-
- /// The graph type of the map.
- typedef _Graph Graph;
- /// The item type of the map.
- typedef _Item Item;
- /// The reference map tag.
- typedef True ReferenceMapTag;
-
- /// The key type of the map.
- typedef _Item Key;
- /// The value type of the map.
- typedef _Value Value;
-
- /// The notifier type.
- typedef typename ItemSetTraits<_Graph, _Item>::ItemNotifier Notifier;
-
- /// The map type.
- typedef VectorMap Map;
- /// The base class of the map.
- typedef typename Notifier::ObserverBase Parent;
-
- /// The reference type of the map;
- typedef typename Container::reference Reference;
- /// The const reference type of the map;
- typedef typename Container::const_reference ConstReference;
-
-
- /// \brief Constructor to attach the new map into the notifier.
- ///
- /// It constructs a map and attachs it into the notifier.
- /// It adds all the items of the graph to the map.
- VectorMap(const Graph& graph) {
- Parent::attach(graph.notifier(Item()));
- container.resize(Parent::notifier()->maxId() + 1);
- }
-
- /// \brief Constructor uses given value to initialize the map.
- ///
- /// It constructs a map uses a given value to initialize the map.
- /// It adds all the items of the graph to the map.
- VectorMap(const Graph& graph, const Value& value) {
- Parent::attach(graph.notifier(Item()));
- container.resize(Parent::notifier()->maxId() + 1, value);
- }
-
- /// \brief Copy constructor
- ///
- /// Copy constructor.
- VectorMap(const VectorMap& _copy) : Parent() {
- if (_copy.attached()) {
- Parent::attach(*_copy.notifier());
- container = _copy.container;
- }
- }
-
- /// \brief Assign operator.
- ///
- /// This operator assigns for each item in the map the
- /// value mapped to the same item in the copied map.
- /// The parameter map should be indiced with the same
- /// itemset because this assign operator does not change
- /// the container of the map.
- VectorMap& operator=(const VectorMap& cmap) {
- return operator=<VectorMap>(cmap);
- }
-
-
- /// \brief Template assign operator.
- ///
- /// The given parameter should be conform to the ReadMap
- /// concecpt and could be indiced by the current item set of
- /// the NodeMap. In this case the value for each item
- /// is assigned by the value of the given ReadMap.
- template <typename CMap>
- VectorMap& operator=(const CMap& cmap) {
- checkConcept<concepts::ReadMap<Key, _Value>, CMap>();
- const typename Parent::Notifier* nf = Parent::notifier();
- Item it;
- for (nf->first(it); it != INVALID; nf->next(it)) {
- set(it, cmap[it]);
- }
- return *this;
- }
-
- public:
-
- /// \brief The subcript operator.
- ///
- /// The subscript operator. The map can be subscripted by the
- /// actual items of the graph.
- Reference operator[](const Key& key) {
- return container[Parent::notifier()->id(key)];
- }
-
- /// \brief The const subcript operator.
- ///
- /// The const subscript operator. The map can be subscripted by the
- /// actual items of the graph.
- ConstReference operator[](const Key& key) const {
- return container[Parent::notifier()->id(key)];
- }
-
-
- /// \brief The setter function of the map.
- ///
- /// It the same as operator[](key) = value expression.
- void set(const Key& key, const Value& value) {
- (*this)[key] = value;
- }
-
- protected:
-
- /// \brief Adds a new key to the map.
- ///
- /// It adds a new key to the map. It called by the observer notifier
- /// and it overrides the add() member function of the observer base.
- virtual void add(const Key& key) {
- int id = Parent::notifier()->id(key);
- if (id >= int(container.size())) {
- container.resize(id + 1);
- }
- }
-
- /// \brief Adds more new keys to the map.
- ///
- /// It adds more new keys to the map. It called by the observer notifier
- /// and it overrides the add() member function of the observer base.
- virtual void add(const std::vector<Key>& keys) {
- int max = container.size() - 1;
- for (int i = 0; i < int(keys.size()); ++i) {
- int id = Parent::notifier()->id(keys[i]);
- if (id >= max) {
- max = id;
- }
- }
- container.resize(max + 1);
- }
-
- /// \brief Erase a key from the map.
- ///
- /// Erase a key from the map. It called by the observer notifier
- /// and it overrides the erase() member function of the observer base.
- virtual void erase(const Key& key) {
- container[Parent::notifier()->id(key)] = Value();
- }
-
- /// \brief Erase more keys from the map.
- ///
- /// Erase more keys from the map. It called by the observer notifier
- /// and it overrides the erase() member function of the observer base.
- virtual void erase(const std::vector<Key>& keys) {
- for (int i = 0; i < int(keys.size()); ++i) {
- container[Parent::notifier()->id(keys[i])] = Value();
- }
- }
-
- /// \brief Buildes the map.
- ///
- /// It buildes the map. It called by the observer notifier
- /// and it overrides the build() member function of the observer base.
- virtual void build() {
- int size = Parent::notifier()->maxId() + 1;
- container.reserve(size);
- container.resize(size);
- }
-
- /// \brief Clear the map.
- ///
- /// It erase all items from the map. It called by the observer notifier
- /// and it overrides the clear() member function of the observer base.
- virtual void clear() {
- container.clear();
- }
-
- private:
-
- Container container;
-
- };
-
-}
-
-#endif
diff --git a/src/lemon/bucket_heap.h b/src/lemon/bucket_heap.h
deleted file mode 100644
index f8b57e1..0000000
--- a/src/lemon/bucket_heap.h
+++ /dev/null
@@ -1,831 +0,0 @@
-/* -*- C++ -*-
- *
- * This file is a part of LEMON, a generic C++ optimization library
- *
- * Copyright (C) 2003-2008
- * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
- * (Egervary Research Group on Combinatorial Optimization, EGRES).
- *
- * Permission to use, modify and distribute this software is granted
- * provided that this copyright notice appears in all copies. For
- * precise terms see the accompanying LICENSE file.
- *
- * This software is provided "AS IS" with no warranty of any kind,
- * express or implied, and with no claim as to its suitability for any
- * purpose.
- *
- */
-
-#ifndef LEMON_BUCKET_HEAP_H
-#define LEMON_BUCKET_HEAP_H
-
-///\ingroup auxdat
-///\file
-///\brief Bucket Heap implementation.
-
-#include <vector>
-#include <utility>
-#include <functional>
-
-namespace lemon {
-
- /// \ingroup auxdat
- ///
- /// \brief A Bucket Heap implementation.
- ///
- /// This class implements the \e bucket \e heap data structure. A \e heap
- /// is a data structure for storing items with specified values called \e
- /// priorities in such a way that finding the item with minimum priority is
- /// efficient. The bucket heap is very simple implementation, it can store
- /// only integer priorities and it stores for each priority in the
- /// \f$ [0..C) \f$ range a list of items. So it should be used only when
- /// the priorities are small. It is not intended to use as dijkstra heap.
- ///
- /// \param _ItemIntMap A read and writable Item int map, used internally
- /// to handle the cross references.
- /// \param minimize If the given parameter is true then the heap gives back
- /// the lowest priority.
- template <typename _ItemIntMap, bool minimize = true >
- class BucketHeap {
-
- public:
- /// \e
- typedef typename _ItemIntMap::Key Item;
- /// \e
- typedef int Prio;
- /// \e
- typedef std::pair<Item, Prio> Pair;
- /// \e
- typedef _ItemIntMap ItemIntMap;
-
- /// \brief Type to represent the items states.
- ///
- /// Each Item element have a state associated to it. It may be "in heap",
- /// "pre heap" or "post heap". The latter two are indifferent from the
- /// heap's point of view, but may be useful to the user.
- ///
- /// The ItemIntMap \e should be initialized in such way that it maps
- /// PRE_HEAP (-1) to any element to be put in the heap...
- enum State {
- IN_HEAP = 0,
- PRE_HEAP = -1,
- POST_HEAP = -2
- };
-
- public:
- /// \brief The constructor.
- ///
- /// The constructor.
- /// \param _index should be given to the constructor, since it is used
- /// internally to handle the cross references. The value of the map
- /// should be PRE_HEAP (-1) for each element.
- explicit BucketHeap(ItemIntMap &_index) : index(_index), minimal(0) {}
-
- /// The number of items stored in the heap.
- ///
- /// \brief Returns the number of items stored in the heap.
- int size() const { return data.size(); }
-
- /// \brief Checks if the heap stores no items.
- ///
- /// Returns \c true if and only if the heap stores no items.
- bool empty() const { return data.empty(); }
-
- /// \brief Make empty this heap.
- ///
- /// Make empty this heap. It does not change the cross reference
- /// map. If you want to reuse a heap what is not surely empty you
- /// should first clear the heap and after that you should set the
- /// cross reference map for each item to \c PRE_HEAP.
- void clear() {
- data.clear(); first.clear(); minimal = 0;
- }
-
- private:
-
- void relocate_last(int idx) {
- if (idx + 1 < int(data.size())) {
- data[idx] = data.back();
- if (data[idx].prev != -1) {
- data[data[idx].prev].next = idx;
- } else {
- first[data[idx].value] = idx;
- }
- if (data[idx].next != -1) {
- data[data[idx].next].prev = idx;
- }
- index[data[idx].item] = idx;
- }
- data.pop_back();
- }
-
- void unlace(int idx) {
- if (data[idx].prev != -1) {
- data[data[idx].prev].next = data[idx].next;
- } else {
- first[data[idx].value] = data[idx].next;
- }
- if (data[idx].next != -1) {
- data[data[idx].next].prev = data[idx].prev;
- }
- }
-
- void lace(int idx) {
- if (int(first.size()) <= data[idx].value) {
- first.resize(data[idx].value + 1, -1);
- }
- data[idx].next = first[data[idx].value];
- if (data[idx].next != -1) {
- data[data[idx].next].prev = idx;
- }
- first[data[idx].value] = idx;
- data[idx].prev = -1;
- }
-
- public:
- /// \brief Insert a pair of item and priority into the heap.
- ///
- /// Adds \c p.first to the heap with priority \c p.second.
- /// \param p The pair to insert.
- void push(const Pair& p) {
- push(p.first, p.second);
- }
-
- /// \brief Insert an item into the heap with the given priority.
- ///
- /// Adds \c i to the heap with priority \c p.
- /// \param i The item to insert.
- /// \param p The priority of the item.
- void push(const Item &i, const Prio &p) {
- int idx = data.size();
- index[i] = idx;
- data.push_back(BucketItem(i, p));
- lace(idx);
- if (p < minimal) {
- minimal = p;
- }
- }
-
- /// \brief Returns the item with minimum priority.
- ///
- /// This method returns the item with minimum priority.
- /// \pre The heap must be nonempty.
- Item top() const {
- while (first[minimal] == -1) {
- ++minimal;
- }
- return data[first[minimal]].item;
- }
-
- /// \brief Returns the minimum priority.
- ///
- /// It returns the minimum priority.
- /// \pre The heap must be nonempty.
- Prio prio() const {
- while (first[minimal] == -1) {
- ++minimal;
- }
- return minimal;
- }
-
- /// \brief Deletes the item with minimum priority.
- ///
- /// This method deletes the item with minimum priority from the heap.
- /// \pre The heap must be non-empty.
- void pop() {
- while (first[minimal] == -1) {
- ++minimal;
- }
- int idx = first[minimal];
- index[data[idx].item] = -2;
- unlace(idx);
- relocate_last(idx);
- }
-
- /// \brief Deletes \c i from the heap.
- ///
- /// This method deletes item \c i from the heap, if \c i was
- /// already stored in the heap.
- /// \param i The item to erase.
- void erase(const Item &i) {
- int idx = index[i];
- index[data[idx].item] = -2;
- unlace(idx);
- relocate_last(idx);
- }
-
-
- /// \brief Returns the priority of \c i.
- ///
- /// This function returns the priority of item \c i.
- /// \pre \c i must be in the heap.
- /// \param i The item.
- Prio operator[](const Item &i) const {
- int idx = index[i];
- return data[idx].value;
- }
-
- /// \brief \c i gets to the heap with priority \c p independently
- /// if \c i was already there.
- ///
- /// This method calls \ref push(\c i, \c p) if \c i is not stored
- /// in the heap and sets the priority of \c i to \c p otherwise.
- /// \param i The item.
- /// \param p The priority.
- void set(const Item &i, const Prio &p) {
- int idx = index[i];
- if (idx < 0) {
- push(i,p);
- } else if (p > data[idx].value) {
- increase(i, p);
- } else {
- decrease(i, p);
- }
- }
-
- /// \brief Decreases the priority of \c i to \c p.
- ///
- /// This method decreases the priority of item \c i to \c p.
- /// \pre \c i must be stored in the heap with priority at least \c
- /// p relative to \c Compare.
- /// \param i The item.
- /// \param p The priority.
- void decrease(const Item &i, const Prio &p) {
- int idx = index[i];
- unlace(idx);
- data[idx].value = p;
- if (p < minimal) {
- minimal = p;
- }
- lace(idx);
- }
-
- /// \brief Increases the priority of \c i to \c p.
- ///
- /// This method sets the priority of item \c i to \c p.
- /// \pre \c i must be stored in the heap with priority at most \c
- /// p relative to \c Compare.
- /// \param i The item.
- /// \param p The priority.
- void increase(const Item &i, const Prio &p) {
- int idx = index[i];
- unlace(idx);
- data[idx].value = p;
- lace(idx);
- }
-
- /// \brief Returns if \c item is in, has already been in, or has
- /// never been in the heap.
- ///
- /// This method returns PRE_HEAP if \c item has never been in the
- /// heap, IN_HEAP if it is in the heap at the moment, and POST_HEAP
- /// otherwise. In the latter case it is possible that \c item will
- /// get back to the heap again.
- /// \param i The item.
- State state(const Item &i) const {
- int idx = index[i];
- if (idx >= 0) idx = 0;
- return State(idx);
- }
-
- /// \brief Sets the state of the \c item in the heap.
- ///
- /// Sets the state of the \c item in the heap. It can be used to
- /// manually clear the heap when it is important to achive the
- /// better time complexity.
- /// \param i The item.
- /// \param st The state. It should not be \c IN_HEAP.
- void state(const Item& i, State st) {
- switch (st) {
- case POST_HEAP:
- case PRE_HEAP:
- if (state(i) == IN_HEAP) {
- erase(i);
- }
- index[i] = st;
- break;
- case IN_HEAP:
- break;
- }
- }
-
- private:
-
- struct BucketItem {
- BucketItem(const Item& _item, int _value)
- : item(_item), value(_value) {}
-
- Item item;
- int value;
-
- int prev, next;
- };
-
- ItemIntMap& index;
- std::vector<int> first;
- std::vector<BucketItem> data;
- mutable int minimal;
-
- }; // class BucketHeap
-
-
- template <typename _ItemIntMap>
- class BucketHeap<_ItemIntMap, false> {
-
- public:
- typedef typename _ItemIntMap::Key Item;
- typedef int Prio;
- typedef std::pair<Item, Prio> Pair;
- typedef _ItemIntMap ItemIntMap;
-
- enum State {
- IN_HEAP = 0,
- PRE_HEAP = -1,
- POST_HEAP = -2
- };
-
- public:
-
- explicit BucketHeap(ItemIntMap &_index) : index(_index), maximal(-1) {}
-
- int size() const { return data.size(); }
- bool empty() const { return data.empty(); }
-
- void clear() {
- data.clear(); first.clear(); maximal = -1;
- }
-
- private:
-
- void relocate_last(int idx) {
- if (idx + 1 != int(data.size())) {
- data[idx] = data.back();
- if (data[idx].prev != -1) {
- data[data[idx].prev].next = idx;
- } else {
- first[data[idx].value] = idx;
- }
- if (data[idx].next != -1) {
- data[data[idx].next].prev = idx;
- }
- index[data[idx].item] = idx;
- }
- data.pop_back();
- }
-
- void unlace(int idx) {
- if (data[idx].prev != -1) {
- data[data[idx].prev].next = data[idx].next;
- } else {
- first[data[idx].value] = data[idx].next;
- }
- if (data[idx].next != -1) {
- data[data[idx].next].prev = data[idx].prev;
- }
- }
-
- void lace(int idx) {
- if (int(first.size()) <= data[idx].value) {
- first.resize(data[idx].value + 1, -1);
- }
- data[idx].next = first[data[idx].value];
- if (data[idx].next != -1) {
- data[data[idx].next].prev = idx;
- }
- first[data[idx].value] = idx;
- data[idx].prev = -1;
- }
-
- public:
-
- void push(const Pair& p) {
- push(p.first, p.second);
- }
-
- void push(const Item &i, const Prio &p) {
- int idx = data.size();
- index[i] = idx;
- data.push_back(BucketItem(i, p));
- lace(idx);
- if (data[idx].value > maximal) {
- maximal = data[idx].value;
- }
- }
-
- Item top() const {
- while (first[maximal] == -1) {
- --maximal;
- }
- return data[first[maximal]].item;
- }
-
- Prio prio() const {
- while (first[maximal] == -1) {
- --maximal;
- }
- return maximal;
- }
-
- void pop() {
- while (first[maximal] == -1) {
- --maximal;
- }
- int idx = first[maximal];
- index[data[idx].item] = -2;
- unlace(idx);
- relocate_last(idx);
- }
-
- void erase(const Item &i) {
- int idx = index[i];
- index[data[idx].item] = -2;
- unlace(idx);
- relocate_last(idx);
- }
-
- Prio operator[](const Item &i) const {
- int idx = index[i];
- return data[idx].value;
- }
-
- void set(const Item &i, const Prio &p) {
- int idx = index[i];
- if (idx < 0) {
- push(i,p);
- } else if (p > data[idx].value) {
- decrease(i, p);
- } else {
- increase(i, p);
- }
- }
-
- void decrease(const Item &i, const Prio &p) {
- int idx = index[i];
- unlace(idx);
- data[idx].value = p;
- if (p > maximal) {
- maximal = p;
- }
- lace(idx);
- }
-
- void increase(const Item &i, const Prio &p) {
- int idx = index[i];
- unlace(idx);
- data[idx].value = p;
- lace(idx);
- }
-
- State state(const Item &i) const {
- int idx = index[i];
- if (idx >= 0) idx = 0;
- return State(idx);
- }
-
- void state(const Item& i, State st) {
- switch (st) {
- case POST_HEAP:
- case PRE_HEAP:
- if (state(i) == IN_HEAP) {
- erase(i);
- }
- index[i] = st;
- break;
- case IN_HEAP:
- break;
- }
- }
-
- private:
-
- struct BucketItem {
- BucketItem(const Item& _item, int _value)
- : item(_item), value(_value) {}
-
- Item item;
- int value;
-
- int prev, next;
- };
-
- ItemIntMap& index;
- std::vector<int> first;
- std::vector<BucketItem> data;
- mutable int maximal;
-
- }; // class BucketHeap
-
- /// \ingroup auxdat
- ///
- /// \brief A Simplified Bucket Heap implementation.
- ///
- /// This class implements a simplified \e bucket \e heap data
- /// structure. It does not provide some functionality but it faster
- /// and simplier data structure than the BucketHeap. The main
- /// difference is that the BucketHeap stores for every key a double
- /// linked list while this class stores just simple lists. In the
- /// other way it does not supports erasing each elements just the
- /// minimal and it does not supports key increasing, decreasing.
- ///
- /// \param _ItemIntMap A read and writable Item int map, used internally
- /// to handle the cross references.
- /// \param minimize If the given parameter is true then the heap gives back
- /// the lowest priority.
- ///
- /// \sa BucketHeap
- template <typename _ItemIntMap, bool minimize = true >
- class SimpleBucketHeap {
-
- public:
- typedef typename _ItemIntMap::Key Item;
- typedef int Prio;
- typedef std::pair<Item, Prio> Pair;
- typedef _ItemIntMap ItemIntMap;
-
- /// \brief Type to represent the items states.
- ///
- /// Each Item element have a state associated to it. It may be "in heap",
- /// "pre heap" or "post heap". The latter two are indifferent from the
- /// heap's point of view, but may be useful to the user.
- ///
- /// The ItemIntMap \e should be initialized in such way that it maps
- /// PRE_HEAP (-1) to any element to be put in the heap...
- enum State {
- IN_HEAP = 0,
- PRE_HEAP = -1,
- POST_HEAP = -2
- };
-
- public:
-
- /// \brief The constructor.
- ///
- /// The constructor.
- /// \param _index should be given to the constructor, since it is used
- /// internally to handle the cross references. The value of the map
- /// should be PRE_HEAP (-1) for each element.
- explicit SimpleBucketHeap(ItemIntMap &_index)
- : index(_index), free(-1), num(0), minimal(0) {}
-
- /// \brief Returns the number of items stored in the heap.
- ///
- /// The number of items stored in the heap.
- int size() const { return num; }
-
- /// \brief Checks if the heap stores no items.
- ///
- /// Returns \c true if and only if the heap stores no items.
- bool empty() const { return num == 0; }
-
- /// \brief Make empty this heap.
- ///
- /// Make empty this heap. It does not change the cross reference
- /// map. If you want to reuse a heap what is not surely empty you
- /// should first clear the heap and after that you should set the
- /// cross reference map for each item to \c PRE_HEAP.
- void clear() {
- data.clear(); first.clear(); free = -1; num = 0; minimal = 0;
- }
-
- /// \brief Insert a pair of item and priority into the heap.
- ///
- /// Adds \c p.first to the heap with priority \c p.second.
- /// \param p The pair to insert.
- void push(const Pair& p) {
- push(p.first, p.second);
- }
-
- /// \brief Insert an item into the heap with the given priority.
- ///
- /// Adds \c i to the heap with priority \c p.
- /// \param i The item to insert.
- /// \param p The priority of the item.
- void push(const Item &i, const Prio &p) {
- int idx;
- if (free == -1) {
- idx = data.size();
- data.push_back(BucketItem(i));
- } else {
- idx = free;
- free = data[idx].next;
- data[idx].item = i;
- }
- index[i] = idx;
- if (p >= int(first.size())) first.resize(p + 1, -1);
- data[idx].next = first[p];
- first[p] = idx;
- if (p < minimal) {
- minimal = p;
- }
- ++num;
- }
-
- /// \brief Returns the item with minimum priority.
- ///
- /// This method returns the item with minimum priority.
- /// \pre The heap must be nonempty.
- Item top() const {
- while (first[minimal] == -1) {
- ++minimal;
- }
- return data[first[minimal]].item;
- }
-
- /// \brief Returns the minimum priority.
- ///
- /// It returns the minimum priority.
- /// \pre The heap must be nonempty.
- Prio prio() const {
- while (first[minimal] == -1) {
- ++minimal;
- }
- return minimal;
- }
-
- /// \brief Deletes the item with minimum priority.
- ///
- /// This method deletes the item with minimum priority from the heap.
- /// \pre The heap must be non-empty.
- void pop() {
- while (first[minimal] == -1) {
- ++minimal;
- }
- int idx = first[minimal];
- index[data[idx].item] = -2;
- first[minimal] = data[idx].next;
- data[idx].next = free;
- free = idx;
- --num;
- }
-
- /// \brief Returns the priority of \c i.
- ///
- /// This function returns the priority of item \c i.
- /// \warning This operator is not a constant time function
- /// because it scans the whole data structure to find the proper
- /// value.
- /// \pre \c i must be in the heap.
- /// \param i The item.
- Prio operator[](const Item &i) const {
- for (int k = 0; k < first.size(); ++k) {
- int idx = first[k];
- while (idx != -1) {
- if (data[idx].item == i) {
- return k;
- }
- idx = data[idx].next;
- }
- }
- return -1;
- }
-
- /// \brief Returns if \c item is in, has already been in, or has
- /// never been in the heap.
- ///
- /// This method returns PRE_HEAP if \c item has never been in the
- /// heap, IN_HEAP if it is in the heap at the moment, and POST_HEAP
- /// otherwise. In the latter case it is possible that \c item will
- /// get back to the heap again.
- /// \param i The item.
- State state(const Item &i) const {
- int idx = index[i];
- if (idx >= 0) idx = 0;
- return State(idx);
- }
-
- private:
-
- struct BucketItem {
- BucketItem(const Item& _item)
- : item(_item) {}
-
- Item item;
- int next;
- };
-
- ItemIntMap& index;
- std::vector<int> first;
- std::vector<BucketItem> data;
- int free, num;
- mutable int minimal;
-
- }; // class SimpleBucketHeap
-
- template <typename _ItemIntMap>
- class SimpleBucketHeap<_ItemIntMap, false> {
-
- public:
- typedef typename _ItemIntMap::Key Item;
- typedef int Prio;
- typedef std::pair<Item, Prio> Pair;
- typedef _ItemIntMap ItemIntMap;
-
- enum State {
- IN_HEAP = 0,
- PRE_HEAP = -1,
- POST_HEAP = -2
- };
-
- public:
-
- explicit SimpleBucketHeap(ItemIntMap &_index)
- : index(_index), free(-1), num(0), maximal(0) {}
-
- int size() const { return num; }
-
- bool empty() const { return num == 0; }
-
- void clear() {
- data.clear(); first.clear(); free = -1; num = 0; maximal = 0;
- }
-
- void push(const Pair& p) {
- push(p.first, p.second);
- }
-
- void push(const Item &i, const Prio &p) {
- int idx;
- if (free == -1) {
- idx = data.size();
- data.push_back(BucketItem(i));
- } else {
- idx = free;
- free = data[idx].next;
- data[idx].item = i;
- }
- index[i] = idx;
- if (p >= int(first.size())) first.resize(p + 1, -1);
- data[idx].next = first[p];
- first[p] = idx;
- if (p > maximal) {
- maximal = p;
- }
- ++num;
- }
-
- Item top() const {
- while (first[maximal] == -1) {
- --maximal;
- }
- return data[first[maximal]].item;
- }
-
- Prio prio() const {
- while (first[maximal] == -1) {
- --maximal;
- }
- return maximal;
- }
-
- void pop() {
- while (first[maximal] == -1) {
- --maximal;
- }
- int idx = first[maximal];
- index[data[idx].item] = -2;
- first[maximal] = data[idx].next;
- data[idx].next = free;
- free = idx;
- --num;
- }
-
- Prio operator[](const Item &i) const {
- for (int k = 0; k < first.size(); ++k) {
- int idx = first[k];
- while (idx != -1) {
- if (data[idx].item == i) {
- return k;
- }
- idx = data[idx].next;
- }
- }
- return -1;
- }
-
- State state(const Item &i) const {
- int idx = index[i];
- if (idx >= 0) idx = 0;
- return State(idx);
- }
-
- private:
-
- struct BucketItem {
- BucketItem(const Item& _item) : item(_item) {}
-
- Item item;
-
- int next;
- };
-
- ItemIntMap& index;
- std::vector<int> first;
- std::vector<BucketItem> data;
- int free, num;
- mutable int maximal;
-
- };
-
-}
-
-#endif
diff --git a/src/lemon/concept_check.h b/src/lemon/concept_check.h
deleted file mode 100644
index 59fa93d..0000000
--- a/src/lemon/concept_check.h
+++ /dev/null
@@ -1,105 +0,0 @@
-/* -*- C++ -*-
- *
- * This file is a part of LEMON, a generic C++ optimization library
- *
- * Copyright (C) 2003-2008
- * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
- * (Egervary Research Group on Combinatorial Optimization, EGRES).
- *
- * Permission to use, modify and distribute this software is granted
- * provided that this copyright notice appears in all copies. For
- * precise terms see the accompanying LICENSE file.
- *
- * This software is provided "AS IS" with no warranty of any kind,
- * express or implied, and with no claim as to its suitability for any
- * purpose.
- *
- */
-
-// Modified for use in LEMON.
-// We should really consider using Boost...
-
-//
-// (C) Copyright Jeremy Siek 2000.
-// Distributed under the Boost Software License, Version 1.0. (See
-// accompanying file LICENSE_1_0.txt or copy at
-// http://www.boost.org/LICENSE_1_0.txt)
-//
-// Revision History:
-// 05 May 2001: Workarounds for HP aCC from Thomas Matelich. (Jeremy Siek)
-// 02 April 2001: Removed limits header altogether. (Jeremy Siek)
-// 01 April 2001: Modified to use new <boost/limits.hpp> header. (JMaddock)
-//
-
-// See http://www.boost.org/libs/concept_check for documentation.
-
-#ifndef LEMON_BOOST_CONCEPT_CHECKS_HPP
-#define LEMON_BOOST_CONCEPT_CHECKS_HPP
-
-namespace lemon {
-
- /*
- "inline" is used for ignore_unused_variable_warning()
- and function_requires() to make sure there is no
- overtarget with g++.
- */
-
- template <class T> inline void ignore_unused_variable_warning(const T&) { }
-
- template <class Concept>
- inline void function_requires()
- {
-#if !defined(NDEBUG)
- void (Concept::*x)() = & Concept::constraints;
- ignore_unused_variable_warning(x);
-#endif
- }
-
- template <typename Concept, typename Type>
- inline void checkConcept() {
-#if !defined(NDEBUG)
- typedef typename Concept::template Constraints<Type> ConceptCheck;
- void (ConceptCheck::*x)() = & ConceptCheck::constraints;
- ignore_unused_variable_warning(x);
-#endif
- }
-#if 0
-#define BOOST_CLASS_REQUIRE(type_var, ns, concept) \
- typedef void (ns::concept <type_var>::* func##type_var##concept)(); \
- template <func##type_var##concept Tp1_> \
- struct concept_checking_##type_var##concept { }; \
- typedef concept_checking_##type_var##concept< \
- BOOST_FPTR ns::concept<type_var>::constraints> \
- concept_checking_typedef_##type_var##concept
-
-#define BOOST_CLASS_REQUIRE2(type_var1, type_var2, ns, concept) \
- typedef void (ns::concept <type_var1,type_var2>::* \
- func##type_var1##type_var2##concept)(); \
- template <func##type_var1##type_var2##concept Tp1_> \
- struct concept_checking_##type_var1##type_var2##concept { }; \
- typedef concept_checking_##type_var1##type_var2##concept< \
- BOOST_FPTR ns::concept<type_var1,type_var2>::constraints> \
- concept_checking_typedef_##type_var1##type_var2##concept
-
-#define BOOST_CLASS_REQUIRE3(tv1, tv2, tv3, ns, concept) \
- typedef void (ns::concept <tv1,tv2,tv3>::* \
- func##tv1##tv2##tv3##concept)(); \
- template <func##tv1##tv2##tv3##concept Tp1_> \
- struct concept_checking_##tv1##tv2##tv3##concept { }; \
- typedef concept_checking_##tv1##tv2##tv3##concept< \
- BOOST_FPTR ns::concept<tv1,tv2,tv3>::constraints> \
- concept_checking_typedef_##tv1##tv2##tv3##concept
-
-#define BOOST_CLASS_REQUIRE4(tv1, tv2, tv3, tv4, ns, concept) \
- typedef void (ns::concept <tv1,tv2,tv3,tv4>::* \
- func##tv1##tv2##tv3##tv4##concept)(); \
- template <func##tv1##tv2##tv3##tv4##concept Tp1_> \
- struct concept_checking_##tv1##tv2##tv3##tv4##concept { }; \
- typedef concept_checking_##tv1##tv2##tv3##tv4##concept< \
- BOOST_FPTR ns::concept<tv1,tv2,tv3,tv4>::constraints> \
- concept_checking_typedef_##tv1##tv2##tv3##tv4##concept
-#endif
-
-} // namespace lemon
-
-#endif // LEMON_BOOST_CONCEPT_CHECKS_HPP
diff --git a/src/lemon/concepts/bpugraph.h b/src/lemon/concepts/bpugraph.h
deleted file mode 100644
index 8b1867d..0000000
--- a/src/lemon/concepts/bpugraph.h
+++ /dev/null
@@ -1,1004 +0,0 @@
-/* -*- C++ -*-
- *
- * This file is a part of LEMON, a generic C++ optimization library
- *
- * Copyright (C) 2003-2008
- * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
- * (Egervary Research Group on Combinatorial Optimization, EGRES).
- *
- * Permission to use, modify and distribute this software is granted
- * provided that this copyright notice appears in all copies. For
- * precise terms see the accompanying LICENSE file.
- *
- * This software is provided "AS IS" with no warranty of any kind,
- * express or implied, and with no claim as to its suitability for any
- * purpose.
- *
- */
-
-/// \ingroup graph_concepts
-/// \file
-/// \brief The concept of Bipartite Undirected Graphs.
-
-#ifndef LEMON_CONCEPT_BPUGRAPH_H
-#define LEMON_CONCEPT_BPUGRAPH_H
-
-#include <lemon/concepts/graph_components.h>
-
-#include <lemon/concepts/graph.h>
-#include <lemon/concepts/ugraph.h>
-
-#include <lemon/bits/utility.h>
-
-namespace lemon {
- namespace concepts {
-
- /// \ingroup graph_concepts
- ///
- /// \brief Class describing the concept of Bipartite Undirected Graphs.
- ///
- /// This class describes the common interface of all
- /// Undirected Bipartite Graphs.
- ///
- /// As all concept describing classes it provides only interface
- /// without any sensible implementation. So any algorithm for
- /// bipartite undirected graph should compile with this class, but it
- /// will not run properly, of course.
- ///
- /// In LEMON bipartite undirected graphs also fulfill the concept of
- /// the undirected graphs (\ref lemon::concepts::UGraph "UGraph Concept").
- ///
- /// You can assume that all undirected bipartite graph can be handled
- /// as an undirected graph and consequently as a static graph.
- ///
- /// The bipartite graph stores two types of nodes which are named
- /// ANode and BNode. The graph type contains two types ANode and
- /// BNode which are inherited from Node type. Moreover they have
- /// constructor which converts Node to either ANode or BNode when
- /// it is possible. Therefor everywhere the Node type can be used
- /// instead of ANode and BNode. So the usage of the ANode and
- /// BNode is not suggested.
- ///
- /// The iteration on the partition can be done with the ANodeIt and
- /// BNodeIt classes. The node map can be used to map values to the nodes
- /// and similarly we can use to map values for just the ANodes and
- /// BNodes the ANodeMap and BNodeMap template classes.
-
- class BpUGraph {
- public:
- /// \brief The undirected graph should be tagged by the
- /// UndirectedTag.
- ///
- /// The undirected graph should be tagged by the UndirectedTag. This
- /// tag helps the enable_if technics to make compile time
- /// specializations for undirected graphs.
- typedef True UndirectedTag;
-
- /// \brief The base type of node iterators,
- /// or in other words, the trivial node iterator.
- ///
- /// This is the base type of each node iterator,
- /// thus each kind of node iterator converts to this.
- /// More precisely each kind of node iterator should be inherited
- /// from the trivial node iterator. The Node class represents
- /// both of two types of nodes.
- class Node {
- public:
- /// Default constructor
-
- /// @warning The default constructor sets the iterator
- /// to an undefined value.
- Node() { }
- /// Copy constructor.
-
- /// Copy constructor.
- ///
- Node(const Node&) { }
-
- /// Invalid constructor \& conversion.
-
- /// This constructor initializes the iterator to be invalid.
- /// \sa Invalid for more details.
- Node(Invalid) { }
- /// Equality operator
-
- /// Two iterators are equal if and only if they point to the
- /// same object or both are invalid.
- bool operator==(Node) const { return true; }
-
- /// Inequality operator
-
- /// \sa operator==(Node n)
- ///
- bool operator!=(Node) const { return true; }
-
- /// Artificial ordering operator.
-
- /// To allow the use of graph descriptors as key type in std::map or
- /// similar associative container we require this.
- ///
- /// \note This operator only have to define some strict ordering of
- /// the items; this order has nothing to do with the iteration
- /// ordering of the items.
- bool operator<(Node) const { return false; }
-
- };
-
- /// \brief Helper class for ANodes.
- ///
- /// This class is just a helper class for ANodes, it is not
- /// suggested to use it directly. It can be converted easily to
- /// node and vice versa. The usage of this class is limited
- /// to use just as template parameters for special map types.
- class ANode : public Node {
- public:
- /// Default constructor
-
- /// @warning The default constructor sets the iterator
- /// to an undefined value.
- ANode() : Node() { }
- /// Copy constructor.
-
- /// Copy constructor.
- ///
- ANode(const ANode&) : Node() { }
-
- /// Construct the same node as ANode.
-
- /// Construct the same node as ANode. It may throws assertion
- /// when the given node is from the BNode set.
- ANode(const Node&) : Node() { }
-
- /// Assign node to A-node.
-
- /// Besides the core graph item functionality each node should
- /// be convertible to the represented A-node if it is it possible.
- ANode& operator=(const Node&) { return *this; }
-
- /// Invalid constructor \& conversion.
-
- /// This constructor initializes the iterator to be invalid.
- /// \sa Invalid for more details.
- ANode(Invalid) { }
- /// Equality operator
-
- /// Two iterators are equal if and only if they point to the
- /// same object or both are invalid.
- bool operator==(ANode) const { return true; }
-
- /// Inequality operator
-
- /// \sa operator==(ANode n)
- ///
- bool operator!=(ANode) const { return true; }
-
- /// Artificial ordering operator.
-
- /// To allow the use of graph descriptors as key type in std::map or
- /// similar associative container we require this.
- ///
- /// \note This operator only have to define some strict ordering of
- /// the items; this order has nothing to do with the iteration
- /// ordering of the items.
- bool operator<(ANode) const { return false; }
-
- };
-
- /// \brief Helper class for BNodes.
- ///
- /// This class is just a helper class for BNodes, it is not
- /// suggested to use it directly. It can be converted easily to
- /// node and vice versa. The usage of this class is limited
- /// to use just as template parameters for special map types.
- class BNode : public Node {
- public:
- /// Default constructor
-
- /// @warning The default constructor sets the iterator
- /// to an undefined value.
- BNode() : Node() { }
- /// Copy constructor.
-
- /// Copy constructor.
- ///
- BNode(const BNode&) : Node() { }
-
- /// Construct the same node as BNode.
-
- /// Construct the same node as BNode. It may throws assertion
- /// when the given node is from the ANode set.
- BNode(const Node&) : Node() { }
-
- /// Assign node to B-node.
-
- /// Besides the core graph item functionality each node should
- /// be convertible to the represented B-node if it is it possible.
- BNode& operator=(const Node&) { return *this; }
-
- /// Invalid constructor \& conversion.
-
- /// This constructor initializes the iterator to be invalid.
- /// \sa Invalid for more details.
- BNode(Invalid) { }
- /// Equality operator
-
- /// Two iterators are equal if and only if they point to the
- /// same object or both are invalid.
- bool operator==(BNode) const { return true; }
-
- /// Inequality operator
-
- /// \sa operator==(BNode n)
- ///
- bool operator!=(BNode) const { return true; }
-
- /// Artificial ordering operator.
-
- /// To allow the use of graph descriptors as key type in std::map or
- /// similar associative container we require this.
- ///
- /// \note This operator only have to define some strict ordering of
- /// the items; this order has nothing to do with the iteration
- /// ordering of the items.
- bool operator<(BNode) const { return false; }
-
- };
-
- /// This iterator goes through each node.
-
- /// This iterator goes through each node.
- /// Its usage is quite simple, for example you can count the number
- /// of nodes in graph \c g of type \c Graph like this:
- ///\code
- /// int count=0;
- /// for (Graph::NodeIt n(g); n!=INVALID; ++n) ++count;
- ///\endcode
- class NodeIt : public Node {
- public:
- /// Default constructor
-
- /// @warning The default constructor sets the iterator
- /// to an undefined value.
- NodeIt() { }
- /// Copy constructor.
-
- /// Copy constructor.
- ///
- NodeIt(const NodeIt& n) : Node(n) { }
- /// Invalid constructor \& conversion.
-
- /// Initialize the iterator to be invalid.
- /// \sa Invalid for more details.
- NodeIt(Invalid) { }
- /// Sets the iterator to the first node.
-
- /// Sets the iterator to the first node of \c g.
- ///
- NodeIt(const BpUGraph&) { }
- /// Node -> NodeIt conversion.
-
- /// Sets the iterator to the node of \c the graph pointed by
- /// the trivial iterator.
- /// This feature necessitates that each time we
- /// iterate the edge-set, the iteration order is the same.
- NodeIt(const BpUGraph&, const Node&) { }
- /// Next node.
-
- /// Assign the iterator to the next node.
- ///
- NodeIt& operator++() { return *this; }
- };
-
- /// This iterator goes through each ANode.
-
- /// This iterator goes through each ANode.
- /// Its usage is quite simple, for example you can count the number
- /// of nodes in graph \c g of type \c Graph like this:
- ///\code
- /// int count=0;
- /// for (Graph::ANodeIt n(g); n!=INVALID; ++n) ++count;
- ///\endcode
- class ANodeIt : public Node {
- public:
- /// Default constructor
-
- /// @warning The default constructor sets the iterator
- /// to an undefined value.
- ANodeIt() { }
- /// Copy constructor.
-
- /// Copy constructor.
- ///
- ANodeIt(const ANodeIt& n) : Node(n) { }
- /// Invalid constructor \& conversion.
-
- /// Initialize the iterator to be invalid.
- /// \sa Invalid for more details.
- ANodeIt(Invalid) { }
- /// Sets the iterator to the first node.
-
- /// Sets the iterator to the first node of \c g.
- ///
- ANodeIt(const BpUGraph&) { }
- /// Node -> ANodeIt conversion.
-
- /// Sets the iterator to the node of \c the graph pointed by
- /// the trivial iterator.
- /// This feature necessitates that each time we
- /// iterate the edge-set, the iteration order is the same.
- ANodeIt(const BpUGraph&, const Node&) { }
- /// Next node.
-
- /// Assign the iterator to the next node.
- ///
- ANodeIt& operator++() { return *this; }
- };
-
- /// This iterator goes through each BNode.
-
- /// This iterator goes through each BNode.
- /// Its usage is quite simple, for example you can count the number
- /// of nodes in graph \c g of type \c Graph like this:
- ///\code
- /// int count=0;
- /// for (Graph::BNodeIt n(g); n!=INVALID; ++n) ++count;
- ///\endcode
- class BNodeIt : public Node {
- public:
- /// Default constructor
-
- /// @warning The default constructor sets the iterator
- /// to an undefined value.
- BNodeIt() { }
- /// Copy constructor.
-
- /// Copy constructor.
- ///
- BNodeIt(const BNodeIt& n) : Node(n) { }
- /// Invalid constructor \& conversion.
-
- /// Initialize the iterator to be invalid.
- /// \sa Invalid for more details.
- BNodeIt(Invalid) { }
- /// Sets the iterator to the first node.
-
- /// Sets the iterator to the first node of \c g.
- ///
- BNodeIt(const BpUGraph&) { }
- /// Node -> BNodeIt conversion.
-
- /// Sets the iterator to the node of \c the graph pointed by
- /// the trivial iterator.
- /// This feature necessitates that each time we
- /// iterate the edge-set, the iteration order is the same.
- BNodeIt(const BpUGraph&, const Node&) { }
- /// Next node.
-
- /// Assign the iterator to the next node.
- ///
- BNodeIt& operator++() { return *this; }
- };
-
-
- /// The base type of the undirected edge iterators.
-
- /// The base type of the undirected edge iterators.
- ///
- class UEdge {
- public:
- /// Default constructor
-
- /// @warning The default constructor sets the iterator
- /// to an undefined value.
- UEdge() { }
- /// Copy constructor.
-
- /// Copy constructor.
- ///
- UEdge(const UEdge&) { }
- /// Initialize the iterator to be invalid.
-
- /// Initialize the iterator to be invalid.
- ///
- UEdge(Invalid) { }
- /// Equality operator
-
- /// Two iterators are equal if and only if they point to the
- /// same object or both are invalid.
- bool operator==(UEdge) const { return true; }
- /// Inequality operator
-
- /// \sa operator==(UEdge n)
- ///
- bool operator!=(UEdge) const { return true; }
-
- /// Artificial ordering operator.
-
- /// To allow the use of graph descriptors as key type in std::map or
- /// similar associative container we require this.
- ///
- /// \note This operator only have to define some strict ordering of
- /// the items; this order has nothing to do with the iteration
- /// ordering of the items.
- bool operator<(UEdge) const { return false; }
- };
-
- /// This iterator goes through each undirected edge.
-
- /// This iterator goes through each undirected edge of a graph.
- /// Its usage is quite simple, for example you can count the number
- /// of undirected edges in a graph \c g of type \c Graph as follows:
- ///\code
- /// int count=0;
- /// for(Graph::UEdgeIt e(g); e!=INVALID; ++e) ++count;
- ///\endcode
- class UEdgeIt : public UEdge {
- public:
- /// Default constructor
-
- /// @warning The default constructor sets the iterator
- /// to an undefined value.
- UEdgeIt() { }
- /// Copy constructor.
-
- /// Copy constructor.
- ///
- UEdgeIt(const UEdgeIt& e) : UEdge(e) { }
- /// Initialize the iterator to be invalid.
-
- /// Initialize the iterator to be invalid.
- ///
- UEdgeIt(Invalid) { }
- /// This constructor sets the iterator to the first undirected edge.
-
- /// This constructor sets the iterator to the first undirected edge.
- UEdgeIt(const BpUGraph&) { }
- /// UEdge -> UEdgeIt conversion
-
- /// Sets the iterator to the value of the trivial iterator.
- /// This feature necessitates that each time we
- /// iterate the undirected edge-set, the iteration order is the
- /// same.
- UEdgeIt(const BpUGraph&, const UEdge&) { }
- /// Next undirected edge
-
- /// Assign the iterator to the next undirected edge.
- UEdgeIt& operator++() { return *this; }
- };
-
- /// \brief This iterator goes trough the incident undirected
- /// edges of a node.
- ///
- /// This iterator goes trough the incident undirected edges
- /// of a certain node
- /// of a graph.
- /// Its usage is quite simple, for example you can compute the
- /// degree (i.e. count the number
- /// of incident edges of a node \c n
- /// in graph \c g of type \c Graph as follows.
- ///\code
- /// int count=0;
- /// for(Graph::IncEdgeIt e(g, n); e!=INVALID; ++e) ++count;
- ///\endcode
- class IncEdgeIt : public UEdge {
- public:
- /// Default constructor
-
- /// @warning The default constructor sets the iterator
- /// to an undefined value.
- IncEdgeIt() { }
- /// Copy constructor.
-
- /// Copy constructor.
- ///
- IncEdgeIt(const IncEdgeIt& e) : UEdge(e) { }
- /// Initialize the iterator to be invalid.
-
- /// Initialize the iterator to be invalid.
- ///
- IncEdgeIt(Invalid) { }
- /// This constructor sets the iterator to first incident edge.
-
- /// This constructor set the iterator to the first incident edge of
- /// the node.
- IncEdgeIt(const BpUGraph&, const Node&) { }
- /// UEdge -> IncEdgeIt conversion
-
- /// Sets the iterator to the value of the trivial iterator \c e.
- /// This feature necessitates that each time we
- /// iterate the edge-set, the iteration order is the same.
- IncEdgeIt(const BpUGraph&, const UEdge&) { }
- /// Next incident edge
-
- /// Assign the iterator to the next incident edge
- /// of the corresponding node.
- IncEdgeIt& operator++() { return *this; }
- };
-
- /// The directed edge type.
-
- /// The directed edge type. It can be converted to the
- /// undirected edge.
- class Edge : public UEdge {
- public:
- /// Default constructor
-
- /// @warning The default constructor sets the iterator
- /// to an undefined value.
- Edge() { }
- /// Copy constructor.
-
- /// Copy constructor.
- ///
- Edge(const Edge& e) : UEdge(e) { }
- /// Initialize the iterator to be invalid.
-
- /// Initialize the iterator to be invalid.
- ///
- Edge(Invalid) { }
- /// Equality operator
-
- /// Two iterators are equal if and only if they point to the
- /// same object or both are invalid.
- bool operator==(Edge) const { return true; }
- /// Inequality operator
-
- /// \sa operator==(Edge n)
- ///
- bool operator!=(Edge) const { return true; }
-
- /// Artificial ordering operator.
-
- /// To allow the use of graph descriptors as key type in std::map or
- /// similar associative container we require this.
- ///
- /// \note This operator only have to define some strict ordering of
- /// the items; this order has nothing to do with the iteration
- /// ordering of the items.
- bool operator<(Edge) const { return false; }
-
- };
- /// This iterator goes through each directed edge.
-
- /// This iterator goes through each edge of a graph.
- /// Its usage is quite simple, for example you can count the number
- /// of edges in a graph \c g of type \c Graph as follows:
- ///\code
- /// int count=0;
- /// for(Graph::EdgeIt e(g); e!=INVALID; ++e) ++count;
- ///\endcode
- class EdgeIt : public Edge {
- public:
- /// Default constructor
-
- /// @warning The default constructor sets the iterator
- /// to an undefined value.
- EdgeIt() { }
- /// Copy constructor.
-
- /// Copy constructor.
- ///
- EdgeIt(const EdgeIt& e) : Edge(e) { }
- /// Initialize the iterator to be invalid.
-
- /// Initialize the iterator to be invalid.
- ///
- EdgeIt(Invalid) { }
- /// This constructor sets the iterator to the first edge.
-
- /// This constructor sets the iterator to the first edge of \c g.
- ///@param g the graph
- EdgeIt(const BpUGraph &g) { ignore_unused_variable_warning(g); }
- /// Edge -> EdgeIt conversion
-
- /// Sets the iterator to the value of the trivial iterator \c e.
- /// This feature necessitates that each time we
- /// iterate the edge-set, the iteration order is the same.
- EdgeIt(const BpUGraph&, const Edge&) { }
- ///Next edge
-
- /// Assign the iterator to the next edge.
- EdgeIt& operator++() { return *this; }
- };
-
- /// This iterator goes trough the outgoing directed edges of a node.
-
- /// This iterator goes trough the \e outgoing edges of a certain node
- /// of a graph.
- /// Its usage is quite simple, for example you can count the number
- /// of outgoing edges of a node \c n
- /// in graph \c g of type \c Graph as follows.
- ///\code
- /// int count=0;
- /// for (Graph::OutEdgeIt e(g, n); e!=INVALID; ++e) ++count;
- ///\endcode
-
- class OutEdgeIt : public Edge {
- public:
- /// Default constructor
-
- /// @warning The default constructor sets the iterator
- /// to an undefined value.
- OutEdgeIt() { }
- /// Copy constructor.
-
- /// Copy constructor.
- ///
- OutEdgeIt(const OutEdgeIt& e) : Edge(e) { }
- /// Initialize the iterator to be invalid.
-
- /// Initialize the iterator to be invalid.
- ///
- OutEdgeIt(Invalid) { }
- /// This constructor sets the iterator to the first outgoing edge.
-
- /// This constructor sets the iterator to the first outgoing edge of
- /// the node.
- ///@param n the node
- ///@param g the graph
- OutEdgeIt(const BpUGraph& n, const Node& g) {
- ignore_unused_variable_warning(n);
- ignore_unused_variable_warning(g);
- }
- /// Edge -> OutEdgeIt conversion
-
- /// Sets the iterator to the value of the trivial iterator.
- /// This feature necessitates that each time we
- /// iterate the edge-set, the iteration order is the same.
- OutEdgeIt(const BpUGraph&, const Edge&) { }
- ///Next outgoing edge
-
- /// Assign the iterator to the next
- /// outgoing edge of the corresponding node.
- OutEdgeIt& operator++() { return *this; }
- };
-
- /// This iterator goes trough the incoming directed edges of a node.
-
- /// This iterator goes trough the \e incoming edges of a certain node
- /// of a graph.
- /// Its usage is quite simple, for example you can count the number
- /// of outgoing edges of a node \c n
- /// in graph \c g of type \c Graph as follows.
- ///\code
- /// int count=0;
- /// for(Graph::InEdgeIt e(g, n); e!=INVALID; ++e) ++count;
- ///\endcode
-
- class InEdgeIt : public Edge {
- public:
- /// Default constructor
-
- /// @warning The default constructor sets the iterator
- /// to an undefined value.
- InEdgeIt() { }
- /// Copy constructor.
-
- /// Copy constructor.
- ///
- InEdgeIt(const InEdgeIt& e) : Edge(e) { }
- /// Initialize the iterator to be invalid.
-
- /// Initialize the iterator to be invalid.
- ///
- InEdgeIt(Invalid) { }
- /// This constructor sets the iterator to first incoming edge.
-
- /// This constructor set the iterator to the first incoming edge of
- /// the node.
- ///@param n the node
- ///@param g the graph
- InEdgeIt(const BpUGraph& g, const Node& n) {
- ignore_unused_variable_warning(n);
- ignore_unused_variable_warning(g);
- }
- /// Edge -> InEdgeIt conversion
-
- /// Sets the iterator to the value of the trivial iterator \c e.
- /// This feature necessitates that each time we
- /// iterate the edge-set, the iteration order is the same.
- InEdgeIt(const BpUGraph&, const Edge&) { }
- /// Next incoming edge
-
- /// Assign the iterator to the next inedge of the corresponding node.
- ///
- InEdgeIt& operator++() { return *this; }
- };
-
- /// \brief Read write map of the nodes to type \c T.
- ///
- /// ReadWrite map of the nodes to type \c T.
- /// \sa Reference
- /// \todo Wrong documentation
- template<class T>
- class NodeMap : public ReadWriteMap< Node, T >
- {
- public:
-
- ///\e
- NodeMap(const BpUGraph&) { }
- ///\e
- NodeMap(const BpUGraph&, T) { }
-
- ///Copy constructor
- NodeMap(const NodeMap& nm) : ReadWriteMap< Node, T >(nm) { }
- ///Assignment operator
- NodeMap& operator=(const NodeMap&) { return *this; }
- ///Assignment operator
- template <typename CMap>
- NodeMap& operator=(const CMap&) {
- checkConcept<ReadMap<Node, T>, CMap>();
- return *this;
- }
- };
-
- /// \brief Read write map of the ANodes to type \c T.
- ///
- /// ReadWrite map of the ANodes to type \c T.
- /// \sa Reference
- /// \todo Wrong documentation
- template<class T>
- class ANodeMap : public ReadWriteMap< Node, T >
- {
- public:
-
- ///\e
- ANodeMap(const BpUGraph&) { }
- ///\e
- ANodeMap(const BpUGraph&, T) { }
-
- ///Copy constructor
- ANodeMap(const ANodeMap& nm) : ReadWriteMap< Node, T >(nm) { }
- ///Assignment operator
- ANodeMap& operator=(const ANodeMap&) { return *this; }
- ///Assignment operator
- template <typename CMap>
- ANodeMap& operator=(const CMap&) {
- checkConcept<ReadMap<Node, T>, CMap>();
- return *this;
- }
- };
-
- /// \brief Read write map of the BNodes to type \c T.
- ///
- /// ReadWrite map of the BNodes to type \c T.
- /// \sa Reference
- /// \todo Wrong documentation
- template<class T>
- class BNodeMap : public ReadWriteMap< Node, T >
- {
- public:
-
- ///\e
- BNodeMap(const BpUGraph&) { }
- ///\e
- BNodeMap(const BpUGraph&, T) { }
-
- ///Copy constructor
- BNodeMap(const BNodeMap& nm) : ReadWriteMap< Node, T >(nm) { }
- ///Assignment operator
- BNodeMap& operator=(const BNodeMap&) { return *this; }
- ///Assignment operator
- template <typename CMap>
- BNodeMap& operator=(const CMap&) {
- checkConcept<ReadMap<Node, T>, CMap>();
- return *this;
- }
- };
-
- /// \brief Read write map of the directed edges to type \c T.
- ///
- /// Reference map of the directed edges to type \c T.
- /// \sa Reference
- /// \todo Wrong documentation
- template<class T>
- class EdgeMap : public ReadWriteMap<Edge,T>
- {
- public:
-
- ///\e
- EdgeMap(const BpUGraph&) { }
- ///\e
- EdgeMap(const BpUGraph&, T) { }
- ///Copy constructor
- EdgeMap(const EdgeMap& em) : ReadWriteMap<Edge,T>(em) { }
- ///Assignment operator
- EdgeMap& operator=(const EdgeMap&) { return *this; }
- ///Assignment operator
- template <typename CMap>
- EdgeMap& operator=(const CMap&) {
- checkConcept<ReadMap<Edge, T>, CMap>();
- return *this;
- }
- };
-
- /// Read write map of the undirected edges to type \c T.
-
- /// Reference map of the edges to type \c T.
- /// \sa Reference
- /// \todo Wrong documentation
- template<class T>
- class UEdgeMap : public ReadWriteMap<UEdge,T>
- {
- public:
-
- ///\e
- UEdgeMap(const BpUGraph&) { }
- ///\e
- UEdgeMap(const BpUGraph&, T) { }
- ///Copy constructor
- UEdgeMap(const UEdgeMap& em) : ReadWriteMap<UEdge,T>(em) {}
- ///Assignment operator
- UEdgeMap &operator=(const UEdgeMap&) { return *this; }
- ///Assignment operator
- template <typename CMap>
- UEdgeMap& operator=(const CMap&) {
- checkConcept<ReadMap<UEdge, T>, CMap>();
- return *this;
- }
- };
-
- /// \brief Direct the given undirected edge.
- ///
- /// Direct the given undirected edge. The returned edge source
- /// will be the given node.
- Edge direct(const UEdge&, const Node&) const {
- return INVALID;
- }
-
- /// \brief Direct the given undirected edge.
- ///
- /// Direct the given undirected edge. The returned edge
- /// represents the given undirected edge and the direction comes
- /// from the given bool. The source of the undirected edge and
- /// the directed edge is the same when the given bool is true.
- Edge direct(const UEdge&, bool) const {
- return INVALID;
- }
-
- /// \brief Returns true when the given node is an ANode.
- ///
- /// Returns true when the given node is an ANode.
- bool aNode(Node) const { return true;}
-
- /// \brief Returns true when the given node is an BNode.
- ///
- /// Returns true when the given node is an BNode.
- bool bNode(Node) const { return true;}
-
- /// \brief Returns the edge's end node which is in the ANode set.
- ///
- /// Returns the edge's end node which is in the ANode set.
- Node aNode(UEdge) const { return INVALID;}
-
- /// \brief Returns the edge's end node which is in the BNode set.
- ///
- /// Returns the edge's end node which is in the BNode set.
- Node bNode(UEdge) const { return INVALID;}
-
- /// \brief Returns true if the edge has default orientation.
- ///
- /// Returns whether the given directed edge is same orientation as
- /// the corresponding undirected edge's default orientation.
- bool direction(Edge) const { return true; }
-
- /// \brief Returns the opposite directed edge.
- ///
- /// Returns the opposite directed edge.
- Edge oppositeEdge(Edge) const { return INVALID; }
-
- /// \brief Opposite node on an edge
- ///
- /// \return the opposite of the given Node on the given UEdge
- Node oppositeNode(Node, UEdge) const { return INVALID; }
-
- /// \brief First node of the undirected edge.
- ///
- /// \return the first node of the given UEdge.
- ///
- /// Naturally undirected edges don't have direction and thus
- /// don't have source and target node. But we use these two methods
- /// to query the two endnodes of the edge. The direction of the edge
- /// which arises this way is called the inherent direction of the
- /// undirected edge, and is used to define the "default" direction
- /// of the directed versions of the edges.
- /// \sa direction
- Node source(UEdge) const { return INVALID; }
-
- /// \brief Second node of the undirected edge.
- Node target(UEdge) const { return INVALID; }
-
- /// \brief Source node of the directed edge.
- Node source(Edge) const { return INVALID; }
-
- /// \brief Target node of the directed edge.
- Node target(Edge) const { return INVALID; }
-
- /// \brief Base node of the iterator
- ///
- /// Returns the base node (the source in this case) of the iterator
- Node baseNode(OutEdgeIt e) const {
- return source(e);
- }
-
- /// \brief Running node of the iterator
- ///
- /// Returns the running node (the target in this case) of the
- /// iterator
- Node runningNode(OutEdgeIt e) const {
- return target(e);
- }
-
- /// \brief Base node of the iterator
- ///
- /// Returns the base node (the target in this case) of the iterator
- Node baseNode(InEdgeIt e) const {
- return target(e);
- }
- /// \brief Running node of the iterator
- ///
- /// Returns the running node (the source in this case) of the
- /// iterator
- Node runningNode(InEdgeIt e) const {
- return source(e);
- }
-
- /// \brief Base node of the iterator
- ///
- /// Returns the base node of the iterator
- Node baseNode(IncEdgeIt) const {
- return INVALID;
- }
-
- /// \brief Running node of the iterator
- ///
- /// Returns the running node of the iterator
- Node runningNode(IncEdgeIt) const {
- return INVALID;
- }
-
- void first(Node&) const {}
- void next(Node&) const {}
-
- void first(Edge&) const {}
- void next(Edge&) const {}
-
- void first(UEdge&) const {}
- void next(UEdge&) const {}
-
- void firstANode(Node&) const {}
- void nextANode(Node&) const {}
-
- void firstBNode(Node&) const {}
- void nextBNode(Node&) const {}
-
- void firstIn(Edge&, const Node&) const {}
- void nextIn(Edge&) const {}
-
- void firstOut(Edge&, const Node&) const {}
- void nextOut(Edge&) const {}
-
- void firstInc(UEdge &, bool &, const Node &) const {}
- void nextInc(UEdge &, bool &) const {}
-
- void firstFromANode(UEdge&, const Node&) const {}
- void nextFromANode(UEdge&) const {}
-
- void firstFromBNode(UEdge&, const Node&) const {}
- void nextFromBNode(UEdge&) const {}
-
- template <typename Graph>
- struct Constraints {
- void constraints() {
- checkConcept<IterableBpUGraphComponent<>, Graph>();
- checkConcept<MappableBpUGraphComponent<>, Graph>();
- }
- };
-
- };
-
- }
-
-}
-
-#endif
diff --git a/src/lemon/concepts/graph.h b/src/lemon/concepts/graph.h
deleted file mode 100644
index 8189803..0000000
--- a/src/lemon/concepts/graph.h
+++ /dev/null
@@ -1,453 +0,0 @@
-/* -*- C++ -*-
- *
- * This file is a part of LEMON, a generic C++ optimization library
- *
- * Copyright (C) 2003-2008
- * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
- * (Egervary Research Group on Combinatorial Optimization, EGRES).
- *
- * Permission to use, modify and distribute this software is granted
- * provided that this copyright notice appears in all copies. For
- * precise terms see the accompanying LICENSE file.
- *
- * This software is provided "AS IS" with no warranty of any kind,
- * express or implied, and with no claim as to its suitability for any
- * purpose.
- *
- */
-
-#ifndef LEMON_CONCEPT_GRAPH_H
-#define LEMON_CONCEPT_GRAPH_H
-
-///\ingroup graph_concepts
-///\file
-///\brief The concept of Directed Graphs.
-
-#include <lemon/bits/invalid.h>
-#include <lemon/bits/utility.h>
-#include <lemon/concepts/maps.h>
-#include <lemon/concept_check.h>
-#include <lemon/concepts/graph_components.h>
-
-namespace lemon {
- namespace concepts {
-
- /// \ingroup graph_concepts
- ///
- /// \brief Class describing the concept of Directed Graphs.
- ///
- /// This class describes the \ref concept "concept" of the
- /// immutable directed graphs.
- ///
- /// Note that actual graph implementation like @ref ListGraph or
- /// @ref SmartGraph may have several additional functionality.
- ///
- /// \sa concept
- class Graph {
- private:
- ///Graphs are \e not copy constructible. Use GraphCopy() instead.
-
- ///Graphs are \e not copy constructible. Use GraphCopy() instead.
- ///
- Graph(const Graph &) {};
- ///\brief Assignment of \ref Graph "Graph"s to another ones are
- ///\e not allowed. Use GraphCopy() instead.
-
- ///Assignment of \ref Graph "Graph"s to another ones are
- ///\e not allowed. Use GraphCopy() instead.
-
- void operator=(const Graph &) {}
- public:
- ///\e
-
- /// Defalult constructor.
-
- /// Defalult constructor.
- ///
- Graph() { }
- /// Class for identifying a node of the graph
-
- /// This class identifies a node of the graph. It also serves
- /// as a base class of the node iterators,
- /// thus they will convert to this type.
- class Node {
- public:
- /// Default constructor
-
- /// @warning The default constructor sets the iterator
- /// to an undefined value.
- Node() { }
- /// Copy constructor.
-
- /// Copy constructor.
- ///
- Node(const Node&) { }
-
- /// Invalid constructor \& conversion.
-
- /// This constructor initializes the iterator to be invalid.
- /// \sa Invalid for more details.
- Node(Invalid) { }
- /// Equality operator
-
- /// Two iterators are equal if and only if they point to the
- /// same object or both are invalid.
- bool operator==(Node) const { return true; }
-
- /// Inequality operator
-
- /// \sa operator==(Node n)
- ///
- bool operator!=(Node) const { return true; }
-
- /// Artificial ordering operator.
-
- /// To allow the use of graph descriptors as key type in std::map or
- /// similar associative container we require this.
- ///
- /// \note This operator only have to define some strict ordering of
- /// the items; this order has nothing to do with the iteration
- /// ordering of the items.
- bool operator<(Node) const { return false; }
-
- };
-
- /// This iterator goes through each node.
-
- /// This iterator goes through each node.
- /// Its usage is quite simple, for example you can count the number
- /// of nodes in graph \c g of type \c Graph like this:
- ///\code
- /// int count=0;
- /// for (Graph::NodeIt n(g); n!=INVALID; ++n) ++count;
- ///\endcode
- class NodeIt : public Node {
- public:
- /// Default constructor
-
- /// @warning The default constructor sets the iterator
- /// to an undefined value.
- NodeIt() { }
- /// Copy constructor.
-
- /// Copy constructor.
- ///
- NodeIt(const NodeIt& n) : Node(n) { }
- /// Invalid constructor \& conversion.
-
- /// Initialize the iterator to be invalid.
- /// \sa Invalid for more details.
- NodeIt(Invalid) { }
- /// Sets the iterator to the first node.
-
- /// Sets the iterator to the first node of \c g.
- ///
- NodeIt(const Graph&) { }
- /// Node -> NodeIt conversion.
-
- /// Sets the iterator to the node of \c the graph pointed by
- /// the trivial iterator.
- /// This feature necessitates that each time we
- /// iterate the edge-set, the iteration order is the same.
- NodeIt(const Graph&, const Node&) { }
- /// Next node.
-
- /// Assign the iterator to the next node.
- ///
- NodeIt& operator++() { return *this; }
- };
-
-
- /// Class for identifying an edge of the graph
-
- /// This class identifies an edge of the graph. It also serves
- /// as a base class of the edge iterators,
- /// thus they will convert to this type.
- class Edge {
- public:
- /// Default constructor
-
- /// @warning The default constructor sets the iterator
- /// to an undefined value.
- Edge() { }
- /// Copy constructor.
-
- /// Copy constructor.
- ///
- Edge(const Edge&) { }
- /// Initialize the iterator to be invalid.
-
- /// Initialize the iterator to be invalid.
- ///
- Edge(Invalid) { }
- /// Equality operator
-
- /// Two iterators are equal if and only if they point to the
- /// same object or both are invalid.
- bool operator==(Edge) const { return true; }
- /// Inequality operator
-
- /// \sa operator==(Edge n)
- ///
- bool operator!=(Edge) const { return true; }
-
- /// Artificial ordering operator.
-
- /// To allow the use of graph descriptors as key type in std::map or
- /// similar associative container we require this.
- ///
- /// \note This operator only have to define some strict ordering of
- /// the items; this order has nothing to do with the iteration
- /// ordering of the items.
- bool operator<(Edge) const { return false; }
- };
-
- /// This iterator goes trough the outgoing edges of a node.
-
- /// This iterator goes trough the \e outgoing edges of a certain node
- /// of a graph.
- /// Its usage is quite simple, for example you can count the number
- /// of outgoing edges of a node \c n
- /// in graph \c g of type \c Graph as follows.
- ///\code
- /// int count=0;
- /// for (Graph::OutEdgeIt e(g, n); e!=INVALID; ++e) ++count;
- ///\endcode
-
- class OutEdgeIt : public Edge {
- public:
- /// Default constructor
-
- /// @warning The default constructor sets the iterator
- /// to an undefined value.
- OutEdgeIt() { }
- /// Copy constructor.
-
- /// Copy constructor.
- ///
- OutEdgeIt(const OutEdgeIt& e) : Edge(e) { }
- /// Initialize the iterator to be invalid.
-
- /// Initialize the iterator to be invalid.
- ///
- OutEdgeIt(Invalid) { }
- /// This constructor sets the iterator to the first outgoing edge.
-
- /// This constructor sets the iterator to the first outgoing edge of
- /// the node.
- OutEdgeIt(const Graph&, const Node&) { }
- /// Edge -> OutEdgeIt conversion
-
- /// Sets the iterator to the value of the trivial iterator.
- /// This feature necessitates that each time we
- /// iterate the edge-set, the iteration order is the same.
- OutEdgeIt(const Graph&, const Edge&) { }
- ///Next outgoing edge
-
- /// Assign the iterator to the next
- /// outgoing edge of the corresponding node.
- OutEdgeIt& operator++() { return *this; }
- };
-
- /// This iterator goes trough the incoming edges of a node.
-
- /// This iterator goes trough the \e incoming edges of a certain node
- /// of a graph.
- /// Its usage is quite simple, for example you can count the number
- /// of outgoing edges of a node \c n
- /// in graph \c g of type \c Graph as follows.
- ///\code
- /// int count=0;
- /// for(Graph::InEdgeIt e(g, n); e!=INVALID; ++e) ++count;
- ///\endcode
-
- class InEdgeIt : public Edge {
- public:
- /// Default constructor
-
- /// @warning The default constructor sets the iterator
- /// to an undefined value.
- InEdgeIt() { }
- /// Copy constructor.
-
- /// Copy constructor.
- ///
- InEdgeIt(const InEdgeIt& e) : Edge(e) { }
- /// Initialize the iterator to be invalid.
-
- /// Initialize the iterator to be invalid.
- ///
- InEdgeIt(Invalid) { }
- /// This constructor sets the iterator to first incoming edge.
-
- /// This constructor set the iterator to the first incoming edge of
- /// the node.
- InEdgeIt(const Graph&, const Node&) { }
- /// Edge -> InEdgeIt conversion
-
- /// Sets the iterator to the value of the trivial iterator \c e.
- /// This feature necessitates that each time we
- /// iterate the edge-set, the iteration order is the same.
- InEdgeIt(const Graph&, const Edge&) { }
- /// Next incoming edge
-
- /// Assign the iterator to the next inedge of the corresponding node.
- ///
- InEdgeIt& operator++() { return *this; }
- };
- /// This iterator goes through each edge.
-
- /// This iterator goes through each edge of a graph.
- /// Its usage is quite simple, for example you can count the number
- /// of edges in a graph \c g of type \c Graph as follows:
- ///\code
- /// int count=0;
- /// for(Graph::EdgeIt e(g); e!=INVALID; ++e) ++count;
- ///\endcode
- class EdgeIt : public Edge {
- public:
- /// Default constructor
-
- /// @warning The default constructor sets the iterator
- /// to an undefined value.
- EdgeIt() { }
- /// Copy constructor.
-
- /// Copy constructor.
- ///
- EdgeIt(const EdgeIt& e) : Edge(e) { }
- /// Initialize the iterator to be invalid.
-
- /// Initialize the iterator to be invalid.
- ///
- EdgeIt(Invalid) { }
- /// This constructor sets the iterator to the first edge.
-
- /// This constructor sets the iterator to the first edge of \c g.
- ///@param g the graph
- EdgeIt(const Graph& g) { ignore_unused_variable_warning(g); }
- /// Edge -> EdgeIt conversion
-
- /// Sets the iterator to the value of the trivial iterator \c e.
- /// This feature necessitates that each time we
- /// iterate the edge-set, the iteration order is the same.
- EdgeIt(const Graph&, const Edge&) { }
- ///Next edge
-
- /// Assign the iterator to the next edge.
- EdgeIt& operator++() { return *this; }
- };
- ///Gives back the target node of an edge.
-
- ///Gives back the target node of an edge.
- ///
- Node target(Edge) const { return INVALID; }
- ///Gives back the source node of an edge.
-
- ///Gives back the source node of an edge.
- ///
- Node source(Edge) const { return INVALID; }
-
- void first(Node&) const {}
- void next(Node&) const {}
-
- void first(Edge&) const {}
- void next(Edge&) const {}
-
-
- void firstIn(Edge&, const Node&) const {}
- void nextIn(Edge&) const {}
-
- void firstOut(Edge&, const Node&) const {}
- void nextOut(Edge&) const {}
-
- /// \brief The base node of the iterator.
- ///
- /// Gives back the base node of the iterator.
- /// It is always the target of the pointed edge.
- Node baseNode(const InEdgeIt&) const { return INVALID; }
-
- /// \brief The running node of the iterator.
- ///
- /// Gives back the running node of the iterator.
- /// It is always the source of the pointed edge.
- Node runningNode(const InEdgeIt&) const { return INVALID; }
-
- /// \brief The base node of the iterator.
- ///
- /// Gives back the base node of the iterator.
- /// It is always the source of the pointed edge.
- Node baseNode(const OutEdgeIt&) const { return INVALID; }
-
- /// \brief The running node of the iterator.
- ///
- /// Gives back the running node of the iterator.
- /// It is always the target of the pointed edge.
- Node runningNode(const OutEdgeIt&) const { return INVALID; }
-
- /// \brief The opposite node on the given edge.
- ///
- /// Gives back the opposite node on the given edge.
- Node oppositeNode(const Node&, const Edge&) const { return INVALID; }
-
- /// \brief Read write map of the nodes to type \c T.
- ///
- /// ReadWrite map of the nodes to type \c T.
- /// \sa Reference
- template<class T>
- class NodeMap : public ReadWriteMap< Node, T > {
- public:
-
- ///\e
- NodeMap(const Graph&) { }
- ///\e
- NodeMap(const Graph&, T) { }
-
- ///Copy constructor
- NodeMap(const NodeMap& nm) : ReadWriteMap< Node, T >(nm) { }
- ///Assignment operator
- template <typename CMap>
- NodeMap& operator=(const CMap&) {
- checkConcept<ReadMap<Node, T>, CMap>();
- return *this;
- }
- };
-
- /// \brief Read write map of the edges to type \c T.
- ///
- /// Reference map of the edges to type \c T.
- /// \sa Reference
- template<class T>
- class EdgeMap : public ReadWriteMap<Edge,T> {
- public:
-
- ///\e
- EdgeMap(const Graph&) { }
- ///\e
- EdgeMap(const Graph&, T) { }
- ///Copy constructor
- EdgeMap(const EdgeMap& em) : ReadWriteMap<Edge,T>(em) { }
- ///Assignment operator
- template <typename CMap>
- EdgeMap& operator=(const CMap&) {
- checkConcept<ReadMap<Edge, T>, CMap>();
- return *this;
- }
- };
-
- template <typename RGraph>
- struct Constraints {
- void constraints() {
- checkConcept<IterableGraphComponent<>, Graph>();
- checkConcept<MappableGraphComponent<>, Graph>();
- }
- };
-
- };
-
- } //namespace concepts
-} //namespace lemon
-
-
-
-#endif // LEMON_CONCEPT_GRAPH_H
diff --git a/src/lemon/concepts/graph_components.h b/src/lemon/concepts/graph_components.h
deleted file mode 100644
index 91aaf95..0000000
--- a/src/lemon/concepts/graph_components.h
+++ /dev/null
@@ -1,2093 +0,0 @@
-/* -*- C++ -*-
- *
- * This file is a part of LEMON, a generic C++ optimization library
- *
- * Copyright (C) 2003-2008
- * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
- * (Egervary Research Group on Combinatorial Optimization, EGRES).
- *
- * Permission to use, modify and distribute this software is granted
- * provided that this copyright notice appears in all copies. For
- * precise terms see the accompanying LICENSE file.
- *
- * This software is provided "AS IS" with no warranty of any kind,
- * express or implied, and with no claim as to its suitability for any
- * purpose.
- *
- */
-
-///\ingroup graph_concepts
-///\file
-///\brief The concept of graph components.
-
-
-#ifndef LEMON_CONCEPT_GRAPH_COMPONENTS_H
-#define LEMON_CONCEPT_GRAPH_COMPONENTS_H
-
-#include <lemon/bits/invalid.h>
-#include <lemon/concepts/maps.h>
-
-#include <lemon/bits/alteration_notifier.h>
-
-namespace lemon {
- namespace concepts {
-
- /// \brief Skeleton class for graph Node and Edge types
- ///
- /// This class describes the interface of Node and Edge (and UEdge
- /// in undirected graphs) subtypes of graph types.
- ///
- /// \note This class is a template class so that we can use it to
- /// create graph skeleton classes. The reason for this is than Node
- /// and Edge types should \em not derive from the same base class.
- /// For Node you should instantiate it with character 'n' and for Edge
- /// with 'e'.
-
-#ifndef DOXYGEN
- template <char _selector = '0'>
-#endif
- class GraphItem {
- public:
- /// \brief Default constructor.
- ///
- /// \warning The default constructor is not required to set
- /// the item to some well-defined value. So you should consider it
- /// as uninitialized.
- GraphItem() {}
- /// \brief Copy constructor.
- ///
- /// Copy constructor.
- ///
- GraphItem(const GraphItem &) {}
- /// \brief Invalid constructor \& conversion.
- ///
- /// This constructor initializes the item to be invalid.
- /// \sa Invalid for more details.
- GraphItem(Invalid) {}
- /// \brief Assign operator for nodes.
- ///
- /// The nodes are assignable.
- ///
- GraphItem& operator=(GraphItem const&) { return *this; }
- /// \brief Equality operator.
- ///
- /// Two iterators are equal if and only if they represents the
- /// same node in the graph or both are invalid.
- bool operator==(GraphItem) const { return false; }
- /// \brief Inequality operator.
- ///
- /// \sa operator==(const Node& n)
- ///
- bool operator!=(GraphItem) const { return false; }
-
- /// \brief Artificial ordering operator.
- ///
- /// To allow the use of graph descriptors as key type in std::map or
- /// similar associative container we require this.
- ///
- /// \note This operator only have to define some strict ordering of
- /// the items; this order has nothing to do with the iteration
- /// ordering of the items.
- bool operator<(GraphItem) const { return false; }
-
- template<typename _GraphItem>
- struct Constraints {
- void constraints() {
- _GraphItem i1;
- _GraphItem i2 = i1;
- _GraphItem i3 = INVALID;
-
- i1 = i2 = i3;
-
- bool b;
- // b = (ia == ib) && (ia != ib) && (ia < ib);
- b = (ia == ib) && (ia != ib);
- b = (ia == INVALID) && (ib != INVALID);
- b = (ia < ib);
- }
-
- const _GraphItem &ia;
- const _GraphItem &ib;
- };
- };
-
- /// \brief An empty base graph class.
- ///
- /// This class provides the minimal set of features needed for a graph
- /// structure. All graph concepts have to be conform to this base
- /// graph. It just provides types for nodes and edges and functions to
- /// get the source and the target of the edges.
- class BaseGraphComponent {
- public:
-
- typedef BaseGraphComponent Graph;
-
- /// \brief Node class of the graph.
- ///
- /// This class represents the Nodes of the graph.
- ///
- typedef GraphItem<'n'> Node;
-
- /// \brief Edge class of the graph.
- ///
- /// This class represents the Edges of the graph.
- ///
- typedef GraphItem<'e'> Edge;
-
- /// \brief Gives back the target node of an edge.
- ///
- /// Gives back the target node of an edge.
- ///
- Node target(const Edge&) const { return INVALID;}
-
- /// \brief Gives back the source node of an edge.
- ///
- /// Gives back the source node of an edge.
- ///
- Node source(const Edge&) const { return INVALID;}
-
- /// \brief Gives back the opposite node on the given edge.
- ///
- /// Gives back the opposite node on the given edge.
- Node oppositeNode(const Node&, const Edge&) const {
- return INVALID;
- }
-
- template <typename _Graph>
- struct Constraints {
- typedef typename _Graph::Node Node;
- typedef typename _Graph::Edge Edge;
-
- void constraints() {
- checkConcept<GraphItem<'n'>, Node>();
- checkConcept<GraphItem<'e'>, Edge>();
- {
- Node n;
- Edge e(INVALID);
- n = graph.source(e);
- n = graph.target(e);
- n = graph.oppositeNode(n, e);
- }
- }
-
- const _Graph& graph;
- };
- };
-
- /// \brief An empty base undirected graph class.
- ///
- /// This class provides the minimal set of features needed for an
- /// undirected graph structure. All undirected graph concepts have
- /// to be conform to this base graph. It just provides types for
- /// nodes, edges and undirected edges and functions to get the
- /// source and the target of the edges and undirected edges,
- /// conversion from edges to undirected edges and function to get
- /// both direction of the undirected edges.
- class BaseUGraphComponent : public BaseGraphComponent {
- public:
- typedef BaseGraphComponent::Node Node;
- typedef BaseGraphComponent::Edge Edge;
- /// \brief Undirected edge class of the graph.
- ///
- /// This class represents the undirected edges of the graph.
- /// The undirected graphs can be used as a directed graph which
- /// for each edge contains the opposite edge too so the graph is
- /// bidirected. The undirected edge represents two opposite
- /// directed edges.
- class UEdge : public GraphItem<'u'> {
- public:
- typedef GraphItem<'u'> Parent;
- /// \brief Default constructor.
- ///
- /// \warning The default constructor is not required to set
- /// the item to some well-defined value. So you should consider it
- /// as uninitialized.
- UEdge() {}
- /// \brief Copy constructor.
- ///
- /// Copy constructor.
- ///
- UEdge(const UEdge &) : Parent() {}
- /// \brief Invalid constructor \& conversion.
- ///
- /// This constructor initializes the item to be invalid.
- /// \sa Invalid for more details.
- UEdge(Invalid) {}
- /// \brief Converter from edge to undirected edge.
- ///
- /// Besides the core graph item functionality each edge should
- /// be convertible to the represented undirected edge.
- UEdge(const Edge&) {}
- /// \brief Assign edge to undirected edge.
- ///
- /// Besides the core graph item functionality each edge should
- /// be convertible to the represented undirected edge.
- UEdge& operator=(const Edge&) { return *this; }
- };
-
- /// \brief Returns the direction of the edge.
- ///
- /// Returns the direction of the edge. Each edge represents an
- /// undirected edge with a direction. It gives back the
- /// direction.
- bool direction(const Edge&) const { return true; }
-
- /// \brief Returns the directed edge.
- ///
- /// Returns the directed edge from its direction and the
- /// represented undirected edge.
- Edge direct(const UEdge&, bool) const { return INVALID;}
-
- /// \brief Returns the directed edge.
- ///
- /// Returns the directed edge from its source and the
- /// represented undirected edge.
- Edge direct(const UEdge&, const Node&) const { return INVALID;}
-
- /// \brief Returns the opposite edge.
- ///
- /// Returns the opposite edge. It is the edge representing the
- /// same undirected edge and has opposite direction.
- Edge oppositeEdge(const Edge&) const { return INVALID;}
-
- /// \brief Gives back the target node of an undirected edge.
- ///
- /// Gives back the target node of an undirected edge. The name
- /// target is a little confusing because the undirected edge
- /// does not have target but it just means that one of the end
- /// node.
- Node target(const UEdge&) const { return INVALID;}
-
- /// \brief Gives back the source node of an undirected edge.
- ///
- /// Gives back the source node of an undirected edge. The name
- /// source is a little confusing because the undirected edge
- /// does not have source but it just means that one of the end
- /// node.
- Node source(const UEdge&) const { return INVALID;}
-
- template <typename _Graph>
- struct Constraints {
- typedef typename _Graph::Node Node;
- typedef typename _Graph::Edge Edge;
- typedef typename _Graph::UEdge UEdge;
-
- void constraints() {
- checkConcept<BaseGraphComponent, _Graph>();
- checkConcept<GraphItem<'u'>, UEdge>();
- {
- Node n;
- UEdge ue(INVALID);
- Edge e;
- n = graph.source(ue);
- n = graph.target(ue);
- e = graph.direct(ue, true);
- e = graph.direct(ue, n);
- e = graph.oppositeEdge(e);
- ue = e;
- bool d = graph.direction(e);
- ignore_unused_variable_warning(d);
- }
- }
-
- const _Graph& graph;
- };
-
- };
-
- /// \brief An empty base bipartite undirected graph class.
- ///
- /// This class provides the minimal set of features needed for an
- /// bipartite undirected graph structure. All bipartite undirected
- /// graph concepts have to be conform to this base graph. It just
- /// provides types for nodes, A-nodes, B-nodes, edges and
- /// undirected edges and functions to get the source and the
- /// target of the edges and undirected edges, conversion from
- /// edges to undirected edges and function to get both direction
- /// of the undirected edges.
- class BaseBpUGraphComponent : public BaseUGraphComponent {
- public:
- typedef BaseUGraphComponent::Node Node;
- typedef BaseUGraphComponent::Edge Edge;
- typedef BaseUGraphComponent::UEdge UEdge;
-
- /// \brief Helper class for A-nodes.
- ///
- /// This class is just a helper class for A-nodes, it is not
- /// suggested to use it directly. It can be converted easily to
- /// node and vice versa. The usage of this class is limited
- /// to use just as template parameters for special map types.
- class ANode : public Node {
- public:
- typedef Node Parent;
-
- /// \brief Default constructor.
- ///
- /// \warning The default constructor is not required to set
- /// the item to some well-defined value. So you should consider it
- /// as uninitialized.
- ANode() {}
- /// \brief Copy constructor.
- ///
- /// Copy constructor.
- ///
- ANode(const ANode &) : Parent() {}
- /// \brief Invalid constructor \& conversion.
- ///
- /// This constructor initializes the item to be invalid.
- /// \sa Invalid for more details.
- ANode(Invalid) {}
- /// \brief Converter from node to A-node.
- ///
- /// Besides the core graph item functionality each node should
- /// be convertible to the represented A-node if it is it possible.
- ANode(const Node&) {}
- /// \brief Assign node to A-node.
- ///
- /// Besides the core graph item functionality each node should
- /// be convertible to the represented A-node if it is it possible.
- ANode& operator=(const Node&) { return *this; }
- };
-
- /// \brief Helper class for B-nodes.
- ///
- /// This class is just a helper class for B-nodes, it is not
- /// suggested to use it directly. It can be converted easily to
- /// node and vice versa. The usage of this class is limited
- /// to use just as template parameters for special map types.
- class BNode : public Node {
- public:
- typedef Node Parent;
-
- /// \brief Default constructor.
- ///
- /// \warning The default constructor is not required to set
- /// the item to some well-defined value. So you should consider it
- /// as uninitialized.
- BNode() {}
- /// \brief Copy constructor.
- ///
- /// Copy constructor.
- ///
- BNode(const BNode &) : Parent() {}
- /// \brief Invalid constructor \& conversion.
- ///
- /// This constructor initializes the item to be invalid.
- /// \sa Invalid for more details.
- BNode(Invalid) {}
- /// \brief Converter from node to B-node.
- ///
- /// Besides the core graph item functionality each node should
- /// be convertible to the represented B-node if it is it possible.
- BNode(const Node&) {}
- /// \brief Assign node to B-node.
- ///
- /// Besides the core graph item functionality each node should
- /// be convertible to the represented B-node if it is it possible.
- BNode& operator=(const Node&) { return *this; }
- };
-
- /// \brief Gives back %true when the node is A-node.
- ///
- /// Gives back %true when the node is A-node.
- bool aNode(const Node&) const { return false; }
-
- /// \brief Gives back %true when the node is B-node.
- ///
- /// Gives back %true when the node is B-node.
- bool bNode(const Node&) const { return false; }
-
- /// \brief Gives back the A-node of the undirected edge.
- ///
- /// Gives back the A-node of the undirected edge.
- Node aNode(const UEdge&) const { return INVALID; }
-
- /// \brief Gives back the B-node of the undirected edge.
- ///
- /// Gives back the B-node of the undirected edge.
- Node bNode(const UEdge&) const { return INVALID; }
-
- template <typename _Graph>
- struct Constraints {
- typedef typename _Graph::Node Node;
- typedef typename _Graph::ANode ANode;
- typedef typename _Graph::BNode BNode;
- typedef typename _Graph::Edge Edge;
- typedef typename _Graph::UEdge UEdge;
-
- void constraints() {
- checkConcept<BaseUGraphComponent, _Graph>();
- checkConcept<GraphItem<'a'>, ANode>();
- checkConcept<GraphItem<'b'>, BNode>();
- {
- Node n;
- UEdge ue(INVALID);
- bool b;
- n = graph.aNode(ue);
- n = graph.bNode(ue);
- b = graph.aNode(n);
- b = graph.bNode(n);
- ANode an;
- an = n; n = an;
- BNode bn;
- bn = n; n = bn;
- ignore_unused_variable_warning(b);
- }
- }
-
- const _Graph& graph;
- };
-
- };
-
- /// \brief An empty idable base graph class.
- ///
- /// This class provides beside the core graph features
- /// core id functions for the graph structure.
- /// The most of the base graphs should be conform to this concept.
- /// The id's are unique and immutable.
- template <typename _Base = BaseGraphComponent>
- class IDableGraphComponent : public _Base {
- public:
-
- typedef _Base Base;
- typedef typename Base::Node Node;
- typedef typename Base::Edge Edge;
-
- /// \brief Gives back an unique integer id for the Node.
- ///
- /// Gives back an unique integer id for the Node.
- ///
- int id(const Node&) const { return -1;}
-
- /// \brief Gives back the node by the unique id.
- ///
- /// Gives back the node by the unique id.
- /// If the graph does not contain node with the given id
- /// then the result of the function is undetermined.
- Node nodeFromId(int) const { return INVALID;}
-
- /// \brief Gives back an unique integer id for the Edge.
- ///
- /// Gives back an unique integer id for the Edge.
- ///
- int id(const Edge&) const { return -1;}
-
- /// \brief Gives back the edge by the unique id.
- ///
- /// Gives back the edge by the unique id.
- /// If the graph does not contain edge with the given id
- /// then the result of the function is undetermined.
- Edge edgeFromId(int) const { return INVALID;}
-
- /// \brief Gives back an integer greater or equal to the maximum
- /// Node id.
- ///
- /// Gives back an integer greater or equal to the maximum Node
- /// id.
- int maxNodeId() const { return -1;}
-
- /// \brief Gives back an integer greater or equal to the maximum
- /// Edge id.
- ///
- /// Gives back an integer greater or equal to the maximum Edge
- /// id.
- int maxEdgeId() const { return -1;}
-
- template <typename _Graph>
- struct Constraints {
-
- void constraints() {
- checkConcept<Base, _Graph >();
- typename _Graph::Node node;
- int nid = graph.id(node);
- nid = graph.id(node);
- node = graph.nodeFromId(nid);
- typename _Graph::Edge edge;
- int eid = graph.id(edge);
- eid = graph.id(edge);
- edge = graph.edgeFromId(eid);
-
- nid = graph.maxNodeId();
- ignore_unused_variable_warning(nid);
- eid = graph.maxEdgeId();
- ignore_unused_variable_warning(eid);
- }
-
- const _Graph& graph;
- };
- };
-
- /// \brief An empty idable base undirected graph class.
- ///
- /// This class provides beside the core undirected graph features
- /// core id functions for the undirected graph structure. The
- /// most of the base undirected graphs should be conform to this
- /// concept. The id's are unique and immutable.
- template <typename _Base = BaseUGraphComponent>
- class IDableUGraphComponent : public IDableGraphComponent<_Base> {
- public:
-
- typedef _Base Base;
- typedef typename Base::UEdge UEdge;
-
- using IDableGraphComponent<_Base>::id;
-
- /// \brief Gives back an unique integer id for the UEdge.
- ///
- /// Gives back an unique integer id for the UEdge.
- ///
- int id(const UEdge&) const { return -1;}
-
- /// \brief Gives back the undirected edge by the unique id.
- ///
- /// Gives back the undirected edge by the unique id. If the
- /// graph does not contain edge with the given id then the
- /// result of the function is undetermined.
- UEdge uEdgeFromId(int) const { return INVALID;}
-
- /// \brief Gives back an integer greater or equal to the maximum
- /// UEdge id.
- ///
- /// Gives back an integer greater or equal to the maximum UEdge
- /// id.
- int maxUEdgeId() const { return -1;}
-
- template <typename _Graph>
- struct Constraints {
-
- void constraints() {
- checkConcept<Base, _Graph >();
- checkConcept<IDableGraphComponent<Base>, _Graph >();
- typename _Graph::UEdge uedge;
- int ueid = graph.id(uedge);
- ueid = graph.id(uedge);
- uedge = graph.uEdgeFromId(ueid);
- ueid = graph.maxUEdgeId();
- ignore_unused_variable_warning(ueid);
- }
-
- const _Graph& graph;
- };
- };
-
- /// \brief An empty idable base bipartite undirected graph class.
- ///
- /// This class provides beside the core bipartite undirected graph
- /// features core id functions for the bipartite undirected graph
- /// structure. The most of the base undirected graphs should be
- /// conform to this concept.
- template <typename _Base = BaseBpUGraphComponent>
- class IDableBpUGraphComponent : public IDableUGraphComponent<_Base> {
- public:
-
- typedef _Base Base;
- typedef typename Base::Node Node;
-
- using IDableUGraphComponent<_Base>::id;
-
- /// \brief Gives back an unique integer id for the ANode.
- ///
- /// Gives back an unique integer id for the ANode.
- ///
- int aNodeId(const Node&) const { return -1;}
-
- /// \brief Gives back the undirected edge by the unique id.
- ///
- /// Gives back the undirected edge by the unique id. If the
- /// graph does not contain edge with the given id then the
- /// result of the function is undetermined.
- Node nodeFromANodeId(int) const { return INVALID;}
-
- /// \brief Gives back an integer greater or equal to the maximum
- /// ANode id.
- ///
- /// Gives back an integer greater or equal to the maximum ANode
- /// id.
- int maxANodeId() const { return -1;}
-
- /// \brief Gives back an unique integer id for the BNode.
- ///
- /// Gives back an unique integer id for the BNode.
- ///
- int bNodeId(const Node&) const { return -1;}
-
- /// \brief Gives back the undirected edge by the unique id.
- ///
- /// Gives back the undirected edge by the unique id. If the
- /// graph does not contain edge with the given id then the
- /// result of the function is undetermined.
- Node nodeFromBNodeId(int) const { return INVALID;}
-
- /// \brief Gives back an integer greater or equal to the maximum
- /// BNode id.
- ///
- /// Gives back an integer greater or equal to the maximum BNode
- /// id.
- int maxBNodeId() const { return -1;}
-
- template <typename _Graph>
- struct Constraints {
-
- void constraints() {
- checkConcept<Base, _Graph >();
- checkConcept<IDableGraphComponent<Base>, _Graph >();
- typename _Graph::Node node(INVALID);
- int id;
- id = graph.aNodeId(node);
- id = graph.bNodeId(node);
- node = graph.nodeFromANodeId(id);
- node = graph.nodeFromBNodeId(id);
- id = graph.maxANodeId();
- id = graph.maxBNodeId();
- }
-
- const _Graph& graph;
- };
- };
-
- /// \brief Skeleton class for graph NodeIt and EdgeIt
- ///
- /// Skeleton class for graph NodeIt and EdgeIt.
- ///
- template <typename _Graph, typename _Item>
- class GraphItemIt : public _Item {
- public:
- /// \brief Default constructor.
- ///
- /// @warning The default constructor sets the iterator
- /// to an undefined value.
- GraphItemIt() {}
- /// \brief Copy constructor.
- ///
- /// Copy constructor.
- ///
- GraphItemIt(const GraphItemIt& ) {}
- /// \brief Sets the iterator to the first item.
- ///
- /// Sets the iterator to the first item of \c the graph.
- ///
- explicit GraphItemIt(const _Graph&) {}
- /// \brief Invalid constructor \& conversion.
- ///
- /// This constructor initializes the item to be invalid.
- /// \sa Invalid for more details.
- GraphItemIt(Invalid) {}
- /// \brief Assign operator for items.
- ///
- /// The items are assignable.
- ///
- GraphItemIt& operator=(const GraphItemIt&) { return *this; }
- /// \brief Next item.
- ///
- /// Assign the iterator to the next item.
- ///
- GraphItemIt& operator++() { return *this; }
- /// \brief Equality operator
- ///
- /// Two iterators are equal if and only if they point to the
- /// same object or both are invalid.
- bool operator==(const GraphItemIt&) const { return true;}
- /// \brief Inequality operator
- ///
- /// \sa operator==(Node n)
- ///
- bool operator!=(const GraphItemIt&) const { return true;}
-
- template<typename _GraphItemIt>
- struct Constraints {
- void constraints() {
- _GraphItemIt it1(g);
- _GraphItemIt it2;
-
- it2 = ++it1;
- ++it2 = it1;
- ++(++it1);
-
- _Item bi = it1;
- bi = it2;
- }
- _Graph& g;
- };
- };
-
- /// \brief Skeleton class for graph InEdgeIt and OutEdgeIt
- ///
- /// \note Because InEdgeIt and OutEdgeIt may not inherit from the same
- /// base class, the _selector is a additional template parameter. For
- /// InEdgeIt you should instantiate it with character 'i' and for
- /// OutEdgeIt with 'o'.
- template <typename _Graph,
- typename _Item = typename _Graph::Edge,
- typename _Base = typename _Graph::Node,
- char _selector = '0'>
- class GraphIncIt : public _Item {
- public:
- /// \brief Default constructor.
- ///
- /// @warning The default constructor sets the iterator
- /// to an undefined value.
- GraphIncIt() {}
- /// \brief Copy constructor.
- ///
- /// Copy constructor.
- ///
- GraphIncIt(GraphIncIt const& gi) : _Item(gi) {}
- /// \brief Sets the iterator to the first edge incoming into or outgoing
- /// from the node.
- ///
- /// Sets the iterator to the first edge incoming into or outgoing
- /// from the node.
- ///
- explicit GraphIncIt(const _Graph&, const _Base&) {}
- /// \brief Invalid constructor \& conversion.
- ///
- /// This constructor initializes the item to be invalid.
- /// \sa Invalid for more details.
- GraphIncIt(Invalid) {}
- /// \brief Assign operator for iterators.
- ///
- /// The iterators are assignable.
- ///
- GraphIncIt& operator=(GraphIncIt const&) { return *this; }
- /// \brief Next item.
- ///
- /// Assign the iterator to the next item.
- ///
- GraphIncIt& operator++() { return *this; }
-
- /// \brief Equality operator
- ///
- /// Two iterators are equal if and only if they point to the
- /// same object or both are invalid.
- bool operator==(const GraphIncIt&) const { return true;}
-
- /// \brief Inequality operator
- ///
- /// \sa operator==(Node n)
- ///
- bool operator!=(const GraphIncIt&) const { return true;}
-
- template <typename _GraphIncIt>
- struct Constraints {
- void constraints() {
- checkConcept<GraphItem<_selector>, _GraphIncIt>();
- _GraphIncIt it1(graph, node);
- _GraphIncIt it2;
-
- it2 = ++it1;
- ++it2 = it1;
- ++(++it1);
- _Item e = it1;
- e = it2;
-
- }
-
- _Item edge;
- _Base node;
- _Graph graph;
- _GraphIncIt it;
- };
- };
-
-
- /// \brief An empty iterable graph class.
- ///
- /// This class provides beside the core graph features
- /// iterator based iterable interface for the graph structure.
- /// This concept is part of the Graph concept.
- template <typename _Base = BaseGraphComponent>
- class IterableGraphComponent : public _Base {
-
- public:
-
- typedef _Base Base;
- typedef typename Base::Node Node;
- typedef typename Base::Edge Edge;
-
- typedef IterableGraphComponent Graph;
-
- /// \name Base iteration
- ///
- /// This interface provides functions for iteration on graph items
- ///
- /// @{
-
- /// \brief Gives back the first node in the iterating order.
- ///
- /// Gives back the first node in the iterating order.
- ///
- void first(Node&) const {}
-
- /// \brief Gives back the next node in the iterating order.
- ///
- /// Gives back the next node in the iterating order.
- ///
- void next(Node&) const {}
-
- /// \brief Gives back the first edge in the iterating order.
- ///
- /// Gives back the first edge in the iterating order.
- ///
- void first(Edge&) const {}
-
- /// \brief Gives back the next edge in the iterating order.
- ///
- /// Gives back the next edge in the iterating order.
- ///
- void next(Edge&) const {}
-
-
- /// \brief Gives back the first of the edges point to the given
- /// node.
- ///
- /// Gives back the first of the edges point to the given node.
- ///
- void firstIn(Edge&, const Node&) const {}
-
- /// \brief Gives back the next of the edges points to the given
- /// node.
- ///
- /// Gives back the next of the edges points to the given node.
- ///
- void nextIn(Edge&) const {}
-
- /// \brief Gives back the first of the edges start from the
- /// given node.
- ///
- /// Gives back the first of the edges start from the given node.
- ///
- void firstOut(Edge&, const Node&) const {}
-
- /// \brief Gives back the next of the edges start from the given
- /// node.
- ///
- /// Gives back the next of the edges start from the given node.
- ///
- void nextOut(Edge&) const {}
-
- /// @}
-
- /// \name Class based iteration
- ///
- /// This interface provides functions for iteration on graph items
- ///
- /// @{
-
- /// \brief This iterator goes through each node.
- ///
- /// This iterator goes through each node.
- ///
- typedef GraphItemIt<Graph, Node> NodeIt;
-
- /// \brief This iterator goes through each node.
- ///
- /// This iterator goes through each node.
- ///
- typedef GraphItemIt<Graph, Edge> EdgeIt;
-
- /// \brief This iterator goes trough the incoming edges of a node.
- ///
- /// This iterator goes trough the \e inccoming edges of a certain node
- /// of a graph.
- typedef GraphIncIt<Graph, Edge, Node, 'i'> InEdgeIt;
-
- /// \brief This iterator goes trough the outgoing edges of a node.
- ///
- /// This iterator goes trough the \e outgoing edges of a certain node
- /// of a graph.
- typedef GraphIncIt<Graph, Edge, Node, 'o'> OutEdgeIt;
-
- /// \brief The base node of the iterator.
- ///
- /// Gives back the base node of the iterator.
- /// It is always the target of the pointed edge.
- Node baseNode(const InEdgeIt&) const { return INVALID; }
-
- /// \brief The running node of the iterator.
- ///
- /// Gives back the running node of the iterator.
- /// It is always the source of the pointed edge.
- Node runningNode(const InEdgeIt&) const { return INVALID; }
-
- /// \brief The base node of the iterator.
- ///
- /// Gives back the base node of the iterator.
- /// It is always the source of the pointed edge.
- Node baseNode(const OutEdgeIt&) const { return INVALID; }
-
- /// \brief The running node of the iterator.
- ///
- /// Gives back the running node of the iterator.
- /// It is always the target of the pointed edge.
- Node runningNode(const OutEdgeIt&) const { return INVALID; }
-
- /// @}
-
- template <typename _Graph>
- struct Constraints {
- void constraints() {
- checkConcept<Base, _Graph>();
-
- {
- typename _Graph::Node node(INVALID);
- typename _Graph::Edge edge(INVALID);
- {
- graph.first(node);
- graph.next(node);
- }
- {
- graph.first(edge);
- graph.next(edge);
- }
- {
- graph.firstIn(edge, node);
- graph.nextIn(edge);
- }
- {
- graph.firstOut(edge, node);
- graph.nextOut(edge);
- }
- }
-
- {
- checkConcept<GraphItemIt<_Graph, typename _Graph::Edge>,
- typename _Graph::EdgeIt >();
- checkConcept<GraphItemIt<_Graph, typename _Graph::Node>,
- typename _Graph::NodeIt >();
- checkConcept<GraphIncIt<_Graph, typename _Graph::Edge,
- typename _Graph::Node, 'i'>, typename _Graph::InEdgeIt>();
- checkConcept<GraphIncIt<_Graph, typename _Graph::Edge,
- typename _Graph::Node, 'o'>, typename _Graph::OutEdgeIt>();
-
- typename _Graph::Node n;
- typename _Graph::InEdgeIt ieit(INVALID);
- typename _Graph::OutEdgeIt oeit(INVALID);
- n = graph.baseNode(ieit);
- n = graph.runningNode(ieit);
- n = graph.baseNode(oeit);
- n = graph.runningNode(oeit);
- ignore_unused_variable_warning(n);
- }
- }
-
- const _Graph& graph;
-
- };
- };
-
- /// \brief An empty iterable undirected graph class.
- ///
- /// This class provides beside the core graph features iterator
- /// based iterable interface for the undirected graph structure.
- /// This concept is part of the UGraph concept.
- template <typename _Base = BaseUGraphComponent>
- class IterableUGraphComponent : public IterableGraphComponent<_Base> {
- public:
-
- typedef _Base Base;
- typedef typename Base::Node Node;
- typedef typename Base::Edge Edge;
- typedef typename Base::UEdge UEdge;
-
-
- typedef IterableUGraphComponent Graph;
-
- /// \name Base iteration
- ///
- /// This interface provides functions for iteration on graph items
- /// @{
-
- using IterableGraphComponent<_Base>::first;
- using IterableGraphComponent<_Base>::next;
-
- /// \brief Gives back the first undirected edge in the iterating
- /// order.
- ///
- /// Gives back the first undirected edge in the iterating order.
- ///
- void first(UEdge&) const {}
-
- /// \brief Gives back the next undirected edge in the iterating
- /// order.
- ///
- /// Gives back the next undirected edge in the iterating order.
- ///
- void next(UEdge&) const {}
-
-
- /// \brief Gives back the first of the undirected edges from the
- /// given node.
- ///
- /// Gives back the first of the undirected edges from the given
- /// node. The bool parameter gives back that direction which
- /// gives a good direction of the uedge so the source of the
- /// directed edge is the given node.
- void firstInc(UEdge&, bool&, const Node&) const {}
-
- /// \brief Gives back the next of the undirected edges from the
- /// given node.
- ///
- /// Gives back the next of the undirected edges from the given
- /// node. The bool parameter should be used as the \c firstInc()
- /// use it.
- void nextInc(UEdge&, bool&) const {}
-
- using IterableGraphComponent<_Base>::baseNode;
- using IterableGraphComponent<_Base>::runningNode;
-
- /// @}
-
- /// \name Class based iteration
- ///
- /// This interface provides functions for iteration on graph items
- ///
- /// @{
-
- /// \brief This iterator goes through each node.
- ///
- /// This iterator goes through each node.
- typedef GraphItemIt<Graph, UEdge> UEdgeIt;
- /// \brief This iterator goes trough the incident edges of a
- /// node.
- ///
- /// This iterator goes trough the incident edges of a certain
- /// node of a graph.
- typedef GraphIncIt<Graph, UEdge, Node, 'u'> IncEdgeIt;
- /// \brief The base node of the iterator.
- ///
- /// Gives back the base node of the iterator.
- Node baseNode(const IncEdgeIt&) const { return INVALID; }
-
- /// \brief The running node of the iterator.
- ///
- /// Gives back the running node of the iterator.
- Node runningNode(const IncEdgeIt&) const { return INVALID; }
-
- /// @}
-
- template <typename _Graph>
- struct Constraints {
- void constraints() {
- checkConcept<IterableGraphComponent<Base>, _Graph>();
-
- {
- typename _Graph::Node node(INVALID);
- typename _Graph::UEdge uedge(INVALID);
- bool dir;
- {
- graph.first(uedge);
- graph.next(uedge);
- }
- {
- graph.firstInc(uedge, dir, node);
- graph.nextInc(uedge, dir);
- }
-
- }
-
- {
- checkConcept<GraphItemIt<_Graph, typename _Graph::UEdge>,
- typename _Graph::UEdgeIt >();
- checkConcept<GraphIncIt<_Graph, typename _Graph::UEdge,
- typename _Graph::Node, 'u'>, typename _Graph::IncEdgeIt>();
-
- typename _Graph::Node n;
- typename _Graph::IncEdgeIt ueit(INVALID);
- n = graph.baseNode(ueit);
- n = graph.runningNode(ueit);
- }
- }
-
- const _Graph& graph;
-
- };
- };
-
- /// \brief An empty iterable bipartite undirected graph class.
- ///
- /// This class provides beside the core graph features iterator
- /// based iterable interface for the bipartite undirected graph
- /// structure. This concept is part of the BpUGraph concept.
- template <typename _Base = BaseUGraphComponent>
- class IterableBpUGraphComponent : public IterableUGraphComponent<_Base> {
- public:
-
- typedef _Base Base;
- typedef typename Base::Node Node;
- typedef typename Base::UEdge UEdge;
-
- typedef IterableBpUGraphComponent Graph;
-
- /// \name Base iteration
- ///
- /// This interface provides functions for iteration on graph items
- /// @{
-
- using IterableUGraphComponent<_Base>::first;
- using IterableUGraphComponent<_Base>::next;
-
- /// \brief Gives back the first A-node in the iterating order.
- ///
- /// Gives back the first undirected A-node in the iterating
- /// order.
- ///
- void firstANode(Node&) const {}
-
- /// \brief Gives back the next A-node in the iterating order.
- ///
- /// Gives back the next A-node in the iterating order.
- ///
- void nextANode(Node&) const {}
-
- /// \brief Gives back the first B-node in the iterating order.
- ///
- /// Gives back the first undirected B-node in the iterating
- /// order.
- ///
- void firstBNode(Node&) const {}
-
- /// \brief Gives back the next B-node in the iterating order.
- ///
- /// Gives back the next B-node in the iterating order.
- ///
- void nextBNode(Node&) const {}
-
-
- /// \brief Gives back the first of the undirected edges start
- /// from the given A-node.
- ///
- /// Gives back the first of the undirected edges start from the
- /// given A-node.
- void firstFromANode(UEdge&, const Node&) const {}
-
- /// \brief Gives back the next of the undirected edges start
- /// from the given A-node.
- ///
- /// Gives back the next of the undirected edges start from the
- /// given A-node.
- void nextFromANode(UEdge&) const {}
-
- /// \brief Gives back the first of the undirected edges start
- /// from the given B-node.
- ///
- /// Gives back the first of the undirected edges start from the
- /// given B-node.
- void firstFromBNode(UEdge&, const Node&) const {}
-
- /// \brief Gives back the next of the undirected edges start
- /// from the given B-node.
- ///
- /// Gives back the next of the undirected edges start from the
- /// given B-node.
- void nextFromBNode(UEdge&) const {}
-
-
- /// @}
-
- /// \name Class based iteration
- ///
- /// This interface provides functions for iteration on graph items
- ///
- /// @{
-
- /// \brief This iterator goes through each A-node.
- ///
- /// This iterator goes through each A-node.
- typedef GraphItemIt<Graph, Node> ANodeIt;
-
- /// \brief This iterator goes through each B-node.
- ///
- /// This iterator goes through each B-node.
- typedef GraphItemIt<Graph, Node> BNodeIt;
-
- /// @}
-
- template <typename _Graph>
- struct Constraints {
- void constraints() {
- checkConcept<IterableUGraphComponent<Base>, _Graph>();
-
- {
- typename _Graph::Node node(INVALID);
- typename _Graph::UEdge uedge(INVALID);
- graph.firstANode(node);
- graph.nextANode(node);
- graph.firstBNode(node);
- graph.nextBNode(node);
-
- graph.firstFromANode(uedge, node);
- graph.nextFromANode(uedge);
- graph.firstFromBNode(uedge, node);
- graph.nextFromBNode(uedge);
- }
- {
- checkConcept<GraphItemIt<_Graph, typename _Graph::Node>,
- typename _Graph::ANodeIt >();
- checkConcept<GraphItemIt<_Graph, typename _Graph::Node>,
- typename _Graph::BNodeIt >();
- }
-
- }
-
- const _Graph& graph;
-
- };
- };
-
- /// \brief An empty alteration notifier graph class.
- ///
- /// This class provides beside the core graph features alteration
- /// notifier interface for the graph structure. This implements
- /// an observer-notifier pattern for each graph item. More
- /// obsevers can be registered into the notifier and whenever an
- /// alteration occured in the graph all the observers will
- /// notified about it.
- template <typename _Base = BaseGraphComponent>
- class AlterableGraphComponent : public _Base {
- public:
-
- typedef _Base Base;
- typedef typename Base::Node Node;
- typedef typename Base::Edge Edge;
-
-
- /// The node observer registry.
- typedef AlterationNotifier<AlterableGraphComponent, Node>
- NodeNotifier;
- /// The edge observer registry.
- typedef AlterationNotifier<AlterableGraphComponent, Edge>
- EdgeNotifier;
-
- /// \brief Gives back the node alteration notifier.
- ///
- /// Gives back the node alteration notifier.
- NodeNotifier& notifier(Node) const {
- return NodeNotifier();
- }
-
- /// \brief Gives back the edge alteration notifier.
- ///
- /// Gives back the edge alteration notifier.
- EdgeNotifier& notifier(Edge) const {
- return EdgeNotifier();
- }
-
- template <typename _Graph>
- struct Constraints {
- void constraints() {
- checkConcept<Base, _Graph>();
- typename _Graph::NodeNotifier& nn
- = graph.notifier(typename _Graph::Node());
-
- typename _Graph::EdgeNotifier& en
- = graph.notifier(typename _Graph::Edge());
-
- ignore_unused_variable_warning(nn);
- ignore_unused_variable_warning(en);
- }
-
- const _Graph& graph;
-
- };
-
- };
-
- /// \brief An empty alteration notifier undirected graph class.
- ///
- /// This class provides beside the core graph features alteration
- /// notifier interface for the graph structure. This implements
- /// an observer-notifier pattern for each graph item. More
- /// obsevers can be registered into the notifier and whenever an
- /// alteration occured in the graph all the observers will
- /// notified about it.
- template <typename _Base = BaseUGraphComponent>
- class AlterableUGraphComponent : public AlterableGraphComponent<_Base> {
- public:
-
- typedef _Base Base;
- typedef typename Base::UEdge UEdge;
-
-
- /// The edge observer registry.
- typedef AlterationNotifier<AlterableUGraphComponent, UEdge>
- UEdgeNotifier;
-
- /// \brief Gives back the edge alteration notifier.
- ///
- /// Gives back the edge alteration notifier.
- UEdgeNotifier& notifier(UEdge) const {
- return UEdgeNotifier();
- }
-
- template <typename _Graph>
- struct Constraints {
- void constraints() {
- checkConcept<AlterableGraphComponent<Base>, _Graph>();
- typename _Graph::UEdgeNotifier& uen
- = graph.notifier(typename _Graph::UEdge());
- ignore_unused_variable_warning(uen);
- }
-
- const _Graph& graph;
-
- };
-
- };
-
- /// \brief An empty alteration notifier bipartite undirected graph
- /// class.
- ///
- /// This class provides beside the core graph features alteration
- /// notifier interface for the graph structure. This implements
- /// an observer-notifier pattern for each graph item. More
- /// obsevers can be registered into the notifier and whenever an
- /// alteration occured in the graph all the observers will
- /// notified about it.
- template <typename _Base = BaseUGraphComponent>
- class AlterableBpUGraphComponent : public AlterableUGraphComponent<_Base> {
- public:
-
- typedef _Base Base;
- typedef typename Base::ANode ANode;
- typedef typename Base::BNode BNode;
-
-
- /// The A-node observer registry.
- typedef AlterationNotifier<AlterableBpUGraphComponent, ANode>
- ANodeNotifier;
-
- /// The B-node observer registry.
- typedef AlterationNotifier<AlterableBpUGraphComponent, BNode>
- BNodeNotifier;
-
- /// \brief Gives back the A-node alteration notifier.
- ///
- /// Gives back the A-node alteration notifier.
- ANodeNotifier& notifier(ANode) const {
- return ANodeNotifier();
- }
-
- /// \brief Gives back the B-node alteration notifier.
- ///
- /// Gives back the B-node alteration notifier.
- BNodeNotifier& notifier(BNode) const {
- return BNodeNotifier();
- }
-
- template <typename _Graph>
- struct Constraints {
- void constraints() {
- checkConcept<AlterableUGraphComponent<Base>, _Graph>();
- typename _Graph::ANodeNotifier& ann
- = graph.notifier(typename _Graph::ANode());
- typename _Graph::BNodeNotifier& bnn
- = graph.notifier(typename _Graph::BNode());
- ignore_unused_variable_warning(ann);
- ignore_unused_variable_warning(bnn);
- }
-
- const _Graph& graph;
-
- };
-
- };
-
-
- /// \brief Class describing the concept of graph maps
- ///
- /// This class describes the common interface of the graph maps
- /// (NodeMap, EdgeMap), that is \ref maps-page "maps" which can be used to
- /// associate data to graph descriptors (nodes or edges).
- template <typename _Graph, typename _Item, typename _Value>
- class GraphMap : public ReadWriteMap<_Item, _Value> {
- public:
-
- typedef ReadWriteMap<_Item, _Value> Parent;
-
- /// The graph type of the map.
- typedef _Graph Graph;
- /// The key type of the map.
- typedef _Item Key;
- /// The value type of the map.
- typedef _Value Value;
-
- /// \brief Construct a new map.
- ///
- /// Construct a new map for the graph.
- explicit GraphMap(const Graph&) {}
- /// \brief Construct a new map with default value.
- ///
- /// Construct a new map for the graph and initalise the values.
- GraphMap(const Graph&, const Value&) {}
- /// \brief Copy constructor.
- ///
- /// Copy Constructor.
- GraphMap(const GraphMap&) : Parent() {}
-
- /// \brief Assign operator.
- ///
- /// Assign operator. It does not mofify the underlying graph,
- /// it just iterates on the current item set and set the map
- /// with the value returned by the assigned map.
- template <typename CMap>
- GraphMap& operator=(const CMap&) {
- checkConcept<ReadMap<Key, Value>, CMap>();
- return *this;
- }
-
- template<typename _Map>
- struct Constraints {
- void constraints() {
- checkConcept<ReadWriteMap<Key, Value>, _Map >();
- // Construction with a graph parameter
- _Map a(g);
- // Constructor with a graph and a default value parameter
- _Map a2(g,t);
- // Copy constructor.
- _Map b(c);
-
- ReadMap<Key, Value> cmap;
- b = cmap;
-
- ignore_unused_variable_warning(a2);
- ignore_unused_variable_warning(b);
- }
-
- const _Map &c;
- const Graph &g;
- const typename GraphMap::Value &t;
- };
-
- };
-
- /// \brief An empty mappable graph class.
- ///
- /// This class provides beside the core graph features
- /// map interface for the graph structure.
- /// This concept is part of the Graph concept.
- template <typename _Base = BaseGraphComponent>
- class MappableGraphComponent : public _Base {
- public:
-
- typedef _Base Base;
- typedef typename Base::Node Node;
- typedef typename Base::Edge Edge;
-
- typedef MappableGraphComponent Graph;
-
- /// \brief ReadWrite map of the nodes.
- ///
- /// ReadWrite map of the nodes.
- ///
- template <typename _Value>
- class NodeMap : public GraphMap<Graph, Node, _Value> {
- public:
- typedef GraphMap<MappableGraphComponent, Node, _Value> Parent;
-
- /// \brief Construct a new map.
- ///
- /// Construct a new map for the graph.
- explicit NodeMap(const MappableGraphComponent& graph)
- : Parent(graph) {}
-
- /// \brief Construct a new map with default value.
- ///
- /// Construct a new map for the graph and initalise the values.
- NodeMap(const MappableGraphComponent& graph, const _Value& value)
- : Parent(graph, value) {}
-
- /// \brief Copy constructor.
- ///
- /// Copy Constructor.
- NodeMap(const NodeMap& nm) : Parent(nm) {}
-
- /// \brief Assign operator.
- ///
- /// Assign operator.
- template <typename CMap>
- NodeMap& operator=(const CMap&) {
- checkConcept<ReadMap<Node, _Value>, CMap>();
- return *this;
- }
-
- };
-
- /// \brief ReadWrite map of the edges.
- ///
- /// ReadWrite map of the edges.
- ///
- template <typename _Value>
- class EdgeMap : public GraphMap<Graph, Edge, _Value> {
- public:
- typedef GraphMap<MappableGraphComponent, Edge, _Value> Parent;
-
- /// \brief Construct a new map.
- ///
- /// Construct a new map for the graph.
- explicit EdgeMap(const MappableGraphComponent& graph)
- : Parent(graph) {}
-
- /// \brief Construct a new map with default value.
- ///
- /// Construct a new map for the graph and initalise the values.
- EdgeMap(const MappableGraphComponent& graph, const _Value& value)
- : Parent(graph, value) {}
-
- /// \brief Copy constructor.
- ///
- /// Copy Constructor.
- EdgeMap(const EdgeMap& nm) : Parent(nm) {}
-
- /// \brief Assign operator.
- ///
- /// Assign operator.
- template <typename CMap>
- EdgeMap& operator=(const CMap&) {
- checkConcept<ReadMap<Edge, _Value>, CMap>();
- return *this;
- }
-
- };
-
-
- template <typename _Graph>
- struct Constraints {
-
- struct Dummy {
- int value;
- Dummy() : value(0) {}
- Dummy(int _v) : value(_v) {}
- };
-
- void constraints() {
- checkConcept<Base, _Graph>();
- { // int map test
- typedef typename _Graph::template NodeMap<int> IntNodeMap;
- checkConcept<GraphMap<_Graph, typename _Graph::Node, int>,
- IntNodeMap >();
- } { // bool map test
- typedef typename _Graph::template NodeMap<bool> BoolNodeMap;
- checkConcept<GraphMap<_Graph, typename _Graph::Node, bool>,
- BoolNodeMap >();
- } { // Dummy map test
- typedef typename _Graph::template NodeMap<Dummy> DummyNodeMap;
- checkConcept<GraphMap<_Graph, typename _Graph::Node, Dummy>,
- DummyNodeMap >();
- }
-
- { // int map test
- typedef typename _Graph::template EdgeMap<int> IntEdgeMap;
- checkConcept<GraphMap<_Graph, typename _Graph::Edge, int>,
- IntEdgeMap >();
- } { // bool map test
- typedef typename _Graph::template EdgeMap<bool> BoolEdgeMap;
- checkConcept<GraphMap<_Graph, typename _Graph::Edge, bool>,
- BoolEdgeMap >();
- } { // Dummy map test
- typedef typename _Graph::template EdgeMap<Dummy> DummyEdgeMap;
- checkConcept<GraphMap<_Graph, typename _Graph::Edge, Dummy>,
- DummyEdgeMap >();
- }
- }
-
- _Graph& graph;
- };
- };
-
- /// \brief An empty mappable base bipartite undirected graph class.
- ///
- /// This class provides beside the core graph features
- /// map interface for the graph structure.
- /// This concept is part of the UGraph concept.
- template <typename _Base = BaseUGraphComponent>
- class MappableUGraphComponent : public MappableGraphComponent<_Base> {
- public:
-
- typedef _Base Base;
- typedef typename Base::UEdge UEdge;
-
- typedef MappableUGraphComponent Graph;
-
- /// \brief ReadWrite map of the uedges.
- ///
- /// ReadWrite map of the uedges.
- ///
- template <typename _Value>
- class UEdgeMap : public GraphMap<Graph, UEdge, _Value> {
- public:
- typedef GraphMap<MappableUGraphComponent, UEdge, _Value> Parent;
-
- /// \brief Construct a new map.
- ///
- /// Construct a new map for the graph.
- explicit UEdgeMap(const MappableUGraphComponent& graph)
- : Parent(graph) {}
-
- /// \brief Construct a new map with default value.
- ///
- /// Construct a new map for the graph and initalise the values.
- UEdgeMap(const MappableUGraphComponent& graph, const _Value& value)
- : Parent(graph, value) {}
-
- /// \brief Copy constructor.
- ///
- /// Copy Constructor.
- UEdgeMap(const UEdgeMap& nm) : Parent(nm) {}
-
- /// \brief Assign operator.
- ///
- /// Assign operator.
- template <typename CMap>
- UEdgeMap& operator=(const CMap&) {
- checkConcept<ReadMap<UEdge, _Value>, CMap>();
- return *this;
- }
-
- };
-
-
- template <typename _Graph>
- struct Constraints {
-
- struct Dummy {
- int value;
- Dummy() : value(0) {}
- Dummy(int _v) : value(_v) {}
- };
-
- void constraints() {
- checkConcept<MappableGraphComponent<Base>, _Graph>();
-
- { // int map test
- typedef typename _Graph::template UEdgeMap<int> IntUEdgeMap;
- checkConcept<GraphMap<_Graph, typename _Graph::UEdge, int>,
- IntUEdgeMap >();
- } { // bool map test
- typedef typename _Graph::template UEdgeMap<bool> BoolUEdgeMap;
- checkConcept<GraphMap<_Graph, typename _Graph::UEdge, bool>,
- BoolUEdgeMap >();
- } { // Dummy map test
- typedef typename _Graph::template UEdgeMap<Dummy> DummyUEdgeMap;
- checkConcept<GraphMap<_Graph, typename _Graph::UEdge, Dummy>,
- DummyUEdgeMap >();
- }
- }
-
- _Graph& graph;
- };
- };
-
- /// \brief An empty mappable base bipartite undirected graph
- /// class.
- ///
- /// This class provides beside the core graph features
- /// map interface for the graph structure.
- /// This concept is part of the BpUGraph concept.
- template <typename _Base = BaseBpUGraphComponent>
- class MappableBpUGraphComponent : public MappableUGraphComponent<_Base> {
- public:
-
- typedef _Base Base;
- typedef typename Base::Node Node;
-
- typedef MappableBpUGraphComponent Graph;
-
- /// \brief ReadWrite map of the A-nodes.
- ///
- /// ReadWrite map of the A-nodes.
- ///
- template <typename _Value>
- class ANodeMap : public GraphMap<Graph, Node, _Value> {
- public:
- typedef GraphMap<MappableBpUGraphComponent, Node, _Value> Parent;
-
- /// \brief Construct a new map.
- ///
- /// Construct a new map for the graph.
- explicit ANodeMap(const MappableBpUGraphComponent& graph)
- : Parent(graph) {}
-
- /// \brief Construct a new map with default value.
- ///
- /// Construct a new map for the graph and initalise the values.
- ANodeMap(const MappableBpUGraphComponent& graph, const _Value& value)
- : Parent(graph, value) {}
-
- /// \brief Copy constructor.
- ///
- /// Copy Constructor.
- ANodeMap(const ANodeMap& nm) : Parent(nm) {}
-
- /// \brief Assign operator.
- ///
- /// Assign operator.
- template <typename CMap>
- ANodeMap& operator=(const CMap&) {
- checkConcept<ReadMap<Node, _Value>, CMap>();
- return *this;
- }
-
- };
-
- /// \brief ReadWrite map of the B-nodes.
- ///
- /// ReadWrite map of the A-nodes.
- ///
- template <typename _Value>
- class BNodeMap : public GraphMap<Graph, Node, _Value> {
- public:
- typedef GraphMap<MappableBpUGraphComponent, Node, _Value> Parent;
-
- /// \brief Construct a new map.
- ///
- /// Construct a new map for the graph.
- explicit BNodeMap(const MappableBpUGraphComponent& graph)
- : Parent(graph) {}
-
- /// \brief Construct a new map with default value.
- ///
- /// Construct a new map for the graph and initalise the values.
- BNodeMap(const MappableBpUGraphComponent& graph, const _Value& value)
- : Parent(graph, value) {}
-
- /// \brief Copy constructor.
- ///
- /// Copy Constructor.
- BNodeMap(const BNodeMap& nm) : Parent(nm) {}
-
- /// \brief Assign operator.
- ///
- /// Assign operator.
- template <typename CMap>
- BNodeMap& operator=(const CMap&) {
- checkConcept<ReadMap<Node, _Value>, CMap>();
- return *this;
- }
-
- };
-
-
- template <typename _Graph>
- struct Constraints {
-
- struct Dummy {
- int value;
- Dummy() : value(0) {}
- Dummy(int _v) : value(_v) {}
- };
-
- void constraints() {
- checkConcept<MappableUGraphComponent<Base>, _Graph>();
-
- { // int map test
- typedef typename _Graph::template ANodeMap<int> IntANodeMap;
- checkConcept<GraphMap<_Graph, typename _Graph::ANode, int>,
- IntANodeMap >();
- } { // bool map test
- typedef typename _Graph::template ANodeMap<bool> BoolANodeMap;
- checkConcept<GraphMap<_Graph, typename _Graph::ANode, bool>,
- BoolANodeMap >();
- } { // Dummy map test
- typedef typename _Graph::template ANodeMap<Dummy> DummyANodeMap;
- checkConcept<GraphMap<_Graph, typename _Graph::ANode, Dummy>,
- DummyANodeMap >();
- }
- }
-
- _Graph& graph;
- };
- };
-
-
- /// \brief An empty extendable graph class.
- ///
- /// This class provides beside the core graph features graph
- /// extendable interface for the graph structure. The main
- /// difference between the base and this interface is that the
- /// graph alterations should handled already on this level.
- template <typename _Base = BaseGraphComponent>
- class ExtendableGraphComponent : public _Base {
- public:
- typedef _Base Base;
-
- typedef typename _Base::Node Node;
- typedef typename _Base::Edge Edge;
-
- /// \brief Adds a new node to the graph.
- ///
- /// Adds a new node to the graph.
- ///
- Node addNode() {
- return INVALID;
- }
-
- /// \brief Adds a new edge connects the given two nodes.
- ///
- /// Adds a new edge connects the the given two nodes.
- Edge addEdge(const Node&, const Node&) {
- return INVALID;
- }
-
- template <typename _Graph>
- struct Constraints {
- void constraints() {
- checkConcept<Base, _Graph>();
- typename _Graph::Node node_a, node_b;
- node_a = graph.addNode();
- node_b = graph.addNode();
- typename _Graph::Edge edge;
- edge = graph.addEdge(node_a, node_b);
- }
-
- _Graph& graph;
- };
- };
-
- /// \brief An empty extendable base undirected graph class.
- ///
- /// This class provides beside the core undirected graph features
- /// core undircted graph extend interface for the graph structure.
- /// The main difference between the base and this interface is
- /// that the graph alterations should handled already on this
- /// level.
- template <typename _Base = BaseUGraphComponent>
- class ExtendableUGraphComponent : public _Base {
- public:
-
- typedef _Base Base;
- typedef typename _Base::Node Node;
- typedef typename _Base::UEdge UEdge;
-
- /// \brief Adds a new node to the graph.
- ///
- /// Adds a new node to the graph.
- ///
- Node addNode() {
- return INVALID;
- }
-
- /// \brief Adds a new edge connects the given two nodes.
- ///
- /// Adds a new edge connects the the given two nodes.
- UEdge addEdge(const Node&, const Node&) {
- return INVALID;
- }
-
- template <typename _Graph>
- struct Constraints {
- void constraints() {
- checkConcept<Base, _Graph>();
- typename _Graph::Node node_a, node_b;
- node_a = graph.addNode();
- node_b = graph.addNode();
- typename _Graph::UEdge uedge;
- uedge = graph.addUEdge(node_a, node_b);
- }
-
- _Graph& graph;
- };
- };
-
- /// \brief An empty extendable base undirected graph class.
- ///
- /// This class provides beside the core bipartite undirected graph
- /// features core undircted graph extend interface for the graph
- /// structure. The main difference between the base and this
- /// interface is that the graph alterations should handled already
- /// on this level.
- template <typename _Base = BaseBpUGraphComponent>
- class ExtendableBpUGraphComponent
- : public ExtendableUGraphComponent<_Base> {
-
- typedef _Base Base;
-
- template <typename _Graph>
- struct Constraints {
- void constraints() {
- checkConcept<ExtendableUGraphComponent<Base>, _Graph>();
- }
- };
- };
-
- /// \brief An empty erasable graph class.
- ///
- /// This class provides beside the core graph features core erase
- /// functions for the graph structure. The main difference between
- /// the base and this interface is that the graph alterations
- /// should handled already on this level.
- template <typename _Base = BaseGraphComponent>
- class ErasableGraphComponent : public _Base {
- public:
-
- typedef _Base Base;
- typedef typename Base::Node Node;
- typedef typename Base::Edge Edge;
-
- /// \brief Erase a node from the graph.
- ///
- /// Erase a node from the graph. This function should
- /// erase all edges connecting to the node.
- void erase(const Node&) {}
-
- /// \brief Erase an edge from the graph.
- ///
- /// Erase an edge from the graph.
- ///
- void erase(const Edge&) {}
-
- template <typename _Graph>
- struct Constraints {
- void constraints() {
- checkConcept<Base, _Graph>();
- typename _Graph::Node node;
- graph.erase(node);
- typename _Graph::Edge edge;
- graph.erase(edge);
- }
-
- _Graph& graph;
- };
- };
-
- /// \brief An empty erasable base undirected graph class.
- ///
- /// This class provides beside the core undirected graph features
- /// core erase functions for the undirceted graph structure. The
- /// main difference between the base and this interface is that
- /// the graph alterations should handled already on this level.
- template <typename _Base = BaseUGraphComponent>
- class ErasableUGraphComponent : public _Base {
- public:
-
- typedef _Base Base;
- typedef typename Base::Node Node;
- typedef typename Base::UEdge UEdge;
-
- /// \brief Erase a node from the graph.
- ///
- /// Erase a node from the graph. This function should erase
- /// edges connecting to the node.
- void erase(const Node&) {}
-
- /// \brief Erase an edge from the graph.
- ///
- /// Erase an edge from the graph.
- ///
- void erase(const UEdge&) {}
-
- template <typename _Graph>
- struct Constraints {
- void constraints() {
- checkConcept<Base, _Graph>();
- typename _Graph::Node node;
- graph.erase(node);
- typename _Graph::Edge edge;
- graph.erase(edge);
- }
-
- _Graph& graph;
- };
- };
-
- /// \brief An empty erasable base bipartite undirected graph class.
- ///
- /// This class provides beside the core bipartite undirected graph
- /// features core erase functions for the undirceted graph
- /// structure. The main difference between the base and this
- /// interface is that the graph alterations should handled already
- /// on this level.
- template <typename _Base = BaseBpUGraphComponent>
- class ErasableBpUGraphComponent : public ErasableUGraphComponent<_Base> {
- public:
-
- typedef _Base Base;
-
- template <typename _Graph>
- struct Constraints {
- void constraints() {
- checkConcept<ErasableUGraphComponent<Base>, _Graph>();
- }
- };
- };
-
- /// \brief An empty clearable base graph class.
- ///
- /// This class provides beside the core graph features core clear
- /// functions for the graph structure. The main difference between
- /// the base and this interface is that the graph alterations
- /// should handled already on this level.
- template <typename _Base = BaseGraphComponent>
- class ClearableGraphComponent : public _Base {
- public:
-
- typedef _Base Base;
-
- /// \brief Erase all nodes and edges from the graph.
- ///
- /// Erase all nodes and edges from the graph.
- ///
- void clear() {}
-
- template <typename _Graph>
- struct Constraints {
- void constraints() {
- checkConcept<Base, _Graph>();
- graph.clear();
- }
-
- _Graph graph;
- };
- };
-
- /// \brief An empty clearable base undirected graph class.
- ///
- /// This class provides beside the core undirected graph features
- /// core clear functions for the undirected graph structure. The
- /// main difference between the base and this interface is that
- /// the graph alterations should handled already on this level.
- template <typename _Base = BaseUGraphComponent>
- class ClearableUGraphComponent : public ClearableGraphComponent<_Base> {
- public:
-
- typedef _Base Base;
-
- template <typename _Graph>
- struct Constraints {
- void constraints() {
- checkConcept<ClearableUGraphComponent<Base>, _Graph>();
- }
-
- _Graph graph;
- };
- };
-
- /// \brief An empty clearable base bipartite undirected graph
- /// class.
- ///
- /// This class provides beside the core bipartite undirected graph
- /// features core clear functions for the undirected graph
- /// structure. The main difference between the base and this
- /// interface is that the graph alterations should handled already
- /// on this level.
- template <typename _Base = BaseUGraphComponent>
- class ClearableBpUGraphComponent : public ClearableUGraphComponent<_Base> {
- public:
-
- typedef _Base Base;
-
- template <typename _Graph>
- struct Constraints {
- void constraints() {
- checkConcept<ClearableBpUGraphComponent<Base>, _Graph>();
- }
-
- };
-
- };
-
- }
-
-}
-
-#endif
diff --git a/src/lemon/concepts/heap.h b/src/lemon/concepts/heap.h
deleted file mode 100644
index 90e8cc0..0000000
--- a/src/lemon/concepts/heap.h
+++ /dev/null
@@ -1,226 +0,0 @@
-/* -*- C++ -*-
- *
- * This file is a part of LEMON, a generic C++ optimization library
- *
- * Copyright (C) 2003-2008
- * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
- * (Egervary Research Group on Combinatorial Optimization, EGRES).
- *
- * Permission to use, modify and distribute this software is granted
- * provided that this copyright notice appears in all copies. For
- * precise terms see the accompanying LICENSE file.
- *
- * This software is provided "AS IS" with no warranty of any kind,
- * express or implied, and with no claim as to its suitability for any
- * purpose.
- *
- */
-
-///\ingroup concept
-///\file
-///\brief Classes for representing heaps.
-///
-
-#ifndef LEMON_CONCEPT_HEAP_H
-#define LEMON_CONCEPT_HEAP_H
-
-#include <lemon/bits/invalid.h>
-
-namespace lemon {
- namespace concepts {
- /// \addtogroup concept
- /// @{
-
-
- /// \brief A concept structure describes the main interface of heaps.
- ///
- /// A concept structure describes the main interface of heaps.
- ///
- template <typename Prio, typename ItemIntMap>
- class Heap {
- public:
-
- ///\brief Type of the items stored in the heap.
- typedef typename ItemIntMap::Key Item;
-
-
- /// \brief Type to represent the items states.
- ///
- /// Each Item element have a state associated to it. It may be "in heap",
- /// "pre heap" or "post heap". The later two are indifferent from the
- /// heap's point of view, but may be useful to the user.
- ///
- /// The ItemIntMap _should_ be initialized in such way, that it maps
- /// PRE_HEAP (-1) to any element to be put in the heap...
- enum State {
- IN_HEAP = 0,
- PRE_HEAP = -1,
- POST_HEAP = -2
- };
-
- /// \brief The constructor.
- ///
- /// The constructor.
- /// \param _iim should be given to the constructor, since it is used
- /// internally to handle the cross references. The value of the map
- /// should be PRE_HEAP (-1) for each element.
- explicit Heap(ItemIntMap &_iim) {}
-
- /// \brief The number of items stored in the heap.
- ///
- /// Returns the number of items stored in the heap.
- int size() const { return 0; }
-
- /// \brief Checks if the heap stores no items.
- ///
- /// Returns \c true if and only if the heap stores no items.
- bool empty() const { return false; }
-
- /// \brief Makes empty this heap.
- ///
- /// Makes this heap empty.
- void clear();
-
- /// \brief Insert an item into the heap with the given heap.
- ///
- /// Adds \c i to the heap with priority \c p.
- /// \param i The item to insert.
- /// \param p The priority of the item.
- void push(const Item &i, const Prio &p) {}
-
- /// \brief Returns the item with minimum priority.
- ///
- /// This method returns the item with minimum priority.
- /// \pre The heap must be nonempty.
- Item top() const {}
-
- /// \brief Returns the minimum priority.
- ///
- /// It returns the minimum priority.
- /// \pre The heap must be nonempty.
- Prio prio() const {}
-
- /// \brief Deletes the item with minimum priority.
- ///
- /// This method deletes the item with minimum priority.
- /// \pre The heap must be non-empty.
- void pop() {}
-
- /// \brief Deletes \c i from the heap.
- ///
- /// This method deletes item \c i from the heap, if \c i was
- /// already stored in the heap.
- /// \param i The item to erase.
- void erase(const Item &i) {}
-
- /// \brief Returns the priority of \c i.
- ///
- /// This function returns the priority of item \c i.
- /// \pre \c i must be in the heap.
- /// \param i The item.
- Prio operator[](const Item &i) const {}
-
- /// \brief \c i gets to the heap with priority \c p independently
- /// if \c i was already there.
- ///
- /// This method calls \ref push(\c i, \c p) if \c i is not stored
- /// in the heap and sets the priority of \c i to \c p otherwise.
- /// It may throw an \e UnderFlowPriorityException.
- /// \param i The item.
- /// \param p The priority.
- void set(const Item &i, const Prio &p) {}
-
- /// \brief Decreases the priority of \c i to \c p.
- ///
- /// This method decreases the priority of item \c i to \c p.
- /// \pre \c i must be stored in the heap with priority at least \c p.
- /// \param i The item.
- /// \param p The priority.
- void decrease(const Item &i, const Prio &p) {}
-
- /// \brief Increases the priority of \c i to \c p.
- ///
- /// This method sets the priority of item \c i to \c p.
- /// \pre \c i must be stored in the heap with priority at most \c
- /// p relative to \c Compare.
- /// \param i The item.
- /// \param p The priority.
- void increase(const Item &i, const Prio &p) {}
-
- /// \brief Returns if \c item is in, has already been in, or has
- /// never been in the heap.
- ///
- /// This method returns PRE_HEAP if \c item has never been in the
- /// heap, IN_HEAP if it is in the heap at the moment, and POST_HEAP
- /// otherwise. In the latter case it is possible that \c item will
- /// get back to the heap again.
- /// \param i The item.
- State state(const Item &i) const {}
-
- /// \brief Sets the state of the \c item in the heap.
- ///
- /// Sets the state of the \c item in the heap. It can be used to
- /// manually clear the heap when it is important to achive the
- /// better time complexity.
- /// \param i The item.
- /// \param st The state. It should not be \c IN_HEAP.
- void state(const Item& i, State st) {}
-
-
- template <typename _Heap>
- struct Constraints {
- public:
-
- void constraints() {
- Item item;
- Prio prio;
-
- item=Item();
- prio=Prio();
-
- ignore_unused_variable_warning(item);
- ignore_unused_variable_warning(prio);
-
- typedef typename _Heap::State State;
- State state;
-
- ignore_unused_variable_warning(state);
-
- _Heap heap1 = _Heap(map);
-
- ignore_unused_variable_warning(heap1);
-
- heap.push(item, prio);
-
- prio = heap.prio();
- item = heap.top();
-
- heap.pop();
-
- heap.set(item, prio);
- heap.decrease(item, prio);
- heap.increase(item, prio);
- prio = heap[item];
-
- heap.erase(item);
-
- state = heap.state(item);
-
- state = _Heap::PRE_HEAP;
- state = _Heap::IN_HEAP;
- state = _Heap::POST_HEAP;
-
- heap.clear();
- }
-
- _Heap& heap;
- ItemIntMap& map;
-
- Constraints() : heap(0), map(0) {}
- };
- };
-
- /// @}
- } // namespace lemon
-}
-#endif // LEMON_CONCEPT_PATH_H
diff --git a/src/lemon/concepts/maps.h b/src/lemon/concepts/maps.h
deleted file mode 100644
index 6d7ace0..0000000
--- a/src/lemon/concepts/maps.h
+++ /dev/null
@@ -1,208 +0,0 @@
-/* -*- C++ -*-
- *
- * This file is a part of LEMON, a generic C++ optimization library
- *
- * Copyright (C) 2003-2008
- * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
- * (Egervary Research Group on Combinatorial Optimization, EGRES).
- *
- * Permission to use, modify and distribute this software is granted
- * provided that this copyright notice appears in all copies. For
- * precise terms see the accompanying LICENSE file.
- *
- * This software is provided "AS IS" with no warranty of any kind,
- * express or implied, and with no claim as to its suitability for any
- * purpose.
- *
- */
-
-#ifndef LEMON_CONCEPT_MAPS_H
-#define LEMON_CONCEPT_MAPS_H
-
-#include <lemon/bits/utility.h>
-#include <lemon/concept_check.h>
-
-///\ingroup concept
-///\file
-///\brief Map concepts checking classes for testing and documenting.
-
-namespace lemon {
-
- namespace concepts {
-
- /// \addtogroup concept
- /// @{
-
- /// Readable map concept
-
- /// Readable map concept.
- ///
- template<typename K, typename T>
- class ReadMap
- {
- public:
- /// The key type of the map.
- typedef K Key;
- /// The value type of the map. (The type of objects associated with the keys).
- typedef T Value;
-
- /// Returns the value associated with a key.
-
- /// Returns the value associated with a key.
- /// \bug Value shouldn't need to be default constructible.
- ///
- Value operator[](const Key &) const {return Value();}
-
- template<typename _ReadMap>
- struct Constraints {
-
- void constraints() {
- Value val = m[key];
- val = m[key];
- typename _ReadMap::Value own_val = m[own_key];
- own_val = m[own_key];
-
- ignore_unused_variable_warning(val);
- ignore_unused_variable_warning(own_val);
- ignore_unused_variable_warning(key);
- }
- Key& key;
- typename _ReadMap::Key& own_key;
- _ReadMap& m;
- };
-
- };
-
-
- /// Writable map concept
-
- /// Writable map concept.
- ///
- template<typename K, typename T>
- class WriteMap
- {
- public:
- /// The key type of the map.
- typedef K Key;
- /// The value type of the map. (The type of objects associated with the keys).
- typedef T Value;
-
- /// Sets the value associated with a key.
- void set(const Key &,const Value &) {}
-
- ///Default constructor
- WriteMap() {}
-
- template <typename _WriteMap>
- struct Constraints {
- void constraints() {
- // No constraints for constructor.
- m.set(key, val);
- m.set(own_key, own_val);
- ignore_unused_variable_warning(key);
- ignore_unused_variable_warning(val);
- ignore_unused_variable_warning(own_key);
- ignore_unused_variable_warning(own_val);
- }
-
- Value& val;
- typename _WriteMap::Value own_val;
- Key& key;
- typename _WriteMap::Key& own_key;
- _WriteMap& m;
-
- };
- };
-
- /// Read/writable map concept
-
- /// Read/writable map concept.
- ///
- template<typename K, typename T>
- class ReadWriteMap : public ReadMap<K,T>,
- public WriteMap<K,T>
- {
- public:
- /// The key type of the map.
- typedef K Key;
- /// The value type of the map. (The type of objects associated with the keys).
- typedef T Value;
-
- /// Returns the value associated with a key.
- Value operator[](const Key &) const {return Value();}
- /// Sets the value associated with a key.
- void set(const Key & ,const Value &) {}
-
- template<typename _ReadWriteMap>
- struct Constraints {
- void constraints() {
- checkConcept<ReadMap<K, T>, _ReadWriteMap >();
- checkConcept<WriteMap<K, T>, _ReadWriteMap >();
- }
- };
- };
-
-
- ///Dereferable map concept
-
- /// Dereferable map concept.
- ///
- /// \todo Rethink this concept.
- template<typename K, typename T, typename R, typename CR>
- class ReferenceMap : public ReadWriteMap<K,T>
- {
- public:
- /// Tag for reference maps.
- typedef True ReferenceMapTag;
- /// The key type of the map.
- typedef K Key;
- /// The value type of the map. (The type of objects associated with the keys).
- typedef T Value;
- /// The reference type of the map.
- typedef R Reference;
- /// The const reference type of the map.
- typedef CR ConstReference;
-
- protected:
- Value tmp;
- public:
-
- ///Returns a reference to the value associated with a key.
- Reference operator[](const Key &) { return tmp; }
- ///Returns a const reference to the value associated with a key.
- ConstReference operator[](const Key &) const { return tmp; }
- /// Sets the value associated with a key.
- void set(const Key &k,const Value &t) { operator[](k)=t; }
-
- template<typename _ReferenceMap>
- struct Constraints {
-
- void constraints() {
- checkConcept<ReadWriteMap<K, T>, _ReferenceMap >();
- m[key] = val;
- val = m[key];
- m[key] = ref;
- ref = m[key];
- m[own_key] = own_val;
- own_val = m[own_key];
- m[own_key] = own_ref;
- own_ref = m[own_key];
- }
-
- typename _ReferenceMap::Key& own_key;
- typename _ReferenceMap::Value& own_val;
- typename _ReferenceMap::Reference own_ref;
- Key& key;
- Value& val;
- Reference ref;
- _ReferenceMap& m;
- };
- };
-
- // @}
-
- } //namespace concepts
-
-} //namespace lemon
-
-#endif // LEMON_CONCEPT_MAPS_H
diff --git a/src/lemon/concepts/matrix_maps.h b/src/lemon/concepts/matrix_maps.h
deleted file mode 100644
index 07943e9..0000000
--- a/src/lemon/concepts/matrix_maps.h
+++ /dev/null
@@ -1,207 +0,0 @@
-/* -*- C++ -*-
- *
- * This file is a part of LEMON, a generic C++ optimization library
- *
- * Copyright (C) 2003-2008
- * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
- * (Egervary Research Group on Combinatorial Optimization, EGRES).
- *
- * Permission to use, modify and distribute this software is granted
- * provided that this copyright notice appears in all copies. For
- * precise terms see the accompanying LICENSE file.
- *
- * This software is provided "AS IS" with no warranty of any kind,
- * express or implied, and with no claim as to its suitability for any
- * purpose.
- *
- */
-
-#ifndef LEMON_CONCEPT_MATRIX_MAPS_H
-#define LEMON_CONCEPT_MATRIX_MAPS_H
-
-#include <lemon/bits/utility.h>
-#include <lemon/concept_check.h>
-
-///\ingroup concept
-///\file
-///\brief MatrixMap concepts checking classes for testing and documenting.
-
-namespace lemon {
-
- namespace concepts {
-
- /// \addtogroup concept
- /// @{
-
- /// Readable matrix map concept
- template <typename K1, typename K2, typename V>
- class ReadMatrixMap
- {
- public:
- /// Map's first key type.
- typedef K1 FirstKey;
- /// Map's second key type.
- typedef K2 SecondKey;
- /// \brief Map's value type.
- /// (The type of objects associated with the pairs of keys).
- typedef V Value;
-
- // \bug Value don't need to be default constructible.
- /// Returns the value associated with a key.
- Value operator()(const FirstKey&, const SecondKey&) const {
- return Value();
- }
-
- template <typename _ReadMatrixMap>
- struct Constraints {
-
- void constraints() {
- Value val = m(first_key, second_key);
- val = m(first_key, second_key);
- typename _ReadMatrixMap::Value own_val =
- m(own_first_key, own_second_key);
- own_val = m(own_first_key, own_second_key);
- ignore_unused_variable_warning(val);
- ignore_unused_variable_warning(own_val);
- }
-
- FirstKey& first_key;
- SecondKey& second_key;
- typename _ReadMatrixMap::FirstKey& own_first_key;
- typename _ReadMatrixMap::SecondKey& own_second_key;
- _ReadMatrixMap& m;
- };
-
- };
-
-
- /// Writable map concept
- template <typename K1, typename K2, typename V>
- class WriteMatrixMap {
- public:
- /// Map's first key type.
- typedef K1 FirstKey;
- /// Map's second key type.
- typedef K2 SecondKey;
- /// \brief Map's value type.
- /// (The type of objects associated with the pairs of keys).
- typedef V Value;
-
- /// Sets the value associated with the pair of keys.
- void set(const FirstKey&, const SecondKey& ,const Value&) {}
-
- template <typename _WriteMatrixMap>
- struct Constraints {
- void constraints() {
- // No constraints for constructor.
- m.set(first_key, second_key, val);
- m.set(own_first_key, own_second_key, own_val);
- }
-
- Value& val;
- typename _WriteMatrixMap::Value own_val;
- FirstKey& first_key;
- SecondKey& second_key;
- typename _WriteMatrixMap::FirstKey& own_first_key;
- typename _WriteMatrixMap::SecondKey& own_second_key;
- _WriteMatrixMap& m;
-
- };
- };
-
- ///Read/Writable map concept
- template<typename K1, typename K2, typename V>
- class ReadWriteMatrixMap
- : public ReadMatrixMap<K1, K2, V>, public WriteMatrixMap<K1, K2, V> {
- public:
- /// Map's first key type.
- typedef K1 FirstKey;
- /// Map's second key type.
- typedef K2 SecondKey;
- /// \brief Map's value type.
- /// (The type of objects associated with the pairs of keys).
- typedef V Value;
-
- /// Returns the value associated with a pair of keys.
- Value operator()(const FirstKey&, const SecondKey&) const {
- return Value();
- }
- /// Sets the value associated with the pair of keys.
- void set(const FirstKey&, const SecondKey& ,const Value&) {}
-
- template<typename _ReadWriteMatrixMap>
- struct Constraints {
- void constraints() {
- checkConcept<ReadMatrixMap<K1, K2, V>, _ReadWriteMatrixMap >();
- checkConcept<WriteMatrixMap<K1, K2, V>, _ReadWriteMatrixMap >();
- }
- };
- };
-
-
- ///Dereferable matrix map concept
- template<typename K1, typename K2, typename V, typename R, typename CR>
- class ReferenceMatrixMap : public ReadWriteMatrixMap<K1, K2, V>
- {
- public:
- /// Tag for reference maps.
- typedef True ReferenceMapTag;
- /// Map's first key type.
- typedef K1 FirstKey;
- /// Map's second key type.
- typedef K1 SecondKey;
- /// Map's value type. (The type of objects associated with the keys).
- typedef V Value;
- /// Map's reference type.
- typedef R Reference;
- /// Map's const reference type.
- typedef CR ConstReference;
-
- protected:
- Value tmp;
- public:
-
- ///Returns a reference to the value associated to a pair of keys.
- Reference operator()(const FirstKey&, const SecondKey&) {
- return tmp;
- }
- ///Returns a const reference to the value associated to a pair of keys.
- ConstReference operator()(const FirstKey&, const SecondKey&) const {
- return tmp;
- }
- /// Sets the value associated with the pair of keys.
- void set(const FirstKey&, const SecondKey& ,const Value&) {}
-
- // \todo rethink this concept
- template<typename _ReferenceMatrixMap>
- struct ReferenceMapConcept {
-
- void constraints() {
- checkConcept<ReadWriteMatrixMap, _ReferenceMatrixMap >();
- m(first_key, second_key) = val;
- val = m(first_key, second_key);
- m(first_key, second_key) = ref;
- ref = m(first_key, second_key);
- m(own_first_key, own_second_key) = own_val;
- own_val = m(own_first_key, own_second_key);
- m(own_first_key, own_second_key) = own_ref;
- own_ref = m(own_first_key, own_second_key);
- }
-
- typename _ReferenceMatrixMap::Key& own_first_key;
- typename _ReferenceMatrixMap::Key& own_second_key;
- typename _ReferenceMatrixMap::Value& own_val;
- typename _ReferenceMatrixMap::Reference& own_ref;
- FirstKey& first_key;
- SecondKey& second_key;
- Value& val;
- Reference& ref;
- _ReferenceMatrixMap& m;
- };
- };
-
- // @}
-
- } //namespace concepts
-} //namespace lemon
-#endif // LEMON_CONCEPT_MATRIX_MAPS_H
diff --git a/src/lemon/concepts/path.h b/src/lemon/concepts/path.h
deleted file mode 100644
index d881d0a..0000000
--- a/src/lemon/concepts/path.h
+++ /dev/null
@@ -1,307 +0,0 @@
-/* -*- C++ -*-
- *
- * This file is a part of LEMON, a generic C++ optimization library
- *
- * Copyright (C) 2003-2008
- * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
- * (Egervary Research Group on Combinatorial Optimization, EGRES).
- *
- * Permission to use, modify and distribute this software is granted
- * provided that this copyright notice appears in all copies. For
- * precise terms see the accompanying LICENSE file.
- *
- * This software is provided "AS IS" with no warranty of any kind,
- * express or implied, and with no claim as to its suitability for any
- * purpose.
- *
- */
-
-///\ingroup concept
-///\file
-///\brief Classes for representing paths in graphs.
-///
-///\todo Iterators have obsolete style
-
-#ifndef LEMON_CONCEPT_PATH_H
-#define LEMON_CONCEPT_PATH_H
-
-#include <lemon/bits/invalid.h>
-#include <lemon/bits/utility.h>
-#include <lemon/concept_check.h>
-
-namespace lemon {
- namespace concepts {
-
- /// \addtogroup concept
- /// @{
-
- /// \brief A skeleton structure for representing directed paths in
- /// a graph.
- ///
- /// A skeleton structure for representing directed paths in a
- /// graph.
- /// \param _Graph The graph type in which the path is.
- ///
- /// In a sense, the path can be treated as a list of edges. The
- /// lemon path type stores just this list. As a consequence it
- /// cannot enumerate the nodes in the path and the zero length
- /// paths cannot store the source.
- ///
- template <typename _Graph>
- class Path {
- public:
-
- /// Type of the underlying graph.
- typedef _Graph Graph;
- /// Edge type of the underlying graph.
- typedef typename Graph::Edge Edge;
-
- class EdgeIt;
-
- /// \brief Default constructor
- Path() {}
-
- /// \brief Template constructor
- template <typename CPath>
- Path(const CPath& cpath) {}
-
- /// \brief Template assigment
- template <typename CPath>
- Path& operator=(const CPath& cpath) {}
-
- /// Length of the path ie. the number of edges in the path.
- int length() const { return 0;}
-
- /// Returns whether the path is empty.
- bool empty() const { return true;}
-
- /// Resets the path to an empty path.
- void clear() {}
-
- /// \brief Lemon style iterator for path edges
- ///
- /// This class is used to iterate on the edges of the paths.
- class EdgeIt {
- public:
- /// Default constructor
- EdgeIt() {}
- /// Invalid constructor
- EdgeIt(Invalid) {}
- /// Constructor for first edge
- EdgeIt(const Path &) {}
-
- /// Conversion to Edge
- operator Edge() const { return INVALID; }
-
- /// Next edge
- EdgeIt& operator++() {return *this;}
-
- /// Comparison operator
- bool operator==(const EdgeIt&) const {return true;}
- /// Comparison operator
- bool operator!=(const EdgeIt&) const {return true;}
- /// Comparison operator
- bool operator<(const EdgeIt&) const {return false;}
-
- };
-
- template <typename _Path>
- struct Constraints {
- void constraints() {
- Path<Graph> pc;
- _Path p, pp(pc);
- int l = p.length();
- int e = p.empty();
- p.clear();
-
- p = pc;
-
- typename _Path::EdgeIt id, ii(INVALID), i(p);
-
- ++i;
- typename Graph::Edge ed = i;
-
- e = (i == ii);
- e = (i != ii);
- e = (i < ii);
-
- ignore_unused_variable_warning(l);
- ignore_unused_variable_warning(pp);
- ignore_unused_variable_warning(e);
- ignore_unused_variable_warning(id);
- ignore_unused_variable_warning(ii);
- ignore_unused_variable_warning(ed);
- }
- };
-
- };
-
- namespace _path_bits {
-
- template <typename _Graph, typename _Path, typename RevPathTag = void>
- struct PathDumperConstraints {
- void constraints() {
- int l = p.length();
- int e = p.empty();
-
- typename _Path::EdgeIt id, i(p);
-
- ++i;
- typename _Graph::Edge ed = i;
-
- e = (i == INVALID);
- e = (i != INVALID);
-
- ignore_unused_variable_warning(l);
- ignore_unused_variable_warning(e);
- ignore_unused_variable_warning(id);
- ignore_unused_variable_warning(ed);
- }
- _Path& p;
- };
-
- template <typename _Graph, typename _Path>
- struct PathDumperConstraints<
- _Graph, _Path,
- typename enable_if<typename _Path::RevPathTag, void>::type
- > {
- void constraints() {
- int l = p.length();
- int e = p.empty();
-
- typename _Path::RevEdgeIt id, i(p);
-
- ++i;
- typename _Graph::Edge ed = i;
-
- e = (i == INVALID);
- e = (i != INVALID);
-
- ignore_unused_variable_warning(l);
- ignore_unused_variable_warning(e);
- ignore_unused_variable_warning(id);
- ignore_unused_variable_warning(ed);
- }
- _Path& p;
- };
-
- }
-
-
- /// \brief A skeleton structure for path dumpers.
- ///
- /// A skeleton structure for path dumpers. The path dumpers are
- /// the generalization of the paths. The path dumpers can
- /// enumerate the edges of the path wheter in forward or in
- /// backward order. In most time these classes are not used
- /// directly rather it used to assign a dumped class to a real
- /// path type.
- ///
- /// The main purpose of this concept is that the shortest path
- /// algorithms can enumerate easily the edges in reverse order.
- /// If we would like to give back a real path from these
- /// algorithms then we should create a temporarly path object. In
- /// Lemon such algorithms gives back a path dumper what can
- /// assigned to a real path and the dumpers can be implemented as
- /// an adaptor class to the predecessor map.
-
- /// \param _Graph The graph type in which the path is.
- ///
- /// The paths can be constructed from any path type by a
- /// template constructor or a template assignment operator.
- ///
- template <typename _Graph>
- class PathDumper {
- public:
-
- /// Type of the underlying graph.
- typedef _Graph Graph;
- /// Edge type of the underlying graph.
- typedef typename Graph::Edge Edge;
-
- /// Length of the path ie. the number of edges in the path.
- int length() const { return 0;}
-
- /// Returns whether the path is empty.
- bool empty() const { return true;}
-
- /// \brief Forward or reverse dumping
- ///
- /// If the RevPathTag is defined and true then reverse dumping
- /// is provided in the path dumper. In this case instead of the
- /// EdgeIt the RevEdgeIt iterator should be implemented in the
- /// dumper.
- typedef False RevPathTag;
-
- /// \brief Lemon style iterator for path edges
- ///
- /// This class is used to iterate on the edges of the paths.
- class EdgeIt {
- public:
- /// Default constructor
- EdgeIt() {}
- /// Invalid constructor
- EdgeIt(Invalid) {}
- /// Constructor for first edge
- EdgeIt(const PathDumper&) {}
-
- /// Conversion to Edge
- operator Edge() const { return INVALID; }
-
- /// Next edge
- EdgeIt& operator++() {return *this;}
-
- /// Comparison operator
- bool operator==(const EdgeIt&) const {return true;}
- /// Comparison operator
- bool operator!=(const EdgeIt&) const {return true;}
- /// Comparison operator
- bool operator<(const EdgeIt&) const {return false;}
-
- };
-
- /// \brief Lemon style iterator for path edges
- ///
- /// This class is used to iterate on the edges of the paths in
- /// reverse direction.
- class RevEdgeIt {
- public:
- /// Default constructor
- RevEdgeIt() {}
- /// Invalid constructor
- RevEdgeIt(Invalid) {}
- /// Constructor for first edge
- RevEdgeIt(const PathDumper &) {}
-
- /// Conversion to Edge
- operator Edge() const { return INVALID; }
-
- /// Next edge
- RevEdgeIt& operator++() {return *this;}
-
- /// Comparison operator
- bool operator==(const RevEdgeIt&) const {return true;}
- /// Comparison operator
- bool operator!=(const RevEdgeIt&) const {return true;}
- /// Comparison operator
- bool operator<(const RevEdgeIt&) const {return false;}
-
- };
-
- template <typename _Path>
- struct Constraints {
- void constraints() {
- function_requires<_path_bits::
- PathDumperConstraints<Graph, _Path> >();
- }
- };
-
- };
-
-
- ///@}
- }
-
-} // namespace lemon
-
-#endif // LEMON_CONCEPT_PATH_H
diff --git a/src/lemon/concepts/ugraph.h b/src/lemon/concepts/ugraph.h
deleted file mode 100644
index 7b0e8be..0000000
--- a/src/lemon/concepts/ugraph.h
+++ /dev/null
@@ -1,702 +0,0 @@
-/* -*- C++ -*-
- *
- * This file is a part of LEMON, a generic C++ optimization library
- *
- * Copyright (C) 2003-2008
- * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
- * (Egervary Research Group on Combinatorial Optimization, EGRES).
- *
- * Permission to use, modify and distribute this software is granted
- * provided that this copyright notice appears in all copies. For
- * precise terms see the accompanying LICENSE file.
- *
- * This software is provided "AS IS" with no warranty of any kind,
- * express or implied, and with no claim as to its suitability for any
- * purpose.
- *
- */
-
-///\ingroup graph_concepts
-///\file
-///\brief The concept of Undirected Graphs.
-
-#ifndef LEMON_CONCEPT_UGRAPH_H
-#define LEMON_CONCEPT_UGRAPH_H
-
-#include <lemon/concepts/graph_components.h>
-#include <lemon/concepts/graph.h>
-#include <lemon/bits/utility.h>
-
-namespace lemon {
- namespace concepts {
-
- /// \ingroup graph_concepts
- ///
- /// \brief Class describing the concept of Undirected Graphs.
- ///
- /// This class describes the common interface of all Undirected
- /// Graphs.
- ///
- /// As all concept describing classes it provides only interface
- /// without any sensible implementation. So any algorithm for
- /// undirected graph should compile with this class, but it will not
- /// run properly, of course.
- ///
- /// The LEMON undirected graphs also fulfill the concept of
- /// directed graphs (\ref lemon::concepts::Graph "Graph
- /// Concept"). Each undirected edges can be seen as two opposite
- /// directed edge and consequently the undirected graph can be
- /// seen as the direceted graph of these directed edges. The
- /// UGraph has the UEdge inner class for the undirected edges and
- /// the Edge type for the directed edges. The Edge type is
- /// convertible to UEdge or inherited from it so from a directed
- /// edge we can get the represented undirected edge.
- ///
- /// In the sense of the LEMON each undirected edge has a default
- /// direction (it should be in every computer implementation,
- /// because the order of undirected edge's nodes defines an
- /// orientation). With the default orientation we can define that
- /// the directed edge is forward or backward directed. With the \c
- /// direction() and \c direct() function we can get the direction
- /// of the directed edge and we can direct an undirected edge.
- ///
- /// The UEdgeIt is an iterator for the undirected edges. We can use
- /// the UEdgeMap to map values for the undirected edges. The InEdgeIt and
- /// OutEdgeIt iterates on the same undirected edges but with opposite
- /// direction. The IncEdgeIt iterates also on the same undirected edges
- /// as the OutEdgeIt and InEdgeIt but it is not convertible to Edge just
- /// to UEdge.
- class UGraph {
- public:
- /// \brief The undirected graph should be tagged by the
- /// UndirectedTag.
- ///
- /// The undirected graph should be tagged by the UndirectedTag. This
- /// tag helps the enable_if technics to make compile time
- /// specializations for undirected graphs.
- typedef True UndirectedTag;
-
- /// \brief The base type of node iterators,
- /// or in other words, the trivial node iterator.
- ///
- /// This is the base type of each node iterator,
- /// thus each kind of node iterator converts to this.
- /// More precisely each kind of node iterator should be inherited
- /// from the trivial node iterator.
- class Node {
- public:
- /// Default constructor
-
- /// @warning The default constructor sets the iterator
- /// to an undefined value.
- Node() { }
- /// Copy constructor.
-
- /// Copy constructor.
- ///
- Node(const Node&) { }
-
- /// Invalid constructor \& conversion.
-
- /// This constructor initializes the iterator to be invalid.
- /// \sa Invalid for more details.
- Node(Invalid) { }
- /// Equality operator
-
- /// Two iterators are equal if and only if they point to the
- /// same object or both are invalid.
- bool operator==(Node) const { return true; }
-
- /// Inequality operator
-
- /// \sa operator==(Node n)
- ///
- bool operator!=(Node) const { return true; }
-
- /// Artificial ordering operator.
-
- /// To allow the use of graph descriptors as key type in std::map or
- /// similar associative container we require this.
- ///
- /// \note This operator only have to define some strict ordering of
- /// the items; this order has nothing to do with the iteration
- /// ordering of the items.
- bool operator<(Node) const { return false; }
-
- };
-
- /// This iterator goes through each node.
-
- /// This iterator goes through each node.
- /// Its usage is quite simple, for example you can count the number
- /// of nodes in graph \c g of type \c Graph like this:
- ///\code
- /// int count=0;
- /// for (Graph::NodeIt n(g); n!=INVALID; ++n) ++count;
- ///\endcode
- class NodeIt : public Node {
- public:
- /// Default constructor
-
- /// @warning The default constructor sets the iterator
- /// to an undefined value.
- NodeIt() { }
- /// Copy constructor.
-
- /// Copy constructor.
- ///
- NodeIt(const NodeIt& n) : Node(n) { }
- /// Invalid constructor \& conversion.
-
- /// Initialize the iterator to be invalid.
- /// \sa Invalid for more details.
- NodeIt(Invalid) { }
- /// Sets the iterator to the first node.
-
- /// Sets the iterator to the first node of \c g.
- ///
- NodeIt(const UGraph&) { }
- /// Node -> NodeIt conversion.
-
- /// Sets the iterator to the node of \c the graph pointed by
- /// the trivial iterator.
- /// This feature necessitates that each time we
- /// iterate the edge-set, the iteration order is the same.
- NodeIt(const UGraph&, const Node&) { }
- /// Next node.
-
- /// Assign the iterator to the next node.
- ///
- NodeIt& operator++() { return *this; }
- };
-
-
- /// The base type of the undirected edge iterators.
-
- /// The base type of the undirected edge iterators.
- ///
- class UEdge {
- public:
- /// Default constructor
-
- /// @warning The default constructor sets the iterator
- /// to an undefined value.
- UEdge() { }
- /// Copy constructor.
-
- /// Copy constructor.
- ///
- UEdge(const UEdge&) { }
- /// Initialize the iterator to be invalid.
-
- /// Initialize the iterator to be invalid.
- ///
- UEdge(Invalid) { }
- /// Equality operator
-
- /// Two iterators are equal if and only if they point to the
- /// same object or both are invalid.
- bool operator==(UEdge) const { return true; }
- /// Inequality operator
-
- /// \sa operator==(UEdge n)
- ///
- bool operator!=(UEdge) const { return true; }
-
- /// Artificial ordering operator.
-
- /// To allow the use of graph descriptors as key type in std::map or
- /// similar associative container we require this.
- ///
- /// \note This operator only have to define some strict ordering of
- /// the items; this order has nothing to do with the iteration
- /// ordering of the items.
- bool operator<(UEdge) const { return false; }
- };
-
- /// This iterator goes through each undirected edge.
-
- /// This iterator goes through each undirected edge of a graph.
- /// Its usage is quite simple, for example you can count the number
- /// of undirected edges in a graph \c g of type \c Graph as follows:
- ///\code
- /// int count=0;
- /// for(Graph::UEdgeIt e(g); e!=INVALID; ++e) ++count;
- ///\endcode
- class UEdgeIt : public UEdge {
- public:
- /// Default constructor
-
- /// @warning The default constructor sets the iterator
- /// to an undefined value.
- UEdgeIt() { }
- /// Copy constructor.
-
- /// Copy constructor.
- ///
- UEdgeIt(const UEdgeIt& e) : UEdge(e) { }
- /// Initialize the iterator to be invalid.
-
- /// Initialize the iterator to be invalid.
- ///
- UEdgeIt(Invalid) { }
- /// This constructor sets the iterator to the first undirected edge.
-
- /// This constructor sets the iterator to the first undirected edge.
- UEdgeIt(const UGraph&) { }
- /// UEdge -> UEdgeIt conversion
-
- /// Sets the iterator to the value of the trivial iterator.
- /// This feature necessitates that each time we
- /// iterate the undirected edge-set, the iteration order is the
- /// same.
- UEdgeIt(const UGraph&, const UEdge&) { }
- /// Next undirected edge
-
- /// Assign the iterator to the next undirected edge.
- UEdgeIt& operator++() { return *this; }
- };
-
- /// \brief This iterator goes trough the incident undirected
- /// edges of a node.
- ///
- /// This iterator goes trough the incident undirected edges
- /// of a certain node of a graph. You should assume that the
- /// loop edges will be iterated twice.
- ///
- /// Its usage is quite simple, for example you can compute the
- /// degree (i.e. count the number of incident edges of a node \c n
- /// in graph \c g of type \c Graph as follows.
- ///
- ///\code
- /// int count=0;
- /// for(Graph::IncEdgeIt e(g, n); e!=INVALID; ++e) ++count;
- ///\endcode
- class IncEdgeIt : public UEdge {
- public:
- /// Default constructor
-
- /// @warning The default constructor sets the iterator
- /// to an undefined value.
- IncEdgeIt() { }
- /// Copy constructor.
-
- /// Copy constructor.
- ///
- IncEdgeIt(const IncEdgeIt& e) : UEdge(e) { }
- /// Initialize the iterator to be invalid.
-
- /// Initialize the iterator to be invalid.
- ///
- IncEdgeIt(Invalid) { }
- /// This constructor sets the iterator to first incident edge.
-
- /// This constructor set the iterator to the first incident edge of
- /// the node.
- IncEdgeIt(const UGraph&, const Node&) { }
- /// UEdge -> IncEdgeIt conversion
-
- /// Sets the iterator to the value of the trivial iterator \c e.
- /// This feature necessitates that each time we
- /// iterate the edge-set, the iteration order is the same.
- IncEdgeIt(const UGraph&, const UEdge&) { }
- /// Next incident edge
-
- /// Assign the iterator to the next incident edge
- /// of the corresponding node.
- IncEdgeIt& operator++() { return *this; }
- };
-
- /// The directed edge type.
-
- /// The directed edge type. It can be converted to the
- /// undirected edge or it should be inherited from the undirected
- /// edge.
- class Edge : public UEdge {
- public:
- /// Default constructor
-
- /// @warning The default constructor sets the iterator
- /// to an undefined value.
- Edge() { }
- /// Copy constructor.
-
- /// Copy constructor.
- ///
- Edge(const Edge& e) : UEdge(e) { }
- /// Initialize the iterator to be invalid.
-
- /// Initialize the iterator to be invalid.
- ///
- Edge(Invalid) { }
- /// Equality operator
-
- /// Two iterators are equal if and only if they point to the
- /// same object or both are invalid.
- bool operator==(Edge) const { return true; }
- /// Inequality operator
-
- /// \sa operator==(Edge n)
- ///
- bool operator!=(Edge) const { return true; }
-
- /// Artificial ordering operator.
-
- /// To allow the use of graph descriptors as key type in std::map or
- /// similar associative container we require this.
- ///
- /// \note This operator only have to define some strict ordering of
- /// the items; this order has nothing to do with the iteration
- /// ordering of the items.
- bool operator<(Edge) const { return false; }
-
- };
- /// This iterator goes through each directed edge.
-
- /// This iterator goes through each edge of a graph.
- /// Its usage is quite simple, for example you can count the number
- /// of edges in a graph \c g of type \c Graph as follows:
- ///\code
- /// int count=0;
- /// for(Graph::EdgeIt e(g); e!=INVALID; ++e) ++count;
- ///\endcode
- class EdgeIt : public Edge {
- public:
- /// Default constructor
-
- /// @warning The default constructor sets the iterator
- /// to an undefined value.
- EdgeIt() { }
- /// Copy constructor.
-
- /// Copy constructor.
- ///
- EdgeIt(const EdgeIt& e) : Edge(e) { }
- /// Initialize the iterator to be invalid.
-
- /// Initialize the iterator to be invalid.
- ///
- EdgeIt(Invalid) { }
- /// This constructor sets the iterator to the first edge.
-
- /// This constructor sets the iterator to the first edge of \c g.
- ///@param g the graph
- EdgeIt(const UGraph &g) { ignore_unused_variable_warning(g); }
- /// Edge -> EdgeIt conversion
-
- /// Sets the iterator to the value of the trivial iterator \c e.
- /// This feature necessitates that each time we
- /// iterate the edge-set, the iteration order is the same.
- EdgeIt(const UGraph&, const Edge&) { }
- ///Next edge
-
- /// Assign the iterator to the next edge.
- EdgeIt& operator++() { return *this; }
- };
-
- /// This iterator goes trough the outgoing directed edges of a node.
-
- /// This iterator goes trough the \e outgoing edges of a certain node
- /// of a graph.
- /// Its usage is quite simple, for example you can count the number
- /// of outgoing edges of a node \c n
- /// in graph \c g of type \c Graph as follows.
- ///\code
- /// int count=0;
- /// for (Graph::OutEdgeIt e(g, n); e!=INVALID; ++e) ++count;
- ///\endcode
-
- class OutEdgeIt : public Edge {
- public:
- /// Default constructor
-
- /// @warning The default constructor sets the iterator
- /// to an undefined value.
- OutEdgeIt() { }
- /// Copy constructor.
-
- /// Copy constructor.
- ///
- OutEdgeIt(const OutEdgeIt& e) : Edge(e) { }
- /// Initialize the iterator to be invalid.
-
- /// Initialize the iterator to be invalid.
- ///
- OutEdgeIt(Invalid) { }
- /// This constructor sets the iterator to the first outgoing edge.
-
- /// This constructor sets the iterator to the first outgoing edge of
- /// the node.
- ///@param n the node
- ///@param g the graph
- OutEdgeIt(const UGraph& n, const Node& g) {
- ignore_unused_variable_warning(n);
- ignore_unused_variable_warning(g);
- }
- /// Edge -> OutEdgeIt conversion
-
- /// Sets the iterator to the value of the trivial iterator.
- /// This feature necessitates that each time we
- /// iterate the edge-set, the iteration order is the same.
- OutEdgeIt(const UGraph&, const Edge&) { }
- ///Next outgoing edge
-
- /// Assign the iterator to the next
- /// outgoing edge of the corresponding node.
- OutEdgeIt& operator++() { return *this; }
- };
-
- /// This iterator goes trough the incoming directed edges of a node.
-
- /// This iterator goes trough the \e incoming edges of a certain node
- /// of a graph.
- /// Its usage is quite simple, for example you can count the number
- /// of outgoing edges of a node \c n
- /// in graph \c g of type \c Graph as follows.
- ///\code
- /// int count=0;
- /// for(Graph::InEdgeIt e(g, n); e!=INVALID; ++e) ++count;
- ///\endcode
-
- class InEdgeIt : public Edge {
- public:
- /// Default constructor
-
- /// @warning The default constructor sets the iterator
- /// to an undefined value.
- InEdgeIt() { }
- /// Copy constructor.
-
- /// Copy constructor.
- ///
- InEdgeIt(const InEdgeIt& e) : Edge(e) { }
- /// Initialize the iterator to be invalid.
-
- /// Initialize the iterator to be invalid.
- ///
- InEdgeIt(Invalid) { }
- /// This constructor sets the iterator to first incoming edge.
-
- /// This constructor set the iterator to the first incoming edge of
- /// the node.
- ///@param n the node
- ///@param g the graph
- InEdgeIt(const UGraph& g, const Node& n) {
- ignore_unused_variable_warning(n);
- ignore_unused_variable_warning(g);
- }
- /// Edge -> InEdgeIt conversion
-
- /// Sets the iterator to the value of the trivial iterator \c e.
- /// This feature necessitates that each time we
- /// iterate the edge-set, the iteration order is the same.
- InEdgeIt(const UGraph&, const Edge&) { }
- /// Next incoming edge
-
- /// Assign the iterator to the next inedge of the corresponding node.
- ///
- InEdgeIt& operator++() { return *this; }
- };
-
- /// \brief Read write map of the nodes to type \c T.
- ///
- /// ReadWrite map of the nodes to type \c T.
- /// \sa Reference
- template<class T>
- class NodeMap : public ReadWriteMap< Node, T >
- {
- public:
-
- ///\e
- NodeMap(const UGraph&) { }
- ///\e
- NodeMap(const UGraph&, T) { }
-
- ///Copy constructor
- NodeMap(const NodeMap& nm) : ReadWriteMap< Node, T >(nm) { }
- ///Assignment operator
- template <typename CMap>
- NodeMap& operator=(const CMap&) {
- checkConcept<ReadMap<Node, T>, CMap>();
- return *this;
- }
- };
-
- /// \brief Read write map of the directed edges to type \c T.
- ///
- /// Reference map of the directed edges to type \c T.
- /// \sa Reference
- template<class T>
- class EdgeMap : public ReadWriteMap<Edge,T>
- {
- public:
-
- ///\e
- EdgeMap(const UGraph&) { }
- ///\e
- EdgeMap(const UGraph&, T) { }
- ///Copy constructor
- EdgeMap(const EdgeMap& em) : ReadWriteMap<Edge,T>(em) { }
- ///Assignment operator
- template <typename CMap>
- EdgeMap& operator=(const CMap&) {
- checkConcept<ReadMap<Edge, T>, CMap>();
- return *this;
- }
- };
-
- /// Read write map of the undirected edges to type \c T.
-
- /// Reference map of the edges to type \c T.
- /// \sa Reference
- template<class T>
- class UEdgeMap : public ReadWriteMap<UEdge,T>
- {
- public:
-
- ///\e
- UEdgeMap(const UGraph&) { }
- ///\e
- UEdgeMap(const UGraph&, T) { }
- ///Copy constructor
- UEdgeMap(const UEdgeMap& em) : ReadWriteMap<UEdge,T>(em) {}
- ///Assignment operator
- template <typename CMap>
- UEdgeMap& operator=(const CMap&) {
- checkConcept<ReadMap<UEdge, T>, CMap>();
- return *this;
- }
- };
-
- /// \brief Direct the given undirected edge.
- ///
- /// Direct the given undirected edge. The returned edge source
- /// will be the given node.
- Edge direct(const UEdge&, const Node&) const {
- return INVALID;
- }
-
- /// \brief Direct the given undirected edge.
- ///
- /// Direct the given undirected edge. The returned edge
- /// represents the given undirected edge and the direction comes
- /// from the given bool. The source of the undirected edge and
- /// the directed edge is the same when the given bool is true.
- Edge direct(const UEdge&, bool) const {
- return INVALID;
- }
-
- /// \brief Returns true if the edge has default orientation.
- ///
- /// Returns whether the given directed edge is same orientation as
- /// the corresponding undirected edge's default orientation.
- bool direction(Edge) const { return true; }
-
- /// \brief Returns the opposite directed edge.
- ///
- /// Returns the opposite directed edge.
- Edge oppositeEdge(Edge) const { return INVALID; }
-
- /// \brief Opposite node on an edge
- ///
- /// \return the opposite of the given Node on the given UEdge
- Node oppositeNode(Node, UEdge) const { return INVALID; }
-
- /// \brief First node of the undirected edge.
- ///
- /// \return the first node of the given UEdge.
- ///
- /// Naturally undirected edges don't have direction and thus
- /// don't have source and target node. But we use these two methods
- /// to query the two nodes of the edge. The direction of the edge
- /// which arises this way is called the inherent direction of the
- /// undirected edge, and is used to define the "default" direction
- /// of the directed versions of the edges.
- /// \sa direction
- Node source(UEdge) const { return INVALID; }
-
- /// \brief Second node of the undirected edge.
- Node target(UEdge) const { return INVALID; }
-
- /// \brief Source node of the directed edge.
- Node source(Edge) const { return INVALID; }
-
- /// \brief Target node of the directed edge.
- Node target(Edge) const { return INVALID; }
-
- void first(Node&) const {}
- void next(Node&) const {}
-
- void first(UEdge&) const {}
- void next(UEdge&) const {}
-
- void first(Edge&) const {}
- void next(Edge&) const {}
-
- void firstOut(Edge&, Node) const {}
- void nextOut(Edge&) const {}
-
- void firstIn(Edge&, Node) const {}
- void nextIn(Edge&) const {}
-
-
- void firstInc(UEdge &, bool &, const Node &) const {}
- void nextInc(UEdge &, bool &) const {}
-
- /// \brief Base node of the iterator
- ///
- /// Returns the base node (the source in this case) of the iterator
- Node baseNode(OutEdgeIt e) const {
- return source(e);
- }
- /// \brief Running node of the iterator
- ///
- /// Returns the running node (the target in this case) of the
- /// iterator
- Node runningNode(OutEdgeIt e) const {
- return target(e);
- }
-
- /// \brief Base node of the iterator
- ///
- /// Returns the base node (the target in this case) of the iterator
- Node baseNode(InEdgeIt e) const {
- return target(e);
- }
- /// \brief Running node of the iterator
- ///
- /// Returns the running node (the source in this case) of the
- /// iterator
- Node runningNode(InEdgeIt e) const {
- return source(e);
- }
-
- /// \brief Base node of the iterator
- ///
- /// Returns the base node of the iterator
- Node baseNode(IncEdgeIt) const {
- return INVALID;
- }
-
- /// \brief Running node of the iterator
- ///
- /// Returns the running node of the iterator
- Node runningNode(IncEdgeIt) const {
- return INVALID;
- }
-
- template <typename Graph>
- struct Constraints {
- void constraints() {
- checkConcept<IterableUGraphComponent<>, Graph>();
- checkConcept<MappableUGraphComponent<>, Graph>();
- }
- };
-
- };
-
- }
-
-}
-
-#endif
diff --git a/src/lemon/dfs.h b/src/lemon/dfs.h
deleted file mode 100644
index a003b69..0000000
--- a/src/lemon/dfs.h
+++ /dev/null
@@ -1,1543 +0,0 @@
-/* -*- C++ -*-
- *
- * This file is a part of LEMON, a generic C++ optimization library
- *
- * Copyright (C) 2003-2008
- * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
- * (Egervary Research Group on Combinatorial Optimization, EGRES).
- *
- * Permission to use, modify and distribute this software is granted
- * provided that this copyright notice appears in all copies. For
- * precise terms see the accompanying LICENSE file.
- *
- * This software is provided "AS IS" with no warranty of any kind,
- * express or implied, and with no claim as to its suitability for any
- * purpose.
- *
- */
-
-#ifndef LEMON_DFS_H
-#define LEMON_DFS_H
-
-///\ingroup search
-///\file
-///\brief Dfs algorithm.
-
-#include <lemon/list_graph.h>
-#include <lemon/graph_utils.h>
-#include <lemon/bits/path_dump.h>
-#include <lemon/bits/invalid.h>
-#include <lemon/error.h>
-#include <lemon/maps.h>
-
-#include <lemon/concept_check.h>
-
-namespace lemon {
-
-
- ///Default traits class of Dfs class.
-
- ///Default traits class of Dfs class.
- ///\param GR Graph type.
- template<class GR>
- struct DfsDefaultTraits
- {
- ///The graph type the algorithm runs on.
- typedef GR Graph;
- ///\brief The type of the map that stores the last
- ///edges of the %DFS paths.
- ///
- ///The type of the map that stores the last
- ///edges of the %DFS paths.
- ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
- ///
- typedef typename Graph::template NodeMap<typename GR::Edge> PredMap;
- ///Instantiates a PredMap.
-
- ///This function instantiates a \ref PredMap.
- ///\param G is the graph, to which we would like to define the PredMap.
- ///\todo The graph alone may be insufficient to initialize
- static PredMap *createPredMap(const GR &G)
- {
- return new PredMap(G);
- }
-
- ///The type of the map that indicates which nodes are processed.
-
- ///The type of the map that indicates which nodes are processed.
- ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
- ///\todo named parameter to set this type, function to read and write.
- typedef NullMap<typename Graph::Node,bool> ProcessedMap;
- ///Instantiates a ProcessedMap.
-
- ///This function instantiates a \ref ProcessedMap.
- ///\param g is the graph, to which
- ///we would like to define the \ref ProcessedMap
-#ifdef DOXYGEN
- static ProcessedMap *createProcessedMap(const GR &g)
-#else
- static ProcessedMap *createProcessedMap(const GR &)
-#endif
- {
- return new ProcessedMap();
- }
- ///The type of the map that indicates which nodes are reached.
-
- ///The type of the map that indicates which nodes are reached.
- ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
- ///\todo named parameter to set this type, function to read and write.
- typedef typename Graph::template NodeMap<bool> ReachedMap;
- ///Instantiates a ReachedMap.
-
- ///This function instantiates a \ref ReachedMap.
- ///\param G is the graph, to which
- ///we would like to define the \ref ReachedMap.
- static ReachedMap *createReachedMap(const GR &G)
- {
- return new ReachedMap(G);
- }
- ///The type of the map that stores the dists of the nodes.
-
- ///The type of the map that stores the dists of the nodes.
- ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
- ///
- typedef typename Graph::template NodeMap<int> DistMap;
- ///Instantiates a DistMap.
-
- ///This function instantiates a \ref DistMap.
- ///\param G is the graph, to which we would like to define the \ref DistMap
- static DistMap *createDistMap(const GR &G)
- {
- return new DistMap(G);
- }
- };
-
- ///%DFS algorithm class.
-
- ///\ingroup search
- ///This class provides an efficient implementation of the %DFS algorithm.
- ///
- ///\param GR The graph type the algorithm runs on. The default value is
- ///\ref ListGraph. The value of GR is not used directly by Dfs, it
- ///is only passed to \ref DfsDefaultTraits.
- ///\param TR Traits class to set various data types used by the algorithm.
- ///The default traits class is
- ///\ref DfsDefaultTraits "DfsDefaultTraits<GR>".
- ///See \ref DfsDefaultTraits for the documentation of
- ///a Dfs traits class.
- ///
- ///\author Jacint Szabo and Alpar Juttner
-#ifdef DOXYGEN
- template <typename GR,
- typename TR>
-#else
- template <typename GR=ListGraph,
- typename TR=DfsDefaultTraits<GR> >
-#endif
- class Dfs {
- public:
- /**
- * \brief \ref Exception for uninitialized parameters.
- *
- * This error represents problems in the initialization
- * of the parameters of the algorithms.
- */
- class UninitializedParameter : public lemon::UninitializedParameter {
- public:
- virtual const char* what() const throw() {
- return "lemon::Dfs::UninitializedParameter";
- }
- };
-
- typedef TR Traits;
- ///The type of the underlying graph.
- typedef typename TR::Graph Graph;
- ///\e
- typedef typename Graph::Node Node;
- ///\e
- typedef typename Graph::NodeIt NodeIt;
- ///\e
- typedef typename Graph::Edge Edge;
- ///\e
- typedef typename Graph::OutEdgeIt OutEdgeIt;
-
- ///\brief The type of the map that stores the last
- ///edges of the %DFS paths.
- typedef typename TR::PredMap PredMap;
- ///The type of the map indicating which nodes are reached.
- typedef typename TR::ReachedMap ReachedMap;
- ///The type of the map indicating which nodes are processed.
- typedef typename TR::ProcessedMap ProcessedMap;
- ///The type of the map that stores the dists of the nodes.
- typedef typename TR::DistMap DistMap;
- private:
- /// Pointer to the underlying graph.
- const Graph *G;
- ///Pointer to the map of predecessors edges.
- PredMap *_pred;
- ///Indicates if \ref _pred is locally allocated (\c true) or not.
- bool local_pred;
- ///Pointer to the map of distances.
- DistMap *_dist;
- ///Indicates if \ref _dist is locally allocated (\c true) or not.
- bool local_dist;
- ///Pointer to the map of reached status of the nodes.
- ReachedMap *_reached;
- ///Indicates if \ref _reached is locally allocated (\c true) or not.
- bool local_reached;
- ///Pointer to the map of processed status of the nodes.
- ProcessedMap *_processed;
- ///Indicates if \ref _processed is locally allocated (\c true) or not.
- bool local_processed;
-
- std::vector<typename Graph::OutEdgeIt> _stack;
- int _stack_head;
-
- ///Creates the maps if necessary.
-
- ///\todo Better memory allocation (instead of new).
- void create_maps()
- {
- if(!_pred) {
- local_pred = true;
- _pred = Traits::createPredMap(*G);
- }
- if(!_dist) {
- local_dist = true;
- _dist = Traits::createDistMap(*G);
- }
- if(!_reached) {
- local_reached = true;
- _reached = Traits::createReachedMap(*G);
- }
- if(!_processed) {
- local_processed = true;
- _processed = Traits::createProcessedMap(*G);
- }
- }
-
- protected:
-
- Dfs() {}
-
- public:
-
- typedef Dfs Create;
-
- ///\name Named template parameters
-
- ///@{
-
- template <class T>
- struct DefPredMapTraits : public Traits {
- typedef T PredMap;
- static PredMap *createPredMap(const Graph &G)
- {
- throw UninitializedParameter();
- }
- };
- ///\brief \ref named-templ-param "Named parameter" for setting
- ///PredMap type
- ///
- ///\ref named-templ-param "Named parameter" for setting PredMap type
- ///
- template <class T>
- struct DefPredMap : public Dfs<Graph, DefPredMapTraits<T> > {
- typedef Dfs<Graph, DefPredMapTraits<T> > Create;
- };
-
-
- template <class T>
- struct DefDistMapTraits : public Traits {
- typedef T DistMap;
- static DistMap *createDistMap(const Graph &)
- {
- throw UninitializedParameter();
- }
- };
- ///\brief \ref named-templ-param "Named parameter" for setting
- ///DistMap type
- ///
- ///\ref named-templ-param "Named parameter" for setting DistMap
- ///type
- template <class T>
- struct DefDistMap {
- typedef Dfs<Graph, DefDistMapTraits<T> > Create;
- };
-
- template <class T>
- struct DefReachedMapTraits : public Traits {
- typedef T ReachedMap;
- static ReachedMap *createReachedMap(const Graph &)
- {
- throw UninitializedParameter();
- }
- };
- ///\brief \ref named-templ-param "Named parameter" for setting
- ///ReachedMap type
- ///
- ///\ref named-templ-param "Named parameter" for setting ReachedMap type
- ///
- template <class T>
- struct DefReachedMap : public Dfs< Graph, DefReachedMapTraits<T> > {
- typedef Dfs< Graph, DefReachedMapTraits<T> > Create;
- };
-
- template <class T>
- struct DefProcessedMapTraits : public Traits {
- typedef T ProcessedMap;
- static ProcessedMap *createProcessedMap(const Graph &)
- {
- throw UninitializedParameter();
- }
- };
- ///\brief \ref named-templ-param "Named parameter" for setting
- ///ProcessedMap type
- ///
- ///\ref named-templ-param "Named parameter" for setting ProcessedMap type
- ///
- template <class T>
- struct DefProcessedMap : public Dfs< Graph, DefProcessedMapTraits<T> > {
- typedef Dfs< Graph, DefProcessedMapTraits<T> > Create;
- };
-
- struct DefGraphProcessedMapTraits : public Traits {
- typedef typename Graph::template NodeMap<bool> ProcessedMap;
- static ProcessedMap *createProcessedMap(const Graph &G)
- {
- return new ProcessedMap(G);
- }
- };
- ///\brief \ref named-templ-param "Named parameter"
- ///for setting the ProcessedMap type to be Graph::NodeMap<bool>.
- ///
- ///\ref named-templ-param "Named parameter"
- ///for setting the ProcessedMap type to be Graph::NodeMap<bool>.
- ///If you don't set it explicitely, it will be automatically allocated.
- template <class T>
- class DefProcessedMapToBeDefaultMap :
- public Dfs< Graph, DefGraphProcessedMapTraits> {
- typedef Dfs< Graph, DefGraphProcessedMapTraits> Create;
- };
-
- ///@}
-
- public:
-
- ///Constructor.
-
- ///\param _G the graph the algorithm will run on.
- ///
- Dfs(const Graph& _G) :
- G(&_G),
- _pred(NULL), local_pred(false),
- _dist(NULL), local_dist(false),
- _reached(NULL), local_reached(false),
- _processed(NULL), local_processed(false)
- { }
-
- ///Destructor.
- ~Dfs()
- {
- if(local_pred) delete _pred;
- if(local_dist) delete _dist;
- if(local_reached) delete _reached;
- if(local_processed) delete _processed;
- }
-
- ///Sets the map storing the predecessor edges.
-
- ///Sets the map storing the predecessor edges.
- ///If you don't use this function before calling \ref run(),
- ///it will allocate one. The destuctor deallocates this
- ///automatically allocated map, of course.
- ///\return <tt> (*this) </tt>
- Dfs &predMap(PredMap &m)
- {
- if(local_pred) {
- delete _pred;
- local_pred=false;
- }
- _pred = &m;
- return *this;
- }
-
- ///Sets the map storing the distances calculated by the algorithm.
-
- ///Sets the map storing the distances calculated by the algorithm.
- ///If you don't use this function before calling \ref run(),
- ///it will allocate one. The destuctor deallocates this
- ///automatically allocated map, of course.
- ///\return <tt> (*this) </tt>
- Dfs &distMap(DistMap &m)
- {
- if(local_dist) {
- delete _dist;
- local_dist=false;
- }
- _dist = &m;
- return *this;
- }
-
- ///Sets the map indicating if a node is reached.
-
- ///Sets the map indicating if a node is reached.
- ///If you don't use this function before calling \ref run(),
- ///it will allocate one. The destuctor deallocates this
- ///automatically allocated map, of course.
- ///\return <tt> (*this) </tt>
- Dfs &reachedMap(ReachedMap &m)
- {
- if(local_reached) {
- delete _reached;
- local_reached=false;
- }
- _reached = &m;
- return *this;
- }
-
- ///Sets the map indicating if a node is processed.
-
- ///Sets the map indicating if a node is processed.
- ///If you don't use this function before calling \ref run(),
- ///it will allocate one. The destuctor deallocates this
- ///automatically allocated map, of course.
- ///\return <tt> (*this) </tt>
- Dfs &processedMap(ProcessedMap &m)
- {
- if(local_processed) {
- delete _processed;
- local_processed=false;
- }
- _processed = &m;
- return *this;
- }
-
- public:
- ///\name Execution control
- ///The simplest way to execute the algorithm is to use
- ///one of the member functions called \c run(...).
- ///\n
- ///If you need more control on the execution,
- ///first you must call \ref init(), then you can add a source node
- ///with \ref addSource().
- ///Finally \ref start() will perform the actual path
- ///computation.
-
- ///@{
-
- ///Initializes the internal data structures.
-
- ///Initializes the internal data structures.
- ///
- void init()
- {
- create_maps();
- _stack.resize(countNodes(*G));
- _stack_head=-1;
- for ( NodeIt u(*G) ; u!=INVALID ; ++u ) {
- _pred->set(u,INVALID);
- // _predNode->set(u,INVALID);
- _reached->set(u,false);
- _processed->set(u,false);
- }
- }
-
- ///Adds a new source node.
-
- ///Adds a new source node to the set of nodes to be processed.
- ///
- ///\warning dists are wrong (or at least strange)
- ///in case of multiple sources.
- void addSource(Node s)
- {
- if(!(*_reached)[s])
- {
- _reached->set(s,true);
- _pred->set(s,INVALID);
- OutEdgeIt e(*G,s);
- if(e!=INVALID) {
- _stack[++_stack_head]=e;
- _dist->set(s,_stack_head);
- }
- else {
- _processed->set(s,true);
- _dist->set(s,0);
- }
- }
- }
-
- ///Processes the next edge.
-
- ///Processes the next edge.
- ///
- ///\return The processed edge.
- ///
- ///\pre The stack must not be empty!
- Edge processNextEdge()
- {
- Node m;
- Edge e=_stack[_stack_head];
- if(!(*_reached)[m=G->target(e)]) {
- _pred->set(m,e);
- _reached->set(m,true);
- ++_stack_head;
- _stack[_stack_head] = OutEdgeIt(*G, m);
- _dist->set(m,_stack_head);
- }
- else {
- m=G->source(e);
- ++_stack[_stack_head];
- }
- while(_stack_head>=0 && _stack[_stack_head]==INVALID) {
- _processed->set(m,true);
- --_stack_head;
- if(_stack_head>=0) {
- m=G->source(_stack[_stack_head]);
- ++_stack[_stack_head];
- }
- }
- return e;
- }
- ///Next edge to be processed.
-
- ///Next edge to be processed.
- ///
- ///\return The next edge to be processed or INVALID if the stack is
- /// empty.
- OutEdgeIt nextEdge()
- {
- return _stack_head>=0?_stack[_stack_head]:INVALID;
- }
-
- ///\brief Returns \c false if there are nodes
- ///to be processed in the queue
- ///
- ///Returns \c false if there are nodes
- ///to be processed in the queue
- bool emptyQueue() { return _stack_head<0; }
- ///Returns the number of the nodes to be processed.
-
- ///Returns the number of the nodes to be processed in the queue.
- int queueSize() { return _stack_head+1; }
-
- ///Executes the algorithm.
-
- ///Executes the algorithm.
- ///
- ///\pre init() must be called and at least one node should be added
- ///with addSource() before using this function.
- ///
- ///This method runs the %DFS algorithm from the root node(s)
- ///in order to
- ///compute the
- ///%DFS path to each node. The algorithm computes
- ///- The %DFS tree.
- ///- The distance of each node from the root(s) in the %DFS tree.
- ///
- void start()
- {
- while ( !emptyQueue() ) processNextEdge();
- }
-
- ///Executes the algorithm until \c dest is reached.
-
- ///Executes the algorithm until \c dest is reached.
- ///
- ///\pre init() must be called and at least one node should be added
- ///with addSource() before using this function.
- ///
- ///This method runs the %DFS algorithm from the root node(s)
- ///in order to
- ///compute the
- ///%DFS path to \c dest. The algorithm computes
- ///- The %DFS path to \c dest.
- ///- The distance of \c dest from the root(s) in the %DFS tree.
- ///
- void start(Node dest)
- {
- while ( !emptyQueue() && G->target(_stack[_stack_head])!=dest )
- processNextEdge();
- }
-
- ///Executes the algorithm until a condition is met.
-
- ///Executes the algorithm until a condition is met.
- ///
- ///\pre init() must be called and at least one node should be added
- ///with addSource() before using this function.
- ///
- ///\param em must be a bool (or convertible) edge map. The algorithm
- ///will stop when it reaches an edge \c e with <tt>em[e]</tt> true.
- ///
- ///\return The reached edge \c e with <tt>em[e]</tt> true or
- ///\c INVALID if no such edge was found.
- ///
- ///\warning Contrary to \ref Bfs and \ref Dijkstra, \c em is an edge map,
- ///not a node map.
- template<class EM>
- Edge start(const EM &em)
- {
- while ( !emptyQueue() && !em[_stack[_stack_head]] )
- processNextEdge();
- return emptyQueue() ? INVALID : _stack[_stack_head];
- }
-
- ///Runs %DFS algorithm to visit all nodes in the graph.
-
- ///This method runs the %DFS algorithm in order to
- ///compute the
- ///%DFS path to each node. The algorithm computes
- ///- The %DFS tree.
- ///- The distance of each node from the root in the %DFS tree.
- ///
- ///\note d.run() is just a shortcut of the following code.
- ///\code
- /// d.init();
- /// for (NodeIt it(graph); it != INVALID; ++it) {
- /// if (!d.reached(it)) {
- /// d.addSource(it);
- /// d.start();
- /// }
- /// }
- ///\endcode
- void run() {
- init();
- for (NodeIt it(*G); it != INVALID; ++it) {
- if (!reached(it)) {
- addSource(it);
- start();
- }
- }
- }
-
- ///Runs %DFS algorithm from node \c s.
-
- ///This method runs the %DFS algorithm from a root node \c s
- ///in order to
- ///compute the
- ///%DFS path to each node. The algorithm computes
- ///- The %DFS tree.
- ///- The distance of each node from the root in the %DFS tree.
- ///
- ///\note d.run(s) is just a shortcut of the following code.
- ///\code
- /// d.init();
- /// d.addSource(s);
- /// d.start();
- ///\endcode
- void run(Node s) {
- init();
- addSource(s);
- start();
- }
-
- ///Finds the %DFS path between \c s and \c t.
-
- ///Finds the %DFS path between \c s and \c t.
- ///
- ///\return The length of the %DFS s---t path if there exists one,
- ///0 otherwise.
- ///\note Apart from the return value, d.run(s,t) is
- ///just a shortcut of the following code.
- ///\code
- /// d.init();
- /// d.addSource(s);
- /// d.start(t);
- ///\endcode
- int run(Node s,Node t) {
- init();
- addSource(s);
- start(t);
- return reached(t)?_stack_head+1:0;
- }
-
- ///@}
-
- ///\name Query Functions
- ///The result of the %DFS algorithm can be obtained using these
- ///functions.\n
- ///Before the use of these functions,
- ///either run() or start() must be called.
-
- ///@{
-
- typedef PredMapPath<Graph, PredMap> Path;
-
- ///Gives back the shortest path.
-
- ///Gives back the shortest path.
- ///\pre The \c t should be reachable from the source.
- Path path(Node t)
- {
- return Path(*G, *_pred, t);
- }
-
- ///The distance of a node from the root(s).
-
- ///Returns the distance of a node from the root(s).
- ///\pre \ref run() must be called before using this function.
- ///\warning If node \c v is unreachable from the root(s) then the return
- ///value of this funcion is undefined.
- int dist(Node v) const { return (*_dist)[v]; }
-
- ///Returns the 'previous edge' of the %DFS tree.
-
- ///For a node \c v it returns the 'previous edge'
- ///of the %DFS path,
- ///i.e. it returns the last edge of a %DFS path from the root(s) to \c
- ///v. It is \ref INVALID
- ///if \c v is unreachable from the root(s) or \c v is a root. The
- ///%DFS tree used here is equal to the %DFS tree used in
- ///\ref predNode().
- ///\pre Either \ref run() or \ref start() must be called before using
- ///this function.
- Edge predEdge(Node v) const { return (*_pred)[v];}
-
- ///Returns the 'previous node' of the %DFS tree.
-
- ///For a node \c v it returns the 'previous node'
- ///of the %DFS tree,
- ///i.e. it returns the last but one node from a %DFS path from the
- ///root(s) to \c v.
- ///It is INVALID if \c v is unreachable from the root(s) or
- ///if \c v itself a root.
- ///The %DFS tree used here is equal to the %DFS
- ///tree used in \ref predEdge().
- ///\pre Either \ref run() or \ref start() must be called before
- ///using this function.
- Node predNode(Node v) const { return (*_pred)[v]==INVALID ? INVALID:
- G->source((*_pred)[v]); }
-
- ///Returns a reference to the NodeMap of distances.
-
- ///Returns a reference to the NodeMap of distances.
- ///\pre Either \ref run() or \ref init() must
- ///be called before using this function.
- const DistMap &distMap() const { return *_dist;}
-
- ///Returns a reference to the %DFS edge-tree map.
-
- ///Returns a reference to the NodeMap of the edges of the
- ///%DFS tree.
- ///\pre Either \ref run() or \ref init()
- ///must be called before using this function.
- const PredMap &predMap() const { return *_pred;}
-
- ///Checks if a node is reachable from the root.
-
- ///Returns \c true if \c v is reachable from the root(s).
- ///\warning The source nodes are inditated as unreachable.
- ///\pre Either \ref run() or \ref start()
- ///must be called before using this function.
- ///
- bool reached(Node v) { return (*_reached)[v]; }
-
- ///@}
- };
-
- ///Default traits class of Dfs function.
-
- ///Default traits class of Dfs function.
- ///\param GR Graph type.
- template<class GR>
- struct DfsWizardDefaultTraits
- {
- ///The graph type the algorithm runs on.
- typedef GR Graph;
- ///\brief The type of the map that stores the last
- ///edges of the %DFS paths.
- ///
- ///The type of the map that stores the last
- ///edges of the %DFS paths.
- ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
- ///
- typedef NullMap<typename Graph::Node,typename GR::Edge> PredMap;
- ///Instantiates a PredMap.
-
- ///This function instantiates a \ref PredMap.
- ///\param g is the graph, to which we would like to define the PredMap.
- ///\todo The graph alone may be insufficient to initialize
-#ifdef DOXYGEN
- static PredMap *createPredMap(const GR &g)
-#else
- static PredMap *createPredMap(const GR &)
-#endif
- {
- return new PredMap();
- }
-
- ///The type of the map that indicates which nodes are processed.
-
- ///The type of the map that indicates which nodes are processed.
- ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
- ///\todo named parameter to set this type, function to read and write.
- typedef NullMap<typename Graph::Node,bool> ProcessedMap;
- ///Instantiates a ProcessedMap.
-
- ///This function instantiates a \ref ProcessedMap.
- ///\param g is the graph, to which
- ///we would like to define the \ref ProcessedMap
-#ifdef DOXYGEN
- static ProcessedMap *createProcessedMap(const GR &g)
-#else
- static ProcessedMap *createProcessedMap(const GR &)
-#endif
- {
- return new ProcessedMap();
- }
- ///The type of the map that indicates which nodes are reached.
-
- ///The type of the map that indicates which nodes are reached.
- ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
- ///\todo named parameter to set this type, function to read and write.
- typedef typename Graph::template NodeMap<bool> ReachedMap;
- ///Instantiates a ReachedMap.
-
- ///This function instantiates a \ref ReachedMap.
- ///\param G is the graph, to which
- ///we would like to define the \ref ReachedMap.
- static ReachedMap *createReachedMap(const GR &G)
- {
- return new ReachedMap(G);
- }
- ///The type of the map that stores the dists of the nodes.
-
- ///The type of the map that stores the dists of the nodes.
- ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
- ///
- typedef NullMap<typename Graph::Node,int> DistMap;
- ///Instantiates a DistMap.
-
- ///This function instantiates a \ref DistMap.
- ///\param g is the graph, to which we would like to define the \ref DistMap
-#ifdef DOXYGEN
- static DistMap *createDistMap(const GR &g)
-#else
- static DistMap *createDistMap(const GR &)
-#endif
- {
- return new DistMap();
- }
- };
-
- /// Default traits used by \ref DfsWizard
-
- /// To make it easier to use Dfs algorithm
- ///we have created a wizard class.
- /// This \ref DfsWizard class needs default traits,
- ///as well as the \ref Dfs class.
- /// The \ref DfsWizardBase is a class to be the default traits of the
- /// \ref DfsWizard class.
- template<class GR>
- class DfsWizardBase : public DfsWizardDefaultTraits<GR>
- {
-
- typedef DfsWizardDefaultTraits<GR> Base;
- protected:
- /// Type of the nodes in the graph.
- typedef typename Base::Graph::Node Node;
-
- /// Pointer to the underlying graph.
- void *_g;
- ///Pointer to the map of reached nodes.
- void *_reached;
- ///Pointer to the map of processed nodes.
- void *_processed;
- ///Pointer to the map of predecessors edges.
- void *_pred;
- ///Pointer to the map of distances.
- void *_dist;
- ///Pointer to the source node.
- Node _source;
-
- public:
- /// Constructor.
-
- /// This constructor does not require parameters, therefore it initiates
- /// all of the attributes to default values (0, INVALID).
- DfsWizardBase() : _g(0), _reached(0), _processed(0), _pred(0),
- _dist(0), _source(INVALID) {}
-
- /// Constructor.
-
- /// This constructor requires some parameters,
- /// listed in the parameters list.
- /// Others are initiated to 0.
- /// \param g is the initial value of \ref _g
- /// \param s is the initial value of \ref _source
- DfsWizardBase(const GR &g, Node s=INVALID) :
- _g(reinterpret_cast<void*>(const_cast<GR*>(&g))),
- _reached(0), _processed(0), _pred(0), _dist(0), _source(s) {}
-
- };
-
- /// A class to make the usage of the Dfs algorithm easier
-
- /// This class is created to make it easier to use the Dfs algorithm.
- /// It uses the functions and features of the plain \ref Dfs,
- /// but it is much simpler to use it.
- ///
- /// Simplicity means that the way to change the types defined
- /// in the traits class is based on functions that returns the new class
- /// and not on templatable built-in classes.
- /// When using the plain \ref Dfs
- /// the new class with the modified type comes from
- /// the original class by using the ::
- /// operator. In the case of \ref DfsWizard only
- /// a function have to be called and it will
- /// return the needed class.
- ///
- /// It does not have own \ref run method. When its \ref run method is called
- /// it initiates a plain \ref Dfs object, and calls the \ref Dfs::run
- /// method of it.
- template<class TR>
- class DfsWizard : public TR
- {
- typedef TR Base;
-
- ///The type of the underlying graph.
- typedef typename TR::Graph Graph;
- //\e
- typedef typename Graph::Node Node;
- //\e
- typedef typename Graph::NodeIt NodeIt;
- //\e
- typedef typename Graph::Edge Edge;
- //\e
- typedef typename Graph::OutEdgeIt OutEdgeIt;
-
- ///\brief The type of the map that stores
- ///the reached nodes
- typedef typename TR::ReachedMap ReachedMap;
- ///\brief The type of the map that stores
- ///the processed nodes
- typedef typename TR::ProcessedMap ProcessedMap;
- ///\brief The type of the map that stores the last
- ///edges of the %DFS paths.
- typedef typename TR::PredMap PredMap;
- ///The type of the map that stores the distances of the nodes.
- typedef typename TR::DistMap DistMap;
-
- public:
- /// Constructor.
- DfsWizard() : TR() {}
-
- /// Constructor that requires parameters.
-
- /// Constructor that requires parameters.
- /// These parameters will be the default values for the traits class.
- DfsWizard(const Graph &g, Node s=INVALID) :
- TR(g,s) {}
-
- ///Copy constructor
- DfsWizard(const TR &b) : TR(b) {}
-
- ~DfsWizard() {}
-
- ///Runs Dfs algorithm from a given node.
-
- ///Runs Dfs algorithm from a given node.
- ///The node can be given by the \ref source function.
- void run()
- {
- if(Base::_source==INVALID) throw UninitializedParameter();
- Dfs<Graph,TR> alg(*reinterpret_cast<const Graph*>(Base::_g));
- if(Base::_reached)
- alg.reachedMap(*reinterpret_cast<ReachedMap*>(Base::_reached));
- if(Base::_processed)
- alg.processedMap(*reinterpret_cast<ProcessedMap*>(Base::_processed));
- if(Base::_pred)
- alg.predMap(*reinterpret_cast<PredMap*>(Base::_pred));
- if(Base::_dist)
- alg.distMap(*reinterpret_cast<DistMap*>(Base::_dist));
- alg.run(Base::_source);
- }
-
- ///Runs Dfs algorithm from the given node.
-
- ///Runs Dfs algorithm from the given node.
- ///\param s is the given source.
- void run(Node s)
- {
- Base::_source=s;
- run();
- }
-
- template<class T>
- struct DefPredMapBase : public Base {
- typedef T PredMap;
- static PredMap *createPredMap(const Graph &) { return 0; };
- DefPredMapBase(const TR &b) : TR(b) {}
- };
-
- ///\brief \ref named-templ-param "Named parameter"
- ///function for setting PredMap type
- ///
- /// \ref named-templ-param "Named parameter"
- ///function for setting PredMap type
- ///
- template<class T>
- DfsWizard<DefPredMapBase<T> > predMap(const T &t)
- {
- Base::_pred=reinterpret_cast<void*>(const_cast<T*>(&t));
- return DfsWizard<DefPredMapBase<T> >(*this);
- }
-
-
- template<class T>
- struct DefReachedMapBase : public Base {
- typedef T ReachedMap;
- static ReachedMap *createReachedMap(const Graph &) { return 0; };
- DefReachedMapBase(const TR &b) : TR(b) {}
- };
-
- ///\brief \ref named-templ-param "Named parameter"
- ///function for setting ReachedMap
- ///
- /// \ref named-templ-param "Named parameter"
- ///function for setting ReachedMap
- ///
- template<class T>
- DfsWizard<DefReachedMapBase<T> > reachedMap(const T &t)
- {
- Base::_pred=reinterpret_cast<void*>(const_cast<T*>(&t));
- return DfsWizard<DefReachedMapBase<T> >(*this);
- }
-
-
- template<class T>
- struct DefProcessedMapBase : public Base {
- typedef T ProcessedMap;
- static ProcessedMap *createProcessedMap(const Graph &) { return 0; };
- DefProcessedMapBase(const TR &b) : TR(b) {}
- };
-
- ///\brief \ref named-templ-param "Named parameter"
- ///function for setting ProcessedMap
- ///
- /// \ref named-templ-param "Named parameter"
- ///function for setting ProcessedMap
- ///
- template<class T>
- DfsWizard<DefProcessedMapBase<T> > processedMap(const T &t)
- {
- Base::_pred=reinterpret_cast<void*>(const_cast<T*>(&t));
- return DfsWizard<DefProcessedMapBase<T> >(*this);
- }
-
- template<class T>
- struct DefDistMapBase : public Base {
- typedef T DistMap;
- static DistMap *createDistMap(const Graph &) { return 0; };
- DefDistMapBase(const TR &b) : TR(b) {}
- };
-
- ///\brief \ref named-templ-param "Named parameter"
- ///function for setting DistMap type
- ///
- /// \ref named-templ-param "Named parameter"
- ///function for setting DistMap type
- ///
- template<class T>
- DfsWizard<DefDistMapBase<T> > distMap(const T &t)
- {
- Base::_dist=reinterpret_cast<void*>(const_cast<T*>(&t));
- return DfsWizard<DefDistMapBase<T> >(*this);
- }
-
- /// Sets the source node, from which the Dfs algorithm runs.
-
- /// Sets the source node, from which the Dfs algorithm runs.
- /// \param s is the source node.
- DfsWizard<TR> &source(Node s)
- {
- Base::_source=s;
- return *this;
- }
-
- };
-
- ///Function type interface for Dfs algorithm.
-
- ///\ingroup search
- ///Function type interface for Dfs algorithm.
- ///
- ///This function also has several
- ///\ref named-templ-func-param "named parameters",
- ///they are declared as the members of class \ref DfsWizard.
- ///The following
- ///example shows how to use these parameters.
- ///\code
- /// dfs(g,source).predMap(preds).run();
- ///\endcode
- ///\warning Don't forget to put the \ref DfsWizard::run() "run()"
- ///to the end of the parameter list.
- ///\sa DfsWizard
- ///\sa Dfs
- template<class GR>
- DfsWizard<DfsWizardBase<GR> >
- dfs(const GR &g,typename GR::Node s=INVALID)
- {
- return DfsWizard<DfsWizardBase<GR> >(g,s);
- }
-
-#ifdef DOXYGEN
- /// \brief Visitor class for dfs.
- ///
- /// It gives a simple interface for a functional interface for dfs
- /// traversal. The traversal on a linear data structure.
- template <typename _Graph>
- struct DfsVisitor {
- typedef _Graph Graph;
- typedef typename Graph::Edge Edge;
- typedef typename Graph::Node Node;
- /// \brief Called when the edge reach a node.
- ///
- /// It is called when the dfs find an edge which target is not
- /// reached yet.
- void discover(const Edge& edge) {}
- /// \brief Called when the node reached first time.
- ///
- /// It is Called when the node reached first time.
- void reach(const Node& node) {}
- /// \brief Called when we step back on an edge.
- ///
- /// It is called when the dfs should step back on the edge.
- void backtrack(const Edge& edge) {}
- /// \brief Called when we step back from the node.
- ///
- /// It is called when we step back from the node.
- void leave(const Node& node) {}
- /// \brief Called when the edge examined but target of the edge
- /// already discovered.
- ///
- /// It called when the edge examined but the target of the edge
- /// already discovered.
- void examine(const Edge& edge) {}
- /// \brief Called for the source node of the dfs.
- ///
- /// It is called for the source node of the dfs.
- void start(const Node& node) {}
- /// \brief Called when we leave the source node of the dfs.
- ///
- /// It is called when we leave the source node of the dfs.
- void stop(const Node& node) {}
-
- };
-#else
- template <typename _Graph>
- struct DfsVisitor {
- typedef _Graph Graph;
- typedef typename Graph::Edge Edge;
- typedef typename Graph::Node Node;
- void discover(const Edge&) {}
- void reach(const Node&) {}
- void backtrack(const Edge&) {}
- void leave(const Node&) {}
- void examine(const Edge&) {}
- void start(const Node&) {}
- void stop(const Node&) {}
-
- template <typename _Visitor>
- struct Constraints {
- void constraints() {
- Edge edge;
- Node node;
- visitor.discover(edge);
- visitor.reach(node);
- visitor.backtrack(edge);
- visitor.leave(node);
- visitor.examine(edge);
- visitor.start(node);
- visitor.stop(edge);
- }
- _Visitor& visitor;
- };
- };
-#endif
-
- /// \brief Default traits class of DfsVisit class.
- ///
- /// Default traits class of DfsVisit class.
- /// \param _Graph Graph type.
- template<class _Graph>
- struct DfsVisitDefaultTraits {
-
- /// \brief The graph type the algorithm runs on.
- typedef _Graph Graph;
-
- /// \brief The type of the map that indicates which nodes are reached.
- ///
- /// The type of the map that indicates which nodes are reached.
- /// It must meet the \ref concepts::WriteMap "WriteMap" concept.
- /// \todo named parameter to set this type, function to read and write.
- typedef typename Graph::template NodeMap<bool> ReachedMap;
-
- /// \brief Instantiates a ReachedMap.
- ///
- /// This function instantiates a \ref ReachedMap.
- /// \param graph is the graph, to which
- /// we would like to define the \ref ReachedMap.
- static ReachedMap *createReachedMap(const Graph &graph) {
- return new ReachedMap(graph);
- }
-
- };
-
- /// %DFS Visit algorithm class.
-
- /// \ingroup search
- /// This class provides an efficient implementation of the %DFS algorithm
- /// with visitor interface.
- ///
- /// The %DfsVisit class provides an alternative interface to the Dfs
- /// class. It works with callback mechanism, the DfsVisit object calls
- /// on every dfs event the \c Visitor class member functions.
- ///
- /// \param _Graph The graph type the algorithm runs on. The default value is
- /// \ref ListGraph. The value of _Graph is not used directly by Dfs, it
- /// is only passed to \ref DfsDefaultTraits.
- /// \param _Visitor The Visitor object for the algorithm. The
- /// \ref DfsVisitor "DfsVisitor<_Graph>" is an empty Visitor which
- /// does not observe the Dfs events. If you want to observe the dfs
- /// events you should implement your own Visitor class.
- /// \param _Traits Traits class to set various data types used by the
- /// algorithm. The default traits class is
- /// \ref DfsVisitDefaultTraits "DfsVisitDefaultTraits<_Graph>".
- /// See \ref DfsVisitDefaultTraits for the documentation of
- /// a Dfs visit traits class.
- ///
- /// \author Jacint Szabo, Alpar Juttner and Balazs Dezso
-#ifdef DOXYGEN
- template <typename _Graph, typename _Visitor, typename _Traits>
-#else
- template <typename _Graph = ListGraph,
- typename _Visitor = DfsVisitor<_Graph>,
- typename _Traits = DfsDefaultTraits<_Graph> >
-#endif
- class DfsVisit {
- public:
-
- /// \brief \ref Exception for uninitialized parameters.
- ///
- /// This error represents problems in the initialization
- /// of the parameters of the algorithms.
- class UninitializedParameter : public lemon::UninitializedParameter {
- public:
- virtual const char* what() const throw()
- {
- return "lemon::DfsVisit::UninitializedParameter";
- }
- };
-
- typedef _Traits Traits;
-
- typedef typename Traits::Graph Graph;
-
- typedef _Visitor Visitor;
-
- ///The type of the map indicating which nodes are reached.
- typedef typename Traits::ReachedMap ReachedMap;
-
- private:
-
- typedef typename Graph::Node Node;
- typedef typename Graph::NodeIt NodeIt;
- typedef typename Graph::Edge Edge;
- typedef typename Graph::OutEdgeIt OutEdgeIt;
-
- /// Pointer to the underlying graph.
- const Graph *_graph;
- /// Pointer to the visitor object.
- Visitor *_visitor;
- ///Pointer to the map of reached status of the nodes.
- ReachedMap *_reached;
- ///Indicates if \ref _reached is locally allocated (\c true) or not.
- bool local_reached;
-
- std::vector<typename Graph::Edge> _stack;
- int _stack_head;
-
- /// \brief Creates the maps if necessary.
- ///
- /// Creates the maps if necessary.
- void create_maps() {
- if(!_reached) {
- local_reached = true;
- _reached = Traits::createReachedMap(*_graph);
- }
- }
-
- protected:
-
- DfsVisit() {}
-
- public:
-
- typedef DfsVisit Create;
-
- /// \name Named template parameters
-
- ///@{
- template <class T>
- struct DefReachedMapTraits : public Traits {
- typedef T ReachedMap;
- static ReachedMap *createReachedMap(const Graph &graph) {
- throw UninitializedParameter();
- }
- };
- /// \brief \ref named-templ-param "Named parameter" for setting
- /// ReachedMap type
- ///
- /// \ref named-templ-param "Named parameter" for setting ReachedMap type
- template <class T>
- struct DefReachedMap : public DfsVisit< Graph, Visitor,
- DefReachedMapTraits<T> > {
- typedef DfsVisit< Graph, Visitor, DefReachedMapTraits<T> > Create;
- };
- ///@}
-
- public:
-
- /// \brief Constructor.
- ///
- /// Constructor.
- ///
- /// \param graph the graph the algorithm will run on.
- /// \param visitor The visitor of the algorithm.
- ///
- DfsVisit(const Graph& graph, Visitor& visitor)
- : _graph(&graph), _visitor(&visitor),
- _reached(0), local_reached(false) {}
-
- /// \brief Destructor.
- ///
- /// Destructor.
- ~DfsVisit() {
- if(local_reached) delete _reached;
- }
-
- /// \brief Sets the map indicating if a node is reached.
- ///
- /// Sets the map indicating if a node is reached.
- /// If you don't use this function before calling \ref run(),
- /// it will allocate one. The destuctor deallocates this
- /// automatically allocated map, of course.
- /// \return <tt> (*this) </tt>
- DfsVisit &reachedMap(ReachedMap &m) {
- if(local_reached) {
- delete _reached;
- local_reached=false;
- }
- _reached = &m;
- return *this;
- }
-
- public:
- /// \name Execution control
- /// The simplest way to execute the algorithm is to use
- /// one of the member functions called \c run(...).
- /// \n
- /// If you need more control on the execution,
- /// first you must call \ref init(), then you can adda source node
- /// with \ref addSource().
- /// Finally \ref start() will perform the actual path
- /// computation.
-
- /// @{
- /// \brief Initializes the internal data structures.
- ///
- /// Initializes the internal data structures.
- ///
- void init() {
- create_maps();
- _stack.resize(countNodes(*_graph));
- _stack_head = -1;
- for (NodeIt u(*_graph) ; u != INVALID ; ++u) {
- _reached->set(u, false);
- }
- }
-
- /// \brief Adds a new source node.
- ///
- /// Adds a new source node to the set of nodes to be processed.
- void addSource(Node s) {
- if(!(*_reached)[s]) {
- _reached->set(s,true);
- _visitor->start(s);
- _visitor->reach(s);
- Edge e;
- _graph->firstOut(e, s);
- if (e != INVALID) {
- _stack[++_stack_head] = e;
- } else {
- _visitor->leave(s);
- }
- }
- }
-
- /// \brief Processes the next edge.
- ///
- /// Processes the next edge.
- ///
- /// \return The processed edge.
- ///
- /// \pre The stack must not be empty!
- Edge processNextEdge() {
- Edge e = _stack[_stack_head];
- Node m = _graph->target(e);
- if(!(*_reached)[m]) {
- _visitor->discover(e);
- _visitor->reach(m);
- _reached->set(m, true);
- _graph->firstOut(_stack[++_stack_head], m);
- } else {
- _visitor->examine(e);
- m = _graph->source(e);
- _graph->nextOut(_stack[_stack_head]);
- }
- while (_stack_head>=0 && _stack[_stack_head] == INVALID) {
- _visitor->leave(m);
- --_stack_head;
- if (_stack_head >= 0) {
- _visitor->backtrack(_stack[_stack_head]);
- m = _graph->source(_stack[_stack_head]);
- _graph->nextOut(_stack[_stack_head]);
- } else {
- _visitor->stop(m);
- }
- }
- return e;
- }
-
- /// \brief Next edge to be processed.
- ///
- /// Next edge to be processed.
- ///
- /// \return The next edge to be processed or INVALID if the stack is
- /// empty.
- Edge nextEdge() {
- return _stack_head >= 0 ? _stack[_stack_head] : INVALID;
- }
-
- /// \brief Returns \c false if there are nodes
- /// to be processed in the queue
- ///
- /// Returns \c false if there are nodes
- /// to be processed in the queue
- bool emptyQueue() { return _stack_head < 0; }
-
- /// \brief Returns the number of the nodes to be processed.
- ///
- /// Returns the number of the nodes to be processed in the queue.
- int queueSize() { return _stack_head + 1; }
-
- /// \brief Executes the algorithm.
- ///
- /// Executes the algorithm.
- ///
- /// \pre init() must be called and at least one node should be added
- /// with addSource() before using this function.
- void start() {
- while ( !emptyQueue() ) processNextEdge();
- }
-
- /// \brief Executes the algorithm until \c dest is reached.
- ///
- /// Executes the algorithm until \c dest is reached.
- ///
- /// \pre init() must be called and at least one node should be added
- /// with addSource() before using this function.
- void start(Node dest) {
- while ( !emptyQueue() && _graph->target(_stack[_stack_head]) != dest )
- processNextEdge();
- }
-
- /// \brief Executes the algorithm until a condition is met.
- ///
- /// Executes the algorithm until a condition is met.
- ///
- /// \pre init() must be called and at least one node should be added
- /// with addSource() before using this function.
- ///
- /// \param em must be a bool (or convertible) edge map. The algorithm
- /// will stop when it reaches an edge \c e with <tt>em[e]</tt> true.
- ///
- ///\return The reached edge \c e with <tt>em[e]</tt> true or
- ///\c INVALID if no such edge was found.
- ///
- /// \warning Contrary to \ref Bfs and \ref Dijkstra, \c em is an edge map,
- /// not a node map.
- template <typename EM>
- Edge start(const EM &em) {
- while ( !emptyQueue() && !em[_stack[_stack_head]] )
- processNextEdge();
- return emptyQueue() ? INVALID : _stack[_stack_head];
- }
-
- /// \brief Runs %DFSVisit algorithm from node \c s.
- ///
- /// This method runs the %DFS algorithm from a root node \c s.
- /// \note d.run(s) is just a shortcut of the following code.
- ///\code
- /// d.init();
- /// d.addSource(s);
- /// d.start();
- ///\endcode
- void run(Node s) {
- init();
- addSource(s);
- start();
- }
-
- /// \brief Runs %DFSVisit algorithm to visit all nodes in the graph.
-
- /// This method runs the %DFS algorithm in order to
- /// compute the %DFS path to each node. The algorithm computes
- /// - The %DFS tree.
- /// - The distance of each node from the root in the %DFS tree.
- ///
- ///\note d.run() is just a shortcut of the following code.
- ///\code
- /// d.init();
- /// for (NodeIt it(graph); it != INVALID; ++it) {
- /// if (!d.reached(it)) {
- /// d.addSource(it);
- /// d.start();
- /// }
- /// }
- ///\endcode
- void run() {
- init();
- for (NodeIt it(*_graph); it != INVALID; ++it) {
- if (!reached(it)) {
- addSource(it);
- start();
- }
- }
- }
- ///@}
-
- /// \name Query Functions
- /// The result of the %DFS algorithm can be obtained using these
- /// functions.\n
- /// Before the use of these functions,
- /// either run() or start() must be called.
- ///@{
- /// \brief Checks if a node is reachable from the root.
- ///
- /// Returns \c true if \c v is reachable from the root(s).
- /// \warning The source nodes are inditated as unreachable.
- /// \pre Either \ref run() or \ref start()
- /// must be called before using this function.
- ///
- bool reached(Node v) { return (*_reached)[v]; }
- ///@}
- };
-
-
-} //END OF NAMESPACE LEMON
-
-#endif
-
diff --git a/src/lemon/error.h b/src/lemon/error.h
deleted file mode 100644
index 01931d8..0000000
--- a/src/lemon/error.h
+++ /dev/null
@@ -1,683 +0,0 @@
-/* -*- C++ -*-
- *
- * This file is a part of LEMON, a generic C++ optimization library
- *
- * Copyright (C) 2003-2008
- * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
- * (Egervary Research Group on Combinatorial Optimization, EGRES).
- *
- * Permission to use, modify and distribute this software is granted
- * provided that this copyright notice appears in all copies. For
- * precise terms see the accompanying LICENSE file.
- *
- * This software is provided "AS IS" with no warranty of any kind,
- * express or implied, and with no claim as to its suitability for any
- * purpose.
- *
- */
-
-#ifndef LEMON_ERROR_H
-#define LEMON_ERROR_H
-
-//! \ingroup exceptions
-//! \file
-//! \brief Basic exception classes and error handling.
-
-#include <exception>
-#include <string>
-#include <sstream>
-#include <iostream>
-#include <cstdlib>
-#include <memory>
-
-namespace lemon {
-
- /// \addtogroup exceptions
- /// @{
-
- /// \brief Exception safe wrapper class.
- ///
- /// Exception safe wrapper class to implement the members of exceptions.
- template <typename _Type>
- class ExceptionMember {
- public:
- typedef _Type Type;
-
- ExceptionMember() throw () {
- try {
- ptr.reset(new Type());
- } catch (...) {}
- }
-
- ExceptionMember(const Type& type) throw () {
- try {
- ptr.reset(new Type());
- if (ptr.get() == 0) return;
- *ptr = type;
- } catch (...) {}
- }
-
- ExceptionMember(const ExceptionMember& copy) throw() {
- try {
- if (!copy.valid()) return;
- ptr.reset(new Type());
- if (ptr.get() == 0) return;
- *ptr = copy.get();
- } catch (...) {}
- }
-
- ExceptionMember& operator=(const ExceptionMember& copy) {
- if (ptr.get() == 0) return;
- try {
- if (!copy.valid()) return;
- *ptr = copy.get();
- } catch (...) {}
- }
-
- void set(const Type& type) {
- if (ptr.get() == 0) return;
- try {
- *ptr = type;
- } catch (...) {}
- }
-
- const Type& get() const {
- return *ptr;
- }
-
- bool valid() const {
- return ptr.get() != 0;
- }
-
- private:
- std::auto_ptr<_Type> ptr;
- };
-
- /// Exception-safe convenient "error message" class.
-
- /// Helper class which provides a convenient ostream-like (operator <<
- /// based) interface to create a string message. Mostly useful in
- /// exception classes (therefore the name).
- class ErrorMessage {
- protected:
- ///\e
-
- ///\todo The good solution is boost::shared_ptr...
- ///
- mutable
- std::auto_ptr<std::ostringstream> buf;
-
- ///\e
- bool init() throw() {
- try {
- buf.reset(new std::ostringstream);
- }
- catch(...) {
- buf.reset();
- }
- return buf.get();
- }
-
- public:
-
- ///\e
- ErrorMessage() throw() { init(); }
-
- ErrorMessage(const ErrorMessage& em) throw() : buf(em.buf) { }
-
- ///\e
- ErrorMessage(const char *msg) throw() {
- init();
- *this << msg;
- }
-
- ///\e
- ErrorMessage(const std::string &msg) throw() {
- init();
- *this << msg;
- }
-
- ///\e
- template <typename T>
- ErrorMessage& operator<<(const T &t) throw() {
- if( ! buf.get() ) return *this;
-
- try {
- *buf << t;
- }
- catch(...) {
- buf.reset();
- }
- return *this;
- }
-
- ///\e
- const char* message() throw() {
- if( ! buf.get() ) return 0;
-
- const char* mes = 0;
- try {
- mes = buf->str().c_str();
- }
- catch(...) {}
- return mes;
- }
-
- };
-
- /**
- * \brief Generic exception class.
- *
- * Base class for exceptions used in LEMON.
- */
- class Exception : public std::exception {
- public:
- ///\e
- Exception() {}
- ///\e
- virtual ~Exception() throw() {}
- ///\e
- virtual const char* what() const throw() {
- return "lemon::Exception";
- }
- };
-
- /**
- * \brief One of the two main subclasses of \ref Exception.
- *
- * Logic errors represent problems in the internal logic of a program;
- * in theory, these are preventable, and even detectable before the
- * program runs (e.g., violations of class invariants).
- *
- * A typical example for this is \ref UninitializedParameter.
- */
- class LogicError : public Exception {
- public:
- virtual const char* what() const throw() {
- return "lemon::LogicError";
- }
- };
-
- /**
- * \brief \ref Exception for uninitialized parameters.
- *
- * This error represents problems in the initialization
- * of the parameters of the algorithms.
- */
- class UninitializedParameter : public LogicError {
- public:
- virtual const char* what() const throw() {
- return "lemon::UninitializedParameter";
- }
- };
-
-
- /**
- * \brief One of the two main subclasses of \ref Exception.
- *
- * Runtime errors represent problems outside the scope of a program;
- * they cannot be easily predicted and can generally only be caught as
- * the program executes.
- */
- class RuntimeError : public Exception {
- public:
- virtual const char* what() const throw() {
- return "lemon::RuntimeError";
- }
- };
-
- ///\e
- class RangeError : public RuntimeError {
- public:
- virtual const char* what() const throw() {
- return "lemon::RangeError";
- }
- };
-
- ///\e
- class IoError : public RuntimeError {
- public:
- virtual const char* what() const throw() {
- return "lemon::IoError";
- }
- };
-
- ///\e
- class DataFormatError : public IoError {
- protected:
- ExceptionMember<std::string> _message;
- ExceptionMember<std::string> _file;
- int _line;
-
- mutable ExceptionMember<std::string> _message_holder;
- public:
-
- DataFormatError(const DataFormatError &dfe) :
- IoError(dfe), _message(dfe._message), _file(dfe._file),
- _line(dfe._line) {}
-
- ///\e
- explicit DataFormatError(const char *the_message)
- : _message(the_message), _line(0) {}
-
- ///\e
- DataFormatError(const std::string &file_name, int line_num,
- const char *the_message)
- : _message(the_message), _line(line_num) { file(file_name); }
-
- ///\e
- void line(int ln) { _line = ln; }
- ///\e
- void message(const std::string& msg) { _message.set(msg); }
- ///\e
- void file(const std::string &fl) { _file.set(fl); }
-
- ///\e
- int line() const { return _line; }
- ///\e
- const char* message() const {
- if (_message.valid() && !_message.get().empty()) {
- return _message.get().c_str();
- } else {
- return 0;
- }
- }
-
- /// \brief Returns the filename.
- ///
- /// Returns \e null if the filename was not specified.
- const char* file() const {
- if (_file.valid() && !_file.get().empty()) {
- return _file.get().c_str();
- } else {
- return 0;
- }
- }
-
- ///\e
- virtual const char* what() const throw() {
- try {
- std::ostringstream ostr;
- ostr << "lemon:DataFormatError" << ": ";
- if (message()) ostr << message();
- if( file() || line() != 0 ) {
- ostr << " (";
- if( file() ) ostr << "in file '" << file() << "'";
- if( file() && line() != 0 ) ostr << " ";
- if( line() != 0 ) ostr << "at line " << line();
- ostr << ")";
- }
- _message_holder.set(ostr.str());
- }
- catch (...) {}
- if( _message_holder.valid()) return _message_holder.get().c_str();
- return "lemon:DataFormatError";
- }
-
- virtual ~DataFormatError() throw() {}
- };
-
- ///\e
- class FileOpenError : public IoError {
- protected:
- ExceptionMember<std::string> _file;
-
- mutable ExceptionMember<std::string> _message_holder;
- public:
-
- FileOpenError(const FileOpenError &foe) :
- IoError(foe), _file(foe._file) {}
-
- ///\e
- explicit FileOpenError(const std::string& fl)
- : _file(fl) {}
-
-
- ///\e
- void file(const std::string &fl) { _file.set(fl); }
-
- /// \brief Returns the filename.
- ///
- /// Returns \e null if the filename was not specified.
- const char* file() const {
- if (_file.valid() && !_file.get().empty()) {
- return _file.get().c_str();
- } else {
- return 0;
- }
- }
-
- ///\e
- virtual const char* what() const throw() {
- try {
- std::ostringstream ostr;
- ostr << "lemon::FileOpenError" << ": ";
- ostr << "Cannot open file - " << file();
- _message_holder.set(ostr.str());
- }
- catch (...) {}
- if( _message_holder.valid()) return _message_holder.get().c_str();
- return "lemon::FileOpenError";
- }
- virtual ~FileOpenError() throw() {}
- };
-
- class IoParameterError : public IoError {
- protected:
- ExceptionMember<std::string> _message;
- ExceptionMember<std::string> _file;
-
- mutable ExceptionMember<std::string> _message_holder;
- public:
-
- IoParameterError(const IoParameterError &ile) :
- IoError(ile), _message(ile._message), _file(ile._file) {}
-
- ///\e
- explicit IoParameterError(const char *the_message)
- : _message(the_message) {}
-
- ///\e
- IoParameterError(const char *file_name, const char *the_message)
- : _message(the_message), _file(file_name) {}
-
- ///\e
- void message(const std::string& msg) { _message.set(msg); }
- ///\e
- void file(const std::string &fl) { _file.set(fl); }
-
- ///\e
- const char* message() const {
- if (_message.valid()) {
- return _message.get().c_str();
- } else {
- return 0;
- }
- }
-
- /// \brief Returns the filename.
- ///
- /// Returns \e null if the filename was not specified.
- const char* file() const {
- if (_file.valid()) {
- return _file.get().c_str();
- } else {
- return 0;
- }
- }
-
- ///\e
- virtual const char* what() const throw() {
- try {
- std::ostringstream ostr;
- if (message()) ostr << message();
- if (file()) ostr << "(when reading file '" << file() << "')";
- _message_holder.set(ostr.str());
- }
- catch (...) {}
- if( _message_holder.valid() ) return _message_holder.get().c_str();
- return "lemon:IoParameterError";
- }
- virtual ~IoParameterError() throw() {}
- };
-
-
- ///\e
- class AssertionFailedError : public LogicError {
- protected:
- const char *assertion;
- const char *file;
- int line;
- const char *function;
- const char *message;
-
- mutable ExceptionMember<std::string> _message_holder;
- public:
- ///\e
- AssertionFailedError(const char *_file, int _line, const char *func,
- const char *msg, const char *_assertion = 0) :
- assertion(_assertion), file(_file), line(_line), function(func),
- message(msg) {}
-
- ///\e
- const char* get_assertion() const { return assertion; }
- ///\e
- const char* get_message() const { return message; }
- ///\e
- const char* get_file() const { return file; }
- ///\e
- const char* get_function() const { return function; }
- ///\e
- int get_line() const { return line; }
-
-
- virtual const char* what() const throw() {
- try {
- std::ostringstream ostr;
- ostr << file << ":" << line << ": ";
- if( function )
- ostr << function << ": ";
- ostr << message;
- if( assertion )
- ostr << " (assertion '" << assertion << "' failed)";
- _message_holder.set(ostr.str());
- return ostr.str().c_str();
- }
- catch(...) {}
- if( _message_holder.valid() ) return _message_holder.get().c_str();
- return "lemon::AssertionFailedError";
- }
- virtual ~AssertionFailedError() throw() {}
- };
-
-
- /**************** Macros ****************/
-
-
- template <typename Exception>
- inline void assert_fail(const char *file, int line,
- const char *func,
- Exception exception,
- const char *assertion = 0,
- bool do_abort=true)
- {
- using namespace std;
- cerr << file << ":" << line << ": ";
- if( func )
- cerr << func << ": ";
- cerr << exception.what();
- if( assertion )
- cerr << " (assertion '" << assertion << "' failed)";
- cerr << endl;
- if(do_abort)
- abort();
- }
-
- template <>
- inline void assert_fail<const char *>(const char *file, int line,
- const char *func,
- const char *message,
- const char *assertion,
- bool do_abort)
- {
- using namespace std;
- cerr << file << ":" << line << ": ";
- if( func )
- cerr << func << ": ";
- cerr << message;
- if( assertion )
- cerr << " (assertion '" << assertion << "' failed)";
- cerr << endl;
- if(do_abort)
- abort();
- }
-
- template <>
- inline void assert_fail<std::string>(const char *file, int line,
- const char *func,
- std::string message,
- const char *assertion,
- bool do_abort)
- {
- assert_fail(file, line, func, message.c_str(), assertion, do_abort);
- }
-
- template <typename Exception>
- inline void assert_fail_failure(const char *file, int line, const char *func,
- Exception exception,
- const char *assertion = 0,
- bool = true)
- {
- throw AssertionFailedError(file, line, func, exception.what(), assertion);
- }
-
- template <>
- inline void assert_fail_failure<const char *>(const char *file, int line,
- const char *func,
- const char *message,
- const char *assertion,
- bool)
- {
- throw AssertionFailedError(file, line, func, message, assertion);
- }
-
- template <>
- inline void assert_fail_failure<std::string>(const char *file, int line,
- const char *func,
- std::string message,
- const char *assertion,
- bool)
- {
- assert_fail_failure(file, line, func, message.c_str(), assertion, true);
- }
-
- template <typename Exception>
- inline void assert_fail_exception(const char *file, int line, const char *func,
- Exception exception,
- const char *assertion = 0, bool = true)
- {
- throw exception;
- }
-
- template <>
- inline void assert_fail_exception<const char *>(const char *file, int line,
- const char *func,
- const char *message,
- const char *assertion,
- bool)
- {
- throw AssertionFailedError(file, line, func, message, assertion);
- }
-
- template <>
- inline void assert_fail_exception<std::string>(const char *file, int line,
- const char *func,
- std::string message,
- const char *assertion,
- bool)
- {
- assert_fail_exception(file, line, func, message.c_str(), assertion, true);
- }
-
-/// @}
-
-}
-#endif // LEMON_ERROR_H
-
-#undef LEMON_ASSERT
-#undef LEMON_FIXME
-
-#ifdef LEMON_ENABLE_ASSERTS
-# define LEMON_ASSERT_ABORT
-#endif
-
-#ifndef LEMON_ASSERT_DO_ABORT
-# define LEMON_ASSERT_DO_ABORT 1
-#endif
-
-#ifndef LEMON_ASSERT_HANDLER
-# if defined LEMON_ASSERT_EXCEPTION
-# define LEMON_ASSERT_HANDLER ::lemon::assert_fail_exception
-# elif defined LEMON_ASSERT_FAILURE
-# define LEMON_ASSERT_HANDLER ::lemon::assert_fail_failure
-# elif defined LEMON_ASSERT_ABORT
-# define LEMON_ASSERT_HANDLER ::lemon::assert_fail
-# else
-# define LEMON_DISABLE_ASSERTS
-# endif
-#endif
-
-#ifdef DOXYGEN
-
-/// \brief Macro for assertions with customizable message
-///
-/// Macro for assertions with customizable message.
-///
-/// The assertions are disabled in the default behaviour. You can
-/// enable the assertions with the
-/// \code
-/// #define LEMON_ENABLE_ASSERTS
-/// \endcode
-/// Then an assert
-/// provides a log on the standard error about the assertion and aborts
-/// the program if LEMON_ASSERT_DO_ABORT is also defined (otherwise the
-/// program keeps on running).
-/// By defining LEMON_ASSERT_FAILURE or
-/// LEMON_ASSERT_EXCEPTION, you can set other behaviour to the
-/// assertions. In case LEMON_ASSERT_FAILURE is given, LEMON_ASSERT
-/// will always throw an \c AssertionFailedError exception with
-/// the \c msg error message. By using
-/// LEMON_ASSERT_EXCEPTION, one can define an arbitrary exception to be thrown.
-///
-/// The LEMON_ASSERT macro should be called with the \c exp parameter
-/// which should be an expression convertible to bool. If the given
-/// parameter is false the assertion is raised and one of the assertion
-/// behaviour will be activated. The \c msg should be either a const
-/// char* message or an exception. When the \c msg is an exception the
-/// \ref lemon::Exception::what() "what()" function is called to retrieve and
-/// display the error message.
-///
-/// \todo We should provide some way to reset to the default behaviour,
-/// shouldn't we?
-///
-/// \todo This whole 'assert' business should be placed in a separate
-/// include file. The boost assert is not guarded by header sentries
-/// which may help to change the behaviour of the assertions in
-/// the files.
-///
-/// \todo __PRETTY_FUNCTION__ should be replaced by something
-/// compiler-independent, like BOOST_CURRENT_FUNCTION
-
-# define LEMON_ASSERT(exp, msg) \
- (static_cast<void> (!!(exp) ? 0 : ( \
- LEMON_ASSERT_HANDLER(__FILE__, __LINE__, \
- __PRETTY_FUNCTION__, \
- msg, #exp, LEMON_ASSERT_DO_ABORT), 0)))
-
-#else
-# if defined LEMON_DISABLE_ASSERTS
-
-# define LEMON_ASSERT(exp, msg) (static_cast<void> (0))
-
-# else
-# define LEMON_ASSERT(exp, msg) \
- (static_cast<void> (!!(exp) ? 0 : ( \
- LEMON_ASSERT_HANDLER(__FILE__, __LINE__, \
- __PRETTY_FUNCTION__, \
- msg, #exp, LEMON_ASSERT_DO_ABORT), 0)))
-# endif
-#endif
-
-/**
- * \brief Macro for mark not yet implemented features.
- *
- * \todo Is this the right place for this? It should be used only in
- * modules under development.
- *
- * \todo __PRETTY_FUNCTION__ should be replaced by something
- * compiler-independent, like BOOST_CURRENT_FUNCTION
- */
-
-# define LEMON_FIXME(msg) \
- (LEMON_ASSERT_HANDLER(__FILE__, __LINE__, __PRETTY_FUNCTION__, \
- "FIXME: " msg))
diff --git a/src/lemon/fib_heap.h b/src/lemon/fib_heap.h
deleted file mode 100644
index c0d632b..0000000
--- a/src/lemon/fib_heap.h
+++ /dev/null
@@ -1,464 +0,0 @@
-/* -*- C++ -*-
- *
- * This file is a part of LEMON, a generic C++ optimization library
- *
- * Copyright (C) 2003-2008
- * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
- * (Egervary Research Group on Combinatorial Optimization, EGRES).
- *
- * Permission to use, modify and distribute this software is granted
- * provided that this copyright notice appears in all copies. For
- * precise terms see the accompanying LICENSE file.
- *
- * This software is provided "AS IS" with no warranty of any kind,
- * express or implied, and with no claim as to its suitability for any
- * purpose.
- *
- */
-
-#ifndef LEMON_FIB_HEAP_H
-#define LEMON_FIB_HEAP_H
-
-///\file
-///\ingroup auxdat
-///\brief Fibonacci Heap implementation.
-
-#include <vector>
-#include <functional>
-#include <lemon/math.h>
-
-namespace lemon {
-
- /// \ingroup auxdat
- ///
- ///\brief Fibonacci Heap.
- ///
- ///This class implements the \e Fibonacci \e heap data structure. A \e heap
- ///is a data structure for storing items with specified values called \e
- ///priorities in such a way that finding the item with minimum priority is
- ///efficient. \c Compare specifies the ordering of the priorities. In a heap
- ///one can change the priority of an item, add or erase an item, etc.
- ///
- ///The methods \ref increase and \ref erase are not efficient in a Fibonacci
- ///heap. In case of many calls to these operations, it is better to use a
- ///\ref BinHeap "binary heap".
- ///
- ///\param _Prio Type of the priority of the items.
- ///\param _ItemIntMap A read and writable Item int map, used internally
- ///to handle the cross references.
- ///\param _Compare A class for the ordering of the priorities. The
- ///default is \c std::less<_Prio>.
- ///
- ///\sa BinHeap
- ///\sa Dijkstra
- ///\author Jacint Szabo
-
-#ifdef DOXYGEN
- template <typename _Prio,
- typename _ItemIntMap,
- typename _Compare>
-#else
- template <typename _Prio,
- typename _ItemIntMap,
- typename _Compare = std::less<_Prio> >
-#endif
- class FibHeap {
- public:
- typedef _ItemIntMap ItemIntMap;
- typedef _Prio Prio;
- typedef typename ItemIntMap::Key Item;
- typedef std::pair<Item,Prio> Pair;
- typedef _Compare Compare;
-
- private:
- class store;
-
- std::vector<store> container;
- int minimum;
- ItemIntMap &iimap;
- Compare comp;
- int num_items;
-
- public:
- ///Status of the nodes
- enum State {
- ///The node is in the heap
- IN_HEAP = 0,
- ///The node has never been in the heap
- PRE_HEAP = -1,
- ///The node was in the heap but it got out of it
- POST_HEAP = -2
- };
-
- /// \brief The constructor
- ///
- /// \c _iimap should be given to the constructor, since it is
- /// used internally to handle the cross references.
- explicit FibHeap(ItemIntMap &_iimap)
- : minimum(0), iimap(_iimap), num_items() {}
-
- /// \brief The constructor
- ///
- /// \c _iimap should be given to the constructor, since it is used
- /// internally to handle the cross references. \c _comp is an
- /// object for ordering of the priorities.
- FibHeap(ItemIntMap &_iimap, const Compare &_comp)
- : minimum(0), iimap(_iimap), comp(_comp), num_items() {}
-
- /// \brief The number of items stored in the heap.
- ///
- /// Returns the number of items stored in the heap.
- int size() const { return num_items; }
-
- /// \brief Checks if the heap stores no items.
- ///
- /// Returns \c true if and only if the heap stores no items.
- bool empty() const { return num_items==0; }
-
- /// \brief Make empty this heap.
- ///
- /// Make empty this heap. It does not change the cross reference
- /// map. If you want to reuse a heap what is not surely empty you
- /// should first clear the heap and after that you should set the
- /// cross reference map for each item to \c PRE_HEAP.
- void clear() {
- container.clear(); minimum = 0; num_items = 0;
- }
-
- /// \brief \c item gets to the heap with priority \c value independently
- /// if \c item was already there.
- ///
- /// This method calls \ref push(\c item, \c value) if \c item is not
- /// stored in the heap and it calls \ref decrease(\c item, \c value) or
- /// \ref increase(\c item, \c value) otherwise.
- void set (const Item& item, const Prio& value) {
- int i=iimap[item];
- if ( i >= 0 && container[i].in ) {
- if ( comp(value, container[i].prio) ) decrease(item, value);
- if ( comp(container[i].prio, value) ) increase(item, value);
- } else push(item, value);
- }
-
- /// \brief Adds \c item to the heap with priority \c value.
- ///
- /// Adds \c item to the heap with priority \c value.
- /// \pre \c item must not be stored in the heap.
- void push (const Item& item, const Prio& value) {
- int i=iimap[item];
- if ( i < 0 ) {
- int s=container.size();
- iimap.set( item, s );
- store st;
- st.name=item;
- container.push_back(st);
- i=s;
- } else {
- container[i].parent=container[i].child=-1;
- container[i].degree=0;
- container[i].in=true;
- container[i].marked=false;
- }
-
- if ( num_items ) {
- container[container[minimum].right_neighbor].left_neighbor=i;
- container[i].right_neighbor=container[minimum].right_neighbor;
- container[minimum].right_neighbor=i;
- container[i].left_neighbor=minimum;
- if ( comp( value, container[minimum].prio) ) minimum=i;
- } else {
- container[i].right_neighbor=container[i].left_neighbor=i;
- minimum=i;
- }
- container[i].prio=value;
- ++num_items;
- }
-
- /// \brief Returns the item with minimum priority relative to \c Compare.
- ///
- /// This method returns the item with minimum priority relative to \c
- /// Compare.
- /// \pre The heap must be nonempty.
- Item top() const { return container[minimum].name; }
-
- /// \brief Returns the minimum priority relative to \c Compare.
- ///
- /// It returns the minimum priority relative to \c Compare.
- /// \pre The heap must be nonempty.
- const Prio& prio() const { return container[minimum].prio; }
-
- /// \brief Returns the priority of \c item.
- ///
- /// It returns the priority of \c item.
- /// \pre \c item must be in the heap.
- const Prio& operator[](const Item& item) const {
- return container[iimap[item]].prio;
- }
-
- /// \brief Deletes the item with minimum priority relative to \c Compare.
- ///
- /// This method deletes the item with minimum priority relative to \c
- /// Compare from the heap.
- /// \pre The heap must be non-empty.
- void pop() {
- /*The first case is that there are only one root.*/
- if ( container[minimum].left_neighbor==minimum ) {
- container[minimum].in=false;
- if ( container[minimum].degree!=0 ) {
- makeroot(container[minimum].child);
- minimum=container[minimum].child;
- balance();
- }
- } else {
- int right=container[minimum].right_neighbor;
- unlace(minimum);
- container[minimum].in=false;
- if ( container[minimum].degree > 0 ) {
- int left=container[minimum].left_neighbor;
- int child=container[minimum].child;
- int last_child=container[child].left_neighbor;
-
- makeroot(child);
-
- container[left].right_neighbor=child;
- container[child].left_neighbor=left;
- container[right].left_neighbor=last_child;
- container[last_child].right_neighbor=right;
- }
- minimum=right;
- balance();
- } // the case where there are more roots
- --num_items;
- }
-
- /// \brief Deletes \c item from the heap.
- ///
- /// This method deletes \c item from the heap, if \c item was already
- /// stored in the heap. It is quite inefficient in Fibonacci heaps.
- void erase (const Item& item) {
- int i=iimap[item];
-
- if ( i >= 0 && container[i].in ) {
- if ( container[i].parent!=-1 ) {
- int p=container[i].parent;
- cut(i,p);
- cascade(p);
- }
- minimum=i; //As if its prio would be -infinity
- pop();
- }
- }
-
- /// \brief Decreases the priority of \c item to \c value.
- ///
- /// This method decreases the priority of \c item to \c value.
- /// \pre \c item must be stored in the heap with priority at least \c
- /// value relative to \c Compare.
- void decrease (Item item, const Prio& value) {
- int i=iimap[item];
- container[i].prio=value;
- int p=container[i].parent;
-
- if ( p!=-1 && comp(value, container[p].prio) ) {
- cut(i,p);
- cascade(p);
- }
- if ( comp(value, container[minimum].prio) ) minimum=i;
- }
-
- /// \brief Increases the priority of \c item to \c value.
- ///
- /// This method sets the priority of \c item to \c value. Though
- /// there is no precondition on the priority of \c item, this
- /// method should be used only if it is indeed necessary to increase
- /// (relative to \c Compare) the priority of \c item, because this
- /// method is inefficient.
- void increase (Item item, const Prio& value) {
- erase(item);
- push(item, value);
- }
-
-
- /// \brief Returns if \c item is in, has already been in, or has never
- /// been in the heap.
- ///
- /// This method returns PRE_HEAP if \c item has never been in the
- /// heap, IN_HEAP if it is in the heap at the moment, and POST_HEAP
- /// otherwise. In the latter case it is possible that \c item will
- /// get back to the heap again.
- State state(const Item &item) const {
- int i=iimap[item];
- if( i>=0 ) {
- if ( container[i].in ) i=0;
- else i=-2;
- }
- return State(i);
- }
-
- /// \brief Sets the state of the \c item in the heap.
- ///
- /// Sets the state of the \c item in the heap. It can be used to
- /// manually clear the heap when it is important to achive the
- /// better time complexity.
- /// \param i The item.
- /// \param st The state. It should not be \c IN_HEAP.
- void state(const Item& i, State st) {
- switch (st) {
- case POST_HEAP:
- case PRE_HEAP:
- if (state(i) == IN_HEAP) {
- erase(i);
- }
- iimap[i] = st;
- break;
- case IN_HEAP:
- break;
- }
- }
-
- private:
-
- void balance() {
-
- int maxdeg=int( std::floor( 2.08*log(double(container.size()))))+1;
-
- std::vector<int> A(maxdeg,-1);
-
- /*
- *Recall that now minimum does not point to the minimum prio element.
- *We set minimum to this during balance().
- */
- int anchor=container[minimum].left_neighbor;
- int next=minimum;
- bool end=false;
-
- do {
- int active=next;
- if ( anchor==active ) end=true;
- int d=container[active].degree;
- next=container[active].right_neighbor;
-
- while (A[d]!=-1) {
- if( comp(container[active].prio, container[A[d]].prio) ) {
- fuse(active,A[d]);
- } else {
- fuse(A[d],active);
- active=A[d];
- }
- A[d]=-1;
- ++d;
- }
- A[d]=active;
- } while ( !end );
-
-
- while ( container[minimum].parent >=0 )
- minimum=container[minimum].parent;
- int s=minimum;
- int m=minimum;
- do {
- if ( comp(container[s].prio, container[minimum].prio) ) minimum=s;
- s=container[s].right_neighbor;
- } while ( s != m );
- }
-
- void makeroot(int c) {
- int s=c;
- do {
- container[s].parent=-1;
- s=container[s].right_neighbor;
- } while ( s != c );
- }
-
- void cut(int a, int b) {
- /*
- *Replacing a from the children of b.
- */
- --container[b].degree;
-
- if ( container[b].degree !=0 ) {
- int child=container[b].child;
- if ( child==a )
- container[b].child=container[child].right_neighbor;
- unlace(a);
- }
-
-
- /*Lacing a to the roots.*/
- int right=container[minimum].right_neighbor;
- container[minimum].right_neighbor=a;
- container[a].left_neighbor=minimum;
- container[a].right_neighbor=right;
- container[right].left_neighbor=a;
-
- container[a].parent=-1;
- container[a].marked=false;
- }
-
- void cascade(int a) {
- if ( container[a].parent!=-1 ) {
- int p=container[a].parent;
-
- if ( container[a].marked==false ) container[a].marked=true;
- else {
- cut(a,p);
- cascade(p);
- }
- }
- }
-
- void fuse(int a, int b) {
- unlace(b);
-
- /*Lacing b under a.*/
- container[b].parent=a;
-
- if (container[a].degree==0) {
- container[b].left_neighbor=b;
- container[b].right_neighbor=b;
- container[a].child=b;
- } else {
- int child=container[a].child;
- int last_child=container[child].left_neighbor;
- container[child].left_neighbor=b;
- container[b].right_neighbor=child;
- container[last_child].right_neighbor=b;
- container[b].left_neighbor=last_child;
- }
-
- ++container[a].degree;
-
- container[b].marked=false;
- }
-
- /*
- *It is invoked only if a has siblings.
- */
- void unlace(int a) {
- int leftn=container[a].left_neighbor;
- int rightn=container[a].right_neighbor;
- container[leftn].right_neighbor=rightn;
- container[rightn].left_neighbor=leftn;
- }
-
-
- class store {
- friend class FibHeap;
-
- Item name;
- int parent;
- int left_neighbor;
- int right_neighbor;
- int child;
- int degree;
- bool marked;
- bool in;
- Prio prio;
-
- store() : parent(-1), child(-1), degree(), marked(false), in(true) {}
- };
- };
-
-} //namespace lemon
-
-#endif //LEMON_FIB_HEAP_H
-
diff --git a/src/lemon/graph_adaptor.h b/src/lemon/graph_adaptor.h
deleted file mode 100644
index 66e75f1..0000000
--- a/src/lemon/graph_adaptor.h
+++ /dev/null
@@ -1,2720 +0,0 @@
-/* -*- C++ -*-
- *
- * This file is a part of LEMON, a generic C++ optimization library
- *
- * Copyright (C) 2003-2008
- * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
- * (Egervary Research Group on Combinatorial Optimization, EGRES).
- *
- * Permission to use, modify and distribute this software is granted
- * provided that this copyright notice appears in all copies. For
- * precise terms see the accompanying LICENSE file.
- *
- * This software is provided "AS IS" with no warranty of any kind,
- * express or implied, and with no claim as to its suitability for any
- * purpose.
- *
- */
-
-#ifndef LEMON_GRAPH_ADAPTOR_H
-#define LEMON_GRAPH_ADAPTOR_H
-
-///\ingroup graph_adaptors
-///\file
-///\brief Several graph adaptors.
-///
-///This file contains several useful graph adaptor functions.
-///
-///\author Marton Makai and Balazs Dezso
-
-#include <lemon/bits/invalid.h>
-#include <lemon/bits/variant.h>
-#include <lemon/maps.h>
-
-#include <lemon/bits/base_extender.h>
-#include <lemon/bits/graph_adaptor_extender.h>
-#include <lemon/bits/graph_extender.h>
-#include <lemon/tolerance.h>
-
-#include <algorithm>
-
-namespace lemon {
-
- ///\brief Base type for the Graph Adaptors
- ///
- ///Base type for the Graph Adaptors
- ///
- ///This is the base type for most of LEMON graph adaptors.
- ///This class implements a trivial graph adaptor i.e. it only wraps the
- ///functions and types of the graph. The purpose of this class is to
- ///make easier implementing graph adaptors. E.g. if an adaptor is
- ///considered which differs from the wrapped graph only in some of its
- ///functions or types, then it can be derived from GraphAdaptor,
- ///and only the
- ///differences should be implemented.
- ///
- ///author Marton Makai
- template<typename _Graph>
- class GraphAdaptorBase {
- public:
- typedef _Graph Graph;
- typedef GraphAdaptorBase Adaptor;
- typedef Graph ParentGraph;
-
- protected:
- Graph* graph;
- GraphAdaptorBase() : graph(0) { }
- void setGraph(Graph& _graph) { graph=&_graph; }
-
- public:
- GraphAdaptorBase(Graph& _graph) : graph(&_graph) { }
-
- typedef typename Graph::Node Node;
- typedef typename Graph::Edge Edge;
-
- void first(Node& i) const { graph->first(i); }
- void first(Edge& i) const { graph->first(i); }
- void firstIn(Edge& i, const Node& n) const { graph->firstIn(i, n); }
- void firstOut(Edge& i, const Node& n ) const { graph->firstOut(i, n); }
-
- void next(Node& i) const { graph->next(i); }
- void next(Edge& i) const { graph->next(i); }
- void nextIn(Edge& i) const { graph->nextIn(i); }
- void nextOut(Edge& i) const { graph->nextOut(i); }
-
- Node source(const Edge& e) const { return graph->source(e); }
- Node target(const Edge& e) const { return graph->target(e); }
-
- typedef NodeNumTagIndicator<Graph> NodeNumTag;
- int nodeNum() const { return graph->nodeNum(); }
-
- typedef EdgeNumTagIndicator<Graph> EdgeNumTag;
- int edgeNum() const { return graph->edgeNum(); }
-
- typedef FindEdgeTagIndicator<Graph> FindEdgeTag;
- Edge findEdge(const Node& u, const Node& v,
- const Edge& prev = INVALID) {
- return graph->findEdge(u, v, prev);
- }
-
- Node addNode() const {
- return Node(graph->addNode());
- }
-
- Edge addEdge(const Node& u, const Node& v) const {
- return Edge(graph->addEdge(u, v));
- }
-
- void erase(const Node& i) const { graph->erase(i); }
- void erase(const Edge& i) const { graph->erase(i); }
-
- void clear() const { graph->clear(); }
-
- int id(const Node& v) const { return graph->id(v); }
- int id(const Edge& e) const { return graph->id(e); }
-
- Node fromNodeId(int ix) const {
- return graph->fromNodeId(ix);
- }
-
- Edge fromEdgeId(int ix) const {
- return graph->fromEdgeId(ix);
- }
-
- int maxNodeId() const {
- return graph->maxNodeId();
- }
-
- int maxEdgeId() const {
- return graph->maxEdgeId();
- }
-
- typedef typename ItemSetTraits<Graph, Node>::ItemNotifier NodeNotifier;
-
- NodeNotifier& notifier(Node) const {
- return graph->notifier(Node());
- }
-
- typedef typename ItemSetTraits<Graph, Edge>::ItemNotifier EdgeNotifier;
-
- EdgeNotifier& notifier(Edge) const {
- return graph->notifier(Edge());
- }
-
- template <typename _Value>
- class NodeMap : public Graph::template NodeMap<_Value> {
- public:
-
- typedef typename Graph::template NodeMap<_Value> Parent;
-
- explicit NodeMap(const Adaptor& ga)
- : Parent(*ga.graph) {}
-
- NodeMap(const Adaptor& ga, const _Value& value)
- : Parent(*ga.graph, value) { }
-
- NodeMap& operator=(const NodeMap& cmap) {
- return operator=<NodeMap>(cmap);
- }
-
- template <typename CMap>
- NodeMap& operator=(const CMap& cmap) {
- Parent::operator=(cmap);
- return *this;
- }
-
- };
-
- template <typename _Value>
- class EdgeMap : public Graph::template EdgeMap<_Value> {
- public:
-
- typedef typename Graph::template EdgeMap<_Value> Parent;
-
- explicit EdgeMap(const Adaptor& ga)
- : Parent(*ga.graph) {}
-
- EdgeMap(const Adaptor& ga, const _Value& value)
- : Parent(*ga.graph, value) {}
-
- EdgeMap& operator=(const EdgeMap& cmap) {
- return operator=<EdgeMap>(cmap);
- }
-
- template <typename CMap>
- EdgeMap& operator=(const CMap& cmap) {
- Parent::operator=(cmap);
- return *this;
- }
-
- };
-
- };
-
- ///\ingroup graph_adaptors
- ///
- ///\brief Trivial Graph Adaptor
- ///
- /// This class is an adaptor which does not change the adapted graph.
- /// It can be used only to test the graph adaptors.
- template <typename _Graph>
- class GraphAdaptor :
- public GraphAdaptorExtender<GraphAdaptorBase<_Graph> > {
- public:
- typedef _Graph Graph;
- typedef GraphAdaptorExtender<GraphAdaptorBase<_Graph> > Parent;
- protected:
- GraphAdaptor() : Parent() { }
-
- public:
- explicit GraphAdaptor(Graph& _graph) { setGraph(_graph); }
- };
-
- /// \brief Just gives back a graph adaptor
- ///
- /// Just gives back a graph adaptor which
- /// should be provide original graph
- template<typename Graph>
- GraphAdaptor<const Graph>
- graphAdaptor(const Graph& graph) {
- return GraphAdaptor<const Graph>(graph);
- }
-
-
- template <typename _Graph>
- class RevGraphAdaptorBase : public GraphAdaptorBase<_Graph> {
- public:
- typedef _Graph Graph;
- typedef GraphAdaptorBase<_Graph> Parent;
- protected:
- RevGraphAdaptorBase() : Parent() { }
- public:
- typedef typename Parent::Node Node;
- typedef typename Parent::Edge Edge;
-
- void firstIn(Edge& i, const Node& n) const { Parent::firstOut(i, n); }
- void firstOut(Edge& i, const Node& n ) const { Parent::firstIn(i, n); }
-
- void nextIn(Edge& i) const { Parent::nextOut(i); }
- void nextOut(Edge& i) const { Parent::nextIn(i); }
-
- Node source(const Edge& e) const { return Parent::target(e); }
- Node target(const Edge& e) const { return Parent::source(e); }
-
- typedef FindEdgeTagIndicator<Graph> FindEdgeTag;
- Edge findEdge(const Node& u, const Node& v,
- const Edge& prev = INVALID) {
- return Parent::findEdge(v, u, prev);
- }
-
- };
-
-
- ///\ingroup graph_adaptors
- ///
- ///\brief A graph adaptor which reverses the orientation of the edges.
- ///
- /// If \c g is defined as
- ///\code
- /// ListGraph g;
- ///\endcode
- /// then
- ///\code
- /// RevGraphAdaptor<ListGraph> ga(g);
- ///\endcode
- /// implements the graph obtained from \c g by
- /// reversing the orientation of its edges.
- ///
- /// A good example of using RevGraphAdaptor is to decide that the
- /// directed graph is wheter strongly connected or not. If from one
- /// node each node is reachable and from each node is reachable this
- /// node then and just then the graph is strongly connected. Instead of
- /// this condition we use a little bit different. From one node each node
- /// ahould be reachable in the graph and in the reversed graph. Now this
- /// condition can be checked with the Dfs algorithm class and the
- /// RevGraphAdaptor algorithm class.
- ///
- /// And look at the code:
- ///
- ///\code
- /// bool stronglyConnected(const Graph& graph) {
- /// if (NodeIt(graph) == INVALID) return true;
- /// Dfs<Graph> dfs(graph);
- /// dfs.run(NodeIt(graph));
- /// for (NodeIt it(graph); it != INVALID; ++it) {
- /// if (!dfs.reached(it)) {
- /// return false;
- /// }
- /// }
- /// typedef RevGraphAdaptor<const Graph> RGraph;
- /// RGraph rgraph(graph);
- /// DfsVisit<RGraph> rdfs(rgraph);
- /// rdfs.run(NodeIt(graph));
- /// for (NodeIt it(graph); it != INVALID; ++it) {
- /// if (!rdfs.reached(it)) {
- /// return false;
- /// }
- /// }
- /// return true;
- /// }
- ///\endcode
- template<typename _Graph>
- class RevGraphAdaptor :
- public GraphAdaptorExtender<RevGraphAdaptorBase<_Graph> > {
- public:
- typedef _Graph Graph;
- typedef GraphAdaptorExtender<
- RevGraphAdaptorBase<_Graph> > Parent;
- protected:
- RevGraphAdaptor() { }
- public:
- explicit RevGraphAdaptor(_Graph& _graph) { setGraph(_graph); }
- };
-
- /// \brief Just gives back a reverse graph adaptor
- ///
- /// Just gives back a reverse graph adaptor
- template<typename Graph>
- RevGraphAdaptor<const Graph>
- revGraphAdaptor(const Graph& graph) {
- return RevGraphAdaptor<const Graph>(graph);
- }
-
- template <typename _Graph, typename NodeFilterMap,
- typename EdgeFilterMap, bool checked = true>
- class SubGraphAdaptorBase : public GraphAdaptorBase<_Graph> {
- public:
- typedef _Graph Graph;
- typedef SubGraphAdaptorBase Adaptor;
- typedef GraphAdaptorBase<_Graph> Parent;
- protected:
- NodeFilterMap* node_filter_map;
- EdgeFilterMap* edge_filter_map;
- SubGraphAdaptorBase() : Parent(),
- node_filter_map(0), edge_filter_map(0) { }
-
- void setNodeFilterMap(NodeFilterMap& _node_filter_map) {
- node_filter_map=&_node_filter_map;
- }
- void setEdgeFilterMap(EdgeFilterMap& _edge_filter_map) {
- edge_filter_map=&_edge_filter_map;
- }
-
- public:
-
- typedef typename Parent::Node Node;
- typedef typename Parent::Edge Edge;
-
- void first(Node& i) const {
- Parent::first(i);
- while (i!=INVALID && !(*node_filter_map)[i]) Parent::next(i);
- }
-
- void first(Edge& i) const {
- Parent::first(i);
- while (i!=INVALID && (!(*edge_filter_map)[i]
- || !(*node_filter_map)[Parent::source(i)]
- || !(*node_filter_map)[Parent::target(i)])) Parent::next(i);
- }
-
- void firstIn(Edge& i, const Node& n) const {
- Parent::firstIn(i, n);
- while (i!=INVALID && (!(*edge_filter_map)[i]
- || !(*node_filter_map)[Parent::source(i)])) Parent::nextIn(i);
- }
-
- void firstOut(Edge& i, const Node& n) const {
- Parent::firstOut(i, n);
- while (i!=INVALID && (!(*edge_filter_map)[i]
- || !(*node_filter_map)[Parent::target(i)])) Parent::nextOut(i);
- }
-
- void next(Node& i) const {
- Parent::next(i);
- while (i!=INVALID && !(*node_filter_map)[i]) Parent::next(i);
- }
-
- void next(Edge& i) const {
- Parent::next(i);
- while (i!=INVALID && (!(*edge_filter_map)[i]
- || !(*node_filter_map)[Parent::source(i)]
- || !(*node_filter_map)[Parent::target(i)])) Parent::next(i);
- }
-
- void nextIn(Edge& i) const {
- Parent::nextIn(i);
- while (i!=INVALID && (!(*edge_filter_map)[i]
- || !(*node_filter_map)[Parent::source(i)])) Parent::nextIn(i);
- }
-
- void nextOut(Edge& i) const {
- Parent::nextOut(i);
- while (i!=INVALID && (!(*edge_filter_map)[i]
- || !(*node_filter_map)[Parent::target(i)])) Parent::nextOut(i);
- }
-
- ///\e
-
- /// This function hides \c n in the graph, i.e. the iteration
- /// jumps over it. This is done by simply setting the value of \c n
- /// to be false in the corresponding node-map.
- void hide(const Node& n) const { node_filter_map->set(n, false); }
-
- ///\e
-
- /// This function hides \c e in the graph, i.e. the iteration
- /// jumps over it. This is done by simply setting the value of \c e
- /// to be false in the corresponding edge-map.
- void hide(const Edge& e) const { edge_filter_map->set(e, false); }
-
- ///\e
-
- /// The value of \c n is set to be true in the node-map which stores
- /// hide information. If \c n was hidden previuosly, then it is shown
- /// again
- void unHide(const Node& n) const { node_filter_map->set(n, true); }
-
- ///\e
-
- /// The value of \c e is set to be true in the edge-map which stores
- /// hide information. If \c e was hidden previuosly, then it is shown
- /// again
- void unHide(const Edge& e) const { edge_filter_map->set(e, true); }
-
- /// Returns true if \c n is hidden.
-
- ///\e
- ///
- bool hidden(const Node& n) const { return !(*node_filter_map)[n]; }
-
- /// Returns true if \c n is hidden.
-
- ///\e
- ///
- bool hidden(const Edge& e) const { return !(*edge_filter_map)[e]; }
-
- typedef False NodeNumTag;
- typedef False EdgeNumTag;
-
- typedef FindEdgeTagIndicator<Graph> FindEdgeTag;
- Edge findEdge(const Node& source, const Node& target,
- const Edge& prev = INVALID) {
- if (!(*node_filter_map)[source] || !(*node_filter_map)[target]) {
- return INVALID;
- }
- Edge edge = Parent::findEdge(source, target, prev);
- while (edge != INVALID && !(*edge_filter_map)[edge]) {
- edge = Parent::findEdge(source, target, edge);
- }
- return edge;
- }
-
- template <typename _Value>
- class NodeMap
- : public SubMapExtender<Adaptor,
- typename Parent::template NodeMap<_Value> >
- {
- public:
- typedef Adaptor Graph;
- //typedef SubMapExtender<Adaptor, typename Parent::
- // template NodeMap<_Value> > Parent;
-
- NodeMap(const Graph& g)
- : Parent(g) {}
- NodeMap(const Graph& g, const _Value& v)
- : Parent(g, v) {}
-
- NodeMap& operator=(const NodeMap& cmap) {
- return operator=<NodeMap>(cmap);
- }
-
- template <typename CMap>
- NodeMap& operator=(const CMap& cmap) {
- Parent::operator=(cmap);
- return *this;
- }
- };
-
- template <typename _Value>
- class EdgeMap
- : public SubMapExtender<Adaptor,
- typename Parent::template EdgeMap<_Value> >
- {
- public:
- typedef Adaptor Graph;
- //typedef SubMapExtender<Adaptor, typename Parent::
- // template EdgeMap<_Value> > Parent;
-
- EdgeMap(const Graph& g)
- : Parent(g) {}
- EdgeMap(const Graph& g, const _Value& v)
- : Parent(g, v) {}
-
- EdgeMap& operator=(const EdgeMap& cmap) {
- return operator=<EdgeMap>(cmap);
- }
-
- template <typename CMap>
- EdgeMap& operator=(const CMap& cmap) {
- Parent::operator=(cmap);
- return *this;
- }
- };
-
- };
-
- template <typename _Graph, typename NodeFilterMap, typename EdgeFilterMap>
- class SubGraphAdaptorBase<_Graph, NodeFilterMap, EdgeFilterMap, false>
- : public GraphAdaptorBase<_Graph> {
- public:
- typedef _Graph Graph;
- typedef SubGraphAdaptorBase Adaptor;
- typedef GraphAdaptorBase<_Graph> Parent;
- protected:
- NodeFilterMap* node_filter_map;
- EdgeFilterMap* edge_filter_map;
- SubGraphAdaptorBase() : Parent(),
- node_filter_map(0), edge_filter_map(0) { }
-
- void setNodeFilterMap(NodeFilterMap& _node_filter_map) {
- node_filter_map=&_node_filter_map;
- }
- void setEdgeFilterMap(EdgeFilterMap& _edge_filter_map) {
- edge_filter_map=&_edge_filter_map;
- }
-
- public:
-
- typedef typename Parent::Node Node;
- typedef typename Parent::Edge Edge;
-
- void first(Node& i) const {
- Parent::first(i);
- while (i!=INVALID && !(*node_filter_map)[i]) Parent::next(i);
- }
-
- void first(Edge& i) const {
- Parent::first(i);
- while (i!=INVALID && !(*edge_filter_map)[i]) Parent::next(i);
- }
-
- void firstIn(Edge& i, const Node& n) const {
- Parent::firstIn(i, n);
- while (i!=INVALID && !(*edge_filter_map)[i]) Parent::nextIn(i);
- }
-
- void firstOut(Edge& i, const Node& n) const {
- Parent::firstOut(i, n);
- while (i!=INVALID && !(*edge_filter_map)[i]) Parent::nextOut(i);
- }
-
- void next(Node& i) const {
- Parent::next(i);
- while (i!=INVALID && !(*node_filter_map)[i]) Parent::next(i);
- }
- void next(Edge& i) const {
- Parent::next(i);
- while (i!=INVALID && !(*edge_filter_map)[i]) Parent::next(i);
- }
- void nextIn(Edge& i) const {
- Parent::nextIn(i);
- while (i!=INVALID && !(*edge_filter_map)[i]) Parent::nextIn(i);
- }
-
- void nextOut(Edge& i) const {
- Parent::nextOut(i);
- while (i!=INVALID && !(*edge_filter_map)[i]) Parent::nextOut(i);
- }
-
- ///\e
-
- /// This function hides \c n in the graph, i.e. the iteration
- /// jumps over it. This is done by simply setting the value of \c n
- /// to be false in the corresponding node-map.
- void hide(const Node& n) const { node_filter_map->set(n, false); }
-
- ///\e
-
- /// This function hides \c e in the graph, i.e. the iteration
- /// jumps over it. This is done by simply setting the value of \c e
- /// to be false in the corresponding edge-map.
- void hide(const Edge& e) const { edge_filter_map->set(e, false); }
-
- ///\e
-
- /// The value of \c n is set to be true in the node-map which stores
- /// hide information. If \c n was hidden previuosly, then it is shown
- /// again
- void unHide(const Node& n) const { node_filter_map->set(n, true); }
-
- ///\e
-
- /// The value of \c e is set to be true in the edge-map which stores
- /// hide information. If \c e was hidden previuosly, then it is shown
- /// again
- void unHide(const Edge& e) const { edge_filter_map->set(e, true); }
-
- /// Returns true if \c n is hidden.
-
- ///\e
- ///
- bool hidden(const Node& n) const { return !(*node_filter_map)[n]; }
-
- /// Returns true if \c n is hidden.
-
- ///\e
- ///
- bool hidden(const Edge& e) const { return !(*edge_filter_map)[e]; }
-
- typedef False NodeNumTag;
- typedef False EdgeNumTag;
-
- typedef FindEdgeTagIndicator<Graph> FindEdgeTag;
- Edge findEdge(const Node& source, const Node& target,
- const Edge& prev = INVALID) {
- if (!(*node_filter_map)[source] || !(*node_filter_map)[target]) {
- return INVALID;
- }
- Edge edge = Parent::findEdge(source, target, prev);
- while (edge != INVALID && !(*edge_filter_map)[edge]) {
- edge = Parent::findEdge(source, target, edge);
- }
- return edge;
- }
-
- template <typename _Value>
- class NodeMap
- : public SubMapExtender<Adaptor,
- typename Parent::template NodeMap<_Value> >
- {
- public:
- typedef Adaptor Graph;
- //typedef SubMapExtender<Adaptor, typename Parent::
- // template NodeMap<_Value> > Parent;
-
- NodeMap(const Graph& g)
- : Parent(g) {}
- NodeMap(const Graph& g, const _Value& v)
- : Parent(g, v) {}
-
- NodeMap& operator=(const NodeMap& cmap) {
- return operator=<NodeMap>(cmap);
- }
-
- template <typename CMap>
- NodeMap& operator=(const CMap& cmap) {
- Parent::operator=(cmap);
- return *this;
- }
- };
-
- template <typename _Value>
- class EdgeMap
- : public SubMapExtender<Adaptor,
- typename Parent::template EdgeMap<_Value> >
- {
- public:
- typedef Adaptor Graph;
- //typedef SubMapExtender<Adaptor, typename Parent::
- // template EdgeMap<_Value> > Parent;
-
- EdgeMap(const Graph& g)
- : Parent(g) {}
- EdgeMap(const Graph& g, const _Value& v)
- : Parent(g, v) {}
-
- EdgeMap& operator=(const EdgeMap& cmap) {
- return operator=<EdgeMap>(cmap);
- }
-
- template <typename CMap>
- EdgeMap& operator=(const CMap& cmap) {
- Parent::operator=(cmap);
- return *this;
- }
- };
-
- };
-
- /// \ingroup graph_adaptors
- ///
- /// \brief A graph adaptor for hiding nodes and edges from a graph.
- ///
- /// SubGraphAdaptor shows the graph with filtered node-set and
- /// edge-set. If the \c checked parameter is true then it filters the edgeset
- /// to do not get invalid edges without source or target.
- /// Let \f$ G=(V, A) \f$ be a directed graph
- /// and suppose that the graph instance \c g of type ListGraph
- /// implements \f$ G \f$.
- /// Let moreover \f$ b_V \f$ and \f$ b_A \f$ be bool-valued functions resp.
- /// on the node-set and edge-set.
- /// SubGraphAdaptor<...>::NodeIt iterates
- /// on the node-set \f$ \{v\in V : b_V(v)=true\} \f$ and
- /// SubGraphAdaptor<...>::EdgeIt iterates
- /// on the edge-set \f$ \{e\in A : b_A(e)=true\} \f$. Similarly,
- /// SubGraphAdaptor<...>::OutEdgeIt and
- /// SubGraphAdaptor<...>::InEdgeIt iterates
- /// only on edges leaving and entering a specific node which have true value.
- ///
- /// If the \c checked template parameter is false then we have to note that
- /// the node-iterator cares only the filter on the node-set, and the
- /// edge-iterator cares only the filter on the edge-set.
- /// This way the edge-map
- /// should filter all edges which's source or target is filtered by the
- /// node-filter.
- ///\code
- /// typedef ListGraph Graph;
- /// Graph g;
- /// typedef Graph::Node Node;
- /// typedef Graph::Edge Edge;
- /// Node u=g.addNode(); //node of id 0
- /// Node v=g.addNode(); //node of id 1
- /// Node e=g.addEdge(u, v); //edge of id 0
- /// Node f=g.addEdge(v, u); //edge of id 1
- /// Graph::NodeMap<bool> nm(g, true);
- /// nm.set(u, false);
- /// Graph::EdgeMap<bool> em(g, true);
- /// em.set(e, false);
- /// typedef SubGraphAdaptor<Graph, Graph::NodeMap<bool>, Graph::EdgeMap<bool> > SubGA;
- /// SubGA ga(g, nm, em);
- /// for (SubGA::NodeIt n(ga); n!=INVALID; ++n) std::cout << g.id(n) << std::endl;
- /// std::cout << ":-)" << std::endl;
- /// for (SubGA::EdgeIt e(ga); e!=INVALID; ++e) std::cout << g.id(e) << std::endl;
- ///\endcode
- /// The output of the above code is the following.
- ///\code
- /// 1
- /// :-)
- /// 1
- ///\endcode
- /// Note that \c n is of type \c SubGA::NodeIt, but it can be converted to
- /// \c Graph::Node that is why \c g.id(n) can be applied.
- ///
- /// For other examples see also the documentation of NodeSubGraphAdaptor and
- /// EdgeSubGraphAdaptor.
- ///
- /// \author Marton Makai
-
- template<typename _Graph, typename NodeFilterMap,
- typename EdgeFilterMap, bool checked = true>
- class SubGraphAdaptor :
- public GraphAdaptorExtender<
- SubGraphAdaptorBase<_Graph, NodeFilterMap, EdgeFilterMap, checked> > {
- public:
- typedef _Graph Graph;
- typedef GraphAdaptorExtender< SubGraphAdaptorBase<_Graph, NodeFilterMap,
- EdgeFilterMap, checked> >
- Parent;
-
- protected:
- SubGraphAdaptor() { }
- public:
-
- SubGraphAdaptor(_Graph& _graph, NodeFilterMap& _node_filter_map,
- EdgeFilterMap& _edge_filter_map) {
- setGraph(_graph);
- setNodeFilterMap(_node_filter_map);
- setEdgeFilterMap(_edge_filter_map);
- }
-
- };
-
- /// \brief Just gives back a sub graph adaptor
- ///
- /// Just gives back a sub graph adaptor
- template<typename Graph, typename NodeFilterMap, typename EdgeFilterMap>
- SubGraphAdaptor<const Graph, NodeFilterMap, EdgeFilterMap>
- subGraphAdaptor(const Graph& graph,
- NodeFilterMap& nfm, EdgeFilterMap& efm) {
- return SubGraphAdaptor<const Graph, NodeFilterMap, EdgeFilterMap>
- (graph, nfm, efm);
- }
-
- template<typename Graph, typename NodeFilterMap, typename EdgeFilterMap>
- SubGraphAdaptor<const Graph, const NodeFilterMap, EdgeFilterMap>
- subGraphAdaptor(const Graph& graph,
- NodeFilterMap& nfm, EdgeFilterMap& efm) {
- return SubGraphAdaptor<const Graph, const NodeFilterMap, EdgeFilterMap>
- (graph, nfm, efm);
- }
-
- template<typename Graph, typename NodeFilterMap, typename EdgeFilterMap>
- SubGraphAdaptor<const Graph, NodeFilterMap, const EdgeFilterMap>
- subGraphAdaptor(const Graph& graph,
- NodeFilterMap& nfm, EdgeFilterMap& efm) {
- return SubGraphAdaptor<const Graph, NodeFilterMap, const EdgeFilterMap>
- (graph, nfm, efm);
- }
-
- template<typename Graph, typename NodeFilterMap, typename EdgeFilterMap>
- SubGraphAdaptor<const Graph, const NodeFilterMap, const EdgeFilterMap>
- subGraphAdaptor(const Graph& graph,
- NodeFilterMap& nfm, EdgeFilterMap& efm) {
- return SubGraphAdaptor<const Graph, const NodeFilterMap,
- const EdgeFilterMap>(graph, nfm, efm);
- }
-
-
-
- ///\ingroup graph_adaptors
- ///
- ///\brief An adaptor for hiding nodes from a graph.
- ///
- ///An adaptor for hiding nodes from a graph.
- ///This adaptor specializes SubGraphAdaptor in the way that only
- ///the node-set
- ///can be filtered. In usual case the checked parameter is true, we get the
- ///induced subgraph. But if the checked parameter is false then we can
- ///filter only isolated nodes.
- ///\author Marton Makai
- template<typename Graph, typename NodeFilterMap, bool checked = true>
- class NodeSubGraphAdaptor :
- public SubGraphAdaptor<Graph, NodeFilterMap,
- ConstMap<typename Graph::Edge,bool>, checked> {
- public:
-
- typedef SubGraphAdaptor<Graph, NodeFilterMap,
- ConstMap<typename Graph::Edge,bool>, checked >
- Parent;
-
- protected:
- ConstMap<typename Graph::Edge, bool> const_true_map;
-
- NodeSubGraphAdaptor() : const_true_map(true) {
- Parent::setEdgeFilterMap(const_true_map);
- }
-
- public:
-
- NodeSubGraphAdaptor(Graph& _graph, NodeFilterMap& _node_filter_map) :
- Parent(), const_true_map(true) {
- Parent::setGraph(_graph);
- Parent::setNodeFilterMap(_node_filter_map);
- Parent::setEdgeFilterMap(const_true_map);
- }
-
- };
-
-
- /// \brief Just gives back a node sub graph adaptor
- ///
- /// Just gives back a node sub graph adaptor
- template<typename Graph, typename NodeFilterMap>
- NodeSubGraphAdaptor<const Graph, NodeFilterMap>
- nodeSubGraphAdaptor(const Graph& graph, NodeFilterMap& nfm) {
- return NodeSubGraphAdaptor<const Graph, NodeFilterMap>(graph, nfm);
- }
-
- template<typename Graph, typename NodeFilterMap>
- NodeSubGraphAdaptor<const Graph, const NodeFilterMap>
- nodeSubGraphAdaptor(const Graph& graph, const NodeFilterMap& nfm) {
- return NodeSubGraphAdaptor<const Graph, const NodeFilterMap>(graph, nfm);
- }
-
- ///\ingroup graph_adaptors
- ///
- ///\brief An adaptor for hiding edges from a graph.
- ///
- ///An adaptor for hiding edges from a graph.
- ///This adaptor specializes SubGraphAdaptor in the way that
- ///only the edge-set
- ///can be filtered. The usefulness of this adaptor is demonstrated in the
- ///problem of searching a maximum number of edge-disjoint shortest paths
- ///between
- ///two nodes \c s and \c t. Shortest here means being shortest w.r.t.
- ///non-negative edge-lengths. Note that
- ///the comprehension of the presented solution
- ///need's some elementary knowledge from combinatorial optimization.
- ///
- ///If a single shortest path is to be
- ///searched between \c s and \c t, then this can be done easily by
- ///applying the Dijkstra algorithm. What happens, if a maximum number of
- ///edge-disjoint shortest paths is to be computed. It can be proved that an
- ///edge can be in a shortest path if and only
- ///if it is tight with respect to
- ///the potential function computed by Dijkstra.
- ///Moreover, any path containing
- ///only such edges is a shortest one.
- ///Thus we have to compute a maximum number
- ///of edge-disjoint paths between \c s and \c t in
- ///the graph which has edge-set
- ///all the tight edges. The computation will be demonstrated
- ///on the following
- ///graph, which is read from the dimacs file \c sub_graph_adaptor_demo.dim.
- ///The full source code is available in \ref sub_graph_adaptor_demo.cc.
- ///If you are interested in more demo programs, you can use
- ///\ref dim_to_dot.cc to generate .dot files from dimacs files.
- ///The .dot file of the following figure was generated by
- ///the demo program \ref dim_to_dot.cc.
- ///
- ///\dot
- ///digraph lemon_dot_example {
- ///node [ shape=ellipse, fontname=Helvetica, fontsize=10 ];
- ///n0 [ label="0 (s)" ];
- ///n1 [ label="1" ];
- ///n2 [ label="2" ];
- ///n3 [ label="3" ];
- ///n4 [ label="4" ];
- ///n5 [ label="5" ];
- ///n6 [ label="6 (t)" ];
- ///edge [ shape=ellipse, fontname=Helvetica, fontsize=10 ];
- ///n5 -> n6 [ label="9, length:4" ];
- ///n4 -> n6 [ label="8, length:2" ];
- ///n3 -> n5 [ label="7, length:1" ];
- ///n2 -> n5 [ label="6, length:3" ];
- ///n2 -> n6 [ label="5, length:5" ];
- ///n2 -> n4 [ label="4, length:2" ];
- ///n1 -> n4 [ label="3, length:3" ];
- ///n0 -> n3 [ label="2, length:1" ];
- ///n0 -> n2 [ label="1, length:2" ];
- ///n0 -> n1 [ label="0, length:3" ];
- ///}
- ///\enddot
- ///
- ///\code
- ///Graph g;
- ///Node s, t;
- ///LengthMap length(g);
- ///
- ///readDimacs(std::cin, g, length, s, t);
- ///
- ///cout << "edges with lengths (of form id, source--length->target): " << endl;
- ///for(EdgeIt e(g); e!=INVALID; ++e)
- /// cout << g.id(e) << ", " << g.id(g.source(e)) << "--"
- /// << length[e] << "->" << g.id(g.target(e)) << endl;
- ///
- ///cout << "s: " << g.id(s) << " t: " << g.id(t) << endl;
- ///\endcode
- ///Next, the potential function is computed with Dijkstra.
- ///\code
- ///typedef Dijkstra<Graph, LengthMap> Dijkstra;
- ///Dijkstra dijkstra(g, length);
- ///dijkstra.run(s);
- ///\endcode
- ///Next, we consrtruct a map which filters the edge-set to the tight edges.
- ///\code
- ///typedef TightEdgeFilterMap<Graph, const Dijkstra::DistMap, LengthMap>
- /// TightEdgeFilter;
- ///TightEdgeFilter tight_edge_filter(g, dijkstra.distMap(), length);
- ///
- ///typedef EdgeSubGraphAdaptor<Graph, TightEdgeFilter> SubGA;
- ///SubGA ga(g, tight_edge_filter);
- ///\endcode
- ///Then, the maximum nimber of edge-disjoint \c s-\c t paths are computed
- ///with a max flow algorithm Preflow.
- ///\code
- ///ConstMap<Edge, int> const_1_map(1);
- ///Graph::EdgeMap<int> flow(g, 0);
- ///
- ///Preflow<SubGA, ConstMap<Edge, int>, Graph::EdgeMap<int> >
- /// preflow(ga, const_1_map, s, t);
- ///preflow.run();
- ///\endcode
- ///Last, the output is:
- ///\code
- ///cout << "maximum number of edge-disjoint shortest path: "
- /// << preflow.flowValue() << endl;
- ///cout << "edges of the maximum number of edge-disjoint shortest s-t paths: "
- /// << endl;
- ///for(EdgeIt e(g); e!=INVALID; ++e)
- /// if (preflow.flow(e))
- /// cout << " " << g.id(g.source(e)) << "--"
- /// << length[e] << "->" << g.id(g.target(e)) << endl;
- ///\endcode
- ///The program has the following (expected :-)) output:
- ///\code
- ///edges with lengths (of form id, source--length->target):
- /// 9, 5--4->6
- /// 8, 4--2->6
- /// 7, 3--1->5
- /// 6, 2--3->5
- /// 5, 2--5->6
- /// 4, 2--2->4
- /// 3, 1--3->4
- /// 2, 0--1->3
- /// 1, 0--2->2
- /// 0, 0--3->1
- ///s: 0 t: 6
- ///maximum number of edge-disjoint shortest path: 2
- ///edges of the maximum number of edge-disjoint shortest s-t paths:
- /// 9, 5--4->6
- /// 8, 4--2->6
- /// 7, 3--1->5
- /// 4, 2--2->4
- /// 2, 0--1->3
- /// 1, 0--2->2
- ///\endcode
- ///
- ///\author Marton Makai
- template<typename Graph, typename EdgeFilterMap>
- class EdgeSubGraphAdaptor :
- public SubGraphAdaptor<Graph, ConstMap<typename Graph::Node,bool>,
- EdgeFilterMap, false> {
- public:
- typedef SubGraphAdaptor<Graph, ConstMap<typename Graph::Node,bool>,
- EdgeFilterMap, false> Parent;
- protected:
- ConstMap<typename Graph::Node, bool> const_true_map;
-
- EdgeSubGraphAdaptor() : const_true_map(true) {
- Parent::setNodeFilterMap(const_true_map);
- }
-
- public:
-
- EdgeSubGraphAdaptor(Graph& _graph, EdgeFilterMap& _edge_filter_map) :
- Parent(), const_true_map(true) {
- Parent::setGraph(_graph);
- Parent::setNodeFilterMap(const_true_map);
- Parent::setEdgeFilterMap(_edge_filter_map);
- }
-
- };
-
- /// \brief Just gives back an edge sub graph adaptor
- ///
- /// Just gives back an edge sub graph adaptor
- template<typename Graph, typename EdgeFilterMap>
- EdgeSubGraphAdaptor<const Graph, EdgeFilterMap>
- edgeSubGraphAdaptor(const Graph& graph, EdgeFilterMap& efm) {
- return EdgeSubGraphAdaptor<const Graph, EdgeFilterMap>(graph, efm);
- }
-
- template<typename Graph, typename EdgeFilterMap>
- EdgeSubGraphAdaptor<const Graph, const EdgeFilterMap>
- edgeSubGraphAdaptor(const Graph& graph, const EdgeFilterMap& efm) {
- return EdgeSubGraphAdaptor<const Graph, const EdgeFilterMap>(graph, efm);
- }
-
- template <typename _Graph>
- class UndirGraphAdaptorBase :
- public UndirGraphExtender<GraphAdaptorBase<_Graph> > {
- public:
- typedef _Graph Graph;
- typedef UndirGraphAdaptorBase Adaptor;
- typedef UndirGraphExtender<GraphAdaptorBase<_Graph> > Parent;
-
- protected:
-
- UndirGraphAdaptorBase() : Parent() {}
-
- public:
-
- typedef typename Parent::UEdge UEdge;
- typedef typename Parent::Edge Edge;
-
- private:
-
- template <typename _Value>
- class EdgeMapBase {
- private:
-
- typedef typename _Graph::template EdgeMap<_Value> MapImpl;
-
- public:
-
- typedef typename MapTraits<MapImpl>::ReferenceMapTag ReferenceMapTag;
-
- typedef _Value Value;
- typedef Edge Key;
-
- EdgeMapBase(const Adaptor& adaptor) :
- forward_map(*adaptor.graph), backward_map(*adaptor.graph) {}
-
- EdgeMapBase(const Adaptor& adaptor, const Value& v)
- : forward_map(*adaptor.graph, v), backward_map(*adaptor.graph, v) {}
-
- void set(const Edge& e, const Value& a) {
- if (Parent::direction(e)) {
- forward_map.set(e, a);
- } else {
- backward_map.set(e, a);
- }
- }
-
- typename MapTraits<MapImpl>::ConstReturnValue operator[](Edge e) const {
- if (Parent::direction(e)) {
- return forward_map[e];
- } else {
- return backward_map[e];
- }
- }
-
- typename MapTraits<MapImpl>::ReturnValue operator[](Edge e) {
- if (Parent::direction(e)) {
- return forward_map[e];
- } else {
- return backward_map[e];
- }
- }
-
- protected:
-
- MapImpl forward_map, backward_map;
-
- };
-
- public:
-
- template <typename _Value>
- class EdgeMap
- : public SubMapExtender<Adaptor, EdgeMapBase<_Value> >
- {
- public:
- typedef Adaptor Graph;
- typedef SubMapExtender<Adaptor, EdgeMapBase<_Value> > Parent;
-
- EdgeMap(const Graph& g)
- : Parent(g) {}
- EdgeMap(const Graph& g, const _Value& v)
- : Parent(g, v) {}
-
- EdgeMap& operator=(const EdgeMap& cmap) {
- return operator=<EdgeMap>(cmap);
- }
-
- template <typename CMap>
- EdgeMap& operator=(const CMap& cmap) {
- Parent::operator=(cmap);
- return *this;
- }
- };
-
- template <typename _Value>
- class UEdgeMap : public Graph::template EdgeMap<_Value> {
- public:
-
- typedef typename Graph::template EdgeMap<_Value> Parent;
-
- explicit UEdgeMap(const Adaptor& ga)
- : Parent(*ga.graph) {}
-
- UEdgeMap(const Adaptor& ga, const _Value& value)
- : Parent(*ga.graph, value) {}
-
- UEdgeMap& operator=(const UEdgeMap& cmap) {
- return operator=<UEdgeMap>(cmap);
- }
-
- template <typename CMap>
- UEdgeMap& operator=(const CMap& cmap) {
- Parent::operator=(cmap);
- return *this;
- }
-
- };
-
- };
-
- template <typename _Graph, typename Enable = void>
- class AlterableUndirGraphAdaptor
- : public UGraphAdaptorExtender<UndirGraphAdaptorBase<_Graph> > {
- public:
- typedef UGraphAdaptorExtender<UndirGraphAdaptorBase<_Graph> > Parent;
-
- protected:
-
- AlterableUndirGraphAdaptor() : Parent() {}
-
- public:
-
- typedef typename Parent::EdgeNotifier UEdgeNotifier;
- typedef InvalidType EdgeNotifier;
-
- };
-
- template <typename _Graph>
- class AlterableUndirGraphAdaptor<
- _Graph,
- typename enable_if<typename _Graph::EdgeNotifier::Notifier>::type >
- : public UGraphAdaptorExtender<UndirGraphAdaptorBase<_Graph> > {
- public:
-
- typedef UGraphAdaptorExtender<UndirGraphAdaptorBase<_Graph> > Parent;
- typedef _Graph Graph;
- typedef typename _Graph::Edge GraphEdge;
-
- protected:
-
- AlterableUndirGraphAdaptor()
- : Parent(), edge_notifier(*this), edge_notifier_proxy(*this) {}
-
- void setGraph(_Graph& g) {
- Parent::setGraph(g);
- edge_notifier_proxy.setNotifier(g.notifier(GraphEdge()));
- }
-
- public:
-
- ~AlterableUndirGraphAdaptor() {
- edge_notifier.clear();
- }
-
- typedef typename Parent::UEdge UEdge;
- typedef typename Parent::Edge Edge;
-
- typedef typename Parent::EdgeNotifier UEdgeNotifier;
-
- using Parent::notifier;
-
- typedef AlterationNotifier<AlterableUndirGraphAdaptor,
- Edge> EdgeNotifier;
- EdgeNotifier& notifier(Edge) const { return edge_notifier; }
-
- protected:
-
- class NotifierProxy : public Graph::EdgeNotifier::ObserverBase {
- public:
-
- typedef typename Graph::EdgeNotifier::ObserverBase Parent;
- typedef AlterableUndirGraphAdaptor AdaptorBase;
-
- NotifierProxy(const AdaptorBase& _adaptor)
- : Parent(), adaptor(&_adaptor) {
- }
-
- virtual ~NotifierProxy() {
- if (Parent::attached()) {
- Parent::detach();
- }
- }
-
- void setNotifier(typename Graph::EdgeNotifier& nf) {
- Parent::attach(nf);
- }
-
-
- protected:
-
- virtual void add(const GraphEdge& ge) {
- std::vector<Edge> edges;
- edges.push_back(AdaptorBase::Parent::direct(ge, true));
- edges.push_back(AdaptorBase::Parent::direct(ge, false));
- adaptor->notifier(Edge()).add(edges);
- }
- virtual void add(const std::vector<GraphEdge>& ge) {
- std::vector<Edge> edges;
- for (int i = 0; i < int(ge.size()); ++i) {
- edges.push_back(AdaptorBase::Parent::direct(ge[i], true));
- edges.push_back(AdaptorBase::Parent::direct(ge[i], false));
- }
- adaptor->notifier(Edge()).add(edges);
- }
- virtual void erase(const GraphEdge& ge) {
- std::vector<Edge> edges;
- edges.push_back(AdaptorBase::Parent::direct(ge, true));
- edges.push_back(AdaptorBase::Parent::direct(ge, false));
- adaptor->notifier(Edge()).erase(edges);
- }
- virtual void erase(const std::vector<GraphEdge>& ge) {
- std::vector<Edge> edges;
- for (int i = 0; i < int(ge.size()); ++i) {
- edges.push_back(AdaptorBase::Parent::direct(ge[i], true));
- edges.push_back(AdaptorBase::Parent::direct(ge[i], false));
- }
- adaptor->notifier(Edge()).erase(edges);
- }
- virtual void build() {
- adaptor->notifier(Edge()).build();
- }
- virtual void clear() {
- adaptor->notifier(Edge()).clear();
- }
-
- const AdaptorBase* adaptor;
- };
-
-
- mutable EdgeNotifier edge_notifier;
- NotifierProxy edge_notifier_proxy;
-
- };
-
-
- ///\ingroup graph_adaptors
- ///
- /// \brief An undirected graph is made from a directed graph by an adaptor
- ///
- /// This adaptor makes an undirected graph from a directed
- /// graph. All edge of the underlying will be showed in the adaptor
- /// as an undirected edge. Let's see an informal example about using
- /// this adaptor:
- ///
- /// There is a network of the streets of a town. Of course there are
- /// some one-way street in the town hence the network is a directed
- /// one. There is a crazy driver who go oppositely in the one-way
- /// street without moral sense. Of course he can pass this streets
- /// slower than the regular way, in fact his speed is half of the
- /// normal speed. How long should he drive to get from a source
- /// point to the target? Let see the example code which calculate it:
- ///
- ///\code
- /// typedef UndirGraphAdaptor<Graph> UGraph;
- /// UGraph ugraph(graph);
- ///
- /// typedef SimpleMap<LengthMap> FLengthMap;
- /// FLengthMap flength(length);
- ///
- /// typedef ScaleMap<LengthMap> RLengthMap;
- /// RLengthMap rlength(length, 2.0);
- ///
- /// typedef UGraph::CombinedEdgeMap<FLengthMap, RLengthMap > ULengthMap;
- /// ULengthMap ulength(flength, rlength);
- ///
- /// Dijkstra<UGraph, ULengthMap> dijkstra(ugraph, ulength);
- /// std::cout << "Driving time : " << dijkstra.run(src, trg) << std::endl;
- ///\endcode
- ///
- /// The combined edge map makes the length map for the undirected
- /// graph. It is created from a forward and reverse map. The forward
- /// map is created from the original length map with a SimpleMap
- /// adaptor which just makes a read-write map from the reference map
- /// i.e. it forgets that it can be return reference to values. The
- /// reverse map is just the scaled original map with the ScaleMap
- /// adaptor. The combination solves that passing the reverse way
- /// takes double time than the original. To get the driving time we
- /// run the dijkstra algorithm on the undirected graph.
- ///
- /// \author Marton Makai and Balazs Dezso
- template<typename _Graph>
- class UndirGraphAdaptor : public AlterableUndirGraphAdaptor<_Graph> {
- public:
- typedef _Graph Graph;
- typedef AlterableUndirGraphAdaptor<_Graph> Parent;
- protected:
- UndirGraphAdaptor() { }
- public:
-
- /// \brief Constructor
- ///
- /// Constructor
- UndirGraphAdaptor(_Graph& _graph) {
- setGraph(_graph);
- }
-
- /// \brief EdgeMap combined from two original EdgeMap
- ///
- /// This class adapts two original graph EdgeMap to
- /// get an edge map on the adaptor.
- template <typename _ForwardMap, typename _BackwardMap>
- class CombinedEdgeMap {
- public:
-
- typedef _ForwardMap ForwardMap;
- typedef _BackwardMap BackwardMap;
-
- typedef typename MapTraits<ForwardMap>::ReferenceMapTag ReferenceMapTag;
-
- typedef typename ForwardMap::Value Value;
- typedef typename Parent::Edge Key;
-
- /// \brief Constructor
- ///
- /// Constructor
- CombinedEdgeMap() : forward_map(0), backward_map(0) {}
-
- /// \brief Constructor
- ///
- /// Constructor
- CombinedEdgeMap(ForwardMap& _forward_map, BackwardMap& _backward_map)
- : forward_map(&_forward_map), backward_map(&_backward_map) {}
-
-
- /// \brief Sets the value associated with a key.
- ///
- /// Sets the value associated with a key.
- void set(const Key& e, const Value& a) {
- if (Parent::direction(e)) {
- forward_map->set(e, a);
- } else {
- backward_map->set(e, a);
- }
- }
-
- /// \brief Returns the value associated with a key.
- ///
- /// Returns the value associated with a key.
- typename MapTraits<ForwardMap>::ConstReturnValue
- operator[](const Key& e) const {
- if (Parent::direction(e)) {
- return (*forward_map)[e];
- } else {
- return (*backward_map)[e];
- }
- }
-
- /// \brief Returns the value associated with a key.
- ///
- /// Returns the value associated with a key.
- typename MapTraits<ForwardMap>::ReturnValue
- operator[](const Key& e) {
- if (Parent::direction(e)) {
- return (*forward_map)[e];
- } else {
- return (*backward_map)[e];
- }
- }
-
- /// \brief Sets the forward map
- ///
- /// Sets the forward map
- void setForwardMap(ForwardMap& _forward_map) {
- forward_map = &_forward_map;
- }
-
- /// \brief Sets the backward map
- ///
- /// Sets the backward map
- void setBackwardMap(BackwardMap& _backward_map) {
- backward_map = &_backward_map;
- }
-
- protected:
-
- ForwardMap* forward_map;
- BackwardMap* backward_map;
-
- };
-
- };
-
- /// \brief Just gives back an undir graph adaptor
- ///
- /// Just gives back an undir graph adaptor
- template<typename Graph>
- UndirGraphAdaptor<const Graph>
- undirGraphAdaptor(const Graph& graph) {
- return UndirGraphAdaptor<const Graph>(graph);
- }
-
- template<typename Graph, typename Number,
- typename CapacityMap, typename FlowMap,
- typename Tol = Tolerance<Number> >
- class ResForwardFilter {
- const CapacityMap* capacity;
- const FlowMap* flow;
- Tol tolerance;
- public:
- typedef typename Graph::Edge Key;
- typedef bool Value;
-
- ResForwardFilter(const CapacityMap& _capacity, const FlowMap& _flow,
- const Tol& _tolerance = Tol())
- : capacity(&_capacity), flow(&_flow), tolerance(_tolerance) { }
-
- ResForwardFilter(const Tol& _tolerance)
- : capacity(0), flow(0), tolerance(_tolerance) { }
-
- void setCapacity(const CapacityMap& _capacity) { capacity = &_capacity; }
- void setFlow(const FlowMap& _flow) { flow = &_flow; }
-
- bool operator[](const typename Graph::Edge& e) const {
- return tolerance.positive((*capacity)[e] - (*flow)[e]);
- }
- };
-
- template<typename Graph, typename Number,
- typename CapacityMap, typename FlowMap,
- typename Tol = Tolerance<Number> >
- class ResBackwardFilter {
- const CapacityMap* capacity;
- const FlowMap* flow;
- Tol tolerance;
- public:
- typedef typename Graph::Edge Key;
- typedef bool Value;
-
- ResBackwardFilter(const CapacityMap& _capacity, const FlowMap& _flow,
- const Tol& _tolerance = Tol())
- : capacity(&_capacity), flow(&_flow), tolerance(_tolerance) { }
- ResBackwardFilter(const Tol& _tolerance = Tol())
- : capacity(0), flow(0), tolerance(_tolerance) { }
- void setCapacity(const CapacityMap& _capacity) { capacity = &_capacity; }
- void setFlow(const FlowMap& _flow) { flow = &_flow; }
- bool operator[](const typename Graph::Edge& e) const {
- return tolerance.positive((*flow)[e]);
- }
- };
-
-
- ///\ingroup graph_adaptors
- ///
- ///\brief An adaptor for composing the residual
- ///graph for directed flow and circulation problems.
- ///
- ///An adaptor for composing the residual graph for directed flow and
- ///circulation problems. Let \f$ G=(V, A) \f$ be a directed graph
- ///and let \f$ F \f$ be a number type. Let moreover \f$ f,c:A\to F \f$,
- ///be functions on the edge-set.
- ///
- ///In the appications of ResGraphAdaptor, \f$ f \f$ usually stands
- ///for a flow and \f$ c \f$ for a capacity function. Suppose that a
- ///graph instange \c g of type \c ListGraph implements \f$ G \f$.
- ///
- ///\code
- /// ListGraph g;
- ///\endcode
- ///
- ///Then ResGraphAdaptor implements the graph structure with node-set
- /// \f$ V \f$ and edge-set \f$ A_{forward}\cup A_{backward} \f$,
- ///where \f$ A_{forward}=\{uv : uv\in A, f(uv)<c(uv)\} \f$ and
- /// \f$ A_{backward}=\{vu : uv\in A, f(uv)>0\} \f$, i.e. the so called
- ///residual graph. When we take the union
- /// \f$ A_{forward}\cup A_{backward} \f$, multilicities are counted, i.e.
- ///if an edge is in both \f$ A_{forward} \f$ and \f$ A_{backward} \f$,
- ///then in the adaptor it appears twice. The following code shows how
- ///such an instance can be constructed.
- ///
- ///\code
- /// typedef ListGraph Graph;
- /// Graph::EdgeMap<int> f(g);
- /// Graph::EdgeMap<int> c(g);
- /// ResGraphAdaptor<Graph, int, Graph::EdgeMap<int>, Graph::EdgeMap<int> > ga(g);
- ///\endcode
- ///\author Marton Makai
- ///
- template<typename Graph, typename Number,
- typename CapacityMap, typename FlowMap,
- typename Tol = Tolerance<Number> >
- class ResGraphAdaptor :
- public EdgeSubGraphAdaptor<
- UndirGraphAdaptor<const Graph>,
- typename UndirGraphAdaptor<const Graph>::template CombinedEdgeMap<
- ResForwardFilter<const Graph, Number, CapacityMap, FlowMap>,
- ResBackwardFilter<const Graph, Number, CapacityMap, FlowMap> > > {
- public:
-
- typedef UndirGraphAdaptor<const Graph> UGraph;
-
- typedef ResForwardFilter<const Graph, Number, CapacityMap, FlowMap>
- ForwardFilter;
-
- typedef ResBackwardFilter<const Graph, Number, CapacityMap, FlowMap>
- BackwardFilter;
-
- typedef typename UGraph::
- template CombinedEdgeMap<ForwardFilter, BackwardFilter>
- EdgeFilter;
-
- typedef EdgeSubGraphAdaptor<UGraph, EdgeFilter> Parent;
-
- protected:
-
- const CapacityMap* capacity;
- FlowMap* flow;
-
- UGraph ugraph;
- ForwardFilter forward_filter;
- BackwardFilter backward_filter;
- EdgeFilter edge_filter;
-
- void setCapacityMap(const CapacityMap& _capacity) {
- capacity=&_capacity;
- forward_filter.setCapacity(_capacity);
- backward_filter.setCapacity(_capacity);
- }
-
- void setFlowMap(FlowMap& _flow) {
- flow=&_flow;
- forward_filter.setFlow(_flow);
- backward_filter.setFlow(_flow);
- }
-
- public:
-
- /// \brief Constructor of the residual graph.
- ///
- /// Constructor of the residual graph. The parameters are the graph type,
- /// the flow map, the capacity map and a tolerance object.
- ResGraphAdaptor(const Graph& _graph, const CapacityMap& _capacity,
- FlowMap& _flow, const Tol& _tolerance = Tol())
- : Parent(), capacity(&_capacity), flow(&_flow), ugraph(_graph),
- forward_filter(_capacity, _flow, _tolerance),
- backward_filter(_capacity, _flow, _tolerance),
- edge_filter(forward_filter, backward_filter)
- {
- Parent::setGraph(ugraph);
- Parent::setEdgeFilterMap(edge_filter);
- }
-
- typedef typename Parent::Edge Edge;
-
- /// \brief Gives back the residual capacity of the edge.
- ///
- /// Gives back the residual capacity of the edge.
- Number rescap(const Edge& edge) const {
- if (UGraph::direction(edge)) {
- return (*capacity)[edge]-(*flow)[edge];
- } else {
- return (*flow)[edge];
- }
- }
-
- /// \brief Augment on the given edge in the residual graph.
- ///
- /// Augment on the given edge in the residual graph. It increase
- /// or decrease the flow on the original edge depend on the direction
- /// of the residual edge.
- void augment(const Edge& e, Number a) const {
- if (UGraph::direction(e)) {
- flow->set(e, (*flow)[e] + a);
- } else {
- flow->set(e, (*flow)[e] - a);
- }
- }
-
- /// \brief Returns the direction of the edge.
- ///
- /// Returns true when the edge is same oriented as the original edge.
- static bool forward(const Edge& e) {
- return UGraph::direction(e);
- }
-
- /// \brief Returns the direction of the edge.
- ///
- /// Returns true when the edge is opposite oriented as the original edge.
- static bool backward(const Edge& e) {
- return !UGraph::direction(e);
- }
-
- /// \brief Gives back the forward oriented residual edge.
- ///
- /// Gives back the forward oriented residual edge.
- static Edge forward(const typename Graph::Edge& e) {
- return UGraph::direct(e, true);
- }
-
- /// \brief Gives back the backward oriented residual edge.
- ///
- /// Gives back the backward oriented residual edge.
- static Edge backward(const typename Graph::Edge& e) {
- return UGraph::direct(e, false);
- }
-
- /// \brief Residual capacity map.
- ///
- /// In generic residual graphs the residual capacity can be obtained
- /// as a map.
- class ResCap {
- protected:
- const ResGraphAdaptor* res_graph;
- public:
- typedef Number Value;
- typedef Edge Key;
- ResCap(const ResGraphAdaptor& _res_graph)
- : res_graph(&_res_graph) {}
-
- Number operator[](const Edge& e) const {
- return res_graph->rescap(e);
- }
-
- };
-
- };
-
-
-
- template <typename _Graph, typename FirstOutEdgesMap>
- class ErasingFirstGraphAdaptorBase : public GraphAdaptorBase<_Graph> {
- public:
- typedef _Graph Graph;
- typedef GraphAdaptorBase<_Graph> Parent;
- protected:
- FirstOutEdgesMap* first_out_edges;
- ErasingFirstGraphAdaptorBase() : Parent(),
- first_out_edges(0) { }
-
- void setFirstOutEdgesMap(FirstOutEdgesMap& _first_out_edges) {
- first_out_edges=&_first_out_edges;
- }
-
- public:
-
- typedef typename Parent::Node Node;
- typedef typename Parent::Edge Edge;
-
- void firstOut(Edge& i, const Node& n) const {
- i=(*first_out_edges)[n];
- }
-
- void erase(const Edge& e) const {
- Node n=source(e);
- Edge f=e;
- Parent::nextOut(f);
- first_out_edges->set(n, f);
- }
- };
-
-
- ///\ingroup graph_adaptors
- ///
- ///\brief For blocking flows.
- ///
- ///This graph adaptor is used for on-the-fly
- ///Dinits blocking flow computations.
- ///For each node, an out-edge is stored which is used when the
- ///\code
- ///OutEdgeIt& first(OutEdgeIt&, const Node&)
- ///\endcode
- ///is called.
- ///
- ///\author Marton Makai
- ///
- template <typename _Graph, typename FirstOutEdgesMap>
- class ErasingFirstGraphAdaptor :
- public GraphAdaptorExtender<
- ErasingFirstGraphAdaptorBase<_Graph, FirstOutEdgesMap> > {
- public:
- typedef _Graph Graph;
- typedef GraphAdaptorExtender<
- ErasingFirstGraphAdaptorBase<_Graph, FirstOutEdgesMap> > Parent;
- ErasingFirstGraphAdaptor(Graph& _graph,
- FirstOutEdgesMap& _first_out_edges) {
- setGraph(_graph);
- setFirstOutEdgesMap(_first_out_edges);
- }
-
- };
-
- /// \brief Base class for split graph adaptor
- ///
- /// Base class of split graph adaptor. In most case you do not need to
- /// use it directly but the documented member functions of this class can
- /// be used with the SplitGraphAdaptor class.
- /// \sa SplitGraphAdaptor
- template <typename _Graph>
- class SplitGraphAdaptorBase
- : public GraphAdaptorBase<const _Graph> {
- public:
-
- typedef _Graph Graph;
-
- typedef GraphAdaptorBase<const _Graph> Parent;
-
- typedef typename Graph::Node GraphNode;
- typedef typename Graph::Edge GraphEdge;
-
- class Node;
- class Edge;
-
- template <typename T> class NodeMap;
- template <typename T> class EdgeMap;
-
-
- class Node : public GraphNode {
- friend class SplitGraphAdaptorBase;
- template <typename T> friend class NodeMap;
- private:
-
- bool in_node;
- Node(GraphNode _node, bool _in_node)
- : GraphNode(_node), in_node(_in_node) {}
-
- public:
-
- Node() {}
- Node(Invalid) : GraphNode(INVALID), in_node(true) {}
-
- bool operator==(const Node& node) const {
- return GraphNode::operator==(node) && in_node == node.in_node;
- }
-
- bool operator!=(const Node& node) const {
- return !(*this == node);
- }
-
- bool operator<(const Node& node) const {
- return GraphNode::operator<(node) ||
- (GraphNode::operator==(node) && in_node < node.in_node);
- }
- };
-
- class Edge {
- friend class SplitGraphAdaptorBase;
- template <typename T> friend class EdgeMap;
- private:
- typedef BiVariant<GraphEdge, GraphNode> EdgeImpl;
-
- explicit Edge(const GraphEdge& edge) : item(edge) {}
- explicit Edge(const GraphNode& node) : item(node) {}
-
- EdgeImpl item;
-
- public:
- Edge() {}
- Edge(Invalid) : item(GraphEdge(INVALID)) {}
-
- bool operator==(const Edge& edge) const {
- if (item.firstState()) {
- if (edge.item.firstState()) {
- return item.first() == edge.item.first();
- }
- } else {
- if (edge.item.secondState()) {
- return item.second() == edge.item.second();
- }
- }
- return false;
- }
-
- bool operator!=(const Edge& edge) const {
- return !(*this == edge);
- }
-
- bool operator<(const Edge& edge) const {
- if (item.firstState()) {
- if (edge.item.firstState()) {
- return item.first() < edge.item.first();
- }
- return false;
- } else {
- if (edge.item.secondState()) {
- return item.second() < edge.item.second();
- }
- return true;
- }
- }
-
- operator GraphEdge() const { return item.first(); }
- operator GraphNode() const { return item.second(); }
-
- };
-
- void first(Node& n) const {
- Parent::first(n);
- n.in_node = true;
- }
-
- void next(Node& n) const {
- if (n.in_node) {
- n.in_node = false;
- } else {
- n.in_node = true;
- Parent::next(n);
- }
- }
-
- void first(Edge& e) const {
- e.item.setSecond();
- Parent::first(e.item.second());
- if (e.item.second() == INVALID) {
- e.item.setFirst();
- Parent::first(e.item.first());
- }
- }
-
- void next(Edge& e) const {
- if (e.item.secondState()) {
- Parent::next(e.item.second());
- if (e.item.second() == INVALID) {
- e.item.setFirst();
- Parent::first(e.item.first());
- }
- } else {
- Parent::next(e.item.first());
- }
- }
-
- void firstOut(Edge& e, const Node& n) const {
- if (n.in_node) {
- e.item.setSecond(n);
- } else {
- e.item.setFirst();
- Parent::firstOut(e.item.first(), n);
- }
- }
-
- void nextOut(Edge& e) const {
- if (!e.item.firstState()) {
- e.item.setFirst(INVALID);
- } else {
- Parent::nextOut(e.item.first());
- }
- }
-
- void firstIn(Edge& e, const Node& n) const {
- if (!n.in_node) {
- e.item.setSecond(n);
- } else {
- e.item.setFirst();
- Parent::firstIn(e.item.first(), n);
- }
- }
-
- void nextIn(Edge& e) const {
- if (!e.item.firstState()) {
- e.item.setFirst(INVALID);
- } else {
- Parent::nextIn(e.item.first());
- }
- }
-
- Node source(const Edge& e) const {
- if (e.item.firstState()) {
- return Node(Parent::source(e.item.first()), false);
- } else {
- return Node(e.item.second(), true);
- }
- }
-
- Node target(const Edge& e) const {
- if (e.item.firstState()) {
- return Node(Parent::target(e.item.first()), true);
- } else {
- return Node(e.item.second(), false);
- }
- }
-
- int id(const Node& n) const {
- return (Parent::id(n) << 1) | (n.in_node ? 0 : 1);
- }
- Node nodeFromId(int ix) const {
- return Node(Parent::nodeFromId(ix >> 1), (ix & 1) == 0);
- }
- int maxNodeId() const {
- return 2 * Parent::maxNodeId() + 1;
- }
-
- int id(const Edge& e) const {
- if (e.item.firstState()) {
- return Parent::id(e.item.first()) << 1;
- } else {
- return (Parent::id(e.item.second()) << 1) | 1;
- }
- }
- Edge edgeFromId(int ix) const {
- if ((ix & 1) == 0) {
- return Edge(Parent::edgeFromId(ix >> 1));
- } else {
- return Edge(Parent::nodeFromId(ix >> 1));
- }
- }
- int maxEdgeId() const {
- return std::max(Parent::maxNodeId() << 1,
- (Parent::maxEdgeId() << 1) | 1);
- }
-
- /// \brief Returns true when the node is in-node.
- ///
- /// Returns true when the node is in-node.
- static bool inNode(const Node& n) {
- return n.in_node;
- }
-
- /// \brief Returns true when the node is out-node.
- ///
- /// Returns true when the node is out-node.
- static bool outNode(const Node& n) {
- return !n.in_node;
- }
-
- /// \brief Returns true when the edge is edge in the original graph.
- ///
- /// Returns true when the edge is edge in the original graph.
- static bool origEdge(const Edge& e) {
- return e.item.firstState();
- }
-
- /// \brief Returns true when the edge binds an in-node and an out-node.
- ///
- /// Returns true when the edge binds an in-node and an out-node.
- static bool bindEdge(const Edge& e) {
- return e.item.secondState();
- }
-
- /// \brief Gives back the in-node created from the \c node.
- ///
- /// Gives back the in-node created from the \c node.
- static Node inNode(const GraphNode& n) {
- return Node(n, true);
- }
-
- /// \brief Gives back the out-node created from the \c node.
- ///
- /// Gives back the out-node created from the \c node.
- static Node outNode(const GraphNode& n) {
- return Node(n, false);
- }
-
- /// \brief Gives back the edge binds the two part of the node.
- ///
- /// Gives back the edge binds the two part of the node.
- static Edge edge(const GraphNode& n) {
- return Edge(n);
- }
-
- /// \brief Gives back the edge of the original edge.
- ///
- /// Gives back the edge of the original edge.
- static Edge edge(const GraphEdge& e) {
- return Edge(e);
- }
-
- typedef True NodeNumTag;
-
- int nodeNum() const {
- return 2 * countNodes(*Parent::graph);
- }
-
- typedef True EdgeNumTag;
-
- int edgeNum() const {
- return countEdges(*Parent::graph) + countNodes(*Parent::graph);
- }
-
- typedef True FindEdgeTag;
-
- Edge findEdge(const Node& u, const Node& v,
- const Edge& prev = INVALID) const {
- if (inNode(u)) {
- if (outNode(v)) {
- if (static_cast<const GraphNode&>(u) ==
- static_cast<const GraphNode&>(v) && prev == INVALID) {
- return Edge(u);
- }
- }
- } else {
- if (inNode(v)) {
- return Edge(findEdge(*Parent::graph, u, v, prev));
- }
- }
- return INVALID;
- }
-
-
- template <typename T>
- class NodeMap : public MapBase<Node, T> {
- typedef typename Parent::template NodeMap<T> NodeImpl;
- public:
- NodeMap(const SplitGraphAdaptorBase& _graph)
- : inNodeMap(_graph), outNodeMap(_graph) {}
- NodeMap(const SplitGraphAdaptorBase& _graph, const T& t)
- : inNodeMap(_graph, t), outNodeMap(_graph, t) {}
- NodeMap& operator=(const NodeMap& cmap) {
- return operator=<NodeMap>(cmap);
- }
- template <typename CMap>
- NodeMap& operator=(const CMap& cmap) {
- Parent::operator=(cmap);
- return *this;
- }
-
- void set(const Node& key, const T& val) {
- if (SplitGraphAdaptorBase::inNode(key)) { inNodeMap.set(key, val); }
- else {outNodeMap.set(key, val); }
- }
-
- typename MapTraits<NodeImpl>::ReturnValue
- operator[](const Node& key) {
- if (SplitGraphAdaptorBase::inNode(key)) { return inNodeMap[key]; }
- else { return outNodeMap[key]; }
- }
-
- typename MapTraits<NodeImpl>::ConstReturnValue
- operator[](const Node& key) const {
- if (SplitGraphAdaptorBase::inNode(key)) { return inNodeMap[key]; }
- else { return outNodeMap[key]; }
- }
-
- private:
- NodeImpl inNodeMap, outNodeMap;
- };
-
- template <typename T>
- class EdgeMap : public MapBase<Edge, T> {
- typedef typename Parent::template EdgeMap<T> EdgeMapImpl;
- typedef typename Parent::template NodeMap<T> NodeMapImpl;
- public:
-
- EdgeMap(const SplitGraphAdaptorBase& _graph)
- : edge_map(_graph), node_map(_graph) {}
- EdgeMap(const SplitGraphAdaptorBase& _graph, const T& t)
- : edge_map(_graph, t), node_map(_graph, t) {}
- EdgeMap& operator=(const EdgeMap& cmap) {
- return operator=<EdgeMap>(cmap);
- }
- template <typename CMap>
- EdgeMap& operator=(const CMap& cmap) {
- Parent::operator=(cmap);
- return *this;
- }
-
- void set(const Edge& key, const T& val) {
- if (SplitGraphAdaptorBase::origEdge(key)) {
- edge_map.set(key.item.first(), val);
- } else {
- node_map.set(key.item.second(), val);
- }
- }
-
- typename MapTraits<EdgeMapImpl>::ReturnValue
- operator[](const Edge& key) {
- if (SplitGraphAdaptorBase::origEdge(key)) {
- return edge_map[key.item.first()];
- } else {
- return node_map[key.item.second()];
- }
- }
-
- typename MapTraits<EdgeMapImpl>::ConstReturnValue
- operator[](const Edge& key) const {
- if (SplitGraphAdaptorBase::origEdge(key)) {
- return edge_map[key.item.first()];
- } else {
- return node_map[key.item.second()];
- }
- }
-
- private:
- typename Parent::template EdgeMap<T> edge_map;
- typename Parent::template NodeMap<T> node_map;
- };
-
-
- };
-
- template <typename _Graph, typename NodeEnable = void,
- typename EdgeEnable = void>
- class AlterableSplitGraphAdaptor
- : public GraphAdaptorExtender<SplitGraphAdaptorBase<_Graph> > {
- public:
-
- typedef GraphAdaptorExtender<SplitGraphAdaptorBase<_Graph> > Parent;
- typedef _Graph Graph;
-
- typedef typename Graph::Node GraphNode;
- typedef typename Graph::Node GraphEdge;
-
- protected:
-
- AlterableSplitGraphAdaptor() : Parent() {}
-
- public:
-
- typedef InvalidType NodeNotifier;
- typedef InvalidType EdgeNotifier;
-
- };
-
- template <typename _Graph, typename EdgeEnable>
- class AlterableSplitGraphAdaptor<
- _Graph,
- typename enable_if<typename _Graph::NodeNotifier::Notifier>::type,
- EdgeEnable>
- : public GraphAdaptorExtender<SplitGraphAdaptorBase<_Graph> > {
- public:
-
- typedef GraphAdaptorExtender<SplitGraphAdaptorBase<_Graph> > Parent;
- typedef _Graph Graph;
-
- typedef typename Graph::Node GraphNode;
- typedef typename Graph::Edge GraphEdge;
-
- typedef typename Parent::Node Node;
- typedef typename Parent::Edge Edge;
-
- protected:
-
- AlterableSplitGraphAdaptor()
- : Parent(), node_notifier(*this), node_notifier_proxy(*this) {}
-
- void setGraph(_Graph& graph) {
- Parent::setGraph(graph);
- node_notifier_proxy.setNotifier(graph.notifier(GraphNode()));
- }
-
- public:
-
- ~AlterableSplitGraphAdaptor() {
- node_notifier.clear();
- }
-
- typedef AlterationNotifier<AlterableSplitGraphAdaptor, Node> NodeNotifier;
- typedef InvalidType EdgeNotifier;
-
- NodeNotifier& notifier(Node) const { return node_notifier; }
-
- protected:
-
- class NodeNotifierProxy : public Graph::NodeNotifier::ObserverBase {
- public:
-
- typedef typename Graph::NodeNotifier::ObserverBase Parent;
- typedef AlterableSplitGraphAdaptor AdaptorBase;
-
- NodeNotifierProxy(const AdaptorBase& _adaptor)
- : Parent(), adaptor(&_adaptor) {
- }
-
- virtual ~NodeNotifierProxy() {
- if (Parent::attached()) {
- Parent::detach();
- }
- }
-
- void setNotifier(typename Graph::NodeNotifier& graph_notifier) {
- Parent::attach(graph_notifier);
- }
-
-
- protected:
-
- virtual void add(const GraphNode& gn) {
- std::vector<Node> nodes;
- nodes.push_back(AdaptorBase::Parent::inNode(gn));
- nodes.push_back(AdaptorBase::Parent::outNode(gn));
- adaptor->notifier(Node()).add(nodes);
- }
-
- virtual void add(const std::vector<GraphNode>& gn) {
- std::vector<Node> nodes;
- for (int i = 0; i < int(gn.size()); ++i) {
- nodes.push_back(AdaptorBase::Parent::inNode(gn[i]));
- nodes.push_back(AdaptorBase::Parent::outNode(gn[i]));
- }
- adaptor->notifier(Node()).add(nodes);
- }
-
- virtual void erase(const GraphNode& gn) {
- std::vector<Node> nodes;
- nodes.push_back(AdaptorBase::Parent::inNode(gn));
- nodes.push_back(AdaptorBase::Parent::outNode(gn));
- adaptor->notifier(Node()).erase(nodes);
- }
-
- virtual void erase(const std::vector<GraphNode>& gn) {
- std::vector<Node> nodes;
- for (int i = 0; i < int(gn.size()); ++i) {
- nodes.push_back(AdaptorBase::Parent::inNode(gn[i]));
- nodes.push_back(AdaptorBase::Parent::outNode(gn[i]));
- }
- adaptor->notifier(Node()).erase(nodes);
- }
- virtual void build() {
- adaptor->notifier(Node()).build();
- }
- virtual void clear() {
- adaptor->notifier(Node()).clear();
- }
-
- const AdaptorBase* adaptor;
- };
-
-
- mutable NodeNotifier node_notifier;
-
- NodeNotifierProxy node_notifier_proxy;
-
- };
-
- template <typename _Graph>
- class AlterableSplitGraphAdaptor<
- _Graph,
- typename enable_if<typename _Graph::NodeNotifier::Notifier>::type,
- typename enable_if<typename _Graph::EdgeNotifier::Notifier>::type>
- : public GraphAdaptorExtender<SplitGraphAdaptorBase<_Graph> > {
- public:
-
- typedef GraphAdaptorExtender<SplitGraphAdaptorBase<_Graph> > Parent;
- typedef _Graph Graph;
-
- typedef typename Graph::Node GraphNode;
- typedef typename Graph::Edge GraphEdge;
-
- typedef typename Parent::Node Node;
- typedef typename Parent::Edge Edge;
-
- protected:
-
- AlterableSplitGraphAdaptor()
- : Parent(), node_notifier(*this), edge_notifier(*this),
- node_notifier_proxy(*this), edge_notifier_proxy(*this) {}
-
- void setGraph(_Graph& g) {
- Parent::setGraph(g);
- node_notifier_proxy.setNotifier(g.notifier(GraphNode()));
- edge_notifier_proxy.setNotifier(g.notifier(GraphEdge()));
- }
-
- public:
-
- ~AlterableSplitGraphAdaptor() {
- node_notifier.clear();
- edge_notifier.clear();
- }
-
- typedef AlterationNotifier<AlterableSplitGraphAdaptor, Node> NodeNotifier;
- typedef AlterationNotifier<AlterableSplitGraphAdaptor, Edge> EdgeNotifier;
-
- NodeNotifier& notifier(Node) const { return node_notifier; }
- EdgeNotifier& notifier(Edge) const { return edge_notifier; }
-
- protected:
-
- class NodeNotifierProxy : public Graph::NodeNotifier::ObserverBase {
- public:
-
- typedef typename Graph::NodeNotifier::ObserverBase Parent;
- typedef AlterableSplitGraphAdaptor AdaptorBase;
-
- NodeNotifierProxy(const AdaptorBase& _adaptor)
- : Parent(), adaptor(&_adaptor) {
- }
-
- virtual ~NodeNotifierProxy() {
- if (Parent::attached()) {
- Parent::detach();
- }
- }
-
- void setNotifier(typename Graph::NodeNotifier& graph_notifier) {
- Parent::attach(graph_notifier);
- }
-
-
- protected:
-
- virtual void add(const GraphNode& gn) {
- std::vector<Node> nodes;
- nodes.push_back(AdaptorBase::Parent::inNode(gn));
- nodes.push_back(AdaptorBase::Parent::outNode(gn));
- adaptor->notifier(Node()).add(nodes);
- adaptor->notifier(Edge()).add(AdaptorBase::Parent::edge(gn));
- }
- virtual void add(const std::vector<GraphNode>& gn) {
- std::vector<Node> nodes;
- std::vector<Edge> edges;
- for (int i = 0; i < int(gn.size()); ++i) {
- edges.push_back(AdaptorBase::Parent::edge(gn[i]));
- nodes.push_back(AdaptorBase::Parent::inNode(gn[i]));
- nodes.push_back(AdaptorBase::Parent::outNode(gn[i]));
- }
- adaptor->notifier(Node()).add(nodes);
- adaptor->notifier(Edge()).add(edges);
- }
- virtual void erase(const GraphNode& gn) {
- adaptor->notifier(Edge()).erase(AdaptorBase::Parent::edge(gn));
- std::vector<Node> nodes;
- nodes.push_back(AdaptorBase::Parent::inNode(gn));
- nodes.push_back(AdaptorBase::Parent::outNode(gn));
- adaptor->notifier(Node()).erase(nodes);
- }
- virtual void erase(const std::vector<GraphNode>& gn) {
- std::vector<Node> nodes;
- std::vector<Edge> edges;
- for (int i = 0; i < int(gn.size()); ++i) {
- edges.push_back(AdaptorBase::Parent::edge(gn[i]));
- nodes.push_back(AdaptorBase::Parent::inNode(gn[i]));
- nodes.push_back(AdaptorBase::Parent::outNode(gn[i]));
- }
- adaptor->notifier(Edge()).erase(edges);
- adaptor->notifier(Node()).erase(nodes);
- }
- virtual void build() {
- std::vector<Edge> edges;
- const typename Parent::Notifier* nf = Parent::notifier();
- GraphNode it;
- for (nf->first(it); it != INVALID; nf->next(it)) {
- edges.push_back(AdaptorBase::Parent::edge(it));
- }
- adaptor->notifier(Node()).build();
- adaptor->notifier(Edge()).add(edges);
- }
- virtual void clear() {
- std::vector<Edge> edges;
- const typename Parent::Notifier* nf = Parent::notifier();
- GraphNode it;
- for (nf->first(it); it != INVALID; nf->next(it)) {
- edges.push_back(AdaptorBase::Parent::edge(it));
- }
- adaptor->notifier(Edge()).erase(edges);
- adaptor->notifier(Node()).clear();
- }
-
- const AdaptorBase* adaptor;
- };
-
- class EdgeNotifierProxy : public Graph::EdgeNotifier::ObserverBase {
- public:
-
- typedef typename Graph::EdgeNotifier::ObserverBase Parent;
- typedef AlterableSplitGraphAdaptor AdaptorBase;
-
- EdgeNotifierProxy(const AdaptorBase& _adaptor)
- : Parent(), adaptor(&_adaptor) {
- }
-
- virtual ~EdgeNotifierProxy() {
- if (Parent::attached()) {
- Parent::detach();
- }
- }
-
- void setNotifier(typename Graph::EdgeNotifier& graph_notifier) {
- Parent::attach(graph_notifier);
- }
-
-
- protected:
-
- virtual void add(const GraphEdge& ge) {
- adaptor->notifier(Edge()).add(AdaptorBase::edge(ge));
- }
- virtual void add(const std::vector<GraphEdge>& ge) {
- std::vector<Edge> edges;
- for (int i = 0; i < int(ge.size()); ++i) {
- edges.push_back(AdaptorBase::edge(ge[i]));
- }
- adaptor->notifier(Edge()).add(edges);
- }
- virtual void erase(const GraphEdge& ge) {
- adaptor->notifier(Edge()).erase(AdaptorBase::edge(ge));
- }
- virtual void erase(const std::vector<GraphEdge>& ge) {
- std::vector<Edge> edges;
- for (int i = 0; i < int(ge.size()); ++i) {
- edges.push_back(AdaptorBase::edge(ge[i]));
- }
- adaptor->notifier(Edge()).erase(edges);
- }
- virtual void build() {
- std::vector<Edge> edges;
- const typename Parent::Notifier* nf = Parent::notifier();
- GraphEdge it;
- for (nf->first(it); it != INVALID; nf->next(it)) {
- edges.push_back(AdaptorBase::Parent::edge(it));
- }
- adaptor->notifier(Edge()).add(edges);
- }
- virtual void clear() {
- std::vector<Edge> edges;
- const typename Parent::Notifier* nf = Parent::notifier();
- GraphEdge it;
- for (nf->first(it); it != INVALID; nf->next(it)) {
- edges.push_back(AdaptorBase::Parent::edge(it));
- }
- adaptor->notifier(Edge()).erase(edges);
- }
-
- const AdaptorBase* adaptor;
- };
-
-
- mutable NodeNotifier node_notifier;
- mutable EdgeNotifier edge_notifier;
-
- NodeNotifierProxy node_notifier_proxy;
- EdgeNotifierProxy edge_notifier_proxy;
-
- };
-
- /// \ingroup graph_adaptors
- ///
- /// \brief Split graph adaptor class
- ///
- /// This is an graph adaptor which splits all node into an in-node
- /// and an out-node. Formaly, the adaptor replaces each \f$ u \f$
- /// node in the graph with two node, \f$ u_{in} \f$ node and
- /// \f$ u_{out} \f$ node. If there is an \f$ (v, u) \f$ edge in the
- /// original graph the new target of the edge will be \f$ u_{in} \f$ and
- /// similarly the source of the original \f$ (u, v) \f$ edge will be
- /// \f$ u_{out} \f$. The adaptor will add for each node in the
- /// original graph an additional edge which will connect
- /// \f$ (u_{in}, u_{out}) \f$.
- ///
- /// The aim of this class is to run algorithm with node costs if the
- /// algorithm can use directly just edge costs. In this case we should use
- /// a \c SplitGraphAdaptor and set the node cost of the graph to the
- /// bind edge in the adapted graph.
- ///
- /// By example a maximum flow algoritm can compute how many edge
- /// disjoint paths are in the graph. But we would like to know how
- /// many node disjoint paths are in the graph. First we have to
- /// adapt the graph with the \c SplitGraphAdaptor. Then run the flow
- /// algorithm on the adapted graph. The bottleneck of the flow will
- /// be the bind edges which bounds the flow with the count of the
- /// node disjoint paths.
- ///
- ///\code
- ///
- /// typedef SplitGraphAdaptor<SmartGraph> SGraph;
- ///
- /// SGraph sgraph(graph);
- ///
- /// typedef ConstMap<SGraph::Edge, int> SCapacity;
- /// SCapacity scapacity(1);
- ///
- /// SGraph::EdgeMap<int> sflow(sgraph);
- ///
- /// Preflow<SGraph, SCapacity>
- /// spreflow(sgraph, scapacity,
- /// SGraph::outNode(source), SGraph::inNode(target));
- ///
- /// spreflow.run();
- ///
- ///\endcode
- ///
- /// The result of the mamixum flow on the original graph
- /// shows the next figure:
- ///
- /// \image html edge_disjoint.png
- /// \image latex edge_disjoint.eps "Edge disjoint paths" width=\textwidth
- ///
- /// And the maximum flow on the adapted graph:
- ///
- /// \image html node_disjoint.png
- /// \image latex node_disjoint.eps "Node disjoint paths" width=\textwidth
- ///
- /// The second solution contains just 3 disjoint paths while the first 4.
- /// The full code can be found in the \ref disjoint_paths_demo.cc demo file.
- ///
- /// This graph adaptor is fully conform to the
- /// \ref concepts::Graph "Graph" concept and
- /// contains some additional member functions and types. The
- /// documentation of some member functions may be found just in the
- /// SplitGraphAdaptorBase class.
- ///
- /// \sa SplitGraphAdaptorBase
- template <typename _Graph>
- class SplitGraphAdaptor : public AlterableSplitGraphAdaptor<_Graph> {
- public:
- typedef AlterableSplitGraphAdaptor<_Graph> Parent;
-
- typedef typename Parent::Node Node;
- typedef typename Parent::Edge Edge;
-
- /// \brief Constructor of the adaptor.
- ///
- /// Constructor of the adaptor.
- SplitGraphAdaptor(_Graph& g) {
- Parent::setGraph(g);
- }
-
- /// \brief NodeMap combined from two original NodeMap
- ///
- /// This class adapt two of the original graph NodeMap to
- /// get a node map on the adapted graph.
- template <typename InNodeMap, typename OutNodeMap>
- class CombinedNodeMap {
- public:
-
- typedef Node Key;
- typedef typename InNodeMap::Value Value;
-
- /// \brief Constructor
- ///
- /// Constructor.
- CombinedNodeMap(InNodeMap& _inNodeMap, OutNodeMap& _outNodeMap)
- : inNodeMap(_inNodeMap), outNodeMap(_outNodeMap) {}
-
- /// \brief The subscript operator.
- ///
- /// The subscript operator.
- Value& operator[](const Key& key) {
- if (Parent::inNode(key)) {
- return inNodeMap[key];
- } else {
- return outNodeMap[key];
- }
- }
-
- /// \brief The const subscript operator.
- ///
- /// The const subscript operator.
- Value operator[](const Key& key) const {
- if (Parent::inNode(key)) {
- return inNodeMap[key];
- } else {
- return outNodeMap[key];
- }
- }
-
- /// \brief The setter function of the map.
- ///
- /// The setter function of the map.
- void set(const Key& key, const Value& value) {
- if (Parent::inNode(key)) {
- inNodeMap.set(key, value);
- } else {
- outNodeMap.set(key, value);
- }
- }
-
- private:
-
- InNodeMap& inNodeMap;
- OutNodeMap& outNodeMap;
-
- };
-
-
- /// \brief Just gives back a combined node map.
- ///
- /// Just gives back a combined node map.
- template <typename InNodeMap, typename OutNodeMap>
- static CombinedNodeMap<InNodeMap, OutNodeMap>
- combinedNodeMap(InNodeMap& in_map, OutNodeMap& out_map) {
- return CombinedNodeMap<InNodeMap, OutNodeMap>(in_map, out_map);
- }
-
- template <typename InNodeMap, typename OutNodeMap>
- static CombinedNodeMap<const InNodeMap, OutNodeMap>
- combinedNodeMap(const InNodeMap& in_map, OutNodeMap& out_map) {
- return CombinedNodeMap<const InNodeMap, OutNodeMap>(in_map, out_map);
- }
-
- template <typename InNodeMap, typename OutNodeMap>
- static CombinedNodeMap<InNodeMap, const OutNodeMap>
- combinedNodeMap(InNodeMap& in_map, const OutNodeMap& out_map) {
- return CombinedNodeMap<InNodeMap, const OutNodeMap>(in_map, out_map);
- }
-
- template <typename InNodeMap, typename OutNodeMap>
- static CombinedNodeMap<const InNodeMap, const OutNodeMap>
- combinedNodeMap(const InNodeMap& in_map, const OutNodeMap& out_map) {
- return CombinedNodeMap<const InNodeMap,
- const OutNodeMap>(in_map, out_map);
- }
-
- /// \brief EdgeMap combined from an original EdgeMap and NodeMap
- ///
- /// This class adapt an original graph EdgeMap and NodeMap to
- /// get an edge map on the adapted graph.
- template <typename GraphEdgeMap, typename GraphNodeMap>
- class CombinedEdgeMap {
- public:
-
- typedef Edge Key;
- typedef typename GraphEdgeMap::Value Value;
-
- /// \brief Constructor
- ///
- /// Constructor.
- CombinedEdgeMap(GraphEdgeMap& _edge_map, GraphNodeMap& _node_map)
- : edge_map(_edge_map), node_map(_node_map) {}
-
- /// \brief The subscript operator.
- ///
- /// The subscript operator.
- void set(const Edge& edge, const Value& val) {
- if (Parent::origEdge(edge)) {
- edge_map.set(edge, val);
- } else {
- node_map.set(edge, val);
- }
- }
-
- /// \brief The const subscript operator.
- ///
- /// The const subscript operator.
- Value operator[](const Key& edge) const {
- if (Parent::origEdge(edge)) {
- return edge_map[edge];
- } else {
- return node_map[edge];
- }
- }
-
- /// \brief The const subscript operator.
- ///
- /// The const subscript operator.
- Value& operator[](const Key& edge) {
- if (Parent::origEdge(edge)) {
- return edge_map[edge];
- } else {
- return node_map[edge];
- }
- }
-
- private:
- GraphEdgeMap& edge_map;
- GraphNodeMap& node_map;
- };
-
- /// \brief Just gives back a combined edge map.
- ///
- /// Just gives back a combined edge map.
- template <typename GraphEdgeMap, typename GraphNodeMap>
- static CombinedEdgeMap<GraphEdgeMap, GraphNodeMap>
- combinedEdgeMap(GraphEdgeMap& edge_map, GraphNodeMap& node_map) {
- return CombinedEdgeMap<GraphEdgeMap, GraphNodeMap>(edge_map, node_map);
- }
-
- template <typename GraphEdgeMap, typename GraphNodeMap>
- static CombinedEdgeMap<const GraphEdgeMap, GraphNodeMap>
- combinedEdgeMap(const GraphEdgeMap& edge_map, GraphNodeMap& node_map) {
- return CombinedEdgeMap<const GraphEdgeMap,
- GraphNodeMap>(edge_map, node_map);
- }
-
- template <typename GraphEdgeMap, typename GraphNodeMap>
- static CombinedEdgeMap<GraphEdgeMap, const GraphNodeMap>
- combinedEdgeMap(GraphEdgeMap& edge_map, const GraphNodeMap& node_map) {
- return CombinedEdgeMap<GraphEdgeMap,
- const GraphNodeMap>(edge_map, node_map);
- }
-
- template <typename GraphEdgeMap, typename GraphNodeMap>
- static CombinedEdgeMap<const GraphEdgeMap, const GraphNodeMap>
- combinedEdgeMap(const GraphEdgeMap& edge_map,
- const GraphNodeMap& node_map) {
- return CombinedEdgeMap<const GraphEdgeMap,
- const GraphNodeMap>(edge_map, node_map);
- }
-
- };
-
- /// \brief Just gives back a split graph adaptor
- ///
- /// Just gives back a split graph adaptor
- template<typename Graph>
- SplitGraphAdaptor<Graph>
- splitGraphAdaptor(const Graph& graph) {
- return SplitGraphAdaptor<Graph>(graph);
- }
-
-
-} //namespace lemon
-
-#endif //LEMON_GRAPH_ADAPTOR_H
-
diff --git a/src/lemon/graph_utils.h b/src/lemon/graph_utils.h
deleted file mode 100644
index 5d11b46..0000000
--- a/src/lemon/graph_utils.h
+++ /dev/null
@@ -1,3179 +0,0 @@
-/* -*- C++ -*-
- *
- * This file is a part of LEMON, a generic C++ optimization library
- *
- * Copyright (C) 2003-2008
- * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
- * (Egervary Research Group on Combinatorial Optimization, EGRES).
- *
- * Permission to use, modify and distribute this software is granted
- * provided that this copyright notice appears in all copies. For
- * precise terms see the accompanying LICENSE file.
- *
- * This software is provided "AS IS" with no warranty of any kind,
- * express or implied, and with no claim as to its suitability for any
- * purpose.
- *
- */
-
-#ifndef LEMON_GRAPH_UTILS_H
-#define LEMON_GRAPH_UTILS_H
-
-#include <iterator>
-#include <vector>
-#include <map>
-#include <lemon/math.h>
-#include <algorithm>
-
-#include <lemon/bits/invalid.h>
-#include <lemon/bits/utility.h>
-#include <lemon/maps.h>
-#include <lemon/bits/traits.h>
-
-#include <lemon/bits/alteration_notifier.h>
-#include <lemon/bits/default_map.h>
-
-///\ingroup gutils
-///\file
-///\brief Graph utilities.
-
-namespace lemon {
-
- /// \addtogroup gutils
- /// @{
-
- ///Creates convenience typedefs for the graph types and iterators
-
- ///This \c \#define creates convenience typedefs for the following types
- ///of \c Graph: \c Node, \c NodeIt, \c Edge, \c EdgeIt, \c InEdgeIt,
- ///\c OutEdgeIt
- ///\note If \c G it a template parameter, it should be used in this way.
- ///\code
- /// GRAPH_TYPEDEFS(typename G);
- ///\endcode
- ///
- ///\warning There are no typedefs for the graph maps because of the lack of
- ///template typedefs in C++.
-#define GRAPH_TYPEDEFS(Graph) \
- typedef Graph:: Node Node; \
- typedef Graph:: NodeIt NodeIt; \
- typedef Graph:: Edge Edge; \
- typedef Graph:: EdgeIt EdgeIt; \
- typedef Graph:: InEdgeIt InEdgeIt; \
- typedef Graph::OutEdgeIt OutEdgeIt
-
- ///Creates convenience typedefs for the undirected graph types and iterators
-
- ///This \c \#define creates the same convenience typedefs as defined by
- ///\ref GRAPH_TYPEDEFS(Graph) and three more, namely it creates
- ///\c UEdge, \c UEdgeIt, \c IncEdgeIt,
- ///
- ///\note If \c G it a template parameter, it should be used in this way.
- ///\code
- /// UGRAPH_TYPEDEFS(typename G);
- ///\endcode
- ///
- ///\warning There are no typedefs for the graph maps because of the lack of
- ///template typedefs in C++.
-#define UGRAPH_TYPEDEFS(Graph) \
- GRAPH_TYPEDEFS(Graph); \
- typedef Graph:: UEdge UEdge; \
- typedef Graph:: UEdgeIt UEdgeIt; \
- typedef Graph:: IncEdgeIt IncEdgeIt
-
- ///\brief Creates convenience typedefs for the bipartite undirected graph
- ///types and iterators
-
- ///This \c \#define creates the same convenience typedefs as defined by
- ///\ref UGRAPH_TYPEDEFS(Graph) and two more, namely it creates
- ///\c ANodeIt, \c BNodeIt,
- ///
- ///\note If \c G it a template parameter, it should be used in this way.
- ///\code
- /// BPUGRAPH_TYPEDEFS(typename G);
- ///\endcode
- ///
- ///\warning There are no typedefs for the graph maps because of the lack of
- ///template typedefs in C++.
-#define BPUGRAPH_TYPEDEFS(Graph) \
- UGRAPH_TYPEDEFS(Graph); \
- typedef Graph::ANode ANode; \
- typedef Graph::BNode BNode; \
- typedef Graph::ANodeIt ANodeIt; \
- typedef Graph::BNodeIt BNodeIt
-
- /// \brief Function to count the items in the graph.
- ///
- /// This function counts the items (nodes, edges etc) in the graph.
- /// The complexity of the function is O(n) because
- /// it iterates on all of the items.
-
- template <typename Graph, typename Item>
- inline int countItems(const Graph& g) {
- typedef typename ItemSetTraits<Graph, Item>::ItemIt ItemIt;
- int num = 0;
- for (ItemIt it(g); it != INVALID; ++it) {
- ++num;
- }
- return num;
- }
-
- // Node counting:
-
- namespace _graph_utils_bits {
-
- template <typename Graph, typename Enable = void>
- struct CountNodesSelector {
- static int count(const Graph &g) {
- return countItems<Graph, typename Graph::Node>(g);
- }
- };
-
- template <typename Graph>
- struct CountNodesSelector<
- Graph, typename
- enable_if<typename Graph::NodeNumTag, void>::type>
- {
- static int count(const Graph &g) {
- return g.nodeNum();
- }
- };
- }
-
- /// \brief Function to count the nodes in the graph.
- ///
- /// This function counts the nodes in the graph.
- /// The complexity of the function is O(n) but for some
- /// graph structures it is specialized to run in O(1).
- ///
- /// If the graph contains a \e nodeNum() member function and a
- /// \e NodeNumTag tag then this function calls directly the member
- /// function to query the cardinality of the node set.
- template <typename Graph>
- inline int countNodes(const Graph& g) {
- return _graph_utils_bits::CountNodesSelector<Graph>::count(g);
- }
-
- namespace _graph_utils_bits {
-
- template <typename Graph, typename Enable = void>
- struct CountANodesSelector {
- static int count(const Graph &g) {
- return countItems<Graph, typename Graph::ANode>(g);
- }
- };
-
- template <typename Graph>
- struct CountANodesSelector<
- Graph, typename
- enable_if<typename Graph::NodeNumTag, void>::type>
- {
- static int count(const Graph &g) {
- return g.aNodeNum();
- }
- };
- }
-
- /// \brief Function to count the anodes in the graph.
- ///
- /// This function counts the anodes in the graph.
- /// The complexity of the function is O(an) but for some
- /// graph structures it is specialized to run in O(1).
- ///
- /// If the graph contains an \e aNodeNum() member function and a
- /// \e NodeNumTag tag then this function calls directly the member
- /// function to query the cardinality of the A-node set.
- template <typename Graph>
- inline int countANodes(const Graph& g) {
- return _graph_utils_bits::CountANodesSelector<Graph>::count(g);
- }
-
- namespace _graph_utils_bits {
-
- template <typename Graph, typename Enable = void>
- struct CountBNodesSelector {
- static int count(const Graph &g) {
- return countItems<Graph, typename Graph::BNode>(g);
- }
- };
-
- template <typename Graph>
- struct CountBNodesSelector<
- Graph, typename
- enable_if<typename Graph::NodeNumTag, void>::type>
- {
- static int count(const Graph &g) {
- return g.bNodeNum();
- }
- };
- }
-
- /// \brief Function to count the bnodes in the graph.
- ///
- /// This function counts the bnodes in the graph.
- /// The complexity of the function is O(bn) but for some
- /// graph structures it is specialized to run in O(1).
- ///
- /// If the graph contains a \e bNodeNum() member function and a
- /// \e NodeNumTag tag then this function calls directly the member
- /// function to query the cardinality of the B-node set.
- template <typename Graph>
- inline int countBNodes(const Graph& g) {
- return _graph_utils_bits::CountBNodesSelector<Graph>::count(g);
- }
-
-
- // Edge counting:
-
- namespace _graph_utils_bits {
-
- template <typename Graph, typename Enable = void>
- struct CountEdgesSelector {
- static int count(const Graph &g) {
- return countItems<Graph, typename Graph::Edge>(g);
- }
- };
-
- template <typename Graph>
- struct CountEdgesSelector<
- Graph,
- typename enable_if<typename Graph::EdgeNumTag, void>::type>
- {
- static int count(const Graph &g) {
- return g.edgeNum();
- }
- };
- }
-
- /// \brief Function to count the edges in the graph.
- ///
- /// This function counts the edges in the graph.
- /// The complexity of the function is O(e) but for some
- /// graph structures it is specialized to run in O(1).
- ///
- /// If the graph contains a \e edgeNum() member function and a
- /// \e EdgeNumTag tag then this function calls directly the member
- /// function to query the cardinality of the edge set.
- template <typename Graph>
- inline int countEdges(const Graph& g) {
- return _graph_utils_bits::CountEdgesSelector<Graph>::count(g);
- }
-
- // Undirected edge counting:
- namespace _graph_utils_bits {
-
- template <typename Graph, typename Enable = void>
- struct CountUEdgesSelector {
- static int count(const Graph &g) {
- return countItems<Graph, typename Graph::UEdge>(g);
- }
- };
-
- template <typename Graph>
- struct CountUEdgesSelector<
- Graph,
- typename enable_if<typename Graph::EdgeNumTag, void>::type>
- {
- static int count(const Graph &g) {
- return g.uEdgeNum();
- }
- };
- }
-
- /// \brief Function to count the undirected edges in the graph.
- ///
- /// This function counts the undirected edges in the graph.
- /// The complexity of the function is O(e) but for some
- /// graph structures it is specialized to run in O(1).
- ///
- /// If the graph contains a \e uEdgeNum() member function and a
- /// \e EdgeNumTag tag then this function calls directly the member
- /// function to query the cardinality of the undirected edge set.
- template <typename Graph>
- inline int countUEdges(const Graph& g) {
- return _graph_utils_bits::CountUEdgesSelector<Graph>::count(g);
-
- }
-
-
- template <typename Graph, typename DegIt>
- inline int countNodeDegree(const Graph& _g, const typename Graph::Node& _n) {
- int num = 0;
- for (DegIt it(_g, _n); it != INVALID; ++it) {
- ++num;
- }
- return num;
- }
-
- /// \brief Function to count the number of the out-edges from node \c n.
- ///
- /// This function counts the number of the out-edges from node \c n
- /// in the graph.
- template <typename Graph>
- inline int countOutEdges(const Graph& _g, const typename Graph::Node& _n) {
- return countNodeDegree<Graph, typename Graph::OutEdgeIt>(_g, _n);
- }
-
- /// \brief Function to count the number of the in-edges to node \c n.
- ///
- /// This function counts the number of the in-edges to node \c n
- /// in the graph.
- template <typename Graph>
- inline int countInEdges(const Graph& _g, const typename Graph::Node& _n) {
- return countNodeDegree<Graph, typename Graph::InEdgeIt>(_g, _n);
- }
-
- /// \brief Function to count the number of the inc-edges to node \c n.
- ///
- /// This function counts the number of the inc-edges to node \c n
- /// in the graph.
- template <typename Graph>
- inline int countIncEdges(const Graph& _g, const typename Graph::Node& _n) {
- return countNodeDegree<Graph, typename Graph::IncEdgeIt>(_g, _n);
- }
-
- namespace _graph_utils_bits {
-
- template <typename Graph, typename Enable = void>
- struct FindEdgeSelector {
- typedef typename Graph::Node Node;
- typedef typename Graph::Edge Edge;
- static Edge find(const Graph &g, Node u, Node v, Edge e) {
- if (e == INVALID) {
- g.firstOut(e, u);
- } else {
- g.nextOut(e);
- }
- while (e != INVALID && g.target(e) != v) {
- g.nextOut(e);
- }
- return e;
- }
- };
-
- template <typename Graph>
- struct FindEdgeSelector<
- Graph,
- typename enable_if<typename Graph::FindEdgeTag, void>::type>
- {
- typedef typename Graph::Node Node;
- typedef typename Graph::Edge Edge;
- static Edge find(const Graph &g, Node u, Node v, Edge prev) {
- return g.findEdge(u, v, prev);
- }
- };
- }
-
- /// \brief Finds an edge between two nodes of a graph.
- ///
- /// Finds an edge from node \c u to node \c v in graph \c g.
- ///
- /// If \c prev is \ref INVALID (this is the default value), then
- /// it finds the first edge from \c u to \c v. Otherwise it looks for
- /// the next edge from \c u to \c v after \c prev.
- /// \return The found edge or \ref INVALID if there is no such an edge.
- ///
- /// Thus you can iterate through each edge from \c u to \c v as it follows.
- ///\code
- /// for(Edge e=findEdge(g,u,v);e!=INVALID;e=findEdge(g,u,v,e)) {
- /// ...
- /// }
- ///\endcode
- ///
- ///\sa EdgeLookUp
- ///\sa AllEdgeLookUp
- ///\sa DynEdgeLookUp
- ///\sa ConEdgeIt
- template <typename Graph>
- inline typename Graph::Edge
- findEdge(const Graph &g, typename Graph::Node u, typename Graph::Node v,
- typename Graph::Edge prev = INVALID) {
- return _graph_utils_bits::FindEdgeSelector<Graph>::find(g, u, v, prev);
- }
-
- /// \brief Iterator for iterating on edges connected the same nodes.
- ///
- /// Iterator for iterating on edges connected the same nodes. It is
- /// higher level interface for the findEdge() function. You can
- /// use it the following way:
- ///\code
- /// for (ConEdgeIt<Graph> it(g, src, trg); it != INVALID; ++it) {
- /// ...
- /// }
- ///\endcode
- ///
- ///\sa findEdge()
- ///\sa EdgeLookUp
- ///\sa AllEdgeLookUp
- ///\sa DynEdgeLookUp
- ///
- /// \author Balazs Dezso
- template <typename _Graph>
- class ConEdgeIt : public _Graph::Edge {
- public:
-
- typedef _Graph Graph;
- typedef typename Graph::Edge Parent;
-
- typedef typename Graph::Edge Edge;
- typedef typename Graph::Node Node;
-
- /// \brief Constructor.
- ///
- /// Construct a new ConEdgeIt iterating on the edges which
- /// connects the \c u and \c v node.
- ConEdgeIt(const Graph& g, Node u, Node v) : graph(g) {
- Parent::operator=(findEdge(graph, u, v));
- }
-
- /// \brief Constructor.
- ///
- /// Construct a new ConEdgeIt which continues the iterating from
- /// the \c e edge.
- ConEdgeIt(const Graph& g, Edge e) : Parent(e), graph(g) {}
-
- /// \brief Increment operator.
- ///
- /// It increments the iterator and gives back the next edge.
- ConEdgeIt& operator++() {
- Parent::operator=(findEdge(graph, graph.source(*this),
- graph.target(*this), *this));
- return *this;
- }
- private:
- const Graph& graph;
- };
-
- namespace _graph_utils_bits {
-
- template <typename Graph, typename Enable = void>
- struct FindUEdgeSelector {
- typedef typename Graph::Node Node;
- typedef typename Graph::UEdge UEdge;
- static UEdge find(const Graph &g, Node u, Node v, UEdge e) {
- bool b;
- if (u != v) {
- if (e == INVALID) {
- g.firstInc(e, b, u);
- } else {
- b = g.source(e) == u;
- g.nextInc(e, b);
- }
- while (e != INVALID && (b ? g.target(e) : g.source(e)) != v) {
- g.nextInc(e, b);
- }
- } else {
- if (e == INVALID) {
- g.firstInc(e, b, u);
- } else {
- b = true;
- g.nextInc(e, b);
- }
- while (e != INVALID && (!b || g.target(e) != v)) {
- g.nextInc(e, b);
- }
- }
- return e;
- }
- };
-
- template <typename Graph>
- struct FindUEdgeSelector<
- Graph,
- typename enable_if<typename Graph::FindEdgeTag, void>::type>
- {
- typedef typename Graph::Node Node;
- typedef typename Graph::UEdge UEdge;
- static UEdge find(const Graph &g, Node u, Node v, UEdge prev) {
- return g.findUEdge(u, v, prev);
- }
- };
- }
-
- /// \brief Finds an uedge between two nodes of a graph.
- ///
- /// Finds an uedge from node \c u to node \c v in graph \c g.
- /// If the node \c u and node \c v is equal then each loop edge
- /// will be enumerated.
- ///
- /// If \c prev is \ref INVALID (this is the default value), then
- /// it finds the first edge from \c u to \c v. Otherwise it looks for
- /// the next edge from \c u to \c v after \c prev.
- /// \return The found edge or \ref INVALID if there is no such an edge.
- ///
- /// Thus you can iterate through each edge from \c u to \c v as it follows.
- ///\code
- /// for(UEdge e = findUEdge(g,u,v); e != INVALID;
- /// e = findUEdge(g,u,v,e)) {
- /// ...
- /// }
- ///\endcode
- ///
- ///\sa ConEdgeIt
-
- template <typename Graph>
- inline typename Graph::UEdge
- findUEdge(const Graph &g, typename Graph::Node u, typename Graph::Node v,
- typename Graph::UEdge p = INVALID) {
- return _graph_utils_bits::FindUEdgeSelector<Graph>::find(g, u, v, p);
- }
-
- /// \brief Iterator for iterating on uedges connected the same nodes.
- ///
- /// Iterator for iterating on uedges connected the same nodes. It is
- /// higher level interface for the findUEdge() function. You can
- /// use it the following way:
- ///\code
- /// for (ConUEdgeIt<Graph> it(g, src, trg); it != INVALID; ++it) {
- /// ...
- /// }
- ///\endcode
- ///
- ///\sa findUEdge()
- ///
- /// \author Balazs Dezso
- template <typename _Graph>
- class ConUEdgeIt : public _Graph::UEdge {
- public:
-
- typedef _Graph Graph;
- typedef typename Graph::UEdge Parent;
-
- typedef typename Graph::UEdge UEdge;
- typedef typename Graph::Node Node;
-
- /// \brief Constructor.
- ///
- /// Construct a new ConUEdgeIt iterating on the edges which
- /// connects the \c u and \c v node.
- ConUEdgeIt(const Graph& g, Node u, Node v) : graph(g) {
- Parent::operator=(findUEdge(graph, u, v));
- }
-
- /// \brief Constructor.
- ///
- /// Construct a new ConUEdgeIt which continues the iterating from
- /// the \c e edge.
- ConUEdgeIt(const Graph& g, UEdge e) : Parent(e), graph(g) {}
-
- /// \brief Increment operator.
- ///
- /// It increments the iterator and gives back the next edge.
- ConUEdgeIt& operator++() {
- Parent::operator=(findUEdge(graph, graph.source(*this),
- graph.target(*this), *this));
- return *this;
- }
- private:
- const Graph& graph;
- };
-
- /// \brief Copy a map.
- ///
- /// This function copies the \c from map to the \c to map. It uses the
- /// given iterator to iterate on the data structure and it uses the \c ref
- /// mapping to convert the from's keys to the to's keys.
- template <typename To, typename From,
- typename ItemIt, typename Ref>
- void copyMap(To& to, const From& from,
- ItemIt it, const Ref& ref) {
- for (; it != INVALID; ++it) {
- to[ref[it]] = from[it];
- }
- }
-
- /// \brief Copy the from map to the to map.
- ///
- /// Copy the \c from map to the \c to map. It uses the given iterator
- /// to iterate on the data structure.
- template <typename To, typename From, typename ItemIt>
- void copyMap(To& to, const From& from, ItemIt it) {
- for (; it != INVALID; ++it) {
- to[it] = from[it];
- }
- }
-
- namespace _graph_utils_bits {
-
- template <typename Graph, typename Item, typename RefMap>
- class MapCopyBase {
- public:
- virtual void copy(const Graph& from, const RefMap& refMap) = 0;
-
- virtual ~MapCopyBase() {}
- };
-
- template <typename Graph, typename Item, typename RefMap,
- typename ToMap, typename FromMap>
- class MapCopy : public MapCopyBase<Graph, Item, RefMap> {
- public:
-
- MapCopy(ToMap& tmap, const FromMap& map)
- : _tmap(tmap), _map(map) {}
-
- virtual void copy(const Graph& graph, const RefMap& refMap) {
- typedef typename ItemSetTraits<Graph, Item>::ItemIt ItemIt;
- for (ItemIt it(graph); it != INVALID; ++it) {
- _tmap.set(refMap[it], _map[it]);
- }
- }
-
- private:
- ToMap& _tmap;
- const FromMap& _map;
- };
-
- template <typename Graph, typename Item, typename RefMap, typename It>
- class ItemCopy : public MapCopyBase<Graph, Item, RefMap> {
- public:
-
- ItemCopy(It& it, const Item& item) : _it(it), _item(item) {}
-
- virtual void copy(const Graph&, const RefMap& refMap) {
- _it = refMap[_item];
- }
-
- private:
- It& _it;
- Item _item;
- };
-
- template <typename Graph, typename Item, typename RefMap, typename Ref>
- class RefCopy : public MapCopyBase<Graph, Item, RefMap> {
- public:
-
- RefCopy(Ref& map) : _map(map) {}
-
- virtual void copy(const Graph& graph, const RefMap& refMap) {
- typedef typename ItemSetTraits<Graph, Item>::ItemIt ItemIt;
- for (ItemIt it(graph); it != INVALID; ++it) {
- _map.set(it, refMap[it]);
- }
- }
-
- private:
- Ref& _map;
- };
-
- template <typename Graph, typename Item, typename RefMap,
- typename CrossRef>
- class CrossRefCopy : public MapCopyBase<Graph, Item, RefMap> {
- public:
-
- CrossRefCopy(CrossRef& cmap) : _cmap(cmap) {}
-
- virtual void copy(const Graph& graph, const RefMap& refMap) {
- typedef typename ItemSetTraits<Graph, Item>::ItemIt ItemIt;
- for (ItemIt it(graph); it != INVALID; ++it) {
- _cmap.set(refMap[it], it);
- }
- }
-
- private:
- CrossRef& _cmap;
- };
-
- template <typename Graph, typename Enable = void>
- struct GraphCopySelector {
- template <typename From, typename NodeRefMap, typename EdgeRefMap>
- static void copy(Graph &to, const From& from,
- NodeRefMap& nodeRefMap, EdgeRefMap& edgeRefMap) {
- for (typename From::NodeIt it(from); it != INVALID; ++it) {
- nodeRefMap[it] = to.addNode();
- }
- for (typename From::EdgeIt it(from); it != INVALID; ++it) {
- edgeRefMap[it] = to.addEdge(nodeRefMap[from.source(it)],
- nodeRefMap[from.target(it)]);
- }
- }
- };
-
- template <typename Graph>
- struct GraphCopySelector<
- Graph,
- typename enable_if<typename Graph::BuildTag, void>::type>
- {
- template <typename From, typename NodeRefMap, typename EdgeRefMap>
- static void copy(Graph &to, const From& from,
- NodeRefMap& nodeRefMap, EdgeRefMap& edgeRefMap) {
- to.build(from, nodeRefMap, edgeRefMap);
- }
- };
-
- template <typename UGraph, typename Enable = void>
- struct UGraphCopySelector {
- template <typename From, typename NodeRefMap, typename UEdgeRefMap>
- static void copy(UGraph &to, const From& from,
- NodeRefMap& nodeRefMap, UEdgeRefMap& uEdgeRefMap) {
- for (typename From::NodeIt it(from); it != INVALID; ++it) {
- nodeRefMap[it] = to.addNode();
- }
- for (typename From::UEdgeIt it(from); it != INVALID; ++it) {
- uEdgeRefMap[it] = to.addEdge(nodeRefMap[from.source(it)],
- nodeRefMap[from.target(it)]);
- }
- }
- };
-
- template <typename UGraph>
- struct UGraphCopySelector<
- UGraph,
- typename enable_if<typename UGraph::BuildTag, void>::type>
- {
- template <typename From, typename NodeRefMap, typename UEdgeRefMap>
- static void copy(UGraph &to, const From& from,
- NodeRefMap& nodeRefMap, UEdgeRefMap& uEdgeRefMap) {
- to.build(from, nodeRefMap, uEdgeRefMap);
- }
- };
-
- template <typename BpUGraph, typename Enable = void>
- struct BpUGraphCopySelector {
- template <typename From, typename ANodeRefMap,
- typename BNodeRefMap, typename UEdgeRefMap>
- static void copy(BpUGraph &to, const From& from,
- ANodeRefMap& aNodeRefMap, BNodeRefMap& bNodeRefMap,
- UEdgeRefMap& uEdgeRefMap) {
- for (typename From::ANodeIt it(from); it != INVALID; ++it) {
- aNodeRefMap[it] = to.addANode();
- }
- for (typename From::BNodeIt it(from); it != INVALID; ++it) {
- bNodeRefMap[it] = to.addBNode();
- }
- for (typename From::UEdgeIt it(from); it != INVALID; ++it) {
- uEdgeRefMap[it] = to.addEdge(aNodeRefMap[from.aNode(it)],
- bNodeRefMap[from.bNode(it)]);
- }
- }
- };
-
- template <typename BpUGraph>
- struct BpUGraphCopySelector<
- BpUGraph,
- typename enable_if<typename BpUGraph::BuildTag, void>::type>
- {
- template <typename From, typename ANodeRefMap,
- typename BNodeRefMap, typename UEdgeRefMap>
- static void copy(BpUGraph &to, const From& from,
- ANodeRefMap& aNodeRefMap, BNodeRefMap& bNodeRefMap,
- UEdgeRefMap& uEdgeRefMap) {
- to.build(from, aNodeRefMap, bNodeRefMap, uEdgeRefMap);
- }
- };
-
-
- }
-
- /// \brief Class to copy a graph.
- ///
- /// Class to copy a graph to another graph (duplicate a graph). The
- /// simplest way of using it is through the \c copyGraph() function.
- template <typename To, typename From>
- class GraphCopy {
- private:
-
- typedef typename From::Node Node;
- typedef typename From::NodeIt NodeIt;
- typedef typename From::Edge Edge;
- typedef typename From::EdgeIt EdgeIt;
-
- typedef typename To::Node TNode;
- typedef typename To::Edge TEdge;
-
- typedef typename From::template NodeMap<TNode> NodeRefMap;
- typedef typename From::template EdgeMap<TEdge> EdgeRefMap;
-
-
- public:
-
-
- /// \brief Constructor for the GraphCopy.
- ///
- /// It copies the content of the \c _from graph into the
- /// \c _to graph.
- GraphCopy(To& _to, const From& _from)
- : from(_from), to(_to) {}
-
- /// \brief Destructor of the GraphCopy
- ///
- /// Destructor of the GraphCopy
- ~GraphCopy() {
- for (int i = 0; i < int(nodeMapCopies.size()); ++i) {
- delete nodeMapCopies[i];
- }
- for (int i = 0; i < int(edgeMapCopies.size()); ++i) {
- delete edgeMapCopies[i];
- }
-
- }
-
- /// \brief Copies the node references into the given map.
- ///
- /// Copies the node references into the given map.
- template <typename NodeRef>
- GraphCopy& nodeRef(NodeRef& map) {
- nodeMapCopies.push_back(new _graph_utils_bits::RefCopy<From, Node,
- NodeRefMap, NodeRef>(map));
- return *this;
- }
-
- /// \brief Copies the node cross references into the given map.
- ///
- /// Copies the node cross references (reverse references) into
- /// the given map.
- template <typename NodeCrossRef>
- GraphCopy& nodeCrossRef(NodeCrossRef& map) {
- nodeMapCopies.push_back(new _graph_utils_bits::CrossRefCopy<From, Node,
- NodeRefMap, NodeCrossRef>(map));
- return *this;
- }
-
- /// \brief Make copy of the given map.
- ///
- /// Makes copy of the given map for the newly created graph.
- /// The new map's key type is the to graph's node type,
- /// and the copied map's key type is the from graph's node
- /// type.
- template <typename ToMap, typename FromMap>
- GraphCopy& nodeMap(ToMap& tmap, const FromMap& map) {
- nodeMapCopies.push_back(new _graph_utils_bits::MapCopy<From, Node,
- NodeRefMap, ToMap, FromMap>(tmap, map));
- return *this;
- }
-
- /// \brief Make a copy of the given node.
- ///
- /// Make a copy of the given node.
- GraphCopy& node(TNode& tnode, const Node& snode) {
- nodeMapCopies.push_back(new _graph_utils_bits::ItemCopy<From, Node,
- NodeRefMap, TNode>(tnode, snode));
- return *this;
- }
-
- /// \brief Copies the edge references into the given map.
- ///
- /// Copies the edge references into the given map.
- template <typename EdgeRef>
- GraphCopy& edgeRef(EdgeRef& map) {
- edgeMapCopies.push_back(new _graph_utils_bits::RefCopy<From, Edge,
- EdgeRefMap, EdgeRef>(map));
- return *this;
- }
-
- /// \brief Copies the edge cross references into the given map.
- ///
- /// Copies the edge cross references (reverse references) into
- /// the given map.
- template <typename EdgeCrossRef>
- GraphCopy& edgeCrossRef(EdgeCrossRef& map) {
- edgeMapCopies.push_back(new _graph_utils_bits::CrossRefCopy<From, Edge,
- EdgeRefMap, EdgeCrossRef>(map));
- return *this;
- }
-
- /// \brief Make copy of the given map.
- ///
- /// Makes copy of the given map for the newly created graph.
- /// The new map's key type is the to graph's edge type,
- /// and the copied map's key type is the from graph's edge
- /// type.
- template <typename ToMap, typename FromMap>
- GraphCopy& edgeMap(ToMap& tmap, const FromMap& map) {
- edgeMapCopies.push_back(new _graph_utils_bits::MapCopy<From, Edge,
- EdgeRefMap, ToMap, FromMap>(tmap, map));
- return *this;
- }
-
- /// \brief Make a copy of the given edge.
- ///
- /// Make a copy of the given edge.
- GraphCopy& edge(TEdge& tedge, const Edge& sedge) {
- edgeMapCopies.push_back(new _graph_utils_bits::ItemCopy<From, Edge,
- EdgeRefMap, TEdge>(tedge, sedge));
- return *this;
- }
-
- /// \brief Executes the copies.
- ///
- /// Executes the copies.
- void run() {
- NodeRefMap nodeRefMap(from);
- EdgeRefMap edgeRefMap(from);
- _graph_utils_bits::GraphCopySelector<To>::
- copy(to, from, nodeRefMap, edgeRefMap);
- for (int i = 0; i < int(nodeMapCopies.size()); ++i) {
- nodeMapCopies[i]->copy(from, nodeRefMap);
- }
- for (int i = 0; i < int(edgeMapCopies.size()); ++i) {
- edgeMapCopies[i]->copy(from, edgeRefMap);
- }
- }
-
- protected:
-
-
- const From& from;
- To& to;
-
- std::vector<_graph_utils_bits::MapCopyBase<From, Node, NodeRefMap>* >
- nodeMapCopies;
-
- std::vector<_graph_utils_bits::MapCopyBase<From, Edge, EdgeRefMap>* >
- edgeMapCopies;
-
- };
-
- /// \brief Copy a graph to another graph.
- ///
- /// Copy a graph to another graph.
- /// The usage of the function:
- ///
- ///\code
- /// copyGraph(trg, src).nodeRef(nr).edgeCrossRef(ecr).run();
- ///\endcode
- ///
- /// After the copy the \c nr map will contain the mapping from the
- /// nodes of the \c from graph to the nodes of the \c to graph and
- /// \c ecr will contain the mapping from the edges of the \c to graph
- /// to the edges of the \c from graph.
- ///
- /// \see GraphCopy
- template <typename To, typename From>
- GraphCopy<To, From> copyGraph(To& to, const From& from) {
- return GraphCopy<To, From>(to, from);
- }
-
- /// \brief Class to copy an undirected graph.
- ///
- /// Class to copy an undirected graph to another graph (duplicate a graph).
- /// The simplest way of using it is through the \c copyUGraph() function.
- template <typename To, typename From>
- class UGraphCopy {
- private:
-
- typedef typename From::Node Node;
- typedef typename From::NodeIt NodeIt;
- typedef typename From::Edge Edge;
- typedef typename From::EdgeIt EdgeIt;
- typedef typename From::UEdge UEdge;
- typedef typename From::UEdgeIt UEdgeIt;
-
- typedef typename To::Node TNode;
- typedef typename To::Edge TEdge;
- typedef typename To::UEdge TUEdge;
-
- typedef typename From::template NodeMap<TNode> NodeRefMap;
- typedef typename From::template UEdgeMap<TUEdge> UEdgeRefMap;
-
- struct EdgeRefMap {
- EdgeRefMap(const To& _to, const From& _from,
- const UEdgeRefMap& _uedge_ref, const NodeRefMap& _node_ref)
- : to(_to), from(_from),
- uedge_ref(_uedge_ref), node_ref(_node_ref) {}
-
- typedef typename From::Edge Key;
- typedef typename To::Edge Value;
-
- Value operator[](const Key& key) const {
- bool forward =
- (from.direction(key) ==
- (node_ref[from.source(static_cast<const UEdge&>(key))] ==
- to.source(uedge_ref[static_cast<const UEdge&>(key)])));
- return to.direct(uedge_ref[key], forward);
- }
-
- const To& to;
- const From& from;
- const UEdgeRefMap& uedge_ref;
- const NodeRefMap& node_ref;
- };
-
-
- public:
-
-
- /// \brief Constructor for the GraphCopy.
- ///
- /// It copies the content of the \c _from graph into the
- /// \c _to graph.
- UGraphCopy(To& _to, const From& _from)
- : from(_from), to(_to) {}
-
- /// \brief Destructor of the GraphCopy
- ///
- /// Destructor of the GraphCopy
- ~UGraphCopy() {
- for (int i = 0; i < int(nodeMapCopies.size()); ++i) {
- delete nodeMapCopies[i];
- }
- for (int i = 0; i < int(edgeMapCopies.size()); ++i) {
- delete edgeMapCopies[i];
- }
- for (int i = 0; i < int(uEdgeMapCopies.size()); ++i) {
- delete uEdgeMapCopies[i];
- }
-
- }
-
- /// \brief Copies the node references into the given map.
- ///
- /// Copies the node references into the given map.
- template <typename NodeRef>
- UGraphCopy& nodeRef(NodeRef& map) {
- nodeMapCopies.push_back(new _graph_utils_bits::RefCopy<From, Node,
- NodeRefMap, NodeRef>(map));
- return *this;
- }
-
- /// \brief Copies the node cross references into the given map.
- ///
- /// Copies the node cross references (reverse references) into
- /// the given map.
- template <typename NodeCrossRef>
- UGraphCopy& nodeCrossRef(NodeCrossRef& map) {
- nodeMapCopies.push_back(new _graph_utils_bits::CrossRefCopy<From, Node,
- NodeRefMap, NodeCrossRef>(map));
- return *this;
- }
-
- /// \brief Make copy of the given map.
- ///
- /// Makes copy of the given map for the newly created graph.
- /// The new map's key type is the to graph's node type,
- /// and the copied map's key type is the from graph's node
- /// type.
- template <typename ToMap, typename FromMap>
- UGraphCopy& nodeMap(ToMap& tmap, const FromMap& map) {
- nodeMapCopies.push_back(new _graph_utils_bits::MapCopy<From, Node,
- NodeRefMap, ToMap, FromMap>(tmap, map));
- return *this;
- }
-
- /// \brief Make a copy of the given node.
- ///
- /// Make a copy of the given node.
- UGraphCopy& node(TNode& tnode, const Node& snode) {
- nodeMapCopies.push_back(new _graph_utils_bits::ItemCopy<From, Node,
- NodeRefMap, TNode>(tnode, snode));
- return *this;
- }
-
- /// \brief Copies the edge references into the given map.
- ///
- /// Copies the edge references into the given map.
- template <typename EdgeRef>
- UGraphCopy& edgeRef(EdgeRef& map) {
- edgeMapCopies.push_back(new _graph_utils_bits::RefCopy<From, Edge,
- EdgeRefMap, EdgeRef>(map));
- return *this;
- }
-
- /// \brief Copies the edge cross references into the given map.
- ///
- /// Copies the edge cross references (reverse references) into
- /// the given map.
- template <typename EdgeCrossRef>
- UGraphCopy& edgeCrossRef(EdgeCrossRef& map) {
- edgeMapCopies.push_back(new _graph_utils_bits::CrossRefCopy<From, Edge,
- EdgeRefMap, EdgeCrossRef>(map));
- return *this;
- }
-
- /// \brief Make copy of the given map.
- ///
- /// Makes copy of the given map for the newly created graph.
- /// The new map's key type is the to graph's edge type,
- /// and the copied map's key type is the from graph's edge
- /// type.
- template <typename ToMap, typename FromMap>
- UGraphCopy& edgeMap(ToMap& tmap, const FromMap& map) {
- edgeMapCopies.push_back(new _graph_utils_bits::MapCopy<From, Edge,
- EdgeRefMap, ToMap, FromMap>(tmap, map));
- return *this;
- }
-
- /// \brief Make a copy of the given edge.
- ///
- /// Make a copy of the given edge.
- UGraphCopy& edge(TEdge& tedge, const Edge& sedge) {
- edgeMapCopies.push_back(new _graph_utils_bits::ItemCopy<From, Edge,
- EdgeRefMap, TEdge>(tedge, sedge));
- return *this;
- }
-
- /// \brief Copies the undirected edge references into the given map.
- ///
- /// Copies the undirected edge references into the given map.
- template <typename UEdgeRef>
- UGraphCopy& uEdgeRef(UEdgeRef& map) {
- uEdgeMapCopies.push_back(new _graph_utils_bits::RefCopy<From, UEdge,
- UEdgeRefMap, UEdgeRef>(map));
- return *this;
- }
-
- /// \brief Copies the undirected edge cross references into the given map.
- ///
- /// Copies the undirected edge cross references (reverse
- /// references) into the given map.
- template <typename UEdgeCrossRef>
- UGraphCopy& uEdgeCrossRef(UEdgeCrossRef& map) {
- uEdgeMapCopies.push_back(new _graph_utils_bits::CrossRefCopy<From,
- UEdge, UEdgeRefMap, UEdgeCrossRef>(map));
- return *this;
- }
-
- /// \brief Make copy of the given map.
- ///
- /// Makes copy of the given map for the newly created graph.
- /// The new map's key type is the to graph's undirected edge type,
- /// and the copied map's key type is the from graph's undirected edge
- /// type.
- template <typename ToMap, typename FromMap>
- UGraphCopy& uEdgeMap(ToMap& tmap, const FromMap& map) {
- uEdgeMapCopies.push_back(new _graph_utils_bits::MapCopy<From, UEdge,
- UEdgeRefMap, ToMap, FromMap>(tmap, map));
- return *this;
- }
-
- /// \brief Make a copy of the given undirected edge.
- ///
- /// Make a copy of the given undirected edge.
- UGraphCopy& uEdge(TUEdge& tuedge, const UEdge& suedge) {
- uEdgeMapCopies.push_back(new _graph_utils_bits::ItemCopy<From, UEdge,
- UEdgeRefMap, TUEdge>(tuedge, suedge));
- return *this;
- }
-
- /// \brief Executes the copies.
- ///
- /// Executes the copies.
- void run() {
- NodeRefMap nodeRefMap(from);
- UEdgeRefMap uEdgeRefMap(from);
- EdgeRefMap edgeRefMap(to, from, uEdgeRefMap, nodeRefMap);
- _graph_utils_bits::UGraphCopySelector<To>::
- copy(to, from, nodeRefMap, uEdgeRefMap);
- for (int i = 0; i < int(nodeMapCopies.size()); ++i) {
- nodeMapCopies[i]->copy(from, nodeRefMap);
- }
- for (int i = 0; i < int(uEdgeMapCopies.size()); ++i) {
- uEdgeMapCopies[i]->copy(from, uEdgeRefMap);
- }
- for (int i = 0; i < int(edgeMapCopies.size()); ++i) {
- edgeMapCopies[i]->copy(from, edgeRefMap);
- }
- }
-
- private:
-
- const From& from;
- To& to;
-
- std::vector<_graph_utils_bits::MapCopyBase<From, Node, NodeRefMap>* >
- nodeMapCopies;
-
- std::vector<_graph_utils_bits::MapCopyBase<From, Edge, EdgeRefMap>* >
- edgeMapCopies;
-
- std::vector<_graph_utils_bits::MapCopyBase<From, UEdge, UEdgeRefMap>* >
- uEdgeMapCopies;
-
- };
-
- /// \brief Copy an undirected graph to another graph.
- ///
- /// Copy an undirected graph to another graph.
- /// The usage of the function:
- ///
- ///\code
- /// copyUGraph(trg, src).nodeRef(nr).edgeCrossRef(ecr).run();
- ///\endcode
- ///
- /// After the copy the \c nr map will contain the mapping from the
- /// nodes of the \c from graph to the nodes of the \c to graph and
- /// \c ecr will contain the mapping from the edges of the \c to graph
- /// to the edges of the \c from graph.
- ///
- /// \see UGraphCopy
- template <typename To, typename From>
- UGraphCopy<To, From>
- copyUGraph(To& to, const From& from) {
- return UGraphCopy<To, From>(to, from);
- }
-
- /// \brief Class to copy a bipartite undirected graph.
- ///
- /// Class to copy a bipartite undirected graph to another graph
- /// (duplicate a graph). The simplest way of using it is through
- /// the \c copyBpUGraph() function.
- template <typename To, typename From>
- class BpUGraphCopy {
- private:
-
- typedef typename From::Node Node;
- typedef typename From::ANode ANode;
- typedef typename From::BNode BNode;
- typedef typename From::NodeIt NodeIt;
- typedef typename From::Edge Edge;
- typedef typename From::EdgeIt EdgeIt;
- typedef typename From::UEdge UEdge;
- typedef typename From::UEdgeIt UEdgeIt;
-
- typedef typename To::Node TNode;
- typedef typename To::Edge TEdge;
- typedef typename To::UEdge TUEdge;
-
- typedef typename From::template ANodeMap<TNode> ANodeRefMap;
- typedef typename From::template BNodeMap<TNode> BNodeRefMap;
- typedef typename From::template UEdgeMap<TUEdge> UEdgeRefMap;
-
- struct NodeRefMap {
- NodeRefMap(const From& _from, const ANodeRefMap& _anode_ref,
- const BNodeRefMap& _bnode_ref)
- : from(_from), anode_ref(_anode_ref), bnode_ref(_bnode_ref) {}
-
- typedef typename From::Node Key;
- typedef typename To::Node Value;
-
- Value operator[](const Key& key) const {
- return from.aNode(key) ? anode_ref[key] : bnode_ref[key];
- }
-
- const From& from;
- const ANodeRefMap& anode_ref;
- const BNodeRefMap& bnode_ref;
- };
-
- struct EdgeRefMap {
- EdgeRefMap(const To& _to, const From& _from,
- const UEdgeRefMap& _uedge_ref, const NodeRefMap& _node_ref)
- : to(_to), from(_from),
- uedge_ref(_uedge_ref), node_ref(_node_ref) {}
-
- typedef typename From::Edge Key;
- typedef typename To::Edge Value;
-
- Value operator[](const Key& key) const {
- bool forward =
- (from.direction(key) ==
- (node_ref[from.source(static_cast<const UEdge&>(key))] ==
- to.source(uedge_ref[static_cast<const UEdge&>(key)])));
- return to.direct(uedge_ref[key], forward);
- }
-
- const To& to;
- const From& from;
- const UEdgeRefMap& uedge_ref;
- const NodeRefMap& node_ref;
- };
-
- public:
-
-
- /// \brief Constructor for the GraphCopy.
- ///
- /// It copies the content of the \c _from graph into the
- /// \c _to graph.
- BpUGraphCopy(To& _to, const From& _from)
- : from(_from), to(_to) {}
-
- /// \brief Destructor of the GraphCopy
- ///
- /// Destructor of the GraphCopy
- ~BpUGraphCopy() {
- for (int i = 0; i < int(aNodeMapCopies.size()); ++i) {
- delete aNodeMapCopies[i];
- }
- for (int i = 0; i < int(bNodeMapCopies.size()); ++i) {
- delete bNodeMapCopies[i];
- }
- for (int i = 0; i < int(nodeMapCopies.size()); ++i) {
- delete nodeMapCopies[i];
- }
- for (int i = 0; i < int(edgeMapCopies.size()); ++i) {
- delete edgeMapCopies[i];
- }
- for (int i = 0; i < int(uEdgeMapCopies.size()); ++i) {
- delete uEdgeMapCopies[i];
- }
-
- }
-
- /// \brief Copies the A-node references into the given map.
- ///
- /// Copies the A-node references into the given map.
- template <typename ANodeRef>
- BpUGraphCopy& aNodeRef(ANodeRef& map) {
- aNodeMapCopies.push_back(new _graph_utils_bits::RefCopy<From, ANode,
- ANodeRefMap, ANodeRef>(map));
- return *this;
- }
-
- /// \brief Copies the A-node cross references into the given map.
- ///
- /// Copies the A-node cross references (reverse references) into
- /// the given map.
- template <typename ANodeCrossRef>
- BpUGraphCopy& aNodeCrossRef(ANodeCrossRef& map) {
- aNodeMapCopies.push_back(new _graph_utils_bits::CrossRefCopy<From,
- ANode, ANodeRefMap, ANodeCrossRef>(map));
- return *this;
- }
-
- /// \brief Make copy of the given A-node map.
- ///
- /// Makes copy of the given map for the newly created graph.
- /// The new map's key type is the to graph's node type,
- /// and the copied map's key type is the from graph's node
- /// type.
- template <typename ToMap, typename FromMap>
- BpUGraphCopy& aNodeMap(ToMap& tmap, const FromMap& map) {
- aNodeMapCopies.push_back(new _graph_utils_bits::MapCopy<From, ANode,
- ANodeRefMap, ToMap, FromMap>(tmap, map));
- return *this;
- }
-
- /// \brief Copies the B-node references into the given map.
- ///
- /// Copies the B-node references into the given map.
- template <typename BNodeRef>
- BpUGraphCopy& bNodeRef(BNodeRef& map) {
- bNodeMapCopies.push_back(new _graph_utils_bits::RefCopy<From, BNode,
- BNodeRefMap, BNodeRef>(map));
- return *this;
- }
-
- /// \brief Copies the B-node cross references into the given map.
- ///
- /// Copies the B-node cross references (reverse references) into
- /// the given map.
- template <typename BNodeCrossRef>
- BpUGraphCopy& bNodeCrossRef(BNodeCrossRef& map) {
- bNodeMapCopies.push_back(new _graph_utils_bits::CrossRefCopy<From,
- BNode, BNodeRefMap, BNodeCrossRef>(map));
- return *this;
- }
-
- /// \brief Make copy of the given B-node map.
- ///
- /// Makes copy of the given map for the newly created graph.
- /// The new map's key type is the to graph's node type,
- /// and the copied map's key type is the from graph's node
- /// type.
- template <typename ToMap, typename FromMap>
- BpUGraphCopy& bNodeMap(ToMap& tmap, const FromMap& map) {
- bNodeMapCopies.push_back(new _graph_utils_bits::MapCopy<From, BNode,
- BNodeRefMap, ToMap, FromMap>(tmap, map));
- return *this;
- }
- /// \brief Copies the node references into the given map.
- ///
- /// Copies the node references into the given map.
- template <typename NodeRef>
- BpUGraphCopy& nodeRef(NodeRef& map) {
- nodeMapCopies.push_back(new _graph_utils_bits::RefCopy<From, Node,
- NodeRefMap, NodeRef>(map));
- return *this;
- }
-
- /// \brief Copies the node cross references into the given map.
- ///
- /// Copies the node cross references (reverse references) into
- /// the given map.
- template <typename NodeCrossRef>
- BpUGraphCopy& nodeCrossRef(NodeCrossRef& map) {
- nodeMapCopies.push_back(new _graph_utils_bits::CrossRefCopy<From, Node,
- NodeRefMap, NodeCrossRef>(map));
- return *this;
- }
-
- /// \brief Make copy of the given map.
- ///
- /// Makes copy of the given map for the newly created graph.
- /// The new map's key type is the to graph's node type,
- /// and the copied map's key type is the from graph's node
- /// type.
- template <typename ToMap, typename FromMap>
- BpUGraphCopy& nodeMap(ToMap& tmap, const FromMap& map) {
- nodeMapCopies.push_back(new _graph_utils_bits::MapCopy<From, Node,
- NodeRefMap, ToMap, FromMap>(tmap, map));
- return *this;
- }
-
- /// \brief Make a copy of the given node.
- ///
- /// Make a copy of the given node.
- BpUGraphCopy& node(TNode& tnode, const Node& snode) {
- nodeMapCopies.push_back(new _graph_utils_bits::ItemCopy<From, Node,
- NodeRefMap, TNode>(tnode, snode));
- return *this;
- }
-
- /// \brief Copies the edge references into the given map.
- ///
- /// Copies the edge references into the given map.
- template <typename EdgeRef>
- BpUGraphCopy& edgeRef(EdgeRef& map) {
- edgeMapCopies.push_back(new _graph_utils_bits::RefCopy<From, Edge,
- EdgeRefMap, EdgeRef>(map));
- return *this;
- }
-
- /// \brief Copies the edge cross references into the given map.
- ///
- /// Copies the edge cross references (reverse references) into
- /// the given map.
- template <typename EdgeCrossRef>
- BpUGraphCopy& edgeCrossRef(EdgeCrossRef& map) {
- edgeMapCopies.push_back(new _graph_utils_bits::CrossRefCopy<From, Edge,
- EdgeRefMap, EdgeCrossRef>(map));
- return *this;
- }
-
- /// \brief Make copy of the given map.
- ///
- /// Makes copy of the given map for the newly created graph.
- /// The new map's key type is the to graph's edge type,
- /// and the copied map's key type is the from graph's edge
- /// type.
- template <typename ToMap, typename FromMap>
- BpUGraphCopy& edgeMap(ToMap& tmap, const FromMap& map) {
- edgeMapCopies.push_back(new _graph_utils_bits::MapCopy<From, Edge,
- EdgeRefMap, ToMap, FromMap>(tmap, map));
- return *this;
- }
-
- /// \brief Make a copy of the given edge.
- ///
- /// Make a copy of the given edge.
- BpUGraphCopy& edge(TEdge& tedge, const Edge& sedge) {
- edgeMapCopies.push_back(new _graph_utils_bits::ItemCopy<From, Edge,
- EdgeRefMap, TEdge>(tedge, sedge));
- return *this;
- }
-
- /// \brief Copies the undirected edge references into the given map.
- ///
- /// Copies the undirected edge references into the given map.
- template <typename UEdgeRef>
- BpUGraphCopy& uEdgeRef(UEdgeRef& map) {
- uEdgeMapCopies.push_back(new _graph_utils_bits::RefCopy<From, UEdge,
- UEdgeRefMap, UEdgeRef>(map));
- return *this;
- }
-
- /// \brief Copies the undirected edge cross references into the given map.
- ///
- /// Copies the undirected edge cross references (reverse
- /// references) into the given map.
- template <typename UEdgeCrossRef>
- BpUGraphCopy& uEdgeCrossRef(UEdgeCrossRef& map) {
- uEdgeMapCopies.push_back(new _graph_utils_bits::CrossRefCopy<From,
- UEdge, UEdgeRefMap, UEdgeCrossRef>(map));
- return *this;
- }
-
- /// \brief Make copy of the given map.
- ///
- /// Makes copy of the given map for the newly created graph.
- /// The new map's key type is the to graph's undirected edge type,
- /// and the copied map's key type is the from graph's undirected edge
- /// type.
- template <typename ToMap, typename FromMap>
- BpUGraphCopy& uEdgeMap(ToMap& tmap, const FromMap& map) {
- uEdgeMapCopies.push_back(new _graph_utils_bits::MapCopy<From, UEdge,
- UEdgeRefMap, ToMap, FromMap>(tmap, map));
- return *this;
- }
-
- /// \brief Make a copy of the given undirected edge.
- ///
- /// Make a copy of the given undirected edge.
- BpUGraphCopy& uEdge(TUEdge& tuedge, const UEdge& suedge) {
- uEdgeMapCopies.push_back(new _graph_utils_bits::ItemCopy<From, UEdge,
- UEdgeRefMap, TUEdge>(tuedge, suedge));
- return *this;
- }
-
- /// \brief Executes the copies.
- ///
- /// Executes the copies.
- void run() {
- ANodeRefMap aNodeRefMap(from);
- BNodeRefMap bNodeRefMap(from);
- NodeRefMap nodeRefMap(from, aNodeRefMap, bNodeRefMap);
- UEdgeRefMap uEdgeRefMap(from);
- EdgeRefMap edgeRefMap(to, from, uEdgeRefMap, nodeRefMap);
- _graph_utils_bits::BpUGraphCopySelector<To>::
- copy(to, from, aNodeRefMap, bNodeRefMap, uEdgeRefMap);
- for (int i = 0; i < int(aNodeMapCopies.size()); ++i) {
- aNodeMapCopies[i]->copy(from, aNodeRefMap);
- }
- for (int i = 0; i < int(bNodeMapCopies.size()); ++i) {
- bNodeMapCopies[i]->copy(from, bNodeRefMap);
- }
- for (int i = 0; i < int(nodeMapCopies.size()); ++i) {
- nodeMapCopies[i]->copy(from, nodeRefMap);
- }
- for (int i = 0; i < int(uEdgeMapCopies.size()); ++i) {
- uEdgeMapCopies[i]->copy(from, uEdgeRefMap);
- }
- for (int i = 0; i < int(edgeMapCopies.size()); ++i) {
- edgeMapCopies[i]->copy(from, edgeRefMap);
- }
- }
-
- private:
-
- const From& from;
- To& to;
-
- std::vector<_graph_utils_bits::MapCopyBase<From, ANode, ANodeRefMap>* >
- aNodeMapCopies;
-
- std::vector<_graph_utils_bits::MapCopyBase<From, BNode, BNodeRefMap>* >
- bNodeMapCopies;
-
- std::vector<_graph_utils_bits::MapCopyBase<From, Node, NodeRefMap>* >
- nodeMapCopies;
-
- std::vector<_graph_utils_bits::MapCopyBase<From, Edge, EdgeRefMap>* >
- edgeMapCopies;
-
- std::vector<_graph_utils_bits::MapCopyBase<From, UEdge, UEdgeRefMap>* >
- uEdgeMapCopies;
-
- };
-
- /// \brief Copy a bipartite undirected graph to another graph.
- ///
- /// Copy a bipartite undirected graph to another graph.
- /// The usage of the function:
- ///
- ///\code
- /// copyBpUGraph(trg, src).aNodeRef(anr).edgeCrossRef(ecr).run();
- ///\endcode
- ///
- /// After the copy the \c nr map will contain the mapping from the
- /// nodes of the \c from graph to the nodes of the \c to graph and
- /// \c ecr will contain the mapping from the edges of the \c to graph
- /// to the edges of the \c from graph.
- ///
- /// \see BpUGraphCopy
- template <typename To, typename From>
- BpUGraphCopy<To, From>
- copyBpUGraph(To& to, const From& from) {
- return BpUGraphCopy<To, From>(to, from);
- }
-
-
- /// @}
-
- /// \addtogroup graph_maps
- /// @{
-
- /// Provides an immutable and unique id for each item in the graph.
-
- /// The IdMap class provides a unique and immutable id for each item of the
- /// same type (e.g. node) in the graph. This id is <ul><li>\b unique:
- /// different items (nodes) get different ids <li>\b immutable: the id of an
- /// item (node) does not change (even if you delete other nodes). </ul>
- /// Through this map you get access (i.e. can read) the inner id values of
- /// the items stored in the graph. This map can be inverted with its member
- /// class \c InverseMap.
- ///
- template <typename _Graph, typename _Item>
- class IdMap {
- public:
- typedef _Graph Graph;
- typedef int Value;
- typedef _Item Item;
- typedef _Item Key;
-
- /// \brief Constructor.
- ///
- /// Constructor of the map.
- explicit IdMap(const Graph& _graph) : graph(&_graph) {}
-
- /// \brief Gives back the \e id of the item.
- ///
- /// Gives back the immutable and unique \e id of the item.
- int operator[](const Item& item) const { return graph->id(item);}
-
- /// \brief Gives back the item by its id.
- ///
- /// Gives back the item by its id.
- Item operator()(int id) { return graph->fromId(id, Item()); }
-
- private:
- const Graph* graph;
-
- public:
-
- /// \brief The class represents the inverse of its owner (IdMap).
- ///
- /// The class represents the inverse of its owner (IdMap).
- /// \see inverse()
- class InverseMap {
- public:
-
- /// \brief Constructor.
- ///
- /// Constructor for creating an id-to-item map.
- explicit InverseMap(const Graph& _graph) : graph(&_graph) {}
-
- /// \brief Constructor.
- ///
- /// Constructor for creating an id-to-item map.
- explicit InverseMap(const IdMap& idMap) : graph(idMap.graph) {}
-
- /// \brief Gives back the given item from its id.
- ///
- /// Gives back the given item from its id.
- ///
- Item operator[](int id) const { return graph->fromId(id, Item());}
-
- private:
- const Graph* graph;
- };
-
- /// \brief Gives back the inverse of the map.
- ///
- /// Gives back the inverse of the IdMap.
- InverseMap inverse() const { return InverseMap(*graph);}
-
- };
-
-
- /// \brief General invertable graph-map type.
-
- /// This type provides simple invertable graph-maps.
- /// The InvertableMap wraps an arbitrary ReadWriteMap
- /// and if a key is set to a new value then store it
- /// in the inverse map.
- ///
- /// The values of the map can be accessed
- /// with stl compatible forward iterator.
- ///
- /// \param _Graph The graph type.
- /// \param _Item The item type of the graph.
- /// \param _Value The value type of the map.
- ///
- /// \see IterableValueMap
- template <typename _Graph, typename _Item, typename _Value>
- class InvertableMap : protected DefaultMap<_Graph, _Item, _Value> {
- private:
-
- typedef DefaultMap<_Graph, _Item, _Value> Map;
- typedef _Graph Graph;
-
- typedef std::map<_Value, _Item> Container;
- Container invMap;
-
- public:
-
- /// The key type of InvertableMap (Node, Edge, UEdge).
- typedef typename Map::Key Key;
- /// The value type of the InvertableMap.
- typedef typename Map::Value Value;
-
-
-
- /// \brief Constructor.
- ///
- /// Construct a new InvertableMap for the graph.
- ///
- explicit InvertableMap(const Graph& graph) : Map(graph) {}
-
- /// \brief Forward iterator for values.
- ///
- /// This iterator is an stl compatible forward
- /// iterator on the values of the map. The values can
- /// be accessed in the [beginValue, endValue) range.
- ///
- class ValueIterator
- : public std::iterator<std::forward_iterator_tag, Value> {
- friend class InvertableMap;
- private:
- ValueIterator(typename Container::const_iterator _it)
- : it(_it) {}
- public:
-
- ValueIterator() {}
-
- ValueIterator& operator++() { ++it; return *this; }
- ValueIterator operator++(int) {
- ValueIterator tmp(*this);
- operator++();
- return tmp;
- }
-
- const Value& operator*() const { return it->first; }
- const Value* operator->() const { return &(it->first); }
-
- bool operator==(ValueIterator jt) const { return it == jt.it; }
- bool operator!=(ValueIterator jt) const { return it != jt.it; }
-
- private:
- typename Container::const_iterator it;
- };
-
- /// \brief Returns an iterator to the first value.
- ///
- /// Returns an stl compatible iterator to the
- /// first value of the map. The values of the
- /// map can be accessed in the [beginValue, endValue)
- /// range.
- ValueIterator beginValue() const {
- return ValueIterator(invMap.begin());
- }
-
- /// \brief Returns an iterator after the last value.
- ///
- /// Returns an stl compatible iterator after the
- /// last value of the map. The values of the
- /// map can be accessed in the [beginValue, endValue)
- /// range.
- ValueIterator endValue() const {
- return ValueIterator(invMap.end());
- }
-
- /// \brief The setter function of the map.
- ///
- /// Sets the mapped value.
- void set(const Key& key, const Value& val) {
- Value oldval = Map::operator[](key);
- typename Container::iterator it = invMap.find(oldval);
- if (it != invMap.end() && it->second == key) {
- invMap.erase(it);
- }
- invMap.insert(make_pair(val, key));
- Map::set(key, val);
- }
-
- /// \brief The getter function of the map.
- ///
- /// It gives back the value associated with the key.
- typename MapTraits<Map>::ConstReturnValue
- operator[](const Key& key) const {
- return Map::operator[](key);
- }
-
- /// \brief Gives back the item by its value.
- ///
- /// Gives back the item by its value.
- Key operator()(const Value& key) const {
- typename Container::const_iterator it = invMap.find(key);
- return it != invMap.end() ? it->second : INVALID;
- }
-
- protected:
-
- /// \brief Erase the key from the map.
- ///
- /// Erase the key to the map. It is called by the
- /// \c AlterationNotifier.
- virtual void erase(const Key& key) {
- Value val = Map::operator[](key);
- typename Container::iterator it = invMap.find(val);
- if (it != invMap.end() && it->second == key) {
- invMap.erase(it);
- }
- Map::erase(key);
- }
-
- /// \brief Erase more keys from the map.
- ///
- /// Erase more keys from the map. It is called by the
- /// \c AlterationNotifier.
- virtual void erase(const std::vector<Key>& keys) {
- for (int i = 0; i < int(keys.size()); ++i) {
- Value val = Map::operator[](keys[i]);
- typename Container::iterator it = invMap.find(val);
- if (it != invMap.end() && it->second == keys[i]) {
- invMap.erase(it);
- }
- }
- Map::erase(keys);
- }
-
- /// \brief Clear the keys from the map and inverse map.
- ///
- /// Clear the keys from the map and inverse map. It is called by the
- /// \c AlterationNotifier.
- virtual void clear() {
- invMap.clear();
- Map::clear();
- }
-
- public:
-
- /// \brief The inverse map type.
- ///
- /// The inverse of this map. The subscript operator of the map
- /// gives back always the item what was last assigned to the value.
- class InverseMap {
- public:
- /// \brief Constructor of the InverseMap.
- ///
- /// Constructor of the InverseMap.
- explicit InverseMap(const InvertableMap& _inverted)
- : inverted(_inverted) {}
-
- /// The value type of the InverseMap.
- typedef typename InvertableMap::Key Value;
- /// The key type of the InverseMap.
- typedef typename InvertableMap::Value Key;
-
- /// \brief Subscript operator.
- ///
- /// Subscript operator. It gives back always the item
- /// what was last assigned to the value.
- Value operator[](const Key& key) const {
- return inverted(key);
- }
-
- private:
- const InvertableMap& inverted;
- };
-
- /// \brief It gives back the just readable inverse map.
- ///
- /// It gives back the just readable inverse map.
- InverseMap inverse() const {
- return InverseMap(*this);
- }
-
-
-
- };
-
- /// \brief Provides a mutable, continuous and unique descriptor for each
- /// item in the graph.
- ///
- /// The DescriptorMap class provides a unique and continuous (but mutable)
- /// descriptor (id) for each item of the same type (e.g. node) in the
- /// graph. This id is <ul><li>\b unique: different items (nodes) get
- /// different ids <li>\b continuous: the range of the ids is the set of
- /// integers between 0 and \c n-1, where \c n is the number of the items of
- /// this type (e.g. nodes) (so the id of a node can change if you delete an
- /// other node, i.e. this id is mutable). </ul> This map can be inverted
- /// with its member class \c InverseMap.
- ///
- /// \param _Graph The graph class the \c DescriptorMap belongs to.
- /// \param _Item The Item is the Key of the Map. It may be Node, Edge or
- /// UEdge.
- template <typename _Graph, typename _Item>
- class DescriptorMap : protected DefaultMap<_Graph, _Item, int> {
-
- typedef _Item Item;
- typedef DefaultMap<_Graph, _Item, int> Map;
-
- public:
- /// The graph class of DescriptorMap.
- typedef _Graph Graph;
-
- /// The key type of DescriptorMap (Node, Edge, UEdge).
- typedef typename Map::Key Key;
- /// The value type of DescriptorMap.
- typedef typename Map::Value Value;
-
- /// \brief Constructor.
- ///
- /// Constructor for descriptor map.
- explicit DescriptorMap(const Graph& _graph) : Map(_graph) {
- Item it;
- const typename Map::Notifier* nf = Map::notifier();
- for (nf->first(it); it != INVALID; nf->next(it)) {
- Map::set(it, invMap.size());
- invMap.push_back(it);
- }
- }
-
- protected:
-
- /// \brief Add a new key to the map.
- ///
- /// Add a new key to the map. It is called by the
- /// \c AlterationNotifier.
- virtual void add(const Item& item) {
- Map::add(item);
- Map::set(item, invMap.size());
- invMap.push_back(item);
- }
-
- /// \brief Add more new keys to the map.
- ///
- /// Add more new keys to the map. It is called by the
- /// \c AlterationNotifier.
- virtual void add(const std::vector<Item>& items) {
- Map::add(items);
- for (int i = 0; i < int(items.size()); ++i) {
- Map::set(items[i], invMap.size());
- invMap.push_back(items[i]);
- }
- }
-
- /// \brief Erase the key from the map.
- ///
- /// Erase the key from the map. It is called by the
- /// \c AlterationNotifier.
- virtual void erase(const Item& item) {
- Map::set(invMap.back(), Map::operator[](item));
- invMap[Map::operator[](item)] = invMap.back();
- invMap.pop_back();
- Map::erase(item);
- }
-
- /// \brief Erase more keys from the map.
- ///
- /// Erase more keys from the map. It is called by the
- /// \c AlterationNotifier.
- virtual void erase(const std::vector<Item>& items) {
- for (int i = 0; i < int(items.size()); ++i) {
- Map::set(invMap.back(), Map::operator[](items[i]));
- invMap[Map::operator[](items[i])] = invMap.back();
- invMap.pop_back();
- }
- Map::erase(items);
- }
-
- /// \brief Build the unique map.
- ///
- /// Build the unique map. It is called by the
- /// \c AlterationNotifier.
- virtual void build() {
- Map::build();
- Item it;
- const typename Map::Notifier* nf = Map::notifier();
- for (nf->first(it); it != INVALID; nf->next(it)) {
- Map::set(it, invMap.size());
- invMap.push_back(it);
- }
- }
-
- /// \brief Clear the keys from the map.
- ///
- /// Clear the keys from the map. It is called by the
- /// \c AlterationNotifier.
- virtual void clear() {
- invMap.clear();
- Map::clear();
- }
-
- public:
-
- /// \brief Returns the maximal value plus one.
- ///
- /// Returns the maximal value plus one in the map.
- unsigned int size() const {
- return invMap.size();
- }
-
- /// \brief Swaps the position of the two items in the map.
- ///
- /// Swaps the position of the two items in the map.
- void swap(const Item& p, const Item& q) {
- int pi = Map::operator[](p);
- int qi = Map::operator[](q);
- Map::set(p, qi);
- invMap[qi] = p;
- Map::set(q, pi);
- invMap[pi] = q;
- }
-
- /// \brief Gives back the \e descriptor of the item.
- ///
- /// Gives back the mutable and unique \e descriptor of the map.
- int operator[](const Item& item) const {
- return Map::operator[](item);
- }
-
- /// \brief Gives back the item by its descriptor.
- ///
- /// Gives back th item by its descriptor.
- Item operator()(int id) const {
- return invMap[id];
- }
-
- private:
-
- typedef std::vector<Item> Container;
- Container invMap;
-
- public:
- /// \brief The inverse map type of DescriptorMap.
- ///
- /// The inverse map type of DescriptorMap.
- class InverseMap {
- public:
- /// \brief Constructor of the InverseMap.
- ///
- /// Constructor of the InverseMap.
- explicit InverseMap(const DescriptorMap& _inverted)
- : inverted(_inverted) {}
-
-
- /// The value type of the InverseMap.
- typedef typename DescriptorMap::Key Value;
- /// The key type of the InverseMap.
- typedef typename DescriptorMap::Value Key;
-
- /// \brief Subscript operator.
- ///
- /// Subscript operator. It gives back the item
- /// that the descriptor belongs to currently.
- Value operator[](const Key& key) const {
- return inverted(key);
- }
-
- /// \brief Size of the map.
- ///
- /// Returns the size of the map.
- unsigned int size() const {
- return inverted.size();
- }
-
- private:
- const DescriptorMap& inverted;
- };
-
- /// \brief Gives back the inverse of the map.
- ///
- /// Gives back the inverse of the map.
- const InverseMap inverse() const {
- return InverseMap(*this);
- }
- };
-
- /// \brief Returns the source of the given edge.
- ///
- /// The SourceMap gives back the source Node of the given edge.
- /// \see TargetMap
- /// \author Balazs Dezso
- template <typename Graph>
- class SourceMap {
- public:
-
- typedef typename Graph::Node Value;
- typedef typename Graph::Edge Key;
-
- /// \brief Constructor
- ///
- /// Constructor
- /// \param _graph The graph that the map belongs to.
- explicit SourceMap(const Graph& _graph) : graph(_graph) {}
-
- /// \brief The subscript operator.
- ///
- /// The subscript operator.
- /// \param edge The edge
- /// \return The source of the edge
- Value operator[](const Key& edge) const {
- return graph.source(edge);
- }
-
- private:
- const Graph& graph;
- };
-
- /// \brief Returns a \ref SourceMap class.
- ///
- /// This function just returns an \ref SourceMap class.
- /// \relates SourceMap
- template <typename Graph>
- inline SourceMap<Graph> sourceMap(const Graph& graph) {
- return SourceMap<Graph>(graph);
- }
-
- /// \brief Returns the target of the given edge.
- ///
- /// The TargetMap gives back the target Node of the given edge.
- /// \see SourceMap
- /// \author Balazs Dezso
- template <typename Graph>
- class TargetMap {
- public:
-
- typedef typename Graph::Node Value;
- typedef typename Graph::Edge Key;
-
- /// \brief Constructor
- ///
- /// Constructor
- /// \param _graph The graph that the map belongs to.
- explicit TargetMap(const Graph& _graph) : graph(_graph) {}
-
- /// \brief The subscript operator.
- ///
- /// The subscript operator.
- /// \param e The edge
- /// \return The target of the edge
- Value operator[](const Key& e) const {
- return graph.target(e);
- }
-
- private:
- const Graph& graph;
- };
-
- /// \brief Returns a \ref TargetMap class.
- ///
- /// This function just returns a \ref TargetMap class.
- /// \relates TargetMap
- template <typename Graph>
- inline TargetMap<Graph> targetMap(const Graph& graph) {
- return TargetMap<Graph>(graph);
- }
-
- /// \brief Returns the "forward" directed edge view of an undirected edge.
- ///
- /// Returns the "forward" directed edge view of an undirected edge.
- /// \see BackwardMap
- /// \author Balazs Dezso
- template <typename Graph>
- class ForwardMap {
- public:
-
- typedef typename Graph::Edge Value;
- typedef typename Graph::UEdge Key;
-
- /// \brief Constructor
- ///
- /// Constructor
- /// \param _graph The graph that the map belongs to.
- explicit ForwardMap(const Graph& _graph) : graph(_graph) {}
-
- /// \brief The subscript operator.
- ///
- /// The subscript operator.
- /// \param key An undirected edge
- /// \return The "forward" directed edge view of undirected edge
- Value operator[](const Key& key) const {
- return graph.direct(key, true);
- }
-
- private:
- const Graph& graph;
- };
-
- /// \brief Returns a \ref ForwardMap class.
- ///
- /// This function just returns an \ref ForwardMap class.
- /// \relates ForwardMap
- template <typename Graph>
- inline ForwardMap<Graph> forwardMap(const Graph& graph) {
- return ForwardMap<Graph>(graph);
- }
-
- /// \brief Returns the "backward" directed edge view of an undirected edge.
- ///
- /// Returns the "backward" directed edge view of an undirected edge.
- /// \see ForwardMap
- /// \author Balazs Dezso
- template <typename Graph>
- class BackwardMap {
- public:
-
- typedef typename Graph::Edge Value;
- typedef typename Graph::UEdge Key;
-
- /// \brief Constructor
- ///
- /// Constructor
- /// \param _graph The graph that the map belongs to.
- explicit BackwardMap(const Graph& _graph) : graph(_graph) {}
-
- /// \brief The subscript operator.
- ///
- /// The subscript operator.
- /// \param key An undirected edge
- /// \return The "backward" directed edge view of undirected edge
- Value operator[](const Key& key) const {
- return graph.direct(key, false);
- }
-
- private:
- const Graph& graph;
- };
-
- /// \brief Returns a \ref BackwardMap class
-
- /// This function just returns a \ref BackwardMap class.
- /// \relates BackwardMap
- template <typename Graph>
- inline BackwardMap<Graph> backwardMap(const Graph& graph) {
- return BackwardMap<Graph>(graph);
- }
-
- /// \brief Potential difference map
- ///
- /// If there is an potential map on the nodes then we
- /// can get an edge map as we get the substraction of the
- /// values of the target and source.
- template <typename Graph, typename NodeMap>
- class PotentialDifferenceMap {
- public:
- typedef typename Graph::Edge Key;
- typedef typename NodeMap::Value Value;
-
- /// \brief Constructor
- ///
- /// Contructor of the map
- explicit PotentialDifferenceMap(const Graph& _graph,
- const NodeMap& _potential)
- : graph(_graph), potential(_potential) {}
-
- /// \brief Const subscription operator
- ///
- /// Const subscription operator
- Value operator[](const Key& edge) const {
- return potential[graph.target(edge)] - potential[graph.source(edge)];
- }
-
- private:
- const Graph& graph;
- const NodeMap& potential;
- };
-
- /// \brief Returns a PotentialDifferenceMap.
- ///
- /// This function just returns a PotentialDifferenceMap.
- /// \relates PotentialDifferenceMap
- template <typename Graph, typename NodeMap>
- PotentialDifferenceMap<Graph, NodeMap>
- potentialDifferenceMap(const Graph& graph, const NodeMap& potential) {
- return PotentialDifferenceMap<Graph, NodeMap>(graph, potential);
- }
-
- /// \brief Map of the node in-degrees.
- ///
- /// This map returns the in-degree of a node. Once it is constructed,
- /// the degrees are stored in a standard NodeMap, so each query is done
- /// in constant time. On the other hand, the values are updated automatically
- /// whenever the graph changes.
- ///
- /// \warning Besides addNode() and addEdge(), a graph structure may provide
- /// alternative ways to modify the graph. The correct behavior of InDegMap
- /// is not guarantied if these additional features are used. For example
- /// the functions \ref ListGraph::changeSource() "changeSource()",
- /// \ref ListGraph::changeTarget() "changeTarget()" and
- /// \ref ListGraph::reverseEdge() "reverseEdge()"
- /// of \ref ListGraph will \e not update the degree values correctly.
- ///
- /// \sa OutDegMap
-
- template <typename _Graph>
- class InDegMap
- : protected ItemSetTraits<_Graph, typename _Graph::Edge>
- ::ItemNotifier::ObserverBase {
-
- public:
-
- typedef _Graph Graph;
- typedef int Value;
- typedef typename Graph::Node Key;
-
- typedef typename ItemSetTraits<_Graph, typename _Graph::Edge>
- ::ItemNotifier::ObserverBase Parent;
-
- private:
-
- class AutoNodeMap : public DefaultMap<_Graph, Key, int> {
- public:
-
- typedef DefaultMap<_Graph, Key, int> Parent;
- typedef typename Parent::Graph Graph;
-
- AutoNodeMap(const Graph& graph) : Parent(graph, 0) {}
-
- virtual void add(const Key& key) {
- Parent::add(key);
- Parent::set(key, 0);
- }
-
- virtual void add(const std::vector<Key>& keys) {
- Parent::add(keys);
- for (int i = 0; i < int(keys.size()); ++i) {
- Parent::set(keys[i], 0);
- }
- }
-
- virtual void build() {
- Parent::build();
- Key it;
- typename Parent::Notifier* nf = Parent::notifier();
- for (nf->first(it); it != INVALID; nf->next(it)) {
- Parent::set(it, 0);
- }
- }
- };
-
- public:
-
- /// \brief Constructor.
- ///
- /// Constructor for creating in-degree map.
- explicit InDegMap(const Graph& _graph) : graph(_graph), deg(_graph) {
- Parent::attach(graph.notifier(typename _Graph::Edge()));
-
- for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) {
- deg[it] = countInEdges(graph, it);
- }
- }
-
- /// Gives back the in-degree of a Node.
- int operator[](const Key& key) const {
- return deg[key];
- }
-
- protected:
-
- typedef typename Graph::Edge Edge;
-
- virtual void add(const Edge& edge) {
- ++deg[graph.target(edge)];
- }
-
- virtual void add(const std::vector<Edge>& edges) {
- for (int i = 0; i < int(edges.size()); ++i) {
- ++deg[graph.target(edges[i])];
- }
- }
-
- virtual void erase(const Edge& edge) {
- --deg[graph.target(edge)];
- }
-
- virtual void erase(const std::vector<Edge>& edges) {
- for (int i = 0; i < int(edges.size()); ++i) {
- --deg[graph.target(edges[i])];
- }
- }
-
- virtual void build() {
- for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) {
- deg[it] = countInEdges(graph, it);
- }
- }
-
- virtual void clear() {
- for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) {
- deg[it] = 0;
- }
- }
- private:
-
- const _Graph& graph;
- AutoNodeMap deg;
- };
-
- /// \brief Map of the node out-degrees.
- ///
- /// This map returns the out-degree of a node. Once it is constructed,
- /// the degrees are stored in a standard NodeMap, so each query is done
- /// in constant time. On the other hand, the values are updated automatically
- /// whenever the graph changes.
- ///
- /// \warning Besides addNode() and addEdge(), a graph structure may provide
- /// alternative ways to modify the graph. The correct behavior of OutDegMap
- /// is not guarantied if these additional features are used. For example
- /// the functions \ref ListGraph::changeSource() "changeSource()",
- /// \ref ListGraph::changeTarget() "changeTarget()" and
- /// \ref ListGraph::reverseEdge() "reverseEdge()"
- /// of \ref ListGraph will \e not update the degree values correctly.
- ///
- /// \sa InDegMap
-
- template <typename _Graph>
- class OutDegMap
- : protected ItemSetTraits<_Graph, typename _Graph::Edge>
- ::ItemNotifier::ObserverBase {
-
- public:
-
- typedef typename ItemSetTraits<_Graph, typename _Graph::Edge>
- ::ItemNotifier::ObserverBase Parent;
-
- typedef _Graph Graph;
- typedef int Value;
- typedef typename Graph::Node Key;
-
- private:
-
- class AutoNodeMap : public DefaultMap<_Graph, Key, int> {
- public:
-
- typedef DefaultMap<_Graph, Key, int> Parent;
- typedef typename Parent::Graph Graph;
-
- AutoNodeMap(const Graph& graph) : Parent(graph, 0) {}
-
- virtual void add(const Key& key) {
- Parent::add(key);
- Parent::set(key, 0);
- }
- virtual void add(const std::vector<Key>& keys) {
- Parent::add(keys);
- for (int i = 0; i < int(keys.size()); ++i) {
- Parent::set(keys[i], 0);
- }
- }
- virtual void build() {
- Parent::build();
- Key it;
- typename Parent::Notifier* nf = Parent::notifier();
- for (nf->first(it); it != INVALID; nf->next(it)) {
- Parent::set(it, 0);
- }
- }
- };
-
- public:
-
- /// \brief Constructor.
- ///
- /// Constructor for creating out-degree map.
- explicit OutDegMap(const Graph& _graph) : graph(_graph), deg(_graph) {
- Parent::attach(graph.notifier(typename _Graph::Edge()));
-
- for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) {
- deg[it] = countOutEdges(graph, it);
- }
- }
-
- /// Gives back the out-degree of a Node.
- int operator[](const Key& key) const {
- return deg[key];
- }
-
- protected:
-
- typedef typename Graph::Edge Edge;
-
- virtual void add(const Edge& edge) {
- ++deg[graph.source(edge)];
- }
-
- virtual void add(const std::vector<Edge>& edges) {
- for (int i = 0; i < int(edges.size()); ++i) {
- ++deg[graph.source(edges[i])];
- }
- }
-
- virtual void erase(const Edge& edge) {
- --deg[graph.source(edge)];
- }
-
- virtual void erase(const std::vector<Edge>& edges) {
- for (int i = 0; i < int(edges.size()); ++i) {
- --deg[graph.source(edges[i])];
- }
- }
-
- virtual void build() {
- for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) {
- deg[it] = countOutEdges(graph, it);
- }
- }
-
- virtual void clear() {
- for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) {
- deg[it] = 0;
- }
- }
- private:
-
- const _Graph& graph;
- AutoNodeMap deg;
- };
-
-
- ///Dynamic edge look up between given endpoints.
-
- ///\ingroup gutils
- ///Using this class, you can find an edge in a graph from a given
- ///source to a given target in amortized time <em>O(log d)</em>,
- ///where <em>d</em> is the out-degree of the source node.
- ///
- ///It is possible to find \e all parallel edges between two nodes with
- ///the \c findFirst() and \c findNext() members.
- ///
- ///See the \ref EdgeLookUp and \ref AllEdgeLookUp classes if your
- ///graph do not changed so frequently.
- ///
- ///This class uses a self-adjusting binary search tree, Sleator's
- ///and Tarjan's Splay tree for guarantee the logarithmic amortized
- ///time bound for edge lookups. This class also guarantees the
- ///optimal time bound in a constant factor for any distribution of
- ///queries.
- ///
- ///\param G The type of the underlying graph.
- ///
- ///\sa EdgeLookUp
- ///\sa AllEdgeLookUp
- template<class G>
- class DynEdgeLookUp
- : protected ItemSetTraits<G, typename G::Edge>::ItemNotifier::ObserverBase
- {
- public:
- typedef typename ItemSetTraits<G, typename G::Edge>
- ::ItemNotifier::ObserverBase Parent;
-
- GRAPH_TYPEDEFS(typename G);
- typedef G Graph;
-
- protected:
-
- class AutoNodeMap : public DefaultMap<G, Node, Edge> {
- public:
-
- typedef DefaultMap<G, Node, Edge> Parent;
-
- AutoNodeMap(const G& graph) : Parent(graph, INVALID) {}
-
- virtual void add(const Node& node) {
- Parent::add(node);
- Parent::set(node, INVALID);
- }
-
- virtual void add(const std::vector<Node>& nodes) {
- Parent::add(nodes);
- for (int i = 0; i < int(nodes.size()); ++i) {
- Parent::set(nodes[i], INVALID);
- }
- }
-
- virtual void build() {
- Parent::build();
- Node it;
- typename Parent::Notifier* nf = Parent::notifier();
- for (nf->first(it); it != INVALID; nf->next(it)) {
- Parent::set(it, INVALID);
- }
- }
- };
-
- const Graph &_g;
- AutoNodeMap _head;
- typename Graph::template EdgeMap<Edge> _parent;
- typename Graph::template EdgeMap<Edge> _left;
- typename Graph::template EdgeMap<Edge> _right;
-
- class EdgeLess {
- const Graph &g;
- public:
- EdgeLess(const Graph &_g) : g(_g) {}
- bool operator()(Edge a,Edge b) const
- {
- return g.target(a)<g.target(b);
- }
- };
-
- public:
-
- ///Constructor
-
- ///Constructor.
- ///
- ///It builds up the search database.
- DynEdgeLookUp(const Graph &g)
- : _g(g),_head(g),_parent(g),_left(g),_right(g)
- {
- Parent::attach(_g.notifier(typename Graph::Edge()));
- refresh();
- }
-
- protected:
-
- virtual void add(const Edge& edge) {
- insert(edge);
- }
-
- virtual void add(const std::vector<Edge>& edges) {
- for (int i = 0; i < int(edges.size()); ++i) {
- insert(edges[i]);
- }
- }
-
- virtual void erase(const Edge& edge) {
- remove(edge);
- }
-
- virtual void erase(const std::vector<Edge>& edges) {
- for (int i = 0; i < int(edges.size()); ++i) {
- remove(edges[i]);
- }
- }
-
- virtual void build() {
- refresh();
- }
-
- virtual void clear() {
- for(NodeIt n(_g);n!=INVALID;++n) {
- _head.set(n, INVALID);
- }
- }
-
- void insert(Edge edge) {
- Node s = _g.source(edge);
- Node t = _g.target(edge);
- _left.set(edge, INVALID);
- _right.set(edge, INVALID);
-
- Edge e = _head[s];
- if (e == INVALID) {
- _head.set(s, edge);
- _parent.set(edge, INVALID);
- return;
- }
- while (true) {
- if (t < _g.target(e)) {
- if (_left[e] == INVALID) {
- _left.set(e, edge);
- _parent.set(edge, e);
- splay(edge);
- return;
- } else {
- e = _left[e];
- }
- } else {
- if (_right[e] == INVALID) {
- _right.set(e, edge);
- _parent.set(edge, e);
- splay(edge);
- return;
- } else {
- e = _right[e];
- }
- }
- }
- }
-
- void remove(Edge edge) {
- if (_left[edge] == INVALID) {
- if (_right[edge] != INVALID) {
- _parent.set(_right[edge], _parent[edge]);
- }
- if (_parent[edge] != INVALID) {
- if (_left[_parent[edge]] == edge) {
- _left.set(_parent[edge], _right[edge]);
- } else {
- _right.set(_parent[edge], _right[edge]);
- }
- } else {
- _head.set(_g.source(edge), _right[edge]);
- }
- } else if (_right[edge] == INVALID) {
- _parent.set(_left[edge], _parent[edge]);
- if (_parent[edge] != INVALID) {
- if (_left[_parent[edge]] == edge) {
- _left.set(_parent[edge], _left[edge]);
- } else {
- _right.set(_parent[edge], _left[edge]);
- }
- } else {
- _head.set(_g.source(edge), _left[edge]);
- }
- } else {
- Edge e = _left[edge];
- if (_right[e] != INVALID) {
- e = _right[e];
- while (_right[e] != INVALID) {
- e = _right[e];
- }
- Edge s = _parent[e];
- _right.set(_parent[e], _left[e]);
- if (_left[e] != INVALID) {
- _parent.set(_left[e], _parent[e]);
- }
-
- _left.set(e, _left[edge]);
- _parent.set(_left[edge], e);
- _right.set(e, _right[edge]);
- _parent.set(_right[edge], e);
-
- _parent.set(e, _parent[edge]);
- if (_parent[edge] != INVALID) {
- if (_left[_parent[edge]] == edge) {
- _left.set(_parent[edge], e);
- } else {
- _right.set(_parent[edge], e);
- }
- }
- splay(s);
- } else {
- _right.set(e, _right[edge]);
- _parent.set(_right[edge], e);
-
- if (_parent[edge] != INVALID) {
- if (_left[_parent[edge]] == edge) {
- _left.set(_parent[edge], e);
- } else {
- _right.set(_parent[edge], e);
- }
- } else {
- _head.set(_g.source(edge), e);
- }
- }
- }
- }
-
- Edge refreshRec(std::vector<Edge> &v,int a,int b)
- {
- int m=(a+b)/2;
- Edge me=v[m];
- if (a < m) {
- Edge left = refreshRec(v,a,m-1);
- _left.set(me, left);
- _parent.set(left, me);
- } else {
- _left.set(me, INVALID);
- }
- if (m < b) {
- Edge right = refreshRec(v,m+1,b);
- _right.set(me, right);
- _parent.set(right, me);
- } else {
- _right.set(me, INVALID);
- }
- return me;
- }
-
- void refresh() {
- for(NodeIt n(_g);n!=INVALID;++n) {
- std::vector<Edge> v;
- for(OutEdgeIt e(_g,n);e!=INVALID;++e) v.push_back(e);
- if(v.size()) {
- std::sort(v.begin(),v.end(),EdgeLess(_g));
- Edge head = refreshRec(v,0,v.size()-1);
- _head.set(n, head);
- _parent.set(head, INVALID);
- }
- else _head.set(n, INVALID);
- }
- }
-
- void zig(Edge v) {
- Edge w = _parent[v];
- _parent.set(v, _parent[w]);
- _parent.set(w, v);
- _left.set(w, _right[v]);
- _right.set(v, w);
- if (_parent[v] != INVALID) {
- if (_right[_parent[v]] == w) {
- _right.set(_parent[v], v);
- } else {
- _left.set(_parent[v], v);
- }
- }
- if (_left[w] != INVALID){
- _parent.set(_left[w], w);
- }
- }
-
- void zag(Edge v) {
- Edge w = _parent[v];
- _parent.set(v, _parent[w]);
- _parent.set(w, v);
- _right.set(w, _left[v]);
- _left.set(v, w);
- if (_parent[v] != INVALID){
- if (_left[_parent[v]] == w) {
- _left.set(_parent[v], v);
- } else {
- _right.set(_parent[v], v);
- }
- }
- if (_right[w] != INVALID){
- _parent.set(_right[w], w);
- }
- }
-
- void splay(Edge v) {
- while (_parent[v] != INVALID) {
- if (v == _left[_parent[v]]) {
- if (_parent[_parent[v]] == INVALID) {
- zig(v);
- } else {
- if (_parent[v] == _left[_parent[_parent[v]]]) {
- zig(_parent[v]);
- zig(v);
- } else {
- zig(v);
- zag(v);
- }
- }
- } else {
- if (_parent[_parent[v]] == INVALID) {
- zag(v);
- } else {
- if (_parent[v] == _left[_parent[_parent[v]]]) {
- zag(v);
- zig(v);
- } else {
- zag(_parent[v]);
- zag(v);
- }
- }
- }
- }
- _head[_g.source(v)] = v;
- }
-
-
- public:
-
- ///Find an edge between two nodes.
-
- ///Find an edge between two nodes in time <em>O(</em>log<em>d)</em>, where
- /// <em>d</em> is the number of outgoing edges of \c s.
- ///\param s The source node
- ///\param t The target node
- ///\return An edge from \c s to \c t if there exists,
- ///\ref INVALID otherwise.
- Edge operator()(Node s, Node t) const
- {
- Edge e = _head[s];
- while (true) {
- if (_g.target(e) == t) {
- const_cast<DynEdgeLookUp&>(*this).splay(e);
- return e;
- } else if (t < _g.target(e)) {
- if (_left[e] == INVALID) {
- const_cast<DynEdgeLookUp&>(*this).splay(e);
- return INVALID;
- } else {
- e = _left[e];
- }
- } else {
- if (_right[e] == INVALID) {
- const_cast<DynEdgeLookUp&>(*this).splay(e);
- return INVALID;
- } else {
- e = _right[e];
- }
- }
- }
- }
-
- ///Find the first edge between two nodes.
-
- ///Find the first edge between two nodes in time
- /// <em>O(</em>log<em>d)</em>, where <em>d</em> is the number of
- /// outgoing edges of \c s.
- ///\param s The source node
- ///\param t The target node
- ///\return An edge from \c s to \c t if there exists, \ref INVALID
- /// otherwise.
- Edge findFirst(Node s, Node t) const
- {
- Edge e = _head[s];
- Edge r = INVALID;
- while (true) {
- if (_g.target(e) < t) {
- if (_right[e] == INVALID) {
- const_cast<DynEdgeLookUp&>(*this).splay(e);
- return r;
- } else {
- e = _right[e];
- }
- } else {
- if (_g.target(e) == t) {
- r = e;
- }
- if (_left[e] == INVALID) {
- const_cast<DynEdgeLookUp&>(*this).splay(e);
- return r;
- } else {
- e = _left[e];
- }
- }
- }
- }
-
- ///Find the next edge between two nodes.
-
- ///Find the next edge between two nodes in time
- /// <em>O(</em>log<em>d)</em>, where <em>d</em> is the number of
- /// outgoing edges of \c s.
- ///\param s The source node
- ///\param t The target node
- ///\return An edge from \c s to \c t if there exists, \ref INVALID
- /// otherwise.
-
- ///\note If \c e is not the result of the previous \c findFirst()
- ///operation then the amorized time bound can not be guaranteed.
-#ifdef DOXYGEN
- Edge findNext(Node s, Node t, Edge e) const
-#else
- Edge findNext(Node, Node t, Edge e) const
-#endif
- {
- if (_right[e] != INVALID) {
- e = _right[e];
- while (_left[e] != INVALID) {
- e = _left[e];
- }
- const_cast<DynEdgeLookUp&>(*this).splay(e);
- } else {
- while (_parent[e] != INVALID && _right[_parent[e]] == e) {
- e = _parent[e];
- }
- if (_parent[e] == INVALID) {
- return INVALID;
- } else {
- e = _parent[e];
- const_cast<DynEdgeLookUp&>(*this).splay(e);
- }
- }
- if (_g.target(e) == t) return e;
- else return INVALID;
- }
-
- };
-
- ///Fast edge look up between given endpoints.
-
- ///\ingroup gutils
- ///Using this class, you can find an edge in a graph from a given
- ///source to a given target in time <em>O(log d)</em>,
- ///where <em>d</em> is the out-degree of the source node.
- ///
- ///It is not possible to find \e all parallel edges between two nodes.
- ///Use \ref AllEdgeLookUp for this purpose.
- ///
- ///\warning This class is static, so you should refresh() (or at least
- ///refresh(Node)) this data structure
- ///whenever the graph changes. This is a time consuming (superlinearly
- ///proportional (<em>O(m</em>log<em>m)</em>) to the number of edges).
- ///
- ///\param G The type of the underlying graph.
- ///
- ///\sa DynEdgeLookUp
- ///\sa AllEdgeLookUp
- template<class G>
- class EdgeLookUp
- {
- public:
- GRAPH_TYPEDEFS(typename G);
- typedef G Graph;
-
- protected:
- const Graph &_g;
- typename Graph::template NodeMap<Edge> _head;
- typename Graph::template EdgeMap<Edge> _left;
- typename Graph::template EdgeMap<Edge> _right;
-
- class EdgeLess {
- const Graph &g;
- public:
- EdgeLess(const Graph &_g) : g(_g) {}
- bool operator()(Edge a,Edge b) const
- {
- return g.target(a)<g.target(b);
- }
- };
-
- public:
-
- ///Constructor
-
- ///Constructor.
- ///
- ///It builds up the search database, which remains valid until the graph
- ///changes.
- EdgeLookUp(const Graph &g) :_g(g),_head(g),_left(g),_right(g) {refresh();}
-
- private:
- Edge refreshRec(std::vector<Edge> &v,int a,int b)
- {
- int m=(a+b)/2;
- Edge me=v[m];
- _left[me] = a<m?refreshRec(v,a,m-1):INVALID;
- _right[me] = m<b?refreshRec(v,m+1,b):INVALID;
- return me;
- }
- public:
- ///Refresh the data structure at a node.
-
- ///Build up the search database of node \c n.
- ///
- ///It runs in time <em>O(d</em>log<em>d)</em>, where <em>d</em> is
- ///the number of the outgoing edges of \c n.
- void refresh(Node n)
- {
- std::vector<Edge> v;
- for(OutEdgeIt e(_g,n);e!=INVALID;++e) v.push_back(e);
- if(v.size()) {
- std::sort(v.begin(),v.end(),EdgeLess(_g));
- _head[n]=refreshRec(v,0,v.size()-1);
- }
- else _head[n]=INVALID;
- }
- ///Refresh the full data structure.
-
- ///Build up the full search database. In fact, it simply calls
- ///\ref refresh(Node) "refresh(n)" for each node \c n.
- ///
- ///It runs in time <em>O(m</em>log<em>D)</em>, where <em>m</em> is
- ///the number of the edges of \c n and <em>D</em> is the maximum
- ///out-degree of the graph.
-
- void refresh()
- {
- for(NodeIt n(_g);n!=INVALID;++n) refresh(n);
- }
-
- ///Find an edge between two nodes.
-
- ///Find an edge between two nodes in time <em>O(</em>log<em>d)</em>, where
- /// <em>d</em> is the number of outgoing edges of \c s.
- ///\param s The source node
- ///\param t The target node
- ///\return An edge from \c s to \c t if there exists,
- ///\ref INVALID otherwise.
- ///
- ///\warning If you change the graph, refresh() must be called before using
- ///this operator. If you change the outgoing edges of
- ///a single node \c n, then
- ///\ref refresh(Node) "refresh(n)" is enough.
- ///
- Edge operator()(Node s, Node t) const
- {
- Edge e;
- for(e=_head[s];
- e!=INVALID&&_g.target(e)!=t;
- e = t < _g.target(e)?_left[e]:_right[e]) ;
- return e;
- }
-
- };
-
- ///Fast look up of all edges between given endpoints.
-
- ///\ingroup gutils
- ///This class is the same as \ref EdgeLookUp, with the addition
- ///that it makes it possible to find all edges between given endpoints.
- ///
- ///\warning This class is static, so you should refresh() (or at least
- ///refresh(Node)) this data structure
- ///whenever the graph changes. This is a time consuming (superlinearly
- ///proportional (<em>O(m</em>log<em>m)</em>) to the number of edges).
- ///
- ///\param G The type of the underlying graph.
- ///
- ///\sa DynEdgeLookUp
- ///\sa EdgeLookUp
- template<class G>
- class AllEdgeLookUp : public EdgeLookUp<G>
- {
- using EdgeLookUp<G>::_g;
- using EdgeLookUp<G>::_right;
- using EdgeLookUp<G>::_left;
- using EdgeLookUp<G>::_head;
-
- GRAPH_TYPEDEFS(typename G);
- typedef G Graph;
-
- typename Graph::template EdgeMap<Edge> _next;
-
- Edge refreshNext(Edge head,Edge next=INVALID)
- {
- if(head==INVALID) return next;
- else {
- next=refreshNext(_right[head],next);
-// _next[head]=next;
- _next[head]=( next!=INVALID && _g.target(next)==_g.target(head))
- ? next : INVALID;
- return refreshNext(_left[head],head);
- }
- }
-
- void refreshNext()
- {
- for(NodeIt n(_g);n!=INVALID;++n) refreshNext(_head[n]);
- }
-
- public:
- ///Constructor
-
- ///Constructor.
- ///
- ///It builds up the search database, which remains valid until the graph
- ///changes.
- AllEdgeLookUp(const Graph &g) : EdgeLookUp<G>(g), _next(g) {refreshNext();}
-
- ///Refresh the data structure at a node.
-
- ///Build up the search database of node \c n.
- ///
- ///It runs in time <em>O(d</em>log<em>d)</em>, where <em>d</em> is
- ///the number of the outgoing edges of \c n.
-
- void refresh(Node n)
- {
- EdgeLookUp<G>::refresh(n);
- refreshNext(_head[n]);
- }
-
- ///Refresh the full data structure.
-
- ///Build up the full search database. In fact, it simply calls
- ///\ref refresh(Node) "refresh(n)" for each node \c n.
- ///
- ///It runs in time <em>O(m</em>log<em>D)</em>, where <em>m</em> is
- ///the number of the edges of \c n and <em>D</em> is the maximum
- ///out-degree of the graph.
-
- void refresh()
- {
- for(NodeIt n(_g);n!=INVALID;++n) refresh(_head[n]);
- }
-
- ///Find an edge between two nodes.
-
- ///Find an edge between two nodes.
- ///\param s The source node
- ///\param t The target node
- ///\param prev The previous edge between \c s and \c t. It it is INVALID or
- ///not given, the operator finds the first appropriate edge.
- ///\return An edge from \c s to \c t after \c prev or
- ///\ref INVALID if there is no more.
- ///
- ///For example, you can count the number of edges from \c u to \c v in the
- ///following way.
- ///\code
- ///AllEdgeLookUp<ListGraph> ae(g);
- ///...
- ///int n=0;
- ///for(Edge e=ae(u,v);e!=INVALID;e=ae(u,v,e)) n++;
- ///\endcode
- ///
- ///Finding the first edge take <em>O(</em>log<em>d)</em> time, where
- /// <em>d</em> is the number of outgoing edges of \c s. Then, the
- ///consecutive edges are found in constant time.
- ///
- ///\warning If you change the graph, refresh() must be called before using
- ///this operator. If you change the outgoing edges of
- ///a single node \c n, then
- ///\ref refresh(Node) "refresh(n)" is enough.
- ///
-#ifdef DOXYGEN
- Edge operator()(Node s, Node t, Edge prev=INVALID) const {}
-#else
- using EdgeLookUp<G>::operator() ;
- Edge operator()(Node s, Node t, Edge prev) const
- {
- return prev==INVALID?(*this)(s,t):_next[prev];
- }
-#endif
-
- };
-
- /// @}
-
-} //END OF NAMESPACE LEMON
-
-#endif
diff --git a/src/lemon/list_graph.h b/src/lemon/list_graph.h
deleted file mode 100644
index 86d033a..0000000
--- a/src/lemon/list_graph.h
+++ /dev/null
@@ -1,2249 +0,0 @@
-/* -*- C++ -*-
- *
- * This file is a part of LEMON, a generic C++ optimization library
- *
- * Copyright (C) 2003-2008
- * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
- * (Egervary Research Group on Combinatorial Optimization, EGRES).
- *
- * Permission to use, modify and distribute this software is granted
- * provided that this copyright notice appears in all copies. For
- * precise terms see the accompanying LICENSE file.
- *
- * This software is provided "AS IS" with no warranty of any kind,
- * express or implied, and with no claim as to its suitability for any
- * purpose.
- *
- */
-
-#ifndef LEMON_LIST_GRAPH_H
-#define LEMON_LIST_GRAPH_H
-
-///\ingroup graphs
-///\file
-///\brief ListGraph, ListUGraph classes.
-
-#include <lemon/bits/base_extender.h>
-#include <lemon/bits/graph_extender.h>
-
-#include <lemon/error.h>
-
-#include <vector>
-#include <list>
-
-namespace lemon {
-
- class ListGraphBase {
-
- protected:
- struct NodeT {
- int first_in, first_out;
- int prev, next;
- };
-
- struct EdgeT {
- int target, source;
- int prev_in, prev_out;
- int next_in, next_out;
- };
-
- std::vector<NodeT> nodes;
-
- int first_node;
-
- int first_free_node;
-
- std::vector<EdgeT> edges;
-
- int first_free_edge;
-
- public:
-
- typedef ListGraphBase Graph;
-
- class Node {
- friend class ListGraphBase;
- protected:
-
- int id;
- explicit Node(int pid) { id = pid;}
-
- public:
- Node() {}
- Node (Invalid) { id = -1; }
- bool operator==(const Node& node) const {return id == node.id;}
- bool operator!=(const Node& node) const {return id != node.id;}
- bool operator<(const Node& node) const {return id < node.id;}
- };
-
- class Edge {
- friend class ListGraphBase;
- protected:
-
- int id;
- explicit Edge(int pid) { id = pid;}
-
- public:
- Edge() {}
- Edge (Invalid) { id = -1; }
- bool operator==(const Edge& edge) const {return id == edge.id;}
- bool operator!=(const Edge& edge) const {return id != edge.id;}
- bool operator<(const Edge& edge) const {return id < edge.id;}
- };
-
-
-
- ListGraphBase()
- : nodes(), first_node(-1),
- first_free_node(-1), edges(), first_free_edge(-1) {}
-
-
- int maxNodeId() const { return nodes.size()-1; }
- int maxEdgeId() const { return edges.size()-1; }
-
- Node source(Edge e) const { return Node(edges[e.id].source); }
- Node target(Edge e) const { return Node(edges[e.id].target); }
-
-
- void first(Node& node) const {
- node.id = first_node;
- }
-
- void next(Node& node) const {
- node.id = nodes[node.id].next;
- }
-
-
- void first(Edge& e) const {
- int n;
- for(n = first_node;
- n!=-1 && nodes[n].first_in == -1;
- n = nodes[n].next);
- e.id = (n == -1) ? -1 : nodes[n].first_in;
- }
-
- void next(Edge& edge) const {
- if (edges[edge.id].next_in != -1) {
- edge.id = edges[edge.id].next_in;
- } else {
- int n;
- for(n = nodes[edges[edge.id].target].next;
- n!=-1 && nodes[n].first_in == -1;
- n = nodes[n].next);
- edge.id = (n == -1) ? -1 : nodes[n].first_in;
- }
- }
-
- void firstOut(Edge &e, const Node& v) const {
- e.id = nodes[v.id].first_out;
- }
- void nextOut(Edge &e) const {
- e.id=edges[e.id].next_out;
- }
-
- void firstIn(Edge &e, const Node& v) const {
- e.id = nodes[v.id].first_in;
- }
- void nextIn(Edge &e) const {
- e.id=edges[e.id].next_in;
- }
-
-
- static int id(Node v) { return v.id; }
- static int id(Edge e) { return e.id; }
-
- static Node nodeFromId(int id) { return Node(id);}
- static Edge edgeFromId(int id) { return Edge(id);}
-
- Node addNode() {
- int n;
-
- if(first_free_node==-1) {
- n = nodes.size();
- nodes.push_back(NodeT());
- } else {
- n = first_free_node;
- first_free_node = nodes[n].next;
- }
-
- nodes[n].next = first_node;
- if(first_node != -1) nodes[first_node].prev = n;
- first_node = n;
- nodes[n].prev = -1;
-
- nodes[n].first_in = nodes[n].first_out = -1;
-
- return Node(n);
- }
-
- Edge addEdge(Node u, Node v) {
- int n;
-
- if (first_free_edge == -1) {
- n = edges.size();
- edges.push_back(EdgeT());
- } else {
- n = first_free_edge;
- first_free_edge = edges[n].next_in;
- }
-
- edges[n].source = u.id;
- edges[n].target = v.id;
-
- edges[n].next_out = nodes[u.id].first_out;
- if(nodes[u.id].first_out != -1) {
- edges[nodes[u.id].first_out].prev_out = n;
- }
-
- edges[n].next_in = nodes[v.id].first_in;
- if(nodes[v.id].first_in != -1) {
- edges[nodes[v.id].first_in].prev_in = n;
- }
-
- edges[n].prev_in = edges[n].prev_out = -1;
-
- nodes[u.id].first_out = nodes[v.id].first_in = n;
-
- return Edge(n);
- }
-
- void erase(const Node& node) {
- int n = node.id;
-
- if(nodes[n].next != -1) {
- nodes[nodes[n].next].prev = nodes[n].prev;
- }
-
- if(nodes[n].prev != -1) {
- nodes[nodes[n].prev].next = nodes[n].next;
- } else {
- first_node = nodes[n].next;
- }
-
- nodes[n].next = first_free_node;
- first_free_node = n;
-
- }
-
- void erase(const Edge& edge) {
- int n = edge.id;
-
- if(edges[n].next_in!=-1) {
- edges[edges[n].next_in].prev_in = edges[n].prev_in;
- }
-
- if(edges[n].prev_in!=-1) {
- edges[edges[n].prev_in].next_in = edges[n].next_in;
- } else {
- nodes[edges[n].target].first_in = edges[n].next_in;
- }
-
-
- if(edges[n].next_out!=-1) {
- edges[edges[n].next_out].prev_out = edges[n].prev_out;
- }
-
- if(edges[n].prev_out!=-1) {
- edges[edges[n].prev_out].next_out = edges[n].next_out;
- } else {
- nodes[edges[n].source].first_out = edges[n].next_out;
- }
-
- edges[n].next_in = first_free_edge;
- first_free_edge = n;
-
- }
-
- void clear() {
- edges.clear();
- nodes.clear();
- first_node = first_free_node = first_free_edge = -1;
- }
-
- protected:
- void changeTarget(Edge e, Node n)
- {
- if(edges[e.id].next_in != -1)
- edges[edges[e.id].next_in].prev_in = edges[e.id].prev_in;
- if(edges[e.id].prev_in != -1)
- edges[edges[e.id].prev_in].next_in = edges[e.id].next_in;
- else nodes[edges[e.id].target].first_in = edges[e.id].next_in;
- if (nodes[n.id].first_in != -1) {
- edges[nodes[n.id].first_in].prev_in = e.id;
- }
- edges[e.id].target = n.id;
- edges[e.id].prev_in = -1;
- edges[e.id].next_in = nodes[n.id].first_in;
- nodes[n.id].first_in = e.id;
- }
- void changeSource(Edge e, Node n)
- {
- if(edges[e.id].next_out != -1)
- edges[edges[e.id].next_out].prev_out = edges[e.id].prev_out;
- if(edges[e.id].prev_out != -1)
- edges[edges[e.id].prev_out].next_out = edges[e.id].next_out;
- else nodes[edges[e.id].source].first_out = edges[e.id].next_out;
- if (nodes[n.id].first_out != -1) {
- edges[nodes[n.id].first_out].prev_out = e.id;
- }
- edges[e.id].source = n.id;
- edges[e.id].prev_out = -1;
- edges[e.id].next_out = nodes[n.id].first_out;
- nodes[n.id].first_out = e.id;
- }
-
- };
-
- typedef GraphExtender<ListGraphBase> ExtendedListGraphBase;
-
- /// \addtogroup graphs
- /// @{
-
- ///A list graph class.
-
- ///This is a simple and fast graph implementation.
- ///
- ///It conforms to the \ref concepts::Graph "Graph concept" and it
- ///also provides several additional useful extra functionalities.
- ///The most of the member functions and nested classes are
- ///documented only in the concept class.
- ///
- ///An important extra feature of this graph implementation is that
- ///its maps are real \ref concepts::ReferenceMap "reference map"s.
- ///
- ///\sa concepts::Graph.
-
- class ListGraph : public ExtendedListGraphBase {
- private:
- ///ListGraph is \e not copy constructible. Use GraphCopy() instead.
-
- ///ListGraph is \e not copy constructible. Use GraphCopy() instead.
- ///
- ListGraph(const ListGraph &) :ExtendedListGraphBase() {};
- ///\brief Assignment of ListGraph to another one is \e not allowed.
- ///Use GraphCopy() instead.
-
- ///Assignment of ListGraph to another one is \e not allowed.
- ///Use GraphCopy() instead.
- void operator=(const ListGraph &) {}
- public:
-
- typedef ExtendedListGraphBase Parent;
-
- /// Constructor
-
- /// Constructor.
- ///
- ListGraph() {}
-
- ///Add a new node to the graph.
-
- /// \return the new node.
- ///
- Node addNode() { return Parent::addNode(); }
-
- ///Add a new edge to the graph.
-
- ///Add a new edge to the graph with source node \c s
- ///and target node \c t.
- ///\return the new edge.
- Edge addEdge(const Node& s, const Node& t) {
- return Parent::addEdge(s, t);
- }
-
- /// Changes the target of \c e to \c n
-
- /// Changes the target of \c e to \c n
- ///
- ///\note The <tt>EdgeIt</tt>s and <tt>OutEdgeIt</tt>s referencing
- ///the changed edge remain valid. However <tt>InEdgeIt</tt>s are
- ///invalidated.
- ///\warning This functionality cannot be used together with the Snapshot
- ///feature.
- void changeTarget(Edge e, Node n) {
- Parent::changeTarget(e,n);
- }
- /// Changes the source of \c e to \c n
-
- /// Changes the source of \c e to \c n
- ///
- ///\note The <tt>EdgeIt</tt>s and <tt>InEdgeIt</tt>s referencing
- ///the changed edge remain valid. However <tt>OutEdgeIt</tt>s are
- ///invalidated.
- ///\warning This functionality cannot be used together with the Snapshot
- ///feature.
- void changeSource(Edge e, Node n) {
- Parent::changeSource(e,n);
- }
-
- /// Invert the direction of an edge.
-
- ///\note The <tt>EdgeIt</tt>s referencing the changed edge remain
- ///valid. However <tt>OutEdgeIt</tt>s and <tt>InEdgeIt</tt>s are
- ///invalidated.
- ///\warning This functionality cannot be used together with the Snapshot
- ///feature.
- void reverseEdge(Edge e) {
- Node t=target(e);
- changeTarget(e,source(e));
- changeSource(e,t);
- }
-
- /// Using this it is possible to avoid the superfluous memory
- /// allocation: if you know that the graph you want to build will
- /// be very large (e.g. it will contain millions of nodes and/or edges)
- /// then it is worth reserving space for this amount before starting
- /// to build the graph.
- /// \sa reserveEdge
- void reserveNode(int n) { nodes.reserve(n); };
-
- /// \brief Using this it is possible to avoid the superfluous memory
- /// allocation.
-
- /// Using this it is possible to avoid the superfluous memory
- /// allocation: if you know that the graph you want to build will
- /// be very large (e.g. it will contain millions of nodes and/or edges)
- /// then it is worth reserving space for this amount before starting
- /// to build the graph.
- /// \sa reserveNode
- void reserveEdge(int m) { edges.reserve(m); };
-
- ///Contract two nodes.
-
- ///This function contracts two nodes.
- ///
- ///Node \p b will be removed but instead of deleting
- ///incident edges, they will be joined to \p a.
- ///The last parameter \p r controls whether to remove loops. \c true
- ///means that loops will be removed.
- ///
- ///\note The <tt>EdgeIt</tt>s
- ///referencing a moved edge remain
- ///valid. However <tt>InEdgeIt</tt>s and <tt>OutEdgeIt</tt>s
- ///may be invalidated.
- ///\warning This functionality cannot be used together with the Snapshot
- ///feature.
- void contract(Node a, Node b, bool r = true)
- {
- for(OutEdgeIt e(*this,b);e!=INVALID;) {
- OutEdgeIt f=e;
- ++f;
- if(r && target(e)==a) erase(e);
- else changeSource(e,a);
- e=f;
- }
- for(InEdgeIt e(*this,b);e!=INVALID;) {
- InEdgeIt f=e;
- ++f;
- if(r && source(e)==a) erase(e);
- else changeTarget(e,a);
- e=f;
- }
- erase(b);
- }
-
- ///Split a node.
-
- ///This function splits a node. First a new node is added to the graph,
- ///then the source of each outgoing edge of \c n is moved to this new node.
- ///If \c connect is \c true (this is the default value), then a new edge
- ///from \c n to the newly created node is also added.
- ///\return The newly created node.
- ///
- ///\note The <tt>EdgeIt</tt>s referencing a moved edge remain
- ///valid. However <tt>InEdgeIt</tt>s and <tt>OutEdgeIt</tt>s may
- ///be invalidated.
- ///
- ///\warning This functionality cannot be used together with the
- ///Snapshot feature. \todo It could be implemented in a bit
- ///faster way.
- Node split(Node n, bool connect = true) {
- Node b = addNode();
- for(OutEdgeIt e(*this,n);e!=INVALID;) {
- OutEdgeIt f=e;
- ++f;
- changeSource(e,b);
- e=f;
- }
- if (connect) addEdge(n,b);
- return b;
- }
-
- ///Split an edge.
-
- ///This function splits an edge. First a new node \c b is added to
- ///the graph, then the original edge is re-targeted to \c
- ///b. Finally an edge from \c b to the original target is added.
- ///\return The newly created node.
- ///\warning This functionality
- ///cannot be used together with the Snapshot feature.
- Node split(Edge e) {
- Node b = addNode();
- addEdge(b,target(e));
- changeTarget(e,b);
- return b;
- }
-
- /// \brief Class to make a snapshot of the graph and restore
- /// to it later.
- ///
- /// Class to make a snapshot of the graph and to restore it
- /// later.
- ///
- /// The newly added nodes and edges can be removed using the
- /// restore() function.
- ///
- /// \warning Edge and node deletions cannot be restored. This
- /// events invalidate the snapshot.
- class Snapshot {
- protected:
-
- typedef Parent::NodeNotifier NodeNotifier;
-
- class NodeObserverProxy : public NodeNotifier::ObserverBase {
- public:
-
- NodeObserverProxy(Snapshot& _snapshot)
- : snapshot(_snapshot) {}
-
- using NodeNotifier::ObserverBase::attach;
- using NodeNotifier::ObserverBase::detach;
- using NodeNotifier::ObserverBase::attached;
-
- protected:
-
- virtual void add(const Node& node) {
- snapshot.addNode(node);
- }
- virtual void add(const std::vector<Node>& nodes) {
- for (int i = nodes.size() - 1; i >= 0; ++i) {
- snapshot.addNode(nodes[i]);
- }
- }
- virtual void erase(const Node& node) {
- snapshot.eraseNode(node);
- }
- virtual void erase(const std::vector<Node>& nodes) {
- for (int i = 0; i < int(nodes.size()); ++i) {
- snapshot.eraseNode(nodes[i]);
- }
- }
- virtual void build() {
- Node node;
- std::vector<Node> nodes;
- for (notifier()->first(node); node != INVALID;
- notifier()->next(node)) {
- nodes.push_back(node);
- }
- for (int i = nodes.size() - 1; i >= 0; --i) {
- snapshot.addNode(nodes[i]);
- }
- }
- virtual void clear() {
- Node node;
- for (notifier()->first(node); node != INVALID;
- notifier()->next(node)) {
- snapshot.eraseNode(node);
- }
- }
-
- Snapshot& snapshot;
- };
-
- class EdgeObserverProxy : public EdgeNotifier::ObserverBase {
- public:
-
- EdgeObserverProxy(Snapshot& _snapshot)
- : snapshot(_snapshot) {}
-
- using EdgeNotifier::ObserverBase::attach;
- using EdgeNotifier::ObserverBase::detach;
- using EdgeNotifier::ObserverBase::attached;
-
- protected:
-
- virtual void add(const Edge& edge) {
- snapshot.addEdge(edge);
- }
- virtual void add(const std::vector<Edge>& edges) {
- for (int i = edges.size() - 1; i >= 0; ++i) {
- snapshot.addEdge(edges[i]);
- }
- }
- virtual void erase(const Edge& edge) {
- snapshot.eraseEdge(edge);
- }
- virtual void erase(const std::vector<Edge>& edges) {
- for (int i = 0; i < int(edges.size()); ++i) {
- snapshot.eraseEdge(edges[i]);
- }
- }
- virtual void build() {
- Edge edge;
- std::vector<Edge> edges;
- for (notifier()->first(edge); edge != INVALID;
- notifier()->next(edge)) {
- edges.push_back(edge);
- }
- for (int i = edges.size() - 1; i >= 0; --i) {
- snapshot.addEdge(edges[i]);
- }
- }
- virtual void clear() {
- Edge edge;
- for (notifier()->first(edge); edge != INVALID;
- notifier()->next(edge)) {
- snapshot.eraseEdge(edge);
- }
- }
-
- Snapshot& snapshot;
- };
-
- ListGraph *graph;
-
- NodeObserverProxy node_observer_proxy;
- EdgeObserverProxy edge_observer_proxy;
-
- std::list<Node> added_nodes;
- std::list<Edge> added_edges;
-
-
- void addNode(const Node& node) {
- added_nodes.push_front(node);
- }
- void eraseNode(const Node& node) {
- std::list<Node>::iterator it =
- std::find(added_nodes.begin(), added_nodes.end(), node);
- if (it == added_nodes.end()) {
- clear();
- edge_observer_proxy.detach();
- throw NodeNotifier::ImmediateDetach();
- } else {
- added_nodes.erase(it);
- }
- }
-
- void addEdge(const Edge& edge) {
- added_edges.push_front(edge);
- }
- void eraseEdge(const Edge& edge) {
- std::list<Edge>::iterator it =
- std::find(added_edges.begin(), added_edges.end(), edge);
- if (it == added_edges.end()) {
- clear();
- node_observer_proxy.detach();
- throw EdgeNotifier::ImmediateDetach();
- } else {
- added_edges.erase(it);
- }
- }
-
- void attach(ListGraph &_graph) {
- graph = &_graph;
- node_observer_proxy.attach(graph->notifier(Node()));
- edge_observer_proxy.attach(graph->notifier(Edge()));
- }
-
- void detach() {
- node_observer_proxy.detach();
- edge_observer_proxy.detach();
- }
-
- bool attached() const {
- return node_observer_proxy.attached();
- }
-
- void clear() {
- added_nodes.clear();
- added_edges.clear();
- }
-
- public:
-
- /// \brief Default constructor.
- ///
- /// Default constructor.
- /// To actually make a snapshot you must call save().
- Snapshot()
- : graph(0), node_observer_proxy(*this),
- edge_observer_proxy(*this) {}
-
- /// \brief Constructor that immediately makes a snapshot.
- ///
- /// This constructor immediately makes a snapshot of the graph.
- /// \param _graph The graph we make a snapshot of.
- Snapshot(ListGraph &_graph)
- : node_observer_proxy(*this),
- edge_observer_proxy(*this) {
- attach(_graph);
- }
-
- /// \brief Make a snapshot.
- ///
- /// Make a snapshot of the graph.
- ///
- /// This function can be called more than once. In case of a repeated
- /// call, the previous snapshot gets lost.
- /// \param _graph The graph we make the snapshot of.
- void save(ListGraph &_graph) {
- if (attached()) {
- detach();
- clear();
- }
- attach(_graph);
- }
-
- /// \brief Undo the changes until the last snapshot.
- //
- /// Undo the changes until the last snapshot created by save().
- void restore() {
- detach();
- for(std::list<Edge>::iterator it = added_edges.begin();
- it != added_edges.end(); ++it) {
- graph->erase(*it);
- }
- for(std::list<Node>::iterator it = added_nodes.begin();
- it != added_nodes.end(); ++it) {
- graph->erase(*it);
- }
- clear();
- }
-
- /// \brief Gives back true when the snapshot is valid.
- ///
- /// Gives back true when the snapshot is valid.
- bool valid() const {
- return attached();
- }
- };
-
- };
-
- ///@}
-
- class ListUGraphBase {
-
- protected:
-
- struct NodeT {
- int first_out;
- int prev, next;
- };
-
- struct EdgeT {
- int target;
- int prev_out, next_out;
- };
-
- std::vector<NodeT> nodes;
-
- int first_node;
-
- int first_free_node;
-
- std::vector<EdgeT> edges;
-
- int first_free_edge;
-
- public:
-
- typedef ListUGraphBase Graph;
-
- class Node;
- class Edge;
- class UEdge;
-
- class Node {
- friend class ListUGraphBase;
- protected:
-
- int id;
- explicit Node(int pid) { id = pid;}
-
- public:
- Node() {}
- Node (Invalid) { id = -1; }
- bool operator==(const Node& node) const {return id == node.id;}
- bool operator!=(const Node& node) const {return id != node.id;}
- bool operator<(const Node& node) const {return id < node.id;}
- };
-
- class UEdge {
- friend class ListUGraphBase;
- protected:
-
- int id;
- explicit UEdge(int pid) { id = pid;}
-
- public:
- UEdge() {}
- UEdge (Invalid) { id = -1; }
- bool operator==(const UEdge& edge) const {return id == edge.id;}
- bool operator!=(const UEdge& edge) const {return id != edge.id;}
- bool operator<(const UEdge& edge) const {return id < edge.id;}
- };
-
- class Edge {
- friend class ListUGraphBase;
- protected:
-
- int id;
- explicit Edge(int pid) { id = pid;}
-
- public:
- operator UEdge() const { return uEdgeFromId(id / 2); }
-
- Edge() {}
- Edge (Invalid) { id = -1; }
- bool operator==(const Edge& edge) const {return id == edge.id;}
- bool operator!=(const Edge& edge) const {return id != edge.id;}
- bool operator<(const Edge& edge) const {return id < edge.id;}
- };
-
-
-
- ListUGraphBase()
- : nodes(), first_node(-1),
- first_free_node(-1), edges(), first_free_edge(-1) {}
-
-
- int maxNodeId() const { return nodes.size()-1; }
- int maxUEdgeId() const { return edges.size() / 2 - 1; }
- int maxEdgeId() const { return edges.size()-1; }
-
- Node source(Edge e) const { return Node(edges[e.id ^ 1].target); }
- Node target(Edge e) const { return Node(edges[e.id].target); }
-
- Node source(UEdge e) const { return Node(edges[2 * e.id].target); }
- Node target(UEdge e) const { return Node(edges[2 * e.id + 1].target); }
-
- static bool direction(Edge e) {
- return (e.id & 1) == 1;
- }
-
- static Edge direct(UEdge e, bool d) {
- return Edge(e.id * 2 + (d ? 1 : 0));
- }
-
- void first(Node& node) const {
- node.id = first_node;
- }
-
- void next(Node& node) const {
- node.id = nodes[node.id].next;
- }
-
- void first(Edge& e) const {
- int n = first_node;
- while (n != -1 && nodes[n].first_out == -1) {
- n = nodes[n].next;
- }
- e.id = (n == -1) ? -1 : nodes[n].first_out;
- }
-
- void next(Edge& e) const {
- if (edges[e.id].next_out != -1) {
- e.id = edges[e.id].next_out;
- } else {
- int n = nodes[edges[e.id ^ 1].target].next;
- while(n != -1 && nodes[n].first_out == -1) {
- n = nodes[n].next;
- }
- e.id = (n == -1) ? -1 : nodes[n].first_out;
- }
- }
-
- void first(UEdge& e) const {
- int n = first_node;
- while (n != -1) {
- e.id = nodes[n].first_out;
- while ((e.id & 1) != 1) {
- e.id = edges[e.id].next_out;
- }
- if (e.id != -1) {
- e.id /= 2;
- return;
- }
- n = nodes[n].next;
- }
- e.id = -1;
- }
-
- void next(UEdge& e) const {
- int n = edges[e.id * 2].target;
- e.id = edges[(e.id * 2) | 1].next_out;
- while ((e.id & 1) != 1) {
- e.id = edges[e.id].next_out;
- }
- if (e.id != -1) {
- e.id /= 2;
- return;
- }
- n = nodes[n].next;
- while (n != -1) {
- e.id = nodes[n].first_out;
- while ((e.id & 1) != 1) {
- e.id = edges[e.id].next_out;
- }
- if (e.id != -1) {
- e.id /= 2;
- return;
- }
- n = nodes[n].next;
- }
- e.id = -1;
- }
-
- void firstOut(Edge &e, const Node& v) const {
- e.id = nodes[v.id].first_out;
- }
- void nextOut(Edge &e) const {
- e.id = edges[e.id].next_out;
- }
-
- void firstIn(Edge &e, const Node& v) const {
- e.id = ((nodes[v.id].first_out) ^ 1);
- if (e.id == -2) e.id = -1;
- }
- void nextIn(Edge &e) const {
- e.id = ((edges[e.id ^ 1].next_out) ^ 1);
- if (e.id == -2) e.id = -1;
- }
-
- void firstInc(UEdge &e, bool& d, const Node& v) const {
- int de = nodes[v.id].first_out;
- if (de != -1 ) {
- e.id = de / 2;
- d = ((de & 1) == 1);
- } else {
- e.id = -1;
- d = true;
- }
- }
- void nextInc(UEdge &e, bool& d) const {
- int de = (edges[(e.id * 2) | (d ? 1 : 0)].next_out);
- if (de != -1 ) {
- e.id = de / 2;
- d = ((de & 1) == 1);
- } else {
- e.id = -1;
- d = true;
- }
- }
-
- static int id(Node v) { return v.id; }
- static int id(Edge e) { return e.id; }
- static int id(UEdge e) { return e.id; }
-
- static Node nodeFromId(int id) { return Node(id);}
- static Edge edgeFromId(int id) { return Edge(id);}
- static UEdge uEdgeFromId(int id) { return UEdge(id);}
-
- Node addNode() {
- int n;
-
- if(first_free_node==-1) {
- n = nodes.size();
- nodes.push_back(NodeT());
- } else {
- n = first_free_node;
- first_free_node = nodes[n].next;
- }
-
- nodes[n].next = first_node;
- if (first_node != -1) nodes[first_node].prev = n;
- first_node = n;
- nodes[n].prev = -1;
-
- nodes[n].first_out = -1;
-
- return Node(n);
- }
-
- UEdge addEdge(Node u, Node v) {
- int n;
-
- if (first_free_edge == -1) {
- n = edges.size();
- edges.push_back(EdgeT());
- edges.push_back(EdgeT());
- } else {
- n = first_free_edge;
- first_free_edge = edges[n].next_out;
- }
-
- edges[n].target = u.id;
- edges[n | 1].target = v.id;
-
- edges[n].next_out = nodes[v.id].first_out;
- if (nodes[v.id].first_out != -1) {
- edges[nodes[v.id].first_out].prev_out = n;
- }
- edges[n].prev_out = -1;
- nodes[v.id].first_out = n;
-
- edges[n | 1].next_out = nodes[u.id].first_out;
- if (nodes[u.id].first_out != -1) {
- edges[nodes[u.id].first_out].prev_out = (n | 1);
- }
- edges[n | 1].prev_out = -1;
- nodes[u.id].first_out = (n | 1);
-
- return UEdge(n / 2);
- }
-
- void erase(const Node& node) {
- int n = node.id;
-
- if(nodes[n].next != -1) {
- nodes[nodes[n].next].prev = nodes[n].prev;
- }
-
- if(nodes[n].prev != -1) {
- nodes[nodes[n].prev].next = nodes[n].next;
- } else {
- first_node = nodes[n].next;
- }
-
- nodes[n].next = first_free_node;
- first_free_node = n;
-
- }
-
- void erase(const UEdge& edge) {
- int n = edge.id * 2;
-
- if (edges[n].next_out != -1) {
- edges[edges[n].next_out].prev_out = edges[n].prev_out;
- }
-
- if (edges[n].prev_out != -1) {
- edges[edges[n].prev_out].next_out = edges[n].next_out;
- } else {
- nodes[edges[n | 1].target].first_out = edges[n].next_out;
- }
-
- if (edges[n | 1].next_out != -1) {
- edges[edges[n | 1].next_out].prev_out = edges[n | 1].prev_out;
- }
-
- if (edges[n | 1].prev_out != -1) {
- edges[edges[n | 1].prev_out].next_out = edges[n | 1].next_out;
- } else {
- nodes[edges[n].target].first_out = edges[n | 1].next_out;
- }
-
- edges[n].next_out = first_free_edge;
- first_free_edge = n;
-
- }
-
- void clear() {
- edges.clear();
- nodes.clear();
- first_node = first_free_node = first_free_edge = -1;
- }
-
- protected:
-
- void changeTarget(UEdge e, Node n) {
- if(edges[2 * e.id].next_out != -1) {
- edges[edges[2 * e.id].next_out].prev_out = edges[2 * e.id].prev_out;
- }
- if(edges[2 * e.id].prev_out != -1) {
- edges[edges[2 * e.id].prev_out].next_out =
- edges[2 * e.id].next_out;
- } else {
- nodes[edges[(2 * e.id) | 1].target].first_out =
- edges[2 * e.id].next_out;
- }
-
- if (nodes[n.id].first_out != -1) {
- edges[nodes[n.id].first_out].prev_out = 2 * e.id;
- }
- edges[(2 * e.id) | 1].target = n.id;
- edges[2 * e.id].prev_out = -1;
- edges[2 * e.id].next_out = nodes[n.id].first_out;
- nodes[n.id].first_out = 2 * e.id;
- }
-
- void changeSource(UEdge e, Node n) {
- if(edges[(2 * e.id) | 1].next_out != -1) {
- edges[edges[(2 * e.id) | 1].next_out].prev_out =
- edges[(2 * e.id) | 1].prev_out;
- }
- if(edges[(2 * e.id) | 1].prev_out != -1) {
- edges[edges[(2 * e.id) | 1].prev_out].next_out =
- edges[(2 * e.id) | 1].next_out;
- } else {
- nodes[edges[2 * e.id].target].first_out =
- edges[(2 * e.id) | 1].next_out;
- }
-
- if (nodes[n.id].first_out != -1) {
- edges[nodes[n.id].first_out].prev_out = ((2 * e.id) | 1);
- }
- edges[2 * e.id].target = n.id;
- edges[(2 * e.id) | 1].prev_out = -1;
- edges[(2 * e.id) | 1].next_out = nodes[n.id].first_out;
- nodes[n.id].first_out = ((2 * e.id) | 1);
- }
-
- };
-
-// typedef UGraphExtender<UndirGraphExtender<ListGraphBase> >
-// ExtendedListUGraphBase;
-
- typedef UGraphExtender<ListUGraphBase> ExtendedListUGraphBase;
-
-
-
- /// \addtogroup graphs
- /// @{
-
- ///An undirected list graph class.
-
- ///This is a simple and fast undirected graph implementation.
- ///
- ///An important extra feature of this graph implementation is that
- ///its maps are real \ref concepts::ReferenceMap "reference map"s.
- ///
- ///It conforms to the
- ///\ref concepts::UGraph "UGraph concept".
- ///
- ///\sa concepts::UGraph.
- ///
- class ListUGraph : public ExtendedListUGraphBase {
- private:
- ///ListUGraph is \e not copy constructible. Use UGraphCopy() instead.
-
- ///ListUGraph is \e not copy constructible. Use UGraphCopy() instead.
- ///
- ListUGraph(const ListUGraph &) :ExtendedListUGraphBase() {};
- ///\brief Assignment of ListUGraph to another one is \e not allowed.
- ///Use UGraphCopy() instead.
-
- ///Assignment of ListUGraph to another one is \e not allowed.
- ///Use UGraphCopy() instead.
- void operator=(const ListUGraph &) {}
- public:
- /// Constructor
-
- /// Constructor.
- ///
- ListUGraph() {}
-
- typedef ExtendedListUGraphBase Parent;
-
- typedef Parent::OutEdgeIt IncEdgeIt;
-
- /// \brief Add a new node to the graph.
- ///
- /// \return the new node.
- ///
- Node addNode() { return Parent::addNode(); }
-
- /// \brief Add a new edge to the graph.
- ///
- /// Add a new edge to the graph with source node \c s
- /// and target node \c t.
- /// \return the new undirected edge.
- UEdge addEdge(const Node& s, const Node& t) {
- return Parent::addEdge(s, t);
- }
- /// \brief Changes the source of \c e to \c n
- ///
- /// Changes the source of \c e to \c n
- ///
- ///\note The <tt>EdgeIt</tt>s and <tt>InEdgeIt</tt>s
- ///referencing the changed edge remain
- ///valid. However <tt>OutEdgeIt</tt>s are invalidated.
- void changeSource(UEdge e, Node n) {
- Parent::changeSource(e,n);
- }
- /// \brief Changes the target of \c e to \c n
- ///
- /// Changes the target of \c e to \c n
- ///
- /// \note The <tt>EdgeIt</tt>s referencing the changed edge remain
- /// valid. However the other iterators may be invalidated.
- void changeTarget(UEdge e, Node n) {
- Parent::changeTarget(e,n);
- }
- /// \brief Changes the source of \c e to \c n
- ///
- /// Changes the source of \c e to \c n. It changes the proper
- /// node of the represented undirected edge.
- ///
- ///\note The <tt>EdgeIt</tt>s and <tt>InEdgeIt</tt>s
- ///referencing the changed edge remain
- ///valid. However <tt>OutEdgeIt</tt>s are invalidated.
- void changeSource(Edge e, Node n) {
- if (Parent::direction(e)) {
- Parent::changeSource(e,n);
- } else {
- Parent::changeTarget(e,n);
- }
- }
- /// \brief Changes the target of \c e to \c n
- ///
- /// Changes the target of \c e to \c n. It changes the proper
- /// node of the represented undirected edge.
- ///
- ///\note The <tt>EdgeIt</tt>s and <tt>OutEdgeIt</tt>s
- ///referencing the changed edge remain
- ///valid. However <tt>InEdgeIt</tt>s are invalidated.
- void changeTarget(Edge e, Node n) {
- if (Parent::direction(e)) {
- Parent::changeTarget(e,n);
- } else {
- Parent::changeSource(e,n);
- }
- }
- /// \brief Contract two nodes.
- ///
- /// This function contracts two nodes.
- ///
- /// Node \p b will be removed but instead of deleting
- /// its neighboring edges, they will be joined to \p a.
- /// The last parameter \p r controls whether to remove loops. \c true
- /// means that loops will be removed.
- ///
- /// \note The <tt>EdgeIt</tt>s referencing a moved edge remain
- /// valid.
- void contract(Node a, Node b, bool r = true) {
- for(IncEdgeIt e(*this, b); e!=INVALID;) {
- IncEdgeIt f = e; ++f;
- if (r && runningNode(e) == a) {
- erase(e);
- } else if (source(e) == b) {
- changeSource(e, a);
- } else {
- changeTarget(e, a);
- }
- e = f;
- }
- erase(b);
- }
-
-
- /// \brief Class to make a snapshot of the graph and restore
- /// to it later.
- ///
- /// Class to make a snapshot of the graph and to restore it
- /// later.
- ///
- /// The newly added nodes and undirected edges can be removed
- /// using the restore() function.
- ///
- /// \warning Edge and node deletions cannot be restored. This
- /// events invalidate the snapshot.
- class Snapshot {
- protected:
-
- typedef Parent::NodeNotifier NodeNotifier;
-
- class NodeObserverProxy : public NodeNotifier::ObserverBase {
- public:
-
- NodeObserverProxy(Snapshot& _snapshot)
- : snapshot(_snapshot) {}
-
- using NodeNotifier::ObserverBase::attach;
- using NodeNotifier::ObserverBase::detach;
- using NodeNotifier::ObserverBase::attached;
-
- protected:
-
- virtual void add(const Node& node) {
- snapshot.addNode(node);
- }
- virtual void add(const std::vector<Node>& nodes) {
- for (int i = nodes.size() - 1; i >= 0; ++i) {
- snapshot.addNode(nodes[i]);
- }
- }
- virtual void erase(const Node& node) {
- snapshot.eraseNode(node);
- }
- virtual void erase(const std::vector<Node>& nodes) {
- for (int i = 0; i < int(nodes.size()); ++i) {
- snapshot.eraseNode(nodes[i]);
- }
- }
- virtual void build() {
- Node node;
- std::vector<Node> nodes;
- for (notifier()->first(node); node != INVALID;
- notifier()->next(node)) {
- nodes.push_back(node);
- }
- for (int i = nodes.size() - 1; i >= 0; --i) {
- snapshot.addNode(nodes[i]);
- }
- }
- virtual void clear() {
- Node node;
- for (notifier()->first(node); node != INVALID;
- notifier()->next(node)) {
- snapshot.eraseNode(node);
- }
- }
-
- Snapshot& snapshot;
- };
-
- class UEdgeObserverProxy : public UEdgeNotifier::ObserverBase {
- public:
-
- UEdgeObserverProxy(Snapshot& _snapshot)
- : snapshot(_snapshot) {}
-
- using UEdgeNotifier::ObserverBase::attach;
- using UEdgeNotifier::ObserverBase::detach;
- using UEdgeNotifier::ObserverBase::attached;
-
- protected:
-
- virtual void add(const UEdge& edge) {
- snapshot.addUEdge(edge);
- }
- virtual void add(const std::vector<UEdge>& edges) {
- for (int i = edges.size() - 1; i >= 0; ++i) {
- snapshot.addUEdge(edges[i]);
- }
- }
- virtual void erase(const UEdge& edge) {
- snapshot.eraseUEdge(edge);
- }
- virtual void erase(const std::vector<UEdge>& edges) {
- for (int i = 0; i < int(edges.size()); ++i) {
- snapshot.eraseUEdge(edges[i]);
- }
- }
- virtual void build() {
- UEdge edge;
- std::vector<UEdge> edges;
- for (notifier()->first(edge); edge != INVALID;
- notifier()->next(edge)) {
- edges.push_back(edge);
- }
- for (int i = edges.size() - 1; i >= 0; --i) {
- snapshot.addUEdge(edges[i]);
- }
- }
- virtual void clear() {
- UEdge edge;
- for (notifier()->first(edge); edge != INVALID;
- notifier()->next(edge)) {
- snapshot.eraseUEdge(edge);
- }
- }
-
- Snapshot& snapshot;
- };
-
- ListUGraph *graph;
-
- NodeObserverProxy node_observer_proxy;
- UEdgeObserverProxy edge_observer_proxy;
-
- std::list<Node> added_nodes;
- std::list<UEdge> added_edges;
-
-
- void addNode(const Node& node) {
- added_nodes.push_front(node);
- }
- void eraseNode(const Node& node) {
- std::list<Node>::iterator it =
- std::find(added_nodes.begin(), added_nodes.end(), node);
- if (it == added_nodes.end()) {
- clear();
- edge_observer_proxy.detach();
- throw NodeNotifier::ImmediateDetach();
- } else {
- added_nodes.erase(it);
- }
- }
-
- void addUEdge(const UEdge& edge) {
- added_edges.push_front(edge);
- }
- void eraseUEdge(const UEdge& edge) {
- std::list<UEdge>::iterator it =
- std::find(added_edges.begin(), added_edges.end(), edge);
- if (it == added_edges.end()) {
- clear();
- node_observer_proxy.detach();
- throw UEdgeNotifier::ImmediateDetach();
- } else {
- added_edges.erase(it);
- }
- }
-
- void attach(ListUGraph &_graph) {
- graph = &_graph;
- node_observer_proxy.attach(graph->notifier(Node()));
- edge_observer_proxy.attach(graph->notifier(UEdge()));
- }
-
- void detach() {
- node_observer_proxy.detach();
- edge_observer_proxy.detach();
- }
-
- bool attached() const {
- return node_observer_proxy.attached();
- }
-
- void clear() {
- added_nodes.clear();
- added_edges.clear();
- }
-
- public:
-
- /// \brief Default constructor.
- ///
- /// Default constructor.
- /// To actually make a snapshot you must call save().
- Snapshot()
- : graph(0), node_observer_proxy(*this),
- edge_observer_proxy(*this) {}
-
- /// \brief Constructor that immediately makes a snapshot.
- ///
- /// This constructor immediately makes a snapshot of the graph.
- /// \param _graph The graph we make a snapshot of.
- Snapshot(ListUGraph &_graph)
- : node_observer_proxy(*this),
- edge_observer_proxy(*this) {
- attach(_graph);
- }
-
- /// \brief Make a snapshot.
- ///
- /// Make a snapshot of the graph.
- ///
- /// This function can be called more than once. In case of a repeated
- /// call, the previous snapshot gets lost.
- /// \param _graph The graph we make the snapshot of.
- void save(ListUGraph &_graph) {
- if (attached()) {
- detach();
- clear();
- }
- attach(_graph);
- }
-
- /// \brief Undo the changes until the last snapshot.
- //
- /// Undo the changes until the last snapshot created by save().
- void restore() {
- detach();
- for(std::list<UEdge>::iterator it = added_edges.begin();
- it != added_edges.end(); ++it) {
- graph->erase(*it);
- }
- for(std::list<Node>::iterator it = added_nodes.begin();
- it != added_nodes.end(); ++it) {
- graph->erase(*it);
- }
- clear();
- }
-
- /// \brief Gives back true when the snapshot is valid.
- ///
- /// Gives back true when the snapshot is valid.
- bool valid() const {
- return attached();
- }
- };
- };
-
-
- class ListBpUGraphBase {
- public:
-
- class NodeSetError : public LogicError {
- public:
- virtual const char* what() const throw() {
- return "lemon::ListBpUGraph::NodeSetError";
- }
- };
-
- protected:
-
- struct NodeT {
- int first_edge, prev, next;
- };
-
- struct UEdgeT {
- int aNode, prev_out, next_out;
- int bNode, prev_in, next_in;
- };
-
- std::vector<NodeT> aNodes;
- std::vector<NodeT> bNodes;
-
- std::vector<UEdgeT> edges;
-
- int first_anode;
- int first_free_anode;
-
- int first_bnode;
- int first_free_bnode;
-
- int first_free_edge;
-
- public:
-
- class Node {
- friend class ListBpUGraphBase;
- protected:
- int id;
-
- explicit Node(int _id) : id(_id) {}
- public:
- Node() {}
- Node(Invalid) { id = -1; }
- bool operator==(const Node i) const {return id==i.id;}
- bool operator!=(const Node i) const {return id!=i.id;}
- bool operator<(const Node i) const {return id<i.id;}
- };
-
- class UEdge {
- friend class ListBpUGraphBase;
- protected:
- int id;
-
- explicit UEdge(int _id) { id = _id;}
- public:
- UEdge() {}
- UEdge (Invalid) { id = -1; }
- bool operator==(const UEdge i) const {return id==i.id;}
- bool operator!=(const UEdge i) const {return id!=i.id;}
- bool operator<(const UEdge i) const {return id<i.id;}
- };
-
- ListBpUGraphBase()
- : first_anode(-1), first_free_anode(-1),
- first_bnode(-1), first_free_bnode(-1),
- first_free_edge(-1) {}
-
- void firstANode(Node& node) const {
- node.id = first_anode != -1 ? (first_anode << 1) : -1;
- }
- void nextANode(Node& node) const {
- node.id = aNodes[node.id >> 1].next;
- }
-
- void firstBNode(Node& node) const {
- node.id = first_bnode != -1 ? (first_bnode << 1) + 1 : -1;
- }
- void nextBNode(Node& node) const {
- node.id = bNodes[node.id >> 1].next;
- }
-
- void first(Node& node) const {
- if (first_anode != -1) {
- node.id = (first_anode << 1);
- } else if (first_bnode != -1) {
- node.id = (first_bnode << 1) + 1;
- } else {
- node.id = -1;
- }
- }
- void next(Node& node) const {
- if (aNode(node)) {
- node.id = aNodes[node.id >> 1].next;
- if (node.id == -1) {
- if (first_bnode != -1) {
- node.id = (first_bnode << 1) + 1;
- }
- }
- } else {
- node.id = bNodes[node.id >> 1].next;
- }
- }
-
- void first(UEdge& edge) const {
- int aid = first_anode;
- while (aid != -1 && aNodes[aid].first_edge == -1) {
- aid = aNodes[aid].next != -1 ?
- aNodes[aid].next >> 1 : -1;
- }
- if (aid != -1) {
- edge.id = aNodes[aid].first_edge;
- } else {
- edge.id = -1;
- }
- }
- void next(UEdge& edge) const {
- int aid = edges[edge.id].aNode >> 1;
- edge.id = edges[edge.id].next_out;
- if (edge.id == -1) {
- aid = aNodes[aid].next != -1 ?
- aNodes[aid].next >> 1 : -1;
- while (aid != -1 && aNodes[aid].first_edge == -1) {
- aid = aNodes[aid].next != -1 ?
- aNodes[aid].next >> 1 : -1;
- }
- if (aid != -1) {
- edge.id = aNodes[aid].first_edge;
- } else {
- edge.id = -1;
- }
- }
- }
-
- void firstFromANode(UEdge& edge, const Node& node) const {
- LEMON_ASSERT((node.id & 1) == 0, NodeSetError());
- edge.id = aNodes[node.id >> 1].first_edge;
- }
- void nextFromANode(UEdge& edge) const {
- edge.id = edges[edge.id].next_out;
- }
-
- void firstFromBNode(UEdge& edge, const Node& node) const {
- LEMON_ASSERT((node.id & 1) == 1, NodeSetError());
- edge.id = bNodes[node.id >> 1].first_edge;
- }
- void nextFromBNode(UEdge& edge) const {
- edge.id = edges[edge.id].next_in;
- }
-
- static int id(const Node& node) {
- return node.id;
- }
- static Node nodeFromId(int id) {
- return Node(id);
- }
- int maxNodeId() const {
- return aNodes.size() > bNodes.size() ?
- aNodes.size() * 2 - 2 : bNodes.size() * 2 - 1;
- }
-
- static int id(const UEdge& edge) {
- return edge.id;
- }
- static UEdge uEdgeFromId(int id) {
- return UEdge(id);
- }
- int maxUEdgeId() const {
- return edges.size();
- }
-
- static int aNodeId(const Node& node) {
- return node.id >> 1;
- }
- static Node nodeFromANodeId(int id) {
- return Node(id << 1);
- }
- int maxANodeId() const {
- return aNodes.size();
- }
-
- static int bNodeId(const Node& node) {
- return node.id >> 1;
- }
- static Node nodeFromBNodeId(int id) {
- return Node((id << 1) + 1);
- }
- int maxBNodeId() const {
- return bNodes.size();
- }
-
- Node aNode(const UEdge& edge) const {
- return Node(edges[edge.id].aNode);
- }
- Node bNode(const UEdge& edge) const {
- return Node(edges[edge.id].bNode);
- }
-
- static bool aNode(const Node& node) {
- return (node.id & 1) == 0;
- }
-
- static bool bNode(const Node& node) {
- return (node.id & 1) == 1;
- }
-
- Node addANode() {
- int aid;
- if (first_free_anode == -1) {
- aid = aNodes.size();
- aNodes.push_back(NodeT());
- } else {
- aid = first_free_anode;
- first_free_anode = aNodes[first_free_anode].next;
- }
- if (first_anode != -1) {
- aNodes[aid].next = first_anode << 1;
- aNodes[first_anode].prev = aid << 1;
- } else {
- aNodes[aid].next = -1;
- }
- aNodes[aid].prev = -1;
- first_anode = aid;
- aNodes[aid].first_edge = -1;
- return Node(aid << 1);
- }
-
- Node addBNode() {
- int bid;
- if (first_free_bnode == -1) {
- bid = bNodes.size();
- bNodes.push_back(NodeT());
- } else {
- bid = first_free_bnode;
- first_free_bnode = bNodes[first_free_bnode].next;
- }
- if (first_bnode != -1) {
- bNodes[bid].next = (first_bnode << 1) + 1;
- bNodes[first_bnode].prev = (bid << 1) + 1;
- } else {
- bNodes[bid].next = -1;
- }
- bNodes[bid].prev = -1;
- first_bnode = bid;
- bNodes[bid].first_edge = -1;
- return Node((bid << 1) + 1);
- }
-
- UEdge addEdge(const Node& source, const Node& target) {
- LEMON_ASSERT(((source.id ^ target.id) & 1) == 1, NodeSetError());
- int edgeId;
- if (first_free_edge != -1) {
- edgeId = first_free_edge;
- first_free_edge = edges[edgeId].next_out;
- } else {
- edgeId = edges.size();
- edges.push_back(UEdgeT());
- }
- if ((source.id & 1) == 0) {
- edges[edgeId].aNode = source.id;
- edges[edgeId].bNode = target.id;
- } else {
- edges[edgeId].aNode = target.id;
- edges[edgeId].bNode = source.id;
- }
- edges[edgeId].next_out = aNodes[edges[edgeId].aNode >> 1].first_edge;
- edges[edgeId].prev_out = -1;
- if (aNodes[edges[edgeId].aNode >> 1].first_edge != -1) {
- edges[aNodes[edges[edgeId].aNode >> 1].first_edge].prev_out = edgeId;
- }
- aNodes[edges[edgeId].aNode >> 1].first_edge = edgeId;
- edges[edgeId].next_in = bNodes[edges[edgeId].bNode >> 1].first_edge;
- edges[edgeId].prev_in = -1;
- if (bNodes[edges[edgeId].bNode >> 1].first_edge != -1) {
- edges[bNodes[edges[edgeId].bNode >> 1].first_edge].prev_in = edgeId;
- }
- bNodes[edges[edgeId].bNode >> 1].first_edge = edgeId;
- return UEdge(edgeId);
- }
-
- void erase(const Node& node) {
- if (aNode(node)) {
- int aid = node.id >> 1;
- if (aNodes[aid].prev != -1) {
- aNodes[aNodes[aid].prev >> 1].next = aNodes[aid].next;
- } else {
- first_anode =
- aNodes[aid].next != -1 ? aNodes[aid].next >> 1 : -1;
- }
- if (aNodes[aid].next != -1) {
- aNodes[aNodes[aid].next >> 1].prev = aNodes[aid].prev;
- }
- aNodes[aid].next = first_free_anode;
- first_free_anode = aid;
- } else {
- int bid = node.id >> 1;
- if (bNodes[bid].prev != -1) {
- bNodes[bNodes[bid].prev >> 1].next = bNodes[bid].next;
- } else {
- first_bnode =
- bNodes[bid].next != -1 ? bNodes[bid].next >> 1 : -1;
- }
- if (bNodes[bid].next != -1) {
- bNodes[bNodes[bid].next >> 1].prev = bNodes[bid].prev;
- }
- bNodes[bid].next = first_free_bnode;
- first_free_bnode = bid;
- }
- }
-
- void erase(const UEdge& edge) {
-
- if (edges[edge.id].prev_out != -1) {
- edges[edges[edge.id].prev_out].next_out = edges[edge.id].next_out;
- } else {
- aNodes[edges[edge.id].aNode >> 1].first_edge = edges[edge.id].next_out;
- }
- if (edges[edge.id].next_out != -1) {
- edges[edges[edge.id].next_out].prev_out = edges[edge.id].prev_out;
- }
-
- if (edges[edge.id].prev_in != -1) {
- edges[edges[edge.id].prev_in].next_in = edges[edge.id].next_in;
- } else {
- bNodes[edges[edge.id].bNode >> 1].first_edge = edges[edge.id].next_in;
- }
- if (edges[edge.id].next_in != -1) {
- edges[edges[edge.id].next_in].prev_in = edges[edge.id].prev_in;
- }
-
- edges[edge.id].next_out = first_free_edge;
- first_free_edge = edge.id;
- }
-
- void clear() {
- aNodes.clear();
- bNodes.clear();
- edges.clear();
- first_anode = -1;
- first_free_anode = -1;
- first_bnode = -1;
- first_free_bnode = -1;
- first_free_edge = -1;
- }
-
- void changeANode(const UEdge& edge, const Node& node) {
- LEMON_ASSERT((node.id & 1) == 0, NodeSetError());
- if (edges[edge.id].prev_out != -1) {
- edges[edges[edge.id].prev_out].next_out = edges[edge.id].next_out;
- } else {
- aNodes[edges[edge.id].aNode >> 1].first_edge = edges[edge.id].next_out;
- }
- if (edges[edge.id].next_out != -1) {
- edges[edges[edge.id].next_out].prev_out = edges[edge.id].prev_out;
- }
- if (aNodes[node.id >> 1].first_edge != -1) {
- edges[aNodes[node.id >> 1].first_edge].prev_out = edge.id;
- }
- edges[edge.id].prev_out = -1;
- edges[edge.id].next_out = aNodes[node.id >> 1].first_edge;
- aNodes[node.id >> 1].first_edge = edge.id;
- edges[edge.id].aNode = node.id;
- }
-
- void changeBNode(const UEdge& edge, const Node& node) {
- LEMON_ASSERT((node.id & 1) == 1, NodeSetError());
- if (edges[edge.id].prev_in != -1) {
- edges[edges[edge.id].prev_in].next_in = edges[edge.id].next_in;
- } else {
- bNodes[edges[edge.id].bNode >> 1].first_edge = edges[edge.id].next_in;
- }
- if (edges[edge.id].next_in != -1) {
- edges[edges[edge.id].next_in].prev_in = edges[edge.id].prev_in;
- }
- if (bNodes[node.id >> 1].first_edge != -1) {
- edges[bNodes[node.id >> 1].first_edge].prev_in = edge.id;
- }
- edges[edge.id].prev_in = -1;
- edges[edge.id].next_in = bNodes[node.id >> 1].first_edge;
- bNodes[node.id >> 1].first_edge = edge.id;
- edges[edge.id].bNode = node.id;
- }
-
- };
-
-
- typedef BpUGraphExtender<BidirBpUGraphExtender<ListBpUGraphBase> >
- ExtendedListBpUGraphBase;
-
- /// \ingroup graphs
- ///
- /// \brief A smart bipartite undirected graph class.
- ///
- /// This is a bipartite undirected graph implementation.
- /// It is conforms to the \ref concepts::BpUGraph "BpUGraph concept".
- ///
- ///An important extra feature of this graph implementation is that
- ///its maps are real \ref concepts::ReferenceMap "reference map"s.
- ///
- /// \sa concepts::BpUGraph.
- ///
- class ListBpUGraph : public ExtendedListBpUGraphBase {
- /// \brief ListBpUGraph is \e not copy constructible.
- ///
- ///ListBpUGraph is \e not copy constructible.
- ListBpUGraph(const ListBpUGraph &) :ExtendedListBpUGraphBase() {};
- /// \brief Assignment of ListBpUGraph to another one is \e not
- /// allowed.
- ///
- /// Assignment of ListBpUGraph to another one is \e not allowed.
- void operator=(const ListBpUGraph &) {}
- public:
- /// \brief Constructor
- ///
- /// Constructor.
- ///
- ListBpUGraph() {}
-
- typedef ExtendedListBpUGraphBase Parent;
- /// \brief Add a new ANode to the graph.
- ///
- /// \return the new node.
- ///
- Node addANode() { return Parent::addANode(); }
-
- /// \brief Add a new BNode to the graph.
- ///
- /// \return the new node.
- ///
- Node addBNode() { return Parent::addBNode(); }
-
- /// \brief Add a new edge to the graph.
- ///
- /// Add a new edge to the graph with an ANode and a BNode.
- /// \return the new undirected edge.
- UEdge addEdge(const Node& s, const Node& t) {
- return Parent::addEdge(s, t);
- }
-
- /// \brief Changes the ANode of \c e to \c n
- ///
- /// Changes the ANode of \c e to \c n
- ///
- ///\note The <tt>EdgeIt</tt>s and <tt>InEdgeIt</tt>s referencing
- ///the changed edge remain valid. However <tt>OutEdgeIt</tt>s are
- ///invalidated.
- void changeANode(UEdge e, Node n) {
- Parent::changeANode(e,n);
- }
-
- /// \brief Changes the BNode of \c e to \c n
- ///
- /// Changes the BNode of \c e to \c n
- ///
- /// \note The <tt>EdgeIt</tt>s and <tt>OutEdgeIt</tt>s
- /// referencing the changed edge remain
- /// valid. However <tt>InEdgeIt</tt>s are invalidated.
- void changeBNode(UEdge e, Node n) {
- Parent::changeBNode(e,n);
- }
-
- /// \brief Changes the source(ANode) of \c e to \c n
- ///
- /// Changes the source(ANode) of \c e to \c n
- ///
- ///\note The <tt>EdgeIt</tt>s and <tt>InEdgeIt</tt>s referencing
- ///the changed edge remain valid. However <tt>OutEdgeIt</tt>s are
- ///invalidated.
- void changeSource(UEdge e, Node n) {
- Parent::changeANode(e,n);
- }
-
- /// \brief Changes the target(BNode) of \c e to \c n
- ///
- /// Changes the target(BNode) of \c e to \c n
- ///
- /// \note The <tt>EdgeIt</tt>s and <tt>OutEdgeIt</tt>s
- /// referencing the changed edge remain
- /// valid. However <tt>InEdgeIt</tt>s are invalidated.
- void changeTarget(UEdge e, Node n) {
- Parent::changeBNode(e,n);
- }
-
- /// \brief Changes the source of \c e to \c n
- ///
- /// Changes the source of \c e to \c n. It changes the proper
- /// node of the represented undirected edge.
- ///
- ///\note The <tt>EdgeIt</tt>s and <tt>InEdgeIt</tt>s
- ///referencing the changed edge remain
- ///valid. However <tt>OutEdgeIt</tt>s are invalidated.
- void changeSource(Edge e, Node n) {
- if (Parent::direction(e)) {
- Parent::changeANode(e,n);
- } else {
- Parent::changeBNode(e,n);
- }
- }
- /// \brief Changes the target of \c e to \c n
- ///
- /// Changes the target of \c e to \c n. It changes the proper
- /// node of the represented undirected edge.
- ///
- ///\note The <tt>EdgeIt</tt>s and <tt>OutEdgeIt</tt>s
- ///referencing the changed edge remain
- ///valid. However <tt>InEdgeIt</tt>s are invalidated.
- void changeTarget(Edge e, Node n) {
- if (Parent::direction(e)) {
- Parent::changeBNode(e,n);
- } else {
- Parent::changeANode(e,n);
- }
- }
- /// \brief Contract two nodes.
- ///
- /// This function contracts two nodes.
- ///
- /// Node \p b will be removed but instead of deleting its
- /// neighboring edges, they will be joined to \p a. The two nodes
- /// should be from the same nodeset, of course.
- ///
- /// \note The <tt>EdgeIt</tt>s referencing a moved edge remain
- /// valid.
- void contract(const Node& a, const Node& b) {
- LEMON_ASSERT(Parent::aNode(a) == Parent::aNode(b), NodeSetError());
- if (Parent::aNode(a)) {
- for (IncEdgeIt e(*this, b); e!=INVALID;) {
- IncEdgeIt f = e; ++f;
- changeSource(e, a);
- e = f;
- }
- } else {
- for (IncEdgeIt e(*this, b); e!=INVALID;) {
- IncEdgeIt f = e; ++f;
- changeTarget(e, a);
- e = f;
- }
- }
- erase(b);
- }
-
- /// \brief Class to make a snapshot of the graph and restore
- /// to it later.
- ///
- /// Class to make a snapshot of the graph and to restore it
- /// later.
- ///
- /// The newly added nodes and undirected edges can be removed
- /// using the restore() function.
- ///
- /// \warning Edge and node deletions cannot be restored. This
- /// events invalidate the snapshot.
- class Snapshot {
- protected:
-
- typedef Parent::NodeNotifier NodeNotifier;
-
- class NodeObserverProxy : public NodeNotifier::ObserverBase {
- public:
-
- NodeObserverProxy(Snapshot& _snapshot)
- : snapshot(_snapshot) {}
-
- using NodeNotifier::ObserverBase::attach;
- using NodeNotifier::ObserverBase::detach;
- using NodeNotifier::ObserverBase::attached;
-
- protected:
-
- virtual void add(const Node& node) {
- snapshot.addNode(node);
- }
- virtual void add(const std::vector<Node>& nodes) {
- for (int i = nodes.size() - 1; i >= 0; ++i) {
- snapshot.addNode(nodes[i]);
- }
- }
- virtual void erase(const Node& node) {
- snapshot.eraseNode(node);
- }
- virtual void erase(const std::vector<Node>& nodes) {
- for (int i = 0; i < int(nodes.size()); ++i) {
- snapshot.eraseNode(nodes[i]);
- }
- }
- virtual void build() {
- Node node;
- std::vector<Node> nodes;
- for (notifier()->first(node); node != INVALID;
- notifier()->next(node)) {
- nodes.push_back(node);
- }
- for (int i = nodes.size() - 1; i >= 0; --i) {
- snapshot.addNode(nodes[i]);
- }
- }
- virtual void clear() {
- Node node;
- for (notifier()->first(node); node != INVALID;
- notifier()->next(node)) {
- snapshot.eraseNode(node);
- }
- }
-
- Snapshot& snapshot;
- };
-
- class UEdgeObserverProxy : public UEdgeNotifier::ObserverBase {
- public:
-
- UEdgeObserverProxy(Snapshot& _snapshot)
- : snapshot(_snapshot) {}
-
- using UEdgeNotifier::ObserverBase::attach;
- using UEdgeNotifier::ObserverBase::detach;
- using UEdgeNotifier::ObserverBase::attached;
-
- protected:
-
- virtual void add(const UEdge& edge) {
- snapshot.addUEdge(edge);
- }
- virtual void add(const std::vector<UEdge>& edges) {
- for (int i = edges.size() - 1; i >= 0; ++i) {
- snapshot.addUEdge(edges[i]);
- }
- }
- virtual void erase(const UEdge& edge) {
- snapshot.eraseUEdge(edge);
- }
- virtual void erase(const std::vector<UEdge>& edges) {
- for (int i = 0; i < int(edges.size()); ++i) {
- snapshot.eraseUEdge(edges[i]);
- }
- }
- virtual void build() {
- UEdge edge;
- std::vector<UEdge> edges;
- for (notifier()->first(edge); edge != INVALID;
- notifier()->next(edge)) {
- edges.push_back(edge);
- }
- for (int i = edges.size() - 1; i >= 0; --i) {
- snapshot.addUEdge(edges[i]);
- }
- }
- virtual void clear() {
- UEdge edge;
- for (notifier()->first(edge); edge != INVALID;
- notifier()->next(edge)) {
- snapshot.eraseUEdge(edge);
- }
- }
-
- Snapshot& snapshot;
- };
-
- ListBpUGraph *graph;
-
- NodeObserverProxy node_observer_proxy;
- UEdgeObserverProxy edge_observer_proxy;
-
- std::list<Node> added_nodes;
- std::list<UEdge> added_edges;
-
-
- void addNode(const Node& node) {
- added_nodes.push_front(node);
- }
- void eraseNode(const Node& node) {
- std::list<Node>::iterator it =
- std::find(added_nodes.begin(), added_nodes.end(), node);
- if (it == added_nodes.end()) {
- clear();
- edge_observer_proxy.detach();
- throw NodeNotifier::ImmediateDetach();
- } else {
- added_nodes.erase(it);
- }
- }
-
- void addUEdge(const UEdge& edge) {
- added_edges.push_front(edge);
- }
- void eraseUEdge(const UEdge& edge) {
- std::list<UEdge>::iterator it =
- std::find(added_edges.begin(), added_edges.end(), edge);
- if (it == added_edges.end()) {
- clear();
- node_observer_proxy.detach();
- throw UEdgeNotifier::ImmediateDetach();
- } else {
- added_edges.erase(it);
- }
- }
-
- void attach(ListBpUGraph &_graph) {
- graph = &_graph;
- node_observer_proxy.attach(graph->notifier(Node()));
- edge_observer_proxy.attach(graph->notifier(UEdge()));
- }
-
- void detach() {
- node_observer_proxy.detach();
- edge_observer_proxy.detach();
- }
-
- bool attached() const {
- return node_observer_proxy.attached();
- }
-
- void clear() {
- added_nodes.clear();
- added_edges.clear();
- }
-
- public:
-
- /// \brief Default constructor.
- ///
- /// Default constructor.
- /// To actually make a snapshot you must call save().
- Snapshot()
- : graph(0), node_observer_proxy(*this),
- edge_observer_proxy(*this) {}
-
- /// \brief Constructor that immediately makes a snapshot.
- ///
- /// This constructor immediately makes a snapshot of the graph.
- /// \param _graph The graph we make a snapshot of.
- Snapshot(ListBpUGraph &_graph)
- : node_observer_proxy(*this),
- edge_observer_proxy(*this) {
- attach(_graph);
- }
-
- /// \brief Make a snapshot.
- ///
- /// Make a snapshot of the graph.
- ///
- /// This function can be called more than once. In case of a repeated
- /// call, the previous snapshot gets lost.
- /// \param _graph The graph we make the snapshot of.
- void save(ListBpUGraph &_graph) {
- if (attached()) {
- detach();
- clear();
- }
- attach(_graph);
- }
-
- /// \brief Undo the changes until the last snapshot.
- //
- /// Undo the changes until the last snapshot created by save().
- void restore() {
- detach();
- for(std::list<UEdge>::iterator it = added_edges.begin();
- it != added_edges.end(); ++it) {
- graph->erase(*it);
- }
- for(std::list<Node>::iterator it = added_nodes.begin();
- it != added_nodes.end(); ++it) {
- graph->erase(*it);
- }
- clear();
- }
-
- /// \brief Gives back true when the snapshot is valid.
- ///
- /// Gives back true when the snapshot is valid.
- bool valid() const {
- return attached();
- }
- };
- };
-
-
- /// @}
-} //namespace lemon
-
-
-#endif
diff --git a/src/lemon/maps.h b/src/lemon/maps.h
deleted file mode 100644
index e586406..0000000
--- a/src/lemon/maps.h
+++ /dev/null
@@ -1,1633 +0,0 @@
-/* -*- C++ -*-
- *
- * This file is a part of LEMON, a generic C++ optimization library
- *
- * Copyright (C) 2003-2008
- * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
- * (Egervary Research Group on Combinatorial Optimization, EGRES).
- *
- * Permission to use, modify and distribute this software is granted
- * provided that this copyright notice appears in all copies. For
- * precise terms see the accompanying LICENSE file.
- *
- * This software is provided "AS IS" with no warranty of any kind,
- * express or implied, and with no claim as to its suitability for any
- * purpose.
- *
- */
-
-#ifndef LEMON_MAPS_H
-#define LEMON_MAPS_H
-
-#include <iterator>
-#include <functional>
-#include <vector>
-
-#include <lemon/bits/utility.h>
-#include <lemon/bits/traits.h>
-
-///\file
-///\ingroup maps
-///\brief Miscellaneous property maps
-///
-#include <map>
-
-namespace lemon {
-
- /// \addtogroup maps
- /// @{
-
- /// Base class of maps.
-
- /// Base class of maps.
- /// It provides the necessary <tt>typedef</tt>s required by the map concept.
- template<typename K, typename T>
- class MapBase {
- public:
- /// The key type of the map.
- typedef K Key;
- /// The value type of the map. (The type of objects associated with the keys).
- typedef T Value;
- };
-
- /// Null map. (a.k.a. DoNothingMap)
-
- /// This map can be used if you have to provide a map only for
- /// its type definitions, or if you have to provide a writable map,
- /// but data written to it is not required (i.e. it will be sent to
- /// <tt>/dev/null</tt>).
- template<typename K, typename T>
- class NullMap : public MapBase<K, T> {
- public:
- typedef MapBase<K, T> Parent;
- typedef typename Parent::Key Key;
- typedef typename Parent::Value Value;
-
- /// Gives back a default constructed element.
- T operator[](const K&) const { return T(); }
- /// Absorbs the value.
- void set(const K&, const T&) {}
- };
-
- ///Returns a \c NullMap class
-
- ///This function just returns a \c NullMap class.
- ///\relates NullMap
- template <typename K, typename V>
- NullMap<K, V> nullMap() {
- return NullMap<K, V>();
- }
-
-
- /// Constant map.
-
- /// This is a \ref concepts::ReadMap "readable" map which assigns a
- /// specified value to each key.
- /// In other aspects it is equivalent to \c NullMap.
- template<typename K, typename T>
- class ConstMap : public MapBase<K, T> {
- private:
- T v;
- public:
-
- typedef MapBase<K, T> Parent;
- typedef typename Parent::Key Key;
- typedef typename Parent::Value Value;
-
- /// Default constructor
-
- /// Default constructor.
- /// The value of the map will be uninitialized.
- /// (More exactly it will be default constructed.)
- ConstMap() {}
-
- /// Constructor with specified initial value
-
- /// Constructor with specified initial value.
- /// \param _v is the initial value of the map.
- ConstMap(const T &_v) : v(_v) {}
-
- ///\e
- T operator[](const K&) const { return v; }
-
- ///\e
- void setAll(const T &t) {
- v = t;
- }
-
- template<typename T1>
- struct rebind {
- typedef ConstMap<K, T1> other;
- };
-
- template<typename T1>
- ConstMap(const ConstMap<K, T1> &, const T &_v) : v(_v) {}
- };
-
- ///Returns a \c ConstMap class
-
- ///This function just returns a \c ConstMap class.
- ///\relates ConstMap
- template<typename K, typename V>
- inline ConstMap<K, V> constMap(const V &v) {
- return ConstMap<K, V>(v);
- }
-
-
- template<typename T, T v>
- struct Const { };
-
- /// Constant map with inlined constant value.
-
- /// This is a \ref concepts::ReadMap "readable" map which assigns a
- /// specified value to each key.
- /// In other aspects it is equivalent to \c NullMap.
- template<typename K, typename V, V v>
- class ConstMap<K, Const<V, v> > : public MapBase<K, V> {
- public:
- typedef MapBase<K, V> Parent;
- typedef typename Parent::Key Key;
- typedef typename Parent::Value Value;
-
- ConstMap() { }
- ///\e
- V operator[](const K&) const { return v; }
- ///\e
- void set(const K&, const V&) { }
- };
-
- ///Returns a \c ConstMap class with inlined value
-
- ///This function just returns a \c ConstMap class with inlined value.
- ///\relates ConstMap
- template<typename K, typename V, V v>
- inline ConstMap<K, Const<V, v> > constMap() {
- return ConstMap<K, Const<V, v> >();
- }
-
- ///Map based on \c std::map
-
- ///This is essentially a wrapper for \c std::map with addition that
- ///you can specify a default value different from \c Value() .
- ///It meets the \ref concepts::ReferenceMap "ReferenceMap" concept.
- template <typename K, typename T, typename Compare = std::less<K> >
- class StdMap : public MapBase<K, T> {
- template <typename K1, typename T1, typename C1>
- friend class StdMap;
- public:
-
- typedef MapBase<K, T> Parent;
- ///Key type
- typedef typename Parent::Key Key;
- ///Value type
- typedef typename Parent::Value Value;
- ///Reference Type
- typedef T& Reference;
- ///Const reference type
- typedef const T& ConstReference;
-
- typedef True ReferenceMapTag;
-
- private:
-
- typedef std::map<K, T, Compare> Map;
- Value _value;
- Map _map;
-
- public:
-
- /// Constructor with specified default value
- StdMap(const T& value = T()) : _value(value) {}
- /// \brief Constructs the map from an appropriate \c std::map, and
- /// explicitly specifies a default value.
- template <typename T1, typename Comp1>
- StdMap(const std::map<Key, T1, Comp1> &map, const T& value = T())
- : _map(map.begin(), map.end()), _value(value) {}
-
- /// \brief Constructs a map from an other \ref StdMap.
- template<typename T1, typename Comp1>
- StdMap(const StdMap<Key, T1, Comp1> &c)
- : _map(c._map.begin(), c._map.end()), _value(c._value) {}
-
- private:
-
- StdMap& operator=(const StdMap&);
-
- public:
-
- ///\e
- Reference operator[](const Key &k) {
- typename Map::iterator it = _map.lower_bound(k);
- if (it != _map.end() && !_map.key_comp()(k, it->first))
- return it->second;
- else
- return _map.insert(it, std::make_pair(k, _value))->second;
- }
-
- /// \e
- ConstReference operator[](const Key &k) const {
- typename Map::const_iterator it = _map.find(k);
- if (it != _map.end())
- return it->second;
- else
- return _value;
- }
-
- /// \e
- void set(const Key &k, const T &t) {
- typename Map::iterator it = _map.lower_bound(k);
- if (it != _map.end() && !_map.key_comp()(k, it->first))
- it->second = t;
- else
- _map.insert(it, std::make_pair(k, t));
- }
-
- /// \e
- void setAll(const T &t) {
- _value = t;
- _map.clear();
- }
-
- template <typename T1, typename C1 = std::less<T1> >
- struct rebind {
- typedef StdMap<Key, T1, C1> other;
- };
- };
-
- ///Returns a \c StdMap class
-
- ///This function just returns a \c StdMap class with specified
- ///default value.
- ///\relates StdMap
- template<typename K, typename V, typename Compare>
- inline StdMap<K, V, Compare> stdMap(const V& value = V()) {
- return StdMap<K, V, Compare>(value);
- }
-
- template<typename K, typename V>
- inline StdMap<K, V, std::less<K> > stdMap(const V& value = V()) {
- return StdMap<K, V, std::less<K> >(value);
- }
-
- ///Returns a \c StdMap class created from an appropriate \c std::map
-
- ///This function just returns a \c StdMap class created from an
- ///appropriate \c std::map.
- ///\relates StdMap
- template<typename K, typename V, typename Compare>
- inline StdMap<K, V, Compare> stdMap( const std::map<K, V, Compare> &map,
- const V& value = V() ) {
- return StdMap<K, V, Compare>(map, value);
- }
-
- /// \brief Map for storing values for keys from the range <tt>[0..size-1]</tt>
- ///
- /// This map has the <tt>[0..size-1]</tt> keyset and the values
- /// are stored in a \c std::vector<T> container. It can be used with
- /// some data structures, for example \c UnionFind, \c BinHeap, when
- /// the used items are small integer numbers.
- template <typename T>
- class IntegerMap : public MapBase<int, T> {
-
- template <typename T1>
- friend class IntegerMap;
-
- public:
-
- typedef MapBase<int, T> Parent;
- ///\e
- typedef typename Parent::Key Key;
- ///\e
- typedef typename Parent::Value Value;
- ///\e
- typedef T& Reference;
- ///\e
- typedef const T& ConstReference;
-
- typedef True ReferenceMapTag;
-
- private:
-
- typedef std::vector<T> Vector;
- Vector _vector;
-
- public:
-
- /// Constructor with specified default value
- IntegerMap(int size = 0, const T& value = T()) : _vector(size, value) {}
-
- /// \brief Constructs the map from an appropriate \c std::vector.
- template <typename T1>
- IntegerMap(const std::vector<T1>& vector)
- : _vector(vector.begin(), vector.end()) {}
-
- /// \brief Constructs a map from an other \ref IntegerMap.
- template <typename T1>
- IntegerMap(const IntegerMap<T1> &c)
- : _vector(c._vector.begin(), c._vector.end()) {}
-
- /// \brief Resize the container
- void resize(int size, const T& value = T()) {
- _vector.resize(size, value);
- }
-
- private:
-
- IntegerMap& operator=(const IntegerMap&);
-
- public:
-
- ///\e
- Reference operator[](Key k) {
- return _vector[k];
- }
-
- /// \e
- ConstReference operator[](Key k) const {
- return _vector[k];
- }
-
- /// \e
- void set(const Key &k, const T& t) {
- _vector[k] = t;
- }
-
- };
-
- ///Returns an \c IntegerMap class
-
- ///This function just returns an \c IntegerMap class.
- ///\relates IntegerMap
- template<typename T>
- inline IntegerMap<T> integerMap(int size = 0, const T& value = T()) {
- return IntegerMap<T>(size, value);
- }
-
- /// @}
-
- /// \addtogroup map_adaptors
- /// @{
-
- /// \brief Identity map.
- ///
- /// This map gives back the given key as value without any
- /// modification.
- template <typename T>
- class IdentityMap : public MapBase<T, T> {
- public:
- typedef MapBase<T, T> Parent;
- typedef typename Parent::Key Key;
- typedef typename Parent::Value Value;
-
- /// \e
- const T& operator[](const T& t) const {
- return t;
- }
- };
-
- ///Returns an \c IdentityMap class
-
- ///This function just returns an \c IdentityMap class.
- ///\relates IdentityMap
- template<typename T>
- inline IdentityMap<T> identityMap() {
- return IdentityMap<T>();
- }
-
-
- ///\brief Convert the \c Value of a map to another type using
- ///the default conversion.
- ///
- ///This \ref concepts::ReadMap "read only map"
- ///converts the \c Value of a map to type \c T.
- ///Its \c Key is inherited from \c M.
- template <typename M, typename T>
- class ConvertMap : public MapBase<typename M::Key, T> {
- const M& m;
- public:
- typedef MapBase<typename M::Key, T> Parent;
- typedef typename Parent::Key Key;
- typedef typename Parent::Value Value;
-
- ///Constructor
-
- ///Constructor
- ///\param _m is the underlying map
- ConvertMap(const M &_m) : m(_m) {};
-
- ///\e
- Value operator[](const Key& k) const {return m[k];}
- };
-
- ///Returns a \c ConvertMap class
-
- ///This function just returns a \c ConvertMap class.
- ///\relates ConvertMap
- template<typename T, typename M>
- inline ConvertMap<M, T> convertMap(const M &m) {
- return ConvertMap<M, T>(m);
- }
-
- ///Simple wrapping of a map
-
- ///This \ref concepts::ReadMap "read only map" returns the simple
- ///wrapping of the given map. Sometimes the reference maps cannot be
- ///combined with simple read maps. This map adaptor wraps the given
- ///map to simple read map.
- ///
- ///\sa SimpleWriteMap
- ///
- /// \todo Revise the misleading name
- template<typename M>
- class SimpleMap : public MapBase<typename M::Key, typename M::Value> {
- const M& m;
-
- public:
- typedef MapBase<typename M::Key, typename M::Value> Parent;
- typedef typename Parent::Key Key;
- typedef typename Parent::Value Value;
-
- ///Constructor
- SimpleMap(const M &_m) : m(_m) {};
- ///\e
- Value operator[](Key k) const {return m[k];}
- };
-
- ///Returns a \c SimpleMap class
-
- ///This function just returns a \c SimpleMap class.
- ///\relates SimpleMap
- template<typename M>
- inline SimpleMap<M> simpleMap(const M &m) {
- return SimpleMap<M>(m);
- }
-
- ///Simple writable wrapping of a map
-
- ///This \ref concepts::ReadWriteMap "read-write map" returns the simple
- ///wrapping of the given map. Sometimes the reference maps cannot be
- ///combined with simple read-write maps. This map adaptor wraps the
- ///given map to simple read-write map.
- ///
- ///\sa SimpleMap
- ///
- /// \todo Revise the misleading name
- template<typename M>
- class SimpleWriteMap : public MapBase<typename M::Key, typename M::Value> {
- M& m;
-
- public:
- typedef MapBase<typename M::Key, typename M::Value> Parent;
- typedef typename Parent::Key Key;
- typedef typename Parent::Value Value;
-
- ///Constructor
- SimpleWriteMap(M &_m) : m(_m) {};
- ///\e
- Value operator[](Key k) const {return m[k];}
- ///\e
- void set(Key k, const Value& c) { m.set(k, c); }
- };
-
- ///Returns a \c SimpleWriteMap class
-
- ///This function just returns a \c SimpleWriteMap class.
- ///\relates SimpleWriteMap
- template<typename M>
- inline SimpleWriteMap<M> simpleWriteMap(M &m) {
- return SimpleWriteMap<M>(m);
- }
-
- ///Sum of two maps
-
- ///This \ref concepts::ReadMap "read only map" returns the sum of the two
- ///given maps.
- ///Its \c Key and \c Value are inherited from \c M1.
- ///The \c Key and \c Value of \c M2 must be convertible to those of \c M1.
- template<typename M1, typename M2>
- class AddMap : public MapBase<typename M1::Key, typename M1::Value> {
- const M1& m1;
- const M2& m2;
-
- public:
- typedef MapBase<typename M1::Key, typename M1::Value> Parent;
- typedef typename Parent::Key Key;
- typedef typename Parent::Value Value;
-
- ///Constructor
- AddMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
- ///\e
- Value operator[](Key k) const {return m1[k]+m2[k];}
- };
-
- ///Returns an \c AddMap class
-
- ///This function just returns an \c AddMap class.
- ///\todo How to call these type of functions?
- ///
- ///\relates AddMap
- template<typename M1, typename M2>
- inline AddMap<M1, M2> addMap(const M1 &m1,const M2 &m2) {
- return AddMap<M1, M2>(m1,m2);
- }
-
- ///Shift a map with a constant.
-
- ///This \ref concepts::ReadMap "read only map" returns the sum of the
- ///given map and a constant value.
- ///Its \c Key and \c Value is inherited from \c M.
- ///
- ///Actually,
- ///\code
- /// ShiftMap<X> sh(x,v);
- ///\endcode
- ///is equivalent to
- ///\code
- /// ConstMap<X::Key, X::Value> c_tmp(v);
- /// AddMap<X, ConstMap<X::Key, X::Value> > sh(x,v);
- ///\endcode
- ///
- ///\sa ShiftWriteMap
- template<typename M, typename C = typename M::Value>
- class ShiftMap : public MapBase<typename M::Key, typename M::Value> {
- const M& m;
- C v;
- public:
- typedef MapBase<typename M::Key, typename M::Value> Parent;
- typedef typename Parent::Key Key;
- typedef typename Parent::Value Value;
-
- ///Constructor
-
- ///Constructor
- ///\param _m is the undelying map
- ///\param _v is the shift value
- ShiftMap(const M &_m, const C &_v ) : m(_m), v(_v) {};
- ///\e
- Value operator[](Key k) const {return m[k] + v;}
- };
-
- ///Shift a map with a constant (ReadWrite version).
-
- ///This \ref concepts::ReadWriteMap "read-write map" returns the sum of the
- ///given map and a constant value. It makes also possible to write the map.
- ///Its \c Key and \c Value are inherited from \c M.
- ///
- ///\sa ShiftMap
- template<typename M, typename C = typename M::Value>
- class ShiftWriteMap : public MapBase<typename M::Key, typename M::Value> {
- M& m;
- C v;
- public:
- typedef MapBase<typename M::Key, typename M::Value> Parent;
- typedef typename Parent::Key Key;
- typedef typename Parent::Value Value;
-
- ///Constructor
-
- ///Constructor
- ///\param _m is the undelying map
- ///\param _v is the shift value
- ShiftWriteMap(M &_m, const C &_v ) : m(_m), v(_v) {};
- /// \e
- Value operator[](Key k) const {return m[k] + v;}
- /// \e
- void set(Key k, const Value& c) { m.set(k, c - v); }
- };
-
- ///Returns a \c ShiftMap class
-
- ///This function just returns an \c ShiftMap class.
- ///\relates ShiftMap
- template<typename M, typename C>
- inline ShiftMap<M, C> shiftMap(const M &m,const C &v) {
- return ShiftMap<M, C>(m,v);
- }
-
- ///Returns a \c ShiftWriteMap class
-
- ///This function just returns a \c ShiftWriteMap class.
- ///\relates ShiftWriteMap
- template<typename M, typename C>
- inline ShiftWriteMap<M, C> shiftMap(M &m,const C &v) {
- return ShiftWriteMap<M, C>(m,v);
- }
-
- ///Difference of two maps
-
- ///This \ref concepts::ReadMap "read only map" returns the difference
- ///of the values of the two given maps.
- ///Its \c Key and \c Value are inherited from \c M1.
- ///The \c Key and \c Value of \c M2 must be convertible to those of \c M1.
-
- template<typename M1, typename M2>
- class SubMap : public MapBase<typename M1::Key, typename M1::Value> {
- const M1& m1;
- const M2& m2;
- public:
- typedef MapBase<typename M1::Key, typename M1::Value> Parent;
- typedef typename Parent::Key Key;
- typedef typename Parent::Value Value;
-
- ///Constructor
- SubMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
- /// \e
- Value operator[](Key k) const {return m1[k]-m2[k];}
- };
-
- ///Returns a \c SubMap class
-
- ///This function just returns a \c SubMap class.
- ///
- ///\relates SubMap
- template<typename M1, typename M2>
- inline SubMap<M1, M2> subMap(const M1 &m1, const M2 &m2) {
- return SubMap<M1, M2>(m1, m2);
- }
-
- ///Product of two maps
-
- ///This \ref concepts::ReadMap "read only map" returns the product of the
- ///values of the two given maps.
- ///Its \c Key and \c Value are inherited from \c M1.
- ///The \c Key and \c Value of \c M2 must be convertible to those of \c M1.
- template<typename M1, typename M2>
- class MulMap : public MapBase<typename M1::Key, typename M1::Value> {
- const M1& m1;
- const M2& m2;
- public:
- typedef MapBase<typename M1::Key, typename M1::Value> Parent;
- typedef typename Parent::Key Key;
- typedef typename Parent::Value Value;
-
- ///Constructor
- MulMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
- /// \e
- Value operator[](Key k) const {return m1[k]*m2[k];}
- };
-
- ///Returns a \c MulMap class
-
- ///This function just returns a \c MulMap class.
- ///\relates MulMap
- template<typename M1, typename M2>
- inline MulMap<M1, M2> mulMap(const M1 &m1,const M2 &m2) {
- return MulMap<M1, M2>(m1,m2);
- }
-
- ///Scales a map with a constant.
-
- ///This \ref concepts::ReadMap "read only map" returns the value of the
- ///given map multiplied from the left side with a constant value.
- ///Its \c Key and \c Value are inherited from \c M.
- ///
- ///Actually,
- ///\code
- /// ScaleMap<X> sc(x,v);
- ///\endcode
- ///is equivalent to
- ///\code
- /// ConstMap<X::Key, X::Value> c_tmp(v);
- /// MulMap<X, ConstMap<X::Key, X::Value> > sc(x,v);
- ///\endcode
- ///
- ///\sa ScaleWriteMap
- template<typename M, typename C = typename M::Value>
- class ScaleMap : public MapBase<typename M::Key, typename M::Value> {
- const M& m;
- C v;
- public:
- typedef MapBase<typename M::Key, typename M::Value> Parent;
- typedef typename Parent::Key Key;
- typedef typename Parent::Value Value;
-
- ///Constructor
-
- ///Constructor
- ///\param _m is the undelying map
- ///\param _v is the scaling value
- ScaleMap(const M &_m, const C &_v ) : m(_m), v(_v) {};
- /// \e
- Value operator[](Key k) const {return v * m[k];}
- };
-
- ///Scales a map with a constant (ReadWrite version).
-
- ///This \ref concepts::ReadWriteMap "read-write map" returns the value of the
- ///given map multiplied from the left side with a constant value. It can
- ///also be used as write map if the \c / operator is defined between
- ///\c Value and \c C and the given multiplier is not zero.
- ///Its \c Key and \c Value are inherited from \c M.
- ///
- ///\sa ScaleMap
- template<typename M, typename C = typename M::Value>
- class ScaleWriteMap : public MapBase<typename M::Key, typename M::Value> {
- M& m;
- C v;
- public:
- typedef MapBase<typename M::Key, typename M::Value> Parent;
- typedef typename Parent::Key Key;
- typedef typename Parent::Value Value;
-
- ///Constructor
-
- ///Constructor
- ///\param _m is the undelying map
- ///\param _v is the scaling value
- ScaleWriteMap(M &_m, const C &_v ) : m(_m), v(_v) {};
- /// \e
- Value operator[](Key k) const {return v * m[k];}
- /// \e
- void set(Key k, const Value& c) { m.set(k, c / v);}
- };
-
- ///Returns a \c ScaleMap class
-
- ///This function just returns a \c ScaleMap class.
- ///\relates ScaleMap
- template<typename M, typename C>
- inline ScaleMap<M, C> scaleMap(const M &m,const C &v) {
- return ScaleMap<M, C>(m,v);
- }
-
- ///Returns a \c ScaleWriteMap class
-
- ///This function just returns a \c ScaleWriteMap class.
- ///\relates ScaleWriteMap
- template<typename M, typename C>
- inline ScaleWriteMap<M, C> scaleMap(M &m,const C &v) {
- return ScaleWriteMap<M, C>(m,v);
- }
-
- ///Quotient of two maps
-
- ///This \ref concepts::ReadMap "read only map" returns the quotient of the
- ///values of the two given maps.
- ///Its \c Key and \c Value are inherited from \c M1.
- ///The \c Key and \c Value of \c M2 must be convertible to those of \c M1.
- template<typename M1, typename M2>
- class DivMap : public MapBase<typename M1::Key, typename M1::Value> {
- const M1& m1;
- const M2& m2;
- public:
- typedef MapBase<typename M1::Key, typename M1::Value> Parent;
- typedef typename Parent::Key Key;
- typedef typename Parent::Value Value;
-
- ///Constructor
- DivMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
- /// \e
- Value operator[](Key k) const {return m1[k]/m2[k];}
- };
-
- ///Returns a \c DivMap class
-
- ///This function just returns a \c DivMap class.
- ///\relates DivMap
- template<typename M1, typename M2>
- inline DivMap<M1, M2> divMap(const M1 &m1,const M2 &m2) {
- return DivMap<M1, M2>(m1,m2);
- }
-
- ///Composition of two maps
-
- ///This \ref concepts::ReadMap "read only map" returns the composition of
- ///two given maps.
- ///That is to say, if \c m1 is of type \c M1 and \c m2 is of \c M2,
- ///then for
- ///\code
- /// ComposeMap<M1, M2> cm(m1,m2);
- ///\endcode
- /// <tt>cm[x]</tt> will be equal to <tt>m1[m2[x]]</tt>.
- ///
- ///Its \c Key is inherited from \c M2 and its \c Value is from \c M1.
- ///\c M2::Value must be convertible to \c M1::Key.
- ///
- ///\sa CombineMap
- ///
- ///\todo Check the requirements.
- template <typename M1, typename M2>
- class ComposeMap : public MapBase<typename M2::Key, typename M1::Value> {
- const M1& m1;
- const M2& m2;
- public:
- typedef MapBase<typename M2::Key, typename M1::Value> Parent;
- typedef typename Parent::Key Key;
- typedef typename Parent::Value Value;
-
- ///Constructor
- ComposeMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
-
- typename MapTraits<M1>::ConstReturnValue
- /// \e
- operator[](Key k) const {return m1[m2[k]];}
- };
- ///Returns a \c ComposeMap class
-
- ///This function just returns a \c ComposeMap class.
- ///
- ///\relates ComposeMap
- template <typename M1, typename M2>
- inline ComposeMap<M1, M2> composeMap(const M1 &m1,const M2 &m2) {
- return ComposeMap<M1, M2>(m1,m2);
- }
-
- ///Combine of two maps using an STL (binary) functor.
-
- ///Combine of two maps using an STL (binary) functor.
- ///
- ///This \ref concepts::ReadMap "read only map" takes two maps and a
- ///binary functor and returns the composition of the two
- ///given maps unsing the functor.
- ///That is to say, if \c m1 and \c m2 is of type \c M1 and \c M2
- ///and \c f is of \c F, then for
- ///\code
- /// CombineMap<M1, M2,F,V> cm(m1,m2,f);
- ///\endcode
- /// <tt>cm[x]</tt> will be equal to <tt>f(m1[x],m2[x])</tt>
- ///
- ///Its \c Key is inherited from \c M1 and its \c Value is \c V.
- ///\c M2::Value and \c M1::Value must be convertible to the corresponding
- ///input parameter of \c F and the return type of \c F must be convertible
- ///to \c V.
- ///
- ///\sa ComposeMap
- ///
- ///\todo Check the requirements.
- template<typename M1, typename M2, typename F,
- typename V = typename F::result_type>
- class CombineMap : public MapBase<typename M1::Key, V> {
- const M1& m1;
- const M2& m2;
- F f;
- public:
- typedef MapBase<typename M1::Key, V> Parent;
- typedef typename Parent::Key Key;
- typedef typename Parent::Value Value;
-
- ///Constructor
- CombineMap(const M1 &_m1,const M2 &_m2,const F &_f = F())
- : m1(_m1), m2(_m2), f(_f) {};
- /// \e
- Value operator[](Key k) const {return f(m1[k],m2[k]);}
- };
-
- ///Returns a \c CombineMap class
-
- ///This function just returns a \c CombineMap class.
- ///
- ///For example if \c m1 and \c m2 are both \c double valued maps, then
- ///\code
- ///combineMap(m1,m2,std::plus<double>())
- ///\endcode
- ///is equivalent to
- ///\code
- ///addMap(m1,m2)
- ///\endcode
- ///
- ///This function is specialized for adaptable binary function
- ///classes and C++ functions.
- ///
- ///\relates CombineMap
- template<typename M1, typename M2, typename F, typename V>
- inline CombineMap<M1, M2, F, V>
- combineMap(const M1& m1,const M2& m2, const F& f) {
- return CombineMap<M1, M2, F, V>(m1,m2,f);
- }
-
- template<typename M1, typename M2, typename F>
- inline CombineMap<M1, M2, F, typename F::result_type>
- combineMap(const M1& m1, const M2& m2, const F& f) {
- return combineMap<M1, M2, F, typename F::result_type>(m1,m2,f);
- }
-
- template<typename M1, typename M2, typename K1, typename K2, typename V>
- inline CombineMap<M1, M2, V (*)(K1, K2), V>
- combineMap(const M1 &m1, const M2 &m2, V (*f)(K1, K2)) {
- return combineMap<M1, M2, V (*)(K1, K2), V>(m1,m2,f);
- }
-
- ///Negative value of a map
-
- ///This \ref concepts::ReadMap "read only map" returns the negative
- ///value of the value returned by the given map.
- ///Its \c Key and \c Value are inherited from \c M.
- ///The unary \c - operator must be defined for \c Value, of course.
- ///
- ///\sa NegWriteMap
- template<typename M>
- class NegMap : public MapBase<typename M::Key, typename M::Value> {
- const M& m;
- public:
- typedef MapBase<typename M::Key, typename M::Value> Parent;
- typedef typename Parent::Key Key;
- typedef typename Parent::Value Value;
-
- ///Constructor
- NegMap(const M &_m) : m(_m) {};
- /// \e
- Value operator[](Key k) const {return -m[k];}
- };
-
- ///Negative value of a map (ReadWrite version)
-
- ///This \ref concepts::ReadWriteMap "read-write map" returns the negative
- ///value of the value returned by the given map.
- ///Its \c Key and \c Value are inherited from \c M.
- ///The unary \c - operator must be defined for \c Value, of course.
- ///
- /// \sa NegMap
- template<typename M>
- class NegWriteMap : public MapBase<typename M::Key, typename M::Value> {
- M& m;
- public:
- typedef MapBase<typename M::Key, typename M::Value> Parent;
- typedef typename Parent::Key Key;
- typedef typename Parent::Value Value;
-
- ///Constructor
- NegWriteMap(M &_m) : m(_m) {};
- /// \e
- Value operator[](Key k) const {return -m[k];}
- /// \e
- void set(Key k, const Value& v) { m.set(k, -v); }
- };
-
- ///Returns a \c NegMap class
-
- ///This function just returns a \c NegMap class.
- ///\relates NegMap
- template <typename M>
- inline NegMap<M> negMap(const M &m) {
- return NegMap<M>(m);
- }
-
- ///Returns a \c NegWriteMap class
-
- ///This function just returns a \c NegWriteMap class.
- ///\relates NegWriteMap
- template <typename M>
- inline NegWriteMap<M> negMap(M &m) {
- return NegWriteMap<M>(m);
- }
-
- ///Absolute value of a map
-
- ///This \ref concepts::ReadMap "read only map" returns the absolute value
- ///of the value returned by the given map.
- ///Its \c Key and \c Value are inherited from \c M.
- ///\c Value must be comparable to \c 0 and the unary \c -
- ///operator must be defined for it, of course.
- ///
- ///\bug We need a unified way to handle the situation below:
- ///\code
- /// struct _UnConvertible {};
- /// template<class A> inline A t_abs(A a) {return _UnConvertible();}
- /// template<> inline int t_abs<>(int n) {return abs(n);}
- /// template<> inline long int t_abs<>(long int n) {return labs(n);}
- /// template<> inline long long int t_abs<>(long long int n) {return ::llabs(n);}
- /// template<> inline float t_abs<>(float n) {return fabsf(n);}
- /// template<> inline double t_abs<>(double n) {return fabs(n);}
- /// template<> inline long double t_abs<>(long double n) {return fabsl(n);}
- ///\endcode
-
-
- template<typename M>
- class AbsMap : public MapBase<typename M::Key, typename M::Value> {
- const M& m;
- public:
- typedef MapBase<typename M::Key, typename M::Value> Parent;
- typedef typename Parent::Key Key;
- typedef typename Parent::Value Value;
-
- ///Constructor
- AbsMap(const M &_m) : m(_m) {};
- /// \e
- Value operator[](Key k) const {
- Value tmp = m[k];
- return tmp >= 0 ? tmp : -tmp;
- }
-
- };
-
- ///Returns an \c AbsMap class
-
- ///This function just returns an \c AbsMap class.
- ///\relates AbsMap
- template<typename M>
- inline AbsMap<M> absMap(const M &m) {
- return AbsMap<M>(m);
- }
-
- ///Converts an STL style functor to a map
-
- ///This \ref concepts::ReadMap "read only map" returns the value
- ///of a given functor.
- ///
- ///Template parameters \c K and \c V will become its
- ///\c Key and \c Value.
- ///In most cases they have to be given explicitly because a
- ///functor typically does not provide \c argument_type and
- ///\c result_type typedefs.
- ///
- ///Parameter \c F is the type of the used functor.
- ///
- ///\sa MapFunctor
- template<typename F,
- typename K = typename F::argument_type,
- typename V = typename F::result_type>
- class FunctorMap : public MapBase<K, V> {
- F f;
- public:
- typedef MapBase<K, V> Parent;
- typedef typename Parent::Key Key;
- typedef typename Parent::Value Value;
-
- ///Constructor
- FunctorMap(const F &_f = F()) : f(_f) {}
- /// \e
- Value operator[](Key k) const { return f(k);}
- };
-
- ///Returns a \c FunctorMap class
-
- ///This function just returns a \c FunctorMap class.
- ///
- ///This function is specialized for adaptable binary function
- ///classes and C++ functions.
- ///
- ///\relates FunctorMap
- template<typename K, typename V, typename F> inline
- FunctorMap<F, K, V> functorMap(const F &f) {
- return FunctorMap<F, K, V>(f);
- }
-
- template <typename F> inline
- FunctorMap<F, typename F::argument_type, typename F::result_type>
- functorMap(const F &f) {
- return FunctorMap<F, typename F::argument_type,
- typename F::result_type>(f);
- }
-
- template <typename K, typename V> inline
- FunctorMap<V (*)(K), K, V> functorMap(V (*f)(K)) {
- return FunctorMap<V (*)(K), K, V>(f);
- }
-
-
- ///Converts a map to an STL style (unary) functor
-
- ///This class Converts a map to an STL style (unary) functor.
- ///That is it provides an <tt>operator()</tt> to read its values.
- ///
- ///For the sake of convenience it also works as
- ///a ususal \ref concepts::ReadMap "readable map",
- ///i.e. <tt>operator[]</tt> and the \c Key and \c Value typedefs also exist.
- ///
- ///\sa FunctorMap
- template <typename M>
- class MapFunctor : public MapBase<typename M::Key, typename M::Value> {
- const M& m;
- public:
- typedef MapBase<typename M::Key, typename M::Value> Parent;
- typedef typename Parent::Key Key;
- typedef typename Parent::Value Value;
-
- typedef typename M::Key argument_type;
- typedef typename M::Value result_type;
-
- ///Constructor
- MapFunctor(const M &_m) : m(_m) {};
- ///\e
- Value operator()(Key k) const {return m[k];}
- ///\e
- Value operator[](Key k) const {return m[k];}
- };
-
- ///Returns a \c MapFunctor class
-
- ///This function just returns a \c MapFunctor class.
- ///\relates MapFunctor
- template<typename M>
- inline MapFunctor<M> mapFunctor(const M &m) {
- return MapFunctor<M>(m);
- }
-
- ///Just readable version of \ref ForkWriteMap
-
- ///This map has two \ref concepts::ReadMap "readable map"
- ///parameters and each read request will be passed just to the
- ///first map. This class is the just readable map type of \c ForkWriteMap.
- ///
- ///The \c Key and \c Value are inherited from \c M1.
- ///The \c Key and \c Value of \c M2 must be convertible from those of \c M1.
- ///
- ///\sa ForkWriteMap
-
- template<typename M1, typename M2>
- class ForkMap : public MapBase<typename M1::Key, typename M1::Value> {
- const M1& m1;
- const M2& m2;
- public:
- typedef MapBase<typename M1::Key, typename M1::Value> Parent;
- typedef typename Parent::Key Key;
- typedef typename Parent::Value Value;
-
- ///Constructor
- ForkMap(const M1 &_m1, const M2 &_m2) : m1(_m1), m2(_m2) {};
- /// \e
- Value operator[](Key k) const {return m1[k];}
- };
-
-
- ///Applies all map setting operations to two maps
-
- ///This map has two \ref concepts::WriteMap "writable map"
- ///parameters and each write request will be passed to both of them.
- ///If \c M1 is also \ref concepts::ReadMap "readable",
- ///then the read operations will return the
- ///corresponding values of \c M1.
- ///
- ///The \c Key and \c Value are inherited from \c M1.
- ///The \c Key and \c Value of \c M2 must be convertible from those of \c M1.
- ///
- ///\sa ForkMap
- template<typename M1, typename M2>
- class ForkWriteMap : public MapBase<typename M1::Key, typename M1::Value> {
- M1& m1;
- M2& m2;
- public:
- typedef MapBase<typename M1::Key, typename M1::Value> Parent;
- typedef typename Parent::Key Key;
- typedef typename Parent::Value Value;
-
- ///Constructor
- ForkWriteMap(M1 &_m1, M2 &_m2) : m1(_m1), m2(_m2) {};
- ///\e
- Value operator[](Key k) const {return m1[k];}
- ///\e
- void set(Key k, const Value &v) {m1.set(k,v); m2.set(k,v);}
- };
-
- ///Returns a \c ForkMap class
-
- ///This function just returns a \c ForkMap class.
- ///\relates ForkMap
- template <typename M1, typename M2>
- inline ForkMap<M1, M2> forkMap(const M1 &m1, const M2 &m2) {
- return ForkMap<M1, M2>(m1,m2);
- }
-
- ///Returns a \c ForkWriteMap class
-
- ///This function just returns a \c ForkWriteMap class.
- ///\relates ForkWriteMap
- template <typename M1, typename M2>
- inline ForkWriteMap<M1, M2> forkMap(M1 &m1, M2 &m2) {
- return ForkWriteMap<M1, M2>(m1,m2);
- }
-
-
-
- /* ************* BOOL MAPS ******************* */
-
- ///Logical 'not' of a map
-
- ///This bool \ref concepts::ReadMap "read only map" returns the
- ///logical negation of the value returned by the given map.
- ///Its \c Key is inherited from \c M, its \c Value is \c bool.
- ///
- ///\sa NotWriteMap
- template <typename M>
- class NotMap : public MapBase<typename M::Key, bool> {
- const M& m;
- public:
- typedef MapBase<typename M::Key, bool> Parent;
- typedef typename Parent::Key Key;
- typedef typename Parent::Value Value;
-
- /// Constructor
- NotMap(const M &_m) : m(_m) {};
- ///\e
- Value operator[](Key k) const {return !m[k];}
- };
-
- ///Logical 'not' of a map (ReadWrie version)
-
- ///This bool \ref concepts::ReadWriteMap "read-write map" returns the
- ///logical negation of the value returned by the given map. When it is set,
- ///the opposite value is set to the original map.
- ///Its \c Key is inherited from \c M, its \c Value is \c bool.
- ///
- ///\sa NotMap
- template <typename M>
- class NotWriteMap : public MapBase<typename M::Key, bool> {
- M& m;
- public:
- typedef MapBase<typename M::Key, bool> Parent;
- typedef typename Parent::Key Key;
- typedef typename Parent::Value Value;
-
- /// Constructor
- NotWriteMap(M &_m) : m(_m) {};
- ///\e
- Value operator[](Key k) const {return !m[k];}
- ///\e
- void set(Key k, bool v) { m.set(k, !v); }
- };
-
- ///Returns a \c NotMap class
-
- ///This function just returns a \c NotMap class.
- ///\relates NotMap
- template <typename M>
- inline NotMap<M> notMap(const M &m) {
- return NotMap<M>(m);
- }
-
- ///Returns a \c NotWriteMap class
-
- ///This function just returns a \c NotWriteMap class.
- ///\relates NotWriteMap
- template <typename M>
- inline NotWriteMap<M> notMap(M &m) {
- return NotWriteMap<M>(m);
- }
-
- namespace _maps_bits {
-
- template <typename Value>
- struct Identity {
- typedef Value argument_type;
- typedef Value result_type;
- Value operator()(const Value& val) const {
- return val;
- }
- };
-
- template <typename _Iterator, typename Enable = void>
- struct IteratorTraits {
- typedef typename std::iterator_traits<_Iterator>::value_type Value;
- };
-
- template <typename _Iterator>
- struct IteratorTraits<_Iterator,
- typename exists<typename _Iterator::container_type>::type>
- {
- typedef typename _Iterator::container_type::value_type Value;
- };
-
- }
-
-
- /// \brief Writable bool map for logging each \c true assigned element
- ///
- /// A \ref concepts::ReadWriteMap "read-write" bool map for logging
- /// each \c true assigned element, i.e it copies all the keys set
- /// to \c true to the given iterator.
- ///
- /// \note The container of the iterator should contain space
- /// for each element.
- ///
- /// The following example shows how you can write the edges found by
- /// the \ref Prim algorithm directly to the standard output.
- ///\code
- /// typedef IdMap<UGraph, UEdge> UEdgeIdMap;
- /// UEdgeIdMap uedgeId(ugraph);
- ///
- /// typedef MapFunctor<UEdgeIdMap> UEdgeIdFunctor;
- /// UEdgeIdFunctor uedgeIdFunctor(uedgeId);
- ///
- /// StoreBoolMap<ostream_iterator<int>, UEdgeIdFunctor>
- /// writerMap(ostream_iterator<int>(cout, " "), uedgeIdFunctor);
- ///
- /// prim(ugraph, cost, writerMap);
- ///\endcode
- ///
- ///\sa BackInserterBoolMap
- ///\sa FrontInserterBoolMap
- ///\sa InserterBoolMap
- template <typename _Iterator,
- typename _Functor =
- _maps_bits::Identity<typename _maps_bits::
- IteratorTraits<_Iterator>::Value> >
- class StoreBoolMap {
- public:
- typedef _Iterator Iterator;
-
- typedef typename _Functor::argument_type Key;
- typedef bool Value;
-
- typedef _Functor Functor;
-
- /// Constructor
- StoreBoolMap(Iterator it, const Functor& functor = Functor())
- : _begin(it), _end(it), _functor(functor) {}
-
- /// Gives back the given iterator set for the first key
- Iterator begin() const {
- return _begin;
- }
-
- /// Gives back the the 'after the last' iterator
- Iterator end() const {
- return _end;
- }
-
- /// The \c set function of the map
- void set(const Key& key, Value value) const {
- if (value) {
- *_end++ = _functor(key);
- }
- }
-
- private:
- Iterator _begin;
- mutable Iterator _end;
- Functor _functor;
- };
-
- /// \brief Writable bool map for logging each \c true assigned element in
- /// a back insertable container.
- ///
- /// Writable bool map for logging each \c true assigned element by pushing
- /// them into a back insertable container.
- /// It can be used to retrieve the items into a standard
- /// container. The next example shows how you can store the
- /// edges found by the Prim algorithm in a vector.
- ///
- ///\code
- /// vector<UEdge> span_tree_uedges;
- /// BackInserterBoolMap<vector<UEdge> > inserter_map(span_tree_uedges);
- /// prim(ugraph, cost, inserter_map);
- ///\endcode
- ///
- ///\sa StoreBoolMap
- ///\sa FrontInserterBoolMap
- ///\sa InserterBoolMap
- template <typename Container,
- typename Functor =
- _maps_bits::Identity<typename Container::value_type> >
- class BackInserterBoolMap {
- public:
- typedef typename Functor::argument_type Key;
- typedef bool Value;
-
- /// Constructor
- BackInserterBoolMap(Container& _container,
- const Functor& _functor = Functor())
- : container(_container), functor(_functor) {}
-
- /// The \c set function of the map
- void set(const Key& key, Value value) {
- if (value) {
- container.push_back(functor(key));
- }
- }
-
- private:
- Container& container;
- Functor functor;
- };
-
- /// \brief Writable bool map for logging each \c true assigned element in
- /// a front insertable container.
- ///
- /// Writable bool map for logging each \c true assigned element by pushing
- /// them into a front insertable container.
- /// It can be used to retrieve the items into a standard
- /// container. For example see \ref BackInserterBoolMap.
- ///
- ///\sa BackInserterBoolMap
- ///\sa InserterBoolMap
- template <typename Container,
- typename Functor =
- _maps_bits::Identity<typename Container::value_type> >
- class FrontInserterBoolMap {
- public:
- typedef typename Functor::argument_type Key;
- typedef bool Value;
-
- /// Constructor
- FrontInserterBoolMap(Container& _container,
- const Functor& _functor = Functor())
- : container(_container), functor(_functor) {}
-
- /// The \c set function of the map
- void set(const Key& key, Value value) {
- if (value) {
- container.push_front(functor(key));
- }
- }
-
- private:
- Container& container;
- Functor functor;
- };
-
- /// \brief Writable bool map for storing each \c true assigned element in
- /// an insertable container.
- ///
- /// Writable bool map for storing each \c true assigned element in an
- /// insertable container. It will insert all the keys set to \c true into
- /// the container.
- ///
- /// For example, if you want to store the cut arcs of the strongly
- /// connected components in a set you can use the next code:
- ///
- ///\code
- /// set<Edge> cut_edges;
- /// InserterBoolMap<set<Edge> > inserter_map(cut_edges);
- /// stronglyConnectedCutEdges(graph, cost, inserter_map);
- ///\endcode
- ///
- ///\sa BackInserterBoolMap
- ///\sa FrontInserterBoolMap
- template <typename Container,
- typename Functor =
- _maps_bits::Identity<typename Container::value_type> >
- class InserterBoolMap {
- public:
- typedef typename Container::value_type Key;
- typedef bool Value;
-
- /// Constructor with specified iterator
-
- /// Constructor with specified iterator.
- /// \param _container The container for storing the elements.
- /// \param _it The elements will be inserted before this iterator.
- /// \param _functor The functor that is used when an element is stored.
- InserterBoolMap(Container& _container, typename Container::iterator _it,
- const Functor& _functor = Functor())
- : container(_container), it(_it), functor(_functor) {}
-
- /// Constructor
-
- /// Constructor without specified iterator.
- /// The elements will be inserted before <tt>_container.end()</tt>.
- /// \param _container The container for storing the elements.
- /// \param _functor The functor that is used when an element is stored.
- InserterBoolMap(Container& _container, const Functor& _functor = Functor())
- : container(_container), it(_container.end()), functor(_functor) {}
-
- /// The \c set function of the map
- void set(const Key& key, Value value) {
- if (value) {
- it = container.insert(it, functor(key));
- ++it;
- }
- }
-
- private:
- Container& container;
- typename Container::iterator it;
- Functor functor;
- };
-
- /// \brief Writable bool map for filling each \c true assigned element with a
- /// given value.
- ///
- /// Writable bool map for filling each \c true assigned element with a
- /// given value. The value can set the container.
- ///
- /// The following code finds the connected components of a graph
- /// and stores it in the \c comp map:
- ///\code
- /// typedef UGraph::NodeMap<int> ComponentMap;
- /// ComponentMap comp(ugraph);
- /// typedef FillBoolMap<UGraph::NodeMap<int> > ComponentFillerMap;
- /// ComponentFillerMap filler(comp, 0);
- ///
- /// Dfs<UGraph>::DefProcessedMap<ComponentFillerMap>::Create dfs(ugraph);
- /// dfs.processedMap(filler);
- /// dfs.init();
- /// for (NodeIt it(ugraph); it != INVALID; ++it) {
- /// if (!dfs.reached(it)) {
- /// dfs.addSource(it);
- /// dfs.start();
- /// ++filler.fillValue();
- /// }
- /// }
- ///\endcode
- template <typename Map>
- class FillBoolMap {
- public:
- typedef typename Map::Key Key;
- typedef bool Value;
-
- /// Constructor
- FillBoolMap(Map& _map, const typename Map::Value& _fill)
- : map(_map), fill(_fill) {}
-
- /// Constructor
- FillBoolMap(Map& _map)
- : map(_map), fill() {}
-
- /// Gives back the current fill value
- const typename Map::Value& fillValue() const {
- return fill;
- }
-
- /// Gives back the current fill value
- typename Map::Value& fillValue() {
- return fill;
- }
-
- /// Sets the current fill value
- void fillValue(const typename Map::Value& _fill) {
- fill = _fill;
- }
-
- /// The \c set function of the map
- void set(const Key& key, Value value) {
- if (value) {
- map.set(key, fill);
- }
- }
-
- private:
- Map& map;
- typename Map::Value fill;
- };
-
-
- /// \brief Writable bool map for storing the sequence number of
- /// \c true assignments.
- ///
- /// Writable bool map that stores for each \c true assigned elements
- /// the sequence number of this setting.
- /// It makes it easy to calculate the leaving
- /// order of the nodes in the \c Dfs algorithm.
- ///
- ///\code
- /// typedef Graph::NodeMap<int> OrderMap;
- /// OrderMap order(graph);
- /// typedef SettingOrderBoolMap<OrderMap> OrderSetterMap;
- /// OrderSetterMap setter(order);
- /// Dfs<Graph>::DefProcessedMap<OrderSetterMap>::Create dfs(graph);
- /// dfs.processedMap(setter);
- /// dfs.init();
- /// for (NodeIt it(graph); it != INVALID; ++it) {
- /// if (!dfs.reached(it)) {
- /// dfs.addSource(it);
- /// dfs.start();
- /// }
- /// }
- ///\endcode
- ///
- /// The storing of the discovering order is more difficult because the
- /// ReachedMap should be readable in the dfs algorithm but the setting
- /// order map is not readable. Thus we must use the fork map:
- ///
- ///\code
- /// typedef Graph::NodeMap<int> OrderMap;
- /// OrderMap order(graph);
- /// typedef SettingOrderBoolMap<OrderMap> OrderSetterMap;
- /// OrderSetterMap setter(order);
- /// typedef Graph::NodeMap<bool> StoreMap;
- /// StoreMap store(graph);
- ///
- /// typedef ForkWriteMap<StoreMap, OrderSetterMap> ReachedMap;
- /// ReachedMap reached(store, setter);
- ///
- /// Dfs<Graph>::DefReachedMap<ReachedMap>::Create dfs(graph);
- /// dfs.reachedMap(reached);
- /// dfs.init();
- /// for (NodeIt it(graph); it != INVALID; ++it) {
- /// if (!dfs.reached(it)) {
- /// dfs.addSource(it);
- /// dfs.start();
- /// }
- /// }
- ///\endcode
- template <typename Map>
- class SettingOrderBoolMap {
- public:
- typedef typename Map::Key Key;
- typedef bool Value;
-
- /// Constructor
- SettingOrderBoolMap(Map& _map)
- : map(_map), counter(0) {}
-
- /// Number of set operations.
- int num() const {
- return counter;
- }
-
- /// The \c set function of the map
- void set(const Key& key, Value value) {
- if (value) {
- map.set(key, counter++);
- }
- }
-
- private:
- Map& map;
- int counter;
- };
-
- /// @}
-}
-
-#endif // LEMON_MAPS_H
diff --git a/src/lemon/math.h b/src/lemon/math.h
deleted file mode 100644
index c837a83..0000000
--- a/src/lemon/math.h
+++ /dev/null
@@ -1,63 +0,0 @@
-/* -*- C++ -*-
- *
- * This file is a part of LEMON, a generic C++ optimization library
- *
- * Copyright (C) 2003-2008
- * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
- * (Egervary Research Group on Combinatorial Optimization, EGRES).
- *
- * Permission to use, modify and distribute this software is granted
- * provided that this copyright notice appears in all copies. For
- * precise terms see the accompanying LICENSE file.
- *
- * This software is provided "AS IS" with no warranty of any kind,
- * express or implied, and with no claim as to its suitability for any
- * purpose.
- *
- */
-
-#ifndef LEMON_MATH_H
-#define LEMON_MATH_H
-
-///\ingroup misc
-///\file
-///\brief Some extensions to the standard \c cmath library.
-///
-///Some extensions to the standard \c cmath library.
-///
-///This file includes the standard math library (cmath).
-
-#include<cmath>
-
-namespace lemon {
-
- /// \addtogroup misc
- /// @{
-
- /// The Euler constant
- const long double E = 2.7182818284590452353602874713526625L;
- /// log_2(e)
- const long double LOG2E = 1.4426950408889634073599246810018921L;
- /// log_10(e)
- const long double LOG10E = 0.4342944819032518276511289189166051L;
- /// ln(2)
- const long double LN2 = 0.6931471805599453094172321214581766L;
- /// ln(10)
- const long double LN10 = 2.3025850929940456840179914546843642L;
- /// pi
- const long double PI = 3.1415926535897932384626433832795029L;
- /// pi/2
- const long double PI_2 = 1.5707963267948966192313216916397514L;
- /// pi/4
- const long double PI_4 = 0.7853981633974483096156608458198757L;
- /// sqrt(2)
- const long double SQRT2 = 1.4142135623730950488016887242096981L;
- /// 1/sqrt(2)
- const long double SQRT1_2 = 0.7071067811865475244008443621048490L;
-
-
- /// @}
-
-} //namespace lemon
-
-#endif //LEMON_TOLERANCE_H
diff --git a/src/lemon/smart_graph.h b/src/lemon/smart_graph.h
deleted file mode 100644
index c47aa29..0000000
--- a/src/lemon/smart_graph.h
+++ /dev/null
@@ -1,1163 +0,0 @@
-/* -*- C++ -*-
- *
- * This file is a part of LEMON, a generic C++ optimization library
- *
- * Copyright (C) 2003-2008
- * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
- * (Egervary Research Group on Combinatorial Optimization, EGRES).
- *
- * Permission to use, modify and distribute this software is granted
- * provided that this copyright notice appears in all copies. For
- * precise terms see the accompanying LICENSE file.
- *
- * This software is provided "AS IS" with no warranty of any kind,
- * express or implied, and with no claim as to its suitability for any
- * purpose.
- *
- */
-
-#ifndef LEMON_SMART_GRAPH_H
-#define LEMON_SMART_GRAPH_H
-
-///\ingroup graphs
-///\file
-///\brief SmartGraph and SmartUGraph classes.
-
-#include <vector>
-
-#include <lemon/bits/invalid.h>
-
-#include <lemon/bits/base_extender.h>
-#include <lemon/bits/graph_extender.h>
-
-#include <lemon/bits/utility.h>
-#include <lemon/error.h>
-
-#include <lemon/bits/graph_extender.h>
-
-namespace lemon {
-
- class SmartGraph;
- ///Base of SmartGraph
-
- ///Base of SmartGraph
- ///
- class SmartGraphBase {
- protected:
-
- struct NodeT
- {
- int first_in, first_out;
- NodeT() {}
- };
- struct EdgeT
- {
- int target, source, next_in, next_out;
- EdgeT() {}
- };
-
- std::vector<NodeT> nodes;
-
- std::vector<EdgeT> edges;
-
-
- public:
-
- typedef SmartGraphBase Graph;
-
- class Node;
- class Edge;
-
-
- public:
-
- SmartGraphBase() : nodes(), edges() { }
- SmartGraphBase(const SmartGraphBase &_g)
- : nodes(_g.nodes), edges(_g.edges) { }
-
- typedef True NodeNumTag;
- typedef True EdgeNumTag;
-
- int nodeNum() const { return nodes.size(); }
- int edgeNum() const { return edges.size(); }
-
- int maxNodeId() const { return nodes.size()-1; }
- int maxEdgeId() const { return edges.size()-1; }
-
- Node addNode() {
- int n = nodes.size();
- nodes.push_back(NodeT());
- nodes[n].first_in = -1;
- nodes[n].first_out = -1;
- return Node(n);
- }
-
- Edge addEdge(Node u, Node v) {
- int n = edges.size();
- edges.push_back(EdgeT());
- edges[n].source = u.id;
- edges[n].target = v.id;
- edges[n].next_out = nodes[u.id].first_out;
- edges[n].next_in = nodes[v.id].first_in;
- nodes[u.id].first_out = nodes[v.id].first_in = n;
-
- return Edge(n);
- }
-
- void clear() {
- edges.clear();
- nodes.clear();
- }
-
- Node source(Edge e) const { return Node(edges[e.id].source); }
- Node target(Edge e) const { return Node(edges[e.id].target); }
-
- static int id(Node v) { return v.id; }
- static int id(Edge e) { return e.id; }
-
- static Node nodeFromId(int id) { return Node(id);}
- static Edge edgeFromId(int id) { return Edge(id);}
-
- class Node {
- friend class SmartGraphBase;
- friend class SmartGraph;
-
- protected:
- int id;
- explicit Node(int _id) : id(_id) {}
- public:
- Node() {}
- Node (Invalid) : id(-1) {}
- bool operator==(const Node i) const {return id == i.id;}
- bool operator!=(const Node i) const {return id != i.id;}
- bool operator<(const Node i) const {return id < i.id;}
- };
-
-
- class Edge {
- friend class SmartGraphBase;
- friend class SmartGraph;
-
- protected:
- int id;
- explicit Edge(int _id) : id(_id) {}
- public:
- Edge() { }
- Edge (Invalid) : id(-1) {}
- bool operator==(const Edge i) const {return id == i.id;}
- bool operator!=(const Edge i) const {return id != i.id;}
- bool operator<(const Edge i) const {return id < i.id;}
- };
-
- void first(Node& node) const {
- node.id = nodes.size() - 1;
- }
-
- static void next(Node& node) {
- --node.id;
- }
-
- void first(Edge& edge) const {
- edge.id = edges.size() - 1;
- }
-
- static void next(Edge& edge) {
- --edge.id;
- }
-
- void firstOut(Edge& edge, const Node& node) const {
- edge.id = nodes[node.id].first_out;
- }
-
- void nextOut(Edge& edge) const {
- edge.id = edges[edge.id].next_out;
- }
-
- void firstIn(Edge& edge, const Node& node) const {
- edge.id = nodes[node.id].first_in;
- }
-
- void nextIn(Edge& edge) const {
- edge.id = edges[edge.id].next_in;
- }
-
- };
-
- typedef GraphExtender<SmartGraphBase> ExtendedSmartGraphBase;
-
- ///\ingroup graphs
- ///
- ///\brief A smart graph class.
- ///
- ///This is a simple and fast graph implementation.
- ///It is also quite memory efficient, but at the price
- ///that <b> it does support only limited (only stack-like)
- ///node and edge deletions</b>.
- ///It conforms to
- ///the \ref concepts::Graph "Graph concept" with an
- ///important extra feature that
- ///its maps are real \ref concepts::ReferenceMap "reference map"s.
- ///
- ///\sa concepts::Graph.
- ///
- ///\author Alpar Juttner
- class SmartGraph : public ExtendedSmartGraphBase {
- public:
-
- typedef ExtendedSmartGraphBase Parent;
-
- private:
-
- ///SmartGraph is \e not copy constructible. Use GraphCopy() instead.
-
- ///SmartGraph is \e not copy constructible. Use GraphCopy() instead.
- ///
- SmartGraph(const SmartGraph &) : ExtendedSmartGraphBase() {};
- ///\brief Assignment of SmartGraph to another one is \e not allowed.
- ///Use GraphCopy() instead.
-
- ///Assignment of SmartGraph to another one is \e not allowed.
- ///Use GraphCopy() instead.
- void operator=(const SmartGraph &) {}
-
- public:
-
- /// Constructor
-
- /// Constructor.
- ///
- SmartGraph() {};
-
- ///Add a new node to the graph.
-
- /// \return the new node.
- ///
- Node addNode() { return Parent::addNode(); }
-
- ///Add a new edge to the graph.
-
- ///Add a new edge to the graph with source node \c s
- ///and target node \c t.
- ///\return the new edge.
- Edge addEdge(const Node& s, const Node& t) {
- return Parent::addEdge(s, t);
- }
-
- /// \brief Using this it is possible to avoid the superfluous memory
- /// allocation.
-
- /// Using this it is possible to avoid the superfluous memory
- /// allocation: if you know that the graph you want to build will
- /// be very large (e.g. it will contain millions of nodes and/or edges)
- /// then it is worth reserving space for this amount before starting
- /// to build the graph.
- /// \sa reserveEdge
- void reserveNode(int n) { nodes.reserve(n); };
-
- /// \brief Using this it is possible to avoid the superfluous memory
- /// allocation.
-
- /// Using this it is possible to avoid the superfluous memory
- /// allocation: if you know that the graph you want to build will
- /// be very large (e.g. it will contain millions of nodes and/or edges)
- /// then it is worth reserving space for this amount before starting
- /// to build the graph.
- /// \sa reserveNode
- void reserveEdge(int m) { edges.reserve(m); };
-
- ///Clear the graph.
-
- ///Erase all the nodes and edges from the graph.
- ///
- void clear() {
- Parent::clear();
- }
-
- ///Split a node.
-
- ///This function splits a node. First a new node is added to the graph,
- ///then the source of each outgoing edge of \c n is moved to this new node.
- ///If \c connect is \c true (this is the default value), then a new edge
- ///from \c n to the newly created node is also added.
- ///\return The newly created node.
- ///
- ///\note The <tt>Edge</tt>s
- ///referencing a moved edge remain
- ///valid. However <tt>InEdge</tt>'s and <tt>OutEdge</tt>'s
- ///may be invalidated.
- ///\warning This functionality cannot be used together with the Snapshot
- ///feature.
- ///\todo It could be implemented in a bit faster way.
- Node split(Node n, bool connect = true)
- {
- Node b = addNode();
- nodes[b.id].first_out=nodes[n.id].first_out;
- nodes[n.id].first_out=-1;
- for(int i=nodes[b.id].first_out;i!=-1;i++) edges[i].source=b.id;
- if(connect) addEdge(n,b);
- return b;
- }
-
- public:
-
- class Snapshot;
-
- protected:
-
- void restoreSnapshot(const Snapshot &s)
- {
- while(s.edge_num<edges.size()) {
- Edge edge = edgeFromId(edges.size()-1);
- Parent::notifier(Edge()).erase(edge);
- nodes[edges.back().source].first_out=edges.back().next_out;
- nodes[edges.back().target].first_in=edges.back().next_in;
- edges.pop_back();
- }
- while(s.node_num<nodes.size()) {
- Node node = nodeFromId(nodes.size()-1);
- Parent::notifier(Node()).erase(node);
- nodes.pop_back();
- }
- }
-
- public:
-
- ///Class to make a snapshot of the graph and to restrore to it later.
-
- ///Class to make a snapshot of the graph and to restrore to it later.
- ///
- ///The newly added nodes and edges can be removed using the
- ///restore() function.
- ///\note After you restore a state, you cannot restore
- ///a later state, in other word you cannot add again the edges deleted
- ///by restore() using another one Snapshot instance.
- ///
- ///\warning If you do not use correctly the snapshot that can cause
- ///either broken program, invalid state of the graph, valid but
- ///not the restored graph or no change. Because the runtime performance
- ///the validity of the snapshot is not stored.
- class Snapshot
- {
- SmartGraph *g;
- protected:
- friend class SmartGraph;
- unsigned int node_num;
- unsigned int edge_num;
- public:
- ///Default constructor.
-
- ///Default constructor.
- ///To actually make a snapshot you must call save().
- ///
- Snapshot() : g(0) {}
- ///Constructor that immediately makes a snapshot
-
- ///This constructor immediately makes a snapshot of the graph.
- ///\param _g The graph we make a snapshot of.
- Snapshot(SmartGraph &_g) :g(&_g) {
- node_num=g->nodes.size();
- edge_num=g->edges.size();
- }
-
- ///Make a snapshot.
-
- ///Make a snapshot of the graph.
- ///
- ///This function can be called more than once. In case of a repeated
- ///call, the previous snapshot gets lost.
- ///\param _g The graph we make the snapshot of.
- void save(SmartGraph &_g)
- {
- g=&_g;
- node_num=g->nodes.size();
- edge_num=g->edges.size();
- }
-
- ///Undo the changes until a snapshot.
-
- ///Undo the changes until a snapshot created by save().
- ///
- ///\note After you restored a state, you cannot restore
- ///a later state, in other word you cannot add again the edges deleted
- ///by restore().
- void restore()
- {
- g->restoreSnapshot(*this);
- }
- };
- };
-
-
- class SmartUGraphBase {
-
- protected:
-
- struct NodeT {
- int first_out;
- };
-
- struct EdgeT {
- int target;
- int next_out;
- };
-
- std::vector<NodeT> nodes;
- std::vector<EdgeT> edges;
-
- int first_free_edge;
-
- public:
-
- typedef SmartUGraphBase Graph;
-
- class Node;
- class Edge;
- class UEdge;
-
- class Node {
- friend class SmartUGraphBase;
- protected:
-
- int id;
- explicit Node(int pid) { id = pid;}
-
- public:
- Node() {}
- Node (Invalid) { id = -1; }
- bool operator==(const Node& node) const {return id == node.id;}
- bool operator!=(const Node& node) const {return id != node.id;}
- bool operator<(const Node& node) const {return id < node.id;}
- };
-
- class UEdge {
- friend class SmartUGraphBase;
- protected:
-
- int id;
- explicit UEdge(int pid) { id = pid;}
-
- public:
- UEdge() {}
- UEdge (Invalid) { id = -1; }
- bool operator==(const UEdge& edge) const {return id == edge.id;}
- bool operator!=(const UEdge& edge) const {return id != edge.id;}
- bool operator<(const UEdge& edge) const {return id < edge.id;}
- };
-
- class Edge {
- friend class SmartUGraphBase;
- protected:
-
- int id;
- explicit Edge(int pid) { id = pid;}
-
- public:
- operator UEdge() const { return uEdgeFromId(id / 2); }
-
- Edge() {}
- Edge (Invalid) { id = -1; }
- bool operator==(const Edge& edge) const {return id == edge.id;}
- bool operator!=(const Edge& edge) const {return id != edge.id;}
- bool operator<(const Edge& edge) const {return id < edge.id;}
- };
-
-
-
- SmartUGraphBase()
- : nodes(), edges() {}
-
-
- int maxNodeId() const { return nodes.size()-1; }
- int maxUEdgeId() const { return edges.size() / 2 - 1; }
- int maxEdgeId() const { return edges.size()-1; }
-
- Node source(Edge e) const { return Node(edges[e.id ^ 1].target); }
- Node target(Edge e) const { return Node(edges[e.id].target); }
-
- Node source(UEdge e) const { return Node(edges[2 * e.id].target); }
- Node target(UEdge e) const { return Node(edges[2 * e.id + 1].target); }
-
- static bool direction(Edge e) {
- return (e.id & 1) == 1;
- }
-
- static Edge direct(UEdge e, bool d) {
- return Edge(e.id * 2 + (d ? 1 : 0));
- }
-
- void first(Node& node) const {
- node.id = nodes.size() - 1;
- }
-
- void next(Node& node) const {
- --node.id;
- }
-
- void first(Edge& edge) const {
- edge.id = edges.size() - 1;
- }
-
- void next(Edge& edge) const {
- --edge.id;
- }
-
- void first(UEdge& edge) const {
- edge.id = edges.size() / 2 - 1;
- }
-
- void next(UEdge& edge) const {
- --edge.id;
- }
-
- void firstOut(Edge &edge, const Node& v) const {
- edge.id = nodes[v.id].first_out;
- }
- void nextOut(Edge &edge) const {
- edge.id = edges[edge.id].next_out;
- }
-
- void firstIn(Edge &edge, const Node& v) const {
- edge.id = ((nodes[v.id].first_out) ^ 1);
- if (edge.id == -2) edge.id = -1;
- }
- void nextIn(Edge &edge) const {
- edge.id = ((edges[edge.id ^ 1].next_out) ^ 1);
- if (edge.id == -2) edge.id = -1;
- }
-
- void firstInc(UEdge &edge, bool& d, const Node& v) const {
- int de = nodes[v.id].first_out;
- if (de != -1) {
- edge.id = de / 2;
- d = ((de & 1) == 1);
- } else {
- edge.id = -1;
- d = true;
- }
- }
- void nextInc(UEdge &edge, bool& d) const {
- int de = (edges[(edge.id * 2) | (d ? 1 : 0)].next_out);
- if (de != -1) {
- edge.id = de / 2;
- d = ((de & 1) == 1);
- } else {
- edge.id = -1;
- d = true;
- }
- }
-
- static int id(Node v) { return v.id; }
- static int id(Edge e) { return e.id; }
- static int id(UEdge e) { return e.id; }
-
- static Node nodeFromId(int id) { return Node(id);}
- static Edge edgeFromId(int id) { return Edge(id);}
- static UEdge uEdgeFromId(int id) { return UEdge(id);}
-
- Node addNode() {
- int n = nodes.size();
- nodes.push_back(NodeT());
- nodes[n].first_out = -1;
-
- return Node(n);
- }
-
- UEdge addEdge(Node u, Node v) {
- int n = edges.size();
- edges.push_back(EdgeT());
- edges.push_back(EdgeT());
-
- edges[n].target = u.id;
- edges[n | 1].target = v.id;
-
- edges[n].next_out = nodes[v.id].first_out;
- nodes[v.id].first_out = n;
-
- edges[n | 1].next_out = nodes[u.id].first_out;
- nodes[u.id].first_out = (n | 1);
-
- return UEdge(n / 2);
- }
-
- void clear() {
- edges.clear();
- nodes.clear();
- }
-
- };
-
- typedef UGraphExtender<SmartUGraphBase> ExtendedSmartUGraphBase;
-
- /// \ingroup graphs
- ///
- /// \brief A smart undirected graph class.
- ///
- /// This is a simple and fast undirected graph implementation.
- /// It is also quite memory efficient, but at the price
- /// that <b> it does support only limited (only stack-like)
- /// node and edge deletions</b>.
- /// Except from this it conforms to
- /// the \ref concepts::UGraph "UGraph concept".
- ///
- ///It also has an
- ///important extra feature that
- ///its maps are real \ref concepts::ReferenceMap "reference map"s.
- ///
- /// \sa concepts::UGraph.
- ///
- class SmartUGraph : public ExtendedSmartUGraphBase {
- private:
-
- ///SmartUGraph is \e not copy constructible. Use UGraphCopy() instead.
-
- ///SmartUGraph is \e not copy constructible. Use UGraphCopy() instead.
- ///
- SmartUGraph(const SmartUGraph &) : ExtendedSmartUGraphBase() {};
-
- ///\brief Assignment of SmartUGraph to another one is \e not allowed.
- ///Use UGraphCopy() instead.
-
- ///Assignment of SmartUGraph to another one is \e not allowed.
- ///Use UGraphCopy() instead.
- void operator=(const SmartUGraph &) {}
-
- public:
-
- typedef ExtendedSmartUGraphBase Parent;
- typedef Parent::OutEdgeIt IncEdgeIt;
-
- /// Constructor
-
- /// Constructor.
- ///
- SmartUGraph() {}
-
- ///Add a new node to the graph.
-
- /// \return the new node.
- ///
- Node addNode() { return Parent::addNode(); }
-
- ///Add a new undirected edge to the graph.
-
- ///Add a new undirected edge to the graph with node \c s
- ///and \c t.
- ///\return the new undirected edge.
- UEdge addEdge(const Node& s, const Node& t) {
- return Parent::addEdge(s, t);
- }
-
- ///Clear the graph.
-
- ///Erase all the nodes and edges from the graph.
- ///
- void clear() {
- Parent::clear();
- }
-
- public:
-
- class Snapshot;
-
- protected:
-
- void saveSnapshot(Snapshot &s)
- {
- s.graph = this;
- s.node_num = nodes.size();
- s.edge_num = edges.size();
- }
-
- void restoreSnapshot(const Snapshot &s)
- {
- while(s.edge_num<edges.size()) {
- int n=edges.size()-1;
- UEdge edge=uEdgeFromId(n/2);
- Parent::notifier(UEdge()).erase(edge);
- std::vector<Edge> dir;
- dir.push_back(edgeFromId(n));
- dir.push_back(edgeFromId(n-1));
- Parent::notifier(Edge()).erase(dir);
- nodes[edges[n].target].first_out=edges[n].next_out;
- nodes[edges[n-1].target].first_out=edges[n-1].next_out;
- edges.pop_back();
- edges.pop_back();
- }
- while(s.node_num<nodes.size()) {
- int n=nodes.size()-1;
- Node node = nodeFromId(n);
- Parent::notifier(Node()).erase(node);
- nodes.pop_back();
- }
- }
-
- public:
-
- ///Class to make a snapshot of the graph and to restrore to it later.
-
- ///Class to make a snapshot of the graph and to restrore to it later.
- ///
- ///The newly added nodes and edges can be removed using the
- ///restore() function.
- ///
- ///\note After you restore a state, you cannot restore
- ///a later state, in other word you cannot add again the edges deleted
- ///by restore() using another one Snapshot instance.
- ///
- ///\warning If you do not use correctly the snapshot that can cause
- ///either broken program, invalid state of the graph, valid but
- ///not the restored graph or no change. Because the runtime performance
- ///the validity of the snapshot is not stored.
- class Snapshot
- {
- SmartUGraph *graph;
- protected:
- friend class SmartUGraph;
- unsigned int node_num;
- unsigned int edge_num;
- public:
- ///Default constructor.
-
- ///Default constructor.
- ///To actually make a snapshot you must call save().
- ///
- Snapshot() : graph(0) {}
- ///Constructor that immediately makes a snapshot
-
- ///This constructor immediately makes a snapshot of the graph.
- ///\param g The graph we make a snapshot of.
- Snapshot(SmartUGraph &g) {
- g.saveSnapshot(*this);
- }
-
- ///Make a snapshot.
-
- ///Make a snapshot of the graph.
- ///
- ///This function can be called more than once. In case of a repeated
- ///call, the previous snapshot gets lost.
- ///\param g The graph we make the snapshot of.
- void save(SmartUGraph &g)
- {
- g.saveSnapshot(*this);
- }
-
- ///Undo the changes until a snapshot.
-
- ///Undo the changes until a snapshot created by save().
- ///
- ///\note After you restored a state, you cannot restore
- ///a later state, in other word you cannot add again the edges deleted
- ///by restore().
- void restore()
- {
- graph->restoreSnapshot(*this);
- }
- };
- };
-
-
- class SmartBpUGraphBase {
- public:
-
- class NodeSetError : public LogicError {
- public:
- virtual const char* what() const throw() {
- return "lemon::SmartBpUGraph::NodeSetError";
- }
- };
-
- protected:
-
- struct NodeT {
- int first;
- NodeT() {}
- NodeT(int _first) : first(_first) {}
- };
-
- struct UEdgeT {
- int aNode, next_out;
- int bNode, next_in;
- };
-
- std::vector<NodeT> aNodes;
- std::vector<NodeT> bNodes;
-
- std::vector<UEdgeT> edges;
-
- public:
-
- class Node {
- friend class SmartBpUGraphBase;
- protected:
- int id;
-
- explicit Node(int _id) : id(_id) {}
- public:
- Node() {}
- Node(Invalid) : id(-1) {}
- bool operator==(const Node i) const {return id==i.id;}
- bool operator!=(const Node i) const {return id!=i.id;}
- bool operator<(const Node i) const {return id<i.id;}
- };
-
- class UEdge {
- friend class SmartBpUGraphBase;
- protected:
- int id;
-
- UEdge(int _id) : id(_id) {}
- public:
- UEdge() {}
- UEdge(Invalid) : id(-1) {}
- bool operator==(const UEdge i) const {return id==i.id;}
- bool operator!=(const UEdge i) const {return id!=i.id;}
- bool operator<(const UEdge i) const {return id<i.id;}
- };
-
- void firstANode(Node& node) const {
- node.id = 2 * aNodes.size() - 2;
- if (node.id < 0) node.id = -1;
- }
- void nextANode(Node& node) const {
- node.id -= 2;
- if (node.id < 0) node.id = -1;
- }
-
- void firstBNode(Node& node) const {
- node.id = 2 * bNodes.size() - 1;
- }
- void nextBNode(Node& node) const {
- node.id -= 2;
- }
-
- void first(Node& node) const {
- if (aNodes.size() > 0) {
- node.id = 2 * aNodes.size() - 2;
- } else {
- node.id = 2 * bNodes.size() - 1;
- }
- }
- void next(Node& node) const {
- node.id -= 2;
- if (node.id == -2) {
- node.id = 2 * bNodes.size() - 1;
- }
- }
-
- void first(UEdge& edge) const {
- edge.id = edges.size() - 1;
- }
- void next(UEdge& edge) const {
- --edge.id;
- }
-
- void firstFromANode(UEdge& edge, const Node& node) const {
- LEMON_ASSERT((node.id & 1) == 0, NodeSetError());
- edge.id = aNodes[node.id >> 1].first;
- }
- void nextFromANode(UEdge& edge) const {
- edge.id = edges[edge.id].next_out;
- }
-
- void firstFromBNode(UEdge& edge, const Node& node) const {
- LEMON_ASSERT((node.id & 1) == 1, NodeSetError());
- edge.id = bNodes[node.id >> 1].first;
- }
- void nextFromBNode(UEdge& edge) const {
- edge.id = edges[edge.id].next_in;
- }
-
- static int id(const Node& node) {
- return node.id;
- }
- static Node nodeFromId(int id) {
- return Node(id);
- }
- int maxNodeId() const {
- return aNodes.size() > bNodes.size() ?
- aNodes.size() * 2 - 2 : bNodes.size() * 2 - 1;
- }
-
- static int id(const UEdge& edge) {
- return edge.id;
- }
- static UEdge uEdgeFromId(int id) {
- return UEdge(id);
- }
- int maxUEdgeId() const {
- return edges.size();
- }
-
- static int aNodeId(const Node& node) {
- return node.id >> 1;
- }
- static Node nodeFromANodeId(int id) {
- return Node(id << 1);
- }
- int maxANodeId() const {
- return aNodes.size();
- }
-
- static int bNodeId(const Node& node) {
- return node.id >> 1;
- }
- static Node nodeFromBNodeId(int id) {
- return Node((id << 1) + 1);
- }
- int maxBNodeId() const {
- return bNodes.size();
- }
-
- Node aNode(const UEdge& edge) const {
- return Node(edges[edge.id].aNode);
- }
- Node bNode(const UEdge& edge) const {
- return Node(edges[edge.id].bNode);
- }
-
- static bool aNode(const Node& node) {
- return (node.id & 1) == 0;
- }
-
- static bool bNode(const Node& node) {
- return (node.id & 1) == 1;
- }
-
- Node addANode() {
- NodeT nodeT;
- nodeT.first = -1;
- aNodes.push_back(nodeT);
- return Node(aNodes.size() * 2 - 2);
- }
-
- Node addBNode() {
- NodeT nodeT;
- nodeT.first = -1;
- bNodes.push_back(nodeT);
- return Node(bNodes.size() * 2 - 1);
- }
-
- UEdge addEdge(const Node& source, const Node& target) {
- LEMON_ASSERT(((source.id ^ target.id) & 1) == 1, NodeSetError());
- UEdgeT edgeT;
- if ((source.id & 1) == 0) {
- edgeT.aNode = source.id;
- edgeT.bNode = target.id;
- } else {
- edgeT.aNode = target.id;
- edgeT.bNode = source.id;
- }
- edgeT.next_out = aNodes[edgeT.aNode >> 1].first;
- aNodes[edgeT.aNode >> 1].first = edges.size();
- edgeT.next_in = bNodes[edgeT.bNode >> 1].first;
- bNodes[edgeT.bNode >> 1].first = edges.size();
- edges.push_back(edgeT);
- return UEdge(edges.size() - 1);
- }
-
- void reserveANode(int n) { aNodes.reserve(n); };
- void reserveBNode(int n) { bNodes.reserve(n); };
-
- void reserveEdge(int m) { edges.reserve(m); };
-
- void clear() {
- aNodes.clear();
- bNodes.clear();
- edges.clear();
- }
-
- typedef True NodeNumTag;
- int nodeNum() const { return aNodes.size() + bNodes.size(); }
- int aNodeNum() const { return aNodes.size(); }
- int bNodeNum() const { return bNodes.size(); }
-
- typedef True EdgeNumTag;
- int uEdgeNum() const { return edges.size(); }
-
- };
-
-
- typedef BpUGraphExtender<BidirBpUGraphExtender<SmartBpUGraphBase> >
- ExtendedSmartBpUGraphBase;
-
- /// \ingroup graphs
- ///
- /// \brief A smart bipartite undirected graph class.
- ///
- /// This is a simple and fast bipartite undirected graph implementation.
- /// It is also quite memory efficient, but at the price
- /// that <b> it does not support node and edge deletions</b>.
- /// Except from this it conforms to
- /// the \ref concepts::BpUGraph "BpUGraph concept".
- ///
- ///It also has an
- ///important extra feature that
- ///its maps are real \ref concepts::ReferenceMap "reference map"s.
- ///
- /// \sa concepts::BpUGraph.
- ///
- class SmartBpUGraph : public ExtendedSmartBpUGraphBase {
- private:
-
- /// \brief SmartBpUGraph is \e not copy constructible.
- ///
- ///SmartBpUGraph is \e not copy constructible.
- SmartBpUGraph(const SmartBpUGraph &) : ExtendedSmartBpUGraphBase() {};
-
- /// \brief Assignment of SmartBpUGraph to another one is \e not
- /// allowed.
- ///
- /// Assignment of SmartBpUGraph to another one is \e not allowed.
- void operator=(const SmartBpUGraph &) {}
-
- public:
-
- typedef ExtendedSmartBpUGraphBase Parent;
-
- ///Constructor
-
- ///Constructor.
- ///
- SmartBpUGraph() : ExtendedSmartBpUGraphBase() {}
-
- ///Add a new ANode to the graph.
-
- /// \return the new node.
- ///
- Node addANode() { return Parent::addANode(); }
-
- ///Add a new BNode to the graph.
-
- /// \return the new node.
- ///
- Node addBNode() { return Parent::addBNode(); }
-
- ///Add a new undirected edge to the graph.
-
- ///Add a new undirected edge to the graph with node \c s
- ///and \c t.
- ///\return the new undirected edge.
- UEdge addEdge(const Node& s, const Node& t) {
- return Parent::addEdge(s, t);
- }
-
- ///Clear the graph.
-
- ///Erase all the nodes and edges from the graph.
- ///
- void clear() {
- Parent::clear();
- }
-
- public:
-
- class Snapshot;
-
- protected:
-
- void restoreSnapshot(const Snapshot &s)
- {
- while(s.edge_num<edges.size()) {
- UEdge edge = uEdgeFromId(edges.size()-1);
- Parent::notifier(UEdge()).erase(edge);
- std::vector<Edge> dir;
- dir.push_back(Parent::direct(edge, true));
- dir.push_back(Parent::direct(edge, false));
- Parent::notifier(Edge()).erase(dir);
- aNodes[edges.back().aNode >> 1].first=edges.back().next_out;
- bNodes[edges.back().bNode >> 1].first=edges.back().next_in;
- edges.pop_back();
- }
- while(s.anode_num<aNodes.size()) {
- Node node = nodeFromANodeId(aNodes.size() - 1);
- Parent::notifier(ANode()).erase(node);
- Parent::notifier(Node()).erase(node);
- aNodes.pop_back();
- }
- while(s.bnode_num<bNodes.size()) {
- Node node = nodeFromBNodeId(bNodes.size() - 1);
- Parent::notifier(BNode()).erase(node);
- Parent::notifier(Node()).erase(node);
- bNodes.pop_back();
- }
- }
-
- public:
-
- ///Class to make a snapshot of the graph and to restrore to it later.
-
- ///Class to make a snapshot of the graph and to restrore to it later.
- ///
- ///The newly added nodes and edges can be removed using the
- ///restore() function.
- ///
- ///\note After you restore a state, you cannot restore
- ///a later state, in other word you cannot add again the edges deleted
- ///by restore() using another one Snapshot instance.
- ///
- ///\warning If you do not use correctly the snapshot that can cause
- ///either broken program, invalid state of the graph, valid but
- ///not the restored graph or no change. Because the runtime performance
- ///the validity of the snapshot is not stored.
- class Snapshot
- {
- SmartBpUGraph *g;
- protected:
- friend class SmartBpUGraph;
- unsigned int anode_num;
- unsigned int bnode_num;
- unsigned int edge_num;
- public:
- ///Default constructor.
-
- ///Default constructor.
- ///To actually make a snapshot you must call save().
- ///
- Snapshot() : g(0) {}
-
- ///Constructor that immediately makes a snapshot
-
- ///This constructor immediately makes a snapshot of the graph.
- ///\param _g The graph we make a snapshot of.
- Snapshot(SmartBpUGraph &_g) : g(&_g) {
- anode_num=g->aNodes.size();
- bnode_num=g->bNodes.size();
- edge_num=g->edges.size();
- }
-
- ///Make a snapshot.
-
- ///Make a snapshot of the graph.
- ///
- ///This function can be called more than once. In case of a repeated
- ///call, the previous snapshot gets lost.
- ///\param _g The graph we make the snapshot of.
- void save(SmartBpUGraph &_g)
- {
- g=&_g;
- anode_num=g->aNodes.size();
- bnode_num=g->bNodes.size();
- edge_num=g->edges.size();
- }
-
- ///Undo the changes until a snapshot.
-
- ///Undo the changes until a snapshot created by save().
- ///
- ///\note After you restored a state, you cannot restore
- ///a later state, in other word you cannot add again the edges deleted
- ///by restore().
- void restore()
- {
- g->restoreSnapshot(*this);
- }
- };
- };
-
-
- /// @}
-} //namespace lemon
-
-
-#endif //LEMON_SMART_GRAPH_H
diff --git a/src/lemon/tolerance.h b/src/lemon/tolerance.h
deleted file mode 100644
index 75c7e71..0000000
--- a/src/lemon/tolerance.h
+++ /dev/null
@@ -1,454 +0,0 @@
-/* -*- C++ -*-
- *
- * This file is a part of LEMON, a generic C++ optimization library
- *
- * Copyright (C) 2003-2008
- * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
- * (Egervary Research Group on Combinatorial Optimization, EGRES).
- *
- * Permission to use, modify and distribute this software is granted
- * provided that this copyright notice appears in all copies. For
- * precise terms see the accompanying LICENSE file.
- *
- * This software is provided "AS IS" with no warranty of any kind,
- * express or implied, and with no claim as to its suitability for any
- * purpose.
- *
- */
-
-#ifndef LEMON_TOLERANCE_H
-#define LEMON_TOLERANCE_H
-
-///\ingroup misc
-///\file
-///\brief A basic tool to handle the anomalies of calculation with
-///floating point numbers.
-///
-///\todo It should be in a module like "Basic tools"
-
-
-namespace lemon {
-
- /// \addtogroup misc
- /// @{
-
- ///\brief A class to provide a basic way to
- ///handle the comparison of numbers that are obtained
- ///as a result of a probably inexact computation.
- ///
- ///Tolerance is a class to provide a basic way to
- ///handle the comparison of numbers that are obtained
- ///as a result of a probably inexact computation.
- ///
- ///This is an abstract class, it should be specialized for all numerical
- ///data types. These specialized classes like \ref Tolerance\<double\>
- ///may offer additional tuning parameters.
- ///
- ///\sa Tolerance<float>
- ///\sa Tolerance<double>
- ///\sa Tolerance<long double>
- ///\sa Tolerance<int>
- ///\sa Tolerance<long long int>
- ///\sa Tolerance<unsigned int>
- ///\sa Tolerance<unsigned long long int>
-
- template<class T>
- class Tolerance
- {
- public:
- typedef T Value;
-
- ///\name Comparisons
- ///The concept is that these bool functions return with \c true only if
- ///the related comparisons hold even if some numerical error appeared
- ///during the computations.
-
- ///@{
-
- ///Returns \c true if \c a is \e surely strictly less than \c b
- static bool less(Value a,Value b) {return false;}
- ///Returns \c true if \c a is \e surely different from \c b
- static bool different(Value a,Value b) {return false;}
- ///Returns \c true if \c a is \e surely positive
- static bool positive(Value a) {return false;}
- ///Returns \c true if \c a is \e surely negative
- static bool negative(Value a) {return false;}
- ///Returns \c true if \c a is \e surely non-zero
- static bool nonZero(Value a) {return false;}
-
- ///@}
-
- ///Returns the zero value.
- static Value zero() {return T();}
-
- // static bool finite(Value a) {}
- // static Value big() {}
- // static Value negativeBig() {}
- };
-
-
- ///Float specialization of \ref Tolerance.
-
- ///Float specialization of \ref Tolerance.
- ///\sa Tolerance
- ///\relates Tolerance
- template<>
- class Tolerance<float>
- {
- static float def_epsilon;
- float _epsilon;
- public:
- ///\e
- typedef float Value;
-
- ///Constructor setting the epsilon tolerance to the default value.
- Tolerance() : _epsilon(def_epsilon) {}
- ///Constructor setting the epsilon tolerance.
- Tolerance(float e) : _epsilon(e) {}
-
- ///Return the epsilon value.
- Value epsilon() const {return _epsilon;}
- ///Set the epsilon value.
- void epsilon(Value e) {_epsilon=e;}
-
- ///Return the default epsilon value.
- static Value defaultEpsilon() {return def_epsilon;}
- ///Set the default epsilon value.
- static void defaultEpsilon(Value e) {def_epsilon=e;}
-
- ///\name Comparisons
- ///See class Tolerance for more details.
-
- ///@{
-
- ///Returns \c true if \c a is \e surely strictly less than \c b
- bool less(Value a,Value b) const {return a+_epsilon<b;}
- ///Returns \c true if \c a is \e surely different from \c b
- bool different(Value a,Value b) const { return less(a,b)||less(b,a); }
- ///Returns \c true if \c a is \e surely positive
- bool positive(Value a) const { return _epsilon<a; }
- ///Returns \c true if \c a is \e surely negative
- bool negative(Value a) const { return -_epsilon>a; }
- ///Returns \c true if \c a is \e surely non-zero
- bool nonZero(Value a) const { return positive(a)||negative(a); };
-
- ///@}
-
- ///Returns zero
- static Value zero() {return 0;}
- };
-
- ///Double specialization of \ref Tolerance.
-
- ///Double specialization of \ref Tolerance.
- ///\sa Tolerance
- ///\relates Tolerance
- template<>
- class Tolerance<double>
- {
- static double def_epsilon;
- double _epsilon;
- public:
- ///\e
- typedef double Value;
-
- ///Constructor setting the epsilon tolerance to the default value.
- Tolerance() : _epsilon(def_epsilon) {}
- ///Constructor setting the epsilon tolerance.
- Tolerance(double e) : _epsilon(e) {}
-
- ///Return the epsilon value.
- Value epsilon() const {return _epsilon;}
- ///Set the epsilon value.
- void epsilon(Value e) {_epsilon=e;}
-
- ///Return the default epsilon value.
- static Value defaultEpsilon() {return def_epsilon;}
- ///Set the default epsilon value.
- static void defaultEpsilon(Value e) {def_epsilon=e;}
-
- ///\name Comparisons
- ///See class Tolerance for more details.
-
- ///@{
-
- ///Returns \c true if \c a is \e surely strictly less than \c b
- bool less(Value a,Value b) const {return a+_epsilon<b;}
- ///Returns \c true if \c a is \e surely different from \c b
- bool different(Value a,Value b) const { return less(a,b)||less(b,a); }
- ///Returns \c true if \c a is \e surely positive
- bool positive(Value a) const { return _epsilon<a; }
- ///Returns \c true if \c a is \e surely negative
- bool negative(Value a) const { return -_epsilon>a; }
- ///Returns \c true if \c a is \e surely non-zero
- bool nonZero(Value a) const { return positive(a)||negative(a); };
-
- ///@}
-
- ///Returns zero
- static Value zero() {return 0;}
- };
-
- ///Long double specialization of \ref Tolerance.
-
- ///Long double specialization of \ref Tolerance.
- ///\sa Tolerance
- ///\relates Tolerance
- template<>
- class Tolerance<long double>
- {
- static long double def_epsilon;
- long double _epsilon;
- public:
- ///\e
- typedef long double Value;
-
- ///Constructor setting the epsilon tolerance to the default value.
- Tolerance() : _epsilon(def_epsilon) {}
- ///Constructor setting the epsilon tolerance.
- Tolerance(long double e) : _epsilon(e) {}
-
- ///Return the epsilon value.
- Value epsilon() const {return _epsilon;}
- ///Set the epsilon value.
- void epsilon(Value e) {_epsilon=e;}
-
- ///Return the default epsilon value.
- static Value defaultEpsilon() {return def_epsilon;}
- ///Set the default epsilon value.
- static void defaultEpsilon(Value e) {def_epsilon=e;}
-
- ///\name Comparisons
- ///See class Tolerance for more details.
-
- ///@{
-
- ///Returns \c true if \c a is \e surely strictly less than \c b
- bool less(Value a,Value b) const {return a+_epsilon<b;}
- ///Returns \c true if \c a is \e surely different from \c b
- bool different(Value a,Value b) const { return less(a,b)||less(b,a); }
- ///Returns \c true if \c a is \e surely positive
- bool positive(Value a) const { return _epsilon<a; }
- ///Returns \c true if \c a is \e surely negative
- bool negative(Value a) const { return -_epsilon>a; }
- ///Returns \c true if \c a is \e surely non-zero
- bool nonZero(Value a) const { return positive(a)||negative(a); };
-
- ///@}
-
- ///Returns zero
- static Value zero() {return 0;}
- };
-
- ///Integer specialization of \ref Tolerance.
-
- ///Integer specialization of \ref Tolerance.
- ///\sa Tolerance
- template<>
- class Tolerance<int>
- {
- public:
- ///\e
- typedef int Value;
-
- ///\name Comparisons
- ///See \ref Tolerance for more details.
-
- ///@{
-
- ///Returns \c true if \c a is \e surely strictly less than \c b
- static bool less(Value a,Value b) { return a<b;}
- ///Returns \c true if \c a is \e surely different from \c b
- static bool different(Value a,Value b) { return a!=b; }
- ///Returns \c true if \c a is \e surely positive
- static bool positive(Value a) { return 0<a; }
- ///Returns \c true if \c a is \e surely negative
- static bool negative(Value a) { return 0>a; }
- ///Returns \c true if \c a is \e surely non-zero
- static bool nonZero(Value a) { return a!=0; };
-
- ///@}
-
- ///Returns zero
- static Value zero() {return 0;}
- };
-
- ///Unsigned integer specialization of \ref Tolerance.
-
- ///Unsigned integer specialization of \ref Tolerance.
- ///\sa Tolerance
- template<>
- class Tolerance<unsigned int>
- {
- public:
- ///\e
- typedef unsigned int Value;
-
- ///\name Comparisons
- ///See \ref Tolerance for more details.
-
- ///@{
-
- ///Returns \c true if \c a is \e surely strictly less than \c b
- static bool less(Value a,Value b) { return a<b;}
- ///Returns \c true if \c a is \e surely different from \c b
- static bool different(Value a,Value b) { return a!=b; }
- ///Returns \c true if \c a is \e surely positive
- static bool positive(Value a) { return 0<a; }
- ///Returns \c true if \c a is \e surely negative
- static bool negative(Value) { return false; }
- ///Returns \c true if \c a is \e surely non-zero
- static bool nonZero(Value a) { return a!=0; };
-
- ///@}
-
- ///Returns zero
- static Value zero() {return 0;}
- };
-
-
- ///Long integer specialization of \ref Tolerance.
-
- ///Long integer specialization of \ref Tolerance.
- ///\sa Tolerance
- template<>
- class Tolerance<long int>
- {
- public:
- ///\e
- typedef long int Value;
-
- ///\name Comparisons
- ///See \ref Tolerance for more details.
-
- ///@{
-
- ///Returns \c true if \c a is \e surely strictly less than \c b
- static bool less(Value a,Value b) { return a<b;}
- ///Returns \c true if \c a is \e surely different from \c b
- static bool different(Value a,Value b) { return a!=b; }
- ///Returns \c true if \c a is \e surely positive
- static bool positive(Value a) { return 0<a; }
- ///Returns \c true if \c a is \e surely negative
- static bool negative(Value a) { return 0>a; }
- ///Returns \c true if \c a is \e surely non-zero
- static bool nonZero(Value a) { return a!=0;};
-
- ///@}
-
- ///Returns zero
- static Value zero() {return 0;}
- };
-
- ///Unsigned long integer specialization of \ref Tolerance.
-
- ///Unsigned long integer specialization of \ref Tolerance.
- ///\sa Tolerance
- template<>
- class Tolerance<unsigned long int>
- {
- public:
- ///\e
- typedef unsigned long int Value;
-
- ///\name Comparisons
- ///See \ref Tolerance for more details.
-
- ///@{
-
- ///Returns \c true if \c a is \e surely strictly less than \c b
- static bool less(Value a,Value b) { return a<b;}
- ///Returns \c true if \c a is \e surely different from \c b
- static bool different(Value a,Value b) { return a!=b; }
- ///Returns \c true if \c a is \e surely positive
- static bool positive(Value a) { return 0<a; }
- ///Returns \c true if \c a is \e surely negative
- static bool negative(Value) { return false; }
- ///Returns \c true if \c a is \e surely non-zero
- static bool nonZero(Value a) { return a!=0;};
-
- ///@}
-
- ///Returns zero
- static Value zero() {return 0;}
- };
-
-#if defined __GNUC__ && !defined __STRICT_ANSI__
-
- ///Long long integer specialization of \ref Tolerance.
-
- ///Long long integer specialization of \ref Tolerance.
- ///\warning This class (more exactly, type <tt>long long</tt>)
- ///is not ansi compatible.
- ///\sa Tolerance
- template<>
- class Tolerance<long long int>
- {
- public:
- ///\e
- typedef long long int Value;
-
- ///\name Comparisons
- ///See \ref Tolerance for more details.
-
- ///@{
-
- ///Returns \c true if \c a is \e surely strictly less than \c b
- static bool less(Value a,Value b) { return a<b;}
- ///Returns \c true if \c a is \e surely different from \c b
- static bool different(Value a,Value b) { return a!=b; }
- ///Returns \c true if \c a is \e surely positive
- static bool positive(Value a) { return 0<a; }
- ///Returns \c true if \c a is \e surely negative
- static bool negative(Value a) { return 0>a; }
- ///Returns \c true if \c a is \e surely non-zero
- static bool nonZero(Value a) { return a!=0;};
-
- ///@}
-
- ///Returns zero
- static Value zero() {return 0;}
- };
-
- ///Unsigned long long integer specialization of \ref Tolerance.
-
- ///Unsigned long long integer specialization of \ref Tolerance.
- ///\warning This class (more exactly, type <tt>unsigned long long</tt>)
- ///is not ansi compatible.
- ///\sa Tolerance
- template<>
- class Tolerance<unsigned long long int>
- {
- public:
- ///\e
- typedef unsigned long long int Value;
-
- ///\name Comparisons
- ///See \ref Tolerance for more details.
-
- ///@{
-
- ///Returns \c true if \c a is \e surely strictly less than \c b
- static bool less(Value a,Value b) { return a<b;}
- ///Returns \c true if \c a is \e surely different from \c b
- static bool different(Value a,Value b) { return a!=b; }
- ///Returns \c true if \c a is \e surely positive
- static bool positive(Value a) { return 0<a; }
- ///Returns \c true if \c a is \e surely negative
- static bool negative(Value) { return false; }
- ///Returns \c true if \c a is \e surely non-zero
- static bool nonZero(Value a) { return a!=0;};
-
- ///@}
-
- ///Returns zero
- static Value zero() {return 0;}
- };
-
-#endif
-
- /// @}
-
-} //namespace lemon
-
-#endif //LEMON_TOLERANCE_H
diff --git a/src/lemon/topology.h b/src/lemon/topology.h
deleted file mode 100644
index 8bec384..0000000
--- a/src/lemon/topology.h
+++ /dev/null
@@ -1,1590 +0,0 @@
-/* -*- C++ -*-
- *
- * This file is a part of LEMON, a generic C++ optimization library
- *
- * Copyright (C) 2003-2008
- * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
- * (Egervary Research Group on Combinatorial Optimization, EGRES).
- *
- * Permission to use, modify and distribute this software is granted
- * provided that this copyright notice appears in all copies. For
- * precise terms see the accompanying LICENSE file.
- *
- * This software is provided "AS IS" with no warranty of any kind,
- * express or implied, and with no claim as to its suitability for any
- * purpose.
- *
- */
-
-#ifndef LEMON_TOPOLOGY_H
-#define LEMON_TOPOLOGY_H
-
-#include <lemon/dfs.h>
-#include <lemon/bfs.h>
-#include <lemon/graph_utils.h>
-#include <lemon/graph_adaptor.h>
-#include <lemon/maps.h>
-
-#include <lemon/concepts/graph.h>
-#include <lemon/concepts/ugraph.h>
-#include <lemon/concept_check.h>
-
-#include <lemon/bin_heap.h>
-#include <lemon/bucket_heap.h>
-
-#include <stack>
-#include <functional>
-
-/// \ingroup graph_prop
-/// \file
-/// \brief Topology related algorithms
-///
-/// Topology related algorithms
-
-namespace lemon {
-
- /// \ingroup graph_prop
- ///
- /// \brief Check that the given undirected graph is connected.
- ///
- /// Check that the given undirected graph connected.
- /// \param graph The undirected graph.
- /// \return %True when there is path between any two nodes in the graph.
- /// \note By definition, the empty graph is connected.
- template <typename UGraph>
- bool connected(const UGraph& graph) {
- checkConcept<concepts::UGraph, UGraph>();
- typedef typename UGraph::NodeIt NodeIt;
- if (NodeIt(graph) == INVALID) return true;
- Dfs<UGraph> dfs(graph);
- dfs.run(NodeIt(graph));
- for (NodeIt it(graph); it != INVALID; ++it) {
- if (!dfs.reached(it)) {
- return false;
- }
- }
- return true;
- }
-
- /// \ingroup graph_prop
- ///
- /// \brief Count the number of connected components of an undirected graph
- ///
- /// Count the number of connected components of an undirected graph
- ///
- /// \param graph The graph. It should be undirected.
- /// \return The number of components
- /// \note By definition, the empty graph consists
- /// of zero connected components.
- template <typename UGraph>
- int countConnectedComponents(const UGraph &graph) {
- checkConcept<concepts::UGraph, UGraph>();
- typedef typename UGraph::Node Node;
- typedef typename UGraph::Edge Edge;
-
- typedef NullMap<Node, Edge> PredMap;
- typedef NullMap<Node, int> DistMap;
-
- int compNum = 0;
- typename Bfs<UGraph>::
- template DefPredMap<PredMap>::
- template DefDistMap<DistMap>::
- Create bfs(graph);
-
- PredMap predMap;
- bfs.predMap(predMap);
-
- DistMap distMap;
- bfs.distMap(distMap);
-
- bfs.init();
- for(typename UGraph::NodeIt n(graph); n != INVALID; ++n) {
- if (!bfs.reached(n)) {
- bfs.addSource(n);
- bfs.start();
- ++compNum;
- }
- }
- return compNum;
- }
-
- /// \ingroup graph_prop
- ///
- /// \brief Find the connected components of an undirected graph
- ///
- /// Find the connected components of an undirected graph.
- ///
- /// \image html connected_components.png
- /// \image latex connected_components.eps "Connected components" width=\textwidth
- ///
- /// \param graph The graph. It should be undirected.
- /// \retval compMap A writable node map. The values will be set from 0 to
- /// the number of the connected components minus one. Each values of the map
- /// will be set exactly once, the values of a certain component will be
- /// set continuously.
- /// \return The number of components
- ///
- template <class UGraph, class NodeMap>
- int connectedComponents(const UGraph &graph, NodeMap &compMap) {
- checkConcept<concepts::UGraph, UGraph>();
- typedef typename UGraph::Node Node;
- typedef typename UGraph::Edge Edge;
- checkConcept<concepts::WriteMap<Node, int>, NodeMap>();
-
- typedef NullMap<Node, Edge> PredMap;
- typedef NullMap<Node, int> DistMap;
-
- int compNum = 0;
- typename Bfs<UGraph>::
- template DefPredMap<PredMap>::
- template DefDistMap<DistMap>::
- Create bfs(graph);
-
- PredMap predMap;
- bfs.predMap(predMap);
-
- DistMap distMap;
- bfs.distMap(distMap);
-
- bfs.init();
- for(typename UGraph::NodeIt n(graph); n != INVALID; ++n) {
- if(!bfs.reached(n)) {
- bfs.addSource(n);
- while (!bfs.emptyQueue()) {
- compMap.set(bfs.nextNode(), compNum);
- bfs.processNextNode();
- }
- ++compNum;
- }
- }
- return compNum;
- }
-
- namespace _topology_bits {
-
- template <typename Graph, typename Iterator >
- struct LeaveOrderVisitor : public DfsVisitor<Graph> {
- public:
- typedef typename Graph::Node Node;
- LeaveOrderVisitor(Iterator it) : _it(it) {}
-
- void leave(const Node& node) {
- *(_it++) = node;
- }
-
- private:
- Iterator _it;
- };
-
- template <typename Graph, typename Map>
- struct FillMapVisitor : public DfsVisitor<Graph> {
- public:
- typedef typename Graph::Node Node;
- typedef typename Map::Value Value;
-
- FillMapVisitor(Map& map, Value& value)
- : _map(map), _value(value) {}
-
- void reach(const Node& node) {
- _map.set(node, _value);
- }
- private:
- Map& _map;
- Value& _value;
- };
-
- template <typename Graph, typename EdgeMap>
- struct StronglyConnectedCutEdgesVisitor : public DfsVisitor<Graph> {
- public:
- typedef typename Graph::Node Node;
- typedef typename Graph::Edge Edge;
-
- StronglyConnectedCutEdgesVisitor(const Graph& graph, EdgeMap& cutMap,
- int& cutNum)
- : _graph(graph), _cutMap(cutMap), _cutNum(cutNum),
- _compMap(graph), _num(0) {
- }
-
- void stop(const Node&) {
- ++_num;
- }
-
- void reach(const Node& node) {
- _compMap.set(node, _num);
- }
-
- void examine(const Edge& edge) {
- if (_compMap[_graph.source(edge)] != _compMap[_graph.target(edge)]) {
- _cutMap.set(edge, true);
- ++_cutNum;
- }
- }
- private:
- const Graph& _graph;
- EdgeMap& _cutMap;
- int& _cutNum;
-
- typename Graph::template NodeMap<int> _compMap;
- int _num;
- };
-
- }
-
-
- /// \ingroup graph_prop
- ///
- /// \brief Check that the given directed graph is strongly connected.
- ///
- /// Check that the given directed graph is strongly connected. The
- /// graph is strongly connected when any two nodes of the graph are
- /// connected with directed paths in both direction.
- /// \return %False when the graph is not strongly connected.
- /// \see connected
- ///
- /// \note By definition, the empty graph is strongly connected.
- template <typename Graph>
- bool stronglyConnected(const Graph& graph) {
- checkConcept<concepts::Graph, Graph>();
-
- typedef typename Graph::Node Node;
- typedef typename Graph::NodeIt NodeIt;
-
- if (NodeIt(graph) == INVALID) return true;
-
- using namespace _topology_bits;
-
- typedef DfsVisitor<Graph> Visitor;
- Visitor visitor;
-
- DfsVisit<Graph, Visitor> dfs(graph, visitor);
- dfs.init();
- dfs.addSource(NodeIt(graph));
- dfs.start();
-
- for (NodeIt it(graph); it != INVALID; ++it) {
- if (!dfs.reached(it)) {
- return false;
- }
- }
-
- typedef RevGraphAdaptor<const Graph> RGraph;
- RGraph rgraph(graph);
-
- typedef DfsVisitor<Graph> RVisitor;
- RVisitor rvisitor;
-
- DfsVisit<RGraph, RVisitor> rdfs(rgraph, rvisitor);
- rdfs.init();
- rdfs.addSource(NodeIt(graph));
- rdfs.start();
-
- for (NodeIt it(graph); it != INVALID; ++it) {
- if (!rdfs.reached(it)) {
- return false;
- }
- }
-
- return true;
- }
-
- /// \ingroup graph_prop
- ///
- /// \brief Count the strongly connected components of a directed graph
- ///
- /// Count the strongly connected components of a directed graph.
- /// The strongly connected components are the classes of an
- /// equivalence relation on the nodes of the graph. Two nodes are in
- /// the same class if they are connected with directed paths in both
- /// direction.
- ///
- /// \param graph The graph.
- /// \return The number of components
- /// \note By definition, the empty graph has zero
- /// strongly connected components.
- template <typename Graph>
- int countStronglyConnectedComponents(const Graph& graph) {
- checkConcept<concepts::Graph, Graph>();
-
- using namespace _topology_bits;
-
- typedef typename Graph::Node Node;
- typedef typename Graph::Edge Edge;
- typedef typename Graph::NodeIt NodeIt;
- typedef typename Graph::EdgeIt EdgeIt;
-
- typedef std::vector<Node> Container;
- typedef typename Container::iterator Iterator;
-
- Container nodes(countNodes(graph));
- typedef LeaveOrderVisitor<Graph, Iterator> Visitor;
- Visitor visitor(nodes.begin());
-
- DfsVisit<Graph, Visitor> dfs(graph, visitor);
- dfs.init();
- for (NodeIt it(graph); it != INVALID; ++it) {
- if (!dfs.reached(it)) {
- dfs.addSource(it);
- dfs.start();
- }
- }
-
- typedef typename Container::reverse_iterator RIterator;
- typedef RevGraphAdaptor<const Graph> RGraph;
-
- RGraph rgraph(graph);
-
- typedef DfsVisitor<Graph> RVisitor;
- RVisitor rvisitor;
-
- DfsVisit<RGraph, RVisitor> rdfs(rgraph, rvisitor);
-
- int compNum = 0;
-
- rdfs.init();
- for (RIterator it = nodes.rbegin(); it != nodes.rend(); ++it) {
- if (!rdfs.reached(*it)) {
- rdfs.addSource(*it);
- rdfs.start();
- ++compNum;
- }
- }
- return compNum;
- }
-
- /// \ingroup graph_prop
- ///
- /// \brief Find the strongly connected components of a directed graph
- ///
- /// Find the strongly connected components of a directed graph. The
- /// strongly connected components are the classes of an equivalence
- /// relation on the nodes of the graph. Two nodes are in
- /// relationship when there are directed paths between them in both
- /// direction. In addition, the numbering of components will satisfy
- /// that there is no edge going from a higher numbered component to
- /// a lower.
- ///
- /// \image html strongly_connected_components.png
- /// \image latex strongly_connected_components.eps "Strongly connected components" width=\textwidth
- ///
- /// \param graph The graph.
- /// \retval compMap A writable node map. The values will be set from 0 to
- /// the number of the strongly connected components minus one. Each value
- /// of the map will be set exactly once, the values of a certain component
- /// will be set continuously.
- /// \return The number of components
- ///
- template <typename Graph, typename NodeMap>
- int stronglyConnectedComponents(const Graph& graph, NodeMap& compMap) {
- checkConcept<concepts::Graph, Graph>();
- typedef typename Graph::Node Node;
- typedef typename Graph::NodeIt NodeIt;
- checkConcept<concepts::WriteMap<Node, int>, NodeMap>();
-
- using namespace _topology_bits;
-
- typedef std::vector<Node> Container;
- typedef typename Container::iterator Iterator;
-
- Container nodes(countNodes(graph));
- typedef LeaveOrderVisitor<Graph, Iterator> Visitor;
- Visitor visitor(nodes.begin());
-
- DfsVisit<Graph, Visitor> dfs(graph, visitor);
- dfs.init();
- for (NodeIt it(graph); it != INVALID; ++it) {
- if (!dfs.reached(it)) {
- dfs.addSource(it);
- dfs.start();
- }
- }
-
- typedef typename Container::reverse_iterator RIterator;
- typedef RevGraphAdaptor<const Graph> RGraph;
-
- RGraph rgraph(graph);
-
- int compNum = 0;
-
- typedef FillMapVisitor<RGraph, NodeMap> RVisitor;
- RVisitor rvisitor(compMap, compNum);
-
- DfsVisit<RGraph, RVisitor> rdfs(rgraph, rvisitor);
-
- rdfs.init();
- for (RIterator it = nodes.rbegin(); it != nodes.rend(); ++it) {
- if (!rdfs.reached(*it)) {
- rdfs.addSource(*it);
- rdfs.start();
- ++compNum;
- }
- }
- return compNum;
- }
-
- /// \ingroup graph_prop
- ///
- /// \brief Find the cut edges of the strongly connected components.
- ///
- /// Find the cut edges of the strongly connected components.
- /// The strongly connected components are the classes of an equivalence
- /// relation on the nodes of the graph. Two nodes are in relationship
- /// when there are directed paths between them in both direction.
- /// The strongly connected components are separated by the cut edges.
- ///
- /// \param graph The graph.
- /// \retval cutMap A writable node map. The values will be set true when the
- /// edge is a cut edge.
- ///
- /// \return The number of cut edges
- template <typename Graph, typename EdgeMap>
- int stronglyConnectedCutEdges(const Graph& graph, EdgeMap& cutMap) {
- checkConcept<concepts::Graph, Graph>();
- typedef typename Graph::Node Node;
- typedef typename Graph::Edge Edge;
- typedef typename Graph::NodeIt NodeIt;
- checkConcept<concepts::WriteMap<Edge, bool>, EdgeMap>();
-
- using namespace _topology_bits;
-
- typedef std::vector<Node> Container;
- typedef typename Container::iterator Iterator;
-
- Container nodes(countNodes(graph));
- typedef LeaveOrderVisitor<Graph, Iterator> Visitor;
- Visitor visitor(nodes.begin());
-
- DfsVisit<Graph, Visitor> dfs(graph, visitor);
- dfs.init();
- for (NodeIt it(graph); it != INVALID; ++it) {
- if (!dfs.reached(it)) {
- dfs.addSource(it);
- dfs.start();
- }
- }
-
- typedef typename Container::reverse_iterator RIterator;
- typedef RevGraphAdaptor<const Graph> RGraph;
-
- RGraph rgraph(graph);
-
- int cutNum = 0;
-
- typedef StronglyConnectedCutEdgesVisitor<RGraph, EdgeMap> RVisitor;
- RVisitor rvisitor(rgraph, cutMap, cutNum);
-
- DfsVisit<RGraph, RVisitor> rdfs(rgraph, rvisitor);
-
- rdfs.init();
- for (RIterator it = nodes.rbegin(); it != nodes.rend(); ++it) {
- if (!rdfs.reached(*it)) {
- rdfs.addSource(*it);
- rdfs.start();
- }
- }
- return cutNum;
- }
-
- namespace _topology_bits {
-
- template <typename Graph>
- class CountBiNodeConnectedComponentsVisitor : public DfsVisitor<Graph> {
- public:
- typedef typename Graph::Node Node;
- typedef typename Graph::Edge Edge;
- typedef typename Graph::UEdge UEdge;
-
- CountBiNodeConnectedComponentsVisitor(const Graph& graph, int &compNum)
- : _graph(graph), _compNum(compNum),
- _numMap(graph), _retMap(graph), _predMap(graph), _num(0) {}
-
- void start(const Node& node) {
- _predMap.set(node, INVALID);
- }
-
- void reach(const Node& node) {
- _numMap.set(node, _num);
- _retMap.set(node, _num);
- ++_num;
- }
-
- void discover(const Edge& edge) {
- _predMap.set(_graph.target(edge), _graph.source(edge));
- }
-
- void examine(const Edge& edge) {
- if (_graph.source(edge) == _graph.target(edge) &&
- _graph.direction(edge)) {
- ++_compNum;
- return;
- }
- if (_predMap[_graph.source(edge)] == _graph.target(edge)) {
- return;
- }
- if (_retMap[_graph.source(edge)] > _numMap[_graph.target(edge)]) {
- _retMap.set(_graph.source(edge), _numMap[_graph.target(edge)]);
- }
- }
-
- void backtrack(const Edge& edge) {
- if (_retMap[_graph.source(edge)] > _retMap[_graph.target(edge)]) {
- _retMap.set(_graph.source(edge), _retMap[_graph.target(edge)]);
- }
- if (_numMap[_graph.source(edge)] <= _retMap[_graph.target(edge)]) {
- ++_compNum;
- }
- }
-
- private:
- const Graph& _graph;
- int& _compNum;
-
- typename Graph::template NodeMap<int> _numMap;
- typename Graph::template NodeMap<int> _retMap;
- typename Graph::template NodeMap<Node> _predMap;
- int _num;
- };
-
- template <typename Graph, typename EdgeMap>
- class BiNodeConnectedComponentsVisitor : public DfsVisitor<Graph> {
- public:
- typedef typename Graph::Node Node;
- typedef typename Graph::Edge Edge;
- typedef typename Graph::UEdge UEdge;
-
- BiNodeConnectedComponentsVisitor(const Graph& graph,
- EdgeMap& compMap, int &compNum)
- : _graph(graph), _compMap(compMap), _compNum(compNum),
- _numMap(graph), _retMap(graph), _predMap(graph), _num(0) {}
-
- void start(const Node& node) {
- _predMap.set(node, INVALID);
- }
-
- void reach(const Node& node) {
- _numMap.set(node, _num);
- _retMap.set(node, _num);
- ++_num;
- }
-
- void discover(const Edge& edge) {
- Node target = _graph.target(edge);
- _predMap.set(target, edge);
- _edgeStack.push(edge);
- }
-
- void examine(const Edge& edge) {
- Node source = _graph.source(edge);
- Node target = _graph.target(edge);
- if (source == target && _graph.direction(edge)) {
- _compMap.set(edge, _compNum);
- ++_compNum;
- return;
- }
- if (_numMap[target] < _numMap[source]) {
- if (_predMap[source] != _graph.oppositeEdge(edge)) {
- _edgeStack.push(edge);
- }
- }
- if (_predMap[source] != INVALID &&
- target == _graph.source(_predMap[source])) {
- return;
- }
- if (_retMap[source] > _numMap[target]) {
- _retMap.set(source, _numMap[target]);
- }
- }
-
- void backtrack(const Edge& edge) {
- Node source = _graph.source(edge);
- Node target = _graph.target(edge);
- if (_retMap[source] > _retMap[target]) {
- _retMap.set(source, _retMap[target]);
- }
- if (_numMap[source] <= _retMap[target]) {
- while (_edgeStack.top() != edge) {
- _compMap.set(_edgeStack.top(), _compNum);
- _edgeStack.pop();
- }
- _compMap.set(edge, _compNum);
- _edgeStack.pop();
- ++_compNum;
- }
- }
-
- private:
- const Graph& _graph;
- EdgeMap& _compMap;
- int& _compNum;
-
- typename Graph::template NodeMap<int> _numMap;
- typename Graph::template NodeMap<int> _retMap;
- typename Graph::template NodeMap<Edge> _predMap;
- std::stack<UEdge> _edgeStack;
- int _num;
- };
-
-
- template <typename Graph, typename NodeMap>
- class BiNodeConnectedCutNodesVisitor : public DfsVisitor<Graph> {
- public:
- typedef typename Graph::Node Node;
- typedef typename Graph::Edge Edge;
- typedef typename Graph::UEdge UEdge;
-
- BiNodeConnectedCutNodesVisitor(const Graph& graph, NodeMap& cutMap,
- int& cutNum)
- : _graph(graph), _cutMap(cutMap), _cutNum(cutNum),
- _numMap(graph), _retMap(graph), _predMap(graph), _num(0) {}
-
- void start(const Node& node) {
- _predMap.set(node, INVALID);
- rootCut = false;
- }
-
- void reach(const Node& node) {
- _numMap.set(node, _num);
- _retMap.set(node, _num);
- ++_num;
- }
-
- void discover(const Edge& edge) {
- _predMap.set(_graph.target(edge), _graph.source(edge));
- }
-
- void examine(const Edge& edge) {
- if (_graph.source(edge) == _graph.target(edge) &&
- _graph.direction(edge)) {
- if (!_cutMap[_graph.source(edge)]) {
- _cutMap.set(_graph.source(edge), true);
- ++_cutNum;
- }
- return;
- }
- if (_predMap[_graph.source(edge)] == _graph.target(edge)) return;
- if (_retMap[_graph.source(edge)] > _numMap[_graph.target(edge)]) {
- _retMap.set(_graph.source(edge), _numMap[_graph.target(edge)]);
- }
- }
-
- void backtrack(const Edge& edge) {
- if (_retMap[_graph.source(edge)] > _retMap[_graph.target(edge)]) {
- _retMap.set(_graph.source(edge), _retMap[_graph.target(edge)]);
- }
- if (_numMap[_graph.source(edge)] <= _retMap[_graph.target(edge)]) {
- if (_predMap[_graph.source(edge)] != INVALID) {
- if (!_cutMap[_graph.source(edge)]) {
- _cutMap.set(_graph.source(edge), true);
- ++_cutNum;
- }
- } else if (rootCut) {
- if (!_cutMap[_graph.source(edge)]) {
- _cutMap.set(_graph.source(edge), true);
- ++_cutNum;
- }
- } else {
- rootCut = true;
- }
- }
- }
-
- private:
- const Graph& _graph;
- NodeMap& _cutMap;
- int& _cutNum;
-
- typename Graph::template NodeMap<int> _numMap;
- typename Graph::template NodeMap<int> _retMap;
- typename Graph::template NodeMap<Node> _predMap;
- std::stack<UEdge> _edgeStack;
- int _num;
- bool rootCut;
- };
-
- }
-
- template <typename UGraph>
- int countBiNodeConnectedComponents(const UGraph& graph);
-
- /// \ingroup graph_prop
- ///
- /// \brief Checks the graph is bi-node-connected.
- ///
- /// This function checks that the undirected graph is bi-node-connected
- /// graph. The graph is bi-node-connected if any two undirected edge is
- /// on same circle.
- ///
- /// \param graph The graph.
- /// \return %True when the graph bi-node-connected.
- template <typename UGraph>
- bool biNodeConnected(const UGraph& graph) {
- return countBiNodeConnectedComponents(graph) == 1;
- }
-
- /// \ingroup graph_prop
- ///
- /// \brief Count the biconnected components.
- ///
- /// This function finds the bi-node-connected components in an undirected
- /// graph. The biconnected components are the classes of an equivalence
- /// relation on the undirected edges. Two undirected edge is in relationship
- /// when they are on same circle.
- ///
- /// \param graph The graph.
- /// \return The number of components.
- template <typename UGraph>
- int countBiNodeConnectedComponents(const UGraph& graph) {
- checkConcept<concepts::UGraph, UGraph>();
- typedef typename UGraph::NodeIt NodeIt;
-
- using namespace _topology_bits;
-
- typedef CountBiNodeConnectedComponentsVisitor<UGraph> Visitor;
-
- int compNum = 0;
- Visitor visitor(graph, compNum);
-
- DfsVisit<UGraph, Visitor> dfs(graph, visitor);
- dfs.init();
-
- for (NodeIt it(graph); it != INVALID; ++it) {
- if (!dfs.reached(it)) {
- dfs.addSource(it);
- dfs.start();
- }
- }
- return compNum;
- }
-
- /// \ingroup graph_prop
- ///
- /// \brief Find the bi-node-connected components.
- ///
- /// This function finds the bi-node-connected components in an undirected
- /// graph. The bi-node-connected components are the classes of an equivalence
- /// relation on the undirected edges. Two undirected edge are in relationship
- /// when they are on same circle.
- ///
- /// \image html node_biconnected_components.png
- /// \image latex node_biconnected_components.eps "bi-node-connected components" width=\textwidth
- ///
- /// \param graph The graph.
- /// \retval compMap A writable uedge map. The values will be set from 0
- /// to the number of the biconnected components minus one. Each values
- /// of the map will be set exactly once, the values of a certain component
- /// will be set continuously.
- /// \return The number of components.
- ///
- template <typename UGraph, typename UEdgeMap>
- int biNodeConnectedComponents(const UGraph& graph,
- UEdgeMap& compMap) {
- checkConcept<concepts::UGraph, UGraph>();
- typedef typename UGraph::NodeIt NodeIt;
- typedef typename UGraph::UEdge UEdge;
- checkConcept<concepts::WriteMap<UEdge, int>, UEdgeMap>();
-
- using namespace _topology_bits;
-
- typedef BiNodeConnectedComponentsVisitor<UGraph, UEdgeMap> Visitor;
-
- int compNum = 0;
- Visitor visitor(graph, compMap, compNum);
-
- DfsVisit<UGraph, Visitor> dfs(graph, visitor);
- dfs.init();
-
- for (NodeIt it(graph); it != INVALID; ++it) {
- if (!dfs.reached(it)) {
- dfs.addSource(it);
- dfs.start();
- }
- }
- return compNum;
- }
-
- /// \ingroup graph_prop
- ///
- /// \brief Find the bi-node-connected cut nodes.
- ///
- /// This function finds the bi-node-connected cut nodes in an undirected
- /// graph. The bi-node-connected components are the classes of an equivalence
- /// relation on the undirected edges. Two undirected edges are in
- /// relationship when they are on same circle. The biconnected components
- /// are separted by nodes which are the cut nodes of the components.
- ///
- /// \param graph The graph.
- /// \retval cutMap A writable edge map. The values will be set true when
- /// the node separate two or more components.
- /// \return The number of the cut nodes.
- template <typename UGraph, typename NodeMap>
- int biNodeConnectedCutNodes(const UGraph& graph, NodeMap& cutMap) {
- checkConcept<concepts::UGraph, UGraph>();
- typedef typename UGraph::Node Node;
- typedef typename UGraph::NodeIt NodeIt;
- checkConcept<concepts::WriteMap<Node, bool>, NodeMap>();
-
- using namespace _topology_bits;
-
- typedef BiNodeConnectedCutNodesVisitor<UGraph, NodeMap> Visitor;
-
- int cutNum = 0;
- Visitor visitor(graph, cutMap, cutNum);
-
- DfsVisit<UGraph, Visitor> dfs(graph, visitor);
- dfs.init();
-
- for (NodeIt it(graph); it != INVALID; ++it) {
- if (!dfs.reached(it)) {
- dfs.addSource(it);
- dfs.start();
- }
- }
- return cutNum;
- }
-
- namespace _topology_bits {
-
- template <typename Graph>
- class CountBiEdgeConnectedComponentsVisitor : public DfsVisitor<Graph> {
- public:
- typedef typename Graph::Node Node;
- typedef typename Graph::Edge Edge;
- typedef typename Graph::UEdge UEdge;
-
- CountBiEdgeConnectedComponentsVisitor(const Graph& graph, int &compNum)
- : _graph(graph), _compNum(compNum),
- _numMap(graph), _retMap(graph), _predMap(graph), _num(0) {}
-
- void start(const Node& node) {
- _predMap.set(node, INVALID);
- }
-
- void reach(const Node& node) {
- _numMap.set(node, _num);
- _retMap.set(node, _num);
- ++_num;
- }
-
- void leave(const Node& node) {
- if (_numMap[node] <= _retMap[node]) {
- ++_compNum;
- }
- }
-
- void discover(const Edge& edge) {
- _predMap.set(_graph.target(edge), edge);
- }
-
- void examine(const Edge& edge) {
- if (_predMap[_graph.source(edge)] == _graph.oppositeEdge(edge)) {
- return;
- }
- if (_retMap[_graph.source(edge)] > _retMap[_graph.target(edge)]) {
- _retMap.set(_graph.source(edge), _retMap[_graph.target(edge)]);
- }
- }
-
- void backtrack(const Edge& edge) {
- if (_retMap[_graph.source(edge)] > _retMap[_graph.target(edge)]) {
- _retMap.set(_graph.source(edge), _retMap[_graph.target(edge)]);
- }
- }
-
- private:
- const Graph& _graph;
- int& _compNum;
-
- typename Graph::template NodeMap<int> _numMap;
- typename Graph::template NodeMap<int> _retMap;
- typename Graph::template NodeMap<Edge> _predMap;
- int _num;
- };
-
- template <typename Graph, typename NodeMap>
- class BiEdgeConnectedComponentsVisitor : public DfsVisitor<Graph> {
- public:
- typedef typename Graph::Node Node;
- typedef typename Graph::Edge Edge;
- typedef typename Graph::UEdge UEdge;
-
- BiEdgeConnectedComponentsVisitor(const Graph& graph,
- NodeMap& compMap, int &compNum)
- : _graph(graph), _compMap(compMap), _compNum(compNum),
- _numMap(graph), _retMap(graph), _predMap(graph), _num(0) {}
-
- void start(const Node& node) {
- _predMap.set(node, INVALID);
- }
-
- void reach(const Node& node) {
- _numMap.set(node, _num);
- _retMap.set(node, _num);
- _nodeStack.push(node);
- ++_num;
- }
-
- void leave(const Node& node) {
- if (_numMap[node] <= _retMap[node]) {
- while (_nodeStack.top() != node) {
- _compMap.set(_nodeStack.top(), _compNum);
- _nodeStack.pop();
- }
- _compMap.set(node, _compNum);
- _nodeStack.pop();
- ++_compNum;
- }
- }
-
- void discover(const Edge& edge) {
- _predMap.set(_graph.target(edge), edge);
- }
-
- void examine(const Edge& edge) {
- if (_predMap[_graph.source(edge)] == _graph.oppositeEdge(edge)) {
- return;
- }
- if (_retMap[_graph.source(edge)] > _retMap[_graph.target(edge)]) {
- _retMap.set(_graph.source(edge), _retMap[_graph.target(edge)]);
- }
- }
-
- void backtrack(const Edge& edge) {
- if (_retMap[_graph.source(edge)] > _retMap[_graph.target(edge)]) {
- _retMap.set(_graph.source(edge), _retMap[_graph.target(edge)]);
- }
- }
-
- private:
- const Graph& _graph;
- NodeMap& _compMap;
- int& _compNum;
-
- typename Graph::template NodeMap<int> _numMap;
- typename Graph::template NodeMap<int> _retMap;
- typename Graph::template NodeMap<Edge> _predMap;
- std::stack<Node> _nodeStack;
- int _num;
- };
-
-
- template <typename Graph, typename EdgeMap>
- class BiEdgeConnectedCutEdgesVisitor : public DfsVisitor<Graph> {
- public:
- typedef typename Graph::Node Node;
- typedef typename Graph::Edge Edge;
- typedef typename Graph::UEdge UEdge;
-
- BiEdgeConnectedCutEdgesVisitor(const Graph& graph,
- EdgeMap& cutMap, int &cutNum)
- : _graph(graph), _cutMap(cutMap), _cutNum(cutNum),
- _numMap(graph), _retMap(graph), _predMap(graph), _num(0) {}
-
- void start(const Node& node) {
- _predMap[node] = INVALID;
- }
-
- void reach(const Node& node) {
- _numMap.set(node, _num);
- _retMap.set(node, _num);
- ++_num;
- }
-
- void leave(const Node& node) {
- if (_numMap[node] <= _retMap[node]) {
- if (_predMap[node] != INVALID) {
- _cutMap.set(_predMap[node], true);
- ++_cutNum;
- }
- }
- }
-
- void discover(const Edge& edge) {
- _predMap.set(_graph.target(edge), edge);
- }
-
- void examine(const Edge& edge) {
- if (_predMap[_graph.source(edge)] == _graph.oppositeEdge(edge)) {
- return;
- }
- if (_retMap[_graph.source(edge)] > _retMap[_graph.target(edge)]) {
- _retMap.set(_graph.source(edge), _retMap[_graph.target(edge)]);
- }
- }
-
- void backtrack(const Edge& edge) {
- if (_retMap[_graph.source(edge)] > _retMap[_graph.target(edge)]) {
- _retMap.set(_graph.source(edge), _retMap[_graph.target(edge)]);
- }
- }
-
- private:
- const Graph& _graph;
- EdgeMap& _cutMap;
- int& _cutNum;
-
- typename Graph::template NodeMap<int> _numMap;
- typename Graph::template NodeMap<int> _retMap;
- typename Graph::template NodeMap<Edge> _predMap;
- int _num;
- };
- }
-
- template <typename UGraph>
- int countBiEdgeConnectedComponents(const UGraph& graph);
-
- /// \ingroup graph_prop
- ///
- /// \brief Checks that the graph is bi-edge-connected.
- ///
- /// This function checks that the graph is bi-edge-connected. The undirected
- /// graph is bi-edge-connected when any two nodes are connected with two
- /// edge-disjoint paths.
- ///
- /// \param graph The undirected graph.
- /// \return The number of components.
- template <typename UGraph>
- bool biEdgeConnected(const UGraph& graph) {
- return countBiEdgeConnectedComponents(graph) == 1;
- }
-
- /// \ingroup graph_prop
- ///
- /// \brief Count the bi-edge-connected components.
- ///
- /// This function count the bi-edge-connected components in an undirected
- /// graph. The bi-edge-connected components are the classes of an equivalence
- /// relation on the nodes. Two nodes are in relationship when they are
- /// connected with at least two edge-disjoint paths.
- ///
- /// \param graph The undirected graph.
- /// \return The number of components.
- template <typename UGraph>
- int countBiEdgeConnectedComponents(const UGraph& graph) {
- checkConcept<concepts::UGraph, UGraph>();
- typedef typename UGraph::NodeIt NodeIt;
-
- using namespace _topology_bits;
-
- typedef CountBiEdgeConnectedComponentsVisitor<UGraph> Visitor;
-
- int compNum = 0;
- Visitor visitor(graph, compNum);
-
- DfsVisit<UGraph, Visitor> dfs(graph, visitor);
- dfs.init();
-
- for (NodeIt it(graph); it != INVALID; ++it) {
- if (!dfs.reached(it)) {
- dfs.addSource(it);
- dfs.start();
- }
- }
- return compNum;
- }
-
- /// \ingroup graph_prop
- ///
- /// \brief Find the bi-edge-connected components.
- ///
- /// This function finds the bi-edge-connected components in an undirected
- /// graph. The bi-edge-connected components are the classes of an equivalence
- /// relation on the nodes. Two nodes are in relationship when they are
- /// connected at least two edge-disjoint paths.
- ///
- /// \image html edge_biconnected_components.png
- /// \image latex edge_biconnected_components.eps "bi-edge-connected components" width=\textwidth
- ///
- /// \param graph The graph.
- /// \retval compMap A writable node map. The values will be set from 0 to
- /// the number of the biconnected components minus one. Each values
- /// of the map will be set exactly once, the values of a certain component
- /// will be set continuously.
- /// \return The number of components.
- ///
- template <typename UGraph, typename NodeMap>
- int biEdgeConnectedComponents(const UGraph& graph, NodeMap& compMap) {
- checkConcept<concepts::UGraph, UGraph>();
- typedef typename UGraph::NodeIt NodeIt;
- typedef typename UGraph::Node Node;
- checkConcept<concepts::WriteMap<Node, int>, NodeMap>();
-
- using namespace _topology_bits;
-
- typedef BiEdgeConnectedComponentsVisitor<UGraph, NodeMap> Visitor;
-
- int compNum = 0;
- Visitor visitor(graph, compMap, compNum);
-
- DfsVisit<UGraph, Visitor> dfs(graph, visitor);
- dfs.init();
-
- for (NodeIt it(graph); it != INVALID; ++it) {
- if (!dfs.reached(it)) {
- dfs.addSource(it);
- dfs.start();
- }
- }
- return compNum;
- }
-
- /// \ingroup graph_prop
- ///
- /// \brief Find the bi-edge-connected cut edges.
- ///
- /// This function finds the bi-edge-connected components in an undirected
- /// graph. The bi-edge-connected components are the classes of an equivalence
- /// relation on the nodes. Two nodes are in relationship when they are
- /// connected with at least two edge-disjoint paths. The bi-edge-connected
- /// components are separted by edges which are the cut edges of the
- /// components.
- ///
- /// \param graph The graph.
- /// \retval cutMap A writable node map. The values will be set true when the
- /// edge is a cut edge.
- /// \return The number of cut edges.
- template <typename UGraph, typename UEdgeMap>
- int biEdgeConnectedCutEdges(const UGraph& graph, UEdgeMap& cutMap) {
- checkConcept<concepts::UGraph, UGraph>();
- typedef typename UGraph::NodeIt NodeIt;
- typedef typename UGraph::UEdge UEdge;
- checkConcept<concepts::WriteMap<UEdge, bool>, UEdgeMap>();
-
- using namespace _topology_bits;
-
- typedef BiEdgeConnectedCutEdgesVisitor<UGraph, UEdgeMap> Visitor;
-
- int cutNum = 0;
- Visitor visitor(graph, cutMap, cutNum);
-
- DfsVisit<UGraph, Visitor> dfs(graph, visitor);
- dfs.init();
-
- for (NodeIt it(graph); it != INVALID; ++it) {
- if (!dfs.reached(it)) {
- dfs.addSource(it);
- dfs.start();
- }
- }
- return cutNum;
- }
-
-
- namespace _topology_bits {
-
- template <typename Graph, typename IntNodeMap>
- class TopologicalSortVisitor : public DfsVisitor<Graph> {
- public:
- typedef typename Graph::Node Node;
- typedef typename Graph::Edge edge;
-
- TopologicalSortVisitor(IntNodeMap& order, int num)
- : _order(order), _num(num) {}
-
- void leave(const Node& node) {
- _order.set(node, --_num);
- }
-
- private:
- IntNodeMap& _order;
- int _num;
- };
-
- }
-
- /// \ingroup graph_prop
- ///
- /// \brief Sort the nodes of a DAG into topolgical order.
- ///
- /// Sort the nodes of a DAG into topolgical order.
- ///
- /// \param graph The graph. It should be directed and acyclic.
- /// \retval order A writable node map. The values will be set from 0 to
- /// the number of the nodes in the graph minus one. Each values of the map
- /// will be set exactly once, the values will be set descending order.
- ///
- /// \see checkedTopologicalSort
- /// \see dag
- template <typename Graph, typename NodeMap>
- void topologicalSort(const Graph& graph, NodeMap& order) {
- using namespace _topology_bits;
-
- checkConcept<concepts::Graph, Graph>();
- checkConcept<concepts::WriteMap<typename Graph::Node, int>, NodeMap>();
-
- typedef typename Graph::Node Node;
- typedef typename Graph::NodeIt NodeIt;
- typedef typename Graph::Edge Edge;
-
- TopologicalSortVisitor<Graph, NodeMap>
- visitor(order, countNodes(graph));
-
- DfsVisit<Graph, TopologicalSortVisitor<Graph, NodeMap> >
- dfs(graph, visitor);
-
- dfs.init();
- for (NodeIt it(graph); it != INVALID; ++it) {
- if (!dfs.reached(it)) {
- dfs.addSource(it);
- dfs.start();
- }
- }
- }
-
- /// \ingroup graph_prop
- ///
- /// \brief Sort the nodes of a DAG into topolgical order.
- ///
- /// Sort the nodes of a DAG into topolgical order. It also checks
- /// that the given graph is DAG.
- ///
- /// \param graph The graph. It should be directed and acyclic.
- /// \retval order A readable - writable node map. The values will be set
- /// from 0 to the number of the nodes in the graph minus one. Each values
- /// of the map will be set exactly once, the values will be set descending
- /// order.
- /// \return %False when the graph is not DAG.
- ///
- /// \see topologicalSort
- /// \see dag
- template <typename Graph, typename NodeMap>
- bool checkedTopologicalSort(const Graph& graph, NodeMap& order) {
- using namespace _topology_bits;
-
- checkConcept<concepts::Graph, Graph>();
- checkConcept<concepts::ReadWriteMap<typename Graph::Node, int>, NodeMap>();
-
- typedef typename Graph::Node Node;
- typedef typename Graph::NodeIt NodeIt;
- typedef typename Graph::Edge Edge;
-
- order = constMap<Node, int, -1>();
-
- TopologicalSortVisitor<Graph, NodeMap>
- visitor(order, countNodes(graph));
-
- DfsVisit<Graph, TopologicalSortVisitor<Graph, NodeMap> >
- dfs(graph, visitor);
-
- dfs.init();
- for (NodeIt it(graph); it != INVALID; ++it) {
- if (!dfs.reached(it)) {
- dfs.addSource(it);
- while (!dfs.emptyQueue()) {
- Edge edge = dfs.nextEdge();
- Node target = graph.target(edge);
- if (dfs.reached(target) && order[target] == -1) {
- return false;
- }
- dfs.processNextEdge();
- }
- }
- }
- return true;
- }
-
- /// \ingroup graph_prop
- ///
- /// \brief Check that the given directed graph is a DAG.
- ///
- /// Check that the given directed graph is a DAG. The DAG is
- /// an Directed Acyclic Graph.
- /// \return %False when the graph is not DAG.
- /// \see acyclic
- template <typename Graph>
- bool dag(const Graph& graph) {
-
- checkConcept<concepts::Graph, Graph>();
-
- typedef typename Graph::Node Node;
- typedef typename Graph::NodeIt NodeIt;
- typedef typename Graph::Edge Edge;
-
- typedef typename Graph::template NodeMap<bool> ProcessedMap;
-
- typename Dfs<Graph>::template DefProcessedMap<ProcessedMap>::
- Create dfs(graph);
-
- ProcessedMap processed(graph);
- dfs.processedMap(processed);
-
- dfs.init();
- for (NodeIt it(graph); it != INVALID; ++it) {
- if (!dfs.reached(it)) {
- dfs.addSource(it);
- while (!dfs.emptyQueue()) {
- Edge edge = dfs.nextEdge();
- Node target = graph.target(edge);
- if (dfs.reached(target) && !processed[target]) {
- return false;
- }
- dfs.processNextEdge();
- }
- }
- }
- return true;
- }
-
- /// \ingroup graph_prop
- ///
- /// \brief Check that the given undirected graph is acyclic.
- ///
- /// Check that the given undirected graph acyclic.
- /// \param graph The undirected graph.
- /// \return %True when there is no circle in the graph.
- /// \see dag
- template <typename UGraph>
- bool acyclic(const UGraph& graph) {
- checkConcept<concepts::UGraph, UGraph>();
- typedef typename UGraph::Node Node;
- typedef typename UGraph::NodeIt NodeIt;
- typedef typename UGraph::Edge Edge;
- Dfs<UGraph> dfs(graph);
- dfs.init();
- for (NodeIt it(graph); it != INVALID; ++it) {
- if (!dfs.reached(it)) {
- dfs.addSource(it);
- while (!dfs.emptyQueue()) {
- Edge edge = dfs.nextEdge();
- Node source = graph.source(edge);
- Node target = graph.target(edge);
- if (dfs.reached(target) &&
- dfs.predEdge(source) != graph.oppositeEdge(edge)) {
- return false;
- }
- dfs.processNextEdge();
- }
- }
- }
- return true;
- }
-
- /// \ingroup graph_prop
- ///
- /// \brief Check that the given undirected graph is tree.
- ///
- /// Check that the given undirected graph is tree.
- /// \param graph The undirected graph.
- /// \return %True when the graph is acyclic and connected.
- template <typename UGraph>
- bool tree(const UGraph& graph) {
- checkConcept<concepts::UGraph, UGraph>();
- typedef typename UGraph::Node Node;
- typedef typename UGraph::NodeIt NodeIt;
- typedef typename UGraph::Edge Edge;
- Dfs<UGraph> dfs(graph);
- dfs.init();
- dfs.addSource(NodeIt(graph));
- while (!dfs.emptyQueue()) {
- Edge edge = dfs.nextEdge();
- Node source = graph.source(edge);
- Node target = graph.target(edge);
- if (dfs.reached(target) &&
- dfs.predEdge(source) != graph.oppositeEdge(edge)) {
- return false;
- }
- dfs.processNextEdge();
- }
- for (NodeIt it(graph); it != INVALID; ++it) {
- if (!dfs.reached(it)) {
- return false;
- }
- }
- return true;
- }
-
- namespace _topology_bits {
-
- template <typename Graph>
- class BipartiteVisitor : public BfsVisitor<Graph> {
- public:
- typedef typename Graph::Edge Edge;
- typedef typename Graph::Node Node;
-
- BipartiteVisitor(const Graph& graph, bool& bipartite)
- : _graph(graph), _part(graph), _bipartite(bipartite) {}
-
- void start(const Node& node) {
- _part[node] = true;
- }
- void discover(const Edge& edge) {
- _part.set(_graph.target(edge), !_part[_graph.source(edge)]);
- }
- void examine(const Edge& edge) {
- _bipartite = _bipartite &&
- _part[_graph.target(edge)] != _part[_graph.source(edge)];
- }
-
- private:
-
- const Graph& _graph;
- typename Graph::template NodeMap<bool> _part;
- bool& _bipartite;
- };
-
- template <typename Graph, typename PartMap>
- class BipartitePartitionsVisitor : public BfsVisitor<Graph> {
- public:
- typedef typename Graph::Edge Edge;
- typedef typename Graph::Node Node;
-
- BipartitePartitionsVisitor(const Graph& graph,
- PartMap& part, bool& bipartite)
- : _graph(graph), _part(part), _bipartite(bipartite) {}
-
- void start(const Node& node) {
- _part.set(node, true);
- }
- void discover(const Edge& edge) {
- _part.set(_graph.target(edge), !_part[_graph.source(edge)]);
- }
- void examine(const Edge& edge) {
- _bipartite = _bipartite &&
- _part[_graph.target(edge)] != _part[_graph.source(edge)];
- }
-
- private:
-
- const Graph& _graph;
- PartMap& _part;
- bool& _bipartite;
- };
- }
-
- /// \ingroup graph_prop
- ///
- /// \brief Check if the given undirected graph is bipartite or not
- ///
- /// The function checks if the given undirected \c graph graph is bipartite
- /// or not. The \ref Bfs algorithm is used to calculate the result.
- /// \param graph The undirected graph.
- /// \return %True if \c graph is bipartite, %false otherwise.
- /// \sa bipartitePartitions
- ///
- /// \author Balazs Attila Mihaly
- template<typename UGraph>
- inline bool bipartite(const UGraph &graph){
- using namespace _topology_bits;
-
- checkConcept<concepts::UGraph, UGraph>();
-
- typedef typename UGraph::NodeIt NodeIt;
- typedef typename UGraph::EdgeIt EdgeIt;
-
- bool bipartite = true;
-
- BipartiteVisitor<UGraph>
- visitor(graph, bipartite);
- BfsVisit<UGraph, BipartiteVisitor<UGraph> >
- bfs(graph, visitor);
- bfs.init();
- for(NodeIt it(graph); it != INVALID; ++it) {
- if(!bfs.reached(it)){
- bfs.addSource(it);
- while (!bfs.emptyQueue()) {
- bfs.processNextNode();
- if (!bipartite) return false;
- }
- }
- }
- return true;
- }
-
- /// \ingroup graph_prop
- ///
- /// \brief Check if the given undirected graph is bipartite or not
- ///
- /// The function checks if the given undirected graph is bipartite
- /// or not. The \ref Bfs algorithm is used to calculate the result.
- /// During the execution, the \c partMap will be set as the two
- /// partitions of the graph.
- /// \param graph The undirected graph.
- /// \retval partMap A writable bool map of nodes. It will be set as the
- /// two partitions of the graph.
- /// \return %True if \c graph is bipartite, %false otherwise.
- ///
- /// \author Balazs Attila Mihaly
- ///
- /// \image html bipartite_partitions.png
- /// \image latex bipartite_partitions.eps "Bipartite partititions" width=\textwidth
- template<typename UGraph, typename NodeMap>
- inline bool bipartitePartitions(const UGraph &graph, NodeMap &partMap){
- using namespace _topology_bits;
-
- checkConcept<concepts::UGraph, UGraph>();
-
- typedef typename UGraph::Node Node;
- typedef typename UGraph::NodeIt NodeIt;
- typedef typename UGraph::EdgeIt EdgeIt;
-
- bool bipartite = true;
-
- BipartitePartitionsVisitor<UGraph, NodeMap>
- visitor(graph, partMap, bipartite);
- BfsVisit<UGraph, BipartitePartitionsVisitor<UGraph, NodeMap> >
- bfs(graph, visitor);
- bfs.init();
- for(NodeIt it(graph); it != INVALID; ++it) {
- if(!bfs.reached(it)){
- bfs.addSource(it);
- while (!bfs.emptyQueue()) {
- bfs.processNextNode();
- if (!bipartite) return false;
- }
- }
- }
- return true;
- }
-
- /// \brief Returns true when there is not loop edge in the graph.
- ///
- /// Returns true when there is not loop edge in the graph.
- template <typename Graph>
- bool loopFree(const Graph& graph) {
- for (typename Graph::EdgeIt it(graph); it != INVALID; ++it) {
- if (graph.source(it) == graph.target(it)) return false;
- }
- return true;
- }
-
- /// \brief Returns true when there is not parallel edges in the graph.
- ///
- /// Returns true when there is not parallel edges in the graph.
- template <typename Graph>
- bool parallelFree(const Graph& graph) {
- typename Graph::template NodeMap<bool> reached(graph, false);
- for (typename Graph::NodeIt n(graph); n != INVALID; ++n) {
- for (typename Graph::OutEdgeIt e(graph, n); e != INVALID; ++e) {
- if (reached[graph.target(e)]) return false;
- reached.set(graph.target(e), true);
- }
- for (typename Graph::OutEdgeIt e(graph, n); e != INVALID; ++e) {
- reached.set(graph.target(e), false);
- }
- }
- return true;
- }
-
- /// \brief Returns true when there is not loop edge and parallel
- /// edges in the graph.
- ///
- /// Returns true when there is not loop edge and parallel edges in
- /// the graph.
- template <typename Graph>
- bool simpleGraph(const Graph& graph) {
- typename Graph::template NodeMap<bool> reached(graph, false);
- for (typename Graph::NodeIt n(graph); n != INVALID; ++n) {
- reached.set(n, true);
- for (typename Graph::OutEdgeIt e(graph, n); e != INVALID; ++e) {
- if (reached[graph.target(e)]) return false;
- reached.set(graph.target(e), true);
- }
- for (typename Graph::OutEdgeIt e(graph, n); e != INVALID; ++e) {
- reached.set(graph.target(e), false);
- }
- reached.set(n, false);
- }
- return true;
- }
-
-} //namespace lemon
-
-#endif //LEMON_TOPOLOGY_H
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/debian-med/cufflinks.git
More information about the debian-med-commit
mailing list