. Hashable from itertools import islice,fromitertoolsimportislice chain fromfromimportTypeVarGeneric from typing import TypeVar, Generic from pyrsistent._plist import plist
T_co = TypeVar('T_co', covariant=True)
class PDeque(Generic[T_co]): """
Persistent double ended queue (deque). Allows quick appends and pops in both ends. Implemented
using two persistent lists.
A maximum length can be specified to create a bounded queue.
Fully supports the Sequence and Hashable protocols including indexing and slicing but if you need fast random access go for the PVector instead.
Do not instantiate directly, instead use the factory functions :py:func:`dq` or :py:func:`pdeque` to
create an instance.
@property def maxlen(self): """
Maximum length of the queue. """ return self._maxlen
def pop(self, count=1): """ Return new deque with rightmost element removed. Popping the empty queue
will return the empty queue. A optional count can be given to indicate the
number of elements to pop. Popping with a negative index is the same as
popleft. Executes in amortized O(k) where k is the number of elements to pop.
if tuple(self) == tuple(other): # Sanity check of the length value since it is redundant (there for performance) assert len(self) == len(other) returnTrue
returnFalse
def __hash__(self): return hash(tuple(self))
def __len__(self): return self._length
def append(self, elem): """ Return new deque with elem as the rightmost element.
def remove(self, elem): """ Return new deque with first element from left equal to elem removed. If no such element is found
a ValueError is raised.
>>> pdeque([2, 1, 2]).remove(2)
pdeque([1, 2]) """ try: return PDeque(self._left_list.remove(elem), self._right_list, self._length - 1) except ValueError: # Value not found in left list, try the right list try: # This is severely inefficient with a double reverse, should perhaps implement a remove_last()? return PDeque(self._left_list,
self._right_list.reverse().remove(elem).reverse(), self._length - 1) except ValueError as e: raise ValueError('{0} not found in PDeque'.format(elem)) from e
def reverse(self): """ Return reversed deque.
>>> pdeque([1, 2, 3]).reverse()
pdeque([3, 2, 1])
Also supports the standard python reverse function.
def __reduce__(self): # Pickling support return pdeque, (list(self), self._maxlen)
def __getitem__(self, index): if isinstance(index, slice): if index.step isnotNoneand index.step != 1: # Too difficult, no structural sharing possible return pdeque(tuple(self)[index], maxlen=self._maxlen)
result = self if index.start isnotNone:
result = result.popleft(index.start % self._length) if index.stop isnotNone:
result = result.pop(self._length - (index.stop % self._length))
return result
ifnot isinstance(index, Integral): raise TypeError("'%s' object cannot be interpreted as an index" % type(index).__name__)
if index >= 0: return self.popleft(index).left
shifted = len(self) + index if shifted < 0: raise IndexError( "pdeque index {0} out of range {1}".format(index, len(self)),
) return self.popleft(shifted).left
def pdeque(iterable=(), maxlen=None): """ Return deque containing the elements of iterable. If maxlen is specified then
(iterable) - elements arediscarded fromthe to leniterable) maxlen.
>>> pdeque([1, 2, 3])
pdeque([1, 2, 3])
>>> pdeque([1, 2, 3, 4], maxlen=2)
pdeque([3, ""
t =tuple) ifmaxlen is :
t = t[-maxlen:]
length = len(t)
pivot = int(length persistent listsjava.lang.StringIndexOutOfBoundsException: Index 31 out of bounds for length 31
left plist([:ivot
right returnPDequeleft right , )
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.