diff --git a/src/OpenFOAM/Make/files b/src/OpenFOAM/Make/files index 432fb6a300..fe85637566 100644 --- a/src/OpenFOAM/Make/files +++ b/src/OpenFOAM/Make/files @@ -63,7 +63,7 @@ primitives/random/Random.C containers/HashTables/HashTbl/HashTblCore.C containers/HashTables/HashTable/HashTableName.C -containers/HashTables/StaticHashTable/StaticHashTableName.C +containers/HashTables/StaticHashTable/StaticHashTableCore.C containers/Lists/SortableList/ParSortableListName.C containers/Lists/PackedList/PackedListName.C containers/Lists/ListOps/ListOps.C diff --git a/src/OpenFOAM/containers/HashTables/HashTbl/HashTbl.C b/src/OpenFOAM/containers/HashTables/HashTbl/HashTbl.C index 4de4263fa6..eb43d430a4 100644 --- a/src/OpenFOAM/containers/HashTables/HashTbl/HashTbl.C +++ b/src/OpenFOAM/containers/HashTables/HashTbl/HashTbl.C @@ -227,25 +227,25 @@ Foam::HashTbl::find template Foam::List Foam::HashTbl::toc() const { - List tofc(nElmts_); - label i = 0; + List keys(nElmts_); + label keyI = 0; for (const_iterator iter = cbegin(); iter != cend(); ++iter) { - tofc[i++] = iter.key(); + keys[keyI++] = iter.key(); } - return tofc; + return keys; } template Foam::List Foam::HashTbl::sortedToc() const { - List sortedList = this->toc(); - sort(sortedList); + List sortedLst = this->toc(); + sort(sortedLst); - return sortedList; + return sortedLst; } @@ -283,7 +283,7 @@ bool Foam::HashTbl::set table_[hashIdx] = new hashedEntry(key, table_[hashIdx], newEntry); nElmts_++; - if (double(nElmts_)/tableSize_ > 0.8) + if (double(nElmts_)/tableSize_ > 0.8 && tableSize_ < maxTableSize) { # ifdef FULLDEBUG if (debug) @@ -377,11 +377,11 @@ bool Foam::HashTbl::iteratorBase::erase() // Mark with special hashIndex value to signal it has been rewound. // The next increment will bring it back to the present location. // - // For the current position 'X', mark it as '-(X+1)', which is - // written as '-X-1' to avoid overflow. - // The extra '-1' is needed to avoid ambiguity for position '0'. - // To retrieve the previous position 'X-1' we would later - // use '-(-X-1) - 2' + // From the current position 'curPos', we wish to continue at + // prevPos='curPos-1', which we mark as markPos='-curPos-1'. + // The negative lets us notice it is special, the extra '-1' + // is needed to avoid ambiguity for position '0'. + // To retrieve prevPos, we would later use '-(markPos+1) - 1' hashIndex_ = -hashIndex_ - 1; } diff --git a/src/OpenFOAM/containers/HashTables/HashTbl/HashTbl.H b/src/OpenFOAM/containers/HashTables/HashTbl/HashTbl.H index a4bc0db883..8ba924f00b 100644 --- a/src/OpenFOAM/containers/HashTables/HashTbl/HashTbl.H +++ b/src/OpenFOAM/containers/HashTables/HashTbl/HashTbl.H @@ -80,6 +80,9 @@ struct HashTblCore //- Return a canonical (power-of-two) size static label canonicalSize(const label); + //- Maximum allowable table size + static const label maxTableSize; + //- Construct null HashTblCore() {} @@ -288,7 +291,7 @@ public: void transfer(HashTbl&); //- Transfer contents to the Xfer container - inline Xfer > xfer(); + inline Xfer< HashTbl > xfer(); // Member Operators diff --git a/src/OpenFOAM/containers/HashTables/HashTbl/HashTblCore.C b/src/OpenFOAM/containers/HashTables/HashTbl/HashTblCore.C index 940684c8b8..3f1d4d3cc6 100644 --- a/src/OpenFOAM/containers/HashTables/HashTbl/HashTblCore.C +++ b/src/OpenFOAM/containers/HashTables/HashTbl/HashTblCore.C @@ -30,4 +30,12 @@ License defineTypeNameAndDebug(Foam::HashTblCore, 0); +const Foam::label Foam::HashTblCore::maxTableSize +( + Foam::HashTblCore::canonicalSize + ( + Foam::labelMax/2 + ) +); + // ************************************************************************* // diff --git a/src/OpenFOAM/containers/HashTables/HashTbl/HashTblI.H b/src/OpenFOAM/containers/HashTables/HashTbl/HashTblI.H index 99d5f56392..7da1ad81cc 100644 --- a/src/OpenFOAM/containers/HashTables/HashTbl/HashTblI.H +++ b/src/OpenFOAM/containers/HashTables/HashTbl/HashTblI.H @@ -221,9 +221,9 @@ Foam::HashTbl::iteratorBase::increment() // A negative index is a special value from erase if (hashIndex_ < 0) { - // old position 'X' was marked as '-(X+1)' - // but we wish to continue at 'X-1' - hashIndex_ = -hashIndex_ - 2; + // the markPos='-curPos-1', but we wish to continue at 'curPos-1' + // thus use '-(markPos+1) -1' + hashIndex_ = -(hashIndex_+1) - 1; } else if (entryPtr_) { diff --git a/src/OpenFOAM/containers/HashTables/StaticHashTable/StaticHashTable.C b/src/OpenFOAM/containers/HashTables/StaticHashTable/StaticHashTable.C index 8d5362b89d..1d0ab2c786 100644 --- a/src/OpenFOAM/containers/HashTables/StaticHashTable/StaticHashTable.C +++ b/src/OpenFOAM/containers/HashTables/StaticHashTable/StaticHashTable.C @@ -33,8 +33,7 @@ License // * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * * // -template -Foam::label Foam::StaticHashTable::canonicalSize(const label size) +Foam::label Foam::StaticHashTableCore::canonicalSize(const label size) { if (size < 1) { @@ -64,8 +63,8 @@ Foam::label Foam::StaticHashTable::canonicalSize(const label size) template Foam::StaticHashTable::StaticHashTable(const label size) : - StaticHashTableName(), - keys_(canonicalSize(size)), + StaticHashTableCore(), + keys_(StaticHashTableCore::canonicalSize(size)), objects_(keys_.size()), nElmts_(0), endIter_(*this, keys_.size(), 0), @@ -89,7 +88,7 @@ Foam::StaticHashTable::StaticHashTable const StaticHashTable& ht ) : - StaticHashTableName(), + StaticHashTableCore(), keys_(ht.keys_), objects_(ht.objects_), nElmts_(ht.nElmts_), @@ -105,7 +104,7 @@ Foam::StaticHashTable::StaticHashTable const Xfer< StaticHashTable >& ht ) : - StaticHashTableName(), + StaticHashTableCore(), keys_(0), objects_(0), nElmts_(0), @@ -224,15 +223,15 @@ Foam::StaticHashTable::find template Foam::List Foam::StaticHashTable::toc() const { - List tofc(nElmts_); - label i = 0; + List keys(nElmts_); + label keyI = 0; for (const_iterator iter = cbegin(); iter != cend(); ++iter) { - tofc[i++] = iter.key(); + keys[keyI++] = iter.key(); } - return tofc; + return keys; } @@ -319,24 +318,9 @@ bool Foam::StaticHashTable::erase(const iterator& cit) if (it.elemIndex_ < 0) { // No previous element in the local list - - // Search back for previous non-zero table entry - while (--it.hashIndex_ >= 0 && !objects_[it.hashIndex_].size()) - {} - - if (it.hashIndex_ >= 0) - { - // The last element in the local list - it.elemIndex_ = objects_[it.hashIndex_].size() - 1; - } - else - { - // No previous found. Mark with special value which is - // - not end() - // - handled by operator++ - it.hashIndex_ = -1; - it.elemIndex_ = 0; - } + // Mark with as special value (see notes in HashTable) + it.hashIndex_ = -it.hashIndex_ - 1; + it.elemIndex_ = 0; } nElmts_--; @@ -407,8 +391,8 @@ Foam::label Foam::StaticHashTable::erase template void Foam::StaticHashTable::resize(const label sz) { - label newSize = canonicalSize(sz); - + label newSize = StaticHashTableCore::canonicalSize(sz); + if (newSize == keys_.size()) { # ifdef FULLDEBUG @@ -543,18 +527,8 @@ bool Foam::StaticHashTable::operator== const StaticHashTable& rhs ) const { - // Are all my elements in rhs? - for (const_iterator iter = cbegin(); iter != cend(); ++iter) - { - const_iterator fnd = rhs.find(iter.key()); + // sizes (number of keys) must match - if (fnd == rhs.cend() || fnd() != iter()) - { - return false; - } - } - - // Are all rhs elements in me? for (const_iterator iter = rhs.cbegin(); iter != rhs.cend(); ++iter) { const_iterator fnd = find(iter.key()); @@ -564,6 +538,7 @@ bool Foam::StaticHashTable::operator== return false; } } + return true; } diff --git a/src/OpenFOAM/containers/HashTables/StaticHashTable/StaticHashTable.H b/src/OpenFOAM/containers/HashTables/StaticHashTable/StaticHashTable.H index 25b5933e9d..09edf50c46 100644 --- a/src/OpenFOAM/containers/HashTables/StaticHashTable/StaticHashTable.H +++ b/src/OpenFOAM/containers/HashTables/StaticHashTable/StaticHashTable.H @@ -76,7 +76,33 @@ template Ostream& operator<< Class StaticHashTableName Declaration \*---------------------------------------------------------------------------*/ -TemplateName(StaticHashTable); +/*---------------------------------------------------------------------------*\ + Class StaticHashTableCore Declaration +\*---------------------------------------------------------------------------*/ + +//- Template-invariant bits for StaticHashTable +struct StaticHashTableCore +{ + //- Return a canonical (power-of-two) size + static label canonicalSize(const label); + + //- Construct null + StaticHashTableCore() + {} + + //- Define template name and debug + ClassName("StaticHashTable"); + + //- A zero-sized end iterator + struct iteratorEnd + { + //- Construct null + iteratorEnd() + {} + }; + +}; + /*---------------------------------------------------------------------------*\ @@ -86,7 +112,7 @@ TemplateName(StaticHashTable); template class StaticHashTable : - public StaticHashTableName + public StaticHashTableCore { // Private data type for table entries diff --git a/src/OpenFOAM/containers/HashTables/StaticHashTable/StaticHashTableName.C b/src/OpenFOAM/containers/HashTables/StaticHashTable/StaticHashTableCore.C similarity index 96% rename from src/OpenFOAM/containers/HashTables/StaticHashTable/StaticHashTableName.C rename to src/OpenFOAM/containers/HashTables/StaticHashTable/StaticHashTableCore.C index cb7df68de3..2ee3b4221e 100644 --- a/src/OpenFOAM/containers/HashTables/StaticHashTable/StaticHashTableName.C +++ b/src/OpenFOAM/containers/HashTables/StaticHashTable/StaticHashTableCore.C @@ -28,6 +28,6 @@ License // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * // -defineTypeNameAndDebug(Foam::StaticHashTableName, 0); +defineTypeNameAndDebug(Foam::StaticHashTableCore, 0); // ************************************************************************* // diff --git a/src/OpenFOAM/containers/HashTables/StaticHashTable/StaticHashTableI.H b/src/OpenFOAM/containers/HashTables/StaticHashTable/StaticHashTableI.H index abd988a4df..bd031708e9 100644 --- a/src/OpenFOAM/containers/HashTables/StaticHashTable/StaticHashTableI.H +++ b/src/OpenFOAM/containers/HashTables/StaticHashTable/StaticHashTableI.H @@ -267,8 +267,13 @@ Foam::StaticHashTable::Iterator TableRef >::operator++() { - // Check for special value from erase. (sets hashIndex to -1) - if (hashIndex_ >= 0) + // A negative index is a special value from erase + // (see notes in HashTable) + if (hashIndex_ < 0) + { + hashIndex_ = -(hashIndex_+1) - 1; + } + else { // Try the next element on the local list elemIndex_++; diff --git a/src/OpenFOAM/containers/HashTables/StaticHashTable/StaticHashTableIO.C b/src/OpenFOAM/containers/HashTables/StaticHashTable/StaticHashTableIO.C index 3280ad7389..5b07784266 100644 --- a/src/OpenFOAM/containers/HashTables/StaticHashTable/StaticHashTableIO.C +++ b/src/OpenFOAM/containers/HashTables/StaticHashTable/StaticHashTableIO.C @@ -37,9 +37,9 @@ Foam::StaticHashTable::StaticHashTable const label size ) : - StaticHashTableName(), - keys_(size), - objects_(size), + StaticHashTableCore(), + keys_(StaticHashTableCore::canonicalSize(size)), + objects_(StaticHashTableCore::canonicalSize(size)), nElmts_(0), endIter_(*this, keys_.size(), 0), endConstIter_(*this, keys_.size(), 0)