GatherBase.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | Copyright (C) 2011-2015 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 \*---------------------------------------------------------------------------*/
25 
26 #include "GatherBase.H"
27 
28 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
29 
30 namespace Foam
31 {
32 
33 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
34 
35 template<class Type>
37 {
38  label sum = 0;
39 
40  forAll(lst, lstI)
41  {
42  sum += lst[lstI].size();
43  }
44 
45  Type result(sum);
46 
47  label index = 0;
48 
49  forAll(lst, lstI)
50  {
51  const Type& sub = lst[lstI];
52 
53  forAll(sub, subI)
54  {
55  result[index++] = sub[subI];
56  }
57  }
58 
59  return result;
60 }
61 
62 
63 template<class DataType, class IndexType, class AddOp>
64 IndexType GatherBase::offset
65 (
66  const List<DataType>& values,
67  const List<IndexType>& indices,
68  AddOp aop
69 )
70 {
71  if (values.size() != indices.size())
72  {
74  << "Input data and indices lists not equal size." << endl
75  << "data size:" << values.size()
76  << " indices:" << indices.size()
77  << abort(FatalError);
78  }
79 
80 
81  label sum = 0;
82 
83  forAll(indices, lstI)
84  {
85  sum += indices[lstI].size();
86  }
87 
88  IndexType result(sum);
89 
90  label index = 0;
91 
92  label offset = 0;
93 
94  forAll(indices, lstI)
95  {
96  const IndexType& sub = indices[lstI];
97 
98  forAll(sub, subI)
99  {
100  result[index++] = aop(sub[subI], offset);
101  }
102  offset += values[lstI].size();
103  }
104 
105  return result;
106 }
107 
108 
109 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
110 
111 } // End namespace Foam
112 
113 // ************************************************************************* //
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:406
Foam::GatherBase::flatten
static T flatten(const List< T >)
Flatten: appends all elements of list into one single list.
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
Foam::AddOp
Definition: GatherBase.H:71
GatherBase.H
Foam::label
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:59
Foam::FatalError
error FatalError
Foam
Namespace for OpenFOAM.
Definition: combustionModel.C:30
Foam::abort
errorManip< error > abort(error &err)
Definition: errorManip.H:131
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:318
Foam::List< Type >
Foam::GatherBase::offset
static IndexType offset(const List< DataType > &values, const List< IndexType > &indices, AddOp aop)
Flatten and offset 'Gathered' indices (into value) so they.
Definition: GatherBase.C:65
Foam::sum
dimensioned< Type > sum(const DimensionedField< Type, GeoMesh > &df)
Definition: DimensionedFieldFunctions.C:333
Foam::List::size
void size(const label)
Override size to be inconsistent with allocated storage.