aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/messagebus/vector_swaps.h
blob: 2edcd99114369f98355cb1ef57d5517b98f27fca (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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#pragma once

#include <util/generic/array_ref.h>
#include <util/generic/noncopyable.h>
#include <util/generic/utility.h>
#include <util/system/yassert.h>

#include <stdlib.h>

template <typename T, class A = std::allocator<T>>
class TVectorSwaps : TNonCopyable {
private:
    T* Start;
    T* Finish;
    T* EndOfStorage;

    void StateCheck() {
        Y_ASSERT(Start <= Finish); 
        Y_ASSERT(Finish <= EndOfStorage); 
    }

public:
    typedef T* iterator;
    typedef const T* const_iterator;

    typedef std::reverse_iterator<iterator> reverse_iterator;
    typedef std::reverse_iterator<const_iterator> const_reverse_iterator;

    TVectorSwaps()
        : Start()
        , Finish()
        , EndOfStorage()
    {
    }

    ~TVectorSwaps() {
        for (size_t i = 0; i < size(); ++i) {
            Start[i].~T();
        }
        free(Start);
    }

    operator TArrayRef<const T>() const {
        return MakeArrayRef(data(), size());
    }

    operator TArrayRef<T>() {
        return MakeArrayRef(data(), size());
    }

    size_t capacity() const {
        return EndOfStorage - Start;
    }

    size_t size() const {
        return Finish - Start;
    }

    bool empty() const {
        return size() == 0;
    }

    T* data() {
        return Start;
    }

    const T* data() const {
        return Start;
    }

    T& operator[](size_t index) {
        Y_ASSERT(index < size()); 
        return Start[index];
    }

    const T& operator[](size_t index) const {
        Y_ASSERT(index < size()); 
        return Start[index];
    }

    iterator begin() {
        return Start;
    }

    iterator end() {
        return Finish;
    }

    const_iterator begin() const {
        return Start;
    }

    const_iterator end() const {
        return Finish;
    }

    reverse_iterator rbegin() {
        return reverse_iterator(end());
    }
    reverse_iterator rend() {
        return reverse_iterator(begin());
    }

    const_reverse_iterator rbegin() const {
        return reverse_iterator(end());
    }
    const_reverse_iterator rend() const {
        return reverse_iterator(begin());
    }

    void swap(TVectorSwaps<T>& that) {
        DoSwap(Start, that.Start);
        DoSwap(Finish, that.Finish);
        DoSwap(EndOfStorage, that.EndOfStorage);
    }

    void reserve(size_t n) {
        if (n <= capacity()) {
            return;
        }

        size_t newCapacity = FastClp2(n);
        TVectorSwaps<T> tmp;
        tmp.Start = (T*)malloc(sizeof(T) * newCapacity);
        Y_VERIFY(!!tmp.Start); 

        tmp.EndOfStorage = tmp.Start + newCapacity;

        for (size_t i = 0; i < size(); ++i) {
            // TODO: catch exceptions
            new (tmp.Start + i) T();
            DoSwap(Start[i], tmp.Start[i]);
        }

        tmp.Finish = tmp.Start + size();

        swap(tmp);

        StateCheck();
    }

    void clear() {
        TVectorSwaps<T> tmp;
        swap(tmp);
    }

    template <class TIterator>
    void insert(iterator pos, TIterator b, TIterator e) {
        Y_VERIFY(pos == end(), "TODO: only insert at the end is implemented"); 

        size_t count = e - b;

        reserve(size() + count);

        TIterator next = b;

        for (size_t i = 0; i < count; ++i) {
            new (Start + size() + i) T();
            DoSwap(Start[size() + i], *next);
            ++next;
        }

        Finish += count;

        StateCheck();
    }

    void push_back(T& elem) {
        insert(end(), &elem, &elem + 1);
    }
};