Skip to content

Commit 62a9644

Browse files
committed
Add FixedGraph constexpr fixed-capacity graph container
1 parent a027c45 commit 62a9644

3 files changed

Lines changed: 1398 additions & 0 deletions

File tree

README.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ The fixed-container types have identical APIs to their std:: equivalents, so you
3131
| `FixedCircularQueue` | `std::queue` API with Circular Buffer semantics |
3232
| `FixedBitset` | `std::bitset` |
3333
| `FixedString` | `std::string` |
34+
| `FixedGraph` | (no direct `std::` equivalent) |
3435
| `FixedMap` | `std::map` |
3536
| `FixedSet` | `std::set` |
3637
| `FixedUnorderedMap` | `std::unordered_map` |
@@ -273,6 +274,63 @@ More examples can be found [here](test/enums_test_common.hpp).
273274
static_assert(s1.max_size() == 11);
274275
```
275276

277+
- FixedGraph
278+
```C++
279+
#include "fixed_containers/fixed_graph.hpp"
280+
using namespace fixed_containers;
281+
282+
// Directed, unweighted graph with capacity for 8 nodes, up to 6 outgoing edges each
283+
using Graph = FixedGraph<int, void, 8, 6, true>; // <NodeType, EdgeType=void (unweighted), MAX_NODES, MAX_EDGES_PER_NODE, DIRECTED>
284+
285+
constexpr auto g = []() {
286+
Graph gr{};
287+
auto a = gr.add_node(0);
288+
auto b = gr.add_node(1);
289+
auto c = gr.add_node(2);
290+
gr.add_edge(a, b); // 0 -> 1
291+
gr.add_edge(b, c); // 1 -> 2
292+
gr.add_edge(a, c); // 0 -> 2
293+
return gr;
294+
}();
295+
296+
static_assert(g.node_count() == 3);
297+
static_assert(g.has_edge(0, 1));
298+
static_assert(g.has_edge(1, 2));
299+
static_assert(g.shortest_path(0, 2).size() == 2); // 0 -> 2 direct
300+
301+
// Runtime example (BFS traversal)
302+
void traverse() {
303+
Graph gr{};
304+
auto n0 = gr.add_node(0);
305+
auto n1 = gr.add_node(1);
306+
auto n2 = gr.add_node(2);
307+
auto n3 = gr.add_node(3);
308+
gr.add_edge(n0, n1);
309+
gr.add_edge(n1, n2);
310+
gr.add_edge(n0, n3);
311+
gr.add_edge(n3, n2);
312+
313+
FixedVector<int, 8> order{};
314+
gr.bfs(n0, [&](std::size_t idx){ order.push_back(static_cast<int>(gr.node_at(idx))); });
315+
// 'order' now holds a BFS visitation order starting from node 0
316+
}
317+
318+
// Weighted undirected example using double edge weights
319+
using WUGraph = FixedGraph<int, double, 6, 6, false>; // undirected weighted
320+
constexpr auto wug = [](){
321+
WUGraph gr{};
322+
auto a = gr.add_node(0);
323+
auto b = gr.add_node(1);
324+
auto c = gr.add_node(2);
325+
gr.add_edge(a, b, 1.5);
326+
gr.add_edge(b, c, 2.25);
327+
gr.add_edge(a, c, 5.0);
328+
// Dijkstra shortest path 0 -> 2 should pick 0-1-2 (1.5 + 2.25 = 3.75 < 5.0)
329+
auto path = gr.dijkstra_shortest_path(a, c);
330+
return gr;
331+
}();
332+
```
333+
276334
- EnumMap
277335
```C++
278336
enum class Color { RED, YELLOW, BLUE};

0 commit comments

Comments
 (0)