Bug 1370632 - Move LookupRemoveIf() to nsBaseHashtable instead so that it can be used on more hashtables types. r=froydnj

MozReview-Commit-ID: 9kQSytPWok5
This commit is contained in:
Mats Palmgren 2017-06-07 15:22:41 +02:00
Родитель 7dd909a5ce
Коммит 6467c38383
2 изменённых файлов: 35 добавлений и 35 удалений

Просмотреть файл

@ -164,6 +164,41 @@ public:
*/
void Remove(KeyType aKey) { this->RemoveEntry(aKey); }
/**
* Looks up aKey in the hashtable and if found calls the given callback
* aFunction with the value. If the callback returns true then the entry
* is removed. If aKey doesn't exist nothing happens.
* The hashtable must not be modified in the callback function.
*
* A typical usage of this API looks like this:
*
* table.LookupRemoveIf(key, [](T* aValue) {
* // ... do stuff using aValue ...
* return aValue->IsEmpty(); // or some other condition to remove it
* });
*
* This is useful for cases where you want to lookup and possibly modify
* the value and then maybe remove the entry but would like to avoid two
* hashtable lookups.
*/
template<class F>
void LookupRemoveIf(KeyType aKey, F aFunction)
{
#ifdef DEBUG
auto tableGeneration = GetGeneration();
#endif
EntryType* ent = this->GetEntry(aKey);
if (!ent) {
return;
}
bool shouldRemove = aFunction(ent->mData);
MOZ_ASSERT(tableGeneration == GetGeneration(),
"hashtable was modified by the LookupRemoveIf callback!");
if (shouldRemove) {
this->RemoveEntry(ent);
}
}
// This is an iterator that also allows entry removal. Example usage:
//
// for (auto iter = table.Iter(); !iter.Done(); iter.Next()) {

Просмотреть файл

@ -136,41 +136,6 @@ public:
* lookups.
*/
MOZ_MUST_USE EntryPtr LookupForAdd(KeyType aKey);
/**
* Looks up aKey in the hashtable and if found calls the given callback
* aFunction with the value. If the callback returns true then the entry
* is removed. If aKey doesn't exist nothing happens.
* The hashtable must not be modified in the callback function.
*
* A typical usage of this API looks like this:
*
* table.LookupRemoveIf(key, [](T* aValue) {
* // ... do stuff using aValue ...
* return aValue->IsEmpty(); // or some other condition to remove it
* });
*
* This is useful for cases where you want to lookup and possibly modify
* the value and then maybe remove the entry but would like to avoid two
* hashtable lookups.
*/
template<class F>
void LookupRemoveIf(KeyType aKey, F aFunction)
{
#ifdef DEBUG
auto tableGeneration = base_type::GetGeneration();
#endif
typename base_type::EntryType* ent = this->GetEntry(aKey);
if (!ent) {
return;
}
bool shouldRemove = aFunction(ent->mData);
MOZ_ASSERT(tableGeneration == base_type::GetGeneration(),
"hashtable was modified by the LookupRemoveIf callback!");
if (shouldRemove) {
this->RemoveEntry(ent);
}
}
};
//