Grok 20.3.2
MinHeap.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2016-2026 Grok Image Compression Inc.
3 *
4 * This source code is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU Affero General Public License, version 3,
6 * as published by the Free Software Foundation.
7 *
8 * This source code is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU Affero General Public License for more details.
12 *
13 * You should have received a copy of the GNU Affero General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 */
17
18#pragma once
19
20#include <mutex>
21#include <queue>
22#include <vector>
23#include <concepts>
24#include <optional>
25
26namespace grk
27{
28
32template<typename T>
34{
35public:
40 void push(T index)
41 {
42 queue.push(index);
43 }
44
51 std::optional<T> push_and_pop(T index)
52 {
53 queue.push(index);
54
55 // While the top of the heap matches the expected next index, pop it and extend the sequence
56 while(!queue.empty() && queue.top() == start)
57 {
58 queue.pop();
59 start++;
60 }
61
62 // Return the greatest index in the contiguous sequence
63 // If start is still 0, no sequence has been established
64 return start > 0 ? std::optional<T>(start - 1) : std::nullopt;
65 }
66
71 size_t size() const
72 {
73 return queue.size();
74 }
75
76private:
77 std::priority_queue<T, std::vector<T>, std::greater<T>> queue;
78 T start = 0;
79};
80
85{
86public:
87 explicit MinHeapLocker(std::mutex& mut) : lock(mut) {}
88
89private:
90 std::lock_guard<std::mutex> lock;
91};
92
97{
98public:
99 explicit MinHeapFakeLocker([[maybe_unused]] std::mutex& mut) {}
100};
101
106template<typename T>
107concept HasGetIndex = requires(T t) {
108 { t.getIndex() } -> std::integral;
109};
110
115template<typename T>
116 requires HasGetIndex<T>
118{
119 bool operator()(const T& a, const T& b) const
120 {
121 return a.getIndex() > b.getIndex();
122 }
123};
124
129template<typename IT>
131{
132protected:
134 mutable std::mutex queue_mutex;
135 IT start;
136};
137
144template<typename T, typename IT, typename L>
145 requires HasGetIndex<T> && std::integral<IT>
146class MinHeap : protected MinHeapBase<IT>
147{
148public:
149 using MinHeapBase<IT>::queue_mutex;
150 using MinHeapBase<IT>::start;
151
156 void push(const T& val)
157 {
158 L locker(queue_mutex);
159 queue.push(val);
160 }
161
167 std::vector<T> push_and_pop(const T& val)
168 {
169 L locker(queue_mutex);
170 queue.push(val);
171 std::vector<T> inSequence;
172 while(!queue.empty() && queue.top().getIndex() == start)
173 {
174 inSequence.push_back(queue.top());
175 queue.pop();
176 start++;
177 }
178 return inSequence;
179 }
180
185 size_t size() const
186 {
187 std::lock_guard<std::mutex> locker(queue_mutex);
188 return queue.size();
189 }
190
191private:
192 std::priority_queue<T, std::vector<T>, MinHeapComparator<T>> queue;
193};
194
199template<typename T>
200 requires HasGetIndex<T>
202{
203 bool operator()(const T* a, const T* b) const
204 {
205 return a->getIndex() > b->getIndex();
206 }
207};
208
215template<typename T, typename IT, typename L>
216 requires HasGetIndex<T> && std::integral<IT>
217class MinHeapPtr : protected MinHeapBase<IT>
218{
219public:
220 using MinHeapBase<IT>::queue_mutex;
221 using MinHeapBase<IT>::start;
222
227 void push(T* val)
228 {
229 L locker(queue_mutex);
230 queue.push(val);
231 }
232
238 std::vector<T*> pop(T* val)
239 {
240 L locker(queue_mutex);
241 if(val)
242 queue.push(val);
243 std::vector<T*> inSequence;
244 while(!queue.empty() && queue.top()->getIndex() == start)
245 {
246 inSequence.push_back(queue.top());
247 queue.pop();
248 start++;
249 }
250 return inSequence;
251 }
252
257 size_t size() const
258 {
259 std::lock_guard<std::mutex> locker(queue_mutex);
260 return queue.size();
261 }
262
263private:
264 std::priority_queue<T*, std::vector<T*>, MinHeapPtrComparator<T>> queue;
265};
266
267} // namespace grk
MinHeapBase()
Definition MinHeap.h:133
IT start
Tracks the next expected index for sequential popping.
Definition MinHeap.h:135
std::mutex queue_mutex
Mutex for thread-safe access.
Definition MinHeap.h:134
MinHeapFakeLocker(std::mutex &mut)
Definition MinHeap.h:99
Thread-safe min-heap for values with customizable locking.
Definition MinHeap.h:147
size_t size() const
Returns the current size of the heap.
Definition MinHeap.h:185
std::priority_queue< T, std::vector< T >, MinHeapComparator< T > > queue
Underlying min-heap.
Definition MinHeap.h:192
std::vector< T > push_and_pop(const T &val)
Pushes a value and pops all consecutive elements starting from 'start'.
Definition MinHeap.h:167
void push(const T &val)
Pushes a value onto the heap.
Definition MinHeap.h:156
MinHeapLocker(std::mutex &mut)
Definition MinHeap.h:87
std::lock_guard< std::mutex > lock
Definition MinHeap.h:90
Thread-safe min-heap for pointers with customizable locking.
Definition MinHeap.h:218
std::vector< T * > pop(T *val)
Optionally pushes a pointer and pops all consecutive elements starting from 'start'.
Definition MinHeap.h:238
std::priority_queue< T *, std::vector< T * >, MinHeapPtrComparator< T > > queue
Underlying min-heap.
Definition MinHeap.h:264
void push(T *val)
Pushes a pointer onto the heap.
Definition MinHeap.h:227
size_t size() const
Returns the current size of the heap.
Definition MinHeap.h:257
A simple non-thread-safe min-heap for tracking contiguous sequences of size_t indices.
Definition MinHeap.h:34
std::priority_queue< T, std::vector< T >, std::greater< T > > queue
Min-heap of indices.
Definition MinHeap.h:77
void push(T index)
Pushes an index onto the heap.
Definition MinHeap.h:40
std::optional< T > push_and_pop(T index)
Pushes an index and returns the greatest integer at the end of the contiguous sequence.
Definition MinHeap.h:51
size_t size() const
Returns the current size of the heap.
Definition MinHeap.h:71
T start
Tracks the next expected index in the sequence.
Definition MinHeap.h:78
Concept ensuring a type T has a getIndex() method returning an integral type.
Definition MinHeap.h:107
ResWindow.
Definition CompressedChunkCache.h:36
Comparator for min-heap ordering based on getIndex() (value version).
Definition MinHeap.h:118
bool operator()(const T &a, const T &b) const
Definition MinHeap.h:119
Comparator for min-heap ordering based on getIndex() (pointer version).
Definition MinHeap.h:202
bool operator()(const T *a, const T *b) const
Definition MinHeap.h:203