Clone PriorityQueue

343 views
Skip to first unread message

Mathieu Bastian

unread,
Feb 10, 2013, 1:51:00 PM2/10/13
to fast...@googlegroups.com
Hi all, 

I'm using a ShortHeapPriorityQueue and I wondered if there is a way to clone or get the content of the queue without modifying it? Right now, it seems impossible to obtain the elements in the queue without dequeuing? 

Maybe I'm missing something... Thanks!


Sebastiano Vigna

unread,
Feb 10, 2013, 4:06:59 PM2/10/13
to fast...@googlegroups.com
No, you're not. Actually, I had the same problem recently, but fortunately it was a queue of objects so I used java.util's. When I wrote the PriorityQueue interface I really did not think that it could have been useful.

The long-term plan is to integrate the java.util PriorityQueue interface with its funny names. At that point there will be an iterator() method that you can iterate on to get the elements in the queue without dequeueing.

In the meantime, you can subclass ShortHeapPriorityQueue and add a method that returns a copy of the heap or the heap itself, depending on your efficiency constraints. Something like

public class MyQueue extends ShortHeapPriorityQueue {
public short[] elements() {
return java.util.Arrays.copyOfRange( heap, 0, size );
}
}

Ciao,

seba

Mathieu Bastian

unread,
Feb 11, 2013, 1:25:39 PM2/11/13
to fast...@googlegroups.com, vi...@di.unimi.it
Ok Thanks Sebastiano that make sense. The iterator() would make it for me.

 Thanks for the workaround, I can use that for now.
Reply all
Reply to author
Forward
0 new messages