|
@@ -206,6 +206,193 @@ library Checkpoints {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ 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, uint208) {
|
|
|
+ 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 optimised 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 memory ckpt = _unsafeAccess(self._checkpoints, pos - 1);
|
|
|
+ return (true, ckpt._key, ckpt._value);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @dev Returns the number of checkpoint.
|
|
|
+ */
|
|
|
+ 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, uint208) {
|
|
|
+ uint256 pos = self.length;
|
|
|
+
|
|
|
+ if (pos > 0) {
|
|
|
+ // Copying to memory is important here.
|
|
|
+ Checkpoint208 memory last = _unsafeAccess(self, pos - 1);
|
|
|
+
|
|
|
+ // Checkpoint keys must be non-decreasing.
|
|
|
+ if (last._key > key) {
|
|
|
+ revert CheckpointUnorderedInsertion();
|
|
|
+ }
|
|
|
+
|
|
|
+ // Update or push new checkpoint
|
|
|
+ if (last._key == key) {
|
|
|
+ _unsafeAccess(self, pos - 1)._value = value;
|
|
|
+ } else {
|
|
|
+ self.push(Checkpoint208({_key: key, _value: value}));
|
|
|
+ }
|
|
|
+ return (last._value, value);
|
|
|
+ } else {
|
|
|
+ self.push(Checkpoint208({_key: key, _value: value}));
|
|
|
+ return (0, value);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @dev Return the index of the last (most recent) checkpoint with key lower 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 _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 is 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;
|
|
|
}
|