mirror of
https://develop.openfoam.com/Development/openfoam.git
synced 2025-11-28 03:28:01 +00:00
STYLE: HashTable documentation
- explicitly mention the value-initialized status for the operator().
This means that the following code will properly use an initialized
zero.
HashTable<label> regionCount;
if (...)
regionCount("region1")++;
... and also this;
if (regionCount("something") > 0)
{
...
}
Note that the OpenFOAM HashTable uses operator[] to provide read and
write access to *existing* entries and will provoke a FatalError if
the entry does not exist.
The operator() provides write access to *existing* entries or will
create the new entry as required.
The STL hashes use operator[] for this purpose.
This commit is contained in:
@ -28,6 +28,7 @@ Description
|
|||||||
#include "hashedWordList.H"
|
#include "hashedWordList.H"
|
||||||
#include "HashSet.H"
|
#include "HashSet.H"
|
||||||
#include "Map.H"
|
#include "Map.H"
|
||||||
|
#include "labelPairHashes.H"
|
||||||
|
|
||||||
using namespace Foam;
|
using namespace Foam;
|
||||||
|
|
||||||
@ -122,7 +123,6 @@ int main(int argc, char *argv[])
|
|||||||
<< (wordHashSet(setA) | wordHashSet(tableA) | wordHashSet(tableB))
|
<< (wordHashSet(setA) | wordHashSet(tableA) | wordHashSet(tableB))
|
||||||
<< nl;
|
<< nl;
|
||||||
|
|
||||||
|
|
||||||
labelHashSet setB
|
labelHashSet setB
|
||||||
{
|
{
|
||||||
1, 11, 42
|
1, 11, 42
|
||||||
@ -130,6 +130,13 @@ int main(int argc, char *argv[])
|
|||||||
|
|
||||||
Info<< "setB : " << setB << endl;
|
Info<< "setB : " << setB << endl;
|
||||||
|
|
||||||
|
labelPair pair(12, 15);
|
||||||
|
setB.set(pair);
|
||||||
|
|
||||||
|
Info<< "setB : " << setB << endl;
|
||||||
|
setB.unset(pair);
|
||||||
|
|
||||||
|
|
||||||
labelHashSet setC(1);
|
labelHashSet setC(1);
|
||||||
setC.insert(2008);
|
setC.insert(2008);
|
||||||
setC.insert(1984);
|
setC.insert(1984);
|
||||||
|
|||||||
@ -51,6 +51,9 @@ void printTraits(const pTraits<T>& p)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#pragma GCC diagnostic warning "-Wmaybe-uninitialized"
|
||||||
|
#pragma GCC diagnostic warning "-Wuninitialized"
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
printTraits<bool>();
|
printTraits<bool>();
|
||||||
@ -71,6 +74,12 @@ int main()
|
|||||||
|
|
||||||
printTraits(pTraits<scalar>(3.14159));
|
printTraits(pTraits<scalar>(3.14159));
|
||||||
|
|
||||||
|
label abc;
|
||||||
|
Info<< "unintialized primitive:"<< abc << endl;
|
||||||
|
|
||||||
|
label def = label();
|
||||||
|
Info<< "intialized primitive:"<< def << endl;
|
||||||
|
|
||||||
Info<< "End\n" << endl;
|
Info<< "End\n" << endl;
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|||||||
@ -86,7 +86,7 @@ public:
|
|||||||
HashSet(const UList<Key>& lst);
|
HashSet(const UList<Key>& lst);
|
||||||
|
|
||||||
//- Construct from an initializer list of Key
|
//- Construct from an initializer list of Key
|
||||||
HashSet(std::initializer_list<Key>);
|
HashSet(std::initializer_list<Key> lst);
|
||||||
|
|
||||||
//- Construct as copy
|
//- Construct as copy
|
||||||
HashSet(const HashSet<Key, Hash>& hs)
|
HashSet(const HashSet<Key, Hash>& hs)
|
||||||
@ -122,7 +122,7 @@ public:
|
|||||||
return HashTable<nil, Key, Hash>::insert(key, nil());
|
return HashTable<nil, Key, Hash>::insert(key, nil());
|
||||||
}
|
}
|
||||||
|
|
||||||
//- Insert keys from a UList of Key
|
//- Insert keys from the list of Key
|
||||||
// Return the number of new elements inserted
|
// Return the number of new elements inserted
|
||||||
label insert(const UList<Key>& lst);
|
label insert(const UList<Key>& lst);
|
||||||
|
|
||||||
|
|||||||
@ -431,7 +431,7 @@ Foam::label Foam::HashTable<T, Key, Hash>::erase
|
|||||||
template<class T, class Key, class Hash>
|
template<class T, class Key, class Hash>
|
||||||
void Foam::HashTable<T, Key, Hash>::resize(const label sz)
|
void Foam::HashTable<T, Key, Hash>::resize(const label sz)
|
||||||
{
|
{
|
||||||
label newSize = HashTableCore::canonicalSize(sz);
|
const label newSize = HashTableCore::canonicalSize(sz);
|
||||||
|
|
||||||
if (newSize == tableSize_)
|
if (newSize == tableSize_)
|
||||||
{
|
{
|
||||||
@ -452,7 +452,7 @@ void Foam::HashTable<T, Key, Hash>::resize(const label sz)
|
|||||||
tmpTable->insert(iter.key(), *iter);
|
tmpTable->insert(iter.key(), *iter);
|
||||||
}
|
}
|
||||||
|
|
||||||
label oldSize = tableSize_;
|
const label oldSize = tableSize_;
|
||||||
tableSize_ = tmpTable->tableSize_;
|
tableSize_ = tmpTable->tableSize_;
|
||||||
tmpTable->tableSize_ = oldSize;
|
tmpTable->tableSize_ = oldSize;
|
||||||
|
|
||||||
|
|||||||
@ -126,7 +126,7 @@ class HashTable
|
|||||||
{
|
{
|
||||||
// Private data type for table entries
|
// Private data type for table entries
|
||||||
|
|
||||||
//- Structure to hold a hashed entry with SLList for collisions
|
//- Structure to hold a hashed entry, with a SLList for collisions
|
||||||
struct hashedEntry
|
struct hashedEntry
|
||||||
{
|
{
|
||||||
//- The lookup key
|
//- The lookup key
|
||||||
@ -139,7 +139,7 @@ class HashTable
|
|||||||
T obj_;
|
T obj_;
|
||||||
|
|
||||||
//- Construct from key, next pointer and object
|
//- Construct from key, next pointer and object
|
||||||
inline hashedEntry(const Key&, hashedEntry* next, const T&);
|
inline hashedEntry(const Key& key, hashedEntry* next, const T& obj);
|
||||||
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
@ -169,7 +169,8 @@ class HashTable
|
|||||||
// No checks for zero-sized tables.
|
// No checks for zero-sized tables.
|
||||||
inline label hashKeyIndex(const Key& key) const;
|
inline label hashKeyIndex(const Key& key) const;
|
||||||
|
|
||||||
//- Assign a new hashedEntry to a possibly already existing key
|
//- Assign a new hashedEntry to a possibly already existing key.
|
||||||
|
// Return true if the new entry was set.
|
||||||
bool set(const Key& key, const T& newEntry, const bool protect);
|
bool set(const Key& key, const T& newEntry, const bool protect);
|
||||||
|
|
||||||
|
|
||||||
@ -201,7 +202,7 @@ public:
|
|||||||
HashTable(const label size = 128);
|
HashTable(const label size = 128);
|
||||||
|
|
||||||
//- Construct from Istream
|
//- Construct from Istream
|
||||||
HashTable(Istream&, const label size = 128);
|
HashTable(Istream& is, const label size = 128);
|
||||||
|
|
||||||
//- Construct as copy
|
//- Construct as copy
|
||||||
HashTable(const HashTable<T, Key, Hash>& ht);
|
HashTable(const HashTable<T, Key, Hash>& ht);
|
||||||
@ -254,9 +255,12 @@ public:
|
|||||||
// Edit
|
// Edit
|
||||||
|
|
||||||
//- Insert a new hashedEntry
|
//- Insert a new hashedEntry
|
||||||
|
// Return true if the entry inserted, which means that it did
|
||||||
|
// not previously exist in the table.
|
||||||
inline bool insert(const Key& key, const T& newEntry);
|
inline bool insert(const Key& key, const T& newEntry);
|
||||||
|
|
||||||
//- Assign a new hashedEntry, overwriting existing entries
|
//- Assign a new hashedEntry, overwriting existing entries.
|
||||||
|
// Returns true.
|
||||||
inline bool set(const Key& key, const T& newEntry);
|
inline bool set(const Key& key, const T& newEntry);
|
||||||
|
|
||||||
//- Erase a hashedEntry specified by given iterator
|
//- Erase a hashedEntry specified by given iterator
|
||||||
@ -306,13 +310,15 @@ public:
|
|||||||
//- Find and return a hashedEntry
|
//- Find and return a hashedEntry
|
||||||
inline const T& operator[](const Key& key) const;
|
inline const T& operator[](const Key& key) const;
|
||||||
|
|
||||||
//- Find and return a hashedEntry, create it null if not present
|
//- Return existing entry or create a new entry.
|
||||||
|
// A newly created entry is created as a nameless T() and is thus
|
||||||
|
// value-initialized. For primitives, this will be zero.
|
||||||
inline T& operator()(const Key& key);
|
inline T& operator()(const Key& key);
|
||||||
|
|
||||||
//- Assignment
|
//- Assignment
|
||||||
void operator=(const HashTable<T, Key, Hash>& rhs);
|
void operator=(const HashTable<T, Key, Hash>& rhs);
|
||||||
|
|
||||||
//- Assignment to an initializer list
|
//- Assignment from an initializer list
|
||||||
void operator=(std::initializer_list<Tuple2<Key, T>> lst);
|
void operator=(std::initializer_list<Tuple2<Key, T>> lst);
|
||||||
|
|
||||||
//- Equality. Hash tables are equal if the keys and values are equal.
|
//- Equality. Hash tables are equal if the keys and values are equal.
|
||||||
|
|||||||
Reference in New Issue
Block a user