Thread overview | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
|
November 07, 2013 BinaryHeap | ||||
---|---|---|---|---|
| ||||
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? |
November 07, 2013 Re: BinaryHeap | ||||
---|---|---|---|---|
| ||||
Posted in reply to Agustin | 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
|
November 07, 2013 Re: BinaryHeap | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | 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[])'
|
November 07, 2013 Re: BinaryHeap | ||||
---|---|---|---|---|
| ||||
Posted in reply to Agustin | 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)
{
....
}
|
November 07, 2013 Re: BinaryHeap | ||||
---|---|---|---|---|
| ||||
Posted in reply to Agustin | 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
|
November 07, 2013 Re: BinaryHeap | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | 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.
|
November 07, 2013 Re: BinaryHeap | ||||
---|---|---|---|---|
| ||||
Posted in reply to Agustin | 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.
|
November 07, 2013 Re: BinaryHeap | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | 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); |
November 27, 2013 Re: BinaryHeap | ||||
---|---|---|---|---|
| ||||
Posted in reply to Agustin | 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!
|
Copyright © 1999-2021 by the D Language Foundation