MinHeap.h
Go to the documentation of this file.
#pragma once
#include <mutex>
#include <queue>
namespace grk
{
class MinHeapLocker
{
public:
MinHeapLocker(std::mutex& mut) : lock(mut) {}
private:
std::lock_guard<std::mutex> lock;
};
class MinHeapFakeLocker
{
public:
MinHeapFakeLocker([[maybe_unused]] std::mutex& mut) {}
};
template<typename T>
struct MinHeapComparator
{
bool operator()(const T a, const T b) const
{
return a.getIndex() > b.getIndex();
}
};
template<typename T, typename IT, typename L>
class MinHeap
{
public:
MinHeap() : nextIndex(0) {}
void push(T val)
{
L locker(queue_mutex);
queue.push(val);
}
T pop(void)
{
L locker(queue_mutex);
if(queue.empty())
return T();
auto val = queue.top();
if(val.getIndex() == nextIndex)
{
queue.pop();
nextIndex++;
return val;
}
return T();
}
size_t size(void)
{
return queue.size();
}
private:
std::priority_queue<T, std::vector<T>, MinHeapComparator<T>> queue;
std::mutex queue_mutex;
IT nextIndex;
};
template<typename T>
struct MinHeapPtrComparator
{
bool operator()(const T* a, const T* b) const
{
return a->getIndex() > b->getIndex();
}
};
template<typename T, typename IT, typename L>
class MinHeapPtr
{
public:
MinHeapPtr() : nextIndex(0) {}
void push(T* val)
{
L locker(queue_mutex);
queue.push(val);
}
T* pop(void)
{
L locker(queue_mutex);
if(queue.empty())
return nullptr;
auto val = queue.top();
if(val->getIndex() == nextIndex)
{
queue.pop();
nextIndex++;
return val;
}
return nullptr;
}
size_t size(void)
{
return queue.size();
}
private:
std::priority_queue<T*, std::vector<T*>, MinHeapPtrComparator<T>> queue;
std::mutex queue_mutex;
IT nextIndex;
};
} // namespace grk
grk::MinHeapFakeLocker
Definition MinHeap.h:18
grk::MinHeapFakeLocker::MinHeapFakeLocker
MinHeapFakeLocker(std::mutex &mut)
Definition MinHeap.h:20
grk::MinHeap
Definition MinHeap.h:34
grk::MinHeap::queue_mutex
std::mutex queue_mutex
Definition MinHeap.h:63
grk::MinHeap::queue
std::priority_queue< T, std::vector< T >, MinHeapComparator< T > > queue
Definition MinHeap.h:62
grk::MinHeap::nextIndex
IT nextIndex
Definition MinHeap.h:64
grk::MinHeap::pop
T pop(void)
Definition MinHeap.h:42
grk::MinHeap::size
size_t size(void)
Definition MinHeap.h:56
grk::MinHeap::MinHeap
MinHeap()
Definition MinHeap.h:36
grk::MinHeap::push
void push(T val)
Definition MinHeap.h:37
grk::MinHeapLocker
Definition MinHeap.h:9
grk::MinHeapLocker::MinHeapLocker
MinHeapLocker(std::mutex &mut)
Definition MinHeap.h:11
grk::MinHeapLocker::lock
std::lock_guard< std::mutex > lock
Definition MinHeap.h:14
grk::MinHeapPtr
Definition MinHeap.h:78
grk::MinHeapPtr::MinHeapPtr
MinHeapPtr()
Definition MinHeap.h:80
grk::MinHeapPtr::queue
std::priority_queue< T *, std::vector< T * >, MinHeapPtrComparator< T > > queue
Definition MinHeap.h:106
grk::MinHeapPtr::push
void push(T *val)
Definition MinHeap.h:81
grk::MinHeapPtr::queue_mutex
std::mutex queue_mutex
Definition MinHeap.h:107
grk::MinHeapPtr::pop
T * pop(void)
Definition MinHeap.h:86
grk::MinHeapPtr::nextIndex
IT nextIndex
Definition MinHeap.h:108
grk::MinHeapPtr::size
size_t size(void)
Definition MinHeap.h:100
grk
Copyright (C) 2016-2023 Grok Image Compression Inc.
Definition ICacheable.h:20
grk::grk_read
void grk_read(const uint8_t *buffer, TYPE *value, uint32_t numBytes)
Definition BufferedStream.h:239
grk::MinHeapComparator
Definition MinHeap.h:25
grk::MinHeapComparator::operator()
bool operator()(const T a, const T b) const
Definition MinHeap.h:26
grk::MinHeapPtrComparator
Definition MinHeap.h:69
grk::MinHeapPtrComparator::operator()
bool operator()(const T *a, const T *b) const
Definition MinHeap.h:70