Skip to main content
Log in

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 trait RepresentableCollectionElement.

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 trait RepresentableCollectionElement.

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 trait RepresentableCollectionElement.

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?