Graphle 1.0.0
A C++20 header-only graph library
Loading...
Searching...
No Matches
vertex_compare.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <common.hpp>
4#include <meta/concepts.hpp>
5
6
7namespace graphle {
8 namespace detail {
9 template <typename V> using edge_for = std::pair<V*, V*>;
10 }
11
12
14 namespace comparators {
16 template <typename Vertex> struct vertex_address {
17 constexpr bool operator()(const Vertex* a, const Vertex* b) const {
18 return a == b;
19 }
20 };
21
23 template <typename Vertex> struct vertex_value {
24 constexpr bool operator()(const Vertex* a, const Vertex* b) const {
25 return (*a) == (*b);
26 }
27 };
28
30 template <typename Vertex, typename VertexCmp> struct edge_as_vertex {
31 VertexCmp vertex_comparator = {};
32
33 using edge = std::pair<Vertex*, Vertex*>;
34 using const_edge = std::pair<const Vertex*, const Vertex*>;
35
36 constexpr bool operator()(const edge& a, const edge& b) const {
37 return vertex_comparator(a.first, b.first) && vertex_comparator(a.second, b.second);
38 }
39
40 constexpr bool operator()(const const_edge& a, const const_edge& b) const {
41 return vertex_comparator(a.first, b.first) && vertex_comparator(a.second, b.second);
42 }
43 };
44 }
45
46
48 namespace hashers {
49 constexpr inline std::size_t hash_combine(std::size_t a, std::size_t b) {
50 // Shamelessly stolen from boost::hash_combine
51 // https://www.boost.org/doc/libs/1_55_0/doc/html/hash/reference.html#boost.hash_combine
52 return a ^ (b + 0x9E3779B9 + (a << 6) + (a >> 2));
53 }
54
55
57 template <typename Vertex> struct vertex_address {
58 constexpr bool operator()(const Vertex* a) const {
59 return std::hash<Vertex*>{}(a);
60 }
61 };
62
64 template <typename Vertex> struct vertex_value {
65 constexpr bool operator()(const Vertex* a) const {
66 return std::hash<Vertex>{}(*a);
67 }
68 };
69
71 template <typename Vertex, typename VertexHash> struct edge_as_vertex {
72 VertexHash vertex_hasher = {};
73
74 using edge = std::pair<Vertex*, Vertex*>;
75 using const_edge = std::pair<const Vertex*, const Vertex*>;
76
77 constexpr bool operator()(const edge& a) const {
78 return hash_combine(vertex_hasher(a.first), vertex_hasher(a.second));
79 }
80
81 constexpr bool operator()(const const_edge& a) const {
82 return hash_combine(vertex_hasher(a.first), vertex_hasher(a.second));
83 }
84 };
85 }
86
87
89 template <typename T, typename Vertex, typename Edge = detail::edge_for<Vertex>> concept graph_compare_traits = requires {
94 };
95
96
98 template <typename Vertex> struct compare_by_address {
103 };
104
105
107 template <typename Vertex> struct compare_by_value {
112 };
113}
std::pair< V *, V * > edge_for
constexpr std::size_t hash_combine(std::size_t a, std::size_t b)
constexpr bool operator()(const edge &a, const edge &b) const
constexpr bool operator()(const const_edge &a, const const_edge &b) const
std::pair< const Vertex *, const Vertex * > const_edge
std::pair< Vertex *, Vertex * > edge
constexpr bool operator()(const Vertex *a, const Vertex *b) const
constexpr bool operator()(const Vertex *a, const Vertex *b) const
constexpr bool operator()(const edge &a) const
std::pair< const Vertex *, const Vertex * > const_edge
std::pair< Vertex *, Vertex * > edge
constexpr bool operator()(const const_edge &a) const
constexpr bool operator()(const Vertex *a) const
constexpr bool operator()(const Vertex *a) const