Skip to content

Expose binary lookup functions in Checkpoint #5609

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/three-banks-sing.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'openzeppelin-solidity': patch
---

Rollback previous commit & added wrapper functions instead
5 changes: 5 additions & 0 deletions .changeset/tidy-birds-happen.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'openzeppelin-solidity': minor
---

Expose binary lookup functions in Checkpoint
96 changes: 96 additions & 0 deletions contracts/utils/structs/Checkpoints.sol
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,22 @@ library Checkpoints {
return pos == len ? 0 : _unsafeAccess(self._checkpoints, pos)._value;
}

/**
* @dev Return the index of the first (oldest) checkpoint with key greater or equal than the search key, or `high`
* if there is none. `low` and `high` define a section where to do the search, with inclusive `low` and exclusive
* `high`.
*
* WARNING: `high` should not be greater than the array's length.
*/
function lowerBinaryLookup(
Trace224 storage self,
uint32 key,
uint256 low,
uint256 high
) internal view returns (uint256) {
return _lowerBinaryLookup(self._checkpoints, key, low, high);
}

/**
* @dev Returns the value in the last (most recent) checkpoint with key lower or equal than the search key, or zero
* if there is none.
Expand Down Expand Up @@ -91,6 +107,22 @@ library Checkpoints {
return pos == 0 ? 0 : _unsafeAccess(self._checkpoints, pos - 1)._value;
}

/**
* @dev Return the index of the first (oldest) checkpoint with key strictly bigger than the search key, or `high`
* if there is none. `low` and `high` define a section where to do the search, with inclusive `low` and exclusive
* `high`.
*
* WARNING: `high` should not be greater than the array's length.
*/
function upperBinaryLookup(
Trace224 storage self,
uint32 key,
uint256 low,
uint256 high
) internal view returns (uint256) {
return _upperBinaryLookup(self._checkpoints, key, low, high);
}

/**
* @dev Returns the value in the most recent checkpoint, or zero if there are no checkpoints.
*/
Expand Down Expand Up @@ -257,6 +289,22 @@ library Checkpoints {
return pos == len ? 0 : _unsafeAccess(self._checkpoints, pos)._value;
}

/**
* @dev Return the index of the first (oldest) checkpoint with key greater or equal than the search key, or `high`
* if there is none. `low` and `high` define a section where to do the search, with inclusive `low` and exclusive
* `high`.
*
* WARNING: `high` should not be greater than the array's length.
*/
function lowerBinaryLookup(
Trace208 storage self,
uint48 key,
uint256 low,
uint256 high
) internal view returns (uint256) {
return _lowerBinaryLookup(self._checkpoints, key, low, high);
}

/**
* @dev Returns the value in the last (most recent) checkpoint with key lower or equal than the search key, or zero
* if there is none.
Expand Down Expand Up @@ -294,6 +342,22 @@ library Checkpoints {
return pos == 0 ? 0 : _unsafeAccess(self._checkpoints, pos - 1)._value;
}

/**
* @dev Return the index of the first (oldest) checkpoint with key strictly bigger than the search key, or `high`
* if there is none. `low` and `high` define a section where to do the search, with inclusive `low` and exclusive
* `high`.
*
* WARNING: `high` should not be greater than the array's length.
*/
function upperBinaryLookup(
Trace208 storage self,
uint48 key,
uint256 low,
uint256 high
) internal view returns (uint256) {
return _upperBinaryLookup(self._checkpoints, key, low, high);
}

/**
* @dev Returns the value in the most recent checkpoint, or zero if there are no checkpoints.
*/
Expand Down Expand Up @@ -460,6 +524,22 @@ library Checkpoints {
return pos == len ? 0 : _unsafeAccess(self._checkpoints, pos)._value;
}

/**
* @dev Return the index of the first (oldest) checkpoint with key greater or equal than the search key, or `high`
* if there is none. `low` and `high` define a section where to do the search, with inclusive `low` and exclusive
* `high`.
*
* WARNING: `high` should not be greater than the array's length.
*/
function lowerBinaryLookup(
Trace160 storage self,
uint96 key,
uint256 low,
uint256 high
) internal view returns (uint256) {
return _lowerBinaryLookup(self._checkpoints, key, low, high);
}

/**
* @dev Returns the value in the last (most recent) checkpoint with key lower or equal than the search key, or zero
* if there is none.
Expand Down Expand Up @@ -497,6 +577,22 @@ library Checkpoints {
return pos == 0 ? 0 : _unsafeAccess(self._checkpoints, pos - 1)._value;
}

/**
* @dev Return the index of the first (oldest) checkpoint with key strictly bigger than the search key, or `high`
* if there is none. `low` and `high` define a section where to do the search, with inclusive `low` and exclusive
* `high`.
*
* WARNING: `high` should not be greater than the array's length.
*/
function upperBinaryLookup(
Trace160 storage self,
uint96 key,
uint256 low,
uint256 high
) internal view returns (uint256) {
return _upperBinaryLookup(self._checkpoints, key, low, high);
}

/**
* @dev Returns the value in the most recent checkpoint, or zero if there are no checkpoints.
*/
Expand Down
32 changes: 32 additions & 0 deletions scripts/generate/templates/Checkpoints.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,22 @@ function lowerLookup(${opts.historyTypeName} storage self, ${opts.keyTypeName} k
return pos == len ? 0 : _unsafeAccess(self.${opts.checkpointFieldName}, pos).${opts.valueFieldName};
}

/**
* @dev Return the index of the first (oldest) checkpoint with key greater or equal than the search key, or \`high\`
* if there is none. \`low\` and \`high\` define a section where to do the search, with inclusive \`low\` and exclusive
* \`high\`.
*
* WARNING: \`high\` should not be greater than the array's length.
*/
function lowerBinaryLookup(
${opts.historyTypeName} storage self,
${opts.keyTypeName} key,
uint256 low,
uint256 high
) internal view returns (uint256) {
return _lowerBinaryLookup(self.${opts.checkpointFieldName}, key, low, high);
}

/**
* @dev Returns the value in the last (most recent) checkpoint with key lower or equal than the search key, or zero
* if there is none.
Expand Down Expand Up @@ -96,6 +112,22 @@ function upperLookupRecent(${opts.historyTypeName} storage self, ${opts.keyTypeN
return pos == 0 ? 0 : _unsafeAccess(self.${opts.checkpointFieldName}, pos - 1).${opts.valueFieldName};
}

/**
* @dev Return the index of the first (oldest) checkpoint with key strictly bigger than the search key, or \`high\`
* if there is none. \`low\` and \`high\` define a section where to do the search, with inclusive \`low\` and exclusive
* \`high\`.
*
* WARNING: \`high\` should not be greater than the array's length.
*/
function upperBinaryLookup(
${opts.historyTypeName} storage self,
${opts.keyTypeName} key,
uint256 low,
uint256 high
) internal view returns (uint256) {
return _upperBinaryLookup(self.${opts.checkpointFieldName}, key, low, high);
}

/**
* @dev Returns the value in the most recent checkpoint, or zero if there are no checkpoints.
*/
Expand Down
Loading