Jolt Physics
A multi core friendly Game Physics Engine
Loading...
Searching...
No Matches
BinaryHeap.h
Go to the documentation of this file.
1// Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
2// SPDX-FileCopyrightText: 2024 Jorrit Rouwe
3// SPDX-License-Identifier: MIT
4
5#pragma once
6
8
13template <typename Iterator, typename Pred>
14void BinaryHeapPush(Iterator inBegin, Iterator inEnd, Pred inPred)
15{
16 using diff_t = typename std::iterator_traits<Iterator>::difference_type;
17 using elem_t = typename std::iterator_traits<Iterator>::value_type;
18
19 // New heap size
20 diff_t count = std::distance(inBegin, inEnd);
21
22 // Start from the last element
23 diff_t current = count - 1;
24 while (current > 0)
25 {
26 // Get current element
27 elem_t &current_elem = *(inBegin + current);
28
29 // Get parent element
30 diff_t parent = (current - 1) >> 1;
31 elem_t &parent_elem = *(inBegin + parent);
32
33 // Sort them so that the parent is larger than the child
34 if (inPred(parent_elem, current_elem))
35 {
36 std::swap(parent_elem, current_elem);
37 current = parent;
38 }
39 else
40 {
41 // When there's no change, we're done
42 break;
43 }
44 }
45}
46
51template <typename Iterator, typename Pred>
52void BinaryHeapPop(Iterator inBegin, Iterator inEnd, Pred inPred)
53{
54 using diff_t = typename std::iterator_traits<Iterator>::difference_type;
55
56 // Begin by moving the highest element to the end, this is the popped element
57 std::swap(*(inEnd - 1), *inBegin);
58
59 // New heap size
60 diff_t count = std::distance(inBegin, inEnd) - 1;
61
62 // Start from the root
63 diff_t largest = 0;
64 for (;;)
65 {
66 // Get first child
67 diff_t child = (largest << 1) + 1;
68
69 // Check if we're beyond the end of the heap, if so the 2nd child is also beyond the end
70 if (child >= count)
71 break;
72
73 // Remember the largest element from the previous iteration
74 diff_t prev_largest = largest;
75
76 // Check if first child is bigger, if so select it
77 if (inPred(*(inBegin + largest), *(inBegin + child)))
78 largest = child;
79
80 // Switch to the second child
81 ++child;
82
83 // Check if second child is bigger, if so select it
84 if (child < count && inPred(*(inBegin + largest), *(inBegin + child)))
85 largest = child;
86
87 // If there was no change, we're done
88 if (prev_largest == largest)
89 break;
90
91 // Swap element
92 std::swap(*(inBegin + prev_largest), *(inBegin + largest));
93 }
94}
95
void BinaryHeapPop(Iterator inBegin, Iterator inEnd, Pred inPred)
Definition BinaryHeap.h:52
JPH_NAMESPACE_BEGIN void BinaryHeapPush(Iterator inBegin, Iterator inEnd, Pred inPred)
Definition BinaryHeap.h:14
#define JPH_NAMESPACE_END
Definition Core.h:414
#define JPH_NAMESPACE_BEGIN
Definition Core.h:408