FSTImmutableSortedDictionary.m 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. #import "Firestore/third_party/Immutable/FSTImmutableSortedDictionary.h"
  2. #import "Firestore/third_party/Immutable/FSTArraySortedDictionary.h"
  3. #import "Firestore/Source/Util/FSTClasses.h"
  4. #import "Firestore/third_party/Immutable/FSTTreeSortedDictionary.h"
  5. NS_ASSUME_NONNULL_BEGIN
  6. const int kSortedDictionaryArrayToRBTreeSizeThreshold = 25;
  7. @implementation FSTImmutableSortedDictionary
  8. + (FSTImmutableSortedDictionary *)dictionaryWithComparator:(NSComparator)comparator {
  9. return [[FSTArraySortedDictionary alloc] initWithComparator:comparator];
  10. }
  11. + (FSTImmutableSortedDictionary *)dictionaryWithDictionary:(NSDictionary *)dictionary
  12. comparator:(NSComparator)comparator {
  13. if (dictionary.count <= kSortedDictionaryArrayToRBTreeSizeThreshold) {
  14. return [FSTArraySortedDictionary dictionaryWithDictionary:dictionary comparator:comparator];
  15. } else {
  16. return [FSTTreeSortedDictionary dictionaryWithDictionary:dictionary comparator:comparator];
  17. }
  18. }
  19. - (FSTImmutableSortedDictionary *)dictionaryBySettingObject:(id)aValue forKey:(id)aKey {
  20. @throw FSTAbstractMethodException(); // NOLINT
  21. }
  22. - (FSTImmutableSortedDictionary *)dictionaryByRemovingObjectForKey:(id)aKey {
  23. @throw FSTAbstractMethodException(); // NOLINT
  24. }
  25. - (BOOL)isEqual:(id)object {
  26. if (![object isKindOfClass:[FSTImmutableSortedDictionary class]]) {
  27. return NO;
  28. }
  29. // TODO(klimt): We could make this more efficient if we put the comparison inside the
  30. // implementations and short-circuit if they share the same tree node, for instance.
  31. FSTImmutableSortedDictionary *other = (FSTImmutableSortedDictionary *)object;
  32. if (self.count != other.count) {
  33. return NO;
  34. }
  35. __block BOOL isEqual = YES;
  36. [self enumerateKeysAndObjectsUsingBlock:^(id key, id value, BOOL *stop) {
  37. id otherValue = [other objectForKey:key];
  38. isEqual = isEqual && (value == otherValue || [value isEqual:otherValue]);
  39. *stop = !isEqual;
  40. }];
  41. return isEqual;
  42. }
  43. - (NSUInteger)hash {
  44. __block NSUInteger hash = 0;
  45. [self enumerateKeysAndObjectsUsingBlock:^(id key, id value, BOOL *stop) {
  46. hash = (hash * 31 + [key hash]) * 17 + [value hash];
  47. }];
  48. return hash;
  49. }
  50. - (NSString *)description {
  51. NSMutableString *str = [[NSMutableString alloc] init];
  52. __block BOOL first = YES;
  53. [str appendString:@"{ "];
  54. [self enumerateKeysAndObjectsUsingBlock:^(id key, id value, BOOL *stop) {
  55. if (!first) {
  56. [str appendString:@", "];
  57. }
  58. first = NO;
  59. [str appendString:[NSString stringWithFormat:@"%@: %@", key, value]];
  60. }];
  61. [str appendString:@" }"];
  62. return str;
  63. }
  64. - (nullable id)objectForKey:(id)key {
  65. @throw FSTAbstractMethodException(); // NOLINT
  66. }
  67. - (id)objectForKeyedSubscript:(id)key {
  68. return [self objectForKey:key];
  69. }
  70. - (nullable id)predecessorKey:(id)key {
  71. @throw FSTAbstractMethodException(); // NOLINT
  72. }
  73. - (NSUInteger)indexOfKey:(id)key {
  74. @throw FSTAbstractMethodException(); // NOLINT
  75. }
  76. - (BOOL)isEmpty {
  77. @throw FSTAbstractMethodException(); // NOLINT
  78. }
  79. - (NSUInteger)count {
  80. @throw FSTAbstractMethodException(); // NOLINT
  81. }
  82. - (id)minKey {
  83. @throw FSTAbstractMethodException(); // NOLINT
  84. }
  85. - (id)maxKey {
  86. @throw FSTAbstractMethodException(); // NOLINT
  87. }
  88. - (void)enumerateKeysAndObjectsUsingBlock:(void (^)(id, id, BOOL *))block {
  89. @throw FSTAbstractMethodException(); // NOLINT
  90. }
  91. - (void)enumerateKeysAndObjectsReverse:(BOOL)reverse usingBlock:(void (^)(id, id, BOOL *))block {
  92. @throw FSTAbstractMethodException(); // NOLINT
  93. }
  94. - (BOOL)containsKey:(id)key {
  95. @throw FSTAbstractMethodException(); // NOLINT
  96. }
  97. - (NSEnumerator *)keyEnumerator {
  98. @throw FSTAbstractMethodException(); // NOLINT
  99. }
  100. - (NSEnumerator *)keyEnumeratorFrom:(id)startKey {
  101. @throw FSTAbstractMethodException(); // NOLINT
  102. }
  103. - (NSEnumerator *)keyEnumeratorFrom:(id)startKey to:(nullable id)endKey {
  104. @throw FSTAbstractMethodException(); // NOLINT
  105. }
  106. - (NSEnumerator *)reverseKeyEnumerator {
  107. @throw FSTAbstractMethodException(); // NOLINT
  108. }
  109. - (NSEnumerator *)reverseKeyEnumeratorFrom:(id)startKey {
  110. @throw FSTAbstractMethodException(); // NOLINT
  111. }
  112. @end
  113. NS_ASSUME_NONNULL_END