XHEAP
It’s like heapq (blazingly fast) but object-oriented + more features.
Read more here for the background story.
Why?
Less code.
What are heaps good for anyway?
When you need the smallest item of a large list—fast and with no overhead.
How?
Let’s suppose you have a heap, you can use pop to get its smallest item.
from xheap import Heap
heap = Heap(['H', 'D', 'B', 'A', 'E', 'C', 'L', 'J', 'I'])
heap.pop() # returns A
heap.pop() # returns B
heap.pop() # returns C
heap.pop() # returns D
Heapsort works this way.
Can I insert an item?
Indeed and it’s as fast as pop. Use push for insertion.
heap = Heap(['A', 'D', 'B', 'H', 'E', 'C', 'L', 'J', 'I'])
heap.push('Z')
Can I remove an item from the middle of a heap?
Yes, that’s what RemovalHeap.remove is supposed to do.
from xheap import RemovalHeap
heap = RemovalHeap(['A', 'D', 'B', 'H', 'E', 'C', 'L', 'J', 'I'])
heap.remove('L')
Can I specify the order of the heap?
Just imagine two heaps of the very same set of items but you need different sorting for each heap. Or you need a max-heap instead of a min-heap. That is what OrderHeap is designed for:
from xheap import OrderHeap
items = [date(2016, 1, 1), date(2016, 1, 2), date(2016, 1, 3), date(2016, 1, 4)]
day_heap = OrderHeap(items, key=lambda date: date.day)
day_heap.peek() # returns date(2016, 1, 1)
weekday_heap = OrderHeap(items, key=lambda date: date.weekday())
weekday_heap.peek() # returns date(2016, 1, 4)
What about both remove+order?
No problem. Use XHeap.
If you wonder why there are 4 distinct heap implementations, it’s a matter of speed. Each additional feature slows a heap down. Thus, you could always use XHeap but beware of the slowdown.
Checking Heap Invariant
A heap is just a list. So, if you tinker with it, you can check whether its invariant still holds:
heap = Heap([4, 3, 7, 6, 1, 2, 9, 8, 5])
heap[3] = 10 # I know what I am doing here
heap.check_invariant() # but better check... ooops
Conclusion
Good
uses C implementation if available (i.e. fast)
object-oriented
no slowdown if you don’t need more than a simple heap
removal possible
custom orders possible
works with Python2 and Python3
Bad
no drawbacks discovered so far ;-)
needs fix/work:
item wrapper which allows duplicate items
decrease-key+increase-key: another missing use-case of heapq
merge heaps
ideas are welcome :-)
Metadata
Release files for xheap 0.17
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| xheap-0.17.tar.gz | 4.5 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| xheap-0.17-py2.py3-none-any.whl | Python 2, Python 3 | none | any | Details |
Total release size: 8.2 kB
Release files / xheap-0.17.tar.gz
| Download URL | xheap-0.17.tar.gz |
|---|---|
| Size | 4.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
975f63bd2afd2680c62c2507ab8cfd209da151827fa1be8ad1f965407bcf7f1f
|
|
BLAKE2b-256 checksum How to use checksums |
2269c353715fa8548cc0d7624bcd1fa161934065b1dcd747910d8c535f66098c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Release files / xheap-0.17-py2.py3-none-any.whl
| Download URL | xheap-0.17-py2.py3-none-any.whl |
|---|---|
| Size | 3.7 kB |
| Tags | Python 2 Python 3 |
|
SHA-256 checksum How to use checksums |
54245eae71a053abf1cd14afa35798d52469a55f80088dccac94f6ccfe888ee0
|
|
BLAKE2b-256 checksum How to use checksums |
f5d3145fd06676d81e2267b459957762dfbfbb175b5656aa409511d804e890c6
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Security for xheap 0.17
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.