www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - BinaryHeap

reply "Agustin" <agustin.l.alvarez hotmail.com> writes:
I'm trying to use BinaryHeap and i found out that i cannot use 
foreach(). My question is, there is any other way to do it?, can 
i iterate a BinaryHeap?
Nov 06 2013
parent reply "bearophile" <bearophileHUGS lycos.com> writes:
Agustin:

 I'm trying to use BinaryHeap and i found out that i cannot use 
 foreach(). My question is, there is any other way to do it?, 
 can i iterate a BinaryHeap?
Please show the code :-) Perhaps you need to look at the head, pop the head item, look at the head, etc. Bye, bearophile
Nov 07 2013
parent reply "Agustin" <agustin.l.alvarez hotmail.com> writes:
On Thursday, 7 November 2013 at 09:00:11 UTC, bearophile wrote:
 Agustin:

 I'm trying to use BinaryHeap and i found out that i cannot use 
 foreach(). My question is, there is any other way to do it?, 
 can i iterate a BinaryHeap?
Please show the code :-) Perhaps you need to look at the head, pop the head item, look at the head, etc. Bye, bearophile
BinaryHeap!(uint[]) heap; foreach(type; heap) { .... } no property 'popFront' for type 'BinaryHeap!(uint[])'
Nov 07 2013
next sibling parent "Agustin" <agustin.l.alvarez hotmail.com> writes:
On Thursday, 7 November 2013 at 12:14:22 UTC, Agustin wrote:
 On Thursday, 7 November 2013 at 09:00:11 UTC, bearophile wrote:
 Agustin:

 I'm trying to use BinaryHeap and i found out that i cannot 
 use foreach(). My question is, there is any other way to do 
 it?, can i iterate a BinaryHeap?
Please show the code :-) Perhaps you need to look at the head, pop the head item, look at the head, etc. Bye, bearophile
BinaryHeap!(uint[]) heap; foreach(type; heap) { .... } no property 'popFront' for type 'BinaryHeap!(uint[])'
It seems that i need to have a pointer to the underlying array. uint[] intArray; BinaryHeap!(uint[]) heap; heap.acquire(intArray); foreach(int; intArray) { .... }
Nov 07 2013
prev sibling parent reply "bearophile" <bearophileHUGS lycos.com> writes:
Agustin:

 no property 'popFront' for type 'BinaryHeap!(uint[])'
Try to use front and removeFront (I don't know why there is removeFront instead of popFront). Bye, bearophile
Nov 07 2013
next sibling parent reply "Agustin" <agustin.l.alvarez hotmail.com> writes:
On Thursday, 7 November 2013 at 12:29:44 UTC, bearophile wrote:
 Agustin:

 no property 'popFront' for type 'BinaryHeap!(uint[])'
Try to use front and removeFront (I don't know why there is removeFront instead of popFront). Bye, bearophile
Saddly i had to do this auto clone = _heap.dup; while (!clone.empty()) { auto item = clone.front(); // ... Do something with item clone.removeFront(); } Iterate directly over a binary heap will be better perfomance wise than doing that because i had to clone the heap to be able to iterate over without removing items from the original heap.
Nov 07 2013
parent "Agustin" <agustin.l.alvarez hotmail.com> writes:
On Thursday, 7 November 2013 at 12:45:11 UTC, Agustin wrote:
 On Thursday, 7 November 2013 at 12:29:44 UTC, bearophile wrote:
 Agustin:

 no property 'popFront' for type 'BinaryHeap!(uint[])'
Try to use front and removeFront (I don't know why there is removeFront instead of popFront). Bye, bearophile
Saddly i had to do this auto clone = _heap.dup; while (!clone.empty()) { auto item = clone.front(); // ... Do something with item clone.removeFront(); } Iterate directly over a binary heap will be better perfomance wise than doing that because i had to clone the heap to be able to iterate over without removing items from the original heap.
By looking at the source, having "private property ref Store _store()" public will help a lot.
Nov 07 2013
prev sibling parent reply "Agustin" <agustin.l.alvarez hotmail.com> writes:
On Thursday, 7 November 2013 at 12:29:44 UTC, bearophile wrote:
 Agustin:

 no property 'popFront' for type 'BinaryHeap!(uint[])'
Try to use front and removeFront (I don't know why there is removeFront instead of popFront). Bye, bearophile
I had to implement a custom IterableBinaryHeap implementation. http://pastebin.com/GeVAeCch IterableBinaryHeap!(Array!uint, "a < b") heap; heap.insert(0); heap.insert(3); heap.insert(2); heap.insert(1); foreach(item; heap) writeln(item);
Nov 07 2013
parent "Agustin" <agustin.l.alvarez hotmail.com> writes:
On Thursday, 7 November 2013 at 14:31:27 UTC, Agustin wrote:
 On Thursday, 7 November 2013 at 12:29:44 UTC, bearophile wrote:
 Agustin:

 no property 'popFront' for type 'BinaryHeap!(uint[])'
Try to use front and removeFront (I don't know why there is removeFront instead of popFront). Bye, bearophile
I had to implement a custom IterableBinaryHeap implementation. http://pastebin.com/GeVAeCch IterableBinaryHeap!(Array!uint, "a < b") heap; heap.insert(0); heap.insert(3); heap.insert(2); heap.insert(1); foreach(item; heap) writeln(item);
The above code was working until i upgrade to DMD32 D Compiler v2.064. Now i got: "Error: cannot uniquely infer foreach argument types" Help!
Nov 27 2013