struct
Set
A set data type.
O(1) average-case amortized add, remove, and membership check.
from collections import Set
var set = Set[Int](1, 2, 3)
print(len(set)) # 3
set.add(4)
for element in set:
print(element[])
set -= Set[Int](3, 4, 5)
print(set == Set[Int](1, 2)) # True
print(set | Set[Int](0, 1) == Set[Int](0, 1, 2)) # True
var element = set.pop()
print(len(set)) # 1
Parameters
- T (
KeyElement
): The element type of the set. Must implement KeyElement.
Implemented traits
AnyType
,
Boolable
,
EqualityComparable
,
Hashable
,
Sized
Methods
__init__
__init__(inout self: Self, /, *ts: T)
Construct a set from initial elements.
Args:
- *ts (
T
): Variadic of elements to add to the set.
__init__(inout self: Self, /, elements: Self)
Explicitly copy another Set instance.
Args:
- elements (
Self
): An existing set to copy.
__init__(inout self: Self, /, elements: List[T])
Construct a set from a List of elements.
Args:
- elements (
List[T]
): A vector of elements to add to the set.
__moveinit__
__moveinit__(inout self: Self, /, owned other: Self)
Move constructor.
Args:
- other (
Self
): The existing Set instance to move from.
__bool__
__bool__(self: Self) -> Bool
Whether the set is non-empty or not.
Returns:
True if the set is non-empty, False if it is empty.
__eq__
__eq__(self: Self, other: Self) -> Bool
Set equality.
Args:
- other (
Self
): Another Set instance to check equality against.
Returns:
True if the sets contain the same elements and False otherwise.
__ne__
__ne__(self: Self, other: Self) -> Bool
Set inequality.
Args:
- other (
Self
): Another Set instance to check equality against.
Returns:
True if the sets are different and False otherwise.
__contains__
__contains__(self: Self, t: T) -> Bool
Whether or not the set contains an element.
Args:
- t (
T
): The element to check membership in the set.
Returns:
Whether or not the set contains the element.
__sub__
__sub__(self: Self, other: Self) -> Self
Set subtraction.
Args:
- other (
Self
): Another Set instance to subtract from this one.
Returns:
A new set containing elements of this set, but not containing any elements which were in the other
set.
__and__
__and__(self: Self, other: Self) -> Self
The set intersection operator.
Args:
- other (
Self
): Another Set instance to intersect with this one.
Returns:
A new set containing only the elements which appear in both this set and the other
set.
__or__
__or__(self: Self, other: Self) -> Self
The set union operator.
Args:
- other (
Self
): Another Set instance to union with this one.
Returns:
A new set containing any elements which appear in either this set or the other
set.
__isub__
__isub__(inout self: Self, other: Self)
In-place set subtraction.
Updates the set to remove any elements from the other
set.
Args:
- other (
Self
): Another Set instance to subtract from this one.
__iand__
__iand__(inout self: Self, other: Self)
In-place set intersection.
Updates the set to contain only the elements which are already in
the set and are also contained in the other
set.
Args:
- other (
Self
): Another Set instance to intersect with this one.
__ior__
__ior__(inout self: Self, other: Self)
In-place set union.
Updates the set to contain all elements in the other
set
as well as keeping all elements it already contained.
Args:
- other (
Self
): Another Set instance to union with this one.
__len__
__len__(self: Self) -> Int
The size of the set.
Returns:
The number of elements in the set.
__hash__
__hash__(self: Self) -> Int
A hash value of the elements in the set.
The hash value is order independent, so s1 == s2 -> hash(s1) == hash(s2).
Returns:
A hash value of the set suitable for non-cryptographic purposes.
__iter__
__iter__[mutability: i1, self_life: lifetime<*(0,0)>](self: !lit.ref<_stdlib::_collections::_set::_Set<:trait<_stdlib::_collections::_dict::_KeyElement> T>, mut=mutability, self_life>) -> _DictKeyIter[T, None, $0, $1, true]
Iterate over elements of the set, returning immutable references.
Returns:
An iterator of immutable references to the set elements.
add
add(inout self: Self, t: T)
Add an element to the set.
Args:
- t (
T
): The element to add to the set.
remove
remove(inout self: Self, t: T)
Remove an element from the set.
Args:
- t (
T
): The element to remove from the set.
Raises:
If the element isn't in the set to remove.
pop
pop(inout self: Self) -> T
Remove any one item from the set, and return it.
As an implementation detail this will remove the first item according to insertion order. This is practically useful for breadth-first search implementations.
Returns:
The element which was removed from the set.
Raises:
If the set is empty.
union
union(self: Self, other: Self) -> Self
Set union.
Args:
- other (
Self
): Another Set instance to union with this one.
Returns:
A new set containing any elements which appear in either this set or the other
set.
intersection
intersection(self: Self, other: Self) -> Self
Set intersection.
Args:
- other (
Self
): Another Set instance to intersect with this one.
Returns:
A new set containing only the elements which appear in both this set and the other
set.
difference
difference(self: Self, other: Self) -> Self
Set difference.
Args:
- other (
Self
): Another Set instance to find the difference with this one.
Returns:
A new set containing elements that are in this set but not in the other
set.
update
update(inout self: Self, other: Self)
In-place set update.
Updates the set to contain all elements in the other
set
as well as keeping all elements it already contained.
Args:
- other (
Self
): Another Set instance to union with this one.
difference_update
difference_update(inout self: Self, other: Self)
In-place set difference update.
Updates the set by removing all elements found in the other
set,
effectively keeping only elements that are unique to this set.
Args:
- other (
Self
): Another Set instance to compare with this one.
intersection_update
intersection_update(inout self: Self, other: Self)
In-place set intersection update.
Updates the set by retaining only elements found in both this set and the other
set,
removing all other elements. The result is the intersection of this set with other
.
Args:
- other (
Self
): Another Set instance to intersect with this one.
remove_all
remove_all(inout self: Self, other: Self)
In-place set subtraction.
Updates the set to remove any elements from the other
set.
Args:
- other (
Self
): Another Set instance to subtract from this one.