2022-04-10 00:17:06 +00:00
|
|
|
#ifndef _RSTL_RESERVED_VECTOR_HPP
|
|
|
|
#define _RSTL_RESERVED_VECTOR_HPP
|
|
|
|
|
|
|
|
#include "types.h"
|
|
|
|
|
2022-08-09 23:03:51 +00:00
|
|
|
#include "rstl/construct.hpp"
|
|
|
|
#include "rstl/pointer_iterator.hpp"
|
2022-04-10 00:17:06 +00:00
|
|
|
|
|
|
|
namespace rstl {
|
2022-09-05 04:01:13 +00:00
|
|
|
template < typename T, int N >
|
2022-04-10 00:17:06 +00:00
|
|
|
class reserved_vector {
|
2022-09-05 04:01:13 +00:00
|
|
|
int x0_count;
|
2022-08-13 01:26:00 +00:00
|
|
|
u8 x4_data[N * sizeof(T)];
|
2022-04-10 00:17:06 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
typedef pointer_iterator< T, reserved_vector< T, N >, void > iterator;
|
|
|
|
typedef const_pointer_iterator< T, reserved_vector< T, N >, void > const_iterator;
|
|
|
|
|
2022-08-13 01:26:00 +00:00
|
|
|
inline iterator begin() { return iterator(data()); }
|
|
|
|
inline const_iterator begin() const { return const_iterator(data()); }
|
|
|
|
inline iterator end() { return iterator(data() + x0_count); }
|
|
|
|
inline const_iterator end() const { return const_iterator(data() + x0_count); }
|
2022-04-10 00:17:06 +00:00
|
|
|
|
2022-08-13 01:26:00 +00:00
|
|
|
reserved_vector() : x0_count(0) {}
|
2022-10-02 21:45:14 +00:00
|
|
|
// reserved_vector(const T& value) : x0_count(N) { rstl::uninitialized_fill_n(data(), N, value); }
|
2022-04-15 19:24:52 +00:00
|
|
|
reserved_vector(const reserved_vector& other) {
|
|
|
|
x0_count = other.size();
|
2022-09-13 04:26:54 +00:00
|
|
|
// rstl::uninitialized_copy_n(other.data(), size(), data());
|
2022-04-15 19:24:52 +00:00
|
|
|
rstl::uninitialized_copy_n(data(), other.data(), size());
|
|
|
|
}
|
|
|
|
reserved_vector& operator=(const reserved_vector& other) {
|
|
|
|
if (this != &other) {
|
|
|
|
clear();
|
|
|
|
rstl::uninitialized_copy_2(other.data(), data(), other.data() + other.size());
|
|
|
|
x0_count = other.x0_count;
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
void clear() {
|
2022-09-05 04:01:13 +00:00
|
|
|
for (int i = 0; i < x0_count; ++i) {
|
2022-10-01 06:19:09 +00:00
|
|
|
rstl::destroy(&data()[i]);
|
2022-04-10 00:17:06 +00:00
|
|
|
}
|
2022-04-15 19:24:52 +00:00
|
|
|
x0_count = 0;
|
|
|
|
}
|
|
|
|
|
2022-08-13 01:26:00 +00:00
|
|
|
~reserved_vector() { clear(); }
|
2022-04-10 00:17:06 +00:00
|
|
|
|
|
|
|
void push_back(const T& in) {
|
2022-09-19 04:19:46 +00:00
|
|
|
iterator out = begin() + x0_count;
|
|
|
|
out = in;
|
|
|
|
++x0_count;
|
2022-04-10 00:17:06 +00:00
|
|
|
}
|
|
|
|
|
2022-08-13 01:26:00 +00:00
|
|
|
inline T* data() { return reinterpret_cast< T* >(x4_data); }
|
|
|
|
inline const T* data() const { return reinterpret_cast< const T* >(x4_data); }
|
2022-09-05 04:01:13 +00:00
|
|
|
inline int size() const { return x0_count; }
|
|
|
|
inline int capacity() const { return N; }
|
2022-08-13 01:26:00 +00:00
|
|
|
inline T& front() { return data()[0]; }
|
|
|
|
inline const T& front() const { return data()[0]; }
|
|
|
|
inline T& back() { return data()[x0_count - 1]; }
|
|
|
|
inline const T& back() const { return data()[x0_count - 1]; }
|
2022-09-05 04:01:13 +00:00
|
|
|
inline T& operator[](int idx) { return data()[idx]; }
|
|
|
|
inline const T& operator[](int idx) const { return data()[idx]; }
|
2022-04-10 00:17:06 +00:00
|
|
|
};
|
|
|
|
} // namespace rstl
|
|
|
|
|
|
|
|
#endif
|