https://github.com/arindas/bheap
A generic binary max heap for implementing a dynamically prioritizable priority queue.
https://github.com/arindas/bheap
binary-heap
Last synced: 7 months ago
JSON representation
A generic binary max heap for implementing a dynamically prioritizable priority queue.
- Host: GitHub
- URL: https://github.com/arindas/bheap
- Owner: arindas
- License: mit
- Created: 2021-10-11T14:30:52.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2021-10-20T11:33:46.000Z (over 4 years ago)
- Last Synced: 2025-07-15T02:34:20.605Z (8 months ago)
- Topics: binary-heap
- Language: Rust
- Homepage: https://arindas.github.io/bheap/bheap/index.html
- Size: 1.76 MB
- Stars: 11
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# bheap
[](https://github.com/arindas/bheap/actions/workflows/ci-tests.yml)
[](https://github.com/arindas/bheap/actions/workflows/rustdoc.yml)
[](https://app.fossa.com/projects/git%2Bgithub.com%2Farindas%2Fbheap?ref=badge_shield)
A generic binary max heap implementation for implementing a dynamically prioritizable priority queue.
This implementation uses a vector as the underlying data-structure. Hence, there is no oppurtunity
for fine grained locking. Users of this crate are request to wrap `bheap::BinaryMaxHeap` with the
required concurrency primitive for use in multithreaded contexts.
## Why is this necessary?
The binary heap implementation provided by the standard library (`use std::collections::binary_heap::BinaryHeap;`),
assumes that the ordering of the elements in the domain is fixed. I needed a binary heap implementation which allowed
for change in ordering of elements at runtime.
## How does it work?
`bheap::BinaryMaxHeap` enforces the `Ord + bheap::Uid` trait bounds on the element type. The `Uid` trait, simply
presents a method for returing a unique `u64` uid for the type.
The struct maintains a `Vec` as the underlying storage buffer and a `HashMap` for maintaining a
mapping from `T::uid()` to position in vector. This map is updated on every heap operation to remain consistent.
When the ordering of an element changes, its position in the heap can be looked up in the heap using the
hashmap. Then, we `heapify_up()` or `heapify_down()` as required to restore heap property.
## Limitations
Since, we use `u64` for uniquely identitfying elements, this heap can only scale up `2^64 = 18446744073709551616` elements.
This was more than enough for my purposes.
Another interesting property of this library is that it has no third party dependencies other than the standard libary.
# License
`bheap` is licensed under the MIT License. See [LICENSE](./LICENSE) for the full license text.
[](https://app.fossa.com/projects/git%2Bgithub.com%2Farindas%2Fbheap?ref=badge_large)