Blender  V3.3
BLI_vector_adaptor.hh
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 
3 #pragma once
4 
15 #include "BLI_span.hh"
16 
17 namespace blender {
18 
19 template<typename T> class VectorAdaptor {
20  private:
21  T *begin_;
22  T *end_;
23  T *capacity_end_;
24 
25  public:
26  VectorAdaptor() : begin_(nullptr), end_(nullptr), capacity_end_(nullptr)
27  {
28  }
29 
31  : begin_(data), end_(data + size), capacity_end_(data + capacity)
32  {
33  }
34 
35  VectorAdaptor(MutableSpan<T> span) : VectorAdaptor(span.data(), span.size(), 0)
36  {
37  }
38 
39  void append(const T &value)
40  {
41  BLI_assert(end_ < capacity_end_);
42  new (end_) T(value);
43  end_++;
44  }
45 
46  void append(T &&value)
47  {
48  BLI_assert(end_ < capacity_end_);
49  new (end_) T(std::move(value));
50  end_++;
51  }
52 
53  void append_n_times(const T &value, int64_t n)
54  {
55  BLI_assert(end_ + n <= capacity_end_);
56  uninitialized_fill_n(end_, n, value);
57  end_ += n;
58  }
59 
60  void extend(Span<T> values)
61  {
62  BLI_assert(end_ + values.size() <= capacity_end_);
63  uninitialized_copy_n(values.data(), values.size(), end_);
64  end_ += values.size();
65  }
66 
67  int64_t capacity() const
68  {
69  return capacity_end_ - begin_;
70  }
71 
72  int64_t size() const
73  {
74  return end_ - begin_;
75  }
76 
77  bool is_empty() const
78  {
79  return begin_ == end_;
80  }
81 
82  bool is_full() const
83  {
84  return end_ == capacity_end_;
85  }
86 };
87 
88 } // namespace blender
#define BLI_assert(a)
Definition: BLI_assert.h:46
constexpr const T * data() const
Definition: BLI_span.hh:203
constexpr int64_t size() const
Definition: BLI_span.hh:240
void append(const T &value)
void extend(Span< T > values)
void append_n_times(const T &value, int64_t n)
VectorAdaptor(MutableSpan< T > span)
VectorAdaptor(T *data, int64_t capacity, int64_t size=0)
#define T
void uninitialized_fill_n(T *dst, int64_t n, const T &value)
void uninitialized_copy_n(const T *src, int64_t n, T *dst)
__int64 int64_t
Definition: stdint.h:89