Blender  V2.93
BLI_vector_adaptor.hh
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  */
16 
17 #pragma once
18 
29 #include "BLI_span.hh"
30 
31 namespace blender {
32 
33 template<typename T> class VectorAdaptor {
34  private:
35  T *begin_;
36  T *end_;
37  T *capacity_end_;
38 
39  public:
40  VectorAdaptor() : begin_(nullptr), end_(nullptr), capacity_end_(nullptr)
41  {
42  }
43 
45  : begin_(data), end_(data + size), capacity_end_(data + capacity)
46  {
47  }
48 
49  VectorAdaptor(MutableSpan<T> span) : VectorAdaptor(span.data(), span.size(), 0)
50  {
51  }
52 
53  void append(const T &value)
54  {
55  BLI_assert(end_ < capacity_end_);
56  new (end_) T(value);
57  end_++;
58  }
59 
60  void append(T &&value)
61  {
62  BLI_assert(end_ < capacity_end_);
63  new (end_) T(std::move(value));
64  end_++;
65  }
66 
67  void append_n_times(const T &value, int64_t n)
68  {
69  BLI_assert(end_ + n <= capacity_end_);
70  uninitialized_fill_n(end_, n, value);
71  end_ += n;
72  }
73 
74  void extend(Span<T> values)
75  {
76  BLI_assert(end_ + values.size() <= capacity_end_);
77  uninitialized_copy_n(values.data(), values.size(), end_);
78  end_ += values.size();
79  }
80 
81  int64_t capacity() const
82  {
83  return capacity_end_ - begin_;
84  }
85 
86  int64_t size() const
87  {
88  return end_ - begin_;
89  }
90 
91  bool is_empty() const
92  {
93  return begin_ == end_;
94  }
95 
96  bool is_full() const
97  {
98  return end_ == capacity_end_;
99  }
100 };
101 
102 } // namespace blender
#define BLI_assert(a)
Definition: BLI_assert.h:58
constexpr const T * data() const
Definition: BLI_span.hh:217
constexpr int64_t size() const
Definition: BLI_span.hh:254
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:92