Skip to content

Commit

Permalink
2023: avoid unnecessary copies when returning std::pair
Browse files Browse the repository at this point in the history
  • Loading branch information
yut23 committed Feb 21, 2024
1 parent f74bba7 commit 98d6aa2
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 6 deletions.
2 changes: 1 addition & 1 deletion 2023/src/day18.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ read_instructions(std::istream &is) {
instructions2.push_back(std::move(color_instr));
}
}
return {instructions1, instructions2};
return {std::move(instructions1), std::move(instructions2)};
}

} // namespace aoc::day18
Expand Down
2 changes: 1 addition & 1 deletion 2023/src/day19.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ std::pair<PartCategorizer, std::vector<Part>> read_input(std::istream &is) {
while (read_part(is, part)) {
parts.push_back(std::move(part));
}
return {cat, parts};
return {std::move(cat), std::move(parts)};
}

class Part2Solver {
Expand Down
4 changes: 2 additions & 2 deletions 2023/src/day24.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ find_intersection_xy(const Hailstone &a, const Hailstone &b) {
}

template <class T>
std::tuple<aoc::ds::Grid<T>, std::vector<T>>
std::pair<aoc::ds::Grid<T>, std::vector<T>>
make_system(const std::vector<aoc::day24::Hailstone> &stones,
const std::pair<std::size_t, std::size_t> &pair_1,
const std::pair<std::size_t, std::size_t> &pair_2) {
Expand Down Expand Up @@ -133,7 +133,7 @@ make_system(const std::vector<aoc::day24::Hailstone> &stones,
}
++i;
}
return {A, b};
return {std::move(A), std::move(b)};
}

std::vector<Hailstone> read_stones(std::istream &is) {
Expand Down
4 changes: 2 additions & 2 deletions 2023/src/graph_traversal.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ longest_path_dag(const Key &source, GetNeighbors &&get_neighbors,
path.emplace_back(it->second.second);
}
std::ranges::reverse(path);
return {max_distance, path};
return {max_distance, std::move(path)};
}

/**
Expand Down Expand Up @@ -511,7 +511,7 @@ a_star(const Key &source, GetNeighbors &&get_neighbors,
path.emplace_back(it->second.key);
}
std::ranges::reverse(path);
return {curr.dist, path};
return {curr.dist, std::move(path)};
}
for (const Key &neighbor : get_neighbors(curr.key)) {
if constexpr (use_visited) {
Expand Down

0 comments on commit 98d6aa2

Please sign in to comment.