This commit is contained in:
blueloveTH 2024-06-02 21:57:15 +08:00
parent 9b3e6bb177
commit ed3e7856e8

View File

@ -1,13 +1,13 @@
#pragma once
#include <cassert>
#include <cstdlib>
#include <cstring>
#include <memory>
#include "pocketpy/common/traits.hpp"
#include "pocketpy/common/types.hpp"
#include <cstdlib>
#include <cstring>
#include <cassert>
#include <memory>
namespace pkpy {
template <typename T>
@ -76,7 +76,6 @@ struct array{
}
};
template <typename T>
struct vector {
T* _data;
@ -86,19 +85,21 @@ struct vector{
using size_type = int;
vector() : _data(nullptr), _capacity(0), _size(0) {}
vector(int size):
_data((T*)std::malloc(sizeof(T) * size)),
_capacity(size), _size(size) {}
vector(vector&& other) noexcept:
_data(other._data), _capacity(other._capacity), _size(other._size) {
vector(int size)
: _data((T*)std::malloc(sizeof(T) * size)),
_capacity(size),
_size(size) {}
vector(vector&& other) noexcept
: _data(other._data), _capacity(other._capacity), _size(other._size) {
other._data = nullptr;
other._capacity = 0;
other._size = 0;
}
vector(const vector& other) = delete;
vector(explicit_copy_t, const vector& other):
_data((T*)std::malloc(sizeof(T) * other._size)),
_capacity(other._size), _size(other._size) {
vector(explicit_copy_t, const vector& other)
: _data((T*)std::malloc(sizeof(T) * other._size)),
_capacity(other._size),
_size(other._size) {
for (int i = 0; i < _size; i++) _data[i] = other._data[i];
}
@ -185,7 +186,8 @@ struct vector{
}
void erase(int index) {
for(int i=index; i<_size-1; i++) _data[i] = std::move(_data[i+1]);
for (int i = index; i < _size - 1; i++)
_data[i] = std::move(_data[i + 1]);
_size--;
}
@ -227,6 +229,7 @@ struct vector{
template <typename T, typename Container = vector<T>>
class stack {
Container vec;
public:
void push(const T& t) { vec.push_back(t); }
void push(T&& t) { vec.push_back(std::move(t)); }
@ -240,7 +243,11 @@ public:
typename Container::size_type size() const { return vec.size(); }
T& top() { return vec.back(); }
const T& top() const { return vec.back(); }
T popx(){ T t = std::move(vec.back()); vec.pop_back(); return t; }
T popx() {
T t = std::move(vec.back());
vec.pop_back();
return t;
}
void reserve(int n) { vec.reserve(n); }
Container& container() { return vec; }
const Container& container() const { return vec; }
@ -258,11 +265,9 @@ public:
} // namespace pkpy
namespace pkpy {
template <typename T, std::size_t N>
class small_vector
{
class small_vector {
alignas(T) char m_buffer[sizeof(T) * N];
T* m_begin;
T* m_end;
@ -281,7 +286,9 @@ namespace pkpy {
using reverse_iterator = std::reverse_iterator<iterator>;
using const_reverse_iterator = std::reverse_iterator<const_iterator>;
[[nodiscard]] bool is_small() const { return m_begin == reinterpret_cast<const T*>(m_buffer); }
[[nodiscard]] bool is_small() const {
return m_begin == reinterpret_cast<const T*>(m_buffer);
}
[[nodiscard]] size_type size() const { return m_end - m_begin; }
[[nodiscard]] size_type capacity() const { return m_max - m_begin; }
[[nodiscard]] bool empty() const { return m_begin == m_end; }
@ -299,41 +306,30 @@ namespace pkpy {
reference back() { return *(end() - 1); }
const_reference back() const { return *(end() - 1); }
reverse_iterator rbegin() { return reverse_iterator(end()); }
const_reverse_iterator rbegin() const
{
const_reverse_iterator rbegin() const {
return const_reverse_iterator(end());
}
reverse_iterator rend() { return reverse_iterator(begin()); }
const_reverse_iterator rend() const
{
const_reverse_iterator rend() const {
return const_reverse_iterator(begin());
}
private:
static void uninitialized_copy_n(const void* src, size_type n, void* dest)
{
if constexpr (std::is_trivially_copyable_v<T>)
{
static void uninitialized_copy_n(const void* src, size_type n, void* dest) {
if constexpr (std::is_trivially_copyable_v<T>) {
std::memcpy(dest, src, sizeof(T) * n);
}
else
{
for (size_type i = 0; i < n; i++)
{
} else {
for (size_type i = 0; i < n; i++) {
::new ((T*)dest + i) T(*((const T*)src + i));
}
}
}
static void uninitialized_relocate_n(void* src, size_type n, void* dest)
{
if constexpr (is_trivially_relocatable_v<T>)
{
static void uninitialized_relocate_n(void* src, size_type n, void* dest) {
if constexpr (is_trivially_relocatable_v<T>) {
std::memcpy(dest, src, sizeof(T) * n);
}
else
{
for (size_type i = 0; i < n; i++)
{
} else {
for (size_type i = 0; i < n; i++) {
::new ((T*)dest + i) T(std::move(*((T*)src + i)));
((T*)src + i)->~T();
}
@ -341,29 +337,28 @@ namespace pkpy {
}
public:
small_vector() : m_begin(reinterpret_cast<T*>(m_buffer)), m_end(m_begin), m_max(m_begin + N) {}
small_vector()
: m_begin(reinterpret_cast<T*>(m_buffer)),
m_end(m_begin),
m_max(m_begin + N) {}
small_vector(const small_vector& other) noexcept
{
small_vector(const small_vector& other) noexcept {
const auto size = other.size();
const auto capacity = other.capacity();
m_begin = reinterpret_cast<T*>(other.is_small() ? m_buffer : std::malloc(sizeof(T) * capacity));
m_begin = reinterpret_cast<T*>(
other.is_small() ? m_buffer : std::malloc(sizeof(T) * capacity));
uninitialized_copy_n(other.m_begin, size, this->m_begin);
m_end = m_begin + size;
m_max = m_begin + capacity;
}
small_vector(small_vector&& other) noexcept
{
if(other.is_small())
{
small_vector(small_vector&& other) noexcept {
if (other.is_small()) {
m_begin = reinterpret_cast<T*>(m_buffer);
uninitialized_relocate_n(other.m_buffer, other.size(), m_buffer);
m_end = m_begin + other.size();
m_max = m_begin + N;
}
else
{
} else {
m_begin = other.m_begin;
m_end = other.m_end;
m_max = other.m_max;
@ -373,55 +368,44 @@ namespace pkpy {
other.m_max = other.m_begin + N;
}
small_vector& operator=(const small_vector& other) noexcept
{
if (this != &other)
{
small_vector& operator=(const small_vector& other) noexcept {
if (this != &other) {
~small_vector();
::new (this) small_vector(other);
}
return *this;
}
small_vector& operator=(small_vector&& other) noexcept
{
if (this != &other)
{
small_vector& operator=(small_vector&& other) noexcept {
if (this != &other) {
~small_vector();
::new (this) small_vector(std::move(other));
}
return *this;
}
~small_vector()
{
~small_vector() {
std::destroy(m_begin, m_end);
if (!is_small()) std::free(m_begin);
}
template <typename... Args>
void emplace_back(Args&& ...args) noexcept
{
if (m_end == m_max)
{
void emplace_back(Args&&... args) noexcept {
if (m_end == m_max) {
const auto new_capacity = capacity() * 2;
const auto size = this->size();
if (!is_small())
{
if constexpr (is_trivially_relocatable_v<T>)
{
m_begin = (pointer)std::realloc(m_begin, sizeof(T) * new_capacity);
}
else
{
auto new_data = (pointer) std::malloc(sizeof(T) * new_capacity);
if (!is_small()) {
if constexpr (is_trivially_relocatable_v<T>) {
m_begin = (pointer)std::realloc(m_begin,
sizeof(T) * new_capacity);
} else {
auto new_data =
(pointer)std::malloc(sizeof(T) * new_capacity);
uninitialized_relocate_n(m_begin, size, new_data);
std::free(m_begin);
m_begin = new_data;
}
}
else
{
} else {
auto new_data = (pointer)std::malloc(sizeof(T) * new_capacity);
uninitialized_relocate_n(m_buffer, size, new_data);
m_begin = new_data;
@ -436,25 +420,21 @@ namespace pkpy {
void push_back(const T& value) { emplace_back(value); }
void push_back(T&& value) { emplace_back(std::move(value)); }
void pop_back()
{
void pop_back() {
m_end--;
if constexpr (!std::is_trivially_destructible_v<T>)
{
if constexpr (!std::is_trivially_destructible_v<T>) {
m_end->~T();
}
}
void clear()
{
void clear() {
std::destroy(m_begin, m_end);
m_end = m_begin;
}
};
template <typename T, std::size_t N>
class small_vector_2: public small_vector<T, N>
{
class small_vector_2 : public small_vector<T, N> {
public:
small_vector_2() = default;
small_vector_2(const small_vector_2& other) = delete;