Skip to content

Commit 33361b1

Browse files
committed
gl: contiguous alg containers
1 parent 0027f14 commit 33361b1

3 files changed

Lines changed: 29 additions & 18 deletions

File tree

include/gl/algorithm/templates/bfs.hpp

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
#include "gl/algorithm/traits.hpp"
99
#include "gl/algorithm/util.hpp"
1010

11-
#include <queue>
11+
#include <vector>
1212

1313
namespace gl::algorithm {
1414

@@ -35,14 +35,16 @@ bool bfs(
3535
return false;
3636

3737
// prepare the node queue
38-
std::queue<search_node<G>> q;
39-
for (const auto& node : initial_queue_content)
40-
q.push(node);
38+
std::vector<search_node<G>> q;
39+
q.reserve(graph.n_vertices());
40+
q.insert(
41+
q.end(), std::ranges::begin(initial_queue_content), std::ranges::end(initial_queue_content)
42+
);
43+
std::size_t q_head = 0uz;
4144

4245
// search the graph
43-
while (not q.empty()) {
44-
const search_node node = q.front();
45-
q.pop();
46+
while (q_head < q.size()) {
47+
const search_node node = q[q_head++];
4648

4749
if constexpr (not traits::c_empty_callback<VisitVertexPredicate>)
4850
if (not visit_vertex_pred(node.vertex_id))
@@ -57,11 +59,13 @@ bool bfs(
5759

5860
for (const auto& edge : graph.out_edges(node.vertex_id)) {
5961
const auto target_vertex_id = edge.other(node.vertex_id);
62+
6063
const auto enqueue = enqueue_vertex_pred(target_vertex_id, edge);
6164
if (enqueue == decision::abort)
6265
return false;
66+
6367
if (enqueue)
64-
q.emplace(target_vertex_id, node.vertex_id);
68+
q.emplace_back(target_vertex_id, node.vertex_id);
6569
}
6670

6771
if constexpr (not traits::c_empty_callback<PostVisitCallback>)

include/gl/algorithm/templates/dfs.hpp

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
#include "gl/algorithm/traits.hpp"
99
#include "gl/algorithm/util.hpp"
1010

11-
#include <stack>
11+
#include <vector>
1212

1313
namespace gl::algorithm {
1414

@@ -35,14 +35,16 @@ bool dfs(
3535
return false;
3636

3737
// prepare the node stack
38-
std::stack<search_node<G>> s;
39-
for (const auto& node : initial_stack_content)
40-
s.push(node);
38+
std::vector<search_node<G>> s;
39+
s.reserve(graph.n_vertices());
40+
s.insert(
41+
s.end(), std::ranges::begin(initial_stack_content), std::ranges::end(initial_stack_content)
42+
);
4143

4244
// search the graph
4345
while (not s.empty()) {
44-
const auto node = s.top();
45-
s.pop();
46+
const auto node = s.back();
47+
s.pop_back();
4648

4749
if constexpr (not traits::c_empty_callback<VisitVertexPredicate>)
4850
if (not visit_vertex_pred(node.vertex_id))
@@ -57,8 +59,13 @@ bool dfs(
5759

5860
for (const auto& edge : graph.out_edges(node.vertex_id)) {
5961
const auto target_vertex_id = edge.other(node.vertex_id);
60-
if (enqueue_vertex_pred(target_vertex_id, edge))
61-
s.emplace(target_vertex_id, node.vertex_id);
62+
63+
const auto enqueue = enqueue_vertex_pred(target_vertex_id, edge);
64+
if (enqueue == decision::abort)
65+
return false;
66+
67+
if (enqueue)
68+
s.emplace_back(target_vertex_id, node.vertex_id);
6269
}
6370

6471
if constexpr (not traits::c_empty_callback<PostVisitCallback>)

include/gl/algorithm/traversal/depth_first_search.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ return_type<Result, predecessors_map<G>> depth_first_search(
3535
init_range<G>(root_vertex_id),
3636
default_visit_vertex_predicate(visited),
3737
default_visit_callback<G, Result>(visited, pred_map),
38-
default_enqueue_vertex_predicate<G>(visited),
38+
default_enqueue_vertex_predicate<G, true>(visited),
3939
pre_visit,
4040
post_visit
4141
);
@@ -47,7 +47,7 @@ return_type<Result, predecessors_map<G>> depth_first_search(
4747
init_range<G>(root_id),
4848
default_visit_vertex_predicate(visited),
4949
default_visit_callback<G, Result>(visited, pred_map),
50-
default_enqueue_vertex_predicate<G>(visited),
50+
default_enqueue_vertex_predicate<G, true>(visited),
5151
pre_visit,
5252
post_visit
5353
);

0 commit comments

Comments
 (0)