View Source: contracts/mixins/EnumerableAddressSet.sol
Based on Library for managing https://en.wikipedia.org/wiki/Set_(abstract_data_type)[sets] of primitive types.
- Sets have the following properties:
-
- Elements are added, removed, and checked for existence in constant time (O(1)).
- Elements are enumerated in O(n). No guarantees are made on the ordering.
- As of v2.5.0, only
address
sets are supported. - Include with
using EnumerableSet for EnumerableSet.AddressSet;
. - Available since v2.5.0.
struct AddressSet {
mapping(address => uint256) index,
address[] values
}
- add(struct EnumerableAddressSet.AddressSet set, address value)
- remove(struct EnumerableAddressSet.AddressSet set, address value)
- contains(struct EnumerableAddressSet.AddressSet set, address value)
- enumerate(struct EnumerableAddressSet.AddressSet set)
- enumerateChunk(struct EnumerableAddressSet.AddressSet set, uint256 start, uint256 count)
- length(struct EnumerableAddressSet.AddressSet set)
- get(struct EnumerableAddressSet.AddressSet set, uint256 index)
Add a value to a set. O(1). Returns false if the value was already in the set.
function add(struct EnumerableAddressSet.AddressSet set, address value) internal nonpayable
returns(bool)
Arguments
Name | Type | Description |
---|---|---|
set | struct EnumerableAddressSet.AddressSet | |
value | address |
Source Code
function add(AddressSet storage set, address value) internal returns (bool) {
if (!contains(set, value)) {
set.index[value] = set.values.push(value);
return true;
} else {
return false;
}
}
Removes a value from a set. O(1). Returns false if the value was not present in the set.
function remove(struct EnumerableAddressSet.AddressSet set, address value) internal nonpayable
returns(bool)
Arguments
Name | Type | Description |
---|---|---|
set | struct EnumerableAddressSet.AddressSet | |
value | address |
Source Code
function remove(AddressSet storage set, address value) internal returns (bool) {
if (contains(set, value)) {
uint256 toDeleteIndex = set.index[value] - 1;
uint256 lastIndex = set.values.length - 1;
// If the element we're deleting is the last one, we can just remove it without doing a swap
if (lastIndex != toDeleteIndex) {
address lastValue = set.values[lastIndex];
// Move the last value to the index where the deleted value is
set.values[toDeleteIndex] = lastValue;
// Update the index for the moved value
set.index[lastValue] = toDeleteIndex + 1; // All indexes are 1-based
}
// Delete the index entry for the deleted value
delete set.index[value];
// Delete the old entry for the moved value
set.values.pop();
return true;
} else {
return false;
}
}
Returns true if the value is in the set. O(1).
function contains(struct EnumerableAddressSet.AddressSet set, address value) internal view
returns(bool)
Arguments
Name | Type | Description |
---|---|---|
set | struct EnumerableAddressSet.AddressSet | |
value | address |
Source Code
function contains(AddressSet storage set, address value) internal view returns (bool) {
return set.index[value] != 0;
}
Returns an array with all values in the set. O(N). 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 {get} instead in these cases.
function enumerate(struct EnumerableAddressSet.AddressSet set) internal view
returns(address[])
Arguments
Name | Type | Description |
---|---|---|
set | struct EnumerableAddressSet.AddressSet |
Source Code
function enumerate(AddressSet storage set) internal view returns (address[] memory) {
address[] memory output = new address[](set.values.length);
for (uint256 i; i < set.values.length; i++) {
output[i] = set.values[i];
}
return output;
}
Returns a chunk of array as recommended in enumerate() to avoid running of gas. 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 {get} instead in these cases.
function enumerateChunk(struct EnumerableAddressSet.AddressSet set, uint256 start, uint256 count) internal view
returns(output address[])
Arguments
Name | Type | Description |
---|---|---|
set | struct EnumerableAddressSet.AddressSet | |
start | uint256 | start index of chunk |
count | uint256 | num of element to return; if count == 0 then returns all the elements from the |
Source Code
function enumerateChunk(
AddressSet storage set,
uint256 start,
uint256 count
) internal view returns (address[] memory output) {
uint256 end = start + count;
require(end >= start, "addition overflow");
end = (set.values.length < end || count == 0) ? set.values.length : end;
if (end == 0 || start >= end) {
return output;
}
output = new address[](end - start);
for (uint256 i; i < end - start; i++) {
output[i] = set.values[i + start];
}
return output;
}
Returns the number of elements on the set. O(1).
function length(struct EnumerableAddressSet.AddressSet set) internal view
returns(uint256)
Arguments
Name | Type | Description |
---|---|---|
set | struct EnumerableAddressSet.AddressSet |
Source Code
function length(AddressSet storage set) internal view returns (uint256) {
return set.values.length;
}
Returns the element stored at position index
in the set. O(1).
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:
* - index
must be strictly less than {length}.
function get(struct EnumerableAddressSet.AddressSet set, uint256 index) internal view
returns(address)
Arguments
Name | Type | Description |
---|---|---|
set | struct EnumerableAddressSet.AddressSet | |
index | uint256 |
Source Code
function get(AddressSet storage set, uint256 index) internal view returns (address) {
return set.values[index];
}
- Address
- Administered
- AdminRole
- AdvancedToken
- AdvancedTokenStorage
- Affiliates
- AffiliatesEvents
- ApprovalReceiver
- BProPriceFeed
- CheckpointsShared
- Constants
- Context
- DevelopmentFund
- DummyContract
- EnumerableAddressSet
- EnumerableBytes32Set
- EnumerableBytes4Set
- ERC20
- ERC20Detailed
- ErrorDecoder
- Escrow
- EscrowReward
- FeedsLike
- FeesEvents
- FeeSharingCollector
- FeeSharingCollectorProxy
- FeeSharingCollectorStorage
- FeesHelper
- FourYearVesting
- FourYearVestingFactory
- FourYearVestingLogic
- FourYearVestingStorage
- GenericTokenSender
- GovernorAlpha
- GovernorVault
- IApproveAndCall
- IChai
- IContractRegistry
- IConverterAMM
- IERC1820Registry
- IERC20_
- IERC20
- IERC777
- IERC777Recipient
- IERC777Sender
- IFeeSharingCollector
- IFourYearVesting
- IFourYearVestingFactory
- IFunctionsList
- ILiquidityMining
- ILiquidityPoolV1Converter
- ILoanPool
- ILoanToken
- ILoanTokenLogicBeacon
- ILoanTokenLogicModules
- ILoanTokenLogicProxy
- ILoanTokenModules
- ILoanTokenWRBTC
- ILockedSOV
- IMoCState
- IModulesProxyRegistry
- Initializable
- InterestUser
- IPot
- IPriceFeeds
- IPriceFeedsExt
- IProtocol
- IRSKOracle
- ISovryn
- ISovrynSwapNetwork
- IStaking
- ISwapsImpl
- ITeamVesting
- ITimelock
- IV1PoolOracle
- IVesting
- IVestingFactory
- IVestingRegistry
- IWrbtc
- IWrbtcERC20
- LenderInterestStruct
- LiquidationHelper
- LiquidityMining
- LiquidityMiningConfigToken
- LiquidityMiningProxy
- LiquidityMiningStorage
- LoanClosingsEvents
- LoanClosingsLiquidation
- LoanClosingsRollover
- LoanClosingsShared
- LoanClosingsWith
- LoanClosingsWithoutInvariantCheck
- LoanInterestStruct
- LoanMaintenance
- LoanMaintenanceEvents
- LoanOpenings
- LoanOpeningsEvents
- LoanParamsStruct
- LoanSettings
- LoanSettingsEvents
- LoanStruct
- LoanToken
- LoanTokenBase
- LoanTokenLogicBeacon
- LoanTokenLogicLM
- LoanTokenLogicProxy
- LoanTokenLogicStandard
- LoanTokenLogicStorage
- LoanTokenLogicWrbtc
- LoanTokenSettingsLowerAdmin
- LockedSOV
- MarginTradeStructHelpers
- Medianizer
- ModuleCommonFunctionalities
- ModulesCommonEvents
- ModulesProxy
- ModulesProxyRegistry
- MultiSigKeyHolders
- MultiSigWallet
- Mutex
- Objects
- OrderStruct
- OrigingVestingCreator
- OriginInvestorsClaim
- Ownable
- Pausable
- PausableOz
- PreviousLoanToken
- PreviousLoanTokenSettingsLowerAdmin
- PriceFeedRSKOracle
- PriceFeeds
- PriceFeedsLocal
- PriceFeedsMoC
- PriceFeedV1PoolOracle
- ProtocolAffiliatesInterface
- ProtocolLike
- ProtocolSettings
- ProtocolSettingsEvents
- ProtocolSettingsLike
- ProtocolSwapExternalInterface
- ProtocolTokenUser
- Proxy
- ProxyOwnable
- ReentrancyGuard
- RewardHelper
- RSKAddrValidator
- SafeERC20
- SafeMath
- SafeMath96
- setGet
- SharedReentrancyGuard
- SignedSafeMath
- SOV
- sovrynProtocol
- StakingAdminModule
- StakingGovernanceModule
- StakingInterface
- StakingProxy
- StakingRewards
- StakingRewardsProxy
- StakingRewardsStorage
- StakingShared
- StakingStakeModule
- StakingStorageModule
- StakingStorageShared
- StakingVestingModule
- StakingWithdrawModule
- State
- SwapsEvents
- SwapsExternal
- SwapsImplLocal
- SwapsImplSovrynSwap
- SwapsUser
- TeamVesting
- Timelock
- TimelockHarness
- TimelockInterface
- TokenSender
- UpgradableProxy
- USDTPriceFeed
- Utils
- VaultController
- Vesting
- VestingCreator
- VestingFactory
- VestingLogic
- VestingRegistry
- VestingRegistry2
- VestingRegistry3
- VestingRegistryLogic
- VestingRegistryProxy
- VestingRegistryStorage
- VestingStorage
- WeightedStakingModule
- WRBTC