FSTImmutableSortedSet.m 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. #import "Firestore/third_party/Immutable/FSTImmutableSortedSet.h"
  2. #import "Firestore/third_party/Immutable/FSTImmutableSortedDictionary.h"
  3. NS_ASSUME_NONNULL_BEGIN
  4. @interface FSTImmutableSortedSet ()
  5. @property(nonatomic, strong) FSTImmutableSortedDictionary *dictionary;
  6. @end
  7. @implementation FSTImmutableSortedSet
  8. + (FSTImmutableSortedSet *)setWithComparator:(NSComparator)comparator {
  9. return [FSTImmutableSortedSet setWithKeysFromDictionary:@{} comparator:comparator];
  10. }
  11. + (FSTImmutableSortedSet *)setWithKeysFromDictionary:(NSDictionary *)dictionary
  12. comparator:(NSComparator)comparator {
  13. FSTImmutableSortedDictionary *setDict =
  14. [FSTImmutableSortedDictionary dictionaryWithDictionary:dictionary comparator:comparator];
  15. return [[FSTImmutableSortedSet alloc] initWithDictionary:setDict];
  16. }
  17. // Designated initializer.
  18. - (id)initWithDictionary:(FSTImmutableSortedDictionary *)dictionary {
  19. self = [super init];
  20. if (self != nil) {
  21. _dictionary = dictionary;
  22. }
  23. return self;
  24. }
  25. - (BOOL)isEqual:(id)object {
  26. if (![object isKindOfClass:[FSTImmutableSortedSet class]]) {
  27. return NO;
  28. }
  29. FSTImmutableSortedSet *other = (FSTImmutableSortedSet *)object;
  30. return [self.dictionary isEqual:other.dictionary];
  31. }
  32. - (NSUInteger)hash {
  33. return [self.dictionary hash];
  34. }
  35. - (BOOL)containsObject:(id)object {
  36. return [self.dictionary containsKey:object];
  37. }
  38. - (FSTImmutableSortedSet *)setByAddingObject:(id)object {
  39. FSTImmutableSortedDictionary *newDictionary =
  40. [self.dictionary dictionaryBySettingObject:[NSNull null] forKey:object];
  41. if (newDictionary != self.dictionary) {
  42. return [[FSTImmutableSortedSet alloc] initWithDictionary:newDictionary];
  43. } else {
  44. return self;
  45. }
  46. }
  47. - (FSTImmutableSortedSet *)setByRemovingObject:(id)object {
  48. FSTImmutableSortedDictionary *newDictionary =
  49. [self.dictionary dictionaryByRemovingObjectForKey:object];
  50. if (newDictionary != self.dictionary) {
  51. return [[FSTImmutableSortedSet alloc] initWithDictionary:newDictionary];
  52. } else {
  53. return self;
  54. }
  55. }
  56. - (id)firstObject {
  57. return [self.dictionary minKey];
  58. }
  59. - (id)lastObject {
  60. return [self.dictionary maxKey];
  61. }
  62. - (id)predecessorObject:(id)entry {
  63. return [self.dictionary predecessorKey:entry];
  64. }
  65. - (NSUInteger)indexOfObject:(id)object {
  66. return [self.dictionary indexOfKey:object];
  67. }
  68. - (NSUInteger)count {
  69. return [self.dictionary count];
  70. }
  71. - (BOOL)isEmpty {
  72. return [self.dictionary isEmpty];
  73. }
  74. - (void)enumerateObjectsUsingBlock:(void (^)(id, BOOL *))block {
  75. [self enumerateObjectsReverse:NO usingBlock:block];
  76. }
  77. - (void)enumerateObjectsFrom:(id)start to:(_Nullable id)end usingBlock:(void (^)(id, BOOL *))block {
  78. NSEnumerator *enumerator = [self.dictionary keyEnumeratorFrom:start to:end];
  79. id item = [enumerator nextObject];
  80. while (item) {
  81. BOOL stop = NO;
  82. block(item, &stop);
  83. if (stop) {
  84. return;
  85. }
  86. item = [enumerator nextObject];
  87. }
  88. }
  89. - (void)enumerateObjectsReverse:(BOOL)reverse usingBlock:(void (^)(id, BOOL *))block {
  90. [self.dictionary enumerateKeysAndObjectsReverse:reverse
  91. usingBlock:^(id key, id value, BOOL *stop) {
  92. block(key, stop);
  93. }];
  94. }
  95. - (NSEnumerator *)objectEnumerator {
  96. return [self.dictionary keyEnumerator];
  97. }
  98. - (NSEnumerator *)objectEnumeratorFrom:(id)startKey {
  99. return [self.dictionary keyEnumeratorFrom:startKey];
  100. }
  101. - (NSString *)description {
  102. NSMutableString *str = [[NSMutableString alloc] init];
  103. __block BOOL first = YES;
  104. [str appendString:@"FSTImmutableSortedSet ( "];
  105. [self enumerateObjectsUsingBlock:^(id obj, BOOL *stop) {
  106. if (!first) {
  107. [str appendString:@", "];
  108. }
  109. first = NO;
  110. [str appendString:[NSString stringWithFormat:@"%@", obj]];
  111. }];
  112. [str appendString:@" )"];
  113. return str;
  114. }
  115. @end
  116. NS_ASSUME_NONNULL_END