PHeap¶
PMinHeap¶
- pyrsistent_extras._pheap.pminheap(items: Union[PHeap[K, V], Iterable[Tuple[K, V]]] = pminheap([])) PMinHeap[K, V][source]¶
Create a
PMinHeapfrom the given items\(O(n)\)
>>> pminheap() pminheap([]) >>> pminheap([(1,'a'), (2,'b'), (3,'c')]) pminheap([(1, 'a'), (2, 'b'), (3, 'c')])
- pyrsistent_extras._pheap.pminheap.fromkeys(items: Iterable[K], value: Optional[V] = None) PMinHeap[K, V]¶
Create a
PMinHeapusing a default value\(O(n)\)
>>> pminheap.fromkeys([1, 2, 3]) pminheap([(1, None), (2, None), (3, None)]) >>> pminheap.fromkeys([1, 2, 3], value='a') pminheap([(1, 'a'), (2, 'a'), (3, 'a')])
- pyrsistent_extras._pheap.hl(*items: Tuple[K, V]) PMinHeap[K, V][source]¶
Shorthand for
pminheap()Mnemonic: Heap Less-than
>>> hl((1,'a'), (2,'b'), (3,'c')) pminheap([(1, 'a'), (2, 'b'), (3, 'c')])
- class pyrsistent_extras._pheap.PMinHeap[source]¶
Persistent heap
Meant for cases where a priority queue is needed.
Do not instantiate directly, instead use the factory functions
hl()orpminheap()to create a min-heap, andhg()orpmaxheap()to create a max-heap.The
PMinHeapandPMaxHeapimplements thetyping.Containerprotocol and istyping.Hashable.The implementation is a binomial heap. Merge/pop operations are \(O(\log{n})\), and peek/push operations are \(O(1)\). Operations are not stable: values pushed with the same key may be popped in a different order.
The following are examples of some common operations on persistent heaps:
>>> 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')])
- __add__(other: Union[PHeap[K, V], Iterable[Tuple[K, V]]]) PHeap[K, V]¶
Return the union of the two heaps
amortized \(O(\log(\min(n,m)))\), worst-case \(O(\log(\max(n,m)))\)
>>> pminheap([(1,'a'), (3,'c')]) + pminheap([(2,'b'), (4,'d')]) pminheap([(1, 'a'), (2, 'b'), (3, 'c'), (4, 'd')]) >>> pmaxheap([(1,'a'), (3,'c')]) + pmaxheap([(2,'b'), (4,'d')]) pmaxheap([(4, 'd'), (3, 'c'), (2, 'b'), (1, 'a')])
- __eq__(other) bool¶
Return self == other
\(O(n\log{n})\) if values are comparable
\(O(n\log{n}+g^2)\) if values are not comparable, where g is the number of values with the same key
>>> xs = pminheap([(1,'a'), (2,'b'), (3,'c')]) >>> xs == pminheap([(1,'a'), (2,'b'), (3,'c')]) True >>> xs == pminheap([(2,'b'), (3,'c'), (4,'d')]) False
- Return type
- __ge__(other) bool¶
Return self >= other
\(O(n\log{n})\)
>>> xs = pminheap([(1,'a'), (2,'b'), (3,'c')]) >>> xs >= pminheap([(1,'a'), (2,'b'), (3,'c')]) True >>> xs >= pminheap([(2,'b'), (3,'c'), (4,'d')]) False >>> xs >= pminheap([(0,'z'), (1,'a'), (2,'b')]) True
- __gt__(other) bool¶
Return self > other
\(O(n\log{n})\)
>>> xs = pminheap([(1,'a'), (2,'b'), (3,'c')]) >>> xs > pminheap([(1,'a'), (2,'b'), (3,'c')]) False >>> xs > pminheap([(2,'b'), (3,'c'), (4,'d')]) False >>> xs > pminheap([(0,'z'), (1,'a'), (2,'b')]) True
- __hash__() int¶
Calculate the hash of the heap.
\(O(n)\)
>>> x1 = pminheap([(1,'a'), (2,'b'), (3,'c')]) >>> x2 = pminheap([(1,'a'), (2,'b'), (3,'c')]) >>> hash(x1) == hash(x2) True
- classmethod __init_subclass__(*args, **kwargs)¶
This method is called when a class is subclassed.
The default implementation does nothing. It may be overridden to extend subclasses.
- __iter__() Iterator[K]¶
Iterate through the heap’s keys
\(O(1)\)
Iterating over the entire heap is \(O(n\log{n})\)
See
keys().>>> list(pminheap([(1,'a'), (2,'b'), (3,'c')]).keys()) [1, 2, 3] >>> list(pmaxheap([(1,'a'), (2,'b'), (3,'c')]).keys()) [3, 2, 1]
- Return type
Iterator[K]
- __le__(other) bool¶
Return self <= other
\(O(n\log{n})\)
>>> xs = pminheap([(1,'a'), (2,'b'), (3,'c')]) >>> xs <= pminheap([(1,'a'), (2,'b'), (3,'c')]) True >>> xs <= pminheap([(2,'b'), (3,'c'), (4,'d')]) True >>> xs <= pminheap([(0,'z'), (1,'a'), (2,'b')]) False
- __len__() int¶
Get the size of the heap
\(O(1)\)
>>> len(pminheap([(1,'a'), (2,'b'), (3,'c')])) 3
- Return type
- __lt__(other) bool¶
Return self < other
\(O(n\log{n})\)
>>> xs = pminheap([(1,'a'), (2,'b'), (3,'c')]) >>> xs < pminheap([(1,'a'), (2,'b'), (3,'c')]) False >>> xs < pminheap([(2,'b'), (3,'c'), (4,'d')]) True >>> xs < pminheap([(0,'z'), (1,'a'), (2,'b')]) False
- __ne__(other) bool¶
Return self != other
\(O(n\log{n})\) if values are comparable
\(O(n\log{n}+g^2)\) if values are not comparable, where g is the number of values with the same key
>>> xs = pminheap([(1,'a'), (2,'b'), (3,'c')]) >>> xs != pminheap([(1,'a'), (2,'b'), (3,'c')]) False >>> xs != pminheap([(2,'b'), (3,'c'), (4,'d')]) True
- Return type
- static __new__(cls, _size, _key, _value, _forest)¶
- __reduce__()¶
Support method for
pickle\(O(n)\)
>>> import pickle >>> pickle.loads(pickle.dumps(pminheap([(1,'a'), (2,'b'), (3,'c')]))) pminheap([(1, 'a'), (2, 'b'), (3, 'c')])
- classmethod __subclasshook__(C)¶
Abstract classes can override this to customize issubclass().
This is invoked early on by abc.ABCMeta.__subclasscheck__(). It should return True, False or NotImplemented. If it returns NotImplemented, the normal algorithm is used. Otherwise, it overrides the normal algorithm (and the outcome is cached).
- __weakref__¶
list of weak references to the object (if defined)
- items(sorted: bool = True) PHeapItems[K, V]¶
Create a view of the heap’s items
\(O(1)\)
Iterating over the entire heap is \(O(n\log{n})\) for
sorted=Trueand \(O(n)\) forsorted=False.Similar to
dict.items().See
PHeapView.>>> list(pminheap([(1,'a'), (2,'b'), (3,'c')]).items()) [(1, 'a'), (2, 'b'), (3, 'c')] >>> list(pmaxheap([(1,'a'), (2,'b'), (3,'c')]).items()) [(3, 'c'), (2, 'b'), (1, 'a')]
- Parameters
sorted (bool) –
- Return type
PHeapItems[K, V]
- keys(sorted: bool = True) PHeapKeys[K, V]¶
Create a view of the heap’s keys
\(O(1)\)
Iterating over the entire heap is \(O(n\log{n})\) for
sorted=Trueand \(O(n)\) forsorted=False.Similar to
dict.keys().See
PHeapView.>>> list(pminheap([(1,'a'), (2,'b'), (3,'c')]).keys()) [1, 2, 3] >>> list(pmaxheap([(1,'a'), (2,'b'), (3,'c')]).keys()) [3, 2, 1]
- Parameters
sorted (bool) –
- Return type
PHeapKeys[K, V]
- merge(other: Union[PHeap[K, V], Iterable[Tuple[K, V]]]) PHeap[K, V]¶
Return the union of the two heaps
amortized \(O(\log(\min(n,m)))\), worst-case \(O(\log(\max(n,m)))\)
>>> pminheap([(1,'a'), (3,'c')]) + pminheap([(2,'b'), (4,'d')]) pminheap([(1, 'a'), (2, 'b'), (3, 'c'), (4, 'd')]) >>> pmaxheap([(1,'a'), (3,'c')]) + pmaxheap([(2,'b'), (4,'d')]) pmaxheap([(4, 'd'), (3, 'c'), (2, 'b'), (1, 'a')])
- peek() Tuple[K, V]¶
Find the min/max element
\(O(1)\)
- Raises
IndexError – if the heap is empty
- Return type
Tuple[K, V]
>>> pminheap([(1,'a'), (2,'b'), (3,'c')]).peek() (1, 'a') >>> pmaxheap([(1,'a'), (2,'b'), (3,'c')]).peek() (3, 'c') >>> pminheap().peek() Traceback (most recent call last): ... IndexError: ...
- pop() Tuple[K, V, PHeap[K, V]]¶
Remove and return the min/max item
\(O(\log{n})\)
- Raises
IndexError – if the heap is empty
- Return type
Tuple[K, V, PHeap[K, V]]
>>> pminheap([(1,'a'), (2,'b'), (3,'c')]).pop() (1, 'a', pminheap([(2, 'b'), (3, 'c')])) >>> pmaxheap([(1,'a'), (2,'b'), (3,'c')]).pop() (3, 'c', pmaxheap([(2, 'b'), (1, 'a')])) >>> pminheap([]).pop() Traceback (most recent call last): ... IndexError: ...
- push(key: K, value: Optional[V] = None) PHeap[K, V]¶
Inserts a value with the specified key
amortized \(O(1)\), worst case \(O(\log{n})\)
>>> pminheap([(1,'a'), (2,'b')]).push(3,'c') pminheap([(1, 'a'), (2, 'b'), (3, 'c')]) >>> pmaxheap([(1,'a'), (2,'b')]).push(3,'c') pmaxheap([(3, 'c'), (2, 'b'), (1, 'a')])
- Parameters
key (K) –
value (Optional[V]) –
- Return type
PHeap[K, V]
- values(sorted: bool = True) PHeapValues[K, V]¶
Create a view of the heap’s values
\(O(1)\)
Iterating over the entire heap is \(O(n\log{n})\) for
sorted=Trueand \(O(n)\) forsorted=False.Similar to
dict.values().See
PHeapView.>>> list(pminheap([(1,'a'), (2,'b'), (3,'c')]).values()) ['a', 'b', 'c'] >>> list(pmaxheap([(1,'a'), (2,'b'), (3,'c')]).values()) ['c', 'b', 'a']
- Parameters
sorted (bool) –
- Return type
PHeapValues[K, V]
PMaxHeap¶
- pyrsistent_extras._pheap.pmaxheap(items: Union[PHeap[K, V], Iterable[Tuple[K, V]]] = pmaxheap([])) PMaxHeap[K, V][source]¶
Create a
PMaxHeapfrom the given items\(O(n)\)
>>> pmaxheap() pmaxheap([]) >>> pmaxheap([(1,'a'), (2,'b'), (3,'c')]) pmaxheap([(3, 'c'), (2, 'b'), (1, 'a')])
- pyrsistent_extras._pheap.pmaxheap.fromkeys(items: Iterable[K], value: Optional[V] = None) PMaxHeap[K, V]¶
Create a
PMaxHeapusing a default value\(O(n)\)
>>> pmaxheap.fromkeys([1, 2, 3]) pmaxheap([(3, None), (2, None), (1, None)]) >>> pmaxheap.fromkeys([1, 2, 3], value='a') pmaxheap([(3, 'a'), (2, 'a'), (1, 'a')])
- pyrsistent_extras._pheap.hg(*items: Tuple[K, V]) PMaxHeap[K, V][source]¶
Shorthand for
pmaxheap()Mnemonic: Heap Greater-than
>>> hg((1,'a'), (2,'b'), (3,'c')) pmaxheap([(3, 'c'), (2, 'b'), (1, 'a')])
- class pyrsistent_extras._pheap.PMaxHeap[source]¶
Persistent heap
Meant for cases where a priority queue is needed.
Do not instantiate directly, instead use the factory functions
hl()orpminheap()to create a min-heap, andhg()orpmaxheap()to create a max-heap.The
PMinHeapandPMaxHeapimplements thetyping.Containerprotocol and istyping.Hashable.The implementation is a binomial heap. Merge/pop operations are \(O(\log{n})\), and peek/push operations are \(O(1)\). Operations are not stable: values pushed with the same key may be popped in a different order.
The following are examples of some common operations on persistent heaps:
>>> 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')])
- __add__(other: Union[PHeap[K, V], Iterable[Tuple[K, V]]]) PHeap[K, V]¶
Return the union of the two heaps
amortized \(O(\log(\min(n,m)))\), worst-case \(O(\log(\max(n,m)))\)
>>> pminheap([(1,'a'), (3,'c')]) + pminheap([(2,'b'), (4,'d')]) pminheap([(1, 'a'), (2, 'b'), (3, 'c'), (4, 'd')]) >>> pmaxheap([(1,'a'), (3,'c')]) + pmaxheap([(2,'b'), (4,'d')]) pmaxheap([(4, 'd'), (3, 'c'), (2, 'b'), (1, 'a')])
- __eq__(other) bool¶
Return self == other
\(O(n\log{n})\) if values are comparable
\(O(n\log{n}+g^2)\) if values are not comparable, where g is the number of values with the same key
>>> xs = pminheap([(1,'a'), (2,'b'), (3,'c')]) >>> xs == pminheap([(1,'a'), (2,'b'), (3,'c')]) True >>> xs == pminheap([(2,'b'), (3,'c'), (4,'d')]) False
- Return type
- __ge__(other) bool¶
Return self >= other
\(O(n\log{n})\)
>>> xs = pminheap([(1,'a'), (2,'b'), (3,'c')]) >>> xs >= pminheap([(1,'a'), (2,'b'), (3,'c')]) True >>> xs >= pminheap([(2,'b'), (3,'c'), (4,'d')]) False >>> xs >= pminheap([(0,'z'), (1,'a'), (2,'b')]) True
- __gt__(other) bool¶
Return self > other
\(O(n\log{n})\)
>>> xs = pminheap([(1,'a'), (2,'b'), (3,'c')]) >>> xs > pminheap([(1,'a'), (2,'b'), (3,'c')]) False >>> xs > pminheap([(2,'b'), (3,'c'), (4,'d')]) False >>> xs > pminheap([(0,'z'), (1,'a'), (2,'b')]) True
- __hash__() int¶
Calculate the hash of the heap.
\(O(n)\)
>>> x1 = pminheap([(1,'a'), (2,'b'), (3,'c')]) >>> x2 = pminheap([(1,'a'), (2,'b'), (3,'c')]) >>> hash(x1) == hash(x2) True
- classmethod __init_subclass__(*args, **kwargs)¶
This method is called when a class is subclassed.
The default implementation does nothing. It may be overridden to extend subclasses.
- __iter__() Iterator[K]¶
Iterate through the heap’s keys
\(O(1)\)
Iterating over the entire heap is \(O(n\log{n})\)
See
keys().>>> list(pminheap([(1,'a'), (2,'b'), (3,'c')]).keys()) [1, 2, 3] >>> list(pmaxheap([(1,'a'), (2,'b'), (3,'c')]).keys()) [3, 2, 1]
- Return type
Iterator[K]
- __le__(other) bool¶
Return self <= other
\(O(n\log{n})\)
>>> xs = pminheap([(1,'a'), (2,'b'), (3,'c')]) >>> xs <= pminheap([(1,'a'), (2,'b'), (3,'c')]) True >>> xs <= pminheap([(2,'b'), (3,'c'), (4,'d')]) True >>> xs <= pminheap([(0,'z'), (1,'a'), (2,'b')]) False
- __len__() int¶
Get the size of the heap
\(O(1)\)
>>> len(pminheap([(1,'a'), (2,'b'), (3,'c')])) 3
- Return type
- __lt__(other) bool¶
Return self < other
\(O(n\log{n})\)
>>> xs = pminheap([(1,'a'), (2,'b'), (3,'c')]) >>> xs < pminheap([(1,'a'), (2,'b'), (3,'c')]) False >>> xs < pminheap([(2,'b'), (3,'c'), (4,'d')]) True >>> xs < pminheap([(0,'z'), (1,'a'), (2,'b')]) False
- __ne__(other) bool¶
Return self != other
\(O(n\log{n})\) if values are comparable
\(O(n\log{n}+g^2)\) if values are not comparable, where g is the number of values with the same key
>>> xs = pminheap([(1,'a'), (2,'b'), (3,'c')]) >>> xs != pminheap([(1,'a'), (2,'b'), (3,'c')]) False >>> xs != pminheap([(2,'b'), (3,'c'), (4,'d')]) True
- Return type
- static __new__(cls, _size, _key, _value, _forest)¶
- __reduce__()¶
Support method for
pickle\(O(n)\)
>>> import pickle >>> pickle.loads(pickle.dumps(pminheap([(1,'a'), (2,'b'), (3,'c')]))) pminheap([(1, 'a'), (2, 'b'), (3, 'c')])
- classmethod __subclasshook__(C)¶
Abstract classes can override this to customize issubclass().
This is invoked early on by abc.ABCMeta.__subclasscheck__(). It should return True, False or NotImplemented. If it returns NotImplemented, the normal algorithm is used. Otherwise, it overrides the normal algorithm (and the outcome is cached).
- __weakref__¶
list of weak references to the object (if defined)
- items(sorted: bool = True) PHeapItems[K, V]¶
Create a view of the heap’s items
\(O(1)\)
Iterating over the entire heap is \(O(n\log{n})\) for
sorted=Trueand \(O(n)\) forsorted=False.Similar to
dict.items().See
PHeapView.>>> list(pminheap([(1,'a'), (2,'b'), (3,'c')]).items()) [(1, 'a'), (2, 'b'), (3, 'c')] >>> list(pmaxheap([(1,'a'), (2,'b'), (3,'c')]).items()) [(3, 'c'), (2, 'b'), (1, 'a')]
- Parameters
sorted (bool) –
- Return type
PHeapItems[K, V]
- keys(sorted: bool = True) PHeapKeys[K, V]¶
Create a view of the heap’s keys
\(O(1)\)
Iterating over the entire heap is \(O(n\log{n})\) for
sorted=Trueand \(O(n)\) forsorted=False.Similar to
dict.keys().See
PHeapView.>>> list(pminheap([(1,'a'), (2,'b'), (3,'c')]).keys()) [1, 2, 3] >>> list(pmaxheap([(1,'a'), (2,'b'), (3,'c')]).keys()) [3, 2, 1]
- Parameters
sorted (bool) –
- Return type
PHeapKeys[K, V]
- merge(other: Union[PHeap[K, V], Iterable[Tuple[K, V]]]) PHeap[K, V]¶
Return the union of the two heaps
amortized \(O(\log(\min(n,m)))\), worst-case \(O(\log(\max(n,m)))\)
>>> pminheap([(1,'a'), (3,'c')]) + pminheap([(2,'b'), (4,'d')]) pminheap([(1, 'a'), (2, 'b'), (3, 'c'), (4, 'd')]) >>> pmaxheap([(1,'a'), (3,'c')]) + pmaxheap([(2,'b'), (4,'d')]) pmaxheap([(4, 'd'), (3, 'c'), (2, 'b'), (1, 'a')])
- peek() Tuple[K, V]¶
Find the min/max element
\(O(1)\)
- Raises
IndexError – if the heap is empty
- Return type
Tuple[K, V]
>>> pminheap([(1,'a'), (2,'b'), (3,'c')]).peek() (1, 'a') >>> pmaxheap([(1,'a'), (2,'b'), (3,'c')]).peek() (3, 'c') >>> pminheap().peek() Traceback (most recent call last): ... IndexError: ...
- pop() Tuple[K, V, PHeap[K, V]]¶
Remove and return the min/max item
\(O(\log{n})\)
- Raises
IndexError – if the heap is empty
- Return type
Tuple[K, V, PHeap[K, V]]
>>> pminheap([(1,'a'), (2,'b'), (3,'c')]).pop() (1, 'a', pminheap([(2, 'b'), (3, 'c')])) >>> pmaxheap([(1,'a'), (2,'b'), (3,'c')]).pop() (3, 'c', pmaxheap([(2, 'b'), (1, 'a')])) >>> pminheap([]).pop() Traceback (most recent call last): ... IndexError: ...
- push(key: K, value: Optional[V] = None) PHeap[K, V]¶
Inserts a value with the specified key
amortized \(O(1)\), worst case \(O(\log{n})\)
>>> pminheap([(1,'a'), (2,'b')]).push(3,'c') pminheap([(1, 'a'), (2, 'b'), (3, 'c')]) >>> pmaxheap([(1,'a'), (2,'b')]).push(3,'c') pmaxheap([(3, 'c'), (2, 'b'), (1, 'a')])
- Parameters
key (K) –
value (Optional[V]) –
- Return type
PHeap[K, V]
- values(sorted: bool = True) PHeapValues[K, V]¶
Create a view of the heap’s values
\(O(1)\)
Iterating over the entire heap is \(O(n\log{n})\) for
sorted=Trueand \(O(n)\) forsorted=False.Similar to
dict.values().See
PHeapView.>>> list(pminheap([(1,'a'), (2,'b'), (3,'c')]).values()) ['a', 'b', 'c'] >>> list(pmaxheap([(1,'a'), (2,'b'), (3,'c')]).values()) ['c', 'b', 'a']
- Parameters
sorted (bool) –
- Return type
PHeapValues[K, V]
Heap Views¶
- class pyrsistent_extras._pheap.PHeapView[source]¶
- __contains__(item)[source]¶
Check if the item is in the heap
\(O(n)\) or \(O(n\log{n})\)
>>> 1 in pminheap([(1,'a'), (2,'b'), (3,'c')]).keys() True >>> 'a' in pminheap([(1,'a'), (2,'b'), (3,'c')]).values() True >>> (1, 'a') in pminheap([(1,'a'), (2,'b'), (3,'c')]).items() True
- abstract __iter__() Iterator[T][source]¶
Iterate through the heap view
\(O(n)\) or \(O(n\log{n})\)
>>> list(pminheap([(1,'a'), (2,'b'), (3,'c')]).keys()) [1, 2, 3] >>> list(pminheap([(1,'a'), (2,'b'), (3,'c')]).values()) ['a', 'b', 'c'] >>> list(pminheap([(1,'a'), (2,'b'), (3,'c')]).items()) [(1, 'a'), (2, 'b'), (3, 'c')]
- Return type
Iterator[T]