1 /************************************************************************* 2 * 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * Copyright 2000, 2010 Oracle and/or its affiliates. 6 * 7 * OpenOffice.org - a multi-platform office productivity suite 8 * 9 * This file is part of OpenOffice.org. 10 * 11 * OpenOffice.org is free software: you can redistribute it and/or modify 12 * it under the terms of the GNU Lesser General Public License version 3 13 * only, as published by the Free Software Foundation. 14 * 15 * OpenOffice.org is distributed in the hope that it will be useful, 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 * GNU Lesser General Public License version 3 for more details 19 * (a copy is included in the LICENSE file that accompanied this code). 20 * 21 * You should have received a copy of the GNU Lesser General Public License 22 * version 3 along with OpenOffice.org. If not, see 23 * <http://www.openoffice.org/license.html> 24 * for a copy of the LGPLv3 License. 25 * 26 ************************************************************************/ 27 #ifndef _BIGINT_HXX 28 #define _BIGINT_HXX 29 30 #include <climits> 31 #include "tools/toolsdllapi.h" 32 #include <tools/solar.h> 33 #include <tools/string.hxx> 34 35 class SvStream; 36 #ifdef _TLBIGINT_INT64 37 struct SbxINT64; 38 struct SbxUINT64; 39 namespace binfilter { class SbxINT64Converter; } 40 #endif 41 42 // ---------- 43 // - BigInt - 44 // ---------- 45 46 #define MAX_DIGITS 8 47 48 class Fraction; 49 50 class TOOLS_DLLPUBLIC BigInt 51 { 52 #ifdef _TLBIGINT_INT64 53 friend class ::binfilter::SbxINT64Converter; 54 #endif 55 56 private: 57 long nVal; 58 unsigned short nNum[MAX_DIGITS]; 59 sal_uInt8 nLen : 5; // Aktuelle Laenge 60 sal_Bool bIsNeg : 1, // Is Sign negative 61 bIsBig : 1, // sal_True == BigInt 62 bIsSet : 1; // Not "Null" (not not 0) 63 64 TOOLS_DLLPRIVATE void MakeBigInt(BigInt const &); 65 TOOLS_DLLPRIVATE void Normalize(); 66 TOOLS_DLLPRIVATE void Mult(BigInt const &, sal_uInt16); 67 TOOLS_DLLPRIVATE void Div(sal_uInt16, sal_uInt16 &); 68 TOOLS_DLLPRIVATE sal_Bool IsLess(BigInt const &) const; 69 TOOLS_DLLPRIVATE void AddLong(BigInt &, BigInt &); 70 TOOLS_DLLPRIVATE void SubLong(BigInt &, BigInt &); 71 TOOLS_DLLPRIVATE void MultLong(BigInt const &, BigInt &) const; 72 TOOLS_DLLPRIVATE void DivLong(BigInt const &, BigInt &) const; 73 TOOLS_DLLPRIVATE void ModLong(BigInt const &, BigInt &) const; 74 TOOLS_DLLPRIVATE sal_Bool ABS_IsLess(BigInt const &) const; 75 76 public: 77 BigInt(); 78 BigInt( short nVal ); 79 BigInt( long nVal ); 80 BigInt( int nVal ); 81 BigInt( double nVal ); 82 BigInt( sal_uInt16 nVal ); 83 BigInt( sal_uInt32 nVal ); 84 BigInt( const BigInt& rBigInt ); 85 BigInt( const ByteString& rString ); 86 BigInt( const UniString& rString ); 87 #ifdef _TLBIGINT_INT64 88 BigInt( const SbxINT64 &r ); 89 BigInt( const SbxUINT64 &r ); 90 #endif 91 92 operator short() const; 93 operator long() const; 94 operator int() const; 95 operator double() const; 96 operator sal_uInt16() const; 97 operator sal_uIntPtr() const; 98 99 void Set( sal_Bool bSet ) { bIsSet = bSet; } 100 ByteString GetByteString() const; 101 UniString GetString() const; 102 103 sal_Bool IsSet() const { return bIsSet; } 104 sal_Bool IsNeg() const; 105 sal_Bool IsZero() const; 106 sal_Bool IsOne() const; 107 sal_Bool IsLong() const { return !bIsBig; } 108 void Abs(); 109 void DivMod( const BigInt &rDivisor, BigInt &rMod ); 110 #ifdef _TLBIGINT_INT64 111 sal_Bool INT64 ( SbxINT64 *p ) const; 112 sal_Bool UINT64( SbxUINT64 *p ) const; 113 #endif 114 115 BigInt& operator =( const BigInt& rVal ); 116 BigInt& operator +=( const BigInt& rVal ); 117 BigInt& operator -=( const BigInt& rVal ); 118 BigInt& operator *=( const BigInt& rVal ); 119 BigInt& operator /=( const BigInt& rVal ); 120 BigInt& operator %=( const BigInt& rVal ); 121 122 BigInt& operator =( const short nValue ); 123 BigInt& operator =( const long nValue ); 124 BigInt& operator =( const int nValue ); 125 BigInt& operator =( const sal_uInt16 nValue ); 126 127 friend inline BigInt operator +( const BigInt& rVal1, const BigInt& rVal2 ); 128 friend inline BigInt operator -( const BigInt& rVal1, const BigInt& rVal2 ); 129 friend inline BigInt operator *( const BigInt& rVal1, const BigInt& rVal2 ); 130 friend inline BigInt operator /( const BigInt& rVal1, const BigInt& rVal2 ); 131 friend inline BigInt operator %( const BigInt& rVal1, const BigInt& rVal2 ); 132 133 TOOLS_DLLPUBLIC friend sal_Bool operator==( const BigInt& rVal1, const BigInt& rVal2 ); 134 friend inline sal_Bool operator!=( const BigInt& rVal1, const BigInt& rVal2 ); 135 TOOLS_DLLPUBLIC friend sal_Bool operator< ( const BigInt& rVal1, const BigInt& rVal2 ); 136 TOOLS_DLLPUBLIC friend sal_Bool operator> ( const BigInt& rVal1, const BigInt& rVal2 ); 137 friend inline sal_Bool operator<=( const BigInt& rVal1, const BigInt& rVal2 ); 138 friend inline sal_Bool operator>=( const BigInt& rVal1, const BigInt& rVal2 ); 139 140 friend class Fraction; 141 }; 142 143 inline BigInt::BigInt() 144 { 145 bIsSet = sal_False; 146 bIsBig = sal_False; 147 nVal = 0; 148 } 149 150 inline BigInt::BigInt( short nValue ) 151 { 152 bIsSet = sal_True; 153 bIsBig = sal_False; 154 nVal = nValue; 155 } 156 157 inline BigInt::BigInt( long nValue ) 158 { 159 bIsSet = sal_True; 160 bIsBig = sal_False; 161 nVal = nValue; 162 } 163 164 inline BigInt::BigInt( int nValue ) 165 { 166 bIsSet = sal_True; 167 bIsBig = sal_False; 168 nVal = nValue; 169 } 170 171 inline BigInt::BigInt( sal_uInt16 nValue ) 172 { 173 bIsSet = sal_True; 174 bIsBig = sal_False; 175 nVal = nValue; 176 } 177 178 inline BigInt::operator short() const 179 { 180 if ( !bIsBig && nVal >= SHRT_MIN && nVal <= SHRT_MAX ) 181 return (short)nVal; 182 else 183 return 0; 184 } 185 186 inline BigInt::operator long() const 187 { 188 if ( !bIsBig ) 189 return nVal; 190 else 191 return 0; 192 } 193 194 inline BigInt::operator int() const 195 { 196 if ( !bIsBig && (nVal == (long)(int)nVal) ) 197 return (int)nVal; 198 else 199 return 0; 200 } 201 202 inline BigInt::operator sal_uInt16() const 203 { 204 if ( !bIsBig && nVal >= 0 && nVal <= USHRT_MAX ) 205 return (sal_uInt16)nVal; 206 else 207 return 0; 208 } 209 210 inline BigInt& BigInt::operator =( const short nValue ) 211 { 212 bIsSet = sal_True; 213 bIsBig = sal_False; 214 nVal = nValue; 215 216 return *this; 217 } 218 219 inline BigInt& BigInt::operator =( const long nValue ) 220 { 221 bIsSet = sal_True; 222 bIsBig = sal_False; 223 nVal = nValue; 224 225 return *this; 226 } 227 228 inline BigInt& BigInt::operator =( const int nValue ) 229 { 230 bIsSet = sal_True; 231 bIsBig = sal_False; 232 nVal = nValue; 233 234 return *this; 235 } 236 237 inline BigInt& BigInt::operator =( const sal_uInt16 nValue ) 238 { 239 bIsSet = sal_True; 240 bIsBig = sal_False; 241 nVal = nValue; 242 243 return *this; 244 } 245 246 inline sal_Bool BigInt::IsNeg() const 247 { 248 if ( !bIsBig ) 249 return (nVal < 0); 250 else 251 return (sal_Bool)bIsNeg; 252 } 253 254 inline sal_Bool BigInt::IsZero() const 255 { 256 if ( bIsBig ) 257 return sal_False; 258 else 259 return (nVal == 0); 260 } 261 262 inline sal_Bool BigInt::IsOne() const 263 { 264 if ( bIsBig ) 265 return sal_False; 266 else 267 return (nVal == 1); 268 } 269 270 inline void BigInt::Abs() 271 { 272 if ( bIsBig ) 273 bIsNeg = sal_False; 274 else if ( nVal < 0 ) 275 nVal = -nVal; 276 } 277 278 inline BigInt operator+( const BigInt &rVal1, const BigInt &rVal2 ) 279 { 280 BigInt aErg( rVal1 ); 281 aErg += rVal2; 282 return aErg; 283 } 284 285 inline BigInt operator-( const BigInt &rVal1, const BigInt &rVal2 ) 286 { 287 BigInt aErg( rVal1 ); 288 aErg -= rVal2; 289 return aErg; 290 } 291 292 inline BigInt operator*( const BigInt &rVal1, const BigInt &rVal2 ) 293 { 294 BigInt aErg( rVal1 ); 295 aErg *= rVal2; 296 return aErg; 297 } 298 299 inline BigInt operator/( const BigInt &rVal1, const BigInt &rVal2 ) 300 { 301 BigInt aErg( rVal1 ); 302 aErg /= rVal2; 303 return aErg; 304 } 305 306 inline BigInt operator%( const BigInt &rVal1, const BigInt &rVal2 ) 307 { 308 BigInt aErg( rVal1 ); 309 aErg %= rVal2; 310 return aErg; 311 } 312 313 inline sal_Bool operator!=( const BigInt& rVal1, const BigInt& rVal2 ) 314 { 315 return !(rVal1 == rVal2); 316 } 317 318 inline sal_Bool operator<=( const BigInt& rVal1, const BigInt& rVal2 ) 319 { 320 return !( rVal1 > rVal2); 321 } 322 323 inline sal_Bool operator>=( const BigInt& rVal1, const BigInt& rVal2 ) 324 { 325 return !(rVal1 < rVal2); 326 } 327 328 #endif 329