Module arrayutil
Various utility functions for working with arrays.
An array is sub-form of a array, the array is simply indexed with numbers like this:
local array = { 1 = "a", 2 = "b", 3 = "c" } local array = { "a", "b", "c" }
Functions
contains (array, item, equals, offset_step) | Gets if the given array contains the given item. |
create2d (start_x, start_y, end_x, end_y, default_value) | Creates a 2D array with the given bounds and sets it to the given default value. |
create3d (start_x, start_y, start_z, end_x, end_y, end_z, default_value) | Creates a 3D array with the given bounds and sets it to the given default value. |
index (array, item, equals, offset_step) | Gets the index of the item in the given array. |
next_matching_column (array, start_index, matcher, reverse) | Finds the next matching column. |
next_matching_row (array, start_index, matcher, reverse) | Finds the next matching row. |
previous_matching_column (array, start_index, matcher) | Finds the previous matching column. |
previous_matching_row (array, start_index, matcher) | Finds the previous matching row. |
reduce2d (array, is_empty) | Removes empty rows and columns at the beginning and the end of the given array. |
swapped_reindex2d (data, new_x, new_y) | Reindexes the given 2D array, swapping the two dimensions. |
swapped_reindex3d (data, new_x, new_y, new_z) | Reindexes the given 3d array, swapping the two dimensions. |
Functions
- contains (array, item, equals, offset_step)
-
Gets if the given array contains the given item.
Parameters:
- array The array to search in.
- item The item to search for, can either be an item or another array.
- equals Optional. The function to determine if items equal each other, defaults to tableutil.equals.
- offset_step Optional. If the given item is an array, this determines how much of the array is skipped before it is tried to match.
Returns:
-
true if the array contains the given item.
- create2d (start_x, start_y, end_x, end_y, default_value)
-
Creates a 2D array with the given bounds and sets it to the given default
value.
Parameters:
- start_x The start of the first dimension, inclusive.
- start_y The start of the second dimension, inclusive.
- end_x The end of the first dimension, inclusive.
- end_y The end of the second dimension, inclusive.
- default_value The default value that will be set, it will be cloned for every entry.
Returns:
-
The created 2D array.
- create3d (start_x, start_y, start_z, end_x, end_y, end_z, default_value)
-
Creates a 3D array with the given bounds and sets it to the given default
value.
Parameters:
- start_x The start of the first dimension, inclusive.
- start_y The start of the second dimension, inclusive.
- start_z The start of the third dimension, inclusive.
- end_x The end of the first dimension, inclusive.
- end_y The end of the second dimension, inclusive.
- end_z The end of the third dimension, inclusive.
- default_value The default value that will be set, it will be cloned for every entry.
Returns:
-
The created 3D array.
- index (array, item, equals, offset_step)
-
Gets the index of the item in the given array.
Parameters:
- array The array to search in.
- item The item to search for, can either be an item or another array.
- equals Optional. The function to determine if items equal each other, defaults to tableutil.equals.
- offset_step Optional. If the given item is an array, this determines how much of the array is skipped before it is tried to match.
Returns:
-
The index of the given item or array, -1 if it was not found.
- next_matching_column (array, start_index, matcher, reverse)
-
Finds the next matching column.
Parameters:
- array The 2D array to search.
- start_index Optional. The index at which to start. Defaults to 1, or if the direction is reversed, the number of columns in the array.
- matcher Optional. The function that is used to determine if the column matches or not. Is expected to take one argument, the item, and return a boolean. The column matches if any of its items matches this condition. Defaults to not nil and not empty string.
- reverse Optional. If the array should be serched backwards. Defaults to false.
Returns:
-
The index of the matching column. -1 if none was found.
- next_matching_row (array, start_index, matcher, reverse)
-
Finds the next matching row.
Parameters:
- array The 2D array to search.
- start_index Optional. The index at which to start. Defaults to 1, or if the direction is reversed, the number of rows in the array.
- matcher Optional. The function that is used to determine if the row matches or not. Is expected to take one argument, the item, and return a boolean. The row matches if any of its items matches this condition. Defaults to not nil and not empty string.
- reverse Optional. If the array should be serched backwards. Defaults to false.
Returns:
-
The index of the matching row. -1 if none was found.
- previous_matching_column (array, start_index, matcher)
-
Finds the previous matching column.
Parameters:
- array The 2D array to search.
- start_index Optional. The index at which to start. Defaults to the number columns in the array.
- matcher Optional. The function that is used to determine if the column matches or not. Is expected to take one argument, the item, and return a boolean. The column matches if any of its items matches this condition. Defaults to not nil and not empty string.
Returns:
-
The index of the matching column. -1 if none was found.
- previous_matching_row (array, start_index, matcher)
-
Finds the previous matching row.
Parameters:
- array The 2D array to search.
- start_index Optional. The index at which to start. Defaults to the number rows in the array.
- matcher Optional. The function that is used to determine if the row matches or not. Is expected to take one argument, the item, and return a boolean. The row matches if any of its items matches this condition. Defaults to not nil and not empty string.
Returns:
-
The index of the matching row. -1 if none was found.
- reduce2d (array, is_empty)
-
Removes empty rows and columns at the beginning and the end of the given
array.
Parameters:
- array The array.
- is_empty Optional. The function used for determining if the item is empty. By default nil and an empty string is considered empty. Expected is a function that takes one item and returns a boolean.
- swapped_reindex2d (data, new_x, new_y)
-
Reindexes the given 2D array, swapping the two dimensions.
Parameters:
- data The array to reindex.
- new_x The new startpoint for the first dimension.
- new_y The new startpoint for the second dimension.
Returns:
-
The reindexed array.
- swapped_reindex3d (data, new_x, new_y, new_z)
-
Reindexes the given 3d array, swapping the two dimensions.
Parameters:
- data The array to reindex.
- new_x The new startpoint for the first dimension.
- new_y The new startpoint for the second dimension.
- new_z The new startpoint for the third dimension.
Returns:
-
The reindexed array.