2022-04-10 00:17:06 +00:00
|
|
|
#ifndef _RSTL_VECTOR_HPP
|
|
|
|
#define _RSTL_VECTOR_HPP
|
|
|
|
|
|
|
|
#include "types.h"
|
|
|
|
|
2022-08-09 23:03:51 +00:00
|
|
|
#include "rstl/pointer_iterator.hpp"
|
|
|
|
#include "rstl/rmemory_allocator.hpp"
|
2022-04-10 00:17:06 +00:00
|
|
|
|
|
|
|
namespace rstl {
|
2022-09-13 04:26:54 +00:00
|
|
|
// template < typename T, typename Alloc >
|
|
|
|
// struct allocator_auto_ptr {
|
|
|
|
// allocator_auto_ptr(T* ptr, Alloc* alloc) : ptr(ptr) {}
|
|
|
|
// ~allocator_auto_ptr() {
|
|
|
|
// if (ptr != nullptr) {
|
|
|
|
// Alloc::deallocate(ptr);
|
|
|
|
// ptr = nullptr;
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
|
|
|
|
// T* release() { T* v = ptr; ptr = nullptr; return v; }
|
|
|
|
|
|
|
|
// private:
|
|
|
|
// T* ptr;
|
|
|
|
// };
|
|
|
|
|
2022-04-10 00:17:06 +00:00
|
|
|
template < typename T, typename Alloc = rmemory_allocator >
|
|
|
|
class vector {
|
|
|
|
Alloc x0_allocator;
|
2022-09-05 04:01:13 +00:00
|
|
|
int x4_count;
|
|
|
|
int x8_capacity;
|
2022-04-10 00:17:06 +00:00
|
|
|
T* xc_items;
|
|
|
|
|
|
|
|
public:
|
|
|
|
typedef pointer_iterator< T, vector< T, Alloc >, Alloc > iterator;
|
|
|
|
typedef const_pointer_iterator< T, vector< T, Alloc >, Alloc > const_iterator;
|
|
|
|
|
|
|
|
inline iterator begin() { return iterator(xc_items); }
|
|
|
|
inline const_iterator begin() const { return const_iterator(xc_items); }
|
|
|
|
inline iterator end() { return iterator(xc_items + x4_count); }
|
|
|
|
inline const_iterator end() const { return const_iterator(xc_items + x4_count); }
|
|
|
|
inline vector() : x4_count(0), x8_capacity(0), xc_items(NULL) {}
|
2022-09-05 04:01:13 +00:00
|
|
|
inline vector(int count) : x4_count(0), x8_capacity(0), xc_items(0) { reserve(count); }
|
2022-04-10 00:17:06 +00:00
|
|
|
vector(const vector& other) {
|
|
|
|
x4_count = other.x4_count;
|
|
|
|
x8_capacity = other.x8_capacity;
|
2022-08-09 23:03:51 +00:00
|
|
|
if (other.x4_count == 0 && other.x8_capacity == 0) {
|
2022-04-10 00:17:06 +00:00
|
|
|
xc_items = NULL;
|
|
|
|
} else {
|
2022-09-05 04:01:13 +00:00
|
|
|
int sz = x8_capacity * sizeof(T);
|
2022-04-11 22:42:08 +00:00
|
|
|
if (sz == 0) {
|
2022-04-10 00:17:06 +00:00
|
|
|
xc_items = NULL;
|
|
|
|
} else {
|
2022-04-11 22:42:08 +00:00
|
|
|
x0_allocator.allocate(xc_items, sz);
|
2022-04-10 00:17:06 +00:00
|
|
|
}
|
2022-09-13 04:26:54 +00:00
|
|
|
// rstl::uninitialized_copy_n(other.data(), x4_count, data());
|
2022-04-11 22:42:08 +00:00
|
|
|
rstl::uninitialized_copy_n(data(), other.data(), x4_count);
|
2022-04-10 00:17:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
~vector() {
|
|
|
|
rstl::destroy(begin(), end());
|
2022-04-11 22:42:08 +00:00
|
|
|
x0_allocator.deallocate(xc_items);
|
2022-04-10 00:17:06 +00:00
|
|
|
}
|
|
|
|
|
2022-09-05 04:01:13 +00:00
|
|
|
void reserve(int size);
|
2022-09-19 04:19:46 +00:00
|
|
|
iterator erase(iterator it);
|
2022-04-15 19:24:52 +00:00
|
|
|
|
2022-04-10 00:17:06 +00:00
|
|
|
void push_back(const T& in) {
|
|
|
|
if (x4_count >= x8_capacity) {
|
|
|
|
reserve(x8_capacity != 0 ? x8_capacity * 2 : 4);
|
|
|
|
}
|
|
|
|
iterator out = begin() + x4_count;
|
|
|
|
out = in;
|
|
|
|
++x4_count;
|
|
|
|
}
|
|
|
|
|
2022-04-15 19:24:52 +00:00
|
|
|
vector& operator=(const vector& other) {
|
|
|
|
if (this == &other)
|
|
|
|
return *this;
|
|
|
|
clear();
|
|
|
|
if (other.size() == 0) {
|
|
|
|
x0_allocator.deallocate(xc_items);
|
|
|
|
x4_count = 0;
|
|
|
|
x8_capacity = 0;
|
|
|
|
xc_items = nullptr;
|
|
|
|
} else {
|
|
|
|
reserve(other.size());
|
|
|
|
rstl::uninitialized_copy(data(), other.data(), other.data() + other.size());
|
|
|
|
x4_count = other.x4_count;
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
void clear() {
|
|
|
|
rstl::destroy(begin(), end());
|
|
|
|
x4_count = 0;
|
|
|
|
}
|
|
|
|
|
2022-04-10 00:17:06 +00:00
|
|
|
inline T* data() { return xc_items; }
|
|
|
|
inline const T* data() const { return xc_items; }
|
2022-09-05 04:01:13 +00:00
|
|
|
inline int size() const { return x4_count; }
|
2022-09-21 05:18:07 +00:00
|
|
|
inline bool empty() const { return x4_count == 0; }
|
2022-09-05 04:01:13 +00:00
|
|
|
inline int capacity() const { return x8_capacity; }
|
2022-09-21 05:18:07 +00:00
|
|
|
inline T& at(int idx) { return xc_items[idx]; }
|
|
|
|
inline const T& at(int idx) const { return xc_items[idx]; }
|
|
|
|
inline T& front() { return at(0); }
|
|
|
|
inline const T& front() const { return at(0); }
|
|
|
|
inline T& back() { return at(x4_count - 1); }
|
|
|
|
inline const T& back() const { return at(x4_count - 1); }
|
2022-09-05 04:01:13 +00:00
|
|
|
inline T& operator[](int idx) { return xc_items[idx]; }
|
|
|
|
inline const T& operator[](int idx) const { return xc_items[idx]; }
|
2022-04-10 00:17:06 +00:00
|
|
|
};
|
2022-04-15 19:24:52 +00:00
|
|
|
|
|
|
|
template < typename T, typename Alloc >
|
2022-09-05 04:01:13 +00:00
|
|
|
void vector< T, Alloc >::reserve(int size) {
|
2022-04-15 19:24:52 +00:00
|
|
|
if (size <= x8_capacity)
|
|
|
|
return;
|
2022-09-05 04:01:13 +00:00
|
|
|
int sz = size * sizeof(T);
|
2022-04-15 19:24:52 +00:00
|
|
|
T* newData;
|
|
|
|
if (sz == 0) {
|
|
|
|
newData = nullptr;
|
|
|
|
} else {
|
|
|
|
x0_allocator.allocate(newData, sz);
|
|
|
|
}
|
|
|
|
rstl::uninitialized_copy(begin(), end(), newData);
|
|
|
|
rstl::destroy(xc_items, xc_items + x4_count);
|
|
|
|
x0_allocator.deallocate(xc_items);
|
|
|
|
xc_items = newData;
|
|
|
|
x8_capacity = size;
|
|
|
|
}
|
2022-08-13 01:26:00 +00:00
|
|
|
|
2022-08-14 18:38:41 +00:00
|
|
|
typedef vector< void > unk_vector;
|
2022-08-13 01:26:00 +00:00
|
|
|
CHECK_SIZEOF(unk_vector, 0x10)
|
2022-04-10 00:17:06 +00:00
|
|
|
} // namespace rstl
|
|
|
|
|
|
|
|
#endif
|