Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
openfoam
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Model registry
Analyze
Contributor analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Development
openfoam
Wiki
Coding
Patterns
HashTable
Changes
Page history
New page
Templates
Clone repository
1912 upgrade guides, start Coding-patterns
authored
5 years ago
by
Mark OLESEN
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
coding/patterns/HashTable.md
+72
-0
72 additions, 0 deletions
coding/patterns/HashTable.md
with
72 additions
and
0 deletions
coding/patterns/HashTable.md
0 → 100644
View page @
b79ba0c4
<!-- --- title: OpenFOAM C++ Coding Patterns (HashTable) -->
[
Back to _coding patterns_
](
/coding/patterns/patterns
)
***We are happy to incorporate content from volunteers!!***
## Coding Patterns
### HashSet
Use range-for to dereference the HashSet iterators directly:
```
labelHashSet ids ...;
for (const label id : ids)
{
os << id << nl;
}
```
**Anti-pattern**
: using iterators:
```
labelHashSet ids ...;
forAllConstIters(ids, iter)
{
os <<iter.key() << nl;
}
// Even worse - using old macros!
forAllConstIter(labelHashSet, ids, iter)
{
os <<iter.key() << nl;
}
```
### HashTable, Map
Use
`lookup()`
to provide default values (const-access) and the
`operator()`
to create new zero-initialized entries if required:
```
Map<label> counter;
...
os << "hits: " << counter.lookup(i, 0) << nl;
++counter(j);
```
**Anti-pattern**
: the manual equivalents:
```
Map<label> counter;
...
os << "hits: " << (counter.found(i) ? *(counter.cfind(i)) : 0) << nl;
Map<label>::iterator iter = counter.find(j);
if (!iter.found())
{
counter.insert(j, 0);
}
else
{
++(*iter);
}
```
----
Copyright (C) 2019 OpenCFD Ltd.
This diff is collapsed.
Click to expand it.