expiring_set-inl.h 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #ifndef EXPIRING_SET_INL_H_
  2. #error "Direct inclusion of this file is not allowed, include expiring_set.h"
  3. // For the sake of sane code completion.
  4. #include "expiring_set.h"
  5. #endif
  6. namespace NYT {
  7. ////////////////////////////////////////////////////////////////////////////////
  8. template <class TItem, class THash, class TEqual>
  9. void TExpiringSet<TItem, THash, TEqual>::SetTtl(TDuration ttl)
  10. {
  11. Ttl_ = ttl;
  12. }
  13. template <class TItem, class THash, class TEqual>
  14. void TExpiringSet<TItem, THash, TEqual>::Insert(TInstant now, const TItem& item)
  15. {
  16. Expire(now);
  17. auto deadline = now + Ttl_;
  18. ItemToDeadline_[item] = deadline;
  19. ExpirationQueue_.push(TItemPack{.Items = {item}, .Deadline = deadline});
  20. }
  21. template <class TItem, class THash, class TEqual>
  22. template <class TItems>
  23. void TExpiringSet<TItem, THash, TEqual>::InsertMany(TInstant now, const TItems& items)
  24. {
  25. Expire(now);
  26. auto deadline = now + Ttl_;
  27. for (const auto& item : items) {
  28. ItemToDeadline_[item] = deadline;
  29. }
  30. ExpirationQueue_.push(TItemPack{.Items = {items.begin(), items.end()}, .Deadline = deadline});
  31. }
  32. template <class TItem, class THash, class TEqual>
  33. void TExpiringSet<TItem, THash, TEqual>::Remove(const TItem& item)
  34. {
  35. ItemToDeadline_.erase(item);
  36. }
  37. template <class TItem, class THash, class TEqual>
  38. void TExpiringSet<TItem, THash, TEqual>::Expire(TInstant now)
  39. {
  40. while (!ExpirationQueue_.empty() && ExpirationQueue_.top().Deadline <= now) {
  41. for (const auto& item : ExpirationQueue_.top().Items) {
  42. if (auto it = ItemToDeadline_.find(item); it != ItemToDeadline_.end()) {
  43. if (it->second <= now) {
  44. ItemToDeadline_.erase(it);
  45. }
  46. }
  47. }
  48. ExpirationQueue_.pop();
  49. }
  50. }
  51. template <class TItem, class THash, class TEqual>
  52. void TExpiringSet<TItem, THash, TEqual>::Clear()
  53. {
  54. ItemToDeadline_ = {};
  55. ExpirationQueue_ = {};
  56. }
  57. template <class TItem, class THash, class TEqual>
  58. template <class TItemLike>
  59. bool TExpiringSet<TItem, THash, TEqual>::Contains(const TItemLike& item) const
  60. {
  61. return ItemToDeadline_.contains(item);
  62. }
  63. template <class TItem, class THash, class TEqual>
  64. int TExpiringSet<TItem, THash, TEqual>::GetSize() const
  65. {
  66. return std::ssize(ItemToDeadline_);
  67. }
  68. template <class TItem, class THash, class TEqual>
  69. bool TExpiringSet<TItem, THash, TEqual>::TItemPack::operator<(const TItemPack& other) const
  70. {
  71. // Reversed ordering for the priority queue.
  72. return Deadline > other.Deadline;
  73. }
  74. ////////////////////////////////////////////////////////////////////////////////
  75. } // namespace NYT