Utils.cpp 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /**
  2. * Copyright (c) 2014-present, Facebook, Inc.
  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. #include "Utils.h"
  8. YGFlexDirection YGFlexDirectionCross(
  9. const YGFlexDirection flexDirection,
  10. const YGDirection direction) {
  11. return YGFlexDirectionIsColumn(flexDirection)
  12. ? YGResolveFlexDirection(YGFlexDirectionRow, direction)
  13. : YGFlexDirectionColumn;
  14. }
  15. float YGFloatMax(const float a, const float b) {
  16. if (!YGFloatIsUndefined(a) && !YGFloatIsUndefined(b)) {
  17. return fmaxf(a, b);
  18. }
  19. return YGFloatIsUndefined(a) ? b : a;
  20. }
  21. float YGFloatMin(const float a, const float b) {
  22. if (!YGFloatIsUndefined(a) && !YGFloatIsUndefined(b)) {
  23. return fminf(a, b);
  24. }
  25. return YGFloatIsUndefined(a) ? b : a;
  26. }
  27. bool YGValueEqual(const YGValue a, const YGValue b) {
  28. if (a.unit != b.unit) {
  29. return false;
  30. }
  31. if (a.unit == YGUnitUndefined ||
  32. (YGFloatIsUndefined(a.value) && YGFloatIsUndefined(b.value))) {
  33. return true;
  34. }
  35. return fabs(a.value - b.value) < 0.0001f;
  36. }
  37. bool YGFloatsEqual(const float a, const float b) {
  38. if (!YGFloatIsUndefined(a) && !YGFloatIsUndefined(b)) {
  39. return fabs(a - b) < 0.0001f;
  40. }
  41. return YGFloatIsUndefined(a) && YGFloatIsUndefined(b);
  42. }
  43. float YGFloatSanitize(const float& val) {
  44. return YGFloatIsUndefined(val) ? 0 : val;
  45. }
  46. float YGUnwrapFloatOptional(const YGFloatOptional& op) {
  47. return op.isUndefined() ? YGUndefined : op.getValue();
  48. }
  49. YGFloatOptional YGFloatOptionalMax(
  50. const YGFloatOptional& op1,
  51. const YGFloatOptional& op2) {
  52. if (!op1.isUndefined() && !op2.isUndefined()) {
  53. return op1.getValue() > op2.getValue() ? op1 : op2;
  54. }
  55. return op1.isUndefined() ? op2 : op1;
  56. }