Skip to content

Commit 46964bc

Browse files
authored
Merge pull request #148 from akrzemi1/example
Example on how to adapt your own graph structure
2 parents 3f90f9e + 6ce3142 commit 46964bc

File tree

3 files changed

+102
-0
lines changed

3 files changed

+102
-0
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# example/AdaptingThirdPartyGraph/CMakeLists.txt
2+
3+
set(EXAMPLE_OUTPUT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/output/")
4+
add_compile_definitions(EXAMPLE_OUTPUT_DIR="${EXAMPLE_OUTPUT_DIR}")
5+
6+
#add_library(catch_main STATIC catch_main.cpp)
7+
#target_link_libraries(catch_main PUBLIC Catch2::Catch2)
8+
#target_link_libraries(catch_main PRIVATE project_options)
9+
#target_link_options(catch_main INTERFACE $<$<CXX_COMPILER_ID:GNU>:-pthread -fconcepts-diagnostics-depth=1>)
10+
11+
add_executable(AdaptingThirdPartyGraph "adapting_a_third_party_graph.cpp")
12+
target_link_libraries(AdaptingThirdPartyGraph PRIVATE project_warnings project_options catch_main Catch2::Catch2WithMain graph)
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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+
}

example/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22

33
add_subdirectory(CppCon2021)
44
add_subdirectory(CppCon2022)
5+
add_subdirectory(AdaptingThirdPartyGraph)

0 commit comments

Comments
 (0)