Changes in / [302d84c2:ee6dbae]


Ignore:
Files:
11 edited

Legend:

Unmodified
Added
Removed
  • libcfa/src/fstream.cfa

    r302d84c2 ree6dbae  
    1010// Created On       : Wed May 27 17:56:53 2015
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Fri Jul 12 12:03:53 2019
    13 // Update Count     : 344
     12// Last Modified On : Thu May 16 08:33:28 2019
     13// Update Count     : 328
    1414//
    1515
     
    2424#include <assert.h>
    2525#include <errno.h>                                                                              // errno
    26 
    27 
    28 //*********************************** ofstream ***********************************
    29 
    3026
    3127#define IO_MSG "I/O error: "
     
    4137        sepSetCur( os, sepGet( os ) );
    4238        sepSetTuple( os, ", " );
    43 } // ?{}
     39}
    4440
    4541// private
     
    6056void ?{}( ofstream & os, const char * name, const char * mode ) {
    6157        open( os, name, mode );
    62 } // ?{}
    63 
     58}
    6459void ?{}( ofstream & os, const char * name ) {
    6560        open( os, name, "w" );
    66 } // ?{}
     61}
    6762
    6863void sepOn( ofstream & os ) { os.sepOnOff = ! getNL( os ); }
     
    9994        os.tupleSeparator[sepSize - 1] = '\0';
    10095} // sepSet
    101 
    102 void ends( ofstream & os ) {
    103         if ( getANL( os ) ) nl( os );
    104         else setPrt( os, false );                                                       // turn off
    105         if ( &os == &exit ) exit( EXIT_FAILURE );
    106         if ( &os == &abort ) abort();
    107 } // ends
    10896
    10997int fail( ofstream & os ) {
     
    169157ofstream & serr = serrFile;
    170158
    171 static ofstream exitFile = { (FILE *)(&_IO_2_1_stdout_) };
    172 ofstream & exit = exitFile;
    173 static ofstream abortFile = { (FILE *)(&_IO_2_1_stderr_) };
    174 ofstream & abort = abortFile;
    175 
    176 
    177 //*********************************** ifstream ***********************************
    178 
     159// static ofstream sexitFile = { (FILE *)(&_IO_2_1_stdout_) };
     160// ofstream & sexit = sexitFile;
     161// static ofstream sabortFile = { (FILE *)(&_IO_2_1_stderr_) };
     162// ofstream & sabort = sabortFile;
     163
     164void nl( ofstream & os ) {
     165        if ( getANL( os ) ) (ofstream &)(nl( os ));                     // implementation only
     166        else setPrt( os, false );                                                       // turn off
     167}
     168
     169//---------------------------------------
    179170
    180171// private
     
    182173        is.file = file;
    183174        is.nlOnOff = false;
    184 } // ?{}
     175}
    185176
    186177// public
     
    189180void ?{}( ifstream & is, const char * name, const char * mode ) {
    190181        open( is, name, mode );
    191 } // ?{}
    192 
     182}
    193183void ?{}( ifstream & is, const char * name ) {
    194184        open( is, name, "r" );
    195 } // ?{}
     185}
    196186
    197187void nlOn( ifstream & os ) { os.nlOnOff = true; }
  • libcfa/src/fstream.hfa

    r302d84c2 ree6dbae  
    1010// Created On       : Wed May 27 17:56:53 2015
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Fri Jul 12 12:03:54 2019
    13 // Update Count     : 165
     12// Last Modified On : Thu May 16 08:34:10 2019
     13// Update Count     : 157
    1414//
    1515
     
    1717
    1818#include "iostream.hfa"
    19 
    20 
    21 //*********************************** ofstream ***********************************
    22 
    2319
    2420enum { sepSize = 16 };
     
    6056void sepSetTuple( ofstream &, const char * );
    6157
    62 void ends( ofstream & os );
    6358int fail( ofstream & );
    6459int flush( ofstream & );
     
    7469
    7570extern ofstream & sout, & serr;
    76 extern ofstream & exit, & abort;
    7771
    78 
    79 //*********************************** ifstream ***********************************
     72// extern ofstream & sout, & serr, & sexit, & sabort;
     73// void nl( ofstream & os );
    8074
    8175
  • libcfa/src/gmp.hfa

    r302d84c2 ree6dbae  
    1010// Created On       : Tue Apr 19 08:43:43 2016
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Fri Jul 12 12:02:55 2019
    13 // Update Count     : 26
     12// Last Modified On : Sat Apr 20 09:01:52 2019
     13// Update Count     : 24
    1414//
    1515
     
    1919
    2020#include <gmp.h>                                                                                // GNU multi-precise integers
    21 #include <fstream.hfa>                                                                  // sout
     21#include <fstream.hfa>                                                                          // sout
    2222
    2323struct Int { mpz_t mpz; };                                                              // wrap GMP implementation
    2424
    25 static inline {
    26         // constructor
    27         void ?{}( Int & this ) { mpz_init( this.mpz ); }
    28         void ?{}( Int & this, Int init ) { mpz_init_set( this.mpz, init.mpz ); }
    29         void ?{}( Int & this, zero_t ) { mpz_init_set_si( this.mpz, 0 ); }
    30         void ?{}( Int & this, one_t ) { mpz_init_set_si( this.mpz, 1 ); }
    31         void ?{}( Int & this, signed long int init ) { mpz_init_set_si( this.mpz, init ); }
    32         void ?{}( Int & this, unsigned long int init ) { mpz_init_set_ui( this.mpz, init ); }
    33         void ?{}( Int & this, const char * val ) { if ( mpz_init_set_str( this.mpz, val, 0 ) ) abort(); }
    34         void ^?{}( Int & this ) { mpz_clear( this.mpz ); }
    35 
    36         // literal
    37         Int ?`mp( signed long int init ) { return (Int){ init }; }
    38         Int ?`mp( unsigned long int init ) { return (Int){ init }; }
    39         Int ?`mp( const char * init ) { return (Int){ init }; }
    40 
    41         // assignment
    42         Int ?=?( Int & lhs, Int rhs ) { mpz_set( lhs.mpz, rhs.mpz ); return lhs; }
    43         Int ?=?( Int & lhs, long int rhs ) { mpz_set_si( lhs.mpz, rhs ); return lhs; }
    44         Int ?=?( Int & lhs, unsigned long int rhs ) { mpz_set_ui( lhs.mpz, rhs ); return lhs; }
    45         Int ?=?( Int & lhs, const char * rhs ) { if ( mpz_set_str( lhs.mpz, rhs, 0 ) ) { printf( "invalid string conversion\n" ); abort(); } return lhs; }
    46 
    47         char ?=?( char & lhs, Int rhs ) { char val = mpz_get_si( rhs.mpz ); lhs = val; return lhs; }
    48         short int ?=?( short int & lhs, Int rhs ) { short int val = mpz_get_si( rhs.mpz ); lhs = val; return lhs; }
    49         int ?=?( int & lhs, Int rhs ) { int val = mpz_get_si( rhs.mpz ); lhs = val; return lhs; }
    50         long int ?=?( long int & lhs, Int rhs ) { long int val = mpz_get_si( rhs.mpz ); lhs = val; return lhs; }
    51         unsigned char ?=?( unsigned char & lhs, Int rhs ) { unsigned char val = mpz_get_ui( rhs.mpz ); lhs = val; return lhs; }
    52         unsigned short int ?=?( unsigned short int & lhs, Int rhs ) { unsigned short int val = mpz_get_ui( rhs.mpz ); lhs = val; return lhs; }
    53         unsigned int ?=?( unsigned int & lhs, Int rhs ) { unsigned int val = mpz_get_ui( rhs.mpz ); lhs = val; return lhs; }
    54         unsigned long int ?=?( unsigned long int & lhs, Int rhs ) { unsigned long int val = mpz_get_ui( rhs.mpz ); lhs = val; return lhs; }
    55 
    56         // conversions
    57         long int narrow( Int val ) { return mpz_get_si( val.mpz ); }
    58         unsigned long int narrow( Int val ) { return mpz_get_ui( val.mpz ); }
    59 
    60         // comparison
    61         int ?==?( Int oper1, Int oper2 ) { return mpz_cmp( oper1.mpz, oper2.mpz ) == 0; }
    62         int ?==?( Int oper1, long int oper2 ) { return mpz_cmp_si( oper1.mpz, oper2 ) == 0; }
    63         int ?==?( long int oper2, Int oper1 ) { return mpz_cmp_si( oper1.mpz, oper2 ) == 0; }
    64         int ?==?( Int oper1, unsigned long int oper2 ) { return mpz_cmp_ui( oper1.mpz, oper2 ) == 0; }
    65         int ?==?( unsigned long int oper2, Int oper1 ) { return mpz_cmp_ui( oper1.mpz, oper2 ) == 0; }
    66 
    67         int ?!=?( Int oper1, Int oper2 ) { return ! ( oper1 == oper2 ); }
    68         int ?!=?( Int oper1, long int oper2 ) { return ! ( oper1 == oper2 ); }
    69         int ?!=?( long int oper1, Int oper2 ) { return ! ( oper1 == oper2 ); }
    70         int ?!=?( Int oper1, unsigned long int oper2 ) { return ! ( oper1 == oper2 ); }
    71         int ?!=?( unsigned long int oper1, Int oper2 ) { return ! ( oper1 == oper2 ); }
    72 
    73         int ?<?( Int oper1, Int oper2 ) { return mpz_cmp( oper1.mpz, oper2.mpz ) < 0; }
    74         int ?<?( Int oper1, long int oper2 ) { return mpz_cmp_si( oper1.mpz, oper2 ) < 0; }
    75         int ?<?( long int oper2, Int oper1 ) { return mpz_cmp_si( oper1.mpz, oper2 ) < 0; }
    76         int ?<?( Int oper1, unsigned long int oper2 ) { return mpz_cmp_ui( oper1.mpz, oper2 ) < 0; }
    77         int ?<?( unsigned long int oper2, Int oper1 ) { return mpz_cmp_ui( oper1.mpz, oper2 ) < 0; }
    78 
    79         int ?<=?( Int oper1, Int oper2 ) { return mpz_cmp( oper1.mpz, oper2.mpz ) <= 0; }
    80         int ?<=?( Int oper1, long int oper2 ) { return mpz_cmp_si( oper1.mpz, oper2 ) <= 0; }
    81         int ?<=?( long int oper2, Int oper1 ) { return mpz_cmp_si( oper1.mpz, oper2 ) <= 0; }
    82         int ?<=?( Int oper1, unsigned long int oper2 ) { return mpz_cmp_ui( oper1.mpz, oper2 ) <= 0; }
    83         int ?<=?( unsigned long int oper2, Int oper1 ) { return mpz_cmp_ui( oper1.mpz, oper2 ) <= 0; }
    84 
    85         int ?>?( Int oper1, Int oper2 ) { return ! ( oper1 <= oper2 ); }
    86         int ?>?( Int oper1, long int oper2 ) { return ! ( oper1 <= oper2 ); }
    87         int ?>?( long int oper1, Int oper2 ) { return ! ( oper1 <= oper2 ); }
    88         int ?>?( Int oper1, unsigned long int oper2 ) { return ! ( oper1 <= oper2 ); }
    89         int ?>?( unsigned long int oper1, Int oper2 ) { return ! ( oper1 <= oper2 ); }
    90 
    91         int ?>=?( Int oper1, Int oper2 ) { return ! ( oper1 < oper2 ); }
    92         int ?>=?( Int oper1, long int oper2 ) { return ! ( oper1 < oper2 ); }
    93         int ?>=?( long int oper1, Int oper2 ) { return ! ( oper1 < oper2 ); }
    94         int ?>=?( Int oper1, unsigned long int oper2 ) { return ! ( oper1 < oper2 ); }
    95         int ?>=?( unsigned long int oper1, Int oper2 ) { return ! ( oper1 < oper2 ); }
    96 
    97         // arithmetic
    98         Int +?( Int oper ) { Int pos; mpz_set( pos.mpz, oper.mpz ); return pos; }
    99         Int -?( Int oper ) { Int neg; mpz_neg( neg.mpz, oper.mpz ); return neg; }
    100         Int ~?( Int oper ) { Int comp; mpz_com( comp.mpz, oper.mpz ); return comp; }
    101 
    102         Int ?&?( Int oper1, Int oper2 ) { Int conjunction; mpz_and( conjunction.mpz, oper1.mpz, oper2.mpz ); return conjunction; }
    103         Int ?&?( Int oper1, long int oper2 ) { Int conjunction, temp; mpz_set_si( temp.mpz, oper2 ); mpz_and( conjunction.mpz, oper1.mpz, temp.mpz ); return conjunction; }
    104         Int ?&?( long int oper1, Int oper2 ) { Int conjunction, temp; mpz_set_si( temp.mpz, oper1 ); mpz_and( conjunction.mpz, temp.mpz, oper2.mpz ); return conjunction; }
    105         Int ?&?( Int oper1, unsigned long int oper2 ) { Int conjunction, temp; mpz_set_ui( temp.mpz, oper2 ); mpz_and( conjunction.mpz, oper1.mpz, temp.mpz ); return conjunction; }
    106         Int ?&?( unsigned long int oper1, Int oper2 ) { Int conjunction, temp; mpz_set_ui( temp.mpz, oper1 ); mpz_and( conjunction.mpz, temp.mpz, oper2.mpz ); return conjunction; }
    107         Int ?&=?( Int & lhs, Int rhs ) { return lhs = lhs & rhs; }
    108 
    109         Int ?|?( Int oper1, Int oper2 ) { Int disjunction; mpz_ior( disjunction.mpz, oper1.mpz, oper2.mpz ); return disjunction; }
    110         Int ?|?( Int oper1, long int oper2 ) { Int disjunction, temp; mpz_set_si( temp.mpz, oper2 ); mpz_ior( disjunction.mpz, oper1.mpz, temp.mpz ); return disjunction; }
    111         Int ?|?( long int oper1, Int oper2 ) { Int disjunction, temp; mpz_set_si( temp.mpz, oper1 ); mpz_ior( disjunction.mpz, temp.mpz, oper2.mpz ); return disjunction; }
    112         Int ?|?( Int oper1, unsigned long int oper2 ) { Int disjunction, temp; mpz_set_ui( temp.mpz, oper2 ); mpz_ior( disjunction.mpz, oper1.mpz, temp.mpz ); return disjunction; }
    113         Int ?|?( unsigned long int oper1, Int oper2 ) { Int disjunction, temp; mpz_set_ui( temp.mpz, oper1 ); mpz_ior( disjunction.mpz, temp.mpz, oper2.mpz ); return disjunction; }
    114         Int ?|=?( Int & lhs, Int rhs ) { return lhs = lhs | rhs; }
    115 
    116         Int ?^?( Int oper1, Int oper2 ) { Int disjunction; mpz_xor( disjunction.mpz, oper1.mpz, oper2.mpz ); return disjunction; }
    117         Int ?^?( Int oper1, long int oper2 ) { Int disjunction, temp; mpz_set_si( temp.mpz, oper2 ); mpz_ior( disjunction.mpz, oper1.mpz, temp.mpz ); return disjunction; }
    118         Int ?^?( long int oper1, Int oper2 ) { Int disjunction, temp; mpz_set_si( temp.mpz, oper1 ); mpz_ior( disjunction.mpz, temp.mpz, oper2.mpz ); return disjunction; }
    119         Int ?^?( Int oper1, unsigned long int oper2 ) { Int disjunction, temp; mpz_set_ui( temp.mpz, oper2 ); mpz_ior( disjunction.mpz, oper1.mpz, temp.mpz ); return disjunction; }
    120         Int ?^?( unsigned long int oper1, Int oper2 ) { Int disjunction, temp; mpz_set_ui( temp.mpz, oper1 ); mpz_ior( disjunction.mpz, temp.mpz, oper2.mpz ); return disjunction; }
    121         Int ?^=?( Int & lhs, Int rhs ) { return lhs = lhs ^ rhs; }
    122 
    123         Int ?+?( Int addend1, Int addend2 ) { Int sum; mpz_add( sum.mpz, addend1.mpz, addend2.mpz ); return sum; }
    124         Int ?+?( Int addend1, long int addend2 ) { Int sum; if ( addend2 >= 0 ) mpz_add_ui( sum.mpz, addend1.mpz, addend2 ); else mpz_sub_ui( sum.mpz, addend1.mpz, -addend2 ); return sum; }
    125         Int ?+?( long int addend2, Int addend1 ) { Int sum; if ( addend2 >= 0 ) mpz_add_ui( sum.mpz, addend1.mpz, addend2 ); else mpz_sub_ui( sum.mpz, addend1.mpz, -addend2 ); return sum; }
    126         Int ?+?( Int addend1, unsigned long int addend2 ) { Int sum; mpz_add_ui( sum.mpz, addend1.mpz, addend2 ); return sum; }
    127         Int ?+?( unsigned long int addend2, Int addend1 ) { Int sum; mpz_add_ui( sum.mpz, addend1.mpz, addend2 ); return sum; }
    128         Int ?+=?( Int & lhs, Int rhs ) { return lhs = lhs + rhs; }
    129         Int ?+=?( Int & lhs, long int rhs ) { return lhs = lhs + rhs; }
    130         Int ?+=?( Int & lhs, unsigned long int rhs ) { return lhs = lhs + rhs; }
    131         Int ++?( Int & lhs ) { return lhs += 1; }
    132         Int ?++( Int & lhs ) { Int ret = lhs; lhs += 1; return ret; }
    133 
    134         Int ?-?( Int minuend, Int subtrahend ) { Int diff; mpz_sub( diff.mpz, minuend.mpz, subtrahend.mpz ); return diff; }
    135         Int ?-?( Int minuend, long int subtrahend ) { Int diff; if ( subtrahend >= 0 ) mpz_sub_ui( diff.mpz, minuend.mpz, subtrahend ); else mpz_add_ui( diff.mpz, minuend.mpz, -subtrahend ); return diff; }
    136         Int ?-?( long int minuend, Int subtrahend ) { Int diff; if ( subtrahend >= 0 ) mpz_ui_sub( diff.mpz, minuend, subtrahend.mpz ); else { mpz_add_ui( diff.mpz, subtrahend.mpz, -minuend ); mpz_neg( diff.mpz, diff.mpz ); } return diff; }
    137         Int ?-?( Int minuend, unsigned long int subtrahend ) { Int diff; mpz_sub_ui( diff.mpz, minuend.mpz, subtrahend ); return diff; }
    138         Int ?-?( unsigned long int minuend, Int subtrahend ) { Int diff; mpz_ui_sub( diff.mpz, minuend, subtrahend.mpz ); return diff; }
    139         Int ?-=?( Int & lhs, Int rhs ) { return lhs = lhs - rhs; }
    140         Int ?-=?( Int & lhs, long int rhs ) { return lhs = lhs - rhs; }
    141         Int ?-=?( Int & lhs, unsigned long int rhs ) { return lhs = lhs - rhs; }
    142         Int --?( Int & lhs ) { return lhs -= 1; }
    143         Int ?--( Int & lhs ) { Int ret = lhs; lhs -= 1; return ret; }
    144 
    145         Int ?*?( Int multiplicator, Int multiplicand ) { Int product; mpz_mul( product.mpz, multiplicator.mpz, multiplicand.mpz ); return product; }
    146         Int ?*?( Int multiplicator, long int multiplicand ) { Int product; mpz_mul_si( product.mpz, multiplicator.mpz, multiplicand ); return product; }
    147         Int ?*?( long int multiplicand, Int multiplicator ) { Int product; mpz_mul_si( product.mpz, multiplicator.mpz, multiplicand ); return product; }
    148         Int ?*?( Int multiplicator, unsigned long int multiplicand ) { Int product; mpz_mul_ui( product.mpz, multiplicator.mpz, multiplicand ); return product; }
    149         Int ?*?( unsigned long int multiplicand, Int multiplicator ) { Int product; mpz_mul_ui( product.mpz, multiplicator.mpz, multiplicand ); return product; }
    150         Int ?*=?( Int & lhs, Int rhs ) { return lhs = lhs * rhs; }
    151         Int ?*=?( Int & lhs, long int rhs ) { return lhs = lhs * rhs; }
    152         Int ?*=?( Int & lhs, unsigned long int rhs ) { return lhs = lhs * rhs; }
    153 
    154         // some code for operators "/" and "%" taken from g++ gmpxx.h
    155         Int ?/?( Int dividend, Int divisor ) { Int quotient; mpz_tdiv_q( quotient.mpz, dividend.mpz, divisor.mpz ); return quotient; }
    156         Int ?/?( Int dividend, unsigned long int divisor ) { Int quotient; mpz_tdiv_q_ui( quotient.mpz, dividend.mpz, divisor ); return quotient; }
    157         Int ?/?( unsigned long int dividend, Int divisor ) {
    158                 Int quotient;
    159                 if ( mpz_sgn( divisor.mpz ) >= 0 ) {
    160                         if ( mpz_fits_ulong_p( divisor.mpz ) )
    161                                 mpz_set_ui( quotient.mpz, dividend / mpz_get_ui( divisor.mpz ) );
    162                         else
    163                                 mpz_set_ui( quotient.mpz, 0 );
    164                 } else {
    165                         mpz_neg( quotient.mpz, divisor.mpz );
    166                         if ( mpz_fits_ulong_p( quotient.mpz ) ) {
    167                                 mpz_set_ui( quotient.mpz, dividend / mpz_get_ui( quotient.mpz ) );
    168                                 mpz_neg( quotient.mpz, quotient.mpz );
    169                         } else
    170                                 mpz_set_ui( quotient.mpz, 0 );
    171                 } // if
    172                 return quotient;
    173         } // ?/?
    174         Int ?/?( Int dividend, long int divisor ) {
    175                 Int quotient;
    176                 if ( divisor >= 0 )
    177                         mpz_tdiv_q_ui( quotient.mpz, dividend.mpz, divisor );
    178                 else {
    179                         mpz_tdiv_q_ui( quotient.mpz, dividend.mpz, -divisor );
     25// constructor
     26static inline void ?{}( Int & this ) { mpz_init( this.mpz ); }
     27static inline void ?{}( Int & this, Int init ) { mpz_init_set( this.mpz, init.mpz ); }
     28static inline void ?{}( Int & this, zero_t ) { mpz_init_set_si( this.mpz, 0 ); }
     29static inline void ?{}( Int & this, one_t ) { mpz_init_set_si( this.mpz, 1 ); }
     30static inline void ?{}( Int & this, signed long int init ) { mpz_init_set_si( this.mpz, init ); }
     31static inline void ?{}( Int & this, unsigned long int init ) { mpz_init_set_ui( this.mpz, init ); }
     32static inline void ?{}( Int & this, const char * val ) { if ( mpz_init_set_str( this.mpz, val, 0 ) ) abort(); }
     33static inline void ^?{}( Int & this ) { mpz_clear( this.mpz ); }
     34
     35// literal
     36static inline Int ?`mp( signed long int init ) { return (Int){ init }; }
     37static inline Int ?`mp( unsigned long int init ) { return (Int){ init }; }
     38static inline Int ?`mp( const char * init ) { return (Int){ init }; }
     39
     40// assignment
     41static inline Int ?=?( Int & lhs, Int rhs ) { mpz_set( lhs.mpz, rhs.mpz ); return lhs; }
     42static inline Int ?=?( Int & lhs, long int rhs ) { mpz_set_si( lhs.mpz, rhs ); return lhs; }
     43static inline Int ?=?( Int & lhs, unsigned long int rhs ) { mpz_set_ui( lhs.mpz, rhs ); return lhs; }
     44static inline Int ?=?( Int & lhs, const char * rhs ) { if ( mpz_set_str( lhs.mpz, rhs, 0 ) ) { printf( "invalid string conversion\n" ); abort(); } return lhs; }
     45
     46static inline char ?=?( char & lhs, Int rhs ) { char val = mpz_get_si( rhs.mpz ); lhs = val; return lhs; }
     47static inline short int ?=?( short int & lhs, Int rhs ) { short int val = mpz_get_si( rhs.mpz ); lhs = val; return lhs; }
     48static inline int ?=?( int & lhs, Int rhs ) { int val = mpz_get_si( rhs.mpz ); lhs = val; return lhs; }
     49static inline long int ?=?( long int & lhs, Int rhs ) { long int val = mpz_get_si( rhs.mpz ); lhs = val; return lhs; }
     50static inline unsigned char ?=?( unsigned char & lhs, Int rhs ) { unsigned char val = mpz_get_ui( rhs.mpz ); lhs = val; return lhs; }
     51static inline unsigned short int ?=?( unsigned short int & lhs, Int rhs ) { unsigned short int val = mpz_get_ui( rhs.mpz ); lhs = val; return lhs; }
     52static inline unsigned int ?=?( unsigned int & lhs, Int rhs ) { unsigned int val = mpz_get_ui( rhs.mpz ); lhs = val; return lhs; }
     53static inline unsigned long int ?=?( unsigned long int & lhs, Int rhs ) { unsigned long int val = mpz_get_ui( rhs.mpz ); lhs = val; return lhs; }
     54
     55// conversions
     56static inline long int narrow( Int val ) { return mpz_get_si( val.mpz ); }
     57static inline unsigned long int narrow( Int val ) { return mpz_get_ui( val.mpz ); }
     58
     59// comparison
     60static inline int ?==?( Int oper1, Int oper2 ) { return mpz_cmp( oper1.mpz, oper2.mpz ) == 0; }
     61static inline int ?==?( Int oper1, long int oper2 ) { return mpz_cmp_si( oper1.mpz, oper2 ) == 0; }
     62static inline int ?==?( long int oper2, Int oper1 ) { return mpz_cmp_si( oper1.mpz, oper2 ) == 0; }
     63static inline int ?==?( Int oper1, unsigned long int oper2 ) { return mpz_cmp_ui( oper1.mpz, oper2 ) == 0; }
     64static inline int ?==?( unsigned long int oper2, Int oper1 ) { return mpz_cmp_ui( oper1.mpz, oper2 ) == 0; }
     65
     66static inline int ?!=?( Int oper1, Int oper2 ) { return ! ( oper1 == oper2 ); }
     67static inline int ?!=?( Int oper1, long int oper2 ) { return ! ( oper1 == oper2 ); }
     68static inline int ?!=?( long int oper1, Int oper2 ) { return ! ( oper1 == oper2 ); }
     69static inline int ?!=?( Int oper1, unsigned long int oper2 ) { return ! ( oper1 == oper2 ); }
     70static inline int ?!=?( unsigned long int oper1, Int oper2 ) { return ! ( oper1 == oper2 ); }
     71
     72static inline int ?<?( Int oper1, Int oper2 ) { return mpz_cmp( oper1.mpz, oper2.mpz ) < 0; }
     73static inline int ?<?( Int oper1, long int oper2 ) { return mpz_cmp_si( oper1.mpz, oper2 ) < 0; }
     74static inline int ?<?( long int oper2, Int oper1 ) { return mpz_cmp_si( oper1.mpz, oper2 ) < 0; }
     75static inline int ?<?( Int oper1, unsigned long int oper2 ) { return mpz_cmp_ui( oper1.mpz, oper2 ) < 0; }
     76static inline int ?<?( unsigned long int oper2, Int oper1 ) { return mpz_cmp_ui( oper1.mpz, oper2 ) < 0; }
     77
     78static inline int ?<=?( Int oper1, Int oper2 ) { return mpz_cmp( oper1.mpz, oper2.mpz ) <= 0; }
     79static inline int ?<=?( Int oper1, long int oper2 ) { return mpz_cmp_si( oper1.mpz, oper2 ) <= 0; }
     80static inline int ?<=?( long int oper2, Int oper1 ) { return mpz_cmp_si( oper1.mpz, oper2 ) <= 0; }
     81static inline int ?<=?( Int oper1, unsigned long int oper2 ) { return mpz_cmp_ui( oper1.mpz, oper2 ) <= 0; }
     82static inline int ?<=?( unsigned long int oper2, Int oper1 ) { return mpz_cmp_ui( oper1.mpz, oper2 ) <= 0; }
     83
     84static inline int ?>?( Int oper1, Int oper2 ) { return ! ( oper1 <= oper2 ); }
     85static inline int ?>?( Int oper1, long int oper2 ) { return ! ( oper1 <= oper2 ); }
     86static inline int ?>?( long int oper1, Int oper2 ) { return ! ( oper1 <= oper2 ); }
     87static inline int ?>?( Int oper1, unsigned long int oper2 ) { return ! ( oper1 <= oper2 ); }
     88static inline int ?>?( unsigned long int oper1, Int oper2 ) { return ! ( oper1 <= oper2 ); }
     89
     90static inline int ?>=?( Int oper1, Int oper2 ) { return ! ( oper1 < oper2 ); }
     91static inline int ?>=?( Int oper1, long int oper2 ) { return ! ( oper1 < oper2 ); }
     92static inline int ?>=?( long int oper1, Int oper2 ) { return ! ( oper1 < oper2 ); }
     93static inline int ?>=?( Int oper1, unsigned long int oper2 ) { return ! ( oper1 < oper2 ); }
     94static inline int ?>=?( unsigned long int oper1, Int oper2 ) { return ! ( oper1 < oper2 ); }
     95
     96// arithmetic
     97static inline Int +?( Int oper ) { Int pos; mpz_set( pos.mpz, oper.mpz ); return pos; }
     98static inline Int -?( Int oper ) { Int neg; mpz_neg( neg.mpz, oper.mpz ); return neg; }
     99static inline Int ~?( Int oper ) { Int comp; mpz_com( comp.mpz, oper.mpz ); return comp; }
     100
     101static inline Int ?&?( Int oper1, Int oper2 ) { Int conjunction; mpz_and( conjunction.mpz, oper1.mpz, oper2.mpz ); return conjunction; }
     102static inline Int ?&?( Int oper1, long int oper2 ) { Int conjunction, temp; mpz_set_si( temp.mpz, oper2 ); mpz_and( conjunction.mpz, oper1.mpz, temp.mpz ); return conjunction; }
     103static inline Int ?&?( long int oper1, Int oper2 ) { Int conjunction, temp; mpz_set_si( temp.mpz, oper1 ); mpz_and( conjunction.mpz, temp.mpz, oper2.mpz ); return conjunction; }
     104static inline Int ?&?( Int oper1, unsigned long int oper2 ) { Int conjunction, temp; mpz_set_ui( temp.mpz, oper2 ); mpz_and( conjunction.mpz, oper1.mpz, temp.mpz ); return conjunction; }
     105static inline Int ?&?( unsigned long int oper1, Int oper2 ) { Int conjunction, temp; mpz_set_ui( temp.mpz, oper1 ); mpz_and( conjunction.mpz, temp.mpz, oper2.mpz ); return conjunction; }
     106static inline Int ?&=?( Int & lhs, Int rhs ) { return lhs = lhs & rhs; }
     107
     108static inline Int ?|?( Int oper1, Int oper2 ) { Int disjunction; mpz_ior( disjunction.mpz, oper1.mpz, oper2.mpz ); return disjunction; }
     109static inline Int ?|?( Int oper1, long int oper2 ) { Int disjunction, temp; mpz_set_si( temp.mpz, oper2 ); mpz_ior( disjunction.mpz, oper1.mpz, temp.mpz ); return disjunction; }
     110static inline Int ?|?( long int oper1, Int oper2 ) { Int disjunction, temp; mpz_set_si( temp.mpz, oper1 ); mpz_ior( disjunction.mpz, temp.mpz, oper2.mpz ); return disjunction; }
     111static inline Int ?|?( Int oper1, unsigned long int oper2 ) { Int disjunction, temp; mpz_set_ui( temp.mpz, oper2 ); mpz_ior( disjunction.mpz, oper1.mpz, temp.mpz ); return disjunction; }
     112static inline Int ?|?( unsigned long int oper1, Int oper2 ) { Int disjunction, temp; mpz_set_ui( temp.mpz, oper1 ); mpz_ior( disjunction.mpz, temp.mpz, oper2.mpz ); return disjunction; }
     113static inline Int ?|=?( Int & lhs, Int rhs ) { return lhs = lhs | rhs; }
     114
     115static inline Int ?^?( Int oper1, Int oper2 ) { Int disjunction; mpz_xor( disjunction.mpz, oper1.mpz, oper2.mpz ); return disjunction; }
     116static inline Int ?^?( Int oper1, long int oper2 ) { Int disjunction, temp; mpz_set_si( temp.mpz, oper2 ); mpz_ior( disjunction.mpz, oper1.mpz, temp.mpz ); return disjunction; }
     117static inline Int ?^?( long int oper1, Int oper2 ) { Int disjunction, temp; mpz_set_si( temp.mpz, oper1 ); mpz_ior( disjunction.mpz, temp.mpz, oper2.mpz ); return disjunction; }
     118static inline Int ?^?( Int oper1, unsigned long int oper2 ) { Int disjunction, temp; mpz_set_ui( temp.mpz, oper2 ); mpz_ior( disjunction.mpz, oper1.mpz, temp.mpz ); return disjunction; }
     119static inline Int ?^?( unsigned long int oper1, Int oper2 ) { Int disjunction, temp; mpz_set_ui( temp.mpz, oper1 ); mpz_ior( disjunction.mpz, temp.mpz, oper2.mpz ); return disjunction; }
     120static inline Int ?^=?( Int & lhs, Int rhs ) { return lhs = lhs ^ rhs; }
     121
     122static inline Int ?+?( Int addend1, Int addend2 ) { Int sum; mpz_add( sum.mpz, addend1.mpz, addend2.mpz ); return sum; }
     123static inline Int ?+?( Int addend1, long int addend2 ) { Int sum; if ( addend2 >= 0 ) mpz_add_ui( sum.mpz, addend1.mpz, addend2 ); else mpz_sub_ui( sum.mpz, addend1.mpz, -addend2 ); return sum; }
     124static inline Int ?+?( long int addend2, Int addend1 ) { Int sum; if ( addend2 >= 0 ) mpz_add_ui( sum.mpz, addend1.mpz, addend2 ); else mpz_sub_ui( sum.mpz, addend1.mpz, -addend2 ); return sum; }
     125static inline Int ?+?( Int addend1, unsigned long int addend2 ) { Int sum; mpz_add_ui( sum.mpz, addend1.mpz, addend2 ); return sum; }
     126static inline Int ?+?( unsigned long int addend2, Int addend1 ) { Int sum; mpz_add_ui( sum.mpz, addend1.mpz, addend2 ); return sum; }
     127static inline Int ?+=?( Int & lhs, Int rhs ) { return lhs = lhs + rhs; }
     128static inline Int ?+=?( Int & lhs, long int rhs ) { return lhs = lhs + rhs; }
     129static inline Int ?+=?( Int & lhs, unsigned long int rhs ) { return lhs = lhs + rhs; }
     130static inline Int ++?( Int & lhs ) { return lhs += 1; }
     131static inline Int ?++( Int & lhs ) { Int ret = lhs; lhs += 1; return ret; }
     132
     133static inline Int ?-?( Int minuend, Int subtrahend ) { Int diff; mpz_sub( diff.mpz, minuend.mpz, subtrahend.mpz ); return diff; }
     134static inline Int ?-?( Int minuend, long int subtrahend ) { Int diff; if ( subtrahend >= 0 ) mpz_sub_ui( diff.mpz, minuend.mpz, subtrahend ); else mpz_add_ui( diff.mpz, minuend.mpz, -subtrahend ); return diff; }
     135static inline Int ?-?( long int minuend, Int subtrahend ) { Int diff; if ( subtrahend >= 0 ) mpz_ui_sub( diff.mpz, minuend, subtrahend.mpz ); else { mpz_add_ui( diff.mpz, subtrahend.mpz, -minuend ); mpz_neg( diff.mpz, diff.mpz ); } return diff; }
     136static inline Int ?-?( Int minuend, unsigned long int subtrahend ) { Int diff; mpz_sub_ui( diff.mpz, minuend.mpz, subtrahend ); return diff; }
     137static inline Int ?-?( unsigned long int minuend, Int subtrahend ) { Int diff; mpz_ui_sub( diff.mpz, minuend, subtrahend.mpz ); return diff; }
     138static inline Int ?-=?( Int & lhs, Int rhs ) { return lhs = lhs - rhs; }
     139static inline Int ?-=?( Int & lhs, long int rhs ) { return lhs = lhs - rhs; }
     140static inline Int ?-=?( Int & lhs, unsigned long int rhs ) { return lhs = lhs - rhs; }
     141static inline Int --?( Int & lhs ) { return lhs -= 1; }
     142static inline Int ?--( Int & lhs ) { Int ret = lhs; lhs -= 1; return ret; }
     143
     144static inline Int ?*?( Int multiplicator, Int multiplicand ) { Int product; mpz_mul( product.mpz, multiplicator.mpz, multiplicand.mpz ); return product; }
     145static inline Int ?*?( Int multiplicator, long int multiplicand ) { Int product; mpz_mul_si( product.mpz, multiplicator.mpz, multiplicand ); return product; }
     146static inline Int ?*?( long int multiplicand, Int multiplicator ) { Int product; mpz_mul_si( product.mpz, multiplicator.mpz, multiplicand ); return product; }
     147static inline Int ?*?( Int multiplicator, unsigned long int multiplicand ) { Int product; mpz_mul_ui( product.mpz, multiplicator.mpz, multiplicand ); return product; }
     148static inline Int ?*?( unsigned long int multiplicand, Int multiplicator ) { Int product; mpz_mul_ui( product.mpz, multiplicator.mpz, multiplicand ); return product; }
     149static inline Int ?*=?( Int & lhs, Int rhs ) { return lhs = lhs * rhs; }
     150static inline Int ?*=?( Int & lhs, long int rhs ) { return lhs = lhs * rhs; }
     151static inline Int ?*=?( Int & lhs, unsigned long int rhs ) { return lhs = lhs * rhs; }
     152
     153// some code for operators "/" and "%" taken from g++ gmpxx.h
     154static inline Int ?/?( Int dividend, Int divisor ) { Int quotient; mpz_tdiv_q( quotient.mpz, dividend.mpz, divisor.mpz ); return quotient; }
     155static inline Int ?/?( Int dividend, unsigned long int divisor ) { Int quotient; mpz_tdiv_q_ui( quotient.mpz, dividend.mpz, divisor ); return quotient; }
     156static inline Int ?/?( unsigned long int dividend, Int divisor ) {
     157        Int quotient;
     158    if ( mpz_sgn( divisor.mpz ) >= 0 ) {
     159                if ( mpz_fits_ulong_p( divisor.mpz ) )
     160                        mpz_set_ui( quotient.mpz, dividend / mpz_get_ui( divisor.mpz ) );
     161                else
     162                        mpz_set_ui( quotient.mpz, 0 );
     163        } else {
     164                mpz_neg( quotient.mpz, divisor.mpz );
     165                if ( mpz_fits_ulong_p( quotient.mpz ) ) {
     166                        mpz_set_ui( quotient.mpz, dividend / mpz_get_ui( quotient.mpz ) );
    180167                        mpz_neg( quotient.mpz, quotient.mpz );
    181                 } // if
    182                 return quotient;
    183         } // ?/?
    184         Int ?/?( long int dividend, Int divisor ) {
    185                 Int quotient;
    186                 if ( mpz_fits_slong_p( divisor.mpz ) )
    187                         mpz_set_si( quotient.mpz, dividend / mpz_get_si( divisor.mpz ) );
    188                 else {
    189                         // if divisor is bigger than a long then the quotient must be zero, unless dividend==LONG_MIN and
    190                         // dividend==-LONG_MIN in which case the quotient is -1
    191                         mpz_set_si( quotient.mpz, mpz_cmpabs_ui( divisor.mpz, (dividend >= 0 ? dividend : -dividend)) == 0 ? -1 : 0 );
    192                 } // if
    193                 return quotient;
    194         } // ?/?
    195         Int ?/=?( Int & lhs, Int rhs ) { return lhs = lhs / rhs; }
    196         Int ?/=?( Int & lhs, long int rhs ) { return lhs = lhs / rhs; }
    197         Int ?/=?( Int & lhs, unsigned long int rhs ) { return lhs = lhs / rhs; }
    198 
    199         [ Int, Int ] div( Int dividend, Int divisor ) { Int quotient, remainder; mpz_fdiv_qr( quotient.mpz, remainder.mpz, dividend.mpz, divisor.mpz ); return [ quotient, remainder ]; }
    200         [ Int, Int ] div( Int dividend, unsigned long int divisor ) { Int quotient, remainder; mpz_fdiv_qr_ui( quotient.mpz, remainder.mpz, dividend.mpz, divisor ); return [ quotient, remainder ]; }
    201 
    202         Int ?%?( Int dividend, Int divisor ) { Int remainder; mpz_tdiv_r( remainder.mpz, dividend.mpz, divisor.mpz ); return remainder; }
    203         Int ?%?( Int dividend, unsigned long int divisor ) { Int remainder; mpz_tdiv_r_ui( remainder.mpz, dividend.mpz, divisor ); return remainder; }
    204         Int ?%?( unsigned long int dividend, Int divisor ) {
    205                 Int remainder;
    206                 if ( mpz_sgn( divisor.mpz ) >= 0 ) {
    207                         if ( mpz_fits_ulong_p( divisor.mpz ) )
    208                                 mpz_set_ui( remainder.mpz, dividend % mpz_get_ui( divisor.mpz ) );
    209                         else
    210                                 mpz_set_ui( remainder.mpz, dividend );
    211                 } else {
    212                         mpz_neg( remainder.mpz, divisor.mpz );
    213                         if ( mpz_fits_ulong_p( remainder.mpz ) )
    214                                 mpz_set_ui( remainder.mpz, dividend % mpz_get_ui( remainder.mpz ) );
    215                         else
    216                                 mpz_set_ui( remainder.mpz, dividend );
    217                 } // if
    218                 return remainder;
    219         } // ?%?
    220         Int ?%?( Int dividend, long int divisor ) {
    221                 Int remainder;
    222                 mpz_tdiv_r_ui( remainder.mpz, dividend.mpz, (divisor >= 0 ? divisor : -divisor));
    223                 return remainder;
    224         } // ?%?
    225         Int ?%?( long int dividend, Int divisor ) {
    226                 Int remainder;
    227                 if ( mpz_fits_slong_p( divisor.mpz ) )
    228                         mpz_set_si( remainder.mpz, dividend % mpz_get_si( divisor.mpz ) );
    229                 else {
    230                         // if divisor is bigger than a long then the remainder is dividend unchanged, unless dividend==LONG_MIN and
    231                         // dividend==-LONG_MIN in which case the remainder is 0
    232                         mpz_set_si( remainder.mpz, mpz_cmpabs_ui( divisor.mpz, (dividend >= 0 ? dividend : -dividend)) == 0 ? 0 : dividend);
    233                 } // if
    234                 return remainder;
    235         } // ?%?
    236         Int ?%=?( Int & lhs, Int rhs ) { return lhs = lhs % rhs; }
    237         Int ?%=?( Int & lhs, long int rhs ) { return lhs = lhs % rhs; }
    238         Int ?%=?( Int & lhs, unsigned long int rhs ) { return lhs = lhs % rhs; }
    239 
    240         Int ?<<?( Int shiften, mp_bitcnt_t shift ) { Int shifted; mpz_mul_2exp( shifted.mpz, shiften.mpz, shift ); return shifted; }
    241         Int ?<<=?( Int & lhs, mp_bitcnt_t shift ) { return lhs = lhs << shift; }
    242         Int ?>>?( Int shiften, mp_bitcnt_t shift ) { Int shifted; mpz_fdiv_q_2exp( shifted.mpz, shiften.mpz, shift ); return shifted; }
    243         Int ?>>=?( Int & lhs, mp_bitcnt_t shift ) { return lhs = lhs >> shift; }
    244 
    245         // number functions
    246         Int abs( Int oper ) { Int positive; mpz_abs( positive.mpz, oper.mpz ); return positive; }
    247         Int fact( unsigned long int N ) { Int factorial; mpz_fac_ui( factorial.mpz, N ); return factorial; }
    248         Int gcd( Int oper1, Int oper2 ) { Int gcdret; mpz_gcd( gcdret.mpz, oper1.mpz, oper2.mpz ); return gcdret; }
    249         Int pow( Int base, unsigned long int exponent ) { Int power; mpz_pow_ui( power.mpz, base.mpz, exponent ); return power; }
    250         Int pow( unsigned long int base, unsigned long int exponent ) { Int power; mpz_ui_pow_ui( power.mpz, base, exponent ); return power; }
    251         void srandom( gmp_randstate_t state ) { gmp_randinit_default( state ); }
    252         Int random( gmp_randstate_t state, mp_bitcnt_t n ) { Int rand; mpz_urandomb( rand.mpz, state, n ); return rand; }
    253         Int random( gmp_randstate_t state, Int n ) { Int rand; mpz_urandomm( rand.mpz, state, n.mpz ); return rand; }
    254         Int random( gmp_randstate_t state, mp_size_t max_size ) { Int rand; mpz_random( rand.mpz, max_size ); return rand; }
    255         int sgn( Int oper ) { return mpz_sgn( oper.mpz ); }
    256         Int sqrt( Int oper ) { Int root; mpz_sqrt( root.mpz, oper.mpz ); return root; }
    257 
    258         // I/O
    259         forall( dtype istype | istream( istype ) )
    260                 istype & ?|?( istype & is, Int & mp ) {
    261                 gmp_scanf( "%Zd", &mp );
    262                 return is;
     168                } else
     169                        mpz_set_ui( quotient.mpz, 0 );
     170        } // if
     171        return quotient;
     172} // ?/?
     173static inline Int ?/?( Int dividend, long int divisor ) {
     174        Int quotient;
     175    if ( divisor >= 0 )
     176                mpz_tdiv_q_ui( quotient.mpz, dividend.mpz, divisor );
     177    else {
     178                mpz_tdiv_q_ui( quotient.mpz, dividend.mpz, -divisor );
     179                mpz_neg( quotient.mpz, quotient.mpz );
     180        } // if
     181        return quotient;
     182} // ?/?
     183static inline Int ?/?( long int dividend, Int divisor ) {
     184        Int quotient;
     185    if ( mpz_fits_slong_p( divisor.mpz ) )
     186                mpz_set_si( quotient.mpz, dividend / mpz_get_si( divisor.mpz ) );
     187    else {
     188        // if divisor is bigger than a long then the quotient must be zero, unless dividend==LONG_MIN and
     189        // dividend==-LONG_MIN in which case the quotient is -1
     190        mpz_set_si( quotient.mpz, mpz_cmpabs_ui( divisor.mpz, (dividend >= 0 ? dividend : -dividend)) == 0 ? -1 : 0 );
     191        } // if
     192        return quotient;
     193} // ?/?
     194static inline Int ?/=?( Int & lhs, Int rhs ) { return lhs = lhs / rhs; }
     195static inline Int ?/=?( Int & lhs, long int rhs ) { return lhs = lhs / rhs; }
     196static inline Int ?/=?( Int & lhs, unsigned long int rhs ) { return lhs = lhs / rhs; }
     197
     198static inline [ Int, Int ] div( Int dividend, Int divisor ) { Int quotient, remainder; mpz_fdiv_qr( quotient.mpz, remainder.mpz, dividend.mpz, divisor.mpz ); return [ quotient, remainder ]; }
     199static inline [ Int, Int ] div( Int dividend, unsigned long int divisor ) { Int quotient, remainder; mpz_fdiv_qr_ui( quotient.mpz, remainder.mpz, dividend.mpz, divisor ); return [ quotient, remainder ]; }
     200
     201static inline Int ?%?( Int dividend, Int divisor ) { Int remainder; mpz_tdiv_r( remainder.mpz, dividend.mpz, divisor.mpz ); return remainder; }
     202static inline Int ?%?( Int dividend, unsigned long int divisor ) { Int remainder; mpz_tdiv_r_ui( remainder.mpz, dividend.mpz, divisor ); return remainder; }
     203static inline Int ?%?( unsigned long int dividend, Int divisor ) {
     204        Int remainder;
     205    if ( mpz_sgn( divisor.mpz ) >= 0 ) {
     206                if ( mpz_fits_ulong_p( divisor.mpz ) )
     207                        mpz_set_ui( remainder.mpz, dividend % mpz_get_ui( divisor.mpz ) );
     208                else
     209                        mpz_set_ui( remainder.mpz, dividend );
     210        } else {
     211                mpz_neg( remainder.mpz, divisor.mpz );
     212                if ( mpz_fits_ulong_p( remainder.mpz ) )
     213                        mpz_set_ui( remainder.mpz, dividend % mpz_get_ui( remainder.mpz ) );
     214                else
     215                        mpz_set_ui( remainder.mpz, dividend );
     216        } // if
     217        return remainder;
     218} // ?%?
     219static inline Int ?%?( Int dividend, long int divisor ) {
     220        Int remainder;
     221    mpz_tdiv_r_ui( remainder.mpz, dividend.mpz, (divisor >= 0 ? divisor : -divisor));
     222        return remainder;
     223} // ?%?
     224static inline Int ?%?( long int dividend, Int divisor ) {
     225        Int remainder;
     226    if ( mpz_fits_slong_p( divisor.mpz ) )
     227                mpz_set_si( remainder.mpz, dividend % mpz_get_si( divisor.mpz ) );
     228        else {
     229                // if divisor is bigger than a long then the remainder is dividend unchanged, unless dividend==LONG_MIN and
     230                // dividend==-LONG_MIN in which case the remainder is 0
     231        mpz_set_si( remainder.mpz, mpz_cmpabs_ui( divisor.mpz, (dividend >= 0 ? dividend : -dividend)) == 0 ? 0 : dividend);
     232        } // if
     233        return remainder;
     234} // ?%?
     235static inline Int ?%=?( Int & lhs, Int rhs ) { return lhs = lhs % rhs; }
     236static inline Int ?%=?( Int & lhs, long int rhs ) { return lhs = lhs % rhs; }
     237static inline Int ?%=?( Int & lhs, unsigned long int rhs ) { return lhs = lhs % rhs; }
     238
     239static inline Int ?<<?( Int shiften, mp_bitcnt_t shift ) { Int shifted; mpz_mul_2exp( shifted.mpz, shiften.mpz, shift ); return shifted; }
     240static inline Int ?<<=?( Int & lhs, mp_bitcnt_t shift ) { return lhs = lhs << shift; }
     241static inline Int ?>>?( Int shiften, mp_bitcnt_t shift ) { Int shifted; mpz_fdiv_q_2exp( shifted.mpz, shiften.mpz, shift ); return shifted; }
     242static inline Int ?>>=?( Int & lhs, mp_bitcnt_t shift ) { return lhs = lhs >> shift; }
     243
     244// number functions
     245static inline Int abs( Int oper ) { Int positive; mpz_abs( positive.mpz, oper.mpz ); return positive; }
     246static inline Int fact( unsigned long int N ) { Int factorial; mpz_fac_ui( factorial.mpz, N ); return factorial; }
     247static inline Int gcd( Int oper1, Int oper2 ) { Int gcdret; mpz_gcd( gcdret.mpz, oper1.mpz, oper2.mpz ); return gcdret; }
     248static inline Int pow( Int base, unsigned long int exponent ) { Int power; mpz_pow_ui( power.mpz, base.mpz, exponent ); return power; }
     249static inline Int pow( unsigned long int base, unsigned long int exponent ) { Int power; mpz_ui_pow_ui( power.mpz, base, exponent ); return power; }
     250static inline void srandom( gmp_randstate_t state ) { gmp_randinit_default( state ); }
     251static inline Int random( gmp_randstate_t state, mp_bitcnt_t n ) { Int rand; mpz_urandomb( rand.mpz, state, n ); return rand; }
     252static inline Int random( gmp_randstate_t state, Int n ) { Int rand; mpz_urandomm( rand.mpz, state, n.mpz ); return rand; }
     253static inline Int random( gmp_randstate_t state, mp_size_t max_size ) { Int rand; mpz_random( rand.mpz, max_size ); return rand; }
     254static inline int sgn( Int oper ) { return mpz_sgn( oper.mpz ); }
     255static inline Int sqrt( Int oper ) { Int root; mpz_sqrt( root.mpz, oper.mpz ); return root; }
     256
     257// I/O
     258static inline forall( dtype istype | istream( istype ) )
     259istype & ?|?( istype & is, Int & mp ) {
     260        gmp_scanf( "%Zd", &mp );
     261        return is;
     262} // ?|?
     263
     264static inline forall( dtype ostype | ostream( ostype ) ) {
     265        ostype & ?|?( ostype & os, Int mp ) {
     266                if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
     267                gmp_printf( "%Zd", mp.mpz );
     268                sepOn( os );
     269                return os;
    263270        } // ?|?
    264271
    265         forall( dtype ostype | ostream( ostype ) ) {
    266                 ostype & ?|?( ostype & os, Int mp ) {
    267                         if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
    268                         gmp_printf( "%Zd", mp.mpz );
    269                         sepOn( os );
    270                         return os;
    271                 } // ?|?
    272 
    273                 void ?|?( ostype & os, Int mp ) {
    274                         (ostype)(os | mp); ends( os );
    275                 } // ?|?
    276         } // distribution
     272        void ?|?( ostype & os, Int mp ) {
     273                (ostype)(os | mp); nl( os );
     274        } // ?|?
    277275} // distribution
    278276
  • libcfa/src/iostream.cfa

    r302d84c2 ree6dbae  
    1010// Created On       : Wed May 27 17:56:53 2015
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Fri Jul 12 12:04:13 2019
    13 // Update Count     : 819
     12// Last Modified On : Thu Jun 13 17:21:10 2019
     13// Update Count     : 812
    1414//
    1515
     
    3030
    3131
    32 //*********************************** ostream ***********************************
     32//*********************************** Ostream ***********************************
    3333
    3434
     
    4040        } // ?|?
    4141        void ?|?( ostype & os, zero_t z ) {
    42                 (ostype &)(os | z); ends( os );
     42                (ostype &)(os | z); nl( os );
    4343        } // ?|?
    4444
     
    4949        } // ?|?
    5050        void ?|?( ostype & os, one_t o ) {
    51                 (ostype &)(os | o); ends( os );
     51                (ostype &)(os | o); nl( os );
    5252        } // ?|?
    5353
     
    5858        } // ?|?
    5959        void ?|?( ostype & os, bool b ) {
    60                 (ostype &)(os | b); ends( os );
     60                (ostype &)(os | b); nl( os );
    6161        } // ?|?
    6262
     
    6767        } // ?|?
    6868        void ?|?( ostype & os, char c ) {
    69                 (ostype &)(os | c); ends( os );
     69                (ostype &)(os | c); nl( os );
    7070        } // ?|?
    7171
     
    7676        } // ?|?
    7777        void ?|?( ostype & os, signed char sc ) {
    78                 (ostype &)(os | sc); ends( os );
     78                (ostype &)(os | sc); nl( os );
    7979        } // ?|?
    8080
     
    8585        } // ?|?
    8686        void ?|?( ostype & os, unsigned char usc ) {
    87                 (ostype &)(os | usc); ends( os );
     87                (ostype &)(os | usc); nl( os );
    8888        } // ?|?
    8989
     
    9494        } // ?|?
    9595        void & ?|?( ostype & os, short int si ) {
    96                 (ostype &)(os | si); ends( os );
     96                (ostype &)(os | si); nl( os );
    9797        } // ?|?
    9898
     
    103103        } // ?|?
    104104        void & ?|?( ostype & os, unsigned short int usi ) {
    105                 (ostype &)(os | usi); ends( os );
     105                (ostype &)(os | usi); nl( os );
    106106        } // ?|?
    107107
     
    112112        } // ?|?
    113113        void & ?|?( ostype & os, int i ) {
    114                 (ostype &)(os | i); ends( os );
     114                (ostype &)(os | i); nl( os );
    115115        } // ?|?
    116116
     
    121121        } // ?|?
    122122        void & ?|?( ostype & os, unsigned int ui ) {
    123                 (ostype &)(os | ui); ends( os );
     123                (ostype &)(os | ui); nl( os );
    124124        } // ?|?
    125125
     
    130130        } // ?|?
    131131        void & ?|?( ostype & os, long int li ) {
    132                 (ostype &)(os | li); ends( os );
     132                (ostype &)(os | li); nl( os );
    133133        } // ?|?
    134134
     
    139139        } // ?|?
    140140        void & ?|?( ostype & os, unsigned long int uli ) {
    141                 (ostype &)(os | uli); ends( os );
     141                (ostype &)(os | uli); nl( os );
    142142        } // ?|?
    143143
     
    148148        } // ?|?
    149149        void & ?|?( ostype & os, long long int lli ) {
    150                 (ostype &)(os | lli); ends( os );
     150                (ostype &)(os | lli); nl( os );
    151151        } // ?|?
    152152
     
    157157        } // ?|?
    158158        void & ?|?( ostype & os, unsigned long long int ulli ) {
    159                 (ostype &)(os | ulli); ends( os );
     159                (ostype &)(os | ulli); nl( os );
    160160        } // ?|?
    161161
     
    180180        } // ?|?
    181181        void & ?|?( ostype & os, float f ) {
    182                 (ostype &)(os | f); ends( os );
     182                (ostype &)(os | f); nl( os );
    183183        } // ?|?
    184184
     
    189189        } // ?|?
    190190        void & ?|?( ostype & os, double d ) {
    191                 (ostype &)(os | d); ends( os );
     191                (ostype &)(os | d); nl( os );
    192192        } // ?|?
    193193
     
    198198        } // ?|?
    199199        void & ?|?( ostype & os, long double ld ) {
    200                 (ostype &)(os | ld); ends( os );
     200                (ostype &)(os | ld); nl( os );
    201201        } // ?|?
    202202
     
    210210        } // ?|?
    211211        void & ?|?( ostype & os, float _Complex fc ) {
    212                 (ostype &)(os | fc); ends( os );
     212                (ostype &)(os | fc); nl( os );
    213213        } // ?|?
    214214
     
    222222        } // ?|?
    223223        void & ?|?( ostype & os, double _Complex dc ) {
    224                 (ostype &)(os | dc); ends( os );
     224                (ostype &)(os | dc); nl( os );
    225225        } // ?|?
    226226
     
    234234        } // ?|?
    235235        void & ?|?( ostype & os, long double _Complex ldc ) {
    236                 (ostype &)(os | ldc); ends( os );
     236                (ostype &)(os | ldc); nl( os );
    237237        } // ?|?
    238238
     
    276276        } // ?|?
    277277        void ?|?( ostype & os, const char * str ) {
    278                 (ostype &)(os | str); ends( os );
     278                (ostype &)(os | str); nl( os );
    279279        } // ?|?
    280280
     
    305305        } // ?|?
    306306        void ?|?( ostype & os, const void * p ) {
    307                 (ostype &)(os | p); ends( os );
     307                (ostype &)(os | p); nl( os );
    308308        } // ?|?
    309309
     
    315315        void ?|?( ostype & os, ostype & (* manip)( ostype & ) ) {
    316316                (ostype &)(manip( os ));
    317                 if ( getPrt( os ) ) ends( os );                                 // something printed ?
     317                if ( getPrt( os ) ) nl( os );                                   // something printed ?
    318318                setPrt( os, false );                                                    // turn off
    319319        } // ?|?
     
    335335        } // nl
    336336
     337        void nl( ostype & os ) {
     338                if ( getANL( os ) ) (ostype &)(nl( os ));               // implementation only
     339                else setPrt( os, false );                                               // turn off
     340        } // nl
     341
    337342        ostype & nonl( ostype & os ) {
    338343                setPrt( os, false );                                                    // turn off
     
    381386        } // ?|?
    382387        void ?|?( ostype & os, T arg, Params rest ) {
    383                 // (ostype &)(?|?( os, arg, rest )); ends( os );
     388                // (ostype &)(?|?( os, arg, rest )); nl( os );
    384389                (ostype &)(os | arg);                                                   // print first argument
    385390                sepSetCur( os, sepGetTuple( os ) );                             // switch to tuple separator
    386391                (ostype &)(os | rest);                                                  // print remaining arguments
    387392                sepSetCur( os, sepGet( os ) );                                  // switch to regular separator
    388                 ends( os );
     393                nl( os );
    389394        } // ?|?
    390395} // distribution
     
    403408} // distribution
    404409
    405 //*********************************** manipulators ***********************************
    406 
    407 //*********************************** integral ***********************************
     410//*********************************** Manipulators ***********************************
     411
     412//*********************************** Integral ***********************************
    408413
    409414static const char * shortbin[] = { "0", "1", "10", "11", "100", "101", "110", "111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111" };
     
    473478                return os; \
    474479        } /* ?|? */ \
    475         void ?|?( ostype & os, _Ostream_Manip(T) f ) { (ostype &)(os | f); ends( os ); } \
     480        void ?|?( ostype & os, _Ostream_Manip(T) f ) { (ostype &)(os | f); nl( os ); } \
    476481} // distribution
    477482
     
    487492IntegralFMTImpl( unsigned long long int, 'u', "%    *ll ", "%    *.*ll " )
    488493
    489 //*********************************** floating point ***********************************
     494//*********************************** Floating Point ***********************************
    490495
    491496#define PrintWithDP2( os, format, val, ... ) \
     
    536541                return os; \
    537542        } /* ?|? */ \
    538         void ?|?( ostype & os, _Ostream_Manip(T) f ) { (ostype &)(os | f); ends( os ); } \
     543        void ?|?( ostype & os, _Ostream_Manip(T) f ) { (ostype &)(os | f); nl( os ); } \
    539544} // distribution
    540545
     
    542547FloatingPointFMTImpl( long double, "%    *L ", "%    *.*L " )
    543548
    544 //*********************************** character ***********************************
     549//*********************************** Character ***********************************
    545550
    546551forall( dtype ostype | ostream( ostype ) ) {
     
    571576                return os;
    572577        } // ?|?
    573         void ?|?( ostype & os, _Ostream_Manip(char) f ) { (ostype &)(os | f); ends( os ); }
     578        void ?|?( ostype & os, _Ostream_Manip(char) f ) { (ostype &)(os | f); nl( os ); }
    574579} // distribution
    575580
    576 //*********************************** C string ***********************************
     581//*********************************** C String ***********************************
    577582
    578583forall( dtype ostype | ostream( ostype ) ) {
     
    616621                return os;
    617622        } // ?|?
    618         void ?|?( ostype & os, _Ostream_Manip(const char *) f ) { (ostype &)(os | f); ends( os ); }
     623        void ?|?( ostype & os, _Ostream_Manip(const char *) f ) { (ostype &)(os | f); nl( os ); }
    619624} // distribution
    620625
    621626
    622 //*********************************** istream ***********************************
     627//*********************************** Istream ***********************************
    623628
    624629
     
    766771} // distribution
    767772
    768 //*********************************** manipulators ***********************************
     773//*********************************** Manipulators ***********************************
    769774
    770775forall( dtype istype | istream( istype ) )
  • libcfa/src/iostream.hfa

    r302d84c2 ree6dbae  
    1010// Created On       : Wed May 27 17:56:53 2015
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Fri Jul 12 12:08:38 2019
    13 // Update Count     : 334
     12// Last Modified On : Thu Jun 13 17:20:21 2019
     13// Update Count     : 325
    1414//
    1515
     
    1919
    2020
    21 //*********************************** ostream ***********************************
     21//*********************************** Ostream ***********************************
    2222
    2323
     
    4747        void sepSetTuple( ostype &, const char * );                     // set tuple separator to string (15 character maximum)
    4848
    49         void ends( ostype & os );                                                       // end of output statement
    5049        int fail( ostype & );
    5150        int flush( ostype & );
     
    9998        void ?|?( ostype &, unsigned long long int );
    10099
    101         ostype & ?|?( ostype &, float );
    102         void ?|?( ostype &, float );
     100        ostype & ?|?( ostype &, float ); // FIX ME: should not be required
     101        void ?|?( ostype &, float ); // FIX ME: should not be required
    103102        ostype & ?|?( ostype &, double );
    104103        void ?|?( ostype &, double );
     
    127126        void ?|?( ostype &, ostype & (*)( ostype & ) );
    128127        ostype & nl( ostype & );
     128        void nl( ostype & );
    129129        ostype & nonl( ostype & );
    130130        ostype & sep( ostype & );
     
    150150} // distribution
    151151
    152 //*********************************** manipulators ***********************************
     152//*********************************** Manipulators ***********************************
    153153
    154154forall( otype T )
     
    169169}; // _Ostream_Manip
    170170
    171 //*********************************** integral ***********************************
     171//*********************************** Integral ***********************************
    172172
    173173// See 6.7.9. 19) The initialization shall occur in initializer list order, each initializer provided for a particular
     
    207207IntegralFMTDecl( unsigned long long int, 'u' )
    208208
    209 //*********************************** floating point ***********************************
     209//*********************************** Floating Point ***********************************
    210210
    211211// Default suffix for values with no fraction is "."
     
    236236FloatingPointFMTDecl( long double )
    237237
    238 //*********************************** character ***********************************
     238//*********************************** Character ***********************************
    239239
    240240static inline {
     
    253253} // ?|?
    254254
    255 //*********************************** C string ***********************************
     255//*********************************** C String ***********************************
    256256
    257257static inline {
     
    272272
    273273
    274 //*********************************** istream ***********************************
     274//*********************************** Istream ***********************************
    275275
    276276
     
    326326} // distribution
    327327
    328 //*********************************** manipulators ***********************************
     328//*********************************** Manipulators ***********************************
    329329
    330330struct _Istream_Cstr {
     
    403403
    404404
    405 //*********************************** time ***********************************
     405//*********************************** Time ***********************************
    406406
    407407
  • libcfa/src/rational.cfa

    r302d84c2 ree6dbae  
    1010// Created On       : Wed Apr  6 17:54:28 2016
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Fri Jul 12 12:02:36 2019
    13 // Update Count     : 183
     12// Last Modified On : Thu Mar 28 17:33:03 2019
     13// Update Count     : 181
    1414//
    1515
     
    167167
    168168                void ?|?( ostype & os, Rational(RationalImpl) r ) {
    169                         (ostype &)(os | r); ends( os );
     169                        (ostype &)(os | r); nl( os );
    170170                } // ?|?
    171171        } // distribution
  • libcfa/src/time.cfa

    r302d84c2 ree6dbae  
    1010// Created On       : Tue Mar 27 13:33:14 2018
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Fri Jul 12 12:03:19 2019
    13 // Update Count     : 59
     12// Last Modified On : Sun Dec 23 22:57:48 2018
     13// Update Count     : 57
    1414//
    1515
     
    3737                if ( ns != 0 ) {                                                                // some ?
    3838                        char buf[16];
    39                         (ostype &)(os | nanomsd( ns, buf ));            // print nanoseconds
     39                        (ostype &)(os | nanomsd( ns, buf ));                    // print nanoseconds
    4040                } // if
    4141                return os;
     
    4343
    4444        void ?|?( ostype & os, Duration dur ) with( dur ) {
    45                 (ostype &)(os | dur); ends( os );
     45                (ostype &)(os | dur); nl( os );
    4646        } // ?|?
    4747} // distribution
     
    150150                long int ns = (tv < 0 ? -tv : tv) % TIMEGRAN;   // compute nanoseconds
    151151                if ( ns == 0 ) {                                                                // none ?
    152                         (ostype &)(os | buf);                                           // print date/time/year
     152                        (ostype &)(os | buf);                                                   // print date/time/year
    153153                } else {
    154154                        buf[19] = '\0';                                                         // truncate to "Wed Jun 30 21:49:08"
    155155                        char buf2[16];
    156156                        nanomsd( ns, buf2 );                                            // compute nanoseconds
    157                         (ostype &)(os | buf | buf2 | ' ' | &buf[20]); // print date/time, nanoseconds and year
     157                        (ostype &)(os | buf | buf2 | ' ' | &buf[20]);   // print date/time, nanoseconds and year
    158158                } // if
    159159                return os;
     
    161161
    162162        void ?|?( ostype & os, Time time ) with( time ) {
    163                 (ostype &)(os | time); ends( os );
     163                (ostype &)(os | time); nl( os );
    164164        } // ?|?
    165165} // distribution
  • tests/loopctrl.cfa

    r302d84c2 ree6dbae  
    1010// Created On       : Wed Aug  8 18:32:59 2018
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Fri Jul 12 12:05:05 2019
    13 // Update Count     : 106
     12// Last Modified On : Sat Apr 13 11:03:09 2019
     13// Update Count     : 104
    1414//
    1515
     
    3232S ?-=?( S & t, one_t ) { t.i -= 1; t.j -= 1; return t; }
    3333ofstream & ?|?( ofstream & os, S v ) { return os | '(' | v.i | v.j | ')'; }
    34 void & ?|?( ofstream & os, S v ) { (ofstream &)(os | v); ends( os ); }
     34void & ?|?( ofstream & os, S v ) { (ofstream &)(os | v); nl( os ); }
    3535
    3636int main() {
  • tests/math1.cfa

    r302d84c2 ree6dbae  
    1010// Created On       : Fri Apr 22 14:59:21 2016
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Fri Jul 12 12:04:56 2019
    13 // Update Count     : 111
     12// Last Modified On : Mon Mar 25 22:56:47 2019
     13// Update Count     : 109
    1414//
    1515
     
    5959        S ?\?( S s, unsigned long y ) { return (S){ s.i \ y }; }
    6060        ofstream & ?|?( ofstream & os, S s ) { return os | s.i; }
    61         void ?|?( ofstream & os, S s ) { (ofstream &)(os | s); ends( os ); }
     61        void ?|?( ofstream & os, S s ) { (ofstream &)(os | s); nl( os ); }
    6262        S s = { 4 };
    6363        S x = s \ 2;
  • tests/sum.cfa

    r302d84c2 ree6dbae  
    1111// Created On       : Wed May 27 17:56:53 2015
    1212// Last Modified By : Peter A. Buhr
    13 // Last Modified On : Fri Jul 12 12:05:16 2019
    14 // Update Count     : 335
     13// Last Modified On : Thu Jun  6 16:18:22 2019
     14// Update Count     : 333
    1515//
    1616
     
    9797        S ?++( S & t ) { S temp = t; t += (S){1}; return temp; }
    9898        ofstream & ?|?( ofstream & os, S v ) { return os | v.i | v.j; }
    99         void ?|?( ofstream & os, S v ) { (ofstream &)(os | v); ends( os ); }
     99        void ?|?( ofstream & os, S v ) { (ofstream &)(os | v); nl( os ); }
    100100
    101101        S s = (S){0}, a[size], v = { low, low };
     
    184184        S ?++( S & t ) { S temp = t; t += (S){1}; return temp; }
    185185        ofstream & ?|?( ofstream & os, S v ) { return os | v.i | v.j; }
    186         void ?|?( ofstream & os, S v ) { (ofstream &)(os | v); ends( os ); }
     186        void ?|?( ofstream & os, S v ) { (ofstream &)(os | v); nl( os ); }
    187187
    188188        S s = (S){0}, a[size], v = { low, low };
  • tests/swap.cfa

    r302d84c2 ree6dbae  
    1010// Created On       : Wed May 27 17:56:53 2015
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Fri Jul 12 12:05:26 2019
    13 // Update Count     : 79
     12// Last Modified On : Sun Dec 23 23:00:49 2018
     13// Update Count     : 77
    1414//
    1515
     
    8585        struct S { int i, j; } s1 = { 1, 2 }, s2 = { 2, 1 };
    8686        ofstream & ?|?( ofstream & os, S s ) { return os | s.i | s.j; }
    87         void ?|?( ofstream & os, S s ) { (ofstream &)(os | s.i | s.j); ends( os ); }
     87        void ?|?( ofstream & os, S s ) { (ofstream &)(os | s.i | s.j); nl( os ); }
    8888        sout | "struct S\t\t" | s1 | "," | s2 | "\t\tswap " | nonl;
    8989        swap( s1, s2 );
Note: See TracChangeset for help on using the changeset viewer.