简体   繁体   English

数组的不可变数据结构替换

[英]Immutable data structure replacement for arrays

What do you use when you need a immutable list with the fastest access/update? 当您需要具有最快访问/更新的不可变列表时,您会使用什么? LinkedList can be slow if you have to access an element from the middle, and it's prohibitive to create and repopulate it. 如果你必须从中间访问一个元素,LinkedList可能会很慢,而且创建和重新填充它是不可能的。 Binary trees? 二叉树? quadtrees? 四叉树?

If updating is very rare (or the collection is small), an array which you don't write to after intialization is worthwhile. 如果更新非常罕见(或者集合很小),那么在初始化之后不写入的数组是值得的。 The much lower constant factors (both in time and space) outweigh the linear time update in these cases. 在这些情况下,低得多的常数因子(时间和空间)都超过线性时间更新。

Apart from that, there are a number of purely functional data structures which provide better bounds for these cases. 除此之外,还有许多纯功能数据结构,为这些情况提供了更好的界限。 2-3 Finger Trees (the data structure behind Haskell's Data.Sequence ) are one example. 2-3个手指树(Haskell的Data.Sequence背后的数据结构)就是一个例子。 Another option are Clojure's vectors and related data structures (eg Relaxed Radix-Balanced Trees), which use trees with high fan-out (32 or more) to keep reads cheap and structural sharing to avoid too many copies. 另一种选择是Clojure的向量和相关的数据结构(例如,轻松的Radix-Balanced Trees),它使用具有高扇出(32或更多)的树来保持读取便宜和结构共享以避免太多副本。

All of these are moderately tricky to implement manually though, especially if performance is important, and I'm not aware of existing implementations (I don't think Clojure's vectors are easy or convenient to use from Java). 所有这些都是手动实现的中等技巧,特别是如果性能很重要,我不知道现有的实现(我不认为Clojure的向量是简单或方便从Java使用)。

I'm not sure I understand what you're looking for but I'll try to give a couple of pointers based on some things I've seen in the standard classes: 我不确定我理解你在寻找什么,但我会尝试根据我在标准课程中看到的一些内容给出一些指示:

  • CopyOnWriteArrayList is a mutable yet threadsafe list because it duplicates the internal array on updates. CopyOnWriteArrayList是一个可变但线程安全的列表,因为它在更新时复制了内部数组。 Perhaps you could adapt some ideas from that, although it's obviously not efficient for large lists. 也许你可以从中调整一些想法,尽管对于大型列表来说显然效率不高。

  • ConcurrentHashMap implements similar ideas on a much more complicated structure. ConcurrentHashMap在更复杂的结构上实现了类似的想法。 It divides the internal hash table into separate partitions, so that changes only need to lock access to the relevant partition. 它将内部哈希表划分为单独的分区,因此更改只需要锁定对相关分区的访问权限。

    For an immutable list you could do something similar: divide the list's internal array into several partitions and treat them all as immutable. 对于不可变列表,您可以执行类似的操作:将列表的内部数组划分为多个分区,并将它们全部视为不可变。 When you need to change the list, you only need to clone one partition and the index of the partitions, which will be cheaper than duplicating the whole list. 当您需要更改列表时,您只需要克隆一个分区和分区的索引,这比复制整个列表要便宜。

  • AWTEventMulticaster achieves similar goals, but duplicates the absolute minimum. AWTEventMulticaster实现了类似的目标,但重复了绝对最小值。 It's a clever binary tree. 这是一个聪明的二叉树。 See the source . 查看来源

With a smaller size of internal partition or block, you can get faster updates, but slower access in general. 使用较小的内部分区或块,您可以获得更快的更新,但通常访问速度较慢。 With a larger block (eg, the entire array) you get slower updates but faster access. 使用更大的块(例如,整个阵列),您可以获得更慢的更新,但访问速度更快。

If you really need fastest access and update, you have to use a mutable array. 如果您确实需要最快的访问和更新,则必须使用可变数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 有不可变(通常为ConcurrentHashMap)数据结构的正式模式,可以定期替换吗? - Is there an official pattern for immutable (usually ConcurrentHashMap) data structure with ability for its replacement at intervals? 有什么方法可以更有效地替换此数据结构? - What could be a more efficient replacement of this data structure? 发送网络数据,可变与不可变数据结构 - Sending network data, mutable vs immutable data structure 一种方法,将可变数据结构声明为输出并实际返回不可变数据结构 - A method declaring a mutable data structure as an output and returning an immutable one actually 如何在Java中构建复杂的,层次结构的不可变数据结构? - How to build a complex, hierarchic immutable data structure in Java? 仅使用数组实现有效的数据结构 - Implementing efficient data structure using Arrays only Java中字符串数组的数组的数据结构 - Data structure for array of string arrays in java 参数化数据结构以容纳特定大小的数组? - Parametrize a data structure to hold arrays of a specific size? 解析数据结构中大小可变的数组-Java - Parse varying sized arrays in data structure - Java 包含字符串数组列表的数据结构 - Data structure that contains a list of arrays of strings
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM