Skip to main content

ArrayHelper

Overview​

License: MIT​

library ArrayHelper

A simple library to work with arrays

Errors info​

InvalidRange​

error InvalidRange(uint256 beginIndex, uint256 endIndex)

Functions info​

lowerBound​

function lowerBound(
uint256[] storage array,
uint256 element_
) internal view returns (uint256 index_)

The function that searches for the index of the first occurring element, which is greater than or equal to the element_. The time complexity is O(log n)

Parameters:

NameTypeDescription
arrayuint256[]the array to search in
element_uint256the element

Return values:

NameTypeDescription
index_uint256the index of the found element or the length of the array if no such element

upperBound​

function upperBound(
uint256[] storage array,
uint256 element_
) internal view returns (uint256 index_)

The function that searches for the index of the first occurring element, which is greater than the element_. The time complexity is O(log n)

Parameters:

NameTypeDescription
arrayuint256[]the array to search in
element_uint256the element

Return values:

NameTypeDescription
index_uint256the index of the found element or the length of the array if no such element

contains​

function contains(
uint256[] storage array,
uint256 element_
) internal view returns (bool)

The function that searches for the element_ and returns whether it is present in the array. The time complexity is O(log n)

Parameters:

NameTypeDescription
arrayuint256[]the array to search in
element_uint256the element

Return values:

NameTypeDescription
[0]boolwhether the element_ is present in the array

getRangeSum​

function getRangeSum(
uint256[] storage prefixes,
uint256 beginIndex_,
uint256 endIndex_
) internal view returns (uint256)

The function that calculates the sum of all array elements from beginIndex_ to endIndex_ inclusive using its prefix sum array

Parameters:

NameTypeDescription
beginIndex_uint256the index of the first range element
endIndex_uint256the index of the last range element

Return values:

NameTypeDescription
[0]uint256the sum of all elements of the range

countPrefixes​

function countPrefixes(
uint256[] memory arr_
) internal pure returns (uint256[] memory prefixes_)

The function to compute the prefix sum array

Parameters:

NameTypeDescription
arr_uint256[]the initial array to be turned into the prefix sum array

Return values:

NameTypeDescription
prefixes_uint256[]the prefix sum array

reverse​

function reverse(
uint256[] memory arr_
) internal pure returns (uint256[] memory reversed_)

The function to reverse a uint256 array

Parameters:

NameTypeDescription
arr_uint256[]the array to reverse

Return values:

NameTypeDescription
reversed_uint256[]the reversed array

reverse​

function reverse(
address[] memory arr_
) internal pure returns (address[] memory reversed_)

The function to reverse an address array

reverse​

function reverse(
bool[] memory arr_
) internal pure returns (bool[] memory reversed_)

The function to reverse a bool array

reverse​

function reverse(
string[] memory arr_
) internal pure returns (string[] memory reversed_)

The function to reverse a string array

reverse​

function reverse(
bytes32[] memory arr_
) internal pure returns (bytes32[] memory reversed_)

The function to reverse a bytes32 array

insert​

function insert(
uint256[] memory to_,
uint256 index_,
uint256[] memory what_
) internal pure returns (uint256)

The function to insert a uint256 array into the other array

Parameters:

NameTypeDescription
to_uint256[]the array to insert into
index_uint256the insertion starting index
what_uint256[]the array to be inserted

Return values:

NameTypeDescription
[0]uint256the index to start the next insertion from

insert​

function insert(
address[] memory to_,
uint256 index_,
address[] memory what_
) internal pure returns (uint256)

The function to insert an address array into the other array

insert​

function insert(
bool[] memory to_,
uint256 index_,
bool[] memory what_
) internal pure returns (uint256)

The function to insert a bool array into the other array

insert​

function insert(
string[] memory to_,
uint256 index_,
string[] memory what_
) internal pure returns (uint256)

The function to insert a string array into the other array

insert​

function insert(
bytes32[] memory to_,
uint256 index_,
bytes32[] memory what_
) internal pure returns (uint256)

The function to insert a bytes32 array into the other array

crop​

function crop(
uint256[] memory array_,
uint256 newLength_
) internal pure returns (uint256[] memory)

The function to crop a uint256 array

Parameters:

NameTypeDescription
array_uint256[]the array to crop
newLength_uint256the new length of the array (has to be less or equal)

Return values:

NameTypeDescription
[0]uint256[]ref to cropped array

crop​

function crop(
address[] memory array_,
uint256 newLength_
) internal pure returns (address[] memory)

The function to crop an address array

crop​

function crop(
bool[] memory array_,
uint256 newLength_
) internal pure returns (bool[] memory)

The function to crop a bool array

crop​

function crop(
string[] memory array_,
uint256 newLength_
) internal pure returns (string[] memory)

The function to crop a string array

crop​

function crop(
bytes32[] memory array_,
uint256 newLength_
) internal pure returns (bytes32[] memory)

The function to crop a bytes32 array