Skip to content
This repository was archived by the owner on Dec 17, 2025. It is now read-only.

Commit b28de35

Browse files
committed
rename internal functions
1 parent 278934c commit b28de35

3 files changed

Lines changed: 21 additions & 34 deletions

File tree

src/LogarithmicBuckets.sol

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -39,17 +39,17 @@ library LogarithmicBuckets {
3939

4040
if (value == 0) {
4141
if (_newValue == 0) revert ZeroValue();
42-
_insert(_buckets, _id, _computeBucket(_newValue), _head);
42+
_insert(_buckets, _id, computeBucket(_newValue), _head);
4343
return;
4444
}
4545

46-
uint256 currentBucket = _computeBucket(value);
46+
uint256 currentBucket = computeBucket(value);
4747
if (_newValue == 0) {
4848
_remove(_buckets, _id, currentBucket);
4949
return;
5050
}
5151

52-
uint256 newBucket = _computeBucket(_newValue);
52+
uint256 newBucket = computeBucket(_newValue);
5353
if (newBucket != currentBucket) {
5454
_remove(_buckets, _id, currentBucket);
5555
_insert(_buckets, _id, newBucket, _head);
@@ -71,7 +71,7 @@ library LogarithmicBuckets {
7171
view
7272
returns (BucketDLL.List storage)
7373
{
74-
uint256 bucket = _computeBucket(_value);
74+
uint256 bucket = computeBucket(_value);
7575
return _buckets.lists[bucket];
7676
}
7777

@@ -82,13 +82,13 @@ library LogarithmicBuckets {
8282
function getMatch(BucketList storage _buckets, uint256 _value) internal view returns (address) {
8383
uint256 bucketsMask = _buckets.bucketsMask;
8484
if (bucketsMask == 0) return address(0);
85-
uint256 lowerMask = _setLowerBits(_value);
85+
uint256 lowerMask = setLowerBits(_value);
8686

87-
uint256 next = _nextBucket(lowerMask, bucketsMask);
87+
uint256 next = nextBucket(lowerMask, bucketsMask);
8888

8989
if (next != 0) return _buckets.lists[next].getHead();
9090

91-
uint256 prev = _prevBucket(lowerMask, bucketsMask);
91+
uint256 prev = prevBucket(lowerMask, bucketsMask);
9292

9393
return _buckets.lists[prev].getHead();
9494
}
@@ -126,14 +126,14 @@ library LogarithmicBuckets {
126126
}
127127

128128
/// @notice Returns the bucket in which the given value would fall.
129-
function _computeBucket(uint256 _value) internal pure returns (uint256) {
130-
uint256 lowerMask = _setLowerBits(_value);
129+
function computeBucket(uint256 _value) internal pure returns (uint256) {
130+
uint256 lowerMask = setLowerBits(_value);
131131
return lowerMask ^ (lowerMask >> 1);
132132
}
133133

134134
/// @notice Sets all the bits lower than (or equal to) the highest bit in the input.
135135
/// @dev This is the same as rounding the input the nearest upper value of the form `2 ** n - 1`.
136-
function _setLowerBits(uint256 x) internal pure returns (uint256 y) {
136+
function setLowerBits(uint256 x) internal pure returns (uint256 y) {
137137
assembly {
138138
x := or(x, shr(1, x))
139139
x := or(x, shr(2, x))
@@ -148,7 +148,7 @@ library LogarithmicBuckets {
148148

149149
/// @notice Returns the following bucket which contains greater values.
150150
/// @dev The bucket returned is the lowest that is in `bucketsMask` and not in `lowerMask`.
151-
function _nextBucket(uint256 lowerMask, uint256 bucketsMask)
151+
function nextBucket(uint256 lowerMask, uint256 bucketsMask)
152152
internal
153153
pure
154154
returns (uint256 bucket)
@@ -161,8 +161,8 @@ library LogarithmicBuckets {
161161

162162
/// @notice Returns the preceding bucket which contains smaller values.
163163
/// @dev The bucket returned is the highest that is in both `bucketsMask` and `lowerMask`.
164-
function _prevBucket(uint256 lowerMask, uint256 bucketsMask) internal pure returns (uint256) {
165-
uint256 lowerBucketsMask = _setLowerBits(lowerMask & bucketsMask);
164+
function prevBucket(uint256 lowerMask, uint256 bucketsMask) internal pure returns (uint256) {
165+
uint256 lowerBucketsMask = setLowerBits(lowerMask & bucketsMask);
166166
return lowerBucketsMask ^ (lowerBucketsMask >> 1);
167167
}
168168
}

test/TestLogarithmicBuckets.t.sol

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ contract TestLogarithmicBuckets is LogarithmicBucketsMock, Test {
123123
}
124124

125125
function testProveComputeBucket(uint256 _value) public {
126-
uint256 bucket = LogarithmicBuckets._computeBucket(_value);
126+
uint256 bucket = LogarithmicBuckets.computeBucket(_value);
127127
unchecked {
128128
// cross-check that bucket == 2^{floor(log_2 value)}, or 0 if value == 0
129129
assertTrue(bucket == 0 || isPowerOfTwo(bucket));
@@ -133,7 +133,7 @@ contract TestLogarithmicBuckets is LogarithmicBucketsMock, Test {
133133
}
134134

135135
function testProveNextBucket(uint256 _value) public {
136-
uint256 curr = LogarithmicBuckets._computeBucket(_value);
136+
uint256 curr = LogarithmicBuckets.computeBucket(_value);
137137
uint256 next = nextBucket(_value);
138138
uint256 bucketsMask = bucketList.bucketsMask;
139139
// check that `next` is a strictly higer non-empty bucket, or zero
@@ -150,7 +150,7 @@ contract TestLogarithmicBuckets is LogarithmicBucketsMock, Test {
150150
}
151151

152152
function testProvePrevBucket(uint256 _value) public {
153-
uint256 curr = LogarithmicBuckets._computeBucket(_value);
153+
uint256 curr = LogarithmicBuckets.computeBucket(_value);
154154
uint256 prev = prevBucket(_value);
155155
uint256 bucketsMask = bucketList.bucketsMask;
156156
// check that `prev` is a non-empty bucket that is lower than or equal to `curr`; or zero

test/mocks/LogarithmicBucketsMock.sol

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,21 +21,8 @@ contract LogarithmicBucketsMock {
2121
return bucketList.getValueOf(_id);
2222
}
2323

24-
function _setLowerBits(uint256 x) private pure returns (uint256 y) {
25-
assembly {
26-
x := or(x, shr(1, x))
27-
x := or(x, shr(2, x))
28-
x := or(x, shr(4, x))
29-
x := or(x, shr(8, x))
30-
x := or(x, shr(16, x))
31-
x := or(x, shr(32, x))
32-
x := or(x, shr(64, x))
33-
y := or(x, shr(128, x))
34-
}
35-
}
36-
3724
function maxBucket() public view returns (uint256) {
38-
uint256 lowerMask = _setLowerBits(bucketList.bucketsMask);
25+
uint256 lowerMask = LogarithmicBuckets.setLowerBits(bucketList.bucketsMask);
3926
return lowerMask ^ (lowerMask >> 1);
4027
}
4128

@@ -63,13 +50,13 @@ contract LogarithmicBucketsMock {
6350

6451
function nextBucket(uint256 _value) internal view returns (uint256) {
6552
uint256 bucketsMask = bucketList.bucketsMask;
66-
uint256 lowerMask = LogarithmicBuckets._setLowerBits(_value);
67-
return LogarithmicBuckets._nextBucket(lowerMask, bucketsMask);
53+
uint256 lowerMask = LogarithmicBuckets.setLowerBits(_value);
54+
return LogarithmicBuckets.nextBucket(lowerMask, bucketsMask);
6855
}
6956

7057
function prevBucket(uint256 _value) internal view returns (uint256) {
7158
uint256 bucketsMask = bucketList.bucketsMask;
72-
uint256 lowerMask = LogarithmicBuckets._setLowerBits(_value);
73-
return LogarithmicBuckets._prevBucket(lowerMask, bucketsMask);
59+
uint256 lowerMask = LogarithmicBuckets.setLowerBits(_value);
60+
return LogarithmicBuckets.prevBucket(lowerMask, bucketsMask);
7461
}
7562
}

0 commit comments

Comments
 (0)