未验证 提交 aa691a17 编写于 作者: S Sam Harwell 提交者: GitHub

Merge pull request #43715 from sharwell/intervaltree-any

🐇 Avoid using an ArrayBuilder for IntervalTree<T>.Any
......@@ -140,9 +140,8 @@ public bool HasIntervalThatContains<TIntrospector>(int start, int length, in TIn
private bool Any<TIntrospector>(int start, int length, TestInterval<TIntrospector> testInterval, in TIntrospector introspector)
where TIntrospector : struct, IIntervalIntrospector<T>
{
using var _ = ArrayBuilder<T>.GetInstance(out var builder);
FillWithIntervalsThatMatch(start, length, testInterval, builder, in introspector, stopAfterFirst: true);
return builder.Count > 0;
var matches = FillWithIntervalsThatMatch(start, length, testInterval, builder: null, in introspector, stopAfterFirst: true);
return matches > 0;
}
private ImmutableArray<T> GetIntervalsThatMatch<TIntrospector>(
......@@ -154,33 +153,38 @@ private bool Any<TIntrospector>(int start, int length, TestInterval<TIntrospecto
return result.ToImmutableAndFree();
}
private void FillWithIntervalsThatMatch<TIntrospector>(
/// <returns>The number of matching intervals found by the method.</returns>
private int FillWithIntervalsThatMatch<TIntrospector>(
int start, int length, TestInterval<TIntrospector> testInterval,
ArrayBuilder<T> builder, in TIntrospector introspector,
ArrayBuilder<T>? builder, in TIntrospector introspector,
bool stopAfterFirst)
where TIntrospector : struct, IIntervalIntrospector<T>
{
if (root == null)
{
return;
return 0;
}
var candidates = s_stackPool.Allocate();
FillWithIntervalsThatMatch(
var matches = FillWithIntervalsThatMatch(
start, length, testInterval,
builder, in introspector,
stopAfterFirst, candidates);
s_stackPool.ClearAndFree(candidates);
return matches;
}
private void FillWithIntervalsThatMatch<TIntrospector>(
/// <returns>The number of matching intervals found by the method.</returns>
private int FillWithIntervalsThatMatch<TIntrospector>(
int start, int length, TestInterval<TIntrospector> testInterval,
ArrayBuilder<T> builder, in TIntrospector introspector,
ArrayBuilder<T>? builder, in TIntrospector introspector,
bool stopAfterFirst, Stack<(Node? node, bool firstTime)> candidates)
where TIntrospector : struct, IIntervalIntrospector<T>
{
var matches = 0;
var end = start + length;
candidates.Push((root, firstTime: true));
......@@ -199,11 +203,12 @@ private bool Any<TIntrospector>(int start, int length, TestInterval<TIntrospecto
// side of it). Now see if it matches our test, and if so return it out.
if (testInterval(currentNode.Value, start, length, in introspector))
{
builder.Add(currentNode.Value);
matches++;
builder?.Add(currentNode.Value);
if (stopAfterFirst)
{
return;
return 1;
}
}
}
......@@ -237,6 +242,8 @@ private bool Any<TIntrospector>(int start, int length, TestInterval<TIntrospecto
}
}
}
return matches;
}
public bool IsEmpty() => this.root == null;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册