Skip to main content

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

Metadata

Release files for heapmap 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for heapmap 0.1.0
File Size Uploaded
heapmap-0.1.0.tar.gz 5.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for heapmap 0.1.0
File Interpreter ABI Platform
heapmap-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 10.8 kB

Release files / heapmap-0.1.0.tar.gz

Download URL heapmap-0.1.0.tar.gz
Size 5.6 kB
Tags Source
SHA-256 checksum
How to use checksums
e7372310110804320e03b129ba38280cc1e506032772e2533cbee15c91e28faf
BLAKE2b-256 checksum
How to use checksums
644bf51059fbc33a8cd75351c64798e37cf5f6b120a6b56c5a5601659d02d7e3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.2

Release files / heapmap-0.1.0-py3-none-any.whl

Download URL heapmap-0.1.0-py3-none-any.whl
Size 5.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
3c8e7ea71f45297f2312d23e99cec2767fb48c8f4e3f53f621f5b1fccb437131
BLAKE2b-256 checksum
How to use checksums
d3ee3f1079817cef2c399699e70050191695c1a19f750e331b41db0d2f761081
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.2

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page