priorityQueue

Dear all,I am struggle with deep copying a PriorityQueue. Anybody can show me how to do this?Cheers,Ke
[130 byte] By [kolapiga] at [2007-11-26 21:57:08]
# 1
post your code plz
java_2006a at 2007-7-10 3:54:11 > top of Java-index,Java Essentials,Java Programming...
# 2

Create an Iterator from your queue:

[url=http://java.sun.com/j2se/1.5.0/docs/api/java/util/PriorityQueue.html#iterator()]http://java.sun.com/j2se/1.5.0/docs/api/java/util/PriorityQueue.html#iterator()[/url]

Create a new queue:

[url=http://java.sun.com/j2se/1.5.0/docs/api/java/util/PriorityQueue.html#PriorityQueue()]http://java.sun.com/j2se/1.5.0/docs/api/java/util/PriorityQueue.html#PriorityQueue()[/url]

And clone each element in your iterator and store it in your new queue:

http://java.sun.com/docs/books/tutorial/java/IandI/objectclass.html

prometheuzza at 2007-7-10 3:54:11 > top of Java-index,Java Essentials,Java Programming...
# 3

Thanks, the code is here

private void excuteTableau(Branch branch){

if(this.getToDoQueue().isEmpty()){

this.getAllBranches().add(branch);

return;

}

// Get an Entry from the top of the queue

ToDoEntry toDoEntry = (ToDoEntry) this.getToDoQueue().peek();

if(toDoEntry.isActive()){

toDoEntry.expandRules();

ArrayList possibleChoiceList = toDoEntry.getPossibleExpansions();

if(possibleChoiceList.size()==1){

Set possibleChoice_ = (Set)(possibleChoiceList.get(0));

this.getToDoQueue().remove(toDoEntry);

branch.getBranchElems().addAll(possibleChoice_);

Iterator iterator = possibleChoice_.iterator();

while(iterator.hasNext()){

ATermAppl a = (ATermAppl)iterator.next();

this.addToDoEntry(a, branch);

}

excuteTableau(branch);

}

else{

this.getToDoQueue().remove(toDoEntry);

// Deep Copy the queue after the removement!!

// ..............

for(int i=0; i<possibleChoiceList.size();i++ ){

Set possibleChoice_ = (Set)(possibleChoiceList.get(i));

Branch splittedBranch = new Branch();

splittedBranch.deepCopy(branch);

this.setReferredBranchForQueue(splittedBranch);

splittedBranch.getBranchElems().addAll(possibleChoice_);

Iterator iterator = possibleChoice_.iterator();

while(iterator.hasNext()){

ATermAppl a = (ATermAppl)iterator.next();

this.addToDoEntry(a, splittedBranch);

}

excuteTableau(splittedBranch);

}

// Resort the queue goes here

// ....................

this.getToDoQueue().add(toDoEntry);

}

}

else{

excuteTableau(branch);

}

}>

kolapiga at 2007-7-10 3:54:11 > top of Java-index,Java Essentials,Java Programming...
# 4
I think using Iterator with not guarantee the order of the queue. Is this right.
kolapiga at 2007-7-10 3:54:11 > top of Java-index,Java Essentials,Java Programming...
# 5
> I think using Iterator with not guarantee the order> of the queue. Is this right.No, but adding the cloned objects in a new priority queue will assure them to get properly ordered again.
prometheuzza at 2007-7-10 3:54:12 > top of Java-index,Java Essentials,Java Programming...
# 6
The thing is that in my code two objects of the same prority in a queue are not treated in the same way.E.G. Assume I have a queue {A, B}, both of which have priority 2;but I have to make sure that the deep copy is {A, B} rather than {B, A}
kolapiga at 2007-7-10 3:54:12 > top of Java-index,Java Essentials,Java Programming...
# 7
> The thing is that in my code two objects of the same> prority in a queue are not treated in the same> way.Well, then they should not have the same priority!
prometheuzza at 2007-7-10 3:54:12 > top of Java-index,Java Essentials,Java Programming...