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
|
namespace System.Collections
{
/// <summary>
/// Performs a binary search within any implementation
/// of <see cref="System.Collections.IList"/>.
/// </summary>
public static class IListBinarySearchExtension
{
/// <summary>
/// Searches for an <paramref name="item"/> within the specified <paramref name="list"/>
/// using the specified <paramref name="comparer"/>.
/// </summary>
/// <typeparam name="T">The type of the list.</typeparam>
/// <param name="list">The list to search in.</param>
/// <param name="comparer">The function used to compare the list's elements.</param>
/// <param name="item">The result of the search.</param>
/// <returns>Returns <b>true</b> on success; <b>false</b> on failure.</returns>
public static bool BinarySearch<T>(this IList list, Func<T, int> comparer, out T item)
{
if (list == null)
throw new ArgumentNullException("list");
if (comparer == null)
throw new ArgumentNullException("comparer");
return BinarySearch(list, comparer, 0, list.Count - 1, out item);
}
/// <summary>
/// Searches for an <paramref name="item"/> within the specified range of the specified
/// <paramref name="list"/> using the specified <paramref name="comparer"/>.
/// </summary>
/// <typeparam name="T">The type of the list.</typeparam>
/// <param name="list">The list to search in.</param>
/// <param name="comparer">The function used to compare the list's elements.</param>
/// <param name="item">The result of the search.</param>
/// <param name="min">The lower bound of the search.</param>
/// <param name="max">The upper bound of the search.</param>
/// <returns>Returns <b>true</b> on success; <b>false</b> on failure.</returns>
public static bool BinarySearch<T>(this IList list, Func<T, int> comparer, int min, int max, out T item)
{
if (list == null)
throw new ArgumentNullException("list");
if (comparer == null)
throw new ArgumentNullException("comparer");
if (min < 0 || min >= list.Count)
throw new ArgumentOutOfRangeException("min");
if (max < 0 || max >= list.Count)
throw new ArgumentOutOfRangeException("max");
while (min <= max)
{
int middle = min + ((max - min) / 2);
int compared = comparer((T)list[middle]);
if (compared < 0)
{
min = middle + 1;
}
else if (compared > 0)
{
max = middle - 1;
}
else
{
item = (T)list[middle];
return true;
}
}
item = default(T);
return false;
}
}
}
|