chore(cpp): introduce data structures

mainly wrappers around heaps for now

Signed-off-by: Matej Focko <mfocko@redhat.com>
This commit is contained in:
Matej Focko 2023-08-12 23:16:31 +02:00
parent c8035c22dc
commit e928da17af
Signed by: mfocko
GPG key ID: 7C47D46246790496

View file

@ -47,6 +47,52 @@ using i64 = std::int64_t;
using u64 = std::uint64_t; using u64 = std::uint64_t;
#pragma endregion /* aliases */ #pragma endregion /* aliases */
#pragma region data structures
template <typename T>
struct max_heap {
using container = std::vector<T>;
typename container::size_type size() const { return h.size(); }
void push(T item) {
h.push_back(item);
std::push_heap(h.begin(), h.end());
}
T pop() {
std::pop_heap(h.begin(), h.end());
T item = std::move(h.back());
h.pop_back();
return item;
}
private:
container h;
};
template <typename T>
struct min_heap {
using container = std::vector<T>;
typename container::size_type size() const { return h.size(); }
void push(T item) {
h.push_back(item);
std::push_heap(h.begin(), h.end(), std::greater<>{});
}
T pop() {
std::pop_heap(h.begin(), h.end(), std::greater<>{});
T item = std::move(h.back());
h.pop_back();
return item;
}
private:
container h;
};
#pragma endregion /* data structures */
#pragma region debug #pragma region debug
void dbg_out() { std::cerr << std::endl; } void dbg_out() { std::cerr << std::endl; }
template <typename Head, typename... Tail> template <typename Head, typename... Tail>