-
Notifications
You must be signed in to change notification settings - Fork 396
Expand file tree
/
Copy pathListExtension.cs
More file actions
49 lines (44 loc) · 1.26 KB
/
ListExtension.cs
File metadata and controls
49 lines (44 loc) · 1.26 KB
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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.Contracts;
namespace ReClassNET.Extensions
{
public static class ListExtension
{
/// <summary>
/// Searches a range of elements in the sorted list for an element using the specified comparer and returns the zero-based index of the element.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="source"></param>
/// <param name="comparer">The comparer to use</param>
/// <returns>The zero-based index in the sorted list, if an item is found; otherwise, a negative number that is the bitwise complement of the index of the next element that is larger or, if there is no larger element, the bitwise complement of <see cref="IList{T}.Count"/>.</returns>
[Pure]
[DebuggerStepThrough]
public static int BinarySearch<T>(this IList<T> source, Func<T, int> comparer)
{
Contract.Requires(source != null);
Contract.Requires(comparer != null);
var lo = 0;
var hi = source.Count - 1;
while (lo <= hi)
{
var i = lo + (hi - lo >> 1);
var order = comparer(source[i]);
if (order == 0)
{
return i;
}
if (order > 0)
{
lo = i + 1;
}
else
{
hi = i - 1;
}
}
return ~lo;
}
}
}