Skip to content

Commit

Permalink
Use a faster search when checking for item in SortedList (dotnet#12041)
Browse files Browse the repository at this point in the history
  • Loading branch information
hartez authored Dec 13, 2022
1 parent 149c20d commit 3cf3624
Showing 1 changed file with 1 addition and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,10 @@ public SortedList(IComparer<T> comparer)

public int IndexOf(T item)
{
// PERF hint: this is a O(n) algorithm but could be rewritten as a O(log n) one.
if (Count == 0)
return ~0;

for (var i = 0; i < Count; i++)
{
var existing = this[i];
var compare = _comparer.Compare(item, existing);
if (compare == 0)
return i;
if (compare < 0)
return ~i;
}

return ~Count;
return _list.BinarySearch(item, _comparer);
}

public void Insert(int index, T item)
Expand Down

0 comments on commit 3cf3624

Please sign in to comment.