【发布时间】:2019-11-07 05:32:31
【问题描述】:
我正在阅读 System.Collections.Immutable 中的 C# 的 ImmutableSortedDictionary 并考虑如何在我的程序中应用它。我非常喜欢 C++ 的 lower_bound 和 upper_bound(请参阅 here),我更期待看到类似的范围查找。但是,类似的方法seem to be strangely absent from the documentation。我错过了什么吗?或者 MS 是否真的提供了一个排序字典而没有对排序范围的有效访问?这似乎不像一个可以在键的IEnumerable 上做的事情,比如扩展方法,所以我有点困惑,我没有看到集合直接提供的东西。
【问题讨论】:
-
Eric Lippert 早在 2008 年就分享了一个 immutable AVL tree implementation。从 cmets 来看,我认为它尚未针对速度或效率进行特别优化,但它实现的
IBinarySearchTree<K,V>看起来更接近于我会期望。我想知道他是否曾经对它进行过进一步的修补? -
ImmutableList<T>类也实现为 AVL 树。来自source code:/// The root node of the AVL tree that stores this set. -
你知道他们的意思是列表使用 AVL true 来实现不变性还是 AVL 树本身是不可变的? (也许没关系,因为它们无论如何都不会暴露树)。
-
根据documentation,以下是
ImmutableList<T>(由AVL 树支持)相对于ImmutableArray<T>(由数组支持)的优势。 使用不可变列表的原因: 1) 更新数据很常见,或者元素的数量预计不会少。 2) 更新集合比迭代内容更重要。 -
这是因为在大型 AVL 树中添加或删除元素时,您可以通过共享大部分节点并仅创建少数新节点来获得新树而不会破坏原始树。 (Persistent data structure - Trees)
标签: c# .net algorithm immutability immutable-collections