Skip to content

Commit a47665f

Browse files
committed
more wip
1 parent b2e499b commit a47665f

5 files changed

Lines changed: 241 additions & 192 deletions

File tree

include/gl/graph.hpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
// - impl::add_edge should accept: id, first, second
77
// - impl::add_edges_from should accept: edge_ids view, source_id, target_id_range
88
// - impl::remove_edge should accept: id, first, second
9+
// - add a get_vertex_properties(id) function
10+
// - add a get_edge_properties(id) function
11+
// - add n_adjacent_edges(id) function
912

1013
#pragma once
1114

@@ -253,8 +256,6 @@ class graph final {
253256
return this->_impl.degree_map();
254257
}
255258

256-
// TODO: add a get_vertex_properties function
257-
258259
[[nodiscard]] gl_attr_force_inline auto vertex_properties_map() const noexcept
259260
requires(not type_traits::is_default_properties_type_v<vertex_properties_type>)
260261
{

include/gl/impl/adjacency_matrix.hpp

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,13 @@
1111
#include "gl/types/types.hpp"
1212
#include "specialized/adjacency_matrix.hpp"
1313

14+
#ifdef GL_TESTING
15+
namespace gl_testing {
16+
struct test_adjacency_matrix;
17+
} // namespace gl_testing
18+
#endif
19+
20+
1421
namespace gl::impl {
1522

1623
template <type_traits::c_matrix_graph_traits GraphTraits>
@@ -164,30 +171,39 @@ class adjacency_matrix final {
164171

165172
[[nodiscard]] gl_attr_force_inline auto adjacent_edges(const types::id_type vertex_id) const {
166173
return this->_matrix[vertex_id] | std::views::enumerate
167-
| std::views::filter([](const auto target_id, const auto edge_id) {
174+
| std::views::filter([](const auto& edge_info) {
175+
const auto& [target_id, edge_id] = edge_info;
168176
return edge_id != constants::invalid_id;
169177
})
170-
| std::views::transform([vertex_id](const auto target_id, const auto edge_id) {
171-
return edge_type{edge_id, vertex_id, target_id};
178+
| std::views::transform([vertex_id](const auto& edge_info) {
179+
const auto& [target_id, edge_id] = edge_info;
180+
return edge_type{edge_id, vertex_id, static_cast<types::id_type>(target_id)};
172181
});
173182
}
174183

175184
[[nodiscard]] gl_attr_force_inline auto adjacent_edges(
176185
const types::id_type vertex_id, const auto& edge_properties_map
177186
) const {
178187
return this->_matrix[vertex_id] | std::views::enumerate
179-
| std::views::filter([](const auto target_id, const auto edge_id) {
188+
| std::views::filter([](const auto& edge_info) {
189+
const auto& [target_id, edge_id] = edge_info;
180190
return edge_id != constants::invalid_id;
181191
})
182-
| std::views::transform(
183-
[vertex_id, &edge_properties_map](const auto target_id, const auto edge_id) {
184-
return edge_type{
185-
edge_id, vertex_id, target_id, edge_properties_map[edge_id]
186-
};
187-
}
188-
);
192+
| std::views::transform([vertex_id, &edge_properties_map](const auto& edge_info) {
193+
const auto& [target_id, edge_id] = edge_info;
194+
return edge_type{
195+
edge_id,
196+
vertex_id,
197+
static_cast<types::id_type>(target_id),
198+
edge_properties_map[edge_id]
199+
};
200+
});
189201
}
190202

203+
#ifdef GL_TESTING
204+
friend struct gl_testing::test_adjacency_matrix;
205+
#endif
206+
191207
private:
192208
using specialized_impl = typename specialized::matrix_impl_traits<adjacency_matrix>::type;
193209
friend specialized_impl;

include/gl/impl/specialized/adjacency_matrix.hpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ struct directed_adjacency_matrix {
151151
const type_traits::c_sized_range_of<types::id_type> auto& target_ids
152152
) {
153153
for (const auto target_id : target_ids)
154-
detail::check_edge_override(self, source_id, target_id);
154+
detail::check_edge_override(self._matrix, source_id, target_id);
155155

156156
auto& matrix_source_row = self._matrix[source_id];
157157
for (auto [edge_id, target_id] : std::views::zip(edge_ids, target_ids))
@@ -170,7 +170,6 @@ struct undirected_adjacency_matrix {
170170
using impl_type = AdjacencyMatrix;
171171
using vertex_type = typename impl_type::vertex_type;
172172
using edge_type = typename impl_type::edge_type;
173-
using edge_ptr_type = typename impl_type::edge_ptr_type;
174173

175174
[[nodiscard]] gl_attr_force_inline static types::size_type in_degree(
176175
const impl_type& self, const types::id_type vertex_id
@@ -240,7 +239,7 @@ struct undirected_adjacency_matrix {
240239
static void add_edge(
241240
impl_type& self, types::id_type edge_id, types::id_type source_id, types::id_type target_id
242241
) {
243-
detail::check_edge_override<impl_type>(self, source_id, target_id);
242+
detail::check_edge_override(self._matrix, source_id, target_id);
244243

245244
self._matrix[source_id][target_id] = edge_id;
246245
if (target_id != source_id)
@@ -254,7 +253,7 @@ struct undirected_adjacency_matrix {
254253
const type_traits::c_sized_range_of<types::id_type> auto& target_ids
255254
) {
256255
for (const auto target_id : target_ids)
257-
detail::check_edge_override(self, source_id, target_id);
256+
detail::check_edge_override(self._matrix, source_id, target_id);
258257

259258
auto& matrix_source_row = self._matrix[source_id];
260259
for (auto [edge_id, target_id] : std::views::zip(edge_ids, target_ids)) {

include/gl/util/ranges.hpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright (c) 2024-2026 Jakub Musiał
2+
// This file is part of the CPP-GL project (https://github.com/SpectraL519/cpp-gl).
3+
// Licensed under the MIT License. See the LICENSE file in the project root for full license information.
4+
5+
#include <ranges>
6+
7+
namespace gl::util {
8+
9+
template <std::ranges::range R>
10+
constexpr auto range_size(R&& r) {
11+
if constexpr (std::ranges::sized_range<R>)
12+
return std::ranges::size(r);
13+
else
14+
// Will consume input ranges!
15+
return std::ranges::distance(std::begin(r), std::end(r));
16+
}
17+
18+
} // namespace gl::util

0 commit comments

Comments
 (0)