Mojo 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
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
,
Comparable
,
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, hint_trivial_type])
Construct a set from a List of elements.
Args:
- βelements (
List[T, hint_trivial_type]
): 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.
__lt__
β
__lt__(self: Self, other: Self) -> Bool
Overloads the < operator for strict subset comparison of sets.
Args:
- βother (
Self
): The set to compare against for the strict subset relationship.
Returns:
True if the set is a strict subset of the other
set, False otherwise.
__le__
β
__le__(self: Self, other: Self) -> Bool
Overloads the <= operator for sets. Works like as issubset
method.
Args:
- βother (
Self
): Another Set instance to check against.
Returns:
True if this set is a subset of the other
set, False otherwise.
__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.
__gt__
β
__gt__(self: Self, other: Self) -> Bool
Overloads the > operator for strict superset comparison of sets.
Args:
- βother (
Self
): The set to compare against for the strict superset relationship.
Returns:
True if the set is a strict superset of the other
set, False otherwise.
__ge__
β
__ge__(self: Self, other: Self) -> Bool
Overloads the >= operator for sets. Works like as issuperset
method.
Args:
- βother (
Self
): Another Set instance to check against.
Returns:
True if this set is a superset of the other
set, 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.
__xor__
β
__xor__(self: Self, other: Self) -> Self
Overloads the ^ operator for sets. Works like as symmetric_difference
method.
Args:
- βother (
Self
): The set to find the symmetric difference with.
Returns:
A new set containing the symmetric difference of the two sets.
__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.
__ixor__
β
__ixor__(inout self: Self, other: Self)
Overloads the ^= operator. Works like as symmetric_difference_update
method.
Updates the set with the symmetric difference of itself and another set.
Args:
- βother (
Self
): The set to find the symmetric difference with.
__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) -> UInt
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.
__str__
β
__str__[U: RepresentableKeyElement](self: Set[U]) -> String
Returns the string representation of the set.
Parameters:
- βU (
RepresentableKeyElement
): The type of the List elements. Must have the traitRepresentableCollectionElement
.
Returns:
The string representation of the set.
__repr__
β
__repr__[U: RepresentableKeyElement](self: Set[U]) -> String
Returns the string representation of the set.
Parameters:
- βU (
RepresentableKeyElement
): The type of the List elements. Must have the traitRepresentableCollectionElement
.
Returns:
The string representation of the set.
format_to
β
format_to[U: RepresentableKeyElement](self: Set[U], inout writer: Formatter)
Write Set string representation to a Formatter
.
Parameters:
- βU (
RepresentableKeyElement
): The type of the List elements. Must have the traitRepresentableCollectionElement
.
Args:
- βwriter (
Formatter
): The formatter to write to.
__iter__
β
__iter__(ref [self_is_lifetime] self: Self) -> _DictKeyIter[$0, T, NoneType, $1._data, 1]
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.
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.
difference_update
β
difference_update(inout self: Self, other: Self)
In-place set subtraction.
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 subtract from this one.
issubset
β
issubset(self: Self, other: Self) -> Bool
Check if this set is a subset of another set.
Args:
- βother (
Self
): Another Set instance to check against.
Returns:
True if this set is a subset of the other
set, False otherwise.
isdisjoint
β
isdisjoint(self: Self, other: Self) -> Bool
Check if this set is disjoint with another set.
Args:
- βother (
Self
): Another Set instance to check against.
Returns:
True if this set is disjoint with the other
set, False otherwise.
issuperset
β
issuperset(self: Self, other: Self) -> Bool
Check if this set is a superset of another set.
Args:
- βother (
Self
): Another Set instance to check against.
Returns:
True if this set is a superset of the other
set, False otherwise.
symmetric_difference
β
symmetric_difference(self: Self, other: Self) -> Self
Returns the symmetric difference of two sets.
Args:
- βother (
Self
): The set to find the symmetric difference with.
Returns:
A new set containing the symmetric difference of the two sets.
symmetric_difference_update
β
symmetric_difference_update(inout self: Self, other: Self)
Updates the set with the symmetric difference of itself and another set.
Args:
- βother (
Self
): The set to find the symmetric difference with.
discard
β
discard(inout self: Self, value: T)
Remove a value from the set if it exists. Pass otherwise.
Args:
- βvalue (
T
): The element to remove from the set.
clear
β
clear(inout self: Self)
Removes all elements from the set.
This method modifies the set in-place, removing all of its elements. After calling this method, the set will be empty.
Was this page helpful?
Thank you! We'll create more content like this.
Thank you for helping us improve!
π What went wrong?