Optimization removeAll and removeWhere in PIVector and PIDeque #180

Merged
andrey merged 1 commits from container_remove_optimize into master 2024-03-13 10:43:03 +03:00
2 changed files with 28 additions and 16 deletions

View File

@@ -1630,12 +1630,15 @@ 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,12 +1654,15 @@ 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,12 +1555,15 @@ 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,12 +1579,15 @@ 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;
} }