compact_queue-inl.h 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. #include "compact_queue.h"
  2. namespace NYT {
  3. ////////////////////////////////////////////////////////////////////////////////
  4. template <class T, size_t N>
  5. void TCompactQueue<T, N>::Push(T value)
  6. {
  7. if (Size_ == Queue_.size()) {
  8. auto oldSize = Queue_.size();
  9. Queue_.resize(2 * oldSize);
  10. if (FrontIndex_ + Size_ > oldSize) {
  11. std::move(
  12. Queue_.begin(),
  13. Queue_.begin() + FrontIndex_,
  14. Queue_.begin() + Size_);
  15. }
  16. }
  17. auto index = FrontIndex_ + Size_;
  18. if (index >= Queue_.size()) {
  19. index -= Queue_.size();
  20. }
  21. Queue_[index] = std::move(value);
  22. ++Size_;
  23. }
  24. template <class T, size_t N>
  25. T TCompactQueue<T, N>::Pop()
  26. {
  27. YT_VERIFY(!Empty());
  28. auto value = std::move(Queue_[FrontIndex_]);
  29. ++FrontIndex_;
  30. if (FrontIndex_ >= Queue_.size()) {
  31. FrontIndex_ -= Queue_.size();
  32. }
  33. --Size_;
  34. return value;
  35. }
  36. template <class T, size_t N>
  37. const T& TCompactQueue<T, N>::Front() const
  38. {
  39. return Queue_[FrontIndex_];
  40. }
  41. template <class T, size_t N>
  42. size_t TCompactQueue<T, N>::Size() const
  43. {
  44. return Size_;
  45. }
  46. template <class T, size_t N>
  47. size_t TCompactQueue<T, N>::Capacity() const
  48. {
  49. return Queue_.capacity();
  50. }
  51. template <class T, size_t N>
  52. bool TCompactQueue<T, N>::Empty() const
  53. {
  54. return Size_ == 0;
  55. }
  56. ////////////////////////////////////////////////////////////////////////////////
  57. } // namespace NYT