1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
|
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using Internal.Runtime.CompilerServices;
namespace System
{
internal static partial class SpanHelpers
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int BinarySearch<T, TComparable>(
this ReadOnlySpan<T> span, TComparable comparable)
where TComparable : IComparable<T>
{
if (comparable == null)
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.comparable);
return BinarySearch(ref MemoryMarshal.GetReference(span), span.Length, comparable);
}
public static int BinarySearch<T, TComparable>(
ref T spanStart, int length, TComparable comparable)
where TComparable : IComparable<T>
{
int lo = 0;
int hi = length - 1;
// If length == 0, hi == -1, and loop will not be entered
while (lo <= hi)
{
// PERF: `lo` or `hi` will never be negative inside the loop,
// so computing median using uints is safe since we know
// `length <= int.MaxValue`, and indices are >= 0
// and thus cannot overflow an uint.
// Saves one subtraction per loop compared to
// `int i = lo + ((hi - lo) >> 1);`
int i = (int)(((uint)hi + (uint)lo) >> 1);
int c = comparable.CompareTo(Unsafe.Add(ref spanStart, i));
if (c == 0)
{
return i;
}
else if (c > 0)
{
lo = i + 1;
}
else
{
hi = i - 1;
}
}
// If none found, then a negative number that is the bitwise complement
// of the index of the next element that is larger than or, if there is
// no larger element, the bitwise complement of `length`, which
// is `lo` at this point.
return ~lo;
}
// Helper to allow sharing all code via IComparable<T> inlineable
internal readonly struct ComparerComparable<T, TComparer> : IComparable<T>
where TComparer : IComparer<T>
{
private readonly T _value;
private readonly TComparer _comparer;
public ComparerComparable(T value, TComparer comparer)
{
_value = value;
_comparer = comparer;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public int CompareTo(T other) => _comparer.Compare(_value, other);
}
}
}
|