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

@ -82,7 +82,7 @@ namespace skyline {
/**
* @return If a supplied span is located entirely inside this span and is effectively a subspan
*/
constexpr bool contains(const span<T, Extent>& other) const {
constexpr bool contains(const span<T, Extent> &other) const {
return this->begin() <= other.begin() && this->end() >= other.end();
}
@ -95,7 +95,7 @@ namespace skyline {
/** Comparision operators for equality and binary searches **/
constexpr bool operator==(const span<T, Extent>& other) const {
constexpr bool operator==(const span<T, Extent> &other) const {
return this->data() == other.data() && this->size() == other.size();
}
@ -103,7 +103,7 @@ namespace skyline {
return this->data() < other.data();
}
constexpr bool operator<(T* pointer) const {
constexpr bool operator<(T *pointer) const {
return this->data() < pointer;
}
@ -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);
}
};
}