зеркало из https://github.com/mozilla/gecko-dev.git
Bug 1287706 part 5 - Add RemoveElementsBy method to nsTArray and nsTObserverArray to allow filtering elements by predicate function. r=froydnj
MozReview-Commit-ID: LjUXYsIuaFL --HG-- extra : source : e0654f189aad7399dd69a920995e8b87f590da79
This commit is contained in:
Родитель
54106b71e9
Коммит
5b8c16a02b
|
@ -13,6 +13,7 @@
|
|||
#include "mozilla/Attributes.h"
|
||||
#include "mozilla/BinarySearch.h"
|
||||
#include "mozilla/fallible.h"
|
||||
#include "mozilla/Function.h"
|
||||
#include "mozilla/InitializerList.h"
|
||||
#include "mozilla/MathAlgorithms.h"
|
||||
#include "mozilla/MemoryReporting.h"
|
||||
|
@ -1589,6 +1590,13 @@ public:
|
|||
// A variation on the RemoveElementsAt method defined above.
|
||||
void Clear() { RemoveElementsAt(0, Length()); }
|
||||
|
||||
// This method removes elements based on the return value of the
|
||||
// callback function aPredicate. If the function returns true for
|
||||
// an element, the element is removed. aPredicate will be called
|
||||
// for each element in order. It is not safe to access the array
|
||||
// inside aPredicate.
|
||||
void RemoveElementsBy(mozilla::function<bool(const elem_type&)> aPredicate);
|
||||
|
||||
// This helper function combines IndexOf with RemoveElementAt to "search
|
||||
// and destroy" the first element that is equal to the given element.
|
||||
// @param aItem The item to search for.
|
||||
|
@ -1893,6 +1901,30 @@ nsTArray_Impl<E, Alloc>::RemoveElementsAt(index_type aStart, size_type aCount)
|
|||
MOZ_ALIGNOF(elem_type));
|
||||
}
|
||||
|
||||
template<typename E, class Alloc>
|
||||
void
|
||||
nsTArray_Impl<E, Alloc>::RemoveElementsBy(mozilla::function<bool(const elem_type&)> aPredicate)
|
||||
{
|
||||
if (base_type::mHdr == EmptyHdr()) {
|
||||
return;
|
||||
}
|
||||
|
||||
index_type j = 0;
|
||||
index_type len = Length();
|
||||
for (index_type i = 0; i < len; ++i) {
|
||||
if (aPredicate(Elements()[i])) {
|
||||
elem_traits::Destruct(Elements() + i);
|
||||
} else {
|
||||
if (j < i) {
|
||||
copy_type::MoveNonOverlappingRegion(Elements() + j, Elements() + i,
|
||||
1, sizeof(elem_type));
|
||||
}
|
||||
++j;
|
||||
}
|
||||
}
|
||||
base_type::mHdr->mLength = j;
|
||||
}
|
||||
|
||||
template<typename E, class Alloc>
|
||||
template<class Item, typename ActualAlloc>
|
||||
auto
|
||||
|
|
|
@ -248,6 +248,21 @@ public:
|
|||
return true;
|
||||
}
|
||||
|
||||
// See nsTArray::RemoveElementsBy.
|
||||
void RemoveElementsBy(mozilla::function<bool(const elem_type&)> aPredicate)
|
||||
{
|
||||
index_type i = 0;
|
||||
mArray.RemoveElementsBy([&](const elem_type& aItem) {
|
||||
if (aPredicate(aItem)) {
|
||||
// This element is going to be removed.
|
||||
AdjustIterators(i, -1);
|
||||
return true;
|
||||
}
|
||||
++i;
|
||||
return false;
|
||||
});
|
||||
}
|
||||
|
||||
// Removes all observers and collapses all iterators to the beginning of
|
||||
// the array. The result is that forward iterators will see all elements
|
||||
// in the array.
|
||||
|
|
Загрузка…
Ссылка в новой задаче