diff --git a/pytype/typegraph/map_util.h b/pytype/typegraph/map_util.h index b59cb9c81..8511f139e 100644 --- a/pytype/typegraph/map_util.h +++ b/pytype/typegraph/map_util.h @@ -41,28 +41,6 @@ bool ContainsKey(const M& map, const K& 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 -const V* FindOrNull(const M& map, const K& 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. -template -V FindPtrOrNull(const M& map, const K& key) { - auto it = map.find(key); - if (it != map.end()) - return it->second; - // V may not be a pointer type, but we're going to assume it is. - return nullptr; -} - } // namespace map_util } // namespace devtools_python_typegraph diff --git a/pytype/typegraph/map_util_test.cc b/pytype/typegraph/map_util_test.cc index c82637aaf..28a60c3ba 100644 --- a/pytype/typegraph/map_util_test.cc +++ b/pytype/typegraph/map_util_test.cc @@ -15,26 +15,6 @@ TEST(MapUtilTest, ContainsKeyTest) { EXPECT_TRUE(ContainsKey(m, 1)); EXPECT_FALSE(ContainsKey(m, 2)); } - -TEST(MapUtilTest, FindOrNullTest) { - std::unordered_map 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 m; - EXPECT_EQ(FindPtrOrNull(m, 1), nullptr); - std::string val = "hello"; - m[1] = &val; - std::string* res = FindPtrOrNull(m, 1); - EXPECT_NE(res, nullptr); - EXPECT_EQ(*res, val); -} - } // namespace } // namespace map_util diff --git a/pytype/typegraph/solver.cc b/pytype/typegraph/solver.cc index 85e1479cf..77b3cc04d 100644 --- a/pytype/typegraph/solver.cc +++ b/pytype/typegraph/solver.cc @@ -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; } diff --git a/pytype/typegraph/typegraph.cc b/pytype/typegraph/typegraph.cc index 750fb8f4c..17e678c00 100644 --- a/pytype/typegraph/typegraph.cc +++ b/pytype/typegraph/typegraph.cc @@ -185,7 +185,11 @@ bool Binding::IsVisible(const CFGNode* viewpoint) const { } Origin* Binding::FindOrigin(const CFGNode* node) const { - return map_util::FindPtrOrNull(node_to_origin_, node); + auto it = node_to_origin_.find(node); + if (it == node_to_origin_.end()) { + return nullptr; + } + return it->second; } Origin* Binding::FindOrAddOrigin(CFGNode* node) {