mirror of openzeppelin-contracts
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
openzeppelin-contracts/contracts/utils/Arrays.sol

488 lines
18 KiB

// 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;
import {SlotDerivation} from "./SlotDerivation.sol";
import {StorageSlot} from "./StorageSlot.sol";
import {Math} from "./math/Math.sol";
/**
merge api docs changes Squashed commit of the following: commit 06243c3e8e86074ff8e9e3f22b7829a2c315d707 Merge: 991882ec 99373558 Author: Francisco Giordano <frangio.1@gmail.com> Date: Thu May 23 18:15:37 2019 -0300 Merge branch 'api-docs' into api-docs-merge commit 991882eca5bb8a3391995154bfb9d53d8a69cb4f Author: Francisco Giordano <frangio.1@gmail.com> Date: Thu May 23 18:08:02 2019 -0300 manually apply docs changes and renamings commit fa1f6e97dd67a76c3cd828d0a5e19b4ac6c37acb Author: Francisco Giordano <frangio.1@gmail.com> Date: Thu May 23 17:36:03 2019 -0300 move functions to new order commit 99373558e3af4905d29bc6f3f542ba93d28a24ab Author: Francisco Giordano <frangio.1@gmail.com> Date: Thu May 23 16:23:40 2019 -0300 add missing docs links commit d180d6c36a6f5460e85473ee5a18992d1449a6ff Author: Francisco Giordano <frangio.1@gmail.com> Date: Thu May 23 16:14:12 2019 -0300 update solidity-docgen dependency fixes uri encoded links commit faab0e50da91cd2f0a409e3ad32e2db127ad319a Author: Francisco Giordano <frangio.1@gmail.com> Date: Thu May 23 16:05:03 2019 -0300 update openzeppelin-docsite and solidity-docgen dependencies add visibility specifiers commit ef305268bb2735e488e35d16819a4b432b3a35e3 Author: Nicolás Venturo <nicolas.venturo@gmail.com> Date: Thu May 23 15:36:45 2019 -0300 Fix guide links. commit 339b20dbfa2d5f6ea02e63c2f3fdcba0fe879c3c Author: Nicolás Venturo <nicolas.venturo@gmail.com> Date: Thu May 23 13:37:51 2019 -0300 Fix typos. commit 6c7b97460578b9eea90b53c280454e361f8f0052 Author: Francisco Giordano <frangio.1@gmail.com> Date: Thu May 23 15:26:29 2019 -0300 fix utilities guide links commit 0e7692a8fd8516a11becc4121d77d792439600b1 Author: Francisco Giordano <frangio.1@gmail.com> Date: Thu May 23 15:23:19 2019 -0300 update solidity-docgen dependency commit ebb8a8651516ece21736c6c3b2577eb1b3487651 Author: Francisco Giordano <frangio.1@gmail.com> Date: Thu May 23 15:15:01 2019 -0300 fix utilities guide links commit 5ec47d62785e1d6e5f8e91edca58f2dc7f87d7a3 Author: Francisco Giordano <frangio.1@gmail.com> Date: Thu May 23 15:14:49 2019 -0300 fix escrow docs ordering commit cdcdc909b16f219a9a3272036b6a8f21e34b48ef Author: Francisco Giordano <frangio.1@gmail.com> Date: Thu May 23 13:35:07 2019 -0300 add wip notice commit 987e2951ae93211c8c70c8288e30573555c57830 Author: Francisco Giordano <frangio.1@gmail.com> Date: Thu May 23 13:09:35 2019 -0300 update openzeppelin-docsite dependency fixes links to old versions commit b00d22c0affac2e2108df8b773dfa1706afcb44e Author: Francisco Giordano <frangio.1@gmail.com> Date: Thu May 23 13:09:28 2019 -0300 fix guide links commit f112a9400c5e5ad495c8e0fdb972e26987b34244 Author: Francisco Giordano <frangio.1@gmail.com> Date: Wed May 22 20:42:37 2019 -0300 update docsite commit 68aacdd56a29e35a84f6732f9293612bbcaf7520 Author: Nicolás Venturo <nicolas.venturo@gmail.com> Date: Wed May 22 20:00:39 2019 -0300 ERC20Capped commit 4edce78bab2c6d140f3ea3f33db71e92ca4d8aaf Author: Nicolás Venturo <nicolas.venturo@gmail.com> Date: Wed May 22 19:52:30 2019 -0300 Unnecessary polish on token docs. commit 2a4c91cf49c2736dc09c1c03cf383911def1a1b2 Author: Francisco Giordano <frangio.1@gmail.com> Date: Wed May 22 19:20:05 2019 -0300 rename guides commit 61dd818ea76d4c170c4ab175c6be0d6067d21a29 Author: Nicolás Venturo <nicolas.venturo@gmail.com> Date: Wed May 22 17:04:09 2019 -0300 ERC1820 docs. commit 77b5f0353123b76358dc6d86bdc646c86c9b0bea Author: Nicolás Venturo <nicolas.venturo@gmail.com> Date: Wed May 22 16:17:34 2019 -0300 Introspection and ERC165. commit 76641a253b3b70279802c5134dd107532eea4b2c Author: Francisco Giordano <frangio.1@gmail.com> Date: Wed May 22 17:59:53 2019 -0300 update docgen commit 7be98bc3fbd3566231f943f01b9acb58567d755b Author: Francisco Giordano <frangio.1@gmail.com> Date: Wed May 22 17:23:50 2019 -0300 update solidity-docgen commit f7268e6e010f8ef9ac83df241a803f91efc08c0c Author: Francisco Giordano <frangio.1@gmail.com> Date: Wed May 22 16:58:31 2019 -0300 update docgen commit 2a8c7a378e8962a5baeb334b2492815f05075f98 Author: Nicolás Venturo <nicolas.venturo@gmail.com> Date: Wed May 22 14:36:35 2019 -0300 Util docs. commit 327ae8ff45a1a523c7591bf4996c4a9b52d7ec7a Author: Francisco Giordano <frangio.1@gmail.com> Date: Wed May 22 13:08:50 2019 -0300 add missing drafts commit 5e7f71335ac8423c0e363ae8c7ad9b2977f202f8 Author: Francisco Giordano <frangio.1@gmail.com> Date: Wed May 22 12:47:41 2019 -0300 tweak ierc20 docs commit cd0e86a0b712f74ffd406e072d4b1fbf4dd2c176 Author: Francisco Giordano <frangio.1@gmail.com> Date: Wed May 22 12:46:45 2019 -0300 add some erc721 docs commit e081184159417f71da14bc0fc110b7b11e29d75d Author: Francisco Giordano <frangio.1@gmail.com> Date: Wed May 22 12:41:46 2019 -0300 update docsite commit 0beb75784022419d47123c2a9fe7a5f1eb87f9b2 Author: Francisco Giordano <frangio.1@gmail.com> Date: Wed May 22 12:22:27 2019 -0300 correct drafts structure commit 2e94b287c7cead7a6c0603205670566461c31abb Author: Francisco Giordano <frangio.1@gmail.com> Date: Wed May 22 11:56:25 2019 -0300 fix docsite-start script commit 0fa4160484309d0851584fe57c0d81a3600977db Author: Francisco Giordano <frangio.1@gmail.com> Date: Wed May 22 11:47:44 2019 -0300 improve docsite start script (automatically watch docgen) commit 9d571897cc03bee92035964cf7a2cfeda1e2f690 Author: Francisco Giordano <frangio.1@gmail.com> Date: Wed May 22 11:30:37 2019 -0300 update solidity-docgen commit 82980f5aefbdfb8a9815a3b7b0e88e970b65dd5d Author: Francisco Giordano <frangio.1@gmail.com> Date: Tue May 21 19:15:13 2019 -0300 edit docs for Secondary commit 00d7a005b0530bee730b77a1b69a95f1b4ffe315 Author: Francisco Giordano <frangio.1@gmail.com> Date: Tue May 21 19:15:13 2019 -0300 edit docs for ownable commit b0c4c2bdf83eca5d4a71792daac603236733d46e Author: Francisco Giordano <frangio.1@gmail.com> Date: Tue May 21 18:27:13 2019 -0300 change title of Math section commit deb788583f191780e55b7f673520eaf13a5c7e23 Author: Francisco Giordano <frangio.1@gmail.com> Date: Tue May 21 18:26:59 2019 -0300 capitalization commit f2bcf85d343ea4a0739fe22a77b1e22c2296ddea Author: Francisco Giordano <frangio.1@gmail.com> Date: Tue May 21 18:26:06 2019 -0300 edit docs for Pausable commit 73ba0cf43dbb44c39c1bf2ee63ef9fe558faa919 Author: Nicolás Venturo <nicolas.venturo@gmail.com> Date: Sat May 18 19:08:06 2019 -0300 Crypto docs. commit 9d6fc6223f51cf2321b2e3217c512579654c3917 Merge: 7e21f8f7 9f1cec12 Author: Nicolás Venturo <nicolas.venturo@gmail.com> Date: Fri May 17 17:23:15 2019 -0300 Merge branch 'api-docs-777' into api-docs commit 9f1cec12e3351fb1b5fc0b59f5ded39928064a56 Author: Nicolás Venturo <nicolas.venturo@gmail.com> Date: Fri May 17 17:22:54 2019 -0300 ERC777 done. commit 7e21f8f7b6982d2f92df302cdf6ec62522d8ffff Author: Francisco Giordano <frangio.1@gmail.com> Date: Fri May 17 16:39:47 2019 -0300 add math docs commit f18d1f17023b6e5b42ae04fc38aa1170e6863864 Author: Nicolás Venturo <nicolas.venturo@gmail.com> Date: Thu May 16 20:01:46 2019 -0300 First draft of ERC777 docs. commit 985c5d305329fd9d400120d86dce5c386e19cd50 Author: Nicolás Venturo <nicolas.venturo@gmail.com> Date: Thu May 16 19:14:32 2019 -0300 Final draft for IERC777. commit bf53f133d987b67f938a329e6d659ba3483aab0b Author: Francisco Giordano <frangio.1@gmail.com> Date: Thu May 16 19:13:37 2019 -0300 more work on ERC20 api docs commit b7c250b7cb4669448cfab5535c4ff70b94a15635 Author: Nicolás Venturo <nicolas.venturo@gmail.com> Date: Thu May 16 17:08:47 2019 -0300 Fix typo. commit 197bbfbfc67a09607ead492b834879c62b3d905a Author: Nicolás Venturo <nicolas.venturo@gmail.com> Date: Thu May 16 17:05:14 2019 -0300 Initial draft of IERC777. commit 7dc3b55161c860437a8f13a2ce5808b1c3dd70a2 Author: Francisco Giordano <frangio.1@gmail.com> Date: Thu May 16 11:58:32 2019 -0300 add payment docs structure commit da16fc4480243181e58c3440e977e76a91a1839a Author: Nicolás Venturo <nicolas.venturo@gmail.com> Date: Thu May 16 16:05:33 2019 -0300 Initial ERC777 docstrings. commit 9f6a7e35bd2f045e6063ca2f93c67b792c0ef47c Author: Francisco Giordano <frangio.1@gmail.com> Date: Wed May 15 22:13:17 2019 -0300 partial pass through ERC20 docs
6 years ago
* @dev Collection of functions related to array types.
*/
library Arrays {
using SlotDerivation for bytes32;
using StorageSlot for bytes32;
/**
* @dev Sort an array of bytes32 (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(
bytes32[] memory array,
function(bytes32, bytes32) pure returns (bool) comp
) internal pure returns (bytes32[] memory) {
_quickSort(_begin(array), _end(array), comp);
return array;
}
/**
* @dev Variant of {sort} that sorts an array of bytes32 in increasing order.
*/
function sort(bytes32[] memory array) internal pure returns (bytes32[] memory) {
sort(array, _defaultComp);
return array;
}
/**
* @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,
function(address, address) pure returns (bool) comp
) internal pure returns (address[] memory) {
sort(_castToBytes32Array(array), _castToBytes32Comp(comp));
return array;
}
/**
* @dev Variant of {sort} that sorts an array of address in increasing order.
*/
function sort(address[] memory array) internal pure returns (address[] memory) {
sort(_castToBytes32Array(array), _defaultComp);
return array;
}
/**
* @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,
function(uint256, uint256) pure returns (bool) comp
) internal pure returns (uint256[] memory) {
sort(_castToBytes32Array(array), _castToBytes32Comp(comp));
return array;
}
/**
* @dev Variant of {sort} that sorts an array of uint256 in increasing order.
*/
function sort(uint256[] memory array) internal pure returns (uint256[] memory) {
sort(_castToBytes32Array(array), _defaultComp);
return array;
}
/**
* @dev Performs a quick sort of a segment of memory. The segment sorted starts at `begin` (inclusive), and stops
* at end (exclusive). Sorting follows the `comp` comparator.
*
* Invariant: `begin <= end`. This is the case when initially called by {sort} and is preserved in subcalls.
*
* IMPORTANT: Memory locations between `begin` and `end` are not validated/zeroed. This function should
* be used only if the limits are within a memory array.
*/
function _quickSort(uint256 begin, uint256 end, function(bytes32, bytes32) pure returns (bool) comp) private pure {
unchecked {
if (end - begin < 0x40) return;
// Use first element as pivot
bytes32 pivot = _mload(begin);
// Position where the pivot should be at the end of the loop
uint256 pos = begin;
for (uint256 it = begin + 0x20; it < end; it += 0x20) {
if (comp(_mload(it), pivot)) {
// If the value stored at the iterator's position comes before the pivot, we increment the
// position of the pivot and move the value there.
pos += 0x20;
_swap(pos, it);
}
}
_swap(begin, pos); // Swap pivot into place
_quickSort(begin, pos, comp); // Sort the left side of the pivot
_quickSort(pos + 0x20, end, comp); // Sort the right side of the pivot
}
}
/**
* @dev Pointer to the memory location of the first element of `array`.
*/
function _begin(bytes32[] memory array) private pure returns (uint256 ptr) {
/// @solidity memory-safe-assembly
assembly {
ptr := add(array, 0x20)
}
}
/**
* @dev Pointer to the memory location of the first memory word (32bytes) after `array`. This is the memory word
* that comes just after the last element of the array.
*/
function _end(bytes32[] memory array) private pure returns (uint256 ptr) {
unchecked {
return _begin(array) + array.length * 0x20;
}
}
/**
* @dev Load memory word (as a bytes32) at location `ptr`.
*/
function _mload(uint256 ptr) private pure returns (bytes32 value) {
assembly {
value := mload(ptr)
}
}
/**
* @dev Swaps the elements memory location `ptr1` and `ptr2`.
*/
function _swap(uint256 ptr1, uint256 ptr2) private pure {
assembly {
let value1 := mload(ptr1)
let value2 := mload(ptr2)
mstore(ptr1, value2)
mstore(ptr2, value1)
}
}
/// @dev Comparator for sorting arrays in increasing order.
function _defaultComp(bytes32 a, bytes32 b) private pure returns (bool) {
return a < b;
}
/// @dev Helper: low level cast address memory array to uint256 memory array
function _castToBytes32Array(address[] memory input) private pure returns (bytes32[] memory output) {
assembly {
output := input
}
}
/// @dev Helper: low level cast uint256 memory array to uint256 memory array
function _castToBytes32Array(uint256[] memory input) private pure returns (bytes32[] memory output) {
assembly {
output := input
}
}
/// @dev Helper: low level cast address comp function to bytes32 comp function
function _castToBytes32Comp(
function(address, address) pure returns (bool) input
) private pure returns (function(bytes32, bytes32) pure returns (bool) output) {
assembly {
output := input
}
}
/// @dev Helper: low level cast uint256 comp function to bytes32 comp function
function _castToBytes32Comp(
function(uint256, uint256) pure returns (bool) input
) private pure returns (function(bytes32, bytes32) pure returns (bool) output) {
assembly {
output := input
}
}
/**
merge api docs changes Squashed commit of the following: commit 06243c3e8e86074ff8e9e3f22b7829a2c315d707 Merge: 991882ec 99373558 Author: Francisco Giordano <frangio.1@gmail.com> Date: Thu May 23 18:15:37 2019 -0300 Merge branch 'api-docs' into api-docs-merge commit 991882eca5bb8a3391995154bfb9d53d8a69cb4f Author: Francisco Giordano <frangio.1@gmail.com> Date: Thu May 23 18:08:02 2019 -0300 manually apply docs changes and renamings commit fa1f6e97dd67a76c3cd828d0a5e19b4ac6c37acb Author: Francisco Giordano <frangio.1@gmail.com> Date: Thu May 23 17:36:03 2019 -0300 move functions to new order commit 99373558e3af4905d29bc6f3f542ba93d28a24ab Author: Francisco Giordano <frangio.1@gmail.com> Date: Thu May 23 16:23:40 2019 -0300 add missing docs links commit d180d6c36a6f5460e85473ee5a18992d1449a6ff Author: Francisco Giordano <frangio.1@gmail.com> Date: Thu May 23 16:14:12 2019 -0300 update solidity-docgen dependency fixes uri encoded links commit faab0e50da91cd2f0a409e3ad32e2db127ad319a Author: Francisco Giordano <frangio.1@gmail.com> Date: Thu May 23 16:05:03 2019 -0300 update openzeppelin-docsite and solidity-docgen dependencies add visibility specifiers commit ef305268bb2735e488e35d16819a4b432b3a35e3 Author: Nicolás Venturo <nicolas.venturo@gmail.com> Date: Thu May 23 15:36:45 2019 -0300 Fix guide links. commit 339b20dbfa2d5f6ea02e63c2f3fdcba0fe879c3c Author: Nicolás Venturo <nicolas.venturo@gmail.com> Date: Thu May 23 13:37:51 2019 -0300 Fix typos. commit 6c7b97460578b9eea90b53c280454e361f8f0052 Author: Francisco Giordano <frangio.1@gmail.com> Date: Thu May 23 15:26:29 2019 -0300 fix utilities guide links commit 0e7692a8fd8516a11becc4121d77d792439600b1 Author: Francisco Giordano <frangio.1@gmail.com> Date: Thu May 23 15:23:19 2019 -0300 update solidity-docgen dependency commit ebb8a8651516ece21736c6c3b2577eb1b3487651 Author: Francisco Giordano <frangio.1@gmail.com> Date: Thu May 23 15:15:01 2019 -0300 fix utilities guide links commit 5ec47d62785e1d6e5f8e91edca58f2dc7f87d7a3 Author: Francisco Giordano <frangio.1@gmail.com> Date: Thu May 23 15:14:49 2019 -0300 fix escrow docs ordering commit cdcdc909b16f219a9a3272036b6a8f21e34b48ef Author: Francisco Giordano <frangio.1@gmail.com> Date: Thu May 23 13:35:07 2019 -0300 add wip notice commit 987e2951ae93211c8c70c8288e30573555c57830 Author: Francisco Giordano <frangio.1@gmail.com> Date: Thu May 23 13:09:35 2019 -0300 update openzeppelin-docsite dependency fixes links to old versions commit b00d22c0affac2e2108df8b773dfa1706afcb44e Author: Francisco Giordano <frangio.1@gmail.com> Date: Thu May 23 13:09:28 2019 -0300 fix guide links commit f112a9400c5e5ad495c8e0fdb972e26987b34244 Author: Francisco Giordano <frangio.1@gmail.com> Date: Wed May 22 20:42:37 2019 -0300 update docsite commit 68aacdd56a29e35a84f6732f9293612bbcaf7520 Author: Nicolás Venturo <nicolas.venturo@gmail.com> Date: Wed May 22 20:00:39 2019 -0300 ERC20Capped commit 4edce78bab2c6d140f3ea3f33db71e92ca4d8aaf Author: Nicolás Venturo <nicolas.venturo@gmail.com> Date: Wed May 22 19:52:30 2019 -0300 Unnecessary polish on token docs. commit 2a4c91cf49c2736dc09c1c03cf383911def1a1b2 Author: Francisco Giordano <frangio.1@gmail.com> Date: Wed May 22 19:20:05 2019 -0300 rename guides commit 61dd818ea76d4c170c4ab175c6be0d6067d21a29 Author: Nicolás Venturo <nicolas.venturo@gmail.com> Date: Wed May 22 17:04:09 2019 -0300 ERC1820 docs. commit 77b5f0353123b76358dc6d86bdc646c86c9b0bea Author: Nicolás Venturo <nicolas.venturo@gmail.com> Date: Wed May 22 16:17:34 2019 -0300 Introspection and ERC165. commit 76641a253b3b70279802c5134dd107532eea4b2c Author: Francisco Giordano <frangio.1@gmail.com> Date: Wed May 22 17:59:53 2019 -0300 update docgen commit 7be98bc3fbd3566231f943f01b9acb58567d755b Author: Francisco Giordano <frangio.1@gmail.com> Date: Wed May 22 17:23:50 2019 -0300 update solidity-docgen commit f7268e6e010f8ef9ac83df241a803f91efc08c0c Author: Francisco Giordano <frangio.1@gmail.com> Date: Wed May 22 16:58:31 2019 -0300 update docgen commit 2a8c7a378e8962a5baeb334b2492815f05075f98 Author: Nicolás Venturo <nicolas.venturo@gmail.com> Date: Wed May 22 14:36:35 2019 -0300 Util docs. commit 327ae8ff45a1a523c7591bf4996c4a9b52d7ec7a Author: Francisco Giordano <frangio.1@gmail.com> Date: Wed May 22 13:08:50 2019 -0300 add missing drafts commit 5e7f71335ac8423c0e363ae8c7ad9b2977f202f8 Author: Francisco Giordano <frangio.1@gmail.com> Date: Wed May 22 12:47:41 2019 -0300 tweak ierc20 docs commit cd0e86a0b712f74ffd406e072d4b1fbf4dd2c176 Author: Francisco Giordano <frangio.1@gmail.com> Date: Wed May 22 12:46:45 2019 -0300 add some erc721 docs commit e081184159417f71da14bc0fc110b7b11e29d75d Author: Francisco Giordano <frangio.1@gmail.com> Date: Wed May 22 12:41:46 2019 -0300 update docsite commit 0beb75784022419d47123c2a9fe7a5f1eb87f9b2 Author: Francisco Giordano <frangio.1@gmail.com> Date: Wed May 22 12:22:27 2019 -0300 correct drafts structure commit 2e94b287c7cead7a6c0603205670566461c31abb Author: Francisco Giordano <frangio.1@gmail.com> Date: Wed May 22 11:56:25 2019 -0300 fix docsite-start script commit 0fa4160484309d0851584fe57c0d81a3600977db Author: Francisco Giordano <frangio.1@gmail.com> Date: Wed May 22 11:47:44 2019 -0300 improve docsite start script (automatically watch docgen) commit 9d571897cc03bee92035964cf7a2cfeda1e2f690 Author: Francisco Giordano <frangio.1@gmail.com> Date: Wed May 22 11:30:37 2019 -0300 update solidity-docgen commit 82980f5aefbdfb8a9815a3b7b0e88e970b65dd5d Author: Francisco Giordano <frangio.1@gmail.com> Date: Tue May 21 19:15:13 2019 -0300 edit docs for Secondary commit 00d7a005b0530bee730b77a1b69a95f1b4ffe315 Author: Francisco Giordano <frangio.1@gmail.com> Date: Tue May 21 19:15:13 2019 -0300 edit docs for ownable commit b0c4c2bdf83eca5d4a71792daac603236733d46e Author: Francisco Giordano <frangio.1@gmail.com> Date: Tue May 21 18:27:13 2019 -0300 change title of Math section commit deb788583f191780e55b7f673520eaf13a5c7e23 Author: Francisco Giordano <frangio.1@gmail.com> Date: Tue May 21 18:26:59 2019 -0300 capitalization commit f2bcf85d343ea4a0739fe22a77b1e22c2296ddea Author: Francisco Giordano <frangio.1@gmail.com> Date: Tue May 21 18:26:06 2019 -0300 edit docs for Pausable commit 73ba0cf43dbb44c39c1bf2ee63ef9fe558faa919 Author: Nicolás Venturo <nicolas.venturo@gmail.com> Date: Sat May 18 19:08:06 2019 -0300 Crypto docs. commit 9d6fc6223f51cf2321b2e3217c512579654c3917 Merge: 7e21f8f7 9f1cec12 Author: Nicolás Venturo <nicolas.venturo@gmail.com> Date: Fri May 17 17:23:15 2019 -0300 Merge branch 'api-docs-777' into api-docs commit 9f1cec12e3351fb1b5fc0b59f5ded39928064a56 Author: Nicolás Venturo <nicolas.venturo@gmail.com> Date: Fri May 17 17:22:54 2019 -0300 ERC777 done. commit 7e21f8f7b6982d2f92df302cdf6ec62522d8ffff Author: Francisco Giordano <frangio.1@gmail.com> Date: Fri May 17 16:39:47 2019 -0300 add math docs commit f18d1f17023b6e5b42ae04fc38aa1170e6863864 Author: Nicolás Venturo <nicolas.venturo@gmail.com> Date: Thu May 16 20:01:46 2019 -0300 First draft of ERC777 docs. commit 985c5d305329fd9d400120d86dce5c386e19cd50 Author: Nicolás Venturo <nicolas.venturo@gmail.com> Date: Thu May 16 19:14:32 2019 -0300 Final draft for IERC777. commit bf53f133d987b67f938a329e6d659ba3483aab0b Author: Francisco Giordano <frangio.1@gmail.com> Date: Thu May 16 19:13:37 2019 -0300 more work on ERC20 api docs commit b7c250b7cb4669448cfab5535c4ff70b94a15635 Author: Nicolás Venturo <nicolas.venturo@gmail.com> Date: Thu May 16 17:08:47 2019 -0300 Fix typo. commit 197bbfbfc67a09607ead492b834879c62b3d905a Author: Nicolás Venturo <nicolas.venturo@gmail.com> Date: Thu May 16 17:05:14 2019 -0300 Initial draft of IERC777. commit 7dc3b55161c860437a8f13a2ce5808b1c3dd70a2 Author: Francisco Giordano <frangio.1@gmail.com> Date: Thu May 16 11:58:32 2019 -0300 add payment docs structure commit da16fc4480243181e58c3440e977e76a91a1839a Author: Nicolás Venturo <nicolas.venturo@gmail.com> Date: Thu May 16 16:05:33 2019 -0300 Initial ERC777 docstrings. commit 9f6a7e35bd2f045e6063ca2f93c67b792c0ef47c Author: Francisco Giordano <frangio.1@gmail.com> Date: Wed May 15 22:13:17 2019 -0300 partial pass through ERC20 docs
6 years ago
* @dev Searches a sorted `array` and returns the first index that contains
* a value greater or equal to `element`. If no such index exists (i.e. all
* values in the array are strictly less than `element`), the array length is
merge api docs changes Squashed commit of the following: commit 06243c3e8e86074ff8e9e3f22b7829a2c315d707 Merge: 991882ec 99373558 Author: Francisco Giordano <frangio.1@gmail.com> Date: Thu May 23 18:15:37 2019 -0300 Merge branch 'api-docs' into api-docs-merge commit 991882eca5bb8a3391995154bfb9d53d8a69cb4f Author: Francisco Giordano <frangio.1@gmail.com> Date: Thu May 23 18:08:02 2019 -0300 manually apply docs changes and renamings commit fa1f6e97dd67a76c3cd828d0a5e19b4ac6c37acb Author: Francisco Giordano <frangio.1@gmail.com> Date: Thu May 23 17:36:03 2019 -0300 move functions to new order commit 99373558e3af4905d29bc6f3f542ba93d28a24ab Author: Francisco Giordano <frangio.1@gmail.com> Date: Thu May 23 16:23:40 2019 -0300 add missing docs links commit d180d6c36a6f5460e85473ee5a18992d1449a6ff Author: Francisco Giordano <frangio.1@gmail.com> Date: Thu May 23 16:14:12 2019 -0300 update solidity-docgen dependency fixes uri encoded links commit faab0e50da91cd2f0a409e3ad32e2db127ad319a Author: Francisco Giordano <frangio.1@gmail.com> Date: Thu May 23 16:05:03 2019 -0300 update openzeppelin-docsite and solidity-docgen dependencies add visibility specifiers commit ef305268bb2735e488e35d16819a4b432b3a35e3 Author: Nicolás Venturo <nicolas.venturo@gmail.com> Date: Thu May 23 15:36:45 2019 -0300 Fix guide links. commit 339b20dbfa2d5f6ea02e63c2f3fdcba0fe879c3c Author: Nicolás Venturo <nicolas.venturo@gmail.com> Date: Thu May 23 13:37:51 2019 -0300 Fix typos. commit 6c7b97460578b9eea90b53c280454e361f8f0052 Author: Francisco Giordano <frangio.1@gmail.com> Date: Thu May 23 15:26:29 2019 -0300 fix utilities guide links commit 0e7692a8fd8516a11becc4121d77d792439600b1 Author: Francisco Giordano <frangio.1@gmail.com> Date: Thu May 23 15:23:19 2019 -0300 update solidity-docgen dependency commit ebb8a8651516ece21736c6c3b2577eb1b3487651 Author: Francisco Giordano <frangio.1@gmail.com> Date: Thu May 23 15:15:01 2019 -0300 fix utilities guide links commit 5ec47d62785e1d6e5f8e91edca58f2dc7f87d7a3 Author: Francisco Giordano <frangio.1@gmail.com> Date: Thu May 23 15:14:49 2019 -0300 fix escrow docs ordering commit cdcdc909b16f219a9a3272036b6a8f21e34b48ef Author: Francisco Giordano <frangio.1@gmail.com> Date: Thu May 23 13:35:07 2019 -0300 add wip notice commit 987e2951ae93211c8c70c8288e30573555c57830 Author: Francisco Giordano <frangio.1@gmail.com> Date: Thu May 23 13:09:35 2019 -0300 update openzeppelin-docsite dependency fixes links to old versions commit b00d22c0affac2e2108df8b773dfa1706afcb44e Author: Francisco Giordano <frangio.1@gmail.com> Date: Thu May 23 13:09:28 2019 -0300 fix guide links commit f112a9400c5e5ad495c8e0fdb972e26987b34244 Author: Francisco Giordano <frangio.1@gmail.com> Date: Wed May 22 20:42:37 2019 -0300 update docsite commit 68aacdd56a29e35a84f6732f9293612bbcaf7520 Author: Nicolás Venturo <nicolas.venturo@gmail.com> Date: Wed May 22 20:00:39 2019 -0300 ERC20Capped commit 4edce78bab2c6d140f3ea3f33db71e92ca4d8aaf Author: Nicolás Venturo <nicolas.venturo@gmail.com> Date: Wed May 22 19:52:30 2019 -0300 Unnecessary polish on token docs. commit 2a4c91cf49c2736dc09c1c03cf383911def1a1b2 Author: Francisco Giordano <frangio.1@gmail.com> Date: Wed May 22 19:20:05 2019 -0300 rename guides commit 61dd818ea76d4c170c4ab175c6be0d6067d21a29 Author: Nicolás Venturo <nicolas.venturo@gmail.com> Date: Wed May 22 17:04:09 2019 -0300 ERC1820 docs. commit 77b5f0353123b76358dc6d86bdc646c86c9b0bea Author: Nicolás Venturo <nicolas.venturo@gmail.com> Date: Wed May 22 16:17:34 2019 -0300 Introspection and ERC165. commit 76641a253b3b70279802c5134dd107532eea4b2c Author: Francisco Giordano <frangio.1@gmail.com> Date: Wed May 22 17:59:53 2019 -0300 update docgen commit 7be98bc3fbd3566231f943f01b9acb58567d755b Author: Francisco Giordano <frangio.1@gmail.com> Date: Wed May 22 17:23:50 2019 -0300 update solidity-docgen commit f7268e6e010f8ef9ac83df241a803f91efc08c0c Author: Francisco Giordano <frangio.1@gmail.com> Date: Wed May 22 16:58:31 2019 -0300 update docgen commit 2a8c7a378e8962a5baeb334b2492815f05075f98 Author: Nicolás Venturo <nicolas.venturo@gmail.com> Date: Wed May 22 14:36:35 2019 -0300 Util docs. commit 327ae8ff45a1a523c7591bf4996c4a9b52d7ec7a Author: Francisco Giordano <frangio.1@gmail.com> Date: Wed May 22 13:08:50 2019 -0300 add missing drafts commit 5e7f71335ac8423c0e363ae8c7ad9b2977f202f8 Author: Francisco Giordano <frangio.1@gmail.com> Date: Wed May 22 12:47:41 2019 -0300 tweak ierc20 docs commit cd0e86a0b712f74ffd406e072d4b1fbf4dd2c176 Author: Francisco Giordano <frangio.1@gmail.com> Date: Wed May 22 12:46:45 2019 -0300 add some erc721 docs commit e081184159417f71da14bc0fc110b7b11e29d75d Author: Francisco Giordano <frangio.1@gmail.com> Date: Wed May 22 12:41:46 2019 -0300 update docsite commit 0beb75784022419d47123c2a9fe7a5f1eb87f9b2 Author: Francisco Giordano <frangio.1@gmail.com> Date: Wed May 22 12:22:27 2019 -0300 correct drafts structure commit 2e94b287c7cead7a6c0603205670566461c31abb Author: Francisco Giordano <frangio.1@gmail.com> Date: Wed May 22 11:56:25 2019 -0300 fix docsite-start script commit 0fa4160484309d0851584fe57c0d81a3600977db Author: Francisco Giordano <frangio.1@gmail.com> Date: Wed May 22 11:47:44 2019 -0300 improve docsite start script (automatically watch docgen) commit 9d571897cc03bee92035964cf7a2cfeda1e2f690 Author: Francisco Giordano <frangio.1@gmail.com> Date: Wed May 22 11:30:37 2019 -0300 update solidity-docgen commit 82980f5aefbdfb8a9815a3b7b0e88e970b65dd5d Author: Francisco Giordano <frangio.1@gmail.com> Date: Tue May 21 19:15:13 2019 -0300 edit docs for Secondary commit 00d7a005b0530bee730b77a1b69a95f1b4ffe315 Author: Francisco Giordano <frangio.1@gmail.com> Date: Tue May 21 19:15:13 2019 -0300 edit docs for ownable commit b0c4c2bdf83eca5d4a71792daac603236733d46e Author: Francisco Giordano <frangio.1@gmail.com> Date: Tue May 21 18:27:13 2019 -0300 change title of Math section commit deb788583f191780e55b7f673520eaf13a5c7e23 Author: Francisco Giordano <frangio.1@gmail.com> Date: Tue May 21 18:26:59 2019 -0300 capitalization commit f2bcf85d343ea4a0739fe22a77b1e22c2296ddea Author: Francisco Giordano <frangio.1@gmail.com> Date: Tue May 21 18:26:06 2019 -0300 edit docs for Pausable commit 73ba0cf43dbb44c39c1bf2ee63ef9fe558faa919 Author: Nicolás Venturo <nicolas.venturo@gmail.com> Date: Sat May 18 19:08:06 2019 -0300 Crypto docs. commit 9d6fc6223f51cf2321b2e3217c512579654c3917 Merge: 7e21f8f7 9f1cec12 Author: Nicolás Venturo <nicolas.venturo@gmail.com> Date: Fri May 17 17:23:15 2019 -0300 Merge branch 'api-docs-777' into api-docs commit 9f1cec12e3351fb1b5fc0b59f5ded39928064a56 Author: Nicolás Venturo <nicolas.venturo@gmail.com> Date: Fri May 17 17:22:54 2019 -0300 ERC777 done. commit 7e21f8f7b6982d2f92df302cdf6ec62522d8ffff Author: Francisco Giordano <frangio.1@gmail.com> Date: Fri May 17 16:39:47 2019 -0300 add math docs commit f18d1f17023b6e5b42ae04fc38aa1170e6863864 Author: Nicolás Venturo <nicolas.venturo@gmail.com> Date: Thu May 16 20:01:46 2019 -0300 First draft of ERC777 docs. commit 985c5d305329fd9d400120d86dce5c386e19cd50 Author: Nicolás Venturo <nicolas.venturo@gmail.com> Date: Thu May 16 19:14:32 2019 -0300 Final draft for IERC777. commit bf53f133d987b67f938a329e6d659ba3483aab0b Author: Francisco Giordano <frangio.1@gmail.com> Date: Thu May 16 19:13:37 2019 -0300 more work on ERC20 api docs commit b7c250b7cb4669448cfab5535c4ff70b94a15635 Author: Nicolás Venturo <nicolas.venturo@gmail.com> Date: Thu May 16 17:08:47 2019 -0300 Fix typo. commit 197bbfbfc67a09607ead492b834879c62b3d905a Author: Nicolás Venturo <nicolas.venturo@gmail.com> Date: Thu May 16 17:05:14 2019 -0300 Initial draft of IERC777. commit 7dc3b55161c860437a8f13a2ce5808b1c3dd70a2 Author: Francisco Giordano <frangio.1@gmail.com> Date: Thu May 16 11:58:32 2019 -0300 add payment docs structure commit da16fc4480243181e58c3440e977e76a91a1839a Author: Nicolás Venturo <nicolas.venturo@gmail.com> Date: Thu May 16 16:05:33 2019 -0300 Initial ERC777 docstrings. commit 9f6a7e35bd2f045e6063ca2f93c67b792c0ef47c Author: Francisco Giordano <frangio.1@gmail.com> Date: Wed May 15 22:13:17 2019 -0300 partial pass through ERC20 docs
6 years ago
* returned. Time complexity O(log n).
*
* NOTE: The `array` is expected to be sorted in ascending order, and to
* contain no repeated elements.
*
* IMPORTANT: Deprecated. This implementation behaves as {lowerBound} but lacks
* support for repeated elements in the array. The {lowerBound} function should
* be used instead.
*/
function findUpperBound(uint256[] storage array, uint256 element) internal view returns (uint256) {
uint256 low = 0;
uint256 high = array.length;
if (high == 0) {
return 0;
}
while (low < high) {
uint256 mid = Math.average(low, high);
// Note that mid will always be strictly less than high (i.e. it will be a valid array index)
// because Math.average rounds towards zero (it does integer division with truncation).
if (unsafeAccess(array, mid).value > element) {
high = mid;
} else {
low = mid + 1;
}
}
// At this point `low` is the exclusive upper bound. We will return the inclusive upper bound.
if (low > 0 && unsafeAccess(array, low - 1).value == element) {
return low - 1;
} else {
return low;
}
}
/**
* @dev Searches an `array` sorted in ascending order and returns the first
* index that contains a value greater or equal than `element`. If no such index
* exists (i.e. all values in the array are strictly less than `element`), the array
* length is returned. Time complexity O(log n).
*
* See C++'s https://en.cppreference.com/w/cpp/algorithm/lower_bound[lower_bound].
*/
function lowerBound(uint256[] storage array, uint256 element) internal view returns (uint256) {
uint256 low = 0;
uint256 high = array.length;
if (high == 0) {
return 0;
}
while (low < high) {
uint256 mid = Math.average(low, high);
// Note that mid will always be strictly less than high (i.e. it will be a valid array index)
// because Math.average rounds towards zero (it does integer division with truncation).
if (unsafeAccess(array, mid).value < element) {
// this cannot overflow because mid < high
unchecked {
low = mid + 1;
}
} else {
high = mid;
}
}
return low;
}
/**
* @dev Searches an `array` sorted in ascending order and returns the first
* index that contains a value strictly greater than `element`. If no such index
* exists (i.e. all values in the array are strictly less than `element`), the array
* length is returned. Time complexity O(log n).
*
* See C++'s https://en.cppreference.com/w/cpp/algorithm/upper_bound[upper_bound].
*/
function upperBound(uint256[] storage array, uint256 element) internal view returns (uint256) {
uint256 low = 0;
uint256 high = array.length;
if (high == 0) {
return 0;
}
while (low < high) {
uint256 mid = Math.average(low, high);
// Note that mid will always be strictly less than high (i.e. it will be a valid array index)
// because Math.average rounds towards zero (it does integer division with truncation).
if (unsafeAccess(array, mid).value > element) {
high = mid;
} else {
// this cannot overflow because mid < high
unchecked {
low = mid + 1;
}
}
}
return low;
}
/**
* @dev Same as {lowerBound}, but with an array in memory.
*/
function lowerBoundMemory(uint256[] memory array, uint256 element) internal pure returns (uint256) {
uint256 low = 0;
uint256 high = array.length;
if (high == 0) {
return 0;
}
while (low < high) {
uint256 mid = Math.average(low, high);
// Note that mid will always be strictly less than high (i.e. it will be a valid array index)
// because Math.average rounds towards zero (it does integer division with truncation).
if (unsafeMemoryAccess(array, mid) < element) {
// this cannot overflow because mid < high
unchecked {
low = mid + 1;
}
} else {
high = mid;
}
}
return low;
}
/**
* @dev Same as {upperBound}, but with an array in memory.
*/
function upperBoundMemory(uint256[] memory array, uint256 element) internal pure returns (uint256) {
uint256 low = 0;
uint256 high = array.length;
if (high == 0) {
return 0;
}
while (low < high) {
uint256 mid = Math.average(low, high);
// Note that mid will always be strictly less than high (i.e. it will be a valid array index)
// because Math.average rounds towards zero (it does integer division with truncation).
if (unsafeMemoryAccess(array, mid) > element) {
high = mid;
} else {
// this cannot overflow because mid < high
unchecked {
low = mid + 1;
}
}
}
return low;
}
/**
* @dev Access an array in an "unsafe" way. Skips solidity "index-out-of-range" check.
*
* WARNING: Only use if you are certain `pos` is lower than the array length.
*/
function unsafeAccess(address[] storage arr, uint256 pos) internal pure returns (StorageSlot.AddressSlot storage) {
bytes32 slot;
/// @solidity memory-safe-assembly
assembly {
slot := arr.slot
}
return slot.deriveArray().offset(pos).getAddressSlot();
}
/**
* @dev Access an array in an "unsafe" way. Skips solidity "index-out-of-range" check.
*
* WARNING: Only use if you are certain `pos` is lower than the array length.
*/
function unsafeAccess(bytes32[] storage arr, uint256 pos) internal pure returns (StorageSlot.Bytes32Slot storage) {
bytes32 slot;
/// @solidity memory-safe-assembly
assembly {
slot := arr.slot
}
return slot.deriveArray().offset(pos).getBytes32Slot();
}
/**
* @dev Access an array in an "unsafe" way. Skips solidity "index-out-of-range" check.
*
* WARNING: Only use if you are certain `pos` is lower than the array length.
*/
function unsafeAccess(uint256[] storage arr, uint256 pos) internal pure returns (StorageSlot.Uint256Slot storage) {
bytes32 slot;
/// @solidity memory-safe-assembly
assembly {
slot := arr.slot
}
return slot.deriveArray().offset(pos).getUint256Slot();
}
/**
* @dev Access an array in an "unsafe" way. Skips solidity "index-out-of-range" check.
*
* WARNING: Only use if you are certain `pos` is lower than the array length.
*/
function unsafeMemoryAccess(address[] memory arr, uint256 pos) internal pure returns (address res) {
assembly {
res := mload(add(add(arr, 0x20), mul(pos, 0x20)))
}
}
/**
* @dev Access an array in an "unsafe" way. Skips solidity "index-out-of-range" check.
*
* WARNING: Only use if you are certain `pos` is lower than the array length.
*/
function unsafeMemoryAccess(bytes32[] memory arr, uint256 pos) internal pure returns (bytes32 res) {
assembly {
res := mload(add(add(arr, 0x20), mul(pos, 0x20)))
}
}
/**
* @dev Access an array in an "unsafe" way. Skips solidity "index-out-of-range" check.
*
* WARNING: Only use if you are certain `pos` is lower than the array length.
*/
function unsafeMemoryAccess(uint256[] memory arr, uint256 pos) internal pure returns (uint256 res) {
assembly {
res := mload(add(add(arr, 0x20), mul(pos, 0x20)))
}
}
/**
* @dev Helper to set the length of an dynamic array. Directly writing to `.length` is forbidden.
*
* WARNING: this does not clear elements if length is reduced, of initialize elements if length is increased.
*/
function unsafeSetLength(address[] storage array, uint256 len) internal {
/// @solidity memory-safe-assembly
assembly {
sstore(array.slot, len)
}
}
/**
* @dev Helper to set the length of an dynamic array. Directly writing to `.length` is forbidden.
*
* WARNING: this does not clear elements if length is reduced, of initialize elements if length is increased.
*/
function unsafeSetLength(bytes32[] storage array, uint256 len) internal {
/// @solidity memory-safe-assembly
assembly {
sstore(array.slot, len)
}
}
/**
* @dev Helper to set the length of an dynamic array. Directly writing to `.length` is forbidden.
*
* WARNING: this does not clear elements if length is reduced, of initialize elements if length is increased.
*/
function unsafeSetLength(uint256[] storage array, uint256 len) internal {
/// @solidity memory-safe-assembly
assembly {
sstore(array.slot, len)
}
}
}