diff --git a/applications/test/DLList/Test-DLList.C b/applications/test/DLList/Test-DLList.C index 8e653c4b20cbee9eb78060e720003139bdee5269..268c8b3ed1e0c89c1291a7862b2ecec511f4e826 100644 --- a/applications/test/DLList/Test-DLList.C +++ b/applications/test/DLList/Test-DLList.C @@ -56,7 +56,6 @@ int main(int argc, char *argv[]) Info<< "element:" << *iter << endl; } - Info<< nl << "And again using the same STL iterator: " << nl << endl; forAllIters(myList, iter) diff --git a/applications/test/HashPtrTable/Test-hashPtrTable.C b/applications/test/HashPtrTable/Test-hashPtrTable.C index 5133d784b18fd09f134de28a24feda41f7f1623b..e62d59686983631413faa7c1f4d896548af65c1c 100644 --- a/applications/test/HashPtrTable/Test-hashPtrTable.C +++ b/applications/test/HashPtrTable/Test-hashPtrTable.C @@ -36,14 +36,9 @@ void printTable(const HashPtrTable<T>& table) { Info<< table.size() << nl << "(" << nl; - for - ( - typename HashPtrTable<T>::const_iterator iter = table.cbegin(); - iter != table.cend(); - ++iter - ) + forAllConstIters(table, iter) { - const T* ptr = *iter; + const T* ptr = iter.object(); Info<< iter.key() << " = "; if (ptr) { @@ -57,6 +52,22 @@ void printTable(const HashPtrTable<T>& table) } Info<< ")" << endl; + + // Values only, with for-range + Info<< "values ("; + for (auto val : table) + { + Info<< ' '; + if (val) + { + Info<< *val; + } + else + { + Info<< "nullptr"; + } + } + Info<< " )" << nl; } @@ -68,7 +79,9 @@ int main() HashPtrTable<double> myTable; myTable.insert("abc", new double(42.1)); myTable.insert("def", nullptr); - myTable.insert("ghi", new double(3.14159)); + myTable.insert("pi", new double(3.14159)); + myTable.insert("natlog", new double(2.718282)); + myTable.insert("sqrt2", new double(1.414214)); // Info<< myTable << endl; printTable(myTable); @@ -79,8 +92,20 @@ int main() printTable(copy); Info<< copy << endl; + Info<<"\nerase some existing and non-existing entries" << nl; + + auto iter = myTable.find("pi"); + myTable.erase(iter); + + iter = myTable.find("unknownKey"); + myTable.erase(iter); + + myTable.erase("abc"); + myTable.erase("unknownKey"); + + printTable(myTable); + return 0; } - // ************************************************************************* // diff --git a/applications/test/HashSet/Test-hashSet.C b/applications/test/HashSet/Test-hashSet.C index 0b219f2e3d1bef47f8d7831043bc6972bb5bbd29..55640829fb2ea0c2aa01630194e5260144253310 100644 --- a/applications/test/HashSet/Test-hashSet.C +++ b/applications/test/HashSet/Test-hashSet.C @@ -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; diff --git a/applications/test/HashTable/Test-hashTable.C b/applications/test/HashTable/Test-hashTable.C index 31bff05b0f840435784592f04bf60bc9e99b8ebe..8352a88ae0c05615b0fff10cd6198a7f6fc50b0b 100644 --- a/applications/test/HashTable/Test-hashTable.C +++ b/applications/test/HashTable/Test-hashTable.C @@ -25,6 +25,9 @@ License #include "HashTable.H" #include "List.H" +#include "SortableList.H" +#include "DynamicList.H" +#include "FlatOutput.H" #include "IOstreams.H" #include "IStringStream.H" #include "OStringStream.H" @@ -163,15 +166,15 @@ int main() << "\ntable2" << table2 << nl; - Info<< "\ntable3" << table3 - << "\nclearStorage table3 ... "; - table3.clearStorage(); - Info<< table3 << nl; + Info<< "\ntable3" << table2 + << "\nclearStorage table2 ... "; + table2.clearStorage(); + Info<< table2 << nl; table1 = { - {"aca", 3.0}, - {"aaw", 6.0}, + {"abc", 3.0}, + {"def", 6.0}, {"acr", 8.0}, {"aec", 10.0} }; @@ -193,7 +196,43 @@ int main() // These do not yet work. Issues resolving the distance. // // List<scalar> table1vals(table1.begin(), table1.end()); - // wordList table1keys(table1.begin(), table1.end()); + + { + Info<<"distance/size: " + << std::distance(table1.begin(), table1.end()) + << "/" << table1.size() + << " and " + << std::distance(table1.keys().begin(), table1.keys().end()) + << "/" << table1.keys().size() + << nl; + + SortableList<word> sortKeys + // DynamicList<word> sortKeys + ( + table1.keys().begin(), + table1.keys().end() + ); + Info<<"sortKeys: " << flatOutput(sortKeys) << nl; + } + + Info<< "\nFrom table1: " << flatOutput(table1.sortedToc()) << nl + << "retain keys: " << flatOutput(table3.sortedToc()) << nl; + + table1.retain(table3); + Info<< "-> " << flatOutput(table1.sortedToc()) << nl; + + Info<< "Lookup non-existent" << nl; + + Info<< table1.lookup("missing-const", 1.2345e+6) + << " // const-access" << nl; + + Info<< table1("missing-inadvertent", 3.14159) + << " // (inadvertent?) non-const access" << nl; + + Info<< table1("missing-autovivify") + << " // Known auto-vivification (non-const access)" << nl; + + Info<<"\ntable1: " << table1 << endl; Info<< "\nDone\n"; diff --git a/applications/test/List/Test-List.C b/applications/test/List/Test-List.C index 9ab2732c2e68ff4e9bb496aa64d27a11720b421f..d68bc4d7baa108673feab7951d386b3904a09317 100644 --- a/applications/test/List/Test-List.C +++ b/applications/test/List/Test-List.C @@ -42,6 +42,7 @@ See also #include "vector.H" #include "labelRange.H" +#include "scalarList.H" #include "ListOps.H" #include "SubList.H" @@ -144,6 +145,18 @@ int main(int argc, char *argv[]) labelList longLabelList = identity(15); + // This does not work: + // scalarList slist = identity(15); + // + // More writing, but does work: + scalarList slist + ( + labelRange::null.begin(), + labelRange::identity(15).end() + ); + + Info<<"scalar identity:" << flatOutput(slist) << endl; + Info<< "labels (contiguous=" << contiguous<label>() << ")" << nl; Info<< "normal: " << longLabelList << nl; @@ -220,7 +233,32 @@ int main(int argc, char *argv[]) } Info<< "sub-sorted: " << flatOutput(longLabelList) << nl; - // Info<<"Slice=" << longLabelList[labelRange(23,5)] << nl; + // construct from a label-range + labelRange range(25,15); + + labelList ident(range.begin(), range.end()); + Info<<"range-list (label)=" << ident << nl; + + List<scalar> sident(range.begin(), range.end()); + Info<<"range-list (scalar)=" << sident << nl; + + // Sub-ranges also work + List<scalar> sident2(range(3), range(10)); + Info<<"range-list (scalar)=" << sident2 << nl; + + // VERY BAD IDEA: List<scalar> sident3(range(10), range(3)); + + // This doesn't work, and don't know what it should do anyhow + // List<vector> vident(range.begin(), range.end()); + // Info<<"range-list (vector)=" << vident << nl; + + // Even weird things like this + List<scalar> sident4 + ( + labelRange().begin(), + labelRange::identity(8).end() + ); + Info<<"range-list (scalar)=" << sident4 << nl; } wordReList reLst; diff --git a/applications/test/labelRanges/Test-labelRanges.C b/applications/test/labelRanges/Test-labelRanges.C index ec2d15233900b2cf6abd25678dfcda3cc474f3d0..5acb0d296c134ed5aa632fe4dceaf98d260715f7 100644 --- a/applications/test/labelRanges/Test-labelRanges.C +++ b/applications/test/labelRanges/Test-labelRanges.C @@ -58,7 +58,7 @@ int main(int argc, char *argv[]) Info<<"test sorting" << endl; DynamicList<labelRange> list1(10); list1.append(labelRange(25, 8)); - list1.append(labelRange(0, 10)); + list1.append(labelRange::identity(8)); list1.append(labelRange(15, 5)); list1.append(labelRange(50, -10)); diff --git a/src/OpenFOAM/containers/HashTables/HashPtrTable/HashPtrTable.C b/src/OpenFOAM/containers/HashTables/HashPtrTable/HashPtrTable.C index 4143d25d85299c2e3ea2510bf1f80d16796a3a50..9370dd26edf59ee2f60dd2182250881d5df471a0 100644 --- a/src/OpenFOAM/containers/HashTables/HashPtrTable/HashPtrTable.C +++ b/src/OpenFOAM/containers/HashTables/HashPtrTable/HashPtrTable.C @@ -72,30 +72,44 @@ Foam::HashPtrTable<T, Key, Hash>::~HashPtrTable() template<class T, class Key, class Hash> T* Foam::HashPtrTable<T, Key, Hash>::remove(iterator& iter) { - T* ptr = iter.object(); - this->parent_type::erase(iter); - return ptr; + if (iter.found()) + { + T* ptr = iter.object(); + this->parent_type::erase(iter); + return ptr; + } + + return nullptr; } template<class T, class Key, class Hash> bool Foam::HashPtrTable<T, Key, Hash>::erase(iterator& iter) { - T* ptr = iter.object(); - - if (this->parent_type::erase(iter)) + if (iter.found()) { - if (ptr) + T* ptr = iter.object(); + + if (this->parent_type::erase(iter)) { - delete ptr; - } + if (ptr) + { + delete ptr; + } - return true; - } - else - { - return false; + return true; + } } + + return false; +} + + +template<class T, class Key, class Hash> +bool Foam::HashPtrTable<T, Key, Hash>::erase(const Key& key) +{ + auto iter = this->find(key); + return this->erase(iter); } diff --git a/src/OpenFOAM/containers/HashTables/HashPtrTable/HashPtrTable.H b/src/OpenFOAM/containers/HashTables/HashPtrTable/HashPtrTable.H index 7cdf3743e311b3acd4e63f42b8fa3e753a7368a3..f83c04e313905400600b2f1733ec49732281e6f8 100644 --- a/src/OpenFOAM/containers/HashTables/HashPtrTable/HashPtrTable.H +++ b/src/OpenFOAM/containers/HashTables/HashPtrTable/HashPtrTable.H @@ -116,12 +116,17 @@ public: // Edit - //- Remove and return the pointer specified by given iterator + //- Remove and return the pointer specified by given iterator. + // Includes a safeguard against the end-iterator. T* remove(iterator& iter); - //- Erase an hashedEntry specified by given iterator + //- Erase an entry specified by given iterator + // Includes a safeguard against the end-iterator. bool erase(iterator& iter); + //- Erase an entry specified by the given key + bool erase(const Key& key); + //- Clear all entries from table void clear(); diff --git a/src/OpenFOAM/containers/HashTables/HashSet/HashSet.C b/src/OpenFOAM/containers/HashTables/HashSet/HashSet.C index 321b8dd5dfe6bf9092ee0fe09dffbf8aae23e004..77ab200a42c7d72c7a7c16b066a88e263da06e12 100644 --- a/src/OpenFOAM/containers/HashTables/HashSet/HashSet.C +++ b/src/OpenFOAM/containers/HashTables/HashSet/HashSet.C @@ -227,16 +227,9 @@ void Foam::HashSet<Key, Hash>::operator|=(const HashSet<Key, Hash>& rhs) template<class Key, class Hash> -void Foam::HashSet<Key, Hash>::operator&=(const HashSet<Key, Hash>& rhs) +inline void Foam::HashSet<Key, Hash>::operator&=(const HashSet<Key, Hash>& rhs) { - // Remove elements not also found in rhs - for (iterator iter = this->begin(); iter != this->end(); ++iter) - { - if (!rhs.found(iter.key())) - { - this->erase(iter); - } - } + this->parent_type::retain(rhs); } @@ -259,13 +252,9 @@ void Foam::HashSet<Key, Hash>::operator^=(const HashSet<Key, Hash>& rhs) template<class Key, class Hash> -void Foam::HashSet<Key, Hash>::operator-=(const HashSet<Key, Hash>& rhs) +inline void Foam::HashSet<Key, Hash>::operator-=(const HashSet<Key, Hash>& rhs) { - // Remove rhs elements from lhs - for (const_iterator iter = rhs.cbegin(); iter != rhs.cend(); ++iter) - { - this->erase(iter.key()); - } + this->parent_type::erase(rhs); } diff --git a/src/OpenFOAM/containers/HashTables/HashSet/HashSet.H b/src/OpenFOAM/containers/HashTables/HashSet/HashSet.H index 300fc111325b22d1cd863f9b94dacb5ae1641419..d6171ad585486595217201e44685c404cda8bfd4 100644 --- a/src/OpenFOAM/containers/HashTables/HashSet/HashSet.H +++ b/src/OpenFOAM/containers/HashTables/HashSet/HashSet.H @@ -277,7 +277,7 @@ public: void operator|=(const HashSet<Key, Hash>& rhs); //- Only retain entries found in both HashSets - void operator&=(const HashSet<Key, Hash>& rhs); + inline void operator&=(const HashSet<Key, Hash>& rhs); //- Only retain unique entries (xor) void operator^=(const HashSet<Key, Hash>& rhs); @@ -289,7 +289,7 @@ public: } //- Remove entries listed in the given HashSet from this HashSet - void operator-=(const HashSet<Key, Hash>& rhs); + inline void operator-=(const HashSet<Key, Hash>& rhs); // IOstream Operator diff --git a/src/OpenFOAM/containers/HashTables/HashTable/HashTable.C b/src/OpenFOAM/containers/HashTables/HashTable/HashTable.C index b0b96c5b19aec6d0e56f0678df055a31e59c8556..276f617dba102f5ef1fcc6b2005c0081f2553967 100644 --- a/src/OpenFOAM/containers/HashTables/HashTable/HashTable.C +++ b/src/OpenFOAM/containers/HashTables/HashTable/HashTable.C @@ -257,7 +257,7 @@ template<class T, class Key, class Hash> bool Foam::HashTable<T, Key, Hash>::set ( const Key& key, - const T& newEntry, + const T& obj, const bool protect ) { @@ -284,7 +284,7 @@ bool Foam::HashTable<T, Key, Hash>::set if (!existing) { // Not found, insert it at the head - table_[hashIdx] = new hashedEntry(key, newEntry, table_[hashIdx]); + table_[hashIdx] = new hashedEntry(key, obj, table_[hashIdx]); nElmts_++; if (double(nElmts_)/tableSize_ > 0.8 && tableSize_ < maxTableSize) @@ -316,7 +316,7 @@ bool Foam::HashTable<T, Key, Hash>::set { // Found - overwrite existing entry // this corresponds to the Perl convention - hashedEntry* ep = new hashedEntry(key, newEntry, existing->next_); + hashedEntry* ep = new hashedEntry(key, obj, existing->next_); // Replace existing element - within list or insert at the head if (prev) @@ -411,7 +411,8 @@ bool Foam::HashTable<T, Key, Hash>::erase(const iterator& iter) template<class T, class Key, class Hash> bool Foam::HashTable<T, Key, Hash>::erase(const Key& key) { - return erase(find(key)); + auto iter = find(key); + return erase(iter); } @@ -450,15 +451,15 @@ Foam::label Foam::HashTable<T, Key, Hash>::erase const HashTable<AnyType, Key, AnyHash>& other ) { - // Remove other keys from this table const label nTotal = this->size(); label changed = 0; - if (other.size() < nTotal) + using other_iter = + typename HashTable<AnyType, Key, AnyHash>::const_iterator; + + if (other.size() <= nTotal) { - // other is smaller, use its keys for removal - using other_iter = - typename HashTable<AnyType, Key, AnyHash>::const_iterator; + // The other is smaller/same-size, use its keys for removal for ( @@ -475,7 +476,7 @@ Foam::label Foam::HashTable<T, Key, Hash>::erase } else { - // other is same/larger: iterate ourselves and check for key in other + // We are smaller: remove if found in the other hash for ( iterator iter = begin(); @@ -494,6 +495,39 @@ Foam::label Foam::HashTable<T, Key, Hash>::erase } +template<class T, class Key, class Hash> +template<class AnyType, class AnyHash> +Foam::label Foam::HashTable<T, Key, Hash>::retain +( + const HashTable<AnyType, Key, AnyHash>& other +) +{ + const label nTotal = this->size(); + label changed = 0; + + if (other.empty()) + { + // Trivial case + changed = nTotal; + this->clear(); + } + else + { + // Inverted logic: remove if *not* found in the other hash + + for (iterator iter = begin(); iter != end(); ++iter) + { + if (!other.found(iter.key()) && erase(iter)) + { + ++changed; + } + } + } + + return changed; +} + + template<class T, class Key, class Hash> void Foam::HashTable<T, Key, Hash>::resize(const label sz) { diff --git a/src/OpenFOAM/containers/HashTables/HashTable/HashTable.H b/src/OpenFOAM/containers/HashTables/HashTable/HashTable.H index bd60050ee443b517b9faaa4cb29caf923076c863..9eef011d10e1b471b8e0d246ed350805758bdeb4 100644 --- a/src/OpenFOAM/containers/HashTables/HashTable/HashTable.H +++ b/src/OpenFOAM/containers/HashTables/HashTable/HashTable.H @@ -44,6 +44,7 @@ Note SourceFiles HashTableI.H HashTable.C + HashTableCoreI.H HashTableCore.C HashTableIO.C @@ -60,6 +61,7 @@ SourceFiles #include "nullObject.H" #include <initializer_list> +#include <iterator> // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // @@ -178,10 +180,17 @@ public: //- Type of values that the HashTable contains. typedef T value_type; + //- The type used for storing into value_type objects. + // This type is usually value_type&. + typedef T* pointer; + //- The type used for storing into value_type objects. // This type is usually value_type&. typedef T& reference; + //- The type used for reading from constant value_type objects. + typedef const T* const_pointer; + //- The type used for reading from constant value_type objects. typedef const T& const_reference; @@ -247,7 +256,7 @@ private: //- Assign a new hash-entry 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& obj, const bool protect); protected: @@ -307,17 +316,20 @@ public: //- Return true if the hash table is empty inline bool empty() const; - //- Return true if hashedEntry is found in table + //- Return true if hashed entry is found in table bool found(const Key& key) const; - //- Find and return an iterator set at the hashedEntry + //- Find and return an iterator set at the hashed entry // If not found iterator = end() iterator find(const Key& key); - //- Find and return an const_iterator set at the hashedEntry + //- Find and return an const_iterator set at the hashed entry // If not found iterator = end() const_iterator find(const Key& key) const; + //- Return hashed entry if it exists, or return the given default + inline const T& lookup(const Key& key, const T& deflt) const; + //- Return the table of contents List<Key> toc() const; @@ -327,42 +339,58 @@ public: // Edit - //- Insert a new hashedEntry + //- Insert a new entry // 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& obj); - //- Assign a new hashedEntry, overwriting existing entries. + //- Assign a new entry, overwriting existing entries. // Returns true. - inline bool set(const Key& key, const T& newEntry); - - //- Erase a hashedEntry specified by given iterator - // This invalidates the iterator until the next ++ operation + inline bool set(const Key& key, const T& obj); + + //- Erase an entry specified by given iterator + // This invalidates the iterator until the next ++ operation. + // + // Includes a safeguard against the end-iterator such that the + // following is safe: + // \code + // auto iter = table.find(unknownKey); + // table.erase(iter); + // \endcode + // which is what \code table.erase(unknownKey) \endcode does anyhow bool erase(const iterator& iter); - //- Erase a hashedEntry specified by the given key + //- Erase an entry specified by the given key bool erase(const Key& key); - //- Remove entries given by the listed keys from this HashTable + //- Remove table entries given by the listed keys // Return the number of elements removed label erase(const UList<Key>& keys); - //- Remove entries given by the listed keys from this HashTable + //- Remove table entries given by the listed keys // Return the number of elements removed template<unsigned Size> label erase(const FixedList<Key, Size>& keys); - //- Remove entries given by the listed keys from this HashTable + //- Remove table entries given by the listed keys // Return the number of elements removed label erase(std::initializer_list<Key> keys); - //- Remove entries given by the given keys from this HashTable + //- Remove table entries given by keys of the other hash-table. // Return the number of elements removed. - // The parameter HashTable needs the same type of key, but the + // + // The other hash-table must have the same type of key, but the // type of values held and the hashing function are arbitrary. template<class AnyType, class AnyHash> label erase(const HashTable<AnyType, Key, AnyHash>& other); + //- Retain table entries given by keys of the other hash-table. + // + // The other hash-table must have the same type of key, but the + // type of values held and the hashing function are arbitrary. + template<class AnyType, class AnyHash> + label retain(const HashTable<AnyType, Key, AnyHash>& other); + //- Resize the hash table for efficiency void resize(const label sz); @@ -383,10 +411,10 @@ public: // Member Operators - //- Find and return a hashedEntry + //- Find and return a hashed entry. FatalError if it does not exist. inline T& operator[](const Key& key); - //- Find and return a hashedEntry + //- Find and return a hashed entry. FatalError if it does not exist. inline const T& operator[](const Key& key) const; //- Return existing entry or create a new entry. @@ -394,6 +422,12 @@ public: // value-initialized. For primitives, this will be zero. inline T& operator()(const Key& key); + //- Return existing entry or insert a new entry. + inline T& operator()(const Key& key, const T& deflt); + + //- Return hashed entry if it exists, or return the given default + inline const T& operator()(const Key& key, const T& deflt) const; + //- Assignment void operator=(const HashTable<T, Key, Hash>& rhs); @@ -423,6 +457,7 @@ protected: // Public typedefs using table_type = this_type; using key_type = this_type::key_type; + using iterator_category = std::forward_iterator_tag; using difference_type = this_type::difference_type; private: @@ -500,16 +535,20 @@ public: public WrappedIterator { public: + using value_type = this_type::key_type; + using pointer = const Key*; using reference = const Key&; - using difference_type = typename WrappedIterator::difference_type; //- Implicit conversion inline key_iterator_base(const WrappedIterator& iter); //- Return the key inline reference operator*() const; - }; + inline reference operator()() const; + inline key_iterator_base& operator++(); + inline key_iterator_base operator++(int); + }; // STL iterator @@ -526,9 +565,9 @@ public: // Public typedefs using table_type = this_type; - using key_type = this_type::key_type; + using value_type = this_type::value_type; + using pointer = this_type::pointer; using reference = this_type::reference; - using difference_type = typename iterator_base::difference_type; // Constructors @@ -574,9 +613,9 @@ public: // Public typedefs using table_type = const this_type; - using key_type = this_type::key_type; + using value_type = const this_type::value_type; + using pointer = this_type::const_pointer; using reference = this_type::const_reference; - using difference_type = typename iterator_base::difference_type; // Constructors diff --git a/src/OpenFOAM/containers/HashTables/HashTable/HashTableI.H b/src/OpenFOAM/containers/HashTables/HashTable/HashTableI.H index f8df2437591dfafd274ff86cb9867d29d113767e..4d8028ca139e61ecd8a0d880d156e6888c199471 100644 --- a/src/OpenFOAM/containers/HashTables/HashTable/HashTableI.H +++ b/src/OpenFOAM/containers/HashTables/HashTable/HashTableI.H @@ -79,10 +79,10 @@ template<class T, class Key, class Hash> inline bool Foam::HashTable<T, Key, Hash>::insert ( const Key& key, - const T& newEntry + const T& obj ) { - return this->set(key, newEntry, true); + return this->set(key, obj, true); } @@ -90,10 +90,10 @@ template<class T, class Key, class Hash> inline bool Foam::HashTable<T, Key, Hash>::set ( const Key& key, - const T& newEntry + const T& obj ) { - return this->set(key, newEntry, false); + return this->set(key, obj, false); } @@ -105,6 +105,18 @@ Foam::HashTable<T, Key, Hash>::xfer() } +template<class T, class Key, class Hash> +inline const T& Foam::HashTable<T, Key, Hash>::lookup +( + const Key& key, + const T& deflt +) const +{ + const_iterator iter = this->find(key); + return iter.found() ? iter.object() : deflt; +} + + // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * // template<class T, class Key, class Hash> @@ -156,6 +168,36 @@ inline T& Foam::HashTable<T, Key, Hash>::operator()(const Key& key) } +template<class T, class Key, class Hash> +inline T& Foam::HashTable<T, Key, Hash>::operator() +( + const Key& key, + const T& deflt +) +{ + iterator iter = this->find(key); + + if (iter.found()) + { + return iter.object(); + } + + this->insert(key, deflt); + return find(key).object(); +} + + +template<class T, class Key, class Hash> +inline const T& Foam::HashTable<T, Key, Hash>::operator() +( + const Key& key, + const T& deflt +) const +{ + return this->lookup(key, deflt); +} + + // * * * * * * * * * * * * * * * iterator base * * * * * * * * * * * * * * * // template<class T, class Key, class Hash> @@ -321,6 +363,39 @@ Foam::HashTable<T, Key, Hash>::key_iterator_base<WrappedIterator> } +template<class T, class Key, class Hash> +template<class WrappedIterator> +inline const Key& +Foam::HashTable<T, Key, Hash>::key_iterator_base<WrappedIterator> +::operator()() const +{ + return this->key(); +} + + +template<class T, class Key, class Hash> +template<class WrappedIterator> +inline Foam::HashTable<T, Key, Hash>::key_iterator_base<WrappedIterator>& +Foam::HashTable<T, Key, Hash>::key_iterator_base<WrappedIterator> +::operator++() +{ + this->increment(); + return *this; +} + + +template<class T, class Key, class Hash> +template<class WrappedIterator> +inline Foam::HashTable<T, Key, Hash>::key_iterator_base<WrappedIterator> +Foam::HashTable<T, Key, Hash>::key_iterator_base<WrappedIterator> +::operator++(int) +{ + key_iterator_base old = *this; + this->increment(); + return old; +} + + // * * * * * * * * * * * * * * * * STL iterator * * * * * * * * * * * * * * // template<class T, class Key, class Hash> diff --git a/src/OpenFOAM/containers/HashTables/StaticHashTable/StaticHashTable.C b/src/OpenFOAM/containers/HashTables/StaticHashTable/StaticHashTable.C index 948a451d47f6695cf49afa0ce7633cd623e2de39..ec30dfc20a0e71eb4a4a631871adb6c2e0b18b02 100644 --- a/src/OpenFOAM/containers/HashTables/StaticHashTable/StaticHashTable.C +++ b/src/OpenFOAM/containers/HashTables/StaticHashTable/StaticHashTable.C @@ -203,7 +203,7 @@ template<class T, class Key, class Hash> bool Foam::StaticHashTable<T, Key, Hash>::set ( const Key& key, - const T& newEntry, + const T& obj, const bool protect ) { @@ -229,7 +229,7 @@ bool Foam::StaticHashTable<T, Key, Hash>::set localObjects.setSize(existing+1); localKeys[existing] = key; - localObjects[existing] = newEntry; + localObjects[existing] = obj; nElmts_++; } @@ -250,7 +250,7 @@ bool Foam::StaticHashTable<T, Key, Hash>::set { // Found - overwrite existing entry // this corresponds to the Perl convention - objects_[hashIdx][existing] = newEntry; + objects_[hashIdx][existing] = obj; } return true; diff --git a/src/OpenFOAM/containers/HashTables/StaticHashTable/StaticHashTable.H b/src/OpenFOAM/containers/HashTables/StaticHashTable/StaticHashTable.H index 639a434d654dcf3f073e861714e0428e6ba0629c..5b0a3ef8a59fb88c9c94cd22278c46eda6765420 100644 --- a/src/OpenFOAM/containers/HashTables/StaticHashTable/StaticHashTable.H +++ b/src/OpenFOAM/containers/HashTables/StaticHashTable/StaticHashTable.H @@ -121,10 +121,10 @@ class StaticHashTable //- Return the hash index of the Key within the current table size. // No checks for zero-sized tables. - inline label hashKeyIndex(const Key&) const; + inline label hashKeyIndex(const Key& key) const; //- Assign a new hashed entry to a possibly already existing key - bool set(const Key&, const T& newElmt, bool protect); + bool set(const Key& key, const T& obj, bool protect); public: diff --git a/src/OpenFOAM/containers/HashTables/StaticHashTable/StaticHashTableI.H b/src/OpenFOAM/containers/HashTables/StaticHashTable/StaticHashTableI.H index cebb3852aa56de015e2067f4b415134b88bed8a0..2e6b5db0ea0e597a30bd6e2fe4481bd41f79b774 100644 --- a/src/OpenFOAM/containers/HashTables/StaticHashTable/StaticHashTableI.H +++ b/src/OpenFOAM/containers/HashTables/StaticHashTable/StaticHashTableI.H @@ -26,8 +26,6 @@ License #include "error.H" #include "IOstreams.H" -// * * * * * * * * * * * * * Private Member Classes * * * * * * * * * * * * // - // * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * * // template<class T, class Key, class Hash> @@ -59,10 +57,10 @@ template<class T, class Key, class Hash> inline bool Foam::StaticHashTable<T, Key, Hash>::insert ( const Key& key, - const T& newEntry + const T& obj ) { - return set(key, newEntry, true); + return set(key, obj, true); } @@ -70,10 +68,10 @@ template<class T, class Key, class Hash> inline bool Foam::StaticHashTable<T, Key, Hash>::set ( const Key& key, - const T& newEntry + const T& obj ) { - return set(key, newEntry, false); + return set(key, obj, false); } diff --git a/src/OpenFOAM/containers/LinkedLists/accessTypes/LList/LList.H b/src/OpenFOAM/containers/LinkedLists/accessTypes/LList/LList.H index cac481545ebe5cf58d9176fcf80e33b74f4b3d60..eefabca3290bb76b3eece74e20a0b6b984bc709c 100644 --- a/src/OpenFOAM/containers/LinkedLists/accessTypes/LList/LList.H +++ b/src/OpenFOAM/containers/LinkedLists/accessTypes/LList/LList.H @@ -254,14 +254,14 @@ public: // Member operators - T& operator*() + T& operator*() const { return static_cast<link&> (LListBase_iterator::operator*()).obj_; } - T& operator()() + T& operator()() const { return operator*(); } @@ -312,14 +312,14 @@ public: // Member operators - const T& operator*() + const T& operator*() const { return static_cast<const link&> (LListBase_const_iterator::operator*()).obj_; } - const T& operator()() + const T& operator()() const { return operator*(); } diff --git a/src/OpenFOAM/containers/LinkedLists/accessTypes/LPtrList/LPtrList.H b/src/OpenFOAM/containers/LinkedLists/accessTypes/LPtrList/LPtrList.H index e2b7e49a5aad4103a7470d553918eebe977378d8..a0e22917f9c7fc1b29a17e653c9ab838cb871497 100644 --- a/src/OpenFOAM/containers/LinkedLists/accessTypes/LPtrList/LPtrList.H +++ b/src/OpenFOAM/containers/LinkedLists/accessTypes/LPtrList/LPtrList.H @@ -196,12 +196,12 @@ public: // Member operators - T& operator*() + T& operator*() const { return *(LList<LListBase, T*>::iterator::operator*()); } - T& operator()() + T& operator()() const { return operator*(); } @@ -235,12 +235,12 @@ public: // Member operators - const T& operator*() + const T& operator*() const { return *(LList<LListBase, T*>::const_iterator::operator*()); } - const T& operator()() + const T& operator()() const { return operator*(); } diff --git a/src/OpenFOAM/containers/LinkedLists/accessTypes/UILList/UILList.H b/src/OpenFOAM/containers/LinkedLists/accessTypes/UILList/UILList.H index 28d1231762f0e0713a2f5c834a9180eaa91a07fe..1f5b01b326424bf317df0a87e20bacc1cb8125c4 100644 --- a/src/OpenFOAM/containers/LinkedLists/accessTypes/UILList/UILList.H +++ b/src/OpenFOAM/containers/LinkedLists/accessTypes/UILList/UILList.H @@ -192,12 +192,12 @@ public: // Member operators - T& operator*() + T& operator*() const { return static_cast<T&>(LListBase_iterator::operator*()); } - T& operator()() + T& operator()() const { return operator*(); } @@ -247,14 +247,14 @@ public: // Member operators - const T& operator*() + const T& operator*() const { return static_cast<const T&> (LListBase_const_iterator::operator*()); } - const T& operator()() + const T& operator()() const { return operator*(); } @@ -309,14 +309,14 @@ public: // Member operators - const T& operator*() + const T& operator*() const { return static_cast<const T&> (LListBase::const_reverse_iterator::operator*()); } - const T& operator()() + const T& operator()() const { return operator*(); } diff --git a/src/OpenFOAM/containers/LinkedLists/linkTypes/DLListBase/DLListBase.C b/src/OpenFOAM/containers/LinkedLists/linkTypes/DLListBase/DLListBase.C index e8e6b721c3f7f76c3449fa5bdf26de37093c5c96..f428836f43bab04790383273b1d2f8d127124852 100644 --- a/src/OpenFOAM/containers/LinkedLists/linkTypes/DLListBase/DLListBase.C +++ b/src/OpenFOAM/containers/LinkedLists/linkTypes/DLListBase/DLListBase.C @@ -188,7 +188,7 @@ Foam::DLListBase::link* Foam::DLListBase::removeHead() if (!first_) { - last_ = 0; + last_ = nullptr; } f->deregister(); @@ -204,8 +204,8 @@ Foam::DLListBase::link* Foam::DLListBase::remove(DLListBase::link* l) if (l == first_ && first_ == last_) { - first_ = 0; - last_ = 0; + first_ = nullptr; + last_ = nullptr; } else if (l == first_) { diff --git a/src/OpenFOAM/containers/LinkedLists/linkTypes/DLListBase/DLListBase.H b/src/OpenFOAM/containers/LinkedLists/linkTypes/DLListBase/DLListBase.H index 3208c250a964832ba77fbd535328533c98bc7f6b..d65363113eafaa3991bbc35040fda8dc8099e71c 100644 --- a/src/OpenFOAM/containers/LinkedLists/linkTypes/DLListBase/DLListBase.H +++ b/src/OpenFOAM/containers/LinkedLists/linkTypes/DLListBase/DLListBase.H @@ -25,9 +25,10 @@ Class Foam::DLListBase Description - Base doubly-linked list. + Base for doubly-linked lists. SourceFiles + DLListBaseI.H DLListBase.C \*---------------------------------------------------------------------------*/ @@ -84,10 +85,10 @@ private: // Private Member Functions //- Disallow default bitwise copy construct - DLListBase(const DLListBase&); + DLListBase(const DLListBase&) = delete; //- Disallow default bitwise assignment - void operator=(const DLListBase&); + void operator=(const DLListBase&) = delete; public: @@ -184,18 +185,14 @@ public: friend class DLListBase; friend class const_iterator; - // Private data + //- Reference to the list this is an iterator for + DLListBase& curList_; - //- Reference to the list this is an iterator for - DLListBase& curList_; + //- Current element + link* curElmt_; - //- Current element - link* curElmt_; - - //- Copy of the link - link curLink_; - - // Private Member Functions + //- Copy of the link + link curLink_; //- Construct for a given SLListBase with nullptr element and link. // Only used to create endIter @@ -206,17 +203,18 @@ public: //- Construct for a given DLListBase and link inline iterator(DLListBase&, link*); - // Member operators + //- Currently pointing at a valid entry + inline bool found() const; - inline void operator=(const iterator&); + inline void operator=(const iterator& iter); - inline bool operator==(const iterator&) const; - inline bool operator!=(const iterator&) const; + inline bool operator==(const iterator& iter) const; + inline bool operator!=(const iterator& iter) const; - inline link& operator*(); + inline link& operator*() const; - inline iterator& operator++(); - inline iterator operator++(int); + inline iterator& operator++(); + inline iterator operator++(int); }; inline iterator begin(); @@ -228,13 +226,11 @@ public: //- An STL-conforming const_iterator class const_iterator { - // Private data + //- Reference to the list this is an iterator for + const DLListBase& curList_; - //- Reference to the list this is an iterator for - const DLListBase& curList_; - - //- Current element - const link* curElmt_; + //- Current element + const link* curElmt_; public: @@ -242,19 +238,20 @@ public: inline const_iterator(const DLListBase&, const link*); //- Construct from a non-const iterator - inline const_iterator(const iterator&); + inline const_iterator(const DLListBase::iterator& iter); - // Member operators + //- Currently pointing at a valid entry + inline bool found() const; - inline void operator=(const const_iterator&); + inline void operator=(const const_iterator& iter); - inline bool operator==(const const_iterator&) const; - inline bool operator!=(const const_iterator&) const; + inline bool operator==(const const_iterator& iter) const; + inline bool operator!=(const const_iterator& iter) const; - inline const link& operator*(); + inline const link& operator*() const; - inline const_iterator& operator++(); - inline const_iterator operator++(int); + inline const_iterator& operator++(); + inline const_iterator operator++(int); }; inline const_iterator cbegin() const; @@ -269,30 +266,29 @@ public: //- An STL-conforming const_reverse_iterator class const_reverse_iterator { - // Private data - - //- Reference to the list this is an reverse_iterator for - const DLListBase& curList_; + //- Reference to the list this is an reverse_iterator for + const DLListBase& curList_; - //- Current element - const link* curElmt_; + //- Current element + const link* curElmt_; public: //- Construct for a given DLListBase and link - inline const_reverse_iterator(const DLListBase&, const link*); + inline const_reverse_iterator(const DLListBase& lst, const link*); - // Member operators + //- Currently pointing at a valid entry + inline bool found() const; - inline void operator=(const const_reverse_iterator&); + inline void operator=(const const_reverse_iterator& iter); - inline bool operator==(const const_reverse_iterator&) const; - inline bool operator!=(const const_reverse_iterator&) const; + inline bool operator==(const const_reverse_iterator& iter) const; + inline bool operator!=(const const_reverse_iterator& iter) const; - inline const link& operator*(); + inline const link& operator*() const; - inline const_reverse_iterator& operator++(); - inline const_reverse_iterator operator++(int); + inline const_reverse_iterator& operator++(); + inline const_reverse_iterator operator++(int); }; inline const_reverse_iterator crbegin() const; diff --git a/src/OpenFOAM/containers/LinkedLists/linkTypes/DLListBase/DLListBaseI.H b/src/OpenFOAM/containers/LinkedLists/linkTypes/DLListBase/DLListBaseI.H index 91c0bc9ddbd2c713c8a54b651b64740c0ff3095a..6dc080777a9b39d4c9a0a9a973023b3d5b9e9de0 100644 --- a/src/OpenFOAM/containers/LinkedLists/linkTypes/DLListBase/DLListBaseI.H +++ b/src/OpenFOAM/containers/LinkedLists/linkTypes/DLListBase/DLListBaseI.H @@ -29,15 +29,15 @@ License inline Foam::DLListBase::link::link() : - prev_(0), - next_(0) + prev_(nullptr), + next_(nullptr) {} inline Foam::DLListBase::DLListBase() : - first_(0), - last_(0), + first_(nullptr), + last_(nullptr), nElmts_(0) {} @@ -63,14 +63,14 @@ inline Foam::DLListBase::~DLListBase() inline bool Foam::DLListBase::link::registered() const { - return prev_ != 0 && next_ != 0; + return prev_ != nullptr && next_ != nullptr; } inline void Foam::DLListBase::link::deregister() { - prev_ = 0; - next_ = 0; + prev_ = nullptr; + next_ = nullptr; } @@ -140,8 +140,8 @@ Foam::DLListBase::last() const inline void Foam::DLListBase::clear() { - first_ = 0; - last_ = 0; + first_ = nullptr; + last_ = nullptr; nElmts_ = 0; } @@ -195,6 +195,12 @@ inline Foam::DLListBase::iterator::iterator(DLListBase& s) {} +inline bool Foam::DLListBase::iterator::found() const +{ + return (curElmt_ != nullptr); +} + + inline void Foam::DLListBase::iterator::operator=(const iterator& iter) { curElmt_ = iter.curElmt_; @@ -215,7 +221,7 @@ inline bool Foam::DLListBase::iterator::operator!=(const iterator& iter) const inline Foam::DLListBase::link& -Foam::DLListBase::iterator::operator*() +Foam::DLListBase::iterator::operator*() const { return *curElmt_; } @@ -226,9 +232,9 @@ Foam::DLListBase::iterator::operator++() { // Check if the curElmt_ is the last element (if it points to itself) // or if the list is empty because the last element may have been removed - if (curLink_.next_ == curElmt_ || curList_.last_ == 0) + if (curLink_.next_ == curElmt_ || curList_.last_ == nullptr) { - curElmt_ = 0; + curElmt_ = nullptr; } else { @@ -282,13 +288,22 @@ inline Foam::DLListBase::const_iterator::const_iterator {} -inline Foam::DLListBase::const_iterator::const_iterator(const iterator& iter) +inline Foam::DLListBase::const_iterator::const_iterator +( + const DLListBase::iterator& iter +) : curList_(iter.curList_), curElmt_(iter.curElmt_) {} +inline bool Foam::DLListBase::const_iterator::found() const +{ + return (curElmt_ != nullptr); +} + + inline void Foam::DLListBase::const_iterator::operator= ( const const_iterator& iter @@ -317,7 +332,7 @@ inline bool Foam::DLListBase::const_iterator::operator!= inline const Foam::DLListBase::link& -Foam::DLListBase::const_iterator::operator*() +Foam::DLListBase::const_iterator::operator*() const { return *curElmt_; } @@ -328,7 +343,7 @@ Foam::DLListBase::const_iterator::operator++() { if (curElmt_ == curList_.last_) { - curElmt_ = 0; + curElmt_ = nullptr; } else { @@ -387,15 +402,21 @@ Foam::DLListBase::end() const inline Foam::DLListBase::const_reverse_iterator::const_reverse_iterator ( - const DLListBase& s, + const DLListBase& lst, const link* elmt ) : - curList_(s), + curList_(lst), curElmt_(elmt) {} +inline bool Foam::DLListBase::const_reverse_iterator::found() const +{ + return (curElmt_ != nullptr); +} + + inline void Foam::DLListBase::const_reverse_iterator::operator= ( const const_reverse_iterator& iter @@ -424,7 +445,7 @@ inline bool Foam::DLListBase::const_reverse_iterator::operator!= inline const Foam::DLListBase::link& -Foam::DLListBase::const_reverse_iterator::operator*() +Foam::DLListBase::const_reverse_iterator::operator*() const { return *curElmt_; } @@ -435,7 +456,7 @@ Foam::DLListBase::const_reverse_iterator::operator++() { if (curElmt_ == curList_.first_) { - curElmt_ = 0; + curElmt_ = nullptr; } else { @@ -460,7 +481,7 @@ Foam::DLListBase::crbegin() const { if (size()) { - return const_reverse_iterator(*this, last()); + return const_reverse_iterator(*this, this->last()); } else { diff --git a/src/OpenFOAM/containers/LinkedLists/linkTypes/SLListBase/SLListBase.C b/src/OpenFOAM/containers/LinkedLists/linkTypes/SLListBase/SLListBase.C index c314cdc7528374d4f28607d01c5736f711cab6ca..693530d595a026350b94b87cb71c2f7b066f3ef8 100644 --- a/src/OpenFOAM/containers/LinkedLists/linkTypes/SLListBase/SLListBase.C +++ b/src/OpenFOAM/containers/LinkedLists/linkTypes/SLListBase/SLListBase.C @@ -79,7 +79,7 @@ Foam::SLListBase::link* Foam::SLListBase::removeHead() { nElmts_--; - if (last_ == 0) + if (last_ == nullptr) { FatalErrorInFunction << "remove from empty list" @@ -90,7 +90,7 @@ Foam::SLListBase::link* Foam::SLListBase::removeHead() if (f == last_) { - last_ = 0; + last_ = nullptr; } else { @@ -132,7 +132,7 @@ Foam::SLListBase::link* Foam::SLListBase::remove(SLListBase::link* it) prev = p; } - return 0; + return nullptr; } diff --git a/src/OpenFOAM/containers/LinkedLists/linkTypes/SLListBase/SLListBase.H b/src/OpenFOAM/containers/LinkedLists/linkTypes/SLListBase/SLListBase.H index add64244557da03a460dcc4e2614a6151ea11809..421922d3b867ff33eb3cd5b8b04bdc2d3bb8f70c 100644 --- a/src/OpenFOAM/containers/LinkedLists/linkTypes/SLListBase/SLListBase.H +++ b/src/OpenFOAM/containers/LinkedLists/linkTypes/SLListBase/SLListBase.H @@ -25,9 +25,10 @@ Class Foam::SLListBase Description - Base singly-linked list. + Base for singly-linked lists. SourceFiles + SLListBaseI.H SLListBase.C \*---------------------------------------------------------------------------*/ @@ -81,10 +82,10 @@ private: // Private Member Functions //- Disallow default bitwise copy construct - SLListBase(const SLListBase&); + SLListBase(const SLListBase&) = delete; //- Disallow default bitwise assignment - void operator=(const SLListBase&); + void operator=(const SLListBase&) = delete; public: @@ -166,18 +167,14 @@ public: friend class SLListBase; friend class const_iterator; - // Private data + //- Reference to the list this is an iterator for + SLListBase& curList_; - //- Reference to the list this is an iterator for - SLListBase& curList_; + //- Current element + link* curElmt_; - //- Current element - link* curElmt_; - - //- Copy of the link - link curLink_; - - // Private Member Functions + //- Copy of the link + link curLink_; //- Construct for a given SLListBase with nullptr element and link. // Only used to create endIter @@ -188,17 +185,18 @@ public: //- Construct for a given SLListBase and link inline iterator(SLListBase&, link*); - // Member operators + //- Currently pointing at a valid entry + inline bool found() const; - inline void operator=(const iterator&); + inline void operator=(const iterator& iter); - inline bool operator==(const iterator&) const; - inline bool operator!=(const iterator&) const; + inline bool operator==(const iterator& iter) const; + inline bool operator!=(const iterator& iter) const; - inline link& operator*(); + inline link& operator*() const; - inline iterator& operator++(); - inline iterator operator++(int); + inline iterator& operator++(); + inline iterator operator++(int); }; inline iterator begin(); @@ -210,13 +208,11 @@ public: //- An STL-conforming const_iterator class const_iterator { - // Private data + //- Reference to the list this is an iterator for + const SLListBase& curList_; - //- Reference to the list this is an iterator for - const SLListBase& curList_; - - //- Current element - const link* curElmt_; + //- Current element + const link* curElmt_; public: @@ -224,20 +220,20 @@ public: inline const_iterator(const SLListBase&, const link*); //- Construct from a non-const iterator - inline const_iterator(const iterator&); - + inline const_iterator(const SLListBase::iterator& iter); - // Member operators + //- Currently pointing at a valid entry + inline bool found() const; - inline void operator=(const const_iterator&); + inline void operator=(const const_iterator& iter); - inline bool operator==(const const_iterator&) const; - inline bool operator!=(const const_iterator&) const; + inline bool operator==(const const_iterator& iter) const; + inline bool operator!=(const const_iterator& iter) const; - inline const link& operator*(); + inline const link& operator*() const; - inline const_iterator& operator++(); - inline const_iterator operator++(int); + inline const_iterator& operator++(); + inline const_iterator operator++(int); }; inline const_iterator cbegin() const; diff --git a/src/OpenFOAM/containers/LinkedLists/linkTypes/SLListBase/SLListBaseI.H b/src/OpenFOAM/containers/LinkedLists/linkTypes/SLListBase/SLListBaseI.H index fc9b9f007c05336e157a6b2b8388a85414b69a6d..8c4ebdb7c372a2f1b38972123126918c32be2360 100644 --- a/src/OpenFOAM/containers/LinkedLists/linkTypes/SLListBase/SLListBaseI.H +++ b/src/OpenFOAM/containers/LinkedLists/linkTypes/SLListBase/SLListBaseI.H @@ -21,9 +21,6 @@ License You should have received a copy of the GNU General Public License along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>. -Description - Base singly-linked list. - \*---------------------------------------------------------------------------*/ #include "error.H" @@ -32,7 +29,7 @@ Description inline Foam::SLListBase::link::link() : - next_(0) + next_(nullptr) {} @@ -44,16 +41,22 @@ inline Foam::SLListBase::link::link(link* p) inline Foam::SLListBase::SLListBase() : - last_(0), + last_(nullptr), nElmts_(0) {} inline Foam::SLListBase::SLListBase(link* a) : - last_(a->next_ = a), - nElmts_(1) -{} + last_(a), + nElmts_(0) +{ + if (a) // protect against nullptr + { + a->next_ = a; + nElmts_ = 1; + } +} // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * // @@ -130,7 +133,7 @@ Foam::SLListBase::last() const inline void Foam::SLListBase::clear() { - last_ = 0; + last_ = nullptr; nElmts_ = 0; } @@ -171,6 +174,12 @@ inline Foam::SLListBase::iterator::iterator(SLListBase& s) {} +inline bool Foam::SLListBase::iterator::found() const +{ + return (curElmt_ != nullptr); +} + + inline void Foam::SLListBase::iterator::operator=(const iterator& iter) { curElmt_ = iter.curElmt_; @@ -190,7 +199,7 @@ inline bool Foam::SLListBase::iterator::operator!=(const iterator& iter) const } -inline Foam::SLListBase::link& Foam::SLListBase::iterator::operator*() +inline Foam::SLListBase::link& Foam::SLListBase::iterator::operator*() const { return *curElmt_; } @@ -198,9 +207,9 @@ inline Foam::SLListBase::link& Foam::SLListBase::iterator::operator*() inline Foam::SLListBase::iterator& Foam::SLListBase::iterator::operator++() { - if (curElmt_ == curList_.last_ || curList_.last_ == 0) + if (curElmt_ == curList_.last_ || curList_.last_ == nullptr) { - curElmt_ = 0; + curElmt_ = nullptr; } else { @@ -255,13 +264,22 @@ inline Foam::SLListBase::const_iterator::const_iterator {} -inline Foam::SLListBase::const_iterator::const_iterator(const iterator& iter) +inline Foam::SLListBase::const_iterator::const_iterator +( + const SLListBase::iterator& iter +) : curList_(iter.curList_), curElmt_(iter.curElmt_) {} +inline bool Foam::SLListBase::const_iterator::found() const +{ + return (curElmt_ != nullptr); +} + + inline void Foam::SLListBase::const_iterator::operator= ( const const_iterator& iter @@ -290,7 +308,7 @@ inline bool Foam::SLListBase::const_iterator::operator!= inline const Foam::SLListBase::link& -Foam::SLListBase::const_iterator::operator*() +Foam::SLListBase::const_iterator::operator*() const { return *curElmt_; } @@ -301,7 +319,7 @@ Foam::SLListBase::const_iterator::operator++() { if (curElmt_ == curList_.last_) { - curElmt_ = 0; + curElmt_ = nullptr; } else { diff --git a/src/OpenFOAM/containers/Lists/DynamicList/DynamicList.H b/src/OpenFOAM/containers/Lists/DynamicList/DynamicList.H index be198f8ffabd8713e98672d3150afb11c09e2303..6c03173401e8566d01371df78e450a2d664ef060 100644 --- a/src/OpenFOAM/containers/Lists/DynamicList/DynamicList.H +++ b/src/OpenFOAM/containers/Lists/DynamicList/DynamicList.H @@ -3,7 +3,7 @@ \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | \\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation - \\/ M anipulation | Copyright (C) 2016 OpenCFD Ltd. + \\/ M anipulation | Copyright (C) 2016-2017 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -59,14 +59,14 @@ class DynamicList; template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv> Ostream& operator<< ( - Ostream&, - const DynamicList<T, SizeInc, SizeMult, SizeDiv>& + Ostream& os, + const DynamicList<T, SizeInc, SizeMult, SizeDiv>& lst ); template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv> Istream& operator>> ( - Istream&, - DynamicList<T, SizeInc, SizeMult, SizeDiv>& + Istream& is, + DynamicList<T, SizeInc, SizeMult, SizeDiv>& lst ); @@ -105,29 +105,37 @@ public: inline DynamicList(); //- Construct given size. - explicit inline DynamicList(const label); + explicit inline DynamicList(const label nElem); //- Construct with given size and value for all elements. - inline DynamicList(const label, const T&); + inline DynamicList(const label nElem, const T& a); //- Construct copy. - inline DynamicList(const DynamicList<T, SizeInc, SizeMult, SizeDiv>&); + inline DynamicList + ( + const DynamicList<T, SizeInc, SizeMult, SizeDiv>& lst + ); //- Construct from UList. Size set to UList size. // Also constructs from DynamicList with different sizing parameters. - explicit inline DynamicList(const UList<T>&); + explicit inline DynamicList(const UList<T>& lst); + + //- Construct given begin/end iterators. + // Uses std::distance to determine the size. + template<class InputIterator> + inline DynamicList(InputIterator begIter, InputIterator endIter); //- Construct from an initializer list. Size set to list size. - explicit inline DynamicList(std::initializer_list<T>); + explicit inline DynamicList(std::initializer_list<T> lst); //- Construct from UIndirectList. Size set to UIndirectList size. - explicit inline DynamicList(const UIndirectList<T>&); + explicit inline DynamicList(const UIndirectList<T>& lst); //- Construct by transferring the parameter contents - explicit inline DynamicList(const Xfer<List<T>>&); + explicit inline DynamicList(const Xfer<List<T>>& lst); //- Construct from Istream. Size set to size of list read. - explicit DynamicList(Istream&); + explicit DynamicList(Istream& is); // Member Functions @@ -143,31 +151,31 @@ public: // The addressed size will be truncated if needed to fit, but will // remain otherwise untouched. // Use this or reserve() in combination with append(). - inline void setCapacity(const label); + inline void setCapacity(const label nElem); //- Alter the addressed list size. // New space will be allocated if required. // Use this to resize the list prior to using the operator[] for // setting values (as per List usage). - inline void setSize(const label); + inline void setSize(const label nElem); //- Alter the addressed list size and fill new space with a // constant. - inline void setSize(const label, const T&); + inline void setSize(const label nElem, const T& t); //- Alter the addressed list size. // New space will be allocated if required. // Use this to resize the list prior to using the operator[] for // setting values (as per List usage). - inline void resize(const label); + inline void resize(const label nElem); //- Alter the addressed list size and fill new space with a // constant. - inline void resize(const label, const T&); + inline void resize(const label nElem, const T& t); //- Reserve allocation space for at least this size. // Never shrinks the allocated size, use setCapacity() for that. - inline void reserve(const label); + inline void reserve(const label nElem); //- Clear the addressed list, i.e. set the size to zero. // Allocated size does not change @@ -181,10 +189,13 @@ public: inline DynamicList<T, SizeInc, SizeMult, SizeDiv>& shrink(); //- Transfer contents of the argument List into this. - inline void transfer(List<T>&); + inline void transfer(List<T>& lst); //- Transfer contents of the argument DynamicList into this. - inline void transfer(DynamicList<T, SizeInc, SizeMult, SizeDiv>&); + inline void transfer + ( + DynamicList<T, SizeInc, SizeMult, SizeDiv>& lst + ); //- Transfer contents to the Xfer container as a plain List inline Xfer<List<T>> xfer(); @@ -195,25 +206,25 @@ public: //- Append an element at the end of the list inline DynamicList<T, SizeInc, SizeMult, SizeDiv>& append ( - const T& + const T& t ); //- Append a List at the end of this list inline DynamicList<T, SizeInc, SizeMult, SizeDiv>& append ( - const UList<T>& + const UList<T>& lst ); //- Append an initializer list at the end of this list. inline DynamicList<T, SizeInc, SizeMult, SizeDiv>& append ( - std::initializer_list<T> + std::initializer_list<T> lst ); //- Append a UIndirectList at the end of this list inline DynamicList<T, SizeInc, SizeMult, SizeDiv>& append ( - const UIndirectList<T>& + const UIndirectList<T>& lst ); //- Remove and return the top element @@ -221,32 +232,35 @@ public: //- Return non-const access to an element, resizing list if // necessary - inline T& operator()(const label); + inline T& operator()(const label elemI); //- Assignment of all addressed entries to the given value - inline void operator=(const T&); + inline void operator=(const T& t); //- Assignment to DynamicList inline void operator= ( - const DynamicList<T, SizeInc, SizeMult, SizeDiv>& + const DynamicList<T, SizeInc, SizeMult, SizeDiv>& lst ); //- Assignment to UList - inline void operator=(const UList<T>&); + inline void operator=(const UList<T>& lst); //- Assignment from initializer list - inline void operator=(std::initializer_list<T>); + inline void operator=(std::initializer_list<T> lst); //- Assignment to UIndirectList - inline void operator=(const UIndirectList<T>&); + inline void operator=(const UIndirectList<T>& lst); // STL member functions //- Erase an element, move the remaining elements to fill the gap // and resize the List - typename UList<T>::iterator erase(typename UList<T>::iterator); + typename UList<T>::iterator erase + ( + typename UList<T>::iterator curIter + ); // IOstream operators @@ -254,15 +268,15 @@ public: // Write DynamicList to Ostream. friend Ostream& operator<< <T, SizeInc, SizeMult, SizeDiv> ( - Ostream&, - const DynamicList<T, SizeInc, SizeMult, SizeDiv>& + Ostream& os, + const DynamicList<T, SizeInc, SizeMult, SizeDiv>& lst ); //- Read from Istream, discarding contents of existing DynamicList. friend Istream& operator>> <T, SizeInc, SizeMult, SizeDiv> ( - Istream&, - DynamicList<T, SizeInc, SizeMult, SizeDiv>& + Istream& is, + DynamicList<T, SizeInc, SizeMult, SizeDiv>& lst ); }; diff --git a/src/OpenFOAM/containers/Lists/DynamicList/DynamicListI.H b/src/OpenFOAM/containers/Lists/DynamicList/DynamicListI.H index 82f28f5cb5552f284db7cf05882ef9dd905e6291..e6ba973b48af96f1b5e83c46215745805f1dc588 100644 --- a/src/OpenFOAM/containers/Lists/DynamicList/DynamicListI.H +++ b/src/OpenFOAM/containers/Lists/DynamicList/DynamicListI.H @@ -80,6 +80,19 @@ inline Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::DynamicList {} +template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv> +template<class InputIterator> +inline Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::DynamicList +( + InputIterator begIter, + InputIterator endIter +) +: + List<T>(begIter, endIter), + capacity_(this->size()) +{} + + template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv> inline Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::DynamicList ( diff --git a/src/OpenFOAM/containers/Lists/FixedList/FixedList.H b/src/OpenFOAM/containers/Lists/FixedList/FixedList.H index bdf75aa40101e8f61511b421a3318a09c0d88179..dce2cad0c63a23bd6c797d67d630fc31702584e6 100644 --- a/src/OpenFOAM/containers/Lists/FixedList/FixedList.H +++ b/src/OpenFOAM/containers/Lists/FixedList/FixedList.H @@ -128,11 +128,12 @@ public: explicit inline FixedList(const T& t); //- Construct from C-array - explicit inline FixedList(const T v[Size]); + explicit inline FixedList(const T lst[Size]); - //- Construct given start and end iterators + //- Construct given begin/end iterators + // Uses std::distance when verifying the size. template<class InputIterator> - inline FixedList(InputIterator first, InputIterator last); + inline FixedList(InputIterator begIter, InputIterator endIter); //- Construct from an initializer list inline FixedList(std::initializer_list<T> lst); diff --git a/src/OpenFOAM/containers/Lists/FixedList/FixedListI.H b/src/OpenFOAM/containers/Lists/FixedList/FixedListI.H index 2f97b931690bcc67f3bc597a635db4faeca3b6d1..1df4fb958a0ba6bf8d3c4c9b813e51072794e946 100644 --- a/src/OpenFOAM/containers/Lists/FixedList/FixedListI.H +++ b/src/OpenFOAM/containers/Lists/FixedList/FixedListI.H @@ -37,7 +37,7 @@ inline Foam::FixedList<T, Size>::FixedList() template<class T, unsigned Size> inline Foam::FixedList<T, Size>::FixedList(const T& t) { - for (unsigned i=0; i<Size; i++) + for (unsigned i=0; i<Size; ++i) { v_[i] = t; } @@ -45,11 +45,11 @@ inline Foam::FixedList<T, Size>::FixedList(const T& t) template<class T, unsigned Size> -inline Foam::FixedList<T, Size>::FixedList(const T v[Size]) +inline Foam::FixedList<T, Size>::FixedList(const T lst[Size]) { - for (unsigned i=0; i<Size; i++) + for (unsigned i=0; i<Size; ++i) { - v_[i] = v[i]; + v_[i] = lst[i]; } } @@ -58,25 +58,33 @@ template<class T, unsigned Size> template<class InputIterator> Foam::FixedList<T, Size>::FixedList ( - InputIterator first, - InputIterator last + InputIterator begIter, + InputIterator endIter ) { - checkSize(std::distance(first, last)); + checkSize(std::distance(begIter, endIter)); - InputIterator iter = first; - for (unsigned i=0; i<Size; i++) + InputIterator iter = begIter; + for (unsigned i=0; i<Size; ++i) { - v_[i] = *iter++; + v_[i] = *iter; + ++iter; } } template<class T, unsigned Size> inline Foam::FixedList<T, Size>::FixedList(std::initializer_list<T> lst) -: - FixedList<T, Size>(lst.begin(), lst.end()) -{} +{ + checkSize(lst.size()); + + auto iter = lst.begin(); + for (unsigned i=0; i<Size; ++i) + { + v_[i] = *iter; + ++iter; + } +} template<class T, unsigned Size> @@ -84,7 +92,7 @@ inline Foam::FixedList<T, Size>::FixedList(const UList<T>& lst) { checkSize(lst.size()); - for (unsigned i=0; i<Size; i++) + for (unsigned i=0; i<Size; ++i) { v_[i] = lst[i]; } @@ -97,9 +105,10 @@ inline Foam::FixedList<T, Size>::FixedList(const SLList<T>& lst) checkSize(lst.size()); typename SLList<T>::const_iterator iter = lst.begin(); - for (unsigned i=0; i<Size; i++) + for (unsigned i=0; i<Size; ++i) { - v_[i] = *iter++; + v_[i] = *iter; + ++iter; } } @@ -107,7 +116,7 @@ inline Foam::FixedList<T, Size>::FixedList(const SLList<T>& lst) template<class T, unsigned Size> inline Foam::FixedList<T, Size>::FixedList(const FixedList<T, Size>& lst) { - for (unsigned i=0; i<Size; i++) + for (unsigned i=0; i<Size; ++i) { v_[i] = lst[i]; } @@ -200,7 +209,7 @@ inline void Foam::FixedList<T, Size>::setSize(const label s) template<class T, unsigned Size> inline void Foam::FixedList<T, Size>::transfer(const FixedList<T, Size>& lst) { - for (unsigned i=0; i<Size; i++) + for (unsigned i=0; i<Size; ++i) { v_[i] = lst[i]; } @@ -276,7 +285,7 @@ inline const T& Foam::FixedList<T, Size>::operator[](const label i) const template<class T, unsigned Size> inline void Foam::FixedList<T, Size>::operator=(const T lst[Size]) { - for (unsigned i=0; i<Size; i++) + for (unsigned i=0; i<Size; ++i) { v_[i] = lst[i]; } @@ -287,7 +296,7 @@ inline void Foam::FixedList<T, Size>::operator=(const UList<T>& lst) { checkSize(lst.size()); - for (unsigned i=0; i<Size; i++) + for (unsigned i=0; i<Size; ++i) { v_[i] = lst[i]; } @@ -299,9 +308,10 @@ inline void Foam::FixedList<T, Size>::operator=(const SLList<T>& lst) checkSize(lst.size()); typename SLList<T>::const_iterator iter = lst.begin(); - for (unsigned i=0; i<Size; i++) + for (unsigned i=0; i<Size; ++i) { - v_[i] = *iter++; + v_[i] = *iter; + ++iter; } } @@ -310,17 +320,18 @@ inline void Foam::FixedList<T, Size>::operator=(std::initializer_list<T> lst) { checkSize(lst.size()); - typename std::initializer_list<T>::iterator iter = lst.begin(); - for (unsigned i=0; i<Size; i++) + auto iter = lst.begin(); + for (unsigned i=0; i<Size; ++i) { - v_[i] = *iter++; + v_[i] = *iter; + ++iter; } } template<class T, unsigned Size> inline void Foam::FixedList<T, Size>::operator=(const T& t) { - for (unsigned i=0; i<Size; i++) + for (unsigned i=0; i<Size; ++i) { v_[i] = t; } @@ -464,7 +475,7 @@ inline unsigned Foam::FixedList<T, Size>::Hash<HashT>::operator() // Hash incrementally unsigned val = seed; - for (unsigned i=0; i<Size; i++) + for (unsigned i=0; i<Size; ++i) { val = HashT()(lst[i], val); } diff --git a/src/OpenFOAM/containers/Lists/List/List.C b/src/OpenFOAM/containers/Lists/List/List.C index 0e4dee96ac722d7c05d9f0127181dcd3dd8abfb4..6ba01a2edc0033cd7af582c7217e664a41e55141 100644 --- a/src/OpenFOAM/containers/Lists/List/List.C +++ b/src/OpenFOAM/containers/Lists/List/List.C @@ -3,7 +3,7 @@ \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | \\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation - \\/ M anipulation | + \\/ M anipulation | Copyright (C) 2017 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -160,7 +160,7 @@ Foam::List<T>::List(List<T>& a, bool reuse) if (reuse) { this->v_ = a.v_; - a.v_ = 0; + a.v_ = nullptr; a.size_ = 0; } else if (this->size_) @@ -186,19 +186,19 @@ Foam::List<T>::List(List<T>& a, bool reuse) template<class T> -Foam::List<T>::List(const UList<T>& a, const labelUList& map) +Foam::List<T>::List(const UList<T>& a, const labelUList& mapAddressing) : - UList<T>(nullptr, map.size()) + UList<T>(nullptr, mapAddressing.size()) { if (this->size_) { - // Note:cannot use List_ELEM since third argument has to be index. + // Note: cannot use List_ELEM since third argument has to be index. alloc(); forAll(*this, i) { - this->operator[](i) = a[map[i]]; + this->operator[](i) = a[mapAddressing[i]]; } } } @@ -206,9 +206,9 @@ Foam::List<T>::List(const UList<T>& a, const labelUList& map) template<class T> template<class InputIterator> -Foam::List<T>::List(InputIterator first, InputIterator last) +Foam::List<T>::List(InputIterator begIter, InputIterator endIter) : - List<T>(first, last, std::distance(first, last)) + List<T>(begIter, endIter, std::distance(begIter, endIter)) {} @@ -231,6 +231,8 @@ Foam::List<T>::List(const PtrList<T>& lst) } +// Note: using first/last is not entirely accurate. +// But since the list size is correct, last() is actually ignored. template<class T> Foam::List<T>::List(const SLList<T>& lst) : @@ -259,7 +261,7 @@ Foam::List<T>::List(const BiIndirectList<T>& lst) template<class T> Foam::List<T>::List(std::initializer_list<T> lst) : - List<T>(lst.begin(), lst.end()) + List<T>(lst.begin(), lst.end(), lst.size()) {} @@ -326,7 +328,7 @@ void Foam::List<T>::setSize(const label newSize) template<class T> void Foam::List<T>::setSize(const label newSize, const T& a) { - label oldSize = label(this->size_); + const label oldSize = label(this->size_); this->setSize(newSize); if (newSize > oldSize) @@ -346,7 +348,7 @@ void Foam::List<T>::transfer(List<T>& a) this->v_ = a.v_; a.size_ = 0; - a.v_ = 0; + a.v_ = nullptr; } @@ -419,14 +421,9 @@ void Foam::List<T>::operator=(const SLList<T>& lst) if (this->size_) { label i = 0; - for - ( - typename SLList<T>::const_iterator iter = lst.begin(); - iter != lst.end(); - ++iter - ) + for (auto iter = lst.cbegin(); iter != lst.cend(); ++iter) { - this->operator[](i++) = iter(); + this->operator[](i++) = *iter; } } } @@ -453,10 +450,11 @@ void Foam::List<T>::operator=(std::initializer_list<T> lst) { reAlloc(lst.size()); - typename std::initializer_list<T>::iterator iter = lst.begin(); + auto iter = lst.begin(); forAll(*this, i) { - this->operator[](i) = *iter++; + this->operator[](i) = *iter; + ++iter; } } diff --git a/src/OpenFOAM/containers/Lists/List/List.H b/src/OpenFOAM/containers/Lists/List/List.H index fdb4559bf75ec8abed82d79bd914775871290cea..c8e3219d9928ad7eaf106a1bbe3c437b1ccf827f 100644 --- a/src/OpenFOAM/containers/Lists/List/List.H +++ b/src/OpenFOAM/containers/Lists/List/List.H @@ -58,7 +58,7 @@ class Ostream; // Forward declaration of friend functions and operators template<class T> class List; -template<class T> Istream& operator>>(Istream&, List<T>&); +template<class T> Istream& operator>>(Istream& is, List<T>& L); template<class T, unsigned Size> class FixedList; template<class T> class PtrList; @@ -95,22 +95,28 @@ class List //- Copy list of given type template<class List2> - inline void copyList(const List2&); + inline void copyList(const List2& lst); //- Allocate storage and copy list of given type template<class List2> - inline void allocCopyList(const List2&); + inline void allocCopyList(const List2& lst); - //- Construct given start and end iterators and number of elements + //- Construct given begin/end iterators and number of elements + // Since the size is provided, the end iterator is actually ignored. template<class InputIterator> - inline List(InputIterator first, InputIterator last, const label s); + inline List + ( + InputIterator begIter, + InputIterator endIter, + const label s + ); protected: //- Override size to be inconsistent with allocated storage. // Use with care - inline void size(const label); + inline void size(const label n); public: @@ -127,55 +133,56 @@ public: inline List(); //- Construct with given size - explicit List(const label); + explicit List(const label s); //- Construct with given size and value for all elements - List(const label, const T&); + List(const label s, const T& a); //- Construct with given size initializing all elements to zero - List(const label, const zero); + List(const label s, const zero); //- Copy constructor - List(const List<T>&); + List(const List<T>& a); //- Copy constructor from list containing another type template<class T2> - explicit List(const List<T2>&); + explicit List(const List<T2>& a); //- Construct by transferring the parameter contents - List(const Xfer<List<T>>&); + List(const Xfer<List<T>>& lst); //- Construct as copy or re-use as specified - List(List<T>&, bool reuse); + List(List<T>& a, bool reuse); //- Construct as subset - List(const UList<T>&, const labelUList& mapAddressing); + List(const UList<T>& a, const labelUList& mapAddressing); - //- Construct given start and end iterators + //- Construct given begin/end iterators. + // Uses std::distance to determine the size. template<class InputIterator> - List(InputIterator first, InputIterator last); + List(InputIterator begIter, InputIterator endIter); //- Construct as copy of FixedList<T, Size> template<unsigned Size> - explicit List(const FixedList<T, Size>&); + explicit List(const FixedList<T, Size>& lst); //- Construct as copy of PtrList<T> - explicit List(const PtrList<T>&); + explicit List(const PtrList<T>& lst); //- Construct as copy of SLList<T> - explicit List(const SLList<T>&); + explicit List(const SLList<T>& lst); //- Construct as copy of UIndirectList<T> - explicit List(const UIndirectList<T>&); + explicit List(const UIndirectList<T>& lst); //- Construct as copy of BiIndirectList<T> - explicit List(const BiIndirectList<T>&); + explicit List(const BiIndirectList<T>& lst); //- Construct from an initializer list - List(std::initializer_list<T>); + List(std::initializer_list<T> lst); //- Construct from Istream - List(Istream&); + List(Istream& is); //- Clone inline autoPtr<List<T>> clone() const; @@ -200,47 +207,48 @@ public: // Edit //- Alias for setSize(const label) - inline void resize(const label); + inline void resize(const label newSize); //- Alias for setSize(const label, const T&) - inline void resize(const label, const T&); + inline void resize(const label newSize, const T& a); //- Reset size of List - void setSize(const label); + void setSize(const label newSize); //- Reset size of List and value for new elements - void setSize(const label, const T&); + void setSize(const label newSize, const T& a); //- Clear the list, i.e. set size to zero inline void clear(); //- Append an element at the end of the list - inline void append(const T&); + inline void append(const T& t); //- Append a List at the end of this list - inline void append(const UList<T>&); + inline void append(const UList<T>& lst); //- Append a UIndirectList at the end of this list - inline void append(const UIndirectList<T>&); + inline void append(const UIndirectList<T>& lst); //- Transfer the contents of the argument List into this list // and annul the argument list - void transfer(List<T>&); + void transfer(List<T>& a); //- Transfer the contents of the argument List into this list // and annul the argument list template<unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv> - void transfer(DynamicList<T, SizeInc, SizeMult, SizeDiv>&); + void transfer(DynamicList<T, SizeInc, SizeMult, SizeDiv>& a); //- Transfer the contents of the argument List into this list // and annul the argument list - void transfer(SortableList<T>&); + void transfer(SortableList<T>& a); //- Transfer contents to the Xfer container inline Xfer<List<T>> xfer(); - //- Return subscript-checked element of UList - inline T& newElmt(const label); + //- Return subscript-checked element of UList. + // Resize list if required. + inline T& newElmt(const label i); //- Disallow implicit shallowCopy @@ -250,25 +258,25 @@ public: // Member operators //- Assignment to UList operator. Takes linear time - void operator=(const UList<T>&); + void operator=(const UList<T>& a); //- Assignment operator. Takes linear time - void operator=(const List<T>&); + void operator=(const List<T>& a); //- Assignment to SLList operator. Takes linear time - void operator=(const SLList<T>&); + void operator=(const SLList<T>& lst); //- Assignment to UIndirectList operator. Takes linear time - void operator=(const UIndirectList<T>&); + void operator=(const UIndirectList<T>& lst); //- Assignment to BiIndirectList operator. Takes linear time - void operator=(const BiIndirectList<T>&); + void operator=(const BiIndirectList<T>& lst); //- Assignment to an initializer list - void operator=(std::initializer_list<T>); + void operator=(std::initializer_list<T> lst); //- Assignment of all entries to the given value - inline void operator=(const T&); + inline void operator=(const T& t); //- Assignment of all entries to zero inline void operator=(const zero); @@ -278,7 +286,7 @@ public: //- Read List from Istream, discarding contents of existing List friend Istream& operator>> <T> - (Istream&, List<T>&); + (Istream& is, List<T>& L); }; @@ -290,7 +298,7 @@ public: // \endcode // Mostly useful for handling command-line arguments template<class T> -List<T> readList(Istream&); +List<T> readList(Istream& is); // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // diff --git a/src/OpenFOAM/containers/Lists/List/ListI.H b/src/OpenFOAM/containers/Lists/List/ListI.H index 5709c258ec7e2642aa1a478d36ab2f17fe8b5627..8aa5bc016aaad44c4cb7f46a2085aab832a2b5b0 100644 --- a/src/OpenFOAM/containers/Lists/List/ListI.H +++ b/src/OpenFOAM/containers/Lists/List/ListI.H @@ -3,7 +3,7 @@ \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | \\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation - \\/ M anipulation | + \\/ M anipulation | Copyright (C) 2017 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -77,8 +77,8 @@ template<class T> template<class InputIterator> inline Foam::List<T>::List ( - InputIterator first, - InputIterator last, + InputIterator begIter, + InputIterator endIter, const label s ) : @@ -88,10 +88,11 @@ inline Foam::List<T>::List { alloc(); - InputIterator iter = first; + InputIterator iter = begIter; forAll(*this, i) { - this->operator[](i) = *iter++; + this->operator[](i) = *iter; + ++iter; } } } @@ -126,7 +127,7 @@ inline void Foam::List<T>::clear() if (this->v_) { delete[] this->v_; - this->v_ = 0; + this->v_ = nullptr; } this->size_ = 0; diff --git a/src/OpenFOAM/containers/Lists/SortableList/SortableList.C b/src/OpenFOAM/containers/Lists/SortableList/SortableList.C index fbafc9cf0fb4731eb84e193908ed70246f8c9df1..a35334507aa58d979fa3263ea3ef1b4bbc3c0ef8 100644 --- a/src/OpenFOAM/containers/Lists/SortableList/SortableList.C +++ b/src/OpenFOAM/containers/Lists/SortableList/SortableList.C @@ -28,7 +28,7 @@ License // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // template<class T> -Foam::SortableList<T>::SortableList() +inline Foam::SortableList<T>::SortableList() {} @@ -51,14 +51,14 @@ Foam::SortableList<T>::SortableList(const Xfer<List<T>>& values) template<class T> -Foam::SortableList<T>::SortableList(const label size) +inline Foam::SortableList<T>::SortableList(const label size) : List<T>(size) {} template<class T> -Foam::SortableList<T>::SortableList(const label size, const T& val) +inline Foam::SortableList<T>::SortableList(const label size, const T& val) : List<T>(size, val) {} @@ -72,6 +72,20 @@ Foam::SortableList<T>::SortableList(const SortableList<T>& lst) {} +template<class T> +template<class InputIterator> +inline Foam::SortableList<T>::SortableList +( + InputIterator begIter, + InputIterator endIter +) +: + List<T>(begIter, endIter) +{ + sort(); +} + + template<class T> Foam::SortableList<T>::SortableList(std::initializer_list<T> values) : @@ -140,9 +154,9 @@ Foam::Xfer<Foam::List<T>> Foam::SortableList<T>::xfer() // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * // template<class T> -inline void Foam::SortableList<T>::operator=(const T& t) +inline void Foam::SortableList<T>::operator=(const T& val) { - UList<T>::operator=(t); + UList<T>::operator=(val); } diff --git a/src/OpenFOAM/containers/Lists/SortableList/SortableList.H b/src/OpenFOAM/containers/Lists/SortableList/SortableList.H index 65fb6dc7f41e700d6db90b1f542c9afe9242e5de..cd005a09c2f42e2905ab509642627a6de5bd8fab 100644 --- a/src/OpenFOAM/containers/Lists/SortableList/SortableList.H +++ b/src/OpenFOAM/containers/Lists/SortableList/SortableList.H @@ -65,27 +65,32 @@ public: // Constructors //- Null constructor, sort later (eg, after assignment or transfer) - SortableList(); + inline SortableList(); //- Construct from UList, sorting immediately - explicit SortableList(const UList<T>&); + explicit SortableList(const UList<T>& values); //- Construct from transferred List, sorting immediately - explicit SortableList(const Xfer<List<T>>&); + explicit SortableList(const Xfer<List<T>>& values); //- Construct given size. Sort later on // The indices remain empty until the list is sorted - explicit SortableList(const label size); + explicit inline SortableList(const label size); //- Construct given size and initial value. Sort later on // The indices remain empty until the list is sorted - SortableList(const label size, const T&); + inline SortableList(const label size, const T& val); + + //- Construct given begin/end iterators. + // Uses std::distance to determine the size. + template<class InputIterator> + inline SortableList(InputIterator begIter, InputIterator endIter); //- Construct as copy - SortableList(const SortableList<T>&); + inline SortableList(const SortableList<T>& lst); //- Construct from an initializer list, sorting immediately - SortableList(std::initializer_list<T>); + SortableList(std::initializer_list<T> values); // Member Functions @@ -122,16 +127,16 @@ public: // Member Operators //- Assignment of all entries to the given value - inline void operator=(const T&); + inline void operator=(const T& val); //- Assignment to UList operator. Takes linear time - inline void operator=(const UList<T>&); + inline void operator=(const UList<T>& lst); //- Assignment operator. Takes linear time - inline void operator=(const SortableList<T>&); + inline void operator=(const SortableList<T>& lst); //- Assignment to an initializer list - void operator=(std::initializer_list<T>); + void operator=(std::initializer_list<T> lst); }; diff --git a/src/OpenFOAM/db/IOobjectList/IOobjectList.C b/src/OpenFOAM/db/IOobjectList/IOobjectList.C index f58bab42140d96864c9a0eae8b0e1b2648e547a8..1f96009a528904e7205c13230f7b9831e6eb6349 100644 --- a/src/OpenFOAM/db/IOobjectList/IOobjectList.C +++ b/src/OpenFOAM/db/IOobjectList/IOobjectList.C @@ -91,9 +91,9 @@ Foam::IOobjectList::IOobjectList } -Foam::IOobjectList::IOobjectList(const IOobjectList& ioOL) +Foam::IOobjectList::IOobjectList(const IOobjectList& iolist) : - HashPtrTable<IOobject>(ioOL) + HashPtrTable<IOobject>(iolist) {} @@ -113,17 +113,7 @@ bool Foam::IOobjectList::add(IOobject& io) bool Foam::IOobjectList::remove(IOobject& io) { - HashPtrTable<IOobject>::iterator iter = - HashPtrTable<IOobject>::find(io.name()); - - if (iter != end()) - { - return erase(iter); - } - else - { - return false; - } + return erase(io.name()); } @@ -131,7 +121,7 @@ Foam::IOobject* Foam::IOobjectList::lookup(const word& name) const { HashPtrTable<IOobject>::const_iterator iter = find(name); - if (iter != end()) + if (iter.found()) { if (IOobject::debug) { @@ -152,68 +142,80 @@ Foam::IOobject* Foam::IOobjectList::lookup(const word& name) const } -Foam::IOobjectList Foam::IOobjectList::lookup(const wordRe& name) const +Foam::IOobjectList Foam::IOobjectList::lookup(const wordRe& matcher) const { - IOobjectList objectsOfName(size()); + IOobjectList results(size()); - forAllConstIter(HashPtrTable<IOobject>, *this, iter) + forAllConstIters(*this, iter) { - if (name.match(iter()->name())) + if (matcher.match(iter.key())) { if (IOobject::debug) { InfoInFunction << "Found " << iter.key() << endl; } - objectsOfName.insert(iter.key(), new IOobject(*iter())); + results.insert + ( + iter.key(), + new IOobject(*(iter.object())) + ); } } - return objectsOfName; + return results; } -Foam::IOobjectList Foam::IOobjectList::lookup(const wordReList& patterns) const +Foam::IOobjectList Foam::IOobjectList::lookup(const wordReList& matcher) const { - wordReListMatcher names(patterns); + wordReListMatcher mat(matcher); - IOobjectList objectsOfName(size()); + IOobjectList results(size()); - forAllConstIter(HashPtrTable<IOobject>, *this, iter) + forAllConstIters(*this, iter) { - if (names.match(iter()->name())) + if (mat.match(iter.key())) { if (IOobject::debug) { InfoInFunction << "Found " << iter.key() << endl; } - objectsOfName.insert(iter.key(), new IOobject(*iter())); + results.insert + ( + iter.key(), + new IOobject(*(iter.object())) + ); } } - return objectsOfName; + return results; } -Foam::IOobjectList Foam::IOobjectList::lookupClass(const word& ClassName) const +Foam::IOobjectList Foam::IOobjectList::lookupClass(const word& clsName) const { - IOobjectList objectsOfClass(size()); + IOobjectList results(size()); - forAllConstIter(HashPtrTable<IOobject>, *this, iter) + forAllConstIters(*this, iter) { - if (iter()->headerClassName() == ClassName) + if (iter()->headerClassName() == clsName) { if (IOobject::debug) { InfoInFunction << "Found " << iter.key() << endl; } - objectsOfClass.insert(iter.key(), new IOobject(*iter())); + results.insert + ( + iter.key(), + new IOobject(*(iter.object())) + ); } } - return objectsOfClass; + return results; } @@ -231,33 +233,33 @@ Foam::wordList Foam::IOobjectList::sortedNames() const Foam::wordList Foam::IOobjectList::names ( - const word& ClassName + const word& clsName ) const { - wordList objectNames(size()); + wordList objNames(size()); label count = 0; - forAllConstIter(HashPtrTable<IOobject>, *this, iter) + forAllConstIters(*this, iter) { - if (iter()->headerClassName() == ClassName) + if (iter()->headerClassName() == clsName) { - objectNames[count++] = iter.key(); + objNames[count++] = iter.key(); } } - objectNames.setSize(count); + objNames.setSize(count); - return objectNames; + return objNames; } Foam::wordList Foam::IOobjectList::names ( - const word& ClassName, + const word& clsName, const wordRe& matcher ) const { - wordList objNames = names(ClassName); + wordList objNames = names(clsName); return wordList(objNames, findStrings(matcher, objNames)); } @@ -265,11 +267,11 @@ Foam::wordList Foam::IOobjectList::names Foam::wordList Foam::IOobjectList::names ( - const word& ClassName, + const word& clsName, const wordReList& matcher ) const { - wordList objNames = names(ClassName); + wordList objNames = names(clsName); return wordList(objNames, findStrings(matcher, objNames)); } @@ -277,10 +279,10 @@ Foam::wordList Foam::IOobjectList::names Foam::wordList Foam::IOobjectList::sortedNames ( - const word& ClassName + const word& clsName ) const { - wordList sortedLst = names(ClassName); + wordList sortedLst = names(clsName); sort(sortedLst); return sortedLst; @@ -289,11 +291,11 @@ Foam::wordList Foam::IOobjectList::sortedNames Foam::wordList Foam::IOobjectList::sortedNames ( - const word& ClassName, + const word& clsName, const wordRe& matcher ) const { - wordList sortedLst = names(ClassName, matcher); + wordList sortedLst = names(clsName, matcher); sort(sortedLst); return sortedLst; @@ -302,11 +304,11 @@ Foam::wordList Foam::IOobjectList::sortedNames Foam::wordList Foam::IOobjectList::sortedNames ( - const word& ClassName, + const word& clsName, const wordReList& matcher ) const { - wordList sortedLst = names(ClassName, matcher); + wordList sortedLst = names(clsName, matcher); sort(sortedLst); return sortedLst; diff --git a/src/OpenFOAM/db/IOobjectList/IOobjectList.H b/src/OpenFOAM/db/IOobjectList/IOobjectList.H index 926135222fd8686869c881496c1537da01b13592..17f75233ed38e01385e752c6edbda806477f386f 100644 --- a/src/OpenFOAM/db/IOobjectList/IOobjectList.H +++ b/src/OpenFOAM/db/IOobjectList/IOobjectList.H @@ -77,7 +77,7 @@ public: ); //- Construct as copy - IOobjectList(const IOobjectList&); + IOobjectList(const IOobjectList& iolist); //- Destructor @@ -87,52 +87,56 @@ public: // Member functions //- Add an IOobject to the list - bool add(IOobject&); + bool add(IOobject& io); //- Remove an IOobject from the list - bool remove(IOobject&); + bool remove(IOobject& io); //- Lookup a given name and return IOobject ptr if found else nullptr IOobject* lookup(const word& name) const; - //- Return the list for all IOobects whose name matches name - IOobjectList lookup(const wordRe& name) const; + //- The list of all IOobects with matching names + IOobjectList lookup(const wordRe& matcher) const; - //- Return the list for all IOobects whose name matches name - IOobjectList lookup(const wordReList& patterns) const; + //- The list of all IOobjects with matching names + IOobjectList lookup(const wordReList& matcher) const; - //- Return the list for all IOobjects of a given class - IOobjectList lookupClass(const word& className) const; + //- The list of all IOobjects with the given class name + IOobjectList lookupClass(const word& clsName) const; //- A list of names of the IOobjects wordList names() const; - //- A list of names of IOobjects of the given class - wordList names(const word& className) const; + //- The names of IOobjects with the given class name + wordList names(const word& clsName) const; - //- A list of names of IOobjects of the given class, - // and that also satisfy the input matcher - wordList names(const word& className, const wordRe&) const; + //- The names of IOobjects with the given class name that also + // have a name satisfying the input matcher + wordList names(const word& clsName, const wordRe& matcher) const; - //- A list of names of IOobjects of the given class, - // and that also satisfy the input matchers - wordList names(const word& className, const wordReList&) const; + //- The names of IOobjects with the given class name that also + // have a name satisfying the input matcher + wordList names(const word& clsName, const wordReList& matcher) const; //- A sorted list of names of the IOobjects wordList sortedNames() const; - //- A sorted list of names of IOobjects of given class - wordList sortedNames(const word& className) const; + //- The sorted names of IOobjects with the given class name + wordList sortedNames(const word& clsName) const; - //- A sorted list of names of IOobjects of the given class, - // and that also satisfy the input matcher - wordList sortedNames(const word& className, const wordRe&) const; + //- The sorted names of IOobjects with the given class name that also + // have a name satisfying the input matcher + wordList sortedNames(const word& clsName, const wordRe& matcher) const; - //- A sorted list of names of IOobjects of the given class, - // and that also satisfy the input matchers - wordList sortedNames(const word& className, const wordReList&) const; + //- The sorted names of IOobjects with the given class name that also + // have a name satisfying the input matcher + wordList sortedNames + ( + const word& clsName, + const wordReList& matcher + ) const; }; diff --git a/src/OpenFOAM/db/dictionary/dictionary.C b/src/OpenFOAM/db/dictionary/dictionary.C index 8fb2751378dc7d0de6f7ec5e6fc4729d836ed321..c2ecd2292cba51e9450275939d5834f407ab7497 100644 --- a/src/OpenFOAM/db/dictionary/dictionary.C +++ b/src/OpenFOAM/db/dictionary/dictionary.C @@ -161,7 +161,7 @@ const Foam::entry* Foam::dictionary::lookupScopedSubEntryPtr bool Foam::dictionary::findInPatterns ( const bool patternMatch, - const word& Keyword, + const word& keyword, DLList<entry*>::const_iterator& wcLink, DLList<autoPtr<regExp>>::const_iterator& reLink ) const @@ -173,8 +173,8 @@ bool Foam::dictionary::findInPatterns if ( patternMatch - ? reLink()->match(Keyword) - : wcLink()->keyword() == Keyword + ? reLink()->match(keyword) + : wcLink()->keyword() == keyword ) { return true; @@ -192,7 +192,7 @@ bool Foam::dictionary::findInPatterns bool Foam::dictionary::findInPatterns ( const bool patternMatch, - const word& Keyword, + const word& keyword, DLList<entry*>::iterator& wcLink, DLList<autoPtr<regExp>>::iterator& reLink ) @@ -204,8 +204,8 @@ bool Foam::dictionary::findInPatterns if ( patternMatch - ? reLink()->match(Keyword) - : wcLink()->keyword() == Keyword + ? reLink()->match(keyword) + : wcLink()->keyword() == keyword ) { return true; @@ -951,11 +951,11 @@ void Foam::dictionary::set(const keyType& k, const dictionary& d) } -bool Foam::dictionary::remove(const word& Keyword) +bool Foam::dictionary::remove(const word& keyword) { - HashTable<entry*>::iterator iter = hashedEntries_.find(Keyword); + HashTable<entry*>::iterator iter = hashedEntries_.find(keyword); - if (iter != hashedEntries_.end()) + if (iter.found()) { // Delete from patterns first DLList<entry*>::iterator wcLink = @@ -964,7 +964,7 @@ bool Foam::dictionary::remove(const word& Keyword) patternRegexps_.begin(); // Find in pattern using exact match only - if (findInPatterns(false, Keyword, wcLink, reLink)) + if (findInPatterns(false, keyword, wcLink, reLink)) { patternEntries_.remove(wcLink); patternRegexps_.remove(reLink); diff --git a/src/OpenFOAM/db/dictionary/dictionary.H b/src/OpenFOAM/db/dictionary/dictionary.H index e00353b9867f0a8fc542c4dc461c8e505dd1419c..876be6ebba02dca07c3a0b800276cf7882590336 100644 --- a/src/OpenFOAM/db/dictionary/dictionary.H +++ b/src/OpenFOAM/db/dictionary/dictionary.H @@ -204,7 +204,7 @@ class dictionary bool findInPatterns ( const bool patternMatch, - const word& Keyword, + const word& keyword, DLList<entry*>::const_iterator& wcLink, DLList<autoPtr<regExp>>::const_iterator& reLink ) const; @@ -213,7 +213,7 @@ class dictionary bool findInPatterns ( const bool patternMatch, - const word& Keyword, + const word& keyword, DLList<entry*>::iterator& wcLink, DLList<autoPtr<regExp>>::iterator& reLink ); @@ -518,7 +518,7 @@ public: void set(const keyType& k, const T& t); //- Remove an entry specified by keyword - bool remove(const word& Keyword); + bool remove(const word& keyword); //- Change the keyword for an entry, // optionally forcing overwrite of an existing entry diff --git a/src/OpenFOAM/db/dictionary/entry/entry.H b/src/OpenFOAM/db/dictionary/entry/entry.H index c37809927a6509034f4ecc3cda14958441bf20b5..dcbe358f1843a0a3773ab923624d65e1d1b3302c 100644 --- a/src/OpenFOAM/db/dictionary/entry/entry.H +++ b/src/OpenFOAM/db/dictionary/entry/entry.H @@ -57,7 +57,7 @@ class dictionary; // Forward declaration of friend functions and operators class entry; -Ostream& operator<<(Ostream&, const entry&); +Ostream& operator<<(Ostream& os, const entry& e); /*---------------------------------------------------------------------------*\ Class entry Declaration @@ -75,11 +75,16 @@ class entry // Private Member Functions - //- Get the next valid keyword. Return true if is valid keyType. - static bool getKeyword(keyType&, token&, Istream&); + //- Get the next valid keyword. Return true if a valid keyType. + static bool getKeyword + ( + keyType& keyword, + token& keywordToken, + Istream& is + ); //- Get the next valid keyword otherwise return false - static bool getKeyword(keyType&, Istream&); + static bool getKeyword(keyType& keyword, Istream& is); public: @@ -90,10 +95,10 @@ public: // Constructors //- Construct from keyword - entry(const keyType&); + entry(const keyType& keyword); //- Construct as copy - entry(const entry&); + entry(const entry& e); //- Construct on freestore as copy with reference to the // dictionary the copy belongs to @@ -107,7 +112,7 @@ public: virtual autoPtr<entry> clone() const; //- Construct from Istream and insert into dictionary - static bool New(dictionary& parentDict, Istream&); + static bool New(dictionary& parentDict, Istream& is); //- Construct on freestore from Istream and return static autoPtr<entry> New(Istream& is); @@ -179,7 +184,7 @@ public: // Ostream operator - friend Ostream& operator<<(Ostream&, const entry&); + friend Ostream& operator<<(Ostream& os, const entry& e); }; diff --git a/src/OpenFOAM/db/dictionary/entry/entryIO.C b/src/OpenFOAM/db/dictionary/entry/entryIO.C index 4f7b3e4f0fd318d7ecd6697968d93a4fda6ccad6..3d554bd619afa429ec0fdeecdcad83b9504abf66 100644 --- a/src/OpenFOAM/db/dictionary/entry/entryIO.C +++ b/src/OpenFOAM/db/dictionary/entry/entryIO.C @@ -106,7 +106,7 @@ bool Foam::entry::getKeyword(keyType& keyword, Istream& is) bool Foam::entry::New(dictionary& parentDict, Istream& is) { - is.fatalCheck("entry::New(const dictionary& parentDict, Istream&)"); + is.fatalCheck(FUNCTION_NAME); keyType keyword; token keyToken; @@ -324,7 +324,7 @@ bool Foam::entry::New(dictionary& parentDict, Istream& is) Foam::autoPtr<Foam::entry> Foam::entry::New(Istream& is) { - is.fatalCheck("entry::New(Istream&)"); + is.fatalCheck(FUNCTION_NAME); keyType keyword; diff --git a/src/OpenFOAM/primitives/ranges/labelRange/labelRange.C b/src/OpenFOAM/primitives/ranges/labelRange/labelRange.C index b1033ac17279b4bf00810ac80b6ede0fe51e63aa..38349e92b2f0892a9ea65e85d7ecf097666b9a29 100644 --- a/src/OpenFOAM/primitives/ranges/labelRange/labelRange.C +++ b/src/OpenFOAM/primitives/ranges/labelRange/labelRange.C @@ -34,6 +34,8 @@ namespace Foam int labelRange::debug(debug::debugSwitch("labelRange", 0)); } +const Foam::labelRange Foam::labelRange::null; + // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // diff --git a/src/OpenFOAM/primitives/ranges/labelRange/labelRange.H b/src/OpenFOAM/primitives/ranges/labelRange/labelRange.H index c4eb8488a62b143cea7ba469ec637804892f35be..dc3483c109618bcf06dde89c95b78912b65eaeff 100644 --- a/src/OpenFOAM/primitives/ranges/labelRange/labelRange.H +++ b/src/OpenFOAM/primitives/ranges/labelRange/labelRange.H @@ -35,6 +35,7 @@ SourceFiles #define labelRange_H #include "label.H" +#include <iterator> // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // @@ -63,26 +64,32 @@ class labelRange public: - static int debug; + // Static Data Members + + static int debug; + + //- An empty range with start=0, size=0. + static const labelRange null; + // STL type definitions similar to what UList has //- Type of values the range contains typedef label value_type; - //- The type that can represent the difference between two iterators - typedef label difference_type; - //- The type that can represent the size of the range typedef label size_type; + // Forward declaration + class const_iterator; + // Constructors //- An empty range with zero for start/size. inline labelRange(); - //- Construct a range from start and size, enforcing non-negative size. + //- Construct a range from start/size, enforcing non-negative size. // Optionally adjust the start to avoid any negative indices. inline labelRange ( @@ -95,6 +102,12 @@ public: labelRange(Istream& is); + // Static Member Functions + + //- An identity range with range[i] == i. + inline static labelRange identity(const label len); + + // Member Functions //- Adjust start position @@ -184,6 +197,9 @@ public: //- Return element in the range, no bounds checking inline label operator[](const label localIndex) const; + //- Return const_iterator to element in the range + inline const_iterator operator()(const label localIndex) const; + //- Increase the size by 1. inline label operator++(); inline label operator++(int); @@ -197,21 +213,30 @@ public: //- Forward iterator with const access class const_iterator + : + public std::iterator + < + std::input_iterator_tag, + label, + label, + const label*, + const label& + > { - //- The current label (not the local index) - label index_; + //- The current (global) value + label value_; public: // Constructors //- Construct from range at given local index. - // A negative index signals the 'end' position - inline const_iterator(const labelRange* range, const label i = 0); + // A negative index is invalid and corresponds to the 'end' + inline const_iterator(const labelRange* range, const label i=0); // Member operators - //- Return the current label + //- Return the current (global) value inline label operator*() const; inline const_iterator& operator++(); diff --git a/src/OpenFOAM/primitives/ranges/labelRange/labelRangeI.H b/src/OpenFOAM/primitives/ranges/labelRange/labelRangeI.H index 0563c1cd22f100df318029dbadff834d11e3d406..dc0e90ae85577ebe492a465e45e9622a4c20d3a8 100644 --- a/src/OpenFOAM/primitives/ranges/labelRange/labelRangeI.H +++ b/src/OpenFOAM/primitives/ranges/labelRange/labelRangeI.H @@ -64,7 +64,7 @@ inline Foam::labelRange::const_iterator::const_iterator const label i ) : - index_ + value_ ( range->start() + ((i < 0 || i > range->size()) ? range->size() : i) @@ -74,14 +74,14 @@ inline Foam::labelRange::const_iterator::const_iterator inline Foam::label Foam::labelRange::const_iterator::operator*() const { - return index_; + return value_; } inline Foam::labelRange::const_iterator& Foam::labelRange::const_iterator::operator++() { - ++index_; + ++value_; return *this; } @@ -90,7 +90,7 @@ inline Foam::labelRange::const_iterator Foam::labelRange::const_iterator::operator++(int) { const_iterator old = *this; - ++index_; + ++value_; return old; } @@ -100,7 +100,7 @@ inline bool Foam::labelRange::const_iterator::operator== const const_iterator& iter ) const { - return (this->index_ == iter.index_); + return (this->value_ == iter.value_); } @@ -109,7 +109,7 @@ inline bool Foam::labelRange::const_iterator::operator!= const const_iterator& iter ) const { - return (this->index_ != iter.index_); + return (this->value_ != iter.value_); } @@ -139,6 +139,12 @@ inline const Foam::labelRange::const_iterator Foam::labelRange::cend() const // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // +inline Foam::labelRange Foam::labelRange::identity(const label len) +{ + return labelRange(0, len); +} + + inline void Foam::labelRange::setStart(const label i) { start_ = i; @@ -264,6 +270,13 @@ inline Foam::label Foam::labelRange::operator[](const label localIndex) const } +inline Foam::labelRange::const_iterator +Foam::labelRange::operator()(const label localIndex) const +{ + return const_iterator(this, localIndex); +} + + inline Foam::label Foam::labelRange::operator++() { return ++size_;