mirror of
https://develop.openfoam.com/Development/openfoam.git
synced 2025-11-28 03:28:01 +00:00
ENH: added HashTable 'lookup' and 'retain' methods
- lookup(): with a default value (const access)
For example,
Map<label> something;
value = something.lookup(key, -1);
being equivalent to the following:
Map<label> something;
value = -1; // bad value
if (something.found(key))
{
value = something[key];
}
except that lookup also makes it convenient to handle const references.
Eg,
const labelList& ids = someHash.lookup(key, labelList());
- For consistency, provide a two parameter HashTable '()' operator.
The lookup() method is, however, normally preferable when
const-only access is to be ensured.
- retain(): the counterpart to erase(), it only retains entries
corresponding to the listed keys.
For example,
HashTable<someType> largeCache;
wordHashSet preserve = ...;
largeCache.retain(preserve);
being roughly equivalent to the following two-stage process,
but with reduced overhead and typing, and fewer potential mistakes.
HashTable<someType> largeCache;
wordHashSet preserve = ...;
{
wordHashSet cull(largeCache.toc()); // all keys
cull.erase(preserve); // except those to preserve
largeCache.erase(cull); //
}
The HashSet &= operator and retain() are functionally equivalent,
but retain() also works with dissimilar value types.
This commit is contained in:
@ -197,8 +197,12 @@ int main(int argc, char *argv[])
|
||||
Info<< "setD has no 11" << endl;
|
||||
}
|
||||
|
||||
Info<< "setB : " << flatOutput(setB) << endl;
|
||||
Info<< "setD : " << flatOutput(setD) << endl;
|
||||
|
||||
setD -= setB;
|
||||
Info<< "setD -= setB : " << flatOutput(setD) << endl;
|
||||
|
||||
// This should not work (yet?)
|
||||
// setD[12] = true;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user