Skip to content

Commit

Permalink
Remove the helper FindOrNull, FindPtrOrNull.
Browse files Browse the repository at this point in the history
These utility are useless and worsens the readability. This is not at all a c++ way to do things. We should rather use the .find and the iterator to check existence.

PiperOrigin-RevId: 703480889
  • Loading branch information
h-joo authored and copybara-github committed Dec 6, 2024
1 parent 720d393 commit a70b7b2
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 47 deletions.
22 changes: 0 additions & 22 deletions pytype/typegraph/map_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,28 +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.
template <typename M>
V<M> FindPtrOrNull(const M& map, const K<M>& key) {
auto it = map.find(key);
if (it != map.end())
return it->second;
// V<M> may not be a pointer type, but we're going to assume it is.
return nullptr;
}

} // namespace map_util

} // namespace devtools_python_typegraph
Expand Down
20 changes: 0 additions & 20 deletions pytype/typegraph/map_util_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,6 @@ TEST(MapUtilTest, ContainsKeyTest) {
EXPECT_TRUE(ContainsKey(m, 1));
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);
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
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
6 changes: 5 additions & 1 deletion pytype/typegraph/typegraph.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit a70b7b2

Please sign in to comment.