3#include "elementa/license.inc"
4#include "elementa/checks.inc"
6#ifndef ELEMENTA_ADTS_GRAPH_PATHS_H
7#define ELEMENTA_ADTS_GRAPH_PATHS_H
61template <GPathKind KIND>
66{
return(kid == GPathKind::kVertexPath ? GPathKind::kEdgePath :
67 GPathKind::kVertexPath); }
70template <
class Vertex,
class Edge, GPathKind KIND>
72 KIND == GPathKind::kVertexPath,
77template <
class Vertex,
class Edge, GPathKind KIND>
78class GPath:
public std::vector< GPathElement<Vertex,Edge,KIND> >
86 static const GPathKind
kKind = KIND;
107 using BasePath::BasePath;
134 const std::function<std::string(
const Vertex & vd)> &
135 vtostr = [](
const Vertex & vd)->std::string
136 {
return(std::to_string(vd)); },
137 const std::function<std::string(
const Edge & ed)> &
138 etostr = [](
const Edge & ed)->std::string
139 {
return(std::to_string(ed)); })
const
140 {
return(to_string_hidden(g,vtostr,etostr,GPathKindType<KIND>{})); }
152 GPathKindType<GPathKind::kVertexPath>)
const;
160 GPathKindType<GPathKind::kEdgePath>)
const;
162 std::string to_string_hidden(
const BaseGraph & g,
163 const std::function<std::string(
const Vertex & vd)> &
165 const std::function<std::string(
const Edge & ed)> &
167 GPathKindType<GPathKind::kVertexPath>)
const;
169 std::string to_string_hidden(
const BaseGraph & g,
170 const std::function<std::string(
const Vertex & vd)> &
172 const std::function<std::string(
const Edge & ed)> &
174 GPathKindType<GPathKind::kEdgePath>)
const;
186template <
class Vertex,
class Edge,
class Weight =
double>
191template <
class Vertex,
class Edge>
198template <
class Vertex,
class Edge>
217template <
class Vertex,
class Edge, GPathKind KIND>
219 GPath<Vertex,Edge,KIND>::other_hidden(
221 const typename BaseGraph::EPred & p,
222 GPathKindType<GPathKind::kVertexPath>)
const
225 GPath<Vertex,Edge,GPathKind::kEdgePath> np;
228 typename BaseGraph::viterator prevvit;
229 for (
const auto & vit : *
this)
231 if (first) first =
false;
232 else np.push_back(g.connected(prevvit,vit,p));
238template <
class Vertex,
class Edge, GPathKind KIND>
240 GPath<Vertex,Edge,KIND>::other_hidden(
242 const typename BaseGraph::EPred & p,
243 GPathKindType<GPathKind::kEdgePath>)
const
246 GPath<Vertex,Edge,GPathKind::kVertexPath> np;
248 auto eit = this->begin();
249 typename BaseGraph::viterator vit0,vit1;
251 while (eit != this->end())
253 g.connection(eit,vit0,vit1);
266template <
class Vertex,
class Edge, GPathKind KIND>
267std::string GPath<Vertex,Edge,KIND>::to_string_hidden(
269 const std::function<std::string(
const Vertex & vd)> & vtostr,
270 const std::function<std::string(
const Edge & ed)> & etostr,
271 GPathKindType<GPathKind::kVertexPath>)
const
273 std::string res{
"VPATH with "};
274 res += std::to_string(this->size()) +
" vertices: < ";
275 for (
const auto & vit: *
this) res += vtostr(*vit) +
" ";
279template <
class Vertex,
class Edge, GPathKind KIND>
280std::string GPath<Vertex,Edge,KIND>::to_string_hidden(
282 const std::function<std::string(
const Vertex & vd)> & vtostr,
283 const std::function<std::string(
const Edge & ed)> & etostr,
284 GPathKindType<GPathKind::kEdgePath>)
const
286 std::string res{
"EPATH with "};
287 res += std::to_string(this->size()) +
" edges: < ";
288 typename BaseGraph::viterator vit0,vit1;
289 for (
const auto & eit: *
this)
291 g.connection(eit,vit0,vit1);
292 res += etostr(*eit) +
":(" + vtostr(*vit0) +
" -> " +
293 vtostr(*vit1) +
") ";
#define ELE_CLASS_ENUM(NameEnumClass,...)
Define an extended enum type.
GPathElement< Vertex, Edge, KIND > VectorElem
The type of the elements of the vector.
std::vector< VectorElem > BasePath
Base type of the path.
Graph< Vertex, Edge > BaseGraph
The graph class on which the path is defined.
std::vector< GPath > Collection
A collection of paths.
static const GPathKind kKind
The kind of the path.
std::string to_string(const BaseGraph &g, const std::function< std::string(const Vertex &vd)> &vtostr=[](const Vertex &vd) ->std::string { return(std::to_string(vd));}, const std::function< std::string(const Edge &ed)> &etostr=[](const Edge &ed) ->std::string { return(std::to_string(ed));}) const
Return a text version of the path.
GPath< Vertex, Edge, kOtherGPathKind(KIND) > other(const BaseGraph &g, const typename BaseGraph::EPred &epred=BaseGraph::trivial_epred) const
Construct a path of the other kind equivalent to this one.
A path in a graph whose vertices are of type Vertex and edges of type Edge.
std::function< Weight(const typename Graph< Vertex, Edge >::eiterator &) > EdgeWeightFunction
A family of functions that return the weight of an edge.
constexpr double value_edge_weight(const typename Graph< Vertex, Edge >::eiterator &eit) noexcept
A family of functions that return the edge value as the weight.
constexpr double trivial_edge_weight(const typename Graph< Vertex, Edge >::eiterator &) noexcept
A family of functions that return 1.0 as the weight of any edge.
typename std::conditional< KIND==GPathKind::kVertexPath, typename Graph< Vertex, Edge >::viterator, typename Graph< Vertex, Edge >::eiterator >::type GPathElement
Type of the elements of a path, according to its kind.
constexpr GPathKind kOtherGPathKind(GPathKind kid)
The kind opposite to a given one.
A type corresponding to each constant value of the kind of a path.
std::function< bool(const Edge &) > EPred
Predicate that indicates which edges to iterate on.
static constexpr bool trivial_epred(const Edge &) noexcept
A trivial predicate that always return true.
All graph classes derive from this one.
A mutable iterator that can have multiple iteration behaviors. Forward case.