aboutsummaryrefslogtreecommitdiffhomepage
path: root/Firebase/Database/third_party/FImmutableSortedDictionary/FImmutableSortedDictionary/FImmutableSortedSet.m
blob: 1953af1a8bff76cf6cf75c18a956b2523255a13e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#import "FImmutableSortedSet.h"
#import "FImmutableSortedDictionary.h"

@interface FImmutableSortedSet ()

@property (nonatomic, strong) FImmutableSortedDictionary *dictionary;

@end

@implementation FImmutableSortedSet

+ (FImmutableSortedSet *)setWithKeysFromDictionary:(NSDictionary *)dictionary comparator:(NSComparator)comparator
{
    FImmutableSortedDictionary *setDict = [FImmutableSortedDictionary fromDictionary:dictionary withComparator:comparator];
    return [[FImmutableSortedSet alloc] initWithDictionary:setDict];
}

- (id)initWithDictionary:(FImmutableSortedDictionary *)dictionary
{
    self = [super init];
    if (self != nil) {
        self->_dictionary = dictionary;
    }
    return self;
}

- (BOOL)contains:(id)object
{
    return [self.dictionary contains:object];
}

- (FImmutableSortedSet *)addObject:(id)object
{
    FImmutableSortedDictionary *newDictionary = [self.dictionary insertKey:object withValue:[NSNull null]];
    if (newDictionary != self.dictionary) {
        return [[FImmutableSortedSet alloc] initWithDictionary:newDictionary];
    } else {
        return self;
    }
}

- (FImmutableSortedSet *)removeObject:(id)object
{
    FImmutableSortedDictionary *newDictionary = [self.dictionary removeObjectForKey:object];
    if (newDictionary != self.dictionary) {
        return [[FImmutableSortedSet alloc] initWithDictionary:newDictionary];
    } else {
        return self;
    }
}

- (BOOL)containsObject:(id)object
{
    return [self.dictionary contains:object];
}

- (id)firstObject
{
    return [self.dictionary minKey];
}

- (id)lastObject
{
    return [self.dictionary maxKey];
}

- (id)predecessorEntry:(id)entry
{
    return [self.dictionary getPredecessorKey:entry];
}

- (NSUInteger)count
{
    return [self.dictionary count];
}

- (BOOL)isEmpty
{
    return [self.dictionary isEmpty];
}

- (void)enumerateObjectsUsingBlock:(void (^)(id, BOOL *))block
{
    [self enumerateObjectsReverse:NO usingBlock:block];
}

- (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];
}

- (NSString *)description
{
    NSMutableString *str = [[NSMutableString alloc] init];
    __block BOOL first = YES;
    [str appendString:@"FImmutableSortedSet ( "];
    [self enumerateObjectsUsingBlock:^(id obj, BOOL *stop) {
        if (!first) {
            [str appendString:@", "];
        }
        first = NO;
        [str appendString:[NSString stringWithFormat:@"%@", obj]];
    }];
    [str appendString:@" )"];
    return str;
}

@end