#ifndef EXPIRING_SET_INL_H_ #error "Direct inclusion of this file is not allowed, include expiring_set.h" // For the sake of sane code completion. #include "expiring_set.h" #endif namespace NYT { //////////////////////////////////////////////////////////////////////////////// template void TExpiringSet::SetTtl(TDuration ttl) { Ttl_ = ttl; } template void TExpiringSet::Insert(TInstant now, const TItem& item) { Expire(now); auto deadline = now + Ttl_; ItemToDeadline_[item] = deadline; ExpirationQueue_.push(TItemPack{.Items = {item}, .Deadline = deadline}); } template template void TExpiringSet::InsertMany(TInstant now, const TItems& items) { Expire(now); auto deadline = now + Ttl_; for (const auto& item : items) { ItemToDeadline_[item] = deadline; } ExpirationQueue_.push(TItemPack{.Items = {items.begin(), items.end()}, .Deadline = deadline}); } template void TExpiringSet::Remove(const TItem& item) { ItemToDeadline_.erase(item); } template void TExpiringSet::Expire(TInstant now) { while (!ExpirationQueue_.empty() && ExpirationQueue_.top().Deadline <= now) { for (const auto& item : ExpirationQueue_.top().Items) { if (auto it = ItemToDeadline_.find(item); it != ItemToDeadline_.end()) { if (it->second <= now) { ItemToDeadline_.erase(it); } } } ExpirationQueue_.pop(); } } template void TExpiringSet::Clear() { ItemToDeadline_ = {}; ExpirationQueue_ = {}; } template template bool TExpiringSet::Contains(const TItemLike& item) const { return ItemToDeadline_.contains(item); } template int TExpiringSet::GetSize() const { return std::ssize(ItemToDeadline_); } template bool TExpiringSet::TItemPack::operator<(const TItemPack& other) const { // Reversed ordering for the priority queue. return Deadline > other.Deadline; } //////////////////////////////////////////////////////////////////////////////// } // namespace NYT