Commit 09efce91 authored by Mark Olesen's avatar Mark Olesen
Browse files

STYLE: make null constructed lists constexpr, noexcept

- can assist the compiler in producing tighter code.
parent 26ec3702
......@@ -115,7 +115,7 @@ public:
// Constructors
//- Construct null
inline DynamicList();
inline constexpr DynamicList() noexcept;
//- Construct an empty list with given reserve size.
explicit inline DynamicList(const label nElem);
......
......@@ -56,7 +56,7 @@ inline void Foam::DynamicList<T, SizeMin>::assignDynList
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
template<class T, int SizeMin>
inline Foam::DynamicList<T, SizeMin>::DynamicList()
inline constexpr Foam::DynamicList<T, SizeMin>::DynamicList() noexcept
:
capacity_(0)
{}
......
......@@ -37,11 +37,11 @@ Foam::label Foam::FixedList<T, Size>::find
{
if (start >= 0)
{
List_CONST_ACCESS(T, *this, lst);
List_CONST_ACCESS(T, *this, list);
for (label i = start; i < label(Size); ++i)
{
if (lst[i] == val)
if (list[i] == val)
{
return i;
}
......
......@@ -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) 2017 OpenCFD Ltd.
\\/ M anipulation | Copyright (C) 2017-2018 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -40,14 +40,16 @@ SourceFiles
#include "bool.H"
#include "label.H"
#include "uLabel.H"
#include "zero.H"
#include "Hash.H"
#include "autoPtr.H"
#include "Swap.H"
#include "SLListFwd.H"
#include <type_traits>
#include <initializer_list>
#include <iterator>
#include <type_traits>
#include <limits>
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
......@@ -76,7 +78,7 @@ class FixedList
{
static_assert
(
Size && Size <= INT_MAX,
Size && Size <= std::numeric_limits<int>::max(),
"Size must be positive (non-zero) and also fit as a signed value"
);
......@@ -90,10 +92,13 @@ protected:
// Protected Member Functions
//- True if there are two or more entries and all entries have
// identical values.
inline bool uniform() const;
//- Write the FixedList with its compound type
void writeEntry(Ostream& os) const;
public:
// STL type definitions
......@@ -139,7 +144,7 @@ public:
{
inline unsigned operator()
(
const FixedList<T, Size>& lst,
const FixedList<T, Size>& list,
unsigned seed = 0
) const;
};
......@@ -156,18 +161,21 @@ public:
//- Null constructor
inline FixedList() = default;
//- Construct from value
//- Construct and initialize all entries to given value
explicit inline FixedList(const T& val);
//- Construct and initialize all entries to zero
explicit inline FixedList(const zero);
//- Copy construct from C-array
explicit inline FixedList(const T lst[Size]);
explicit inline FixedList(const T list[Size]);
//- Copy constructor
inline FixedList(const FixedList<T, Size>& lst);
inline FixedList(const FixedList<T, Size>& list);
//- Move construct by using move assignment for the individual
//- list elements
inline FixedList(FixedList<T, Size>&& lst);
inline FixedList(FixedList<T, Size>&& list);
//- Construct given begin/end iterators
// Uses std::distance when verifying the size.
......@@ -175,13 +183,13 @@ public:
inline FixedList(InputIterator begIter, InputIterator endIter);
//- Construct from an initializer list
inline FixedList(std::initializer_list<T> lst);
inline FixedList(std::initializer_list<T> list);
//- Construct from UList
explicit inline FixedList(const UList<T>& lst);
explicit inline FixedList(const UList<T>& list);
//- Construct from SLList
explicit inline FixedList(const SLList<T>& lst);
explicit inline FixedList(const SLList<T>& list);
//- Construct from Istream
FixedList(Istream& is);
......@@ -250,7 +258,7 @@ public:
inline void checkIndex(const label i) const;
// Search
// Search
//- Find index of the first occurence of the value.
// Linear search.
......@@ -261,7 +269,7 @@ public:
inline bool found(const T& val, const label start=0) const;
// Edit
// Edit
//- Dummy resize function
// needed to make FixedList consistent with List
......@@ -285,7 +293,7 @@ public:
//- Transfer by swapping using a move assignment for the content
//- of the individual list elements
inline void transfer(FixedList<T, Size>& lst);
inline void transfer(FixedList<T, Size>& list);
// Member operators
......@@ -297,25 +305,25 @@ public:
inline const T& operator[](const label i) const;
//- Assignment to array operator. Takes linear time
inline void operator=(const T lst[Size]);
inline void operator=(const T list[Size]);
//- Assignment to UList operator. Takes linear time
inline void operator=(const UList<T>& lst);
inline void operator=(const UList<T>& list);
//- Assignment to SLList operator. Takes linear time
inline void operator=(const SLList<T>& lst);
inline void operator=(const SLList<T>& list);
//- Assignment to an initializer list. Takes linear time
inline void operator=(std::initializer_list<T> lst);
inline void operator=(std::initializer_list<T> list);
//- Assignment of all entries to the given value
inline void operator=(const T& val);
//- Copy assignment
inline void operator=(const FixedList<T, Size>& lst);
inline void operator=(const FixedList<T, Size>& list);
//- Move assignment
inline void operator=(FixedList<T, Size>&& lst);
inline void operator=(FixedList<T, Size>&& list);
// Random access iterator (non-const)
......@@ -378,7 +386,7 @@ public:
inline bool empty() const;
//- Swap lists by swapping the content of the individual list elements
inline void swap(FixedList<T, Size>& lst);
inline void swap(FixedList<T, Size>& list);
// STL member operators
......@@ -421,14 +429,14 @@ public:
friend Istream& operator>> <T, Size>
(
Istream& is,
FixedList<T, Size>& lst
FixedList<T, Size>& list
);
//- Write to Ostream, as per writeList() with shortListLen=10
friend Ostream& operator<< <T, Size>
(
Ostream& os,
const FixedList<T, Size>& lst
const FixedList<T, Size>& list
);
};
......
......@@ -30,6 +30,30 @@ License
#include <type_traits>
#include <utility>
// * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * //
template<class T, unsigned Size>
inline bool Foam::FixedList<T, Size>::uniform() const
{
if (Size > 1)
{
const T& val = first();
for (unsigned i=1; i<Size; ++i)
{
if (val != (*this)[i])
{
return false;
}
}
return true;
}
return false;
}
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
template<class T, unsigned Size>
......@@ -43,31 +67,41 @@ inline Foam::FixedList<T, Size>::FixedList(const T& val)
template<class T, unsigned Size>
inline Foam::FixedList<T, Size>::FixedList(const T lst[Size])
inline Foam::FixedList<T, Size>::FixedList(const zero)
{
for (unsigned i=0; i<Size; ++i)
{
v_[i] = Zero;
}
}
template<class T, unsigned Size>
inline Foam::FixedList<T, Size>::FixedList(const T list[Size])
{
for (unsigned i=0; i<Size; ++i)
{
v_[i] = lst[i];
v_[i] = list[i];
}
}
template<class T, unsigned Size>
inline Foam::FixedList<T, Size>::FixedList(const FixedList<T, Size>& lst)
inline Foam::FixedList<T, Size>::FixedList(const FixedList<T, Size>& list)
{
for (unsigned i=0; i<Size; ++i)
{
v_[i] = lst.v_[i];
v_[i] = list.v_[i];
}
}
template<class T, unsigned Size>
inline Foam::FixedList<T, Size>::FixedList(FixedList<T, Size>&& lst)
inline Foam::FixedList<T, Size>::FixedList(FixedList<T, Size>&& list)
{
for (unsigned i=0; i<Size; ++i)
{
v_[i] = std::move(lst.v_[i]);
v_[i] = std::move(list.v_[i]);
}
}
......@@ -92,11 +126,11 @@ inline Foam::FixedList<T, Size>::FixedList
template<class T, unsigned Size>
inline Foam::FixedList<T, Size>::FixedList(std::initializer_list<T> lst)
inline Foam::FixedList<T, Size>::FixedList(std::initializer_list<T> list)
{
checkSize(lst.size());
checkSize(list.size());
auto iter = lst.begin();
auto iter = list.begin();
for (unsigned i=0; i<Size; ++i)
{
v_[i] = *iter;
......@@ -106,23 +140,23 @@ inline Foam::FixedList<T, Size>::FixedList(std::initializer_list<T> lst)
template<class T, unsigned Size>
inline Foam::FixedList<T, Size>::FixedList(const UList<T>& lst)
inline Foam::FixedList<T, Size>::FixedList(const UList<T>& list)
{
checkSize(lst.size());
checkSize(list.size());
for (unsigned i=0; i<Size; ++i)
{
v_[i] = lst[i];
v_[i] = list[i];
}
}
template<class T, unsigned Size>
inline Foam::FixedList<T, Size>::FixedList(const SLList<T>& lst)
inline Foam::FixedList<T, Size>::FixedList(const SLList<T>& list)
{
checkSize(lst.size());
checkSize(list.size());
auto iter = lst.begin();
auto iter = list.begin();
for (unsigned i=0; i<Size; ++i)
{
v_[i] = *iter;
......@@ -256,21 +290,21 @@ inline void Foam::FixedList<T, Size>::setSize(const label n)
template<class T, unsigned Size>
inline void Foam::FixedList<T, Size>::swap(FixedList<T, Size>& lst)
inline void Foam::FixedList<T, Size>::swap(FixedList<T, Size>& list)
{
for (unsigned i=0; i<Size; ++i)
{
Foam::Swap(v_[i], lst.v_[i]);
Foam::Swap(v_[i], list.v_[i]);
}
}
template<class T, unsigned Size>
inline void Foam::FixedList<T, Size>::transfer(FixedList<T, Size>& lst)
inline void Foam::FixedList<T, Size>::transfer(FixedList<T, Size>& list)
{
for (unsigned i=0; i<Size; ++i)
{
v_[i] = std::move(lst[i]);
v_[i] = std::move(list[i]);
}
}
......@@ -342,31 +376,31 @@ 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])
inline void Foam::FixedList<T, Size>::operator=(const T list[Size])
{
for (unsigned i=0; i<Size; ++i)
{
v_[i] = lst[i];
v_[i] = list[i];
}
}
template<class T, unsigned Size>
inline void Foam::FixedList<T, Size>::operator=(const UList<T>& lst)
inline void Foam::FixedList<T, Size>::operator=(const UList<T>& list)
{
checkSize(lst.size());
checkSize(list.size());
for (unsigned i=0; i<Size; ++i)
{
v_[i] = lst[i];
v_[i] = list[i];
}
}
template<class T, unsigned Size>
inline void Foam::FixedList<T, Size>::operator=(const SLList<T>& lst)
inline void Foam::FixedList<T, Size>::operator=(const SLList<T>& list)
{
checkSize(lst.size());
checkSize(list.size());
typename SLList<T>::const_iterator iter = lst.begin();
auto iter = list.begin();
for (unsigned i=0; i<Size; ++i)
{
v_[i] = *iter;
......@@ -375,11 +409,11 @@ inline void Foam::FixedList<T, Size>::operator=(const SLList<T>& lst)
}
template<class T, unsigned Size>
inline void Foam::FixedList<T, Size>::operator=(std::initializer_list<T> lst)
inline void Foam::FixedList<T, Size>::operator=(std::initializer_list<T> list)
{
checkSize(lst.size());
checkSize(list.size());
auto iter = lst.begin();
auto iter = list.begin();
for (unsigned i=0; i<Size; ++i)
{
v_[i] = *iter;
......@@ -397,22 +431,22 @@ inline void Foam::FixedList<T, Size>::operator=(const T& val)
}
template<class T, unsigned Size>
inline void Foam::FixedList<T, Size>::operator=(const FixedList<T, Size>& lst)
inline void Foam::FixedList<T, Size>::operator=(const FixedList<T, Size>& list)
{
for (unsigned i=0; i<Size; ++i)
{
v_[i] = lst.v_[i];
v_[i] = list.v_[i];
}
}
template<class T, unsigned Size>
inline void Foam::FixedList<T, Size>::operator=(FixedList<T, Size>&& lst)
inline void Foam::FixedList<T, Size>::operator=(FixedList<T, Size>&& list)
{
// No significant speedup observed for copy assignment on simple types,
// use move assignment for generality with more complex types
for (unsigned i=0; i<Size; ++i)
{
v_[i] = std::move(lst.v_[i]);
v_[i] = std::move(list.v_[i]);
}
}
......@@ -540,14 +574,14 @@ template<class T, unsigned Size>
template<class HashT>
inline unsigned Foam::FixedList<T, Size>::Hash<HashT>::operator()
(
const FixedList<T, Size>& lst,
const FixedList<T, Size>& list,
unsigned seed
) const
{
if (contiguous<T>())
{
// Hash directly
return Hasher(lst.v_, sizeof(lst.v_), seed);
return Hasher(list.v_, sizeof(list.v_), seed);
}
// Hash incrementally
......@@ -555,7 +589,7 @@ inline unsigned Foam::FixedList<T, Size>::Hash<HashT>::operator()
for (unsigned i=0; i<Size; ++i)
{
val = HashT()(lst[i], val);
val = HashT()(list[i], val);
}
return val;
......
......@@ -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) 2017 OpenCFD Ltd.
\\/ M anipulation | Copyright (C) 2017-2018 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -67,35 +67,16 @@ Foam::Ostream& Foam::FixedList<T, Size>::writeList
const label shortListLen
) const
{
const FixedList<T, Size>& lst = *this;
const FixedList<T, Size>& list = *this;
// Write list contents depending on data format
if (os.format() == IOstream::ASCII || !contiguous<T>())
{
// Can the contents be considered 'uniform' (ie, identical)?
bool uniform = (Size > 1 && contiguous<T>());
if (uniform)
if (contiguous<T>() && list.uniform())
{
for (unsigned i=0; i<Size; ++i)
{
if (lst[i] != lst[0])
{
uniform = false;
break;
}
}
}
// Two or more entries, and all entries have identical values.
if (uniform)
{
// Write size (so it is valid dictionary entry) and start delimiter
os << Size << token::BEGIN_BLOCK;
// Contents
os << lst[0];
// End delimiter
os << token::END_BLOCK;
os << Size << token::BEGIN_BLOCK << list[0] << token::END_BLOCK;
}
else if
(
......@@ -110,7 +91,7 @@ Foam::Ostream& Foam::FixedList<T, Size>::writeList
for (unsigned i=0; i<Size; ++i)
{
if (i) os << token::SPACE;
os << lst[i];
os << list[i];
}
// End delimiter
......@@ -124,7 +105,7 @@ Foam::Ostream& Foam::FixedList<T, Size>::writeList
// Contents
for (unsigned i=0; i<Size; ++i)
{
os << lst[i] << nl;
os << list[i] << nl;
}
// End delimiter
......@@ -136,7 +117,7 @@ Foam::Ostream& Foam::FixedList<T, Size>::writeList
// Binary, contiguous
// write(...) includes surrounding start/end delimiters
os.write(reinterpret_cast<const char*>(lst.cdata()), Size*sizeof(T));
os.write(reinterpret_cast<const char*>(list.cdata()), Size*sizeof(T));
}
os.check(FUNCTION_NAME);
......@@ -154,7 +135,7 @@ Foam::FixedList<T, Size>::FixedList(Istream& is)
template<class T, unsigned Size>
Foam::Istream& Foam::operator>>(Foam::Istream& is, FixedList<T, Size>& lst)
Foam::Istream& Foam::operator>>(Foam::Istream& is, FixedList<T, Size>& list)
{
is.fatalCheck(FUNCTION_NAME);
......@@ -169,7 +150,7 @@ Foam::Istream& Foam::operator>>(Foam::Istream& is, FixedList<T, Size>& lst)
if (firstToken.isCompound())
{
lst = dynamicCast<token::Compound<List<T>>>
list = dynamicCast<token::Compound<List<T>>>
(
firstToken.transferCompoundToken(is)
);
......@@ -179,7 +160,7 @@ Foam::Istream& Foam::operator>>(Foam::Istream& is, FixedList<T, Size>& lst)
const label len = firstToken.labelToken();
// List lengths must match
lst.checkSize(len);
list.checkSize(len);
}
else if (!firstToken.isPunctuation())
{
......@@ -202,7 +183,7 @@ Foam::Istream& Foam::operator>>(Foam::Istream& is, FixedList<T, Size>& lst)
{
for (unsigned i=0; i<Size; ++i)
{
is >> lst[i];
is >> list[i];
is.fatalCheck
(
......@@ -224,7 +205,7 @@ Foam::Istream& Foam::operator>>(Foam::Istream& is, FixedList<T, Size>& lst)
for (unsigned i=0; i<Size; ++i)
{
lst[i] = element; // Copy the value
list[i] = element; // Copy the value
}
}
......@@ -235,7 +216,7 @@ Foam::Istream& Foam::operator>>(Foam::Istream& is, FixedList<T, Size>& lst)
{
// Binary and contiguous
is.read(reinterpret_cast<char*>(lst.data()), Size*sizeof(T));
is.read(reinterpret_cast<char*>(list.data()), Size*sizeof(T));
is.fatalCheck
(
......@@ -249,9 +230,9 @@ Foam::Istream& Foam::operator>>(Foam::Istream& is, FixedList<T, Size>& lst)
template<class T, unsigned Size>
Foam::Ostream& Foam::operator<<(Ostream& os, const FixedList<T, Size>& lst)