Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FastQueue doesn't return some nodes in some cases #49

Closed
iRumba opened this issue Jul 27, 2020 · 3 comments
Closed

FastQueue doesn't return some nodes in some cases #49

iRumba opened this issue Jul 27, 2020 · 3 comments

Comments

@iRumba
Copy link

iRumba commented Jul 27, 2020

I give you one case

image

        [Test]
        public void TestEveryDequeued()
        {
            TQueue queue2 = CreateQueue();
            var node1 = new Node(1);
            var node2 = new Node(1);
            var node3 = new Node(1);

            queue2.Enqueue(node1, 1);
            queue2.Enqueue(node2, 1);
            queue2.Enqueue(node3, 1);

            var dequeuedNodes = new List<Node>();

            for (var i = 0; i < 1000; i++)
            {
                dequeuedNodes.Add(queue2.Dequeue());
                queue2.Enqueue(new Node(1), 1);
            }

            Assert.Contains(node1, dequeuedNodes);
            Assert.Contains(node2, dequeuedNodes);
            Assert.Contains(node3, dequeuedNodes);
        }

Second assertion will failed with any number of iterations

My first debug was on paper by code :)

image

You can see what happenes

@BlueRaja
Copy link
Owner

BlueRaja commented Jul 27, 2020

You're not dequeuing every node in the queue, so that test shouldn't necessarily be expected to pass...

As mentioned in the documentation, FastPriorityQueue is not stable, meaning if multiple nodes have the same priority, the order they're dequeued is arbitrary. If you need a stable queue, use StablePriorityQueue instead.

@iRumba
Copy link
Author

iRumba commented Jul 27, 2020

As mentioned in the documentation, FastPriorityQueue is not stable, meaning if multiple nodes have the same priority, the order they're dequeued is arbitrary. If you need a stable queue, use StablePriorityQueue instead.

But it means
We have not stable lib. See docs. :)

You can change this behavior. Fast method is change all usings HasHigherPriority to HasHigherOrEqualPriority in CascadeDown method.
You can try.

@BlueRaja
Copy link
Owner

BlueRaja commented Aug 9, 2020

No, that is not sufficient to make the data structure stable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants