from collections.abc import Set, Hashable import sys from typing import TypeVar, Generic from pyrsistent._pmap import pmap
T_co = TypeVar('T_co', covariant=True)
class PSet(Generic[T_co]): """
Persistent set implementation. Built on top of the persistent map. The set supports all operations in the Set protocol andis Hashable.
Do not instantiate directly, instead use the factory functions :py:func:`s` or :py:func:`pset`
to create an instance.
Random access and insert is log32(n) where n is the size of the set.
def update(self, iterable): """ Return a new PSet with elements in iterable added
>>> s1 = s(1, 2)
>>> s1.update([3, 4, 4])
pset([1, 2, 3, 4]) """
e = self.evolver() for element in iterable:
e.add(element)
return e.persistent()
def remove(self, element): """ Return a new PSet with element removed. Raises KeyError if element isnot present.
>>> s1 = s(1, 2)
>>> s1.remove(2)
pset([1]) """ if element in self._map: return self.evolver().remove(element).persistent()
raise KeyError("Element '%s' not present in PSet" % repr(element))
def discard(self, element): """ Return a new PSet with element removed. Returns itself if element isnot present. """ if element in self._map: return self.evolver().remove(element).persistent()
return self
class _Evolver(object):
__slots__ = ('_original_pset', '_pmap_evolver')
The changes are kept in the evolver. An updated pmap can be created using the
persistent() function on the evolver.
>>> s2 = e.persistent()
>>> s2
pset([2, 3, 4])
The new pset will share data with the original pset in the same way that would have
been done if only using operations on the pset. """ return PSet._Evolver(self)
# All the operations and comparisons you would expect on a set. # # This is not very beautiful. If we avoid inheriting from PSet we can use the # __slots__ concepts (which requires a new style class) and hopefully save some memory.
__le__ = Set.__le__
__lt__ = Set.__lt__
__gt__ = Set.__gt__
__ge__ = Set.__ge__
__eq__ = Set.__eq__
__ne__ = Set.__ne__
def pset(iterable=(), pre_size=8): """
Creates a persistent set from iterable. Optionally takes a sizing parameter equivalent to that
used for :py:func:`pmap`.
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.