Skip to content

Commit 53d20ab

Browse files
author
LoneWandererProductions
committed
Some smaller changes
1 parent 1719367 commit 53d20ab

2 files changed

Lines changed: 27 additions & 11 deletions

File tree

CommonExtendedObjectsTests/Utilities.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ public void BinarySearchPerformanceAndCorrectness()
283283
// Warm-up to avoid JIT bias
284284
for (int i = 0; i < 10_000; i++)
285285
{
286-
Utility.BinarySearch(keysSpan, N, i * 2);
286+
Utility.BinarySearch(keysSpan, i * 2);
287287
}
288288

289289
var sw = Stopwatch.StartNew();
@@ -292,7 +292,7 @@ public void BinarySearchPerformanceAndCorrectness()
292292
for (int i = 0; i < N; i++)
293293
{
294294
int val = i * 2;
295-
int idx = Utility.BinarySearch(keysSpan, N, val);
295+
int idx = Utility.BinarySearch(keysSpan, val);
296296

297297
Assert.IsTrue(idx >= 0, $"Key {val} should be found.");
298298
Assert.AreEqual(val, sortedKeys[idx]);
@@ -321,7 +321,7 @@ public void CompareCustomVsArrayBinarySearch_Performance()
321321
// Warm-up both methods
322322
for (int i = 0; i < 10_000; i++)
323323
{
324-
Utility.BinarySearch(keysSpan, N, i * 2);
324+
Utility.BinarySearch(keysSpan, i * 2);
325325
Array.BinarySearch(sortedKeys, i * 2);
326326
}
327327

ExtendedSystemObjects/Utility.cs

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -88,21 +88,37 @@ public static long GetFirstAvailableIndex(List<long> lst)
8888
}
8989

9090
/// <summary>
91-
/// Binary search of an int element.
91+
/// Performs binary search on a sorted span of integers.
9292
/// </summary>
93-
/// <param name="sortedKeys">The sorted keys.</param>
94-
/// <param name="count">The count.</param>
95-
/// <param name="target">The target.</param>
96-
/// <returns>Index of the element</returns>
93+
/// <param name="sortedKeys">The sorted span of integers.</param>
94+
/// <param name="target">The value to search for.</param>
95+
/// <returns>
96+
/// Index of the element if found; otherwise, the bitwise complement of the insertion index.
97+
/// </returns>
9798
[MethodImpl(MethodImplOptions.AggressiveInlining)]
98-
public static int BinarySearch(ReadOnlySpan<int> sortedKeys, int count, int target)
99+
public static int BinarySearch(ReadOnlySpan<int> sortedKeys, int target)
100+
{
101+
return BinarySearch(sortedKeys, sortedKeys.Length, target);
102+
}
103+
104+
/// <summary>
105+
/// Internal binary search method using a specified count of elements.
106+
/// </summary>
107+
/// <param name="sortedKeys">The sorted span of integers.</param>
108+
/// <param name="count">The number of elements to consider from the start of the span.</param>
109+
/// <param name="target">The value to search for.</param>
110+
/// <returns>
111+
/// Index of the element if found; otherwise, the bitwise complement of the insertion index.
112+
/// </returns>
113+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
114+
internal static int BinarySearch(ReadOnlySpan<int> sortedKeys, int count, int target)
99115
{
100116
int left = 0, right = count - 1;
101117

102118
while (left <= right)
103119
{
104-
var mid = left + ((right - left) >> 1);
105-
var midKey = sortedKeys[mid];
120+
int mid = left + ((right - left) >> 1);
121+
int midKey = sortedKeys[mid];
106122

107123
if (midKey == target)
108124
return mid;

0 commit comments

Comments
 (0)