1
0
Fork 0
cp-templates/trees/assoc_heap.hh

76 lines
2.3 KiB
C++
Raw Normal View History

2024-02-02 22:31:03 +08:00
template <typename _Tp, typename _Sequence = vector<_Tp>,
typename _T_Hash = hash<_Tp>>
class assoc_heap {
private:
template <typename T> static constexpr auto __map_types(T _t) {
return make_tuple(std::priority_queue<_Tp, _Sequence, T>(_t));
}
2024-02-02 22:39:35 +08:00
2024-02-02 22:31:03 +08:00
template <typename T, typename... U> static constexpr auto __map_types(T _t, U... _o) {
return tuple_cat(make_tuple(std::priority_queue<_Tp, _Sequence, T>(_t)), __map_types(_o...));
}
2024-02-02 22:39:35 +08:00
template <typename T> static constexpr void __push(const _Tp& _val, T& _c) {
2024-02-02 22:31:03 +08:00
_c.push(_val);
}
2024-02-02 22:39:35 +08:00
template <typename T, typename... U> static constexpr void __push(const _Tp& _val, T& _c, U&... _o) {
2024-02-02 22:31:03 +08:00
__push(_val, _c);
__push(_val, _o...);
}
template <typename _Tc, typename _T_CC> class __assoc_heap {
private:
_Tc _content;
_T_CC _comp;
unordered_map<_Tp, size_t, _T_Hash> _count;
size_t _size;
template <size_t i> void __roll() {
while (get<i>(_content).size() && !_count[get<i>(_content).top()]) {
get<i>(_content).pop();
}
}
public:
__assoc_heap(_Tc _content, _T_CC _comp) :
_content(_content), _comp(_comp), _size(0) {}
2024-02-03 00:51:40 +08:00
template <size_t i> constexpr const _Tp& top() {
2024-02-02 22:31:03 +08:00
__roll<i>();
return get<i>(_content).top();
}
template <size_t i> constexpr void pop() {
__roll<i>();
2024-02-02 22:39:35 +08:00
_count[top<i>()] -= 1;
2024-02-02 22:31:03 +08:00
--_size;
get<i>(_content).pop();
}
2024-02-02 22:39:35 +08:00
constexpr void push(const _Tp& __val) {
2024-02-02 22:31:03 +08:00
_count[__val] += 1;
++_size;
apply([&] (auto&... cs) { __push(__val, cs...); }, _content);
}
constexpr size_t size() const {
return _size;
}
constexpr bool empty() const {
return !_size;
}
2024-02-02 22:39:35 +08:00
template <typename... T> constexpr void emplace(const T&... _val) {
2024-02-02 22:31:03 +08:00
push(_Tp(_val...));
}
};
public:
2024-02-03 00:51:40 +08:00
template<typename... _T_cs> static auto make(_T_cs... _comp) {
auto comp = make_tuple(_comp...);
auto _container = apply([&](auto... all) { return __map_types(all...); }, comp);
return __assoc_heap<decltype(_container), tuple<_T_cs...>>(_container, std::move(comp));
2024-02-02 22:31:03 +08:00
}
};