Graphle 1.0.0
A C++20 header-only graph library
Loading...
Searching...
No Matches
edge_utils.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <common.hpp>
4#include <graph/graph.hpp>
5#include <meta/concepts.hpp>
7
8#include <algorithm>
9
10
11namespace graphle::util {
13 template <graph_ref G, auto Field> struct edge_field_filter {
15
16 constexpr bool operator()(edge_of<G> edge) const {
17 return vertex_compare_of<G>{}(edge.*Field, vertex);
18 }
19 };
20
25
26
38 template <graph_ref G> requires (
42 ) constexpr inline auto in_edges(G&& graph, vertex_of<G> vertex) {
43 using GL = std::remove_cvref_t<G>;
44 using edge = edge_of<G>;
45
46 if constexpr (GL::has_in_edges) {
48 }
49
50 else if constexpr (GL::has_out_edges && !GL::is_directed) {
51 return graph.get_out_edges(vertex) | views::transform(util::transpose_edge);
52 }
53
54 else if constexpr (GL::has_edge_list) {
55 return graph.get_edges() | views::filter(edge_to_filter<G> { vertex });
56 }
57 }
58
59
71 template <graph_ref G> requires (
75 ) constexpr inline auto out_edges(G&& graph, vertex_of<G> vertex) {
76 using GL = std::remove_cvref_t<G>;
77 using edge = edge_of<G>;
78
79 if constexpr (GL::has_out_edges) {
81 }
82
83 else if constexpr (GL::has_in_edges && !GL::is_directed) {
84 return graph.get_in_edges(vertex) | views::transform(util::transpose_edge);
85 }
86
87 else if constexpr (GL::has_edge_list) {
88 return graph.get_edges() | views::filter(edge_from_filter<G> { vertex });
89 }
90 }
91
92
104 template <graph_ref G> requires (
108 ) constexpr inline std::size_t in_degree(G&& graph, vertex_of<G> vertex) {
109 using GL = std::remove_cvref_t<G>;
110 using edge = edge_of<G>;
111
112 if constexpr (GL::has_in_edges) {
113 return rng::size(graph.get_in_edges(vertex));
114 }
115
116 else if constexpr (GL::has_out_edges && !GL::is_directed) {
117 return rng::size(graph.get_out_edges(vertex));
118 }
119
120 else if constexpr (GL::has_edge_list) {
121 return rng::distance(graph.get_edges() | views::filter(edge_to_filter<G> { vertex }));
122 }
123 }
124
125
137 template <graph_ref G> requires (
141 ) constexpr inline std::size_t out_degree(G&& graph, vertex_of<G> vertex) {
142 using GL = std::remove_cvref_t<G>;
143 using edge = edge_of<G>;
144
145 if constexpr (GL::has_out_edges) {
146 return rng::size(graph.get_out_edges(vertex));
147 }
148
149 else if constexpr (GL::has_in_edges && !GL::is_directed) {
150 return rng::size(graph.get_in_edges(vertex));
151 }
152
153 else if constexpr (GL::has_edge_list) {
154 return rng::distance(graph.get_edges() | views::filter(edge_from_filter<G> { vertex }));
155 }
156 }
157
158
170 template <directed_graph G> requires (edge_list_graph<G> || in_edges_graph<G>)
171 constexpr inline bool is_root(G&& graph, vertex_of<G> vertex) {
172 return in_degree(graph, vertex) == 0;
173 }
174
175
187 template <directed_graph G> requires (edge_list_graph<G> || out_edges_graph<G>)
188 constexpr inline bool is_branch(G&& graph, vertex_of<G> vertex) {
189 return out_degree(graph, vertex) > 0;
190 }
191
192
204 template <directed_graph G> requires (edge_list_graph<G> || out_edges_graph<G>)
205 constexpr inline bool is_leaf(G&& graph, vertex_of<G> vertex) {
206 return out_degree(graph, vertex) == 0;
207 }
208}
typename std::remove_cvref_t< G >::edge_type edge_of
Definition: graph.hpp:93
typename std::remove_cvref_t< G >::vertex_compare_t vertex_compare_of
Definition: graph.hpp:95
typename std::remove_cvref_t< G >::vertex_type vertex_of
Definition: graph.hpp:91
constexpr bool is_leaf(G &&graph, vertex_of< G > vertex)
Definition: edge_utils.hpp:205
constexpr bool is_root(G &&graph, vertex_of< G > vertex)
Definition: edge_utils.hpp:171
constexpr auto in_edges(G &&graph, vertex_of< G > vertex)
Definition: edge_utils.hpp:42
constexpr auto out_edges(G &&graph, vertex_of< G > vertex)
Definition: edge_utils.hpp:75
constexpr std::size_t in_degree(G &&graph, vertex_of< G > vertex)
Definition: edge_utils.hpp:108
constexpr bool is_branch(G &&graph, vertex_of< G > vertex)
Definition: edge_utils.hpp:188
constexpr std::size_t out_degree(G &&graph, vertex_of< G > vertex)
Definition: edge_utils.hpp:141
constexpr transpose_edge_t transpose_edge
Definition: functional.hpp:87
GetOutEdges get_out_edges
Definition: graph.hpp:80
GetEdges get_edges
Definition: graph.hpp:79
GetInEdges get_in_edges
Definition: graph.hpp:81
constexpr bool operator()(edge_of< G > edge) const
Definition: edge_utils.hpp:16