AlgorithmUtil

Description

static class Tinman.Core.AlgorithmUtil

Helper class that provides some commonly used algorithms for element sequences.

Algorithms that use IComparable or IEquatable are provided in three variants, where each variant is identified by the name suffix:

  • *Default :
    The algorithm uses a generic delegate to operate on the elements, which may cause virtual calls or boxing at runtime.

  • *Object :
    The algorithm makes null-safe use of the natural semantic of the element objects, which may cause virtual calls at runtime. Special overloads exist for the string type, which make use of StringUtil.Compare, in order to provide the same behaviour (independently of platform, culture, etc.).

  • *Value :
    The algorithm makes direct use of the natural semantic of the element values.

Public / Methods

Choose


[Pure]
public static method Choose → (5)<T>

idx in : int32

[0..3]
The index.

idx0 in : T

The value to return if idx in is 0.

idx1 in : T

The value to return if idx in is 1.

idx2 in : T

The value to return if idx in is 2.

idx3 in : T

The value to return if idx in is 3.

returns → T

The value chosen by idx in.

Chooses one of the four values by the given index.

Search​Binary​Default


[Pure]
public static method SearchBinaryDefault → (5)<T>

value in : T

The value to search.

array in : T [ ]

[not-null]
The array, which must be sorted in ascending order, according to compare in.

compare in : CompareDelegate<T>

[not-null]
The compare to use.

offset opt : int32 = 0

[0..array.Length]
Index of first array element to search.

count opt : int32 = -1

[-1..array.Length-offset]
Length of array range to search. If -1, all remaining elements will be used.

returns → int32

Index of the array element that equals value in. If the value has not been found in the array, -idx-1 is returned, where idx is the insertion index of value in.

Runs a binary search for value in in the given array.

See also

CompareDelegate

Search​Binary​Object

2 overloads


[Pure]
public static method SearchBinaryObject1 → (4)
<T ref : IComparable<T>>

value in : T

The value to search.

array in : T [ ]

[not-null]
The array, which must be sorted in ascending order, according to SystemUtil.CompareObject.

offset opt : int32 = 0

[0..array.Length]
Index of first array element to search.

count opt : int32 = -1

[-1..array.Length-offset]
Length of array range to search. If -1, all remaining elements will be used.

returns → int32

Index of the array element that equals value in. If the value has not been found in the array, -idx-1 is returned, where idx is the insertion index of value in.

Runs a binary search for value in in the given array.


[Pure]
public static method SearchBinaryObject2 → (4)

value in : string

The value to search.

array in : string [ ]

[not-null]
The array, which must be sorted in ascending order, according to StringUtil.Compare.

offset opt : int32 = 0

[0..array.Length]
Index of first array element to search.

count opt : int32 = -1

[-1..array.Length-offset]
Length of array range to search. If -1, all remaining elements will be used.

returns → int32

Index of the array element that equals value in. If the value has not been found in the array, -idx-1 is returned, where idx is the insertion index of value in.

Runs a binary search for value in in the given array.

Search​Binary​Value


[Pure]
public static method SearchBinaryValue → (4)
<T val : IComparable<T>>

value in : T

The value to search.

array in : T [ ]

[not-null]
The array, which must be sorted in ascending order, according to SystemUtil.CompareValue.

offset opt : int32 = 0

[0..array.Length]
Index of first array element to search.

count opt : int32 = -1

[-1..array.Length-offset]
Length of array range to search. If -1, all remaining elements will be used.

returns → int32

Index of the array element that equals value in. If the value has not been found in the array, -idx-1 is returned, where idx is the insertion index of value in.

Runs a binary search for value in in the given array.

Search​Linear​Default


[Pure]
public static method SearchLinearDefault → (5)<T>

value in : T

The value to find in array in.

array in : T [ ]

[not-null]
The array to search.

equals opt : EqualsDelegate<T> = null

Optional equality compare. If null, SystemUtil.EqualsDefault will be used.

offset opt : int32 = 0

[0..array.Length]
Offset into array in to first element to search.

count opt : int32 = -1

[-1..array.Length-offset]
Number of elements in array in to search. If -1, all remaining elements will be searched.

returns → int32

The index of the found array element or -1 iff not found.

Finds the smallest index of the array element that is equal to the given value, according to equals opt.

See also

EqualsDelegate

Search​Linear​Object


[Pure]
public static method SearchLinearObject → (4)
<T ref : IEquatable<T>>

value in : T

The value to find in array in.

array in : T [ ]

[not-null]
The array to search.

offset opt : int32 = 0

[0..array.Length]
Offset into array in to first element to search.

count opt : int32 = -1

[-1..array.Length-offset]
Number of elements in array in to search. If -1, all remaining elements will be searched.

returns → int32

The index of the found array element or -1 iff not found.

Finds the smallest index of the array element that is equal to the given value.

Search​Linear​Value


[Pure]
public static method SearchLinearValue → (4)
<T val : IEquatable<T>>

value in : T

The value to find in array in.

array in : T [ ]

[not-null]
The array to search.

offset opt : int32 = 0

[0..array.Length]
Offset into array in to first element to search.

count opt : int32 = -1

[-1..array.Length-offset]
Number of elements in array in to search. If -1, all remaining elements will be searched.

returns → int32

The index of the found array element or -1 iff not found.

Finds the smallest index of the array element that is equal to the given value.

Shuffle

2 overloads


public static method Shuffle1 → (4)<T>

random in : RandomNumber

[not-null]
The random number generator to use.

array in : T [ ]

[not-null]
The array to shuffle.

offset opt : int32 = 0

[0..array.Length]
Offset to first element in array in to shuffle. Defaults to 0.

count opt : int32 = -1

[-1..array.Length-offset]
Number of elements to shuffle, beginning at offset opt. If -1, all elements up to the end of the array in will be shuffled.

Shuffles the elements of the given array randomly.


public static method Shuffle2 → (4)<T>

random in : RandomNumber

[not-null]
The random number generator to use.

list in : IVector<T>

[not-null]
The list to shuffle.

offset opt : int32 = 0

[0..list.Count]
Offset to first element in list in to shuffle.

count opt : int32 = -1

[-1..list.Count-offset]
Number of elements to shuffle, beginning at offset opt. If -1, all elements up to the end of the list in will be shuffled.

Shuffles the elements of the given list randomly.

Sort​Array​Default


public static method SortArrayDefault → (4)<T>

array in : T [ ]

[not-null]
The array that holds the range to sort.

comparer in : CompareDelegate<T>

[not-null]
The compare to use.

offset opt : int32 = 0

[0..array.Length]
Index of first array element to sort.

count opt : int32 = -1

[-1..array.Length-offset]
Length of array range to sort. If -1, all remaining elements will be used.

Sorts the given array range.

The method uses an unstable hybrid quicksort (median-3 pivot) / insertion sort algorithm.

See also

CompareDelegate

Sort​Array​Object

2 overloads


public static method SortArrayObject1 → (3)
<T ref : IComparable<T>>

array in : T [ ]

[not-null]
The array that holds the range to sort.

offset opt : int32 = 0

[0..array.Length]
Index of first array element to sort.

count opt : int32 = -1

[-1..array.Length-offset]
Length of array range to sort. If -1, all remaining elements will be used.

Sorts the given array range.

The method uses an unstable hybrid quicksort (median-3 pivot) / insertion sort algorithm.


public static method SortArrayObject2 → (3)

array in : string [ ]

[not-null]
The array that holds the range to sort.

offset opt : int32 = 0

[0..array.Length]
Index of first array element to sort.

count opt : int32 = -1

[-1..array.Length-offset]
Length of array range to sort. If -1, all remaining elements will be used.

Sorts the given array range.

The method uses an unstable hybrid quicksort (median-3 pivot) / insertion sort algorithm.

Sort​Array​Value


public static method SortArrayValue → (3)
<T val : IComparable<T>>

array in : T [ ]

[not-null]
The array that holds the range to sort.

offset opt : int32 = 0

[0..array.Length]
Index of first array element to sort.

count opt : int32 = -1

[-1..array.Length-offset]
Length of array range to sort. If -1, all remaining elements will be used.

Sorts the given array range.

The method uses an unstable hybrid quicksort (median-3 pivot) / insertion sort algorithm.

Sort​Index​Array​Default


public static method SortIndexArrayDefault → (5)<T>

array in : T [ ]

[not-null]
The array that holds the elements to compare. This array will not be modified.

indices in : int32 [ ]

[not-null]
The array indices to be sorted (the values in this array are used to access array in).

comparer in : CompareDelegate<T>

[not-null]
The compare to use.

offset opt : int32 = 0

[0..array.Length]
Index of first array element to sort.

count opt : int32 = -1

[-1..array.Length-offset]
Length of array range to sort. If -1, all remaining elements will be used.

Sorts the given array range.

The method uses a stable hybrid quicksort (median-3 pivot) / insertion sort algorithm.

See also

CompareDelegate

Sort​Index​Array​Object

2 overloads


public static method SortIndexArrayObject1 → (4)
<T ref : IComparable<T>>

array in : T [ ]

[not-null]
The array that holds the elements to compare. This array will not be modified.

indices in : int32 [ ]

[not-null]
The array indices to be sorted (the values in this array are used to access array in).

offset opt : int32 = 0

[0..array.Length]
Index of first array element to sort.

count opt : int32 = -1

[-1..array.Length-offset]
Length of array range to sort. If -1, all remaining elements will be used.

Sorts the given array range.

The method uses a stable hybrid quicksort (median-3 pivot) / insertion sort algorithm.


public static method SortIndexArrayObject2 → (4)

array in : string [ ]

[not-null]
The array that holds the elements to compare. This array will not be modified.

indices in : int32 [ ]

[not-null]
The array indices to be sorted (the values in this array are used to access array in).

offset opt : int32 = 0

[0..array.Length]
Index of first array element to sort.

count opt : int32 = -1

[-1..array.Length-offset]
Length of array range to sort. If -1, all remaining elements will be used.

Sorts the given array range.

The method uses a stable hybrid quicksort (median-3 pivot) / insertion sort algorithm.

Sort​Index​Array​Value


public static method SortIndexArrayValue → (4)
<T val : IComparable<T>>

array in : T [ ]

[not-null]
The array that holds the elements to compare. This array will not be modified.

indices in : int32 [ ]

[not-null]
The array indices to be sorted (the values in this array are used to access array in).

offset opt : int32 = 0

[0..array.Length]
Index of first array element to sort.

count opt : int32 = -1

[-1..array.Length-offset]
Length of array range to sort. If -1, all remaining elements will be used.

Sorts the given array range.

The method uses a stable hybrid quicksort (median-3 pivot) / insertion sort algorithm.

Sort​List​Default


public static method SortListDefault → (4)<T>

list in : IVector<T>

[not-null]
The list that holds the range to sort.

comparer in : CompareDelegate<T>

[not-null]
The compare to use.

offset opt : int32 = 0

[0..list.Count]
Index of first list element to sort.

count opt : int32 = -1

[-1..list.Count-offset]
Length of list range to sort. If -1, all remaining elements will be used.

Sorts the given list range.

The method uses an unstable hybrid quicksort (median-3 pivot) / insertion sort algorithm.

See also

CompareDelegate

Sort​List​Object

2 overloads


public static method SortListObject1 → (3)
<T ref : IComparable<T>>

list in : IVector<T>

[not-null]
The list that holds the range to sort.

offset opt : int32 = 0

[0..list.Count]
Index of first list element to sort.

count opt : int32 = -1

[-1..list.Count-offset]
Length of list range to sort. If -1, all remaining elements will be used.

Sorts the given list range.

The method uses an unstable hybrid quicksort (median-3 pivot) / insertion sort algorithm.


public static method SortListObject2 → (3)

list in : IVector<string>

[not-null]
The list that holds the range to sort.

offset opt : int32 = 0

[0..list.Count]
Index of first list element to sort.

count opt : int32 = -1

[-1..list.Count-offset]
Length of list range to sort. If -1, all remaining elements will be used.

Sorts the given list range.

The method uses an unstable hybrid quicksort (median-3 pivot) / insertion sort algorithm.

Sort​List​Value


public static method SortListValue → (3)
<T val : IComparable<T>>

list in : IVector<T>

[not-null]
The list that holds the range to sort.

offset opt : int32 = 0

[0..list.Count]
Index of first list element to sort.

count opt : int32 = -1

[-1..list.Count-offset]
Length of list range to sort. If -1, all remaining elements will be used.

Sorts the given list range.

The method uses an unstable hybrid quicksort (median-3 pivot) / insertion sort algorithm.

Sort​Tuple4

2 overloads


[Pure]
public static method SortTuple41 → (4)

d0 in : int64

First value to sort.

d1 in : int64

Second value to sort.

d2 in : int64

Third value to sort.

d3 in : int64

Fourth value to sort.

returns → int32

Bits 0..1 : sort order index of d0 in
Bits 2..3 : sort order index of d1 in
Bits 4..5 : sort order index of d2 in
Bits 6..7 : sort order index of d3 in

Sorts the given values.


[Pure]
public static method SortTuple42 → (4)

d0 in : float64

First value to sort.

d1 in : float64

Second value to sort.

d2 in : float64

Third value to sort.

d3 in : float64

Fourth value to sort.

returns → int32

Bits 0..1 : sort order index of d0 in
Bits 2..3 : sort order index of d1 in
Bits 4..5 : sort order index of d2 in
Bits 6..7 : sort order index of d3 in

Sorts the given values.

Sort​Tuple4​Default


[Pure]
public static method SortTuple4Default → (5)<T>

d0 in : T

First value to sort.

d1 in : T

Second value to sort.

d2 in : T

Third value to sort.

d3 in : T

Fourth value to sort.

compare in : CompareDelegate<T>

[not-null]
The compare to use.

returns → Vec4I

The sort order:
Vec4I.X: sort order index of d0 in
Vec4I.Y: sort order index of d1 in
Vec4I.Z: sort order index of d2 in
Vec4I.W: sort order index of d3 in

Sorts the given values.

See also

CompareDelegate

Sort​Tuple4​Value


[Pure]
public static method SortTuple4Value → (4)
<T val : IComparable<T>>

d0 in : T

First value to sort.

d1 in : T

Second value to sort.

d2 in : T

Third value to sort.

d3 in : T

Fourth value to sort.

returns → Vec4I

The sort order:
Vec4I.X: sort order index of d0 in
Vec4I.Y: sort order index of d1 in
Vec4I.Z: sort order index of d2 in
Vec4I.W: sort order index of d3 in

Sorts the given values.

Sort​Tuple8

2 overloads


[Pure]
public static method SortTuple81 → (8)

d0 in : int64

First value to sort.

d1 in : int64

Second value to sort.

d2 in : int64

Third value to sort.

d3 in : int64

Fourth value to sort.

d4 in : int64

Fifth value to sort.

d5 in : int64

Sixth value to sort.

d6 in : int64

Seventh value to sort.

d7 in : int64

Eighth value to sort.

returns → int32

Bits 0.. 2 : sort order index of d0 in
Bits 3.. 5 : sort order index of d1 in
Bits 6.. 8 : sort order index of d2 in
Bits 9..11 : sort order index of d3 in
Bits 12..14 : sort order index of d4 in
Bits 15..17 : sort order index of d5 in
Bits 18..20 : sort order index of d6 in
Bits 21..23 : sort order index of d7 in

Sorts the given values.


[Pure]
public static method SortTuple82 → (8)

d0 in : float64

First value to sort.

d1 in : float64

Second value to sort.

d2 in : float64

Third value to sort.

d3 in : float64

Fourth value to sort.

d4 in : float64

Fifth value to sort.

d5 in : float64

Sixth value to sort.

d6 in : float64

Seventh value to sort.

d7 in : float64

Eighth value to sort.

returns → int32

Bits 0.. 2 : sort order index of d0 in
Bits 3.. 5 : sort order index of d1 in
Bits 6.. 8 : sort order index of d2 in
Bits 9..11 : sort order index of d3 in
Bits 12..14 : sort order index of d4 in
Bits 15..17 : sort order index of d5 in
Bits 18..20 : sort order index of d6 in
Bits 21..23 : sort order index of d7 in

Sorts the given values.