forked from izrik/FbxSharp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIListHelper.cs
42 lines (31 loc) · 1.03 KB
/
IListHelper.cs
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
using System;
using System.Collections.Generic;
namespace FbxSharp
{
public static class IListHelper
{
public static int BinarySearchEqualOrLessThan<T>(this IList<T> list, T value, IComparer<T> comparer=null)
{
if (list == null) throw new ArgumentNullException("list");
if (list.Count == 0) return -1;
comparer = comparer ?? Comparer<T>.Default;
int lower = 0;
int upper = list.Count - 1;
int c = comparer.Compare(value, list[lower]);
if (c == -1) return -1;
if (c == 0) return lower;
if (comparer.Compare(value, list[upper]) >= 0) return upper;
while (upper - lower > 1)
{
int i = lower + (upper - lower) / 2;
c = comparer.Compare(value, list[i]);
if (c == 0) return i;
if (c > 0)
lower = i;
else
upper = i;
}
return lower;
}
}
}