Skip to content

Commit c996ccc

Browse files
committed
clang alignment
1 parent 37e9ccc commit c996ccc

15 files changed

Lines changed: 279 additions & 85 deletions
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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+
#pragma once
6+
7+
#define GL_PRAGMA(x) _Pragma(#x)
8+
9+
#if defined(__clang__)
10+
11+
#define GL_SUPPRESS_WARNING_BEGIN(w) \
12+
GL_PRAGMA(clang diagnostic push) \
13+
GL_PRAGMA(clang diagnostic ignored w)
14+
15+
#define GL_SUPPRESS_WARNING_END GL_PRAGMA(clang diagnostic pop)
16+
17+
#define GL_SUPPRESS_CLANG_WARNING_BEGIN(w) GL_SUPPRESS_WARNING_BEGIN(w)
18+
19+
// GCC-only warning: Push on Clang to keep the END macro balanced, but don't ignore
20+
#define GL_SUPPRESS_GCC_WARNING_BEGIN(w) GL_PRAGMA(clang diagnostic push)
21+
22+
#elif defined(__GNUC__)
23+
24+
#define GL_SUPPRESS_WARNING_BEGIN(w) \
25+
GL_PRAGMA(GCC diagnostic push) \
26+
GL_PRAGMA(GCC diagnostic ignored w)
27+
28+
#define GL_SUPPRESS_WARNING_END GL_PRAGMA(GCC diagnostic pop)
29+
30+
#define GL_SUPPRESS_GCC_WARNING_BEGIN(w) GL_SUPPRESS_WARNING_BEGIN(w)
31+
32+
// Clang-only warning: Push on GCC to keep the END macro balanced, but don't ignore
33+
#define GL_SUPPRESS_CLANG_WARNING_BEGIN(w) GL_PRAGMA(GCC diagnostic push)
34+
35+
#else
36+
37+
#define GL_SUPPRESS_WARNING_BEGIN(w)
38+
#define GL_SUPPRESS_WARNING_END
39+
#define GL_SUPPRESS_GCC_WARNING_BEGIN(w)
40+
#define GL_SUPPRESS_CLANG_WARNING_BEGIN(w)
41+
42+
#endif

include/gl/impl/specialized/adjacency_list.hpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@
88
#include "gl/decl/impl_tags.hpp"
99
#include "gl/graph_traits.hpp"
1010
#include "gl/traits.hpp"
11+
#include "gl/types/core.hpp"
1112

1213
#include <algorithm>
14+
#include <cstddef>
1315
#include <format>
1416
#include <iostream>
1517
#include <ranges>
@@ -77,7 +79,9 @@ struct directed_adjacency_list {
7779
[[nodiscard]] static size_type in_degree(const impl_type& self, id_type vertex_id) {
7880
size_type in_deg = 0uz;
7981
for (const auto& adjacent_edges : self._list)
80-
in_deg += std::ranges::count(adjacent_edges, vertex_id, &item_type::vertex_id);
82+
in_deg += static_cast<size_type>(
83+
std::ranges::count(adjacent_edges, vertex_id, &item_type::vertex_id)
84+
);
8185

8286
return in_deg;
8387
}
@@ -150,7 +154,7 @@ struct directed_adjacency_list {
150154
}
151155

152156
// remove the list of edges incident from the vertex entirely
153-
self._list.erase(std::next(std::begin(self._list), vertex_idx));
157+
self._list.erase(self._list.begin() + static_cast<std::ptrdiff_t>(vertex_id));
154158
return removed_edges;
155159
}
156160

@@ -251,7 +255,7 @@ struct undirected_adjacency_list {
251255
const auto removed_edges =
252256
self._list[vertex_idx] | std::views::transform(&item_type::edge_id)
253257
| std::ranges::to<std::vector>();
254-
self._list.erase(std::next(std::begin(self._list), vertex_idx));
258+
self._list.erase(self._list.begin() + static_cast<std::ptrdiff_t>(vertex_id));
255259
return removed_edges;
256260
}
257261

include/gl/impl/specialized/adjacency_matrix.hpp

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@
44

55
#pragma once
66

7+
#include "gl/attributes/diagnostics.hpp"
78
#include "gl/constants.hpp"
89
#include "gl/decl/impl_tags.hpp"
910
#include "gl/graph_traits.hpp"
1011
#include "gl/types/core.hpp"
1112

1213
#include <algorithm>
14+
#include <cstddef>
1315
#include <vector>
1416

1517
namespace gl::impl {
@@ -76,19 +78,28 @@ struct directed_adjacency_matrix {
7678
self._matrix.emplace_back(new_n_vertices, invalid_id);
7779
}
7880

81+
GL_SUPPRESS_WARNING_BEGIN("-Wsign-conversion")
82+
83+
// NOTE: Indexing into a row which might be a vector (requires size type) or a span/subrange (requires difference type)
84+
7985
[[nodiscard]] gl_attr_force_inline static size_type in_degree(
8086
const impl_type& self, id_type vertex_id
8187
) {
82-
return std::ranges::count_if(self._matrix, [vertex_id](const auto& row) {
83-
return row[to_idx(vertex_id)] != invalid_id;
84-
});
88+
return static_cast<size_type>(std::ranges::count_if(
89+
self._matrix,
90+
[vertex_id](const auto& row) { return row[to_idx(vertex_id)] != invalid_id; }
91+
));
8592
}
8693

94+
GL_SUPPRESS_WARNING_END
95+
8796
[[nodiscard]] gl_attr_force_inline static size_type out_degree(
8897
const impl_type& self, id_type vertex_id
8998
) {
9099
return self._matrix[vertex_id].size()
91-
- std::ranges::count(self._matrix[vertex_id], invalid_id_v<id_type>);
100+
- static_cast<size_type>(
101+
std::ranges::count(self._matrix[vertex_id], invalid_id_v<id_type>)
102+
);
92103
}
93104

94105
[[nodiscard]] gl_attr_force_inline static size_type degree(
@@ -143,12 +154,12 @@ struct directed_adjacency_matrix {
143154
| std::views::filter([](auto edge_id) { return edge_id != invalid_id; })
144155
| std::ranges::to<std::vector>();
145156

146-
self._matrix.erase(std::next(std::begin(self._matrix), vertex_idx));
157+
self._matrix.erase(self._matrix.begin() + static_cast<std::ptrdiff_t>(vertex_id));
147158

148159
for (auto& row : self._matrix) {
149160
if (const auto edge_id = row[vertex_idx]; edge_id != invalid_id)
150161
removed_edges.push_back(edge_id);
151-
row.erase(std::next(std::begin(row), vertex_idx));
162+
row.erase(row.begin() + static_cast<std::ptrdiff_t>(vertex_id));
152163
}
153164

154165
return removed_edges;
@@ -225,7 +236,9 @@ struct undirected_adjacency_matrix {
225236
) {
226237
const auto vertex_idx = to_idx(vertex_id);
227238
return self._matrix.size()
228-
- std::ranges::count(self._matrix[vertex_idx], invalid_id_v<id_type>)
239+
- static_cast<size_type>(
240+
std::ranges::count(self._matrix[vertex_idx], invalid_id_v<id_type>)
241+
)
229242
+ static_cast<size_type>(self._matrix[vertex_idx][vertex_idx] != invalid_id);
230243
}
231244

@@ -264,9 +277,9 @@ struct undirected_adjacency_matrix {
264277
| std::views::filter([](auto edge_id) { return edge_id != invalid_id; })
265278
| std::ranges::to<std::vector>();
266279

267-
self._matrix.erase(std::next(std::begin(self._matrix), vertex_idx));
280+
self._matrix.erase(self._matrix.begin() + static_cast<std::ptrdiff_t>(vertex_id));
268281
for (auto& row : self._matrix)
269-
row.erase(std::next(std::begin(row), vertex_idx));
282+
row.erase(row.begin() + static_cast<std::ptrdiff_t>(vertex_id));
270283

271284
return removed_edges;
272285
}

include/gl/impl/specialized/flat_adjacency_list.hpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "gl/decl/impl_tags.hpp"
99
#include "gl/graph_traits.hpp"
1010
#include "gl/impl/specialized/adjacency_list.hpp"
11+
#include "gl/types/core.hpp"
1112
#include "gl/types/flat_jagged_vector.hpp"
1213

1314
#include <algorithm>
@@ -42,7 +43,9 @@ struct directed_flat_adjacency_list {
4243
}
4344

4445
[[nodiscard]] static size_type in_degree(const impl_type& self, id_type vertex_id) {
45-
return std::ranges::count(self._list.data_view(), vertex_id, &item_type::vertex_id);
46+
return static_cast<size_type>(
47+
std::ranges::count(self._list.data_view(), vertex_id, &item_type::vertex_id)
48+
);
4649
}
4750

4851
[[nodiscard]] gl_attr_force_inline static size_type out_degree(

include/gl/impl/specialized/flat_adjacency_matrix.hpp

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "gl/types/flat_matrix.hpp"
1313

1414
#include <algorithm>
15+
#include <cstddef>
1516
#include <format>
1617
#include <ranges>
1718
#include <vector>
@@ -45,16 +46,16 @@ struct directed_flat_adjacency_matrix {
4546
[[nodiscard]] gl_attr_force_inline static size_type in_degree(
4647
const impl_type& self, id_type vertex_id
4748
) {
48-
return std::ranges::count_if(self._matrix.col(to_idx(vertex_id)), [](auto edge_id) {
49-
return edge_id != invalid_id;
50-
});
49+
return static_cast<size_type>(std::ranges::count_if(
50+
self._matrix.col(to_idx(vertex_id)), [](auto edge_id) { return edge_id != invalid_id; }
51+
));
5152
}
5253

5354
[[nodiscard]] gl_attr_force_inline static size_type out_degree(
5455
const impl_type& self, id_type vertex_id
5556
) {
5657
const auto row = self._matrix[to_idx(vertex_id)];
57-
return row.size() - std::ranges::count(row, invalid_id_v<id_type>);
58+
return row.size() - static_cast<size_type>(std::ranges::count(row, invalid_id_v<id_type>));
5859
}
5960

6061
[[nodiscard]] gl_attr_force_inline static size_type degree(
@@ -67,7 +68,7 @@ struct directed_flat_adjacency_matrix {
6768

6869
for (auto v_idx = 0uz; v_idx < self._matrix.n_rows(); ++v_idx)
6970
deg += static_cast<size_type>(row[v_idx] != invalid_id)
70-
+ static_cast<size_type>(col[v_idx] != invalid_id);
71+
+ static_cast<size_type>(col[static_cast<std::ptrdiff_t>(v_idx)] != invalid_id);
7172

7273
return deg;
7374
}
@@ -119,7 +120,9 @@ struct directed_flat_adjacency_matrix {
119120
for (auto r_idx = 0uz; r_idx < self._matrix.n_rows(); ++r_idx) {
120121
if (r_idx == vertex_idx)
121122
continue;
122-
if (const auto edge_id = col[r_idx]; edge_id != invalid_id)
123+
124+
const auto edge_id = col[static_cast<std::ptrdiff_t>(r_idx)];
125+
if (edge_id != invalid_id)
123126
removed_edges.push_back(edge_id);
124127
}
125128

@@ -197,7 +200,8 @@ struct undirected_flat_adjacency_matrix {
197200
) {
198201
const auto vertex_idx = to_idx(vertex_id);
199202
const auto row = self._matrix[vertex_idx];
200-
return self._matrix.n_cols() - std::ranges::count(row, invalid_id_v<id_type>)
203+
return self._matrix.n_cols()
204+
- static_cast<size_type>(std::ranges::count(row, invalid_id_v<id_type>))
201205
+ static_cast<size_type>(self._matrix[vertex_idx, vertex_idx] != invalid_id);
202206
}
203207

include/gl/types/flat_jagged_vector.hpp

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#pragma once
66

77
#include <concepts>
8+
#include <cstddef>
89
#include <cstdint>
910
#include <format>
1011
#include <initializer_list>
@@ -29,6 +30,7 @@ namespace gl {
2930
/// @todo Implement assign, and swap methods.
3031
/// @todo Implement iterator-based insert, emplace and erase methods.
3132
/// @todo Add `operator<<` overload for `std::ostream` and specialize `std::formatter`.
33+
/// @todo Use `std::ptrdiff_t` instead of `std::size_t` for offset values.
3234
template <std::semiregular T>
3335
class flat_jagged_vector {
3436
public:
@@ -903,23 +905,24 @@ class flat_jagged_vector {
903905
requires std::convertible_to<std::ranges::range_reference_t<R>, value_type>
904906
void insert(size_type pos, R&& r) {
905907
const auto beg = this->_offsets[pos];
908+
const auto beg_pos = static_cast<std::ptrdiff_t>(beg);
906909
const auto old_size = this->_data.size();
907910

908911
this->_ensure_offset_capacity();
909912

910913
if constexpr (std::ranges::contiguous_range<R>) {
911914
auto* ptr = std::ranges::data(r);
912915
const auto n = std::ranges::size(r);
913-
this->_data.insert(this->_data.begin() + beg, ptr, ptr + n);
916+
this->_data.insert(this->_data.begin() + beg_pos, ptr, ptr + n);
914917
}
915918
else {
916919
this->_data.insert(
917-
this->_data.begin() + beg, std::ranges::begin(r), std::ranges::end(r)
920+
this->_data.begin() + beg_pos, std::ranges::begin(r), std::ranges::end(r)
918921
);
919922
}
920923

921924
const auto inserted = this->_data.size() - old_size;
922-
this->_offsets.insert(this->_offsets.begin() + pos, beg);
925+
this->_offsets.insert(this->_offsets.begin() + static_cast<std::ptrdiff_t>(pos), beg);
923926
for (size_type i = pos + 1uz; i < this->_offsets.size(); i++)
924927
this->_offsets[i] += inserted;
925928
}
@@ -949,12 +952,12 @@ class flat_jagged_vector {
949952
/// the erased segment in the underlying vector, $S$ is the number of segments after `pos`,
950953
/// and $L$ is the size of the erased segment. Erasing the **last** segment is $O(L)$.
951954
void erase(size_type pos) {
952-
const auto start = this->_offsets[pos];
953-
const auto end = this->_offsets[pos + 1uz];
954-
const auto len = end - start;
955+
const auto start = static_cast<std::ptrdiff_t>(this->_offsets[pos]);
956+
const auto end = static_cast<std::ptrdiff_t>(this->_offsets[pos + 1uz]);
957+
const auto len = static_cast<size_type>(end - start);
955958

956959
this->_data.erase(this->_data.begin() + start, this->_data.begin() + end);
957-
this->_offsets.erase(this->_offsets.begin() + pos);
960+
this->_offsets.erase(this->_offsets.begin() + static_cast<std::ptrdiff_t>(pos));
958961
for (size_type i = pos; i < this->_offsets.size(); i++)
959962
this->_offsets[i] -= len;
960963
}
@@ -1019,7 +1022,8 @@ class flat_jagged_vector {
10191022
/// @note **Time Complexity:** Amortized $O(E + S)$ where $E$ is the number of elements after
10201023
/// the insertion point in the underlying vector, and $S$ is the number of segments after `seg`.
10211024
void insert(size_type seg, size_type pos, const value_type& value) {
1022-
this->_data.insert(this->_data.begin() + this->_offsets[seg] + pos, value);
1025+
const auto insert_pos = static_cast<std::ptrdiff_t>(this->_offsets[seg] + pos);
1026+
this->_data.insert(this->_data.begin() + insert_pos, value);
10231027
for (size_type i = seg + 1uz; i < this->_offsets.size(); i++)
10241028
this->_offsets[i]++;
10251029
}
@@ -1037,9 +1041,8 @@ class flat_jagged_vector {
10371041
/// the insertion point in the underlying vector, and $S$ is the number of segments after `seg`.
10381042
template <class... Args>
10391043
void emplace(size_type seg, size_type pos, Args&&... args) {
1040-
this->_data.emplace(
1041-
this->_data.begin() + this->_offsets[seg] + pos, std::forward<Args>(args)...
1042-
);
1044+
const auto insert_pos = static_cast<std::ptrdiff_t>(this->_offsets[seg] + pos);
1045+
this->_data.emplace(this->_data.begin() + insert_pos, std::forward<Args>(args)...);
10431046
for (size_type i = seg + 1uz; i < this->_offsets.size(); i++)
10441047
this->_offsets[i]++;
10451048
}
@@ -1052,7 +1055,8 @@ class flat_jagged_vector {
10521055
/// @note **Time Complexity:** $O(E + S)$ where $E$ is the number of elements after the erased
10531056
/// position in the underlying vector, and $S$ is the number of segments after `seg`.
10541057
void erase(size_type seg, size_type pos) {
1055-
this->_data.erase(this->_data.begin() + this->_offsets[seg] + pos);
1058+
const auto erase_pos = static_cast<std::ptrdiff_t>(this->_offsets[seg] + pos);
1059+
this->_data.erase(this->_data.begin() + erase_pos);
10561060
for (size_type i = seg + 1uz; i < this->_offsets.size(); i++)
10571061
this->_offsets[i]--;
10581062
}
@@ -1076,16 +1080,16 @@ class flat_jagged_vector {
10761080
const auto curr_count = this->segment_size(seg);
10771081
if (n < curr_count) {
10781082
const auto diff = curr_count - n;
1079-
const auto start = this->_offsets[seg] + n;
1080-
const auto end = this->_offsets[seg + 1uz];
1083+
const auto start = static_cast<std::ptrdiff_t>(this->_offsets[seg] + n);
1084+
const auto end = static_cast<std::ptrdiff_t>(this->_offsets[seg + 1uz]);
10811085

10821086
this->_data.erase(this->_data.begin() + start, this->_data.begin() + end);
10831087
for (size_type i = seg + 1uz; i < this->_offsets.size(); i++)
10841088
this->_offsets[i] -= diff;
10851089
}
10861090
else if (n > curr_count) {
10871091
const auto diff = n - curr_count;
1088-
const auto pos = this->_offsets[seg + 1uz];
1092+
const auto pos = static_cast<std::ptrdiff_t>(this->_offsets[seg + 1uz]);
10891093

10901094
this->_data.insert(this->_data.begin() + pos, diff, value_type());
10911095
for (size_type i = seg + 1uz; i < this->_offsets.size(); i++)
@@ -1113,16 +1117,16 @@ class flat_jagged_vector {
11131117
const auto curr_count = this->segment_size(seg);
11141118
if (n < curr_count) {
11151119
const auto diff = curr_count - n;
1116-
const auto start = this->_offsets[seg] + n;
1117-
const auto end = this->_offsets[seg + 1uz];
1120+
const auto start = static_cast<std::ptrdiff_t>(this->_offsets[seg] + n);
1121+
const auto end = static_cast<std::ptrdiff_t>(this->_offsets[seg + 1uz]);
11181122

11191123
this->_data.erase(this->_data.begin() + start, this->_data.begin() + end);
11201124
for (size_type i = seg + 1uz; i < this->_offsets.size(); i++)
11211125
this->_offsets[i] -= diff;
11221126
}
11231127
else if (n > curr_count) {
11241128
const auto diff = n - curr_count;
1125-
const auto pos = this->_offsets[seg + 1uz];
1129+
const auto pos = static_cast<std::ptrdiff_t>(this->_offsets[seg + 1uz]);
11261130

11271131
this->_data.insert(this->_data.begin() + pos, diff, value);
11281132
for (size_type i = seg + 1uz; i < this->_offsets.size(); i++)

0 commit comments

Comments
 (0)