6 #ifndef RAVEN_PREVECTOR_H 7 #define RAVEN_PREVECTOR_H 15 #include <type_traits> 36 template<
unsigned int N,
typename T,
typename Size = u
int32_t,
typename Diff =
int32_t>
59 const T&
operator[](size_type pos)
const {
return ptr[pos]; }
110 const T&
operator[](size_type pos)
const {
return ptr[pos]; }
151 char direct[
sizeof(T) * N];
165 if (new_capacity <= N) {
183 char* new_indirect =
static_cast<char*
>(malloc(((
size_t)
sizeof(T)) * new_capacity));
184 assert(new_indirect);
186 T* dst =
reinterpret_cast<T*
>(new_indirect);
210 template<
typename InputIterator>
211 void assign(InputIterator first, InputIterator last) {
212 size_type n = last - first;
217 while (first != last) {
219 new(
static_cast<void*
>(
item_ptr(
size() - 1))) T(*first);
230 explicit prevector(size_type n,
const T& val = T()) : _size(0) {
238 template<
typename InputIterator>
239 prevector(InputIterator first, InputIterator last) : _size(0) {
240 size_type n = last - first;
242 while (first != last) {
244 new(
static_cast<void*
>(
item_ptr(
size() - 1))) T(*first);
252 while (it != other.
end()) {
264 if (&other ==
this) {
270 while (it != other.
end()) {
318 if (
size() > new_size) {
324 while (
size() < new_size) {
345 size_type p = pos -
begin();
346 size_type new_size =
size() + 1;
352 new(
static_cast<void*
>(
item_ptr(p))) T(value);
357 size_type p = pos -
begin();
358 size_type new_size =
size() + count;
364 for (size_type i = 0; i < count; i++) {
365 new(
static_cast<void*
>(
item_ptr(p + i))) T(value);
369 template<
typename InputIterator>
371 size_type p = pos -
begin();
372 difference_type count = last - first;
373 size_type new_size =
size() + count;
379 while (first != last) {
380 new(
static_cast<void*
>(
item_ptr(p))) T(*first);
387 return erase(pos, pos + 1);
398 char* endp = (
char*)&(*
end());
399 if (!std::is_trivially_destructible<T>::value) {
408 memmove(&(*first), &(*last), endp - ((
char*)(&(*last))));
413 size_type new_size =
size() + 1;
443 std::swap(_size, other.
_size);
447 if (!std::is_trivially_destructible<T>::value) {
464 if ((*b1) != (*b2)) {
474 return !(*
this == other);
477 bool operator<(const prevector<N, T, Size, Diff>& other)
const {
478 if (
size() < other.size()) {
481 if (
size() > other.size()) {
512 const value_type*
data()
const {
518 #endif // RAVEN_PREVECTOR_H bool operator!=(const prevector< N, T, Size, Diff > &other) const
T * direct_ptr(difference_type pos)
std::bidirectional_iterator_tag iterator_category
prevector & operator=(const prevector< N, T, Size, Diff > &other)
const value_type & const_reference
const_iterator operator--(int)
void resize(size_type new_size)
const T * operator->() const
const T & operator[](size_type pos) const
const value_type * const_pointer
void assign(size_type n, const T &val)
T * indirect_ptr(difference_type pos)
iterator operator+(size_type n)
bool operator<=(const_iterator x) const
iterator operator-(size_type n)
prevector(prevector< N, T, Size, Diff > &&other)
void assign(InputIterator first, InputIterator last)
reverse_iterator operator++(int)
iterator insert(iterator pos, const T &value)
reverse_iterator operator--(int)
const T & operator[](size_type pos) const
bool operator!=(iterator x) const
const_iterator & operator-=(size_type n)
const_reverse_iterator & operator--()
const_iterator & operator++()
void insert(iterator pos, InputIterator first, InputIterator last)
const_reverse_iterator operator--(int)
reverse_iterator(T *ptr_)
const T * operator->() const
const value_type * data() const
const T * direct_ptr(difference_type pos) const
size_t allocated_memory() const
bool operator==(const prevector< N, T, Size, Diff > &other) const
const_reverse_iterator & operator++()
T & operator[](size_type pos)
bool operator==(const_reverse_iterator x) const
bool operator>=(iterator x) const
prevector(const prevector< N, T, Size, Diff > &other)
prevector(size_type n, const T &val=T())
const_reverse_iterator operator++(int)
bool operator>=(const_iterator x) const
const T * item_ptr(difference_type pos) const
void push_back(const T &value)
prevector & operator=(prevector< N, T, Size, Diff > &&other)
const T & operator*() const
const_reverse_iterator rend() const
const_reverse_iterator rbegin() const
T & operator[](size_type pos)
void swap(prevector< N, T, Size, Diff > &other)
bool operator<=(iterator x) const
void insert(iterator pos, size_type count, const T &value)
const_iterator operator+(size_type n)
const_reverse_iterator(reverse_iterator x)
bool operator>(const_iterator x) const
bool operator==(iterator x) const
reverse_iterator rbegin()
char direct[sizeof(T) *N]
iterator & operator+=(size_type n)
iterator erase(iterator pos)
bool operator<(iterator x) const
const_reverse_iterator(const T *ptr_)
const T & operator[](size_type pos) const
const_iterator(iterator x)
reverse_iterator & operator++()
Implements a drop-in replacement for std::vector<T> which stores up to N elements directly (without h...
bool operator!=(const_reverse_iterator x) const
const_iterator & operator--()
bool operator==(reverse_iterator x) const
const_iterator operator-(size_type n)
void reserve(size_type new_capacity)
const T * operator->() const
void change_capacity(size_type new_capacity)
prevector(InputIterator first, InputIterator last)
T * item_ptr(difference_type pos)
void * memcpy(void *a, const void *b, size_t c)
bool operator>(iterator x) const
reverse_iterator & operator--()
std::random_access_iterator_tag iterator_category
union prevector::direct_or_indirect _union
const T & operator*() const
std::bidirectional_iterator_tag iterator_category
const_iterator operator++(int)
std::random_access_iterator_tag iterator_category
iterator erase(iterator first, iterator last)
bool operator!=(const_iterator x) const
difference_type friend operator-(iterator a, iterator b)
const T * indirect_ptr(difference_type pos) const
bool operator<(const_iterator x) const
const T & operator*() const
const_iterator end() const
iterator & operator-=(size_type n)
void * memmove(void *a, const void *b, size_t c)
const_iterator begin() const
bool operator==(const_iterator x) const
bool operator!=(reverse_iterator x) const
const_iterator & operator+=(size_type n)
const_iterator(const T *ptr_)
difference_type friend operator-(const_iterator a, const_iterator b)