Changeset b429026 for src/libcfa


Ignore:
Timestamp:
Jun 4, 2018, 11:55:02 AM (7 years ago)
Author:
Rob Schluntz <rschlunt@…>
Branches:
ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, deferred_resn, demangler, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, with_gc
Children:
ee3c93d
Parents:
0a75b77 (diff), 249d6e6 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge branch 'master' of plg.uwaterloo.ca:/u/cforall/software/cfa/cfa-cc

Location:
src/libcfa
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • src/libcfa/iostream

    r0a75b77 rb429026  
    1010// Created On       : Wed May 27 17:56:53 2015
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Sat Apr 28 13:08:24 2018
    13 // Update Count     : 152
     12// Last Modified On : Sat Jun  2 08:07:55 2018
     13// Update Count     : 153
    1414//
    1515
     
    5656// implement writable for intrinsic types
    5757
    58 forall( dtype ostype | ostream( ostype ) ) ostype & ?|?( ostype &, _Bool );
     58forall( dtype ostype | ostream( ostype ) ) {
     59        ostype & ?|?( ostype &, _Bool );
    5960
    60 forall( dtype ostype | ostream( ostype ) ) ostype & ?|?( ostype &, char );
    61 forall( dtype ostype | ostream( ostype ) ) ostype & ?|?( ostype &, signed char );
    62 forall( dtype ostype | ostream( ostype ) ) ostype & ?|?( ostype &, unsigned char );
     61        ostype & ?|?( ostype &, char );
     62        ostype & ?|?( ostype &, signed char );
     63        ostype & ?|?( ostype &, unsigned char );
    6364
    64 forall( dtype ostype | ostream( ostype ) ) ostype & ?|?( ostype &, short int );
    65 forall( dtype ostype | ostream( ostype ) ) ostype & ?|?( ostype &, unsigned short int );
    66 forall( dtype ostype | ostream( ostype ) ) ostype & ?|?( ostype &, int );
    67 forall( dtype ostype | ostream( ostype ) ) ostype & ?|?( ostype &, unsigned int );
    68 forall( dtype ostype | ostream( ostype ) ) ostype & ?|?( ostype &, long int );
    69 forall( dtype ostype | ostream( ostype ) ) ostype & ?|?( ostype &, long long int );
    70 forall( dtype ostype | ostream( ostype ) ) ostype & ?|?( ostype &, unsigned long int );
    71 forall( dtype ostype | ostream( ostype ) ) ostype & ?|?( ostype &, unsigned long long int );
     65        ostype & ?|?( ostype &, short int );
     66        ostype & ?|?( ostype &, unsigned short int );
     67        ostype & ?|?( ostype &, int );
     68        ostype & ?|?( ostype &, unsigned int );
     69        ostype & ?|?( ostype &, long int );
     70        ostype & ?|?( ostype &, long long int );
     71        ostype & ?|?( ostype &, unsigned long int );
     72        ostype & ?|?( ostype &, unsigned long long int );
    7273
    73 forall( dtype ostype | ostream( ostype ) ) ostype & ?|?( ostype &, float ); // FIX ME: should not be required
    74 forall( dtype ostype | ostream( ostype ) ) ostype & ?|?( ostype &, double );
    75 forall( dtype ostype | ostream( ostype ) ) ostype & ?|?( ostype &, long double );
     74        ostype & ?|?( ostype &, float ); // FIX ME: should not be required
     75        ostype & ?|?( ostype &, double );
     76        ostype & ?|?( ostype &, long double );
    7677
    77 forall( dtype ostype | ostream( ostype ) ) ostype & ?|?( ostype &, float _Complex );
    78 forall( dtype ostype | ostream( ostype ) ) ostype & ?|?( ostype &, double _Complex );
    79 forall( dtype ostype | ostream( ostype ) ) ostype & ?|?( ostype &, long double _Complex );
     78        ostype & ?|?( ostype &, float _Complex );
     79        ostype & ?|?( ostype &, double _Complex );
     80        ostype & ?|?( ostype &, long double _Complex );
    8081
    81 forall( dtype ostype | ostream( ostype ) ) ostype & ?|?( ostype &, const char * );
    82 //forall( dtype ostype | ostream( ostype ) ) ostype & ?|?( ostype &, const char16_t * );
     82        ostype & ?|?( ostype &, const char * );
     83        // ostype & ?|?( ostype &, const char16_t * );
    8384#if ! ( __ARM_ARCH_ISA_ARM == 1 && __ARM_32BIT_STATE == 1 ) // char32_t == wchar_t => ambiguous
    84 //forall( dtype ostype | ostream( ostype ) ) ostype & ?|?( ostype &, const char32_t * );
     85        // ostype & ?|?( ostype &, const char32_t * );
    8586#endif // ! ( __ARM_ARCH_ISA_ARM == 1 && __ARM_32BIT_STATE == 1 )
    86 //forall( dtype ostype | ostream( ostype ) ) ostype & ?|?( ostype &, const wchar_t * );
    87 forall( dtype ostype | ostream( ostype ) ) ostype & ?|?( ostype &, const void * );
     87        // ostype & ?|?( ostype &, const wchar_t * );
     88        ostype & ?|?( ostype &, const void * );
     89
     90        // manipulators
     91        ostype & ?|?( ostype &, ostype & (*)( ostype & ) );
     92        ostype & endl( ostype & );
     93        ostype & sep( ostype & );
     94        ostype & sepTuple( ostype & );
     95        ostype & sepOn( ostype & );
     96        ostype & sepOff( ostype & );
     97        ostype & sepDisable( ostype & );
     98        ostype & sepEnable( ostype & );
     99} // distribution
    88100
    89101// tuples
    90102forall( dtype ostype, otype T, ttype Params | writeable( T, ostype ) | { ostype & ?|?( ostype &, Params ); } )
    91103ostype & ?|?( ostype & os, T arg, Params rest );
    92 
    93 // manipulators
    94 forall( dtype ostype | ostream( ostype ) ) ostype & ?|?( ostype &, ostype & (*)( ostype & ) );
    95 forall( dtype ostype | ostream( ostype ) ) ostype & endl( ostype & );
    96 forall( dtype ostype | ostream( ostype ) ) ostype & sep( ostype & );
    97 forall( dtype ostype | ostream( ostype ) ) ostype & sepTuple( ostype & );
    98 forall( dtype ostype | ostream( ostype ) ) ostype & sepOn( ostype & );
    99 forall( dtype ostype | ostream( ostype ) ) ostype & sepOff( ostype & );
    100 forall( dtype ostype | ostream( ostype ) ) ostype & sepDisable( ostype & );
    101 forall( dtype ostype | ostream( ostype ) ) ostype & sepEnable( ostype & );
    102104
    103105// writes the range [begin, end) to the given stream
     
    124126}; // readable
    125127
    126 forall( dtype istype | istream( istype ) ) istype & ?|?( istype &, _Bool & );
     128forall( dtype istype | istream( istype ) ) {
     129        istype & ?|?( istype &, _Bool & );
    127130
    128 forall( dtype istype | istream( istype ) ) istype & ?|?( istype &, char & );
    129 forall( dtype istype | istream( istype ) ) istype & ?|?( istype &, signed char & );
    130 forall( dtype istype | istream( istype ) ) istype & ?|?( istype &, unsigned char & );
     131        istype & ?|?( istype &, char & );
     132        istype & ?|?( istype &, signed char & );
     133        istype & ?|?( istype &, unsigned char & );
    131134
    132 forall( dtype istype | istream( istype ) ) istype & ?|?( istype &, short int & );
    133 forall( dtype istype | istream( istype ) ) istype & ?|?( istype &, unsigned short int & );
    134 forall( dtype istype | istream( istype ) ) istype & ?|?( istype &, int & );
    135 forall( dtype istype | istream( istype ) ) istype & ?|?( istype &, unsigned int & );
    136 forall( dtype istype | istream( istype ) ) istype & ?|?( istype &, long int & );
    137 forall( dtype istype | istream( istype ) ) istype & ?|?( istype &, long long int & );
    138 forall( dtype istype | istream( istype ) ) istype & ?|?( istype &, unsigned long int & );
    139 forall( dtype istype | istream( istype ) ) istype & ?|?( istype &, unsigned long long int & );
     135        istype & ?|?( istype &, short int & );
     136        istype & ?|?( istype &, unsigned short int & );
     137        istype & ?|?( istype &, int & );
     138        istype & ?|?( istype &, unsigned int & );
     139        istype & ?|?( istype &, long int & );
     140        istype & ?|?( istype &, long long int & );
     141        istype & ?|?( istype &, unsigned long int & );
     142        istype & ?|?( istype &, unsigned long long int & );
    140143
    141 forall( dtype istype | istream( istype ) ) istype & ?|?( istype &, float & );
    142 forall( dtype istype | istream( istype ) ) istype & ?|?( istype &, double & );
    143 forall( dtype istype | istream( istype ) ) istype & ?|?( istype &, long double & );
     144        istype & ?|?( istype &, float & );
     145        istype & ?|?( istype &, double & );
     146        istype & ?|?( istype &, long double & );
    144147
    145 forall( dtype istype | istream( istype ) ) istype & ?|?( istype &, float _Complex & );
    146 forall( dtype istype | istream( istype ) ) istype & ?|?( istype &, double _Complex & );
    147 forall( dtype istype | istream( istype ) ) istype & ?|?( istype &, long double _Complex & );
     148        istype & ?|?( istype &, float _Complex & );
     149        istype & ?|?( istype &, double _Complex & );
     150        istype & ?|?( istype &, long double _Complex & );
    148151
    149 // manipulators
    150 forall( dtype istype | istream( istype ) ) istype & ?|?( istype &, istype & (*)( istype & ) );
    151 forall( dtype istype | istream( istype ) ) istype & endl( istype & is );
     152        // manipulators
     153        istype & ?|?( istype &, istype & (*)( istype & ) );
     154        istype & endl( istype & is );
     155} // distribution
    152156
    153157struct _Istream_cstrUC { char * s; };
  • src/libcfa/iostream.c

    r0a75b77 rb429026  
    1010// Created On       : Wed May 27 17:56:53 2015
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Sat Apr 28 13:08:25 2018
    13 // Update Count     : 469
     12// Last Modified On : Sat Jun  2 08:24:56 2018
     13// Update Count     : 471
    1414//
    1515
     
    2626}
    2727
    28 forall( dtype ostype | ostream( ostype ) )
    29 ostype & ?|?( ostype & os, _Bool b ) {
    30         if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
    31         fmt( os, "%s", b ? "true" : "false" );
    32         return os;
    33 } // ?|?
    34 
    35 forall( dtype ostype | ostream( ostype ) )
    36 ostype & ?|?( ostype & os, char ch ) {
    37         fmt( os, "%c", ch );
    38         if ( ch == '\n' ) setNL( os, true );
    39         sepOff( os );
    40         return os;
    41 } // ?|?
    42 
    43 forall( dtype ostype | ostream( ostype ) )
    44 ostype & ?|?( ostype & os, signed char c ) {
    45         if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
    46         fmt( os, "%hhd", c );
    47         return os;
    48 } // ?|?
    49 
    50 forall( dtype ostype | ostream( ostype ) )
    51 ostype & ?|?( ostype & os, unsigned char c ) {
    52         if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
    53         fmt( os, "%hhu", c );
    54         return os;
    55 } // ?|?
    56 
    57 forall( dtype ostype | ostream( ostype ) )
    58 ostype & ?|?( ostype & os, short int si ) {
    59         if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
    60         fmt( os, "%hd", si );
    61         return os;
    62 } // ?|?
    63 
    64 forall( dtype ostype | ostream( ostype ) )
    65 ostype & ?|?( ostype & os, unsigned short int usi ) {
    66         if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
    67         fmt( os, "%hu", usi );
    68         return os;
    69 } // ?|?
    70 
    71 forall( dtype ostype | ostream( ostype ) )
    72 ostype & ?|?( ostype & os, int i ) {
    73         if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
    74         fmt( os, "%d", i );
    75         return os;
    76 } // ?|?
    77 
    78 forall( dtype ostype | ostream( ostype ) )
    79 ostype & ?|?( ostype & os, unsigned int ui ) {
    80         if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
    81         fmt( os, "%u", ui );
    82         return os;
    83 } // ?|?
    84 
    85 forall( dtype ostype | ostream( ostype ) )
    86 ostype & ?|?( ostype & os, long int li ) {
    87         if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
    88         fmt( os, "%ld", li );
    89         return os;
    90 } // ?|?
    91 
    92 forall( dtype ostype | ostream( ostype ) )
    93 ostype & ?|?( ostype & os, unsigned long int uli ) {
    94         if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
    95         fmt( os, "%lu", uli );
    96         return os;
    97 } // ?|?
    98 
    99 forall( dtype ostype | ostream( ostype ) )
    100 ostype & ?|?( ostype & os, long long int lli ) {
    101         if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
    102         fmt( os, "%lld", lli );
    103         return os;
    104 } // ?|?
    105 
    106 forall( dtype ostype | ostream( ostype ) )
    107 ostype & ?|?( ostype & os, unsigned long long int ulli ) {
    108         if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
    109         fmt( os, "%llu", ulli );
    110         return os;
    111 } // ?|?
    112 
    113 forall( dtype ostype | ostream( ostype ) )
    114 ostype & ?|?( ostype & os, float f ) {
    115         if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
    116         fmt( os, "%g", f );
    117         return os;
    118 } // ?|?
    119 
    120 forall( dtype ostype | ostream( ostype ) )
    121 ostype & ?|?( ostype & os, double d ) {
    122         if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
    123         fmt( os, "%.*lg", DBL_DIG, d );
    124         return os;
    125 } // ?|?
    126 
    127 forall( dtype ostype | ostream( ostype ) )
    128 ostype & ?|?( ostype & os, long double ld ) {
    129         if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
    130         fmt( os, "%.*Lg", LDBL_DIG, ld );
    131         return os;
    132 } // ?|?
    133 
    134 forall( dtype ostype | ostream( ostype ) )
    135 ostype & ?|?( ostype & os, float _Complex fc ) {
    136         if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
    137         fmt( os, "%g%+gi", crealf( fc ), cimagf( fc ) );
    138         return os;
    139 } // ?|?
    140 
    141 forall( dtype ostype | ostream( ostype ) )
    142 ostype & ?|?( ostype & os, double _Complex dc ) {
    143         if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
    144         fmt( os, "%.*lg%+.*lgi", DBL_DIG, creal( dc ), DBL_DIG, cimag( dc ) );
    145         return os;
    146 } // ?|?
    147 
    148 forall( dtype ostype | ostream( ostype ) )
    149 ostype & ?|?( ostype & os, long double _Complex ldc ) {
    150         if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
    151         fmt( os, "%.*Lg%+.*Lgi", LDBL_DIG, creall( ldc ), LDBL_DIG, cimagl( ldc ) );
    152         return os;
    153 } // ?|?
    154 
    155 forall( dtype ostype | ostream( ostype ) )
    156 ostype & ?|?( ostype & os, const char * str ) {
    157         enum { Open = 1, Close, OpenClose };
    158         static const unsigned char mask[256] @= {
    159                 // opening delimiters, no space after
    160                 ['('] : Open, ['['] : Open, ['{'] : Open,
    161                 ['='] : Open, ['$'] : Open, [(unsigned char)'£'] : Open, [(unsigned char)'¥'] : Open,
    162                 [(unsigned char)'¡'] : Open, [(unsigned char)'¿'] : Open, [(unsigned char)'«'] : Open,
    163                 // closing delimiters, no space before
    164                 [','] : Close, ['.'] : Close, [';'] : Close, ['!'] : Close, ['?'] : Close,
    165                 ['%'] : Close, [(unsigned char)'¢'] : Close, [(unsigned char)'»'] : Close,
    166                 [')'] : Close, [']'] : Close, ['}'] : Close,
    167                 // opening-closing delimiters, no space before or after
    168                 ['\''] : OpenClose, ['`'] : OpenClose, ['"'] : OpenClose, [':'] : OpenClose,
    169                 [' '] : OpenClose, ['\f'] : OpenClose, ['\n'] : OpenClose, ['\r'] : OpenClose, ['\t'] : OpenClose, ['\v'] : OpenClose, // isspace
    170         }; // mask
    171 
    172   if ( str[0] == '\0' ) { sepOff( os ); return os; }            // null string => no separator
    173 
    174         // first character IS NOT spacing or closing punctuation => add left separator
    175         unsigned char ch = str[0];                                                      // must make unsigned
    176         if ( sepPrt( os ) && mask[ ch ] != Close && mask[ ch ] != OpenClose ) {
    177                 fmt( os, "%s", sepGetCur( os ) );
    178         } // if
    179 
    180         // if string starts line, must reset to determine open state because separator is off
    181         sepReset( os );                                                                         // reset separator
    182 
    183         // last character IS spacing or opening punctuation => turn off separator for next item
    184         size_t len = strlen( str );
    185         ch = str[len - 1];                                                                      // must make unsigned
    186         if ( sepPrt( os ) && mask[ ch ] != Open && mask[ ch ] != OpenClose ) {
     28forall( dtype ostype | ostream( ostype ) ) {
     29        ostype & ?|?( ostype & os, _Bool b ) {
     30                if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
     31                fmt( os, "%s", b ? "true" : "false" );
     32                return os;
     33        } // ?|?
     34
     35        ostype & ?|?( ostype & os, char ch ) {
     36                fmt( os, "%c", ch );
     37                if ( ch == '\n' ) setNL( os, true );
     38                sepOff( os );
     39                return os;
     40        } // ?|?
     41
     42        ostype & ?|?( ostype & os, signed char c ) {
     43                if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
     44                fmt( os, "%hhd", c );
     45                return os;
     46        } // ?|?
     47
     48        ostype & ?|?( ostype & os, unsigned char c ) {
     49                if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
     50                fmt( os, "%hhu", c );
     51                return os;
     52        } // ?|?
     53
     54        ostype & ?|?( ostype & os, short int si ) {
     55                if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
     56                fmt( os, "%hd", si );
     57                return os;
     58        } // ?|?
     59
     60        ostype & ?|?( ostype & os, unsigned short int usi ) {
     61                if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
     62                fmt( os, "%hu", usi );
     63                return os;
     64        } // ?|?
     65
     66        ostype & ?|?( ostype & os, int i ) {
     67                if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
     68                fmt( os, "%d", i );
     69                return os;
     70        } // ?|?
     71
     72        ostype & ?|?( ostype & os, unsigned int ui ) {
     73                if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
     74                fmt( os, "%u", ui );
     75                return os;
     76        } // ?|?
     77
     78        ostype & ?|?( ostype & os, long int li ) {
     79                if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
     80                fmt( os, "%ld", li );
     81                return os;
     82        } // ?|?
     83
     84        ostype & ?|?( ostype & os, unsigned long int uli ) {
     85                if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
     86                fmt( os, "%lu", uli );
     87                return os;
     88        } // ?|?
     89
     90        ostype & ?|?( ostype & os, long long int lli ) {
     91                if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
     92                fmt( os, "%lld", lli );
     93                return os;
     94        } // ?|?
     95
     96        ostype & ?|?( ostype & os, unsigned long long int ulli ) {
     97                if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
     98                fmt( os, "%llu", ulli );
     99                return os;
     100        } // ?|?
     101
     102        ostype & ?|?( ostype & os, float f ) {
     103                if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
     104                fmt( os, "%g", f );
     105                return os;
     106        } // ?|?
     107
     108        ostype & ?|?( ostype & os, double d ) {
     109                if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
     110                fmt( os, "%.*lg", DBL_DIG, d );
     111                return os;
     112        } // ?|?
     113
     114        ostype & ?|?( ostype & os, long double ld ) {
     115                if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
     116                fmt( os, "%.*Lg", LDBL_DIG, ld );
     117                return os;
     118        } // ?|?
     119
     120        ostype & ?|?( ostype & os, float _Complex fc ) {
     121                if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
     122                fmt( os, "%g%+gi", crealf( fc ), cimagf( fc ) );
     123                return os;
     124        } // ?|?
     125
     126        ostype & ?|?( ostype & os, double _Complex dc ) {
     127                if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
     128                fmt( os, "%.*lg%+.*lgi", DBL_DIG, creal( dc ), DBL_DIG, cimag( dc ) );
     129                return os;
     130        } // ?|?
     131
     132        ostype & ?|?( ostype & os, long double _Complex ldc ) {
     133                if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
     134                fmt( os, "%.*Lg%+.*Lgi", LDBL_DIG, creall( ldc ), LDBL_DIG, cimagl( ldc ) );
     135                return os;
     136        } // ?|?
     137
     138        ostype & ?|?( ostype & os, const char * str ) {
     139                enum { Open = 1, Close, OpenClose };
     140                static const unsigned char mask[256] @= {
     141                        // opening delimiters, no space after
     142                        ['('] : Open, ['['] : Open, ['{'] : Open,
     143                        ['='] : Open, ['$'] : Open, [(unsigned char)'£'] : Open, [(unsigned char)'¥'] : Open,
     144                        [(unsigned char)'¡'] : Open, [(unsigned char)'¿'] : Open, [(unsigned char)'«'] : Open,
     145                        // closing delimiters, no space before
     146                        [','] : Close, ['.'] : Close, [';'] : Close, ['!'] : Close, ['?'] : Close,
     147                        ['%'] : Close, [(unsigned char)'¢'] : Close, [(unsigned char)'»'] : Close,
     148                        [')'] : Close, [']'] : Close, ['}'] : Close,
     149                        // opening-closing delimiters, no space before or after
     150                        ['\''] : OpenClose, ['`'] : OpenClose, ['"'] : OpenClose, [':'] : OpenClose,
     151                        [' '] : OpenClose, ['\f'] : OpenClose, ['\n'] : OpenClose, ['\r'] : OpenClose, ['\t'] : OpenClose, ['\v'] : OpenClose, // isspace
     152                }; // mask
     153
     154          if ( str[0] == '\0' ) { sepOff( os ); return os; } // null string => no separator
     155
     156                // first character IS NOT spacing or closing punctuation => add left separator
     157                unsigned char ch = str[0];                                              // must make unsigned
     158                if ( sepPrt( os ) && mask[ ch ] != Close && mask[ ch ] != OpenClose ) {
     159                        fmt( os, "%s", sepGetCur( os ) );
     160                } // if
     161
     162                // if string starts line, must reset to determine open state because separator is off
     163                sepReset( os );                                                                 // reset separator
     164
     165                // last character IS spacing or opening punctuation => turn off separator for next item
     166                size_t len = strlen( str );
     167                ch = str[len - 1];                                                              // must make unsigned
     168                if ( sepPrt( os ) && mask[ ch ] != Open && mask[ ch ] != OpenClose ) {
     169                        sepOn( os );
     170                } else {
     171                        sepOff( os );
     172                } // if
     173                if ( ch == '\n' ) setNL( os, true );                    // check *AFTER* sepPrt call above as it resets NL flag
     174                return write( os, str, len );
     175        } // ?|?
     176
     177//      ostype & ?|?( ostype & os, const char16_t * str ) {
     178//              if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
     179//              fmt( os, "%ls", str );
     180//              return os;
     181//      } // ?|?
     182
     183// #if ! ( __ARM_ARCH_ISA_ARM == 1 && __ARM_32BIT_STATE == 1 ) // char32_t == wchar_t => ambiguous
     184//      ostype & ?|?( ostype & os, const char32_t * str ) {
     185//              if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
     186//              fmt( os, "%ls", str );
     187//              return os;
     188//      } // ?|?
     189// #endif // ! ( __ARM_ARCH_ISA_ARM == 1 && __ARM_32BIT_STATE == 1 )
     190
     191//      ostype & ?|?( ostype & os, const wchar_t * str ) {
     192//              if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
     193//              fmt( os, "%ls", str );
     194//              return os;
     195//      } // ?|?
     196
     197        ostype & ?|?( ostype & os, const void * p ) {
     198                if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
     199                fmt( os, "%p", p );
     200                return os;
     201        } // ?|?
     202
     203
     204        // manipulators
     205        ostype & ?|?( ostype & os, ostype & (* manip)( ostype & ) ) {
     206                return manip( os );
     207        } // ?|?
     208
     209        ostype & sep( ostype & os ) {
     210                os | sepGet( os );
     211                return os;
     212        } // sep
     213
     214        ostype & sepTuple( ostype & os ) {
     215                os | sepGetTuple( os );
     216                return os;
     217        } // sepTuple
     218
     219        ostype & endl( ostype & os ) {
     220                os | '\n';
     221                setNL( os, true );
     222                flush( os );
     223                sepOff( os );                                                                   // prepare for next line
     224                return os;
     225        } // endl
     226
     227        ostype & sepOn( ostype & os ) {
    187228                sepOn( os );
    188         } else {
     229                return os;
     230        } // sepOn
     231
     232        ostype & sepOff( ostype & os ) {
    189233                sepOff( os );
    190         } // if
    191         if ( ch == '\n' ) setNL( os, true );                            // check *AFTER* sepPrt call above as it resets NL flag
    192         return write( os, str, len );
    193 } // ?|?
    194 
    195 // forall( dtype ostype | ostream( ostype ) )
    196 // ostype & ?|?( ostype & os, const char16_t * str ) {
    197 //      if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
    198 //      fmt( os, "%ls", str );
    199 //      return os;
    200 // } // ?|?
    201 
    202 // #if ! ( __ARM_ARCH_ISA_ARM == 1 && __ARM_32BIT_STATE == 1 ) // char32_t == wchar_t => ambiguous
    203 // forall( dtype ostype | ostream( ostype ) )
    204 // ostype & ?|?( ostype & os, const char32_t * str ) {
    205 //      if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
    206 //      fmt( os, "%ls", str );
    207 //      return os;
    208 // } // ?|?
    209 // #endif // ! ( __ARM_ARCH_ISA_ARM == 1 && __ARM_32BIT_STATE == 1 )
    210 
    211 // forall( dtype ostype | ostream( ostype ) )
    212 // ostype & ?|?( ostype & os, const wchar_t * str ) {
    213 //      if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
    214 //      fmt( os, "%ls", str );
    215 //      return os;
    216 // } // ?|?
    217 
    218 forall( dtype ostype | ostream( ostype ) )
    219 ostype & ?|?( ostype & os, const void * p ) {
    220         if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
    221         fmt( os, "%p", p );
    222         return os;
    223 } // ?|?
     234                return os;
     235        } // sepOff
     236
     237        ostype & sepEnable( ostype & os ) {
     238                sepEnable( os );
     239                return os;
     240        } // sepEnable
     241
     242        ostype & sepDisable( ostype & os ) {
     243                sepDisable( os );
     244                return os;
     245        } // sepDisable
     246} // distribution
    224247
    225248
     
    234257} // ?|?
    235258
    236 
    237 // manipulators
    238 forall( dtype ostype | ostream( ostype ) )
    239 ostype & ?|?( ostype & os, ostype & (* manip)( ostype & ) ) {
    240         return manip( os );
    241 } // ?|?
    242 
    243 forall( dtype ostype | ostream( ostype ) )
    244 ostype & sep( ostype & os ) {
    245         os | sepGet( os );
    246         return os;
    247 } // sep
    248 
    249 forall( dtype ostype | ostream( ostype ) )
    250 ostype & sepTuple( ostype & os ) {
    251         os | sepGetTuple( os );
    252         return os;
    253 } // sepTuple
    254 
    255 forall( dtype ostype | ostream( ostype ) )
    256 ostype & endl( ostype & os ) {
    257         os | '\n';
    258         setNL( os, true );
    259         flush( os );
    260         sepOff( os );                                                                           // prepare for next line
    261         return os;
    262 } // endl
    263 
    264 forall( dtype ostype | ostream( ostype ) )
    265 ostype & sepOn( ostype & os ) {
    266         sepOn( os );
    267         return os;
    268 } // sepOn
    269 
    270 forall( dtype ostype | ostream( ostype ) )
    271 ostype & sepOff( ostype & os ) {
    272         sepOff( os );
    273         return os;
    274 } // sepOff
    275 
    276 forall( dtype ostype | ostream( ostype ) )
    277 ostype & sepEnable( ostype & os ) {
    278         sepEnable( os );
    279         return os;
    280 } // sepEnable
    281 
    282 forall( dtype ostype | ostream( ostype ) )
    283 ostype & sepDisable( ostype & os ) {
    284         sepDisable( os );
    285         return os;
    286 } // sepDisable
    287 
    288259//---------------------------------------
    289260
     261// writes the range [begin, end) to the given stream
    290262forall( dtype ostype, otype elt_type | writeable( elt_type, ostype ), otype iterator_type | iterator( iterator_type, elt_type ) )
    291263void write( iterator_type begin, iterator_type end, ostype & os ) {
     
    302274//---------------------------------------
    303275
    304 forall( dtype istype | istream( istype ) )
    305 istype & ?|?( istype & is, _Bool & b ) {
    306         char val[6];
    307         fmt( is, "%5s", val );
    308         if ( strcmp( val, "true" ) == 0 ) b = true;
    309         else if ( strcmp( val, "false" ) == 0 ) b = false;
    310         else {
    311                 fprintf( stderr, "invalid _Bool constant\n" );
    312                 abort();
    313         } // if
    314         return is;
    315 } // ?|?
    316 
    317 forall( dtype istype | istream( istype ) )
    318 istype & ?|?( istype & is, char & c ) {
    319         fmt( is, "%c", &c );                                                            // must pass pointer through varg to fmt
    320         return is;
    321 } // ?|?
    322 
    323 forall( dtype istype | istream( istype ) )
    324 istype & ?|?( istype & is, signed char & sc ) {
    325         fmt( is, "%hhd", &sc );
    326         return is;
    327 } // ?|?
    328 
    329 forall( dtype istype | istream( istype ) )
    330 istype & ?|?( istype & is, unsigned char & usc ) {
    331         fmt( is, "%hhu", &usc );
    332         return is;
    333 } // ?|?
    334 
    335 forall( dtype istype | istream( istype ) )
    336 istype & ?|?( istype & is, short int & si ) {
    337         fmt( is, "%hd", &si );
    338         return is;
    339 } // ?|?
    340 
    341 forall( dtype istype | istream( istype ) )
    342 istype & ?|?( istype & is, unsigned short int & usi ) {
    343         fmt( is, "%hu", &usi );
    344         return is;
    345 } // ?|?
    346 
    347 forall( dtype istype | istream( istype ) )
    348 istype & ?|?( istype & is, int & i ) {
    349         fmt( is, "%d", &i );
    350         return is;
    351 } // ?|?
    352 
    353 forall( dtype istype | istream( istype ) )
    354 istype & ?|?( istype & is, unsigned int & ui ) {
    355         fmt( is, "%u", &ui );
    356         return is;
    357 } // ?|?
    358 
    359 forall( dtype istype | istream( istype ) )
    360 istype & ?|?( istype & is, long int & li ) {
    361         fmt( is, "%ld", &li );
    362         return is;
    363 } // ?|?
    364 
    365 forall( dtype istype | istream( istype ) )
    366 istype & ?|?( istype & is, unsigned long int & ulli ) {
    367         fmt( is, "%lu", &ulli );
    368         return is;
    369 } // ?|?
    370 
    371 forall( dtype istype | istream( istype ) )
    372 istype & ?|?( istype & is, long long int & lli ) {
    373         fmt( is, "%lld", &lli );
    374         return is;
    375 } // ?|?
    376 
    377 forall( dtype istype | istream( istype ) )
    378 istype & ?|?( istype & is, unsigned long long int & ulli ) {
    379         fmt( is, "%llu", &ulli );
    380         return is;
    381 } // ?|?
    382 
    383 
    384 forall( dtype istype | istream( istype ) )
    385 istype & ?|?( istype & is, float & f ) {
    386         fmt( is, "%f", &f );
    387         return is;
    388 } // ?|?
    389 
    390 forall( dtype istype | istream( istype ) )
    391 istype & ?|?( istype & is, double & d ) {
    392         fmt( is, "%lf", &d );
    393         return is;
    394 } // ?|?
    395 
    396 forall( dtype istype | istream( istype ) )
    397 istype & ?|?( istype & is, long double & ld ) {
    398         fmt( is, "%Lf", &ld );
    399         return is;
    400 } // ?|?
    401 
    402 
    403 forall( dtype istype | istream( istype ) )
    404 istype & ?|?( istype & is, float _Complex & fc ) {
    405         float re, im;
    406         fmt( is, "%g%gi", &re, &im );
    407         fc = re + im * _Complex_I;
    408         return is;
    409 } // ?|?
    410 
    411 forall( dtype istype | istream( istype ) )
    412 istype & ?|?( istype & is, double _Complex & dc ) {
    413         double re, im;
    414         fmt( is, "%lf%lfi", &re, &im );
    415         dc = re + im * _Complex_I;
    416         return is;
    417 } // ?|?
    418 
    419 forall( dtype istype | istream( istype ) )
    420 istype & ?|?( istype & is, long double _Complex & ldc ) {
    421         long double re, im;
    422         fmt( is, "%Lf%Lfi", &re, &im );
    423         ldc = re + im * _Complex_I;
    424         return is;
    425 } // ?|?
    426 
    427 forall( dtype istype | istream( istype ) )
    428 istype & ?|?( istype & is, istype & (* manip)( istype & ) ) {
    429         return manip( is );
    430 } // ?|?
    431 
    432 forall( dtype istype | istream( istype ) )
    433 istype & endl( istype & is ) {
    434         fmt( is, "%*[ \t\f\n\r\v]" );                                           // ignore whitespace
    435         return is;
    436 } // endl
     276forall( dtype istype | istream( istype ) ) {
     277        istype & ?|?( istype & is, _Bool & b ) {
     278                char val[6];
     279                fmt( is, "%5s", val );
     280                if ( strcmp( val, "true" ) == 0 ) b = true;
     281                else if ( strcmp( val, "false" ) == 0 ) b = false;
     282                else {
     283                        fprintf( stderr, "invalid _Bool constant\n" );
     284                        abort();
     285                } // if
     286                return is;
     287        } // ?|?
     288
     289        istype & ?|?( istype & is, char & c ) {
     290                fmt( is, "%c", &c );                                                    // must pass pointer through varg to fmt
     291                return is;
     292        } // ?|?
     293
     294        istype & ?|?( istype & is, signed char & sc ) {
     295                fmt( is, "%hhd", &sc );
     296                return is;
     297        } // ?|?
     298
     299        istype & ?|?( istype & is, unsigned char & usc ) {
     300                fmt( is, "%hhu", &usc );
     301                return is;
     302        } // ?|?
     303
     304        istype & ?|?( istype & is, short int & si ) {
     305                fmt( is, "%hd", &si );
     306                return is;
     307        } // ?|?
     308
     309        istype & ?|?( istype & is, unsigned short int & usi ) {
     310                fmt( is, "%hu", &usi );
     311                return is;
     312        } // ?|?
     313
     314        istype & ?|?( istype & is, int & i ) {
     315                fmt( is, "%d", &i );
     316                return is;
     317        } // ?|?
     318
     319        istype & ?|?( istype & is, unsigned int & ui ) {
     320                fmt( is, "%u", &ui );
     321                return is;
     322        } // ?|?
     323
     324        istype & ?|?( istype & is, long int & li ) {
     325                fmt( is, "%ld", &li );
     326                return is;
     327        } // ?|?
     328
     329        istype & ?|?( istype & is, unsigned long int & ulli ) {
     330                fmt( is, "%lu", &ulli );
     331                return is;
     332        } // ?|?
     333
     334        istype & ?|?( istype & is, long long int & lli ) {
     335                fmt( is, "%lld", &lli );
     336                return is;
     337        } // ?|?
     338
     339        istype & ?|?( istype & is, unsigned long long int & ulli ) {
     340                fmt( is, "%llu", &ulli );
     341                return is;
     342        } // ?|?
     343
     344
     345        istype & ?|?( istype & is, float & f ) {
     346                fmt( is, "%f", &f );
     347                return is;
     348        } // ?|?
     349
     350        istype & ?|?( istype & is, double & d ) {
     351                fmt( is, "%lf", &d );
     352                return is;
     353        } // ?|?
     354
     355        istype & ?|?( istype & is, long double & ld ) {
     356                fmt( is, "%Lf", &ld );
     357                return is;
     358        } // ?|?
     359
     360
     361        istype & ?|?( istype & is, float _Complex & fc ) {
     362                float re, im;
     363                fmt( is, "%g%gi", &re, &im );
     364                fc = re + im * _Complex_I;
     365                return is;
     366        } // ?|?
     367
     368        istype & ?|?( istype & is, double _Complex & dc ) {
     369                double re, im;
     370                fmt( is, "%lf%lfi", &re, &im );
     371                dc = re + im * _Complex_I;
     372                return is;
     373        } // ?|?
     374
     375        istype & ?|?( istype & is, long double _Complex & ldc ) {
     376                long double re, im;
     377                fmt( is, "%Lf%Lfi", &re, &im );
     378                ldc = re + im * _Complex_I;
     379                return is;
     380        } // ?|?
     381
     382
     383        // manipulators
     384        istype & ?|?( istype & is, istype & (* manip)( istype & ) ) {
     385                return manip( is );
     386        } // ?|?
     387
     388        istype & endl( istype & is ) {
     389                fmt( is, "%*[ \t\f\n\r\v]" );                                   // ignore whitespace
     390                return is;
     391        } // endl
     392} // distribution
    437393
    438394_Istream_cstrUC cstr( char * str ) { return (_Istream_cstrUC){ str }; }
  • src/libcfa/rational

    r0a75b77 rb429026  
    1212// Created On       : Wed Apr  6 17:56:25 2016
    1313// Last Modified By : Peter A. Buhr
    14 // Last Modified On : Wed Dec  6 23:12:53 2017
    15 // Update Count     : 97
     14// Last Modified On : Sat Jun  2 09:10:01 2018
     15// Update Count     : 105
    1616//
    1717
     
    4646// implementation
    4747
    48 forall( otype RationalImpl | arithmetic( RationalImpl ) )
    49 struct Rational {
    50         RationalImpl numerator, denominator;                            // invariant: denominator > 0
    51 }; // Rational
     48forall( otype RationalImpl | arithmetic( RationalImpl ) ) {
     49        struct Rational {
     50                RationalImpl numerator, denominator;                    // invariant: denominator > 0
     51        }; // Rational
    5252
    53 // constructors
     53        // constructors
    5454
    55 forall( otype RationalImpl | arithmetic( RationalImpl ) )
    56 void ?{}( Rational(RationalImpl) & r );
     55        void ?{}( Rational(RationalImpl) & r );
     56        void ?{}( Rational(RationalImpl) & r, RationalImpl n );
     57        void ?{}( Rational(RationalImpl) & r, RationalImpl n, RationalImpl d );
     58        void ?{}( Rational(RationalImpl) & r, zero_t );
     59        void ?{}( Rational(RationalImpl) & r, one_t );
    5760
    58 forall( otype RationalImpl | arithmetic( RationalImpl ) )
    59 void ?{}( Rational(RationalImpl) & r, RationalImpl n );
     61        // numerator/denominator getter
    6062
    61 forall( otype RationalImpl | arithmetic( RationalImpl ) )
    62 void ?{}( Rational(RationalImpl) & r, RationalImpl n, RationalImpl d );
     63        RationalImpl numerator( Rational(RationalImpl) r );
     64        RationalImpl denominator( Rational(RationalImpl) r );
     65        [ RationalImpl, RationalImpl ] ?=?( & [ RationalImpl, RationalImpl ] dest, Rational(RationalImpl) src );
    6366
    64 forall( otype RationalImpl | arithmetic( RationalImpl ) )
    65 void ?{}( Rational(RationalImpl) & r, zero_t );
     67        // numerator/denominator setter
    6668
    67 forall( otype RationalImpl | arithmetic( RationalImpl ) )
    68 void ?{}( Rational(RationalImpl) & r, one_t );
     69        RationalImpl numerator( Rational(RationalImpl) r, RationalImpl n );
     70        RationalImpl denominator( Rational(RationalImpl) r, RationalImpl d );
    6971
    70 // numerator/denominator getter
     72        // comparison
    7173
    72 forall( otype RationalImpl | arithmetic( RationalImpl ) )
    73 RationalImpl numerator( Rational(RationalImpl) r );
     74        int ?==?( Rational(RationalImpl) l, Rational(RationalImpl) r );
     75        int ?!=?( Rational(RationalImpl) l, Rational(RationalImpl) r );
     76        int ?<?( Rational(RationalImpl) l, Rational(RationalImpl) r );
     77        int ?<=?( Rational(RationalImpl) l, Rational(RationalImpl) r );
     78        int ?>?( Rational(RationalImpl) l, Rational(RationalImpl) r );
     79        int ?>=?( Rational(RationalImpl) l, Rational(RationalImpl) r );
    7480
    75 forall( otype RationalImpl | arithmetic( RationalImpl ) )
    76 RationalImpl denominator( Rational(RationalImpl) r );
     81        // arithmetic
    7782
    78 forall( otype RationalImpl | arithmetic( RationalImpl ) )
    79 [ RationalImpl, RationalImpl ] ?=?( & [ RationalImpl, RationalImpl ] dest, Rational(RationalImpl) src );
     83        Rational(RationalImpl) +?( Rational(RationalImpl) r );
     84        Rational(RationalImpl) -?( Rational(RationalImpl) r );
     85        Rational(RationalImpl) ?+?( Rational(RationalImpl) l, Rational(RationalImpl) r );
     86        Rational(RationalImpl) ?-?( Rational(RationalImpl) l, Rational(RationalImpl) r );
     87        Rational(RationalImpl) ?*?( Rational(RationalImpl) l, Rational(RationalImpl) r );
     88        Rational(RationalImpl) ?/?( Rational(RationalImpl) l, Rational(RationalImpl) r );
    8089
    81 // numerator/denominator setter
     90        // I/O
     91        forall( dtype istype | istream( istype ) | { istype & ?|?( istype &, RationalImpl & ); } )
     92        istype & ?|?( istype &, Rational(RationalImpl) & );
    8293
    83 forall( otype RationalImpl | arithmetic( RationalImpl ) )
    84 RationalImpl numerator( Rational(RationalImpl) r, RationalImpl n );
    85 
    86 forall( otype RationalImpl | arithmetic( RationalImpl ) )
    87 RationalImpl denominator( Rational(RationalImpl) r, RationalImpl d );
    88 
    89 // comparison
    90 
    91 forall( otype RationalImpl | arithmetic( RationalImpl ) )
    92 int ?==?( Rational(RationalImpl) l, Rational(RationalImpl) r );
    93 
    94 forall( otype RationalImpl | arithmetic( RationalImpl ) )
    95 int ?!=?( Rational(RationalImpl) l, Rational(RationalImpl) r );
    96 
    97 forall( otype RationalImpl | arithmetic( RationalImpl ) )
    98 int ?<?( Rational(RationalImpl) l, Rational(RationalImpl) r );
    99 
    100 forall( otype RationalImpl | arithmetic( RationalImpl ) )
    101 int ?<=?( Rational(RationalImpl) l, Rational(RationalImpl) r );
    102 
    103 forall( otype RationalImpl | arithmetic( RationalImpl ) )
    104 int ?>?( Rational(RationalImpl) l, Rational(RationalImpl) r );
    105 
    106 forall( otype RationalImpl | arithmetic( RationalImpl ) )
    107 int ?>=?( Rational(RationalImpl) l, Rational(RationalImpl) r );
    108 
    109 // arithmetic
    110 
    111 forall( otype RationalImpl | arithmetic( RationalImpl ) )
    112 Rational(RationalImpl) +?( Rational(RationalImpl) r );
    113 
    114 forall( otype RationalImpl | arithmetic( RationalImpl ) )
    115 Rational(RationalImpl) -?( Rational(RationalImpl) r );
    116 
    117 forall( otype RationalImpl | arithmetic( RationalImpl ) )
    118 Rational(RationalImpl) ?+?( Rational(RationalImpl) l, Rational(RationalImpl) r );
    119 
    120 forall( otype RationalImpl | arithmetic( RationalImpl ) )
    121 Rational(RationalImpl) ?-?( Rational(RationalImpl) l, Rational(RationalImpl) r );
    122 
    123 forall( otype RationalImpl | arithmetic( RationalImpl ) )
    124 Rational(RationalImpl) ?*?( Rational(RationalImpl) l, Rational(RationalImpl) r );
    125 
    126 forall( otype RationalImpl | arithmetic( RationalImpl ) )
    127 Rational(RationalImpl) ?/?( Rational(RationalImpl) l, Rational(RationalImpl) r );
     94        forall( dtype ostype | ostream( ostype ) | { ostype & ?|?( ostype &, RationalImpl ); } )
     95        ostype & ?|?( ostype &, Rational(RationalImpl ) );
     96} // distribution
    12897
    12998// conversion
     
    133102Rational(RationalImpl) narrow( double f, RationalImpl md );
    134103
    135 // I/O
    136 forall( otype RationalImpl | arithmetic( RationalImpl ) )
    137 forall( dtype istype | istream( istype ) | { istype & ?|?( istype &, RationalImpl & ); } )
    138 istype & ?|?( istype &, Rational(RationalImpl) & );
    139 
    140 forall( otype RationalImpl | arithmetic( RationalImpl ) )
    141 forall( dtype ostype | ostream( ostype ) | { ostype & ?|?( ostype &, RationalImpl ); } )
    142 ostype & ?|?( ostype &, Rational(RationalImpl ) );
    143 
    144104// Local Variables: //
    145105// mode: c //
  • src/libcfa/rational.c

    r0a75b77 rb429026  
    1010// Created On       : Wed Apr  6 17:54:28 2016
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Wed Dec  6 23:13:58 2017
    13 // Update Count     : 156
     12// Last Modified On : Sat Jun  2 09:24:33 2018
     13// Update Count     : 162
    1414//
    1515
     
    1818#include "stdlib"
    1919
    20 // helper routines
    21 
    22 // Calculate greatest common denominator of two numbers, the first of which may be negative. Used to reduce rationals.
    23 // alternative: https://en.wikipedia.org/wiki/Binary_GCD_algorithm
    24 forall( otype RationalImpl | arithmetic( RationalImpl ) )
    25 static RationalImpl gcd( RationalImpl a, RationalImpl b ) {
    26         for ( ;; ) {                                                                            // Euclid's algorithm
    27                 RationalImpl r = a % b;
    28           if ( r == (RationalImpl){0} ) break;
    29                 a = b;
    30                 b = r;
    31         } // for
    32         return b;
    33 } // gcd
    34 
    35 forall( otype RationalImpl | arithmetic( RationalImpl ) )
    36 static RationalImpl simplify( RationalImpl & n, RationalImpl & d ) {
    37         if ( d == (RationalImpl){0} ) {
    38                 serr | "Invalid rational number construction: denominator cannot be equal to 0." | endl;
    39                 exit( EXIT_FAILURE );
    40         } // exit
    41         if ( d < (RationalImpl){0} ) { d = -d; n = -n; }        // move sign to numerator
    42         return gcd( abs( n ), d );                                                      // simplify
    43 } // Rationalnumber::simplify
    44 
    45 
    46 // constructors
    47 
    48 forall( otype RationalImpl | arithmetic( RationalImpl ) )
    49 void ?{}( Rational(RationalImpl) & r ) {
    50         r{ (RationalImpl){0}, (RationalImpl){1} };
    51 } // rational
    52 
    53 forall( otype RationalImpl | arithmetic( RationalImpl ) )
    54 void ?{}( Rational(RationalImpl) & r, RationalImpl n ) {
    55         r{ n, (RationalImpl){1} };
    56 } // rational
    57 
    58 forall( otype RationalImpl | arithmetic( RationalImpl ) )
    59 void ?{}( Rational(RationalImpl) & r, RationalImpl n, RationalImpl d ) {
    60         RationalImpl t = simplify( n, d );                                      // simplify
    61         r.numerator = n / t;
    62         r.denominator = d / t;
    63 } // rational
    64 
    65 
    66 // getter for numerator/denominator
    67 
    68 forall( otype RationalImpl | arithmetic( RationalImpl ) )
    69 RationalImpl numerator( Rational(RationalImpl) r ) {
    70         return r.numerator;
    71 } // numerator
    72 
    73 forall( otype RationalImpl | arithmetic( RationalImpl ) )
    74 RationalImpl denominator( Rational(RationalImpl) r ) {
    75         return r.denominator;
    76 } // denominator
    77 
    78 forall( otype RationalImpl | arithmetic( RationalImpl ) )
    79 [ RationalImpl, RationalImpl ] ?=?( & [ RationalImpl, RationalImpl ] dest, Rational(RationalImpl) src ) {
    80         return dest = src.[ numerator, denominator ];
    81 }
    82 
    83 // setter for numerator/denominator
    84 
    85 forall( otype RationalImpl | arithmetic( RationalImpl ) )
    86 RationalImpl numerator( Rational(RationalImpl) r, RationalImpl n ) {
    87         RationalImpl prev = r.numerator;
    88         RationalImpl t = gcd( abs( n ), r.denominator );                // simplify
    89         r.numerator = n / t;
    90         r.denominator = r.denominator / t;
    91         return prev;
    92 } // numerator
    93 
    94 forall( otype RationalImpl | arithmetic( RationalImpl ) )
    95 RationalImpl denominator( Rational(RationalImpl) r, RationalImpl d ) {
    96         RationalImpl prev = r.denominator;
    97         RationalImpl t = simplify( r.numerator, d );                    // simplify
    98         r.numerator = r.numerator / t;
    99         r.denominator = d / t;
    100         return prev;
    101 } // denominator
    102 
    103 
    104 // comparison
    105 
    106 forall( otype RationalImpl | arithmetic( RationalImpl ) )
    107 int ?==?( Rational(RationalImpl) l, Rational(RationalImpl) r ) {
    108         return l.numerator * r.denominator == l.denominator * r.numerator;
    109 } // ?==?
    110 
    111 forall( otype RationalImpl | arithmetic( RationalImpl ) )
    112 int ?!=?( Rational(RationalImpl) l, Rational(RationalImpl) r ) {
    113         return ! ( l == r );
    114 } // ?!=?
    115 
    116 forall( otype RationalImpl | arithmetic( RationalImpl ) )
    117 int ?<?( Rational(RationalImpl) l, Rational(RationalImpl) r ) {
    118         return l.numerator * r.denominator < l.denominator * r.numerator;
    119 } // ?<?
    120 
    121 forall( otype RationalImpl | arithmetic( RationalImpl ) )
    122 int ?<=?( Rational(RationalImpl) l, Rational(RationalImpl) r ) {
    123         return l.numerator * r.denominator <= l.denominator * r.numerator;
    124 } // ?<=?
    125 
    126 forall( otype RationalImpl | arithmetic( RationalImpl ) )
    127 int ?>?( Rational(RationalImpl) l, Rational(RationalImpl) r ) {
    128         return ! ( l <= r );
    129 } // ?>?
    130 
    131 forall( otype RationalImpl | arithmetic( RationalImpl ) )
    132 int ?>=?( Rational(RationalImpl) l, Rational(RationalImpl) r ) {
    133         return ! ( l < r );
    134 } // ?>=?
    135 
    136 
    137 // arithmetic
    138 
    139 forall( otype RationalImpl | arithmetic( RationalImpl ) )
    140 Rational(RationalImpl) +?( Rational(RationalImpl) r ) {
    141         Rational(RationalImpl) t = { r.numerator, r.denominator };
    142         return t;
    143 } // +?
    144 
    145 forall( otype RationalImpl | arithmetic( RationalImpl ) )
    146 Rational(RationalImpl) -?( Rational(RationalImpl) r ) {
    147         Rational(RationalImpl) t = { -r.numerator, r.denominator };
    148         return t;
    149 } // -?
    150 
    151 forall( otype RationalImpl | arithmetic( RationalImpl ) )
    152 Rational(RationalImpl) ?+?( Rational(RationalImpl) l, Rational(RationalImpl) r ) {
    153         if ( l.denominator == r.denominator ) {                         // special case
    154                 Rational(RationalImpl) t = { l.numerator + r.numerator, l.denominator };
    155                 return t;
    156         } else {
    157                 Rational(RationalImpl) t = { l.numerator * r.denominator + l.denominator * r.numerator, l.denominator * r.denominator };
    158                 return t;
    159         } // if
    160 } // ?+?
    161 
    162 forall( otype RationalImpl | arithmetic( RationalImpl ) )
    163 Rational(RationalImpl) ?-?( Rational(RationalImpl) l, Rational(RationalImpl) r ) {
    164         if ( l.denominator == r.denominator ) {                         // special case
    165                 Rational(RationalImpl) t = { l.numerator - r.numerator, l.denominator };
    166                 return t;
    167         } else {
    168                 Rational(RationalImpl) t = { l.numerator * r.denominator - l.denominator * r.numerator, l.denominator * r.denominator };
    169                 return t;
    170         } // if
    171 } // ?-?
    172 
    173 forall( otype RationalImpl | arithmetic( RationalImpl ) )
    174 Rational(RationalImpl) ?*?( Rational(RationalImpl) l, Rational(RationalImpl) r ) {
    175         Rational(RationalImpl) t = { l.numerator * r.numerator, l.denominator * r.denominator };
    176         return t;
    177 } // ?*?
    178 
    179 forall( otype RationalImpl | arithmetic( RationalImpl ) )
    180 Rational(RationalImpl) ?/?( Rational(RationalImpl) l, Rational(RationalImpl) r ) {
    181         if ( r.numerator < (RationalImpl){0} ) {
    182                 r.numerator = -r.numerator;
    183                 r.denominator = -r.denominator;
    184         } // if
    185         Rational(RationalImpl) t = { l.numerator * r.denominator, l.denominator * r.numerator };
    186         return t;
    187 } // ?/?
    188 
     20forall( otype RationalImpl | arithmetic( RationalImpl ) ) {
     21        // helper routines
     22
     23        // Calculate greatest common denominator of two numbers, the first of which may be negative. Used to reduce
     24        // rationals.  alternative: https://en.wikipedia.org/wiki/Binary_GCD_algorithm
     25        static RationalImpl gcd( RationalImpl a, RationalImpl b ) {
     26                for ( ;; ) {                                                                    // Euclid's algorithm
     27                        RationalImpl r = a % b;
     28                  if ( r == (RationalImpl){0} ) break;
     29                        a = b;
     30                        b = r;
     31                } // for
     32                return b;
     33        } // gcd
     34
     35        static RationalImpl simplify( RationalImpl & n, RationalImpl & d ) {
     36                if ( d == (RationalImpl){0} ) {
     37                        serr | "Invalid rational number construction: denominator cannot be equal to 0." | endl;
     38                        exit( EXIT_FAILURE );
     39                } // exit
     40                if ( d < (RationalImpl){0} ) { d = -d; n = -n; } // move sign to numerator
     41                return gcd( abs( n ), d );                                              // simplify
     42        } // Rationalnumber::simplify
     43
     44        // constructors
     45
     46        void ?{}( Rational(RationalImpl) & r ) {
     47                r{ (RationalImpl){0}, (RationalImpl){1} };
     48        } // rational
     49
     50        void ?{}( Rational(RationalImpl) & r, RationalImpl n ) {
     51                r{ n, (RationalImpl){1} };
     52        } // rational
     53
     54        void ?{}( Rational(RationalImpl) & r, RationalImpl n, RationalImpl d ) {
     55                RationalImpl t = simplify( n, d );                              // simplify
     56                r.numerator = n / t;
     57                r.denominator = d / t;
     58        } // rational
     59
     60
     61        // getter for numerator/denominator
     62
     63        RationalImpl numerator( Rational(RationalImpl) r ) {
     64                return r.numerator;
     65        } // numerator
     66
     67        RationalImpl denominator( Rational(RationalImpl) r ) {
     68                return r.denominator;
     69        } // denominator
     70
     71        [ RationalImpl, RationalImpl ] ?=?( & [ RationalImpl, RationalImpl ] dest, Rational(RationalImpl) src ) {
     72                return dest = src.[ numerator, denominator ];
     73        } // ?=?
     74
     75        // setter for numerator/denominator
     76
     77        RationalImpl numerator( Rational(RationalImpl) r, RationalImpl n ) {
     78                RationalImpl prev = r.numerator;
     79                RationalImpl t = gcd( abs( n ), r.denominator ); // simplify
     80                r.numerator = n / t;
     81                r.denominator = r.denominator / t;
     82                return prev;
     83        } // numerator
     84
     85        RationalImpl denominator( Rational(RationalImpl) r, RationalImpl d ) {
     86                RationalImpl prev = r.denominator;
     87                RationalImpl t = simplify( r.numerator, d );    // simplify
     88                r.numerator = r.numerator / t;
     89                r.denominator = d / t;
     90                return prev;
     91        } // denominator
     92
     93        // comparison
     94
     95        int ?==?( Rational(RationalImpl) l, Rational(RationalImpl) r ) {
     96                return l.numerator * r.denominator == l.denominator * r.numerator;
     97        } // ?==?
     98
     99        int ?!=?( Rational(RationalImpl) l, Rational(RationalImpl) r ) {
     100                return ! ( l == r );
     101        } // ?!=?
     102
     103        int ?<?( Rational(RationalImpl) l, Rational(RationalImpl) r ) {
     104                return l.numerator * r.denominator < l.denominator * r.numerator;
     105        } // ?<?
     106
     107        int ?<=?( Rational(RationalImpl) l, Rational(RationalImpl) r ) {
     108                return l.numerator * r.denominator <= l.denominator * r.numerator;
     109        } // ?<=?
     110
     111        int ?>?( Rational(RationalImpl) l, Rational(RationalImpl) r ) {
     112                return ! ( l <= r );
     113        } // ?>?
     114
     115        int ?>=?( Rational(RationalImpl) l, Rational(RationalImpl) r ) {
     116                return ! ( l < r );
     117        } // ?>=?
     118
     119        // arithmetic
     120
     121        Rational(RationalImpl) +?( Rational(RationalImpl) r ) {
     122                Rational(RationalImpl) t = { r.numerator, r.denominator };
     123                return t;
     124        } // +?
     125
     126        Rational(RationalImpl) -?( Rational(RationalImpl) r ) {
     127                Rational(RationalImpl) t = { -r.numerator, r.denominator };
     128                return t;
     129        } // -?
     130
     131        Rational(RationalImpl) ?+?( Rational(RationalImpl) l, Rational(RationalImpl) r ) {
     132                if ( l.denominator == r.denominator ) {                 // special case
     133                        Rational(RationalImpl) t = { l.numerator + r.numerator, l.denominator };
     134                        return t;
     135                } else {
     136                        Rational(RationalImpl) t = { l.numerator * r.denominator + l.denominator * r.numerator, l.denominator * r.denominator };
     137                        return t;
     138                } // if
     139        } // ?+?
     140
     141        Rational(RationalImpl) ?-?( Rational(RationalImpl) l, Rational(RationalImpl) r ) {
     142                if ( l.denominator == r.denominator ) {                 // special case
     143                        Rational(RationalImpl) t = { l.numerator - r.numerator, l.denominator };
     144                        return t;
     145                } else {
     146                        Rational(RationalImpl) t = { l.numerator * r.denominator - l.denominator * r.numerator, l.denominator * r.denominator };
     147                        return t;
     148                } // if
     149        } // ?-?
     150
     151        Rational(RationalImpl) ?*?( Rational(RationalImpl) l, Rational(RationalImpl) r ) {
     152                Rational(RationalImpl) t = { l.numerator * r.numerator, l.denominator * r.denominator };
     153                return t;
     154        } // ?*?
     155
     156        Rational(RationalImpl) ?/?( Rational(RationalImpl) l, Rational(RationalImpl) r ) {
     157                if ( r.numerator < (RationalImpl){0} ) {
     158                        r.numerator = -r.numerator;
     159                        r.denominator = -r.denominator;
     160                } // if
     161                Rational(RationalImpl) t = { l.numerator * r.denominator, l.denominator * r.numerator };
     162                return t;
     163        } // ?/?
     164
     165        // I/O
     166
     167        forall( dtype istype | istream( istype ) | { istype & ?|?( istype &, RationalImpl & ); } )
     168        istype & ?|?( istype & is, Rational(RationalImpl) & r ) {
     169                RationalImpl t;
     170                is | r.numerator | r.denominator;
     171                t = simplify( r.numerator, r.denominator );
     172                r.numerator /= t;
     173                r.denominator /= t;
     174                return is;
     175        } // ?|?
     176
     177        forall( dtype ostype | ostream( ostype ) | { ostype & ?|?( ostype &, RationalImpl ); } )
     178        ostype & ?|?( ostype & os, Rational(RationalImpl ) r ) {
     179                return os | r.numerator | '/' | r.denominator;
     180        } // ?|?
     181} // distribution
    189182
    190183// conversion
     
    195188} // widen
    196189
    197 // http://www.ics.uci.edu/~eppstein/numth/frap.c
    198190forall( otype RationalImpl | arithmetic( RationalImpl ) | { double convert( RationalImpl ); RationalImpl convert( double ); } )
    199191Rational(RationalImpl) narrow( double f, RationalImpl md ) {
     192        // http://www.ics.uci.edu/~eppstein/numth/frap.c
    200193        if ( md <= (RationalImpl){1} ) {                                        // maximum fractional digits too small?
    201194                return (Rational(RationalImpl)){ convert( f ), (RationalImpl){1}}; // truncate fraction
     
    224217} // narrow
    225218
    226 
    227 // I/O
    228 
    229 forall( otype RationalImpl | arithmetic( RationalImpl ) )
    230 forall( dtype istype | istream( istype ) | { istype & ?|?( istype &, RationalImpl & ); } )
    231 istype & ?|?( istype & is, Rational(RationalImpl) & r ) {
    232         RationalImpl t;
    233         is | r.numerator | r.denominator;
    234         t = simplify( r.numerator, r.denominator );
    235         r.numerator /= t;
    236         r.denominator /= t;
    237         return is;
    238 } // ?|?
    239 
    240 forall( otype RationalImpl | arithmetic( RationalImpl ) )
    241 forall( dtype ostype | ostream( ostype ) | { ostype & ?|?( ostype &, RationalImpl ); } )
    242 ostype & ?|?( ostype & os, Rational(RationalImpl ) r ) {
    243         return os | r.numerator | '/' | r.denominator;
    244 } // ?|?
    245 
    246219// Local Variables: //
    247220// tab-width: 4 //
  • src/libcfa/stdlib

    r0a75b77 rb429026  
    1010// Created On       : Thu Jan 28 17:12:35 2016
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Wed May 16 07:53:10 2018
    13 // Update Count     : 300
     12// Last Modified On : Sat Jun  2 08:46:35 2018
     13// Update Count     : 306
    1414//
    1515
    1616#pragma once
    1717
    18 #include <stdlib.h>                                                                             // strto*, *abs
     18#include <stdlib.h>                                                                             // allocation, strto*, *abs
     19extern "C" {
     20        void * memalign( size_t align, size_t size );
     21        void * aligned_alloc( size_t align, size_t size );
     22        void * memset( void * dest, int c, size_t size );
     23} // extern "C"
    1924
    2025//---------------------------------------
     
    2732//---------------------------------------
    2833
    29 // C dynamic allocation
    3034static inline forall( dtype T | sized(T) ) {
     35        // C dynamic allocation
     36
    3137        T * malloc( void ) {
    3238                // printf( "* malloc\n" );
     
    5157        } // realloc
    5258
    53         extern "C" { void * memalign( size_t align, size_t size ); } // use default C routine for void *
    5459        T * memalign( size_t align ) {
    5560                //printf( "X4\n" );
     
    5762        } // memalign
    5863
    59         extern "C" { void * aligned_alloc( size_t align, size_t size ); } // use default C routine for void *
    6064        T * aligned_alloc( size_t align ) {
    6165                //printf( "X5\n" );
     
    7074
    7175        // Cforall dynamic allocation
    72         extern "C" { void * memset( void * dest, int c, size_t size ); } // use default C routine for void *
    7376
    7477        T * alloc( void ) {
     
    103106forall( dtype T | sized(T) ) T * alloc( T ptr[], size_t dim, char fill );
    104107
    105 static inline forall( dtype T | sized(T) ) T * align_alloc( size_t align ) {
    106         //printf( "X13\n" );
    107         return (T *)memalign( align, sizeof(T) );
    108 } // align_alloc
    109 static inline forall( dtype T | sized(T) ) T * align_alloc( size_t align, char fill ) {
    110         //printf( "X14\n" );
    111     T * ptr = (T *)memalign( align, sizeof(T) );
    112     return (T *)memset( ptr, (int)fill, sizeof(T) );
    113 } // align_alloc
    114 
    115 static inline forall( dtype T | sized(T) ) T * align_alloc( size_t align, size_t dim ) {
    116         //printf( "X15\n" );
    117         return (T *)memalign( align, dim * sizeof(T) );
    118 } // align_alloc
    119 static inline forall( dtype T | sized(T) ) T * align_alloc( size_t align, size_t dim, char fill ) {
    120         //printf( "X16\n" );
    121     T * ptr = (T *)memalign( align, dim * sizeof(T) );
    122     return (T *)memset( ptr, (int)fill, dim * sizeof(T) );
    123 } // align_alloc
    124 
    125 
    126 // data, non-array types
    127 static inline forall( dtype T | sized(T) ) T * memset( T * dest, char c ) {
    128         //printf( "X17\n" );
    129         return (T *)memset( dest, c, sizeof(T) );
    130 } // memset
    131 extern "C" { void * memcpy( void * dest, const void * src, size_t size ); } // use default C routine for void *
    132 static inline forall( dtype T | sized(T) ) T * memcpy( T * dest, const T * src ) {
    133         //printf( "X18\n" );
    134         return (T *)memcpy( dest, src, sizeof(T) );
    135 } // memcpy
    136 
    137 // data, array types
    138 static inline forall( dtype T | sized(T) ) T * memset( T dest[], size_t dim, char c ) {
    139         //printf( "X19\n" );
    140         return (T *)(void *)memset( dest, c, dim * sizeof(T) ); // C memset
    141 } // memset
    142 static inline forall( dtype T | sized(T) ) T * memcpy( T dest[], const T src[], size_t dim ) {
    143         //printf( "X20\n" );
    144         return (T *)(void *)memcpy( dest, src, dim * sizeof(T) ); // C memcpy
    145 } // memcpy
     108
     109static inline forall( dtype T | sized(T) ) {
     110        T * align_alloc( size_t align ) {
     111                //printf( "X13\n" );
     112                return (T *)memalign( align, sizeof(T) );
     113        } // align_alloc
     114
     115        T * align_alloc( size_t align, char fill ) {
     116                //printf( "X14\n" );
     117                T * ptr = (T *)memalign( align, sizeof(T) );
     118                return (T *)memset( ptr, (int)fill, sizeof(T) );
     119        } // align_alloc
     120
     121        T * align_alloc( size_t align, size_t dim ) {
     122                //printf( "X15\n" );
     123                return (T *)memalign( align, dim * sizeof(T) );
     124        } // align_alloc
     125
     126        T * align_alloc( size_t align, size_t dim, char fill ) {
     127                //printf( "X16\n" );
     128                T * ptr = (T *)memalign( align, dim * sizeof(T) );
     129                return (T *)memset( ptr, (int)fill, dim * sizeof(T) );
     130        } // align_alloc
     131} // distribution
     132
     133
     134static inline forall( dtype T | sized(T) ) {
     135        // data, non-array types
     136
     137        T * memset( T * dest, char c ) {
     138                //printf( "X17\n" );
     139                return (T *)memset( dest, c, sizeof(T) );
     140        } // memset
     141
     142        extern "C" { void * memcpy( void * dest, const void * src, size_t size ); } // use default C routine for void *
     143
     144        T * memcpy( T * dest, const T * src ) {
     145                //printf( "X18\n" );
     146                return (T *)memcpy( dest, src, sizeof(T) );
     147        } // memcpy
     148} // distribution
     149
     150static inline forall( dtype T | sized(T) ) {
     151        // data, array types
     152
     153        T * memset( T dest[], size_t dim, char c ) {
     154                //printf( "X19\n" );
     155                return (T *)(void *)memset( dest, c, dim * sizeof(T) ); // C memset
     156        } // memset
     157
     158        T * memcpy( T dest[], const T src[], size_t dim ) {
     159                //printf( "X20\n" );
     160                return (T *)(void *)memcpy( dest, src, dim * sizeof(T) ); // C memcpy
     161        } // memcpy
     162} // distribution
    146163
    147164// allocation/deallocation and constructor/destructor, non-array types
     
    189206//---------------------------------------
    190207
    191 forall( otype E | { int ?<?( E, E ); } )
    192 E * bsearch( E key, const E * vals, size_t dim );
    193 
    194 forall( otype E | { int ?<?( E, E ); } )
    195 size_t bsearch( E key, const E * vals, size_t dim );
    196 
    197 forall( otype K, otype E | { int ?<?( K, K ); K getKey( const E & ); } )
    198 E * bsearch( K key, const E * vals, size_t dim );
    199 
    200 forall( otype K, otype E | { int ?<?( K, K ); K getKey( const E & ); } )
    201 size_t bsearch( K key, const E * vals, size_t dim );
    202 
    203 
    204 forall( otype E | { int ?<?( E, E ); } )
    205 E * bsearchl( E key, const E * vals, size_t dim );
    206 
    207 forall( otype E | { int ?<?( E, E ); } )
    208 size_t bsearchl( E key, const E * vals, size_t dim );
    209 
    210 forall( otype K, otype E | { int ?<?( K, K ); K getKey( const E & ); } )
    211 E * bsearchl( K key, const E * vals, size_t dim );
    212 
    213 forall( otype K, otype E | { int ?<?( K, K ); K getKey( const E & ); } )
    214 size_t bsearchl( K key, const E * vals, size_t dim );
    215 
    216 
    217 forall( otype E | { int ?<?( E, E ); } )
    218 E * bsearchu( E key, const E * vals, size_t dim );
    219 
    220 forall( otype E | { int ?<?( E, E ); } )
    221 size_t bsearchu( E key, const E * vals, size_t dim );
    222 
    223 forall( otype K, otype E | { int ?<?( K, K ); K getKey( const E & ); } )
    224 E * bsearchu( K key, const E * vals, size_t dim );
    225 
    226 forall( otype K, otype E | { int ?<?( K, K ); K getKey( const E & ); } )
    227 size_t bsearchu( K key, const E * vals, size_t dim );
    228 
    229 
    230 forall( otype E | { int ?<?( E, E ); } )
    231 void qsort( E * vals, size_t dim );
     208forall( otype E | { int ?<?( E, E ); } ) {
     209        E * bsearch( E key, const E * vals, size_t dim );
     210        size_t bsearch( E key, const E * vals, size_t dim );
     211        E * bsearchl( E key, const E * vals, size_t dim );
     212        size_t bsearchl( E key, const E * vals, size_t dim );
     213        E * bsearchu( E key, const E * vals, size_t dim );
     214        size_t bsearchu( E key, const E * vals, size_t dim );
     215
     216        void qsort( E * vals, size_t dim );
     217} // distribution
     218
     219forall( otype K, otype E | { int ?<?( K, K ); K getKey( const E & ); } ) {
     220        E * bsearch( K key, const E * vals, size_t dim );
     221        size_t bsearch( K key, const E * vals, size_t dim );
     222        E * bsearchl( K key, const E * vals, size_t dim );
     223        size_t bsearchl( K key, const E * vals, size_t dim );
     224        E * bsearchu( K key, const E * vals, size_t dim );
     225        size_t bsearchu( K key, const E * vals, size_t dim );
     226} // distribution
    232227
    233228//---------------------------------------
  • src/libcfa/stdlib.c

    r0a75b77 rb429026  
    1010// Created On       : Thu Jan 28 17:10:29 2016
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Wed Jan  3 08:29:29 2018
    13 // Update Count     : 444
     12// Last Modified On : Sat Jun  2 06:15:05 2018
     13// Update Count     : 448
    1414//
    1515
     
    130130//---------------------------------------
    131131
    132 forall( otype E | { int ?<?( E, E ); } )
    133 E * bsearch( E key, const E * vals, size_t dim ) {
    134         int cmp( const void * t1, const void * t2 ) {
    135                 return *(E *)t1 < *(E *)t2 ? -1 : *(E *)t2 < *(E *)t1 ? 1 : 0;
    136         } // cmp
    137         return (E *)bsearch( &key, vals, dim, sizeof(E), cmp );
    138 } // bsearch
    139 
    140 forall( otype E | { int ?<?( E, E ); } )
    141 size_t bsearch( E key, const E * vals, size_t dim ) {
    142         E * result = bsearch( key, vals, dim );
    143         return result ? result - vals : dim;                            // pointer subtraction includes sizeof(E)
    144 } // bsearch
    145 
    146 forall( otype K, otype E | { int ?<?( K, K ); K getKey( const E & ); } )
    147 E * bsearch( K key, const E * vals, size_t dim ) {
    148         int cmp( const void * t1, const void * t2 ) {
    149                 return *(K *)t1 < getKey( *(E *)t2 ) ? -1 : getKey( *(E *)t2 ) < *(K *)t1 ? 1 : 0;
    150         } // cmp
    151         return (E *)bsearch( &key, vals, dim, sizeof(E), cmp );
    152 } // bsearch
    153 
    154 forall( otype K, otype E | { int ?<?( K, K ); K getKey( const E & ); } )
    155 size_t bsearch( K key, const E * vals, size_t dim ) {
    156         E * result = bsearch( key, vals, dim );
    157         return result ? result - vals : dim;                            // pointer subtraction includes sizeof(E)
    158 } // bsearch
    159 
    160 
    161 forall( otype E | { int ?<?( E, E ); } )
    162 size_t bsearchl( E key, const E * vals, size_t dim ) {
    163         size_t l = 0, m, h = dim;
    164         while ( l < h ) {
    165                 m = (l + h) / 2;
    166                 if ( (E &)(vals[m]) < key ) {                                   // cast away const
    167                         l = m + 1;
    168                 } else {
    169                         h = m;
    170                 } // if
    171         } // while
    172         return l;
    173 } // bsearchl
    174 
    175 forall( otype E | { int ?<?( E, E ); } )
    176 E * bsearchl( E key, const E * vals, size_t dim ) {
    177         size_t posn = bsearchl( key, vals, dim );
    178         return (E *)(&vals[posn]);                                                      // cast away const
    179 } // bsearchl
    180 
    181 forall( otype K, otype E | { int ?<?( K, K ); K getKey( const E & ); } )
    182 size_t bsearchl( K key, const E * vals, size_t dim ) {
    183         size_t l = 0, m, h = dim;
    184         while ( l < h ) {
    185                 m = (l + h) / 2;
    186                 if ( getKey( vals[m] ) < key ) {
    187                         l = m + 1;
    188                 } else {
    189                         h = m;
    190                 } // if
    191         } // while
    192         return l;
    193 } // bsearchl
    194 
    195 forall( otype K, otype E | { int ?<?( K, K ); K getKey( const E & ); } )
    196 E * bsearchl( K key, const E * vals, size_t dim ) {
    197         size_t posn = bsearchl( key, vals, dim );
    198         return (E *)(&vals[posn]);                                                      // cast away const
    199 } // bsearchl
    200 
    201 
    202 forall( otype E | { int ?<?( E, E ); } )
    203 size_t bsearchu( E key, const E * vals, size_t dim ) {
    204         size_t l = 0, m, h = dim;
    205         while ( l < h ) {
    206                 m = (l + h) / 2;
    207                 if ( ! ( key < (E &)(vals[m]) ) ) {                             // cast away const
    208                         l = m + 1;
    209                 } else {
    210                         h = m;
    211                 } // if
    212         } // while
    213         return l;
    214 } // bsearchu
    215 
    216 forall( otype E | { int ?<?( E, E ); } )
    217 E * bsearchu( E key, const E * vals, size_t dim ) {
    218         size_t posn = bsearchu( key, vals, dim );
    219         return (E *)(&vals[posn]);
    220 } // bsearchu
    221 
    222 forall( otype K, otype E | { int ?<?( K, K ); K getKey( const E & ); } )
    223 size_t bsearchu( K key, const E * vals, size_t dim ) {
    224         size_t l = 0, m, h = dim;
    225         while ( l < h ) {
    226                 m = (l + h) / 2;
    227                 if ( ! ( key < getKey( vals[m] ) ) ) {
    228                         l = m + 1;
    229                 } else {
    230                         h = m;
    231                 } // if
    232         } // while
    233         return l;
    234 } // bsearchu
    235 
    236 forall( otype K, otype E | { int ?<?( K, K ); K getKey( const E & ); } )
    237 E * bsearchu( K key, const E * vals, size_t dim ) {
    238         size_t posn = bsearchu( key, vals, dim );
    239         return (E *)(&vals[posn]);
    240 } // bsearchu
    241 
    242 
    243 forall( otype E | { int ?<?( E, E ); } )
    244 void qsort( E * vals, size_t dim ) {
    245         int cmp( const void * t1, const void * t2 ) {
    246                 return *(E *)t1 < *(E *)t2 ? -1 : *(E *)t2 < *(E *)t1 ? 1 : 0;
    247         } // cmp
    248         qsort( vals, dim, sizeof(E), cmp );
    249 } // qsort
     132forall( otype E | { int ?<?( E, E ); } ) {
     133        E * bsearch( E key, const E * vals, size_t dim ) {
     134                int cmp( const void * t1, const void * t2 ) {
     135                        return *(E *)t1 < *(E *)t2 ? -1 : *(E *)t2 < *(E *)t1 ? 1 : 0;
     136                } // cmp
     137                return (E *)bsearch( &key, vals, dim, sizeof(E), cmp );
     138        } // bsearch
     139
     140        size_t bsearch( E key, const E * vals, size_t dim ) {
     141                E * result = bsearch( key, vals, dim );
     142                return result ? result - vals : dim;                    // pointer subtraction includes sizeof(E)
     143        } // bsearch
     144
     145        size_t bsearchl( E key, const E * vals, size_t dim ) {
     146                size_t l = 0, m, h = dim;
     147                while ( l < h ) {
     148                        m = (l + h) / 2;
     149                        if ( (E &)(vals[m]) < key ) {                           // cast away const
     150                                l = m + 1;
     151                        } else {
     152                                h = m;
     153                        } // if
     154                } // while
     155                return l;
     156        } // bsearchl
     157
     158        E * bsearchl( E key, const E * vals, size_t dim ) {
     159                size_t posn = bsearchl( key, vals, dim );
     160                return (E *)(&vals[posn]);                                              // cast away const
     161        } // bsearchl
     162
     163        size_t bsearchu( E key, const E * vals, size_t dim ) {
     164                size_t l = 0, m, h = dim;
     165                while ( l < h ) {
     166                        m = (l + h) / 2;
     167                        if ( ! ( key < (E &)(vals[m]) ) ) {                     // cast away const
     168                                l = m + 1;
     169                        } else {
     170                                h = m;
     171                        } // if
     172                } // while
     173                return l;
     174        } // bsearchu
     175
     176        E * bsearchu( E key, const E * vals, size_t dim ) {
     177                size_t posn = bsearchu( key, vals, dim );
     178                return (E *)(&vals[posn]);
     179        } // bsearchu
     180
     181
     182        void qsort( E * vals, size_t dim ) {
     183                int cmp( const void * t1, const void * t2 ) {
     184                        return *(E *)t1 < *(E *)t2 ? -1 : *(E *)t2 < *(E *)t1 ? 1 : 0;
     185                } // cmp
     186                qsort( vals, dim, sizeof(E), cmp );
     187        } // qsort
     188} // distribution
     189
     190
     191forall( otype K, otype E | { int ?<?( K, K ); K getKey( const E & ); } ) {
     192        E * bsearch( K key, const E * vals, size_t dim ) {
     193                int cmp( const void * t1, const void * t2 ) {
     194                        return *(K *)t1 < getKey( *(E *)t2 ) ? -1 : getKey( *(E *)t2 ) < *(K *)t1 ? 1 : 0;
     195                } // cmp
     196                return (E *)bsearch( &key, vals, dim, sizeof(E), cmp );
     197        } // bsearch
     198
     199        size_t bsearch( K key, const E * vals, size_t dim ) {
     200                E * result = bsearch( key, vals, dim );
     201                return result ? result - vals : dim;                    // pointer subtraction includes sizeof(E)
     202        } // bsearch
     203
     204        size_t bsearchl( K key, const E * vals, size_t dim ) {
     205                size_t l = 0, m, h = dim;
     206                while ( l < h ) {
     207                        m = (l + h) / 2;
     208                        if ( getKey( vals[m] ) < key ) {
     209                                l = m + 1;
     210                        } else {
     211                                h = m;
     212                        } // if
     213                } // while
     214                return l;
     215        } // bsearchl
     216
     217        E * bsearchl( K key, const E * vals, size_t dim ) {
     218                size_t posn = bsearchl( key, vals, dim );
     219                return (E *)(&vals[posn]);                                              // cast away const
     220        } // bsearchl
     221
     222        size_t bsearchu( K key, const E * vals, size_t dim ) {
     223                size_t l = 0, m, h = dim;
     224                while ( l < h ) {
     225                        m = (l + h) / 2;
     226                        if ( ! ( key < getKey( vals[m] ) ) ) {
     227                                l = m + 1;
     228                        } else {
     229                                h = m;
     230                        } // if
     231                } // while
     232                return l;
     233        } // bsearchu
     234
     235        E * bsearchu( K key, const E * vals, size_t dim ) {
     236                size_t posn = bsearchu( key, vals, dim );
     237                return (E *)(&vals[posn]);
     238        } // bsearchu
     239} // distribution
    250240
    251241//---------------------------------------
Note: See TracChangeset for help on using the changeset viewer.