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
172
173
174
175
176
177
178
179
180
181
182
|
#ifndef PERSISTENT_QUEUE_INL_H_
#error "Direct inclusion of this file is not allowed, include persistent_queue.h"
// For the sake of sane code completion.
#include "persistent_queue.h"
#endif
#include <yt/yt/core/misc/serialize.h>
namespace NYT {
////////////////////////////////////////////////////////////////////////////////
template <class T, size_t ChunkSize>
TPersistentQueueIterator<T, ChunkSize>::TPersistentQueueIterator()
{ }
template <class T, size_t ChunkSize>
TPersistentQueueIterator<T, ChunkSize>& TPersistentQueueIterator<T, ChunkSize>::operator++()
{
YT_ASSERT(CurrentChunk_);
YT_ASSERT(CurrentIndex_ >= 0 && CurrentIndex_ < ChunkSize);
++CurrentIndex_;
if (CurrentIndex_ == ChunkSize) {
CurrentChunk_ = CurrentChunk_->Next;
CurrentIndex_ = 0;
}
return *this;
}
template <class T, size_t ChunkSize>
TPersistentQueueIterator<T, ChunkSize> TPersistentQueueIterator<T, ChunkSize>::operator++(int)
{
auto result = *this;
++(*this);
return result;
}
template <class T, size_t ChunkSize>
const T& TPersistentQueueIterator<T, ChunkSize>::operator*() const
{
return CurrentChunk_->Elements[CurrentIndex_];
}
template <class T, size_t ChunkSize>
TPersistentQueueIterator<T, ChunkSize>::TPersistentQueueIterator(
TChunkPtr chunk,
size_t index)
: CurrentChunk_(std::move(chunk))
, CurrentIndex_(index)
{ }
////////////////////////////////////////////////////////////////////////////////
template <class T, size_t ChunkSize>
size_t TPersistentQueueBase<T, ChunkSize>::Size() const
{
return Size_;
}
template <class T, size_t ChunkSize>
bool TPersistentQueueBase<T, ChunkSize>::Empty() const
{
return Size_ == 0;
}
template <class T, size_t ChunkSize>
auto TPersistentQueueBase<T, ChunkSize>::Begin() const -> TIterator
{
return Tail_;
}
template <class T, size_t ChunkSize>
auto TPersistentQueueBase<T, ChunkSize>::End() const -> TIterator
{
return Head_;
}
template <class T, size_t ChunkSize>
auto TPersistentQueueBase<T, ChunkSize>::begin() const -> TIterator
{
return Begin();
}
template <class T, size_t ChunkSize>
auto TPersistentQueueBase<T, ChunkSize>::end() const -> TIterator
{
return End();
}
////////////////////////////////////////////////////////////////////////////////
template <class T, size_t ChunkSize>
template <class C>
void TPersistentQueueSnapshot<T, ChunkSize>::Save(C& context) const
{
using NYT::Save;
TSizeSerializer::Save(context, this->Size());
for (const auto& value : *this) {
Save(context, value);
}
}
////////////////////////////////////////////////////////////////////////////////
template <class T, size_t ChunkSize>
void TPersistentQueue<T, ChunkSize>::Enqueue(T value)
{
auto& head = this->Head_;
auto& tail = this->Tail_;
auto& size = this->Size_;
if (!head.CurrentChunk_) {
auto chunk = New<TChunk>();
head.CurrentChunk_ = tail.CurrentChunk_ = chunk;
head.CurrentIndex_ = tail.CurrentIndex_ = 0;
}
head.CurrentChunk_->Elements[head.CurrentIndex_++] = std::move(value);
++size;
if (head.CurrentIndex_ == ChunkSize) {
auto chunk = New<TChunk>();
head.CurrentChunk_->Next = chunk;
head.CurrentChunk_ = chunk;
head.CurrentIndex_ = 0;
}
}
template <class T, size_t ChunkSize>
T TPersistentQueue<T, ChunkSize>::Dequeue()
{
auto& tail = this->Tail_;
auto& size = this->Size_;
YT_ASSERT(size != 0);
auto result = std::move(tail.CurrentChunk_->Elements[tail.CurrentIndex_++]);
--size;
if (tail.CurrentIndex_ == ChunkSize) {
tail.CurrentChunk_ = tail.CurrentChunk_->Next;
tail.CurrentIndex_ = 0;
}
return result;
}
template <class T, size_t ChunkSize>
void TPersistentQueue<T, ChunkSize>::Clear()
{
this->Head_ = TPersistentQueueIterator<T, ChunkSize>();
this->Tail_ = TPersistentQueueIterator<T, ChunkSize>();
this->Size_ = 0;
}
template <class T, size_t ChunkSize>
auto TPersistentQueue<T, ChunkSize>::MakeSnapshot() const -> TSnapshot
{
TSnapshot snapshot;
snapshot.Head_ = this->Head_;
snapshot.Tail_ = this->Tail_;
snapshot.Size_ = this->Size_;
return snapshot;
}
template <class T, size_t ChunkSize>
template <class C>
void TPersistentQueue<T, ChunkSize>::Load(C& context)
{
using NYT::Load;
YT_VERIFY(this->Empty());
auto size = TSizeSerializer::Load(context);
for (size_t index = 0; index < size; ++index) {
Enqueue(Load<T>(context));
}
}
////////////////////////////////////////////////////////////////////////////////
} // namespace NYT
|