Skip to content

Commit 8bc5aad

Browse files
committed
to_diff
1 parent 19ad18d commit 8bc5aad

7 files changed

Lines changed: 33 additions & 40 deletions

File tree

include/gl/impl/specialized/adjacency_list.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ struct directed_adjacency_list {
154154
}
155155

156156
// remove the list of edges incident from the vertex entirely
157-
self._list.erase(self._list.begin() + static_cast<std::ptrdiff_t>(vertex_id));
157+
self._list.erase(self._list.begin() + to_diff(vertex_id));
158158
return removed_edges;
159159
}
160160

@@ -255,7 +255,7 @@ struct undirected_adjacency_list {
255255
const auto removed_edges =
256256
self._list[vertex_idx] | std::views::transform(&item_type::edge_id)
257257
| std::ranges::to<std::vector>();
258-
self._list.erase(self._list.begin() + static_cast<std::ptrdiff_t>(vertex_id));
258+
self._list.erase(self._list.begin() + to_diff(vertex_id));
259259
return removed_edges;
260260
}
261261

include/gl/impl/specialized/adjacency_matrix.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -154,12 +154,12 @@ struct directed_adjacency_matrix {
154154
| std::views::filter([](auto edge_id) { return edge_id != invalid_id; })
155155
| std::ranges::to<std::vector>();
156156

157-
self._matrix.erase(self._matrix.begin() + static_cast<std::ptrdiff_t>(vertex_id));
158-
157+
const auto vertex_pos = to_diff(vertex_id);
158+
self._matrix.erase(self._matrix.begin() + vertex_pos);
159159
for (auto& row : self._matrix) {
160160
if (const auto edge_id = row[vertex_idx]; edge_id != invalid_id)
161161
removed_edges.push_back(edge_id);
162-
row.erase(row.begin() + static_cast<std::ptrdiff_t>(vertex_id));
162+
row.erase(row.begin() + vertex_pos);
163163
}
164164

165165
return removed_edges;
@@ -281,7 +281,7 @@ struct undirected_adjacency_matrix {
281281
| std::views::filter([](auto edge_id) { return edge_id != invalid_id; })
282282
| std::ranges::to<std::vector>();
283283

284-
const auto vertex_pos = static_cast<std::ptrdiff_t>(vertex_id);
284+
const auto vertex_pos = to_diff(vertex_id);
285285
self._matrix.erase(self._matrix.begin() + vertex_pos);
286286
for (auto& row : self._matrix)
287287
row.erase(row.begin() + vertex_pos);

include/gl/types/core.hpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ concept c_id_type = std::unsigned_integral<T>;
2828
return static_cast<size_type>(id);
2929
}
3030

31-
// TODO: replace static_cast calls with to_diff
3231
[[nodiscard]] gl_attr_force_inline constexpr std::ptrdiff_t to_diff(std::integral auto i) noexcept {
3332
return static_cast<std::ptrdiff_t>(i);
3433
}

include/gl/types/flat_jagged_vector.hpp

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ class flat_jagged_vector {
5454

5555
/// @brief Random access iterator over segments of the `flat_jagged_vector`.
5656
///
57-
/// This iterator dereferences to a `segment_type` (span of elements in a single segment),
57+
/// This iterator dereferences to a `segment_type` (subrange of elements in a single segment),
5858
/// allowing efficient iteration and random access to individual segments. The iterator maintains
5959
/// pointers to the element data and the offsets array for dereferencing.
6060
///
@@ -74,13 +74,13 @@ class flat_jagged_vector {
7474
using iterator_concept = std::random_access_iterator_tag;
7575
/// @brief Legacy iterator category (random access)
7676
using iterator_category = std::random_access_iterator_tag;
77-
/// @brief Type of segment this iterator dereferences to (span or const span)
77+
/// @brief Type of segment this iterator dereferences to (subrange or const subrange)
7878
using value_type = std::conditional_t<Const, const_segment_type, segment_type>;
7979
/// @brief Signed integral difference type
8080
using difference_type = std::ptrdiff_t;
81-
/// @brief Pointer type (void because segment iterators dereference to spans)
81+
/// @brief Pointer type (void because segment iterators dereference to subranges)
8282
using pointer = void;
83-
/// @brief Reference type (span of elements)
83+
/// @brief Reference type (subrange of elements)
8484
using reference = value_type;
8585

8686
/// @brief Default constructor creates a null iterator
@@ -101,7 +101,7 @@ class flat_jagged_vector {
101101
}
102102

103103
/// @brief Dereferences the iterator to the current segment.
104-
/// @return A span representing the segment at the current position
104+
/// @return A subrange representing the segment at the current position
105105
[[nodiscard]] reference operator*() const noexcept {
106106
const auto beg = to_diff(*this->_offset_ptr);
107107
const auto end = to_diff(*(this->_offset_ptr + 1uz));
@@ -472,7 +472,7 @@ class flat_jagged_vector {
472472

473473
/// @brief Returns the segment at the given index without bounds checking.
474474
/// @param i The index of the segment to access
475-
/// @return A span representing the segment at index i
475+
/// @return A subrange representing the segment at index i
476476
/// @pre `i < size()`; otherwise Undefined Behavior
477477
/// @warning No bounds checking is performed for performance. Use `at()` for bounds-checked access.
478478
/// Calling on an out-of-bounds index results in Undefined Behavior.
@@ -484,7 +484,7 @@ class flat_jagged_vector {
484484

485485
/// @brief Returns a const segment at the given index without bounds checking.
486486
/// @param i The index of the segment to access
487-
/// @return A const span representing the segment at index i
487+
/// @return A const subrange representing the segment at index i
488488
/// @pre `i < size()`; otherwise Undefined Behavior
489489
/// @warning No bounds checking is performed for performance. Use `at()` for bounds-checked access.
490490
/// Calling on an out-of-bounds index results in Undefined Behavior.
@@ -518,7 +518,7 @@ class flat_jagged_vector {
518518

519519
/// @brief Returns the segment at the given index with bounds checking.
520520
/// @param i The index of the segment
521-
/// @return A span representing the segment at index i
521+
/// @return A subrange representing the segment at index i
522522
/// @exception std::out_of_range If `i >= size()`
523523
/// @note Provides the same safety as `std::vector::at()`
524524
[[nodiscard]] segment_type at(size_type i) {
@@ -528,7 +528,7 @@ class flat_jagged_vector {
528528

529529
/// @brief Returns a const segment at the given index with bounds checking.
530530
/// @param i The index of the segment
531-
/// @return A const span representing the segment at index i
531+
/// @return A const subrange representing the segment at index i
532532
/// @exception std::out_of_range If `i >= size()`
533533
/// @note Provides the same safety as `std::vector::at()`
534534
[[nodiscard]] const_segment_type at(size_type i) const {
@@ -561,31 +561,31 @@ class flat_jagged_vector {
561561
}
562562

563563
/// @brief Returns the first segment without bounds checking.
564-
/// @return A span representing the first segment
564+
/// @return A subrange representing the first segment
565565
/// @pre Container must not be empty; otherwise Undefined Behavior
566566
/// @warning No bounds checking. Results in Undefined Behavior if container is empty.
567567
[[nodiscard]] segment_type front() noexcept {
568568
return (*this)[0uz];
569569
}
570570

571571
/// @brief Returns a const reference to the first segment without bounds checking.
572-
/// @return A const span representing the first segment
572+
/// @return A const subrange representing the first segment
573573
/// @pre Container must not be empty; otherwise Undefined Behavior
574574
/// @warning No bounds checking. Results in Undefined Behavior if container is empty.
575575
[[nodiscard]] const_segment_type front() const noexcept {
576576
return (*this)[0uz];
577577
}
578578

579579
/// @brief Returns the last segment without bounds checking.
580-
/// @return A span representing the last segment
580+
/// @return A subrange representing the last segment
581581
/// @pre Container must not be empty; otherwise Undefined Behavior
582582
/// @warning No bounds checking. Results in Undefined Behavior if container is empty.
583583
[[nodiscard]] segment_type back() noexcept {
584584
return (*this)[this->size() - 1uz];
585585
}
586586

587587
/// @brief Returns a const reference to the last segment without bounds checking.
588-
/// @return A const span representing the last segment
588+
/// @return A const subrange representing the last segment
589589
/// @pre Container must not be empty; otherwise Undefined Behavior
590590
/// @warning No bounds checking. Results in Undefined Behavior if container is empty.
591591
[[nodiscard]] const_segment_type back() const noexcept {
@@ -670,15 +670,15 @@ class flat_jagged_vector {
670670
return this->_data.size();
671671
}
672672

673-
/// @brief Returns a span over all element data in flattened form.
674-
/// @return A span of all elements in the underlying `_data` array.
673+
/// @brief Returns a subrange of all element data in flattened form.
674+
/// @return A subrange of all elements in the underlying `_data` array.
675675
/// @note Allows direct access to the flattened representation of all segments.
676676
[[nodiscard]] segment_type data_view() noexcept {
677677
return segment_type(this->_data);
678678
}
679679

680-
/// @brief Returns a const span over all element data in flattened form.
681-
/// @return A const span of all elements in the underlying `_data` array.
680+
/// @brief Returns a const subrange of all element data in flattened form.
681+
/// @return A const subrange of all elements in the underlying `_data` array.
682682
/// @note Allows direct access to the flattened representation of all segments.
683683
[[nodiscard]] const_segment_type data_view() const noexcept {
684684
return const_segment_type(this->_data);

include/hgl/impl/incidence_list.hpp

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -169,9 +169,7 @@ class incidence_list<hgl::undirected_t, ImplTag> final {
169169
template <element_type Element>
170170
void _remove(const id_type id) noexcept {
171171
if constexpr (Element == layout_tag::major_element) { // remove major
172-
this->_major_storage.erase(
173-
this->_major_storage.begin() + static_cast<std::ptrdiff_t>(id)
174-
);
172+
this->_major_storage.erase(this->_major_storage.begin() + to_diff(id));
175173
}
176174
else { // remove minor
177175
for (auto& minor_storage : this->_major_storage) {
@@ -449,12 +447,8 @@ class incidence_list<hgl::bf_directed_t, ImplTag> final {
449447
template <element_type Element>
450448
void _remove(const id_type id) noexcept {
451449
if constexpr (Element == layout_tag::major_element) { // remove major
452-
this->_tail_storage.erase(
453-
this->_tail_storage.begin() + static_cast<std::ptrdiff_t>(id)
454-
);
455-
this->_head_storage.erase(
456-
this->_head_storage.begin() + static_cast<std::ptrdiff_t>(id)
457-
);
450+
this->_tail_storage.erase(this->_tail_storage.begin() + to_diff(id));
451+
this->_head_storage.erase(this->_head_storage.begin() + to_diff(id));
458452
}
459453
else { // remove minor
460454
for (auto& minor_storage : this->_tail_storage)

include/hgl/impl/incidence_matrix.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -166,14 +166,14 @@ class incidence_matrix<hgl::undirected_t, ImplTag> final {
166166
template <element_type Element>
167167
void _remove(const id_type id) noexcept {
168168
if constexpr (Element == layout_tag::major_element) { // remove major
169-
this->_matrix.erase(this->_matrix.begin() + static_cast<std::ptrdiff_t>(id));
169+
this->_matrix.erase(this->_matrix.begin() + to_diff(id));
170170
}
171171
else { // remove minor
172172
if (this->_matrix_row_size == 0uz)
173173
return;
174174
this->_matrix_row_size--;
175175
for (auto& row : this->_matrix) {
176-
row.erase(row.begin() + static_cast<std::ptrdiff_t>(id));
176+
row.erase(row.begin() + to_diff(id));
177177
}
178178
}
179179
}
@@ -464,14 +464,14 @@ class incidence_matrix<hgl::bf_directed_t, ImplTag> final {
464464
template <element_type Element>
465465
void _remove(const id_type id) noexcept {
466466
if constexpr (Element == layout_tag::major_element) { // remove major
467-
this->_matrix.erase(this->_matrix.begin() + static_cast<std::ptrdiff_t>(id));
467+
this->_matrix.erase(this->_matrix.begin() + to_diff(id));
468468
}
469469
else { // remove minor
470470
if (this->_matrix_row_size == 0uz)
471471
return;
472472
this->_matrix_row_size--;
473473
for (auto& row : this->_matrix)
474-
row.erase(row.begin() + static_cast<std::ptrdiff_t>(id));
474+
row.erase(row.begin() + to_diff(id));
475475
}
476476
}
477477

tests/source/gl/test_graph_topology_builders.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ TEST_CASE_TEMPLATE_DEFINE(
285285
SUBCASE("path(n_vertices) should build a one-way path graph of size n_vertices") {
286286
const auto path = gl::topology::path<graph_type>(constants::n_elements_top);
287287
const auto n_source_vertices = path.order() - 1uz;
288-
const auto last_vertex_pos = static_cast<std::ptrdiff_t>(n_source_vertices);
288+
const auto last_vertex_pos = gl::to_diff(n_source_vertices);
289289

290290
verify_graph_size(path, constants::n_elements_top, n_source_vertices);
291291

@@ -299,7 +299,7 @@ TEST_CASE_TEMPLATE_DEFINE(
299299
SUBCASE("bidirectional_path(n_vertices) should build a two-way path graph of size n_vertices") {
300300
const auto path = gl::topology::bidirectional_path<graph_type>(constants::n_elements_top);
301301
const auto n_source_vertices = path.order() - 1uz;
302-
const auto last_vertex_pos = static_cast<std::ptrdiff_t>(n_source_vertices);
302+
const auto last_vertex_pos = gl::to_diff(n_source_vertices);
303303

304304
verify_graph_size(path, constants::n_elements_top, 2uz * n_source_vertices);
305305

@@ -389,7 +389,7 @@ TEST_CASE_TEMPLATE_DEFINE(
389389
CAPTURE(path);
390390

391391
const auto n_source_vertices = path.order() - 1uz;
392-
const auto last_vertex_pos = static_cast<std::ptrdiff_t>(n_source_vertices);
392+
const auto last_vertex_pos = gl::to_diff(n_source_vertices);
393393
verify_graph_size(path, constants::n_elements_top, n_source_vertices);
394394

395395
const auto vertices = path.vertices();

0 commit comments

Comments
 (0)