initial commit for version 1.6.x patch release
[OpenFOAM-1.6.x.git] / src / OpenFOAM / primitives / ints / label / label.H
blobcd15e9ea8bb923117c59f158709d69d97babb4e8
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 1991-2009 OpenCFD Ltd.
6      \\/     M anipulation  |
7 -------------------------------------------------------------------------------
8 License
9     This file is part of OpenFOAM.
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 the
13     Free Software Foundation; either version 2 of the License, or (at your
14     option) any later version.
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.
21     You should have received a copy of the GNU General Public License
22     along with OpenFOAM; if not, write to the Free Software Foundation,
23     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 Typedef
26     Foam::label
28 Description
29     A label is an int/long/long long depending on the range desired.
31     A readLabel function is defined so that label can be constructed from
32     Istream.
34 \*---------------------------------------------------------------------------*/
36 #ifndef label_H
37 #define label_H
39 #include <climits>
40 #include <cstdlib>
42 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
45 #if FOAM_LABEL64
46 #    define FOAM_LABEL_MAX 9000000000000000000
47 #else
48 #    define FOAM_LABEL_MAX 2000000000
49 #endif
52 #if INT_MAX > FOAM_LABEL_MAX
54 // Define label as an int
56 # undef  FOAM_LABEL_MAX
57 # define FOAM_LABEL_MAX INT_MAX
59 # include "int.H"
61 namespace Foam
63     typedef int label;
65     static const label labelMin = INT_MIN;
66     static const label labelMax = INT_MAX;
68     inline label readLabel(Istream& is)
69     {
70         return readInt(is);
71     }
73 } // End namespace Foam
76 #elif LONG_MAX > FOAM_LABEL_MAX
77 // Define label as a long
79 # undef  FOAM_LABEL_MAX
80 # define FOAM_LABEL_MAX LONG_MAX
82 # include "int.H"
83 # include "long.H"
85 namespace Foam
87     typedef long label;
89     static const label labelMin = LONG_MIN;
90     static const label labelMax = LONG_MAX;
92     inline label readLabel(Istream& is)
93     {
94         return readLong(is);
95     }
97 } // End namespace Foam
100 #elif LLONG_MAX > FOAM_LABEL_MAX
102 // Define label as a long long
104 # undef  FOAM_LABEL_MAX
105 # define FOAM_LABEL_MAX LLONG_MAX
107 # include "int.H"
108 # include "long.H"
109 # include "longLong.H"
111 namespace Foam
113     typedef long long label;
115     static const label labelMin = LLONG_MIN;
116     static const label labelMax = LLONG_MAX;
118     inline label readLabel(Istream& is)
119     {
120         return readLongLong(is);
121     }
123 } // End namespace Foam
125 #endif
128 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
130 #include "pTraits.H"
131 #include "direction.H"
133 namespace Foam
136 //- template specialization for pTraits<label>
137 template<>
138 class pTraits<label>
140     label p_;
142 public:
144     //- Component type
145     typedef label cmptType;
147     // Member constants
149         enum
150         {
151             dim = 3,         // Dimensionality of space
152             rank = 0,        // Rank of label is 0
153             nComponents = 1  // Number of components in label is 1
154         };
156     // Static data members
158         static const char* const typeName;
159         static const char* componentNames[];
160         static const label zero;
161         static const label one;
162         static const label min;
163         static const label max;
165     // Constructors
167         //- Construct from Istream
168         pTraits(Istream&);
170     // Member Functions
172         operator label() const
173         {
174             return p_;
175         }
179 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
181 //- Raise one label to the power of another
182 label pow(label a, label b);
184 //- Evaluate n! : n <= 12
185 label factorial(label n);
188 #define MAXMIN(retType, type1, type2)              \
189                                                    \
190 inline retType max(const type1 s1, const type2 s2) \
191 {                                                  \
192     return (s1 > s2)? s1: s2;                      \
193 }                                                  \
194                                                    \
195 inline retType min(const type1 s1, const type2 s2) \
196 {                                                  \
197     return (s1 < s2)? s1: s2;                      \
201 MAXMIN(char, char, char)
202 MAXMIN(short, short, short)
203 MAXMIN(int, int, int)
204 MAXMIN(long, long, long)
205 MAXMIN(long long, long long, long long)
207 MAXMIN(unsigned char, unsigned char, unsigned char)
208 MAXMIN(unsigned short, unsigned short, unsigned short)
209 MAXMIN(unsigned int, unsigned int, unsigned int)
210 MAXMIN(unsigned long, unsigned long, unsigned long)
211 MAXMIN(unsigned long long, unsigned long long, unsigned long long)
213 MAXMIN(long, int, long)
214 MAXMIN(long long, int, long long)
215 MAXMIN(long long, long long, int)
217 inline label& setComponent(label& l, const direction)
219     return l;
222 inline label component(const label l, const direction)
224     return l;
227 inline label mag(const label l)
229     return ::abs(l);
232 inline label sign(const label s)
234     return (s >= 0)? 1: -1;
237 inline label pos(const label s)
239     return (s >= 0)? 1: 0;
242 inline label neg(const label s)
244     return (s < 0)? 1: 0;
248 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
250 } // End namespace Foam
252 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
254 #endif
256 // ************************************************************************* //