slot 0.0.1
A real time UI render framework
载入中...
搜索中...
未找到
Comparison.h
浏览该文件的文档.
1/*
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 */
7
8#pragma once
9
10#include <algorithm>
11#include <array>
12#include <cmath>
13#include <concepts>
14
15#include <yoga/Yoga.h>
16
17namespace facebook::yoga {
18
19constexpr bool isUndefined(std::floating_point auto value) {
20 return value != value;
21}
22
23constexpr bool isDefined(std::floating_point auto value) {
24 return !isUndefined(value);
25}
26
30constexpr bool isinf(auto value) {
31 return value == +std::numeric_limits<decltype(value)>::infinity() ||
32 value == -std::numeric_limits<decltype(value)>::infinity();
33}
34
35constexpr auto maxOrDefined(
36 std::floating_point auto a,
37 std::floating_point auto b) {
38 if (yoga::isDefined(a) && yoga::isDefined(b)) {
39 return std::max(a, b);
40 }
41 return yoga::isUndefined(a) ? b : a;
42}
43
44constexpr auto minOrDefined(
45 std::floating_point auto a,
46 std::floating_point auto b) {
47 if (yoga::isDefined(a) && yoga::isDefined(b)) {
48 return std::min(a, b);
49 }
50
51 return yoga::isUndefined(a) ? b : a;
52}
53
54// Custom equality functions using a hardcoded epsilon of 0.0001f, or returning
55// true if both floats are NaN.
56inline bool inexactEquals(float a, float b) {
57 if (yoga::isDefined(a) && yoga::isDefined(b)) {
58 return std::abs(a - b) < 0.0001f;
59 }
61}
62
63inline bool inexactEquals(double a, double b) {
64 if (yoga::isDefined(a) && yoga::isDefined(b)) {
65 return std::abs(a - b) < 0.0001;
66 }
68}
69
70template <std::size_t Size, typename ElementT>
72 const std::array<ElementT, Size>& val1,
73 const std::array<ElementT, Size>& val2) {
74 bool areEqual = true;
75 for (std::size_t i = 0; i < Size && areEqual; ++i) {
76 areEqual = inexactEquals(val1[i], val2[i]);
77 }
78 return areEqual;
79}
80
81} // namespace facebook::yoga
Definition Benchmark.cpp:19
constexpr bool isDefined(std::floating_point auto value)
Definition Comparison.h:23
static bool isUndefined(json &j)
Definition TreeDeserialization.cpp:24
constexpr bool isinf(auto value)
Definition Comparison.h:30
constexpr auto maxOrDefined(std::floating_point auto a, std::floating_point auto b)
Definition Comparison.h:35
constexpr auto minOrDefined(std::floating_point auto a, std::floating_point auto b)
Definition Comparison.h:44
bool inexactEquals(float a, float b)
Definition Comparison.h:56