Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Rename keys to values
  • Loading branch information
nventuro committed Mar 27, 2020
commit 1ef8ba700cf50a38197dd62e9b898b872ac14dca
2 changes: 1 addition & 1 deletion contracts/mocks/EnumerableSetMock.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ pragma solidity ^0.6.0;

import "../utils/EnumerableSet.sol";

contract EnumerableSetMock{
contract EnumerableSetMock {
using EnumerableSet for EnumerableSet.AddressSet;

event TransactionResult(bool result);
Expand Down
82 changes: 41 additions & 41 deletions contracts/utils/EnumerableSet.sol
Original file line number Diff line number Diff line change
Expand Up @@ -20,60 +20,60 @@ pragma solidity ^0.6.0;
library EnumerableSet {

struct AddressSet {
address[] keys;
// Position of the key in the `keys` array, plus 1 because index 0
// means a key is not in the set.
address[] values;
// Position of the value in the `values` array, plus 1 because index 0
// means a value is not in the set.
mapping (address => uint256) indexes;
}

/**
* @dev Add a key to a set. O(1).
* @dev Add a value to a set. O(1).
*
* Returns false if the key was already in the set.
* Returns false if the value was already in the set.
*/
function add(AddressSet storage set, address key)
function add(AddressSet storage set, address value)
internal
returns (bool)
{
if (!contains(set, key)) {
set.keys.push(key);
// The key is stored at length-1, but we add 1 to all indexes
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
set.indexes[key] = set.keys.length;
set.indexes[value] = set.values.length;
return true;
} else {
return false;
}
}

/**
* @dev Removes a key from a set. O(1).
* @dev Removes a value from a set. O(1).
*
* Returns false if the key was not present in the set.
* Returns false if the value was not present in the set.
*/
function remove(AddressSet storage set, address key)
function remove(AddressSet storage set, address value)
internal
returns (bool)
{
if (contains(set, key)){
uint256 toDeleteIndex = set.indexes[key] - 1;
uint256 lastIndex = set.keys.length - 1;
if (contains(set, value)){
uint256 toDeleteIndex = set.indexes[value] - 1;
uint256 lastIndex = set.values.length - 1;

// If the key we're deleting is the last one, we can just remove it without doing a swap
// If the value we're deleting is the last one, we can just remove it without doing a swap
if (lastIndex != toDeleteIndex) {
address lastKey = set.keys[lastIndex];
address lastvalue = set.values[lastIndex];

// Move the last key to the index where the deleted key is
set.keys[toDeleteIndex] = lastKey;
// Update the index for the moved key
set.indexes[lastKey] = toDeleteIndex + 1; // All indexes are 1-based
// Move the last value to the index where the deleted value is
set.values[toDeleteIndex] = lastvalue;
// Update the index for the moved value
set.indexes[lastvalue] = toDeleteIndex + 1; // All indexes are 1-based
}

// Delete the slot where the moved key was stored
set.keys.pop();
// Delete the slot where the moved value was stored
set.values.pop();

// Delete the index for the deleted slot
delete set.indexes[key];
delete set.indexes[value];

return true;
} else {
Expand All @@ -82,21 +82,21 @@ library EnumerableSet {
}

/**
* @dev Returns true if the key is in the set. O(1).
* @dev Returns true if the value is in the set. O(1).
*/
function contains(AddressSet storage set, address key)
function contains(AddressSet storage set, address value)
internal
view
returns (bool)
{
return set.indexes[key] != 0;
return set.indexes[value] != 0;
}

/**
* @dev Returns an array with all keys in the set. O(N).
* @dev Returns an array with all values in the set. O(N).
*
* Note that there are no guarantees on the ordering of keys inside the
* array, and it may change when more keys are added or removed.
* Note that there are no guarantees on the ordering of values inside the
* array, and it may change when more values are added or removed.

* WARNING: This function may run out of gas on large sets: use {length} and
* {at} instead in these cases.
Expand All @@ -106,29 +106,29 @@ library EnumerableSet {
view
returns (address[] memory)
{
address[] memory output = new address[](set.keys.length);
for (uint256 i; i < set.keys.length; i++){
output[i] = set.keys[i];
address[] memory output = new address[](set.values.length);
for (uint256 i; i < set.values.length; i++){
output[i] = set.values[i];
}
return output;
}

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

/**
* @dev Returns the key stored at position `index` in the set. O(1).
* @dev Returns the value stored at position `index` in the set. O(1).
*
* Note that there are no guarantees on the ordering of keys inside the
* array, and it may change when more keys are added or removed.
* Note that there are no guarantees on the ordering of values inside the
* array, and it may change when more values are added or removed.
*
* Requirements:
*
Expand All @@ -139,7 +139,7 @@ library EnumerableSet {
view
returns (address)
{
require(set.keys.length > index, "EnumerableSet: index out of bounds");
return set.keys[index];
require(set.values.length > index, "EnumerableSet: index out of bounds");
return set.values[index];
}
}