PIVector and PIDeque some code clean

This commit is contained in:
Бычков Андрей
2022-08-23 16:47:33 +03:00
parent 443eeed38b
commit b35ec1f30a
2 changed files with 51 additions and 49 deletions

View File

@@ -211,7 +211,6 @@ public:
inline PIVector<T> & operator =(const PIVector<T> & v) {
if (this == &v) return *this;
clear();
deleteT(piv_data, piv_size);
alloc(v.piv_size);
newT(piv_data, v.piv_data, piv_size);
return *this;
@@ -1163,7 +1162,7 @@ public:
PIVector<T> getRange(size_t index, size_t count) const {
if (index >= piv_size || count == 0) return PIVector<T>();
if (index + count > piv_size) count = piv_size - index;
return PIVector(&(piv_data[index]), count);
return PIVector(piv_data + index, count);
}
//! \~english Clear array, remove all elements.
@@ -1262,7 +1261,7 @@ public:
//! \~\sa \a size(), \a clear()
inline PIVector<T> & resize(size_t new_size, const T & e = T()) {
if (new_size < piv_size) {
deleteT(&(piv_data[new_size]), piv_size - new_size);
deleteT(piv_data + new_size, piv_size - new_size);
piv_size = new_size;
} else if (new_size > piv_size) {
expand(new_size, e);
@@ -1283,8 +1282,7 @@ public:
//! \~\sa \a size(), \a clear()
inline PIVector<T> & resize(size_t new_size, std::function<T(size_t i)> f) {
if (new_size < piv_size) {
T * de = &(piv_data[new_size]);
deleteT(de, piv_size - new_size);
deleteT(piv_data + new_size, piv_size - new_size);
piv_size = new_size;
} else if (new_size > piv_size) {
expand(new_size, f);
@@ -1349,7 +1347,7 @@ public:
alloc(piv_size + 1);
if (index < piv_size - 1) {
size_t os = piv_size - index - 1;
memmove((void*)(&(piv_data[index + 1])), (const void*)(&(piv_data[index])), os * sizeof(T));
memmove((void*)(piv_data + index + 1), (const void*)(piv_data + index), os * sizeof(T));
}
PIINTROSPECTION_CONTAINER_USED(T, 1)
elementNew(piv_data + index, e);
@@ -1366,7 +1364,7 @@ public:
alloc(piv_size + 1);
if (index < piv_size - 1) {
size_t os = piv_size - index - 1;
memmove((void*)(&(piv_data[index + 1])), (const void*)(&(piv_data[index])), os * sizeof(T));
memmove((void*)(piv_data + index + 1), (const void*)(piv_data + index), os * sizeof(T));
}
PIINTROSPECTION_CONTAINER_USED(T, 1)
elementNew(piv_data + index, std::move(e));
@@ -1390,7 +1388,7 @@ public:
ssize_t os = piv_size - index;
alloc(piv_size + v.piv_size);
if (os > 0) {
memmove((void*)(&(piv_data[index + v.piv_size])), (const void*)(&(piv_data[index])), os * sizeof(T));
memmove((void*)(piv_data + index + v.piv_size), (const void*)(piv_data + index), os * sizeof(T));
}
newT(piv_data + index, v.piv_data, v.piv_size);
return *this;
@@ -1407,10 +1405,11 @@ public:
//! [списка инициализации C++11](https://ru.cppreference.com/w/cpp/utility/initializer_list).
//! \~\sa \a append(), \a prepend(), \a remove()
inline PIVector<T> & insert(size_t index, std::initializer_list<T> init_list) {
if (init_list.size() == 0) return *this;
ssize_t os = piv_size - index;
alloc(piv_size + init_list.size());
if (os > 0) {
memmove((void*)(&(piv_data[index + init_list.size()])), (const void*)(&(piv_data[index])), os * sizeof(T));
memmove((void*)(piv_data + index + init_list.size()), (const void*)(piv_data + index), os * sizeof(T));
}
newT(piv_data + index, init_list.begin(), init_list.size());
return *this;
@@ -1429,14 +1428,14 @@ public:
if (count == 0) return *this;
if (index + count >= piv_size) {
if (index < piv_size) {
deleteT(&(piv_data[index]), piv_size - index);
deleteT(piv_data + index, piv_size - index);
piv_size = index;
}
return *this;
}
size_t os = piv_size - index - count;
deleteT(&(piv_data[index]), count);
memmove((void*)(&(piv_data[index])), (const void*)(&(piv_data[index + count])), os * sizeof(T));
deleteT(piv_data + index, count);
memmove((void*)(piv_data + index), (const void*)(piv_data + index + count), os * sizeof(T));
piv_size -= count;
return *this;
}
@@ -1680,6 +1679,7 @@ public:
//! [списка инициализации C++11](https://ru.cppreference.com/w/cpp/utility/initializer_list).
//! \~\sa \a push_back()
inline PIVector<T> & push_back(std::initializer_list<T> init_list) {
if (init_list.size() == 0) return *this;
size_t ps = piv_size;
alloc(piv_size + init_list.size());
newT(piv_data + ps, init_list.begin(), init_list.size());
@@ -1693,6 +1693,7 @@ public:
//! \~russian Перегруженая функция.
//! \~\sa \a push_back()
inline PIVector<T> & push_back(const PIVector<T> & v) {
if (v.isEmpty()) return *this;
#ifndef NDEBUG
if (&v == this) {
printf("error with PIVector<%s>::push_back\n", __PIP_TYPENAME__(T));
@@ -1815,8 +1816,7 @@ public:
//! \endcode
//! \~\sa \a push_back(), \a append(), \a prepend(), \a insert()
inline PIVector<T> & push_front(const T & e) {
insert(0, e);
return *this;
return insert(0, e);
}
//! \~english Appends the given element `e` to the begin of the array.
@@ -1826,8 +1826,7 @@ public:
//! \~russian Перегруженая функция.
//! \~\sa \a push_front()
inline PIVector<T> & push_front(T && e) {
insert(0, std::move(e));
return *this;
return insert(0, std::move(e));
}
//! \~english Appends the given array `v` to the begin of the array.
@@ -1842,8 +1841,7 @@ public:
//! \endcode
//! \~\sa \a push_front()
inline PIVector<T> & push_front(const PIVector<T> & v) {
insert(0, v);
return *this;
return insert(0, v);
}
//! \~english Appends the given elements to the begin of the array.
@@ -1857,8 +1855,7 @@ public:
//! [списка инициализации C++11](https://ru.cppreference.com/w/cpp/utility/initializer_list).
//! \~\sa \a append()
inline PIVector<T> & push_front(std::initializer_list<T> init_list) {
insert(0, init_list);
return *this;
return insert(0, init_list);
}
//! \~english Appends the given element `e` to the begin of the array.
@@ -1927,7 +1924,7 @@ public:
//! \~\sa \a pop_front(), \a take_back(), \a take_front()
inline PIVector<T> & pop_back() {
if (piv_size == 0) return *this;
deleteT(&(piv_data[piv_size-1]), 1);
deleteT(piv_data + piv_size - 1, 1);
piv_size = piv_size - 1;
return *this;
}
@@ -1994,7 +1991,8 @@ public:
//! \~\sa \a map()
template <typename ST>
inline PIVector<ST> toType() const {
PIVector<ST> ret; ret.reserve(piv_size);
PIVector<ST> ret;
ret.reserve(piv_size);
for (size_t i = 0; i < piv_size; ++i) {
ret << ST(piv_data[i]);
}
@@ -2341,7 +2339,7 @@ public:
assert(rows*cols == piv_size);
PIVector<PIVector<T>> ret;
if (isEmpty()) return ret;
ret.resize(rows);
ret.expand(rows);
if (order == ReshapeByRow) {
for (size_t r = 0; r < rows; r++) {
ret[r] = PIVector<T>(&(piv_data[r*cols]), cols);
@@ -2425,14 +2423,15 @@ public:
private:
inline void _reset() {
piv_size = piv_rsize = 0;
piv_size = 0;
piv_rsize = 0;
piv_data = nullptr;
}
inline size_t asize(size_t s) {
if (s == 0) return 0;
if (piv_rsize + piv_rsize >= s && piv_rsize < s) {
return piv_rsize + piv_rsize;
if (piv_rsize * 2 >= s && piv_rsize < s) {
return piv_rsize * 2;
}
ssize_t t = _PIContainerConstants<T>::minCountPoT(), s_ = s - 1;
while (s_ >> t) ++t;
@@ -2517,9 +2516,10 @@ private:
inline void elementDelete(T & from) {}
inline void dealloc() {
if (piv_data != nullptr) free((uchar*)piv_data);
piv_data = nullptr;
piv_size = piv_rsize = 0;
if (piv_data != nullptr) {
free((void*)piv_data);
piv_data = nullptr;
}
}
inline void expand(size_t new_size, const T & e = T()) {