Sets

The galactic.algebras.set module defines several set classes.

class FIFOSet(*args: Iterable[galactic.algebras.set._fifo._T])

The FIFOSet is a kind of set that remembers the order of its entries.

Examples

>>> from galactic.algebras.set import FIFOSet
>>> a = FIFOSet[int]([3, 5, 1, 2, 6, 2])
>>> list(a)
[3, 5, 1, 2, 6]
>>> list(a | FIFOSet[int]([1, 5, 7 ,8, 9]))
[3, 5, 1, 2, 6, 7, 8, 9]
__init__(*args: Iterable[galactic.algebras.set._fifo._T]) None

Initialise a FIFOSet instance.

The initializer can take 0 or 1 argument that should be iterable.

Raises:
  • TypeError – If two or more arguments are given.

  • TypeError – If the argument is not iterable.

add(value: galactic.algebras.set._fifo._T, last: bool = True) None

Add value to the set.

Parameters:
  • value (_T) – The value to add.

  • last (bool) – Where to add.

clear() None

Clear the FIFOSet.

discard(value: galactic.algebras.set._fifo._T) None

Discard value from the set if it is present.

Parameters:

value (_T) – The value to remove.

isdisjoint(other)

Return True if two sets have a null intersection.

move_to_end(value: galactic.algebras.set._fifo._T, last: bool = True) None

Move an existing value to either end of a FIFOSet.

The value is moved to the right end if last is True (the default) or to the beginning if last is False.

Parameters:
  • value (_T) – The value to move

  • last (bool) – Where to move

Raises:

KeyError – if the value does not exist.

pop(last: bool = False) galactic.algebras.set._fifo._T

Remove and return an existing value from either end of a FIFOSet.

Returns:

The value removed.

Return type:

_T

Parameters:

last (bool) – Where to remove

Raises:

KeyError – if the FIFOSet is empty.

remove(value: galactic.algebras.set._fifo._T) None

Remove value from the set if it is present.

Parameters:

value (_T) – The value to remove.

Raises:

KeyError – if the value does not exist.

class FIFOSetFactory(*args, **kwds)

The FIFOSetFactory class provides a _from_iterable() class methods.

This method is used by builtin set classes when set operations are executed.

class IterableSet(*args, **kwds)

The IterableSet class implements some basic operations.

This allow sub-classes to only implement the __iter__() method.

isdisjoint(other)

Return True if two sets have a null intersection.

class FiniteSubSet(universe: galactic.algebras.set.FiniteUniverse[galactic.algebras.set._finite._T], iterable: Optional[Iterable[galactic.algebras.set._finite._T]] = None)

The FiniteSubSet class represents subsets of a finite universe.

Instances stores membership using a compact array of intervals represented by an array of int. For example, using the abcdef universe, the abde subset will be represented by [0,2,3,5] meaning:

  • from element 0 (included) to element 2 (excluded) of universe

  • from element 3 (included) to element 5 (excluded) of universe

Examples

>>> from galactic.algebras.set import FiniteUniverse, FiniteSubSet
>>> universe = FiniteUniverse[str]("abcdef")
>>> subset1 = FiniteSubSet[str](universe, "abde")
>>> list(subset1.universe)
['a', 'b', 'c', 'd', 'e', 'f']
>>> len(subset1)
4
>>> "a" in subset1
True
>>> "f" in subset1
False
>>> list(~subset1)
['c', 'f']
>>> subset2 = FiniteSubSet[str](universe, "bdef")
>>> list(subset1 | subset2)
['a', 'b', 'd', 'e', 'f']
>>> list(subset1 & subset2)
['b', 'd', 'e']
>>> list(subset1 ^ subset2)
['a', 'f']
>>> subset1 & subset2 <= subset1
True
>>> subset1 | subset2 >= subset1
True
>>> subset1.isdisjoint(subset2)
False
__init__(universe: galactic.algebras.set.FiniteUniverse[galactic.algebras.set._finite._T], iterable: Optional[Iterable[galactic.algebras.set._finite._T]] = None) None

Initialise a FiniteSubSet instance.

Parameters:

universe (FiniteUniverse[_T]) – A finite universe.

Keyword Arguments:

iterable (Iterable[_T], optional) – An optional iterable of values.

isdisjoint(other: Any) bool

Get the disjointness with another set.

Parameters:

other (Any) – An iterable of values.

Returns:

True if the set has no elements in common with other. Sets are disjoint if and only if their intersection is the empty set.

Return type:

bool

property universe: galactic.algebras.set.FiniteUniverse[galactic.algebras.set._finite._T]

Get the underlying universe.

Returns:

The underlying universe.

Return type:

FiniteUniverse[T]

class FiniteUniverse(iterable: Iterable[galactic.algebras.set._finite._T])

The FiniteUniverse class is used to represent finite universe.

Examples

>>> from galactic.algebras.set import FiniteUniverse
>>> universe = FiniteUniverse[str]("abcdef")
>>> list(universe)
['a', 'b', 'c', 'd', 'e', 'f']
>>> list(reversed(universe))
['f', 'e', 'd', 'c', 'b', 'a']
>>> universe.index('c')
2
>>> universe.count('c')
1
>>> universe.count('g')
0
__init__(iterable: Iterable[galactic.algebras.set._finite._T]) None

Initialise a FiniteUniverse instance.

Parameters:

iterable (Iterable[_T]) – An iterable of hashable values.

count(value: Any) int

Get the total number of occurrences of value.

Parameters:

value (Any) – The value whose count is requested.

Returns:

1 if the value is in the universe, else 0.

Return type:

int

index(value: Any, start: Optional[int] = None, stop: Optional[int] = None) int

Get the index of a value.

Parameters:

value (_T) – The value whose index is requested.

Keyword Arguments:
  • start (int, optional) – Where to start.

  • stop (int, optional) – Where to stop.

Raises:
  • TypeError – If the value is not hashable.

  • ValueError – If the value is not in the universe or not found in the interval.

isdisjoint(other)

Return True if two sets have a null intersection.