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 plainfor 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
- Fork the repo
- Create a topic branch
- Submit a PR
License
MIT © Your Name
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e7372310110804320e03b129ba38280cc1e506032772e2533cbee15c91e28faf
|
|
| MD5 |
7dd0ba07156fd82ee2a73c647b507b67
|
|
| BLAKE2b-256 |
644bf51059fbc33a8cd75351c64798e37cf5f6b120a6b56c5a5601659d02d7e3
|
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3c8e7ea71f45297f2312d23e99cec2767fb48c8f4e3f53f621f5b1fccb437131
|
|
| MD5 |
d12bee654f92c2080a806435ee00bf5d
|
|
| BLAKE2b-256 |
d3ee3f1079817cef2c399699e70050191695c1a19f750e331b41db0d2f761081
|