NSSet实现 [英] NSSet implementation

查看:131
本文介绍了NSSet实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题只是出于好奇心,但是NSSet如何实现?它后面有什么数据结构,以及添加和删除元素的访问时间是什么?如果我不得不猜测,我会说这是某种哈希表/字典数据结构,但是在这种情况下为什么区分NSSet和NSMutableSet?

This question is just out of curiosity but, how is NSSet implemented? What data structure is behind it and what are the access times for adding and removing elements? If I had to guess, I'd say it was some sort of hashtable/dictionary data structure, but in that case why differentiate between NSSet and NSMutableSet?

推荐答案

好吧,正如Bavarious在评论中指出的,Apple的实际CoreFoundation来源是 open and available为您的阅读 NSSet <$ c之上实施$ c> CFSet ,其代码是从哈希表模板生成的(如 CFDictionary ),使用 CFBasicHash 来做这项工作。

Well, as Bavarious pointed out in a comment, Apple's actual CoreFoundation source is open and available for your perusal too. NSSet is implemented on top of CFSet, whose code is generated (as is that of CFDictionary) from a hash table template, using CFBasicHash to do the work.

可变性和不变性之间的区别似乎是结构中的标志问题( CFBasicHash.h ),从我的阅读到目前为止只影响函数调用,如 CFBasicHashAddValue ;有一个简单的检查可变性。然而,Cobbal似乎很可能是两者之间的复制/保留行为(我只是还没有读到这么远)。

The difference between mutablility and immutability seems to be the matter of a flag in the structure (line 91 of CFBasicHash.h), and from my reading so far just affects calls to functions such as CFBasicHashAddValue; there's a simple check for the mutability. It seems likely, however, that Cobbal is right about the copy/retain behavior between the two (I just haven't read that far yet).

上一页:

当我想知道实现细节时,我发现有趣和有教育意识的人可以仔细阅读 GNUstep 来源。他们当然不能保证以苹果做的方式来实现,但在某些情况下它们可能是有帮助的。他们的基金会版本: http://gnu.ethz.ch/debian/ gnustep / gnustep-base-1.20.0 / Headers / Foundation / (我希望这是最新版本,如果没有,有人请纠正我。)

PREVIOUSLY:
I find it interesting and educational occasionally to peruse the GNUstep sources when I'm wondering about implementation details. They are, of course, not at all guaranteed to be implemented the way that Apple did it, but they can be helpful in some cases. Their version of Foundation: http://gnu.ethz.ch/debian/gnustep/gnustep-base-1.20.0/Headers/Foundation/ (I hope that's the most recent version. If not, someone please correct me.)

这篇关于NSSet实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆