2024-03-12 20:22阅读: 7评论: 0推荐: 0

基于 concept 实现的 Binary Heap

Binary Heap

一个基于 concept 的二叉堆板子实现。

template <typename Ty, typename Compare, typename Container = std::vector<Ty>>
requires requires(Compare comp, Ty a, Ty b) {
{ comp(a, b) } -> std::same_as<bool>;
} &&
requires(Container cont, Ty value) {
{ cont[0] } -> std::same_as<Ty &>;
{ cont.front() } -> std::same_as<Ty &>;
{ cont.back() } -> std::same_as<Ty &>;
cont.push_back(value);
cont.emplace_back(value);
cont.emplace_back(std::move(value));
cont.pop_back();
} && requires(const Container cont) {
{ cont[0] } -> std::same_as<const Ty &>;
{ cont.front() } -> std::same_as<const Ty &>;
{ cont.back() } -> std::same_as<const Ty &>;
{ cont.size() } -> std::same_as<std::size_t>;
{ cont.empty() } -> std::same_as<bool>;
}
class BinaryHeap {
public:
BinaryHeap() : cont_(), comp_() {}
explicit BinaryHeap(const Compare &comp) : cont_(), comp_(comp) {}
explicit BinaryHeap(Compare comp) : cont_(), comp_(std::move(comp)) {}
BinaryHeap(const BinaryHeap &other)
: cont_(other.cont_), comp_(other.comp_) {}
BinaryHeap(BinaryHeap &&other)
: cont_(std::move(other.cont_)), comp_(std::move(other.comp_)) {}
template <typename Comp, typename Cont>
explicit BinaryHeap(Comp &&comp, Cont &&cont)
: cont_(std::forward<Cont>(cont)), comp_(std::forward<Comp>(comp)) {
for (int i = static_cast<int>(this->cont_.size()) - 1; i >= 0; i--) {
this->Down(i);
}
}
auto operator=(BinaryHeap other) -> BinaryHeap & {
this->Swap(other);
return *this;
}
~BinaryHeap() noexcept = default;
auto Swap(BinaryHeap &other) noexcept -> void {
std::swap(this->cont_, other.cont_);
std::swap(this->comp_, other.comp_);
}
public:
auto Size() const noexcept -> std::size_t { return this->cont_.size(); }
auto Empty() const noexcept -> bool { return this->cont_.empty(); }
auto Push(Ty value) -> void {
this->cont_.emplace_back(std::move(value));
this->Up(this->Size() - 1);
}
template <typename... Ts>
requires requires(Ts &&...args) { Ty(std::forward<Ts>(args)...); }
auto Emplace(Ts &&...args) -> void {
this->cont_.emplace_back(std::forward<Ts>(args)...);
this->Up(this->Size() - 1);
}
auto Top() const -> const Ty & { return this->cont_[0]; }
auto Top() -> Ty & { return this->cont_[0]; }
auto Pop() -> void {
std::swap(this->cont_.front(), this->cont_.back());
this->cont_.pop_back();
this->Down(0);
}
private:
auto Up(std::size_t idx) -> void {
while (idx > 0 &&
this->comp_(this->cont_[idx], this->cont_[(idx - 1) / 2])) {
std::swap(this->cont_[(idx - 1) / 2], this->cont_[idx]);
idx = (idx - 1) / 2;
}
}
auto Down(std::size_t idx) -> void {
while (idx * 2 + 1 < this->Size()) {
auto t = idx * 2 + 1;
if (t + 1 < this->Size() &&
this->comp_(this->cont_[t + 1], this->cont_[t])) {
t += 1;
}
if (this->comp_(this->cont_[idx], this->cont_[t])) {
break;
}
std::swap(this->cont_[idx], this->cont_[t]);
idx = t;
}
}
private:
Container cont_;
Compare comp_;
};
template <typename Comp, typename Cont>
BinaryHeap(Comp &&, Cont &&)
-> BinaryHeap<typename Cont::value_type, Comp, Cont>;

本文作者:フランドール·スカーレット

本文链接:https://www.cnblogs.com/FlandreScarlet/p/18069160

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   フランドール·スカーレット  阅读(7)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
收起
  1. 1 Scarborough Fair 山田タマル
  2. 2 Faster Than Light Paradox Interactive,Andreas Waldetoft
  3. 3 世界は可愛く出来ている 上海アリス幻樂団
Scarborough Fair - 山田タマル
00:00 / 00:00
An audio error has occurred, player will skip forward in 2 seconds.

作词 : Pual Simon

作曲 : Pual Simon

作词 : イングランド民謡

作曲:加藤达也

Are you going to Scarborough Fair?

Parsley, sage, rosemary and thyme

Remember me to one who lives there

He once was a true love of mine

Tell him to make me a cambric shirt

Tell him to make me a cambric shirt

Parsley, sage, rosemary and thyme

Without no seams nor needlework

Then he'll be a true love of mine.

Tell him to find me an acre of land

Tell him to find me an acre of land

Parsley, sage, rosemary and thyme

Between the salt water and the sea strand

Then he'll be a true love of mine

Are you going to Scarborough Fair?