21template <
typename T,
typename I,
typename P = std::less<T>>
36 size_t size()
const {
return heap_.size(); }
55 bool less_(
size_t posA,
size_t posB )
const;
57 void lift_(
size_t pos, I elemId );
60 std::vector<Element> heap_;
65template <
typename T,
typename I,
typename P>
67 : heap_(
size, { I(), def } )
68 , id2PosInHeap_(
size )
72 for ( I i{ size_t( 0 ) }; i <
size; ++i )
79template <
typename T,
typename I,
typename P>
81 : heap_( std::move( elms ) )
82 , id2PosInHeap_( heap_.
size() )
86 std::make_heap( heap_.begin(), heap_.end(), [
this](
const Element & a,
const Element & b )
88 if ( pred_( a.val, b.val ) )
90 if ( pred_( b.val, a.val ) )
95 for (
size_t i = 0; i < heap_.size(); ++i )
96 id2PosInHeap_[heap_[i].
id] = i;
99template <
typename T,
typename I,
typename P>
103 assert ( heap_.size() == id2PosInHeap_.size() );
104 while ( heap_.size() <
size )
107 heap_.push_back( { i, def } );
108 id2PosInHeap_.push_back( i );
111 assert ( heap_.size() == id2PosInHeap_.size() );
114template <
typename T,
typename I,
typename P>
117 size_t pos = id2PosInHeap_[ elemId ];
118 assert( heap_[pos].
id == elemId );
119 if ( pred_( newVal, heap_[pos].val ) )
120 setSmallerValue( elemId, newVal );
121 else if ( pred_( heap_[pos].val, newVal ) )
122 setLargerValue( elemId, newVal );
125template <
typename T,
typename I,
typename P>
128 size_t pos = id2PosInHeap_[ elemId ];
129 assert( heap_[pos].
id == elemId );
130 assert( !( pred_( newVal, heap_[pos].val ) ) );
131 heap_[pos].val = newVal;
132 lift_( pos, elemId );
135template <
typename T,
typename I,
typename P>
140 size_t parentPos = ( pos - 1 ) / 2;
141 if ( !( less_( parentPos, pos ) ) )
143 auto parentId = heap_[parentPos].id;
144 assert( id2PosInHeap_[parentId] == parentPos );
145 std::swap( heap_[parentPos], heap_[pos] );
146 std::swap( parentPos, pos );
147 id2PosInHeap_[parentId] = parentPos;
149 id2PosInHeap_[elemId] = pos;
152template <
typename T,
typename I,
typename P>
155 size_t pos = id2PosInHeap_[ elemId ];
156 assert( heap_[pos].
id == elemId );
157 assert( !( pred_( heap_[pos].val, newVal ) ) );
158 heap_[pos].val = newVal;
161 size_t child1Pos = 2 * pos + 1;
162 if ( child1Pos >= heap_.size() )
164 auto child1Id = heap_[child1Pos].id;
165 size_t child2Pos = 2 * pos + 2;
166 if ( child2Pos >= heap_.size() )
168 assert( id2PosInHeap_[child1Id] == child1Pos );
169 if ( !( less_( child1Pos, pos ) ) )
171 std::swap( heap_[child1Pos], heap_[pos] );
172 std::swap( child1Pos, pos );
173 id2PosInHeap_[child1Id] = child1Pos;
177 auto child2Id = heap_[child2Pos].id;
178 if ( !( less_( child1Pos, pos ) ) && !( less_( child1Pos, child2Pos ) ) )
180 std::swap( heap_[child1Pos], heap_[pos] );
181 std::swap( child1Pos, pos );
182 id2PosInHeap_[child1Id] = child1Pos;
184 else if ( !( less_( child2Pos, pos ) ) )
186 assert( !( less_( child2Pos, child1Pos ) ) );
187 std::swap( heap_[child2Pos], heap_[pos] );
188 std::swap( child2Pos, pos );
189 id2PosInHeap_[child2Id] = child2Pos;
193 assert( !( less_( pos, child1Pos ) ) );
194 assert( !( less_( pos, child2Pos ) ) );
198 id2PosInHeap_[elemId] = pos;
201template <
typename T,
typename I,
typename P>
204 const auto & a = heap_[posA];
205 const auto & b = heap_[posB];
206 if ( pred_( a.val, b.val ) )
208 if ( pred_( b.val, a.val ) )
#define MR_LIFETIMEBOUND
Definition MRMacros.h:81
#define MR_LIFETIME_CAPTURE_BY_NESTED(x)
Definition MRMacros.h:86
#define MR_LIFETIMEBOUND_NESTED
Definition MRMacros.h:84
#define MR_TIMER
namespace MR
Definition MRTimer.h:56
stores map from element id in[0, size) to T;
Definition MRHeap.h:23
const Element & top() const MR_LIFETIMEBOUND
returns the element with the largest value
Definition MRHeap.h:42
size_t size() const
returns the size of the heap
Definition MRHeap.h:36
const T & value(I elemId) const MR_LIFETIMEBOUND
returns the value associated with given element
Definition MRHeap.h:40
Element setTopValue(const T &newVal MR_LIFETIME_CAPTURE_BY_NESTED(this))
sets new value to the current top element, returning its previous value
Definition MRHeap.h:51
void increaseValue(I elemId, const U &inc)
Definition MRHeap.h:49
std::vector<T>-like container that requires specific indexing type,
Definition MRVector.h:23
void setValue(I elemId, const T &newVal MR_LIFETIME_CAPTURE_BY_NESTED(this))
sets new value to given element
Definition MRHeap.h:115
void resize(size_t size, T def MR_LIFETIME_CAPTURE_BY_NESTED(this)={})
increases the size of the heap by adding elements at the end
Definition MRHeap.h:100
void setSmallerValue(I elemId, const T &newVal MR_LIFETIME_CAPTURE_BY_NESTED(this))
Definition MRHeap.h:153
void setLargerValue(I elemId, const T &newVal MR_LIFETIME_CAPTURE_BY_NESTED(this))
sets new value to given element, which shall be larger/smaller than the current value
Definition MRHeap.h:126
Heap(size_t size, T def MR_LIFETIMEBOUND_NESTED={}, P pred={})
constructs heap for given number of elements, assigning given default value to each element
Definition MRHeap.h:66
ImVec2 size(const ViewportRectangle &rect)
Definition MRViewport.h:32
only for bindings generation
Definition MRCameraOrientationPlugin.h:8
T val
Definition MRHeap.h:28
I id
Definition MRHeap.h:27