STYLE: expand use of ListLoop macros (#2624)

- the old List_FOR_ALL macro only remained in use in relatively few
  places. Replace with the expanded equivalent and move the looping
  parameter out of the macro and give an explicit name (eg, loopLen)
  which simplifies the addition of any loop pragmas in the various
  TFOR_ALL... macros (for example).
This commit is contained in:
Mark Olesen 2022-10-28 18:38:24 +02:00
parent 9db3547bd3
commit 454f7960b0
7 changed files with 203 additions and 94 deletions

View File

@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2017-2020 OpenCFD Ltd.
Copyright (C) 2017-2022 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -38,7 +38,7 @@ Foam::label Foam::IndirectListBase<T, Addr>::find
if (pos >= 0 && len)
{
List_CONST_ACCESS(T, values_, vals);
const T* const vals = values_.begin();
while (pos < len)
{
@ -69,7 +69,7 @@ Foam::label Foam::IndirectListBase<T, Addr>::rfind
pos = addr_.size()-1;
}
List_CONST_ACCESS(T, values_, vals);
const T* const vals = values_.begin();
while (pos >= 0)
{

View File

@ -29,8 +29,8 @@ Description
\*---------------------------------------------------------------------------*/
#ifndef ListLoopM_H
#define ListLoopM_H
#ifndef Foam_ListLoopM_H
#define Foam_ListLoopM_H
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
// Element access looping
@ -43,6 +43,7 @@ Description
#define List_CONST_ACCESS(type, f, fp) \
const type* const __restrict__ fp = (f).begin()
// Loop over all elements
#define List_FOR_ALL(f, i) \
const label _n##i = (f).size(); \
for (label i=0; i<_n##i; ++i)

View File

@ -26,10 +26,6 @@ License
\*---------------------------------------------------------------------------*/
#include <utility>
#include "ListOps.H"
#include "ListLoopM.H"
// * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * //
template<class IntListType>
@ -1324,15 +1320,14 @@ Foam::List<T> Foam::ListOps::create
List<T> output(len);
if (len)
{
List_ACCESS(T, output, out);
List_CONST_ACCESS(T2, input, in);
// ie, std::transform(input.begin(), input.end(), output.begin(), op);
for (label i = 0; i < len; ++i)
{
out[i] = op(in[i]);
}
const T2* in = input.begin();
T* out = output.begin();
for (label i = 0; i < len; ++i)
{
out[i] = op(in[i]);
}
return output;
@ -1351,16 +1346,15 @@ Foam::List<T> Foam::ListOps::create
List<T> output(len);
if (len)
{
T* out = output.begin();
// ie, std::transform(first, last, output.begin(), op);
while (first != last)
{
*out = op(*first);
++first;
++out;
}
T* out = output.begin();
while (first != last)
{
*out = op(*first);
++first;
++out;
}
return output;

View File

@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2016 OpenFOAM Foundation
Copyright (C) 2019 OpenCFD Ltd.
Copyright (C) 2019-2022 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -47,7 +47,9 @@ void component
const direction d
)
{
forAll(sf, i)
const label loopLen = (sf).size();
for (label i = 0; i < loopLen; ++i)
{
component(sf[i], f[i], d);
}
@ -57,7 +59,9 @@ void component
template<template<class> class Field, class Type>
void T(FieldField<Field, Type>& f1, const FieldField<Field, Type>& f2)
{
forAll(f1, i)
const label loopLen = (f1).size();
for (label i = 0; i < loopLen; ++i)
{
T(f1[i], f2[i]);
}
@ -71,7 +75,9 @@ void pow
const FieldField<Field, Type>& vf
)
{
forAll(f, i)
const label loopLen = (f).size();
for (label i = 0; i < loopLen; ++i)
{
pow(f[i], vf[i]);
}
@ -122,7 +128,9 @@ void sqr
const FieldField<Field, Type>& vf
)
{
forAll(f, i)
const label loopLen = (f).size();
for (label i = 0; i < loopLen; ++i)
{
sqr(f[i], vf[i]);
}
@ -165,7 +173,9 @@ void magSqr
const FieldField<Field, Type>& f
)
{
forAll(sf, i)
const label loopLen = (sf).size();
for (label i = 0; i < loopLen; ++i)
{
magSqr(sf[i], f[i]);
}
@ -210,7 +220,9 @@ void mag
const FieldField<Field, Type>& f
)
{
forAll(sf, i)
const label loopLen = (sf).size();
for (label i = 0; i < loopLen; ++i)
{
mag(sf[i], f[i]);
}
@ -255,7 +267,9 @@ void cmptMax
const FieldField<Field, Type>& f
)
{
forAll(cf, i)
const label loopLen = (cf).size();
for (label i = 0; i < loopLen; ++i)
{
cmptMax(cf[i], f[i]);
}
@ -304,7 +318,9 @@ void cmptMin
const FieldField<Field, Type>& f
)
{
forAll(cf, i)
const label loopLen = (cf).size();
for (label i = 0; i < loopLen; ++i)
{
cmptMin(cf[i], f[i]);
}
@ -353,7 +369,9 @@ void cmptAv
const FieldField<Field, Type>& f
)
{
forAll(cf, i)
const label loopLen = (cf).size();
for (label i = 0; i < loopLen; ++i)
{
cmptAv(cf[i], f[i]);
}
@ -402,7 +420,9 @@ void cmptMag
const FieldField<Field, Type>& f
)
{
forAll(cf, i)
const label loopLen = (cf).size();
for (label i = 0; i < loopLen; ++i)
{
cmptMag(cf[i], f[i]);
}
@ -451,7 +471,9 @@ Type max(const FieldField<Field, Type>& f)
{
Type result = pTraits<Type>::min;
forAll(f, i)
const label loopLen = (f).size();
for (label i = 0; i < loopLen; ++i)
{
if (f[i].size())
{
@ -471,7 +493,9 @@ Type min(const FieldField<Field, Type>& f)
{
Type result = pTraits<Type>::max;
forAll(f, i)
const label loopLen = (f).size();
for (label i = 0; i < loopLen; ++i)
{
if (f[i].size())
{
@ -488,14 +512,16 @@ TMP_UNARY_FUNCTION(Type, min)
template<template<class> class Field, class Type>
Type sum(const FieldField<Field, Type>& f)
{
Type Sum = Zero;
Type result = Zero;
forAll(f, i)
const label loopLen = (f).size();
for (label i = 0; i < loopLen; ++i)
{
Sum += sum(f[i]);
result += sum(f[i]);
}
return Sum;
return result;
}
TMP_UNARY_FUNCTION(Type, sum)
@ -507,7 +533,9 @@ typename typeOfMag<Type>::type sumMag(const FieldField<Field, Type>& f)
magType result = Zero;
forAll(f, i)
const label loopLen = (f).size();
for (label i = 0; i < loopLen; ++i)
{
result += sumMag(f[i]);
}
@ -520,21 +548,20 @@ TMP_UNARY_FUNCTION(typename typeOfMag<Type>::type, sumMag)
template<template<class> class Field, class Type>
Type average(const FieldField<Field, Type>& f)
{
if (f.size())
label n = 0;
const label loopLen = (f).size();
for (label i = 0; i < loopLen; ++i)
{
label n = 0;
n += f[i].size();
}
forAll(f, i)
{
n += f[i].size();
}
if (n)
{
Type avrg = sum(f)/n;
if (n)
{
Type avrg = sum(f)/n;
return avrg;
}
return avrg;
}
WarningInFunction
@ -551,7 +578,9 @@ MinMax<Type> minMax(const FieldField<Field, Type>& f)
{
MinMax<Type> result;
forAll(f, i)
const label loopLen = (f).size();
for (label i = 0; i < loopLen; ++i)
{
result += minMax(f[i]);
}
@ -566,7 +595,9 @@ scalarMinMax minMaxMag(const FieldField<Field, Type>& f)
{
scalarMinMax result;
forAll(f, i)
const label loopLen = (f).size();
for (label i = 0; i < loopLen; ++i)
{
result += minMaxMag(f[i]);
}
@ -605,7 +636,9 @@ Type gAverage(const FieldField<Field, Type>& f)
{
label n = 0;
forAll(f, i)
const label loopLen = (f).size();
for (label i = 0; i < loopLen; ++i)
{
n += f[i].size();
}
@ -675,7 +708,9 @@ void opFunc \
const FieldField<Field2, Type2>& f2 \
) \
{ \
forAll(f, i) \
const label loopLen = (f).size(); \
\
for (label i = 0; i < loopLen; ++i) \
{ \
opFunc(f[i], f1[i], f2[i]); \
} \
@ -811,7 +846,9 @@ void opFunc \
const VectorSpace<Form,Cmpt,nCmpt>& vs \
) \
{ \
forAll(f, i) \
const label loopLen = (f).size(); \
\
for (label i = 0; i < loopLen; ++i) \
{ \
opFunc(f[i], f1[i], vs); \
} \
@ -881,7 +918,9 @@ void opFunc \
const FieldField<Field, Type>& f1 \
) \
{ \
forAll(f, i) \
const label loopLen = (f).size(); \
\
for (label i = 0; i < loopLen; ++i) \
{ \
opFunc(f[i], vs, f1[i]); \
} \

View File

@ -6,6 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2017 OpenFOAM Foundation
Copyright (C) 2022 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -39,7 +40,9 @@ void Func \
const FieldField<Field, Type>& f \
) \
{ \
forAll(res, i) \
const label loopLen = (res).size(); \
\
for (label i = 0; i < loopLen; ++i) \
{ \
Func(res[i], f[i]); \
} \
@ -83,7 +86,9 @@ void OpFunc \
const FieldField<Field, Type>& f \
) \
{ \
forAll(res, i) \
const label loopLen = (res).size(); \
\
for (label i = 0; i < loopLen; ++i) \
{ \
OpFunc(res[i], f[i]); \
} \
@ -128,7 +133,9 @@ void Func \
const FieldField<Field, Type2>& f2 \
) \
{ \
forAll(f, i) \
const label loopLen = (f).size(); \
\
for (label i = 0; i < loopLen; ++i) \
{ \
Func(f[i], f1[i], f2[i]); \
} \
@ -212,7 +219,9 @@ void Func \
const FieldField<Field, Type2>& f2 \
) \
{ \
forAll(f, i) \
const label loopLen = (f).size(); \
\
for (label i = 0; i < loopLen; ++i) \
{ \
Func(f[i], s, f2[i]); \
} \
@ -260,7 +269,9 @@ void Func \
const Type2& s \
) \
{ \
forAll(f, i) \
const label loopLen = (f).size(); \
\
for (label i = 0; i < loopLen; ++i) \
{ \
Func(f[i], f1[i], s); \
} \
@ -315,7 +326,9 @@ void OpFunc \
const FieldField<Field, Type2>& f2 \
) \
{ \
forAll(f, i) \
const label loopLen = (f).size(); \
\
for (label i = 0; i < loopLen; ++i) \
{ \
OpFunc(f[i], f1[i], f2[i]); \
} \
@ -399,7 +412,9 @@ void OpFunc \
const FieldField<Field, Type2>& f2 \
) \
{ \
forAll(f, i) \
const label loopLen = (f).size(); \
\
for (label i = 0; i < loopLen; ++i) \
{ \
OpFunc(f[i], s, f2[i]); \
} \
@ -447,7 +462,9 @@ void OpFunc \
const Type2& s \
) \
{ \
forAll(f, i) \
const label loopLen = (f).size(); \
\
for (label i = 0; i < loopLen; ++i) \
{ \
OpFunc(f[i], f1[i], s); \
} \

View File

@ -481,13 +481,13 @@ sumProd(const UList<Type>& f1, const UList<Type>& f2)
template<class Type>
Type sumCmptProd(const UList<Type>& f1, const UList<Type>& f2)
{
Type SumProd = Zero;
Type result = Zero;
if (f1.size() && (f1.size() == f2.size()))
{
TFOR_ALL_S_OP_FUNC_F_F
(
Type,
SumProd,
result,
+=,
cmptMultiply,
Type,
@ -496,7 +496,7 @@ Type sumCmptProd(const UList<Type>& f1, const UList<Type>& f2)
f2
)
}
return SumProd;
return result;
}

View File

@ -6,6 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2016 OpenFOAM Foundation
Copyright (C) 2022 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -30,8 +31,8 @@ Description
\*---------------------------------------------------------------------------*/
#ifndef FieldM_H
#define FieldM_H
#ifndef Foam_FieldM_H
#define Foam_FieldM_H
#include "error.H"
#include "ListLoopM.H"
@ -123,12 +124,17 @@ void checkFields
List_CONST_ACCESS(typeF2, f2, f2P); \
\
/* Loop: f1 OP FUNC(f2) */ \
List_FOR_ALL(f1, i) \
const label loopLen = (f1).size(); \
\
/* pragmas... */ \
for (label i = 0; i < loopLen; ++i) \
{ \
(f1P[i]) OP FUNC(f2P[i]); \
}
// Member function : this f1 OP f2.FUNC()
#define TFOR_ALL_F_OP_F_FUNC(typeF1, f1, OP, typeF2, f2, FUNC) \
\
/* Check fields have same size */ \
@ -139,7 +145,10 @@ void checkFields
List_CONST_ACCESS(typeF2, f2, f2P); \
\
/* Loop: f1 OP f2.FUNC() */ \
List_FOR_ALL(f1, i) \
const label loopLen = (f1).size(); \
\
/* pragmas... */ \
for (label i = 0; i < loopLen; ++i) \
{ \
(f1P[i]) OP (f2P[i]).FUNC(); \
}
@ -158,7 +167,10 @@ void checkFields
List_CONST_ACCESS(typeF3, f3, f3P); \
\
/* Loop: f1 OP FUNC(f2, f3) */ \
List_FOR_ALL(f1, i) \
const label loopLen = (f1).size(); \
\
/* pragmas... */ \
for (label i = 0; i < loopLen; ++i) \
{ \
(f1P[i]) OP FUNC((f2P[i]), (f3P[i])); \
}
@ -176,7 +188,10 @@ void checkFields
List_CONST_ACCESS(typeF2, f2, f2P); \
\
/* Loop: s OP FUNC(f1, f2) */ \
List_FOR_ALL(f1, i) \
const label loopLen = (f1).size(); \
\
/* pragmas, reduction... */ \
for (label i = 0; i < loopLen; ++i) \
{ \
(s) OP FUNC((f1P[i]), (f2P[i])); \
}
@ -194,7 +209,10 @@ void checkFields
List_CONST_ACCESS(typeF2, f2, f2P); \
\
/* Loop: f1 OP FUNC(f2, s) */ \
List_FOR_ALL(f1, i) \
const label loopLen = (f1).size(); \
\
/* pragmas... */ \
for (label i = 0; i < loopLen; ++i) \
{ \
(f1P[i]) OP FUNC((f2P[i]), (s)); \
}
@ -208,7 +226,10 @@ void checkFields
List_CONST_ACCESS(typeF, f, fP); \
\
/* Loop: s1 OP FUNC(f, s2) */ \
List_FOR_ALL(f, i) \
const label loopLen = (f).size(); \
\
/* pragmas, reduction... */ \
for (label i = 0; i < loopLen; ++i) \
{ \
(s1) OP FUNC((fP[i]), (s2)); \
}
@ -226,7 +247,10 @@ void checkFields
List_CONST_ACCESS(typeF2, f2, f2P); \
\
/* Loop: f1 OP1 f2 OP2 f3 */ \
List_FOR_ALL(f1, i) \
const label loopLen = (f1).size(); \
\
/* pragmas... */ \
for (label i = 0; i < loopLen; ++i) \
{ \
(f1P[i]) OP FUNC((s), (f2P[i])); \
}
@ -240,7 +264,10 @@ void checkFields
List_ACCESS(typeF1, f1, f1P); \
\
/* Loop: f1 OP FUNC(s1, s2) */ \
List_FOR_ALL(f1, i) \
const label loopLen = (f1).size(); \
\
/* pragmas... */ \
for (label i = 0; i < loopLen; ++i) \
{ \
(f1P[i]) OP FUNC((s1), (s2)); \
}
@ -258,7 +285,10 @@ void checkFields
List_CONST_ACCESS(typeF2, f2, f2P); \
\
/* Loop: f1 OP f2 FUNC(s) */ \
List_FOR_ALL(f1, i) \
const label loopLen = (f1).size(); \
\
/* pragmas... */ \
for (label i = 0; i < loopLen; ++i) \
{ \
(f1P[i]) OP (f2P[i]) FUNC((s)); \
}
@ -277,7 +307,10 @@ void checkFields
List_CONST_ACCESS(typeF3, f3, f3P); \
\
/* Loop: f1 OP1 f2 OP2 f3 */ \
List_FOR_ALL(f1, i) \
const label loopLen = (f1).size(); \
\
/* pragmas... */ \
for (label i = 0; i < loopLen; ++i) \
{ \
(f1P[i]) OP1 (f2P[i]) OP2 (f3P[i]); \
}
@ -295,7 +328,10 @@ void checkFields
List_CONST_ACCESS(typeF2, f2, f2P); \
\
/* Loop: f1 OP1 s OP2 f2 */ \
List_FOR_ALL(f1, i) \
const label loopLen = (f1).size(); \
\
/* pragmas... */ \
for (label i = 0; i < loopLen; ++i) \
{ \
(f1P[i]) OP1 (s) OP2 (f2P[i]); \
}
@ -313,7 +349,10 @@ void checkFields
List_CONST_ACCESS(typeF2, f2, f2P); \
\
/* Loop f1 OP1 s OP2 f2 */ \
List_FOR_ALL(f1, i) \
const label loopLen = (f1).size(); \
\
/* pragmas... */ \
for (label i = 0; i < loopLen; ++i) \
{ \
(f1P[i]) OP1 (f2P[i]) OP2 (s); \
}
@ -331,11 +370,15 @@ void checkFields
List_CONST_ACCESS(typeF2, f2, f2P); \
\
/* Loop: f1 OP f2 */ \
List_FOR_ALL(f1, i) \
const label loopLen = (f1).size(); \
\
/* pragmas... */ \
for (label i = 0; i < loopLen; ++i) \
{ \
(f1P[i]) OP (f2P[i]); \
}
// Member operator : this field f1 OP1 OP2 f2
#define TFOR_ALL_F_OP_OP_F(typeF1, f1, OP1, OP2, typeF2, f2) \
@ -348,7 +391,10 @@ void checkFields
List_CONST_ACCESS(typeF2, f2, f2P); \
\
/* Loop: f1 OP1 OP2 f2 */ \
List_FOR_ALL(f1, i) \
const label loopLen = (f1).size(); \
\
/* pragmas... */ \
for (label i = 0; i < loopLen; ++i) \
{ \
(f1P[i]) OP1 OP2 (f2P[i]); \
}
@ -362,7 +408,10 @@ void checkFields
List_ACCESS(typeF, f, fP); \
\
/* Loop: f OP s */ \
List_FOR_ALL(f, i) \
const label loopLen = (f).size(); \
\
/* pragmas... */ \
for (label i = 0; i < loopLen; ++i) \
{ \
(fP[i]) OP (s); \
}
@ -378,7 +427,10 @@ void checkFields
List_CONST_ACCESS(typeF, f, fP); \
\
/* Loop: s OP f */ \
List_FOR_ALL(f, i) \
const label loopLen = (f).size(); \
\
/* pragmas, reduction... */ \
for (label i = 0; i < loopLen; ++i) \
{ \
(s) OP (fP[i]); \
}
@ -392,8 +444,11 @@ void checkFields
List_CONST_ACCESS(typeF1, f1, f1P); \
List_CONST_ACCESS(typeF2, f2, f2P); \
\
/* Loop: s OP f */ \
List_FOR_ALL(f1, i) \
/* Loop: s OP1 f1 OP2 f2 */ \
const label loopLen = (f1).size(); \
\
/* pragmas, reduction... */ \
for (label i = 0; i < loopLen; ++i) \
{ \
(s) OP1 (f1P[i]) OP2 (f2P[i]); \
}
@ -407,7 +462,10 @@ void checkFields
List_CONST_ACCESS(typeF, f, fP); \
\
/* Loop: s OP FUNC(f) */ \
List_FOR_ALL(f, i) \
const label loopLen = (f).size(); \
\
/* pragmas, reduction... */ \
for (label i = 0; i < loopLen; ++i) \
{ \
(s) OP FUNC(fP[i]); \
}