命名以可预测顺序存储键的字典结构? [英] Naming a dictionary structure that stores keys in a predictable order?

查看:24
本文介绍了命名以可预测顺序存储键的字典结构?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

注意:虽然我的特定上下文是 Objective-C,但我的问题实际上超越了编程语言的选择.此外,我将其标记为主观",因为否则肯定会有人抱怨,但我个人认为这几乎完全是客观的.另外,我知道这个相关的SO问题,但因为这是一个更大的问题,我认为最好把它作为一个单独的问题.请不要在没有完全阅读和理解问题的情况下批评问题.谢谢!

Note: Although my particular context is Objective-C, my question actually transcends programming language choice. Also, I tagged it as "subjective" since someone is bound to complain otherwise, but I personally think it's almost entirely objective. Also, I'm aware of this related SO question, but since this was a bigger issue, I thought it better to make this a separate question. Please don't criticize the question without reading and understanding it fully. Thanks!

我们大多数人都熟悉字典 抽象数据类型 存储键值关联,根据我们选择的语言,我们将其称为映射、字典、关联数组、哈希等.字典的简单定义可以概括为三个属性:

Most of us are familiar with the dictionary abstract data type that stores key-value associations, whether we call it a map, dictionary, associative array, hash, etc. depending on our language of choice. A simple definition of a dictionary can be summarized by three properties:

  1. 通过键(而不是通过索引,如数组)访问值.
  2. 每个键都与一个值相关联.
  3. 每个键都必须是唯一的.

任何其他属性都可以说是为特定目的提供便利或特化.例如,某些语言(尤其是 PHP 和 Python 等脚本语言)模糊了字典和数组之间的界限,并且确实为字典提供了排序.尽管这很有用,但这样的添加并不是字典的基本特征.从纯粹意义上讲,字典的实际实现细节是无关紧要的.

Any other properties are arguably conveniences or specializations for a particular purpose. For example, some languages (especially scripting languages such as PHP and Python) blur the line between dictionaries and arrays and do provide ordering for dictionaries. As useful as this can be, such additions are not a fundamental characteristics of a dictionary. In a pure sense, the actual implementation details of a dictionary are irrelevant.

对于我的问题,最重要的观察是未定义枚举键的顺序——字典可以按它认为最方便的任何顺序提供键,这取决于客户根据需要组织它们.

For my question, the most important observation is that the order in which keys are enumerated is not defined — a dictionary may provide keys in whatever order it finds most convenient, and it is up to the client to organize them as desired.

创建了自定义字典,这些字典强加了特定的键顺序,包括自然排序(基于对象比较)和插入顺序.很明显,将前者命名为 SortedDictionary 上的某个变体(我实际上已经实现了),但后者问题更大.我见过 LinkedHashMapLinkedMap (Java), OrderedDictionary (.NET)、OrderedDictionary (Flash), OrderedDict (Python) 和 OrderedDictionary(目标 C).其中一些更加成熟,一些更加概念验证.

I've created custom dictionaries that impose specific key orderings, including natural sorted order (based on object comparisons) and insertion order. It's obvious to name the former some variant on SortedDictionary (which I've actually already implemented), but the latter is more problematic. I've seen LinkedHashMap and LinkedMap (Java), OrderedDictionary (.NET), OrderedDictionary (Flash), OrderedDict (Python), and OrderedDictionary (Objective-C). Some of these are more mature, some are more proof-of-concept.

LinkedHashMap 是根据 Java 集合传统中的实现命名的——linked"是因为它使用双向链表来跟踪插入顺序,hash"是因为它是 HashMap 的子类.除了用户不需要担心这一点之外,类名甚至没有真正表明它的作用.使用 ordered 似乎是现有代码之间的共识,但关于这个主题的网络搜索也揭示了有序"和排序"之间可以理解的混淆,我也有同样的感觉..NET 实现甚至对明显的用词不当进行了评论,并建议它应该是IndexedDictionary",因为您可以在排序中的特定点检索和插入对象.

LinkedHashMap is named according to implementation in the tradition of Java collections — "linked" because it uses a doubly-linked list to track insertion order, and "hash" because it subclasses HashMap. Besides the fact that user shouldn't need to worry about that, the class name doesn't really even indicate what it does. Using ordered seems like the consensus among existing code, but web searches on this topic also revealed understandable confusion between "ordered" and "sorted", and I feel the same. The .NET implementation even has a comment about the apparent misnomer, and suggests that it should be "IndexedDictionary" instead, owing to the fact that you can retrieve and insert objects at a specific point in the ordering.

我正在设计一个框架和 API,我想尽可能聪明地命名该类.从我的角度来看,indexed 可能会起作用(取决于人们如何解释它,以及基于字典的宣传功能),ordered 是不精确的,并且有太多的潜力混淆,并且 linked 是正确的"(向 Monty Python 道歉).;-)

I'm designing a framework and APIs and I want to name the class as intelligently as possible. From my standpoint, indexed would probably work (depending on how people interpret it, and based on the advertised functionality of the dictionary), ordered is imprecise and has too much potential for confusion, and linked "is right out" (apologies to Monty Python). ;-)

作为用户,您认为哪个名称最有意义?是否有一个特定的名称准确地说明了该类的作用?(如果合适,我不反对使用稍长的名称,如 InsertionOrderDictionary.)

As a user, what name would make the most sense to you? Is there a particular name that says exactly what the class does? (I'm not averse to using slightly longer names like InsertionOrderDictionary if appropriate.)

另一个很有可能(在我下面的回答中讨论)是IndexedDictionary.我真的不喜欢插入顺序",因为如果您允许用户在特定索引处插入键、对键重新排序等,这没有意义.

Another strong possibility (discussed in my answer below) is IndexedDictionary. I don't really like "insertion order" because it doesn't make sense if you allow the user to insert keys at a specific index, reorder the keys, etc.

推荐答案

我投票给 OrderedDictionary,原因如下:

I vote OrderedDictionary, for the following reasons:

Indexed"从不在 Cocoa 类中使用,除非在一个实例中.它总是作为名词出现(NSIndexSet、NSIndexPath、objectAtIndex: 等).当Index"作为动词出现时只有一个实例,它位于 NSPropertyDescription 的indexed"属性上:isIndexed 和 setIndexed.NSPropertyDescription 大致类似于数据库中的表列,其中索引"是指优化以加快搜索时间.因此,当 NSPropertyDescription 成为 Core Data 框架的一部分时,isIndexed"和setIndexed"将等同于 SQL 数据库中的索引是有道理的.因此,将其称为IndexedDictionary"似乎是多余的,因为创建数据库中的索引是为了加快查找时间,但是字典已经具有 O(1) 查找时间.但是,将其称为IndexDictionary"也用词不当,因为 Cocoa 中的索引"指的是位置,而不是顺序.两者在语义上是不同的.

"Indexed" is never used in Cocoa classes, except in one instance. It always appears as a noun (NSIndexSet, NSIndexPath, objectAtIndex:, etc). There is only one instance when "Index" appears as a verb, which is on NSPropertyDescription's "indexed" property: isIndexed and setIndexed. NSPropertyDescription is roughly analogous to a table column in a database, where "indexing" refers to optimizing to speed up search times. It would therefore make sense that with NSPropertyDescription being part of the Core Data framework, that "isIndexed" and "setIndexed" would be equivalent to an index in a SQL database. Therefore, to call it "IndexedDictionary" would seem redundant, since indices in databases are created to speed up lookup time, but a dictionary already has O(1) lookup time. However, to call it "IndexDictionary" would also be a misnomer, since an "index" in Cocoa refers to position, not order. The two are semantically different.

我理解您对OrderedDictionary"的担忧,但在 Cocoa 中已经有了先例.当用户想要维护一个特定的序列时,他们使用有序":-[NSApplicationorderedDocuments]、-[NSWindoworderedIndex]、-[NSApplicationorderedWindows]等.所以,John Pirie的想法大多是正确的.

I understand your concern over "OrderedDictionary", but the precedent has already been set in Cocoa. When users want to maintain a specific sequence, they use "ordered": -[NSApplication orderedDocuments], -[NSWindow orderedIndex], -[NSApplication orderedWindows], etc. So, John Pirie has mostly the right idea.

但是,您不希望插入字典成为用户的负担.他们希望创建一个字典一次,然后让它保持适当的顺序.他们甚至不想以特定顺序请求对象.订单说明应在初始化时完成.

However, you don't want to make insertion into the dictionary a burden on your users. They'll want to create a dictionary once and then have it maintain an appropriate order. They won't even want to request objects in a specific order. Order specification should be done during initialization.

因此,我建议将 OrderedDictonary 设为类簇,并带有 InsertionOrderDictionary、NaturalOrderDictionary 和 CustomOrderDictionary 的私有子类.然后,用户只需像这样创建一个 OrderedDictionary:

Therefore, I recommend making OrderedDictonary a class cluster, with private subclasses of InsertionOrderDictionary and NaturalOrderDictionary and CustomOrderDictionary. Then, the user simply creates an OrderedDictionary like so:

OrderedDictionary * dict = [[OrderedDictionary alloc] initWithOrder:kInsertionOrder];
//or kNaturalOrder, etc

对于 CustomOrderDictionary,你可以让他们给你一个比较选择器,或者甚至(如果他们运行 10.6)一个块.我认为这将为未来的扩展提供最大的灵活性,同时仍然保持一个合适的名称.

For a CustomOrderDictionary, you could have them give you a comparison selector, or even (if they're running 10.6) a block. I think this would provide the most flexibility for future expansion while still maintain an appropriate name.

这篇关于命名以可预测顺序存储键的字典结构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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