- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
来自 SortedSet<T>.GetEnumerator
文档:
This method is an O(1) operation
SortedDictionary<K, V>.GetEnumerator
文档:
This method is an O(log n) operation, where n is count.
SortedDictionary<K, V>
,这两个陈述是否都为真?在内部实现为
SortedSet<KeyValuePair<K, V>
?我查了
GetEnumerator
SortedDictionary
的代码类 - 它直接使用
SortedSet
的枚举器。我注意到
SortedSet
的枚举器实现,在我看来它确实具有 O(log n) 特性(这是代码):
public SortedSet<T>.Enumerator GetEnumerator()
{
return new SortedSet<T>.Enumerator(this);
}
//which calls this constructor:
internal Enumerator(SortedSet<T> set)
{
this.tree = set;
this.tree.VersionCheck();
this.version = this.tree.version;
this.stack = new Stack<SortedSet<T>.Node>(2 * SortedSet<T>.log2(set.Count + 1));
this.current = (SortedSet<T>.Node) null;
this.reverse = false;
this.siInfo = (SerializationInfo) null;
this.Intialize();
}
private void Intialize()
{
this.current = (SortedSet<T>.Node) null;
SortedSet<T>.Node node1 = this.tree.root;
while (node1 != null)
{
SortedSet<T>.Node node2 = this.reverse ? node1.Right : node1.Left;
SortedSet<T>.Node node3 = this.reverse ? node1.Left : node1.Right;
if (this.tree.IsWithinRange(node1.Item))
{
this.stack.Push(node1);
node1 = node2;
}
else
node1 = node2 == null || !this.tree.IsWithinRange(node2.Item) ? node3 : node2;
}
}
SortedSet<T>.GetEnumerator
是 O(log n) 吗? 关于
GetEnumerator
的性能没什么好说的打电话,只是确保我理解正确。
最佳答案
我完全同意你的看法。SortedSet
内部采用红黑树结构,保证平衡( Wikipedia ; Red-Black Trees, R. Sedgewick, Princeton University ),因此高度受限于 2 * log2(n + 1)
.甚至一个 code comment在 SortedSet.cs 中指出了这一点,并相应地设置了枚举器堆栈的大小。while
准备堆栈的循环是 O(log n) 在初始化和处理 ( MoveNext
) 枚举器中。
意见反馈 MSDN documentation引用提交的这个讨论。
更新:
到今天微软终于更新了文档。对于 4.0 版本,它仍然声明它是一个 O(1) 操作。虽然我对此表示怀疑,但我可以就此放弃。
关于.net - 为什么 SortedDictionary<K, V>.GetEnumerator O(log n) 但 SortedSet<T>.GetEnumerator O(1)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24781153/
在 .net 框架中,有一个通用的 IEnumerable继承自非泛型 IEnumerable 的接口(interface),并且他们都有 GetEnumerator()方法。这两者之间的唯一区别Ge
致力于实现 IEnumerable 接口(interface)。我实际上找到了两个需要实现的方法: public IEnumerator GetEnumerator() System.Collecti
查看这段代码: public class myWords : IEnumerable { string[] f = "I love you".Split(new string[]{"lo"},
基本上假设您有一些收藏: public class FurCollection : IEnumerable { public IEnumerator GetEnumerator() {
List为什么要定义这三个方法? public Enumerator GetEnumerator() => new Enumerator(this); IEnumera
我对 Reflection.Emit 有疑问。我想要动态创建的类,它具有 ICollection 的简单实现。我定义的所有方法都很好,而不是接下来的两个: public IEnumerator Get
来自 SortedSet.GetEnumerator 文档: This method is an O(1) operation 来自 SortedDictionary.GetEnumerator 文档
我正在编写一个应该能够显示任何数据列表的控件。我想要做的是模仿 for-in 构造,因为我检查包含 Current 属性和 MoveNext 方法的公共(public) GetEnumerator 函
我正在使用 JSOM 查找特定列表中特定项目的特定字段中的值。总的来说,代码查找用户的组成员身份(使用 SPServices),然后使用该值来匹配列表中的项目(列表包含填充有组名称的“Title”字段
我不能重载 GetEnumerator () 吗 IEnumerator IEnumerable.GetEnumerator ( T[] val1,T[] val2) { .... some co
我正在为 C# 开发并发字典实现,我想知道这个 GetEnumerator() 实现是否实际上是(线程)安全的。 它并没有做一个实际的快照,所以我想知道它是否会在以后读/写内部字典时搞砸,或者它是否会
我有一个 BinaryTree类和一个BinaryTreeNode对于持有节点,我已经制作了树并为它编写了前序、后序和中序方法。 但我不知道怎么写IEnumerator为此(我只想对 GetEnume
假设我有一个非常 长的字符串,并且想要获取一个从字符串中间的给定索引处开始的枚举器(为了 LINQ 的优点)。也就是说, var longString = GetLongString(); var i
我想将枚举器用于 Delphi XE2 的通用集合。我想知道,谁拥有函数 GetEnumerator 返回的 TEnumerator (我在文档中没有找到任何明确的答案): 我是否拥有它并需要在使用后
在我们的代码库中,我们有一个数据访问层,提供类似于以下的方法: public IEnumerable GetFoos() { var collection = new Collection()
我真的不明白为什么在这个例子中,这个类定义了两个GetEnumerator方法:一个显式实现接口(interface),另一个隐式 如我所知。那为什么? class FormattedAddresse
我有一个简单的代码,目的是在文件不存在或其中的数据版本已过时时通过文件或数据库返回枚举器。简单的。但是,我正在为该方法的工作原理而苦苦挣扎。原因如下: public override IEnumera
我尝试在 SharePoint 中接收用户子网站的子网站。在第一次调用接收子网站(函数 getSubWebs())时,一切正常:子网站已接收,我可以遍历它们。 但是当我尝试使用相同类型的代码获取这些网
我想在哈希集上使用 foreach,但 VS 不允许。 这是我到目前为止所拥有的: { class Team { private HashSet team; public Team()
我想遍历更新面板的各种控件。在使用控件 ID 进行迭代时,我想删除一些控件。 但我不知道如何使用 GetEnumerator 方法遍历更新面板的控件? 如果我们可以通过其他方式进行迭代,请告诉我。 最
我是一名优秀的程序员,十分优秀!