Skip to main content

A list-like implementation of heap/PriorityQueue

Project description

heap_class

list-like implementation of heap/PriorityQueue in Python

Usage

>>> from heap_class import Heap
>>> h = Heap([3, 1, 9, 20], max=True)
>>> h.pop()
20
>>> h.peek()  # same as h[0]
9
>>> h.push(17)  # or h.append(17)
>>> h[0]  # same as h.peek()
17
>>> h[1]  # inefficient, but works
9
>>> y = reversed(h)
>>> y.peek()
1
>>> y  # repr is inefficient, but correct
Heap([1, 3, 9, 17], max=False)
>>> 9 in y
True
>>> y.raw()  # heap structure
[1, 3, 17, 9]
>>> set(y.raw())  # raw helpful for fast casting
{1, 3, 17, 9}

Complex entries and *args creation supported.

>>> h = Heap((6,4), (6,9), (10,2), max=True)
>>> h.pop()
(10, 2)
>>> h.pop()
(6, 9)

This is rather hard in heapq because of the different
forms of negation.  Easy here.

>>> h = Heap(('aa', 4), ('aa', 5), ('zz', 2), ('zz', 1), max=True)
>>> h.pop()
('zz', 2)

Custom keys are supported:

>>> vals = [('Adam', 'Smith'), ('Zeta', 'Jones')]
>>> h = Heap(vals, key=lambda name: name[1])
>>> h.peek()  # Jones comes before Smith
('Zeta', 'Jones')
>>> h.push(('Aaron', 'Allen'))
>>> h.peek()
('Aaron', 'Allen')


Replace the top item with a new one.  Order changes.

>>> h.replace(('Annie', 'Sun'))
('Aaron', 'Allen')

>>> for ordered_name in h:
...     print(ordered_name)
('Zeta', 'Jones')
('Adam', 'Smith')
('Annie', 'Sun')

The heap is not changed:

>>> h.peek()
('Zeta', 'Jones')

Note that if you plan to iterate through the whole
Heap, sorting with the same key is more efficient:

>>> for ordered_name in sorted(h.raw(), key=lambda name: name[1]):
...     print(ordered_name)
('Zeta', 'Jones')
('Adam', 'Smith')
('Annie', 'Sun')

Credit

Copyright (c) 2022 Michael Scott Asato Cuthbert

License

MIT

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

heap_class-0.9.0b1.tar.gz (8.3 kB view details)

Uploaded Source

Built Distribution

heap_class-0.9.0b1-py3-none-any.whl (7.4 kB view details)

Uploaded Python 3

File details

Details for the file heap_class-0.9.0b1.tar.gz.

File metadata

  • Download URL: heap_class-0.9.0b1.tar.gz
  • Upload date:
  • Size: 8.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.10.6

File hashes

Hashes for heap_class-0.9.0b1.tar.gz
Algorithm Hash digest
SHA256 69de9ce9cd9ff41833a894c0651651ddb7db6c88f4c839d1661e63c90496f2dd
MD5 fae23e735222032d222d9e4a5c571478
BLAKE2b-256 771daf4bf02d42b3278ca8eced3972edc8fd535e8d6d6aa01eb2bc453018c003

See more details on using hashes here.

File details

Details for the file heap_class-0.9.0b1-py3-none-any.whl.

File metadata

File hashes

Hashes for heap_class-0.9.0b1-py3-none-any.whl
Algorithm Hash digest
SHA256 75e8005eb3e4f04dfe0d366bd839f52471586ca3191fc3f381bc472dd72acf64
MD5 f8b4d47c3290717af8ecc67d9e496800
BLAKE2b-256 eb34ec1af79d1bf04ec009f8287cd11d3c4663a1f7ce174de5ed365c046a675c

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