Skip to content

Commit

Permalink
Simplify code with abseil container algorithms
Browse files Browse the repository at this point in the history
Thanks for the tip Martin!
  • Loading branch information
eguiraud committed Oct 2, 2023
1 parent a59c3a2 commit 0ddf67f
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ cc_library(
hdrs = ["graph.h"],
deps = [
":graph_cc_proto",
"@abseil-cpp//absl/algorithm:container",
"@abseil-cpp//absl/status:status",
"@abseil-cpp//absl/status:statusor",
"@abseil-cpp//absl/container:flat_hash_map",
Expand Down
8 changes: 5 additions & 3 deletions src/graph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ under certain conditions: see LICENSE.
#include <cassert>
#include <cstddef> // std::size_t
#include <fstream>
#include <iterator>

#include "absl/status/status.h"
#include "absl/status/statusor.h"
Expand Down Expand Up @@ -71,10 +72,11 @@ std::size_t find_var_idx(std::string_view name, const Inputs& inputs) {
// TODO this should _not_ be in the hot loop. do it once at the beginning.
std::vector<std::string> var_names;
var_names.reserve(inputs.size());
for (auto& [name, value] : inputs) var_names.push_back(name);
std::sort(var_names.begin(), var_names.end());
absl::c_transform(inputs, std::back_inserter(var_names),
[](const auto& p) { return p.first; });
absl::c_sort(var_names);

auto it = std::find(var_names.begin(), var_names.end(), name);
auto it = absl::c_find(var_names, name);
return std::distance(var_names.begin(), it);
}
} // end of anonymous namespace
Expand Down

0 comments on commit 0ddf67f

Please sign in to comment.