123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630 |
- // SPDX-License-Identifier: MIT
- // OpenZeppelin Contracts (last updated v5.3.0) (utils/structs/Checkpoints.sol)
- // This file was procedurally generated from scripts/generate/templates/Checkpoints.js.
- pragma solidity ^0.8.20;
- import {Math} from "../math/Math.sol";
- /**
- * @dev This library defines the `Trace*` struct, for checkpointing values as they change at different points in
- * time, and later looking up past values by block number. See {Votes} as an example.
- *
- * To create a history of checkpoints define a variable type `Checkpoints.Trace*` in your contract, and store a new
- * checkpoint for the current transaction block using the {push} function.
- */
- library Checkpoints {
- /**
- * @dev A value was attempted to be inserted on a past checkpoint.
- */
- error CheckpointUnorderedInsertion();
- struct Trace224 {
- Checkpoint224[] _checkpoints;
- }
- struct Checkpoint224 {
- uint32 _key;
- uint224 _value;
- }
- /**
- * @dev Pushes a (`key`, `value`) pair into a Trace224 so that it is stored as the checkpoint.
- *
- * Returns previous value and new value.
- *
- * IMPORTANT: Never accept `key` as a user input, since an arbitrary `type(uint32).max` key set will disable the
- * library.
- */
- function push(
- Trace224 storage self,
- uint32 key,
- uint224 value
- ) internal returns (uint224 oldValue, uint224 newValue) {
- return _insert(self._checkpoints, key, value);
- }
- /**
- * @dev Returns the value in the first (oldest) checkpoint with key greater or equal than the search key, or zero if
- * there is none.
- */
- function lowerLookup(Trace224 storage self, uint32 key) internal view returns (uint224) {
- uint256 len = self._checkpoints.length;
- uint256 pos = _lowerBinaryLookup(self._checkpoints, key, 0, len);
- return pos == len ? 0 : _unsafeAccess(self._checkpoints, pos)._value;
- }
- /**
- * @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.
- */
- function upperLookup(Trace224 storage self, uint32 key) internal view returns (uint224) {
- uint256 len = self._checkpoints.length;
- uint256 pos = _upperBinaryLookup(self._checkpoints, key, 0, len);
- return pos == 0 ? 0 : _unsafeAccess(self._checkpoints, pos - 1)._value;
- }
- /**
- * @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.
- *
- * NOTE: This is a variant of {upperLookup} that is optimized to find "recent" checkpoint (checkpoints with high
- * keys).
- */
- function upperLookupRecent(Trace224 storage self, uint32 key) internal view returns (uint224) {
- uint256 len = self._checkpoints.length;
- uint256 low = 0;
- uint256 high = len;
- if (len > 5) {
- uint256 mid = len - Math.sqrt(len);
- if (key < _unsafeAccess(self._checkpoints, mid)._key) {
- high = mid;
- } else {
- low = mid + 1;
- }
- }
- uint256 pos = _upperBinaryLookup(self._checkpoints, key, low, high);
- return pos == 0 ? 0 : _unsafeAccess(self._checkpoints, pos - 1)._value;
- }
- /**
- * @dev Returns the value in the most recent checkpoint, or zero if there are no checkpoints.
- */
- function latest(Trace224 storage self) internal view returns (uint224) {
- uint256 pos = self._checkpoints.length;
- return pos == 0 ? 0 : _unsafeAccess(self._checkpoints, pos - 1)._value;
- }
- /**
- * @dev Returns whether there is a checkpoint in the structure (i.e. it is not empty), and if so the key and value
- * in the most recent checkpoint.
- */
- function latestCheckpoint(Trace224 storage self) internal view returns (bool exists, uint32 _key, uint224 _value) {
- uint256 pos = self._checkpoints.length;
- if (pos == 0) {
- return (false, 0, 0);
- } else {
- Checkpoint224 storage ckpt = _unsafeAccess(self._checkpoints, pos - 1);
- return (true, ckpt._key, ckpt._value);
- }
- }
- /**
- * @dev Returns the number of checkpoints.
- */
- function length(Trace224 storage self) internal view returns (uint256) {
- return self._checkpoints.length;
- }
- /**
- * @dev Returns checkpoint at given position.
- */
- function at(Trace224 storage self, uint32 pos) internal view returns (Checkpoint224 memory) {
- return self._checkpoints[pos];
- }
- /**
- * @dev Pushes a (`key`, `value`) pair into an ordered list of checkpoints, either by inserting a new checkpoint,
- * or by updating the last one.
- */
- function _insert(
- Checkpoint224[] storage self,
- uint32 key,
- uint224 value
- ) private returns (uint224 oldValue, uint224 newValue) {
- uint256 pos = self.length;
- if (pos > 0) {
- Checkpoint224 storage last = _unsafeAccess(self, pos - 1);
- uint32 lastKey = last._key;
- uint224 lastValue = last._value;
- // Checkpoint keys must be non-decreasing.
- if (lastKey > key) {
- revert CheckpointUnorderedInsertion();
- }
- // Update or push new checkpoint
- if (lastKey == key) {
- last._value = value;
- } else {
- self.push(Checkpoint224({_key: key, _value: value}));
- }
- return (lastValue, value);
- } else {
- self.push(Checkpoint224({_key: key, _value: value}));
- return (0, 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(
- Checkpoint224[] storage self,
- uint32 key,
- uint256 low,
- uint256 high
- ) private view returns (uint256) {
- while (low < high) {
- uint256 mid = Math.average(low, high);
- if (_unsafeAccess(self, mid)._key > key) {
- high = mid;
- } else {
- low = mid + 1;
- }
- }
- return high;
- }
- /**
- * @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(
- Checkpoint224[] storage self,
- uint32 key,
- uint256 low,
- uint256 high
- ) private view returns (uint256) {
- while (low < high) {
- uint256 mid = Math.average(low, high);
- if (_unsafeAccess(self, mid)._key < key) {
- low = mid + 1;
- } else {
- high = mid;
- }
- }
- return high;
- }
- /**
- * @dev Access an element of the array without performing bounds check. The position is assumed to be within bounds.
- */
- function _unsafeAccess(
- Checkpoint224[] storage self,
- uint256 pos
- ) private pure returns (Checkpoint224 storage result) {
- assembly {
- mstore(0, self.slot)
- result.slot := add(keccak256(0, 0x20), pos)
- }
- }
- struct Trace208 {
- Checkpoint208[] _checkpoints;
- }
- struct Checkpoint208 {
- uint48 _key;
- uint208 _value;
- }
- /**
- * @dev Pushes a (`key`, `value`) pair into a Trace208 so that it is stored as the checkpoint.
- *
- * Returns previous value and new value.
- *
- * IMPORTANT: Never accept `key` as a user input, since an arbitrary `type(uint48).max` key set will disable the
- * library.
- */
- function push(
- Trace208 storage self,
- uint48 key,
- uint208 value
- ) internal returns (uint208 oldValue, uint208 newValue) {
- return _insert(self._checkpoints, key, value);
- }
- /**
- * @dev Returns the value in the first (oldest) checkpoint with key greater or equal than the search key, or zero if
- * there is none.
- */
- function lowerLookup(Trace208 storage self, uint48 key) internal view returns (uint208) {
- uint256 len = self._checkpoints.length;
- uint256 pos = _lowerBinaryLookup(self._checkpoints, key, 0, len);
- return pos == len ? 0 : _unsafeAccess(self._checkpoints, pos)._value;
- }
- /**
- * @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.
- */
- function upperLookup(Trace208 storage self, uint48 key) internal view returns (uint208) {
- uint256 len = self._checkpoints.length;
- uint256 pos = _upperBinaryLookup(self._checkpoints, key, 0, len);
- return pos == 0 ? 0 : _unsafeAccess(self._checkpoints, pos - 1)._value;
- }
- /**
- * @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.
- *
- * NOTE: This is a variant of {upperLookup} that is optimized to find "recent" checkpoint (checkpoints with high
- * keys).
- */
- function upperLookupRecent(Trace208 storage self, uint48 key) internal view returns (uint208) {
- uint256 len = self._checkpoints.length;
- uint256 low = 0;
- uint256 high = len;
- if (len > 5) {
- uint256 mid = len - Math.sqrt(len);
- if (key < _unsafeAccess(self._checkpoints, mid)._key) {
- high = mid;
- } else {
- low = mid + 1;
- }
- }
- uint256 pos = _upperBinaryLookup(self._checkpoints, key, low, high);
- return pos == 0 ? 0 : _unsafeAccess(self._checkpoints, pos - 1)._value;
- }
- /**
- * @dev Returns the value in the most recent checkpoint, or zero if there are no checkpoints.
- */
- function latest(Trace208 storage self) internal view returns (uint208) {
- uint256 pos = self._checkpoints.length;
- return pos == 0 ? 0 : _unsafeAccess(self._checkpoints, pos - 1)._value;
- }
- /**
- * @dev Returns whether there is a checkpoint in the structure (i.e. it is not empty), and if so the key and value
- * in the most recent checkpoint.
- */
- function latestCheckpoint(Trace208 storage self) internal view returns (bool exists, uint48 _key, uint208 _value) {
- uint256 pos = self._checkpoints.length;
- if (pos == 0) {
- return (false, 0, 0);
- } else {
- Checkpoint208 storage ckpt = _unsafeAccess(self._checkpoints, pos - 1);
- return (true, ckpt._key, ckpt._value);
- }
- }
- /**
- * @dev Returns the number of checkpoints.
- */
- function length(Trace208 storage self) internal view returns (uint256) {
- return self._checkpoints.length;
- }
- /**
- * @dev Returns checkpoint at given position.
- */
- function at(Trace208 storage self, uint32 pos) internal view returns (Checkpoint208 memory) {
- return self._checkpoints[pos];
- }
- /**
- * @dev Pushes a (`key`, `value`) pair into an ordered list of checkpoints, either by inserting a new checkpoint,
- * or by updating the last one.
- */
- function _insert(
- Checkpoint208[] storage self,
- uint48 key,
- uint208 value
- ) private returns (uint208 oldValue, uint208 newValue) {
- uint256 pos = self.length;
- if (pos > 0) {
- Checkpoint208 storage last = _unsafeAccess(self, pos - 1);
- uint48 lastKey = last._key;
- uint208 lastValue = last._value;
- // Checkpoint keys must be non-decreasing.
- if (lastKey > key) {
- revert CheckpointUnorderedInsertion();
- }
- // Update or push new checkpoint
- if (lastKey == key) {
- last._value = value;
- } else {
- self.push(Checkpoint208({_key: key, _value: value}));
- }
- return (lastValue, value);
- } else {
- self.push(Checkpoint208({_key: key, _value: value}));
- return (0, 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(
- Checkpoint208[] storage self,
- uint48 key,
- uint256 low,
- uint256 high
- ) private view returns (uint256) {
- while (low < high) {
- uint256 mid = Math.average(low, high);
- if (_unsafeAccess(self, mid)._key > key) {
- high = mid;
- } else {
- low = mid + 1;
- }
- }
- return high;
- }
- /**
- * @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(
- Checkpoint208[] storage self,
- uint48 key,
- uint256 low,
- uint256 high
- ) private view returns (uint256) {
- while (low < high) {
- uint256 mid = Math.average(low, high);
- if (_unsafeAccess(self, mid)._key < key) {
- low = mid + 1;
- } else {
- high = mid;
- }
- }
- return high;
- }
- /**
- * @dev Access an element of the array without performing bounds check. The position is assumed to be within bounds.
- */
- function _unsafeAccess(
- Checkpoint208[] storage self,
- uint256 pos
- ) private pure returns (Checkpoint208 storage result) {
- assembly {
- mstore(0, self.slot)
- result.slot := add(keccak256(0, 0x20), pos)
- }
- }
- struct Trace160 {
- Checkpoint160[] _checkpoints;
- }
- struct Checkpoint160 {
- uint96 _key;
- uint160 _value;
- }
- /**
- * @dev Pushes a (`key`, `value`) pair into a Trace160 so that it is stored as the checkpoint.
- *
- * Returns previous value and new value.
- *
- * IMPORTANT: Never accept `key` as a user input, since an arbitrary `type(uint96).max` key set will disable the
- * library.
- */
- function push(
- Trace160 storage self,
- uint96 key,
- uint160 value
- ) internal returns (uint160 oldValue, uint160 newValue) {
- return _insert(self._checkpoints, key, value);
- }
- /**
- * @dev Returns the value in the first (oldest) checkpoint with key greater or equal than the search key, or zero if
- * there is none.
- */
- function lowerLookup(Trace160 storage self, uint96 key) internal view returns (uint160) {
- uint256 len = self._checkpoints.length;
- uint256 pos = _lowerBinaryLookup(self._checkpoints, key, 0, len);
- return pos == len ? 0 : _unsafeAccess(self._checkpoints, pos)._value;
- }
- /**
- * @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.
- */
- function upperLookup(Trace160 storage self, uint96 key) internal view returns (uint160) {
- uint256 len = self._checkpoints.length;
- uint256 pos = _upperBinaryLookup(self._checkpoints, key, 0, len);
- return pos == 0 ? 0 : _unsafeAccess(self._checkpoints, pos - 1)._value;
- }
- /**
- * @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.
- *
- * NOTE: This is a variant of {upperLookup} that is optimized to find "recent" checkpoint (checkpoints with high
- * keys).
- */
- function upperLookupRecent(Trace160 storage self, uint96 key) internal view returns (uint160) {
- uint256 len = self._checkpoints.length;
- uint256 low = 0;
- uint256 high = len;
- if (len > 5) {
- uint256 mid = len - Math.sqrt(len);
- if (key < _unsafeAccess(self._checkpoints, mid)._key) {
- high = mid;
- } else {
- low = mid + 1;
- }
- }
- uint256 pos = _upperBinaryLookup(self._checkpoints, key, low, high);
- return pos == 0 ? 0 : _unsafeAccess(self._checkpoints, pos - 1)._value;
- }
- /**
- * @dev Returns the value in the most recent checkpoint, or zero if there are no checkpoints.
- */
- function latest(Trace160 storage self) internal view returns (uint160) {
- uint256 pos = self._checkpoints.length;
- return pos == 0 ? 0 : _unsafeAccess(self._checkpoints, pos - 1)._value;
- }
- /**
- * @dev Returns whether there is a checkpoint in the structure (i.e. it is not empty), and if so the key and value
- * in the most recent checkpoint.
- */
- function latestCheckpoint(Trace160 storage self) internal view returns (bool exists, uint96 _key, uint160 _value) {
- uint256 pos = self._checkpoints.length;
- if (pos == 0) {
- return (false, 0, 0);
- } else {
- Checkpoint160 storage ckpt = _unsafeAccess(self._checkpoints, pos - 1);
- return (true, ckpt._key, ckpt._value);
- }
- }
- /**
- * @dev Returns the number of checkpoints.
- */
- function length(Trace160 storage self) internal view returns (uint256) {
- return self._checkpoints.length;
- }
- /**
- * @dev Returns checkpoint at given position.
- */
- function at(Trace160 storage self, uint32 pos) internal view returns (Checkpoint160 memory) {
- return self._checkpoints[pos];
- }
- /**
- * @dev Pushes a (`key`, `value`) pair into an ordered list of checkpoints, either by inserting a new checkpoint,
- * or by updating the last one.
- */
- function _insert(
- Checkpoint160[] storage self,
- uint96 key,
- uint160 value
- ) private returns (uint160 oldValue, uint160 newValue) {
- uint256 pos = self.length;
- if (pos > 0) {
- Checkpoint160 storage last = _unsafeAccess(self, pos - 1);
- uint96 lastKey = last._key;
- uint160 lastValue = last._value;
- // Checkpoint keys must be non-decreasing.
- if (lastKey > key) {
- revert CheckpointUnorderedInsertion();
- }
- // Update or push new checkpoint
- if (lastKey == key) {
- last._value = value;
- } else {
- self.push(Checkpoint160({_key: key, _value: value}));
- }
- return (lastValue, value);
- } else {
- self.push(Checkpoint160({_key: key, _value: value}));
- return (0, 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(
- Checkpoint160[] storage self,
- uint96 key,
- uint256 low,
- uint256 high
- ) private view returns (uint256) {
- while (low < high) {
- uint256 mid = Math.average(low, high);
- if (_unsafeAccess(self, mid)._key > key) {
- high = mid;
- } else {
- low = mid + 1;
- }
- }
- return high;
- }
- /**
- * @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(
- Checkpoint160[] storage self,
- uint96 key,
- uint256 low,
- uint256 high
- ) private view returns (uint256) {
- while (low < high) {
- uint256 mid = Math.average(low, high);
- if (_unsafeAccess(self, mid)._key < key) {
- low = mid + 1;
- } else {
- high = mid;
- }
- }
- return high;
- }
- /**
- * @dev Access an element of the array without performing bounds check. The position is assumed to be within bounds.
- */
- function _unsafeAccess(
- Checkpoint160[] storage self,
- uint256 pos
- ) private pure returns (Checkpoint160 storage result) {
- assembly {
- mstore(0, self.slot)
- result.slot := add(keccak256(0, 0x20), pos)
- }
- }
- }
|