Skip to content

Commit

Permalink
Remove the helper FindOrNull.
Browse files Browse the repository at this point in the history
This utility is useless and worsens the readability. This is not at all a c++ way to do things.

PiperOrigin-RevId: 703164659
  • Loading branch information
h-joo authored and copybara-github committed Dec 5, 2024
1 parent 720d393 commit cdece4f
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 23 deletions.
10 changes: 0 additions & 10 deletions pytype/typegraph/map_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,6 @@ bool ContainsKey(const M& map, const K<M>& key) {
return map.find(key) != map.end();
}

// FindOrNull returns a pointer to the value associated with the
// given key if it exists, or nullptr otherwise.
template<typename M>
const V<M>* FindOrNull(const M& map, const K<M>& key) {
auto it = map.find(key);
if (it != map.end())
return &(it->second);
return nullptr;
}

// FindPtrOrNull returns the pointer value associated with a given key. It is
// designed to be used with maps of keys to pointers. It does not differentiate
// between keys that are not in the map and keys that are mapped to nullptr.
Expand Down
9 changes: 0 additions & 9 deletions pytype/typegraph/map_util_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,6 @@ TEST(MapUtilTest, ContainsKeyTest) {
EXPECT_FALSE(ContainsKey(m, 2));
}

TEST(MapUtilTest, FindOrNullTest) {
std::unordered_map<int, bool> m;
EXPECT_EQ(FindOrNull(m, 1), nullptr);
m[1] = true;
const bool* res = FindOrNull(m, 1);
EXPECT_NE(res, nullptr);
EXPECT_EQ(*res, true);
}

TEST(MapUtilTest, FindPtrOrNullTest) {
std::unordered_map<int, std::string*> m;
EXPECT_EQ(FindPtrOrNull(m, 1), nullptr);
Expand Down
8 changes: 4 additions & 4 deletions pytype/typegraph/solver.cc
Original file line number Diff line number Diff line change
Expand Up @@ -421,17 +421,17 @@ bool Solver::CanHaveSolution(
bool Solver::RecallOrFindSolution(
const internal::State& state, internal::StateSet& seen_states,
int current_depth) {
const bool* status = map_util::FindOrNull(solved_states_, state);
if (status) {
auto it = solved_states_.find(state);
if (it != solved_states_.end()) {
state_cache_hits_ += 1;
query_metrics_.back().set_from_cache(true);
std::string indent(current_depth, ' ');
if (*status) {
if (it->second) {
LOG(INFO) << indent << "Known state: solvable.";
} else {
LOG(INFO) << indent << "Known state: not solvable.";
}
return *status;
return it->second;
} else {
state_cache_misses_ += 1;
}
Expand Down

0 comments on commit cdece4f

Please sign in to comment.