LCOV - code coverage report
Current view: top level - usr/local/src/libreoffice/include/tools - bigint.hxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 32 72 44.4 %
Date: 2013-07-09 Functions: 8 17 47.1 %
Legend: Lines: hit not hit

          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             : #endif
      33             : 
      34             : #define MAX_DIGITS 8
      35             : 
      36             : class Fraction;
      37             : 
      38             : class TOOLS_DLLPUBLIC SAL_WARN_UNUSED BigInt
      39             : {
      40             : private:
      41             :     long            nVal;
      42             :     unsigned short  nNum[MAX_DIGITS];
      43             :     sal_uInt8       nLen        : 5;    // current length
      44             :     sal_Bool        bIsNeg      : 1,    // Is Sign negative?
      45             :                     bIsBig      : 1,    // sal_True == BigInt
      46             :                     bIsSet      : 1;    // Not "Null" (not "not 0")
      47             : 
      48             :     TOOLS_DLLPRIVATE void MakeBigInt(BigInt const &);
      49             :     TOOLS_DLLPRIVATE void Normalize();
      50             :     TOOLS_DLLPRIVATE void Mult(BigInt const &, sal_uInt16);
      51             :     TOOLS_DLLPRIVATE void Div(sal_uInt16, sal_uInt16 &);
      52             :     TOOLS_DLLPRIVATE bool IsLess(BigInt const &) const;
      53             :     TOOLS_DLLPRIVATE void AddLong(BigInt &, BigInt &);
      54             :     TOOLS_DLLPRIVATE void SubLong(BigInt &, BigInt &);
      55             :     TOOLS_DLLPRIVATE void MultLong(BigInt const &, BigInt &) const;
      56             :     TOOLS_DLLPRIVATE void DivLong(BigInt const &, BigInt &) const;
      57             :     TOOLS_DLLPRIVATE void ModLong(BigInt const &, BigInt &) const;
      58             :     TOOLS_DLLPRIVATE bool ABS_IsLess(BigInt const &) const;
      59             : 
      60             : public:
      61             :                     BigInt();
      62             :                     BigInt( short nVal );
      63             :                     BigInt( long nVal );
      64             :                     BigInt( int nVal );
      65             :                     BigInt( double nVal );
      66             :                     BigInt( sal_uInt16 nVal );
      67             :                     BigInt( sal_uInt32 nVal );
      68             :                     BigInt( const BigInt& rBigInt );
      69             :                     BigInt( const OUString& rString );
      70             : #ifdef _TLBIGINT_INT64
      71             :                     BigInt( const SbxINT64  &r );
      72             :                     BigInt( const SbxUINT64 &r );
      73             : #endif
      74             : 
      75             :     operator        short() const;
      76             :     operator        long()  const;
      77             :     operator        int()   const;
      78             :     operator        double() const;
      79             :     operator        sal_uInt16() const;
      80             :     operator        sal_uIntPtr() const;
      81             : 
      82             :     void            Set( bool bSet ) { bIsSet = bSet ? sal_True : sal_False; }
      83             :     OUString        GetString() const;
      84             : 
      85             :     bool        IsSet() const { return (bool)bIsSet; }
      86             :     bool        IsNeg() const;
      87             :     bool        IsZero() const;
      88             :     bool        IsOne() const;
      89             :     bool        IsLong() const { return !((bool)bIsBig); }
      90             :     void            Abs();
      91             : #ifdef _TLBIGINT_INT64
      92             :     bool        INT64 ( SbxINT64  *p ) const;
      93             :     bool        UINT64( SbxUINT64 *p ) const;
      94             : #endif
      95             : 
      96             :     BigInt&         operator  =( const BigInt& rVal );
      97             :     BigInt&         operator +=( const BigInt& rVal );
      98             :     BigInt&         operator -=( const BigInt& rVal );
      99             :     BigInt&         operator *=( const BigInt& rVal );
     100             :     BigInt&         operator /=( const BigInt& rVal );
     101             :     BigInt&         operator %=( const BigInt& rVal );
     102             : 
     103             :     BigInt&         operator  =( const short      nValue );
     104             :     BigInt&         operator  =( const long       nValue );
     105             :     BigInt&         operator  =( const int        nValue );
     106             :     BigInt&         operator  =( const sal_uInt16 nValue );
     107             : 
     108             :     friend inline   BigInt operator +( const BigInt& rVal1, const BigInt& rVal2 );
     109             :     friend inline   BigInt operator -( const BigInt& rVal1, const BigInt& rVal2 );
     110             :     friend inline   BigInt operator *( const BigInt& rVal1, const BigInt& rVal2 );
     111             :     friend inline   BigInt operator /( const BigInt& rVal1, const BigInt& rVal2 );
     112             :     friend inline   BigInt operator %( const BigInt& rVal1, const BigInt& rVal2 );
     113             : 
     114             :     TOOLS_DLLPUBLIC friend          bool operator==( const BigInt& rVal1, const BigInt& rVal2 );
     115             :     friend inline   bool operator!=( const BigInt& rVal1, const BigInt& rVal2 );
     116             :     TOOLS_DLLPUBLIC friend          bool operator< ( const BigInt& rVal1, const BigInt& rVal2 );
     117             :     TOOLS_DLLPUBLIC friend          bool operator> ( const BigInt& rVal1, const BigInt& rVal2 );
     118             :     friend inline   bool operator<=( const BigInt& rVal1, const BigInt& rVal2 );
     119             :     friend inline   bool operator>=( const BigInt& rVal1, const BigInt& rVal2 );
     120             : 
     121             :     friend class Fraction;
     122             : };
     123             : 
     124      736672 : inline BigInt::BigInt()
     125             : {
     126      736672 :     bIsSet = sal_False;
     127      736672 :     bIsBig = sal_False;
     128      736672 :     nVal   = 0;
     129      736672 : }
     130             : 
     131           0 : inline BigInt::BigInt( short nValue )
     132             : {
     133           0 :     bIsSet = sal_True;
     134           0 :     bIsBig = sal_False;
     135           0 :     nVal   = nValue;
     136           0 : }
     137             : 
     138    19075696 : inline BigInt::BigInt( long nValue )
     139             : {
     140    19075696 :     bIsSet = sal_True;
     141    19075696 :     bIsBig = sal_False;
     142    19075696 :     nVal   = nValue;
     143    19075696 : }
     144             : 
     145        1040 : inline BigInt::BigInt( int nValue )
     146             : {
     147        1040 :     bIsSet = sal_True;
     148        1040 :     bIsBig = sal_False;
     149        1040 :     nVal   = nValue;
     150        1040 : }
     151             : 
     152             : inline BigInt::BigInt( sal_uInt16 nValue )
     153             : {
     154             :     bIsSet = sal_True;
     155             :     bIsBig = sal_False;
     156             :     nVal   = nValue;
     157             : }
     158             : 
     159           0 : inline BigInt::operator short() const
     160             : {
     161           0 :     if ( !bIsBig && nVal >= SHRT_MIN && nVal <= SHRT_MAX )
     162           0 :         return (short)nVal;
     163             :     else
     164           0 :         return 0;
     165             : }
     166             : 
     167     5398568 : inline BigInt::operator long() const
     168             : {
     169     5398568 :     if ( !bIsBig )
     170     5398568 :         return nVal;
     171             :     else
     172           0 :         return 0;
     173             : }
     174             : 
     175             : inline BigInt::operator int() const
     176             : {
     177             :     if ( !bIsBig && (nVal == (long)(int)nVal) )
     178             :         return (int)nVal;
     179             :     else
     180             :         return 0;
     181             : }
     182             : 
     183             : inline BigInt::operator sal_uInt16() const
     184             : {
     185             :     if ( !bIsBig && nVal >= 0 && nVal <= (long)USHRT_MAX )
     186             :         return (sal_uInt16)nVal;
     187             :     else
     188             :         return 0;
     189             : }
     190             : 
     191             : inline BigInt& BigInt::operator =( const short nValue )
     192             : {
     193             :     bIsSet = sal_True;
     194             :     bIsBig = sal_False;
     195             :     nVal   = nValue;
     196             : 
     197             :     return *this;
     198             : }
     199             : 
     200           0 : inline BigInt& BigInt::operator =( const long nValue )
     201             : {
     202           0 :     bIsSet = sal_True;
     203           0 :     bIsBig = sal_False;
     204           0 :     nVal   = nValue;
     205             : 
     206           0 :     return *this;
     207             : }
     208             : 
     209           0 : inline BigInt& BigInt::operator =( const int nValue )
     210             : {
     211           0 :     bIsSet = sal_True;
     212           0 :     bIsBig = sal_False;
     213           0 :     nVal   = nValue;
     214             : 
     215           0 :     return *this;
     216             : }
     217             : 
     218             : inline BigInt& BigInt::operator =( const sal_uInt16 nValue )
     219             : {
     220             :     bIsSet = sal_True;
     221             :     bIsBig = sal_False;
     222             :     nVal   = nValue;
     223             : 
     224             :     return *this;
     225             : }
     226             : 
     227       14276 : inline bool BigInt::IsNeg() const
     228             : {
     229       14276 :     if ( !bIsBig )
     230       14178 :         return (nVal < 0);
     231             :     else
     232          98 :         return (bool)bIsNeg;
     233             : }
     234             : 
     235           0 : inline bool BigInt::IsZero() const
     236             : {
     237           0 :     if ( bIsBig )
     238           0 :         return sal_False;
     239             :     else
     240           0 :         return (nVal == 0);
     241             : }
     242             : 
     243           0 : inline bool BigInt::IsOne() const
     244             : {
     245           0 :     if ( bIsBig )
     246           0 :         return sal_False;
     247             :     else
     248           0 :         return (nVal == 1);
     249             : }
     250             : 
     251           0 : inline void BigInt::Abs()
     252             : {
     253           0 :     if ( bIsBig )
     254           0 :         bIsNeg = sal_False;
     255           0 :     else if ( nVal < 0 )
     256           0 :         nVal = -nVal;
     257           0 : }
     258             : 
     259        1115 : inline BigInt operator+( const BigInt &rVal1, const BigInt &rVal2 )
     260             : {
     261        1115 :     BigInt aErg( rVal1 );
     262        1115 :     aErg += rVal2;
     263        1115 :     return aErg;
     264             : }
     265             : 
     266             : inline BigInt operator-( const BigInt &rVal1, const BigInt &rVal2 )
     267             : {
     268             :     BigInt aErg( rVal1 );
     269             :     aErg -= rVal2;
     270             :     return aErg;
     271             : }
     272             : 
     273           0 : inline BigInt operator*( const BigInt &rVal1, const BigInt &rVal2 )
     274             : {
     275           0 :     BigInt aErg( rVal1 );
     276           0 :     aErg *= rVal2;
     277           0 :     return aErg;
     278             : }
     279             : 
     280        1476 : inline BigInt operator/( const BigInt &rVal1, const BigInt &rVal2 )
     281             : {
     282        1476 :     BigInt aErg( rVal1 );
     283        1476 :     aErg /= rVal2;
     284        1476 :     return aErg;
     285             : }
     286             : 
     287             : inline BigInt operator%( const BigInt &rVal1, const BigInt &rVal2 )
     288             : {
     289             :     BigInt aErg( rVal1 );
     290             :     aErg %= rVal2;
     291             :     return aErg;
     292             : }
     293             : 
     294           0 : inline bool operator!=( const BigInt& rVal1, const BigInt& rVal2 )
     295             : {
     296           0 :     return !(rVal1 == rVal2);
     297             : }
     298             : 
     299             : inline bool operator<=( const BigInt& rVal1, const BigInt& rVal2 )
     300             : {
     301             :     return !( rVal1 > rVal2);
     302             : }
     303             : 
     304         123 : inline bool operator>=( const BigInt& rVal1, const BigInt& rVal2 )
     305             : {
     306         123 :     return !(rVal1 < rVal2);
     307             : }
     308             : 
     309             : #endif
     310             : 
     311             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10