LCOV - code coverage report
Current view: top level - include/tools - bigint.hxx (source / functions) Hit Total Coverage
Test: commit e02a6cb2c3e2b23b203b422e4e0680877f232636 Lines: 0 72 0.0 %
Date: 2014-04-14 Functions: 0 17 0.0 %
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 INCLUDED_TOOLS_BIGINT_HXX
      20             : #define INCLUDED_TOOLS_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             :     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             : 
      84             :     bool        IsSet() const { return (bool)bIsSet; }
      85             :     bool        IsNeg() const;
      86             :     bool        IsZero() const;
      87             :     bool        IsOne() const;
      88             :     bool        IsLong() const { return !((bool)bIsBig); }
      89             :     void            Abs();
      90             : #ifdef _TLBIGINT_INT64
      91             :     bool        INT64 ( SbxINT64  *p ) const;
      92             :     bool        UINT64( SbxUINT64 *p ) const;
      93             : #endif
      94             : 
      95             :     BigInt&         operator  =( const BigInt& rVal );
      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             : 
     102             :     BigInt&         operator  =( const short      nValue );
     103             :     BigInt&         operator  =( const long       nValue );
     104             :     BigInt&         operator  =( const int        nValue );
     105             :     BigInt&         operator  =( const sal_uInt16 nValue );
     106             : 
     107             :     friend inline   BigInt operator +( const BigInt& rVal1, const BigInt& rVal2 );
     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             : 
     113             :     TOOLS_DLLPUBLIC friend          bool operator==( const BigInt& rVal1, const BigInt& rVal2 );
     114             :     friend inline   bool operator!=( const BigInt& rVal1, const BigInt& rVal2 );
     115             :     TOOLS_DLLPUBLIC friend          bool operator< ( const BigInt& rVal1, const BigInt& rVal2 );
     116             :     TOOLS_DLLPUBLIC friend          bool operator> ( const BigInt& rVal1, const BigInt& rVal2 );
     117             :     friend inline   bool operator<=( const BigInt& rVal1, const BigInt& rVal2 );
     118             :     friend inline   bool operator>=( const BigInt& rVal1, const BigInt& rVal2 );
     119             : 
     120             :     friend class Fraction;
     121             : };
     122             : 
     123           0 : inline BigInt::BigInt()
     124             : {
     125           0 :     bIsSet = false;
     126           0 :     bIsBig = false;
     127           0 :     nVal   = 0;
     128           0 : }
     129             : 
     130           0 : inline BigInt::BigInt( short nValue )
     131             : {
     132           0 :     bIsSet = true;
     133           0 :     bIsBig = false;
     134           0 :     nVal   = nValue;
     135           0 : }
     136             : 
     137           0 : inline BigInt::BigInt( long nValue )
     138             : {
     139           0 :     bIsSet = true;
     140           0 :     bIsBig = false;
     141           0 :     nVal   = nValue;
     142           0 : }
     143             : 
     144           0 : inline BigInt::BigInt( int nValue )
     145             : {
     146           0 :     bIsSet = true;
     147           0 :     bIsBig = false;
     148           0 :     nVal   = nValue;
     149           0 : }
     150             : 
     151             : inline BigInt::BigInt( sal_uInt16 nValue )
     152             : {
     153             :     bIsSet = true;
     154             :     bIsBig = false;
     155             :     nVal   = nValue;
     156             : }
     157             : 
     158           0 : inline BigInt::operator short() const
     159             : {
     160           0 :     if ( !bIsBig && nVal >= SHRT_MIN && nVal <= SHRT_MAX )
     161           0 :         return (short)nVal;
     162             :     else
     163           0 :         return 0;
     164             : }
     165             : 
     166           0 : inline BigInt::operator long() const
     167             : {
     168           0 :     if ( !bIsBig )
     169           0 :         return nVal;
     170             :     else
     171           0 :         return 0;
     172             : }
     173             : 
     174             : inline BigInt::operator int() const
     175             : {
     176             :     if ( !bIsBig && (nVal == (long)(int)nVal) )
     177             :         return (int)nVal;
     178             :     else
     179             :         return 0;
     180             : }
     181             : 
     182             : inline BigInt::operator sal_uInt16() const
     183             : {
     184             :     if ( !bIsBig && nVal >= 0 && nVal <= (long)USHRT_MAX )
     185             :         return (sal_uInt16)nVal;
     186             :     else
     187             :         return 0;
     188             : }
     189             : 
     190             : inline BigInt& BigInt::operator =( const short nValue )
     191             : {
     192             :     bIsSet = true;
     193             :     bIsBig = false;
     194             :     nVal   = nValue;
     195             : 
     196             :     return *this;
     197             : }
     198             : 
     199           0 : inline BigInt& BigInt::operator =( const long nValue )
     200             : {
     201           0 :     bIsSet = true;
     202           0 :     bIsBig = false;
     203           0 :     nVal   = nValue;
     204             : 
     205           0 :     return *this;
     206             : }
     207             : 
     208           0 : inline BigInt& BigInt::operator =( const int nValue )
     209             : {
     210           0 :     bIsSet = true;
     211           0 :     bIsBig = false;
     212           0 :     nVal   = nValue;
     213             : 
     214           0 :     return *this;
     215             : }
     216             : 
     217             : inline BigInt& BigInt::operator =( const sal_uInt16 nValue )
     218             : {
     219             :     bIsSet = true;
     220             :     bIsBig = false;
     221             :     nVal   = nValue;
     222             : 
     223             :     return *this;
     224             : }
     225             : 
     226           0 : inline bool BigInt::IsNeg() const
     227             : {
     228           0 :     if ( !bIsBig )
     229           0 :         return (nVal < 0);
     230             :     else
     231           0 :         return (bool)bIsNeg;
     232             : }
     233             : 
     234           0 : inline bool BigInt::IsZero() const
     235             : {
     236           0 :     if ( bIsBig )
     237           0 :         return false;
     238             :     else
     239           0 :         return (nVal == 0);
     240             : }
     241             : 
     242           0 : inline bool BigInt::IsOne() const
     243             : {
     244           0 :     if ( bIsBig )
     245           0 :         return false;
     246             :     else
     247           0 :         return (nVal == 1);
     248             : }
     249             : 
     250           0 : inline void BigInt::Abs()
     251             : {
     252           0 :     if ( bIsBig )
     253           0 :         bIsNeg = false;
     254           0 :     else if ( nVal < 0 )
     255           0 :         nVal = -nVal;
     256           0 : }
     257             : 
     258           0 : inline BigInt operator+( const BigInt &rVal1, const BigInt &rVal2 )
     259             : {
     260           0 :     BigInt aErg( rVal1 );
     261           0 :     aErg += rVal2;
     262           0 :     return aErg;
     263             : }
     264             : 
     265             : inline BigInt operator-( const BigInt &rVal1, const BigInt &rVal2 )
     266             : {
     267             :     BigInt aErg( rVal1 );
     268             :     aErg -= rVal2;
     269             :     return aErg;
     270             : }
     271             : 
     272           0 : inline BigInt operator*( const BigInt &rVal1, const BigInt &rVal2 )
     273             : {
     274           0 :     BigInt aErg( rVal1 );
     275           0 :     aErg *= rVal2;
     276           0 :     return aErg;
     277             : }
     278             : 
     279           0 : inline BigInt operator/( const BigInt &rVal1, const BigInt &rVal2 )
     280             : {
     281           0 :     BigInt aErg( rVal1 );
     282           0 :     aErg /= rVal2;
     283           0 :     return aErg;
     284             : }
     285             : 
     286             : inline BigInt operator%( const BigInt &rVal1, const BigInt &rVal2 )
     287             : {
     288             :     BigInt aErg( rVal1 );
     289             :     aErg %= rVal2;
     290             :     return aErg;
     291             : }
     292             : 
     293           0 : inline bool operator!=( const BigInt& rVal1, const BigInt& rVal2 )
     294             : {
     295           0 :     return !(rVal1 == rVal2);
     296             : }
     297             : 
     298             : inline bool operator<=( const BigInt& rVal1, const BigInt& rVal2 )
     299             : {
     300             :     return !( rVal1 > rVal2);
     301             : }
     302             : 
     303           0 : inline bool operator>=( const BigInt& rVal1, const BigInt& rVal2 )
     304             : {
     305           0 :     return !(rVal1 < rVal2);
     306             : }
     307             : 
     308             : #endif
     309             : 
     310             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10