Skip to content

Commit 5e3ba29

Browse files
RenanSouza2ernestognwAmxx
authored
Procedurally generate Arrays.sol (#4859)
Co-authored-by: ernestognw <[email protected]> Co-authored-by: Hadrien Croubois <[email protected]>
1 parent d1f39cd commit 5e3ba29

File tree

5 files changed

+416
-19
lines changed

5 files changed

+416
-19
lines changed

contracts/utils/Arrays.sol

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// SPDX-License-Identifier: MIT
22
// OpenZeppelin Contracts (last updated v5.0.0) (utils/Arrays.sol)
3+
// This file was procedurally generated from scripts/generate/templates/Arrays.js.
34

45
pragma solidity ^0.8.20;
56

@@ -35,11 +36,20 @@ library Arrays {
3536
* @dev Variant of {sort} that sorts an array of bytes32 in increasing order.
3637
*/
3738
function sort(bytes32[] memory array) internal pure returns (bytes32[] memory) {
38-
return sort(array, _defaultComp);
39+
sort(array, _defaultComp);
40+
return array;
3941
}
4042

4143
/**
42-
* @dev Variant of {sort} that sorts an array of address following a provided comparator function.
44+
* @dev Sort an array of address (in memory) following the provided comparator function.
45+
*
46+
* This function does the sorting "in place", meaning that it overrides the input. The object is returned for
47+
* convenience, but that returned value can be discarded safely if the caller has a memory pointer to the array.
48+
*
49+
* 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
50+
* array. Using it in view functions that are executed through `eth_call` is safe, but one should be very careful
51+
* when executing this as part of a transaction. If the array being sorted is too large, the sort operation may
52+
* consume more gas than is available in a block, leading to potential DoS.
4353
*/
4454
function sort(
4555
address[] memory array,
@@ -58,7 +68,15 @@ library Arrays {
5868
}
5969

6070
/**
61-
* @dev Variant of {sort} that sorts an array of uint256 following a provided comparator function.
71+
* @dev Sort an array of uint256 (in memory) following the provided comparator function.
72+
*
73+
* This function does the sorting "in place", meaning that it overrides the input. The object is returned for
74+
* convenience, but that returned value can be discarded safely if the caller has a memory pointer to the array.
75+
*
76+
* 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
77+
* array. Using it in view functions that are executed through `eth_call` is safe, but one should be very careful
78+
* when executing this as part of a transaction. If the array being sorted is too large, the sort operation may
79+
* consume more gas than is available in a block, leading to potential DoS.
6280
*/
6381
function sort(
6482
uint256[] memory array,

scripts/generate/run.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ for (const [file, template] of Object.entries({
3737
'utils/structs/EnumerableMap.sol': './templates/EnumerableMap.js',
3838
'utils/structs/Checkpoints.sol': './templates/Checkpoints.js',
3939
'utils/StorageSlot.sol': './templates/StorageSlot.js',
40+
'utils/Arrays.sol': './templates/Arrays.js',
4041
})) {
4142
generateFromTemplate(file, template, './contracts/');
4243
}

0 commit comments

Comments
 (0)