GraphLib
Bearbeitung der Aufgabe Mini Graph Library für OOP WiSe 2023/24
Loading...
Searching...
No Matches
edge.h
Go to the documentation of this file.
1#ifndef GRAPHLIB_INTERNAL_EDGE_H
2#define GRAPHLIB_INTERNAL_EDGE_H
3
4#include <cstddef>
5#include <iostream>
6#include <ostream>
7
9
17
18namespace GraphLib::Internal {
19
25template <valid_id_type I>
26struct Edge {
27public:
28 using id_type = I;
29
31
40 bool operator<(const Edge& right) const
41 {
42 return node_id < right.node_id;
43 }
44
51 bool operator==(const Edge& other) const
52 {
53 return node_id == other.node_id;
54 }
55
65 template <typename D>
67 void print_with_data(std::ostream& os, D value) const
68 {
69 os << value;
70 }
71};
72
83template <valid_id_type I>
84std::ostream& operator<<(std::ostream& os, const Edge<I>& edge)
85{
86 os << edge.node_id;
87 return os;
88}
89
96template <valid_id_type I, valid_weight_type W>
97struct WeightedEdge : public Edge<I> {
98public:
99 using weight_type = W;
100
102
112 bool operator<(const WeightedEdge& right) const
113 {
114 // First check Ids, if they are the same compare weight
115 // Maybe change this in Future?
116 if (this->node_id == right.node_id) {
117 return this->weight < right.weight;
118 } else {
119 return this->node_id < right.node_id;
120 };
121 }
122
129 bool operator==(const WeightedEdge& other) const
130 {
131 return this->node_id == other.node_id && weight == other.weight;
132 }
133
143 template <typename D>
145 void print_with_data(std::ostream& os, D value) const
146 {
147 os << value << " (" << this->weight << ")";
148 }
149};
150
162template <valid_id_type I, valid_weight_type W>
163std::ostream& operator<<(std::ostream& os, const WeightedEdge<I, W>& edge)
164{
165 os << edge.node_id << " (" << edge.weight << ")";
166 return os;
167}
168
169} // namespace GraphLib::Internal
170
171#endif
std::ostream & operator<<(std::ostream &os, const Graph< D, I, E > &graph)
Stream insertion operator for any valid graph type.
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
Defines C++20 concepts used for type constraints in the GraphLib library.
Represents an unweighted edge to a node identified by an ID.
Definition edge.h:26
id_type node_id
The ID of the node this edge points to.
Definition edge.h:30
bool operator<(const Edge &right) const
Less-than operator comparing node IDs.
Definition edge.h:40
bool operator==(const Edge &other) const
Equality operator comparing node IDs.
Definition edge.h:51
I id_type
Alias for the node ID type.
Definition edge.h:28
void print_with_data(std::ostream &os, D value) const
Prints an unweighted Edge with its data instead of ID.
Definition edge.h:67
Represents a weighted edge to a node identified by an ID, with an associated weight.
Definition edge.h:97
W weight_type
Alias for the weight type.
Definition edge.h:99
bool operator<(const WeightedEdge &right) const
Less-than operator comparing node IDs and weights.
Definition edge.h:112
void print_with_data(std::ostream &os, D value) const
Prints an weighted Edge with its data instead of ID.
Definition edge.h:145
weight_type weight
The weight associated with this edge.
Definition edge.h:101
bool operator==(const WeightedEdge &other) const
Equality operator comparing both node IDs and weights.
Definition edge.h:129