1+ // Copyright (C) 2025 Andrzej Krzemienski.
2+ //
3+ // Use, modification, and distribution is subject to the Boost Software
4+ // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
5+ // http://www.boost.org/LICENSE_1_0.txt)
6+ //
7+ // This test file demonstrates how one can adapt one's own graph container for use with
8+ // this Graph Library
9+
10+ #include < graph/views/depth_first_search.hpp>
11+ #include < graph/graph.hpp>
12+ #include < string>
13+ #include < vector>
14+ #include < iostream>
15+
16+
17+ namespace MyLibrary
18+ { // custom graph container, conceptually an adjacency list
19+
20+ struct MyEdge
21+ {
22+ std::string content;
23+ int indexOfTarget;
24+ };
25+
26+ struct MyVertex
27+ {
28+ std::string content;
29+ std::vector<MyEdge> outEdges;
30+ };
31+
32+ class MyGraph
33+ {
34+ std::vector<MyVertex> _vertices;
35+ public:
36+ MyVertex const * getVertexByIndex (int index) const
37+ { return &_vertices[static_cast <unsigned >(index)]; }
38+
39+ std::vector<MyVertex> const & getAllVertices () const // !! one of customization points
40+ { return _vertices; } // forced me to add this fun
41+
42+ void setTopology (std::vector<MyVertex> t) { _vertices = std::move (t); }
43+ };
44+
45+ }
46+
47+ namespace MyLibrary
48+ { // customization for graph, unintrusive
49+ // although forcing me to provide `vertices()` is superfluous
50+
51+ auto vertices (MyGraph const & g)
52+ { return std::views::all (g.getAllVertices ()); } // for vertex_range_t
53+
54+ auto edges (MyGraph const &, const MyLibrary::MyVertex& v)
55+ { return std::views::all (v.outEdges ); }
56+
57+ auto edges (MyGraph const & g, int i)
58+ { return edges (g, *g.getVertexByIndex (i)); }
59+
60+ int vertex_id (MyGraph const & g, std::vector<MyVertex>::const_iterator it)
61+ { return static_cast <int >(std::distance (g.getAllVertices ().begin (), it)); }
62+
63+ int target_id (MyGraph const &, MyEdge const & uv)
64+ { return uv.indexOfTarget ; }
65+
66+ }
67+
68+
69+ int main ()
70+ {
71+ static_assert (graph::adjacency_list<MyLibrary::MyGraph>);
72+
73+ const MyLibrary::MyGraph g = [] { // populate the graph
74+ MyLibrary::MyGraph r;
75+ std::vector<MyLibrary::MyVertex> topo { // A |
76+ /* 0*/ {" A" , {{" " , 1 }, {" " , 2 }}}, // / \ |
77+ /* 1*/ {" B" , {{" " , 3 }}}, // B C |
78+ /* 2*/ {" C" , {{" " , 3 }}}, // \ / |
79+ /* 3*/ {" D" , {}} // D |
80+ };
81+ r.setTopology (std::move (topo));
82+ return r;
83+ }();
84+
85+ for (auto const & [vid, v] : graph::views::vertices_depth_first_search (g, 0 ))
86+ std::cout << v.content << " " ;
87+
88+ std::cout << std::endl;
89+ }
0 commit comments