Skip to content

Commit

Permalink
Procedurally generate Arrays.sol (OpenZeppelin#4859)
Browse files Browse the repository at this point in the history
Co-authored-by: ernestognw <ernestognw@gmail.com>
Co-authored-by: Hadrien Croubois <hadrien.croubois@gmail.com>
  • Loading branch information
3 people authored Mar 27, 2024
1 parent d1f39cd commit 5e3ba29
Show file tree
Hide file tree
Showing 5 changed files with 416 additions and 19 deletions.
24 changes: 21 additions & 3 deletions contracts/utils/Arrays.sol
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.0.0) (utils/Arrays.sol)
// This file was procedurally generated from scripts/generate/templates/Arrays.js.

pragma solidity ^0.8.20;

Expand Down Expand Up @@ -35,11 +36,20 @@ library Arrays {
* @dev Variant of {sort} that sorts an array of bytes32 in increasing order.
*/
function sort(bytes32[] memory array) internal pure returns (bytes32[] memory) {
return sort(array, _defaultComp);
sort(array, _defaultComp);
return array;
}

/**
* @dev Variant of {sort} that sorts an array of address following a provided comparator function.
* @dev Sort an array of address (in memory) following the provided comparator function.
*
* This function does the sorting "in place", meaning that it overrides the input. The object is returned for
* convenience, but that returned value can be discarded safely if the caller has a memory pointer to the array.
*
* NOTE: this function's cost is `O(n · log(n))` in average and `O(n²)` in the worst case, with n the length of the
* array. Using it in view functions that are executed through `eth_call` is safe, but one should be very careful
* when executing this as part of a transaction. If the array being sorted is too large, the sort operation may
* consume more gas than is available in a block, leading to potential DoS.
*/
function sort(
address[] memory array,
Expand All @@ -58,7 +68,15 @@ library Arrays {
}

/**
* @dev Variant of {sort} that sorts an array of uint256 following a provided comparator function.
* @dev Sort an array of uint256 (in memory) following the provided comparator function.
*
* This function does the sorting "in place", meaning that it overrides the input. The object is returned for
* convenience, but that returned value can be discarded safely if the caller has a memory pointer to the array.
*
* NOTE: this function's cost is `O(n · log(n))` in average and `O(n²)` in the worst case, with n the length of the
* array. Using it in view functions that are executed through `eth_call` is safe, but one should be very careful
* when executing this as part of a transaction. If the array being sorted is too large, the sort operation may
* consume more gas than is available in a block, leading to potential DoS.
*/
function sort(
uint256[] memory array,
Expand Down
1 change: 1 addition & 0 deletions scripts/generate/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ for (const [file, template] of Object.entries({
'utils/structs/EnumerableMap.sol': './templates/EnumerableMap.js',
'utils/structs/Checkpoints.sol': './templates/Checkpoints.js',
'utils/StorageSlot.sol': './templates/StorageSlot.js',
'utils/Arrays.sol': './templates/Arrays.js',
})) {
generateFromTemplate(file, template, './contracts/');
}
Expand Down
Loading

0 comments on commit 5e3ba29

Please sign in to comment.