Skip to content

mingmingrr/pyrsistent-extras

Repository files navigation

Pyrsistent-Extras

docs-badge coverage-badge tests-badge mypy-badge pypi-badge

Extra data structures for pyrsistent

Below are examples of common usage patterns for some of the structures and features. More information and full documentation for all data structures is available in the documentation.

PSequence

Persistent sequences implemented with finger trees, with O(\log{n}) merge/split/lookup and O(1) access at both ends.

>>> from pyrsistent_extras import psequence
>>> seq1 = psequence([1, 2, 3])
>>> seq1
psequence([1, 2, 3])
>>> seq2 = seq1.append(4)
>>> seq2
psequence([1, 2, 3, 4])
>>> seq3 = seq1 + seq2
>>> seq3
psequence([1, 2, 3, 1, 2, 3, 4])
>>> seq1
psequence([1, 2, 3])
>>> seq3[3]
1
>>> seq3[2:5]
psequence([3, 1, 2])
>>> seq1.set(1, 99)
psequence([1, 99, 3])

PHeap

Persistent heaps implemented with binomial heaps, with O(1) findMin/insert and O(\log{n}) merge/deleteMin. Comes in two flavors: PMinHeap and PMaxHeap.

>>> from pyrsistent_extras import pminheap
>>> heap1 = pminheap([(1,'a'), (2,'b')])
>>> heap1
pminheap([(1, 'a'), (2, 'b')])
>>> heap2 = heap1.push(3,'c')
>>> heap2
pminheap([(1, 'a'), (2, 'b'), (3, 'c')])
>>> heap3 = heap1 + heap2
>>> heap3
pminheap([(1, 'a'), (1, 'a'), (2, 'b'), (2, 'b'), (3, 'c')])
>>> key, value, heap4 = heap3.pop()
>>> (key, value)
(1, 'a')
>>> heap4
pminheap([(1, 'a'), (2, 'b'), (2, 'b'), (3, 'c')])