Improve naming consystency in EnumerableSet (#5776)

This commit is contained in:
Hadrien Croubois
2025-07-03 21:29:43 +02:00
committed by GitHub
parent 292b3542fc
commit 5936cd8025
2 changed files with 63 additions and 63 deletions

View File

@ -519,12 +519,12 @@ library EnumerableSet {
* Returns true if the value was added to the set, that is if it was not * Returns true if the value was added to the set, that is if it was not
* already present. * already present.
*/ */
function add(StringSet storage self, string memory value) internal returns (bool) { function add(StringSet storage set, string memory value) internal returns (bool) {
if (!contains(self, value)) { if (!contains(set, value)) {
self._values.push(value); set._values.push(value);
// The value is stored at length-1, but we add 1 to all indexes // The value is stored at length-1, but we add 1 to all indexes
// and use 0 as a sentinel value // and use 0 as a sentinel value
self._positions[value] = self._values.length; set._positions[value] = set._values.length;
return true; return true;
} else { } else {
return false; return false;
@ -537,33 +537,33 @@ library EnumerableSet {
* Returns true if the value was removed from the set, that is if it was * Returns true if the value was removed from the set, that is if it was
* present. * present.
*/ */
function remove(StringSet storage self, string memory value) internal returns (bool) { function remove(StringSet storage set, string memory value) internal returns (bool) {
// We cache the value's position to prevent multiple reads from the same storage slot // We cache the value's position to prevent multiple reads from the same storage slot
uint256 position = self._positions[value]; uint256 position = set._positions[value];
if (position != 0) { if (position != 0) {
// Equivalent to contains(self, value) // Equivalent to contains(set, value)
// To delete an element from the _values array in O(1), we swap the element to delete with the last one in // To delete an element from the _values array in O(1), we swap the element to delete with the last one in
// the array, and then remove the last element (sometimes called as 'swap and pop'). // the array, and then remove the last element (sometimes called as 'swap and pop').
// This modifies the order of the array, as noted in {at}. // This modifies the order of the array, as noted in {at}.
uint256 valueIndex = position - 1; uint256 valueIndex = position - 1;
uint256 lastIndex = self._values.length - 1; uint256 lastIndex = set._values.length - 1;
if (valueIndex != lastIndex) { if (valueIndex != lastIndex) {
string memory lastValue = self._values[lastIndex]; string memory lastValue = set._values[lastIndex];
// Move the lastValue to the index where the value to delete is // Move the lastValue to the index where the value to delete is
self._values[valueIndex] = lastValue; set._values[valueIndex] = lastValue;
// Update the tracked position of the lastValue (that was just moved) // Update the tracked position of the lastValue (that was just moved)
self._positions[lastValue] = position; set._positions[lastValue] = position;
} }
// Delete the slot where the moved value was stored // Delete the slot where the moved value was stored
self._values.pop(); set._values.pop();
// Delete the tracked position for the deleted slot // Delete the tracked position for the deleted slot
delete self._positions[value]; delete set._positions[value];
return true; return true;
} else { } else {
@ -588,15 +588,15 @@ library EnumerableSet {
/** /**
* @dev Returns true if the value is in the set. O(1). * @dev Returns true if the value is in the set. O(1).
*/ */
function contains(StringSet storage self, string memory value) internal view returns (bool) { function contains(StringSet storage set, string memory value) internal view returns (bool) {
return self._positions[value] != 0; return set._positions[value] != 0;
} }
/** /**
* @dev Returns the number of values on the set. O(1). * @dev Returns the number of values on the set. O(1).
*/ */
function length(StringSet storage self) internal view returns (uint256) { function length(StringSet storage set) internal view returns (uint256) {
return self._values.length; return set._values.length;
} }
/** /**
@ -609,8 +609,8 @@ library EnumerableSet {
* *
* - `index` must be strictly less than {length}. * - `index` must be strictly less than {length}.
*/ */
function at(StringSet storage self, uint256 index) internal view returns (string memory) { function at(StringSet storage set, uint256 index) internal view returns (string memory) {
return self._values[index]; return set._values[index];
} }
/** /**
@ -621,8 +621,8 @@ library EnumerableSet {
* this function has an unbounded cost, and using it as part of a state-changing function may render the function * this function has an unbounded cost, and using it as part of a state-changing function may render the function
* uncallable if the set grows to a point where copying to memory consumes too much gas to fit in a block. * uncallable if the set grows to a point where copying to memory consumes too much gas to fit in a block.
*/ */
function values(StringSet storage self) internal view returns (string[] memory) { function values(StringSet storage set) internal view returns (string[] memory) {
return self._values; return set._values;
} }
/** /**
@ -661,12 +661,12 @@ library EnumerableSet {
* Returns true if the value was added to the set, that is if it was not * Returns true if the value was added to the set, that is if it was not
* already present. * already present.
*/ */
function add(BytesSet storage self, bytes memory value) internal returns (bool) { function add(BytesSet storage set, bytes memory value) internal returns (bool) {
if (!contains(self, value)) { if (!contains(set, value)) {
self._values.push(value); set._values.push(value);
// The value is stored at length-1, but we add 1 to all indexes // The value is stored at length-1, but we add 1 to all indexes
// and use 0 as a sentinel value // and use 0 as a sentinel value
self._positions[value] = self._values.length; set._positions[value] = set._values.length;
return true; return true;
} else { } else {
return false; return false;
@ -679,33 +679,33 @@ library EnumerableSet {
* Returns true if the value was removed from the set, that is if it was * Returns true if the value was removed from the set, that is if it was
* present. * present.
*/ */
function remove(BytesSet storage self, bytes memory value) internal returns (bool) { function remove(BytesSet storage set, bytes memory value) internal returns (bool) {
// We cache the value's position to prevent multiple reads from the same storage slot // We cache the value's position to prevent multiple reads from the same storage slot
uint256 position = self._positions[value]; uint256 position = set._positions[value];
if (position != 0) { if (position != 0) {
// Equivalent to contains(self, value) // Equivalent to contains(set, value)
// To delete an element from the _values array in O(1), we swap the element to delete with the last one in // To delete an element from the _values array in O(1), we swap the element to delete with the last one in
// the array, and then remove the last element (sometimes called as 'swap and pop'). // the array, and then remove the last element (sometimes called as 'swap and pop').
// This modifies the order of the array, as noted in {at}. // This modifies the order of the array, as noted in {at}.
uint256 valueIndex = position - 1; uint256 valueIndex = position - 1;
uint256 lastIndex = self._values.length - 1; uint256 lastIndex = set._values.length - 1;
if (valueIndex != lastIndex) { if (valueIndex != lastIndex) {
bytes memory lastValue = self._values[lastIndex]; bytes memory lastValue = set._values[lastIndex];
// Move the lastValue to the index where the value to delete is // Move the lastValue to the index where the value to delete is
self._values[valueIndex] = lastValue; set._values[valueIndex] = lastValue;
// Update the tracked position of the lastValue (that was just moved) // Update the tracked position of the lastValue (that was just moved)
self._positions[lastValue] = position; set._positions[lastValue] = position;
} }
// Delete the slot where the moved value was stored // Delete the slot where the moved value was stored
self._values.pop(); set._values.pop();
// Delete the tracked position for the deleted slot // Delete the tracked position for the deleted slot
delete self._positions[value]; delete set._positions[value];
return true; return true;
} else { } else {
@ -730,15 +730,15 @@ library EnumerableSet {
/** /**
* @dev Returns true if the value is in the set. O(1). * @dev Returns true if the value is in the set. O(1).
*/ */
function contains(BytesSet storage self, bytes memory value) internal view returns (bool) { function contains(BytesSet storage set, bytes memory value) internal view returns (bool) {
return self._positions[value] != 0; return set._positions[value] != 0;
} }
/** /**
* @dev Returns the number of values on the set. O(1). * @dev Returns the number of values on the set. O(1).
*/ */
function length(BytesSet storage self) internal view returns (uint256) { function length(BytesSet storage set) internal view returns (uint256) {
return self._values.length; return set._values.length;
} }
/** /**
@ -751,8 +751,8 @@ library EnumerableSet {
* *
* - `index` must be strictly less than {length}. * - `index` must be strictly less than {length}.
*/ */
function at(BytesSet storage self, uint256 index) internal view returns (bytes memory) { function at(BytesSet storage set, uint256 index) internal view returns (bytes memory) {
return self._values[index]; return set._values[index];
} }
/** /**
@ -763,8 +763,8 @@ library EnumerableSet {
* this function has an unbounded cost, and using it as part of a state-changing function may render the function * this function has an unbounded cost, and using it as part of a state-changing function may render the function
* uncallable if the set grows to a point where copying to memory consumes too much gas to fit in a block. * uncallable if the set grows to a point where copying to memory consumes too much gas to fit in a block.
*/ */
function values(BytesSet storage self) internal view returns (bytes[] memory) { function values(BytesSet storage set) internal view returns (bytes[] memory) {
return self._values; return set._values;
} }
/** /**

View File

@ -324,12 +324,12 @@ struct ${name} {
* Returns true if the value was added to the set, that is if it was not * Returns true if the value was added to the set, that is if it was not
* already present. * already present.
*/ */
function add(${name} storage self, ${value.type} memory value) internal returns (bool) { function add(${name} storage set, ${value.type} memory value) internal returns (bool) {
if (!contains(self, value)) { if (!contains(set, value)) {
self._values.push(value); set._values.push(value);
// The value is stored at length-1, but we add 1 to all indexes // The value is stored at length-1, but we add 1 to all indexes
// and use 0 as a sentinel value // and use 0 as a sentinel value
self._positions[value] = self._values.length; set._positions[value] = set._values.length;
return true; return true;
} else { } else {
return false; return false;
@ -342,33 +342,33 @@ function add(${name} storage self, ${value.type} memory value) internal returns
* Returns true if the value was removed from the set, that is if it was * Returns true if the value was removed from the set, that is if it was
* present. * present.
*/ */
function remove(${name} storage self, ${value.type} memory value) internal returns (bool) { function remove(${name} storage set, ${value.type} memory value) internal returns (bool) {
// We cache the value's position to prevent multiple reads from the same storage slot // We cache the value's position to prevent multiple reads from the same storage slot
uint256 position = self._positions[value]; uint256 position = set._positions[value];
if (position != 0) { if (position != 0) {
// Equivalent to contains(self, value) // Equivalent to contains(set, value)
// To delete an element from the _values array in O(1), we swap the element to delete with the last one in // To delete an element from the _values array in O(1), we swap the element to delete with the last one in
// the array, and then remove the last element (sometimes called as 'swap and pop'). // the array, and then remove the last element (sometimes called as 'swap and pop').
// This modifies the order of the array, as noted in {at}. // This modifies the order of the array, as noted in {at}.
uint256 valueIndex = position - 1; uint256 valueIndex = position - 1;
uint256 lastIndex = self._values.length - 1; uint256 lastIndex = set._values.length - 1;
if (valueIndex != lastIndex) { if (valueIndex != lastIndex) {
${value.type} memory lastValue = self._values[lastIndex]; ${value.type} memory lastValue = set._values[lastIndex];
// Move the lastValue to the index where the value to delete is // Move the lastValue to the index where the value to delete is
self._values[valueIndex] = lastValue; set._values[valueIndex] = lastValue;
// Update the tracked position of the lastValue (that was just moved) // Update the tracked position of the lastValue (that was just moved)
self._positions[lastValue] = position; set._positions[lastValue] = position;
} }
// Delete the slot where the moved value was stored // Delete the slot where the moved value was stored
self._values.pop(); set._values.pop();
// Delete the tracked position for the deleted slot // Delete the tracked position for the deleted slot
delete self._positions[value]; delete set._positions[value];
return true; return true;
} else { } else {
@ -393,15 +393,15 @@ function clear(${name} storage set) internal {
/** /**
* @dev Returns true if the value is in the set. O(1). * @dev Returns true if the value is in the set. O(1).
*/ */
function contains(${name} storage self, ${value.type} memory value) internal view returns (bool) { function contains(${name} storage set, ${value.type} memory value) internal view returns (bool) {
return self._positions[value] != 0; return set._positions[value] != 0;
} }
/** /**
* @dev Returns the number of values on the set. O(1). * @dev Returns the number of values on the set. O(1).
*/ */
function length(${name} storage self) internal view returns (uint256) { function length(${name} storage set) internal view returns (uint256) {
return self._values.length; return set._values.length;
} }
/** /**
@ -414,8 +414,8 @@ function length(${name} storage self) internal view returns (uint256) {
* *
* - \`index\` must be strictly less than {length}. * - \`index\` must be strictly less than {length}.
*/ */
function at(${name} storage self, uint256 index) internal view returns (${value.type} memory) { function at(${name} storage set, uint256 index) internal view returns (${value.type} memory) {
return self._values[index]; return set._values[index];
} }
/** /**
@ -426,8 +426,8 @@ function at(${name} storage self, uint256 index) internal view returns (${value.
* this function has an unbounded cost, and using it as part of a state-changing function may render the function * this function has an unbounded cost, and using it as part of a state-changing function may render the function
* uncallable if the set grows to a point where copying to memory consumes too much gas to fit in a block. * uncallable if the set grows to a point where copying to memory consumes too much gas to fit in a block.
*/ */
function values(${name} storage self) internal view returns (${value.type}[] memory) { function values(${name} storage set) internal view returns (${value.type}[] memory) {
return self._values; return set._values;
} }
/** /**