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
4 changes: 1 addition & 3 deletions include/gl/algorithm/pathfinding/dijkstra.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ template <
empty_callback{}, // visit callback
[&paths, &negative_edge](id_type vertex_id, const edge_type& in_edge)
-> decision { // enqueue predicate
const auto pred_id = in_edge.incident_vertex(vertex_id);
const auto pred_id = in_edge.other(vertex_id);

const auto edge_weight = get_weight<G>(in_edge);
if (edge_weight < 0) {
Expand Down Expand Up @@ -117,10 +117,8 @@ template <traits::c_id_type IdType, traits::c_random_access_range_of<IdType> IdR
while (true) {
path.push_front(current_vertex);
IdType predecessor = predecessor_map[to_idx(current_vertex)];

if (predecessor == current_vertex)
break;

current_vertex = predecessor;
}

Expand Down
20 changes: 10 additions & 10 deletions include/gl/algorithm/spanning_tree/prim_mst.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@ template <traits::c_undirected_graph G>
std::vector<bool> visited(n_vertices, false);
queue_type edge_queue;

// insert the edges adjacent to the root vertex to the queue
// insert the edges incident with the root vertex to the queue
if (root_id == invalid_id)
root_id = initial_id;

for (const auto& edge : graph.adjacent_edges(root_id))
for (const auto& edge : graph.incident_edges(root_id))
edge_queue.emplace(edge);

// mark the root vertex as visited
Expand All @@ -74,9 +74,9 @@ template <traits::c_undirected_graph G>
visited[min_edge_tgt] = true;
++n_vertices_in_mst;

// enqueue all edges adjacent to the `target` vertex if they lead to unvisited verties
for (const auto& edge : graph.adjacent_edges(min_edge.target()))
if (not visited[to_idx(edge.incident_vertex(min_edge.target()))])
// enqueue all edges incident with the `target` vertex if they lead to unvisited verties
for (const auto& edge : graph.incident_edges(min_edge.target()))
if (not visited[to_idx(edge.other(min_edge.target()))])
edge_queue.emplace(edge);
}

Expand Down Expand Up @@ -132,13 +132,13 @@ requires traits::c_has_numeric_limits_max<vertex_distance_type<G>>
}

// Update adjacent vertices
for (const auto& edge : graph.adjacent_edges(vertex_id)) {
for (const auto& edge : graph.incident_edges(vertex_id)) {
const auto edge_weight = get_weight<G>(edge);
const auto incident_vertex_idx = to_idx(edge.incident_vertex(vertex_id));
const auto target_vertex_idx = to_idx(edge.other(vertex_id));

if (not in_mst[incident_vertex_idx] and edge_weight < min_cost[incident_vertex_idx]) {
min_cost[incident_vertex_idx] = edge_weight;
min_cost_edges[incident_vertex_idx].emplace(edge);
if (not in_mst[target_vertex_idx] and edge_weight < min_cost[target_vertex_idx]) {
min_cost[target_vertex_idx] = edge_weight;
min_cost_edges[target_vertex_idx].emplace(edge);
}
}

Expand Down
10 changes: 4 additions & 6 deletions include/gl/algorithm/templates/bfs.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,13 @@ bool bfs(
if (not visit(node.vertex_id, node.pred_id))
return false;

for (const auto& edge : graph.adjacent_edges(node.vertex_id)) {
const auto incident_vertex_id = edge.incident_vertex(node.vertex_id);

const auto enqueue = enqueue_vertex_pred(incident_vertex_id, edge);
for (const auto& edge : graph.incident_edges(node.vertex_id)) {
const auto target_vertex_id = edge.other(node.vertex_id);
const auto enqueue = enqueue_vertex_pred(target_vertex_id, edge);
if (enqueue == decision::abort)
return false;

if (enqueue)
q.emplace(incident_vertex_id, node.vertex_id);
q.emplace(target_vertex_id, node.vertex_id);
}

if constexpr (not traits::c_empty_callback<PostVisitCallback>)
Expand Down
16 changes: 8 additions & 8 deletions include/gl/algorithm/templates/dfs.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@ bool dfs(
if (not visit(node.vertex_id, node.pred_id))
return false;

for (const auto& edge : graph.adjacent_edges(node.vertex_id)) {
const auto incident_vertex_id = edge.incident_vertex(node.vertex_id);
if (enqueue_vertex_pred(incident_vertex_id, edge))
s.emplace(incident_vertex_id, node.vertex_id);
for (const auto& edge : graph.incident_edges(node.vertex_id)) {
const auto target_vertex_id = edge.other(node.vertex_id);
if (enqueue_vertex_pred(target_vertex_id, edge))
s.emplace(target_vertex_id, node.vertex_id);
}

if constexpr (not traits::c_empty_callback<PostVisitCallback>)
Expand Down Expand Up @@ -96,12 +96,12 @@ void r_dfs(
visit(vertex_id, pred_id);

// recursively search vertices adjacent to the current vertex
for (const auto& edge : graph.adjacent_edges(vertex_id)) {
const auto& incident_vertex_id = edge.incident_vertex(vertex_id);
if (enqueue_vertex_pred(incident_vertex_id, edge))
for (const auto& edge : graph.incident_edges(vertex_id)) {
const auto target_vertex_id = edge.other(vertex_id);
if (enqueue_vertex_pred(target_vertex_id, edge))
r_dfs(
graph,
incident_vertex_id,
target_vertex_id,
vertex_id,
visit_vertex_pred,
visit,
Expand Down
10 changes: 4 additions & 6 deletions include/gl/algorithm/templates/pfs.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,13 @@ bool pfs(
if (not visit(node.vertex_id, node.pred_id))
return false;

for (const auto& edge : graph.adjacent_edges(node.vertex_id)) {
const auto incident_vertex_id = edge.incident_vertex(node.vertex_id);

const auto enqueue = enqueue_vertex_pred(incident_vertex_id, edge);
for (const auto& edge : graph.incident_edges(node.vertex_id)) {
const auto target_vertex_id = edge.other(node.vertex_id);
const auto enqueue = enqueue_vertex_pred(target_vertex_id, edge);
if (enqueue == decision::abort)
return false;

if (enqueue)
q.emplace(incident_vertex_id, node.vertex_id);
q.emplace(target_vertex_id, node.vertex_id);
}
if constexpr (not traits::c_empty_callback<PostVisitCallback>)
post_visit(node.vertex_id);
Expand Down
2 changes: 1 addition & 1 deletion include/gl/algorithm/topology/coloring.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ template <
if (in_edge.is_loop())
return decision::abort; // graph is not bipartite

const auto pred_id = in_edge.incident_vertex(vertex_id);
const auto pred_id = in_edge.other(vertex_id);

auto& v_color = coloring[to_idx(vertex_id)];
auto p_color = coloring[to_idx(pred_id)];
Expand Down
12 changes: 6 additions & 6 deletions include/gl/conversion.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,14 @@ struct to_impl<impl::flat_list_t, impl::list_t> {
auto& source_list = source._impl._list;

std::size_t total_items = 0uz;
for (const auto& adj : source_list)
total_items += adj.size();
for (const auto& inc : source_list)
total_items += inc.size();

target_list.reserve_segments(source_list.size());
target_list.reserve_data(total_items);

for (auto& adj : source_list)
target_list.push_back(std::move(adj));
for (auto& inc : source_list)
target_list.push_back(std::move(inc));
}
};

Expand All @@ -106,8 +106,8 @@ struct to_impl<impl::list_t, impl::flat_list_t> {
auto& source_list = source._impl._list;

target_list.reserve(source_list.size());
for (auto adj : source_list)
target_list.emplace_back(adj.begin(), adj.end());
for (auto inc : source_list)
target_list.emplace_back(inc.begin(), inc.end());
}
};

Expand Down
3 changes: 1 addition & 2 deletions include/gl/edge_descriptor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,7 @@ class edge_descriptor final {

// clang-format on

// returns the `other` vertex or throws error if the given vertex is not incident with the edge
[[nodiscard]] const id_type incident_vertex(const id_type vertex_id) const {
[[nodiscard]] const id_type other(const id_type vertex_id) const {
if (vertex_id == this->_vertices.first)
return this->_vertices.second;

Expand Down
22 changes: 10 additions & 12 deletions include/gl/graph.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -293,18 +293,16 @@ class graph final {
return this->at(vertex.id());
}

// TODO: rename to incident_edges
[[nodiscard]] inline auto adjacent_edges(const id_type vertex_id) const {
[[nodiscard]] inline auto incident_edges(const id_type vertex_id) const {
this->_verify_vertex_id(vertex_id);
if constexpr (traits::c_non_empty_properties<edge_properties_type>)
return this->_impl.adjacent_edges(vertex_id, this->_edge_properties);
return this->_impl.incident_edges(vertex_id, this->_edge_properties);
else
return this->_impl.adjacent_edges(vertex_id);
return this->_impl.incident_edges(vertex_id);
}

// TODO: rename to incident_edges
[[nodiscard]] gl_attr_force_inline auto adjacent_edges(const vertex_type& vertex) const {
return this->adjacent_edges(vertex.id());
[[nodiscard]] gl_attr_force_inline auto incident_edges(const vertex_type& vertex) const {
return this->incident_edges(vertex.id());
}

[[nodiscard]] inline auto in_edges(const id_type vertex_id) const {
Expand Down Expand Up @@ -695,8 +693,8 @@ class graph final {
);

for (const auto& vertex : this->vertices()) {
os << "- " << vertex << "\n adjacent edges:\n";
for (const auto& edge : this->adjacent_edges(vertex.id()))
os << "- " << vertex << "\n incident edges:\n";
for (const auto& edge : this->incident_edges(vertex.id()))
os << "\t- " << edge << '\n';
}
}
Expand All @@ -706,7 +704,7 @@ class graph final {

for (const auto& vertex : this->vertices()) {
os << "- " << vertex << " :";
for (const auto& edge : this->adjacent_edges(vertex.id()))
for (const auto& edge : this->incident_edges(vertex.id()))
os << ' ' << edge;
os << '\n';
}
Expand Down Expand Up @@ -736,7 +734,7 @@ class graph final {
if constexpr (traits::c_writable<typename edge_type::properties_type>) {
if (with_edge_properties) {
const auto print_incident_edges = [this, &os](const id_type vertex_id) {
for (const auto& edge : this->adjacent_edges(vertex_id)) {
for (const auto& edge : this->incident_edges(vertex_id)) {
if (edge.source() != vertex_id)
continue; // vertex is not the source
os << edge.source() << ' ' << edge.target() << ' ' << edge.properties()
Expand All @@ -752,7 +750,7 @@ class graph final {
}

const auto print_incident_edges = [this, &os](const id_type vertex_id) {
for (const auto& edge : this->adjacent_edges(vertex_id)) {
for (const auto& edge : this->incident_edges(vertex_id)) {
if (edge.source() != vertex_id)
continue; // vertex is not the source
os << edge.source() << ' ' << edge.target() << '\n';
Expand Down
30 changes: 15 additions & 15 deletions include/gl/impl/adjacency_list.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class adjacency_list final {

using vertex_type = typename GraphTraits::vertex_type;
using edge_type = typename GraphTraits::edge_type;
using item_type = specialized::adjacency_list_item<id_type>;
using item_type = specialized::incidence_item<id_type>;
using adjacency_storage_type = typename specialized::adjacency_list_impl_traits<
adjacency_list>::template storage_type<item_type>;

Expand Down Expand Up @@ -120,9 +120,9 @@ class adjacency_list final {
[[nodiscard]] std::optional<edge_type> get_edge(id_type source_id, id_type target_id) const
requires(traits::c_has_empty_properties<edge_type>)
{
const auto& adjacent_edges = this->_list[to_idx(source_id)];
const auto item_it = std::ranges::find(adjacent_edges, target_id, &item_type::vertex_id);
if (item_it == adjacent_edges.cend())
const auto& incident_edges = this->_list[to_idx(source_id)];
const auto item_it = std::ranges::find(incident_edges, target_id, &item_type::vertex_id);
if (item_it == incident_edges.cend())
return std::nullopt;
return std::make_optional<edge_type>(item_it->edge_id, source_id, target_id);
}
Expand All @@ -132,11 +132,11 @@ class adjacency_list final {
) const
requires(traits::c_has_non_empty_properties<edge_type>)
{
const auto& adjacent_edges = this->_list[to_idx(source_id)];
const auto item_it = std::ranges::find(adjacent_edges, target_id, [](const auto& item) {
const auto& incident_edges = this->_list[to_idx(source_id)];
const auto item_it = std::ranges::find(incident_edges, target_id, [](const auto& item) {
return item.vertex_id;
});
if (item_it == adjacent_edges.cend())
if (item_it == incident_edges.cend())
return std::nullopt;
return std::make_optional<edge_type>(
item_it->edge_id, source_id, target_id, *edge_properties_map[to_idx(item_it->edge_id)]
Expand Down Expand Up @@ -173,8 +173,8 @@ class adjacency_list final {

gl_attr_force_inline void remove_edge(const edge_type& edge) {
specialized_impl::remove_edge(*this, edge);
for (auto&& adj : this->_list)
for (auto& item : adj)
for (auto&& inc : this->_list)
for (auto& item : inc)
item.edge_id -= static_cast<id_type>(item.edge_id > edge.id());
}

Expand All @@ -188,13 +188,13 @@ class adjacency_list final {
return removed_edge_ids;
}

[[nodiscard]] gl_attr_force_inline auto adjacent_edges(id_type vertex_id) const
[[nodiscard]] gl_attr_force_inline auto incident_edges(id_type vertex_id) const
requires(traits::c_has_empty_properties<edge_type>)
{
return this->out_edges(vertex_id);
}

[[nodiscard]] gl_attr_force_inline auto adjacent_edges(
[[nodiscard]] gl_attr_force_inline auto incident_edges(
id_type vertex_id, const auto& edge_properties_map
) const
requires(traits::c_has_non_empty_properties<edge_type>)
Expand Down Expand Up @@ -257,14 +257,14 @@ class adjacency_list final {
[[nodiscard]] gl_attr_force_inline auto at(id_type vertex_id) const
requires(traits::c_has_empty_properties<edge_type>)
{
return this->adjacent_edges(vertex_id);
return this->incident_edges(vertex_id);
}

[[nodiscard]] gl_attr_force_inline auto at(id_type vertex_id, const auto& edge_properties_map)
const
requires(traits::c_has_non_empty_properties<edge_type>)
{
return this->adjacent_edges(vertex_id, edge_properties_map);
return this->incident_edges(vertex_id, edge_properties_map);
}

// --- comparison ---
Expand All @@ -290,8 +290,8 @@ class adjacency_list final {
std::ranges::unique(removed_edge_ids).begin(), removed_edge_ids.end()
);

for (auto&& adj : this->_list) {
for (auto& edge_item : adj) {
for (auto&& inc : this->_list) {
for (auto& edge_item : inc) {
auto it = std::ranges::lower_bound(removed_edge_ids, edge_item.edge_id);
if (it != removed_edge_ids.end() and *it == edge_item.edge_id)
edge_item.edge_id = invalid_id; // edge was removed
Expand Down
4 changes: 2 additions & 2 deletions include/gl/impl/adjacency_matrix.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -178,13 +178,13 @@ class adjacency_matrix final {
return removed_edge_ids;
}

[[nodiscard]] gl_attr_force_inline auto adjacent_edges(id_type vertex_id) const
[[nodiscard]] gl_attr_force_inline auto incident_edges(id_type vertex_id) const
requires(traits::c_has_empty_properties<edge_type>)
{
return this->out_edges(vertex_id);
}

[[nodiscard]] gl_attr_force_inline auto adjacent_edges(
[[nodiscard]] gl_attr_force_inline auto incident_edges(
id_type vertex_id, const auto& edge_properties_map
) const
requires(traits::c_has_non_empty_properties<edge_type>)
Expand Down
Loading
Loading