GraphLib
Bearbeitung der Aufgabe Mini Graph Library für OOP WiSe 2023/24
Loading...
Searching...
No Matches
concepts.h
Go to the documentation of this file.
1#ifndef GRAPHLIB_INTERNAL_CONCEPTS_H
2#define GRAPHLIB_INTERNAL_CONCEPTS_H
3
4#include <concepts>
5#include <iostream>
6#include <iterator>
7#include <type_traits>
8
17
18namespace GraphLib::Internal {
19
20//
21// ID, data and weight type constraints
22//
23
24// first, a helper concept:
25
31template <typename T>
32concept is_printable = requires(T a, std::ostream& os) {
33 { os << a } -> std::same_as<std::ostream&>;
34};
35
36// with that, the fundamental types we use in our graphs:
37
45template <typename T>
46concept valid_data_type = true; // TODO: any restrictions needed here?
47
60template <typename T>
61concept valid_id_type = std::totally_ordered<T> && std::is_default_constructible_v<T> && is_printable<T> &&
62 std::is_unsigned_v<T> && std::incrementable<T>;
63
75template <typename T>
76concept valid_weight_type = std::totally_ordered<T> && std::is_default_constructible_v<T> && is_printable<T> &&
77 std::numeric_limits<T>::has_infinity == true;
78
79//
80// Graph type constraints
81//
82
83// forward-declare the two edge types with their constraints:
84
85template <valid_id_type I>
86struct Edge;
87
88template <valid_id_type I, valid_weight_type W>
89struct WeightedEdge;
90
97template <typename E, typename I>
98concept valid_edge_type = std::is_aggregate_v<E> && requires { typename E::id_type; } && std::derived_from<E, Edge<I>>;
99
106template <typename E, typename I>
107concept valid_weighted_edge_type = std::is_aggregate_v<E> && requires {
108 typename E::id_type;
109 typename E::weight_type;
110} && std::derived_from<E, WeightedEdge<I, typename E::weight_type>>;
111
112// forward-declare the five abstract graph types with their type constraints:
113
114template <valid_data_type D, valid_id_type I, valid_edge_type<I> E>
115class Graph;
116
117template <valid_data_type D, valid_id_type I, valid_edge_type<I> E>
118class DirectedGraph;
119
120template <valid_data_type D, valid_id_type I, valid_edge_type<I> E>
121class UndirectedGraph;
122
123template <valid_data_type D, valid_id_type I, valid_weighted_edge_type<I> E>
124class WeightedGraph;
125
126template <valid_data_type D, valid_id_type I, valid_edge_type<I> E>
127class UnweightedGraph;
128
134template <typename G>
135concept has_graph_types = requires {
136 typename G::data_type;
137 typename G::id_type;
138 typename G::edge_type;
139};
140
146template <typename G>
149 std::derived_from<G, Graph<typename G::data_type, typename G::id_type, typename G::edge_type>>;
150
151// finally, the concepts to constrain a type G to descend from the first level of our inheritance:
152
158template <typename G>
161 std::derived_from<G, DirectedGraph<typename G::data_type, typename G::id_type, typename G::edge_type>>;
162
168template <typename G>
171 std::derived_from<G, UndirectedGraph<typename G::data_type, typename G::id_type, typename G::edge_type>>;
172
178template <typename G>
181 std::derived_from<G, WeightedGraph<typename G::data_type, typename G::id_type, typename G::edge_type>>;
182
188template <typename G>
191 std::derived_from<G, UnweightedGraph<typename G::data_type, typename G::id_type, typename G::edge_type>>;
192
196template <typename G>
198
202template <typename G>
204
208template <typename G>
210
214template <typename G>
216
217} // namespace GraphLib::Internal
218
219#endif
A graph class representing directed graphs.
Abstract base class for a generic graph structure.
A graph class representing undirected graphs.
A graph class representing unweighted graphs.
A graph class representing weighted graphs.
Checks if a type provides the core type aliases (data_type, id_type, edge_type).
Definition concepts.h:135
Checks whether a type supports streaming to std::ostream.
Definition concepts.h:32
Placeholder for user-defined data types used in vertices or edges.
Definition concepts.h:46
Checks if a type derives from DirectedGraph.
Definition concepts.h:159
Checks whether a type is an aggregate and derived from Edge<I>.
Definition concepts.h:98
Checks if a type derives from Graph<D, I, E>.
Definition concepts.h:147
Concept for a valid ID type in graph components.
Definition concepts.h:61
Checks if a type derives from UndirectedGraph.
Definition concepts.h:169
Graph type that is both unweighted and directed.
Definition concepts.h:197
Checks if a type derives from UnweightedGraph.
Definition concepts.h:189
Graph type that is both unweighted and undirected.
Definition concepts.h:203
Concept for a valid edge weight type.
Definition concepts.h:76
Graph type that is both weighted and directed.
Definition concepts.h:209
Checks whether a type is an aggregate and derived from WeightedEdge<I, W>.
Definition concepts.h:107
Checks if a type derives from WeightedGraph.
Definition concepts.h:179
Graph type that is both weighted and undirected.
Definition concepts.h:215
Represents an unweighted edge to a node identified by an ID.
Definition edge.h:26
Represents a weighted edge to a node identified by an ID, with an associated weight.
Definition edge.h:97