=============

PriorityQueue

.. image:: https://img.shields.io/crates/v/priority-queue.svg :target: https://crates.io/crates/priority-queue .. image:: https://travis-ci.org/garro95/priority-queue.svg?branch=master :target: https://travis-ci.org/garro95/priority-queue

This crate implements a Priority Queue with a function to change the priority of an object. Priority and items are stored in an IndexMap and the queue is implemented as a Heap of indexes.

Please read the API documentation here__

__ https://docs.rs/priority-queue/

Usage

To use this crate, simply add the following string to your Cargo.toml:

  priority-queue = "1.1.0"

Version numbers follow the semver__ convention.

__ https://semver.org/

Then use the data structure inside your Rust source code as in the following Example.

Remember that, if you need serde support, you should compile using --features serde.

Example

.. code:: rust

  extern crate priority_queue; // not necessary in Rust edition 2018

  use priority_queue::PriorityQueue;

  fn main() {
      let mut pq = PriorityQueue::new();

      assert!(pq.is_empty());
      pq.push("Apples", 5);
      pq.push("Bananas", 8);
      pq.push("Strawberries", 23);

      assert_eq!(pq.peek(), Some((&"Strawberries", &23)));

      for (item, _) in pq.into_sorted_iter() {
          println!("{}", item);
      }
  }

Note: in recent versions of Rust (edition 2018) the extern crate priority_queue is not necessary anymore!

Speeding up

You can use custom BuildHasher for the underlying IndexMap and therefore achieve better performance. For example you can create the queue with the speedy FxHash_ hasher:

.. code:: rust

  use hashbrown::hash_map::DefaultHashBuilder;

  let mut pq = PriorityQueue::<_, _, DefaultHashBuilder>::with_default_hasher();

.. _FxHash: https://github.com/Amanieu/hashbrown

Attention: FxHash does not offer any protection for dos attacks. This means that some pathological inputs can make the operations on the hashmap O(n^2). Use the standard hasher if you cannot control the inputs.

Benchmarks

Some benchmarks have been run to compare the performances of this priority queue to the standard BinaryHeap, also using the FxHash hasher. On a Ryzen 9 3900X, the benchmarks produced the following results: :: test benchmarks::prioritychangeonlargequeue ... bench: 20 ns/iter (+/- 0) test benchmarks::prioritychangeonlargequeuefx ... bench: 7 ns/iter (+/- 0) test benchmarks::prioritychangeonlargequeuestd ... bench: 255,098 ns/iter (+/- 45,542) test benchmarks::prioritychangeonsmallqueue ... bench: 19 ns/iter (+/- 0) test benchmarks::prioritychangeonsmallqueuefx ... bench: 7 ns/iter (+/- 0) test benchmarks::prioritychangeonsmallqueuestd ... bench: 1,741 ns/iter (+/- 24) test benchmarks::pushandpop ... bench: 37 ns/iter (+/- 0) test benchmarks::pushandpopfx ... bench: 25 ns/iter (+/- 0) test benchmarks::pushandpoponlargequeue ... bench: 185 ns/iter (+/- 3) test benchmarks::pushandpoponlargequeuefx ... bench: 118 ns/iter (+/- 1) test benchmarks::pushandpoponlargequeuestd ... bench: 33 ns/iter (+/- 6) test benchmarks::pushandpop_std ... bench: 4 ns/iter (+/- 0)

The priority change on the standard queue was obtained with the following:

.. code:: rust

    pq = pq.drain().map(|Entry(i, p)| {
    if i == 50_000 {
        Entry(i, p/2)
    } else {
        Entry(i, p)
    }
    }).collect()

The interpretation of the benchmarks is that the data structure provided by this crate is generally slightly slower then the standard Binary Heap. On small queues (<10000 elements), also the change_priority function, obtained on the standard Binary Heap with the code above, is roughly as fast as the one provided by PriorityQueue. With the queue becoming bigger though, PriorityQueue performs much faster on priority change operations.

Contributing

Feel free to contribute to this project with pull requests and/or issues. All contribution should be under a license compatible with the GNU LGPL and with the MPL.

Changes