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