Metadata-Version: 2.4
Name: rheapdict
Version: 0.1.0
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Rust
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: Implementation :: CPython
Classifier: Topic :: Software Development :: Libraries
Requires-Dist: pytest ; extra == 'dev'
Requires-Dist: hypothesis ; extra == 'dev'
Requires-Dist: heapdict ; extra == 'dev'
Requires-Dist: pqdict ; extra == 'dev'
Requires-Dist: ruff==0.15.21 ; extra == 'dev'
Provides-Extra: dev
License-File: LICENSE
Summary: Rust-powered indexed priority queue (heap + dict with decrease-key) for Python
Keywords: heap,priority queue,heapdict,pqdict,decrease-key,dijkstra
License: MIT
Requires-Python: >=3.11
Description-Content-Type: text/markdown; charset=UTF-8; variant=GFM
Project-URL: Homepage, https://github.com/f4hy/rheapdict
Project-URL: Issues, https://github.com/f4hy/rheapdict/issues
Project-URL: Repository, https://github.com/f4hy/rheapdict

# rheapdict

A Rust-powered **indexed priority queue** for Python: a dict that is also a
heap, with `O(log n)` *decrease-key* / *increase-key*. Exposed via
[PyO3](https://pyo3.rs).

```python
from rheapdict import HeapDict

pq = HeapDict({"a": 3, "b": 1})
pq["c"] = 2          # insert
pq["a"] = 0          # decrease-key, O(log n)
pq.popitem()         # -> ("a", 0)
```

## Why

Python's `heapq` has no way to change an item's priority, so algorithms like
Dijkstra, A\*, Prim, LFU caches, and discrete-event schedulers either push
duplicate entries and skip stale ones, or use an indexed priority queue. The
existing indexed PQs (`heapdict`, last released 2019, and `pqdict`) are pure
Python, so every sift step runs in the interpreter.

`rheapdict` keeps the key→slot index in a real `dict` (so hashing and
equality keep exact Python semantics, at C speed) and runs the heap in Rust.
When both priorities are exact `int`/`float`, it compares them natively.
Anything else (tuples, `Decimal`, custom objects) falls back to Python's `<`.

## Install

```sh
pip install rheapdict
```

Prebuilt `abi3` wheels (Linux/macOS/Windows, Python 3.11+).

## Benchmarks

Single-threaded, release build, random `float` priorities and `str` keys.
Source: [`benchmarks/bench.py`](./benchmarks/bench.py).

| workload | n | rheapdict | heapdict | pqdict |
|---|---|---|---|---|
| push all, pop all | 100,000 | **0.13s** | 0.91s | 0.86s |
| push n + 2n priority updates, pop all | 100,000 | **0.22s** | 3.25s | 1.43s |
| Dijkstra, 8 edges/node | 100,000 | **0.46s** | 2.10s | 1.48s |
| push all, pop all | 1,000,000 | **2.16s** | 17.5s | 15.2s |
| push n + 2n priority updates, pop all | 1,000,000 | **3.43s** | 55.4s | 22.8s |
| Dijkstra, 8 edges/node | 1,000,000 | **7.40s** | 37.0s | 24.8s |

That is 4–16x faster than `heapdict`/`pqdict`. For reference, plain `heapq`
with `(priority, key)` tuples (no decrease-key at all) takes 2.63s for the
1M push/pop workload, so `rheapdict` is faster than that too, because it
avoids comparing tuples in Python.

## API

`HeapDict` implements the `MutableMapping` interface (`len`, `in`, `[]`,
`del`, `get`, `pop`, `setdefault`, `update`, `keys`/`values`/`items`,
`clear`, `copy`) plus the following:

| method | meaning |
|---|---|
| `popitem()` | remove and return the top `(key, priority)` |
| `peekitem()` / `topitem()` | return the top `(key, priority)` without removing it |
| `top()` | return the top key |
| `pop()` | with no arguments, remove and return the top **key** (pqdict semantics) |
| `push(k, p)` / `additem(k, p)` | insert a new key (`KeyError` if it is already present) |
| `updateitem(k, p)` | change an existing key (`KeyError` if it is missing) |
| `pushpopitem(k, p)` | push then pop in one step (faster than the two calls) |
| `HeapDict(..., max_heap=True)` | pop the largest priority first |

`heapdict` and `pqdict` are exported as aliases to ease migration.

### Semantics

- **Ties pop FIFO**: equal priorities come out in insertion order, so the
  output is deterministic (neither `heapdict` nor `heapq` guarantees this).
  An item keeps its insertion position when its priority is updated.
- **Iteration** (`iter`, `keys()`, ...) is in insertion order, not priority
  order, and is a snapshot (mutating while iterating is safe).
- **Empty pops** raise `KeyError`, matching `dict.popitem`.
- **Incomparable priorities** (e.g. `1` vs `"x"`) raise `TypeError`. As with
  `heapq`, the item is still stored, and the heap stays structurally valid.
- **Large ints**: `int` vs `float` comparisons are exact (ints beyond 2^53
  fall back to Python comparison).
- Picklable and copyable. Subclassable.

> [!NOTE]
> **Compatibility with `heapdict` / `pqdict`**
>
> `heapdict`'s full API (`[]=`, `popitem`, `peekitem`, mapping methods) works
> unchanged. `pqdict`'s `top`, `topitem`, `pop()`, `additem`, `updateitem`,
> and `pushpopitem` work too. Not supported: `pqdict`'s `key=` and
> `precedes=` constructor arguments (use `max_heap=True`, or store a
> transformed priority) and its `popkeys()`/`popvalues()`/`popitems()`
> iterators.

## Development

```sh
cargo test --no-default-features      # Rust unit tests of the core heap
pip install -e '.[dev]' && maturin develop --release
pytest                                # includes a Hypothesis model test
python benchmarks/bench.py
```

