int-type.hpp
Go to the documentation of this file.00001 /* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */ 00002 /* 00003 * Main authors: 00004 * Christian Schulte <schulte@gecode.org> 00005 * 00006 * Copyright: 00007 * Christian Schulte, 2008 00008 * 00009 * Last modified: 00010 * $Date: 2010-01-26 13:22:47 +0100 (Tue, 26 Jan 2010) $ by $Author: schulte $ 00011 * $Revision: 10238 $ 00012 * 00013 * This file is part of Gecode, the generic constraint 00014 * development environment: 00015 * http://www.gecode.org 00016 * 00017 * Permission is hereby granted, free of charge, to any person obtaining 00018 * a copy of this software and associated documentation files (the 00019 * "Software"), to deal in the Software without restriction, including 00020 * without limitation the rights to use, copy, modify, merge, publish, 00021 * distribute, sublicense, and/or sell copies of the Software, and to 00022 * permit persons to whom the Software is furnished to do so, subject to 00023 * the following conditions: 00024 * 00025 * The above copyright notice and this permission notice shall be 00026 * included in all copies or substantial portions of the Software. 00027 * 00028 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 00029 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 00030 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 00031 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 00032 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 00033 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 00034 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 00035 * 00036 */ 00037 00038 #include <climits> 00039 00040 namespace Gecode { namespace Support { 00041 00043 enum IntType { 00044 IT_CHAR = 0, 00045 IT_SHRT = 1, 00046 IT_INT = 2 00047 }; 00048 00050 IntType u_type(unsigned int n); 00052 IntType s_type(signed int n); 00053 00055 template<class IntType> 00056 class IntTypeTraits {}; 00057 00059 template<> 00060 class IntTypeTraits<signed char> { 00061 public: 00063 typedef unsigned char utype; 00065 typedef signed char stype; 00067 static const signed char min = SCHAR_MIN; 00069 static const signed char max = SCHAR_MAX; 00071 static const IntType description = IT_CHAR; 00072 }; 00074 template<> 00075 class IntTypeTraits<unsigned char> { 00076 public: 00078 typedef unsigned char utype; 00080 typedef signed char stype; 00082 static const unsigned char min = 0; 00084 static const unsigned char max = UCHAR_MAX; 00086 static const IntType description = IT_CHAR; 00087 }; 00089 template<> 00090 class IntTypeTraits<signed short int> { 00091 public: 00093 typedef unsigned short int utype; 00095 typedef signed short int stype; 00097 static const signed short int min = SHRT_MIN; 00099 static const signed short int max = SHRT_MAX; 00101 static const IntType description = IT_SHRT; 00102 }; 00104 template<> 00105 class IntTypeTraits<unsigned short int> { 00106 public: 00108 typedef unsigned short int utype; 00110 typedef signed short int stype; 00112 static const unsigned short int min = 0; 00114 static const unsigned short int max = USHRT_MAX; 00116 static const IntType description = IT_SHRT; 00117 }; 00119 template<> 00120 class IntTypeTraits<signed int> { 00121 public: 00123 typedef unsigned int utype; 00125 typedef signed int stype; 00127 static const signed int min = INT_MIN; 00129 static const signed int max = INT_MAX; 00131 static const IntType description = IT_INT; 00132 }; 00134 template<> 00135 class IntTypeTraits<unsigned int> { 00136 public: 00138 typedef unsigned int utype; 00140 typedef signed int stype; 00142 static const unsigned int min = 0; 00144 static const unsigned int max = UINT_MAX; 00146 static const IntType description = IT_INT; 00147 }; 00148 00149 00150 forceinline IntType 00151 u_type(unsigned int n) { 00152 if (n < UCHAR_MAX) 00153 return IT_CHAR; 00154 else if (n < USHRT_MAX) 00155 return IT_SHRT; 00156 else 00157 return IT_INT; 00158 } 00159 00160 forceinline IntType 00161 s_type(int n) { 00162 if ((n > SCHAR_MIN) && (n < SCHAR_MAX)) 00163 return IT_CHAR; 00164 else if ((n > SHRT_MIN) && (n < SHRT_MAX)) 00165 return IT_SHRT; 00166 else 00167 return IT_INT; 00168 } 00169 00170 }} 00171 00172 // STATISTICS: support-any