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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
|
#import "Firestore/third_party/Immutable/FSTTreeSortedDictionary.h"
#import "Firestore/third_party/Immutable/FSTLLRBEmptyNode.h"
#import "Firestore/third_party/Immutable/FSTLLRBValueNode.h"
#import "Firestore/third_party/Immutable/FSTTreeSortedDictionaryEnumerator.h"
NS_ASSUME_NONNULL_BEGIN
@interface FSTTreeSortedDictionary ()
- (FSTTreeSortedDictionary *)dictionaryBySettingObject:(id)aValue forKey:(id)aKey;
@property(nonatomic, strong) id<FSTLLRBNode> root;
@property(nonatomic, copy, readwrite) NSComparator comparator;
@end
@implementation FSTTreeSortedDictionary
+ (FSTTreeSortedDictionary *)dictionaryWithDictionary:(NSDictionary *)dictionary
comparator:(NSComparator)comparator {
__block FSTTreeSortedDictionary *dict =
[[FSTTreeSortedDictionary alloc] initWithComparator:comparator];
[dictionary enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
dict = [dict dictionaryBySettingObject:obj forKey:key];
}];
return dict;
}
- (id)initWithComparator:(NSComparator)aComparator {
return [self initWithComparator:aComparator withRoot:[FSTLLRBEmptyNode emptyNode]];
}
// Designated initializer.
- (id)initWithComparator:(NSComparator)aComparator withRoot:(id<FSTLLRBNode>)aRoot {
self = [super init];
if (self) {
self.root = aRoot;
self.comparator = aComparator;
}
return self;
}
/**
* Returns a copy of the map, with the specified key/value added or replaced.
*/
- (FSTTreeSortedDictionary *)dictionaryBySettingObject:(id)aValue forKey:(id)aKey {
return [[FSTTreeSortedDictionary alloc]
initWithComparator:self.comparator
withRoot:[[self.root insertKey:aKey forValue:aValue withComparator:self.comparator]
copyWith:nil
withValue:nil
withColor:FSTLLRBColorBlack
withLeft:nil
withRight:nil]];
}
- (FSTTreeSortedDictionary *)dictionaryByRemovingObjectForKey:(id)aKey {
// Remove is somewhat expensive even if the key doesn't exist (the tree does rebalancing and
// stuff). So avoid it.
if (![self containsKey:aKey]) {
return self;
} else {
return [[FSTTreeSortedDictionary alloc]
initWithComparator:self.comparator
withRoot:[[self.root remove:aKey withComparator:self.comparator]
copyWith:nil
withValue:nil
withColor:FSTLLRBColorBlack
withLeft:nil
withRight:nil]];
}
}
- (nullable id)objectForKey:(id)key {
NSComparisonResult cmp;
id<FSTLLRBNode> node = self.root;
while (![node isEmpty]) {
cmp = self.comparator(key, node.key);
if (cmp == NSOrderedSame) {
return node.value;
} else if (cmp == NSOrderedAscending) {
node = node.left;
} else {
node = node.right;
}
}
return nil;
}
- (NSUInteger)indexOfKey:(id)key {
NSUInteger prunedNodes = 0;
id<FSTLLRBNode> node = self.root;
while (![node isEmpty]) {
NSComparisonResult cmp = self.comparator(key, node.key);
if (cmp == NSOrderedSame) {
return prunedNodes + node.left.count;
} else if (cmp == NSOrderedAscending) {
node = node.left;
} else if (cmp == NSOrderedDescending) {
prunedNodes += node.left.count + 1;
node = node.right;
}
}
return NSNotFound;
}
- (BOOL)isEmpty {
return [self.root isEmpty];
}
- (NSUInteger)count {
return [self.root count];
}
- (id)minKey {
return [self.root minKey];
}
- (id)maxKey {
return [self.root maxKey];
}
- (void)enumerateKeysAndObjectsUsingBlock:(void (^)(id, id, BOOL *))block {
[self enumerateKeysAndObjectsReverse:NO usingBlock:block];
}
- (void)enumerateKeysAndObjectsReverse:(BOOL)reverse usingBlock:(void (^)(id, id, BOOL *))block {
if (reverse) {
__block BOOL stop = NO;
[self.root reverseTraversal:^BOOL(id key, id value) {
block(key, value, &stop);
return stop;
}];
} else {
__block BOOL stop = NO;
[self.root inorderTraversal:^BOOL(id key, id value) {
block(key, value, &stop);
return stop;
}];
}
}
- (BOOL)containsKey:(id)key {
return ([self objectForKey:key] != nil);
}
- (NSEnumerator *)keyEnumerator {
return [[FSTTreeSortedDictionaryEnumerator alloc] initWithImmutableSortedDictionary:self
startKey:nil
endKey:nil
isReverse:NO];
}
- (NSEnumerator *)keyEnumeratorFrom:(id)startKey {
return [[FSTTreeSortedDictionaryEnumerator alloc] initWithImmutableSortedDictionary:self
startKey:startKey
endKey:nil
isReverse:NO];
}
- (NSEnumerator *)keyEnumeratorFrom:(id)startKey to:(nullable id)endKey {
return [[FSTTreeSortedDictionaryEnumerator alloc] initWithImmutableSortedDictionary:self
startKey:startKey
endKey:endKey
isReverse:NO];
}
- (NSEnumerator *)reverseKeyEnumerator {
return [[FSTTreeSortedDictionaryEnumerator alloc] initWithImmutableSortedDictionary:self
startKey:nil
endKey:nil
isReverse:YES];
}
- (NSEnumerator *)reverseKeyEnumeratorFrom:(id)startKey {
return [[FSTTreeSortedDictionaryEnumerator alloc] initWithImmutableSortedDictionary:self
startKey:startKey
endKey:nil
isReverse:YES];
}
#pragma mark -
#pragma mark Tree Builder
// Code to efficiently build a red black tree.
typedef struct {
unsigned int bits;
unsigned short count;
unsigned short current;
} Base12List;
unsigned int LogBase2(unsigned int num) {
return (unsigned int)(log(num) / log(2));
}
/**
* Works like an iterator, so it moves to the next bit. Do not call more than list->count times.
* @return whether or not the next bit is a 1 in base {1,2}.
*/
BOOL Base12ListNext(Base12List *list) {
BOOL result = !(list->bits & (0x1 << list->current));
list->current--;
return result;
}
static inline unsigned BitMask(int x) {
return (x >= sizeof(unsigned) * CHAR_BIT) ? (unsigned)-1 : (1U << x) - 1;
}
/**
* We represent the base{1,2} number as the combination of a binary number and a number of bits that
* we care about. We iterate backwards, from most significant bit to least, to build up the llrb
* nodes. 0 base 2 => 1 base {1,2}, 1 base 2 => 2 base {1,2}
*/
Base12List *NewBase12List(unsigned int length) {
size_t sz = sizeof(Base12List);
Base12List *list = calloc(1, sz);
// Calculate the number of bits that we care about
list->count = (unsigned short)LogBase2(length + 1);
unsigned int mask = BitMask(list->count);
list->bits = (length + 1) & mask;
list->current = list->count - 1;
return list;
}
void FreeBase12List(Base12List *list) {
free(list);
}
+ (id<FSTLLRBNode>)buildBalancedTree:(NSArray *)keys
dictionary:(NSDictionary *)dictionary
subArrayStartIndex:(NSUInteger)startIndex
length:(NSUInteger)length {
length = MIN(keys.count - startIndex, length); // Bound length by the actual length of the array
if (length == 0) {
return nil;
} else if (length == 1) {
id key = keys[startIndex];
return [[FSTLLRBValueNode alloc] initWithKey:key
withValue:dictionary[key]
withColor:FSTLLRBColorBlack
withLeft:nil
withRight:nil];
} else {
NSUInteger middle = length / 2;
id<FSTLLRBNode> left = [FSTTreeSortedDictionary buildBalancedTree:keys
dictionary:dictionary
subArrayStartIndex:startIndex
length:middle];
id<FSTLLRBNode> right = [FSTTreeSortedDictionary buildBalancedTree:keys
dictionary:dictionary
subArrayStartIndex:(startIndex + middle + 1)
length:middle];
id key = keys[startIndex + middle];
return [[FSTLLRBValueNode alloc] initWithKey:key
withValue:dictionary[key]
withColor:FSTLLRBColorBlack
withLeft:left
withRight:right];
}
}
+ (nullable id<FSTLLRBNode>)rootFrom12List:(Base12List *)base12List
keyList:(NSArray *)keyList
dictionary:(NSDictionary *)dictionary {
__block FSTLLRBValueNode *root = nil;
__block FSTLLRBValueNode *node = nil;
__block NSUInteger index = keyList.count;
void (^buildPennant)(FSTLLRBColor, NSUInteger) = ^(FSTLLRBColor color, NSUInteger chunkSize) {
NSUInteger startIndex = index - chunkSize + 1;
index -= chunkSize;
id key = keyList[index];
FSTLLRBValueNode *childTree = [self buildBalancedTree:keyList
dictionary:dictionary
subArrayStartIndex:startIndex
length:(chunkSize - 1)];
FSTLLRBValueNode *pennant = [[FSTLLRBValueNode alloc] initWithKey:key
withValue:dictionary[key]
withColor:color
withLeft:nil
withRight:childTree];
if (node) {
// This is the only place this property is set.
node.left = pennant;
node = pennant;
} else {
root = pennant;
node = pennant;
}
};
for (int i = 0; i < base12List->count; ++i) {
BOOL isOne = Base12ListNext(base12List);
NSUInteger chunkSize = (NSUInteger)pow(2.0, base12List->count - (i + 1));
if (isOne) {
buildPennant(FSTLLRBColorBlack, chunkSize);
} else {
buildPennant(FSTLLRBColorBlack, chunkSize);
buildPennant(FSTLLRBColorRed, chunkSize);
}
}
return root;
}
/**
* Uses the algorithm linked here:
* http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.46.1458
*/
+ (FSTImmutableSortedDictionary *)fromDictionary:(NSDictionary *)dictionary
withComparator:(NSComparator)comparator {
// Steps:
// 0. Sort the array
// 1. Calculate the 1-2 number
// 2. Build From 1-2 number
// 0. for each digit in 1-2 number
// 0. calculate chunk size
// 1. build 1 or 2 pennants of that size
// 2. attach pennants and update node pointer
// 1. return root
NSMutableArray *sortedKeyList = [NSMutableArray arrayWithCapacity:dictionary.count];
[dictionary enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
[sortedKeyList addObject:key];
}];
[sortedKeyList sortUsingComparator:comparator];
[sortedKeyList enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
if (idx > 0) {
if (comparator(sortedKeyList[idx - 1], obj) != NSOrderedAscending) {
[NSException raise:NSInvalidArgumentException
format:
@"Can't create FSTImmutableSortedDictionary "
@"with keys with same ordering!"];
}
}
}];
Base12List *list = NewBase12List((unsigned int)sortedKeyList.count);
id<FSTLLRBNode> root = [self rootFrom12List:list keyList:sortedKeyList dictionary:dictionary];
FreeBase12List(list);
if (root != nil) {
return [[FSTTreeSortedDictionary alloc] initWithComparator:comparator withRoot:root];
} else {
return [[FSTTreeSortedDictionary alloc] initWithComparator:comparator];
}
}
@end
NS_ASSUME_NONNULL_END
|