@@ -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