Skip to content

Commit

Permalink
Add hash function for IntervalDomain
Browse files Browse the repository at this point in the history
Summary: Move hash function for IntervalDomain from MT to sparta since it appears quite re-usable.

Reviewed By: arnaudvenet

Differential Revision: D48239105

fbshipit-source-id: 2bff16f61d839aa1fc4cf71b3574a8c3c95feb2e
  • Loading branch information
Yuh Shin Ong authored and facebook-github-bot committed Aug 11, 2023
1 parent 250e104 commit 980e2b2
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
19 changes: 19 additions & 0 deletions sparta/include/sparta/IntervalDomain.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
#include <limits>
#include <ostream>

#include <boost/functional/hash.hpp>

#include <sparta/AbstractDomain.h>

namespace sparta {
Expand Down Expand Up @@ -264,3 +266,20 @@ inline std::ostream& operator<<(std::ostream& o,
}

} // namespace sparta

template <typename Num>
struct std::hash<sparta::IntervalDomain<Num>> {
std::size_t operator()(const sparta::IntervalDomain<Num>& interval) const {
std::size_t seed = 0;

if (interval.is_bottom()) {
boost::hash_combine(seed, sparta::IntervalDomain<Num>::MAX);
boost::hash_combine(seed, sparta::IntervalDomain<Num>::MIN);
} else {
boost::hash_combine(seed, interval.lower_bound());
boost::hash_combine(seed, interval.upper_bound());
}

return seed;
}
};
19 changes: 19 additions & 0 deletions sparta/test/IntervalDomainTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,4 +148,23 @@ TEST(IntervalDomainTest, lattice) {
EXPECT_EQ(top.narrowing(b).narrowing(a), Domain::finite(0, 4));
}

TEST(IntervalDomainTest, hash) {
const auto top = Domain::top();
const auto bot = Domain::bottom();

const auto a = Domain::finite(0, 5);

EXPECT_NE(std::hash<Domain>()(top), std::hash<Domain>()(bot));
EXPECT_NE(std::hash<Domain>()(top), std::hash<Domain>()(a));
EXPECT_EQ(std::hash<Domain>()(top), std::hash<Domain>()(top));

EXPECT_EQ(std::hash<Domain>()(bot), std::hash<Domain>()(bot));
EXPECT_NE(std::hash<Domain>()(bot), std::hash<Domain>()(a));
EXPECT_NE(std::hash<Domain>()(bot), std::hash<Domain>()(top));

EXPECT_NE(std::hash<Domain>()(a), std::hash<Domain>()(bot));
EXPECT_EQ(std::hash<Domain>()(a), std::hash<Domain>()(a));
EXPECT_NE(std::hash<Domain>()(a), std::hash<Domain>()(top));
}

} // namespace

0 comments on commit 980e2b2

Please sign in to comment.