aboutsummaryrefslogtreecommitdiffhomepage
path: root/Firebase/Database/third_party/FImmutableSortedDictionary/FImmutableSortedDictionary/FImmutableSortedDictionary.m
blob: 659c63b142b2f3dabdd2d99de149e15e037939b9 (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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#import "FImmutableSortedDictionary.h"
#import "FArraySortedDictionary.h"
#import "FTreeSortedDictionary.h"

#define THROW_ABSTRACT_METHOD_EXCEPTION(sel) do { \
  @throw [NSException exceptionWithName:NSInternalInconsistencyException \
  reason:[NSString stringWithFormat:@"You must override %@ in a subclass", NSStringFromSelector(sel)] \
  userInfo:nil]; \
} while(0)

@implementation FImmutableSortedDictionary

+ (FImmutableSortedDictionary *)dictionaryWithComparator:(NSComparator)comparator
{
    return [[FArraySortedDictionary alloc] initWithComparator:comparator];
}

+ (FImmutableSortedDictionary *)fromDictionary:(NSDictionary *)dictionary withComparator:(NSComparator)comparator
{
    if (dictionary.count <= SORTED_DICTIONARY_ARRAY_TO_RB_TREE_SIZE_THRESHOLD) {
        return [FArraySortedDictionary fromDictionary:dictionary withComparator:comparator];
    } else {
        return [FTreeSortedDictionary fromDictionary:dictionary withComparator:comparator];
    }
}

- (FImmutableSortedDictionary *) insertKey:(id)aKey withValue:(id)aValue {
    THROW_ABSTRACT_METHOD_EXCEPTION(@selector(insertKey:withValue:));
}

- (FImmutableSortedDictionary *) removeKey:(id)aKey {
    THROW_ABSTRACT_METHOD_EXCEPTION(@selector(removeKey:));
}

- (id) get:(id) key {
    THROW_ABSTRACT_METHOD_EXCEPTION(@selector(get:));
}

- (id) getPredecessorKey:(id) key {
    THROW_ABSTRACT_METHOD_EXCEPTION(@selector(getPredecessorKey:));
}

- (BOOL) isEmpty {
    THROW_ABSTRACT_METHOD_EXCEPTION(@selector(isEmpty));
}

- (int) count {
    THROW_ABSTRACT_METHOD_EXCEPTION(@selector((count)));
}

- (id) minKey {
    THROW_ABSTRACT_METHOD_EXCEPTION(@selector(minKey));
}

- (id) maxKey {
    THROW_ABSTRACT_METHOD_EXCEPTION(@selector(maxKey));
}

- (void) enumerateKeysAndObjectsUsingBlock:(void (^)(id, id, BOOL *))block {
    THROW_ABSTRACT_METHOD_EXCEPTION(@selector(enumerateKeysAndObjectsUsingBlock:));
}

- (void) enumerateKeysAndObjectsReverse:(BOOL)reverse usingBlock:(void (^)(id, id, BOOL *))block {
    THROW_ABSTRACT_METHOD_EXCEPTION(@selector(enumerateKeysAndObjectsReverse:usingBlock:));
}

- (BOOL) contains:(id)key {
    THROW_ABSTRACT_METHOD_EXCEPTION(@selector(contains:));
}

- (NSEnumerator *) keyEnumerator {
    THROW_ABSTRACT_METHOD_EXCEPTION(@selector(keyEnumerator));
}

- (NSEnumerator *) keyEnumeratorFrom:(id)startKey {
    THROW_ABSTRACT_METHOD_EXCEPTION(@selector(keyEnumeratorFrom:));
}

- (NSEnumerator *) reverseKeyEnumerator {
    THROW_ABSTRACT_METHOD_EXCEPTION(@selector(reverseKeyEnumerator));
}

- (NSEnumerator *) reverseKeyEnumeratorFrom:(id)startKey {
    THROW_ABSTRACT_METHOD_EXCEPTION(@selector(reverseKeyEnumeratorFrom:));
}

- (BOOL)isEqual:(id)object {
    if (![object isKindOfClass:[FImmutableSortedDictionary class]]) {
        return NO;
    }
    FImmutableSortedDictionary *other = (FImmutableSortedDictionary *)object;
    if (self.count != other.count) {
        return NO;
    }
    __block BOOL isEqual = YES;
    [self enumerateKeysAndObjectsUsingBlock:^(id key, id value, BOOL *stop) {
        id otherValue = [other objectForKey:key];
        isEqual = isEqual && (value == otherValue || [value isEqual:otherValue]);
        *stop = !isEqual;
    }];
    return isEqual;
}

- (NSUInteger)hash {
    __block NSUInteger hash = 0;
    [self enumerateKeysAndObjectsUsingBlock:^(id key, id value, BOOL *stop) {
        hash = (hash * 31 + [key hash]) * 17 + [value hash];
    }];
    return hash;
}

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

#pragma mark -
#pragma mark Methods similar to NSMutableDictionary

- (FImmutableSortedDictionary *) setObject:(__unsafe_unretained id)anObject forKey:(__unsafe_unretained id)aKey {
    return [self insertKey:aKey withValue:anObject];
}

- (FImmutableSortedDictionary *) removeObjectForKey:(__unsafe_unretained id)aKey {
    return [self removeKey:aKey];
}

- (id) objectForKey:(__unsafe_unretained id)key {
    return [self get:key];
}

@end