Skip to content

Commit

Permalink
graph_traversal: change how unneeded handlers are passed
Browse files Browse the repository at this point in the history
Instead of having N! overloads for each function that can take N
optional callbacks (`is_target`, `visit`, and `visit_with_parent`), each
with their own copy of the default implementation, I now use a stub
struct (`detail::optional_func`) that implements the default behavior
which the templated function type defaults to.

Callers now have to pass `{}` in place of the previously-omitted
callback argument, which I think is cleaner and safer.
  • Loading branch information
yut23 committed Dec 17, 2024
1 parent bef401e commit 24338f8
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 173 deletions.
2 changes: 1 addition & 1 deletion 2023/src/day16.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ int GraphHelper::count_energized(const GraphHelper::Key &source) const {
[this](int id, auto &&handler) {
process_component_neighbors(id, handler);
},
visit_with_parent);
/*is_target*/ {}, visit_with_parent);

return std::count(energized.data().begin(), energized.data().end(), true);
}
Expand Down
1 change: 1 addition & 0 deletions 2023/src/day19.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,7 @@ long Part2Solver::solve() {
aoc::graph::dfs<use_seen>(
source,
[this](const Key &key, auto &&visit) { process_neighbors(key, visit); },
/*is_target*/ {},
std::bind_front(&Part2Solver::visit_with_parent, this));

if constexpr (aoc::DEBUG) {
Expand Down
3 changes: 2 additions & 1 deletion 2023/src/day21.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ struct Garden {
return true;
};

aoc::graph::bfs_manual_dedupe(source, process_neighbors, visit);
aoc::graph::bfs_manual_dedupe(source, process_neighbors,
/*is_target*/ {}, visit);

return distances;
}
Expand Down
2 changes: 1 addition & 1 deletion 2024/src/day10.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ aoc::ds::Grid<int> IslandMap::calc_scores() const {
[this](const Pos &pos, auto &&visit) {
this->process_neighbors(pos, visit);
},
visit_with_parent);
/*is_target*/ {}, visit_with_parent);
}

return scores;
Expand Down
3 changes: 2 additions & 1 deletion 2024/src/day12.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,8 @@ void Garden::process_plot(const Pos &pos, char plant_type) {
++region.area;
return true;
};
aoc::graph::bfs_manual_dedupe(pos, process_neighbors, visit);
aoc::graph::bfs_manual_dedupe(pos, process_neighbors, /*is_target*/ {},
visit);
}

template <aoc::Part part>
Expand Down
2 changes: 1 addition & 1 deletion 2024/src/day16.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ int Maze::find_shortest_path() const {
[this](const Key &key, auto &&visit_neighbor) {
this->process_neighbors(key, visit_neighbor);
},
std::bind_front(&Maze::get_distance, this), is_target);
std::bind_front(&Maze::get_distance, this), is_target, /*visit*/ {});

if constexpr (aoc::DEBUG) {
if (distance >= 0) {
Expand Down
Loading

0 comments on commit 24338f8

Please sign in to comment.