diff --git a/applications/test/DynamicList/Test-DynamicList.C b/applications/test/DynamicList/Test-DynamicList.C index 7b73a214c93f337aa10f5810af01ea1239dd90a1..c8e2ed4a93be0d6b8b2a0b713463e2189da48ea9 100644 --- a/applications/test/DynamicList/Test-DynamicList.C +++ b/applications/test/DynamicList/Test-DynamicList.C @@ -111,13 +111,15 @@ int main(int argc, char *argv[]) // test the transfer between DynamicLists - DynamicList<label, 1, 0> dlA; + DynamicList<label, 1, 0> dlA + { + 0, 1, 2, 3, 4 + }; + dlA.append({ 5, 6 }); + dlA = { 1, 2, 4 }; + DynamicList<label, 1, 0> dlB; - for (label i = 0; i < 5; i++) - { - dlA.append(i); - } dlA.setCapacity(10); Info<< "<dlA>" << dlA << "</dlA>" << nl << "sizes: " diff --git a/applications/test/HashSet/Test-hashSet.C b/applications/test/HashSet/Test-hashSet.C index 0d22207c2f1ee54aba9875b49826a4a9046ca09a..bc219e9b968e88b15742a439a0dc0d3ad2c30fef 100644 --- a/applications/test/HashSet/Test-hashSet.C +++ b/applications/test/HashSet/Test-hashSet.C @@ -25,6 +25,7 @@ Description \*---------------------------------------------------------------------------*/ +#include "hashedWordList.H" #include "HashSet.H" #include "Map.H" @@ -35,31 +36,57 @@ using namespace Foam; int main(int argc, char *argv[]) { - wordHashSet setA(0); - HashTable<label, word> tableA; + hashedWordList words + { + "abc", + "def", + "ghi" + }; + words = { "def", "ghi", "xy", "all", "begin", "all" }; - HashTable<nil> tableB; - Map<label> mapA; + wordHashSet setA + { + "xx", + "yy", + "zz" + }; - setA.insert("kjhk"); - setA.insert("kjhk2"); + setA = { "kjhk", "kjhk2", "abced" }; - tableA.insert("value1", 1); - tableA.insert("value2", 2); - tableA.insert("value3", 3); + HashTable<label, word> tableA + { + { "value1", 1 }, + { "value2", 2 }, + { "value3", 3 } + }; + HashTable<nil> tableB; tableB.insert("value4", nil()); tableB.insert("value5", nil()); tableB.insert("value6", nil()); - mapA.set(1, 1); - mapA.set(2, 2); - mapA.set(3, 3); + Map<label> mapA + { + { 1, 1 }, + { 2, 2 }, + { 3, 3 } + }; mapA.set(4, 4); - Info<< setA << endl; - Info<< tableA << endl; - Info<< mapA << endl; + Info<< "hashedWordList: " << words << nl + << "with lookup: " << words.lookup() << endl; + + words.sort(); + Info<< "hashedWordList: " << words << nl + << "with lookup: " << words.lookup() << endl; + + words.uniq(); + Info<< "hashedWordList: " << words << nl + << "with lookup: " << words.lookup() << endl; + + Info<< "wordHashSet: " << setA << endl; + Info<< "Table-HashSet: " << tableA << endl; + Info<< "Map<label>: " << mapA << endl; Info<< "create from HashSet: "; Info<< wordHashSet(setA) << endl; @@ -76,9 +103,10 @@ int main(int argc, char *argv[]) << nl; - labelHashSet setB(1); - setB.insert(11); - setB.insert(42); + labelHashSet setB + { + 1, 11, 42 + }; Info<< "setB : " << setB << endl; @@ -89,11 +117,7 @@ int main(int argc, char *argv[]) Info<< "setC : " << setC << endl; labelHashSet setD(1); - setD.insert(11); - setD.insert(100); - setD.insert(49); - setD.insert(36); - setD.insert(2008); + setD.insert({11, 100, 49, 36, 2008}); Info<< "setD : " << setD << endl; @@ -131,7 +155,7 @@ int main(int argc, char *argv[]) } else { - Info<< "setD has no 0" << endl; + Info<< "setD has no 11" << endl; } Info<< "setD : " << setD << endl; diff --git a/src/OpenFOAM/containers/HashTables/HashSet/HashSet.C b/src/OpenFOAM/containers/HashTables/HashSet/HashSet.C index 08dc33d3cfc8eaa4f096a0441a0d907f8032dba9..75bea5ce59211370f38d3fa4ea24839f2e2f695a 100644 --- a/src/OpenFOAM/containers/HashTables/HashSet/HashSet.C +++ b/src/OpenFOAM/containers/HashTables/HashSet/HashSet.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) 2016 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -42,6 +42,18 @@ Foam::HashSet<Key, Hash>::HashSet(const UList<Key>& lst) } +template<class Key, class Hash> +Foam::HashSet<Key, Hash>::HashSet(std::initializer_list<Key> lst) +: + HashTable<nil, Key, Hash>(2*lst.size()) +{ + for (const Key& k : lst) + { + this->insert(k); + } +} + + template<class Key, class Hash> template<class AnyType, class AnyHash> Foam::HashSet<Key, Hash>::HashSet @@ -81,6 +93,21 @@ Foam::label Foam::HashSet<Key, Hash>::insert(const UList<Key>& lst) return count; } +template<class Key, class Hash> +Foam::label Foam::HashSet<Key, Hash>::insert(std::initializer_list<Key> lst) +{ + label count = 0; + for (const Key& k : lst) + { + if (this->insert(k)) + { + ++count; + } + } + + return count; +} + // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * // diff --git a/src/OpenFOAM/containers/HashTables/HashSet/HashSet.H b/src/OpenFOAM/containers/HashTables/HashSet/HashSet.H index bbc04789bde89475ac1a64c6870fb3c25158b30c..3328ac65b2e6551341495540620963212cd90469 100644 --- a/src/OpenFOAM/containers/HashTables/HashSet/HashSet.H +++ b/src/OpenFOAM/containers/HashTables/HashSet/HashSet.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) 2016 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -85,6 +85,9 @@ public: //- Construct from UList of Key HashSet(const UList<Key>&); + //- Construct from an initializer list of Key + HashSet(std::initializer_list<Key>); + //- Construct as copy HashSet(const HashSet<Key, Hash>& hs) : @@ -121,7 +124,11 @@ public: //- Insert keys from a UList of Key // Return the number of new elements inserted - label insert(const UList<Key>&); + label insert(const UList<Key>& lst); + + //- Insert keys from a initializer list of Key + // Return the number of new elements inserted + label insert(std::initializer_list<Key> lst); //- Same as insert (cannot overwrite nil content) bool set(const Key& key) @@ -135,6 +142,12 @@ public: return insert(lst); } + //- Same as insert (cannot overwrite nil content) + label set(std::initializer_list<Key> lst) + { + return insert(lst); + } + //- Unset the specified key - same as erase bool unset(const Key& key) { diff --git a/src/OpenFOAM/containers/Lists/DynamicList/DynamicList.H b/src/OpenFOAM/containers/Lists/DynamicList/DynamicList.H index 6d85a609b12e1ff94dd732cab829df79b0d53d21..be198f8ffabd8713e98672d3150afb11c09e2303 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 | + \\/ M anipulation | Copyright (C) 2016 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -117,6 +117,9 @@ public: // Also constructs from DynamicList with different sizing parameters. explicit inline DynamicList(const UList<T>&); + //- Construct from an initializer list. Size set to list size. + explicit inline DynamicList(std::initializer_list<T>); + //- Construct from UIndirectList. Size set to UIndirectList size. explicit inline DynamicList(const UIndirectList<T>&); @@ -201,6 +204,12 @@ public: const UList<T>& ); + //- Append an initializer list at the end of this list. + inline DynamicList<T, SizeInc, SizeMult, SizeDiv>& append + ( + std::initializer_list<T> + ); + //- Append a UIndirectList at the end of this list inline DynamicList<T, SizeInc, SizeMult, SizeDiv>& append ( @@ -226,6 +235,9 @@ public: //- Assignment to UList inline void operator=(const UList<T>&); + //- Assignment from initializer list + inline void operator=(std::initializer_list<T>); + //- Assignment to UIndirectList inline void operator=(const UIndirectList<T>&); diff --git a/src/OpenFOAM/containers/Lists/DynamicList/DynamicListI.H b/src/OpenFOAM/containers/Lists/DynamicList/DynamicListI.H index 97a0b724830ab5c89dfb49fcd815da9cb5aee5a8..82f28f5cb5552f284db7cf05882ef9dd905e6291 100644 --- a/src/OpenFOAM/containers/Lists/DynamicList/DynamicListI.H +++ b/src/OpenFOAM/containers/Lists/DynamicList/DynamicListI.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) 2016 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -80,6 +80,17 @@ inline Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::DynamicList {} +template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv> +inline Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::DynamicList +( + std::initializer_list<T> lst +) +: + List<T>(lst), + capacity_(lst.size()) +{} + + template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv> inline Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::DynamicList ( @@ -325,6 +336,24 @@ Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::append } +template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv> +inline Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>& +Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::append +( + std::initializer_list<T> lst +) +{ + label nextFree = List<T>::size(); + setSize(nextFree + lst.size()); + + for (const T& val : lst) + { + this->operator[](nextFree++) = val; + } + return *this; +} + + template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv> inline Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>& Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::append @@ -441,6 +470,29 @@ inline void Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::operator= } +template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv> +inline void Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::operator= +( + std::initializer_list<T> lst +) +{ + if (capacity_ >= lst.size()) + { + // Can copy w/o reallocating, match initial size to avoid reallocation + List<T>::size(lst.size()); + List<T>::operator=(lst); + } + else + { + // Make everything available for the copy operation + List<T>::size(capacity_); + + List<T>::operator=(lst); + capacity_ = List<T>::size(); + } +} + + template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv> inline void Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::operator= ( diff --git a/src/OpenFOAM/primitives/strings/lists/hashedWordList.C b/src/OpenFOAM/primitives/strings/lists/hashedWordList.C index 4a7cde89edaa8eb0ca4926f1a94e1eacb27a8a71..ea744b0036cc752d8ba49da5cb896f7ea3cb0b54 100644 --- a/src/OpenFOAM/primitives/strings/lists/hashedWordList.C +++ b/src/OpenFOAM/primitives/strings/lists/hashedWordList.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) 2016 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -69,6 +69,14 @@ Foam::hashedWordList::hashedWordList(const Xfer<List<word>>& names) } +Foam::hashedWordList::hashedWordList(std::initializer_list<word> lst) +: + List<word>(lst) +{ + rehash(); +} + + Foam::hashedWordList::hashedWordList ( const label nNames, @@ -138,6 +146,40 @@ void Foam::hashedWordList::transfer(List<word>& lst) } +void Foam::hashedWordList::sort() +{ + Foam::sort(*this); + rehash(); +} + + +void Foam::hashedWordList::uniq() +{ + if (size() != indices_.size()) + { + // sizes don't match, which means there appear to be duplicates + + indices_.clear(); + label nElem = 0; + forAll(*this, i) + { + const word& item = List<word>::operator[](i); + + if (indices_.insert(item, nElem)) + { + if (nElem != i) + { + List<word>::operator[](nElem) = item; + } + ++nElem; + } + } + + List<word>::setSize(nElem); + } +} + + // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * // Foam::Istream& Foam::operator>>(Istream& is, hashedWordList& lst) diff --git a/src/OpenFOAM/primitives/strings/lists/hashedWordList.H b/src/OpenFOAM/primitives/strings/lists/hashedWordList.H index b906f42f18d371932184167b96745c47539fd3a3..d36b9721b200d214954e18d6a3898d1222ae9cf9 100644 --- a/src/OpenFOAM/primitives/strings/lists/hashedWordList.H +++ b/src/OpenFOAM/primitives/strings/lists/hashedWordList.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) 2016 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -61,7 +61,8 @@ class hashedWordList { // Private data - HashTable<label, word> indices_; + //- Hash of words/indices + HashTable<label,word> indices_; // Private Member Functions @@ -79,11 +80,14 @@ public: //- Copy constructor. hashedWordList(const hashedWordList&); - //- Construct from list of names - hashedWordList(const UList<word>& names); + //- Construct from list of words + hashedWordList(const UList<word>&); + + //- Construct from an initializer list + hashedWordList(std::initializer_list<word>); //- Construct by transferring the parameter contents - hashedWordList(const Xfer<List<word>>& names); + hashedWordList(const Xfer<List<word>>&); //- Construct from number and list of names hashedWordList(const label nNames, const char** names); @@ -109,15 +113,27 @@ public: //- Does the list contain the specified name inline bool contains(const word&) const; + //- Return the hash of words/indices for inspection + inline const HashTable<label,word>& lookup() const; + //- Transfer the contents of the argument List into this list // and annul the argument list. void transfer(List<word>&); + //- Sort the list and rehash the indices + void sort(); + + //- Adjust the list if necessary to eliminate duplicate entries + void uniq(); + // Member Operators - //- Assignment operator from list of names - inline void operator=(const UList<word>& names); + //- Assignment operator from list of words + inline void operator=(const UList<word>&); + + //- Assignment operator from initializer list + inline void operator=(std::initializer_list<word>); //- Assignment operator. inline void operator=(const hashedWordList&); diff --git a/src/OpenFOAM/primitives/strings/lists/hashedWordListI.H b/src/OpenFOAM/primitives/strings/lists/hashedWordListI.H index 45a83ccd357a959d402a8cdc9e310b681e8b29ff..9dc7e5790d97f05a9b1746b2456d16eba53824fd 100644 --- a/src/OpenFOAM/primitives/strings/lists/hashedWordListI.H +++ b/src/OpenFOAM/primitives/strings/lists/hashedWordListI.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) 2016 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -25,6 +25,13 @@ License // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // +inline const Foam::HashTable<Foam::label,Foam::word>& +Foam::hashedWordList::lookup() const +{ + return indices_; +} + + inline bool Foam::hashedWordList::found(const word& name) const { return indices_.found(name); @@ -46,6 +53,13 @@ inline void Foam::hashedWordList::operator=(const UList<word>& lst) } +inline void Foam::hashedWordList::operator=(std::initializer_list<word> lst) +{ + List<word>::operator=(lst); + rehash(); +} + + inline void Foam::hashedWordList::operator=(const hashedWordList& lst) { operator=(static_cast<const UList<word>&>(lst));