Slide 1

Slide 1 text

Foundation data structures Cyril @notorca Lashkevich

Slide 2

Slide 2 text

Agenda NSMutableDictionary/NSMutableSet/NSCountedSet CFMutableDictionary/CFMutableSet/CFBag NSMapTable/NSHashTable/NSPointerArray CFBitVector/CFTree

Slide 3

Slide 3 text

NSMutableDictionary Key-value paris collection Key is copied < NSCopying > Implemented as hash-map (hash, isEqual:) enumerateKeysAndObjectsUsingBlock: is more optimal than keyEnumerator Not thread safe in contrast to NSDictionary

Slide 4

Slide 4 text

Hash/isEqual: invariant [a isEqual:b] -> [a hash] == [b hash] return 42; is valid hash implementation return [self.a hash] ^ [self.b hash] ^ [self.c hash];

Slide 5

Slide 5 text

typedef id (^DictBlock)(id);
 DictBlock empty = ^id (id key) { return nil; };
 DictBlock add(DictBlock d, id key, id obj) {
 return ^id (id k) {
 return [k isEqual:key] ? obj : d(k);
 };
 } DictBlock d = empty; d = add(d, @(1), @"abc"); d = add(d, @"def", @(5)); NSLog(@"%@ : %@", @(1), d(@(1))); NSLog(@"%@ : %@", @"def", d(@"def")); NSLog(@"%@ : %@", @(42), d(@(42))); 1 : abc def : 5 42 : (null)

Slide 6

Slide 6 text

NSMutableSet Unordered collection of distinct (hash/isEqual:) objects Objects should not change after adding to NSMutableSet Not thread safe in contrast to NSSet

Slide 7

Slide 7 text

NSCountedSet Like NSMutableSet, but with counter associated with each object Mutable, but without word „mutable” in the class name Not thread safe

Slide 8

Slide 8 text

CFMutableDictionary Toll-free bridge for NSMutableDictionary More flexible Sources are published Worst Expected Access O(n) O(1) Insert/ Delete O(n*n) O(1)

Slide 9

Slide 9 text

Flexibility of CFMutableDictionary Full control of memory management, comparing and hashing CFMutableDictionaryRef CFDictionaryCreateMutable( CFAllocatorRef allocator, CFIndex capacity, const CFDictionaryKeyCallBacks *keyCallBacks, const CFDictionaryValueCallBacks *valueCallBacks );

Slide 10

Slide 10 text

CFMutableDictionary callbacks CFDictionaryRetainCallBack CFDictionaryReleaseCallBack CFDictionaryCopyDescriptionCallBack CFDictionaryEqualCallBack CFDictionaryHashCallBack

Slide 11

Slide 11 text

CFMutableSet Toll-free bridge for NSMutableSet CFSetCallBacks similar to CFDictionaryValueCall
 Backs Not a toll-free bridge for NSCountedSet, but with same functionality CFBagCallbacks similar to CFSetCallBacks CFBag

Slide 12

Slide 12 text

CFBasicHash Heart of hash tables implementation in CF CFBasicHash.c, common for Dictionary/Set/Bag Hash table size is a prime number Implements different rehashing modes for collisions (but Dictionary is using only Linear) CFBasicHash.h is private and not included into SDK

Slide 13

Slide 13 text

NSMapTable/NSHashTable Same as NSMutableDictionary/NSMutableSet but there is possible to choose mode for storing keys and values: weak, copy, retain, unsafe_unretained From iOS 6.0

Slide 14

Slide 14 text

NSPointerArray Similar to NSMutableArray but with full memory management control Has setCount: method Can store NULL Uses NSPointerFunctions

Slide 15

Slide 15 text

NSCache NSMutableDictionary for caching Thread safe Does not copy keys Frees automaticaly

Slide 16

Slide 16 text

CFBitVector/CFMutableBitVector Ordered collection of 0 and 1 Can be emulated with NSMutableArray of @(YES) and @(NO) or NSIndexSet

Slide 17

Slide 17 text

CFTree Helper functions to create and manage hierarchical structures. For each node: one parent and multiple children Can be sorted A lot of boilerplate code to use

Slide 18

Slide 18 text

Bonus. CFArray Not a simple wrapper for C array! Worst access time is O(log n) Internal structure can be changed when array size changes Implemented as deque of buckets

Slide 19

Slide 19 text

Questions?