Skip to main content

A clean priority queue library with several types and utility helpers.

Project description

PriorityQ is a library for managing a priority queue (PQ) with a cleaner API to enable custom comparators, finding references to values efficiently (in constant time) and deleting values from the PQ. This was developed because the current heapq module (in python’s standard library) does not provide an efficient find operation (it is O(n)) and has no easy way to deleting an element and ensuring the heap invariant afterwards.

Features

  • O(1) finding of elements

  • Deletion of elements possible (in O(log n)).

  • Adjusting of the priority of an element without requiring a deletion followed by an insertion.

  • Opaque handles to elements that can be used to reference to the same item again.

  • Duplicate elements allowed.

  • Custom comparator function can be passed to the PQ itself instead of needing to implement __cmp__.

It is simple to use

To create a PQ simply do:

# A simple object with a comparator
class Item(object):
    def __init__(self, value):
        self.value = value
    def __cmp__(self, another):
        return cmp(self.value, another.value)

from priorityq import PQ
pq = PQ()
pq.heapify([Item(r) for r in [1, 10, 2, 20, 4, 7, 9, 3, 5, 6]])

print list(pq)

# Should print:
# 1 2 3 4 5 6 7 9 10 20

handle_10 = pq.find(25)   #   Happens in O(1)

handle_10.value = 12      #   Modify its value - O(log n)
pq.adjust(handle_10)      #   Indicate to the heap to reprioritise/adjust it

print list(pq)

# Should print:
# 1 2 3 4 5 6 7 9 20 25

handle_10.value = 10      #   Modify its value using the same opaque handle as before
pq.adjust(handle_10)      #   Indicate to the heap to reprioritise/adjust it

print list(pq)

# Should print:
# 1 2 3 4 5 6 7 9 10 20

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

priorityq-0.0.5.tar.gz (6.6 kB view details)

Uploaded Source

Built Distribution

priorityq-0.0.5-py2.py3-none-any.whl (13.0 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file priorityq-0.0.5.tar.gz.

File metadata

  • Download URL: priorityq-0.0.5.tar.gz
  • Upload date:
  • Size: 6.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for priorityq-0.0.5.tar.gz
Algorithm Hash digest
SHA256 617eb9d65d97aca94541b2de13acebdcb1f8d8c269e22a46e6bab076a55b10fd
MD5 360bdb6c86f055a181effa4a94612f1e
BLAKE2b-256 1fbc12c0c42954cd3ce1961608259f1dc22620d48d435d0999dfe8ebecbc1c48

See more details on using hashes here.

File details

Details for the file priorityq-0.0.5-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for priorityq-0.0.5-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 cd9ac54f5cec3c67bbbdf19b15ae0bc653332a31459ad2c8078b0ef38eea5961
MD5 d257471fc5407eff51f2a21b77ceae9f
BLAKE2b-256 cb7b99cbb97d3e3138b83f645c78ae582cbfaba1b43481feff22210d1222ec49

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page