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