FSTImmutableSortedSet.m 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. - (NSUInteger)indexOfObject:(id)object {
  63. return [self.dictionary indexOfKey:object];
  64. }
  65. - (NSUInteger)count {
  66. return [self.dictionary count];
  67. }
  68. - (BOOL)isEmpty {
  69. return [self.dictionary isEmpty];
  70. }
  71. - (void)enumerateObjectsUsingBlock:(void (^)(id, BOOL *))block {
  72. [self enumerateObjectsReverse:NO usingBlock:block];
  73. }
  74. - (void)enumerateObjectsFrom:(id)start to:(_Nullable id)end usingBlock:(void (^)(id, BOOL *))block {
  75. NSEnumerator *enumerator = [self.dictionary keyEnumeratorFrom:start to:end];
  76. id item = [enumerator nextObject];
  77. while (item) {
  78. BOOL stop = NO;
  79. block(item, &stop);
  80. if (stop) {
  81. return;
  82. }
  83. item = [enumerator nextObject];
  84. }
  85. }
  86. - (void)enumerateObjectsReverse:(BOOL)reverse usingBlock:(void (^)(id, BOOL *))block {
  87. [self.dictionary enumerateKeysAndObjectsReverse:reverse
  88. usingBlock:^(id key, id value, BOOL *stop) {
  89. block(key, stop);
  90. }];
  91. }
  92. - (NSEnumerator *)objectEnumerator {
  93. return [self.dictionary keyEnumerator];
  94. }
  95. - (NSEnumerator *)objectEnumeratorFrom:(id)startKey {
  96. return [self.dictionary keyEnumeratorFrom:startKey];
  97. }
  98. - (NSString *)description {
  99. NSMutableString *str = [[NSMutableString alloc] init];
  100. __block BOOL first = YES;
  101. [str appendString:@"FSTImmutableSortedSet ( "];
  102. [self enumerateObjectsUsingBlock:^(id obj, BOOL *stop) {
  103. if (!first) {
  104. [str appendString:@", "];
  105. }
  106. first = NO;
  107. [str appendString:[NSString stringWithFormat:@"%@", obj]];
  108. }];
  109. [str appendString:@" )"];
  110. return str;
  111. }
  112. @end
  113. NS_ASSUME_NONNULL_END