25#include <RecastAssert.h>
40typedef void* (rcAllocFunc)(
size_t size, rcAllocHint hint);
45typedef void (rcFreeFunc)(
void* ptr);
50void rcAllocSetCustom(rcAllocFunc *allocFunc, rcFreeFunc *freeFunc);
57void* rcAlloc(
size_t size, rcAllocHint hint);
62void rcFree(
void* ptr);
68inline void*
operator new(size_t,
const rcNewTag&,
void* p) {
return p; }
69inline void operator delete(
void*,
const rcNewTag&,
void*) {}
73typedef intptr_t rcSizeType;
74#define RC_SIZE_MAX INTPTR_MAX
78#if defined(__GNUC__) || defined(__clang__)
79#define rcLikely(x) __builtin_expect((x), true)
80#define rcUnlikely(x) __builtin_expect((x), false)
82#define rcLikely(x) (x)
83#define rcUnlikely(x) (x)
94template <
typename T, rcAllocH
int H>
100 static void construct(T* p,
const T& v) { ::new(
rcNewTag(), (
void*)p) T(v); }
101 static void construct(T* p) { ::new(
rcNewTag(), (
void*)p) T; }
102 static void construct_range(T* begin, T* end);
103 static void construct_range(T* begin, T* end,
const T& value);
104 static void copy_range(T* dst,
const T* begin,
const T* end);
105 void destroy_range(rcSizeType begin, rcSizeType end);
107 T* allocate_and_copy(rcSizeType size);
108 void resize_impl(rcSizeType size,
const T* value);
110 typedef rcSizeType size_type;
111 typedef T value_type;
115 explicit rcVectorBase(rcSizeType count) : m_size(0), m_cap(0), m_data(0) { resize(count); }
116 rcVectorBase(rcSizeType count,
const T& value) : m_size(0), m_cap(0), m_data(0) { resize(count, value); }
117 rcVectorBase(
const T* begin,
const T* end) : m_size(0), m_cap(0), m_data(0) { assign(begin, end); }
118 ~rcVectorBase() { destroy_range(0, m_size); rcFree(m_data); }
121 bool reserve(rcSizeType size);
123 void assign(rcSizeType count,
const T& value) { clear(); resize(count, value); }
124 void assign(
const T* begin,
const T* end);
126 void resize(rcSizeType size) { resize_impl(size, NULL); }
127 void resize(rcSizeType size,
const T& value) { resize_impl(size, &value); }
129 void clear() { destroy_range(0, m_size); m_size = 0; }
131 void push_back(
const T& value);
132 void pop_back() { rcAssert(m_size > 0); back().~T(); m_size--; }
134 rcSizeType size()
const {
return m_size; }
135 rcSizeType capacity()
const {
return m_cap; }
136 bool empty()
const {
return size() == 0; }
138 const T& operator[](rcSizeType i)
const { rcAssert(i >= 0 && i < m_size);
return m_data[i]; }
139 T& operator[](rcSizeType i) { rcAssert(i >= 0 && i < m_size);
return m_data[i]; }
141 const T& front()
const { rcAssert(m_size);
return m_data[0]; }
142 T& front() { rcAssert(m_size);
return m_data[0]; }
143 const T& back()
const { rcAssert(m_size);
return m_data[m_size - 1]; };
144 T& back() { rcAssert(m_size);
return m_data[m_size - 1]; };
145 const T* data()
const {
return m_data; }
146 T* data() {
return m_data; }
148 T* begin() {
return m_data; }
149 T* end() {
return m_data + m_size; }
150 const T* begin()
const {
return m_data; }
151 const T* end()
const {
return m_data + m_size; }
159template<
typename T, rcAllocH
int H>
161 if (count <= m_cap) {
164 T* new_data = allocate_and_copy(count);
168 destroy_range(0, m_size);
174template <
typename T, rcAllocH
int H>
176 rcAssert(RC_SIZE_MAX /
static_cast<rcSizeType
>(
sizeof(T)) >= size);
177 T* new_data =
static_cast<T*
>(rcAlloc(
sizeof(T) * size, H));
179 copy_range(new_data, m_data, m_data + m_size);
183template <
typename T, rcAllocH
int H>
186 reserve(end - begin);
187 m_size = end - begin;
188 copy_range(m_data, begin, end);
190template <
typename T, rcAllocH
int H>
194 if (rcLikely(m_size < m_cap)) {
195 construct(m_data + m_size++, value);
199 rcAssert(RC_SIZE_MAX / 2 >= m_size);
200 rcSizeType new_cap = m_size ? 2*m_size : 1;
201 T* data = allocate_and_copy(new_cap);
204 construct(data + m_size, value);
205 destroy_range(0, m_size);
211template <
typename T, rcAllocH
int H>
214 destroy_range(size, m_size);
216 }
else if (size > m_size) {
217 T* new_data = allocate_and_copy(size);
221 construct_range(new_data + m_size, new_data + size, *value);
223 construct_range(new_data + m_size, new_data + size);
225 destroy_range(0, m_size);
232template <
typename T, rcAllocH
int H>
235 rcSizeType tmp_cap = other.m_cap;
236 rcSizeType tmp_size = other.m_size;
237 T* tmp_data = other.m_data;
240 other.m_size = m_size;
241 other.m_data = m_data;
248template <
typename T, rcAllocH
int H>
250 for (T* p = begin; p < end; p++) {
255template <
typename T, rcAllocH
int H>
257 for (T* p = begin; p < end; p++) {
262template <
typename T, rcAllocH
int H>
264 for (rcSizeType i = 0 ; i < end - begin; i++) {
265 construct(dst + i, begin[i]);
268template <
typename T, rcAllocH
int H>
270 for (rcSizeType i = begin; i < end; i++) {
304 void push(
int item) { m_impl.push_back(item); }
305 void resize(
int size) { m_impl.resize(size); }
308 int v = m_impl.back();
312 int size()
const {
return static_cast<int>(m_impl.size()); }
313 int& operator[](
int index) {
return m_impl[index]; }
314 int operator[](
int index)
const {
return m_impl[index]; }
334 inline operator T*() {
return ptr; }
Legacy class. Prefer rcVector<int>.
Definition RecastAlloc.h:299
Definition RecastAlloc.h:286
Definition RecastAlloc.h:320
rcScopedDelete()
Constructs an instance with a null pointer.
Definition RecastAlloc.h:325
rcScopedDelete(T *p)
Definition RecastAlloc.h:329
Definition RecastAlloc.h:276
Definition RecastAlloc.h:95
Definition RecastAlloc.h:67