Implement SpanEqual and SpanHash

We desire the ability to hash and check equality of data across spans to use associative containers such as `std::unordered_map` with spans. The implemented functions provide an easy way to do that.
This commit is contained in:
PixelyIon 2022-04-15 20:37:01 +05:30
parent df5d1256c2
commit 0baa90d641

View File

@ -155,4 +155,26 @@ namespace skyline {
span(Container &) -> span<typename Container::value_type>;
template<typename Container>
span(const Container &) -> span<const typename Container::value_type>;
/**
* @return If the contents of the two spans are byte-for-byte equal
*/
template<typename T, size_t Extent = std::dynamic_extent>
struct SpanEqual {
constexpr bool operator()(const span<T, Extent> &lhs, const span<T, Extent> &rhs) const {
if (lhs.size_bytes() != rhs.size_bytes())
return false;
return std::equal(lhs.begin(), lhs.end(), rhs.begin());
}
};
/**
* @return A hash of the contents of the span
*/
template<typename T, size_t Extent = std::dynamic_extent>
struct SpanHash {
constexpr size_t operator()(const skyline::span<T, Extent> &x) const {
return XXH64(x.data(), x.size_bytes(), 0);
}
};
}