Table of Contents

Class Compare

Namespace
Sdl3Sharp.Utilities
Assembly
Sdl3Sharp.dll

Provides methods to sort or search in lists of items

public static class Compare
Inheritance
Compare
Inherited Members

Methods

BSearch<T>(in T, ReadOnlySpan<T>, Comparer<T>)

Performs a binary search on a previously sorted list of items

public static int BSearch<T>(in T key, ReadOnlySpan<T> span, Comparer<T> comparer) where T : unmanaged

Parameters

key T

A comparable key item equal to the item that is being searched for

span ReadOnlySpan<T>

The list of items to search in

comparer Comparer<T>

A Comparer<T> delegate used to compare the items

Returns

int

The index into the span where a matching item to the given key was found, if it was found; otherwise, -1

Type Parameters

T

The type of items to search for

Remarks

The list of items (span) must be previously sorted (e.g. by using QSort<T>(Span<T>, Comparer<T>)) or else the result will be unrelyable.

Note: The result of this method is somewhat different to SDL's native equivalent. While SDL_bsearch() returns a pointer to the matching item into the given list or returns NULL when no match was found, this method returns the index to the matching item into the given list (span) or returns -1 when no match was found.

Exceptions

ArgumentNullException

comparer was null

BSearch<T>(in T, T[], Comparer<T>)

Performs a binary search on a previously sorted list of items

public static int BSearch<T>(in T key, T[] array, Comparer<T> comparer) where T : unmanaged

Parameters

key T

A comparable key item equal to the item that is being searched for

array T[]

The list of items to search in

comparer Comparer<T>

A Comparer<T> delegate used to compare the items

Returns

int

The index into the array where a matching item to the given key was found, if it was found; otherwise, -1

Type Parameters

T

The type of items to search for

Remarks

The list of items (array) must be previously sorted (e.g. by using QSort<T>(T[], Comparer<T>)) or else the result will be unrelyable.

Note: The result of this method is somewhat different to SDL's native equivalent. While SDL_bsearch() returns a pointer to the matching item into the given list or returns NULL when no match was found, this method returns the index to the matching item into the given list (array) or returns -1 when no match was found.

Exceptions

ArgumentNullException

array was null

  • or - comparer was null

QSort<T>(Span<T>, Comparer<T>)

Sorts a list of items

public static void QSort<T>(Span<T> span, Comparer<T> comparer) where T : unmanaged

Parameters

span Span<T>

The list of items to sort

comparer Comparer<T>

A Comparer<T> delegate used to compare the items

Type Parameters

T

The type of items to sort

Remarks

After this returns, the contents of the span are sorted in regards to the given comparer.

Exceptions

ArgumentNullException

comparer was null

QSort<T>(T[], Comparer<T>)

Sorts a list of items

public static void QSort<T>(T[] array, Comparer<T> comparer) where T : unmanaged

Parameters

array T[]

The list of items to sort

comparer Comparer<T>

A Comparer<T> delegate used to compare the items

Type Parameters

T

The type of items to sort

Remarks

After this returns, the contents of the array are sorted in regards to the given comparer.

Exceptions

ArgumentNullException

array was null

  • or - comparer was null