美文网首页iCoder
iOS数据结构NSMapTable,NSHashTable和NS

iOS数据结构NSMapTable,NSHashTable和NS

作者: Trigger_o | 来源:发表于2020-09-10 17:13 被阅读0次

map就是键值对集合

  • HashMap:最常用,根据key的HashCode 值来存储数据,根据key可以直接获取它的Value,同时它具有很快的访问速度。HashMap最多只允许一条记录的key值为Null(多条会覆盖);允许多条记录的Value为 Null。非同步的。
  • TreeMap: 能够把它保存的记录根据key排序,默认是按升序排序,也可以指定排序的比较器,当用Iterator 遍历TreeMap时,得到的记录是排过序的。TreeMap不允许key的值为null。非同步的。
  • Hashtable: 与 HashMap类似,不同的是:key和value的值均不允许为null;它支持线程的同步,即任一时刻只有一个线程能写Hashtable,因此也导致了Hashtale在写入时会比较慢。
  • LinkedHashMap: 保存了记录的插入顺序,在用Iterator遍历LinkedHashMap时,先得到的记录肯定是先插入的.在遍历的时候会比HashMap慢。key和value均允许为空,非同步的。

hashMap是数组和链表的组合,数组是主体,链表用来处理其他问题如hash值冲突
hashMap是如何工作的

在iOS中的应用:

  • NSMapTable
    NSMapTable是NSDictionary的通用版,是可变的,可以存储任意的指针,通过指针来进行相等性和散列检查
    NSDictionary必须存放对象,由于对象存储在特定位置,因此NSDictionary 中要求 key 的值不能改变,否则value对象就找不到了,所以key会被copy到特定的位置,所以key还必须得遵循NSCopying协议.也就是说NSDictionary的key需要是字符串对象或者NSNumber对象,不宜使用其他复杂的对象间的隐射;
    NSMapTable可以实现object - object的隐射
//创建一个NSMapTable对象
NSMapTable *mapTable = [NSMapTable mapTableWithKeyOptions:NSMapTableCopyIn valueOptions:NSMapTableWeakMemory];
[mapTable setObject:self forKey:@"delegate"];

key和value的内存策略

static const NSPointerFunctionsOptions NSMapTableStrongMemory API_AVAILABLE(macos(10.5), ios(6.0), watchos(2.0), tvos(9.0)) = NSPointerFunctionsStrongMemory;
static const NSPointerFunctionsOptions NSMapTableZeroingWeakMemory API_DEPRECATED("GC no longer supported", macos(10.5,10.8)) API_UNAVAILABLE(ios, watchos, tvos) = NSPointerFunctionsZeroingWeakMemory;
static const NSPointerFunctionsOptions NSMapTableCopyIn API_AVAILABLE(macos(10.5), ios(6.0), watchos(2.0), tvos(9.0)) = NSPointerFunctionsCopyIn;
static const NSPointerFunctionsOptions NSMapTableObjectPointerPersonality API_AVAILABLE(macos(10.5), ios(6.0), watchos(2.0), tvos(9.0)) = NSPointerFunctionsObjectPointerPersonality;
static const NSPointerFunctionsOptions NSMapTableWeakMemory API_AVAILABLE(macos(10.8), ios(6.0), watchos(2.0), tvos(9.0)) = NSPointerFunctionsWeakMemory;

NSPointerFunctionsStrongMemory : 引用计数+1
NSMapTableZeroingWeakMemory :已废弃
NSPointerFunctionsCopyIn : 遵循NSCopying协议 copy一份
NSPointerFunctionsObjectPointerPersonality : 使用偏移后指针,进行hash和直接比较等同性
NSPointerFunctionsWeakMemory : 保存一个week指针,不会增加引用计数

当key或value使用NSPointerFunctionsWeakMemory时,只要有一个被释放,整项键值对都会被移除.

  • NSHashTable
    NSHashTable是NSSet的通用版,可变,可以存储任意的指针,通过指针来进行相等性和散列检查,关键同样在于内存策略
NSHashTable *hashTable = [NSHashTable hashTableWithOptions:NSPointerFunctionsCopyIn];
    [hashTable addObject:@"hello"];
    [hashTable addObject:@10];
    [hashTable addObject:@"world"];
    [hashTable removeObject:@"world"];
    NSLog(@"Members: %@", [hashTable allObjects]);

-NSPointerArray
NSPointerArray是NSArray的通用版,可变,可以存储NULL,并且算作一个元素,可以直接set count,可以存放所有指针类型(void *),并且遵循NSFastEnumeration可以快速遍历.

NSPointerArray *pointerArray = [[NSPointerArray alloc]initWithOptions:NSPointerFunctionsStrongMemory];
    pointerArray.count = 5;
    [pointerArray allObjects];
    // 指定索引处的指针
    void *point = [pointerArray pointerAtIndex:0];//nil
    // 数组中添加指针对象
    [pointerArray addPointer:@"2"];//(2)
    // 移除指定索引处的元素
    [pointerArray removePointerAtIndex:0];//(2)
    // 指定索引出插入元素
    [pointerArray insertPointer:@"1" atIndex:0];//(1,2)
    // 替换指定索引处的对象
    [pointerArray replacePointerAtIndex:0 withPointer:@"2"];//(2,2)
    // 删除数组中的nil值
    [pointerArray compact];
    // 获取数组的功能项
    NSPointerFunctions *Functions = [pointerArray pointerFunctions];

相关文章

网友评论

    本文标题:iOS数据结构NSMapTable,NSHashTable和NS

    本文链接:https://www.haomeiwen.com/subject/gcppektx.html