Arrays.sol 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts (last updated v4.8.0-rc.2) (utils/Arrays.sol)
  3. pragma solidity ^0.8.0;
  4. import "./StorageSlot.sol";
  5. import "./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. if (array.length == 0) {
  22. return 0;
  23. }
  24. uint256 low = 0;
  25. uint256 high = array.length;
  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 down (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. /// @solidity memory-safe-assembly
  51. assembly {
  52. mstore(0, arr.slot)
  53. slot := add(keccak256(0, 0x20), pos)
  54. }
  55. return slot.getAddressSlot();
  56. }
  57. /**
  58. * @dev Access an array in an "unsafe" way. Skips solidity "index-out-of-range" check.
  59. *
  60. * WARNING: Only use if you are certain `pos` is lower than the array length.
  61. */
  62. function unsafeAccess(bytes32[] storage arr, uint256 pos) internal pure returns (StorageSlot.Bytes32Slot storage) {
  63. bytes32 slot;
  64. /// @solidity memory-safe-assembly
  65. assembly {
  66. mstore(0, arr.slot)
  67. slot := add(keccak256(0, 0x20), pos)
  68. }
  69. return slot.getBytes32Slot();
  70. }
  71. /**
  72. * @dev Access an array in an "unsafe" way. Skips solidity "index-out-of-range" check.
  73. *
  74. * WARNING: Only use if you are certain `pos` is lower than the array length.
  75. */
  76. function unsafeAccess(uint256[] storage arr, uint256 pos) internal pure returns (StorageSlot.Uint256Slot storage) {
  77. bytes32 slot;
  78. /// @solidity memory-safe-assembly
  79. assembly {
  80. mstore(0, arr.slot)
  81. slot := add(keccak256(0, 0x20), pos)
  82. }
  83. return slot.getUint256Slot();
  84. }
  85. }