diff options
author | 2020-09-29 15:29:37 -0700 | |
---|---|---|
committer | 2021-02-22 18:40:25 -0800 | |
commit | ce58d5b19aac30ab10f08ad16a1e437bffe43e11 (patch) | |
tree | fd02fd54c7cfc3ff0df762e2485e57c30d34da5e | |
parent | a3d8f05fed62e9a5fc91b65e4e7e3ffe8290e9b3 (diff) |
libui: Add std::hash support for Rects/Region
Adds support for std::hash to Rect, FloatRect, and Region.
Bug: 158790260
Test: libui unit tests
Change-Id: I593bad1d3111d9b28984de5c2d80695c124e30b6
-rw-r--r-- | libs/ui/Android.bp | 2 | ||||
-rw-r--r-- | libs/ui/include/ui/FloatRect.h | 11 | ||||
-rw-r--r-- | libs/ui/include/ui/Rect.h | 10 | ||||
-rw-r--r-- | libs/ui/include/ui/Region.h | 14 | ||||
-rw-r--r-- | libs/ui/tests/Rect_test.cpp | 29 | ||||
-rw-r--r-- | libs/ui/tests/Region_test.cpp | 12 |
6 files changed, 78 insertions, 0 deletions
diff --git a/libs/ui/Android.bp b/libs/ui/Android.bp index 714ee3e909..a8d476250b 100644 --- a/libs/ui/Android.bp +++ b/libs/ui/Android.bp @@ -220,9 +220,11 @@ cc_library_headers { }, header_libs: [ "libnativewindow_headers", + "libmath_headers", ], export_header_lib_headers: [ "libnativewindow_headers", + "libmath_headers", ], min_sdk_version: "29", } diff --git a/libs/ui/include/ui/FloatRect.h b/libs/ui/include/ui/FloatRect.h index bec2552fde..69cadfe32e 100644 --- a/libs/ui/include/ui/FloatRect.h +++ b/libs/ui/include/ui/FloatRect.h @@ -16,6 +16,7 @@ #pragma once +#include <math/HashCombine.h> #include <ostream> namespace android { @@ -60,3 +61,13 @@ static inline void PrintTo(const FloatRect& rect, ::std::ostream* os) { } } // namespace android + +namespace std { + +template <> +struct hash<android::FloatRect> { + size_t operator()(const android::FloatRect& rect) const { + return android::hashCombine(rect.left, rect.top, rect.right, rect.bottom); + } +}; +} // namespace std diff --git a/libs/ui/include/ui/Rect.h b/libs/ui/include/ui/Rect.h index 58323e553e..9e24a077ff 100644 --- a/libs/ui/include/ui/Rect.h +++ b/libs/ui/include/ui/Rect.h @@ -24,6 +24,7 @@ #include <utils/Log.h> #include <utils/TypeHelpers.h> +#include <math/HashCombine.h> #include <ui/FloatRect.h> #include <ui/Point.h> #include <ui/Size.h> @@ -234,4 +235,13 @@ ANDROID_BASIC_TYPES_TRAITS(Rect) }; // namespace android +namespace std { +template <> +struct hash<android::Rect> { + size_t operator()(const android::Rect& rect) const { + return android::hashCombine(rect.left, rect.top, rect.right, rect.bottom); + } +}; +} // namespace std + #endif // ANDROID_UI_RECT diff --git a/libs/ui/include/ui/Region.h b/libs/ui/include/ui/Region.h index 6bb7b8d21c..927c334c85 100644 --- a/libs/ui/include/ui/Region.h +++ b/libs/ui/include/ui/Region.h @@ -21,6 +21,7 @@ #include <sys/types.h> #include <ostream> +#include <math/HashCombine.h> #include <ui/Rect.h> #include <utils/Flattenable.h> @@ -234,4 +235,17 @@ static inline void PrintTo(const Region& region, ::std::ostream* os) { // --------------------------------------------------------------------------- }; // namespace android +namespace std { +template <> +struct hash<android::Region> { + size_t operator()(const android::Region& region) const { + size_t hash = 0; + for (const android::Rect& rect : region) { + android::hashCombineSingle(hash, rect); + } + return hash; + } +}; +} // namespace std + #endif // ANDROID_UI_REGION_H diff --git a/libs/ui/tests/Rect_test.cpp b/libs/ui/tests/Rect_test.cpp index 5499a5b507..9cc36bb15b 100644 --- a/libs/ui/tests/Rect_test.cpp +++ b/libs/ui/tests/Rect_test.cpp @@ -259,4 +259,33 @@ TEST(RectTest, toFloatRect) { EXPECT_EQ(FloatRect(10.f, 20.f, 50.f, 60.f), floatRect); } +TEST(RectTest, RectHash) { + const std::vector<Rect> rects = { + Rect(10, 20, 50, 60), Rect(11, 20, 50, 60), Rect(11, 21, 50, 60), + Rect(11, 21, 51, 60), Rect(11, 21, 51, 61), + }; + + for (const auto& a : rects) { + for (const auto& b : rects) { + const bool hashEq = std::hash<Rect>{}(a) == std::hash<Rect>{}(b); + EXPECT_EQ(a == b, hashEq); + } + } +} + +TEST(RectTest, FloatRectHash) { + const std::vector<FloatRect> floatRects = { + Rect(10, 20, 50, 60).toFloatRect(), Rect(11, 20, 50, 60).toFloatRect(), + Rect(11, 21, 50, 60).toFloatRect(), Rect(11, 21, 51, 60).toFloatRect(), + Rect(11, 21, 51, 61).toFloatRect(), + }; + + for (const auto& a : floatRects) { + for (const auto& b : floatRects) { + const bool hashEq = std::hash<FloatRect>{}(a) == std::hash<FloatRect>{}(b); + EXPECT_EQ(a == b, hashEq); + } + } +} + } // namespace android::ui diff --git a/libs/ui/tests/Region_test.cpp b/libs/ui/tests/Region_test.cpp index c6b826d66e..74924bdce8 100644 --- a/libs/ui/tests/Region_test.cpp +++ b/libs/ui/tests/Region_test.cpp @@ -167,5 +167,17 @@ TEST_F(RegionTest, EqualsToSelf) { ASSERT_TRUE(touchableRegion.contains(50, 50)); } +TEST_F(RegionTest, RegionHash) { + Region region1; + region1.addRectUnchecked(10, 20, 30, 40); + region1.addRectUnchecked(40, 30, 20, 10); + + Region region2; + region2.addRectUnchecked(11, 20, 30, 40); + region2.addRectUnchecked(40, 31, 20, 10); + + EXPECT_NE(std::hash<Region>{}(region1), std::hash<Region>{}(region2)); +} + }; // namespace android |