Arrays.sol 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts (last updated v5.0.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. * NOTE: The `array` is expected to be sorted in ascending order, and to
  18. * contain no repeated elements.
  19. *
  20. * IMPORTANT: Deprecated. This implementation behaves as {lowerBound} but lacks
  21. * support for repeated elements in the array. The {lowerBound} function should
  22. * be used instead.
  23. */
  24. function findUpperBound(uint256[] storage array, uint256 element) internal view returns (uint256) {
  25. uint256 low = 0;
  26. uint256 high = array.length;
  27. if (high == 0) {
  28. return 0;
  29. }
  30. while (low < high) {
  31. uint256 mid = Math.average(low, high);
  32. // Note that mid will always be strictly less than high (i.e. it will be a valid array index)
  33. // because Math.average rounds towards zero (it does integer division with truncation).
  34. if (unsafeAccess(array, mid).value > element) {
  35. high = mid;
  36. } else {
  37. low = mid + 1;
  38. }
  39. }
  40. // At this point `low` is the exclusive upper bound. We will return the inclusive upper bound.
  41. if (low > 0 && unsafeAccess(array, low - 1).value == element) {
  42. return low - 1;
  43. } else {
  44. return low;
  45. }
  46. }
  47. /**
  48. * @dev Searches an `array` sorted in ascending order and returns the first
  49. * index that contains a value greater or equal than `element`. If no such index
  50. * exists (i.e. all values in the array are strictly less than `element`), the array
  51. * length is returned. Time complexity O(log n).
  52. *
  53. * See C++'s https://en.cppreference.com/w/cpp/algorithm/lower_bound[lower_bound].
  54. */
  55. function lowerBound(uint256[] storage array, uint256 element) internal view returns (uint256) {
  56. uint256 low = 0;
  57. uint256 high = array.length;
  58. if (high == 0) {
  59. return 0;
  60. }
  61. while (low < high) {
  62. uint256 mid = Math.average(low, high);
  63. // Note that mid will always be strictly less than high (i.e. it will be a valid array index)
  64. // because Math.average rounds towards zero (it does integer division with truncation).
  65. if (unsafeAccess(array, mid).value < element) {
  66. // this cannot overflow because mid < high
  67. unchecked {
  68. low = mid + 1;
  69. }
  70. } else {
  71. high = mid;
  72. }
  73. }
  74. return low;
  75. }
  76. /**
  77. * @dev Searches an `array` sorted in ascending order and returns the first
  78. * index that contains a value strictly greater than `element`. If no such index
  79. * exists (i.e. all values in the array are strictly less than `element`), the array
  80. * length is returned. Time complexity O(log n).
  81. *
  82. * See C++'s https://en.cppreference.com/w/cpp/algorithm/upper_bound[upper_bound].
  83. */
  84. function upperBound(uint256[] storage array, uint256 element) internal view returns (uint256) {
  85. uint256 low = 0;
  86. uint256 high = array.length;
  87. if (high == 0) {
  88. return 0;
  89. }
  90. while (low < high) {
  91. uint256 mid = Math.average(low, high);
  92. // Note that mid will always be strictly less than high (i.e. it will be a valid array index)
  93. // because Math.average rounds towards zero (it does integer division with truncation).
  94. if (unsafeAccess(array, mid).value > element) {
  95. high = mid;
  96. } else {
  97. // this cannot overflow because mid < high
  98. unchecked {
  99. low = mid + 1;
  100. }
  101. }
  102. }
  103. return low;
  104. }
  105. /**
  106. * @dev Same as {lowerBound}, but with an array in memory.
  107. */
  108. function lowerBoundMemory(uint256[] memory array, uint256 element) internal pure returns (uint256) {
  109. uint256 low = 0;
  110. uint256 high = array.length;
  111. if (high == 0) {
  112. return 0;
  113. }
  114. while (low < high) {
  115. uint256 mid = Math.average(low, high);
  116. // Note that mid will always be strictly less than high (i.e. it will be a valid array index)
  117. // because Math.average rounds towards zero (it does integer division with truncation).
  118. if (unsafeMemoryAccess(array, mid) < element) {
  119. // this cannot overflow because mid < high
  120. unchecked {
  121. low = mid + 1;
  122. }
  123. } else {
  124. high = mid;
  125. }
  126. }
  127. return low;
  128. }
  129. /**
  130. * @dev Same as {upperBound}, but with an array in memory.
  131. */
  132. function upperBoundMemory(uint256[] memory array, uint256 element) internal pure returns (uint256) {
  133. uint256 low = 0;
  134. uint256 high = array.length;
  135. if (high == 0) {
  136. return 0;
  137. }
  138. while (low < high) {
  139. uint256 mid = Math.average(low, high);
  140. // Note that mid will always be strictly less than high (i.e. it will be a valid array index)
  141. // because Math.average rounds towards zero (it does integer division with truncation).
  142. if (unsafeMemoryAccess(array, mid) > element) {
  143. high = mid;
  144. } else {
  145. // this cannot overflow because mid < high
  146. unchecked {
  147. low = mid + 1;
  148. }
  149. }
  150. }
  151. return low;
  152. }
  153. /**
  154. * @dev Access an array in an "unsafe" way. Skips solidity "index-out-of-range" check.
  155. *
  156. * WARNING: Only use if you are certain `pos` is lower than the array length.
  157. */
  158. function unsafeAccess(address[] storage arr, uint256 pos) internal pure returns (StorageSlot.AddressSlot storage) {
  159. bytes32 slot;
  160. // We use assembly to calculate the storage slot of the element at index `pos` of the dynamic array `arr`
  161. // following https://docs.soliditylang.org/en/v0.8.20/internals/layout_in_storage.html#mappings-and-dynamic-arrays.
  162. /// @solidity memory-safe-assembly
  163. assembly {
  164. mstore(0, arr.slot)
  165. slot := add(keccak256(0, 0x20), pos)
  166. }
  167. return slot.getAddressSlot();
  168. }
  169. /**
  170. * @dev Access an array in an "unsafe" way. Skips solidity "index-out-of-range" check.
  171. *
  172. * WARNING: Only use if you are certain `pos` is lower than the array length.
  173. */
  174. function unsafeAccess(bytes32[] storage arr, uint256 pos) internal pure returns (StorageSlot.Bytes32Slot storage) {
  175. bytes32 slot;
  176. // We use assembly to calculate the storage slot of the element at index `pos` of the dynamic array `arr`
  177. // following https://docs.soliditylang.org/en/v0.8.20/internals/layout_in_storage.html#mappings-and-dynamic-arrays.
  178. /// @solidity memory-safe-assembly
  179. assembly {
  180. mstore(0, arr.slot)
  181. slot := add(keccak256(0, 0x20), pos)
  182. }
  183. return slot.getBytes32Slot();
  184. }
  185. /**
  186. * @dev Access an array in an "unsafe" way. Skips solidity "index-out-of-range" check.
  187. *
  188. * WARNING: Only use if you are certain `pos` is lower than the array length.
  189. */
  190. function unsafeAccess(uint256[] storage arr, uint256 pos) internal pure returns (StorageSlot.Uint256Slot storage) {
  191. bytes32 slot;
  192. // We use assembly to calculate the storage slot of the element at index `pos` of the dynamic array `arr`
  193. // following https://docs.soliditylang.org/en/v0.8.20/internals/layout_in_storage.html#mappings-and-dynamic-arrays.
  194. /// @solidity memory-safe-assembly
  195. assembly {
  196. mstore(0, arr.slot)
  197. slot := add(keccak256(0, 0x20), pos)
  198. }
  199. return slot.getUint256Slot();
  200. }
  201. /**
  202. * @dev Access an array in an "unsafe" way. Skips solidity "index-out-of-range" check.
  203. *
  204. * WARNING: Only use if you are certain `pos` is lower than the array length.
  205. */
  206. function unsafeMemoryAccess(uint256[] memory arr, uint256 pos) internal pure returns (uint256 res) {
  207. assembly {
  208. res := mload(add(add(arr, 0x20), mul(pos, 0x20)))
  209. }
  210. }
  211. /**
  212. * @dev Access an array in an "unsafe" way. Skips solidity "index-out-of-range" check.
  213. *
  214. * WARNING: Only use if you are certain `pos` is lower than the array length.
  215. */
  216. function unsafeMemoryAccess(address[] memory arr, uint256 pos) internal pure returns (address res) {
  217. assembly {
  218. res := mload(add(add(arr, 0x20), mul(pos, 0x20)))
  219. }
  220. }
  221. }