Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 42 additions & 42 deletions contracts/utils/structs/EnumerableSet.sol
Original file line number Diff line number Diff line change
Expand Up @@ -519,12 +519,12 @@ library EnumerableSet {
* Returns true if the value was added to the set, that is if it was not
* already present.
*/
function add(StringSet storage self, string memory value) internal returns (bool) {
if (!contains(self, value)) {
self._values.push(value);
function add(StringSet storage set, string memory value) internal returns (bool) {
if (!contains(set, value)) {
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
self._positions[value] = self._values.length;
set._positions[value] = set._values.length;
return true;
} else {
return false;
Expand All @@ -537,33 +537,33 @@ library EnumerableSet {
* Returns true if the value was removed from the set, that is if it was
* 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
uint256 position = self._positions[value];
uint256 position = set._positions[value];

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
// 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 valueIndex = position - 1;
uint256 lastIndex = self._values.length - 1;
uint256 lastIndex = set._values.length - 1;

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
self._values[valueIndex] = lastValue;
set._values[valueIndex] = lastValue;
// 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
self._values.pop();
set._values.pop();

// Delete the tracked position for the deleted slot
delete self._positions[value];
delete set._positions[value];

return true;
} else {
Expand All @@ -588,15 +588,15 @@ library EnumerableSet {
/**
* @dev Returns true if the value is in the set. O(1).
*/
function contains(StringSet storage self, string memory value) internal view returns (bool) {
return self._positions[value] != 0;
function contains(StringSet storage set, string memory value) internal view returns (bool) {
return set._positions[value] != 0;
}

/**
* @dev Returns the number of values on the set. O(1).
*/
function length(StringSet storage self) internal view returns (uint256) {
return self._values.length;
function length(StringSet storage set) internal view returns (uint256) {
return set._values.length;
}

/**
Expand All @@ -609,8 +609,8 @@ library EnumerableSet {
*
* - `index` must be strictly less than {length}.
*/
function at(StringSet storage self, uint256 index) internal view returns (string memory) {
return self._values[index];
function at(StringSet storage set, uint256 index) internal view returns (string memory) {
return set._values[index];
}

/**
Expand All @@ -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
* 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) {
return self._values;
function values(StringSet storage set) internal view returns (string[] memory) {
return set._values;
}

/**
Expand Down Expand Up @@ -661,12 +661,12 @@ library EnumerableSet {
* Returns true if the value was added to the set, that is if it was not
* already present.
*/
function add(BytesSet storage self, bytes memory value) internal returns (bool) {
if (!contains(self, value)) {
self._values.push(value);
function add(BytesSet storage set, bytes memory value) internal returns (bool) {
if (!contains(set, value)) {
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
self._positions[value] = self._values.length;
set._positions[value] = set._values.length;
return true;
} else {
return false;
Expand All @@ -679,33 +679,33 @@ library EnumerableSet {
* Returns true if the value was removed from the set, that is if it was
* 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
uint256 position = self._positions[value];
uint256 position = set._positions[value];

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
// 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 valueIndex = position - 1;
uint256 lastIndex = self._values.length - 1;
uint256 lastIndex = set._values.length - 1;

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
self._values[valueIndex] = lastValue;
set._values[valueIndex] = lastValue;
// 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
self._values.pop();
set._values.pop();

// Delete the tracked position for the deleted slot
delete self._positions[value];
delete set._positions[value];

return true;
} else {
Expand All @@ -730,15 +730,15 @@ library EnumerableSet {
/**
* @dev Returns true if the value is in the set. O(1).
*/
function contains(BytesSet storage self, bytes memory value) internal view returns (bool) {
return self._positions[value] != 0;
function contains(BytesSet storage set, bytes memory value) internal view returns (bool) {
return set._positions[value] != 0;
}

/**
* @dev Returns the number of values on the set. O(1).
*/
function length(BytesSet storage self) internal view returns (uint256) {
return self._values.length;
function length(BytesSet storage set) internal view returns (uint256) {
return set._values.length;
}

/**
Expand All @@ -751,8 +751,8 @@ library EnumerableSet {
*
* - `index` must be strictly less than {length}.
*/
function at(BytesSet storage self, uint256 index) internal view returns (bytes memory) {
return self._values[index];
function at(BytesSet storage set, uint256 index) internal view returns (bytes memory) {
return set._values[index];
}

/**
Expand All @@ -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
* 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) {
return self._values;
function values(BytesSet storage set) internal view returns (bytes[] memory) {
return set._values;
}

/**
Expand Down
42 changes: 21 additions & 21 deletions scripts/generate/templates/EnumerableSet.js
Original file line number Diff line number Diff line change
Expand Up @@ -324,12 +324,12 @@ struct ${name} {
* Returns true if the value was added to the set, that is if it was not
* already present.
*/
function add(${name} storage self, ${value.type} memory value) internal returns (bool) {
if (!contains(self, value)) {
self._values.push(value);
function add(${name} storage set, ${value.type} memory value) internal returns (bool) {
if (!contains(set, value)) {
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
self._positions[value] = self._values.length;
set._positions[value] = set._values.length;
return true;
} else {
return false;
Expand All @@ -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
* 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
uint256 position = self._positions[value];
uint256 position = set._positions[value];

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
// 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 valueIndex = position - 1;
uint256 lastIndex = self._values.length - 1;
uint256 lastIndex = set._values.length - 1;

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
self._values[valueIndex] = lastValue;
set._values[valueIndex] = lastValue;
// 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
self._values.pop();
set._values.pop();

// Delete the tracked position for the deleted slot
delete self._positions[value];
delete set._positions[value];

return true;
} else {
Expand All @@ -393,15 +393,15 @@ function clear(${name} storage set) internal {
/**
* @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) {
return self._positions[value] != 0;
function contains(${name} storage set, ${value.type} memory value) internal view returns (bool) {
return set._positions[value] != 0;
}

/**
* @dev Returns the number of values on the set. O(1).
*/
function length(${name} storage self) internal view returns (uint256) {
return self._values.length;
function length(${name} storage set) internal view returns (uint256) {
return set._values.length;
}

/**
Expand All @@ -414,8 +414,8 @@ function length(${name} storage self) internal view returns (uint256) {
*
* - \`index\` must be strictly less than {length}.
*/
function at(${name} storage self, uint256 index) internal view returns (${value.type} memory) {
return self._values[index];
function at(${name} storage set, uint256 index) internal view returns (${value.type} memory) {
return set._values[index];
}

/**
Expand All @@ -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
* 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) {
return self._values;
function values(${name} storage set) internal view returns (${value.type}[] memory) {
return set._values;
}

/**
Expand Down