Skip to content

Commit

Permalink
Fix typo in Heap.sol documentation (#5121)
Browse files Browse the repository at this point in the history
  • Loading branch information
Amxx authored Jul 24, 2024
1 parent 231fae3 commit 659f306
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 32 deletions.
38 changes: 19 additions & 19 deletions contracts/utils/structs/Heap.sol
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,17 @@ import {Panic} from "../Panic.sol";
* ```
*
* The structure is ordered so that each node is bigger than its parent. An immediate consequence is that the
* highest priority value is the one at the root. This value can be lookup up in constant time (O(1)) at
* highest priority value is the one at the root. This value can be looked up in constant time (O(1)) at
* `heap.data[heap.data[0].index].value`
*
* The structure is designed to perform the following operations with the corresponding complexities:
*
* * peek (get the highest priority in set): O(1)
* * insert (insert a value in the set): 0(log(n))
* * pop (remove the highest priority value in set): O(log(n))
* * replace (replace the highest priority value in set with a new value): O(log(n))
* * length (get the number of elements in the set): O(1)
* * clear (remove all elements in the set): O(1)
* * peek (get the highest priority value): O(1)
* * insert (insert a value): O(log(n))
* * pop (remove the highest priority value): O(log(n))
* * replace (replace the highest priority value with a new value): O(log(n))
* * length (get the number of elements): O(1)
* * clear (remove all elements): O(1)
*/
library Heap {
using Math for *;
Expand All @@ -45,7 +45,7 @@ library Heap {
/**
* @dev Binary heap that support values of type uint256.
*
* Each element of that structures uses 2 storage slots.
* Each element of that structure uses 2 storage slots.
*/
struct Uint256Heap {
Uint256HeapNode[] data;
Expand Down Expand Up @@ -101,9 +101,9 @@ library Heap {
Uint256HeapNode storage lastNode = _unsafeNodeAccess(self, last);
uint256 rootDataValue = rootData.value;

// if root is not the last element of the data array (that will get pop-ed), reorder the data array.
// if root is not the last element of the data array (that will get popped), reorder the data array.
if (rootIdx != last) {
// get details about the value stored in the last element of the array (that will get pop-ed)
// get details about the value stored in the last element of the array (that will get popped)
uint64 lastDataIdx = lastNode.lookup;
uint256 lastDataValue = lastNode.value;
// copy these values to the location of the root (that is safe, and that we no longer use)
Expand Down Expand Up @@ -253,7 +253,7 @@ library Heap {
uint256 right = 2 * pos + 2; // this could overflow uint64

if (right < size) {
// the check guarantees that `left` and `right` are both valid uint32
// the check guarantees that `left` and `right` are both valid uint64
uint64 lIndex = uint64(left);
uint64 rIndex = uint64(right);
uint256 lValue = _unsafeNodeAccess(self, _unsafeNodeAccess(self, lIndex).index).value;
Expand All @@ -264,7 +264,7 @@ library Heap {
_siftDown(self, size, index, value, comp);
}
} else if (left < size) {
// the check guarantees that `left` is a valid uint32
// the check guarantees that `left` is a valid uint64
uint64 lIndex = uint64(left);
uint256 lValue = _unsafeNodeAccess(self, _unsafeNodeAccess(self, lIndex).index).value;
if (comp(lValue, value)) {
Expand All @@ -279,7 +279,7 @@ library Heap {
* comparator, and moving toward the root of the underlying tree.
*
* NOTE: This is a private function that is called in a trusted context with already cached parameters. `value`
* could be extracted from `self` and `pos`, but that would require redundant storage read. This parameters is not
* could be extracted from `self` and `pos`, but that would require redundant storage read. These parameters are not
* verified. It is the caller role to make sure the parameters are correct.
*/
function _siftUp(
Expand Down Expand Up @@ -312,7 +312,7 @@ library Heap {
/**
* @dev Binary heap that support values of type uint208.
*
* Each element of that structures uses 1 storage slots.
* Each element of that structure uses 1 storage slots.
*/
struct Uint208Heap {
Uint208HeapNode[] data;
Expand Down Expand Up @@ -368,9 +368,9 @@ library Heap {
Uint208HeapNode storage lastNode = _unsafeNodeAccess(self, last);
uint208 rootDataValue = rootData.value;

// if root is not the last element of the data array (that will get pop-ed), reorder the data array.
// if root is not the last element of the data array (that will get popped), reorder the data array.
if (rootIdx != last) {
// get details about the value stored in the last element of the array (that will get pop-ed)
// get details about the value stored in the last element of the array (that will get popped)
uint24 lastDataIdx = lastNode.lookup;
uint208 lastDataValue = lastNode.value;
// copy these values to the location of the root (that is safe, and that we no longer use)
Expand Down Expand Up @@ -520,7 +520,7 @@ library Heap {
uint256 right = 2 * pos + 2; // this could overflow uint24

if (right < size) {
// the check guarantees that `left` and `right` are both valid uint32
// the check guarantees that `left` and `right` are both valid uint24
uint24 lIndex = uint24(left);
uint24 rIndex = uint24(right);
uint208 lValue = _unsafeNodeAccess(self, _unsafeNodeAccess(self, lIndex).index).value;
Expand All @@ -531,7 +531,7 @@ library Heap {
_siftDown(self, size, index, value, comp);
}
} else if (left < size) {
// the check guarantees that `left` is a valid uint32
// the check guarantees that `left` is a valid uint24
uint24 lIndex = uint24(left);
uint208 lValue = _unsafeNodeAccess(self, _unsafeNodeAccess(self, lIndex).index).value;
if (comp(lValue, value)) {
Expand All @@ -546,7 +546,7 @@ library Heap {
* comparator, and moving toward the root of the underlying tree.
*
* NOTE: This is a private function that is called in a trusted context with already cached parameters. `value`
* could be extracted from `self` and `pos`, but that would require redundant storage read. This parameters is not
* could be extracted from `self` and `pos`, but that would require redundant storage read. These parameters are not
* verified. It is the caller role to make sure the parameters are correct.
*/
function _siftUp(
Expand Down
26 changes: 13 additions & 13 deletions scripts/generate/templates/Heap.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,25 +29,25 @@ import {Panic} from "../Panic.sol";
* \`\`\`
*
* The structure is ordered so that each node is bigger than its parent. An immediate consequence is that the
* highest priority value is the one at the root. This value can be lookup up in constant time (O(1)) at
* highest priority value is the one at the root. This value can be looked up in constant time (O(1)) at
* \`heap.data[heap.data[0].index].value\`
*
* The structure is designed to perform the following operations with the corresponding complexities:
*
* * peek (get the highest priority in set): O(1)
* * insert (insert a value in the set): 0(log(n))
* * pop (remove the highest priority value in set): O(log(n))
* * replace (replace the highest priority value in set with a new value): O(log(n))
* * length (get the number of elements in the set): O(1)
* * clear (remove all elements in the set): O(1)
* * peek (get the highest priority value): O(1)
* * insert (insert a value): O(log(n))
* * pop (remove the highest priority value): O(log(n))
* * replace (replace the highest priority value with a new value): O(log(n))
* * length (get the number of elements): O(1)
* * clear (remove all elements): O(1)
*/
`;

const generate = ({ struct, node, valueType, indexType, blockSize }) => `\
/**
* @dev Binary heap that support values of type ${valueType}.
*
* Each element of that structures uses ${blockSize} storage slots.
* Each element of that structure uses ${blockSize} storage slots.
*/
struct ${struct} {
${node}[] data;
Expand Down Expand Up @@ -103,9 +103,9 @@ function pop(
${node} storage lastNode = _unsafeNodeAccess(self, last);
${valueType} rootDataValue = rootData.value;
// if root is not the last element of the data array (that will get pop-ed), reorder the data array.
// if root is not the last element of the data array (that will get popped), reorder the data array.
if (rootIdx != last) {
// get details about the value stored in the last element of the array (that will get pop-ed)
// get details about the value stored in the last element of the array (that will get popped)
${indexType} lastDataIdx = lastNode.lookup;
${valueType} lastDataValue = lastNode.value;
// copy these values to the location of the root (that is safe, and that we no longer use)
Expand Down Expand Up @@ -255,7 +255,7 @@ function _siftDown(
uint256 right = 2 * pos + 2; // this could overflow ${indexType}
if (right < size) {
// the check guarantees that \`left\` and \`right\` are both valid uint32
// the check guarantees that \`left\` and \`right\` are both valid ${indexType}
${indexType} lIndex = ${indexType}(left);
${indexType} rIndex = ${indexType}(right);
${valueType} lValue = _unsafeNodeAccess(self, _unsafeNodeAccess(self, lIndex).index).value;
Expand All @@ -266,7 +266,7 @@ function _siftDown(
_siftDown(self, size, index, value, comp);
}
} else if (left < size) {
// the check guarantees that \`left\` is a valid uint32
// the check guarantees that \`left\` is a valid ${indexType}
${indexType} lIndex = ${indexType}(left);
${valueType} lValue = _unsafeNodeAccess(self, _unsafeNodeAccess(self, lIndex).index).value;
if (comp(lValue, value)) {
Expand All @@ -281,7 +281,7 @@ function _siftDown(
* comparator, and moving toward the root of the underlying tree.
*
* NOTE: This is a private function that is called in a trusted context with already cached parameters. \`value\`
* could be extracted from \`self\` and \`pos\`, but that would require redundant storage read. This parameters is not
* could be extracted from \`self\` and \`pos\`, but that would require redundant storage read. These parameters are not
* verified. It is the caller role to make sure the parameters are correct.
*/
function _siftUp(
Expand Down

0 comments on commit 659f306

Please sign in to comment.