galactic.algebras.set.core

Contents

galactic.algebras.set.core#

Defines core set data structures for universes and FIFO sets.

Classes

FIFO Sets:

Universes and Subsets:

  • IndexRange for representing universes of integers

  • Universe for creating universes of hashable values

  • SubSet for creating subsets of universes


class FrozenFIFOSet(*elements)#

Bases: Set[_T]

Represents an immutable set that preserves FIFO order.

Initialize an instance with zero or one argument, which should be iterable.

Parameters:

*elements (Iterable[TypeVar(_T)]) – Elements to include in the set

Examples

>>> from galactic.algebras.set.core import FrozenFIFOSet
>>> a = FrozenFIFOSet[int]([3, 5, 1, 2, 6, 2])
>>> a
<galactic.algebras.set.core.FrozenFIFOSet object at 0x...>
>>> list(a)
[3, 5, 1, 2, 6]
>>> list(a | FrozenFIFOSet[int]([1, 5, 7 ,8, 9]))
[3, 5, 1, 2, 6, 7, 8, 9]

Notes

  • This class is a frozen version of FIFOSet. It is immutable and does not allow any modifications after creation. It is useful for creating immutable sets that maintain the order of insertion, which can be used in contexts where immutability is required, such as in functional programming paradigms or when using sets as keys in dictionaries.

  • It is based on collections.OrderedDict to maintain the order of insertion while providing set-like behavior.

  • The class implements the collections.abc.Set interface, allowing it to be used as a set in Python. It provides methods for checking membership, iterating over the elements, and performing set operations like union, intersection, difference, and symmetric difference.

__hash__()#

Compute the hash value of the set.

Returns:

The hash value of the set

Return type:

int

Notes

  • The hash value is computed based on the elements of the set and is cached for efficiency.

__copy__()#

Create a shallow copy of this set.

Returns:

A shallow copy of this set

Return type:

Self

copy()#

Compute a copy of this set.

Returns:

A copy of this set

Return type:

Self

__contains__(elem)#

Check if an element belongs to the set.

Parameters:

elem (object) – The element to check

Returns:

True if the element belongs to the set, False otherwise

Return type:

bool

__iter__()#

Iterate over the elements in the set.

Returns:

An iterator over the elements in the set

Return type:

Iterator[TypeVar(_T)]

__len__()#

Get the number of elements in the set.

Returns:

The number of elements in the set

Return type:

int

property head: _T#

Get the first element of the set.

Returns:

The first element

Return type:

_T

Raises:

KeyError – If the set is empty

property tail: _T#

Get the last element of the set.

Returns:

The last element

Return type:

_T

Raises:

KeyError – If the set is empty

__reversed__()#

Iterate over the elements in the set in reverse order.

Returns:

An iterator over the elements in reverse order

Return type:

Iterator[TypeVar(_T)]

isdisjoint(other)#

Check if the set has no elements in common with another iterable.

Parameters:

other (Iterable[Any]) – An iterable to compare

Returns:

True if the set is disjoint with the other iterable, False otherwise

Return type:

bool

issubset(other)#

Check if the set is a subset of another iterable.

Parameters:

other (Iterable[Any]) – An iterable to compare

Returns:

True if the set is a subset, False otherwise

Return type:

bool

issuperset(other)#

Check if the set is a superset of another iterable.

Parameters:

other (Iterable[Any]) – An iterable to compare

Returns:

True if the set is a superset, False otherwise

Return type:

bool

union(*others)#

Compute the union of the set and the provided iterables.

Parameters:

*others (Iterable[TypeVar(_T)]) – A sequence of iterables

Returns:

The union of the set and the provided iterables

Return type:

Self

intersection(*others)#

Compute the intersection of the set and the provided iterables.

Parameters:

*others (Iterable[Any]) – A sequence of iterables

Returns:

The intersection of the set and the provided iterables

Return type:

Self

difference(*others)#

Compute the difference between the set and the provided iterables.

Parameters:

*others (Iterable[Any]) – A sequence of iterables

Returns:

The difference between the set and the provided iterables

Return type:

Self

symmetric_difference(other)#

Compute the symmetric difference between the set and the provided iterable.

Parameters:

other (Iterable[TypeVar(_T)]) – An iterable of elements

Returns:

The symmetric difference between the set and the provided iterable

Return type:

Self

class FIFOSet(*elements)#

Bases: FrozenFIFOSet[_T], MutableSet[_T]

Represents a mutable set that preserves FIFO order.

Examples

>>> from galactic.algebras.set.core import FIFOSet
>>> a = FIFOSet[int]([3, 5, 1, 2, 6, 2])
>>> a
<galactic.algebras.set.core.FIFOSet object at 0x...>
>>> list(a)
[3, 5, 1, 2, 6]
>>> list(a | FIFOSet[int]([1, 5, 7 ,8, 9]))
[3, 5, 1, 2, 6, 7, 8, 9]

Notes

  • This class is a mutable version of FrozenFIFOSet. It allows adding, removing, and modifying elements while preserving the order of insertion.

  • It is based on collections.OrderedDict to maintain the order of insertion while providing set-like behavior.

  • The class implements the collections.abc.MutableSet interface, allowing it to be used as a mutable set in Python. It provides methods for adding, removing, and modifying elements, as well as performing set operations like union, intersection, difference, and symmetric difference.

  • It also provides methods for moving elements to the beginning or end of the set, allowing for flexible manipulation of the order of elements.

  • It is useful for scenarios where the order of insertion matters, such as in queues or ordered sets, while still providing the functionality of a set.

  • It is particularly useful in applications where the order of elements needs to be preserved, such as in task scheduling, event handling, or maintaining a history of actions.

  • It is also useful in scenarios where elements need to be added or removed frequently while maintaining the order of insertion, such as in caching mechanisms or maintaining a list of recently used items.

add(elem)#

Add an element to the set.

Parameters:

elem (TypeVar(_T)) – The element to add

Return type:

None

remove(elem)#

Remove an element from the set if it exists.

Parameters:

elem (TypeVar(_T)) – The element to remove

Return type:

None

discard(elem)#

Discard an element from the set.

Parameters:

elem (TypeVar(_T)) – The element to discard

Return type:

None

pop()#

Remove and return the first element from the set.

Returns:

The removed element

Return type:

TypeVar(_T)

clear()#

Remove all elements from the set.

Return type:

None

update(*others)#

Update a set with the union of itself and others.

Parameters:

*others (Iterable[TypeVar(_T)]) – A sequence of iterable

Return type:

None

intersection_update(*others)#

Update a set with the intersection of itself and others.

Parameters:

*others (Iterable[Any]) – A sequence of iterable

Return type:

None

difference_update(*others)#

Update a set with the difference of itself and others.

Parameters:

*others (Iterable[Any]) – A sequence of iterable

Return type:

None

symmetric_difference_update(other)#

Update a set with the symmetric difference of itself and the other.

Parameters:

other (Iterable[TypeVar(_T)]) – An iterable of elements

Return type:

None

move_to_end(elem, last=True)#

Move an existing element to the beginning or end of the set.

Parameters:
  • elem (TypeVar(_T)) – The element to move

  • last (bool, default: True) – If True, move to the end; otherwise, move to the beginning

Return type:

None

additem(elem, last=True)#

Add an element to the set at the beginning or end.

Parameters:
  • elem (TypeVar(_T)) – The element to add

  • last (bool, default: True) – If True, add to the end; otherwise, add to the beginning

Return type:

None

popitem(last=False)#

Remove and return an element from the beginning or end of the set.

Parameters:

last (bool, default: False) – If True, remove from the end; otherwise, remove from the beginning

Returns:

The removed element

Return type:

TypeVar(_T)

Raises:

KeyError – If the set is empty

class IndexRange(length=0)#

Bases: Sequence[int]

Represents a continuous universe of integers starting at 0.

Parameters:

length (int, default: 0) – The length of the universe

Examples

>>> from galactic.algebras.set.core import IndexRange
>>> universe = IndexRange(10)
>>> universe
<galactic.algebras.set.core.IndexRange object at 0x...>
>>> list(universe)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> list(reversed(universe))
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
>>> universe.index(2)
2
>>> universe.count(2)
1
>>> universe.count(10)
0

Notes

  • The IndexRange class represents a universe of integers starting from 0 up to a specified length.

  • It supports basic collection operations like containment, length, and iteration.

  • The universe can be indexed using integers or slices, and it raises an IndexError if an index is out of range.

  • The index method returns the index of an element, and the count method returns the number of occurrences of an element in the universe.

Hint

The IndexRange class is useful for representing sets of integers starting from 0, allowing for efficient membership testing and set operations.

__hash__()#

Get the hash of the universe.

Returns:

The hash of the universe

Return type:

int

Notes

The hash is computed based on the length of the universe.

__contains__(elem)#

Test if an element is in the universe.

Parameters:

elem (object) – The element to test

Returns:

True if the element is in the universe, else False

Return type:

bool

__iter__()#

Get an iterator over the universe.

Returns:

An iterator over the universe

Return type:

Iterator[int]

__len__()#

Get the length of the universe.

Returns:

The length of the universe

Return type:

int

__reversed__()#

Get a reversed iterator over the universe.

Returns:

A reversed iterator over the universe

Return type:

Iterator[int]

__getitem__(index)#
Overloads:
  • self, index (int) → int

  • self, index (slice) → Sequence[int]

Get an element or a slice from the universe.

Parameters:

index (int | slice) – The index or slice to get

Returns:

The element or slice requested

Return type:

int | Sequence[int]

Raises:
  • IndexError – If the index is out of range

  • TypeError – If the index is not an integer or a slice

index(elem, start=None, stop=None)#

Get the index of an element.

Parameters:
  • elem (int) – The element whose index is requested

  • start (int | None, default: None) – Where to start

  • stop (int | None, default: None) – Where to stop

Returns:

The index requested

Return type:

int

Raises:

ValueError – If the element is not in the universe or not found in the interval

count(elem)#

Get the total number of occurrences of the element.

Parameters:

elem (int) – The element whose count is requested

Returns:

1 if the element is in the universe, else 0

Return type:

int

class Universe(*elements)#

Bases: Sequence[_T]

Represents a universe of elements.

Parameters:

*elements (Iterable[TypeVar(_T)]) – Elements of the universe

Examples

>>> from galactic.algebras.set.core import Universe
>>> universe = Universe[str]("abc")
>>> universe
<galactic.algebras.set.core.Universe object at 0x...>
>>> list(universe)
['a', 'b', 'c']
>>> list(reversed(universe))
['c', 'b', 'a']
>>> universe.index('c')
2
>>> universe.count('c')
1
>>> universe.count('g')
0

Notes

  • The universe is a sequence of elements that can be indexed and iterated over.

  • The elements are stored in a tuple, and an index is maintained for fast lookups.

  • The universe is immutable after creation, and its elements are unique.

  • The universe can be used to create subsets using the SubSet class.

  • The universe supports basic collection operations like containment, length, and iteration.

Hint

The Universe class is useful for representing a set of elements that can be indexed and iterated over, allowing for efficient membership testing and set operations.

__hash__()#

Get the hash of the universe.

Returns:

The hash of the universe

Return type:

int

Notes

The hash is computed based on the elements of the universe. It is cached after the first computation for efficiency.

__contains__(elem)#

Test if an element is in the universe.

Parameters:

elem (object) – The element to test

Returns:

True if the element is in the universe, else False

Return type:

bool

__iter__()#

Get an iterator over the universe.

Returns:

An iterator over the universe

Return type:

Iterator[TypeVar(_T)]

__len__()#

Get the length of the universe.

Returns:

The length of the universe

Return type:

int

__reversed__()#

Get a reversed iterator over the universe.

Returns:

A reversed iterator over the universe

Return type:

Iterator[TypeVar(_T)]

__getitem__(index)#
Overloads:
  • self, index (int) → _T

  • self, index (slice) → Sequence[_T]

Get an element or a slice from the universe.

Parameters:

index (int | slice) – The index or slice to get

Returns:

The element or slice requested

Return type:

_T | Sequence[_T]

index(elem, start=None, stop=None)#

Get the index of an element.

Parameters:
  • elem (TypeVar(_T)) – The element whose index is requested

  • start (int | None, default: None) – Where to start

  • stop (int | None, default: None) – Where to stop

Returns:

The index requested

Return type:

int

Raises:

ValueError – If the element is not in the universe or not found in the interval

count(elem)#

Get the total number of occurrences of the element.

Parameters:

elem (TypeVar(_T)) – The element whose count is requested

Returns:

1 if the element is in the universe, else 0

Return type:

int

class SubSet(universe, *elements, bitmap=None)#

Bases: Generic[_T]

Represents a subset of a universe.

Membership is stored using a compact bitmap.

Parameters:
  • universe (Sequence[TypeVar(_T)]) – The universe

  • *elements (Iterable[TypeVar(_T)]) – Elements to include in the subset

  • bitmap (AbstractBitMap | None, default: None) – Bitmap representing the subset

Examples

>>> from galactic.algebras.set.core import Universe, SubSet
>>> from pprint import pprint
>>> universe = Universe[str]("abcdef")
>>> universe
<galactic.algebras.set.core.Universe object at 0x...>
>>> set1 = SubSet[str](universe, "abde")
>>> set1
<galactic.algebras.set.core.SubSet object at 0x...>
>>> list(set1.universe)
['a', 'b', 'c', 'd', 'e', 'f']
>>> len(set1)
4
>>> "a" in set1
True
>>> "f" in set1
False
>>> list(~set1)
['c', 'f']
>>> set2 = SubSet[str](universe, "bdef")
>>> set2
<galactic.algebras.set.core.SubSet object at 0x...>
>>> list(set2.bitmap)
[1, 3, 4, 5]
>>> list(set1 | set2)
['a', 'b', 'd', 'e', 'f']
>>> list((set1 | set2).bitmap)
[0, 1, 3, 4, 5]
>>> list(set1 & set2)
['b', 'd', 'e']
>>> list(set1 ^ set2)
['a', 'f']
>>> set1 & set2 <= set1
True
>>> set1 | set2 >= set1
True
>>> set1.isdisjoint(set2)
False
>>> pprint([list(subset) for subset in set1.subsets()])
[[],
 ['a'],
 ['b'],
 ['d'],
 ['e'],
 ['a', 'b'],
 ['a', 'd'],
 ['a', 'e'],
 ['b', 'd'],
 ['b', 'e'],
 ['d', 'e'],
 ['a', 'b', 'd'],
 ['a', 'b', 'e'],
 ['a', 'd', 'e'],
 ['b', 'd', 'e'],
 ['a', 'b', 'd', 'e']]
>>> pprint([list(superset) for superset in set1.supersets(strict=True)])
[['a', 'b', 'c', 'd', 'e'],
 ['a', 'b', 'd', 'e', 'f'],
 ['a', 'b', 'c', 'd', 'e', 'f']]

Notes

  • The SubSet class represents a subset of a universe.

  • It uses a bitmap to efficiently store membership information.

  • The universe is a sequence of elements, and the subset can be created from elements or an existing bitmap.

  • The subset supports basic set operations like union, intersection, difference, and symmetric difference.

  • It also supports set comparison operations like subset, superset, and disjointness.

  • The subset can be iterated over, and it provides methods to generate all subsets and supersets.

  • The SubSet class is generic and can be used with any type of elements.

  • The universe must be a sequence, and the elements must be present in the universe.

Hint

The SubSet class is useful for representing sets of elements from a larger universe, allowing for efficient membership testing and set operations.

__hash__()#

Get the hash of the subset.

Returns:

The hash of the subset

Return type:

int

Notes

The hash is computed based on the universe and the bitmap representation. It is cached after the first computation for efficiency.

__eq__(other)#

Test for equality between this set and another.

Parameters:

other (object) – Another subset

Returns:

True if the sets are equal

Return type:

bool

__contains__(elem)#

Test if an element is in the set.

Parameters:

elem (object) – The element to test

Returns:

True if the element is in the set, else False

Return type:

bool

__iter__()#

Get an iterator over the set.

Returns:

An iterator over the set

Return type:

Iterator[TypeVar(_T)]

__len__()#

Get the length of the set.

Returns:

The length of the set

Return type:

int

__ne__(other)#

Test for inequality between this set and another.

Parameters:

other (object) – Another subset

Returns:

True if the sets are not equal

Return type:

bool

__lt__(other)#

Test if the set is less than the other.

Parameters:

other (object) – Another subset

Returns:

True if the set is less than the other

Return type:

bool

Raises:

ValueError – If the universes of the sets are different

Notes

If other is not an instance of self type, this method returns NotImplemented.

__le__(other)#

Test if the set is less than or equal to the other.

Parameters:

other (object) – Another subset

Returns:

True if the set is less than or equal to the other

Return type:

bool

Raises:

ValueError – If the universes of the sets are different

Notes

If other is not an instance of self type, this method returns NotImplemented.

__gt__(other)#

Test if the set is greater than the other.

Parameters:

other (object) – Another subset

Returns:

True if the set is greater than the other

Return type:

bool

Raises:

ValueError – If the universes of the sets are different

Notes

If other is not an instance of self type, this method returns NotImplemented.

__ge__(other)#

Test if the set is greater than or equal to the other.

Parameters:

other (Self) – Another subset

Returns:

True if the set is greater than or equal to the other

Return type:

bool

Raises:

ValueError – If the universes of the sets are different

Notes

If other is not an instance of self type, this method returns NotImplemented.

isdisjoint(other)#

Test if the set is disjoint from the other.

Parameters:

other (Self) – Another subset

Returns:

True if the set is disjoint from the other

Return type:

bool

Raises:
  • ValueError – If the universes of the sets are different

  • TypeError – If other is not an instance of self type

issubset(other)#

Test if the set is a subset of the other.

Parameters:

other (Self) – Another subset

Returns:

True if the set is a subset of the other

Return type:

bool

Raises:
  • ValueError – If the universes of the sets are different

  • TypeError – If other is not an instance of self type

issuperset(other)#

Test if the set is a superset of the other.

Parameters:

other (Self) – Another subset

Returns:

True if the set is a superset of the other.

Return type:

bool

Raises:
  • ValueError – If the universes of the sets are different

  • TypeError – If other is not an instance of self type

__or__(other)#

Compute the union of the set and the other.

Parameters:

other (object) – Another subset

Returns:

The union of the set and the other

Return type:

Self

Notes

If other is not an instance of self type, this method returns NotImplemented.

__and__(other)#

Compute the intersection between the set and the other.

Parameters:

other (object) – Another subset

Returns:

The intersection between the set and the other

Return type:

Self

Raises:

ValueError – If the universes of the sets are different

Notes

If other is not an instance of self type, this method returns NotImplemented.

__sub__(other)#

Compute the difference between the set and the other.

Parameters:

other (object) – Another subset

Returns:

The difference between the set and the other

Return type:

Self

Raises:

ValueError – If the universes of the sets are different

Notes

If other is not an instance of self type, this method returns NotImplemented.

__xor__(other)#

Compute the symmetric difference between the set and the other.

Parameters:

other (object) – Another subset

Returns:

The symmetric difference between the set and the other

Return type:

Self

Raises:

ValueError – If the universes of the sets are different

Notes

If other is not an instance of self type, this method returns NotImplemented.

__invert__()#

Compute the complement of the set.

Returns:

The complement of the set

Return type:

Self

union(*others)#

Compute the union of the set and the others.

Parameters:

*others (Self) – A sequence of subsets

Returns:

The union of the set and the others

Return type:

Self

Raises:
  • ValueError – If the universes of the sets are different

  • TypeError – If any other is not an instance of self type

intersection(*others)#

Compute the intersection between the set and the others.

Parameters:

*others (Self) – A sequence of iterable

Returns:

The intersection between the set and the others

Return type:

Self

Raises:
  • ValueError – If the universes of the sets are different

  • TypeError – If any other is not an instance of self type

difference(*others)#

Compute the difference between the set and the others.

Parameters:

*others (Self) – A sequence of iterable

Returns:

The difference between the set and the others

Return type:

Self

Raises:
  • ValueError – If the universes of the sets are different

  • TypeError – If any other is not an instance of self type

symmetric_difference(other)#

Compute the symmetric difference between the set and the other.

Parameters:

other (Self) – An iterable of elements

Returns:

The symmetric difference between the set and the other

Return type:

Self

Raises:
  • ValueError – If the universes of the sets are different

  • TypeError – If any other is not an instance of self type

subsets(strict=None)#

Get an iterator over the subset of this set (including itself).

Parameters:

strict (bool | None, default: None) – Are the subsets strict?

Returns:

An iterator over the subsets

Return type:

Iterator[Self]

supersets(strict=None)#

Get an iterator over the supersets of this set (including itself).

Parameters:

strict (bool | None, default: None) – Are the subsets strict?

Returns:

An iterator over the supersets

Return type:

Iterator[Self]

property bitmap: AbstractBitMap#

Get the underlying bitmap.

Returns:

The underlying bitmap

Return type:

AbstractBitMap

property universe: Sequence[_T]#

Get the underlying universe.

Returns:

The underlying universe

Return type:

Sequence[_T]