C# 执行';ImmutableSortedSet.IsSubsetOf';方法
我只是想知道ImmutableSortedSet.IsSubsetOf方法的性能(源代码是) 在最初的实现中,我们有以下代码:C# 执行';ImmutableSortedSet.IsSubsetOf';方法,c#,.net,algorithm,immutability,C#,.net,Algorithm,Immutability,我只是想知道ImmutableSortedSet.IsSubsetOf方法的性能(源代码是) 在最初的实现中,我们有以下代码: public bool IsSubsetOf(IEnumerable<T> other) { Requires.NotNull(other, nameof(other)); if (this.IsEmpty) { return true;
public bool IsSubsetOf(IEnumerable<T> other)
{
Requires.NotNull(other, nameof(other));
if (this.IsEmpty)
{
return true;
}
//...comments...
var otherSet = new SortedSet<T>(other, this.KeyComparer);
int matches = 0;
foreach (T item in otherSet)
{
if (this.Contains(item))
{
matches++;
}
}
return matches == this.Count;
}
public bool issuestof(IEnumerable其他)
{
Requires.NotNull(其他,nameof(其他));
如果(这个是空的)
{
返回true;
}
//…评论。。。
var otherSet=新的分拣数据集(其他,此.keycomarer);
int匹配=0;
foreach(其他集合中的T项)
{
如果(本文件包含(项目))
{
匹配++;
}
}
返回匹配项==此.Count;
}
我们迭代“otherSet”,并尝试在原始排序集中为每个项查找它。我们需要O(m log(n))时间,其中“m”是“otherSet”的大小,“n”是原始排序集的大小
但我们有两个排序集。我们可以使用与“合并排序”算法相同的方法。我的意思是,这种方法可以通过以下方式实现(它只是一个原型,而不是用于生产的代码):
bool IsSubsetOf(IEnumerable其他)
{
如果(这个是空的)
{
返回true;
}
var otherSet=新的分拣数据集(其他,此.keycomarer);
int匹配=0;
var otherEnumerator=otherSet.GetEnumerator();
var thisEnumerator=this.GetEnumerator();
bool isEndOfThis=!thisEnumerator.MoveNext();
bool isEndOfOther=!otherEnumerator.MoveNext();
而(!isEndOfThis&!isEndOfOther)
{
int comparisonResult=this.keycomarer.Compare(otherEnumerator.Current,thisEnumerator.Current);
//我们成功了。让我们移动两个迭代器。
如果(比较结果==0)
{
匹配++;
isEndOfThis=!thisEnumerator.MoveNext();
isEndOfOther=!otherEnumerator.MoveNext();
}
//setEnumerator.Current大于其他setEnumerator.Current。
否则如果(比较结果<0)
{
isEndOfOther=!otherEnumerator.MoveNext();
}
//setEnumerator.Current小于其他setEnumerator.Current。
其他的
{
返回false;
}
}
返回匹配项==此.Count;
}
这个实现需要O(r)时间,其中'r'=max('n','m')
因此,我的问题是“为什么最初的实现没有使用我描述的方法?”。可能是由于时间限制。或者,一般情况下,这种算法不会经常遇到最坏的情况。但是好消息是,如果您确信您的版本在所有情况下都更好,那么您可以发出拉取请求,并且它可能会在下一个.net版本中实现:)。新的SortedSet(其他,这个.KeyComparer)的复杂性是什么?“新的SortedSet(其他,这个.KeyComparer)”的复杂性是O(n log(n))因为在这个构造函数中我们创建了AVL树。可能是由于时间限制。或者,一般情况下,这种算法不会经常遇到最坏的情况。但是好消息是,如果您确信您的版本在所有情况下都更好,那么您可以发出拉取请求,并且它可能会在下一个.net版本中实现:)。新的SortedSet(其他,这个.KeyComparer)的复杂性是什么?“新的SortedSet(其他,这个.KeyComparer)”的复杂性是O(n log(n))因为在这个构造函数中,我们创建了AVL树。
bool IsSubsetOf<T>(IEnumerable<T> other)
{
if (this.IsEmpty)
{
return true;
}
var otherSet = new SortedSet<T>(other, this.KeyComparer);
int matches = 0;
var otherEnumerator = otherSet.GetEnumerator();
var thisEnumerator = this.GetEnumerator();
bool isEndOfThis = !thisEnumerator.MoveNext();
bool isEndOfOther = !otherEnumerator.MoveNext();
while (!isEndOfThis && !isEndOfOther)
{
int comparisonResult = this.KeyComparer.Compare(otherEnumerator.Current, thisEnumerator.Current);
//we have a hit. let's move both iterators.
if (comparisonResult == 0)
{
matches++;
isEndOfThis = !thisEnumerator.MoveNext();
isEndOfOther = !otherEnumerator.MoveNext();
}
//setEnumerator.Current is more than otherSetEnumerator.Current.
else if (comparisonResult < 0)
{
isEndOfOther = !otherEnumerator.MoveNext();
}
//setEnumerator.Current is less than otherSetEnumerator.Current.
else
{
return false;
}
}
return matches == this.Count;
}