GitHub

API reference for HashSet.

class HashSet[A = Unsubscripted]

Build one with HashSet.empty(), HashSet.make(*values) or HashSet.from_iterable(values).

Functions

__init__

__init__(root=None, size=0) -> None
ParameterTypeDescription
rootNode | None = None
sizeint = 0
def __init__(self, root: Node | None = None, size: int = 0) -> None:
    self._root = root
    self._size = size

empty

empty() -> HashSet[A]

Spelled HashSet[str].empty().

Returns HashSet[A]

@classmethod
def empty(cls) -> HashSet[A]:
    """Spelled ``HashSet[str].empty()``."""
    return HashSet()

make

make[B](*values: B = ()) -> HashSet[B]
make[B](*values: B = ()) -> HashSet[B]
ParameterTypeDescription
values= ()
@overload
@classmethod
def make[B](cls: type[HashSet[Unsubscripted]], *values: B) -> HashSet[B]: ...
@overload
@classmethod
def make[B](cls: type[HashSet[B]], *values: B) -> HashSet[B]: ...
@classmethod
def make[B](cls, *values: B) -> HashSet[B]:
    return HashSet.from_iterable(values)

from_iterable

from_iterable[B](values: Iterable[B]) -> HashSet[B]
from_iterable[B](values: Iterable[B]) -> HashSet[B]

The set of values; the first of equal values is kept.

ParameterTypeDescription
values
@overload
@classmethod
def from_iterable[B](cls: type[HashSet[Unsubscripted]], values: Iterable[B]) -> HashSet[B]: ...
@overload
@classmethod
def from_iterable[B](cls: type[HashSet[B]], values: Iterable[B]) -> HashSet[B]: ...
@classmethod
def from_iterable[B](cls, values: Iterable[B]) -> HashSet[B]:
    """The set of ``values``; the first of equal values is kept."""
    return HashSet[B]()._added(values)

is_empty

is_empty() -> bool

Returns bool

def is_empty(self) -> bool:
    return self._size == 0

has

has(value) -> bool
ParameterTypeDescription
valueobject

Returns bool

def has(self, value: object) -> bool:
    return self._has_ident(equal_key(value))

add

add[B](value) -> HashSet[B]

The set with value; the same set when it has it already.

ParameterTypeDescription
valueB

Returns HashSet[B]

def add[B](self: HashSet[B], value: B) -> HashSet[B]:
    """The set with ``value``; the same set when it has it already."""
    return self._added((value,))

remove

remove[B](value) -> HashSet[B]

The set without value; the same set when it lacks it.

ParameterTypeDescription
valueB

Returns HashSet[B]

def remove[B](self: HashSet[B], value: B) -> HashSet[B]:
    """The set without ``value``; the same set when it lacks it."""
    ident = equal_key(value)
    root = _hamt.remove(self._root, _hamt.hash_of(ident), ident)
    return self if root is self._root else HashSet(root, self._size - 1)

union

union[B](that) -> HashSet[A | B]
ParameterTypeDescription
thatHashSet[B]

Returns HashSet[A | B]

def union[B](self, that: HashSet[B]) -> HashSet[A | B]:
    return self._added(that)

intersection

intersection(that) -> HashSet[A]

The members of this set that that has too.

ParameterTypeDescription
thatHashSet[Any]

Returns HashSet[A]

def intersection(self, that: HashSet[Any]) -> HashSet[A]:
    """The members of this set that ``that`` has too."""
    return self._kept(lambda e: that._has_ident(e.ident))

difference

difference(that) -> HashSet[A]

The members of this set that that lacks.

ParameterTypeDescription
thatHashSet[Any]

Returns HashSet[A]

def difference(self, that: HashSet[Any]) -> HashSet[A]:
    """The members of this set that ``that`` lacks."""
    return self._kept(lambda e: not that._has_ident(e.ident))

is_subset

is_subset(that) -> bool

Whether that has every member of this set.

ParameterTypeDescription
thatHashSet[Any]

Returns bool

def is_subset(self, that: HashSet[Any]) -> bool:
    """Whether ``that`` has every member of this set."""
    return all(that._has_ident(e.ident) for e in self._entries())

map

map[B](f) -> HashSet[B]

f of every member; equal results are kept once.

ParameterTypeDescription
fCallable[[A], B]

Returns HashSet[B]

def map[B](self, f: Callable[[A], B]) -> HashSet[B]:
    """``f`` of every member; equal results are kept once."""
    return HashSet.from_iterable(f(v) for v in list(self))

filter

filter[B](predicate: Callable[[A], TypeIs[B]]) -> HashSet[B]
filter(predicate: Callable[[A], bool]) -> HashSet[A]
ParameterTypeDescription
predicate
@overload
def filter[B](self, predicate: Callable[[A], TypeIs[B]]) -> HashSet[B]: ...
@overload
def filter(self, predicate: Callable[[A], bool]) -> HashSet[A]: ...
def filter(self, predicate: Callable[[A], bool]) -> HashSet[Any]:
    return self._kept(lambda e: predicate(e.key))

some

some(predicate) -> bool
ParameterTypeDescription
predicateCallable[[A], bool]

Returns bool

def some(self, predicate: Callable[[A], bool]) -> bool:
    return any(predicate(v) for v in self)

every

every(predicate) -> bool
ParameterTypeDescription
predicateCallable[[A], bool]

Returns bool

def every(self, predicate: Callable[[A], bool]) -> bool:
    return all(predicate(v) for v in self)

reduce

reduce[Z](zero, f) -> Z
ParameterTypeDescription
zeroZ
fCallable[[Z, A], Z]

Returns Z

def reduce[Z](self, zero: Z, f: Callable[[Z, A], Z]) -> Z:
    out = zero
    for v in self:
        out = f(out, v)
    return out