Thread overview | ||||||
---|---|---|---|---|---|---|
|
February 19, 2015 Problem Instantiating a BinaryHeap with a Comparison Function the needs this | ||||
---|---|---|---|---|
| ||||
I can understand how to correctly define an instance of BinaryHeap in my class DijkstraWalker at https://github.com/nordlow/justd/blob/master/knet/traversal.d#L264 because the comparsion function can't ge access to the class member distMap I get the error need 'this' for 'distMap' of type 'Tuple!(double, Ref!(Node))[Ref!(Node)]' What to do? |
February 19, 2015 Re: Problem Instantiating a BinaryHeap with a Comparison Function the needs this | ||||
---|---|---|---|---|
| ||||
Posted in reply to Nordlöw | On Thursday, 19 February 2015 at 11:56:19 UTC, Nordlöw wrote:
> I can understand how to correctly define an instance of BinaryHeap in my class DijkstraWalker at
>
> https://github.com/nordlow/justd/blob/master/knet/traversal.d#L264
>
> because the comparsion function can't ge access to the class member distMap
>
> I get the error
>
> need 'this' for 'distMap' of type 'Tuple!(double, Ref!(Node))[Ref!(Node)]'
>
> What to do?
I don't know if the lambda form can access the outer scope. Try different forms for the comparer:
auto comparer = delegate (a, b)
{
return this.distMap[a][0] < this.distMap[b][0];
}
BinaryHeap!(Nds, comparer) pendingNds;
or
BinaryHeap!(Nds, "this.distMap[a][0] < this.distMap[b][0]") pendingNds;
Caveat: this is off the top of my head and no way even tested or read the docs. :)
|
February 19, 2015 Re: Problem Instantiating a BinaryHeap with a Comparison Function the needs this | ||||
---|---|---|---|---|
| ||||
Posted in reply to Nordlöw | On Thursday, 19 February 2015 at 11:56:19 UTC, Nordlöw wrote:
Please provide reduced examples.
This fails:
class C
{
int[] a;
alias BH = BinaryHeap!(int[], (x, y) => (x+a < y));
}
This works:
class C
{
int[] a;
void foo() {
alias BH = BinaryHeap!(int[], (x, y) => (x+a < y));
}
}
But will create an instance of BinaryHeap per member function.
> What to do?
Dunno.
|
February 19, 2015 Re: Problem Instantiating a BinaryHeap with a Comparison Function the needs this | ||||
---|---|---|---|---|
| ||||
Posted in reply to Tobias Pankrath | On Thursday, 19 February 2015 at 14:12:51 UTC, Tobias Pankrath wrote: > On Thursday, 19 February 2015 at 11:56:19 UTC, Nordlöw wrote: > > Please provide reduced examples. > > This fails: > > class C > { > int[] a; > alias BH = BinaryHeap!(int[], (x, y) => (x+a < y)); > } > > This works: > > class C > { > int[] a; > > void foo() { > alias BH = BinaryHeap!(int[], (x, y) => (x+a < y)); > } > } > > But will create an instance of BinaryHeap per member function. > >> What to do? > > Dunno. I modified my algorithm to be more like http://rosettacode.org/wiki/Dijkstra%27s_algorithm#D which doesn't require a special comparison function for RedBlackTree. See update at: https://github.com/nordlow/justd/blob/master/knet/traversal.d#L108 Thanks anyway. |
Copyright © 1999-2021 by the D Language Foundation