4

I've written a struct called Node, and want to be able to use pointers to that struct as entries in a Phobos BinaryHeap. However, I am not sure how opEquals and opCmp are implemented for pointers to structs (or in fact, in general). I've not been able to find anything in the documentation to help me. Could anyone point me in the right direction?

Koz Ross
  • 3,040
  • 2
  • 24
  • 44

1 Answers1

3

If you have an array of these Node* you can do something like that:

Node*[] arr = ....;
auto heap = heapify!(yourCustomCompareFuncGoesHere)(arr);

If you can't use heapify for whatever reason you can create a BinaryHeap by:

BinaryHeap!(Node*[], yourCustomCompareFuncGoesHere) heap;

yourCustomCompareFuncGoesHere will be passed as an alias template parameter to the heap and used for the "is less comparison" for sorting. Compare to the struct signature of BinaryHeap in the phobos docs.

burner
  • 323
  • 2
  • 10
  • For this custom compare function, what kind of function signature does ``BinaryHeap`` expect? I've tried using this method, with... odd results. – Koz Ross Aug 02 '14 at 03:32
  • OK, it seems like it wants a function that returns a boolean. – Koz Ross Aug 09 '14 at 04:46