forked from OctaForge/libostd
vector: clear does not free buffer + add operator= for initializer list
parent
58faefa317
commit
ed24618061
|
@ -84,18 +84,17 @@ namespace octa {
|
||||||
|
|
||||||
~Vector() noexcept(IsNothrowDestructible<T>::value) {
|
~Vector() noexcept(IsNothrowDestructible<T>::value) {
|
||||||
clear();
|
clear();
|
||||||
|
delete[] (uchar *)p_buf;
|
||||||
|
p_buf = nullptr;
|
||||||
|
p_cap = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void clear() noexcept(IsNothrowDestructible<T>::value) {
|
void clear() noexcept(IsNothrowDestructible<T>::value) {
|
||||||
if (p_cap > 0) {
|
if (p_len > 0 && !octa::IsPod<T>()) {
|
||||||
if (!octa::IsPod<T>()) {
|
T *cur = p_buf, *last = p_buf + p_len;
|
||||||
T *cur = p_buf, *last = p_buf + p_len;
|
while (cur != last) (*cur++).~T();
|
||||||
while (cur != last) (*cur++).~T();
|
|
||||||
}
|
|
||||||
delete[] (uchar *)p_buf;
|
|
||||||
p_buf = nullptr;
|
|
||||||
p_len = p_cap = 0;
|
|
||||||
}
|
}
|
||||||
|
p_len = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
Vector<T> &operator=(const Vector<T> &v) noexcept(
|
Vector<T> &operator=(const Vector<T> &v) noexcept(
|
||||||
|
@ -112,6 +111,7 @@ namespace octa {
|
||||||
p_len = v.p_len;
|
p_len = v.p_len;
|
||||||
} else {
|
} else {
|
||||||
clear();
|
clear();
|
||||||
|
delete[] (uchar *)p_buf;
|
||||||
p_len = v.p_len;
|
p_len = v.p_len;
|
||||||
p_cap = v.p_cap;
|
p_cap = v.p_cap;
|
||||||
p_buf = (T *)new uchar[p_cap * sizeof(T)];
|
p_buf = (T *)new uchar[p_cap * sizeof(T)];
|
||||||
|
@ -134,12 +134,32 @@ namespace octa {
|
||||||
IsNothrowDestructible<T>::value
|
IsNothrowDestructible<T>::value
|
||||||
) {
|
) {
|
||||||
clear();
|
clear();
|
||||||
|
delete[] (uchar *)p_buf;
|
||||||
p_len = v.p_len;
|
p_len = v.p_len;
|
||||||
p_cap = v.p_cap;
|
p_cap = v.p_cap;
|
||||||
p_buf = v.disown();
|
p_buf = v.disown();
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Vector<T> &operator=(InitializerList<T> il) noexcept(
|
||||||
|
IsNothrowDestructible<T>::value &&
|
||||||
|
IsNothrowCopyConstructible<T>::value
|
||||||
|
) {
|
||||||
|
clear();
|
||||||
|
size_t ilen = il.length();
|
||||||
|
reserve(ilen);
|
||||||
|
if (octa::IsPod<T>()) {
|
||||||
|
memcpy(p_buf, il.get(), ilen);
|
||||||
|
} else {
|
||||||
|
T *buf = p_buf, *ibuf = il.get(), *last = il.get() + ilen;
|
||||||
|
while (ibuf != last) {
|
||||||
|
new (buf++) T(*ibuf++);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
p_len = ilen;
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
void resize(size_t n, const T &v = T()) noexcept(
|
void resize(size_t n, const T &v = T()) noexcept(
|
||||||
IsNothrowDestructible<T>::value
|
IsNothrowDestructible<T>::value
|
||||||
&& IsNothrowMoveConstructible<T>::value
|
&& IsNothrowMoveConstructible<T>::value
|
||||||
|
|
Loading…
Reference in New Issue