美文网首页iOS开发
CFBinaryHeapCreate的使用

CFBinaryHeapCreate的使用

作者: 熊猫人和熊猫君 | 来源:发表于2018-08-21 15:33 被阅读79次

背景

有时候我们需要维护一个,有顺序的数组,如数据源带有优先级的,低优先级排起数组前面,高优先级排素组后面。为了动态高效维护该“数组”,可以用CFBinaryHeapCreate。

CFBinaryHeapCreate

Creates a new mutable or fixed-mutable binary heap.

简要代码演示:

- (instancetype)init
{
    if (self = [super init]) {
        CFBinaryHeapCallBacks callbacks = (CFBinaryHeapCallBacks) {
            .version = 0,
            .retain = &KGModuleItemPriorityRetain,
            .release = &KGModuleItemPriorityRelease,
            .copyDescription = &CFCopyDescription,
            .compare = &KGModuleItemPriorityCompare
        };
        
        _modules = CFBinaryHeapCreate(kCFAllocatorDefault, 0, &callbacks, NULL);
        _moduleCache = [NSMutableDictionary dictionary];
    }
    
    return self;
}

static const void *KGModuleItemPriorityRetain(CFAllocatorRef allocator, const void *ptr) {
    return CFRetain(ptr);
}

static void KGModuleItemPriorityRelease(CFAllocatorRef allocator, const void *ptr) {
    CFRelease(ptr);
}

static CFComparisonResult KGModuleItemPriorityCompare(const void *ptr1, const void *ptr2, void *info)
{
    KGAppModuleModel *item1 = (__bridge KGAppModuleModel *)ptr1;
    KGAppModuleModel *item2 = (__bridge KGAppModuleModel *)ptr2;
    
    if (item1.priority < item2.priority) {  // greator first
        return kCFCompareLessThan;
    }
    
    if (item1.priority > item2.priority) {
        return kCFCompareGreaterThan;
    }
    
    if (item1.sequence > item2.sequence) {  // lesser first
        return kCFCompareLessThan;
    }
    
    if (item1.sequence < item2.sequence) {
        return kCFCompareGreaterThan;
    }
    
    return kCFCompareEqualTo;
}

- (void)kgEnumerateModulesUsingBlock:(__attribute__((noescape)) void (^)(id module, BOOL *stop))block
{
    CFIndex count = CFBinaryHeapGetCount(_modules);
    const void **list = calloc(count, sizeof(const void *));
    CFBinaryHeapGetValues(_modules, list);
    
    CFArrayRef objects = CFArrayCreate(kCFAllocatorDefault, list, count, &kCFTypeArrayCallBacks);
    
    NSArray *items = (__bridge_transfer NSArray *)objects;
    
    [items enumerateObjectsWithOptions:NSEnumerationReverse
                            usingBlock:^(KGAppModuleModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                                block(obj.module, stop);
                            }];
}

备注:

CFBinaryHeapAddValue,该函数作用,把对象加入CF管理,同时把OC管理的对象生命周期CF管理,会触发KGModuleItemPriorityRetain。

CFBinaryHeapGetValues,该函数作用是把之前加入对堆的数据取出来,存入到一个我们自定义的C数组里面去,取出来的结果是根据我们的排序规则KGModuleItemPriorityCompare排序的,是有序的。该函数会触发KGModuleItemPriorityRetain,本质是调用了CFBinaryHeapAddValue,也会触发KGModuleItemPriorityRelease。

相关文章

  • CFBinaryHeapCreate的使用

    背景 有时候我们需要维护一个,有顺序的数组,如数据源带有优先级的,低优先级排起数组前面,高优先级排素组后面。为了动...

  • iconfont的使用(下载使用)

    1、下载文件 2、在生命周期中引入项目 beforeCreate () { var domModule = ...

  • Gson的使用--使用注解

    Gson为了简化序列化和反序列化的过程,提供了很多注解,这些注解大致分为三类,我们一一的介绍一下。 自定义字段的名...

  • 记录使用iframe的使用

    默认记录一下----可以说 这是我第一次使用iframe 之前都没有使用过; 使用方式: 自己开发就用了这几个属...

  • with的使用

    下面例子可以具体说明with如何工作: 运行代码,输出如下

  • this的使用

    什么是this? this是一个关键字,这个关键字总是返回一个对象;简单说,就是返回属性或方法“当前”所在的对象。...

  • this的使用

    JS中this调用有几种情况 一:纯粹的函数调用 这是函数的最通常用法,属于全局性调用,因此this就代表全局对象...

  • ==的使用

    积累日常遇到的编码规范,良好的编码习惯,持续更新。。。 日常使用==用于判断的时候,习惯性将比较值写前面,变量写后...

  • this的使用

    1.默认绑定,就是函数立即执行。 函数立即执行就是指向window,但是如果是node环境,就是指向全局conso...

  • %in% 的使用

    写在前面:From 生信技能书向量难点之一:%in% 难点 (1)== 与 %in% 的区别== 强调位置,x和对...

网友评论

    本文标题:CFBinaryHeapCreate的使用

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