Skip to content

Commit 8854265

Browse files
committed
code cleanup
1 parent a85861e commit 8854265

20 files changed

Lines changed: 149 additions & 83 deletions

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,6 @@ The instructions and requirements of working on the `CPP-GL` project can be foun
128128

129129
## Compiler support
130130

131-
<!--TODO: verify whether the min compiler version is correct for C++23-->
132131
| Compiler | Min version |
133132
| :-: | :-: |
134133
| GNU G++ | 14 |

docs/graph.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,7 @@ Based on the specified traits, the `graph` class defines the following types:
344344
- `source_id: types::id_type` – the ID of the source vertex.
345345
- `target_id_range: const IdRange&` – a range of target vertex IDs to connect to the source vertex.
346346
- *Return type*: `void`
347+
- *NOTE:* For an adjacency matrix representation passing a range with duplicate IDs will result in an error.
347348

348349
- **`graph.add_edges_from(source, target_range)`**:
349350
- *Description*: Adds multiple edges from a specified source vertex to a range of target vertices (specified by references).
@@ -353,6 +354,7 @@ Based on the specified traits, the `graph` class defines the following types:
353354
- `source: const vertex_type&` – the source vertex.
354355
- `target_range: const VertexRefRange&` – a range of target vertex references to connect to the source vertex.
355356
- *Return type*: `void`
357+
- *NOTE:* For an adjacency matrix representation passing a range with duplicate vertices will result in an error.
356358

357359
> [!IMPORTANT]
358360
> Behaviour of adding an edge between `first` and `second`:

include/gl/algorithm/dijkstra.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,8 @@ template <
118118
const auto& edge = negative_edge.value();
119119
throw std::invalid_argument(std::format(
120120
"[alg::dijkstra_shortest_paths] Found an edge with a negative weight: [{}, {} | w={}]",
121-
edge.first(),
122-
edge.second(),
121+
edge.source(),
122+
edge.target(),
123123
get_weight<GraphType>(edge)
124124
));
125125
}

include/gl/edge_descriptor.hpp

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,19 @@ class edge_descriptor final {
3030
}
3131

3232
explicit edge_descriptor(
33-
const types::id_type id, const types::id_type first, const types::id_type second
33+
const types::id_type id, const types::id_type source, const types::id_type target
3434
)
3535
requires(type_traits::c_empty_properties<properties_type>)
36-
: _id(id), _vertices(first, second) {}
36+
: _id(id), _vertices(source, target) {}
3737

3838
explicit edge_descriptor(
3939
const types::id_type id,
40-
const types::id_type first,
41-
const types::id_type second,
40+
const types::id_type source,
41+
const types::id_type target,
4242
properties_type& properties
4343
)
4444
requires(type_traits::c_non_empty_properties<properties_type>)
45-
: _id(id), _vertices(first, second), _properties(properties) {}
45+
: _id(id), _vertices(source, target), _properties(properties) {}
4646

4747
[[nodiscard]] gl_attr_force_inline static edge_descriptor invalid() noexcept
4848
requires(type_traits::c_empty_properties<properties_type>)
@@ -114,12 +114,12 @@ class edge_descriptor final {
114114
}
115115

116116
// TODO: rename to source
117-
[[nodiscard]] gl_attr_force_inline const types::id_type first() const noexcept {
117+
[[nodiscard]] gl_attr_force_inline const types::id_type source() const noexcept {
118118
return this->_vertices.first;
119119
}
120120

121121
// TODO: rename to target
122-
[[nodiscard]] gl_attr_force_inline const types::id_type second() const noexcept {
122+
[[nodiscard]] gl_attr_force_inline const types::id_type target() const noexcept {
123123
return this->_vertices.second;
124124
}
125125

@@ -158,7 +158,9 @@ class edge_descriptor final {
158158
}
159159

160160
[[nodiscard]] gl_attr_force_inline properties_ref_type properties() const {
161-
// TODO: throw if edge is invalid
161+
if (not this->is_valid())
162+
throw std::logic_error("Cannot access properties of an invalid edge");
163+
162164
return this->_properties.get();
163165
}
164166

@@ -180,7 +182,7 @@ class edge_descriptor final {
180182
}
181183

182184
if (io::is_option_set(os, io::graph_option::verbose)) {
183-
os << "[first: " << this->_vertices.first << ", second: " << this->_vertices.second
185+
os << "[source: " << this->_vertices.first << ", target: " << this->_vertices.second
184186
<< " | properties: " << this->_properties.get() << "]";
185187
}
186188
else {
@@ -192,7 +194,7 @@ class edge_descriptor final {
192194

193195
void _write_no_properties(std::ostream& os) const {
194196
if (io::is_option_set(os, io::graph_option::verbose))
195-
os << "[first: " << this->_vertices.first << ", second: " << this->_vertices.first
197+
os << "[source: " << this->_vertices.first << ", target: " << this->_vertices.first
196198
<< "]";
197199
else
198200
os << "[" << this->_vertices.first << ", " << this->_vertices.second << "]";

include/gl/edge_tags.hpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@ class edge_descriptor;
2626

2727
namespace type_traits {
2828

29-
// TODO: use concepts
30-
3129
template <typename T>
3230
inline constexpr bool is_directed_v = false;
3331

include/gl/graph.hpp

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,6 @@ class graph final {
300300
const types::id_type source_id,
301301
const type_traits::c_sized_range_of<types::id_type> auto& target_id_range
302302
) {
303-
// TODO: validate no duplicate target ids
304303
this->_verify_vertex_id(source_id);
305304

306305
for (const auto target_id : target_id_range) {
@@ -320,7 +319,6 @@ class graph final {
320319
const vertex_type& source,
321320
const type_traits::c_sized_range_of<vertex_type> auto& target_range
322321
) {
323-
// TODO: validate no duplicate targets
324322
this->_verify_vertex_id(source.id());
325323

326324
for (const auto& target : target_range) {
@@ -472,7 +470,7 @@ class graph final {
472470
[[nodiscard]] bool are_incident(const edge_type& edge_1, const edge_type& edge_2) const {
473471
this->_verify_edge(edge_1);
474472
this->_verify_edge(edge_2);
475-
return edge_1.is_incident_with(edge_2.first()) or edge_1.is_incident_with(edge_2.second());
473+
return edge_1.is_incident_with(edge_2.source()) or edge_1.is_incident_with(edge_2.target());
476474
}
477475

478476
// --- property methods ---
@@ -541,13 +539,13 @@ class graph final {
541539
}
542540

543541
void _verify_edge(const edge_type& edge) const {
544-
if (edge.id() >= this->_n_unique_edges or not this->has_vertex(edge.first())
545-
or not this->has_vertex(edge.second()))
542+
if (edge.id() >= this->_n_unique_edges or not this->has_vertex(edge.source())
543+
or not this->has_vertex(edge.target()))
546544
throw std::invalid_argument(std::format(
547545
"Got invalid edge [id = {}, vertices = ({}, {})]",
548546
edge.id(),
549-
edge.first(),
550-
edge.second()
547+
edge.source(),
548+
edge.target()
551549
));
552550
}
553551

@@ -623,9 +621,9 @@ class graph final {
623621
if (with_edge_properties) {
624622
const auto print_incident_edges = [this, &os](const types::id_type vertex_id) {
625623
for (const auto& edge : this->adjacent_edges(vertex_id)) {
626-
if (edge.first() != vertex_id)
624+
if (edge.source() != vertex_id)
627625
continue; // vertex is not the source
628-
os << edge.first() << ' ' << edge.second() << ' ' << edge.properties()
626+
os << edge.source() << ' ' << edge.target() << ' ' << edge.properties()
629627
<< '\n';
630628
}
631629
};
@@ -639,9 +637,9 @@ class graph final {
639637

640638
const auto print_incident_edges = [this, &os](const types::id_type vertex_id) {
641639
for (const auto& edge : this->adjacent_edges(vertex_id)) {
642-
if (edge.first() != vertex_id)
640+
if (edge.source() != vertex_id)
643641
continue; // vertex is not the source
644-
os << edge.first() << ' ' << edge.second() << '\n';
642+
os << edge.source() << ' ' << edge.target() << '\n';
645643
}
646644
};
647645

include/gl/impl/adjacency_list.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class adjacency_list final {
2121
public:
2222
using vertex_type = typename GraphTraits::vertex_type;
2323
using edge_type = typename GraphTraits::edge_type;
24-
using edge_item_list_type = std::vector<specialized::edge_list_item>;
24+
using edge_item_list_type = std::vector<specialized::adjacency_list_item>;
2525
using adjacency_list_type = std::vector<edge_item_list_type>;
2626

2727
adjacency_list(const adjacency_list&) = delete;
@@ -105,7 +105,7 @@ class adjacency_list final {
105105

106106
[[nodiscard]] gl_attr_force_inline bool has_edge(const edge_type& edge) const {
107107
return std::ranges::contains(
108-
this->_list[edge.first()], specialized::edge_list_item{edge.id(), edge.second()}
108+
this->_list[edge.source()], specialized::adjacency_list_item{edge.id(), edge.target()}
109109
);
110110
}
111111

include/gl/impl/adjacency_matrix.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ class adjacency_matrix final {
115115
}
116116

117117
[[nodiscard]] bool has_edge(const edge_type& edge) const {
118-
return this->_matrix[edge.first()][edge.second()] == edge.id();
118+
return this->_matrix[edge.source()][edge.target()] == edge.id();
119119
}
120120

121121
[[nodiscard]] std::optional<edge_type> get_edge(

include/gl/impl/specialized/adjacency_list.hpp

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,27 +19,26 @@ class adjacency_list;
1919

2020
namespace specialized {
2121

22-
// adjacency list edge item
23-
struct edge_list_item { // TODO: rename to adjacency_list_item
22+
struct adjacency_list_item {
2423
types::id_type id;
2524
types::id_type target_id;
2625

27-
[[nodiscard]] bool operator==(const edge_list_item&) const = default;
26+
[[nodiscard]] bool operator==(const adjacency_list_item&) const = default;
2827
};
2928

3029
namespace detail {
3130

3231
[[nodiscard]] auto strict_find(
33-
type_traits::c_range_of<edge_list_item> auto& edge_list, const auto& edge
32+
type_traits::c_range_of<adjacency_list_item> auto& edge_list, const auto& edge
3433
) {
3534
// find the edge by address
36-
const auto it = std::ranges::find(edge_list, edge.id(), &edge_list_item::id);
35+
const auto it = std::ranges::find(edge_list, edge.id(), &adjacency_list_item::id);
3736
if (it == edge_list.end())
3837
throw std::invalid_argument(std::format(
3938
"Got invalid edge [id = {} | vertices = ({}, {})]",
4039
edge.id(),
41-
edge.first(),
42-
edge.second()
40+
edge.source(),
41+
edge.target()
4342
));
4443

4544
return it;
@@ -58,7 +57,8 @@ struct directed_adjacency_list {
5857
) {
5958
types::size_type in_deg = constants::default_size;
6059
for (const auto& adjacent_edges : self._list)
61-
in_deg += std::ranges::count(adjacent_edges, vertex_id, &edge_list_item::target_id);
60+
in_deg +=
61+
std::ranges::count(adjacent_edges, vertex_id, &adjacency_list_item::target_id);
6262

6363
return in_deg;
6464
}
@@ -112,7 +112,7 @@ struct directed_adjacency_list {
112112
impl_type& self, const types::id_type vertex_id
113113
) {
114114
auto removed_edges =
115-
self._list[vertex_id] | std::views::transform(&edge_list_item::id)
115+
self._list[vertex_id] | std::views::transform(&adjacency_list_item::id)
116116
| std::ranges::to<std::vector>();
117117

118118
// remove all edges incident to the vertex
@@ -157,7 +157,7 @@ struct directed_adjacency_list {
157157
}
158158

159159
gl_attr_force_inline static void remove_edge(impl_type& self, const edge_type& edge) {
160-
auto& adj_edges = self._list[edge.first()];
160+
auto& adj_edges = self._list[edge.source()];
161161
adj_edges.erase(detail::strict_find(adj_edges, edge));
162162
}
163163
};
@@ -226,7 +226,7 @@ struct undirected_adjacency_list {
226226

227227
// remove the list of edges incident from the vertex entirely
228228
const auto removed_edges =
229-
self._list[vertex_id] | std::views::transform(&edge_list_item::id)
229+
self._list[vertex_id] | std::views::transform(&adjacency_list_item::id)
230230
| std::ranges::to<std::vector>();
231231
self._list.erase(std::next(std::begin(self._list), vertex_id));
232232
return removed_edges;
@@ -257,8 +257,8 @@ struct undirected_adjacency_list {
257257
}
258258

259259
static void remove_edge(impl_type& self, const edge_type& edge) {
260-
auto& adj_edges_first = self._list[edge.first()];
261-
auto& adj_edges_second = self._list[edge.second()];
260+
auto& adj_edges_first = self._list[edge.source()];
261+
auto& adj_edges_second = self._list[edge.target()];
262262

263263
if (edge.is_loop()) {
264264
adj_edges_first.erase(detail::strict_find(adj_edges_first, edge));

include/gl/impl/specialized/adjacency_matrix.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ struct undirected_adjacency_matrix {
275275
detail::strict_get(self._matrix, edge) = constants::invalid_id;
276276
// if the edge was found in the first matrix cell,
277277
// it will also be present in the second matrix cell
278-
self._matrix[edge.second()][edge.first()] = constants::invalid_id;
278+
self._matrix[edge.target()][edge.source()] = constants::invalid_id;
279279
}
280280
}
281281
};

0 commit comments

Comments
 (0)