diff --git a/include/container/seadObjArray.h b/include/container/seadObjArray.h index ab4c1bc2..a6759a85 100644 --- a/include/container/seadObjArray.h +++ b/include/container/seadObjArray.h @@ -140,7 +140,7 @@ class ObjArray : public PtrArrayImpl void sort() { sort(compareT); } void sort(CompareCallback cmp) { PtrArrayImpl::sort(cmp); } void heapSort() { heapSort(compareT); } - void heapSort(CompareCallback cmp) { PtrArrayImpl::heapSort_(cmp); } + void heapSort(CompareCallback cmp) { PtrArrayImpl::heapSort(cmp); } bool equal(const ObjArray& other, CompareCallback cmp) const { diff --git a/include/container/seadPtrArray.h b/include/container/seadPtrArray.h index b79294a8..b2011ec3 100644 --- a/include/container/seadPtrArray.h +++ b/include/container/seadPtrArray.h @@ -173,17 +173,15 @@ class PtrArrayImpl void sort(CompareCallbackImpl cmp); - template - void heapSort_(Compare cmp) + template + void heapSort(s32 (*cmpT)(const T* a, const T* b)) { - // Note: Nintendo did not use - const auto less_cmp = [&](const void* a, const void* b) { - return cmp(static_cast(a), static_cast(b)) < 0; - }; - std::make_heap(mPtrs, mPtrs + size(), less_cmp); - std::sort_heap(mPtrs, mPtrs + size(), less_cmp); + // Symbols show that `sort()` accepts a `void*` comparer, but needs to receive a `T*` + // comparer in order to match SMO. This overload exists to safely accept a `T*` comparer. + // This cast is UB, but we know that `cmpT` and `cmpVoid` have the same representation. + auto cmpVoid = reinterpret_cast(cmpT); + heapSort(cmpVoid); } - void heapSort(CompareCallbackImpl cmp); s32 compare(const PtrArrayImpl& other, CompareCallbackImpl cmp) const; @@ -256,7 +254,7 @@ class PtrArray : public PtrArrayImpl void sort() { sort(compareT); } void sort(CompareCallback cmp) { PtrArrayImpl::sort(cmp); } void heapSort() { heapSort(compareT); } - void heapSort(CompareCallback cmp) { PtrArrayImpl::heapSort_(cmp); } + void heapSort(CompareCallback cmp) { PtrArrayImpl::heapSort(cmp); } bool equal(const PtrArray& other, CompareCallback cmp) const {