SortedArray
A class to create sorted arrays. Must contain objects comparable to
one another (that can use the <
and ==
operators). Numbers and
strings support these operators by default.
local SortedArray = require("SortedArray")
local Array1 = SortedArray.new({1, 2, 3}) -- Gets sorted.
local Array2 = SortedArray.new()
Functions
new
SortedArray.
new
(
BaseArray:Â
Array
<
T
>
?
,
--
An array of data which will be sorted upon instantiation. If this is omitted, an empty array is used.
Comparison:Â
<
T
>
(
(
A:Â
T
,
B:Â
T
)
 →Â
boolean
)
?
--
An optional comparison function which is used to customize the element sorting, which will be given two elements A
and B
from the array as parameters. The function should return a boolean value specifying whether the first argument should be before the second argument in the sequence. If no comparison function is passed, the Lua-default A < B
sorting is used.
) →Â
SortedArray
<
T
>
Instantiates and returns a new SortedArray, with optional parameters.
ForEach
SortedArray:
ForEach
(
Function:Â
<
T
>
(
Value:Â
T
,
Index:Â
int
,
)
 →Â
(
)
--
The function you are running.
) →Â
(
)
Runs the given function on every element in the array.
SortedArray.new({1, 2, 3}):ForEach(function(Value)
print(Value)
end) -- prints 1, 2, and 3
Map
SortedArray:
Map
(
Predicate:Â
<
T
>
(
Value:Â
T
,
Index:Â
int
,
)
 →Â
T?
--
The function you are running.
) →Â
Array
<
T
>
--
The mapped array.
Maps the SortedArray to a new array using the given predicate.
print(SortedArray.new({1, 2, 3}):Map(function(Value)
return Value * 2
end)) -- {2, 4, 6}
MapToSortedArray
SortedArray:
MapToSortedArray
(
Predicate:Â
<
T
>
(
Value:Â
T
,
Index:Â
int
,
)
 →Â
T?
--
The function you are running.
) →Â
SortedArray
<
T
>
--
The mapped array.
Maps the SortedArray to a new SortedArray using the given predicate.
print(SortedArray.new({1, 2, 3}):MapToSortedArray(function(Value)
return Value * 2
end)) -- SortedArray<[2, 4, 6]>
Some
SortedArray:
Some
(
Predicate:Â
<
T
>
(
Value:Â
T
,
Index:Â
int
,
)
 →Â
boolean?
--
The function you are running.
) →Â
boolean
--
Whether or not the predicate was satisfied.
Runs every the given predicate on every element in the array to check if some value in the array satisfies the predicate.
print(SortedArray.new({2, 4, 6, 8}):Some(function(Value)
return Value == 4
end)) -- true
print(SortedArray.new({1, 2, 4, 6, 8}):Some(function(Value)
return Value == 3
end)) -- false
Every
SortedArray:
Every
(
Predicate:Â
<
T
>
(
Value:Â
T
,
Index:Â
int
,
)
 →Â
boolean?
--
The function you are running.
) →Â
boolean
--
Whether or not the predicate was satisfied.
Runs every the given predicate on every element in the array to check if every value in the array satisfies the predicate.
print(SortedArray.new({2, 4, 6, 8}):Every(function(Value)
return Value % 2 == 0
end)) -- true
print(SortedArray.new({1, 2, 4, 6, 8}):Every(function(Value)
return Value % 2 == 0
end)) -- false
Reduce
SortedArray:
Reduce
(
Predicate:Â
<
T
>
(
Accumulator:Â
T
,
Value:Â
T
,
Index:Â
int
,
)
 →Â
T
,
--
The function you are running.
InitialValue:Â
T?
--
The initial value of the accumulator. Defaults to the first value in the SortedArray.
) →Â
T
--
The final value of the accumulator.
The Reduce method executes a user-supplied "reducer" callback function on each element of the array, in order, passing in the return value from the calculation on the preceding element. The final result of running the reducer across all elements of the array is a single value.
local Array = SortedArray.new({1, 2, 3, 4})
local function Reducer(PreviousValue, CurrentValue)
return PreviousValue + CurrentValue
end
print(Array:Reduce(Reducer)) -- 10
print(Array:Reduce(Reducer, 5)) -- 15
ReduceRight
SortedArray:
ReduceRight
(
Predicate:Â
<
T
>
(
Accumulator:Â
T
,
Value:Â
T
,
Index:Â
int
,
)
 →Â
T
,
--
The function you are running.
InitialValue:Â
T?
--
The initial value of the accumulator. Defaults to the first value in the SortedArray.
) →Â
T
--
The final value of the accumulator.
The ReduceRight method applies a function against an accumulator and each value of the array (from right-to-left) to reduce it to a single value.
local Array = SortedArray.new({2, 30, 45, 100})
local function Reducer(PreviousValue, CurrentValue)
return PreviousValue - CurrentValue
end
print(Array:ReduceRight(Reducer)) -- prints 23
print(Array:ReduceRight(Reducer, 2)) -- prints -175
Filter
SortedArray:
Filter
(
Predicate:Â
<
T
>
(
Value:Â
T
,
Index:Â
int
,
)
 →Â
boolean?
--
The function you are running.
) →Â
Array
<
T
>
--
The filtered array.
The Filter method creates a new array with all elements that pass the test implemented by the provided function.
local EvenArray = SortedArray.new({1, 2, 3, 4, 5, 6, 7, 8, 9, 10}):Filter(function(Value)
return Value % 2 == 0
end)
print(EvenArray) -- {2, 4, 6, 8, 10}
FilterToSortedArray
SortedArray:
FilterToSortedArray
(
Predicate:Â
<
T
>
(
Value:Â
T
,
Index:Â
int
,
)
 →Â
boolean?
--
The function you are running.
) →Â
SortedArray
<
T
>
--
The filtered array.
The FilterToSortedArray method creates a new SortedArray with all elements that pass the test implemented by the provided function.
local EvenArray = SortedArray.new({1, 2, 3, 4, 5, 6, 7, 8, 9, 10}):FilterToSortedArray(function(Value)
return Value % 2 == 0
end)
print(EvenArray) -- SortedArray<[2, 4, 6, 8, 10]>
Slice
SortedArray:
Slice
(
StartIndex:Â
int?
,
--
The zero-based index at which to start extraction. A negative index can be used, indicating an offset from the end of the sequence. Slice(-2)
extracts the last two elements in the sequence. If this is not provided, it'll default to 0
. If it is greater than the index range of the sequence, an empty array is returned.
EndIndex:Â
int?
--
Zero-based index before which to end extraction. Slice
extracts up to but not including EndIndex
. For example, Slice(1, 4)
extracts the second element through the fourth element (elements indexed 1, 2, and 3). A negative index can be used, indicating an offset from the end of the sequence. Slice(2, -1)
extracts the third element through the second-to-last element in the sequence. If EndIndex
is omitted, Slice
extracts through the end of the sequence (#self
). If EndIndex
is greater than the length of the sequence, slice extracts through to the end of the sequence (#self
).
) →Â
Array
<
T
>
--
The sliced array.
The Slice()
method returns a shallow copy of a portion of an array into a new SortedArray object selected from StartIndex
to EndIndex
(EndIndex
not included) where StartIndex
and EndIndex
represent the index of items in that array. The original array will not be modified.
local Array = SortedArray.new({"Ant", "Bison", "Camel", "Duck", "Elephant"})
print(Array:Slice(2)) -- {Camel, Duck, Elephant}
print(Array:Slice(2, 4)) -- {Camel, Duck}
print(Array:Slice(1, 5)) -- {Bison, Camel, Duck, Elephant}
print(Array:Slice(-2)) -- {Duck, Elephant}
print(Array:Slice(2, -1)) -- {Camel, Duck}
SliceToSortedArray
SortedArray:
SliceToSortedArray
(
StartIndex:Â
int?
,
--
The zero-based index at which to start extraction. A negative index can be used, indicating an offset from the end of the sequence. Slice(-2)
extracts the last two elements in the sequence. If this is not provided, it'll default to 0
. If it is greater than the index range of the sequence, an empty array is returned.
EndIndex:Â
int?
--
Zero-based index before which to end extraction. Slice
extracts up to but not including EndIndex
. For example, Slice(1, 4)
extracts the second element through the fourth element (elements indexed 1, 2, and 3). A negative index can be used, indicating an offset from the end of the sequence. Slice(2, -1)
extracts the third element through the second-to-last element in the sequence. If EndIndex
is omitted, Slice
extracts through the end of the sequence (#self
). If EndIndex
is greater than the length of the sequence, slice extracts through to the end of the sequence (#self
).
) →Â
SortedArray
<
T
>
--
The sliced array.
The Slice()
method returns a shallow copy of a portion of an array into a new SortedArray object selected from StartIndex
to EndIndex
(EndIndex
not included) where StartIndex
and EndIndex
represent the index of items in that array. The original array will not be modified.
local Array = SortedArray.new({"Ant", "Bison", "Camel", "Duck", "Elephant"})
print(Array:SliceToSortedArray(2)) -- SortedArray<[Camel, Duck, Elephant]>
print(Array:SliceToSortedArray(2, 4)) -- SortedArray<[Camel, Duck]>
print(Array:SliceToSortedArray(1, 5)) -- SortedArray<[Bison, Camel, Duck, Elephant]>
print(Array:SliceToSortedArray(-2)) -- SortedArray<[Duck, Elephant]>
print(Array:SliceToSortedArray(2, -1)) -- SortedArray<[Camel, Duck]>
MapFilter
SortedArray:
MapFilter
(
Predicate:Â
<
T
>
(
Value:Â
T
,
Index:Â
int
,
)
 →Â
T?
--
The function to map and filter with.
) →Â
Array
<
T
>
--
The mapped and filtered array.
A combination function of Filter
and Map
. If the predicate function returns nil, the value will not be included in the new list. Any other result will add the result value to the new list.
local Array = SortedArray.new({1, 2, 3, 4, 5, 6, 7, 8, 9, 10}):MapFilter(function(Value)
return if Value % 2 == 0 then if Value % 3 == 0 then nil else Value else nil
end)
print(Array) -- {2, 4, 8, 10}
MapFilterToSortedArray
SortedArray:
MapFilterToSortedArray
(
Predicate:Â
<
T
>
(
Value:Â
T
,
Index:Â
int
,
)
 →Â
T?
--
The function to map and filter with.
) →Â
SortedArray
<
T
>
--
The mapped and filtered array.
A combination function of Filter
and Map
. If the predicate function returns nil, the value will not be included in the new list. Any other result will add the result value to the new list.
local Array = SortedArray.new({1, 2, 3, 4, 5, 6, 7, 8, 9, 10}):MapFilterToSortedArray(function(Value)
return if Value % 2 == 0 then if Value % 3 == 0 then nil else Value else nil
end)
print(Array) -- SortedArray<[2, 4, 8, 10]>
Insert
SortedArray:
Insert
(
Value:Â
T
--
The value you are inserting.
) →Â
int
--
The index the value was inserted at.
Inserts an element in the proper place which would preserve the array's orderedness. Returns the index the element was inserted.
local Array = SortedArray.new({1, 3, 5})
print(Array:Insert(2)) -- 2
print(Array:Insert(6)) -- 5
print(Array:Insert(4)) -- 4
print(Array) -- SortedArray<[1, 2, 3, 4, 5, 6]>
Find
SortedArray:
Find
(
Value:Â
T
,
--
The element to find or something that will be matched by the Eq
function.
Eq:Â
(
<
T
>
(
Value:Â
T
,
Other:Â
T
)
 →Â
boolean
)
?
,
--
An optional function which checks for equality between the passed-in element and the other elements in the SortedArray.
Lt:Â
(
<
T
>
(
Value:Â
T
,
Other:Â
T
)
 →Â
boolean
)
?
,
--
An optional less-than comparison function, which falls back on the comparison passed in from SortedArray.new
.
Low:Â
int?
,
--
The lowest index to search. Defaults to 1.
High:Â
int?
--
The high index to search. Defaults to the length of the SortedArray.
) →Â
int?
--
The numerical index of the element which was found, else nil.
Finds an Element in a SortedArray and returns its position (or nil if non-existant).
local Array = SortedArray.new({1, 2, 3, 4, 5})
print(Array:Find(3)) -- 3
print(Array:Find(6)) -- nil
Copy
SortedArray:
Copy
(
) →Â
Array
<
T
>
--
The shallow copied array.
Makes a shallow copy of the SortedArray.
print(SortedArray.new({1, 2, 3, 4, 5}):Copy()) -- {1, 2, 3, 4, 5}
Clone
Makes a shallow copy of the SortedArray and returns a new SortedArray.
print(SortedArray.new({1, 2, 3, 4, 5}):Clone()) -- SortedArray<[1, 2, 3, 4, 5]>
RemoveElement
SortedArray:
RemoveElement
(
Signature:Â
T
,
--
The value you want to remove.
Eq:Â
(
<
T
>
(
Value:Â
T
,
Other:Â
T
)
 →Â
boolean
)
?
,
--
An optional function which checks for equality between the passed-in element and the other elements in the SortedArray.
Lt:Â
(
<
T
>
(
Value:Â
T
,
Other:Â
T
)
 →Â
boolean
)
?
--
An optional less-than comparison function, which falls back on the comparison passed in from SortedArray.new
.
) →Â
T?
--
The removed value.
Searches the array via SortedArray:Find(Signature, Eq, Lt)
. If found, it removes the value and returns the value, otherwise returns nil. Only removes a single occurence.
local Array = SortedArray.new({1, 2, 3, 4, 5})
print(Array:RemoveElement(3)) -- 3
print(Array:RemoveElement(6)) -- nil
Sort
Does table.sort(self, self.Comparison)
and returns the SortedArray.
SortIndex
SortedArray:
SortIndex
(
Index:Â
int
--
The index to resort.
) →Â
int
--
The new position.
Removes the value at Index and re-inserts it. This is useful for when a value may have updated in a way that could change it's position in a SortedArray. Returns Index.
SortElement
SortedArray:
SortElement
(
Signature:Â
T
,
--
The value you want to re-sort.
Eq:Â
(
<
T
>
(
Value:Â
T
,
Other:Â
T
)
 →Â
boolean
)
?
,
--
An optional function which checks for equality between the passed-in element and the other elements in the SortedArray.
Lt:Â
(
<
T
>
(
Value:Â
T
,
Other:Â
T
)
 →Â
boolean
)
?
--
An optional less-than comparison function, which falls back on the comparison passed in from SortedArray.new
.
) →Â
int
--
The new position.
Calls RemoveElement(Signature, Eq, Lt)
and re-inserts the value. This is useful for when a value may have updated in a way that could change its position in a SortedArray. Returns Index.
Iterator
SortedArray:
Iterator
(
) →Â
IteratorFunction
--
The ipairs
iterator.
Performance
If you care about performance, do not use this function. Just do for Index, Value in ipairs(SortedArray) do
directly.
This returns an iterator for the SortedArray. This only exists for consistency reasons.
Concat
SortedArray:
Concat
(
Separator:Â
string?
,
--
The separator of the entries.
StartIndex:Â
int?
,
--
The index to start concatenating from.
EndIndex:Â
int?
--
The index to end concatenating at.
) →Â
string
--
The stringify SortedArray.
Calls table.concat
on the SortedArray.
RemoveIndex
SortedArray:
RemoveIndex
(
Index:Â
int?
--
The index to remove.
) →Â
T?
--
The removed value.
Calls table.remove
on the SortedArray.
Unpack
SortedArray:
Unpack
(
StartIndex:Â
int?
,
--
The index to start unpacking at.
EndIndex:Â
int?
--
The index to end unpacking at.
) →Â
...T
--
The unpacked array.
Calls table.unpack
on the SortedArray.
GetIntersection
SortedArray:
GetIntersection
(
Eq:Â
(
<
T
>
(
Value:Â
T
,
Other:Â
T
)
 →Â
boolean
)
?
,
--
An optional function which checks for equality between the passed-in element and the other elements in the SortedArray.
Lt:Â
(
<
T
>
(
Value:Â
T
,
Other:Â
T
)
 →Â
boolean
)
?
--
An optional less-than comparison function, which falls back on the comparison passed in from SortedArray.new
.
) →Â
SortedArray
<
T
>
--
A SortedArray with the common values between self and SortedArray2.
Returns a SortedArray of Commonalities between self and another SortedArray. If applicable, the returned SortedArray will inherit the Comparison function from self.
Errors
Type | Description |
---|---|
"InvalidSortedArray" | Thrown when SortedArray2 is not a SortedArray. |