Documentation

Unity Engine


User Manual

Script Reference

Unity Engine


BinarySearch

Finds a value in a sorted array by binary search.
Read time 9 minutesLast updated 13 days ago

Definition

  • Type: Method
  • Namespace: Unity.Collections
  • Assembly: UnityEngine.ManagedKernelModule

BinarySearch<T>(`<>*, int, T)

Finds a value in a sorted array by binary search.
public static int BinarySearch<T>(T* ptr, int length, T value) where T : unmanaged, IComparable<T>

Parameters

ptr

The array to search.

length

The number of elements to search. Indexes greater than or equal to
length
won't be searched.

value

T
The value to locate.

Returns

Type

Description

intThe zero-based index of the value to find in the sorted array. If it is not found, a negative number that is the bitwise complement of the index of the next element larger than item or, if there is no larger element, the bitwise complement of the length.

Remarks

If the array is not sorted, the value might not be found, even if it's present in the array.

BinarySearch<T, U>(`<>*, int, T, T)

Finds a value in a sorted array by binary search using a custom comparison.
public static int BinarySearch<T, U>(T* ptr, int length, T value, U comp) where T : unmanaged where U : IComparer<T>

Parameters

ptr

The array to search.

length

The number of elements to search. Indexes greater than or equal to
length
won't be searched.

value

T
The value to locate.

comp

T
The comparison function used to determine the relative order of the elements.

Returns

Type

Description

intThe zero-based index of the value to find in the sorted array. If it is not found, a negative number that is the bitwise complement of the index of the next element larger than item or, if there is no larger element, the bitwise complement of the length.

Remarks

If the array is not sorted, the value might not be found, even if it's present in the array.

BinarySearch<T>(NativeArray<T>, T)

Finds a value in this sorted array by binary search.
public static int BinarySearch<T>(this NativeArray<T> container, T value) where T : unmanaged, IComparable<T>

Parameters

The array to search.

value

T
The value to locate.

Returns

Type

Description

intThe zero-based index of the value to find in the sorted array. If it is not found, a negative number that is the bitwise complement of the index of the next element larger than item or, if there is no larger element, the bitwise complement of the length.

Remarks

If the array is not sorted, the value might not be found, even if it's present in this array.

BinarySearch<T, U>(NativeArray<T>, T, T)

Finds a value in this sorted array by binary search using a custom comparison.
public static int BinarySearch<T, U>(this NativeArray<T> container, T value, U comp) where T : unmanaged where U : IComparer<T>

Parameters

The array to search.

value

T
The value to locate.

comp

T
The comparison function used to determine the relative order of the elements.

Returns

Type

Description

intThe zero-based index of the value to find in the sorted array. If it is not found, a negative number that is the bitwise complement of the index of the next element larger than item or, if there is no larger element, the bitwise complement of the length.

Remarks

If the array is not sorted, the value might not be found, even if it's present in this array.

BinarySearch<T>(ReadOnly, T)

Finds a value in a sorted array by binary search.
public static int BinarySearch<T>(this NativeArray<T>.ReadOnly container, T value) where T : unmanaged, IComparable<T>

Parameters

container


value

T
The value to locate.

Returns

Type

Description

intThe zero-based index of the value to find in the sorted array. If it is not found, a negative number that is the bitwise complement of the index of the next element larger than item or, if there is no larger element, the bitwise complement of the length.

Remarks

If the array is not sorted, the value might not be found, even if it's present in the array.

BinarySearch<T, U>(ReadOnly, T, T)

Finds a value in a sorted array by binary search.
public static int BinarySearch<T, U>(this NativeArray<T>.ReadOnly container, T value, U comp) where T : unmanaged where U : IComparer<T>

Parameters

container


value

T
The value to locate.

comp

T

Returns

Type

Description

intThe zero-based index of the value to find in the sorted array. If it is not found, a negative number that is the bitwise complement of the index of the next element larger than item or, if there is no larger element, the bitwise complement of the length.

Remarks

If the array is not sorted, the value might not be found, even if it's present in the array.

BinarySearch<T>(NativeList<T>, T)

Finds a value in this sorted list by binary search.
public static int BinarySearch<T>(this NativeList<T> container, T value) where T : unmanaged, IComparable<T>

Parameters

container

The list to search.

value

T
The value to locate.

Returns

Type

Description

intThe zero-based index of the value to find in the sorted list. If it is not found, a negative number that is the bitwise complement of the index of the next element larger than item or, if there is no larger element, the bitwise complement of the length.

Remarks

If this list is not sorted, the value might not be found, even if it's present in this list.

BinarySearch<T, U>(NativeList<T>, T, T)

Finds a value in this sorted list by binary search using a custom comparison.
public static int BinarySearch<T, U>(this NativeList<T> container, T value, U comp) where T : unmanaged where U : IComparer<T>

Parameters

container

The list to search.

value

T
The value to locate.

comp

T
The comparison function used to determine the relative order of the elements.

Returns

Type

Description

intThe zero-based index of the value to find in the sorted list. If it is not found, a negative number that is the bitwise complement of the index of the next element larger than item or, if there is no larger element, the bitwise complement of the length.

Remarks

If this list is not sorted, the value may not be found, even if it's present in this list.

BinarySearch<T>(UnsafeList<T>, T)

Finds a value in this sorted list by binary search.
public static int BinarySearch<T>(this UnsafeList<T> container, T value) where T : unmanaged, IComparable<T>

Parameters

container

The list to search.

value

T
The value to locate.

Returns

Type

Description

intThe zero-based index of the value to find in the sorted list. If it is not found, a negative number that is the bitwise complement of the index of the next element larger than item or, if there is no larger element, the bitwise complement of the length.

Remarks

If this list is not sorted, the value might not be found, even if it's present in this list.

BinarySearch<T, U>(UnsafeList<T>, T, T)

Finds a value in this sorted list by binary search using a custom comparison.
public static int BinarySearch<T, U>(this UnsafeList<T> container, T value, U comp) where T : unmanaged where U : IComparer<T>

Parameters

container

The list to search.

value

T
The value to locate.

comp

T
The comparison function used to determine the relative order of the elements.

Returns

Type

Description

intThe zero-based index of the value to find in the sorted list. If it is not found, a negative number that is the bitwise complement of the index of the next element larger than item or, if there is no larger element, the bitwise complement of the length.

Remarks

If this list is not sorted, the value might not be found, even if it's present in this list.

BinarySearch<T>(ReadOnlySpan<T>, T)

Finds a value in this sorted ReadOnlySpan by binary search.
public static int BinarySearch<T>(this ReadOnlySpan<T> roSpan, T value) where T : unmanaged, IComparable<T>

Parameters

The ReadOnlySpan to search.

value

T
The value to locate.

Returns

Type

Description

intThe zero-based index of the value to find in the sorted ReadOnlySpan. If it is not found, a negative number that is the bitwise complement of the index of the next element larger than item or, if there is no larger element, the bitwise complement of the length.

Remarks

If this ReadOnlySpan is not sorted, the value might not be found, even if it's present in this ReadOnlySpan.

BinarySearch<T, U>(ReadOnlySpan<T>, T, T)

Finds a value in this sorted ReadOnlySpan by binary search using a custom comparison.
public static int BinarySearch<T, U>(this ReadOnlySpan<T> roSpan, T value, U comp) where T : unmanaged where U : IComparer<T>

Parameters

The ReadOnlySpan to search.

value

T
The value to locate.

comp

T
The comparison function used to determine the relative order of the elements.

Returns

Type

Description

intThe zero-based index of the value to find in the sorted ReadOnlySpan. If it is not found, a negative number that is the bitwise complement of the index of the next element larger than item or, if there is no larger element, the bitwise complement of the length.

Remarks

If this ReadOnlySpan is not sorted, the value might not be found, even if it's present in this ReadOnlySpan.

BinarySearch<T>(NativeSlice<T>, T)

Finds a value in this sorted slice by binary search.
public static int BinarySearch<T>(this NativeSlice<T> container, T value) where T : unmanaged, IComparable<T>

Parameters

The slice to search.

value

T
The value to locate.

Returns

Type

Description

intThe zero-based index of the value to find in the sorted slice. If it is not found, a negative number that is the bitwise complement of the index of the next element larger than item or, if there is no larger element, the bitwise complement of the length.

Remarks

If this slice is not sorted, the value might not be found, even if it's present in this slice.

BinarySearch<T, U>(NativeSlice<T>, T, T)

Finds a value in this sorted slice by binary search using a custom comparison.
public static int BinarySearch<T, U>(this NativeSlice<T> container, T value, U comp) where T : unmanaged where U : IComparer<T>

Parameters

The slice to search.

value

T
The value to locate.

comp

T
The comparison function used to determine the relative order of the elements.

Returns

Type

Description

intThe zero-based index of the value to find in the sorted slice. If it is not found, a negative number that is the bitwise complement of the index of the next element larger than item or, if there is no larger element, the bitwise complement of the length.

Remarks

If this slice is not sorted, the value might not be found, even if it's present in this slice.