For the complete Mojo documentation index, see llms.txt. Markdown versions of all pages are available by appending .md to any URL (e.g. /docs/manual/basics.md).
Set
struct Set[T: KeyElement, H: Hasher = AHasher[SIMD[DType.uint64, SIMDLength(4)](0)]]
A set data type.
O(1) average-case amortized add, remove, and membership check.
from std.collections import Set
var set = { 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 implementKeyElement(i.e.Movable & Hashable & Equatable). Methods that fundamentally need to copy elements (union,intersection,__or__, iteration, ...) are conditionally available viawhere conforms_to(T, Copyable)clauses. WhenTis notImplicitlyDeletable, the set has no implicit destructor and must be torn down withdeinit_with(). - H (
Hasher): The type of the hasher used to hash keys.
Implemented traits
AnyType,
Boolable,
Comparable,
Copyable,
Equatable,
Hashable,
ImplicitlyDeletable,
Iterable,
IterableOwned,
Movable,
Sized,
Writable
comptime members
IteratorOwnedType
comptime IteratorOwnedType = _DictKeyIterOwned[T(ImplicitlyDeletable & Equatable & Movable & Hashable), NoneType, H]
The owned iterator type for this set.
IteratorType
comptime IteratorType[iterable_mut: Bool, //, iterable_origin: Origin[mut=iterable_mut]] = _DictKeyIter[T(ImplicitlyDeletable & Equatable & Copyable & Hashable), NoneType, H, iterable_origin]
The iterator type for this set.
Parameters
- iterable_mut (
Bool): Whether the iterable is mutable. - iterable_origin (
Origin[mut=iterable_mut]): The origin of the iterable.
Methods
__init__
def __init__(out self)
Construct an empty set.
def __init__(out self, *ts: T, *, __set_literal__: NoneType = None) where conforms_to(T, ImplicitlyDeletable) if conforms_to(T, Copyable) else conforms_to(T, Copyable)
Construct a set from initial elements.
Args:
- *ts (
T): Variadic of elements to add to the set. - set_literal (
NoneType): Tell Mojo to use this method for set literals.
def __init__(out self, elements: List[T]) where conforms_to(T, ImplicitlyDeletable) if conforms_to(T, Copyable) else conforms_to(T, Copyable)
Construct a set from a List of elements.
Args:
- elements (
List[T]): A vector of elements to add to the set.
__deinit__
def __deinit__(deinit self) where conforms_to(T, ImplicitlyDeletable)
Destroy all elements in the set and free its memory.
Constraints:
T must be ImplicitlyDeletable. When it is not, the set has no
implicit destructor and must be torn down with deinit_with().
__bool__
def __bool__(self) -> Bool
Whether the set is non-empty or not.
Returns:
Bool: True if the set is non-empty, False if it is empty.
__lt__
def __lt__(self, other: Self) -> Bool where conforms_to(T, Equatable) if conforms_to(T, Copyable) else conforms_to(T, Copyable)
Overloads the < operator for strict subset comparison of sets.
Args:
- other (
Self): The set to compare against for the strict subset relationship.
Returns:
Bool: True if the set is a strict subset of the other set, False otherwise.
__le__
def __le__(self, other: Self) -> Bool where conforms_to(T, Equatable) if conforms_to(T, Copyable) else conforms_to(T, Copyable)
Overloads the <= operator for sets. Works like as issubset method.
Args:
- other (
Self): Another Set instance to check against.
Returns:
Bool: True if this set is a subset of the other set, False otherwise.
__eq__
def __eq__(self, other: Self) -> Bool where conforms_to(T, Equatable) if conforms_to(T, Copyable) else conforms_to(T, Copyable)
Set equality.
Args:
- other (
Self): Another Set instance to check equality against.
Returns:
Bool: True if the sets contain the same elements and False otherwise.
__gt__
def __gt__(self, other: Self) -> Bool where conforms_to(T, Equatable) if conforms_to(T, Copyable) else conforms_to(T, Copyable)
Overloads the > operator for strict superset comparison of sets.
Args:
- other (
Self): The set to compare against for the strict superset relationship.
Returns:
Bool: True if the set is a strict superset of the other set, False otherwise.
__ge__
def __ge__(self, other: Self) -> Bool where conforms_to(T, Equatable) if conforms_to(T, Copyable) else conforms_to(T, Copyable)
Overloads the >= operator for sets. Works like as issuperset method.
Args:
- other (
Self): Another Set instance to check against.
Returns:
Bool: True if this set is a superset of the other set, False otherwise.
__contains__
def __contains__(self, t: T) -> Bool
Whether or not the set contains an element.
Args:
- t (
T): The element to check membership in the set.
Returns:
Bool: Whether or not the set contains the element.
__sub__
def __sub__(self, other: Self) -> Self where conforms_to(T, ImplicitlyDeletable) if conforms_to(T, Copyable) else conforms_to(T, Copyable)
Set subtraction.
Args:
- other (
Self): Another Set instance to subtract from this one.
Returns:
Self: A new set containing elements of this set, but not containing
any elements which were in the other set.
__and__
def __and__(self, other: Self) -> Self where conforms_to(T, ImplicitlyDeletable) if conforms_to(T, Copyable) else conforms_to(T, Copyable)
The set intersection operator.
Args:
- other (
Self): Another Set instance to intersect with this one.
Returns:
Self: A new set containing only the elements which appear in both
this set and the other set.
__or__
def __or__(self, other: Self) -> Self where conforms_to(T, ImplicitlyDeletable) if conforms_to(T, Copyable) else conforms_to(T, Copyable)
The set union operator.
Args:
- other (
Self): Another Set instance to union with this one.
Returns:
Self: A new set containing any elements which appear in either
this set or the other set.
__xor__
def __xor__(self, other: Self) -> Self where conforms_to(T, ImplicitlyDeletable) if conforms_to(T, Copyable) else conforms_to(T, Copyable)
Overloads the ^ operator for sets. Works like as symmetric_difference method.
Args:
- other (
Self): The set to find the symmetric difference with.
Returns:
Self: A new set containing the symmetric difference of the two sets.
__isub__
def __isub__(mut self, other: Self) where conforms_to(T, ImplicitlyDeletable) if conforms_to(T, Copyable) else conforms_to(T, Copyable)
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__
def __iand__(mut self, other: Self) where conforms_to(T, ImplicitlyDeletable) if conforms_to(T, Copyable) else conforms_to(T, Copyable)
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__
def __ixor__(mut self, other: Self) where conforms_to(T, ImplicitlyDeletable) if conforms_to(T, Copyable) else conforms_to(T, Copyable)
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__
def __ior__(mut self, other: Self) where conforms_to(T, ImplicitlyDeletable) if conforms_to(T, Copyable) else conforms_to(T, Copyable)
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.
deinit_with
def deinit_with(deinit self, deinit_func: T, /) where (eq T.T, T)
Consume the set, deinitializing each element with a closure.
Use this to tear down a Set whose element type is not
ImplicitlyDeletable.
Args:
- deinit_func (
T): A closure called once per element to destroy it.
__len__
__hash__
def __hash__(self, mut hasher: T) where conforms_to(T, Hashable) if conforms_to(T, Copyable) else conforms_to(T, Copyable)
Updates hasher with the underlying values.
The update is order independent, so s1 == s2 -> hash(s1) == hash(s2).
Args:
- hasher (
T): The hasher instance.
write_to
def write_to(self, mut writer: T) where conforms_to(T, Writable) if conforms_to(T, Copyable) else conforms_to(T, Copyable)
Write this set to a Writer.
Args:
- writer (
T): The object to write to.
write_repr_to
def write_repr_to(self, mut writer: T) where conforms_to(T, Writable) if conforms_to(T, Copyable) else conforms_to(T, Copyable)
Write this set to a Writer.
Args:
- writer (
T): The object to write to.
__iter__
def __iter__(deinit self) -> Self.IteratorOwnedType where conforms_to(T, ImplicitlyDeletable)
Consume the set and iterate over its elements.
Constraints:
T must be ImplicitlyDeletable; consuming iteration drops the
backing dictionary's value slots in place.
Returns:
Self.IteratorOwnedType: An iterator that owns the set's elements.
def __iter__(ref self) -> _DictKeyIter[T(ImplicitlyDeletable & Equatable & Copyable & Hashable), NoneType, H, origin_of(self)]
Iterate over elements of the set, returning immutable references.
Returns:
_DictKeyIter[T(ImplicitlyDeletable & Equatable & Copyable & Hashable), NoneType, H, origin_of(self)]: An iterator of immutable references to the set elements.
add
def add(mut self, var t: T) where conforms_to(T, ImplicitlyDeletable)
Add an element to the set.
Constraints:
T must be ImplicitlyDeletable; adding a duplicate discards the
incoming element in place.
Args:
- t (
T): The element to add to the set.
insert
def insert(mut self, var t: T) -> Optional[T]
Insert an element, returning any displaced equal element.
Unlike add, a displaced equal element is moved out and returned
rather than destroyed in place, so this works when T is not
ImplicitlyDeletable. The caller owns the returned element.
Args:
- t (
T): The element to insert into the set.
Returns:
Optional[T]: The previously-present equal element if one was displaced,
otherwise an empty Optional.
remove
def remove(mut self, t: T) where conforms_to(T, ImplicitlyDeletable)
Remove an element from the set.
Constraints:
T must be ImplicitlyDeletable; the removed element is destroyed
in place.
Args:
- t (
T): The element to remove from the set.
Raises:
If the element isn't in the set to remove.
pop
def pop(mut self) -> T
Remove any one item from the set, and return it.
As an implementation detail this will remove the last item according to insertion order.
Returns:
T: The element which was removed from the set.
Raises:
If the set is empty.
union
def union(self, other: Self) -> Self where conforms_to(T, ImplicitlyDeletable) if conforms_to(T, Copyable) else conforms_to(T, Copyable)
Set union.
Args:
- other (
Self): Another Set instance to union with this one.
Returns:
Self: A new set containing any elements which appear in either
this set or the other set.
intersection
def intersection(self, other: Self) -> Self where conforms_to(T, ImplicitlyDeletable) if conforms_to(T, Copyable) else conforms_to(T, Copyable)
Set intersection.
Args:
- other (
Self): Another Set instance to intersect with this one.
Returns:
Self: A new set containing only the elements which appear in both
this set and the other set.
difference
def difference(self, other: Self) -> Self where conforms_to(T, ImplicitlyDeletable) if conforms_to(T, Copyable) else conforms_to(T, Copyable)
Set difference.
Args:
- other (
Self): Another Set instance to find the difference with this one.
Returns:
Self: A new set containing elements that are in this set but not in
the other set.
update
def update(mut self, other: Self) where conforms_to(T, ImplicitlyDeletable) if conforms_to(T, Copyable) else conforms_to(T, Copyable)
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
def intersection_update(mut self, other: Self) where conforms_to(T, ImplicitlyDeletable) if conforms_to(T, Copyable) else conforms_to(T, Copyable)
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
def difference_update(mut self, other: Self) where conforms_to(T, ImplicitlyDeletable) if conforms_to(T, Copyable) else conforms_to(T, Copyable)
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
def issubset(self, other: Self) -> Bool where conforms_to(T, Copyable)
Check if this set is a subset of another set.
Args:
- other (
Self): Another Set instance to check against.
Returns:
Bool: True if this set is a subset of the other set, False otherwise.
isdisjoint
def isdisjoint(self, other: Self) -> Bool where conforms_to(T, Copyable)
Check if this set is disjoint with another set.
Args:
- other (
Self): Another Set instance to check against.
Returns:
Bool: True if this set is disjoint with the other set, False otherwise.
issuperset
def issuperset(self, other: Self) -> Bool where conforms_to(T, Copyable)
Check if this set is a superset of another set.
Args:
- other (
Self): Another Set instance to check against.
Returns:
Bool: True if this set is a superset of the other set, False otherwise.
symmetric_difference
def symmetric_difference(self, other: Self) -> Self where conforms_to(T, ImplicitlyDeletable) if conforms_to(T, Copyable) else conforms_to(T, Copyable)
Returns the symmetric difference of two sets.
Args:
- other (
Self): The set to find the symmetric difference with.
Returns:
Self: A new set containing the symmetric difference of the two sets.
symmetric_difference_update
def symmetric_difference_update(mut self, other: Self) where conforms_to(T, ImplicitlyDeletable) if conforms_to(T, Copyable) else conforms_to(T, Copyable)
Updates the set with the symmetric difference of itself and another set.
Args:
- other (
Self): The set to find the symmetric difference with.
discard
def discard(mut self, value: T) where conforms_to(T, ImplicitlyDeletable)
Remove a value from the set if it exists. Pass otherwise.
Constraints:
T must be ImplicitlyDeletable; a removed element is destroyed
in place.
Args:
- value (
T): The element to remove from the set.
clear
def clear(mut self) where conforms_to(T, ImplicitlyDeletable)
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.
Constraints:
T must be ImplicitlyDeletable, since every element is destroyed
in place.
clear_with
def clear_with(mut self, destroy_func: T, /) where (eq T.T, T)
Remove all elements, disposing each with a closure.
The closure counterpart of clear: instead of destroying each element
in place, it hands each element to destroy_func. Use this to clear a
Set whose element type is not ImplicitlyDeletable. The set's
capacity is retained.
Args:
- destroy_func (
T): A closure called once per element to destroy it.