Rename internal variables in EnumerableSet for improved readability (#4577)

Co-authored-by: Francisco Giordano <fg@frang.io>
This commit is contained in:
Hadrien Croubois
2023-09-07 04:10:19 +02:00
committed by GitHub
parent 5a77c9995f
commit 25c416d01c
2 changed files with 32 additions and 32 deletions

View File

@ -51,9 +51,9 @@ library EnumerableSet {
struct Set {
// Storage of set values
bytes32[] _values;
// Position of the value in the `values` array, plus 1 because index 0
// means a value is not in the set.
mapping(bytes32 value => uint256) _indexes;
// Position is the index of the value in the `values` array plus 1.
// Position 0 is used to mean a value is not in the set.
mapping(bytes32 value => uint256) _positions;
}
/**
@ -67,7 +67,7 @@ library EnumerableSet {
set._values.push(value);
// The value is stored at length-1, but we add 1 to all indexes
// and use 0 as a sentinel value
set._indexes[value] = set._values.length;
set._positions[value] = set._values.length;
return true;
} else {
return false;
@ -81,32 +81,32 @@ library EnumerableSet {
* present.
*/
function _remove(Set storage set, bytes32 value) private returns (bool) {
// We read and store the value's index to prevent multiple reads from the same storage slot
uint256 valueIndex = set._indexes[value];
// We cache the value's position to prevent multiple reads from the same storage slot
uint256 position = set._positions[value];
if (valueIndex != 0) {
if (position != 0) {
// 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
// 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}.
uint256 toDeleteIndex = valueIndex - 1;
uint256 valueIndex = position - 1;
uint256 lastIndex = set._values.length - 1;
if (lastIndex != toDeleteIndex) {
if (valueIndex != lastIndex) {
bytes32 lastValue = set._values[lastIndex];
// Move the last value to the index where the value to delete is
set._values[toDeleteIndex] = lastValue;
// Update the index for the moved value
set._indexes[lastValue] = valueIndex; // Replace lastValue's index to valueIndex
// Move the lastValue to the index where the value to delete is
set._values[valueIndex] = lastValue;
// Update the tracked position of the lastValue (that was just moved)
set._positions[lastValue] = position;
}
// Delete the slot where the moved value was stored
set._values.pop();
// Delete the index for the deleted slot
delete set._indexes[value];
// Delete the tracked position for the deleted slot
delete set._positions[value];
return true;
} else {
@ -118,7 +118,7 @@ library EnumerableSet {
* @dev Returns true if the value is in the set. O(1).
*/
function _contains(Set storage set, bytes32 value) private view returns (bool) {
return set._indexes[value] != 0;
return set._positions[value] != 0;
}
/**

View File

@ -61,9 +61,9 @@ const defaultSet = () => `\
struct Set {
// Storage of set values
bytes32[] _values;
// Position of the value in the \`values\` array, plus 1 because index 0
// means a value is not in the set.
mapping(bytes32 value => uint256) _indexes;
// Position is the index of the value in the \`values\` array plus 1.
// Position 0 is used to mean a value is not in the set.
mapping(bytes32 value => uint256) _positions;
}
/**
@ -77,7 +77,7 @@ function _add(Set storage set, bytes32 value) private returns (bool) {
set._values.push(value);
// The value is stored at length-1, but we add 1 to all indexes
// and use 0 as a sentinel value
set._indexes[value] = set._values.length;
set._positions[value] = set._values.length;
return true;
} else {
return false;
@ -91,32 +91,32 @@ function _add(Set storage set, bytes32 value) private returns (bool) {
* present.
*/
function _remove(Set storage set, bytes32 value) private returns (bool) {
// We read and store the value's index to prevent multiple reads from the same storage slot
uint256 valueIndex = set._indexes[value];
// We cache the value's position to prevent multiple reads from the same storage slot
uint256 position = set._positions[value];
if (valueIndex != 0) {
if (position != 0) {
// 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
// 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}.
uint256 toDeleteIndex = valueIndex - 1;
uint256 valueIndex = position - 1;
uint256 lastIndex = set._values.length - 1;
if (lastIndex != toDeleteIndex) {
if (valueIndex != lastIndex) {
bytes32 lastValue = set._values[lastIndex];
// Move the last value to the index where the value to delete is
set._values[toDeleteIndex] = lastValue;
// Update the index for the moved value
set._indexes[lastValue] = valueIndex; // Replace lastValue's index to valueIndex
// Move the lastValue to the index where the value to delete is
set._values[valueIndex] = lastValue;
// Update the tracked position of the lastValue (that was just moved)
set._positions[lastValue] = position;
}
// Delete the slot where the moved value was stored
set._values.pop();
// Delete the index for the deleted slot
delete set._indexes[value];
// Delete the tracked position for the deleted slot
delete set._positions[value];
return true;
} else {
@ -128,7 +128,7 @@ function _remove(Set storage set, bytes32 value) private returns (bool) {
* @dev Returns true if the value is in the set. O(1).
*/
function _contains(Set storage set, bytes32 value) private view returns (bool) {
return set._indexes[value] != 0;
return set._positions[value] != 0;
}
/**