Skip to content

Commit 39fbb7e

Browse files
authored
D3128: add allocator parameters to algorithm synopses and documentation (#133)
Add Alloc template parameter (defaulting to std::allocator<byte>) and const Alloc& alloc parameter to all algorithms that use internal dynamic storage: Traversal: - breadth_first_search (single- and multi-source) - depth_first_search - topological_sort (single-source, multi-source, full-graph) Shortest paths: - dijkstra_shortest_paths (single- and multi-source) - dijkstra_shortest_distances (single- and multi-source) Components: - articulation_points - biconnected_components - connected_components - kosaraju (both overloads) - tarjan_scc MST: - kruskal (both overloads) - inplace_kruskal (both overloads) - prim Update algorithms.tex: add alloc remarks to each affected algorithm section and fix lstinputlisting line ranges for connected_components.hpp and mst.hpp after synopsis expansion.
1 parent ce26adf commit 39fbb7e

13 files changed

Lines changed: 126 additions & 58 deletions
Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
template <adjacency_list G, class Visitor = empty_visitor>
1+
template <adjacency_list G, class Visitor = empty_visitor,
2+
class Alloc = allocator<byte>>
23
void breadth_first_search(
3-
G&& g, // graph
4-
vertex_id_t<G> source, // starting vertex\_id
5-
Visitor&& visitor = empty_visitor())
4+
G&& g, // graph
5+
vertex_id_t<G> source, // starting vertex\_id
6+
Visitor&& visitor = empty_visitor(),
7+
const Alloc& alloc = Alloc())
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
template <adjacency_list G, input_range Sources, class Visitor = empty_visitor>
1+
template <adjacency_list G, input_range Sources, class Visitor = empty_visitor,
2+
class Alloc = allocator<byte>>
23
requires convertible_to<range_value_t<Sources>, vertex_id_t<G>>
34
void breadth_first_search(G&& g, // graph
45
const Sources& sources,
5-
Visitor&& visitor = empty_visitor())
6+
Visitor&& visitor = empty_visitor(),
7+
const Alloc& alloc = Alloc())
Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
/*
22
* Hopcroft-Tarjan Articulation Points
33
*/
4-
template <adjacency_list G, class Iter>
4+
template <adjacency_list G, class Iter, class Alloc = allocator<byte>>
55
requires output_iterator<Iter, vertex_id_t<G>>
6-
void articulation_points(G&& g, Iter cut_vertices);
6+
void articulation_points(G&& g, Iter cut_vertices, const Alloc& alloc = Alloc());
77

88
/*
99
* Hopcroft-Tarjan Biconnected Components
1010
*/
11-
template <adjacency_list G, class OuterContainer>
12-
void biconnected_components(G&& g, OuterContainer& components);
11+
template <adjacency_list G, class OuterContainer, class Alloc = allocator<byte>>
12+
void biconnected_components(G&& g, OuterContainer& components, const Alloc& alloc = Alloc());
1313

1414
/*
1515
* Connected Components
1616
*/
17-
template <adjacency_list G, class ComponentFn>
17+
template <adjacency_list G, class ComponentFn, class Alloc = allocator<byte>>
1818
requires vertex_property_fn_for<ComponentFn, G>
19-
size_t connected_components(G&& g, ComponentFn&& component);
19+
size_t connected_components(G&& g, ComponentFn&& component, const Alloc& alloc = Alloc());
2020

2121
/*
2222
* Afforest Connected Components
@@ -32,10 +32,12 @@ void afforest(G&& g, GT&& g_t, Component& component, const size_t neighbor_round
3232
/*
3333
* Kosaraju Strongly Connected Components
3434
*/
35-
template <adjacency_list G, adjacency_list GT, class ComponentFn>
35+
template <adjacency_list G, adjacency_list GT, class ComponentFn,
36+
class Alloc = allocator<byte>>
3637
requires vertex_property_fn_for<ComponentFn, G>
37-
void kosaraju(G&& g, GT&& g_t, ComponentFn&& component);
38+
void kosaraju(G&& g, GT&& g_t, ComponentFn&& component, const Alloc& alloc = Alloc());
3839

39-
template <bidirectional_adjacency_list G, class ComponentFn>
40+
template <bidirectional_adjacency_list G, class ComponentFn,
41+
class Alloc = allocator<byte>>
4042
requires vertex_property_fn_for<ComponentFn, G>
41-
void kosaraju(G&& g, ComponentFn&& component);
43+
void kosaraju(G&& g, ComponentFn&& component, const Alloc& alloc = Alloc());
Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
template <adjacency_list G, class Visitor = empty_visitor>
1+
template <adjacency_list G, class Visitor = empty_visitor,
2+
class Alloc = allocator<byte>>
23
void depth_first_search(
3-
G&& g, // graph
4-
vertex_id_t<G> source, // starting vertex\_id
5-
Visitor&& visitor = empty_visitor())
4+
G&& g, // graph
5+
vertex_id_t<G> source, // starting vertex\_id
6+
Visitor&& visitor = empty_visitor(),
7+
const Alloc& alloc = Alloc())

D3128_Algorithms/src/dijkstra_shortest_dists.hpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ template <adjacency_list G,
44
const edge_t<G>&)>,
55
class Visitor = empty_visitor,
66
class Compare = less<distance_fn_value_t<DistanceFn, G>>,
7-
class Combine = plus<distance_fn_value_t<DistanceFn, G>>>
7+
class Combine = plus<distance_fn_value_t<DistanceFn, G>>,
8+
class Alloc = allocator<byte>>
89
requires distance_fn_for<DistanceFn, G> &&
910
basic_edge_weight_function<G, WF, distance_fn_value_t<DistanceFn, G>, Compare, Combine>
1011
constexpr void dijkstra_shortest_distances(
@@ -15,4 +16,5 @@ constexpr void dijkstra_shortest_distances(
1516
const edge_t<G>& uv) { return distance_fn_value_t<DistanceFn, G>(1); },
1617
Visitor&& visitor = empty_visitor(),
1718
Compare&& compare = less<distance_fn_value_t<DistanceFn, G>>(),
18-
Combine&& combine = plus<distance_fn_value_t<DistanceFn, G>>());
19+
Combine&& combine = plus<distance_fn_value_t<DistanceFn, G>>(),
20+
const Alloc& alloc = Alloc());

D3128_Algorithms/src/dijkstra_shortest_dists_multi.hpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ template <adjacency_list G,
55
const edge_t<G>&)>,
66
class Visitor = empty_visitor,
77
class Compare = less<distance_fn_value_t<DistanceFn, G>>,
8-
class Combine = plus<distance_fn_value_t<DistanceFn, G>>>
8+
class Combine = plus<distance_fn_value_t<DistanceFn, G>>,
9+
class Alloc = allocator<byte>>
910
requires distance_fn_for<DistanceFn, G> &&
1011
convertible_to<range_value_t<Sources>, vertex_id_t<G>> &&
1112
basic_edge_weight_function<G, WF, distance_fn_value_t<DistanceFn, G>, Compare, Combine>
@@ -17,4 +18,5 @@ constexpr void dijkstra_shortest_distances(
1718
const edge_t<G>& uv) { return distance_fn_value_t<DistanceFn, G>(1); },
1819
Visitor&& visitor = empty_visitor(),
1920
Compare&& compare = less<distance_fn_value_t<DistanceFn, G>>(),
20-
Combine&& combine = plus<distance_fn_value_t<DistanceFn, G>>());
21+
Combine&& combine = plus<distance_fn_value_t<DistanceFn, G>>(),
22+
const Alloc& alloc = Alloc());

D3128_Algorithms/src/dijkstra_shortest_paths.hpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ template <adjacency_list G,
55
const edge_t<G>&)>,
66
class Visitor = empty_visitor,
77
class Compare = less<distance_fn_value_t<DistanceFn, G>>,
8-
class Combine = plus<distance_fn_value_t<DistanceFn, G>>>
8+
class Combine = plus<distance_fn_value_t<DistanceFn, G>>,
9+
class Alloc = allocator<byte>>
910
requires distance_fn_for<DistanceFn, G> &&
1011
predecessor_fn_for<PredecessorFn, G> &&
1112
basic_edge_weight_function<G, WF, distance_fn_value_t<DistanceFn, G>, Compare, Combine>
@@ -18,4 +19,5 @@ constexpr void dijkstra_shortest_paths(
1819
const edge_t<G>& uv) { return distance_fn_value_t<DistanceFn, G>(1); },
1920
Visitor&& visitor = empty_visitor(),
2021
Compare&& compare = less<distance_fn_value_t<DistanceFn, G>>(),
21-
Combine&& combine = plus<distance_fn_value_t<DistanceFn, G>>());
22+
Combine&& combine = plus<distance_fn_value_t<DistanceFn, G>>(),
23+
const Alloc& alloc = Alloc());

D3128_Algorithms/src/dijkstra_shortest_paths_multi.hpp

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,20 @@ template <adjacency_list G,
66
const edge_t<G>&)>,
77
class Visitor = empty_visitor,
88
class Compare = less<distance_fn_value_t<DistanceFn, G>>,
9-
class Combine = plus<distance_fn_value_t<DistanceFn, G>>>
9+
class Combine = plus<distance_fn_value_t<DistanceFn, G>>,
10+
class Alloc = allocator<byte>>
1011
requires distance_fn_for<DistanceFn, G> &&
1112
predecessor_fn_for<PredecessorFn, G> &&
1213
convertible_to<range_value_t<Sources>, vertex_id_t<G>> &&
1314
basic_edge_weight_function<G, WF, distance_fn_value_t<DistanceFn, G>, Compare, Combine>
1415
constexpr void dijkstra_shortest_paths(
15-
G&& g,
16-
const Sources& sources,
17-
DistanceFn&& distance,
16+
G&& g,
17+
const Sources& sources,
18+
DistanceFn&& distance,
1819
PredecessorFn&& predecessor,
19-
WF&& weight = [](const auto&,
20+
WF&& weight = [](const auto&,
2021
const edge_t<G>& uv) { return distance_fn_value_t<DistanceFn, G>(1); },
21-
Visitor&& visitor = empty_visitor(),
22-
Compare&& compare = less<distance_fn_value_t<DistanceFn, G>>(),
23-
Combine&& combine = plus<distance_fn_value_t<DistanceFn, G>>());
22+
Visitor&& visitor = empty_visitor(),
23+
Compare&& compare = less<distance_fn_value_t<DistanceFn, G>>(),
24+
Combine&& combine = plus<distance_fn_value_t<DistanceFn, G>>(),
25+
const Alloc& alloc = Alloc());

D3128_Algorithms/src/mst.hpp

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,26 @@
11
/*
22
* Kruskal's Algorithm
33
*/
4-
template <x_index_edgelist_range IELR, x_index_edgelist_range OELR>
5-
auto kruskal(IELR&& e, OELR&& t);
4+
template <x_index_edgelist_range IELR, x_index_edgelist_range OELR,
5+
class Alloc = allocator<byte>>
6+
auto kruskal(IELR&& e, OELR&& t, const Alloc& alloc = Alloc());
67

7-
template <x_index_edgelist_range IELR, x_index_edgelist_range OELR, class CompareOp>
8-
auto kruskal(IELR&& e, OELR&& t, CompareOp compare);
8+
template <x_index_edgelist_range IELR, x_index_edgelist_range OELR, class CompareOp,
9+
class Alloc = allocator<byte>>
10+
auto kruskal(IELR&& e, OELR&& t, CompareOp compare, const Alloc& alloc = Alloc());
911

1012
/*
1113
* Inplace Kruskal's Algorithm
1214
*/
13-
template <x_index_edgelist_range IELR, x_index_edgelist_range OELR>
15+
template <x_index_edgelist_range IELR, x_index_edgelist_range OELR,
16+
class Alloc = allocator<byte>>
1417
requires permutable<iterator_t<IELR>>
15-
auto inplace_kruskal(IELR&& e, OELR&& t);
18+
auto inplace_kruskal(IELR&& e, OELR&& t, const Alloc& alloc = Alloc());
1619

17-
template <x_index_edgelist_range IELR, x_index_edgelist_range OELR, class CompareOp>
20+
template <x_index_edgelist_range IELR, x_index_edgelist_range OELR, class CompareOp,
21+
class Alloc = allocator<byte>>
1822
requires permutable<iterator_t<IELR>>
19-
auto inplace_kruskal(IELR&& e, OELR&& t, CompareOp compare);
23+
auto inplace_kruskal(IELR&& e, OELR&& t, CompareOp compare, const Alloc& alloc = Alloc());
2024

2125
/*
2226
* Prim's Algorithm
@@ -26,7 +30,8 @@ template <adjacency_list G,
2630
class PredecessorFn,
2731
class WF = function<distance_fn_value_t<WeightFn, G>(const remove_reference_t<G>&,
2832
const edge_t<G>&)>,
29-
class CompareOp = less<distance_fn_value_t<WeightFn, G>>>
33+
class CompareOp = less<distance_fn_value_t<WeightFn, G>>,
34+
class Alloc = allocator<byte>>
3035
requires distance_fn_for<WeightFn, G> &&
3136
is_arithmetic_v<distance_fn_value_t<WeightFn, G>> &&
3237
predecessor_fn_for<PredecessorFn, G> &&
@@ -37,4 +42,5 @@ auto prim(G&& g,
3742
WeightFn&& weight,
3843
PredecessorFn&& predecessor,
3944
WF&& weight_fn = [](const auto& gr, const edge_t<G>& uv) { return edge_value(gr, uv); },
40-
CompareOp compare = less<distance_fn_value_t<WeightFn, G>>());
45+
CompareOp compare = less<distance_fn_value_t<WeightFn, G>>(),
46+
const Alloc& alloc = Alloc());
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* Tarjan's Strongly Connected Components
33
*/
4-
template <adjacency_list G, class ComponentFn>
4+
template <adjacency_list G, class ComponentFn, class Alloc = allocator<byte>>
55
requires vertex_property_fn_for<ComponentFn, G>
6-
size_t tarjan_scc(G&& g, ComponentFn&& component);
6+
size_t tarjan_scc(G&& g, ComponentFn&& component, const Alloc& alloc = Alloc());

0 commit comments

Comments
 (0)