Optimization removeAll and removeWhere in PIVector and PIDeque

This commit is contained in:
2024-03-07 20:57:33 +03:00
parent 263fa18726
commit ddda58b2ad
2 changed files with 28 additions and 16 deletions

View File

@@ -1630,11 +1630,14 @@ public:
//! \endcode //! \endcode
//! \~\sa \a remove(), \a removeOne(), \a removeWhere() //! \~\sa \a remove(), \a removeOne(), \a removeWhere()
inline PIDeque<T> & removeAll(const T & e) { inline PIDeque<T> & removeAll(const T & e) {
for (size_t i = 0; i < pid_size; ++i) { ssize_t j = indexOf(e);
if (pid_data[i + pid_start] == e) { if (j != -1) {
remove(i); for (size_t i = j + 1; i < pid_size; ++i) {
--i; if (pid_data[i + pid_start] != e) {
pid_data[j++ + pid_start] = std::move(pid_data[i + pid_start]);
}
} }
remove(j, pid_size - j);
} }
return *this; return *this;
} }
@@ -1651,11 +1654,14 @@ public:
//! \endcode //! \endcode
//! \~\sa \a remove(), \a removeOne(), \a removeWhere() //! \~\sa \a remove(), \a removeOne(), \a removeWhere()
inline PIDeque<T> & removeWhere(std::function<bool(const T & e)> test) { inline PIDeque<T> & removeWhere(std::function<bool(const T & e)> test) {
for (size_t i = 0; i < pid_size; ++i) { ssize_t j = indexWhere(test);
if (test(pid_data[i + pid_start])) { if (j != -1) {
remove(i); for (size_t i = j + 1; i < pid_size; ++i) {
--i; if (!test(pid_data[i + pid_start])) {
pid_data[j++ + pid_start] = std::move(pid_data[i + pid_start]);
}
} }
remove(j, pid_size - j);
} }
return *this; return *this;
} }

View File

@@ -1555,11 +1555,14 @@ public:
//! \endcode //! \endcode
//! \~\sa \a remove(), \a removeOne(), \a removeWhere() //! \~\sa \a remove(), \a removeOne(), \a removeWhere()
inline PIVector<T> & removeAll(const T & e) { inline PIVector<T> & removeAll(const T & e) {
for (ssize_t i = 0; i < size_s(); ++i) { ssize_t j = indexOf(e);
if (piv_data[i] == e) { if (j != -1) {
remove(i); for (size_t i = j + 1; i < piv_size; ++i) {
--i; if (piv_data[i] != e) {
piv_data[j++] = std::move(piv_data[i]);
}
} }
remove(j, piv_size - j);
} }
return *this; return *this;
} }
@@ -1576,11 +1579,14 @@ public:
//! \endcode //! \endcode
//! \~\sa \a remove(), \a removeOne(), \a removeWhere() //! \~\sa \a remove(), \a removeOne(), \a removeWhere()
inline PIVector<T> & removeWhere(std::function<bool(const T & e)> test) { inline PIVector<T> & removeWhere(std::function<bool(const T & e)> test) {
for (ssize_t i = 0; i < size_s(); ++i) { ssize_t j = indexWhere(test);
if (test(piv_data[i])) { if (j != -1) {
remove(i); for (size_t i = j + 1; i < piv_size; ++i) {
--i; if (!test(piv_data[i])) {
piv_data[j++] = std::move(piv_data[i]);
}
} }
remove(j, piv_size - j);
} }
return *this; return *this;
} }