fastqueue.h 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. #pragma once
  2. #include <util/memory/smallobj.h>
  3. #include "ptr.h"
  4. template <class T>
  5. class TFastQueue {
  6. struct THelper: public TObjectFromPool<THelper>, public TIntrusiveListItem<THelper> {
  7. inline THelper(const T& t)
  8. : Obj(t)
  9. {
  10. }
  11. T Obj;
  12. };
  13. public:
  14. inline TFastQueue()
  15. : Pool_(TDefaultAllocator::Instance())
  16. , Size_(0)
  17. {
  18. }
  19. inline void Push(const T& t) {
  20. Queue_.PushFront(new (&Pool_) THelper(t));
  21. ++Size_;
  22. }
  23. inline T Pop() {
  24. Y_ASSERT(!this->Empty());
  25. THolder<THelper> tmp(Queue_.PopBack());
  26. --Size_;
  27. return tmp->Obj;
  28. }
  29. inline size_t Size() const noexcept {
  30. return Size_;
  31. }
  32. Y_PURE_FUNCTION inline bool Empty() const noexcept {
  33. return !this->Size();
  34. }
  35. inline explicit operator bool() const noexcept {
  36. return !this->Empty();
  37. }
  38. private:
  39. typename THelper::TPool Pool_;
  40. TIntrusiveListWithAutoDelete<THelper, TDelete> Queue_;
  41. size_t Size_;
  42. };