From 0baa90d641ff8763e8258e73da44421b53567bce Mon Sep 17 00:00:00 2001 From: PixelyIon Date: Fri, 15 Apr 2022 20:37:01 +0530 Subject: [PATCH] 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. --- app/src/main/cpp/skyline/common/span.h | 28 +++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/app/src/main/cpp/skyline/common/span.h b/app/src/main/cpp/skyline/common/span.h index 2904adf7..131a1a37 100644 --- a/app/src/main/cpp/skyline/common/span.h +++ b/app/src/main/cpp/skyline/common/span.h @@ -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& other) const { + constexpr bool contains(const span &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& other) const { + constexpr bool operator==(const span &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; template span(const Container &) -> span; + + /** + * @return If the contents of the two spans are byte-for-byte equal + */ + template + struct SpanEqual { + constexpr bool operator()(const span &lhs, const span &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 + struct SpanHash { + constexpr size_t operator()(const skyline::span &x) const { + return XXH64(x.data(), x.size_bytes(), 0); + } + }; }