From a237b7ce6ef0c971f3c980bfc37685050d3eb31a Mon Sep 17 00:00:00 2001 From: Mark Olesen Date: Sun, 1 Nov 2009 00:54:56 +0100 Subject: [PATCH] HashTbl - iterator/const_iterator implemented in terms of an iteratorBase --- .../containers/HashTables/HashTbl/HashTbl.C | 140 ++----- .../containers/HashTables/HashTbl/HashTbl.H | 130 +++---- .../containers/HashTables/HashTbl/HashTblI.H | 351 +++++------------- .../containers/HashTables/HashTbl/HashTblIO.C | 13 +- 4 files changed, 171 insertions(+), 463 deletions(-) diff --git a/src/OpenFOAM/containers/HashTables/HashTbl/HashTbl.C b/src/OpenFOAM/containers/HashTables/HashTbl/HashTbl.C index fae8c21ded..1e3723f739 100644 --- a/src/OpenFOAM/containers/HashTables/HashTbl/HashTbl.C +++ b/src/OpenFOAM/containers/HashTables/HashTbl/HashTbl.C @@ -65,9 +65,7 @@ Foam::HashTbl::HashTbl(const label size) HashTblName(), nElmts_(0), tableSize_(canonicalSize(size)), - table_(NULL), - endIter_(), - endConstIter_() + table_(NULL) { if (tableSize_) { @@ -87,9 +85,7 @@ Foam::HashTbl::HashTbl(const HashTbl& ht) HashTblName(), nElmts_(0), tableSize_(ht.tableSize_), - table_(NULL), - endIter_(), - endConstIter_() + table_(NULL) { if (tableSize_) { @@ -116,9 +112,7 @@ Foam::HashTbl::HashTbl HashTblName(), nElmts_(0), tableSize_(0), - table_(NULL), - endIter_(), - endConstIter_() + table_(NULL) { transfer(ht()); } @@ -344,10 +338,10 @@ bool Foam::HashTbl::set template bool Foam::HashTbl::iteratorBase::erase() { - // note: elmtPtr_ is NULL for end(), so this catches that too - if (elmtPtr_) + // note: entryPtr_ is NULL for end(), so this catches that too + if (entryPtr_) { - // Search element before elmtPtr_ + // Search element before entryPtr_ hashedEntry* prev = 0; for @@ -357,7 +351,7 @@ bool Foam::HashTbl::iteratorBase::erase() ep = ep->next_ ) { - if (ep == elmtPtr_) + if (ep == entryPtr_) { break; } @@ -366,19 +360,20 @@ bool Foam::HashTbl::iteratorBase::erase() if (prev) { - // has an element before elmtPtr - reposition to there - prev->next_ = elmtPtr_->next_; - delete elmtPtr_; - elmtPtr_ = prev; + // has an element before entryPtr - reposition to there + prev->next_ = entryPtr_->next_; + delete entryPtr_; + entryPtr_ = prev; } else { - // elmtPtr was first element on SLList - hashTable_->table_[hashIndex_] = elmtPtr_->next_; - delete elmtPtr_; + // entryPtr was first element on SLList + hashTable_->table_[hashIndex_] = entryPtr_->next_; + delete entryPtr_; - // assign any non-NULL value so it doesn't look like end()/cend() - elmtPtr_ = reinterpret_cast(hashTable_); + // assign any non-NULL pointer value so it doesn't look + // like end()/cend() + entryPtr_ = reinterpret_cast(this); // Mark with special hashIndex value to signal it has been rewound. // The next increment will bring it back to the present location. @@ -411,92 +406,15 @@ bool Foam::HashTbl::iteratorBase::erase() template bool Foam::HashTbl::erase(const iterator& cit) { - // note: elmtPtr_ is NULL for end(), so this catches that too - if (cit.elmtPtr_) - { - // Search element before elmtPtr_ - hashedEntry* prev = 0; - - for (hashedEntry* ep = table_[cit.hashIndex_]; ep; ep = ep->next_) - { - if (ep == cit.elmtPtr_) - { - break; - } - prev = ep; - } - - // adjust iterator after erase - iterator& iter = const_cast(cit); - - if (prev) - { - // has an element before elmtPtr - reposition to there - prev->next_ = iter.elmtPtr_->next_; - delete iter.elmtPtr_; - iter.elmtPtr_ = prev; - } - else - { - // elmtPtr was first element on SLList - table_[iter.hashIndex_] = iter.elmtPtr_->next_; - delete iter.elmtPtr_; - - // assign an non-NULL value so it doesn't look like end()/cend() - iter.elmtPtr_ = reinterpret_cast(this); - - // 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' - iter.hashIndex_ = -iter.hashIndex_ - 1; - } - - nElmts_--; - -# ifdef FULLDEBUG - if (debug) - { - Info<< "HashTbl::erase(const iterator&) : " - << "hashedEntry " << iter.elmtPtr_->key_ << " removed.\n"; - } -# endif - - return true; - } - else - { -# ifdef FULLDEBUG - if (debug) - { - Info<< "HashTbl::erase(const iterator&) : " - << "cannot remove hashedEntry from hash table\n"; - } -# endif - - return false; - } + // adjust iterator after erase + return const_cast(cit).erase(); } template bool Foam::HashTbl::erase(const Key& key) { - // we could also simply do: erase(find(key)); - iterator fnd = find(key); - - if (fnd != end()) - { - return erase(fnd); - } - else - { - return false; - } + return erase(find(key)); } @@ -560,22 +478,22 @@ void Foam::HashTbl::resize(const label sz) return; } - HashTbl* newTable = new HashTbl(newSize); + HashTbl* tmpTable = new HashTbl(newSize); for (const_iterator iter = cbegin(); iter != cend(); ++iter) { - newTable->insert(iter.key(), *iter); + tmpTable->insert(iter.key(), *iter); } - label oldTableSize = tableSize_; - tableSize_ = newTable->tableSize_; - newTable->tableSize_ = oldTableSize; + label oldSize = tableSize_; + tableSize_ = tmpTable->tableSize_; + tmpTable->tableSize_ = oldSize; hashedEntry** oldTable = table_; - table_ = newTable->table_; - newTable->table_ = oldTable; + table_ = tmpTable->table_; + tmpTable->table_ = oldTable; - delete newTable; + delete tmpTable; } @@ -687,7 +605,7 @@ bool Foam::HashTbl::operator== const HashTbl& rhs ) const { - // sizes (ie, number of keys) must match + // sizes (number of keys) must match if (size() != rhs.size()) { return false; diff --git a/src/OpenFOAM/containers/HashTables/HashTbl/HashTbl.H b/src/OpenFOAM/containers/HashTables/HashTbl/HashTbl.H index 8095045f2d..55c9bcb045 100644 --- a/src/OpenFOAM/containers/HashTables/HashTbl/HashTbl.H +++ b/src/OpenFOAM/containers/HashTables/HashTbl/HashTbl.H @@ -88,6 +88,7 @@ class HashTbl { // Private data type for table entries + //- Structure to hold a hashed entry with SLList for collisions struct hashedEntry { //- The lookup key @@ -99,18 +100,15 @@ class HashTbl //- The data object T obj_; - //- Constructors + //- Construct from key, next pointer and object + inline hashedEntry(const Key&, hashedEntry* next, const T&); - //- Construct given key, next pointer and object - inline hashedEntry - ( - const Key&, - hashedEntry* next, - const T& newEntry - ); + private: + //- Disallow default bitwise copy construct + hashedEntry(const hashedEntry&); - //- Dissallow construction as copy - hashedEntry(const hashedEntry&); + //- Disallow default bitwise assignment + void operator=(const hashedEntry&); }; @@ -222,7 +220,7 @@ public: inline bool set(const Key&, const T& newElmt); //- Erase a hashedEntry specified by given iterator - // This invalidates the iterator until the operator++ + // This invalidates the iterator until the next operator++ bool erase(const iterator&); //- Erase a hashedEntry specified by the given key @@ -235,7 +233,7 @@ public: //- Remove entries given by the given keys from this HashTbl // Return the number of elements removed. // The parameter HashTbl needs the same type of key, but the - // type of values held and the hashing function is arbitrary. + // type of values held and the hashing function are arbitrary. template label erase(const HashTbl&); @@ -306,24 +304,23 @@ public: //- The iterator base for HashTbl // Note: data and functions are protected, to allow reuse by iterator // and prevent most external usage. + // iterator and const_iterator have the same size, allowing + // us to reinterpret_cast between them (if desired) class iteratorBase { - friend class HashTbl; - - public: - - // Protected Data + // Private Data //- Pointer to the HashTbl for which this is an iterator // This also lets us use the default bitwise copy/assignment HashTbl* hashTable_; //- Current element - hashedEntry* elmtPtr_; + hashedEntry* entryPtr_; //- Current hash index label hashIndex_; + protected: // Protected Member Functions @@ -333,22 +330,32 @@ public: inline iteratorBase(); //- Construct from hash table, moving to its 'begin' position - inline iteratorBase + inline explicit iteratorBase ( const HashTbl* curHashTbl ); //- Construct from hash table, element and hash index - inline iteratorBase + inline explicit iteratorBase ( const HashTbl* curHashTbl, const hashedEntry* elmt, const label hashIndex ); - inline void incr(); + //- Increment to the next position + inline void increment(); + + //- Erase the HashTbl element at the current position bool erase(); + + //- Return non-const access to referenced object + inline T& object(); + + //- Return const access to referenced object + inline const T& cobject() const; + public: // Member operators @@ -358,9 +365,6 @@ public: //- Return the Key corresponding to the iterator inline const Key& key() const; - //- Return referenced object - inline const T& object() const; - //- Compare hashedEntry element pointers inline bool operator==(const iteratorBase&) const; inline bool operator!=(const iteratorBase&) const; @@ -369,26 +373,21 @@ public: //- An STL-conforming iterator class iterator + : + public iteratorBase { friend class HashTbl; - friend class const_iterator; - - // Private data - - //- Pointer to the HashTbl for which this is an iterator - // This also lets us use the default bitwise copy/assignment - HashTbl* hashTable_; - - //- Current element - hashedEntry* elmtPtr_; - - //- Current hash index - label hashIndex_; // Private Member Functions + //- Construct from hash table, moving to its 'begin' position + inline explicit iterator + ( + HashTbl* curHashTbl + ); + //- Construct from hash table, element and hash index - inline iterator + inline explicit iterator ( HashTbl* curHashTbl, hashedEntry* elmt, @@ -404,19 +403,11 @@ public: // Member operators + //- Conversion to a const_iterator + inline operator const_iterator() const; + // Access - //- Return the Key corresponding to the iterator - inline const Key& key() const; - - //- Compare hashedEntry element pointers - inline bool operator==(const iterator&) const; - inline bool operator!=(const iterator&) const; - - //- Compare hashedEntry element pointers - inline bool operator==(const const_iterator&) const; - inline bool operator!=(const const_iterator&) const; - //- Return referenced hash value inline T& operator*(); inline T& operator()(); @@ -429,9 +420,6 @@ public: inline iterator operator++(int); }; - //- iterator set to the begining of the HashTbl - inline iteratorBase beginBase(); - //- iterator set to the begining of the HashTbl inline iterator begin(); @@ -443,26 +431,21 @@ public: //- An STL-conforming const_iterator class const_iterator + : + public iteratorBase { friend class HashTbl; - friend class iterator; - - // Private data - - //- Pointer to the HashTbl for which this is an iterator - // This also lets us use the default bitwise copy/assignment - const HashTbl* hashTable_; - - //- Current element - const hashedEntry* elmtPtr_; - - //- Current hash index - label hashIndex_; // Private Member Functions + //- Construct from hash table, moving to its 'begin' position + inline explicit const_iterator + ( + const HashTbl* curHashTbl + ); + //- Construct from hash table, element and hash index - inline const_iterator + inline explicit const_iterator ( const HashTbl* curHashTbl, const hashedEntry* elmt, @@ -476,25 +459,10 @@ public: //- Construct null (end iterator) inline const_iterator(); - //- Construct from the non-const iterator - inline const_iterator(const iterator&); - - // Member operators // Access - //- Return the Key corresponding to the iterator - inline const Key& key() const; - - //- Compare hashedEntry element pointers - inline bool operator==(const const_iterator&) const; - inline bool operator!=(const const_iterator&) const; - - //- Compare hashedEntry element pointers - inline bool operator==(const iterator&) const; - inline bool operator!=(const iterator&) const; - //- Return referenced hash value inline const T& operator*() const; inline const T& operator()() const; diff --git a/src/OpenFOAM/containers/HashTables/HashTbl/HashTblI.H b/src/OpenFOAM/containers/HashTables/HashTbl/HashTblI.H index 181d7be9e5..7bb4e37c5d 100644 --- a/src/OpenFOAM/containers/HashTables/HashTbl/HashTblI.H +++ b/src/OpenFOAM/containers/HashTables/HashTbl/HashTblI.H @@ -33,12 +33,12 @@ inline Foam::HashTbl::hashedEntry::hashedEntry ( const Key& key, hashedEntry* next, - const T& newEntry + const T& obj ) : key_(key), next_(next), - obj_(newEntry) + obj_(obj) {} @@ -159,13 +159,13 @@ inline T& Foam::HashTbl::operator()(const Key& key) } -// * * * * * * * * * * * * * * * * STL iterator * * * * * * * * * * * * * * // +// * * * * * * * * * * * * * * * iterator base * * * * * * * * * * * * * * * // template inline Foam::HashTbl::iteratorBase::iteratorBase() : hashTable_(0), - elmtPtr_(0), + entryPtr_(0), hashIndex_(0) {} @@ -177,7 +177,7 @@ inline Foam::HashTbl::iteratorBase::iteratorBase ) : hashTable_(const_cast*>(hashTbl)), - elmtPtr_(0), + entryPtr_(0), hashIndex_(0) { if (hashTable_->nElmts_ && hashTable_->table_) @@ -185,7 +185,7 @@ inline Foam::HashTbl::iteratorBase::iteratorBase // find first non-NULL table entry while ( - !(elmtPtr_ = hashTable_->table_[hashIndex_]) + !(entryPtr_ = hashTable_->table_[hashIndex_]) && ++hashIndex_ < hashTable_->tableSize_ ) {} @@ -193,7 +193,7 @@ inline Foam::HashTbl::iteratorBase::iteratorBase if (hashIndex_ >= hashTable_->tableSize_) { // make into an end iterator - elmtPtr_ = 0; + entryPtr_ = 0; hashIndex_ = 0; } else @@ -213,22 +213,14 @@ inline Foam::HashTbl::iteratorBase::iteratorBase ) : hashTable_(const_cast*>(hashTbl)), - elmtPtr_(const_cast(elmt)), + entryPtr_(const_cast(elmt)), hashIndex_(hashIndex) {} -template -inline typename Foam::HashTbl::iteratorBase -Foam::HashTbl::beginBase() -{ - return iteratorBase(this); -} - - template inline void -Foam::HashTbl::iteratorBase::incr() +Foam::HashTbl::iteratorBase::increment() { // A negative index is a special value from erase if (hashIndex_ < 0) @@ -237,10 +229,10 @@ Foam::HashTbl::iteratorBase::incr() // but we wish to continue at 'X-1' hashIndex_ = -hashIndex_ - 2; } - else if (elmtPtr_ && elmtPtr_->next_) + else if (entryPtr_ && entryPtr_->next_) { - // Do we have additional elements on the SLList? - elmtPtr_ = elmtPtr_->next_; + // Move to next element on the SLList + entryPtr_ = entryPtr_->next_; return; } @@ -248,14 +240,14 @@ Foam::HashTbl::iteratorBase::incr() while ( ++hashIndex_ < hashTable_->tableSize_ - && !(elmtPtr_ = hashTable_->table_[hashIndex_]) + && !(entryPtr_ = hashTable_->table_[hashIndex_]) ) {} if (hashIndex_ >= hashTable_->tableSize_) { // make into an end iterator - elmtPtr_ = 0; + entryPtr_ = 0; hashIndex_ = 0; } } @@ -265,15 +257,23 @@ template inline const Key& Foam::HashTbl::iteratorBase::key() const { - return elmtPtr_->key_; + return entryPtr_->key_; +} + + +template +inline T& +Foam::HashTbl::iteratorBase::object() +{ + return entryPtr_->obj_; } template inline const T& -Foam::HashTbl::iteratorBase::object() const +Foam::HashTbl::iteratorBase::cobject() const { - return elmtPtr_->obj_; + return entryPtr_->obj_; } @@ -283,7 +283,7 @@ inline bool Foam::HashTbl::iteratorBase::operator== const iteratorBase& iter ) const { - return elmtPtr_ == iter.elmtPtr_; + return entryPtr_ == iter.entryPtr_; } @@ -293,10 +293,29 @@ inline bool Foam::HashTbl::iteratorBase::operator!= const iteratorBase& iter ) const { - return elmtPtr_ != iter.elmtPtr_; + return entryPtr_ != iter.entryPtr_; } +// * * * * * * * * * * * * * * * * STL iterator * * * * * * * * * * * * * * // + +template +inline Foam::HashTbl::iterator::iterator() +: + iteratorBase() +{} + + +template +inline Foam::HashTbl::iterator::iterator +( + HashTbl* hashTbl +) +: + iteratorBase(hashTbl) +{} + + template inline Foam::HashTbl::iterator::iterator ( @@ -305,58 +324,18 @@ inline Foam::HashTbl::iterator::iterator const label hashIndex ) : - hashTable_(hashTbl), - elmtPtr_(elmt), - hashIndex_(hashIndex) + iteratorBase(hashTbl, elmt, hashIndex) {} template -inline Foam::HashTbl::iterator::iterator() -: - hashTable_(0), - elmtPtr_(0), - hashIndex_(0) -{} - - -template -inline bool Foam::HashTbl::iterator::operator== -( - const iterator& iter -) const +inline Foam::HashTbl::iterator::operator +typename Foam::HashTbl::const_iterator() const { - return elmtPtr_ == iter.elmtPtr_; -} - - -template -inline bool Foam::HashTbl::iterator::operator!= -( - const iterator& iter -) const -{ - return elmtPtr_ != iter.elmtPtr_; -} - - -template -inline bool Foam::HashTbl::iterator::operator== -( - const const_iterator& iter -) const -{ - return elmtPtr_ == iter.elmtPtr_; -} - - -template -inline bool Foam::HashTbl::iterator::operator!= -( - const const_iterator& iter -) const -{ - return elmtPtr_ != iter.elmtPtr_; + return *reinterpret_cast + < + const typename Foam::HashTbl::const_iterator* + >(this); } @@ -364,7 +343,7 @@ template inline T& Foam::HashTbl::iterator::operator*() { - return elmtPtr_->obj_; + return this->object(); } @@ -372,7 +351,7 @@ template inline T& Foam::HashTbl::iterator::operator()() { - return elmtPtr_->obj_; + return this->object(); } @@ -380,7 +359,7 @@ template inline const T& Foam::HashTbl::iterator::operator*() const { - return elmtPtr_->obj_; + return this->cobject(); } @@ -388,7 +367,7 @@ template inline const T& Foam::HashTbl::iterator::operator()() const { - return elmtPtr_->obj_; + return this->cobject(); } @@ -397,56 +376,18 @@ inline typename Foam::HashTbl::iterator& Foam::HashTbl::iterator::operator++() { - // 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; - } - else if (elmtPtr_ && elmtPtr_->next_) - { - // Do we have additional elements on the SLList? - elmtPtr_ = elmtPtr_->next_; - return *this; - } - - // Step to the next table entry - while - ( - ++hashIndex_ < hashTable_->tableSize_ - && !(elmtPtr_ = hashTable_->table_[hashIndex_]) - ) - {} - - if (hashIndex_ >= hashTable_->tableSize_) - { - // make an end iterator - elmtPtr_ = 0; - hashIndex_ = 0; - } + this->increment(); return *this; } template inline typename Foam::HashTbl::iterator -Foam::HashTbl::iterator::operator++ -( - int -) +Foam::HashTbl::iterator::operator++(int) { - iterator tmp = *this; - ++*this; - return tmp; -} - - -template -inline -const Key& Foam::HashTbl::iterator::key() const -{ - return elmtPtr_->key_; + iterator old = *this; + this->increment(); + return old; } @@ -454,33 +395,7 @@ template inline typename Foam::HashTbl::iterator Foam::HashTbl::begin() { - label hashIdx = 0; - - if (nElmts_) - { - while (table_ && !table_[hashIdx] && ++hashIdx < tableSize_) - {} - } - else - { - hashIdx = tableSize_; - } - - if (hashIdx >= tableSize_) - { -# ifdef FULLDEBUG - if (debug) - { - Info<< "HashTbl is empty\n"; - } -# endif - - return HashTbl::endIter_; - } - else - { - return iterator(this, table_[hashIdx], hashIdx); - } + return iterator(this); } @@ -497,9 +412,17 @@ Foam::HashTbl::end() template inline Foam::HashTbl::const_iterator::const_iterator() : - hashTable_(0), - elmtPtr_(0), - hashIndex_(0) + iteratorBase() +{} + + +template +inline Foam::HashTbl::const_iterator::const_iterator +( + const HashTbl* hashTbl +) +: + iteratorBase(hashTbl) {} @@ -511,76 +434,23 @@ inline Foam::HashTbl::const_iterator::const_iterator const label hashIndex ) : - hashTable_(hashTbl), - elmtPtr_(elmt), - hashIndex_(hashIndex) + iteratorBase(hashTbl, elmt, hashIndex) {} -template -inline Foam::HashTbl::const_iterator::const_iterator -( - const iterator& iter -) -: - hashTable_(iter.hashTable_), - elmtPtr_(iter.elmtPtr_), - hashIndex_(iter.hashIndex_) -{} - - -template -inline bool Foam::HashTbl::const_iterator::operator== -( - const const_iterator& iter -) const -{ - return elmtPtr_ == iter.elmtPtr_; -} - - -template -inline bool Foam::HashTbl::const_iterator::operator!= -( - const const_iterator& iter -) const -{ - return elmtPtr_ != iter.elmtPtr_; -} - - -template -inline bool Foam::HashTbl::const_iterator::operator== -( - const iterator& iter -) const -{ - return elmtPtr_ == iter.elmtPtr_; -} - - -template -inline bool Foam::HashTbl::const_iterator::operator!= -( - const iterator& iter -) const -{ - return elmtPtr_ != iter.elmtPtr_; -} - - template inline const T& Foam::HashTbl::const_iterator::operator*() const { - return elmtPtr_->obj_; + return this->cobject(); } + template inline const T& Foam::HashTbl::const_iterator::operator()() const { - return elmtPtr_->obj_; + return this->cobject(); } @@ -589,43 +459,18 @@ inline typename Foam::HashTbl::const_iterator& Foam::HashTbl::const_iterator::operator++() { - if - ( - !(elmtPtr_ = elmtPtr_->next_) - && ++hashIndex_ < hashTable_->tableSize_ - && !(elmtPtr_ = hashTable_->table_[hashIndex_]) - ) - { - while - ( - ++hashIndex_ < hashTable_->tableSize_ - && !(elmtPtr_ = hashTable_->table_[hashIndex_]) - ) - {} - } - + this->increment(); return *this; } template inline typename Foam::HashTbl::const_iterator -Foam::HashTbl::const_iterator::operator++ -( - int -) +Foam::HashTbl::const_iterator::operator++(int) { - const_iterator tmp = *this; - ++*this; - return tmp; -} - - -template -inline -const Key& Foam::HashTbl::const_iterator::key() const -{ - return elmtPtr_->key_; + const_iterator old = *this; + this->increment(); + return old; } @@ -633,33 +478,7 @@ template inline typename Foam::HashTbl::const_iterator Foam::HashTbl::cbegin() const { - label hashIdx = 0; - - if (nElmts_) - { - while (table_ && !table_[hashIdx] && ++hashIdx < tableSize_) - {} - } - else - { - hashIdx = tableSize_; - } - - if (hashIdx >= tableSize_) - { -# ifdef FULLDEBUG - if (debug) - { - Info<< "HashTbl is empty\n"; - } -# endif - - return HashTbl::endConstIter_; - } - else - { - return const_iterator(this, table_[hashIdx], hashIdx); - } + return const_iterator(this); } @@ -683,7 +502,7 @@ template inline const typename Foam::HashTbl::const_iterator& Foam::HashTbl::end() const { - return HashTbl::endConstIter_; + return this->cend(); } diff --git a/src/OpenFOAM/containers/HashTables/HashTbl/HashTblIO.C b/src/OpenFOAM/containers/HashTables/HashTbl/HashTblIO.C index b3ebbd8827..cff8d1e660 100644 --- a/src/OpenFOAM/containers/HashTables/HashTbl/HashTblIO.C +++ b/src/OpenFOAM/containers/HashTables/HashTbl/HashTblIO.C @@ -36,13 +36,16 @@ Foam::HashTbl::HashTbl(Istream& is, const label size) HashTblName(), nElmts_(0), tableSize_(canonicalSize(size)), - table_(new hashedEntry*[tableSize_]), - endIter_(), - endConstIter_() + table_(NULL) { - for (label hashIdx = 0; hashIdx < tableSize_; hashIdx++) + if (tableSize_) { - table_[hashIdx] = 0; + table_ = new hashedEntry*[tableSize_]; + + for (label hashIdx = 0; hashIdx < tableSize_; hashIdx++) + { + table_[hashIdx] = 0; + } } operator>>(is, *this);