insert optimize

This commit is contained in:
Бычков Андрей
2022-08-22 17:47:45 +03:00
parent 49e553c551
commit 4635e9ba4f

View File

@@ -1418,7 +1418,7 @@ public:
}
#endif
assert(&v != this);
bool dir = pid_rsize <= 2 ? true : (index >= pid_rsize / 2 ? true : false);
bool dir = v.size() > pid_size ? true : (index >= pid_rsize / 2 ? true : false);
if (dir) {
ssize_t os = pid_size - index;
alloc_forward(pid_size + v.pid_size);
@@ -1446,7 +1446,7 @@ public:
//! [списка инициализации C++11](https://ru.cppreference.com/w/cpp/utility/initializer_list).
//! \~\sa \a append(), \a prepend(), \a remove()
inline PIDeque<T> & insert(size_t index, std::initializer_list<T> init_list) {
bool dir = pid_rsize <= 2 ? true : (index >= pid_rsize / 2 ? true : false);
bool dir = init_list.size() > pid_size ? true : (index >= pid_rsize / 2 ? true : false);
if (dir) {
ssize_t os = ssize_t(pid_size) - index;
alloc_forward(pid_size + init_list.size());
@@ -1751,6 +1751,7 @@ public:
//! \~russian Перегруженая функция.
//! \~\sa \a push_back()
inline PIDeque<T> & push_back(const PIDeque<T> & v) {
if (v.isEmpty()) return *this;
#ifndef NDEBUG
if (&v == this) {
printf("error with PIDeque<%s>::append\n", __PIP_TYPENAME__(T));
@@ -1880,8 +1881,9 @@ public:
//! \endcode
//! \~\sa \a push_back(), \a append(), \a prepend(), \a insert()
inline PIDeque<T> & push_front(const T & e) {
if (isEmpty()) push_back(e);
insert(0, e);
if (isEmpty()) return push_back(e);
alloc_backward(pid_size + 1, -1);
elementNew(pid_data + pid_start, e);
return *this;
}
@@ -1892,8 +1894,9 @@ public:
//! \~russian Перегруженая функция.
//! \~\sa \a push_front()
inline PIDeque<T> & push_front(T && e) {
if (isEmpty()) push_back(std::move(e));
insert(0, std::move(e));
if (isEmpty()) return push_back(std::move(e));
alloc_backward(pid_size + 1, -1);
elementNew(pid_data + pid_start, std::move(e));
return *this;
}
@@ -1909,9 +1912,7 @@ public:
//! \endcode
//! \~\sa \a push_front()
inline PIDeque<T> & push_front(const PIDeque<T> & v) {
if (isEmpty()) push_back(v);
insert(0, v);
return *this;
return insert(0, v);
}
//! \~english Appends the given elements to the begin of the array.
@@ -1925,9 +1926,7 @@ public:
//! [списка инициализации C++11](https://ru.cppreference.com/w/cpp/utility/initializer_list).
//! \~\sa \a append()
inline PIDeque<T> & push_front(std::initializer_list<T> init_list) {
if (isEmpty()) push_back(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.
@@ -2600,7 +2599,7 @@ private:
inline void dealloc() {
if (pid_data != nullptr) {
free((uchar*)pid_data);
free((void*)pid_data);
pid_data = nullptr;
}
}
@@ -2608,7 +2607,7 @@ private:
inline void checkMove() {
if (pid_size >= 4) {
if (pid_size < pid_rsize / 6) {
if (pid_start < (pid_size + pid_size) || ssize_t(pid_start) > (ssize_t(pid_rsize) - ssize_t(pid_size) - ssize_t(pid_size))) {
if (pid_start < (pid_size * 2) || ssize_t(pid_start) > (ssize_t(pid_rsize) - (ssize_t(pid_size) * 2))) {
size_t ns = (pid_rsize - pid_size) / 2;
if (pid_start != ns) {
memmove((void*)(pid_data + ns), (const void*)(pid_data + pid_start), pid_size * sizeof(T));
@@ -2628,7 +2627,7 @@ private:
inline void alloc_forward(size_t new_size) {
if (pid_start + new_size <= pid_rsize) {
pid_size = new_size;
checkMove();
if (pid_start > 0) checkMove();
return;
}
pid_size = new_size;