Git Product home page Git Product logo

Comments (3)

garro95 avatar garro95 commented on June 15, 2024 1

The order is arbitrary but deterministic, in the sense that you can repeat the exact same experiment with the same results, but equal element inserted in a certain order can be extracted in reverse order. It does not depend on the hasher, but only on the heap algorithm.

I will properly update the documentation with this information.
Thank you.

from priority-queue.

MatejSloboda avatar MatejSloboda commented on June 15, 2024

To clarify, is this test guaranteed to succeed?

let mut a = PriorityQueue::new();
let mut b = PriorityQueue::new();

a.push("Apples", 1);
b.push("Apples", 1);
a.push("Oranges", 1);
b.push("Oranges", 1);
a.push("Pears", 2);
b.push("Pears", 2);
a.push("Lemons", 1);
b.push("Lemons", 1);

// note, we don't care if "Apples" get popped before "Oranges"
// we only care that queues 'a' and 'b' will pop them in the same (arbitrary) order 
// if we performed the same operations in the same order on queues 'a' and 'b'
assert_eq!(a.pop(), b.pop());
assert_eq!(a.pop(), b.pop());
assert_eq!(a.pop(), b.pop());
assert_eq!(a.pop(), b.pop());

In other words, if I have two queues and apply the same operations in the same order, are they guaranteed to remain synchronized?

from priority-queue.

garro95 avatar garro95 commented on June 15, 2024

Yes. And also

use priority_queue::PriorityQueue;
use hashbrown::hash_map::DefaultHashBuilder;

fn main() {
    let mut a = PriorityQueue::new();
    let mut b = PriorityQueue::<_, _, DefaultHashBuilder>::with_default_hasher();

    a.push("Apples", 1);
    b.push("Apples", 1);
    a.push("Oranges", 1);
    b.push("Oranges", 1);
    a.push("Pears", 2);
    b.push("Pears", 2);
    a.push("Lemons", 1);
    b.push("Lemons", 1);

    // note, we don't care if "Apples" get popped before "Oranges"
    // we only care that queues 'a' and 'b' will pop them in the same (arbitrary) order 
    // if we performed the same operations in the same order on queues 'a' and 'b'
    assert_eq!(a.pop(), b.pop());
    assert_eq!(a.pop(), b.pop());
    assert_eq!(a.pop(), b.pop());
    assert_eq!(a.pop(), b.pop());
}

does not fail

from priority-queue.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.