aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/actors/util/recentwnd.h
blob: 29425301e40dc444bd2650db0b8c69aad13c0c25 (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
55
56
57
58
59
60
61
62
63
64
65
66
67
#pragma once 

#include <util/generic/deque.h> 
 
template <typename TElem, 
          template <typename, typename...> class TContainer = TDeque>
class TRecentWnd { 
public: 
    TRecentWnd(ui32 wndSize)
        : MaxWndSize_(wndSize)
    {
    }
 
    void Push(const TElem& elem) { 
        if (Window_.size() == MaxWndSize_) 
            Window_.erase(Window_.begin()); 
        Window_.emplace_back(elem); 
    } 
 
    void Push(TElem&& elem) { 
        if (Window_.size() == MaxWndSize_) 
            Window_.erase(Window_.begin()); 
        Window_.emplace_back(std::move(elem)); 
    } 
 
    TElem& Last() {
        return Window_.back();
    }
    const TElem& Last() const {
        return Window_.back();
    }
    bool Full() const {
        return Window_.size() == MaxWndSize_;
    }
    ui64 Size() const {
        return Window_.size();
    }
 
    using const_iterator = typename TContainer<TElem>::const_iterator; 
 
    const_iterator begin() {
        return Window_.begin();
    }
    const_iterator end() {
        return Window_.end();
    }
 
    void Reset(ui32 wndSize = 0) { 
        Window_.clear(); 
        if (wndSize != 0) { 
            MaxWndSize_ = wndSize; 
        } 
    } 
 
    void ResetWnd(ui32 wndSize) { 
        Y_VERIFY(wndSize != 0); 
        MaxWndSize_ = wndSize; 
        if (Window_.size() > MaxWndSize_) { 
            Window_.erase(Window_.begin(), 
                          Window_.begin() + Window_.size() - MaxWndSize_); 
        } 
    } 

private: 
    TContainer<TElem> Window_; 
    ui32 MaxWndSize_;
};