Skip to content

Commit cfff7e2

Browse files
committed
graph_traversal: rename second argument of ProcessNeighbors to process
This is clearer than `visit`, which is confusable with the top-level Visit function.
1 parent 615926d commit cfff7e2

File tree

10 files changed

+33
-31
lines changed

10 files changed

+33
-31
lines changed

2023/src/day17.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class CityMap {
4444
private:
4545
aoc::ds::Grid<unsigned char> block_costs;
4646

47-
void process_neighbors(bool ultra, const Key &key, auto &&visit) const;
47+
void process_neighbors(bool ultra, const Key &key, auto &&process) const;
4848
int get_distance(const Key &from, const Key &to) const;
4949

5050
public:
@@ -64,13 +64,13 @@ std::ostream &operator<<(std::ostream &os, const CityMap::Key &key) {
6464
}
6565

6666
void CityMap::process_neighbors(bool ultra, const Key &key,
67-
auto &&visit) const {
67+
auto &&process) const {
6868
const int min_straight_moves = ultra ? 4 : 1;
6969
const int max_straight_moves = ultra ? 10 : 3;
7070
if (key.pos.x == 0 && key.pos.y == 0 &&
7171
key.orient == Orientation::horizontal) {
7272
// allow going down initially
73-
visit({key.pos, Orientation::vertical});
73+
process({key.pos, Orientation::vertical});
7474
}
7575
for (const auto orient : {Orientation::horizontal, Orientation::vertical}) {
7676
if (orient == key.orient) {
@@ -88,7 +88,7 @@ void CityMap::process_neighbors(bool ultra, const Key &key,
8888
if (!block_costs.in_bounds(neighbor.pos)) {
8989
continue;
9090
}
91-
visit(neighbor);
91+
process(neighbor);
9292
}
9393
}
9494
}

2023/src/day19.hpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ class Part2Solver {
237237

238238
std::multimap<Key, Key> prev{};
239239

240-
void process_neighbors(const Key &key, auto &&visit) const;
240+
void process_neighbors(const Key &key, auto &&process) const;
241241

242242
void visit_with_parent(const Key &key, const Key &parent, int);
243243

@@ -257,16 +257,16 @@ std::ostream &operator<<(std::ostream &os, const Part2Solver::Key &key) {
257257
return os;
258258
}
259259

260-
void Part2Solver::process_neighbors(const Key &key, auto &&visit) const {
260+
void Part2Solver::process_neighbors(const Key &key, auto &&process) const {
261261
const auto &[name, index] = key;
262262
if (name == "A" || name == "R") {
263263
return;
264264
}
265265
const Rule &rule = cat.at(name, index);
266266
if (rule.conditional()) {
267-
visit({name, index + 1});
267+
process({name, index + 1});
268268
}
269-
visit({rule.dest, 0});
269+
process({rule.dest, 0});
270270
}
271271

272272
void Part2Solver::visit_with_parent(const Key &key, const Key &parent, int) {
@@ -292,7 +292,9 @@ long Part2Solver::solve() {
292292
constexpr bool use_seen = false;
293293
aoc::graph::dfs<use_seen>(
294294
source,
295-
[this](const Key &key, auto &&visit) { process_neighbors(key, visit); },
295+
[this](const Key &key, auto &&process) {
296+
process_neighbors(key, process);
297+
},
296298
/*is_target*/ {},
297299
std::bind_front(&Part2Solver::visit_with_parent, this));
298300

2023/src/day20.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -336,8 +336,8 @@ MessageBus MessageBus::read_modules(std::istream &is) {
336336
void MessageBus::identify_components() {
337337
const std::string root = "broadcaster";
338338
const auto process_neighbors = [this](const std::string &name,
339-
auto &&visit) {
340-
std::ranges::for_each(modules.at(name)->outputs, visit);
339+
auto &&process) {
340+
std::ranges::for_each(modules.at(name)->outputs, process);
341341
};
342342
components = aoc::graph::tarjan_scc(root, process_neighbors).first;
343343

2023/src/day21.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,12 @@ struct Garden {
4242
distances[source] = 0;
4343

4444
const auto process_neighbors = [this, &distances](const Key &key,
45-
auto &&visit) {
45+
auto &&process) {
4646
for (const AbsDirection &dir : aoc::DIRECTIONS) {
4747
Pos pos = key + Delta(dir, true);
4848
if (stones.in_bounds(pos) && stones[pos] &&
4949
distances[pos] == std::numeric_limits<int>::max()) {
50-
visit(std::move(pos));
50+
process(std::move(pos));
5151
}
5252
}
5353
};

2023/src/day22.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ int part_2(const aoc::day22::BrickStack &stack) {
4646
}
4747

4848
const auto process_neighbors = [&stack](const Brick *brick,
49-
auto &&visit) {
50-
std::ranges::for_each(stack.supporting.at(brick), visit);
49+
auto &&process) {
50+
std::ranges::for_each(stack.supporting.at(brick), process);
5151
};
5252
const auto ordering = aoc::graph::topo_sort(root, process_neighbors);
5353

2023/src/day23.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -236,8 +236,8 @@ int TrailMap::part_1() const {
236236
std::cerr << "}\n";
237237
}
238238
const auto process_neighbors = [&fwd_edges = fwd_edges](const Key key,
239-
auto &&visit) {
240-
std::ranges::for_each(fwd_edges[key], visit);
239+
auto &&process) {
240+
std::ranges::for_each(fwd_edges[key], process);
241241
};
242242
const auto is_target = [&target = target](const Key key) -> bool {
243243
return key == target;

2024/src/day10.hpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class IslandMap : public aoc::ds::Grid<height_t> {
3232
std::vector<Pos> peaks{};
3333

3434
explicit IslandMap(std::vector<std::vector<height_t>> &&height_map);
35-
void process_neighbors(const Pos &key, auto &&visit) const;
35+
void process_neighbors(const Pos &key, auto &&process) const;
3636
template <aoc::Part part>
3737
aoc::ds::Grid<int> calc_scores() const;
3838

@@ -99,8 +99,8 @@ aoc::ds::Grid<int> IslandMap::calc_scores() const {
9999
constexpr bool use_seen = part == PART_1;
100100
aoc::graph::dfs_rec<use_seen>(
101101
peak,
102-
[this](const Pos &pos, auto &&visit) {
103-
this->process_neighbors(pos, visit);
102+
[this](const Pos &pos, auto &&process) {
103+
this->process_neighbors(pos, process);
104104
},
105105
/*is_target*/ {}, visit_with_parent);
106106
}
@@ -109,7 +109,7 @@ aoc::ds::Grid<int> IslandMap::calc_scores() const {
109109
}
110110

111111
// walk down from peaks to trailheads
112-
void IslandMap::process_neighbors(const Pos &pos, auto &&visit) const {
112+
void IslandMap::process_neighbors(const Pos &pos, auto &&process) const {
113113
height_t curr_height = at(pos);
114114
if (curr_height == 0) {
115115
return;
@@ -118,7 +118,7 @@ void IslandMap::process_neighbors(const Pos &pos, auto &&visit) const {
118118
for (const auto &dir : DIRECTIONS) {
119119
Pos neighbor = pos + Delta(dir, true);
120120
if (in_bounds(neighbor) && (*this)[neighbor] == curr_height - 1) {
121-
visit(neighbor);
121+
process(neighbor);
122122
}
123123
}
124124
}

2024/src/day16.hpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class Maze {
4444
Pos start_pos;
4545
Pos end_pos;
4646

47-
void process_neighbors(const Key &key, auto &&visit) const;
47+
void process_neighbors(const Key &key, auto &&process) const;
4848
int get_distance(const Key &from, const Key &to) const;
4949
bool is_target(const Key &key) const { return key.pos == end_pos; }
5050

@@ -89,15 +89,15 @@ std::ostream &operator<<(std::ostream &os, const Maze::Key &key) {
8989
return os;
9090
}
9191

92-
void Maze::process_neighbors(const Key &key, auto &&visit) const {
92+
void Maze::process_neighbors(const Key &key, auto &&process) const {
9393
// try moving straight
9494
Key neighbor{key.pos + Delta(key.dir, true), key.dir};
9595
if (grid.in_bounds(neighbor.pos) && grid[neighbor.pos] != '#') {
96-
visit(neighbor);
96+
process(neighbor);
9797
}
9898
// try turning
99-
visit(Key{key.pos, directions::turn(key.dir, RelDirection::left)});
100-
visit(Key{key.pos, directions::turn(key.dir, RelDirection::right)});
99+
process(Key{key.pos, directions::turn(key.dir, RelDirection::left)});
100+
process(Key{key.pos, directions::turn(key.dir, RelDirection::right)});
101101
}
102102

103103
int Maze::get_distance(const Key &from, const Key &to) const {

aoc_lib/src/graph_traversal.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ using maybe_unordered_map =
6868
template <class Func, class Key>
6969
concept ProcessNeighbors =
7070
requires(Func process_neighbors, const Key &key,
71-
std::function<void(const Key &key)> &visitor) {
72-
process_neighbors(key, visitor);
71+
std::function<void(const Key &key)> &process) {
72+
process_neighbors(key, process);
7373
};
7474

7575
// cppcheck fails to parse this if the || comes after the requires statement

aoc_lib/src/test_graph.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ std::size_t test_tarjan_scc() {
2424
+[](const std::unordered_map<int, std::vector<int>> &adj,
2525
int root) -> std::pair<std::vector<std::vector<int>>,
2626
std::set<std::pair<int, int>>> {
27-
const auto process_neighbors = [&adj](int key, auto &&visit) {
28-
std::ranges::for_each(adj.at(key), visit);
27+
const auto process_neighbors = [&adj](int key, auto &&process) {
28+
std::ranges::for_each(adj.at(key), process);
2929
};
3030
auto result = aoc::graph::tarjan_scc(root, process_neighbors);
3131
for (auto &component : result.first) {

0 commit comments

Comments
 (0)