www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Problem Instantiating a BinaryHeap with a Comparison Function the

reply =?UTF-8?B?Ik5vcmRsw7Z3Ig==?= <per.nordlow gmail.com> writes:
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?
Feb 19 2015
next sibling parent "Gary Willoughby" <dev nomad.so> writes:
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. :)
Feb 19 2015
prev sibling parent reply "Tobias Pankrath" <tobias pankrath.net> writes:
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.
Feb 19 2015
parent =?UTF-8?B?Ik5vcmRsw7Z3Ig==?= <per.nordlow gmail.com> writes:
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.
Feb 19 2015