-
Notifications
You must be signed in to change notification settings - Fork 101
/
BinarySearch.cs
220 lines (182 loc) · 6.16 KB
/
BinarySearch.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
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
using System;
using System.Collections.Generic;
namespace MasterMemory.Internal
{
internal static class BinarySearch
{
public static int FindFirst<T, TKey>(T[] array, TKey key, Func<T, TKey> selector, IComparer<TKey> comparer)
{
var lo = 0;
var hi = array.Length - 1;
while (lo <= hi)
{
var mid = (int)(((uint)hi + (uint)lo) >> 1);
var found = comparer.Compare(selector(array[mid]), key);
if (found == 0) return mid;
if (found < 0)
{
lo = mid + 1;
}
else
{
hi = mid - 1;
}
}
return -1;
}
public static int FindFirstIntKey<T>(T[] array, int key, Func<T, int> selector)
{
var lo = 0;
var hi = array.Length - 1;
while (lo <= hi)
{
var mid = (int)(((uint)hi + (uint)lo) >> 1);
// compare inlining
var selectedValue = selector(array[mid]);
var found = (selectedValue < key) ? -1 : (selectedValue > key) ? 1 : 0;
if (found == 0) return mid;
if (found < 0)
{
lo = mid + 1;
}
else
{
hi = mid - 1;
}
}
return -1;
}
// lo = 0, hi = Count.
public static int FindClosest<T, TKey>(T[] array, int lo, int hi, TKey key, Func<T, TKey> selector, IComparer<TKey> comparer, bool selectLower)
{
if (array.Length == 0) return -1;
lo = lo - 1;
while (hi - lo > 1)
{
var mid = lo + ((hi - lo) >> 1);
var found = comparer.Compare(selector(array[mid]), key);
if (found == 0)
{
lo = hi = mid;
break;
}
if (found >= 1)
{
hi = mid;
}
else
{
lo = mid;
}
}
return selectLower ? lo : hi;
}
// default lo = 0, hi = array.Count
public static int LowerBound<T, TKey>(T[] array, int lo, int hi, TKey key, Func<T, TKey> selector, IComparer<TKey> comparer)
{
while (lo < hi)
{
var mid = lo + ((hi - lo) >> 1);
var found = comparer.Compare(key, selector(array[mid]));
if (found <= 0)
{
hi = mid;
}
else
{
lo = mid + 1;
}
}
var index = lo;
if (index == -1 || array.Length <= index)
{
return -1;
}
// check final
return (comparer.Compare(key, selector(array[index])) == 0)
? index
: -1;
}
public static int UpperBound<T, TKey>(T[] array, int lo, int hi, TKey key, Func<T, TKey> selector, IComparer<TKey> comparer)
{
while (lo < hi)
{
var mid = lo + ((hi - lo) >> 1);
var found = comparer.Compare(key, selector(array[mid]));
if (found >= 0)
{
lo = mid + 1;
}
else
{
hi = mid;
}
}
var index = (lo == 0) ? 0 : lo - 1;
if (index == -1 || array.Length <= index)
{
return -1;
}
// check final
return (comparer.Compare(key, selector(array[index])) == 0)
? index
: -1;
}
//... want the lowest index of Key <= Value
//... returns 0 if key is <= all values in array
//... returns array.Length if key is > all values in array
public static int LowerBoundClosest<T, TKey>(T[] array, int lo, int hi, TKey key, Func<T, TKey> selector, IComparer<TKey> comparer)
{
while (lo < hi)
{
var mid = lo + ((hi - lo) >> 1);
var found = comparer.Compare(key, selector(array[mid]));
if (found <= 0) //... Key is <= value at mid
{
hi = mid;
}
else
{
lo = mid + 1; //... Notice that lo starts at zero and can only increase
}
}
var index = lo; //... index will always be zero or greater
if ( array.Length <= index)
{
return array.Length;
}
// check final
return (comparer.Compare(key, selector(array[index])) <= 0)
? index
: -1;
}
//... want the highest index of Key >= Value
//... returns -1 if key is < than all values in array
//... returns array.Length - 1 if key is >= than all values in array
public static int UpperBoundClosest<T, TKey>(T[] array, int lo, int hi, TKey key, Func<T, TKey> selector, IComparer<TKey> comparer)
{
while (lo < hi)
{
var mid = lo + ((hi - lo) >> 1);
var found = comparer.Compare(key, selector(array[mid]));
if (found >= 0) //... Key >= value at mid
{
lo = mid + 1; //... Note lo starts at zero and can only increase
}
else
{
hi = mid;
}
}
var index = (lo == 0) ? 0 : lo - 1; //... index will always be zero or greater
if ( index >= array.Length )
{
return array.Length;
}
// check final
return (comparer.Compare(key, selector(array[index])) >= 0)
? index
: -1;
}
}
}