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

View File

@ -1,25 +1,25 @@
#pragma once #pragma once
#include <cassert>
#include <cstdlib>
#include <cstring>
#include <memory>
#include "pocketpy/common/traits.hpp" #include "pocketpy/common/traits.hpp"
#include "pocketpy/common/types.hpp" #include "pocketpy/common/types.hpp"
#include <cstdlib> namespace pkpy {
#include <cstring>
#include <cassert>
#include <memory>
namespace pkpy{ template <typename T>
struct array {
template<typename T>
struct array{
T* _data; T* _data;
int _size; int _size;
using size_type = int; using size_type = int;
array(): _data(nullptr), _size(0) {} array() : _data(nullptr), _size(0) {}
array(int size): _data((T*)std::malloc(sizeof(T) * size)), _size(size) {} array(int size) : _data((T*)std::malloc(sizeof(T) * size)), _size(size) {}
array(array&& other) noexcept: _data(other._data), _size(other._size) { array(array&& other) noexcept : _data(other._data), _size(other._size) {
other._data = nullptr; other._data = nullptr;
other._size = 0; other._size = 0;
} }
@ -27,12 +27,12 @@ struct array{
array(explicit_copy_t, const array& other) { array(explicit_copy_t, const array& other) {
_data = (T*)std::malloc(sizeof(T) * other._size); _data = (T*)std::malloc(sizeof(T) * other._size);
_size = other._size; _size = other._size;
for(int i=0; i<_size; i++) _data[i] = other._data[i]; for (int i = 0; i < _size; i++) _data[i] = other._data[i];
} }
array(T* data, int size): _data(data), _size(size) {} array(T* data, int size) : _data(data), _size(size) {}
array& operator=(array&& other) noexcept{ array& operator=(array&& other) noexcept {
if(_data){ if (_data) {
std::destroy(begin(), end()); std::destroy(begin(), end());
std::free(_data); std::free(_data);
} }
@ -57,8 +57,8 @@ struct array{
int size() const { return _size; } int size() const { return _size; }
T* begin() const{ return _data; } T* begin() const { return _data; }
T* end() const{ return _data + _size; } T* end() const { return _data + _size; }
T* data() const { return _data; } T* data() const { return _data; }
std::pair<T*, int> detach() noexcept { std::pair<T*, int> detach() noexcept {
@ -69,42 +69,43 @@ struct array{
} }
~array() { ~array() {
if(_data){ if (_data) {
std::destroy(begin(), end()); std::destroy(begin(), end());
std::free(_data); std::free(_data);
} }
} }
}; };
template <typename T>
template<typename T> struct vector {
struct vector{
T* _data; T* _data;
int _capacity; int _capacity;
int _size; int _size;
using size_type = int; using size_type = int;
vector(): _data(nullptr), _capacity(0), _size(0) {} vector() : _data(nullptr), _capacity(0), _size(0) {}
vector(int size): vector(int size)
_data((T*)std::malloc(sizeof(T) * size)), : _data((T*)std::malloc(sizeof(T) * size)),
_capacity(size), _size(size) {} _capacity(size),
vector(vector&& other) noexcept: _size(size) {}
_data(other._data), _capacity(other._capacity), _size(other._size) { vector(vector&& other) noexcept
: _data(other._data), _capacity(other._capacity), _size(other._size) {
other._data = nullptr; other._data = nullptr;
other._capacity = 0; other._capacity = 0;
other._size = 0; other._size = 0;
} }
vector(const vector& other) = delete; vector(const vector& other) = delete;
vector(explicit_copy_t, const vector& other): vector(explicit_copy_t, const vector& other)
_data((T*)std::malloc(sizeof(T) * other._size)), : _data((T*)std::malloc(sizeof(T) * other._size)),
_capacity(other._size), _size(other._size) { _capacity(other._size),
for(int i=0; i<_size; i++) _data[i] = other._data[i]; _size(other._size) {
for (int i = 0; i < _size; i++) _data[i] = other._data[i];
} }
// allow move // allow move
vector& operator=(vector&& other) noexcept{ vector& operator=(vector&& other) noexcept {
if(_data){ if (_data) {
std::destroy(begin(), end()); std::destroy(begin(), end());
std::free(_data); std::free(_data);
} }
@ -117,87 +118,88 @@ struct vector{
bool empty() const { return _size == 0; } bool empty() const { return _size == 0; }
int size() const { return _size; } int size() const { return _size; }
int capacity() const { return _capacity; } int capacity() const { return _capacity; }
T& back() { return _data[_size-1]; } T& back() { return _data[_size - 1]; }
T* begin() const { return _data; } T* begin() const { return _data; }
T* end() const { return _data + _size; } T* end() const { return _data + _size; }
T* data() const { return _data; } T* data() const { return _data; }
void reserve(int cap){ void reserve(int cap) {
if(cap < 4) cap = 4; // minimum capacity if (cap < 4) cap = 4; // minimum capacity
if(cap <= capacity()) return; if (cap <= capacity()) return;
T* new_data = (T*)std::malloc(sizeof(T) * cap); T* new_data = (T*)std::malloc(sizeof(T) * cap);
if constexpr(is_trivially_relocatable_v<T>){ if constexpr (is_trivially_relocatable_v<T>) {
std::memcpy(new_data, _data, sizeof(T) * _size); std::memcpy(new_data, _data, sizeof(T) * _size);
}else{ } else {
for(int i=0; i<_size; i++){ for (int i = 0; i < _size; i++) {
new(&new_data[i]) T(std::move(_data[i])); new (&new_data[i]) T(std::move(_data[i]));
_data[i].~T(); _data[i].~T();
} }
} }
if(_data) std::free(_data); if (_data) std::free(_data);
_data = new_data; _data = new_data;
_capacity = cap; _capacity = cap;
} }
void resize(int size){ void resize(int size) {
reserve(size); reserve(size);
_size = size; _size = size;
} }
void push_back(const T& t){ void push_back(const T& t) {
if(_size == _capacity) reserve(_capacity * 2); if (_size == _capacity) reserve(_capacity * 2);
new (&_data[_size++]) T(t); new (&_data[_size++]) T(t);
} }
void push_back(T&& t){ void push_back(T&& t) {
if(_size == _capacity) reserve(_capacity * 2); if (_size == _capacity) reserve(_capacity * 2);
new(&_data[_size++]) T(std::move(t)); new (&_data[_size++]) T(std::move(t));
} }
bool contains(const T& t) const { bool contains(const T& t) const {
for(int i=0; i<_size; i++){ for (int i = 0; i < _size; i++) {
if(_data[i] == t) return true; if (_data[i] == t) return true;
} }
return false; return false;
} }
template<typename... Args> template <typename... Args>
void emplace_back(Args&&... args){ void emplace_back(Args&&... args) {
if(_size == _capacity) reserve(_capacity * 2); if (_size == _capacity) reserve(_capacity * 2);
new(&_data[_size++]) T(std::forward<Args>(args)...); new (&_data[_size++]) T(std::forward<Args>(args)...);
} }
T& operator[](int i) { return _data[i]; } T& operator[](int i) { return _data[i]; }
const T& operator[](int i) const { return _data[i]; } const T& operator[](int i) const { return _data[i]; }
void extend(T* begin, T* end){ void extend(T* begin, T* end) {
int n = end - begin; int n = end - begin;
reserve(_size + n); reserve(_size + n);
for(int i=0; i<n; i++) new(&_data[_size++]) T(begin[i]); for (int i = 0; i < n; i++) new (&_data[_size++]) T(begin[i]);
} }
void insert(int index, const T& t){ void insert(int index, const T& t) {
if(_size == _capacity) reserve(_capacity * 2); if (_size == _capacity) reserve(_capacity * 2);
for(int i=_size; i>index; i--) _data[i] = std::move(_data[i-1]); for (int i = _size; i > index; i--) _data[i] = std::move(_data[i - 1]);
_data[index] = t; _data[index] = t;
_size++; _size++;
} }
void erase(int index){ 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--; _size--;
} }
void pop_back(){ void pop_back() {
assert(_size > 0); assert(_size > 0);
_size--; _size--;
if constexpr(!std::is_trivially_destructible_v<T>){ if constexpr (!std::is_trivially_destructible_v<T>) {
_data[_size].~T(); _data[_size].~T();
} }
} }
void clear(){ void clear() {
std::destroy(begin(), end()); std::destroy(begin(), end());
_size = 0; _size = 0;
} }
@ -210,45 +212,50 @@ struct vector{
return retval; return retval;
} }
void swap(vector& other){ void swap(vector& other) {
std::swap(_data, other._data); std::swap(_data, other._data);
std::swap(_capacity, other._capacity); std::swap(_capacity, other._capacity);
std::swap(_size, other._size); std::swap(_size, other._size);
} }
~vector(){ ~vector() {
if(_data){ if (_data) {
std::destroy(begin(), end()); std::destroy(begin(), end());
std::free(_data); std::free(_data);
} }
} }
}; };
template <typename T, typename Container=vector<T>> template <typename T, typename Container = vector<T>>
class stack{ class stack {
Container vec; Container vec;
public:
void push(const T& t){ vec.push_back(t); } public:
void push(T&& t){ vec.push_back(std::move(t)); } void push(const T& t) { vec.push_back(t); }
template<typename... Args> void push(T&& t) { vec.push_back(std::move(t)); }
void emplace(Args&&... args){ template <typename... Args>
void emplace(Args&&... args) {
vec.emplace_back(std::forward<Args>(args)...); vec.emplace_back(std::forward<Args>(args)...);
} }
void pop(){ vec.pop_back(); } void pop() { vec.pop_back(); }
void clear(){ vec.clear(); } void clear() { vec.clear(); }
bool empty() const { return vec.empty(); } bool empty() const { return vec.empty(); }
typename Container::size_type size() const { return vec.size(); } typename Container::size_type size() const { return vec.size(); }
T& top(){ return vec.back(); } T& top() { return vec.back(); }
const T& top() const { 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() {
void reserve(int n){ vec.reserve(n); } T t = std::move(vec.back());
Container& container() { return vec; } vec.pop_back();
return t;
}
void reserve(int n) { vec.reserve(n); }
Container& container() { return vec; }
const Container& container() const { return vec; } const Container& container() const { return vec; }
}; };
template <typename T, typename Container=vector<T>> template <typename T, typename Container = vector<T>>
class stack_no_copy: public stack<T, Container>{ class stack_no_copy : public stack<T, Container> {
public: public:
stack_no_copy() = default; stack_no_copy() = default;
stack_no_copy(const stack_no_copy& other) = delete; stack_no_copy(const stack_no_copy& other) = delete;
stack_no_copy& operator=(const stack_no_copy& other) = delete; stack_no_copy& operator=(const stack_no_copy& other) = delete;
@ -256,210 +263,183 @@ public:
stack_no_copy& operator=(stack_no_copy&& other) noexcept = default; stack_no_copy& operator=(stack_no_copy&& other) noexcept = default;
}; };
} // namespace pkpy } // namespace pkpy
namespace pkpy { namespace pkpy {
template<typename T, std::size_t N> template <typename T, std::size_t N>
class small_vector class small_vector {
{ alignas(T) char m_buffer[sizeof(T) * N];
alignas(T) char m_buffer[sizeof(T) * N]; T* m_begin;
T* m_begin; T* m_end;
T* m_end; T* m_max;
T* m_max;
public: public:
using value_type = T; using value_type = T;
using size_type = int; using size_type = int;
using difference_type = int; using difference_type = int;
using reference = T&; using reference = T&;
using const_reference = const T&; using const_reference = const T&;
using pointer = T*; using pointer = T*;
using const_pointer = const T*; using const_pointer = const T*;
using iterator = T*; using iterator = T*;
using const_iterator = const T*; using const_iterator = const T*;
using reverse_iterator = std::reverse_iterator<iterator>; using reverse_iterator = std::reverse_iterator<iterator>;
using const_reverse_iterator = std::reverse_iterator<const_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 {
[[nodiscard]] size_type size() const { return m_end - m_begin; } return m_begin == reinterpret_cast<const T*>(m_buffer);
[[nodiscard]] size_type capacity() const { return m_max - m_begin; } }
[[nodiscard]] bool empty() const { return m_begin == m_end; } [[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; }
pointer data() { return m_begin; } pointer data() { return m_begin; }
const_pointer data() const { return m_begin; } const_pointer data() const { return m_begin; }
reference operator[](size_type index) { return m_begin[index]; } reference operator[](size_type index) { return m_begin[index]; }
const_reference operator[](size_type index) const { return m_begin[index]; } const_reference operator[](size_type index) const { return m_begin[index]; }
iterator begin() { return m_begin; } iterator begin() { return m_begin; }
const_iterator begin() const { return m_begin; } const_iterator begin() const { return m_begin; }
iterator end() { return m_end; } iterator end() { return m_end; }
const_iterator end() const { return m_end; } const_iterator end() const { return m_end; }
reference front() { return *begin(); } reference front() { return *begin(); }
const_reference front() const { return *begin(); } const_reference front() const { return *begin(); }
reference back() { return *(end() - 1); } reference back() { return *(end() - 1); }
const_reference back() const { return *(end() - 1); } const_reference back() const { return *(end() - 1); }
reverse_iterator rbegin() { return reverse_iterator(end()); } reverse_iterator rbegin() { return reverse_iterator(end()); }
const_reverse_iterator rbegin() const const_reverse_iterator rbegin() const {
{ return const_reverse_iterator(end());
return const_reverse_iterator(end()); }
} reverse_iterator rend() { return reverse_iterator(begin()); }
reverse_iterator rend() { return reverse_iterator(begin()); } const_reverse_iterator rend() const {
const_reverse_iterator rend() const return const_reverse_iterator(begin());
{ }
return const_reverse_iterator(begin());
} private:
private: static void uninitialized_copy_n(const void* src, size_type n, void* dest) {
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);
if constexpr (std::is_trivially_copyable_v<T>) } else {
{ for (size_type i = 0; i < n; i++) {
std::memcpy(dest, src, sizeof(T) * n); ::new ((T*)dest + i) T(*((const T*)src + 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) static void uninitialized_relocate_n(void* src, size_type n, void* dest) {
{ if constexpr (is_trivially_relocatable_v<T>) {
if constexpr (is_trivially_relocatable_v<T>) std::memcpy(dest, src, sizeof(T) * n);
{ } else {
std::memcpy(dest, src, sizeof(T) * n); for (size_type i = 0; i < n; i++) {
} ::new ((T*)dest + i) T(std::move(*((T*)src + i)));
else ((T*)src + i)->~T();
{
for (size_type i = 0; i < n; i++)
{
::new((T*) dest + i) T(std::move(*((T*) src + i)));
((T*) src + i)->~T();
}
} }
} }
}
public: 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 size = other.size(); const auto capacity = other.capacity();
const auto capacity = other.capacity(); m_begin = reinterpret_cast<T*>(
m_begin = reinterpret_cast<T*>(other.is_small() ? m_buffer : std::malloc(sizeof(T) * capacity)); other.is_small() ? m_buffer : std::malloc(sizeof(T) * capacity));
uninitialized_copy_n(other.m_begin, size, this->m_begin); uninitialized_copy_n(other.m_begin, size, this->m_begin);
m_end = m_begin + size; m_end = m_begin + size;
m_max = m_begin + capacity; m_max = m_begin + capacity;
}
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 {
m_begin = other.m_begin;
m_end = other.m_end;
m_max = other.m_max;
} }
other.m_begin = reinterpret_cast<T*>(other.m_buffer);
other.m_end = other.m_begin;
other.m_max = other.m_begin + N;
}
small_vector(small_vector&& other) noexcept small_vector& operator=(const small_vector& other) noexcept {
{ if (this != &other) {
if(other.is_small()) ~small_vector();
{ ::new (this) small_vector(other);
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
{
m_begin = other.m_begin;
m_end = other.m_end;
m_max = other.m_max;
}
other.m_begin = reinterpret_cast<T*>(other.m_buffer);
other.m_end = other.m_begin;
other.m_max = other.m_begin + N;
} }
return *this;
}
small_vector& operator=(const small_vector& other) noexcept small_vector& operator=(small_vector&& other) noexcept {
{ if (this != &other) {
if (this != &other) ~small_vector();
{ ::new (this) small_vector(std::move(other));
~small_vector();
::new (this) small_vector(other);
}
return *this;
} }
return *this;
}
small_vector& operator=(small_vector&& other) noexcept ~small_vector() {
{ std::destroy(m_begin, m_end);
if (this != &other) if (!is_small()) std::free(m_begin);
{ }
~small_vector();
:: new (this) small_vector(std::move(other));
}
return *this;
}
~small_vector() template <typename... Args>
{ void emplace_back(Args&&... args) noexcept {
std::destroy(m_begin, m_end); if (m_end == m_max) {
if (!is_small()) std::free(m_begin); const auto new_capacity = capacity() * 2;
} const auto size = this->size();
if (!is_small()) {
template<typename... Args> if constexpr (is_trivially_relocatable_v<T>) {
void emplace_back(Args&& ...args) noexcept m_begin = (pointer)std::realloc(m_begin,
{ sizeof(T) * new_capacity);
if (m_end == m_max) } else {
{ auto new_data =
const auto new_capacity = capacity() * 2; (pointer)std::malloc(sizeof(T) * new_capacity);
const auto size = this->size(); uninitialized_relocate_n(m_begin, size, new_data);
if (!is_small()) std::free(m_begin);
{
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
{
auto new_data = (pointer) std::malloc(sizeof(T) * new_capacity);
uninitialized_relocate_n(m_buffer, size, new_data);
m_begin = new_data; m_begin = new_data;
} }
m_end = m_begin + size; } else {
m_max = m_begin + new_capacity; auto new_data = (pointer)std::malloc(sizeof(T) * new_capacity);
uninitialized_relocate_n(m_buffer, size, new_data);
m_begin = new_data;
} }
::new(m_end) T(std::forward<Args>(args)...); m_end = m_begin + size;
m_end++; m_max = m_begin + new_capacity;
} }
::new (m_end) T(std::forward<Args>(args)...);
m_end++;
}
void push_back(const T& value) { emplace_back(value); } void push_back(const T& value) { emplace_back(value); }
void push_back(T&& value) { emplace_back(std::move(value)); } void push_back(T&& value) { emplace_back(std::move(value)); }
void pop_back() void pop_back() {
{ m_end--;
m_end--; if constexpr (!std::is_trivially_destructible_v<T>) {
if constexpr (!std::is_trivially_destructible_v<T>) m_end->~T();
{
m_end->~T();
}
} }
}
void clear() void clear() {
{ std::destroy(m_begin, m_end);
std::destroy(m_begin, m_end); m_end = m_begin;
m_end = m_begin; }
} };
};
template<typename T, std::size_t N> 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:
public: small_vector_2() = default;
small_vector_2() = default; small_vector_2(const small_vector_2& other) = delete;
small_vector_2(const small_vector_2& other) = delete; small_vector_2& operator=(const small_vector_2& other) = delete;
small_vector_2& operator=(const small_vector_2& other) = delete; small_vector_2(small_vector_2&& other) = delete;
small_vector_2(small_vector_2&& other) = delete; small_vector_2& operator=(small_vector_2&& other) = delete;
small_vector_2& operator=(small_vector_2&& other) = delete; };
}; } // namespace pkpy
} // namespace pkpy