Skip to content

Commit 112a859

Browse files
authored
NONE: Testing compilation flags fix
- Fixed settings compilation flags for testing targets - Aligned the implementation to satisfy all warnings - Added missing tests - Implemented a basic `concat_view` structure in order to work around the GCC's false-positive warnings
1 parent 2ad5a98 commit 112a859

35 files changed

Lines changed: 743 additions & 289 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/graph.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ class graph final {
212212
}
213213
}
214214

215-
gl_attr_force_inline void remove_vertex(const size_type vertex_id) {
215+
gl_attr_force_inline void remove_vertex(const id_type vertex_id) {
216216
this->_verify_vertex_id(vertex_id);
217217
this->_remove_vertex_impl(vertex_id);
218218
}

include/gl/impl/adjacency_list.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ class adjacency_list final {
297297
edge_item.edge_id = invalid_id; // edge was removed
298298
else
299299
// shift by the number of removed IDs < edge-id
300-
edge_item.edge_id -= std::ranges::distance(removed_edge_ids.begin(), it);
300+
edge_item.edge_id -= static_cast<id_type>(it - removed_edge_ids.begin());
301301

302302
// align the vertex id
303303
edge_item.vertex_id -= edge_item.vertex_id > removed_vertex_id;

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: 23 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,10 @@ 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+
const auto vertex_pos = static_cast<std::ptrdiff_t>(vertex_id);
281+
self._matrix.erase(self._matrix.begin() + vertex_pos);
268282
for (auto& row : self._matrix)
269-
row.erase(std::next(std::begin(row), vertex_idx));
283+
row.erase(row.begin() + vertex_pos);
270284

271285
return removed_edges;
272286
}

include/gl/impl/specialized/flat_adjacency_list.hpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@ struct directed_flat_adjacency_list {
4242
}
4343

4444
[[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);
45+
return static_cast<size_type>(
46+
std::ranges::count(self._list.data_view(), vertex_id, &item_type::vertex_id)
47+
);
4648
}
4749

4850
[[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/topology/binary_tree.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
#include "gl/constants.hpp"
88
#include "gl/conversion.hpp"
99
#include "gl/graph.hpp"
10-
#include "gl/util/pow.hpp"
10+
#include "gl/util/math.hpp"
1111

1212
#include <initializer_list>
1313

include/gl/topology/cycle.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ template <traits::c_graph GraphType>
1717
GraphType graph{n_vertices};
1818

1919
for (id_type source_id = initial_id; source_id < n_vertices; ++source_id)
20-
graph.add_edge(source_id, (source_id + 1uz) % n_vertices);
20+
graph.add_edge(source_id, static_cast<id_type>((source_id + 1uz) % n_vertices));
2121

2222
return graph;
2323
}
@@ -36,7 +36,7 @@ template <traits::c_graph GraphType>
3636
GraphType graph{n_vertices};
3737

3838
for (id_type source_id = initial_id; source_id < n_vertices; ++source_id) {
39-
const auto target_id = (source_id + 1uz) % n_vertices;
39+
const auto target_id = static_cast<id_type>((source_id + 1uz) % n_vertices);
4040
graph.add_edge(source_id, target_id);
4141
graph.add_edge(target_id, source_id);
4242
}

0 commit comments

Comments
 (0)