Branch libreoffice-5-0-4
[LibreOffice.git] / include / tools / bigint.hxx
blobac0cb9297d563265eb954f0652b3769a7aae8820
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
19 #ifndef INCLUDED_TOOLS_BIGINT_HXX
20 #define INCLUDED_TOOLS_BIGINT_HXX
22 #include <climits>
23 #include <rtl/ustring.hxx>
24 #include <tools/toolsdllapi.h>
25 #include <tools/solar.h>
27 class SvStream;
29 #define MAX_DIGITS 8
31 class Fraction;
33 class TOOLS_DLLPUBLIC SAL_WARN_UNUSED BigInt
35 private:
36 long nVal;
37 unsigned short nNum[MAX_DIGITS];
38 sal_uInt8 nLen : 5; // current length
39 bool bIsNeg : 1, // Is Sign negative?
40 bIsBig : 1, // sal_True == BigInt
41 bIsSet : 1; // Not "Null" (not "not 0")
43 TOOLS_DLLPRIVATE void MakeBigInt(BigInt const &);
44 TOOLS_DLLPRIVATE void Normalize();
45 TOOLS_DLLPRIVATE void Mult(BigInt const &, sal_uInt16);
46 TOOLS_DLLPRIVATE void Div(sal_uInt16, sal_uInt16 &);
47 TOOLS_DLLPRIVATE bool IsLess(BigInt const &) const;
48 TOOLS_DLLPRIVATE void AddLong(BigInt &, BigInt &);
49 TOOLS_DLLPRIVATE void SubLong(BigInt &, BigInt &);
50 TOOLS_DLLPRIVATE void MultLong(BigInt const &, BigInt &) const;
51 TOOLS_DLLPRIVATE void DivLong(BigInt const &, BigInt &) const;
52 TOOLS_DLLPRIVATE void ModLong(BigInt const &, BigInt &) const;
53 TOOLS_DLLPRIVATE bool ABS_IsLess(BigInt const &) const;
55 public:
56 BigInt()
57 : nVal(0)
58 , nLen(0)
59 , bIsNeg(false)
60 , bIsBig(false)
61 , bIsSet(false)
65 BigInt(short nValue)
66 : nVal(nValue)
67 , nLen(0)
68 , bIsNeg(false)
69 , bIsBig(false)
70 , bIsSet(true)
74 BigInt(long nValue)
75 : nVal(nValue)
76 , nLen(0)
77 , bIsNeg(false)
78 , bIsBig(false)
79 , bIsSet(true)
83 BigInt(int nValue)
84 : nVal(nValue)
85 , nLen(0)
86 , bIsNeg(false)
87 , bIsBig(false)
88 , bIsSet(true)
92 BigInt( double nVal );
94 BigInt(sal_uInt16 nValue)
95 : nVal(nValue)
96 , nLen(0)
97 , bIsNeg(false)
98 , bIsBig(false)
99 , bIsSet(true)
103 BigInt( sal_uInt32 nVal );
104 #if SAL_TYPES_SIZEOFLONG < SAL_TYPES_SIZEOFLONGLONG
105 BigInt( long long nVal );
106 #endif
107 BigInt( const BigInt& rBigInt );
108 BigInt( const OUString& rString );
110 operator short() const;
111 operator long() const;
112 operator int() const;
113 operator double() const;
114 operator sal_uInt16() const;
115 operator sal_uIntPtr() const;
117 void Set( bool bSet ) { bIsSet = bSet; }
119 bool IsSet() const { return (bool)bIsSet; }
120 bool IsNeg() const;
121 bool IsZero() const;
122 bool IsOne() const;
123 bool IsLong() const { return !((bool)bIsBig); }
124 void Abs();
126 BigInt& operator =( const BigInt& rVal );
127 BigInt& operator +=( const BigInt& rVal );
128 BigInt& operator -=( const BigInt& rVal );
129 BigInt& operator *=( const BigInt& rVal );
130 BigInt& operator /=( const BigInt& rVal );
131 BigInt& operator %=( const BigInt& rVal );
133 BigInt& operator =( const short nValue );
134 BigInt& operator =( const long nValue );
135 BigInt& operator =( const int nValue );
136 BigInt& operator =( const sal_uInt16 nValue );
138 friend inline BigInt operator +( const BigInt& rVal1, const BigInt& rVal2 );
139 friend inline BigInt operator -( const BigInt& rVal1, const BigInt& rVal2 );
140 friend inline BigInt operator *( const BigInt& rVal1, const BigInt& rVal2 );
141 friend inline BigInt operator /( const BigInt& rVal1, const BigInt& rVal2 );
142 friend inline BigInt operator %( const BigInt& rVal1, const BigInt& rVal2 );
144 TOOLS_DLLPUBLIC friend bool operator==( const BigInt& rVal1, const BigInt& rVal2 );
145 friend inline bool operator!=( const BigInt& rVal1, const BigInt& rVal2 );
146 TOOLS_DLLPUBLIC friend bool operator< ( const BigInt& rVal1, const BigInt& rVal2 );
147 TOOLS_DLLPUBLIC friend bool operator> ( const BigInt& rVal1, const BigInt& rVal2 );
148 friend inline bool operator<=( const BigInt& rVal1, const BigInt& rVal2 );
149 friend inline bool operator>=( const BigInt& rVal1, const BigInt& rVal2 );
151 friend class Fraction;
154 inline BigInt::operator short() const
156 if ( !bIsBig && nVal >= SHRT_MIN && nVal <= SHRT_MAX )
157 return (short)nVal;
158 else
159 return 0;
162 inline BigInt::operator long() const
164 if ( !bIsBig )
165 return nVal;
166 else
167 return 0;
170 inline BigInt::operator int() const
172 if ( !bIsBig && (nVal == (long)(int)nVal) )
173 return (int)nVal;
174 else
175 return 0;
178 inline BigInt::operator sal_uInt16() const
180 if ( !bIsBig && nVal >= 0 && nVal <= (long)USHRT_MAX )
181 return (sal_uInt16)nVal;
182 else
183 return 0;
186 inline BigInt& BigInt::operator =( const short nValue )
188 bIsSet = true;
189 bIsBig = false;
190 nVal = nValue;
192 return *this;
195 inline BigInt& BigInt::operator =( const long nValue )
197 bIsSet = true;
198 bIsBig = false;
199 nVal = nValue;
201 return *this;
204 inline BigInt& BigInt::operator =( const int nValue )
206 bIsSet = true;
207 bIsBig = false;
208 nVal = nValue;
210 return *this;
213 inline BigInt& BigInt::operator =( const sal_uInt16 nValue )
215 bIsSet = true;
216 bIsBig = false;
217 nVal = nValue;
219 return *this;
222 inline bool BigInt::IsNeg() const
224 if ( !bIsBig )
225 return (nVal < 0);
226 else
227 return (bool)bIsNeg;
230 inline bool BigInt::IsZero() const
232 if ( bIsBig )
233 return false;
234 else
235 return (nVal == 0);
238 inline bool BigInt::IsOne() const
240 if ( bIsBig )
241 return false;
242 else
243 return (nVal == 1);
246 inline void BigInt::Abs()
248 if ( bIsBig )
249 bIsNeg = false;
250 else if ( nVal < 0 )
251 nVal = -nVal;
254 inline BigInt operator+( const BigInt &rVal1, const BigInt &rVal2 )
256 BigInt aErg( rVal1 );
257 aErg += rVal2;
258 return aErg;
261 inline BigInt operator-( const BigInt &rVal1, const BigInt &rVal2 )
263 BigInt aErg( rVal1 );
264 aErg -= rVal2;
265 return aErg;
268 inline BigInt operator*( const BigInt &rVal1, const BigInt &rVal2 )
270 BigInt aErg( rVal1 );
271 aErg *= rVal2;
272 return aErg;
275 inline BigInt operator/( const BigInt &rVal1, const BigInt &rVal2 )
277 BigInt aErg( rVal1 );
278 aErg /= rVal2;
279 return aErg;
282 inline BigInt operator%( const BigInt &rVal1, const BigInt &rVal2 )
284 BigInt aErg( rVal1 );
285 aErg %= rVal2;
286 return aErg;
289 inline bool operator!=( const BigInt& rVal1, const BigInt& rVal2 )
291 return !(rVal1 == rVal2);
294 inline bool operator<=( const BigInt& rVal1, const BigInt& rVal2 )
296 return !( rVal1 > rVal2);
299 inline bool operator>=( const BigInt& rVal1, const BigInt& rVal2 )
301 return !(rVal1 < rVal2);
304 #endif
306 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */