Welcome to mirror list, hosted at ThFree Co, Russian Federation.

BLI_linear_allocated_vector.h « blenlib « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: a70f83c98fb2217e415289d0acf7e8271dce560a (plain)
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
#pragma once

#include "BLI_linear_allocator.h"
#include "BLI_memory_utils_cxx.h"
#include "BLI_index_range.h"

namespace BLI {

template<typename T> class LinearAllocatedVector : BLI::NonCopyable {
 private:
  T *m_begin;
  T *m_end;
  T *m_capacity_end;

#ifdef DEBUG
  uint m_debug_size;
#  define UPDATE_VECTOR_SIZE(ptr) (ptr)->m_debug_size = (ptr)->m_end - (ptr)->m_begin
#else
#  define UPDATE_VECTOR_SIZE(ptr) ((void)0)
#endif

 public:
  LinearAllocatedVector() : m_begin(nullptr), m_end(nullptr), m_capacity_end(nullptr)
  {
    UPDATE_VECTOR_SIZE(this);
  }

  ~LinearAllocatedVector()
  {
    destruct_n(m_begin, this->size());
  }

  LinearAllocatedVector(LinearAllocatedVector &&other)
  {
    m_begin = other.m_begin;
    m_end = other.m_end;
    m_capacity_end = other.m_capacity_end;

    other.m_begin = nullptr;
    other.m_end = nullptr;
    other.m_capacity_end = nullptr;

    UPDATE_VECTOR_SIZE(this);
    UPDATE_VECTOR_SIZE(&other);
  }

  LinearAllocatedVector &operator=(LinearAllocatedVector &&other)
  {
    if (this == &other) {
      return *this;
    }

    m_begin = other.m_begin;
    m_end = other.m_end;
    m_capacity_end = other.m_capacity_end;

    other.m_begin = nullptr;
    other.m_end = nullptr;
    other.m_capacity_end = nullptr;

    UPDATE_VECTOR_SIZE(this);
    UPDATE_VECTOR_SIZE(&other);

    return *this;
  }

  operator ArrayRef<T>() const
  {
    return ArrayRef<T>(m_begin, this->size());
  }

  operator MutableArrayRef<T>()
  {
    return MutableArrayRef<T>(m_begin, this->size());
  }

  ArrayRef<T> as_ref() const
  {
    return *this;
  }

  MutableArrayRef<T> as_mutable_ref() const
  {
    return *this;
  }

  IndexRange index_range() const
  {
    return IndexRange(this->size());
  }

  uint size() const
  {
    return m_end - m_begin;
  }

  uint capacity() const
  {
    return m_capacity_end - m_begin;
  }

  void clear()
  {
    destruct_n(m_begin, this->size());
    m_end = m_begin;
    UPDATE_VECTOR_SIZE(this);
  }

  void append_unchecked(const T &value)
  {
    BLI_assert(m_end < m_capacity_end);
    new (m_end) T(value);
    m_end++;
    UPDATE_VECTOR_SIZE(this);
  }

  template<typename AllocT> void append(const T &value, LinearAllocator<AllocT> &allocator)
  {
    if (m_end == m_capacity_end) {
      this->grow(this->size() + 1, allocator);
    }
    this->append_unchecked(value);
  }

  template<typename AllocT>
  uint append_and_get_index(const T &value, LinearAllocator<AllocT> &allocator)
  {
    uint index = this->size();
    this->append(value, allocator);
    return index;
  }

  bool contains(const T &value) const
  {
    for (const T &current : *this) {
      if (current == value) {
        return true;
      }
    }
    return false;
  }

  const T &operator[](uint index) const
  {
    BLI_assert(index < this->size());
    return m_begin[index];
  }

  T &operator[](uint index)
  {
    BLI_assert(index < this->size());
    return m_begin[index];
  }

  const T *begin() const
  {
    return m_begin;
  }

  const T *end() const
  {
    return m_end;
  }

  T *begin()
  {
    return m_begin;
  }

  T *end()
  {
    return m_end;
  }

  void remove_and_reorder(uint index)
  {
    BLI_assert(index < this->size());
    T *element_to_remove = m_begin + index;
    m_end--;
    if (element_to_remove < m_end) {
      *element_to_remove = std::move(*m_end);
    }
    destruct(m_end);
    UPDATE_VECTOR_SIZE(this);
  }

  int index_try(const T &value) const
  {
    for (T *current = m_begin; current != m_end; current++) {
      if (*current == value) {
        return current - m_begin;
      }
    }
    return -1;
  }

  uint index(const T &value) const
  {
    int index = this->index_try(value);
    BLI_assert(index >= 0);
    return (uint)index;
  }

  void remove_first_occurrence_and_reorder(const T &value)
  {
    uint index = this->index(value);
    this->remove_and_reorder((uint)index);
  }

 private:
  template<typename AllocT>
  BLI_NOINLINE void grow(uint min_capacity, LinearAllocator<AllocT> &allocator)
  {
    if (min_capacity <= this->capacity()) {
      return;
    }

    uint size = this->size();
    min_capacity = power_of_2_max_u(min_capacity);

    T *new_begin = (T *)allocator.allocate(sizeof(T) * min_capacity, alignof(T));
    uninitialized_relocate_n(m_begin, size, new_begin);

    m_begin = new_begin;
    m_end = new_begin + size;
    m_capacity_end = new_begin + min_capacity;
  }
};

}  // namespace BLI