From bde743ed25166a0b320ae157bfb1d68064f531c9 Mon Sep 17 00:00:00 2001 From: Gil Date: Tue, 3 Oct 2017 08:55:22 -0700 Subject: Release 4.3.0 (#327) Initial release of Firestore at 0.8.0 Bump FirebaseCommunity to 0.1.3 --- .../third_party/Immutable/FSTImmutableSortedSet.m | 144 +++++++++++++++++++++ 1 file changed, 144 insertions(+) create mode 100644 Firestore/third_party/Immutable/FSTImmutableSortedSet.m (limited to 'Firestore/third_party/Immutable/FSTImmutableSortedSet.m') diff --git a/Firestore/third_party/Immutable/FSTImmutableSortedSet.m b/Firestore/third_party/Immutable/FSTImmutableSortedSet.m new file mode 100644 index 0000000..a13a79e --- /dev/null +++ b/Firestore/third_party/Immutable/FSTImmutableSortedSet.m @@ -0,0 +1,144 @@ +#import "FSTImmutableSortedSet.h" + +#import "FSTImmutableSortedDictionary.h" + +NS_ASSUME_NONNULL_BEGIN + +@interface FSTImmutableSortedSet () +@property(nonatomic, strong) FSTImmutableSortedDictionary *dictionary; +@end + +@implementation FSTImmutableSortedSet + ++ (FSTImmutableSortedSet *)setWithComparator:(NSComparator)comparator { + return [FSTImmutableSortedSet setWithKeysFromDictionary:@{} comparator:comparator]; +} + ++ (FSTImmutableSortedSet *)setWithKeysFromDictionary:(NSDictionary *)dictionary + comparator:(NSComparator)comparator { + FSTImmutableSortedDictionary *setDict = + [FSTImmutableSortedDictionary dictionaryWithDictionary:dictionary comparator:comparator]; + return [[FSTImmutableSortedSet alloc] initWithDictionary:setDict]; +} + +// Designated initializer. +- (id)initWithDictionary:(FSTImmutableSortedDictionary *)dictionary { + self = [super init]; + if (self != nil) { + _dictionary = dictionary; + } + return self; +} + +- (BOOL)isEqual:(id)object { + if (![object isKindOfClass:[FSTImmutableSortedSet class]]) { + return NO; + } + + FSTImmutableSortedSet *other = (FSTImmutableSortedSet *)object; + + return [self.dictionary isEqual:other.dictionary]; +} + +- (NSUInteger)hash { + return [self.dictionary hash]; +} + +- (BOOL)containsObject:(id)object { + return [self.dictionary containsKey:object]; +} + +- (FSTImmutableSortedSet *)setByAddingObject:(id)object { + FSTImmutableSortedDictionary *newDictionary = + [self.dictionary dictionaryBySettingObject:[NSNull null] forKey:object]; + if (newDictionary != self.dictionary) { + return [[FSTImmutableSortedSet alloc] initWithDictionary:newDictionary]; + } else { + return self; + } +} + +- (FSTImmutableSortedSet *)setByRemovingObject:(id)object { + FSTImmutableSortedDictionary *newDictionary = + [self.dictionary dictionaryByRemovingObjectForKey:object]; + if (newDictionary != self.dictionary) { + return [[FSTImmutableSortedSet alloc] initWithDictionary:newDictionary]; + } else { + return self; + } +} + +- (id)firstObject { + return [self.dictionary minKey]; +} + +- (id)lastObject { + return [self.dictionary maxKey]; +} + +- (id)predecessorObject:(id)entry { + return [self.dictionary predecessorKey:entry]; +} + +- (NSUInteger)indexOfObject:(id)object { + return [self.dictionary indexOfKey:object]; +} + +- (NSUInteger)count { + return [self.dictionary count]; +} + +- (BOOL)isEmpty { + return [self.dictionary isEmpty]; +} + +- (void)enumerateObjectsUsingBlock:(void (^)(id, BOOL *))block { + [self enumerateObjectsReverse:NO usingBlock:block]; +} + +- (void)enumerateObjectsFrom:(id)start to:(_Nullable id)end usingBlock:(void (^)(id, BOOL *))block { + NSEnumerator *enumerator = [self.dictionary keyEnumeratorFrom:start to:end]; + id item = [enumerator nextObject]; + while (item) { + BOOL stop = NO; + block(item, &stop); + if (stop) { + return; + } + item = [enumerator nextObject]; + } +} + +- (void)enumerateObjectsReverse:(BOOL)reverse usingBlock:(void (^)(id, BOOL *))block { + [self.dictionary enumerateKeysAndObjectsReverse:reverse + usingBlock:^(id key, id value, BOOL *stop) { + block(key, stop); + }]; +} + +- (NSEnumerator *)objectEnumerator { + return [self.dictionary keyEnumerator]; +} + +- (NSEnumerator *)objectEnumeratorFrom:(id)startKey { + return [self.dictionary keyEnumeratorFrom:startKey]; +} + +- (NSString *)description { + NSMutableString *str = [[NSMutableString alloc] init]; + __block BOOL first = YES; + [str appendString:@"FSTImmutableSortedSet ( "]; + [self enumerateObjectsUsingBlock:^(id obj, BOOL *stop) { + if (!first) { + [str appendString:@", "]; + } + first = NO; + [str appendString:[NSString stringWithFormat:@"%@", obj]]; + }]; + [str appendString:@" )"]; + return str; +} + +@end + +NS_ASSUME_NONNULL_END -- cgit v1.2.3