Skip to main content
Log in

Mojo struct

Set

struct Set[T: KeyElement]

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, UnknownDestructibility

Methods

__init__

__init__(out self, *ts: T)

Construct a set from initial elements.

Args:

  • *ts (T): Variadic of elements to add to the set.

__init__(out self, elements: Self)

Explicitly copy another Set instance.

Args:

  • elements (Self): An existing set to copy.

__init__(out 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__(out self, owned other: Self)

Move constructor.

Args:

  • other (Self): The existing Set instance to move from.

__bool__

__bool__(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, 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, 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, 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, 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, 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, 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, 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, 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, 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, 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, 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__(mut 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__(mut 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__(mut 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__(mut 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) -> Int

The size of the set.

Returns:

The number of elements in the set.

__hash__

__hash__(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.

write_to

write_to[W: Writer, U: RepresentableKeyElement](self: Set[U], mut writer: W)

Write Set string representation to a Writer.

Parameters:

  • W (Writer): A type conforming to the Writable trait.
  • U (RepresentableKeyElement): The type of the List elements. Must have the trait RepresentableCollectionElement.

Args:

  • writer (W): The object to write to.

__iter__

__iter__(ref self) -> _DictKeyIter[T, NoneType, self_is_origin._data]

Iterate over elements of the set, returning immutable references.

Returns:

An iterator of immutable references to the set elements.

add

add(mut self, t: T)

Add an element to the set.

Args:

  • t (T): The element to add to the set.

remove

remove(mut 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(mut 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, 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, 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, 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(mut 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(mut 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(mut 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, 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, 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, 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, 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(mut 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(mut 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(mut 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.