// HugeInt.h: interface for the CHugeInt class.
//
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_HUGEINT_H__5B7DCBAC_6D89_4C55_B51F_D96B5E741B51__INCLUDED_)
#define AFX_HUGEINT_H__5B7DCBAC_6D89_4C55_B51F_D96B5E741B51__INCLUDED_

#if _MSC_VER > 1000
    #pragma once
#endif // _MSC_VER > 1000

#include "HugeCalc.h"
#include "RadixConverter.h"

class CHugeSInt;
class CHugeInt;

typedef struct tagCVECTOR_HHUGEINT
{
    CONST CHugeInt ** p_start;
    UINT32 u32Size;

#ifdef __cplusplus
    tagCVECTOR_HHUGEINT( CONST CHugeInt ** CONST _p_start = NULL, CONST UINT32 _u32Size = 0 )
        : p_start( _p_start ), u32Size( _u32Size )
    {
    };
#endif
} CVECTOR_HHUGEINT, FAR *PCVECTOR_HHUGEINT;
typedef CONST CVECTOR_HHUGEINT FAR *LPCVECTOR_HHUGEINT;

#define HI_API HUGECALC_API

class HI_API CHugeInt
{
    friend class CHugeIntX;
    friend CRadixConverter& CRadixConverter::operator =( CONST CHugeInt& right );
    // CompareAbs
    HI_API friend CONST SINT32 CompareAbs( CONST UINT32 u32Num, CONST CHugeInt& right );
    HI_API friend CONST SINT32 CompareAbs( CONST SINT32 s32Num, CONST CHugeInt& right );

    HI_API friend CONST SINT32 CompareAbs( CONST CHugeInt& left, CONST UINT32 u32Num );
    HI_API friend CONST SINT32 CompareAbs( CONST CHugeInt& left, CONST SINT32 s32Num );

    HI_API friend CONST SINT32 CompareAbs( CONST CHugeInt& left, CONST CHugeInt& right );

    // Compare
    HI_API friend CONST SINT32 Compare( CONST UINT32 u32Num, CONST CHugeInt& right );
    HI_API friend CONST SINT32 Compare( CONST SINT32 s32Num, CONST CHugeInt& right );

    HI_API friend CONST SINT32 Compare( CONST CHugeInt& left, CONST UINT32 u32Num );
    HI_API friend CONST SINT32 Compare( CONST CHugeInt& left, CONST SINT32 s32Num );

    HI_API friend CONST SINT32 Compare( CONST CHugeInt& left, CONST CHugeInt& right );

    // PowMod
    HI_API friend CONST UINT32 PowMod( CONST UINT32 u32Base, CONST CHugeInt& hugeExp, CONST UINT32 u32Mod );
    HI_API friend CONST SINT64 PowMod( CONST CHugeInt& hugeBase, CONST UINT32 u32Exp, CONST UINT32 u32Mod );
    HI_API friend CONST SINT64 PowMod( CONST CHugeInt& hugeBase, CONST SINT32 s32Exp, CONST UINT32 u32Mod );
    HI_API friend CONST SINT64 PowMod( CONST CHugeInt& hugeBase, CONST CHugeInt& hugeExp, CONST UINT32 u32Mod );

    // relation operators
    HI_API friend CONST BOOL operator ==( CONST UINT32 u32Num, CONST CHugeInt& right );
    HI_API friend CONST BOOL operator ==( CONST SINT32 s32Num, CONST CHugeInt& right );

    HI_API friend CONST BOOL operator ==( CONST CHugeInt& left, CONST UINT32 u32Num );
    HI_API friend CONST BOOL operator ==( CONST CHugeInt& left, CONST SINT32 s32Num );

    HI_API friend CONST BOOL operator ==( CONST CHugeInt& left, CONST CHugeInt& right );

    HI_API friend CONST BOOL operator !=( CONST UINT32 u32Num, CONST CHugeInt& right );
    HI_API friend CONST BOOL operator !=( CONST SINT32 s32Num, CONST CHugeInt& right );

    HI_API friend CONST BOOL operator !=( CONST CHugeInt& left, CONST UINT32 u32Num );
    HI_API friend CONST BOOL operator !=( CONST CHugeInt& left, CONST SINT32 s32Num );

    HI_API friend CONST BOOL operator !=( CONST CHugeInt& left, CONST CHugeInt& right );

    HI_API friend CONST BOOL operator <( CONST UINT32 u32Num, CONST CHugeInt& right );
    HI_API friend CONST BOOL operator <( CONST SINT32 s32Num, CONST CHugeInt& right );

    HI_API friend CONST BOOL operator <( CONST CHugeInt& left, CONST UINT32 u32Num );
    HI_API friend CONST BOOL operator <( CONST CHugeInt& left, CONST SINT32 s32Num );

    HI_API friend CONST BOOL operator <( CONST CHugeInt& left, CONST CHugeInt& right );

    HI_API friend CONST BOOL operator <=( CONST UINT32 u32Num, CONST CHugeInt& right );
    HI_API friend CONST BOOL operator <=( CONST SINT32 s32Num, CONST CHugeInt& right );

    HI_API friend CONST BOOL operator <=( CONST CHugeInt& left, CONST UINT32 u32Num );
    HI_API friend CONST BOOL operator <=( CONST CHugeInt& left, CONST SINT32 s32Num );

    HI_API friend CONST BOOL operator <=( CONST CHugeInt& left, CONST CHugeInt& right );

    HI_API friend CONST BOOL operator >( CONST UINT32 u32Num, CONST CHugeInt& right );
    HI_API friend CONST BOOL operator >( CONST SINT32 s32Num, CONST CHugeInt& right );

    HI_API friend CONST BOOL operator >( CONST CHugeInt& left, CONST UINT32 u32Num );
    HI_API friend CONST BOOL operator >( CONST CHugeInt& left, CONST SINT32 s32Num );

    HI_API friend CONST BOOL operator >( CONST CHugeInt& left, CONST CHugeInt& right );

    HI_API friend CONST BOOL operator >=( CONST UINT32 u32Num, CONST CHugeInt& right );
    HI_API friend CONST BOOL operator >=( CONST SINT32 s32Num, CONST CHugeInt& right );

    HI_API friend CONST BOOL operator >=( CONST CHugeInt& left, CONST UINT32 u32Num );
    HI_API friend CONST BOOL operator >=( CONST CHugeInt& left, CONST SINT32 s32Num );

    HI_API friend CONST BOOL operator >=( CONST CHugeInt& left, CONST CHugeInt& right );

    // +
    HI_API friend CONST CHugeInt operator +( CONST UINT32 u32Num, CONST CHugeInt& right );
    HI_API friend CONST CHugeInt operator +( CONST SINT32 s32Num, CONST CHugeInt& right );

    HI_API friend CONST CHugeInt operator +( CONST CHugeInt& left, CONST UINT32 u32Num );
    HI_API friend CONST CHugeInt operator +( CONST CHugeInt& left, CONST SINT32 s32Num );

    HI_API friend CONST CHugeInt operator +( CONST CHugeInt& left, CONST CHugeInt& right );

    // -
    HI_API friend CONST CHugeInt operator -( CONST UINT32 u32Num, CONST CHugeInt& right );
    HI_API friend CONST CHugeInt operator -( CONST SINT32 s32Num, CONST CHugeInt& right );

    HI_API friend CONST CHugeInt operator -( CONST CHugeInt& left, CONST UINT32 u32Num );
    HI_API friend CONST CHugeInt operator -( CONST CHugeInt& left, CONST SINT32 s32Num );

    HI_API friend CONST CHugeInt operator -( CONST CHugeInt& left, CONST CHugeInt& right );

    // *
    HI_API friend CONST CHugeInt operator *( CONST UINT32 u32Num, CONST CHugeInt& right );
    HI_API friend CONST CHugeInt operator *( CONST SINT32 s32Num, CONST CHugeInt& right );

    HI_API friend CONST CHugeInt operator *( CONST CHugeInt& left, CONST UINT32 u32Num );
    HI_API friend CONST CHugeInt operator *( CONST CHugeInt& left, CONST SINT32 s32Num );

    HI_API friend CONST CHugeInt operator *( CONST CHugeInt& left, CONST CHugeInt& right );

    // Division, return quotient
    HI_API friend CONST SINT64 Div( CONST UINT32 u32Dividend, CONST CHugeInt& hugeDivisor, UINT32 * CONST pRemainder = NULL );
    HI_API friend CONST SINT32 Div( CONST SINT32 s32Dividend, CONST CHugeInt& hugeDivisor, SINT32 * CONST pRemainder = NULL );

    // /
    HI_API friend CONST SINT64 operator /( CONST UINT32 u32Num, CONST CHugeInt& right );
    HI_API friend CONST SINT32 operator /( CONST SINT32 s32Num, CONST CHugeInt& right );

    HI_API friend CONST CHugeInt operator /( CONST CHugeInt& left, CONST UINT32 u32Num );
    HI_API friend CONST CHugeInt operator /( CONST CHugeInt& left, CONST SINT32 s32Num );

    HI_API friend CONST CHugeInt operator /( CONST CHugeInt& left, CONST CHugeInt& right );

    // %
    HI_API friend CONST UINT32 operator %( CONST UINT32 u32Num, CONST CHugeInt& right );
    HI_API friend CONST SINT32 operator %( CONST SINT32 s32Num, CONST CHugeInt& right );

    HI_API friend CONST SINT64 operator %( CONST CHugeInt& left, CONST UINT32 u32Num );
    HI_API friend CONST SINT32 operator %( CONST CHugeInt& left, CONST SINT32 s32Num );

    HI_API friend CONST CHugeInt operator %( CONST CHugeInt& left, CONST CHugeInt& right );

public:
    // Constructor
    explicit CHugeInt( VOID );
    explicit CHugeInt( CONST SINT32& s32Num );
    explicit CHugeInt( CONST SINT64& s64Num );
    explicit CHugeInt( CONST UINT32& u32Num );
    explicit CHugeInt( CONST UINT64& u64Num );
    explicit CHugeInt( CONST LPCTSTR lpszNum );
    explicit CHugeInt( CONST CRadixConverter& RadixConverter );
    explicit CHugeInt( CONST CHugeInt& right );
    explicit CHugeInt( CONST CHugeIntX& right );

    // destructor
    /*virtual */~CHugeInt( );

    static CONST UINT32 GetCount( VOID );

    // reload operator ->
    CHugeInt * CONST operator ->( VOID );

    // get property
    CONST SIGN GetSign( VOID ) CONST;
    CONST BOOL operator !( VOID ) CONST;
    CONST BOOL IsAbsOne( VOID ) CONST;
    CONST BOOL IsOdd( VOID ) CONST;
    CONST BOOL IsEven( VOID ) CONST;
    CONST BOOL IsPrime( VOID ) CONST;
    CONST PRIMALITY TestPrimality( CONST UINT32 u32Repeat = 5 ) CONST;
    CONST UINT32 GetBits( VOID ) CONST;
    CONST UINT32 GetDigits( VOID ) CONST;
    CONST UINT32 GetTailDecZeros( VOID ) CONST;

    // Return non-zero if the value of (*this) fits in SINT32, UINT32, SINT64 or UINT64, respectively. Otherwise, return zero.
    operator CONST SINT32( VOID ) CONST;
    operator CONST SINT64( VOID ) CONST;
    operator CONST UINT32( VOID ) CONST;
    operator CONST UINT64( VOID ) CONST;
    // as GetStr( FS_FS_NORMAL )
    operator CONST LPCTSTR( VOID ) CONST;

    CONST CHugeInt operator -( VOID ) CONST;

    CHugeInt& Abs( VOID );
    CHugeInt& Negate( VOID );
    CHugeInt& Swap( CHugeInt& right );

    CHugeInt& Ceil( CONST UINT32 u32DecDigits );
    CHugeInt& Truncate( CONST UINT32 u32DecDigits );
    CHugeInt& Floor( CONST UINT32 u32DecDigits );

    CHugeInt& Random( CONST UINT32 u32DecDigits );
    CHugeInt& GeneratePrime( CONST UINT32 u32DecDigits );

    // Returns the largest prime number less than the argument.
    CHugeInt& PreviousPrime( CONST CHugeInt& hBenchmark );
    // Returns the smallest prime number greater than the argument.
    CHugeInt& NextPrime( CONST CHugeInt& hBenchmark );

    // reload operators
    CHugeInt& operator =( CONST SINT32& s32Num );
    CHugeInt& operator =( CONST SINT64& s64Num );
    CHugeInt& operator =( CONST UINT32& u32Num );
    CHugeInt& operator =( CONST UINT64& u64Num );
    CHugeInt& operator =( CONST LPCTSTR lpszNum );
    CHugeInt& operator =( CONST CRadixConverter& RadixConverter );
    CHugeInt& operator =( CONST CHugeInt& right );
    CHugeInt& operator =( CONST CHugeIntX& right );

    CHugeInt& SetHexStr( CONST LPCTSTR lpszHexNum );

    CHugeInt& operator +=( CONST UINT32 u32Num );
    CHugeInt& operator +=( CONST SINT32 s32Num );
    CHugeInt& operator +=( CONST CHugeInt& right );

    CHugeInt& operator ++( VOID );
    CONST CHugeInt operator ++( CONST INT );

    CHugeInt& operator -=( CONST UINT32 u32Num );
    CHugeInt& operator -=( CONST SINT32 s32Num );
    CHugeInt& operator -=( CONST CHugeInt& right );

    CHugeInt& operator --( VOID );
    CONST CHugeInt operator --( CONST INT );

    CHugeInt& operator *=( CONST UINT32 u32Num );
    CHugeInt& operator *=( CONST SINT32 s32Num );
    CHugeInt& operator *=( CONST CHugeInt& right );

    CHugeInt& operator /=( CONST UINT32 u32Num );
    CHugeInt& operator /=( CONST SINT32 s32Num );
    CHugeInt& operator /=( CONST CHugeInt& right );

    CHugeInt& operator %=( CONST UINT32 u32Num );
    CHugeInt& operator %=( CONST SINT32 s32Num );
    CHugeInt& operator %=( CONST CHugeInt& right );

    CHugeInt& operator <<=( CONST UINT32 u32BitLShift );
    CHugeInt& operator >>=( CONST UINT32 u32BitRShift );

    CONST CHugeInt operator <<( CONST UINT32 u32BitLShift ) CONST;
    CONST CHugeInt operator >>( CONST UINT32 u32BitRShift ) CONST;

    CHugeInt& operator &=( CONST CHugeInt& right );
    CHugeInt& operator ^=( CONST CHugeInt& right );
    CHugeInt& operator |=( CONST CHugeInt& right );

    CONST CHugeInt operator &( CONST CHugeInt& right ) CONST;
    CONST CHugeInt operator ^( CONST CHugeInt& right ) CONST;
    CONST CHugeInt operator |( CONST CHugeInt& right ) CONST;

    // notice: base on decimal system !!
    CHugeInt& DecLShift( CONST UINT32 u32DecLShift );
    CHugeInt& DecRShift( CONST UINT32 u32DecRShift );

    // Multiplication, return product
    CHugeInt& Mul( CONST UINT32 u32Multiplicand, CONST CHugeInt& hugeMultiplier );
    CHugeInt& Mul( CONST SINT32 s32Multiplicand, CONST CHugeInt& hugeMultiplier );

    CHugeInt& Mul( CONST CHugeInt& hugeMultiplicand, CONST UINT32 u32Multiplier );
    CHugeInt& Mul( CONST CHugeInt& hugeMultiplicand, CONST SINT32 s32Multiplier );

    CHugeInt& Mul( CONST CHugeInt& hugeMultiplicand, CONST CHugeInt& hugeMultiplier );

    // Division, return quotient
    CHugeInt& Div( CONST CHugeInt& hugeDividend, CONST UINT32 u32Divisor, SINT64 * CONST pRemainder = NULL );
    CHugeInt& Div( CONST CHugeInt& hugeDividend, CONST SINT32 s32Divisor, SINT32 * CONST pRemainder = NULL );

    CHugeInt& Div( CONST CHugeInt& hugeDividend, CONST CHugeInt& hugeDivisor, CHugeInt * CONST pRemainder = NULL );

    CHugeInt& Pow( CONST UINT32 u32Exp );
    CHugeInt& Pow( CONST CHugeInt& hugeBase, CONST UINT32 u32Exp );

    // Extraction
    CHugeInt& Root( CONST CHugeInt& hugeRadicand, CONST UINT32 u32Exp, CHugeInt * CONST pRemainder = NULL, BOOL * CONST pIsReal = NULL );

    // Logarithm
    CONST UINT32 Log( CONST UINT32 u32Base, BOOL * CONST pIsExact = NULL ) CONST;
    CONST UINT32 Log( CONST CHugeInt& hugeBase, BOOL * CONST pIsExact = NULL ) CONST;

    // PrimeFactorial(n) gives the product of the first n primes.
    CHugeInt& PrimeFactorial( CONST UINT32 u32Index );
    // Primorial(n) gives the product of the primes less than or equal to n.
    CHugeInt& Primorial( CONST UINT32 u32Num );

    // Factorial, Permutation, Combination
    CHugeInt& Factorial( CONST UINT32 n );
    CHugeInt& Factorial2( CONST UINT32 n );
    CHugeInt& Permutation( CONST UINT32 n, CONST UINT32 r );
    CHugeInt& Combination( CONST UINT32 n, CONST UINT32 r );

    // Fibonacci numbers
    CHugeInt& Fibonacci( CONST SINT32 n );

    // Lucas numbers
    CHugeInt& Lucas( CONST SINT32 n );

    // MulMod
    CHugeInt& MulMod( CONST CHugeInt& hugeMultiplicand, CONST CHugeInt& hugeMultiplier, CONST CHugeInt& hugeMod );
    CHugeInt& MulMod( CONST CHugeInt& hugeMultiplier, CONST CHugeInt& hugeMod );

    // InvertMod: x = InvertMod( b, m ) && !(!x) <==> ( b * x ) mod m = 1
    CHugeInt& InvertMod( CONST UINT32 u32InvertBase, CONST CHugeInt& hugeMod );
    CHugeInt& InvertMod( CONST CHugeInt& hugeInvertBase, CONST CHugeInt& hugeMod );
    CHugeInt& InvertMod( CONST CHugeInt& hugeMod );

    // PowMod
    CHugeInt& PowMod( CONST UINT32 u32Base, CONST UINT32 u32Exp, CONST CHugeInt& hugeMod );
    CHugeInt& PowMod( CONST UINT32 u32Base, CONST SINT32 s32Exp, CONST CHugeInt& hugeMod );
    CHugeInt& PowMod( CONST UINT32 u32Base, CONST CHugeInt& hugeExp, CONST CHugeInt& hugeMod );
    CHugeInt& PowMod( CONST CHugeInt& hugeBase, CONST UINT32 u32Exp, CONST CHugeInt& hugeMod );
    CHugeInt& PowMod( CONST CHugeInt& hugeBase, CONST SINT32 s32Exp, CONST CHugeInt& hugeMod );
    CHugeInt& PowMod( CONST CHugeInt& hugeBase, CONST CHugeInt& hugeExp, CONST CHugeInt& hugeMod );

    // ModPowTen: *this %= 10^u32Exp
    CHugeInt& ModPowTen( CONST UINT32 u32Exp );

    // Greatest Common Divisor
    CHugeInt& Gcd( CONST UINT32 u32Num, CONST CHugeInt& right );
    CHugeInt& Gcd( CONST SINT32 s32Num, CONST CHugeInt& right );
    CHugeInt& Gcd( CONST CHugeInt& left, CONST UINT32 u32Num );
    CHugeInt& Gcd( CONST CHugeInt& left, CONST SINT32 s32Num );

    CHugeInt& Gcd( CONST CHugeInt& left, CONST CHugeInt& right );

    CHugeInt& Gcd( CONST CVECTOR_HHUGEINT& vLpHugeInt );

    // GcdEx: g = GcdEx( x, y, u, v ) = u * x + v * y. g is always positive, even if one or both of u and v are negative.
    CHugeInt& GcdEx( CHugeInt& x, CHugeInt& y, CONST CHugeInt& u, CONST CHugeInt& v );

    // Lowest Common Multiple
    CHugeInt& Lcm( CONST CVECTOR_UINT32& vU32Num );

    CHugeInt& Lcm( CONST UINT32 u32Num, CONST CHugeInt& right );
    CHugeInt& Lcm( CONST SINT32 s32Num, CONST CHugeInt& right );
    CHugeInt& Lcm( CONST CHugeInt& left, CONST UINT32 u32Num );
    CHugeInt& Lcm( CONST CHugeInt& left, CONST SINT32 s32Num );

    CHugeInt& Lcm( CONST CHugeInt& left, CONST CHugeInt& right );

    CHugeInt& Lcm( CONST CVECTOR_HHUGEINT& vLpHugeInt );

    CHugeInt& Product( CONST CVECTOR_UINT32& vU32Num );
    CHugeInt& Product( CONST CVECTOR_HHUGEINT& vLpHugeInt );

    // if u32Exp==0, then call function: Product( ... );
    CHugeInt& SumsOfLikePowers( CONST CVECTOR_UINT32& vU32Num, CONST UINT32 u32Exp = 1 );
    CHugeInt& SumsOfLikePowers( CONST CVECTOR_HHUGEINT& vLpHugeInt, CONST UINT32 u32Exp = 1 );

    // Output
    CONST LPCTSTR GetStr( CONST BYTE byFormat = FS_DEFAULT, UINT32 * CONST pStrLen = NULL ) CONST;

    // no "0x"
    CONST LPCTSTR GetHexStr( CONST BYTE byFormat = FS_DEFAULT, UINT32 * CONST pStrLen = NULL ) CONST;

    // This function can convert the huge integer to a string of digits in another Radix system.
    CONST LPCTSTR GetStrRadix( CONST UINT32 u32Radix = 16, UINT32 * CONST pDigits = NULL, CONST BYTE byFormat = FS_DEFAULT, CONST UINT32 u32BandLength = 4, UINT32 * CONST pStrLen = NULL ) CONST;

    VOID FreeStrBuffer( VOID ) CONST;

protected:

private:
    explicit CHugeInt( CONST CHugeSInt& right );
    CHugeInt& operator =( CONST CHugeSInt& right );

    CHugeSInt * CONST m_pSHuge;
};

#endif // !defined(AFX_HUGEINT_H__5B7DCBAC_6D89_4C55_B51F_D96B5E741B51__INCLUDED_)