label.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 | www.openfoam.com
6 \\/ M anipulation |
7-------------------------------------------------------------------------------
8 Copyright (C) 2011-2015 OpenFOAM Foundation
9 Copyright (C) 2019 OpenCFD Ltd.
10-------------------------------------------------------------------------------
11License
12 This file is part of OpenFOAM.
13
14 OpenFOAM is free software: you can redistribute it and/or modify it
15 under the terms of the GNU General Public License as published by
16 the Free Software Foundation, either version 3 of the License, or
17 (at your option) any later version.
18
19 OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26
27\*---------------------------------------------------------------------------*/
28
29#include "error.H"
30#include "label.H"
31#include "Istream.H"
32
33// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
34
35#if WM_LABEL_SIZE == 32
36const char* const Foam::pTraits<int32_t>::typeName = "label";
37const char* const Foam::pTraits<int64_t>::typeName = "int64";
38#elif WM_LABEL_SIZE == 64
39const char* const Foam::pTraits<int32_t>::typeName = "int32";
40const char* const Foam::pTraits<int64_t>::typeName = "label";
41#endif
42
43
44// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
45
47{
48 label val(0);
49 readRawLabel(is, &val, 1);
50 return val;
51}
52
53
54void Foam::readRawLabel(Istream& is, label* data, size_t nElem)
55{
56 // No check for binary vs ascii, the caller knows what they are doing
57
58 #if WM_LABEL_SIZE == 32
59
60 // Defined label as int32, non-native type is int64
61 // Handle type narrowing limits
62
63 typedef int64_t nonNative;
64
65 if (is.checkLabelSize<nonNative>())
66 {
67 nonNative parsed;
68
69 for (const label* endData = data + nElem; data != endData; ++data)
70 {
71 is.readRaw(reinterpret_cast<char*>(&parsed), sizeof(nonNative));
72
73 // Type narrowing
74 // Overflow: silently fix, or raise error?
75 if (parsed < labelMin)
76 {
77 *data = labelMin;
78 }
79 else if (parsed > labelMax)
80 {
81 *data = labelMax;
82 }
83 else
84 {
85 *data = label(parsed);
86 }
87 }
88 }
89 else
90 {
91 // Read with native size
92 is.readRaw(reinterpret_cast<char*>(data), nElem*sizeof(label));
93 }
94
95 #elif WM_LABEL_SIZE == 64
96
97 // Defined label as int64, non-native type is int32
98
99 typedef int32_t nonNative;
100
101 if (is.checkLabelSize<nonNative>())
102 {
103 nonNative parsed;
104
105 for (const label* endData = data + nElem; data != endData; ++data)
106 {
107 is.readRaw(reinterpret_cast<char*>(&parsed), sizeof(nonNative));
108
109 *data = label(parsed);
110 }
111 }
112 else
113 {
114 // Read with native size
115 is.readRaw(reinterpret_cast<char*>(data), nElem*sizeof(label));
116 }
117
118 #endif
119}
120
121
122// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
123
124Foam::label Foam::pow(label a, label b)
125{
126 label ans = 1;
127 for (label i=0; i<b; i++)
128 {
129 ans *= a;
130 }
131
132 #ifdef FULLDEBUG
133 if (b < 0)
134 {
136 << "negative value for b is not supported"
137 << abort(FatalError);
138 }
139 #endif
140
141 return ans;
142}
143
144
145Foam::label Foam::factorial(label n)
146{
147 static label factTable[13] =
148 {
149 1, 1, 2, 6, 24, 120, 720, 5040, 40320,
150 362880, 3628800, 39916800, 479001600
151 };
152
153 #ifdef FULLDEBUG
154 if (n > 12 && n < 0)
155 {
157 << "n value out of range"
158 << abort(FatalError);
159 }
160 #endif
161
162 return factTable[n];
163}
164
165
166// ************************************************************************* //
label n
std::enable_if< std::is_integral< T >::value, bool >::type checkLabelSize() const noexcept
Definition: IOstream.H:300
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:64
virtual Istream & readRaw(char *, std::streamsize)=0
Low-level raw binary read.
Database for solution data, solver performance and other reduced data.
Definition: data.H:58
A traits class, which is primarily used for primitives.
Definition: pTraits.H:59
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:453
constexpr label labelMin
Definition: label.H:60
constexpr label labelMax
Definition: label.H:61
dimensionedScalar pow(const dimensionedScalar &ds, const dimensionedScalar &expt)
label readRawLabel(Istream &is)
Read raw label from binary stream.
Definition: label.C:46
errorManip< error > abort(error &err)
Definition: errorManip.H:144
label factorial(label n)
Evaluate n! : 0 < n <= 12.
Definition: label.C:145
error FatalError
volScalarField & b
Definition: createFields.H:27