1*8b851043SAndrew Rist /**************************************************************
2cdf0e10cSrcweir *
3*8b851043SAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one
4*8b851043SAndrew Rist * or more contributor license agreements. See the NOTICE file
5*8b851043SAndrew Rist * distributed with this work for additional information
6*8b851043SAndrew Rist * regarding copyright ownership. The ASF licenses this file
7*8b851043SAndrew Rist * to you under the Apache License, Version 2.0 (the
8*8b851043SAndrew Rist * "License"); you may not use this file except in compliance
9*8b851043SAndrew Rist * with the License. You may obtain a copy of the License at
10*8b851043SAndrew Rist *
11*8b851043SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0
12*8b851043SAndrew Rist *
13*8b851043SAndrew Rist * Unless required by applicable law or agreed to in writing,
14*8b851043SAndrew Rist * software distributed under the License is distributed on an
15*8b851043SAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*8b851043SAndrew Rist * KIND, either express or implied. See the License for the
17*8b851043SAndrew Rist * specific language governing permissions and limitations
18*8b851043SAndrew Rist * under the License.
19*8b851043SAndrew Rist *
20*8b851043SAndrew Rist *************************************************************/
21*8b851043SAndrew Rist
22*8b851043SAndrew Rist
23cdf0e10cSrcweir #ifndef _BIGINT_HXX
24cdf0e10cSrcweir #define _BIGINT_HXX
25cdf0e10cSrcweir
26cdf0e10cSrcweir #include <climits>
27cdf0e10cSrcweir #include "tools/toolsdllapi.h"
28cdf0e10cSrcweir #include <tools/solar.h>
29cdf0e10cSrcweir #include <tools/string.hxx>
30cdf0e10cSrcweir
31cdf0e10cSrcweir class SvStream;
32cdf0e10cSrcweir #ifdef _TLBIGINT_INT64
33cdf0e10cSrcweir struct SbxINT64;
34cdf0e10cSrcweir struct SbxUINT64;
35cdf0e10cSrcweir namespace binfilter { class SbxINT64Converter; }
36cdf0e10cSrcweir #endif
37cdf0e10cSrcweir
38cdf0e10cSrcweir // ----------
39cdf0e10cSrcweir // - BigInt -
40cdf0e10cSrcweir // ----------
41cdf0e10cSrcweir
42cdf0e10cSrcweir #define MAX_DIGITS 8
43cdf0e10cSrcweir
44cdf0e10cSrcweir class Fraction;
45cdf0e10cSrcweir
46cdf0e10cSrcweir class TOOLS_DLLPUBLIC BigInt
47cdf0e10cSrcweir {
48cdf0e10cSrcweir #ifdef _TLBIGINT_INT64
49cdf0e10cSrcweir friend class ::binfilter::SbxINT64Converter;
50cdf0e10cSrcweir #endif
51cdf0e10cSrcweir
52cdf0e10cSrcweir private:
53cdf0e10cSrcweir long nVal;
54cdf0e10cSrcweir unsigned short nNum[MAX_DIGITS];
55cdf0e10cSrcweir sal_uInt8 nLen : 5; // Aktuelle Laenge
56cdf0e10cSrcweir sal_Bool bIsNeg : 1, // Is Sign negative
57cdf0e10cSrcweir bIsBig : 1, // sal_True == BigInt
58cdf0e10cSrcweir bIsSet : 1; // Not "Null" (not not 0)
59cdf0e10cSrcweir
60cdf0e10cSrcweir TOOLS_DLLPRIVATE void MakeBigInt(BigInt const &);
61cdf0e10cSrcweir TOOLS_DLLPRIVATE void Normalize();
62cdf0e10cSrcweir TOOLS_DLLPRIVATE void Mult(BigInt const &, sal_uInt16);
63cdf0e10cSrcweir TOOLS_DLLPRIVATE void Div(sal_uInt16, sal_uInt16 &);
64cdf0e10cSrcweir TOOLS_DLLPRIVATE sal_Bool IsLess(BigInt const &) const;
65cdf0e10cSrcweir TOOLS_DLLPRIVATE void AddLong(BigInt &, BigInt &);
66cdf0e10cSrcweir TOOLS_DLLPRIVATE void SubLong(BigInt &, BigInt &);
67cdf0e10cSrcweir TOOLS_DLLPRIVATE void MultLong(BigInt const &, BigInt &) const;
68cdf0e10cSrcweir TOOLS_DLLPRIVATE void DivLong(BigInt const &, BigInt &) const;
69cdf0e10cSrcweir TOOLS_DLLPRIVATE void ModLong(BigInt const &, BigInt &) const;
70cdf0e10cSrcweir TOOLS_DLLPRIVATE sal_Bool ABS_IsLess(BigInt const &) const;
71cdf0e10cSrcweir
72cdf0e10cSrcweir public:
73cdf0e10cSrcweir BigInt();
74cdf0e10cSrcweir BigInt( short nVal );
75cdf0e10cSrcweir BigInt( long nVal );
76cdf0e10cSrcweir BigInt( int nVal );
77cdf0e10cSrcweir BigInt( double nVal );
78cdf0e10cSrcweir BigInt( sal_uInt16 nVal );
79cdf0e10cSrcweir BigInt( sal_uInt32 nVal );
80cdf0e10cSrcweir BigInt( const BigInt& rBigInt );
81cdf0e10cSrcweir BigInt( const ByteString& rString );
82cdf0e10cSrcweir BigInt( const UniString& rString );
83cdf0e10cSrcweir #ifdef _TLBIGINT_INT64
84cdf0e10cSrcweir BigInt( const SbxINT64 &r );
85cdf0e10cSrcweir BigInt( const SbxUINT64 &r );
86cdf0e10cSrcweir #endif
87cdf0e10cSrcweir
88cdf0e10cSrcweir operator short() const;
89cdf0e10cSrcweir operator long() const;
90cdf0e10cSrcweir operator int() const;
91cdf0e10cSrcweir operator double() const;
92cdf0e10cSrcweir operator sal_uInt16() const;
93cdf0e10cSrcweir operator sal_uIntPtr() const;
94cdf0e10cSrcweir
Set(sal_Bool bSet)95cdf0e10cSrcweir void Set( sal_Bool bSet ) { bIsSet = bSet; }
96cdf0e10cSrcweir ByteString GetByteString() const;
97cdf0e10cSrcweir UniString GetString() const;
98cdf0e10cSrcweir
IsSet() const99cdf0e10cSrcweir sal_Bool IsSet() const { return bIsSet; }
100cdf0e10cSrcweir sal_Bool IsNeg() const;
101cdf0e10cSrcweir sal_Bool IsZero() const;
102cdf0e10cSrcweir sal_Bool IsOne() const;
IsLong() const103cdf0e10cSrcweir sal_Bool IsLong() const { return !bIsBig; }
104cdf0e10cSrcweir void Abs();
105cdf0e10cSrcweir void DivMod( const BigInt &rDivisor, BigInt &rMod );
106cdf0e10cSrcweir #ifdef _TLBIGINT_INT64
107cdf0e10cSrcweir sal_Bool INT64 ( SbxINT64 *p ) const;
108cdf0e10cSrcweir sal_Bool UINT64( SbxUINT64 *p ) const;
109cdf0e10cSrcweir #endif
110cdf0e10cSrcweir
111cdf0e10cSrcweir BigInt& operator =( const BigInt& rVal );
112cdf0e10cSrcweir BigInt& operator +=( const BigInt& rVal );
113cdf0e10cSrcweir BigInt& operator -=( const BigInt& rVal );
114cdf0e10cSrcweir BigInt& operator *=( const BigInt& rVal );
115cdf0e10cSrcweir BigInt& operator /=( const BigInt& rVal );
116cdf0e10cSrcweir BigInt& operator %=( const BigInt& rVal );
117cdf0e10cSrcweir
118cdf0e10cSrcweir BigInt& operator =( const short nValue );
119cdf0e10cSrcweir BigInt& operator =( const long nValue );
120cdf0e10cSrcweir BigInt& operator =( const int nValue );
121cdf0e10cSrcweir BigInt& operator =( const sal_uInt16 nValue );
122cdf0e10cSrcweir
123cdf0e10cSrcweir friend inline BigInt operator +( const BigInt& rVal1, const BigInt& rVal2 );
124cdf0e10cSrcweir friend inline BigInt operator -( const BigInt& rVal1, const BigInt& rVal2 );
125cdf0e10cSrcweir friend inline BigInt operator *( const BigInt& rVal1, const BigInt& rVal2 );
126cdf0e10cSrcweir friend inline BigInt operator /( const BigInt& rVal1, const BigInt& rVal2 );
127cdf0e10cSrcweir friend inline BigInt operator %( const BigInt& rVal1, const BigInt& rVal2 );
128cdf0e10cSrcweir
129cdf0e10cSrcweir TOOLS_DLLPUBLIC friend sal_Bool operator==( const BigInt& rVal1, const BigInt& rVal2 );
130cdf0e10cSrcweir friend inline sal_Bool operator!=( const BigInt& rVal1, const BigInt& rVal2 );
131cdf0e10cSrcweir TOOLS_DLLPUBLIC friend sal_Bool operator< ( const BigInt& rVal1, const BigInt& rVal2 );
132cdf0e10cSrcweir TOOLS_DLLPUBLIC friend sal_Bool operator> ( const BigInt& rVal1, const BigInt& rVal2 );
133cdf0e10cSrcweir friend inline sal_Bool operator<=( const BigInt& rVal1, const BigInt& rVal2 );
134cdf0e10cSrcweir friend inline sal_Bool operator>=( const BigInt& rVal1, const BigInt& rVal2 );
135cdf0e10cSrcweir
136cdf0e10cSrcweir friend class Fraction;
137cdf0e10cSrcweir };
138cdf0e10cSrcweir
BigInt()139cdf0e10cSrcweir inline BigInt::BigInt()
140cdf0e10cSrcweir {
141cdf0e10cSrcweir bIsSet = sal_False;
142cdf0e10cSrcweir bIsBig = sal_False;
143cdf0e10cSrcweir nVal = 0;
144cdf0e10cSrcweir }
145cdf0e10cSrcweir
BigInt(short nValue)146cdf0e10cSrcweir inline BigInt::BigInt( short nValue )
147cdf0e10cSrcweir {
148cdf0e10cSrcweir bIsSet = sal_True;
149cdf0e10cSrcweir bIsBig = sal_False;
150cdf0e10cSrcweir nVal = nValue;
151cdf0e10cSrcweir }
152cdf0e10cSrcweir
BigInt(long nValue)153cdf0e10cSrcweir inline BigInt::BigInt( long nValue )
154cdf0e10cSrcweir {
155cdf0e10cSrcweir bIsSet = sal_True;
156cdf0e10cSrcweir bIsBig = sal_False;
157cdf0e10cSrcweir nVal = nValue;
158cdf0e10cSrcweir }
159cdf0e10cSrcweir
BigInt(int nValue)160cdf0e10cSrcweir inline BigInt::BigInt( int nValue )
161cdf0e10cSrcweir {
162cdf0e10cSrcweir bIsSet = sal_True;
163cdf0e10cSrcweir bIsBig = sal_False;
164cdf0e10cSrcweir nVal = nValue;
165cdf0e10cSrcweir }
166cdf0e10cSrcweir
BigInt(sal_uInt16 nValue)167cdf0e10cSrcweir inline BigInt::BigInt( sal_uInt16 nValue )
168cdf0e10cSrcweir {
169cdf0e10cSrcweir bIsSet = sal_True;
170cdf0e10cSrcweir bIsBig = sal_False;
171cdf0e10cSrcweir nVal = nValue;
172cdf0e10cSrcweir }
173cdf0e10cSrcweir
174cdf0e10cSrcweir inline BigInt::operator short() const
175cdf0e10cSrcweir {
176cdf0e10cSrcweir if ( !bIsBig && nVal >= SHRT_MIN && nVal <= SHRT_MAX )
177cdf0e10cSrcweir return (short)nVal;
178cdf0e10cSrcweir else
179cdf0e10cSrcweir return 0;
180cdf0e10cSrcweir }
181cdf0e10cSrcweir
182cdf0e10cSrcweir inline BigInt::operator long() const
183cdf0e10cSrcweir {
184cdf0e10cSrcweir if ( !bIsBig )
185cdf0e10cSrcweir return nVal;
186cdf0e10cSrcweir else
187cdf0e10cSrcweir return 0;
188cdf0e10cSrcweir }
189cdf0e10cSrcweir
190cdf0e10cSrcweir inline BigInt::operator int() const
191cdf0e10cSrcweir {
192cdf0e10cSrcweir if ( !bIsBig && (nVal == (long)(int)nVal) )
193cdf0e10cSrcweir return (int)nVal;
194cdf0e10cSrcweir else
195cdf0e10cSrcweir return 0;
196cdf0e10cSrcweir }
197cdf0e10cSrcweir
operator sal_uInt16() const198cdf0e10cSrcweir inline BigInt::operator sal_uInt16() const
199cdf0e10cSrcweir {
200cdf0e10cSrcweir if ( !bIsBig && nVal >= 0 && nVal <= USHRT_MAX )
201cdf0e10cSrcweir return (sal_uInt16)nVal;
202cdf0e10cSrcweir else
203cdf0e10cSrcweir return 0;
204cdf0e10cSrcweir }
205cdf0e10cSrcweir
operator =(const short nValue)206cdf0e10cSrcweir inline BigInt& BigInt::operator =( const short nValue )
207cdf0e10cSrcweir {
208cdf0e10cSrcweir bIsSet = sal_True;
209cdf0e10cSrcweir bIsBig = sal_False;
210cdf0e10cSrcweir nVal = nValue;
211cdf0e10cSrcweir
212cdf0e10cSrcweir return *this;
213cdf0e10cSrcweir }
214cdf0e10cSrcweir
operator =(const long nValue)215cdf0e10cSrcweir inline BigInt& BigInt::operator =( const long nValue )
216cdf0e10cSrcweir {
217cdf0e10cSrcweir bIsSet = sal_True;
218cdf0e10cSrcweir bIsBig = sal_False;
219cdf0e10cSrcweir nVal = nValue;
220cdf0e10cSrcweir
221cdf0e10cSrcweir return *this;
222cdf0e10cSrcweir }
223cdf0e10cSrcweir
operator =(const int nValue)224cdf0e10cSrcweir inline BigInt& BigInt::operator =( const int nValue )
225cdf0e10cSrcweir {
226cdf0e10cSrcweir bIsSet = sal_True;
227cdf0e10cSrcweir bIsBig = sal_False;
228cdf0e10cSrcweir nVal = nValue;
229cdf0e10cSrcweir
230cdf0e10cSrcweir return *this;
231cdf0e10cSrcweir }
232cdf0e10cSrcweir
operator =(const sal_uInt16 nValue)233cdf0e10cSrcweir inline BigInt& BigInt::operator =( const sal_uInt16 nValue )
234cdf0e10cSrcweir {
235cdf0e10cSrcweir bIsSet = sal_True;
236cdf0e10cSrcweir bIsBig = sal_False;
237cdf0e10cSrcweir nVal = nValue;
238cdf0e10cSrcweir
239cdf0e10cSrcweir return *this;
240cdf0e10cSrcweir }
241cdf0e10cSrcweir
IsNeg() const242cdf0e10cSrcweir inline sal_Bool BigInt::IsNeg() const
243cdf0e10cSrcweir {
244cdf0e10cSrcweir if ( !bIsBig )
245cdf0e10cSrcweir return (nVal < 0);
246cdf0e10cSrcweir else
247cdf0e10cSrcweir return (sal_Bool)bIsNeg;
248cdf0e10cSrcweir }
249cdf0e10cSrcweir
IsZero() const250cdf0e10cSrcweir inline sal_Bool BigInt::IsZero() const
251cdf0e10cSrcweir {
252cdf0e10cSrcweir if ( bIsBig )
253cdf0e10cSrcweir return sal_False;
254cdf0e10cSrcweir else
255cdf0e10cSrcweir return (nVal == 0);
256cdf0e10cSrcweir }
257cdf0e10cSrcweir
IsOne() const258cdf0e10cSrcweir inline sal_Bool BigInt::IsOne() const
259cdf0e10cSrcweir {
260cdf0e10cSrcweir if ( bIsBig )
261cdf0e10cSrcweir return sal_False;
262cdf0e10cSrcweir else
263cdf0e10cSrcweir return (nVal == 1);
264cdf0e10cSrcweir }
265cdf0e10cSrcweir
Abs()266cdf0e10cSrcweir inline void BigInt::Abs()
267cdf0e10cSrcweir {
268cdf0e10cSrcweir if ( bIsBig )
269cdf0e10cSrcweir bIsNeg = sal_False;
270cdf0e10cSrcweir else if ( nVal < 0 )
271cdf0e10cSrcweir nVal = -nVal;
272cdf0e10cSrcweir }
273cdf0e10cSrcweir
operator +(const BigInt & rVal1,const BigInt & rVal2)274cdf0e10cSrcweir inline BigInt operator+( const BigInt &rVal1, const BigInt &rVal2 )
275cdf0e10cSrcweir {
276cdf0e10cSrcweir BigInt aErg( rVal1 );
277cdf0e10cSrcweir aErg += rVal2;
278cdf0e10cSrcweir return aErg;
279cdf0e10cSrcweir }
280cdf0e10cSrcweir
operator -(const BigInt & rVal1,const BigInt & rVal2)281cdf0e10cSrcweir inline BigInt operator-( const BigInt &rVal1, const BigInt &rVal2 )
282cdf0e10cSrcweir {
283cdf0e10cSrcweir BigInt aErg( rVal1 );
284cdf0e10cSrcweir aErg -= rVal2;
285cdf0e10cSrcweir return aErg;
286cdf0e10cSrcweir }
287cdf0e10cSrcweir
operator *(const BigInt & rVal1,const BigInt & rVal2)288cdf0e10cSrcweir inline BigInt operator*( const BigInt &rVal1, const BigInt &rVal2 )
289cdf0e10cSrcweir {
290cdf0e10cSrcweir BigInt aErg( rVal1 );
291cdf0e10cSrcweir aErg *= rVal2;
292cdf0e10cSrcweir return aErg;
293cdf0e10cSrcweir }
294cdf0e10cSrcweir
operator /(const BigInt & rVal1,const BigInt & rVal2)295cdf0e10cSrcweir inline BigInt operator/( const BigInt &rVal1, const BigInt &rVal2 )
296cdf0e10cSrcweir {
297cdf0e10cSrcweir BigInt aErg( rVal1 );
298cdf0e10cSrcweir aErg /= rVal2;
299cdf0e10cSrcweir return aErg;
300cdf0e10cSrcweir }
301cdf0e10cSrcweir
operator %(const BigInt & rVal1,const BigInt & rVal2)302cdf0e10cSrcweir inline BigInt operator%( const BigInt &rVal1, const BigInt &rVal2 )
303cdf0e10cSrcweir {
304cdf0e10cSrcweir BigInt aErg( rVal1 );
305cdf0e10cSrcweir aErg %= rVal2;
306cdf0e10cSrcweir return aErg;
307cdf0e10cSrcweir }
308cdf0e10cSrcweir
operator !=(const BigInt & rVal1,const BigInt & rVal2)309cdf0e10cSrcweir inline sal_Bool operator!=( const BigInt& rVal1, const BigInt& rVal2 )
310cdf0e10cSrcweir {
311cdf0e10cSrcweir return !(rVal1 == rVal2);
312cdf0e10cSrcweir }
313cdf0e10cSrcweir
operator <=(const BigInt & rVal1,const BigInt & rVal2)314cdf0e10cSrcweir inline sal_Bool operator<=( const BigInt& rVal1, const BigInt& rVal2 )
315cdf0e10cSrcweir {
316cdf0e10cSrcweir return !( rVal1 > rVal2);
317cdf0e10cSrcweir }
318cdf0e10cSrcweir
operator >=(const BigInt & rVal1,const BigInt & rVal2)319cdf0e10cSrcweir inline sal_Bool operator>=( const BigInt& rVal1, const BigInt& rVal2 )
320cdf0e10cSrcweir {
321cdf0e10cSrcweir return !(rVal1 < rVal2);
322cdf0e10cSrcweir }
323cdf0e10cSrcweir
324cdf0e10cSrcweir #endif
325