summaryrefslogtreecommitdiffstats
path: root/contrib/libs/apache/arrow_next/cpp/src/arrow/util/small_vector.h
blob: 8bf6273100fdac86a49e3f3f59e525851374fb5f (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
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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
#pragma clang system_header
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements.  See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership.  The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License.  You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied.  See the License for the
// specific language governing permissions and limitations
// under the License.

#pragma once

#include <algorithm>
#include <cassert>
#include <cstddef>
#include <initializer_list>
#include <iterator>
#include <limits>
#include <new>
#include <type_traits>
#include <utility>

#include "contrib/libs/apache/arrow_next/cpp/src/arrow/util/aligned_storage.h"
#include "contrib/libs/apache/arrow_next/cpp/src/arrow/util/macros.h"

namespace arrow20 {
namespace internal {

template <typename T, size_t N, bool NonTrivialDestructor>
struct StaticVectorStorageBase {
  using storage_type = AlignedStorage<T>;

  storage_type static_data_[N];
  size_t size_ = 0;

  void destroy() noexcept {}
};

template <typename T, size_t N>
struct StaticVectorStorageBase<T, N, true> {
  using storage_type = AlignedStorage<T>;

  storage_type static_data_[N];
  size_t size_ = 0;

  ~StaticVectorStorageBase() noexcept { destroy(); }

  void destroy() noexcept { storage_type::destroy_several(static_data_, size_); }
};

template <typename T, size_t N, bool D = !std::is_trivially_destructible<T>::value>
struct StaticVectorStorage : public StaticVectorStorageBase<T, N, D> {
  using Base = StaticVectorStorageBase<T, N, D>;
  using typename Base::storage_type;

  using Base::size_;
  using Base::static_data_;

  StaticVectorStorage() noexcept = default;

  constexpr storage_type* storage_ptr() { return static_data_; }

  constexpr const storage_type* const_storage_ptr() const { return static_data_; }

  // Adjust storage size, but don't initialize any objects
  void bump_size(size_t addend) {
    assert(size_ + addend <= N);
    size_ += addend;
  }

  void ensure_capacity(size_t min_capacity) { assert(min_capacity <= N); }

  // Adjust storage size, but don't destroy any objects
  void reduce_size(size_t reduce_by) {
    assert(reduce_by <= size_);
    size_ -= reduce_by;
  }

  // Move objects from another storage, but don't destroy any objects currently
  // stored in *this.
  // You need to call destroy() first if necessary (e.g. in a
  // move assignment operator).
  void move_construct(StaticVectorStorage&& other) noexcept {
    size_ = other.size_;
    if (size_ != 0) {
      // Use a compile-time memcpy size (N) for trivial types
      storage_type::move_construct_several(other.static_data_, static_data_, size_, N);
    }
  }

  constexpr size_t capacity() const { return N; }

  constexpr size_t max_size() const { return N; }

  void reserve(size_t n) {}

  void clear() {
    storage_type::destroy_several(static_data_, size_);
    size_ = 0;
  }
};

template <typename T, size_t N>
struct SmallVectorStorage {
  using storage_type = AlignedStorage<T>;

  storage_type static_data_[N];
  size_t size_ = 0;
  storage_type* data_ = static_data_;
  size_t dynamic_capacity_ = 0;

  SmallVectorStorage() noexcept = default;

  ~SmallVectorStorage() { destroy(); }

  constexpr storage_type* storage_ptr() { return data_; }

  constexpr const storage_type* const_storage_ptr() const { return data_; }

  void bump_size(size_t addend) {
    const size_t new_size = size_ + addend;
    ensure_capacity(new_size);
    size_ = new_size;
  }

  void ensure_capacity(size_t min_capacity) {
    if (dynamic_capacity_) {
      // Grow dynamic storage if necessary
      if (min_capacity > dynamic_capacity_) {
        size_t new_capacity = std::max(dynamic_capacity_ * 2, min_capacity);
        reallocate_dynamic(new_capacity);
      }
    } else if (min_capacity > N) {
      switch_to_dynamic(min_capacity);
    }
  }

  void reduce_size(size_t reduce_by) {
    assert(reduce_by <= size_);
    size_ -= reduce_by;
  }

  void destroy() noexcept {
    storage_type::destroy_several(data_, size_);
    if (dynamic_capacity_) {
      delete[] data_;
    }
  }

  void move_construct(SmallVectorStorage&& other) noexcept {
    size_ = other.size_;
    dynamic_capacity_ = other.dynamic_capacity_;
    if (dynamic_capacity_) {
      data_ = other.data_;
      other.data_ = other.static_data_;
      other.dynamic_capacity_ = 0;
      other.size_ = 0;
    } else if (size_ != 0) {
      // Use a compile-time memcpy size (N) for trivial types
      storage_type::move_construct_several(other.static_data_, static_data_, size_, N);
    }
  }

  constexpr size_t capacity() const { return dynamic_capacity_ ? dynamic_capacity_ : N; }

  constexpr size_t max_size() const { return std::numeric_limits<size_t>::max(); }

  void reserve(size_t n) {
    if (dynamic_capacity_) {
      if (n > dynamic_capacity_) {
        reallocate_dynamic(n);
      }
    } else if (n > N) {
      switch_to_dynamic(n);
    }
  }

  void clear() {
    storage_type::destroy_several(data_, size_);
    size_ = 0;
  }

 private:
  void switch_to_dynamic(size_t new_capacity) {
    dynamic_capacity_ = new_capacity;
    data_ = new storage_type[new_capacity];
    storage_type::move_construct_several_and_destroy_source(static_data_, data_, size_);
  }

  void reallocate_dynamic(size_t new_capacity) {
    assert(new_capacity >= size_);
    auto new_data = new storage_type[new_capacity];
    storage_type::move_construct_several_and_destroy_source(data_, new_data, size_);
    delete[] data_;
    dynamic_capacity_ = new_capacity;
    data_ = new_data;
  }
};

template <typename T, size_t N, typename Storage>
class StaticVectorImpl {
 private:
  Storage storage_;

  T* data_ptr() { return storage_.storage_ptr()->get(); }

  constexpr const T* const_data_ptr() const {
    return storage_.const_storage_ptr()->get();
  }

 public:
  using size_type = size_t;
  using difference_type = ptrdiff_t;
  using value_type = T;
  using pointer = T*;
  using const_pointer = const T*;
  using reference = T&;
  using const_reference = const T&;
  using iterator = T*;
  using const_iterator = const T*;
  using reverse_iterator = std::reverse_iterator<iterator>;
  using const_reverse_iterator = std::reverse_iterator<const_iterator>;

  constexpr StaticVectorImpl() noexcept = default;

  // Move and copy constructors
  StaticVectorImpl(StaticVectorImpl&& other) noexcept {
    storage_.move_construct(std::move(other.storage_));
  }

  StaticVectorImpl& operator=(StaticVectorImpl&& other) noexcept {
    if (ARROW_PREDICT_TRUE(&other != this)) {
      // TODO move_assign?
      storage_.destroy();
      storage_.move_construct(std::move(other.storage_));
    }
    return *this;
  }

  StaticVectorImpl(const StaticVectorImpl& other) {
    init_by_copying(other.storage_.size_, other.const_data_ptr());
  }

  StaticVectorImpl& operator=(const StaticVectorImpl& other) noexcept {
    if (ARROW_PREDICT_TRUE(&other != this)) {
      assign_by_copying(other.storage_.size_, other.data());
    }
    return *this;
  }

  // Automatic conversion from std::vector<T>, for convenience
  StaticVectorImpl(const std::vector<T>& other) {  // NOLINT: explicit
    init_by_copying(other.size(), other.data());
  }

  StaticVectorImpl(std::vector<T>&& other) noexcept {  // NOLINT: explicit
    init_by_moving(other.size(), other.data());
  }

  StaticVectorImpl& operator=(const std::vector<T>& other) {
    assign_by_copying(other.size(), other.data());
    return *this;
  }

  StaticVectorImpl& operator=(std::vector<T>&& other) noexcept {
    assign_by_moving(other.size(), other.data());
    return *this;
  }

  // Constructing from count and optional initialization value
  explicit StaticVectorImpl(size_t count) {
    storage_.bump_size(count);
    auto* p = storage_.storage_ptr();
    for (size_t i = 0; i < count; ++i) {
      p[i].construct();
    }
  }

  StaticVectorImpl(size_t count, const T& value) {
    storage_.bump_size(count);
    auto* p = storage_.storage_ptr();
    for (size_t i = 0; i < count; ++i) {
      p[i].construct(value);
    }
  }

  StaticVectorImpl(std::initializer_list<T> values) {
    storage_.bump_size(values.size());
    auto* p = storage_.storage_ptr();
    for (auto&& v : values) {
      // Unfortunately, cannot move initializer values
      p++->construct(v);
    }
  }

  // Size inspection

  constexpr bool empty() const { return storage_.size_ == 0; }

  constexpr size_t size() const { return storage_.size_; }

  constexpr size_t capacity() const { return storage_.capacity(); }

  constexpr size_t max_size() const { return storage_.max_size(); }

  // Data access

  T& operator[](size_t i) { return data_ptr()[i]; }

  constexpr const T& operator[](size_t i) const { return const_data_ptr()[i]; }

  T& front() { return data_ptr()[0]; }

  constexpr const T& front() const { return const_data_ptr()[0]; }

  T& back() { return data_ptr()[storage_.size_ - 1]; }

  constexpr const T& back() const { return const_data_ptr()[storage_.size_ - 1]; }

  T* data() { return data_ptr(); }

  constexpr const T* data() const { return const_data_ptr(); }

  // Iterators

  iterator begin() { return iterator(data_ptr()); }

  constexpr const_iterator begin() const { return const_iterator(const_data_ptr()); }

  constexpr const_iterator cbegin() const { return const_iterator(const_data_ptr()); }

  iterator end() { return iterator(data_ptr() + storage_.size_); }

  constexpr const_iterator end() const {
    return const_iterator(const_data_ptr() + storage_.size_);
  }

  constexpr const_iterator cend() const {
    return const_iterator(const_data_ptr() + storage_.size_);
  }

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

  constexpr const_reverse_iterator rbegin() const {
    return const_reverse_iterator(end());
  }

  constexpr const_reverse_iterator crbegin() const {
    return const_reverse_iterator(end());
  }

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

  constexpr const_reverse_iterator rend() const {
    return const_reverse_iterator(begin());
  }

  constexpr const_reverse_iterator crend() const {
    return const_reverse_iterator(begin());
  }

  // Mutations

  void reserve(size_t n) { storage_.reserve(n); }

  void clear() { storage_.clear(); }

  void push_back(const T& value) {
    storage_.bump_size(1);
    storage_.storage_ptr()[storage_.size_ - 1].construct(value);
  }

  void push_back(T&& value) {
    storage_.bump_size(1);
    storage_.storage_ptr()[storage_.size_ - 1].construct(std::move(value));
  }

  template <typename... Args>
  void emplace_back(Args&&... args) {
    storage_.bump_size(1);
    storage_.storage_ptr()[storage_.size_ - 1].construct(std::forward<Args>(args)...);
  }

  template <typename InputIt>
  iterator insert(const_iterator insert_at, InputIt first, InputIt last) {
    const size_t n = storage_.size_;
    const size_t it_size = static_cast<size_t>(last - first);  // XXX might be O(n)?
    const size_t pos = static_cast<size_t>(insert_at - const_data_ptr());
    storage_.bump_size(it_size);
    auto* p = storage_.storage_ptr();
    if (it_size == 0) {
      return p[pos].get();
    }
    const size_t end_pos = pos + it_size;

    // Move [pos; n) to [end_pos; end_pos + n - pos)
    size_t i = n;
    size_t j = end_pos + n - pos;
    while (j > std::max(n, end_pos)) {
      p[--j].move_construct(&p[--i]);
    }
    while (j > end_pos) {
      p[--j].move_assign(&p[--i]);
    }
    assert(j == end_pos);
    // Copy [first; last) to [pos; end_pos)
    j = pos;
    while (j < std::min(n, end_pos)) {
      p[j++].assign(*first++);
    }
    while (j < end_pos) {
      p[j++].construct(*first++);
    }
    assert(first == last);
    return p[pos].get();
  }

  void resize(size_t n) {
    const size_t old_size = storage_.size_;
    if (n > storage_.size_) {
      storage_.bump_size(n - old_size);
      auto* p = storage_.storage_ptr();
      for (size_t i = old_size; i < n; ++i) {
        p[i].construct(T{});
      }
    } else {
      auto* p = storage_.storage_ptr();
      for (size_t i = n; i < old_size; ++i) {
        p[i].destroy();
      }
      storage_.reduce_size(old_size - n);
    }
  }

  void resize(size_t n, const T& value) {
    const size_t old_size = storage_.size_;
    if (n > storage_.size_) {
      storage_.bump_size(n - old_size);
      auto* p = storage_.storage_ptr();
      for (size_t i = old_size; i < n; ++i) {
        p[i].construct(value);
      }
    } else {
      auto* p = storage_.storage_ptr();
      for (size_t i = n; i < old_size; ++i) {
        p[i].destroy();
      }
      storage_.reduce_size(old_size - n);
    }
  }

 private:
  template <typename InputIt>
  void init_by_copying(size_t n, InputIt src) {
    storage_.bump_size(n);
    auto* dest = storage_.storage_ptr();
    for (size_t i = 0; i < n; ++i, ++src) {
      dest[i].construct(*src);
    }
  }

  template <typename InputIt>
  void init_by_moving(size_t n, InputIt src) {
    init_by_copying(n, std::make_move_iterator(src));
  }

  template <typename InputIt>
  void assign_by_copying(size_t n, InputIt src) {
    const size_t old_size = storage_.size_;
    if (n > old_size) {
      storage_.bump_size(n - old_size);
      auto* dest = storage_.storage_ptr();
      for (size_t i = 0; i < old_size; ++i, ++src) {
        dest[i].assign(*src);
      }
      for (size_t i = old_size; i < n; ++i, ++src) {
        dest[i].construct(*src);
      }
    } else {
      auto* dest = storage_.storage_ptr();
      for (size_t i = 0; i < n; ++i, ++src) {
        dest[i].assign(*src);
      }
      for (size_t i = n; i < old_size; ++i) {
        dest[i].destroy();
      }
      storage_.reduce_size(old_size - n);
    }
  }

  template <typename InputIt>
  void assign_by_moving(size_t n, InputIt src) {
    assign_by_copying(n, std::make_move_iterator(src));
  }
};

template <typename T, size_t N>
using StaticVector = StaticVectorImpl<T, N, StaticVectorStorage<T, N>>;

template <typename T, size_t N>
using SmallVector = StaticVectorImpl<T, N, SmallVectorStorage<T, N>>;

}  // namespace internal
}  // namespace arrow20