mirror of
https://github.com/pocketpy/pocketpy
synced 2025-10-21 12:00:18 +00:00
fix vector
This commit is contained in:
parent
bbe226b21f
commit
99de613655
@ -143,8 +143,8 @@ struct vector {
|
|||||||
|
|
||||||
vector(const vector& other) = delete;
|
vector(const vector& other) = delete;
|
||||||
|
|
||||||
vector(explicit_copy_t, const vector& other) :
|
vector(explicit_copy_t, const vector& other) : _capacity(other._size), _size(other._size) {
|
||||||
_capacity(other._size), _size(other._size), _data((T*)std::malloc(sizeof(T) * _capacity)) {
|
_data = (T*)std::malloc(sizeof(T) * _capacity);
|
||||||
uninitialized_copy_n(other._data, _size, _data);
|
uninitialized_copy_n(other._data, _size, _data);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -185,10 +185,14 @@ struct vector {
|
|||||||
_capacity = 0;
|
_capacity = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void reserve(int cap) {
|
T* _grow(int cap) {
|
||||||
if(cap < 4) cap = 4; // minimum capacity
|
if(cap < 4) cap = 4; // minimum capacity
|
||||||
if(cap <= capacity()) return;
|
if(cap <= capacity()) return _data;
|
||||||
T* new_data = (T*)std::malloc(sizeof(T) * cap);
|
return (T*)std::malloc(sizeof(T) * cap);
|
||||||
|
}
|
||||||
|
|
||||||
|
void reserve(int cap) {
|
||||||
|
T* new_data = _grow(cap);
|
||||||
uninitialized_relocate_n(_data, _size, new_data);
|
uninitialized_relocate_n(_data, _size, new_data);
|
||||||
if(_data) std::free(_data);
|
if(_data) std::free(_data);
|
||||||
_data = new_data;
|
_data = new_data;
|
||||||
@ -228,10 +232,12 @@ struct vector {
|
|||||||
assert(it >= begin() && it <= end());
|
assert(it >= begin() && it <= end());
|
||||||
int pos = it - begin();
|
int pos = it - begin();
|
||||||
if(_size == _capacity) {
|
if(_size == _capacity) {
|
||||||
T* new_data = (T*)std::malloc(sizeof(T) * _capacity * 2);
|
T* new_data = _grow(_capacity * 2);
|
||||||
uninitialized_relocate_n(_data, pos, new_data);
|
uninitialized_relocate_n(_data, pos, new_data);
|
||||||
new (new_data + pos) T(t);
|
new (new_data + pos) T(t);
|
||||||
uninitialized_relocate_n(_data + pos, _size - pos, new_data + pos + 1);
|
uninitialized_relocate_n(_data + pos, _size - pos, new_data + pos + 1);
|
||||||
|
if(_data) std::free(_data);
|
||||||
|
_data = new_data;
|
||||||
} else {
|
} else {
|
||||||
uninitialized_relocate_n<true, true>(_data + pos, _size - pos, _data + pos + 1);
|
uninitialized_relocate_n<true, true>(_data + pos, _size - pos, _data + pos + 1);
|
||||||
new (_data + pos) T(t);
|
new (_data + pos) T(t);
|
||||||
|
@ -358,12 +358,12 @@ int Str::count(const Str& sub) const {
|
|||||||
return cnt;
|
return cnt;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::map<std::string_view, uint16_t>& _interned() {
|
static std::map<std::string_view, uint16_t>& _interned() {
|
||||||
static std::map<std::string_view, uint16_t> interned;
|
static std::map<std::string_view, uint16_t> interned;
|
||||||
return interned;
|
return interned;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::map<uint16_t, std::string>& _r_interned() {
|
static std::map<uint16_t, std::string>& _r_interned() {
|
||||||
static std::map<uint16_t, std::string> r_interned;
|
static std::map<uint16_t, std::string> r_interned;
|
||||||
return r_interned;
|
return r_interned;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user