-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathUtilities.cs
More file actions
50 lines (46 loc) · 1.4 KB
/
Utilities.cs
File metadata and controls
50 lines (46 loc) · 1.4 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
50
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SnapCall
{
public static class Utilities
{
public static uint? BinarySearch<T>(this IList<T> list, IComparable<T> item)
{
uint low = 0;
uint high = (uint)list.Count - 1;
while (true)
{
if (low > high)
{
return null;
}
uint index = ((low + high) / 2);
var comparison = item.CompareTo(list.ElementAt((int)index));
if (comparison > 0) low = index + 1;
else if (comparison < 0) high = index - 1;
else return index;
}
}
public static int? BinaryInsert<T>(this IList<T> list, IComparable<T> item)
{
int low = 0;
int high = list.Count - 1;
while (true)
{
if (low > high)
{
list.Insert(low, (T)item);
return low;
}
int index = (int)((low + high) / 2);
var comparison = item.CompareTo(list.ElementAt(index));
if (comparison > 0) low = index + 1;
else if (comparison < 0) high = index - 1;
else return null;
}
}
}
}