Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*
3 : : * This file is part of the LibreOffice project.
4 : : *
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/.
8 : : *
9 : : * This file incorporates work covered by the following license notice:
10 : : *
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 .
18 : : */
19 : : #ifndef _BIGINT_HXX
20 : : #define _BIGINT_HXX
21 : :
22 : : #include <climits>
23 : : #include <rtl/ustring.hxx>
24 : : #include "tools/toolsdllapi.h"
25 : : #include <tools/solar.h>
26 : :
27 : : class SvStream;
28 : :
29 : : #ifdef _TLBIGINT_INT64
30 : : struct SbxINT64;
31 : : struct SbxUINT64;
32 : : namespace binfilter { class SbxINT64Converter; }
33 : : #endif
34 : :
35 : : #define MAX_DIGITS 8
36 : :
37 : : class Fraction;
38 : :
39 : : class TOOLS_DLLPUBLIC BigInt
40 : : {
41 : : #ifdef _TLBIGINT_INT64
42 : : friend class ::binfilter::SbxINT64Converter;
43 : : #endif
44 : :
45 : : private:
46 : : long nVal;
47 : : unsigned short nNum[MAX_DIGITS];
48 : : sal_uInt8 nLen : 5; // current length
49 : : sal_Bool bIsNeg : 1, // Is Sign negative?
50 : : bIsBig : 1, // sal_True == BigInt
51 : : bIsSet : 1; // Not "Null" (not "not 0")
52 : :
53 : : TOOLS_DLLPRIVATE void MakeBigInt(BigInt const &);
54 : : TOOLS_DLLPRIVATE void Normalize();
55 : : TOOLS_DLLPRIVATE void Mult(BigInt const &, sal_uInt16);
56 : : TOOLS_DLLPRIVATE void Div(sal_uInt16, sal_uInt16 &);
57 : : TOOLS_DLLPRIVATE sal_Bool IsLess(BigInt const &) const;
58 : : TOOLS_DLLPRIVATE void AddLong(BigInt &, BigInt &);
59 : : TOOLS_DLLPRIVATE void SubLong(BigInt &, BigInt &);
60 : : TOOLS_DLLPRIVATE void MultLong(BigInt const &, BigInt &) const;
61 : : TOOLS_DLLPRIVATE void DivLong(BigInt const &, BigInt &) const;
62 : : TOOLS_DLLPRIVATE void ModLong(BigInt const &, BigInt &) const;
63 : : TOOLS_DLLPRIVATE sal_Bool ABS_IsLess(BigInt const &) const;
64 : :
65 : : public:
66 : : BigInt();
67 : : BigInt( short nVal );
68 : : BigInt( long nVal );
69 : : BigInt( int nVal );
70 : : BigInt( double nVal );
71 : : BigInt( sal_uInt16 nVal );
72 : : BigInt( sal_uInt32 nVal );
73 : : BigInt( const BigInt& rBigInt );
74 : : BigInt( const rtl::OUString& rString );
75 : : #ifdef _TLBIGINT_INT64
76 : : BigInt( const SbxINT64 &r );
77 : : BigInt( const SbxUINT64 &r );
78 : : #endif
79 : :
80 : : operator short() const;
81 : : operator long() const;
82 : : operator int() const;
83 : : operator double() const;
84 : : operator sal_uInt16() const;
85 : : operator sal_uIntPtr() const;
86 : :
87 : : void Set( sal_Bool bSet ) { bIsSet = bSet; }
88 : : rtl::OUString GetString() const;
89 : :
90 : : sal_Bool IsSet() const { return bIsSet; }
91 : : sal_Bool IsNeg() const;
92 : : sal_Bool IsZero() const;
93 : : sal_Bool IsOne() const;
94 : : sal_Bool IsLong() const { return !bIsBig; }
95 : : void Abs();
96 : : #ifdef _TLBIGINT_INT64
97 : : sal_Bool INT64 ( SbxINT64 *p ) const;
98 : : sal_Bool UINT64( SbxUINT64 *p ) const;
99 : : #endif
100 : :
101 : : BigInt& operator =( const BigInt& rVal );
102 : : BigInt& operator +=( const BigInt& rVal );
103 : : BigInt& operator -=( const BigInt& rVal );
104 : : BigInt& operator *=( const BigInt& rVal );
105 : : BigInt& operator /=( const BigInt& rVal );
106 : : BigInt& operator %=( const BigInt& rVal );
107 : :
108 : : BigInt& operator =( const short nValue );
109 : : BigInt& operator =( const long nValue );
110 : : BigInt& operator =( const int nValue );
111 : : BigInt& operator =( const sal_uInt16 nValue );
112 : :
113 : : friend inline BigInt operator +( const BigInt& rVal1, const BigInt& rVal2 );
114 : : friend inline BigInt operator -( const BigInt& rVal1, const BigInt& rVal2 );
115 : : friend inline BigInt operator *( const BigInt& rVal1, const BigInt& rVal2 );
116 : : friend inline BigInt operator /( const BigInt& rVal1, const BigInt& rVal2 );
117 : : friend inline BigInt operator %( const BigInt& rVal1, const BigInt& rVal2 );
118 : :
119 : : TOOLS_DLLPUBLIC friend sal_Bool operator==( const BigInt& rVal1, const BigInt& rVal2 );
120 : : friend inline sal_Bool operator!=( const BigInt& rVal1, const BigInt& rVal2 );
121 : : TOOLS_DLLPUBLIC friend sal_Bool operator< ( const BigInt& rVal1, const BigInt& rVal2 );
122 : : TOOLS_DLLPUBLIC friend sal_Bool operator> ( const BigInt& rVal1, const BigInt& rVal2 );
123 : : friend inline sal_Bool operator<=( const BigInt& rVal1, const BigInt& rVal2 );
124 : : friend inline sal_Bool operator>=( const BigInt& rVal1, const BigInt& rVal2 );
125 : :
126 : : friend class Fraction;
127 : : };
128 : :
129 : 0 : inline BigInt::BigInt()
130 : : {
131 : 0 : bIsSet = sal_False;
132 : 0 : bIsBig = sal_False;
133 : 0 : nVal = 0;
134 : 0 : }
135 : :
136 : 0 : inline BigInt::BigInt( short nValue )
137 : : {
138 : 0 : bIsSet = sal_True;
139 : 0 : bIsBig = sal_False;
140 : 0 : nVal = nValue;
141 : 0 : }
142 : :
143 : 1293232 : inline BigInt::BigInt( long nValue )
144 : : {
145 : 1293232 : bIsSet = sal_True;
146 : 1293232 : bIsBig = sal_False;
147 : 1293232 : nVal = nValue;
148 : 1293232 : }
149 : :
150 : 2072 : inline BigInt::BigInt( int nValue )
151 : : {
152 : 2072 : bIsSet = sal_True;
153 : 2072 : bIsBig = sal_False;
154 : 2072 : nVal = nValue;
155 : 2072 : }
156 : :
157 : : inline BigInt::BigInt( sal_uInt16 nValue )
158 : : {
159 : : bIsSet = sal_True;
160 : : bIsBig = sal_False;
161 : : nVal = nValue;
162 : : }
163 : :
164 : 0 : inline BigInt::operator short() const
165 : : {
166 [ # # ][ # # ]: 0 : if ( !bIsBig && nVal >= SHRT_MIN && nVal <= SHRT_MAX )
[ # # ]
167 : 0 : return (short)nVal;
168 : : else
169 : 0 : return 0;
170 : : }
171 : :
172 : 323137 : inline BigInt::operator long() const
173 : : {
174 [ + - ]: 323137 : if ( !bIsBig )
175 : 323137 : return nVal;
176 : : else
177 : 323137 : return 0;
178 : : }
179 : :
180 : : inline BigInt::operator int() const
181 : : {
182 : : if ( !bIsBig && (nVal == (long)(int)nVal) )
183 : : return (int)nVal;
184 : : else
185 : : return 0;
186 : : }
187 : :
188 : : inline BigInt::operator sal_uInt16() const
189 : : {
190 : : if ( !bIsBig && nVal >= 0 && nVal <= USHRT_MAX )
191 : : return (sal_uInt16)nVal;
192 : : else
193 : : return 0;
194 : : }
195 : :
196 : : inline BigInt& BigInt::operator =( const short nValue )
197 : : {
198 : : bIsSet = sal_True;
199 : : bIsBig = sal_False;
200 : : nVal = nValue;
201 : :
202 : : return *this;
203 : : }
204 : :
205 : 0 : inline BigInt& BigInt::operator =( const long nValue )
206 : : {
207 : 0 : bIsSet = sal_True;
208 : 0 : bIsBig = sal_False;
209 : 0 : nVal = nValue;
210 : :
211 : 0 : return *this;
212 : : }
213 : :
214 : 0 : inline BigInt& BigInt::operator =( const int nValue )
215 : : {
216 : 0 : bIsSet = sal_True;
217 : 0 : bIsBig = sal_False;
218 : 0 : nVal = nValue;
219 : :
220 : 0 : return *this;
221 : : }
222 : :
223 : : inline BigInt& BigInt::operator =( const sal_uInt16 nValue )
224 : : {
225 : : bIsSet = sal_True;
226 : : bIsBig = sal_False;
227 : : nVal = nValue;
228 : :
229 : : return *this;
230 : : }
231 : :
232 : 3546 : inline sal_Bool BigInt::IsNeg() const
233 : : {
234 [ + + ]: 3546 : if ( !bIsBig )
235 : 3356 : return (nVal < 0);
236 : : else
237 : 3546 : return (sal_Bool)bIsNeg;
238 : : }
239 : :
240 : 0 : inline sal_Bool BigInt::IsZero() const
241 : : {
242 [ # # ]: 0 : if ( bIsBig )
243 : 0 : return sal_False;
244 : : else
245 : 0 : return (nVal == 0);
246 : : }
247 : :
248 : : inline sal_Bool BigInt::IsOne() const
249 : : {
250 : : if ( bIsBig )
251 : : return sal_False;
252 : : else
253 : : return (nVal == 1);
254 : : }
255 : :
256 : 0 : inline void BigInt::Abs()
257 : : {
258 [ # # ]: 0 : if ( bIsBig )
259 : 0 : bIsNeg = sal_False;
260 [ # # ]: 0 : else if ( nVal < 0 )
261 : 0 : nVal = -nVal;
262 : 0 : }
263 : :
264 : 2012 : inline BigInt operator+( const BigInt &rVal1, const BigInt &rVal2 )
265 : : {
266 : 2012 : BigInt aErg( rVal1 );
267 : 2012 : aErg += rVal2;
268 : 2012 : return aErg;
269 : : }
270 : :
271 : : inline BigInt operator-( const BigInt &rVal1, const BigInt &rVal2 )
272 : : {
273 : : BigInt aErg( rVal1 );
274 : : aErg -= rVal2;
275 : : return aErg;
276 : : }
277 : :
278 : 0 : inline BigInt operator*( const BigInt &rVal1, const BigInt &rVal2 )
279 : : {
280 : 0 : BigInt aErg( rVal1 );
281 : 0 : aErg *= rVal2;
282 : 0 : return aErg;
283 : : }
284 : :
285 : 2952 : inline BigInt operator/( const BigInt &rVal1, const BigInt &rVal2 )
286 : : {
287 : 2952 : BigInt aErg( rVal1 );
288 : 2952 : aErg /= rVal2;
289 : 2952 : 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 : 0 : inline sal_Bool operator!=( const BigInt& rVal1, const BigInt& rVal2 )
300 : : {
301 : 0 : return !(rVal1 == rVal2);
302 : : }
303 : :
304 : : inline sal_Bool operator<=( const BigInt& rVal1, const BigInt& rVal2 )
305 : : {
306 : : return !( rVal1 > rVal2);
307 : : }
308 : :
309 : 246 : inline sal_Bool operator>=( const BigInt& rVal1, const BigInt& rVal2 )
310 : : {
311 : 246 : return !(rVal1 < rVal2);
312 : : }
313 : :
314 : : #endif
315 : :
316 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|