Skip to content

Commit c2f029e

Browse files
committed
Implement container selector infrastructure (Phase 3.2.1)
Add complete container selector system for adjacency_list with support for all standard container types: New files: - container_selectors.hpp: All selector tags and container_gen mechanism - test_container_selectors.cpp: Comprehensive tests for all selectors Selectors implemented: - vecS (std::vector) - index-based descriptors - listS (std::list) - stable iterator-based descriptors - setS (std::set) - ordered, unique, stable descriptors - mapS (std::set) - ordered, unique - multisetS (std::multiset) - ordered, allows parallel edges - hash_setS (std::unordered_set) - unordered, unique - hash_mapS (std::unordered_set) - unordered, unique - hash_multisetS (std::unordered_multiset) - parallel edges - hash_multimapS (std::unordered_multiset) - parallel edges adjacency_list changes: - New template signature: <OutEdgeListS, VertexListS, DirectedS, VP, EP, GP> - Implementations for vecS, listS, setS vertex containers - Added simple_adjacency_list alias for backward compatibility - Added std::hash specialization for stored_out_edge All 27 tests pass.
1 parent 14f4e18 commit c2f029e

15 files changed

Lines changed: 2142 additions & 314 deletions

modern/examples/bfs_lambda_visitor.cpp

Lines changed: 47 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
#include <bgl/modern/adjacency_list.hpp>
88
#include <bgl/modern/breadth_first_search.hpp>
9+
#include <bgl/modern/visitor_callbacks.hpp>
910
#include <iostream>
1011
#include <vector>
1112
#include <string>
@@ -21,7 +22,7 @@ int main() {
2122
std::cout << "=== BFS with Lambda Visitors ===\n\n";
2223

2324
// Create a graph representing a directory structure
24-
adjacency_list<directed_tag, VertexProps> g(10);
25+
simple_adjacency_list<directed_tag, VertexProps> g(10);
2526

2627
g[0] = {"root", 0};
2728
g[1] = {"home", 1};
@@ -61,13 +62,11 @@ int main() {
6162
// Example 2: Tree edge tracking
6263
std::cout << "\n2. Tree structure:\n";
6364
{
64-
bfs_callbacks callbacks{
65-
.on_tree_edge = [&g](auto e, const auto& graph) {
66-
auto src = source(e, graph);
67-
auto tgt = target(e, graph);
68-
std::cout << " " << g[src].name << " -> " << g[tgt].name << "\n";
69-
}
70-
};
65+
auto callbacks = on_tree_edge([&g](auto e, const auto& graph) {
66+
auto src = source(e, graph);
67+
auto tgt = target(e, graph);
68+
std::cout << " " << g[src].name << " -> " << g[tgt].name << "\n";
69+
});
7170

7271
breadth_first_search(g, 0, callbacks);
7372
}
@@ -77,11 +76,9 @@ int main() {
7776
{
7877
std::vector<std::vector<std::string>> levels(4);
7978

80-
bfs_callbacks callbacks{
81-
.on_discover_vertex = [&](auto v, const auto&) {
82-
levels[g[v].value].push_back(g[v].name);
83-
}
84-
};
79+
auto callbacks = on_discover_vertex([&](auto v, const auto&) {
80+
levels[g[v].value].push_back(g[v].name);
81+
});
8582

8683
breadth_first_search(g, 0, callbacks);
8784

@@ -100,23 +97,18 @@ int main() {
10097
int edge_count = 0;
10198
int vertex_count = 0;
10299

103-
bfs_callbacks callbacks{
104-
.on_initialize_vertex = [&](auto v, const auto& graph) {
105-
vertex_count++;
106-
},
107-
.on_discover_vertex = [&g](auto v, const auto&) {
108-
std::cout << " → Discovered " << g[v].name << "\n";
109-
},
110-
.on_examine_vertex = [&g](auto v, const auto&) {
111-
std::cout << " ⊙ Examining " << g[v].name << "\n";
112-
},
113-
.on_tree_edge = [&](auto e, const auto& graph) {
114-
edge_count++;
115-
},
116-
.on_finish_vertex = [&g](auto v, const auto&) {
117-
std::cout << " ✓ Finished " << g[v].name << "\n";
118-
}
119-
};
100+
// For multiple callbacks, chain the helper functions
101+
auto callbacks = bfs_callbacks(
102+
[&](auto v, const auto& graph) { vertex_count++; }, // initialize
103+
[&g](auto v, const auto&) { std::cout << " → Discovered " << g[v].name << "\n"; }, // discover
104+
[&g](auto v, const auto&) { std::cout << " ⊙ Examining " << g[v].name << "\n"; }, // examine
105+
null_callback{}, // examine_edge
106+
[&](auto e, const auto& graph) { edge_count++; }, // tree_edge
107+
null_callback{}, // non_tree_edge
108+
null_callback{}, // gray_target
109+
null_callback{}, // black_target
110+
[&g](auto v, const auto&) { std::cout << " ✓ Finished " << g[v].name << "\n"; } // finish
111+
);
120112

121113
breadth_first_search(g, 0, callbacks);
122114

@@ -158,20 +150,34 @@ int main() {
158150
double avg_out_degree = 0.0;
159151
} stats;
160152

161-
bfs_callbacks callbacks{
162-
.on_examine_vertex = [&](auto v, const auto& graph) {
163-
stats.total_vertices++;
164-
auto degree = out_degree(v, graph);
165-
stats.avg_out_degree += degree;
166-
167-
if (degree == 0) {
168-
stats.leaf_nodes++;
169-
} else {
170-
stats.internal_nodes++;
171-
}
153+
// Multiple events require the full bfs_callbacks struct
154+
// We pass all callbacks in constructor order
155+
auto examine_cb = [&](auto v, const auto& graph) {
156+
stats.total_vertices++;
157+
auto degree = out_degree(v, graph);
158+
stats.avg_out_degree += degree;
159+
160+
if (degree == 0) {
161+
stats.leaf_nodes++;
162+
} else {
163+
stats.internal_nodes++;
172164
}
173165
};
174166

167+
auto callbacks = bfs_callbacks<
168+
null_callback, // initialize
169+
null_callback, // discover
170+
decltype(examine_cb), // examine
171+
null_callback, // examine_edge
172+
null_callback, // tree_edge
173+
null_callback, // non_tree_edge
174+
null_callback, // gray_target
175+
null_callback, // black_target
176+
null_callback // finish
177+
>{
178+
.on_examine_vertex = examine_cb
179+
};
180+
175181
breadth_first_search(g, 0, callbacks);
176182

177183
stats.avg_out_degree /= stats.total_vertices;

modern/examples/dijkstra_ranges.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ int main() {
2525
std::cout << "=== Dijkstra's Algorithm with C++20 Ranges ===\n\n";
2626

2727
// Create a weighted graph
28-
adjacency_list<directed_tag, VertexProps, EdgeProps> g(6);
28+
simple_adjacency_list<directed_tag, VertexProps, EdgeProps> g(6);
2929

3030
// Set vertex names
3131
g[0].name = "A";

0 commit comments

Comments
 (0)