Arrays.sol 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts (last updated v4.9.0) (utils/Arrays.sol)
  3. pragma solidity ^0.8.20;
  4. import {StorageSlot} from "./StorageSlot.sol";
  5. import {Math} from "./math/Math.sol";
  6. /**
  7. * @dev Collection of functions related to array types.
  8. */
  9. library Arrays {
  10. using StorageSlot for bytes32;
  11. /**
  12. * @dev Searches a sorted `array` and returns the first index that contains
  13. * a value greater or equal to `element`. If no such index exists (i.e. all
  14. * values in the array are strictly less than `element`), the array length is
  15. * returned. Time complexity O(log n).
  16. *
  17. * `array` is expected to be sorted in ascending order, and to contain no
  18. * repeated elements.
  19. */
  20. function findUpperBound(uint256[] storage array, uint256 element) internal view returns (uint256) {
  21. uint256 low = 0;
  22. uint256 high = array.length;
  23. if (high == 0) {
  24. return 0;
  25. }
  26. while (low < high) {
  27. uint256 mid = Math.average(low, high);
  28. // Note that mid will always be strictly less than high (i.e. it will be a valid array index)
  29. // because Math.average rounds towards zero (it does integer division with truncation).
  30. if (unsafeAccess(array, mid).value > element) {
  31. high = mid;
  32. } else {
  33. low = mid + 1;
  34. }
  35. }
  36. // At this point `low` is the exclusive upper bound. We will return the inclusive upper bound.
  37. if (low > 0 && unsafeAccess(array, low - 1).value == element) {
  38. return low - 1;
  39. } else {
  40. return low;
  41. }
  42. }
  43. /**
  44. * @dev Access an array in an "unsafe" way. Skips solidity "index-out-of-range" check.
  45. *
  46. * WARNING: Only use if you are certain `pos` is lower than the array length.
  47. */
  48. function unsafeAccess(address[] storage arr, uint256 pos) internal pure returns (StorageSlot.AddressSlot storage) {
  49. bytes32 slot;
  50. // We use assembly to calculate the storage slot of the element at index `pos` of the dynamic array `arr`
  51. // following https://docs.soliditylang.org/en/v0.8.20/internals/layout_in_storage.html#mappings-and-dynamic-arrays.
  52. /// @solidity memory-safe-assembly
  53. assembly {
  54. mstore(0, arr.slot)
  55. slot := add(keccak256(0, 0x20), pos)
  56. }
  57. return slot.getAddressSlot();
  58. }
  59. /**
  60. * @dev Access an array in an "unsafe" way. Skips solidity "index-out-of-range" check.
  61. *
  62. * WARNING: Only use if you are certain `pos` is lower than the array length.
  63. */
  64. function unsafeAccess(bytes32[] storage arr, uint256 pos) internal pure returns (StorageSlot.Bytes32Slot storage) {
  65. bytes32 slot;
  66. // We use assembly to calculate the storage slot of the element at index `pos` of the dynamic array `arr`
  67. // following https://docs.soliditylang.org/en/v0.8.20/internals/layout_in_storage.html#mappings-and-dynamic-arrays.
  68. /// @solidity memory-safe-assembly
  69. assembly {
  70. mstore(0, arr.slot)
  71. slot := add(keccak256(0, 0x20), pos)
  72. }
  73. return slot.getBytes32Slot();
  74. }
  75. /**
  76. * @dev Access an array in an "unsafe" way. Skips solidity "index-out-of-range" check.
  77. *
  78. * WARNING: Only use if you are certain `pos` is lower than the array length.
  79. */
  80. function unsafeAccess(uint256[] storage arr, uint256 pos) internal pure returns (StorageSlot.Uint256Slot storage) {
  81. bytes32 slot;
  82. // We use assembly to calculate the storage slot of the element at index `pos` of the dynamic array `arr`
  83. // following https://docs.soliditylang.org/en/v0.8.20/internals/layout_in_storage.html#mappings-and-dynamic-arrays.
  84. /// @solidity memory-safe-assembly
  85. assembly {
  86. mstore(0, arr.slot)
  87. slot := add(keccak256(0, 0x20), pos)
  88. }
  89. return slot.getUint256Slot();
  90. }
  91. /**
  92. * @dev Access an array in an "unsafe" way. Skips solidity "index-out-of-range" check.
  93. *
  94. * WARNING: Only use if you are certain `pos` is lower than the array length.
  95. */
  96. function unsafeMemoryAccess(uint256[] memory arr, uint256 pos) internal pure returns (uint256 res) {
  97. assembly {
  98. res := mload(add(add(arr, 0x20), mul(pos, 0x20)))
  99. }
  100. }
  101. /**
  102. * @dev Access an array in an "unsafe" way. Skips solidity "index-out-of-range" check.
  103. *
  104. * WARNING: Only use if you are certain `pos` is lower than the array length.
  105. */
  106. function unsafeMemoryAccess(address[] memory arr, uint256 pos) internal pure returns (address res) {
  107. assembly {
  108. res := mload(add(add(arr, 0x20), mul(pos, 0x20)))
  109. }
  110. }
  111. }