Search code examples
pointersstructdphobosbinary-heap

Comparing pointers to structs for the purpose of Phobos' Binary Heap


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?


Solution

  • 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.