Heap.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. const format = require('../format-lines');
  2. const { TYPES } = require('./Heap.opts');
  3. const { capitalize } = require('../../helpers');
  4. /* eslint-disable max-len */
  5. const header = `\
  6. pragma solidity ^0.8.20;
  7. import {Math} from "../math/Math.sol";
  8. import {SafeCast} from "../math/SafeCast.sol";
  9. import {Comparators} from "../Comparators.sol";
  10. import {Panic} from "../Panic.sol";
  11. /**
  12. * @dev Library for managing https://en.wikipedia.org/wiki/Binary_heap[binary heap] that can be used as
  13. * https://en.wikipedia.org/wiki/Priority_queue[priority queue].
  14. *
  15. * Heaps are represented as an array of Node objects. This array stores two overlapping structures:
  16. * * A tree structure where the first element (index 0) is the root, and where the node at index i is the child of the
  17. * node at index (i-1)/2 and the father of nodes at index 2*i+1 and 2*i+2. Each node stores the index (in the array)
  18. * where the corresponding value is stored.
  19. * * A list of payloads values where each index contains a value and a lookup index. The type of the value depends on
  20. * the variant being used. The lookup is the index of the node (in the tree) that points to this value.
  21. *
  22. * Some invariants:
  23. * \`\`\`
  24. * i == heap.data[heap.data[i].index].lookup // for all indices i
  25. * i == heap.data[heap.data[i].lookup].index // for all indices i
  26. * \`\`\`
  27. *
  28. * The structure is ordered so that each node is bigger than its parent. An immediate consequence is that the
  29. * highest priority value is the one at the root. This value can be lookup up in constant time (O(1)) at
  30. * \`heap.data[heap.data[0].index].value\`
  31. *
  32. * The structure is designed to perform the following operations with the corresponding complexities:
  33. *
  34. * * peek (get the highest priority in set): O(1)
  35. * * insert (insert a value in the set): 0(log(n))
  36. * * pop (remove the highest priority value in set): O(log(n))
  37. * * replace (replace the highest priority value in set with a new value): O(log(n))
  38. * * length (get the number of elements in the set): O(1)
  39. * * clear (remove all elements in the set): O(1)
  40. */
  41. `;
  42. const generate = ({ struct, node, valueType, indexType, blockSize }) => `\
  43. /**
  44. * @dev Binary heap that support values of type ${valueType}.
  45. *
  46. * Each element of that structures uses ${blockSize} storage slots.
  47. */
  48. struct ${struct} {
  49. ${node}[] data;
  50. }
  51. /**
  52. * @dev Internal node type for ${struct}. Stores a value of type ${valueType}.
  53. */
  54. struct ${node} {
  55. ${valueType} value;
  56. ${indexType} index; // position -> value
  57. ${indexType} lookup; // value -> position
  58. }
  59. /**
  60. * @dev Lookup the root element of the heap.
  61. */
  62. function peek(${struct} storage self) internal view returns (${valueType}) {
  63. // self.data[0] will \`ARRAY_ACCESS_OUT_OF_BOUNDS\` panic if heap is empty.
  64. return _unsafeNodeAccess(self, self.data[0].index).value;
  65. }
  66. /**
  67. * @dev Remove (and return) the root element for the heap using the default comparator.
  68. *
  69. * NOTE: All inserting and removal from a heap should always be done using the same comparator. Mixing comparator
  70. * during the lifecycle of a heap will result in undefined behavior.
  71. */
  72. function pop(${struct} storage self) internal returns (${valueType}) {
  73. return pop(self, Comparators.lt);
  74. }
  75. /**
  76. * @dev Remove (and return) the root element for the heap using the provided comparator.
  77. *
  78. * NOTE: All inserting and removal from a heap should always be done using the same comparator. Mixing comparator
  79. * during the lifecycle of a heap will result in undefined behavior.
  80. */
  81. function pop(
  82. ${struct} storage self,
  83. function(uint256, uint256) view returns (bool) comp
  84. ) internal returns (${valueType}) {
  85. unchecked {
  86. ${indexType} size = length(self);
  87. if (size == 0) Panic.panic(Panic.EMPTY_ARRAY_POP);
  88. ${indexType} last = size - 1;
  89. // get root location (in the data array) and value
  90. ${node} storage rootNode = _unsafeNodeAccess(self, 0);
  91. ${indexType} rootIdx = rootNode.index;
  92. ${node} storage rootData = _unsafeNodeAccess(self, rootIdx);
  93. ${node} storage lastNode = _unsafeNodeAccess(self, last);
  94. ${valueType} rootDataValue = rootData.value;
  95. // if root is not the last element of the data array (that will get pop-ed), reorder the data array.
  96. if (rootIdx != last) {
  97. // get details about the value stored in the last element of the array (that will get pop-ed)
  98. ${indexType} lastDataIdx = lastNode.lookup;
  99. ${valueType} lastDataValue = lastNode.value;
  100. // copy these values to the location of the root (that is safe, and that we no longer use)
  101. rootData.value = lastDataValue;
  102. rootData.lookup = lastDataIdx;
  103. // update the tree node that used to point to that last element (value now located where the root was)
  104. _unsafeNodeAccess(self, lastDataIdx).index = rootIdx;
  105. }
  106. // get last leaf location (in the data array) and value
  107. ${indexType} lastIdx = lastNode.index;
  108. ${valueType} lastValue = _unsafeNodeAccess(self, lastIdx).value;
  109. // move the last leaf to the root, pop last leaf ...
  110. rootNode.index = lastIdx;
  111. _unsafeNodeAccess(self, lastIdx).lookup = 0;
  112. self.data.pop();
  113. // ... and heapify
  114. _siftDown(self, last, 0, lastValue, comp);
  115. // return root value
  116. return rootDataValue;
  117. }
  118. }
  119. /**
  120. * @dev Insert a new element in the heap using the default comparator.
  121. *
  122. * NOTE: All inserting and removal from a heap should always be done using the same comparator. Mixing comparator
  123. * during the lifecycle of a heap will result in undefined behavior.
  124. */
  125. function insert(${struct} storage self, ${valueType} value) internal {
  126. insert(self, value, Comparators.lt);
  127. }
  128. /**
  129. * @dev Insert a new element in the heap using the provided comparator.
  130. *
  131. * NOTE: All inserting and removal from a heap should always be done using the same comparator. Mixing comparator
  132. * during the lifecycle of a heap will result in undefined behavior.
  133. */
  134. function insert(
  135. ${struct} storage self,
  136. ${valueType} value,
  137. function(uint256, uint256) view returns (bool) comp
  138. ) internal {
  139. ${indexType} size = length(self);
  140. if (size == type(${indexType}).max) Panic.panic(Panic.RESOURCE_ERROR);
  141. self.data.push(${struct}Node({index: size, lookup: size, value: value}));
  142. _siftUp(self, size, value, comp);
  143. }
  144. /**
  145. * @dev Return the root element for the heap, and replace it with a new value, using the default comparator.
  146. * This is equivalent to using {pop} and {insert}, but requires only one rebalancing operation.
  147. *
  148. * NOTE: All inserting and removal from a heap should always be done using the same comparator. Mixing comparator
  149. * during the lifecycle of a heap will result in undefined behavior.
  150. */
  151. function replace(${struct} storage self, ${valueType} newValue) internal returns (${valueType}) {
  152. return replace(self, newValue, Comparators.lt);
  153. }
  154. /**
  155. * @dev Return the root element for the heap, and replace it with a new value, using the provided comparator.
  156. * This is equivalent to using {pop} and {insert}, but requires only one rebalancing operation.
  157. *
  158. * NOTE: All inserting and removal from a heap should always be done using the same comparator. Mixing comparator
  159. * during the lifecycle of a heap will result in undefined behavior.
  160. */
  161. function replace(
  162. ${struct} storage self,
  163. ${valueType} newValue,
  164. function(uint256, uint256) view returns (bool) comp
  165. ) internal returns (${valueType}) {
  166. ${indexType} size = length(self);
  167. if (size == 0) Panic.panic(Panic.EMPTY_ARRAY_POP);
  168. // position of the node that holds the data for the root
  169. ${indexType} rootIdx = _unsafeNodeAccess(self, 0).index;
  170. // storage pointer to the node that holds the data for the root
  171. ${node} storage rootData = _unsafeNodeAccess(self, rootIdx);
  172. // cache old value and replace it
  173. ${valueType} oldValue = rootData.value;
  174. rootData.value = newValue;
  175. // re-heapify
  176. _siftDown(self, size, 0, newValue, comp);
  177. // return old root value
  178. return oldValue;
  179. }
  180. /**
  181. * @dev Returns the number of elements in the heap.
  182. */
  183. function length(${struct} storage self) internal view returns (${indexType}) {
  184. return self.data.length.to${capitalize(indexType)}();
  185. }
  186. /**
  187. * @dev Removes all elements in the heap.
  188. */
  189. function clear(${struct} storage self) internal {
  190. ${struct}Node[] storage data = self.data;
  191. /// @solidity memory-safe-assembly
  192. assembly {
  193. sstore(data.slot, 0)
  194. }
  195. }
  196. /*
  197. * @dev Swap node \`i\` and \`j\` in the tree.
  198. */
  199. function _swap(${struct} storage self, ${indexType} i, ${indexType} j) private {
  200. ${node} storage ni = _unsafeNodeAccess(self, i);
  201. ${node} storage nj = _unsafeNodeAccess(self, j);
  202. ${indexType} ii = ni.index;
  203. ${indexType} jj = nj.index;
  204. // update pointers to the data (swap the value)
  205. ni.index = jj;
  206. nj.index = ii;
  207. // update lookup pointers for consistency
  208. _unsafeNodeAccess(self, ii).lookup = j;
  209. _unsafeNodeAccess(self, jj).lookup = i;
  210. }
  211. /**
  212. * @dev Perform heap maintenance on \`self\`, starting at position \`pos\` (with the \`value\`), using \`comp\` as a
  213. * comparator, and moving toward the leafs of the underlying tree.
  214. *
  215. * NOTE: This is a private function that is called in a trusted context with already cached parameters. \`length\`
  216. * and \`value\` could be extracted from \`self\` and \`pos\`, but that would require redundant storage read. These
  217. * parameters are not verified. It is the caller role to make sure the parameters are correct.
  218. */
  219. function _siftDown(
  220. ${struct} storage self,
  221. ${indexType} size,
  222. ${indexType} pos,
  223. ${valueType} value,
  224. function(uint256, uint256) view returns (bool) comp
  225. ) private {
  226. uint256 left = 2 * pos + 1; // this could overflow ${indexType}
  227. uint256 right = 2 * pos + 2; // this could overflow ${indexType}
  228. if (right < size) {
  229. // the check guarantees that \`left\` and \`right\` are both valid uint32
  230. ${indexType} lIndex = ${indexType}(left);
  231. ${indexType} rIndex = ${indexType}(right);
  232. ${valueType} lValue = _unsafeNodeAccess(self, _unsafeNodeAccess(self, lIndex).index).value;
  233. ${valueType} rValue = _unsafeNodeAccess(self, _unsafeNodeAccess(self, rIndex).index).value;
  234. if (comp(lValue, value) || comp(rValue, value)) {
  235. ${indexType} index = ${indexType}(comp(lValue, rValue).ternary(lIndex, rIndex));
  236. _swap(self, pos, index);
  237. _siftDown(self, size, index, value, comp);
  238. }
  239. } else if (left < size) {
  240. // the check guarantees that \`left\` is a valid uint32
  241. ${indexType} lIndex = ${indexType}(left);
  242. ${valueType} lValue = _unsafeNodeAccess(self, _unsafeNodeAccess(self, lIndex).index).value;
  243. if (comp(lValue, value)) {
  244. _swap(self, pos, lIndex);
  245. _siftDown(self, size, lIndex, value, comp);
  246. }
  247. }
  248. }
  249. /**
  250. * @dev Perform heap maintenance on \`self\`, starting at position \`pos\` (with the \`value\`), using \`comp\` as a
  251. * comparator, and moving toward the root of the underlying tree.
  252. *
  253. * NOTE: This is a private function that is called in a trusted context with already cached parameters. \`value\`
  254. * could be extracted from \`self\` and \`pos\`, but that would require redundant storage read. This parameters is not
  255. * verified. It is the caller role to make sure the parameters are correct.
  256. */
  257. function _siftUp(
  258. ${struct} storage self,
  259. ${indexType} pos,
  260. ${valueType} value,
  261. function(uint256, uint256) view returns (bool) comp
  262. ) private {
  263. unchecked {
  264. while (pos > 0) {
  265. ${indexType} parent = (pos - 1) / 2;
  266. ${valueType} parentValue = _unsafeNodeAccess(self, _unsafeNodeAccess(self, parent).index).value;
  267. if (comp(parentValue, value)) break;
  268. _swap(self, pos, parent);
  269. pos = parent;
  270. }
  271. }
  272. }
  273. function _unsafeNodeAccess(
  274. ${struct} storage self,
  275. ${indexType} pos
  276. ) private pure returns (${node} storage result) {
  277. assembly ("memory-safe") {
  278. mstore(0x00, self.slot)
  279. result.slot := add(keccak256(0x00, 0x20), ${blockSize == 1 ? 'pos' : `mul(pos, ${blockSize})`})
  280. }
  281. }
  282. `;
  283. // GENERATE
  284. module.exports = format(
  285. header.trimEnd(),
  286. 'library Heap {',
  287. format(
  288. [].concat(
  289. 'using Math for *;',
  290. 'using SafeCast for *;',
  291. '',
  292. TYPES.map(type => generate(type)),
  293. ),
  294. ).trimEnd(),
  295. '}',
  296. );