aboutsummaryrefslogtreecommitdiffstats
path: root/util/generic/fastqueue.h
blob: 6f01fec4d637dd85723742cda788c16512c7e5da (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#pragma once

#include <util/memory/smallobj.h>
#include "ptr.h"

template <class T>
class TFastQueue {
    struct THelper: public TObjectFromPool<THelper>, public TIntrusiveListItem<THelper> {
        inline THelper(const T& t)
            : Obj(t)
        {
        }

        T Obj;
    };

public:
    inline TFastQueue()
        : Pool_(TDefaultAllocator::Instance())
        , Size_(0)
    {
    }

    inline void Push(const T& t) {
        Queue_.PushFront(new (&Pool_) THelper(t));
        ++Size_;
    }

    inline T Pop() {
        Y_ASSERT(!this->Empty()); 

        THolder<THelper> tmp(Queue_.PopBack());
        --Size_;

        return tmp->Obj;
    }

    inline size_t Size() const noexcept {
        return Size_;
    }

    Y_PURE_FUNCTION inline bool Empty() const noexcept {
        return !this->Size();
    }

    inline explicit operator bool() const noexcept {
        return !this->Empty();
    }

private:
    typename THelper::TPool Pool_;
    TIntrusiveListWithAutoDelete<THelper, TDelete> Queue_;
    size_t Size_;
};