OpenFOAM logo
Open Source CFD Toolkit

label.H

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------------*\
00002   =========                 |
00003   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
00004    \\    /   O peration     |
00005     \\  /    A nd           | Copyright (C) 1991-2005 OpenCFD Ltd.
00006      \\/     M anipulation  |
00007 -------------------------------------------------------------------------------
00008 License
00009     This file is part of OpenFOAM.
00010 
00011     OpenFOAM is free software; you can redistribute it and/or modify it
00012     under the terms of the GNU General Public License as published by the
00013     Free Software Foundation; either version 2 of the License, or (at your
00014     option) any later version.
00015 
00016     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
00017     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00018     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00019     for more details.
00020 
00021     You should have received a copy of the GNU General Public License
00022     along with OpenFOAM; if not, write to the Free Software Foundation,
00023     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
00024 
00025 Type
00026     label
00027 
00028 Description
00029     A label is an int if INT_MAX is large enough otherwise a long.
00030     A readLabel function is defined so that label can be constructed from
00031     Istream.
00032 
00033 \*---------------------------------------------------------------------------*/
00034 
00035 #ifndef label_H
00036 #define label_H
00037 
00038 #include <climits>
00039 #include <cstdlib>
00040 
00041 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00042 
00043 #if INT_MAX > 2000000000
00044 
00045 // Define label as an int
00046 
00047 #include "int.H"
00048 
00049 namespace Foam
00050 {
00051     typedef int label;
00052 
00053     static const label labelMax = INT_MAX;
00054     static const label labelMin = INT_MIN;
00055 
00056     inline label readLabel(Istream& is)
00057     {
00058         return readInt(is);
00059     }
00060 
00061 } // End namespace Foam
00062 
00063 #else
00064 
00065 // Define label as a long
00066 
00067 #include "long.H"
00068 
00069 namespace Foam
00070 {
00071     typedef long label;
00072 
00073     static const label labelMax = LONG_MAX;
00074     static const label labelMin = LONG_MIN;
00075 
00076     inline label readLabel(Istream& is)
00077     {
00078         return readLong(is);
00079     }
00080 
00081 } // End namespace Foam
00082 
00083 #endif
00084 
00085 
00086 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00087 
00088 #include "pTraits.H"
00089 
00090 namespace Foam
00091 {
00092 
00093 // template specialisation for pTraits<label>
00094 template<>
00095 class pTraits<label>
00096 {
00097     label p_;
00098 
00099 public:
00100 
00101     //- Component type
00102     typedef label cmptType;
00103 
00104     // Member constants
00105 
00106         enum
00107         {
00108             dim = 3,         // Dimensionality of space
00109             rank = 0,        // Rank od label is 0
00110             nComponents = 1  // Number of components in label is 1
00111         };
00112 
00113     // Static data members
00114 
00115         static const char* const typeName;
00116         static const char* componentNames[];
00117         static const label zero;
00118         static const label one;
00119 
00120     // Constructors
00121 
00122         //- Construct from Istream
00123         pTraits(Istream& is);
00124 
00125     // Member Functions
00126 
00127         operator label() const
00128         {
00129             return p_;
00130         }
00131 };
00132 
00133 
00134 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00135 
00136 //- Raise one label to the power of another
00137 label pow(label a, label b);
00138 
00139 //- Evaluate nCr : values of n and r <= 12
00140 label nCr(label n, label r);
00141 
00142 //- Evaluate n! : n <= 12
00143 label factorial(label n);
00144 
00145 
00146 #define MAXMIN(retType, type1, type2)              \
00147                                                    \
00148 inline retType max(const type1 s1, const type2 s2) \
00149 {                                                  \
00150     return (s1 > s2)? s1: s2;                      \
00151 }                                                  \
00152                                                    \
00153 inline retType min(const type1 s1, const type2 s2) \
00154 {                                                  \
00155     return (s1 < s2)? s1: s2;                      \
00156 }
00157 
00158 
00159 MAXMIN(char, char, char)
00160 MAXMIN(short, short, short)
00161 MAXMIN(int, int, int)
00162 MAXMIN(long, long, long)
00163 MAXMIN(unsigned char, unsigned char, unsigned char)
00164 MAXMIN(unsigned short, unsigned short, unsigned short)
00165 MAXMIN(unsigned int, unsigned int, unsigned int)
00166 MAXMIN(unsigned long, unsigned long, unsigned long)
00167 MAXMIN(long, int, long)
00168 MAXMIN(long, long, int)
00169 
00170 
00171 inline label mag(const label l)
00172 {
00173     return ::abs(l);
00174 }
00175 
00176 inline label sign(const label s)
00177 {
00178     return (s >= 0)? 1: -1;
00179 }
00180 
00181 inline label pos(const label s)
00182 {
00183     return (s >= 0)? 1: 0;
00184 }
00185 
00186 inline label neg(const label s)
00187 {
00188     return (s < 0)? 1: 0;
00189 }
00190 
00191 
00192 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00193 
00194 } // End namespace Foam
00195 
00196 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00197 
00198 #endif
00199 
00200 // ************************************************************************* //
For further information go to www.openfoam.org