lemon/concept/path.h
changeset 2259 da142c310d02
parent 1993 2115143eceea
equal deleted inserted replaced
5:661c6becfc0d 6:4877deb78376
    35 
    35 
    36 
    36 
    37     //! \brief A skeleton structure for representing directed paths in a graph.
    37     //! \brief A skeleton structure for representing directed paths in a graph.
    38     //!
    38     //!
    39     //! A skeleton structure for representing directed paths in a graph.
    39     //! A skeleton structure for representing directed paths in a graph.
    40     //! \param GR The graph type in which the path is.
    40     //! \param _Graph The graph type in which the path is.
    41     //!
    41     //!
    42     //! In a sense, the path can be treated as a graph, for it has \c NodeIt
    42     //! In a sense, the path can be treated as a graph, for it has \c NodeIt
    43     //! and \c EdgeIt with the same usage. These types converts to the \c Node
    43     //! and \c EdgeIt with the same usage. These types converts to the \c Node
    44     //! and \c Edge of the original graph.
    44     //! and \c Edge of the original graph.
    45     template<typename GR>
    45     template<typename _Graph>
    46     class Path {
    46     class Path {
    47     public:
    47     public:
    48 
    48 
    49       /// Type of the underlying graph.
    49       /// Type of the underlying graph.
    50       typedef /*typename*/ GR Graph;
    50       typedef _Graph Graph;
    51       /// Edge type of the underlying graph.
    51       /// Edge type of the underlying graph.
    52       typedef typename Graph::Edge GraphEdge;
    52       typedef typename Graph::Edge Edge;
    53       /// Node type of the underlying graph.
    53       /// Node type of the underlying graph.
    54      typedef typename Graph::Node GraphNode;
    54       typedef typename Graph::Node Node;
       
    55 
    55       class NodeIt;
    56       class NodeIt;
    56       class EdgeIt;
    57       class EdgeIt;
    57 
    58 
    58       /// \param _g The graph in which the path is.
    59       /// \param _g The graph in which the path is.
    59       ///
    60       ///
    60       Path(const Graph &_g) {
    61       Path(const Graph &_g) {
    61 	ignore_unused_variable_warning(_g);
    62 	ignore_unused_variable_warning(_g);
    62       }
    63       }
    63 
    64 
    64       /// Length of the path.
    65       /// Length of the path ie. the number of edges in the path.
    65       int length() const {return 0;}
    66       int length() const {return 0;}
       
    67 
    66       /// Returns whether the path is empty.
    68       /// Returns whether the path is empty.
    67       bool empty() const { return true;}
    69       bool empty() const { return true;}
    68 
    70 
    69       /// Resets the path to an empty path.
    71       /// Resets the path to an empty path.
    70       void clear() {}
    72       void clear() {}
    71 
    73 
    72       /// \brief Starting point of the path.
    74       /// \brief Starting point of the path.
    73       ///
    75       ///
    74       /// Starting point of the path.
    76       /// Starting point of the path.
    75       /// Returns INVALID if the path is empty.
    77       /// Returns INVALID if the path is empty.
    76       GraphNode/*It*/ target() const {return INVALID;}
    78       Node target() const {return INVALID;}
    77       /// \brief End point of the path.
    79       /// \brief End point of the path.
    78       ///
    80       ///
    79       /// End point of the path.
    81       /// End point of the path.
    80       /// Returns INVALID if the path is empty.
    82       /// Returns INVALID if the path is empty.
    81       GraphNode/*It*/ source() const {return INVALID;}
    83       Node source() const {return INVALID;}
    82 
       
    83       /// \brief First NodeIt/EdgeIt.
       
    84       ///
       
    85       /// Initializes node or edge iterator to point to the first
       
    86       /// node or edge.
       
    87       template<typename It>
       
    88       It& first(It &i) const { return i=It(*this); }
       
    89 
    84 
    90       /// \brief The target of an edge.
    85       /// \brief The target of an edge.
    91       ///
    86       ///
    92       /// Returns node iterator pointing to the target node of the
    87       /// Returns node iterator pointing to the target node of the
    93       /// given edge iterator.
    88       /// given edge iterator.
    97       ///
    92       ///
    98       /// Returns node iterator pointing to the source node of the
    93       /// Returns node iterator pointing to the source node of the
    99       /// given edge iterator.
    94       /// given edge iterator.
   100       NodeIt source(const EdgeIt&) const {return INVALID;}
    95       NodeIt source(const EdgeIt&) const {return INVALID;}
   101 
    96 
   102 
    97       /// \brief Iterator class to iterate on the nodes of the paths
   103       /* Iterator classes */
    98       ///
   104 
    99       /// This class is used to iterate on the nodes of the paths
   105       /**
   100       ///
   106        * \brief Iterator class to iterate on the edges of the paths
   101       /// Of course it converts to Graph::Node.
   107        *
   102       class NodeIt {
   108        * This class is used to iterate on the edges of the paths
   103       public:
   109        *
   104 	/// Default constructor
   110        * Of course it converts to Graph::Edge
   105 	NodeIt() {}
   111        *
   106 	/// Invalid constructor
   112        */
   107 	NodeIt(Invalid) {}
       
   108 	/// Constructor with starting point
       
   109 	NodeIt(const Path &) {}
       
   110 
       
   111 	///Conversion to Graph::Node
       
   112 	operator Node() const { return INVALID; }
       
   113 	/// Next node
       
   114 	NodeIt& operator++() {return *this;}
       
   115 
       
   116 	/// Comparison operator
       
   117 	bool operator==(const NodeIt&) const {return true;}
       
   118 	/// Comparison operator
       
   119 	bool operator!=(const NodeIt&) const {return true;}
       
   120  	/// Comparison operator
       
   121  	bool operator<(const NodeIt&) const {return false;}
       
   122 
       
   123       };
       
   124 
       
   125       /// \brief Iterator class to iterate on the edges of the paths
       
   126       ///
       
   127       /// This class is used to iterate on the edges of the paths
       
   128       ///
       
   129       /// Of course it converts to Graph::Edge
   113       class EdgeIt {
   130       class EdgeIt {
   114       public:
   131       public:
   115 	/// Default constructor
   132 	/// Default constructor
   116 	EdgeIt() {}
   133 	EdgeIt() {}
   117 	/// Invalid constructor
   134 	/// Invalid constructor
   118 	EdgeIt(Invalid) {}
   135 	EdgeIt(Invalid) {}
   119 	/// Constructor with starting point
   136 	/// Constructor with starting point
   120 	EdgeIt(const Path &) {}
   137 	EdgeIt(const Path &) {}
   121 
   138 
   122 	operator GraphEdge () const {}
   139 	operator Edge() const { return INVALID; }
   123 
   140 
   124 	/// Next edge
   141 	/// Next edge
   125 	EdgeIt& operator++() {return *this;}
   142 	EdgeIt& operator++() {return *this;}
   126 
   143 
   127 	/// Comparison operator
   144 	/// Comparison operator
   128 	bool operator==(const EdgeIt&) const {return true;}
   145 	bool operator==(const EdgeIt&) const {return true;}
   129 	/// Comparison operator
   146 	/// Comparison operator
   130 	bool operator!=(const EdgeIt&) const {return true;}
   147 	bool operator!=(const EdgeIt&) const {return true;}
   131 // 	/// Comparison operator
   148  	/// Comparison operator
   132 //      /// \todo It is not clear what is the "natural" ordering.
   149  	bool operator<(const EdgeIt&) const {return false;}
   133 // 	bool operator<(const EdgeIt& e) const {}
   150 
   134 
   151       };
   135       };
   152 
   136 
       
   137       /**
       
   138        * \brief Iterator class to iterate on the nodes of the paths
       
   139        *
       
   140        * This class is used to iterate on the nodes of the paths
       
   141        *
       
   142        * Of course it converts to Graph::Node.
       
   143        *
       
   144        */
       
   145       class NodeIt {
       
   146       public:
       
   147 	/// Default constructor
       
   148 	NodeIt() {}
       
   149 	/// Invalid constructor
       
   150 	NodeIt(Invalid) {}
       
   151 	/// Constructor with starting point
       
   152 	NodeIt(const Path &) {}
       
   153 
       
   154 	///Conversion to Graph::Node
       
   155 	operator const GraphNode& () const {}
       
   156 	/// Next node
       
   157 	NodeIt& operator++() {return *this;}
       
   158 
       
   159 	/// Comparison operator
       
   160 	bool operator==(const NodeIt&) const {return true;}
       
   161 	/// Comparison operator
       
   162 	bool operator!=(const NodeIt&) const {return true;}
       
   163 // 	/// Comparison operator
       
   164 //      /// \todo It is not clear what is the "natural" ordering.
       
   165 // 	bool operator<(const NodeIt& e) const {}
       
   166 
       
   167       };
       
   168 
   153 
   169       friend class Builder;
   154       friend class Builder;
   170 
   155 
   171       /**
   156       /// \brief Class to build paths
   172        * \brief Class to build paths
   157       ///
   173        *
   158       /// This class is used to fill a path with edges.
   174        * This class is used to fill a path with edges.
   159       ///
   175        *
   160       /// You can push new edges to the front and to the back of the path in
   176        * You can push new edges to the front and to the back of the path in
   161       /// arbitrary order then you should commit these changes to the graph.
   177        * arbitrary order then you should commit these changes to the graph.
   162       ///
   178        *
   163       /// While the builder is active (after the first modifying
   179        * While the builder is active (after the first modifying
   164       /// operation and until the call of \ref commit()) the
   180        * operation and until the call of \ref commit()) the
   165       /// underlining Path is in a "transitional" state (operations on
   181        * underlining Path is in a "transitional" state (operations on
   166       /// it have undefined result).
   182        * it have undefined result).
       
   183        */
       
   184       class Builder {
   167       class Builder {
   185       public:
   168       public:
   186 
   169 
   187         Path &P;
   170         /// Constructor
   188 
   171 
   189 	///\param _p the path you want to fill in.
   172         /// Constructor
       
   173 	/// \param _path the path you want to fill in.
   190 	///
   174 	///
   191 
   175 
   192 	Builder(Path &_p) : P(_p) {}
   176 	Builder(Path &_path) { ignore_unused_variable_warning(_path); }
   193 
   177 
   194 	/// Sets the starting node of the path.
   178 	/// Sets the starting node of the path.
   195 
   179 
   196 	/// Sets the starting node of the path. Edge added to the path
   180 	/// Sets the starting node of the path. Edge added to the path
   197 	/// afterwards have to be incident to this node.
   181 	/// afterwards have to be incident to this node.
   198 	/// You \em must start building an empty path with these functions.
   182 	/// You \em must start building an empty path with these functions.
   199 	/// (And you \em must \em not use it later).
   183 	/// (And you \em must \em not use it later).
   200 	/// \sa pushFront()
   184 	/// \sa pushFront()
   201 	/// \sa pushBack()
   185 	/// \sa pushBack()
   202 	void setStartNode(const GraphNode &) {}
   186 	void setStartNode(const Node &) {}
   203 
   187 
   204 	///Push a new edge to the front of the path
   188 	///Push a new edge to the front of the path
   205 
   189 
   206 	///Push a new edge to the front of the path.
   190 	///Push a new edge to the front of the path.
   207 	///If the path is empty, you \em must call \ref setStartNode() before
   191 	///If the path is empty, you \em must call \ref setStartNode() before
   208 	///the first use of \ref pushFront().
   192 	///the first use of \ref pushFront().
   209 	void pushFront(const GraphEdge&) {}
   193 	void pushFront(const Edge&) {}
   210 
   194 
   211 	///Push a new edge to the back of the path
   195 	///Push a new edge to the back of the path
   212 
   196 
   213 	///Push a new edge to the back of the path.
   197 	///Push a new edge to the back of the path.
   214 	///If the path is empty, you \em must call \ref setStartNode() before
   198 	///If the path is empty, you \em must call \ref setStartNode() before
   215 	///the first use of \ref pushBack().
   199 	///the first use of \ref pushBack().
   216 	void pushBack(const GraphEdge&) {}
   200 	void pushBack(const Edge&) {}
   217 
   201 
   218 	///Commit the changes to the path.
   202 	///Commit the changes to the path.
       
   203 
       
   204 	///Commit the changes to the path.
       
   205         ///
   219 	void commit() {}
   206 	void commit() {}
   220 
   207 
   221 	///Reserve (front) storage for the builder in advance.
   208 	///Reserve (front) storage for the builder in advance.
   222 
   209 
   223 	///If you know a reasonable upper bound on the number of the edges
   210 	///If you know a reasonable upper bound on the number of the edges
   229 	///If you know a reasonable upper bound on the number of the edges
   216 	///If you know a reasonable upper bound on the number of the edges
   230 	///to add to the back of the path,
   217 	///to add to the back of the path,
   231 	///using this function you may speed up the building.
   218 	///using this function you may speed up the building.
   232 	void reserveBack(size_t) {}
   219 	void reserveBack(size_t) {}
   233       };
   220       };
       
   221 
       
   222       template <typename _Path>
       
   223       struct Constraints {
       
   224 	void constraints() {
       
   225           typedef typename _Path::Node Node;
       
   226           typedef typename _Path::NodeIt NodeIt;
       
   227           typedef typename Graph::Node GraphNode;
       
   228 
       
   229           typedef typename _Path::Edge Edge;
       
   230           typedef typename _Path::EdgeIt EdgeIt;
       
   231           typedef typename Graph::Edge GraphEdge;
       
   232 
       
   233           typedef typename _Path::Builder Builder;
       
   234 
       
   235           path = _Path(graph);
       
   236 
       
   237           bool b = cpath.empty();
       
   238           int l = cpath.length();
       
   239 
       
   240           Node gn;
       
   241           Edge ge;
       
   242           gn = cpath.source();
       
   243           gn = cpath.target();
       
   244 
       
   245           NodeIt nit;
       
   246           EdgeIt eit(INVALID);
       
   247           nit = path.source(eit);
       
   248           nit = path.target(eit);
       
   249           
       
   250           nit = NodeIt();
       
   251           nit = NodeIt(cpath);
       
   252           nit = INVALID;
       
   253           gn = nit;
       
   254           ++nit;
       
   255           b = nit == nit;
       
   256           b = nit != nit;
       
   257           b = nit < nit;
       
   258 
       
   259           eit = EdgeIt();
       
   260           eit = EdgeIt(cpath);
       
   261           eit = INVALID;
       
   262           ge = eit;
       
   263           ++eit;
       
   264           b = eit == eit;
       
   265           b = eit != eit;
       
   266           b = eit < eit;
       
   267 
       
   268           size_t st = 0;
       
   269 
       
   270           Builder builder(path); 
       
   271           builder.setStartNode(gn);
       
   272           builder.pushFront(ge);
       
   273           builder.pushBack(ge);
       
   274           builder.commit();
       
   275           builder.reserveFront(st);
       
   276           builder.reserveBack(st);
       
   277           
       
   278           ignore_unused_variable_warning(l);
       
   279           ignore_unused_variable_warning(b);
       
   280 	}
       
   281 
       
   282         const Graph& graph;
       
   283         const _Path& cpath;
       
   284         _Path& path;
       
   285       };
       
   286 
   234     };
   287     };
   235 
   288 
   236   ///@}
   289   ///@}
   237   }
   290   }
   238 
   291