Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Foundation data structures

Foundation data structures

Cyril Lashkevich

March 13, 2014
Tweet

More Decks by Cyril Lashkevich

Other Decks in Programming

Transcript

  1. 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
  2. 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];
  3. 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)
  4. NSMutableSet Unordered collection of distinct (hash/isEqual:) objects Objects should not

    change after adding to NSMutableSet Not thread safe in contrast to NSSet
  5. NSCountedSet Like NSMutableSet, but with counter associated with each object

    Mutable, but without word „mutable” in the class name Not thread safe
  6. Flexibility of CFMutableDictionary Full control of memory management, comparing and

    hashing CFMutableDictionaryRef CFDictionaryCreateMutable( CFAllocatorRef allocator, CFIndex capacity, const CFDictionaryKeyCallBacks *keyCallBacks, const CFDictionaryValueCallBacks *valueCallBacks );
  7. 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
  8. 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
  9. 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
  10. NSPointerArray Similar to NSMutableArray but with full memory management control

    Has setCount: method Can store NULL Uses NSPointerFunctions
  11. CFBitVector/CFMutableBitVector Ordered collection of 0 and 1 Can be emulated

    with NSMutableArray of @(YES) and @(NO) or NSIndexSet
  12. 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
  13. 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