Skip to main content

A thread-safe, map-backed min/max heap implementation for Python.

Project description

heapmap

A thread-safe heap that behaves like a dict but maintains heap ordering.

Features:

  • Supports both min-heap and max-heap modes via a simple flag

  • Dictionary-style access:

    • h[key] = priority (insert/update)
    • prio = h[key] (lookup)
    • del h[key] (remove)
  • Bulk initialization: HeapMap({'a':1, 'b':2})

  • Priority operations:

    • h.popitem() & h.peekitem() for root (min or max)
    • h.update(key, new_prio) to change an existing key’s priority
  • Introspection:

    • len(h), key in h, h.is_empty()
  • Safe iteration:

    • .keys(), .values(), .items(), and plain for k, v in h:
    • Detects concurrent modifications and raises RuntimeError

Installation

pip install heapmap

Quickstart

from heapmap import HeapMap

# Min-heap example
h = HeapMap({'a':5, 'b':2, 'c':3})
print(h.peekitem())  # ('b', 2)

# Insert
h['d'] = 1
print(h.popitem())   # ('d', 1)

# Update
h.update('a', 0)
print(h.peekitem())  # ('a', 0)

# Iterate
for key, prio in h.items():
    print(f"{key} -> {prio}")

API Reference

class HeapMap(MutableMapping):
    def __init__(self, initial=None, is_max_heap=False):
        """Initialize with optional dict and heap mode."""

    def __getitem__(self, key):
        """Get priority for key."""

    def __setitem__(self, key, priority):
        """Insert or update key with priority."""

    def __delitem__(self, key):
        """Remove key from heap."""

    def popitem(self):
        """Remove and return root (min or max) item."""

    def peekitem(self):
        """Return root item without removing it."""

    def update(self, key, new_priority):
        """Change priority and reheapify."""

    def clear(self):
        """Remove all items."""

    def is_empty(self):
        """Return True if heap is empty."""

    def keys(self):
        """Iterator over keys (safe to detect mods)."""

    def values(self):
        """Iterator over priorities."""

    def items(self):
        """Iterator over (key, priority) pairs."""

    def __len__(self):
        """Number of items in heap."""

    def __contains__(self, key):
        """Membership test."""

Testing

pip install -e .[test]  # installs pytest
pytest

Contributing

  1. Fork the repo
  2. Create a topic branch
  3. Submit a PR

License

MIT © Your Name

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

heapmap-0.1.0.tar.gz (5.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

heapmap-0.1.0-py3-none-any.whl (5.2 kB view details)

Uploaded Python 3

File details

Details for the file heapmap-0.1.0.tar.gz.

File metadata

  • Download URL: heapmap-0.1.0.tar.gz
  • Upload date:
  • Size: 5.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.2

File hashes

Hashes for heapmap-0.1.0.tar.gz
Algorithm Hash digest
SHA256 e7372310110804320e03b129ba38280cc1e506032772e2533cbee15c91e28faf
MD5 7dd0ba07156fd82ee2a73c647b507b67
BLAKE2b-256 644bf51059fbc33a8cd75351c64798e37cf5f6b120a6b56c5a5601659d02d7e3

See more details on using hashes here.

File details

Details for the file heapmap-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: heapmap-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 5.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.2

File hashes

Hashes for heapmap-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3c8e7ea71f45297f2312d23e99cec2767fb48c8f4e3f53f621f5b1fccb437131
MD5 d12bee654f92c2080a806435ee00bf5d
BLAKE2b-256 d3ee3f1079817cef2c399699e70050191695c1a19f750e331b41db0d2f761081

See more details on using hashes here.

Supported by

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