Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions include/gl/attributes/diagnostics.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright (c) 2024-2026 Jakub Musiał
// This file is part of the CPP-GL project (https://github.com/SpectraL519/cpp-gl).
// Licensed under the MIT License. See the LICENSE file in the project root for full license information.

#pragma once

#define GL_PRAGMA(x) _Pragma(#x)

#if defined(__clang__)

#define GL_SUPPRESS_WARNING_BEGIN(w) \
GL_PRAGMA(clang diagnostic push) \
GL_PRAGMA(clang diagnostic ignored w)

#define GL_SUPPRESS_WARNING_END GL_PRAGMA(clang diagnostic pop)

#define GL_SUPPRESS_CLANG_WARNING_BEGIN(w) GL_SUPPRESS_WARNING_BEGIN(w)

// GCC-only warning: Push on Clang to keep the END macro balanced, but don't ignore
#define GL_SUPPRESS_GCC_WARNING_BEGIN(w) GL_PRAGMA(clang diagnostic push)

#elif defined(__GNUC__)

#define GL_SUPPRESS_WARNING_BEGIN(w) \
GL_PRAGMA(GCC diagnostic push) \
GL_PRAGMA(GCC diagnostic ignored w)

#define GL_SUPPRESS_WARNING_END GL_PRAGMA(GCC diagnostic pop)

#define GL_SUPPRESS_GCC_WARNING_BEGIN(w) GL_SUPPRESS_WARNING_BEGIN(w)

// Clang-only warning: Push on GCC to keep the END macro balanced, but don't ignore
#define GL_SUPPRESS_CLANG_WARNING_BEGIN(w) GL_PRAGMA(GCC diagnostic push)

#else

#define GL_SUPPRESS_WARNING_BEGIN(w)
#define GL_SUPPRESS_WARNING_END
#define GL_SUPPRESS_GCC_WARNING_BEGIN(w)
#define GL_SUPPRESS_CLANG_WARNING_BEGIN(w)

#endif
2 changes: 1 addition & 1 deletion include/gl/graph.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ class graph final {
}
}

gl_attr_force_inline void remove_vertex(const size_type vertex_id) {
gl_attr_force_inline void remove_vertex(const id_type vertex_id) {
this->_verify_vertex_id(vertex_id);
this->_remove_vertex_impl(vertex_id);
}
Expand Down
2 changes: 1 addition & 1 deletion include/gl/impl/adjacency_list.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ class adjacency_list final {
edge_item.edge_id = invalid_id; // edge was removed
else
// shift by the number of removed IDs < edge-id
edge_item.edge_id -= std::ranges::distance(removed_edge_ids.begin(), it);
edge_item.edge_id -= static_cast<id_type>(it - removed_edge_ids.begin());

// align the vertex id
edge_item.vertex_id -= edge_item.vertex_id > removed_vertex_id;
Expand Down
10 changes: 7 additions & 3 deletions include/gl/impl/specialized/adjacency_list.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
#include "gl/decl/impl_tags.hpp"
#include "gl/graph_traits.hpp"
#include "gl/traits.hpp"
#include "gl/types/core.hpp"

#include <algorithm>
#include <cstddef>
#include <format>
#include <iostream>
#include <ranges>
Expand Down Expand Up @@ -77,7 +79,9 @@ struct directed_adjacency_list {
[[nodiscard]] static size_type in_degree(const impl_type& self, id_type vertex_id) {
size_type in_deg = 0uz;
for (const auto& adjacent_edges : self._list)
in_deg += std::ranges::count(adjacent_edges, vertex_id, &item_type::vertex_id);
in_deg += static_cast<size_type>(
std::ranges::count(adjacent_edges, vertex_id, &item_type::vertex_id)
);

return in_deg;
}
Expand Down Expand Up @@ -150,7 +154,7 @@ struct directed_adjacency_list {
}

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

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

Expand Down
32 changes: 23 additions & 9 deletions include/gl/impl/specialized/adjacency_matrix.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@

#pragma once

#include "gl/attributes/diagnostics.hpp"
#include "gl/constants.hpp"
#include "gl/decl/impl_tags.hpp"
#include "gl/graph_traits.hpp"
#include "gl/types/core.hpp"

#include <algorithm>
#include <cstddef>
#include <vector>

namespace gl::impl {
Expand Down Expand Up @@ -76,19 +78,28 @@ struct directed_adjacency_matrix {
self._matrix.emplace_back(new_n_vertices, invalid_id);
}

GL_SUPPRESS_WARNING_BEGIN("-Wsign-conversion")

// NOTE: Indexing into a row which might be a vector (requires size type) or a span/subrange (requires difference type)

[[nodiscard]] gl_attr_force_inline static size_type in_degree(
const impl_type& self, id_type vertex_id
) {
return std::ranges::count_if(self._matrix, [vertex_id](const auto& row) {
return row[to_idx(vertex_id)] != invalid_id;
});
return static_cast<size_type>(std::ranges::count_if(
self._matrix,
[vertex_id](const auto& row) { return row[to_idx(vertex_id)] != invalid_id; }
));
}

GL_SUPPRESS_WARNING_END

[[nodiscard]] gl_attr_force_inline static size_type out_degree(
const impl_type& self, id_type vertex_id
) {
return self._matrix[vertex_id].size()
- std::ranges::count(self._matrix[vertex_id], invalid_id_v<id_type>);
- static_cast<size_type>(
std::ranges::count(self._matrix[vertex_id], invalid_id_v<id_type>)
);
}

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

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

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

return removed_edges;
Expand Down Expand Up @@ -225,7 +236,9 @@ struct undirected_adjacency_matrix {
) {
const auto vertex_idx = to_idx(vertex_id);
return self._matrix.size()
- std::ranges::count(self._matrix[vertex_idx], invalid_id_v<id_type>)
- static_cast<size_type>(
std::ranges::count(self._matrix[vertex_idx], invalid_id_v<id_type>)
)
+ static_cast<size_type>(self._matrix[vertex_idx][vertex_idx] != invalid_id);
}

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

self._matrix.erase(std::next(std::begin(self._matrix), vertex_idx));
const auto vertex_pos = static_cast<std::ptrdiff_t>(vertex_id);
self._matrix.erase(self._matrix.begin() + vertex_pos);
for (auto& row : self._matrix)
row.erase(std::next(std::begin(row), vertex_idx));
row.erase(row.begin() + vertex_pos);

return removed_edges;
}
Expand Down
4 changes: 3 additions & 1 deletion include/gl/impl/specialized/flat_adjacency_list.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ struct directed_flat_adjacency_list {
}

[[nodiscard]] static size_type in_degree(const impl_type& self, id_type vertex_id) {
return std::ranges::count(self._list.data_view(), vertex_id, &item_type::vertex_id);
return static_cast<size_type>(
std::ranges::count(self._list.data_view(), vertex_id, &item_type::vertex_id)
);
}

[[nodiscard]] gl_attr_force_inline static size_type out_degree(
Expand Down
18 changes: 11 additions & 7 deletions include/gl/impl/specialized/flat_adjacency_matrix.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "gl/types/flat_matrix.hpp"

#include <algorithm>
#include <cstddef>
#include <format>
#include <ranges>
#include <vector>
Expand Down Expand Up @@ -45,16 +46,16 @@ struct directed_flat_adjacency_matrix {
[[nodiscard]] gl_attr_force_inline static size_type in_degree(
const impl_type& self, id_type vertex_id
) {
return std::ranges::count_if(self._matrix.col(to_idx(vertex_id)), [](auto edge_id) {
return edge_id != invalid_id;
});
return static_cast<size_type>(std::ranges::count_if(
self._matrix.col(to_idx(vertex_id)), [](auto edge_id) { return edge_id != invalid_id; }
));
}

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

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

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

return deg;
}
Expand Down Expand Up @@ -119,7 +120,9 @@ struct directed_flat_adjacency_matrix {
for (auto r_idx = 0uz; r_idx < self._matrix.n_rows(); ++r_idx) {
if (r_idx == vertex_idx)
continue;
if (const auto edge_id = col[r_idx]; edge_id != invalid_id)

const auto edge_id = col[static_cast<std::ptrdiff_t>(r_idx)];
if (edge_id != invalid_id)
removed_edges.push_back(edge_id);
}

Expand Down Expand Up @@ -197,7 +200,8 @@ struct undirected_flat_adjacency_matrix {
) {
const auto vertex_idx = to_idx(vertex_id);
const auto row = self._matrix[vertex_idx];
return self._matrix.n_cols() - std::ranges::count(row, invalid_id_v<id_type>)
return self._matrix.n_cols()
- static_cast<size_type>(std::ranges::count(row, invalid_id_v<id_type>))
+ static_cast<size_type>(self._matrix[vertex_idx, vertex_idx] != invalid_id);
}

Expand Down
2 changes: 1 addition & 1 deletion include/gl/topology/binary_tree.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include "gl/constants.hpp"
#include "gl/conversion.hpp"
#include "gl/graph.hpp"
#include "gl/util/pow.hpp"
#include "gl/util/math.hpp"

#include <initializer_list>

Expand Down
4 changes: 2 additions & 2 deletions include/gl/topology/cycle.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ template <traits::c_graph GraphType>
GraphType graph{n_vertices};

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

return graph;
}
Expand All @@ -36,7 +36,7 @@ template <traits::c_graph GraphType>
GraphType graph{n_vertices};

for (id_type source_id = initial_id; source_id < n_vertices; ++source_id) {
const auto target_id = (source_id + 1uz) % n_vertices;
const auto target_id = static_cast<id_type>((source_id + 1uz) % n_vertices);
graph.add_edge(source_id, target_id);
graph.add_edge(target_id, source_id);
}
Expand Down
Loading
Loading