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
diff --git a/libs/ui/Android.bp b/libs/ui/Android.bp
index 714ee3e..a8d4762 100644
--- a/libs/ui/Android.bp
+++ b/libs/ui/Android.bp
@@ -220,9 +220,11 @@
},
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 bec2552..69cadfe 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 @@
}
} // 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 58323e5..9e24a07 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 @@
}; // 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 6bb7b8d..927c334 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 @@
// ---------------------------------------------------------------------------
}; // 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 5499a5b..9cc36bb 100644
--- a/libs/ui/tests/Rect_test.cpp
+++ b/libs/ui/tests/Rect_test.cpp
@@ -259,4 +259,33 @@
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 c6b826d..74924bd 100644
--- a/libs/ui/tests/Region_test.cpp
+++ b/libs/ui/tests/Region_test.cpp
@@ -167,5 +167,17 @@
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