Heap queue (priority queue) implementation.
Python Makefile
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
heapqueue
tests
.gitignore
.travis.yml
LICENSE
MANIFEST.in
Makefile
README.rst
setup.cfg
setup.py

README.rst

heapqueue

Travis CI Build Latest PyPI Version

Heap queue (priority queue) implementation.

How to

Install

pip install heapqueue

Import and init an empty heap queue

from heapqueue import BinaryHeap

queue = BinaryHeap()

# BinaryHeap can accept two custom parameters
# comp: a function that indicates how to compare two elements
#     return -1 if a < b, 1 if a > b, and 0 if equal
# size: fixed size of heap queue to truncate results
queue = BinaryHeap(comp=lambda a, b: a['val'] - b['val'], size=3)

Insert elements

# primitive data types
queue.push(9)
queue.push('hello')

# objects
# note: `comp` is necessary if you want to push objects
queue.push({'foo': 'a', 'bar': 1})
queue.push({'foo': 'b', 'bar': 3})

Get min/max elements

# delete and return the top(max/min) one
queue.pop()

# return a sorted list according to `comp` and `size`
# note: this is a property, not a method
queue.sorted_list

# BinaryHeap can handle iteration as well
for item in queue:
    print(item)

# you can also just have a look at top one without deleting it
queue.peek()

Remove elements

# remove item equals to 12
queue.remove(12)

# remove item using `comp` for objects
queue.remove({'foo': 'a', 'bar': 1})

License

Code and documentation are available according to the MIT License (see LICENSE file).