Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit ce58d5b1 authored by Dan Stoza's avatar Dan Stoza Committed by Ady Abraham
Browse files

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
parent a3d8f05f
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -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",
}
+11 −0
Original line number Diff line number Diff line
@@ -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
+10 −0
Original line number Diff line number Diff line
@@ -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
+14 −0
Original line number Diff line number Diff line
@@ -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
+29 −0
Original line number Diff line number Diff line
@@ -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
Loading