mirror of
https://gitlab.com/mfocko/Codeforces.git
synced 2024-11-09 13:49:06 +01:00
chore(cpp): introduce data structures
mainly wrappers around heaps for now Signed-off-by: Matej Focko <mfocko@redhat.com>
This commit is contained in:
parent
c8035c22dc
commit
e928da17af
1 changed files with 46 additions and 0 deletions
|
@ -47,6 +47,52 @@ using i64 = std::int64_t;
|
|||
using u64 = std::uint64_t;
|
||||
#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
|
||||
void dbg_out() { std::cerr << std::endl; }
|
||||
template <typename Head, typename... Tail>
|
||||
|
|
Loading…
Reference in a new issue