ENH: Support more C++11 initializer lists (issue #261)
DynamicList ----------- - construction, assignment and append HashSet ------- - construction, insert, set. - assignment will use the implicit List constructor hashedWordList -------------- - construction, assignment - additional sort() and uniq() methods. - Readonly access to HashTable information via lookup() method. - NB: could avoid 'const char**' constructors in the future
This commit is contained in:
parent
749d68f888
commit
1967fd3dad
@ -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: "
|
||||
|
@ -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" };
|
||||
|
||||
wordHashSet setA
|
||||
{
|
||||
"xx",
|
||||
"yy",
|
||||
"zz"
|
||||
};
|
||||
|
||||
setA = { "kjhk", "kjhk2", "abced" };
|
||||
|
||||
HashTable<label, word> tableA
|
||||
{
|
||||
{ "value1", 1 },
|
||||
{ "value2", 2 },
|
||||
{ "value3", 3 }
|
||||
};
|
||||
|
||||
HashTable<nil> tableB;
|
||||
Map<label> mapA;
|
||||
|
||||
setA.insert("kjhk");
|
||||
setA.insert("kjhk2");
|
||||
|
||||
tableA.insert("value1", 1);
|
||||
tableA.insert("value2", 2);
|
||||
tableA.insert("value3", 3);
|
||||
|
||||
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;
|
||||
|
@ -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 * * * * * * * * * * * * * //
|
||||
|
||||
|
@ -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)
|
||||
{
|
||||
|
@ -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>&);
|
||||
|
||||
|
@ -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=
|
||||
(
|
||||
|
@ -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)
|
||||
|
@ -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&);
|
||||
|
@ -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));
|
||||
|
Loading…
Reference in New Issue
Block a user