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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
|
#pragma once
#include <yql/essentials/minikql/defs.h>
#include <yql/essentials/minikql/mkql_alloc.h>
#include <yql/essentials/public/udf/udf_value.h>
namespace NKikimr {
namespace NMiniKQL {
template <typename T>
struct TListChunk {
private:
using TSelf = TListChunk<T>;
static void PlacementNew(T* ptr, ui64 count) {
T* end = ptr + count;
for (; ptr != end; ptr++) {
new (ptr) T;
}
}
public:
void CheckMagic() {
Y_DEBUG_ABORT_UNLESS(Magic_ == TListChunkMagic);
}
static TSelf* AllocateChunk(ui64 length) {
const auto block = TWithDefaultMiniKQLAlloc::AllocWithSize(length * sizeof(T) + sizeof(TListChunk));
const auto ptr = new (block) TListChunk(length);
PlacementNew(reinterpret_cast<T*>(ptr + 1), length);
return ptr;
}
TListChunk(ui64 size)
: Magic_(TListChunkMagic)
, Refs_(1)
, DataEnd_(DataBegin() + size)
{
}
~TListChunk() {
CheckMagic();
for (auto it = DataBegin(); it != DataEnd(); it++) {
it->~T();
}
TWithDefaultMiniKQLAlloc::FreeWithSize(
static_cast<void*>(this), sizeof(TListChunk) + sizeof(T) * (DataEnd() - DataBegin()));
}
inline T* DataBegin() {
CheckMagic();
return reinterpret_cast<T*>(this + 1);
}
inline const T* DataEnd() {
CheckMagic();
return DataEnd_;
}
ui64 Size() {
CheckMagic();
return DataEnd() - DataBegin();
}
void Ref() {
CheckMagic();
Refs_++;
}
void UnRef() {
CheckMagic();
if (--Refs_ == 0) {
this->~TListChunk();
}
}
private:
static const ui32 TListChunkMagic = 12322846;
const ui32 Magic_;
ui32 Refs_;
const T* DataEnd_;
};
template <typename T, ui64 MinChunkSize = 48>
class TListRepresentation {
public:
using TSelf = TListRepresentation<T, MinChunkSize>;
using TChunk = TListChunk<T>;
private:
enum Type {
Normal,
Freezed
};
void NewNormal(const T* begin1, const T* end1, const T* begin2, const T* end2) {
Type_ = Type::Normal;
ui64 oldLength = (end1 - begin1) + (end2 - begin2);
ui64 newLength = std::max((oldLength << 1) - 1, (MinChunkSize + sizeof(T) - 1) / sizeof(T) + 1);
Chunk_ = TChunk::AllocateChunk(newLength);
Begin_ = Chunk_->DataBegin() + ((newLength - oldLength) >> 2);
End_ = std::copy(begin2, end2, std::copy(begin1, end1, Begin_));
}
TListRepresentation(TChunk* chunk, T* begin, T* end, Type type)
: Chunk_(chunk)
, Begin_(begin)
, End_(end)
, Type_(type)
{
if (Chunk_) {
Chunk_->Ref();
}
}
TListRepresentation(T* begin1, T* end1, T* begin2, T* end2)
{
NewNormal(begin1, end1, begin2, end2);
}
public:
struct TIterator {
TIterator()
: Owner_(nullptr)
, Position_(nullptr)
{}
TIterator(const TListRepresentation& owner)
: Owner_(&owner)
, Position_(owner.Begin_)
{
}
TIterator(const TIterator& other)
: Owner_(other.Owner_)
, Position_(other.Position_)
{}
TIterator& operator=(const TIterator& other)
{
Owner_ = other.Owner_;
Position_ = other.Position_;
return *this;
}
bool AtEnd() const {
return Position_ == Owner_->End_;
}
const T& Current() const {
return *Position_;
}
// use with care, list may be shared
T& MutableCurrent() {
return *Position_;
}
void Next() {
Position_++;
}
const TListRepresentation* Owner_;
T* Position_;
};
struct TReverseIterator {
TReverseIterator()
: Owner_(nullptr)
, Position_(nullptr)
{
}
TReverseIterator(const TListRepresentation& owner)
: Owner_(&owner)
, Position_(owner.End_)
{
}
TReverseIterator(const TIterator& other)
: Owner_(other.Owner_)
, Position_(other.Position_)
{
}
TReverseIterator& operator=(const TReverseIterator& other)
{
Owner_ = other.Owner_;
Position_ = other.Position_;
return *this;
}
bool AtEnd() const {
return Position_ == Owner_->Begin_;
}
const T& Current() const {
return *(Position_ - 1);
}
// use with care, list may be shared
T& MutableCurrent() {
return *(Position_ - 1);
}
void Next() {
Position_--;
}
private:
const TListRepresentation* Owner_;
T* Position_;
};
TListRepresentation()
: Chunk_(nullptr)
, Begin_(nullptr)
, End_(nullptr)
, Type_(Type::Freezed)
{
}
~TListRepresentation() {
if (Chunk_) {
Chunk_->UnRef();
}
}
TListRepresentation(const TSelf& other)
: Chunk_(other.Chunk_)
, Begin_(other.Begin_)
, End_(other.End_)
, Type_(other.Type_)
{
other.Type_ = Type::Freezed;
if (Chunk_) {
Chunk_->Ref();
}
}
TListRepresentation(TSelf&& other)
: Chunk_(other.Chunk_)
, Begin_(other.Begin_)
, End_(other.End_)
, Type_(other.Type_)
{
other.Chunk_ = nullptr;
other.Begin_ = nullptr;
other.End_ = nullptr;
other.Type_ = Type::Freezed;
}
void operator=(const TSelf& other) {
if (this != &other) {
if (other.Chunk_) {
other.Chunk_->Ref();
}
if (Chunk_) {
Chunk_->UnRef();
}
Chunk_ = other.Chunk_;
Begin_ = other.Begin_;
End_ = other.End_;
Type_ = other.Type_;
other.Type_ = Type::Freezed;
}
}
void operator=(TSelf&& other) {
if (Chunk_) {
Chunk_->UnRef();
}
Chunk_ = other.Chunk_;
Begin_ = other.Begin_;
End_ = other.End_;
Type_ = other.Type_;
other.Chunk_ = nullptr;
other.Begin_ = nullptr;
other.End_ = nullptr;
other.Type_ = Type::Freezed;
}
inline void FromSingleElement(T&& element) {
Type_ = Type::Normal;
ui64 chunkLength = (MinChunkSize + sizeof(T) - 1) / sizeof(T);
Chunk_ = TChunk::AllocateChunk(chunkLength);
Begin_ = Chunk_->DataBegin() + (chunkLength >> 2);
End_ = Begin_ + 1;
*Begin_ = std::move(element);
}
TListRepresentation(T&& element)
{
FromSingleElement(std::move(element));
}
TListRepresentation(T&& element, const TSelf& that)
{
if (!that.Chunk_) {
FromSingleElement(std::move(element));
return;
}
if ((that.Type_ == Type::Normal) && (that.Begin_ != that.Chunk_->DataBegin())) {
Type_ = Type::Normal;
that.Type_ = Type::Freezed;
Chunk_ = that.Chunk_;
Chunk_->Ref();
Begin_ = that.Begin_;
End_ = that.End_;
*(--Begin_) = std::move(element);
} else {
NewNormal(&element, &element + 1, that.Begin_, that.End_);
}
}
TListRepresentation(const TSelf& that, T&& element)
{
if (!that.Chunk_) {
FromSingleElement(std::move(element));
return;
}
if ((that.Type_ == Type::Normal) && (that.End_ != that.Chunk_->DataEnd())) {
Type_ = Type::Normal;
that.Type_ = Type::Freezed;
Chunk_ = that.Chunk_;
Chunk_->Ref();
Begin_ = that.Begin_;
End_ = that.End_ + 1;
*(that.End_) = std::move(element);
} else {
NewNormal(that.Begin_, that.End_, &element, &element + 1);
}
}
ui64 GetLength() const {
return End_ - Begin_;
}
TIterator GetIterator() const {
return TIterator(*this);
}
TReverseIterator GetReverseIterator() const {
return TReverseIterator(*this);
}
TSelf Append(T&& right) const {
return TSelf(*this, std::move(right));
}
TSelf Prepend(T&& left) const {
return TSelf(std::move(left), *this);
}
TSelf MassPrepend(T* begin, T* end) const {
if ((Type_ == Type::Normal) && (Chunk_->DataBegin() + (end - begin) <= Begin_)) {
Type_ = Type::Freezed;
return TSelf(Chunk_, std::copy_backward(begin, end, Begin_), End_, Type::Normal);
} else {
return TSelf(begin, end, Begin_, End_);
}
}
TSelf MassAppend(T* begin, T* end) const {
if ((Type_ == Type::Normal) && (End_ + (end - begin) <= Chunk_->DataEnd())) {
Type_ = Type::Freezed;
return TSelf(Chunk_, Begin_, std::copy(begin, end, End_), Type::Normal);
} else {
return TSelf(Begin_, End_, begin, end);
}
}
TSelf Extend(const TSelf& right) const {
ui64 thisLength = GetLength();
ui64 rightLength = right.GetLength();
if (!thisLength)
return TSelf(right);
if (!rightLength)
return TSelf(*this);
if (Type_ == Type::Freezed) {
if (right.Type_ == Type::Freezed) {
return TSelf(Begin_, End_, right.Begin_, right.End_);
} else {
return right.MassPrepend(Begin_, End_);
}
} else if ((right.Type_ == Type::Freezed) || (thisLength > rightLength)) {
return MassAppend(right.Begin_, right.End_);
} else {
return right.MassPrepend(Begin_, End_);
}
}
TSelf SkipFromBegin(ui64 count) const {
Y_DEBUG_ABORT_UNLESS((count > 0) && (count < GetLength()));
return TSelf(Chunk_, Begin_ + count, End_, Type::Freezed);
}
TSelf SkipFromEnd(ui64 count) const {
Y_DEBUG_ABORT_UNLESS((count > 0) && (count < GetLength()));
return TSelf(Chunk_, Begin_, End_ - count, Type::Freezed);
}
T GetItemByIndex(ui64 index) const {
Y_DEBUG_ABORT_UNLESS((index >= 0) && (index < GetLength()));
return Begin_[index];
}
T* GetItems() const {
return Begin_;
}
private:
TChunk* Chunk_;
T* Begin_;
T* End_;
mutable Type Type_;
};
using TDefaultListRepresentation = TListRepresentation<NUdf::TUnboxedValue>;
}
}
|