Skip to content

Commit 635df30

Browse files
committed
more alignment
1 parent e785abd commit 635df30

5 files changed

Lines changed: 56 additions & 19 deletions

File tree

include/gl/impl/specialized/flat_adjacency_matrix.hpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,36 @@
1818

1919
namespace gl::impl::specialized {
2020

21+
namespace detail {
22+
23+
template <traits::c_id_type IdType>
24+
[[nodiscard]] auto& strict_get(flat_matrix<IdType>& id_matrix, const auto& edge) {
25+
// get the edge and validate the address
26+
const auto [source_id, target_id] = edge.incident_vertices();
27+
auto& edge_id = id_matrix[to_idx(source_id), to_idx(target_id)];
28+
if (edge.id() != edge_id)
29+
throw std::invalid_argument(std::format(
30+
"Got invalid edge [id = {} | vertices = ({}, {})]", edge.id(), source_id, target_id
31+
));
32+
33+
return edge_id;
34+
}
35+
36+
template <traits::c_id_type IdType>
37+
inline void check_edge_override(
38+
const flat_matrix<IdType>& id_matrix, const IdType source_id, const IdType target_id
39+
) {
40+
if (const auto edge_id = id_matrix[to_idx(source_id), to_idx(target_id)]; edge_id != invalid_id)
41+
throw std::logic_error(std::format(
42+
"Cannot override an existing edge: [id = {}, vertices = ({}, {})]",
43+
edge_id,
44+
source_id,
45+
target_id
46+
));
47+
}
48+
49+
} // namespace detail
50+
2151
template <traits::c_instantiation_of<adjacency_matrix> AdjacencyMatrix>
2252
requires(traits::c_directed_edge<typename AdjacencyMatrix::edge_type>)
2353
struct directed_flat_adjacency_matrix {

include/gl/types/flat_jagged_vector.hpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@
1313
#include <stdexcept>
1414
#include <vector>
1515

16-
// TODO: use std::ranges::subrange instead of std::span
16+
// TODO:
17+
// - use std::ranges::subrange instead of std::span
18+
// - define container_type
19+
// - use std::ptrdiff_t for offsets
1720

1821
namespace gl {
1922

include/gl/types/flat_matrix.hpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,9 @@ class flat_matrix {
9999
/// @brief Dereferences the iterator to the current row.
100100
/// @return A subrange representing the row at the current position
101101
[[nodiscard]] reference operator*() const noexcept {
102-
const auto row_beg = this->_data_iter + this->_row_idx * this->_row_size;
103-
return reference(row_beg, row_beg + this->_row_size);
102+
const auto row_beg =
103+
this->_data_iter + static_cast<std::ptrdiff_t>(this->_row_idx * this->_row_size);
104+
return reference(row_beg, row_beg + static_cast<std::ptrdiff_t>(this->_row_size));
104105
}
105106

106107
/// @brief Random access to a row at an offset from the current position.
@@ -484,8 +485,8 @@ class flat_matrix {
484485
/// @pre `r < n_rows()`; otherwise Undefined Behavior
485486
/// @warning No bounds checking is performed for performance.
486487
[[nodiscard]] row_type operator[](size_type r) {
487-
const auto row_beg = this->_data.begin() + r * this->_n_cols;
488-
return row_type(row_beg, row_beg + this->_n_cols);
488+
const auto row_beg = this->_data.begin() + static_cast<std::ptrdiff_t>(r * this->_n_cols);
489+
return row_type(row_beg, row_beg + static_cast<std::ptrdiff_t>(this->_n_cols));
489490
}
490491

491492
/// @brief Returns a const row at the given index without bounds checking.
@@ -494,8 +495,8 @@ class flat_matrix {
494495
/// @pre `r < n_rows()`; otherwise Undefined Behavior
495496
/// @warning No bounds checking is performed.
496497
[[nodiscard]] const_row_type operator[](size_type r) const {
497-
const auto row_beg = this->_data.begin() + r * this->_n_cols;
498-
return const_row_type(row_beg, row_beg + this->_n_cols);
498+
const auto row_beg = this->_data.begin() + static_cast<std::ptrdiff_t>(r * this->_n_cols);
499+
return const_row_type(row_beg, row_beg + static_cast<std::ptrdiff_t>(this->_n_cols));
499500
}
500501

501502
/// @brief Returns a reference to an element without bounds checking.

tests/CMakeLists.txt

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,6 @@
11
cmake_minimum_required(VERSION 3.12)
22
project(cpp-gl-test)
33

4-
# Set compile options
5-
if(NOT DEFINED CMAKE_CXX_FLAGS)
6-
set(
7-
CMAKE_CXX_FLAGS
8-
"-Werror -Wall -Wextra -Wcast-align -Wconversion -Wsign-conversion -Wunreachable-code -Wuninitialized -Wunused -pedantic -g -O2"
9-
CACHE STRING "Default C++ compile flags" FORCE
10-
)
11-
endif()
12-
134
# Common structure
145
set(SOURCE_DIRS "app")
156
set(INCLUDE_DIRS "include" "external")
@@ -33,7 +24,21 @@ function(add_test_target TARGET_NAME SOURCE_DIRS DATA_DIR TESTING_DEF)
3324
)
3425

3526
target_include_directories(${TARGET_NAME} PRIVATE ${INCLUDE_DIRS})
36-
target_compile_options(${TARGET_NAME} PRIVATE ${CMAKE_CXX_FLAGS})
27+
# TODO: uncomment and align
28+
# target_compile_options(${TARGET_NAME} PRIVATE
29+
# -Werror
30+
# -Wall
31+
# -Wextra
32+
# -Wcast-align
33+
# -Wconversion
34+
# -Wsign-conversion
35+
# -Wunreachable-code
36+
# -Wuninitialized
37+
# -Wunused
38+
# -pedantic
39+
# -g
40+
# -O2
41+
# )
3742
target_compile_definitions(${TARGET_NAME} PRIVATE
3843
${TESTING_DEF}
3944
TEST_DATA_PATH="${DATA_DIR}"

tests/source/gl/test_adjacency_matrix.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,6 @@ TEST_CASE_TEMPLATE_DEFINE(
7474
}
7575

7676
SUBCASE("add_edge should throw an error if the vertices are already incident") {
77-
using edge_type = typename SutType::edge_type;
78-
7977
SutType sut{constants::n_elements};
8078
sut.add_edge(fixture.next_edge_id++, constants::v1_id, constants::v2_id);
8179
REQUIRE(sut.has_edge(constants::v1_id, constants::v2_id));

0 commit comments

Comments
 (0)