BWAPI
|
00001 /**************************************************************************** 00002 * Core Library Version 1.7, August 2004 00003 * Copyright (c) 1995-2004 Exact Computation Project 00004 * All rights reserved. 00005 * 00006 * This file is part of CORE (http://cs.nyu.edu/exact/core/); you may 00007 * redistribute it under the terms of the Q Public License version 1.0. 00008 * See the file LICENSE.QPL distributed with CORE. 00009 * 00010 * Licensees holding a valid commercial license may use this file in 00011 * accordance with the commercial license agreement provided with the 00012 * software. 00013 * 00014 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE 00015 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 00016 * 00017 * 00018 * File: extLong.h 00019 * Synopsis: 00020 * An extended class for long 00021 * 00022 * Written by 00023 * Koji Ouchi <ouchi@simulation.nyu.edu> 00024 * Chee Yap <yap@cs.nyu.edu> 00025 * Igor Pechtchanski <pechtcha@cs.nyu.edu>, 00026 * Vijay Karamcheti <vijayk@cs.nyu.edu>, 00027 * Chen Li <chenli@cs.nyu.edu> 00028 * Zilin Du <zilin@cs.nyu.edu> 00029 * 00030 * WWW URL: http://cs.nyu.edu/exact/ 00031 * Email: exact@cs.nyu.edu 00032 * 00033 * $URL: svn+ssh://scm.gforge.inria.fr/svn/cgal/branches/CGAL-3.5-branch/Core/include/CGAL/CORE/extLong.h $ 00034 * $Id: extLong.h 37060 2007-03-13 18:10:39Z reichel $ 00035 ***************************************************************************/ 00036 00037 #ifndef _CORE_EXTLONG_H_ 00038 #define _CORE_EXTLONG_H_ 00039 00040 #include <CGAL/CORE/Impl.h> 00041 #include <CGAL/CORE/CoreAux.h> 00042 00043 CORE_BEGIN_NAMESPACE 00044 00045 #ifndef LONG_MAX 00046 #error "haven't define LONG_MAX" 00047 #endif 00048 00049 #ifndef LONG_MIN 00050 #error "haven't define LONG_MIN" 00051 #endif 00052 00053 // LONG_MAX and LONG_MIN is assumed in this class: 00054 00055 const long EXTLONG_MAX = LONG_MAX; 00056 const long EXTLONG_MIN = LONG_MIN + 1; 00057 const long EXTLONG_NAN = LONG_MIN; 00058 const unsigned long U_EXTLONG_MAX = LONG_MAX; 00059 00062 class extLong { 00063 private: 00064 long val; 00065 int flag; 00066 00071 static void add(extLong& z, long x, long y); 00072 00073 public: 00074 00076 00077 00078 extLong(); 00080 extLong(bool isNaN); 00082 extLong(int); 00084 extLong(unsigned int); 00086 extLong(long); 00088 extLong(unsigned long); 00090 00092 00093 extLong& operator +=(const extLong&); 00094 extLong& operator -=(const extLong&); 00095 extLong& operator *=(const extLong&); 00096 extLong& operator /=(const extLong&); 00098 00100 00101 extLong& operator++(); 00102 extLong operator++(int); 00103 extLong& operator--(); 00104 extLong operator--(int); 00105 extLong operator-() const; 00107 00109 00110 std::string toString() const { 00111 std::stringstream st; 00112 st << (*this); 00113 return st.str(); 00114 } 00115 long toLong() const; 00117 00119 00120 long asLong() const; 00121 bool isInfty() const; 00122 bool isTiny() const; 00123 bool isNaN() const; 00124 int sign() const; 00126 int cmp(const extLong &) const; 00128 00131 friend std::ostream& operator <<(std::ostream&, const extLong&); 00133 00134 static const extLong& getNaNLong(); 00135 static const extLong& getPosInfty(); 00136 static const extLong& getNegInfty(); 00137 }; 00138 00139 // constants (Globally) 00140 #define CORE_NaNLong extLong::getNaNLong() 00141 #define CORE_posInfty extLong::getPosInfty() 00142 #define CORE_negInfty extLong::getNegInfty() 00143 00144 const extLong EXTLONG_ZERO(0); 00145 const extLong EXTLONG_ONE(1); 00146 const extLong EXTLONG_TWO(2); 00147 const extLong EXTLONG_THREE(3); 00148 const extLong EXTLONG_FOUR(4); 00149 const extLong EXTLONG_FIVE(5); 00150 const extLong EXTLONG_SIX(6); 00151 const extLong EXTLONG_SEVEN(7); 00152 const extLong EXTLONG_EIGHT(8); 00153 00154 // inline functions 00155 00156 // private comparison function 00157 inline int extLong::cmp(const extLong& x) const { 00158 if (isNaN() || x.isNaN()) { 00159 core_error("Two extLong NaN's cannot be compared!", 00160 __FILE__, __LINE__, false); 00161 } 00162 return (val == x.val) ? 0 : ((val > x.val) ? 1 : -1); 00163 } 00164 00165 // default constructor (cheapest one) 00166 inline extLong::extLong() : val(0), flag(0) {} 00167 00168 inline extLong::extLong(int i) : val(i), flag(0) { 00169 if (val == EXTLONG_MAX) 00170 flag = 1; 00171 else if (val <= EXTLONG_MIN) 00172 flag = -1; 00173 } 00174 00175 inline extLong::extLong(unsigned int ui) : val(ui), flag(0) { 00176 if (val >= EXTLONG_MAX) { 00177 val = EXTLONG_MAX; 00178 flag = 1; 00179 } 00180 } 00181 00182 inline extLong::extLong(long l) : val(l), flag(0) { 00183 if (val >= EXTLONG_MAX) 00184 flag = 1; 00185 else if (val <= EXTLONG_MIN) 00186 flag = -1; 00187 } 00188 00189 inline extLong::extLong(unsigned long u) { 00190 if (u >= U_EXTLONG_MAX) { 00191 val = EXTLONG_MAX; 00192 flag = 1; 00193 } else { 00194 val = static_cast<long>(u); 00195 flag = 0; 00196 } 00197 } 00198 00199 // isNaN defaults to false 00200 inline extLong::extLong(bool isNaN) : val(0), flag(0) { 00201 if (isNaN) { 00202 val = EXTLONG_NAN; 00203 flag = 2; 00204 } 00205 } 00206 00207 // comparison operators 00208 inline bool operator== (const extLong& x, const extLong& y) { 00209 return x.cmp(y) == 0; 00210 } 00211 00212 inline bool operator!= (const extLong& x, const extLong& y) { 00213 return x.cmp(y) != 0; 00214 } 00215 00216 inline bool operator< (const extLong& x, const extLong& y) { 00217 return x.cmp(y) < 0; 00218 } 00219 00220 inline bool operator<= (const extLong& x, const extLong& y) { 00221 return x.cmp(y) <= 0; 00222 } 00223 00224 inline bool operator> (const extLong& x, const extLong& y) { 00225 return x.cmp(y) > 0; 00226 } 00227 00228 inline bool operator>= (const extLong& x, const extLong& y) { 00229 return x.cmp(y) >= 0; 00230 } 00231 00232 // arithmetic operators 00233 inline extLong operator+ (const extLong& x, const extLong& y) { 00234 return extLong(x)+=y; 00235 } 00236 00237 inline extLong operator- (const extLong& x, const extLong& y) { 00238 return extLong(x)-=y; 00239 } 00240 00241 inline extLong operator* (const extLong& x, const extLong& y) { 00242 return extLong(x)*=y; 00243 } 00244 00245 inline extLong operator/ (const extLong& x, const extLong& y) { 00246 return extLong(x)/=y; 00247 } 00248 00249 inline extLong& extLong::operator++ () { 00250 *this += 1; 00251 return *this; 00252 } 00253 00254 inline extLong extLong::operator++ (int) { 00255 extLong r(*this); 00256 *this += 1; 00257 return r; 00258 } 00259 00260 inline extLong& extLong::operator-- () { 00261 *this -= 1; 00262 return *this; 00263 } 00264 00265 inline extLong extLong::operator-- (int) { 00266 extLong r(*this); 00267 *this -= 1; 00268 return r; 00269 } 00270 00271 // conversion to long 00272 inline long extLong::toLong() const { 00273 return val; 00274 } 00275 00276 // builtin functions 00277 inline long extLong::asLong() const { 00278 return val; 00279 } 00280 00281 inline bool extLong::isInfty() const { 00282 return (flag == 1); 00283 } 00284 00285 inline bool extLong::isTiny() const { 00286 return (flag == -1); 00287 } 00288 00289 inline bool extLong::isNaN() const { 00290 return (flag == 2); 00291 } 00292 00293 CORE_END_NAMESPACE 00294 #endif // _CORE_EXTLONG_H_ 00295