18#ifndef AGXDATA_VECTOR_H
19#define AGXDATA_VECTOR_H
55 void resize(
size_t size,
const T& value = T());
65 T*
increment(
size_t numElements = 1,
const T& value = T());
72 template <
typename T2>
90 void erase(
size_t index);
91 void erase(
size_t start,
size_t end);
110 template <
typename T2>
111 bool findAndErase(
const T2& element,
bool searchMultiple =
false);
114 void insert(
size_t index,
const T& value);
116 template <
typename InputIterator>
117 void insert(
iterator it, InputIterator first, InputIterator last);
120 void reallocate(
size_t size);
129 template <
typename T>
134 template <
typename T>
140 template <
typename T>
146 template <
typename T>
150 size_t size = this->buffer()->size();
151 size_t oldSize = this->buffer()->sizeParameter->get();
155 this->buffer()->sizeParameter->updateEvent.removeCallback(&this->buffer()->m_resizeCallback);
156 this->buffer()->sizeParameter->set(size);
157 this->buffer()->sizeParameter->updateEvent.addCallbackFirst(&this->buffer()->m_resizeCallback);
159 this->buffer()->m_eventDispatch.triggerResizeEvent(this->buffer(), (
agx::Index)size, (
agx::Index)oldSize);
164 template <
typename T>
169 if (size > this->buffer()->
m_size)
171 this->increment(size - this->buffer()->
m_size, value);
176 this->reallocate(size);
178 this->destroyElements(size, this->buffer()->
m_size);
182 template <
typename T>
185 this->buffer()->
reserve(std::max((
size_t)4, size));
190 template <
typename T>
194 this->reallocate(size);
197 template <
typename T>
202 size_t currentSize = this->buffer()->m_size;
203 size_t newSize = currentSize + numElements;
207 this->buffer()->m_size = newSize;
208 this->range().end() = newSize;
210 for(
size_t i = currentSize; i < newSize; ++i)
211 ::new((
void *)(this->ptr() + i)) T(value);
213 return this->ptr() + currentSize;
216 template <
typename T>
220 agxAssert(this->buffer()->size() >= numElements);
222 size_t newSize = this->buffer()->m_size - numElements;
223 for (
size_t i = newSize; i < this->buffer()->m_size; ++i)
226 this->range().end() = newSize;
227 this->buffer()->m_size = newSize;
230 template <
typename T>
template <
typename T2>
238 this->buffer()->m_size++;
239 this->range().end()++;
240 ::new((
void *)&this->back()) T(value);
243 template <
typename T>
250 this->range().end()--;
251 this->buffer()->m_size--;
254 template <
typename T>
259 size_t index = position - this->begin();
261 return this->begin() + index;
264 template <
typename T>
269 size_t startIndex = start - this->begin();
270 size_t endIndex = end - this->begin();
271 this->erase(startIndex, endIndex);
273 return this->begin() + endIndex;
276 template <
typename T>
281 for (
size_t i = index + 1; i < this->buffer()->m_size; i++)
282 this->ptr()[i-1] = this->ptr()[i];
285 this->ptr()[this->buffer()->m_size - 1].~T();
287 this->buffer()->m_size--;
290 template <
typename T>
295 agxAssert1(start < end && end <= this->buffer()->
m_size,
"Erase bounds are not with array bounds!");
296 size_t numElements = end - start;
299 for (
size_t i = end; i < this->buffer()->m_size; i++)
300 this->ptr()[i-numElements] = this->ptr()[i];
303 for(
size_t i = this->buffer()->
m_size - numElements; i < this->buffer()->m_size; ++i)
306 this->buffer()->m_size -= numElements;
309 template <
typename T>
314 size_t index = position - this->begin();
315 this->eraseFast(index);
316 return this->begin() + index;
319 template <
typename T>
323 agxAssertN(index < this->buffer()->
m_size,
"Array index %llu is out of bounds, current size is %llu", (
long long unsigned)index, (
long long unsigned)this->buffer()->
m_size);
325 this->ptr()[index] = this->ptr()[this->buffer()->m_size-1];
326 this->ptr()[this->buffer()->m_size - 1].~T();
327 this->buffer()->m_size--;
330 template <
typename T>
334 this->decrement(this->buffer()->
m_size);
337 template <
typename T>
template <
typename T2>
344 for (
size_t i = 0; i < this->buffer()->m_size; i++)
346 if (this->ptr()[i] == element)
361 template <
typename T>
366 size_t index = position - this->begin();
367 this->insert(index, value);
368 return this->begin() + index;
371 template <
typename T>
376 agxAssertN(index <= this->buffer()->
m_size,
"Insert with invalid index %llu, size is %llu", (
long long unsigned)index, (
long long unsigned)this->buffer()->
m_size);
381 for (
size_t i = this->buffer()->
m_size; i > index; i--) {
382 ::new((
void *)&this->ptr()[i]) T(this->ptr()[i-1]);
383 this->ptr()[i-1].~T();
386 ::new((
void *)&this->ptr()[index]) T(value);
387 this->buffer()->m_size++;
390 template <
typename T>
template <
typename InputIterator>
395 size_t numElements = std::distance( first, last );
397 if (numElements == 0)
400 size_t index = it - this->begin();
405 for (
size_t i = this->buffer()->
m_size; i > index; i--) {
406 ::new((
void *)&this->ptr()[i + numElements - 1]) T(this->ptr()[i-1]);
407 this->ptr()[i-1].~T();
410 for (; first != last; first++)
411 ::new((
void *)&this->ptr()[index++]) T(*first);
413 this->buffer()->m_size += numElements;
#define AGX_TEMPLATED_TYPE_BINDING(_Type, _Name)
#define AGX_VECTOR_SHRINK_THRESHOLD
const agxData::Buffer * buffer() const
Type-specific Array used for fast access into the data held by a Buffer.
Abstract representation of a data buffer.
agxData::EntityStorage * getStorage()
static agx::Real CAPACITY_MULTIPLIER
std::random_access_iterator_tag iterator_category
iterator erase(iterator position)
STL erase functionality.
void resize(size_t size, const T &value=T())
Resize the vector, which then enables direct addressing using the bracket '[]' operator.
void push_back(const T2 &value)
Append a new data element to the vector.
iterator eraseFast(const_iterator position)
Fast erase, replacing the erased element with the last element.
T * increment(size_t numElements=1, const T &value=T())
Resize using a increment.
ptrdiff_t difference_type
void clear()
Remove all elements, optionally with maintained buffer allocation.
bool findAndErase(const T2 &element, bool searchMultiple=false)
Find and erase an element.
void pop_back()
Remove the back element.
void reserve(size_t size)
Reserve capacity in the vector.
const T & const_reference
iterator insert(iterator position, const T &value)
void commit()
Commit all changes.
void decrement(size_t numElements=1)
#define agxAssert1(expr, msg)
#define agxAssertN(expr, format,...)
Contains classes for low level data storage for AGX.
Vector< agx::UInt32 > GenericVector