FSTComparison.m 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /*
  2. * Copyright 2017 Google
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import "Firestore/Source/Util/FSTComparison.h"
  17. NS_ASSUME_NONNULL_BEGIN
  18. union DoubleBits {
  19. double d;
  20. uint64_t bits;
  21. };
  22. const NSComparator FSTNumberComparator = ^NSComparisonResult(NSNumber *left, NSNumber *right) {
  23. return [left compare:right];
  24. };
  25. const NSComparator FSTStringComparator = ^NSComparisonResult(NSString *left, NSString *right) {
  26. return FSTCompareStrings(left, right);
  27. };
  28. NSComparisonResult FSTCompareStrings(NSString *left, NSString *right) {
  29. // NOTE: NSLiteralSearch is necessary to compare the raw character codes. By default,
  30. // precomposed characters are considered equivalent to their decomposed equivalents.
  31. return [left compare:right options:NSLiteralSearch];
  32. }
  33. NSComparisonResult FSTCompareBools(BOOL left, BOOL right) {
  34. if (!left) {
  35. return right ? NSOrderedAscending : NSOrderedSame;
  36. } else {
  37. return right ? NSOrderedSame : NSOrderedDescending;
  38. }
  39. }
  40. NSComparisonResult FSTCompareInts(int left, int right) {
  41. if (left > right) {
  42. return NSOrderedDescending;
  43. }
  44. if (right > left) {
  45. return NSOrderedAscending;
  46. }
  47. return NSOrderedSame;
  48. }
  49. NSComparisonResult FSTCompareInt32s(int32_t left, int32_t right) {
  50. if (left > right) {
  51. return NSOrderedDescending;
  52. }
  53. if (right > left) {
  54. return NSOrderedAscending;
  55. }
  56. return NSOrderedSame;
  57. }
  58. NSComparisonResult FSTCompareInt64s(int64_t left, int64_t right) {
  59. if (left > right) {
  60. return NSOrderedDescending;
  61. }
  62. if (right > left) {
  63. return NSOrderedAscending;
  64. }
  65. return NSOrderedSame;
  66. }
  67. NSComparisonResult FSTCompareUIntegers(NSUInteger left, NSUInteger right) {
  68. if (left > right) {
  69. return NSOrderedDescending;
  70. }
  71. if (right > left) {
  72. return NSOrderedAscending;
  73. }
  74. return NSOrderedSame;
  75. }
  76. NSComparisonResult FSTCompareDoubles(double left, double right) {
  77. // NaN sorts equal to itself and before any other number.
  78. if (left < right) {
  79. return NSOrderedAscending;
  80. } else if (left > right) {
  81. return NSOrderedDescending;
  82. } else if (left == right) {
  83. return NSOrderedSame;
  84. } else {
  85. // One or both left and right is NaN.
  86. if (isnan(left)) {
  87. return isnan(right) ? NSOrderedSame : NSOrderedAscending;
  88. } else {
  89. return NSOrderedDescending;
  90. }
  91. }
  92. }
  93. static const double LONG_MIN_VALUE_AS_DOUBLE = (double)LLONG_MIN;
  94. static const double LONG_MAX_VALUE_AS_DOUBLE = (double)LLONG_MAX;
  95. NSComparisonResult FSTCompareMixed(double doubleValue, int64_t longValue) {
  96. // LLONG_MIN has an exact representation as double, so to check for a value outside the range
  97. // representable by long, we have to check for strictly less than LLONG_MIN. Note that this also
  98. // handles negative infinity.
  99. if (doubleValue < LONG_MIN_VALUE_AS_DOUBLE) {
  100. return NSOrderedAscending;
  101. }
  102. // LLONG_MAX has no exact representation as double (casting as we've done makes 2^63, which is
  103. // larger than LLONG_MAX), so consider any value greater than or equal to the threshold to be out
  104. // of range. This also handles positive infinity.
  105. if (doubleValue >= LONG_MAX_VALUE_AS_DOUBLE) {
  106. return NSOrderedDescending;
  107. }
  108. // In Firestore NaN is defined to compare before all other numbers.
  109. if (isnan(doubleValue)) {
  110. return NSOrderedAscending;
  111. }
  112. int64_t doubleAsLong = (int64_t)doubleValue;
  113. NSComparisonResult cmp = FSTCompareInt64s(doubleAsLong, longValue);
  114. if (cmp != NSOrderedSame) {
  115. return cmp;
  116. }
  117. // At this point the long representations are equal but this could be due to rounding.
  118. double longAsDouble = (double)longValue;
  119. return FSTCompareDoubles(doubleValue, longAsDouble);
  120. }
  121. NSComparisonResult FSTCompareBytes(NSData *left, NSData *right) {
  122. NSUInteger minLength = MIN(left.length, right.length);
  123. int result = memcmp(left.bytes, right.bytes, minLength);
  124. if (result < 0) {
  125. return NSOrderedAscending;
  126. } else if (result > 0) {
  127. return NSOrderedDescending;
  128. } else if (left.length < right.length) {
  129. return NSOrderedAscending;
  130. } else if (left.length > right.length) {
  131. return NSOrderedDescending;
  132. } else {
  133. return NSOrderedSame;
  134. }
  135. }
  136. /** Helper to normalize a double and then return the raw bits as a uint64_t. */
  137. uint64_t FSTDoubleBits(double d) {
  138. if (isnan(d)) {
  139. d = NAN;
  140. }
  141. union DoubleBits converter = {.d = d};
  142. return converter.bits;
  143. }
  144. BOOL FSTDoubleBitwiseEquals(double left, double right) {
  145. return FSTDoubleBits(left) == FSTDoubleBits(right);
  146. }
  147. NSUInteger FSTDoubleBitwiseHash(double d) {
  148. uint64_t bits = FSTDoubleBits(d);
  149. // Note that x ^ (x >> 32) works fine for both 32 and 64 bit definitions of NSUInteger
  150. return (((NSUInteger)bits) ^ (NSUInteger)(bits >> 32));
  151. }
  152. NS_ASSUME_NONNULL_END