-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathAddressSet.sol
54 lines (49 loc) · 2.19 KB
/
AddressSet.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
pragma solidity ^0.5.0;
/// @title An implementation of the set data structure for addresses.
/// @author Noah Zinsmeister
/// @dev O(1) insertion, removal, contains, and length functions.
library AddressSet {
struct Set {
address[] members;
mapping(address => uint) memberIndices;
}
/// @dev Inserts an element into a set. If the element already exists in the set, the function is a no-op.
/// @param self The set to insert into.
/// @param other The element to insert.
function insert(Set storage self, address other) internal {
if (!contains(self, other)) {
self.memberIndices[other] = self.members.push(other);
}
}
/// @dev Removes an element from a set. If the element does not exist in the set, the function is a no-op.
/// @param self The set to remove from.
/// @param other The element to remove.
function remove(Set storage self, address other) internal {
if (contains(self, other)) {
// replace other with the last element
self.members[self.memberIndices[other] - 1] = self.members[length(self) - 1];
// reflect this change in the indices
self.memberIndices[self.members[self.memberIndices[other] - 1]] = self.memberIndices[other];
delete self.memberIndices[other];
// remove the last element
self.members.pop();
}
}
/// @dev Checks set membership.
/// @param self The set to check membership in.
/// @param other The element to check membership of.
/// @return true if the element is in the set, false otherwise.
function contains(Set storage self, address other) internal view returns (bool) {
return ( // solium-disable-line operator-whitespace
self.memberIndices[other] > 0 &&
self.members.length >= self.memberIndices[other] &&
self.members[self.memberIndices[other] - 1] == other
);
}
/// @dev Returns the number of elements in a set.
/// @param self The set to check the length of.
/// @return The number of elements in the set.
function length(Set storage self) internal view returns (uint) {
return self.members.length;
}
}