clojure的APersistentMap实现之间有什么区别 [英] What is the difference between clojure's APersistentMap implementations

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

问题描述

我试图弄清楚PersistentHashMap,PersistentArrayMap,PersistentTreeMap和PersistentStructMap有什么区别。

I'm trying to figure out what the difference is between a PersistentHashMap, PersistentArrayMap, PersistentTreeMap, and PersistentStructMap.

如果我使用 {:a 1} 它给了我一个PersistentArrayMap,但是如果我给它除键之外的对象或东西,可以将此更改为其他任何吗?

Also if I use {:a 1} it gives me a PersistentArrayMap but can this change to any of the other ones if I give it objects or things other than keys?

推荐答案

您列出的四个实现分为三类:

The four implementations you list fall into three groups:


  1. 文字 PersistentArrayMap PersistentHashMap :处理地图文字时使用的基本地图类型(尽管构造函数在处理重复键方面也具有不同的行为-在Clojure 1.5.x文字中,当发现重复键时会抛出异常,构造函数的工作方式类似于从左到右重复 conj ing;这种行为在版本之间不断发展。当数组映射超过一定数量的条目(9 IIRC)时,它将被提升为哈希映射。存在数组映射是因为它们对于小型映射更快。它们与哈希图的不同之处还在于,它们在提升为哈希图之前会按插入顺序保留条目(您可以使用 clojure.core / array-map 来获取任意大的数组图,如果您真的知道可以从插入顺序遍历中受益,并且地图不会太大,可能仅比通常的阈值稍大一点,这可能会很有用;请注意,随后的 assoc 到这样的超大数组映射将返回哈希映射)。数组映射使用具有交错的键和值的数组。 PHM使用了Phil Bagwell哈希数组映射树的持久版本,该哈希数组具有用于哈希冲突的单独链接,以及用于大多数为空和至少一半的节点的单独节点类型,并且很容易成为Clojure中最复杂的数据结构。

  1. "literal": PersistentArrayMap and PersistentHashMap: basic map types used when dealing with map literals (though constructor functions are also available with different behaviour around handling duplicate keys -- in Clojure 1.5.x literals throw exceptions when they discover duplicate keys, constructor functions work like left-to-right repeated conjing; this behaviour has been evolving from version to version). Array maps get promoted to hash maps when growing beyond a certain number of entries (9 IIRC). Array maps exist because they are faster for small maps; they also differ from hash maps in that they keep entries in insertion order prior to promotion to hash map (you can use clojure.core/array-map to get arbitrarily large array maps, which may be useful if you really know you'd benefit from insertion-order traversals and the map won't be too large, perhaps just a bit over the usual threshold; NB. a subsequent assoc to such an oversized array map will return a hash map). Array maps use arrays with keys and values interleaved; the PHM uses a persistent version of Phil Bagwell's hash array mapped trie with separate chaining for hash collisions and separate node types for mostly-empty and at-least-half-full nodes and is easily the most complex data structure in Clojure.

已排序 PersistentTreeMap 实例仅通过特殊请求创建(对 sorted-map sorted-map-by )。它们被实现为红黑树,并按特定的顺序维护条目,如果使用 sorted-map创建,则由默认的 compare 比较器指定。 code>或用户提供的比较器(如果使用 sorted-map-by 创建的。

sorted: PersistentTreeMap instances are created by special request only (a call to sorted-map or sorted-map-by). They are implemented as red-black trees and maintain entries in a particular order, as specified by the default compare comparator if created with sorted-map or a user-supplied comparator if created with sorted-map-by.

专用,可能已弃用 PersistentStructMap 并不经常使用,并且大多被认为已弃用,而倾向于记录,尽管我实际上不能如果有正式的弃用通知,请立即记住。最初的目的是为地图提供对某些常用键的快速访问。现在,可以在使用关键字进行字段访问时使用记录来完成记录(关键字位于操作员位置:(:foo instance-of-some-record-with-field-foo)),尽管要特别注意的是记录不是 = 到具有相同条目的常规地图。

special-purpose, probably deprecated: PersistentStructMap is not used very often and mostly viewed as deprecated in favour of records, although I actually can't remember right now if there ever was an official deprecation notice. The original purpose was to provide maps with particularly fast access to certain often-used keys. This can now be accomplished with records when using keywords for field access (with the keyword in the operator position: (:foo instance-of-some-record-with-field-foo)), though it's important to note that records are not = to regular maps with the same entries.

所有这四个内置映射类型都属于同一个相等分区,也就是说,上述四个类之一的任何两个映射都是相等的,如果(且仅如果),它们包含相同的键(由Clojure的 = 确定),具有相同的对应值。如上文第3条所述,记录类似于地图,但是每种记录类型都形成其自己的相等分区。

All these four built-in map types fall into the same "equality partition", that is, any two maps of one of the four classes mentioned above will be equal if (and only if) they contain the same keys (as determined by Clojure's =) with the same corresponding values. Records, as mentioned in 3. above, are map-like, but each record type forms its own equality partition.

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

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