Changeset d36c117 for src/libcfa


Ignore:
Timestamp:
May 15, 2017, 12:04:49 PM (9 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, resolv-new, with_gc
Children:
9c951e3
Parents:
ce8c12f (diff), fae2cf8 (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' into references

Location:
src/libcfa
Files:
1 added
7 edited

Legend:

Unmodified
Added
Removed
  • src/libcfa/Makefile.am

    rce8c12f rd36c117  
    1111## Created On       : Sun May 31 08:54:01 2015
    1212## Last Modified By : Peter A. Buhr
    13 ## Last Modified On : Sat Mar 25 18:00:10 2017
    14 ## Update Count     : 212
     13## Last Modified On : Sun May 14 21:04:21 2017
     14## Update Count     : 214
    1515###############################################################################
    1616
     
    7676
    7777cfa_includedir = $(CFA_INCDIR)
    78 nobase_cfa_include_HEADERS = ${headers} ${stdhdr} concurrency/invoke.h
     78nobase_cfa_include_HEADERS = ${headers} ${stdhdr} gmp concurrency/invoke.h
    7979
    8080CLEANFILES = libcfa-prelude.c
  • src/libcfa/Makefile.in

    rce8c12f rd36c117  
    183183        containers/vector concurrency/coroutine concurrency/thread \
    184184        concurrency/kernel concurrency/monitor ${shell echo stdhdr/*} \
    185         concurrency/invoke.h
     185        gmp concurrency/invoke.h
    186186HEADERS = $(nobase_cfa_include_HEADERS)
    187187ETAGS = etags
     
    324324stdhdr = ${shell echo stdhdr/*}
    325325cfa_includedir = $(CFA_INCDIR)
    326 nobase_cfa_include_HEADERS = ${headers} ${stdhdr} concurrency/invoke.h
     326nobase_cfa_include_HEADERS = ${headers} ${stdhdr} gmp concurrency/invoke.h
    327327CLEANFILES = libcfa-prelude.c
    328328all: all-am
  • src/libcfa/iostream.c

    rce8c12f rd36c117  
    1010// Created On       : Wed May 27 17:56:53 2015
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Thu Mar 23 08:20:40 2017
    13 // Update Count     : 367
     12// Last Modified On : Mon May  8 18:24:23 2017
     13// Update Count     : 369
    1414//
    1515
     
    160160                [(unsigned char)'¡'] : Open, [(unsigned char)'¿'] : Open, [(unsigned char)'«'] : Open,
    161161                // closing delimiters, no space before
    162                 [','] : Close, ['.'] : Close, [':'] : Close, [';'] : Close, ['!'] : Close, ['?'] : Close,
     162                [','] : Close, ['.'] : Close, [';'] : Close, ['!'] : Close, ['?'] : Close,
    163163                ['%'] : Close, [(unsigned char)'¢'] : Close, [(unsigned char)'»'] : Close,
    164164                [')'] : Close, [']'] : Close, ['}'] : Close,
    165165                // opening-closing delimiters, no space before or after
    166                 ['\''] : OpenClose, ['`'] : OpenClose, ['"'] : OpenClose,
     166                ['\''] : OpenClose, ['`'] : OpenClose, ['"'] : OpenClose, [':'] : OpenClose,
    167167                [' '] : OpenClose, ['\f'] : OpenClose, ['\n'] : OpenClose, ['\r'] : OpenClose, ['\t'] : OpenClose, ['\v'] : OpenClose, // isspace
    168168        }; // mask
  • src/libcfa/rational

    rce8c12f rd36c117  
    1212// Created On       : Wed Apr  6 17:56:25 2016
    1313// Last Modified By : Peter A. Buhr
    14 // Last Modified On : Mon May  1 08:25:06 2017
    15 // Update Count     : 33
     14// Last Modified On : Sun May 14 16:49:13 2017
     15// Update Count     : 78
    1616//
    1717
     
    2121#include "iostream"
    2222
     23trait scalar( otype T ) {
     24};
     25
     26trait arithmetic( otype T | scalar( T ) ) {
     27        int !?( T );
     28        int ?==?( T, T );
     29        int ?!=?( T, T );
     30        int ?<?( T, T );
     31        int ?<=?( T, T );
     32        int ?>?( T, T );
     33        int ?>=?( T, T );
     34        void ?{}( T *, zero_t );
     35        void ?{}( T *, one_t );
     36        T +?( T );
     37        T -?( T );
     38        T ?+?( T, T );
     39        T ?-?( T, T );
     40        T ?*?( T, T );
     41        T ?/?( T, T );
     42        T ?%?( T, T );
     43        T ?/=?( T *, T );
     44        T abs( T );
     45};
     46
    2347// implementation
    24 typedef long int RationalImpl;
     48
     49forall ( otype RationalImpl | arithmetic( RationalImpl ) )
    2550struct Rational {
    26         RationalImpl numerator, denominator;                                    // invariant: denominator > 0
     51        RationalImpl numerator, denominator;                            // invariant: denominator > 0
    2752}; // Rational
    2853
    29 // constants
    30 extern struct Rational 0;
    31 extern struct Rational 1;
     54// constructors
    3255
    33 // constructors
    34 void ?{}( Rational * r );
    35 void ?{}( Rational * r, RationalImpl n );
    36 void ?{}( Rational * r, RationalImpl n, RationalImpl d );
     56forall ( otype RationalImpl | arithmetic( RationalImpl ) )
     57void ?{}( Rational(RationalImpl) * r );
     58
     59forall ( otype RationalImpl | arithmetic( RationalImpl ) )
     60void ?{}( Rational(RationalImpl) * r, RationalImpl n );
     61
     62forall ( otype RationalImpl | arithmetic( RationalImpl ) )
     63void ?{}( Rational(RationalImpl) * r, RationalImpl n, RationalImpl d );
     64
     65forall ( otype RationalImpl | arithmetic( RationalImpl ) )
     66void ?{}( Rational(RationalImpl) * r, zero_t );
     67
     68forall ( otype RationalImpl | arithmetic( RationalImpl ) )
     69void ?{}( Rational(RationalImpl) * r, one_t );
    3770
    3871// getter for numerator/denominator
    39 RationalImpl numerator( Rational r );
    40 RationalImpl denominator( Rational r );
    41 [ RationalImpl, RationalImpl ] ?=?( * [ RationalImpl, RationalImpl ] dest, Rational src );
     72
     73forall ( otype RationalImpl | arithmetic( RationalImpl ) )
     74RationalImpl numerator( Rational(RationalImpl) r );
     75
     76forall ( otype RationalImpl | arithmetic( RationalImpl ) )
     77RationalImpl denominator( Rational(RationalImpl) r );
     78forall ( otype RationalImpl | arithmetic( RationalImpl ) )
     79[ RationalImpl, RationalImpl ] ?=?( * [ RationalImpl, RationalImpl ] dest, Rational(RationalImpl) src );
     80
    4281// setter for numerator/denominator
    43 RationalImpl numerator( Rational r, RationalImpl n );
    44 RationalImpl denominator( Rational r, RationalImpl d );
     82
     83forall ( otype RationalImpl | arithmetic( RationalImpl ) )
     84RationalImpl numerator( Rational(RationalImpl) r, RationalImpl n );
     85
     86forall ( otype RationalImpl | arithmetic( RationalImpl ) )
     87RationalImpl denominator( Rational(RationalImpl) r, RationalImpl d );
    4588
    4689// comparison
    47 int ?==?( Rational l, Rational r );
    48 int ?!=?( Rational l, Rational r );
    49 int ?<?( Rational l, Rational r );
    50 int ?<=?( Rational l, Rational r );
    51 int ?>?( Rational l, Rational r );
    52 int ?>=?( Rational l, Rational r );
     90
     91forall ( otype RationalImpl | arithmetic( RationalImpl ) )
     92int ?==?( Rational(RationalImpl) l, Rational(RationalImpl) r );
     93
     94forall ( otype RationalImpl | arithmetic( RationalImpl ) )
     95int ?!=?( Rational(RationalImpl) l, Rational(RationalImpl) r );
     96
     97forall ( otype RationalImpl | arithmetic( RationalImpl ) )
     98int ?<?( Rational(RationalImpl) l, Rational(RationalImpl) r );
     99
     100forall ( otype RationalImpl | arithmetic( RationalImpl ) )
     101int ?<=?( Rational(RationalImpl) l, Rational(RationalImpl) r );
     102
     103forall ( otype RationalImpl | arithmetic( RationalImpl ) )
     104int ?>?( Rational(RationalImpl) l, Rational(RationalImpl) r );
     105
     106forall ( otype RationalImpl | arithmetic( RationalImpl ) )
     107int ?>=?( Rational(RationalImpl) l, Rational(RationalImpl) r );
    53108
    54109// arithmetic
    55 Rational -?( Rational r );
    56 Rational ?+?( Rational l, Rational r );
    57 Rational ?-?( Rational l, Rational r );
    58 Rational ?*?( Rational l, Rational r );
    59 Rational ?/?( Rational l, Rational r );
     110
     111forall ( otype RationalImpl | arithmetic( RationalImpl ) )
     112Rational(RationalImpl) +?( Rational(RationalImpl) r );
     113
     114forall ( otype RationalImpl | arithmetic( RationalImpl ) )
     115Rational(RationalImpl) -?( Rational(RationalImpl) r );
     116
     117forall ( otype RationalImpl | arithmetic( RationalImpl ) )
     118Rational(RationalImpl) ?+?( Rational(RationalImpl) l, Rational(RationalImpl) r );
     119
     120forall ( otype RationalImpl | arithmetic( RationalImpl ) )
     121Rational(RationalImpl) ?-?( Rational(RationalImpl) l, Rational(RationalImpl) r );
     122
     123forall ( otype RationalImpl | arithmetic( RationalImpl ) )
     124Rational(RationalImpl) ?*?( Rational(RationalImpl) l, Rational(RationalImpl) r );
     125
     126forall ( otype RationalImpl | arithmetic( RationalImpl ) )
     127Rational(RationalImpl) ?/?( Rational(RationalImpl) l, Rational(RationalImpl) r );
    60128
    61129// conversion
    62 double widen( Rational r );
    63 Rational narrow( double f, RationalImpl md );
     130// forall ( otype RationalImpl | arithmetic( RationalImpl ) )
     131// double widen( Rational(RationalImpl) r );
     132// forall ( otype RationalImpl | arithmetic( RationalImpl ) )
     133// Rational(RationalImpl) narrow( double f, RationalImpl md );
    64134
    65135// I/O
    66 forall( dtype istype | istream( istype ) ) istype * ?|?( istype *, Rational * );
    67 forall( dtype ostype | ostream( ostype ) ) ostype * ?|?( ostype *, Rational );
     136forall ( otype RationalImpl | arithmetic( RationalImpl ) )
     137forall( dtype istype | istream( istype ) | { istype * ?|?( istype *, RationalImpl * ); } )
     138istype * ?|?( istype *, Rational(RationalImpl) * );
     139
     140forall ( otype RationalImpl | arithmetic( RationalImpl ) )
     141forall( dtype ostype | ostream( ostype ) | { ostype * ?|?( ostype *, RationalImpl ); } )
     142ostype * ?|?( ostype *, Rational(RationalImpl ) );
    68143
    69144#endif // RATIONAL_H
  • src/libcfa/rational.c

    rce8c12f rd36c117  
    1010// Created On       : Wed Apr  6 17:54:28 2016
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Thu Apr 27 17:05:06 2017
    13 // Update Count     : 51
     12// Last Modified On : Sun May 14 17:25:19 2017
     13// Update Count     : 131
    1414//
    1515
     
    1717#include "fstream"
    1818#include "stdlib"
    19 #include "math"                                                                                 // floor
    20 
    21 
    22 // constants
    23 
    24 struct Rational 0 = {0, 1};
    25 struct Rational 1 = {1, 1};
    26 
    2719
    2820// helper routines
     
    3022// Calculate greatest common denominator of two numbers, the first of which may be negative. Used to reduce rationals.
    3123// alternative: https://en.wikipedia.org/wiki/Binary_GCD_algorithm
     24forall ( otype RationalImpl | arithmetic( RationalImpl ) )
    3225static RationalImpl gcd( RationalImpl a, RationalImpl b ) {
    3326        for ( ;; ) {                                                                            // Euclid's algorithm
    3427                RationalImpl r = a % b;
    35           if ( r == 0 ) break;
     28          if ( r == (RationalImpl){0} ) break;
    3629                a = b;
    3730                b = r;
     
    4033} // gcd
    4134
    42 static RationalImpl simplify( RationalImpl *n, RationalImpl *d ) {
    43         if ( *d == 0 ) {
     35forall ( otype RationalImpl | arithmetic( RationalImpl ) )
     36static RationalImpl simplify( RationalImpl * n, RationalImpl * d ) {
     37        if ( *d == (RationalImpl){0} ) {
    4438                serr | "Invalid rational number construction: denominator cannot be equal to 0." | endl;
    4539                exit( EXIT_FAILURE );
    4640        } // exit
    47         if ( *d < 0 ) { *d = -*d; *n = -*n; }                           // move sign to numerator
     41        if ( *d < (RationalImpl){0} ) { *d = -*d; *n = -*n; } // move sign to numerator
    4842        return gcd( abs( *n ), *d );                                            // simplify
    4943} // Rationalnumber::simplify
     
    5246// constructors
    5347
    54 void ?{}( Rational * r ) {
    55         r{ 0, 1 };
     48forall ( otype RationalImpl | arithmetic( RationalImpl ) )
     49void ?{}( Rational(RationalImpl) * r ) {
     50        r{ (RationalImpl){0}, (RationalImpl){1} };
    5651} // rational
    5752
    58 void ?{}( Rational * r, RationalImpl n ) {
    59         r{ n, 1 };
     53forall ( otype RationalImpl | arithmetic( RationalImpl ) )
     54void ?{}( Rational(RationalImpl) * r, RationalImpl n ) {
     55        r{ n, (RationalImpl){1} };
    6056} // rational
    6157
    62 void ?{}( Rational * r, RationalImpl n, RationalImpl d ) {
     58forall ( otype RationalImpl | arithmetic( RationalImpl ) )
     59void ?{}( Rational(RationalImpl) * r, RationalImpl n, RationalImpl d ) {
    6360        RationalImpl t = simplify( &n, &d );                            // simplify
    6461        r->numerator = n / t;
     
    6966// getter for numerator/denominator
    7067
    71 RationalImpl numerator( Rational r ) {
     68forall ( otype RationalImpl | arithmetic( RationalImpl ) )
     69RationalImpl numerator( Rational(RationalImpl) r ) {
    7270        return r.numerator;
    7371} // numerator
    7472
    75 RationalImpl denominator( Rational r ) {
     73forall ( otype RationalImpl | arithmetic( RationalImpl ) )
     74RationalImpl denominator( Rational(RationalImpl) r ) {
    7675        return r.denominator;
    7776} // denominator
    7877
    79 [ RationalImpl, RationalImpl ] ?=?( * [ RationalImpl, RationalImpl ] dest, Rational src ) {
     78forall ( otype RationalImpl | arithmetic( RationalImpl ) )
     79[ RationalImpl, RationalImpl ] ?=?( * [ RationalImpl, RationalImpl ] dest, Rational(RationalImpl) src ) {
    8080        return *dest = src.[ numerator, denominator ];
    8181}
     
    8383// setter for numerator/denominator
    8484
    85 RationalImpl numerator( Rational r, RationalImpl n ) {
     85forall ( otype RationalImpl | arithmetic( RationalImpl ) )
     86RationalImpl numerator( Rational(RationalImpl) r, RationalImpl n ) {
    8687        RationalImpl prev = r.numerator;
    8788        RationalImpl t = gcd( abs( n ), r.denominator );                // simplify
     
    9192} // numerator
    9293
    93 RationalImpl denominator( Rational r, RationalImpl d ) {
     94forall ( otype RationalImpl | arithmetic( RationalImpl ) )
     95RationalImpl denominator( Rational(RationalImpl) r, RationalImpl d ) {
    9496        RationalImpl prev = r.denominator;
    9597        RationalImpl t = simplify( &r.numerator, &d );                  // simplify
     
    102104// comparison
    103105
    104 int ?==?( Rational l, Rational r ) {
     106forall ( otype RationalImpl | arithmetic( RationalImpl ) )
     107int ?==?( Rational(RationalImpl) l, Rational(RationalImpl) r ) {
    105108        return l.numerator * r.denominator == l.denominator * r.numerator;
    106109} // ?==?
    107110
    108 int ?!=?( Rational l, Rational r ) {
     111forall ( otype RationalImpl | arithmetic( RationalImpl ) )
     112int ?!=?( Rational(RationalImpl) l, Rational(RationalImpl) r ) {
    109113        return ! ( l == r );
    110114} // ?!=?
    111115
    112 int ?<?( Rational l, Rational r ) {
     116forall ( otype RationalImpl | arithmetic( RationalImpl ) )
     117int ?<?( Rational(RationalImpl) l, Rational(RationalImpl) r ) {
    113118        return l.numerator * r.denominator < l.denominator * r.numerator;
    114119} // ?<?
    115120
    116 int ?<=?( Rational l, Rational r ) {
    117         return l < r || l == r;
     121forall ( otype RationalImpl | arithmetic( RationalImpl ) )
     122int ?<=?( Rational(RationalImpl) l, Rational(RationalImpl) r ) {
     123        return l.numerator * r.denominator <= l.denominator * r.numerator;
    118124} // ?<=?
    119125
    120 int ?>?( Rational l, Rational r ) {
     126forall ( otype RationalImpl | arithmetic( RationalImpl ) )
     127int ?>?( Rational(RationalImpl) l, Rational(RationalImpl) r ) {
    121128        return ! ( l <= r );
    122129} // ?>?
    123130
    124 int ?>=?( Rational l, Rational r ) {
     131forall ( otype RationalImpl | arithmetic( RationalImpl ) )
     132int ?>=?( Rational(RationalImpl) l, Rational(RationalImpl) r ) {
    125133        return ! ( l < r );
    126134} // ?>=?
     
    129137// arithmetic
    130138
    131 Rational -?( Rational r ) {
    132         Rational t = { -r.numerator, r.denominator };
     139forall ( otype RationalImpl | arithmetic( RationalImpl ) )
     140Rational(RationalImpl) +?( Rational(RationalImpl) r ) {
     141        Rational(RationalImpl) t = { r.numerator, r.denominator };
     142        return t;
     143} // +?
     144
     145forall ( otype RationalImpl | arithmetic( RationalImpl ) )
     146Rational(RationalImpl) -?( Rational(RationalImpl) r ) {
     147        Rational(RationalImpl) t = { -r.numerator, r.denominator };
    133148        return t;
    134149} // -?
    135150
    136 Rational ?+?( Rational l, Rational r ) {
     151forall ( otype RationalImpl | arithmetic( RationalImpl ) )
     152Rational(RationalImpl) ?+?( Rational(RationalImpl) l, Rational(RationalImpl) r ) {
    137153        if ( l.denominator == r.denominator ) {                         // special case
    138                 Rational t = { l.numerator + r.numerator, l.denominator };
     154                Rational(RationalImpl) t = { l.numerator + r.numerator, l.denominator };
    139155                return t;
    140156        } else {
    141                 Rational t = { l.numerator * r.denominator + l.denominator * r.numerator, l.denominator * r.denominator };
     157                Rational(RationalImpl) t = { l.numerator * r.denominator + l.denominator * r.numerator, l.denominator * r.denominator };
    142158                return t;
    143159        } // if
    144160} // ?+?
    145161
    146 Rational ?-?( Rational l, Rational r ) {
     162forall ( otype RationalImpl | arithmetic( RationalImpl ) )
     163Rational(RationalImpl) ?-?( Rational(RationalImpl) l, Rational(RationalImpl) r ) {
    147164        if ( l.denominator == r.denominator ) {                         // special case
    148                 Rational t = { l.numerator - r.numerator, l.denominator };
     165                Rational(RationalImpl) t = { l.numerator - r.numerator, l.denominator };
    149166                return t;
    150167        } else {
    151                 Rational t = { l.numerator * r.denominator - l.denominator * r.numerator, l.denominator * r.denominator };
     168                Rational(RationalImpl) t = { l.numerator * r.denominator - l.denominator * r.numerator, l.denominator * r.denominator };
    152169                return t;
    153170        } // if
    154171} // ?-?
    155172
    156 Rational ?*?( Rational l, Rational r ) {
    157         Rational t = { l.numerator * r.numerator, l.denominator * r.denominator };
     173forall ( otype RationalImpl | arithmetic( RationalImpl ) )
     174Rational(RationalImpl) ?*?( Rational(RationalImpl) l, Rational(RationalImpl) r ) {
     175        Rational(RationalImpl) t = { l.numerator * r.numerator, l.denominator * r.denominator };
    158176        return t;
    159177} // ?*?
    160178
    161 Rational ?/?( Rational l, Rational r ) {
    162         if ( r.numerator < 0 ) {
     179forall ( otype RationalImpl | arithmetic( RationalImpl ) )
     180Rational(RationalImpl) ?/?( Rational(RationalImpl) l, Rational(RationalImpl) r ) {
     181        if ( r.numerator < (RationalImpl){0} ) {
    163182                r.numerator = -r.numerator;
    164183                r.denominator = -r.denominator;
    165184        } // if
    166         Rational t = { l.numerator * r.denominator, l.denominator * r.numerator };
     185        Rational(RationalImpl) t = { l.numerator * r.denominator, l.denominator * r.numerator };
    167186        return t;
    168187} // ?/?
     
    171190// conversion
    172191
    173 double widen( Rational r ) {
    174         return (double)r.numerator / (double)r.denominator;
    175 } // widen
    176 
    177 // http://www.ics.uci.edu/~eppstein/numth/frap.c
    178 Rational narrow( double f, RationalImpl md ) {
    179         if ( md <= 1 ) {                                                                        // maximum fractional digits too small?
    180                 return (Rational){ f, 1};                                               // truncate fraction
    181         } // if
    182 
    183         // continued fraction coefficients
    184         RationalImpl m00 = 1, m11 = 1, m01 = 0, m10 = 0;
    185         RationalImpl ai, t;
    186 
    187         // find terms until denom gets too big
    188         for ( ;; ) {
    189                 ai = (RationalImpl)f;
    190           if ( ! (m10 * ai + m11 <= md) ) break;
    191                 t = m00 * ai + m01;
    192                 m01 = m00;
    193                 m00 = t;
    194                 t = m10 * ai + m11;
    195                 m11 = m10;
    196                 m10 = t;
    197                 t = (double)ai;
    198           if ( f == t ) break;                                                          // prevent division by zero
    199                 f = 1 / (f - t);
    200           if ( f > (double)0x7FFFFFFF ) break;                          // representation failure
    201         }
    202         return (Rational){ m00, m10 };
    203 } // narrow
     192// forall ( otype RationalImpl | arithmetic( RationalImpl ) )
     193// double widen( Rational(RationalImpl) r ) {
     194//      return (double)r.numerator / (double)r.denominator;
     195// } // widen
     196
     197// // http://www.ics.uci.edu/~eppstein/numth/frap.c
     198// forall ( otype RationalImpl | arithmetic( RationalImpl ) )
     199// Rational(RationalImpl) narrow( double f, RationalImpl md ) {
     200//      if ( md <= 1 ) {                                                                        // maximum fractional digits too small?
     201//              return (Rational(RationalImpl)){ f, 1};                 // truncate fraction
     202//      } // if
     203
     204//      // continued fraction coefficients
     205//      RationalImpl m00 = 1, m11 = 1, m01 = 0, m10 = 0;
     206//      RationalImpl ai, t;
     207
     208//      // find terms until denom gets too big
     209//      for ( ;; ) {
     210//              ai = (RationalImpl)f;
     211//        if ( ! (m10 * ai + m11 <= md) ) break;
     212//              t = m00 * ai + m01;
     213//              m01 = m00;
     214//              m00 = t;
     215//              t = m10 * ai + m11;
     216//              m11 = m10;
     217//              m10 = t;
     218//              t = (double)ai;
     219//        if ( f == t ) break;                                                          // prevent division by zero
     220//        f = 1 / (f - (double)t);
     221//        if ( f > (double)0x7FFFFFFF ) break;                          // representation failure
     222//      }
     223//      return (Rational(RationalImpl)){ m00, m10 };
     224// } // narrow
    204225
    205226
    206227// I/O
    207228
    208 forall( dtype istype | istream( istype ) )
    209 istype * ?|?( istype *is, Rational *r ) {
     229forall ( otype RationalImpl | arithmetic( RationalImpl ) )
     230forall( dtype istype | istream( istype ) | { istype * ?|?( istype *, RationalImpl * ); } )
     231istype * ?|?( istype * is, Rational(RationalImpl) * r ) {
    210232        RationalImpl t;
    211233        is | &(r->numerator) | &(r->denominator);
     
    216238} // ?|?
    217239
    218 forall( dtype ostype | ostream( ostype ) )
    219 ostype * ?|?( ostype *os, Rational r ) {
     240forall ( otype RationalImpl | arithmetic( RationalImpl ) )
     241forall( dtype ostype | ostream( ostype ) | { ostype * ?|?( ostype *, RationalImpl ); } )
     242ostype * ?|?( ostype * os, Rational(RationalImpl ) r ) {
    220243        return os | r.numerator | '/' | r.denominator;
    221244} // ?|?
  • src/libcfa/stdlib

    rce8c12f rd36c117  
    1010// Created On       : Thu Jan 28 17:12:35 2016
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Sat Apr  1 17:35:24 2017
    13 // Update Count     : 104
     12// Last Modified On : Tue May  9 08:42:44 2017
     13// Update Count     : 107
    1414//
    1515
     
    8484forall( otype T | { int ?<?( T, T ); } )
    8585T * bsearch( T key, const T * arr, size_t dimension );
     86
    8687forall( otype T | { int ?<?( T, T ); } )
    8788unsigned int bsearch( T key, const T * arr, size_t dimension );
     89
    8890
    8991forall( otype T | { int ?<?( T, T ); } )
     
    107109double abs( double _Complex );
    108110long double abs( long double _Complex );
     111forall ( otype T | { void ?{}( T *, zero_t ); int ?<?( T, T ); T -?( T ); } )
     112T abs( T );
    109113
    110114//---------------------------------------
  • src/libcfa/stdlib.c

    rce8c12f rd36c117  
    1010// Created On       : Thu Jan 28 17:10:29 2016
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Sun Apr 16 10:41:05 2017
    13 // Update Count     : 189
     12// Last Modified On : Tue May  9 08:43:00 2017
     13// Update Count     : 191
    1414//
    1515
     
    2727} // extern "C"
    2828
    29 forall( dtype T | sized(T) ) T * malloc( void ) {
    30         //printf( "malloc1\n" );
    31     return (T *)(void*)malloc( (size_t)sizeof(T) );
     29forall( dtype T | sized(T) ) T * malloc( void ) {               // type-safe
     30    return (T *)(void *)malloc( (size_t)sizeof(T) );
    3231} // malloc
    33 forall( dtype T | sized(T) ) T * malloc( char fill ) {
    34         //printf( "malloc3\n" );
    35         T * ptr = (T *)(void*)malloc( (size_t)sizeof(T) );
     32
     33forall( dtype T | sized(T) ) T * malloc( char fill ) {  // initial with fill value (like calloc)
     34        T * ptr = (T *)(void *)malloc( (size_t)sizeof(T) );
    3635    return memset( ptr, (int)fill, sizeof(T) );
    3736} // malloc
    3837
    39 forall( dtype T | sized(T) ) T * calloc( size_t nmemb ) {
    40         //printf( "calloc\n" );
     38forall( dtype T | sized(T) ) T * malloc( T * ptr, size_t size ) { // alternative realloc
     39    return (T *)realloc( ptr, size );
     40} // malloc
     41
     42forall( dtype T | sized(T) ) T * malloc( T * ptr, size_t size, unsigned char fill ) { // alternative realloc with fill value
     43    return (T *)realloc( ptr, size, fill );
     44} // malloc
     45
     46
     47forall( dtype T | sized(T) ) T * calloc( size_t nmemb ) { // type-safe array initialization with fill 0
    4148    return (T *)calloc( nmemb, sizeof(T) );
    4249} // calloc
    4350
    44 forall( dtype T | sized(T) ) T * realloc( T * ptr, size_t size ) {
    45         //printf( "realloc1\n" );
     51
     52forall( dtype T | sized(T) ) T * realloc( T * ptr, size_t size ) { // type-safe
    4653    return (T *)(void *)realloc( (void *)ptr, size );
    4754} // realloc
    48 forall( dtype T | sized(T) ) T * realloc( T * ptr, size_t size, unsigned char fill ) {
    49         //printf( "realloc2\n" );
     55
     56forall( dtype T | sized(T) ) T * realloc( T * ptr, size_t size, unsigned char fill ) { // alternative realloc with fill value
    5057    char * nptr = (T *)(void *)realloc( (void *)ptr, size );
    5158    size_t unused = malloc_usable_size( nptr );
     
    5461} // realloc
    5562
    56 forall( dtype T | sized(T) ) T * malloc( T * ptr, size_t size ) {
    57         //printf( "malloc4\n" );
    58     return (T *)realloc( ptr, size );
    59 } // malloc
    60 forall( dtype T | sized(T) ) T * malloc( T * ptr, size_t size, unsigned char fill ) {
    61         //printf( "malloc5\n" );
    62     return (T *)realloc( ptr, size, fill );
    63 } // malloc
    64 
    65 forall( dtype T | sized(T) ) T * aligned_alloc( size_t alignment ) {
    66         //printf( "aligned_alloc\n" );
     63
     64forall( dtype T | sized(T) ) T * aligned_alloc( size_t alignment ) { // aligned allocation
    6765    return (T *)memalign( alignment, sizeof(T) );
    6866} // aligned_alloc
    6967
    7068forall( dtype T | sized(T) ) T * memalign( size_t alignment ) {
    71         //printf( "memalign\n" );
    7269    return (T *)memalign( alignment, sizeof(T) );
    7370} // memalign
    7471
    7572forall( dtype T | sized(T) ) int posix_memalign( T ** ptr, size_t alignment ) {
    76         //printf( "posix_memalign\n" );
    7773    return posix_memalign( (void **)ptr, alignment, sizeof(T) );
    7874} // posix_memalign
    7975
    80 forall( dtype T, ttype Params | sized(T) | { void ?{}( T *, Params ); } )
     76
     77forall( dtype T, ttype Params | sized(T) | { void ?{}( T *, Params ); } ) //  new
    8178T * new( Params p ) {
    8279        return ((T *)malloc()){ p };
    83 }
    84 
    85 forall( dtype T | { void ^?{}(T *); } )
     80} // new
     81
     82forall( dtype T | { void ^?{}(T *); } )                                 // delete
    8683void delete( T * ptr ) {
    87   if ( ptr ) {
    88     ^ptr{};
    89     free( ptr );
    90   }
    91 }
     84        if ( ptr ) {
     85                ^ptr{};
     86                free( ptr );
     87        }
     88} // delete
    9289
    9390forall( dtype T, ttype Params | { void ^?{}(T *); void delete(Params); } )
     
    9895        }
    9996        delete( rest );
    100 }
     97} // delete
    10198
    10299//---------------------------------------
     
    106103        if ( sscanf( ptr, "%d", &i ) == EOF ) {}
    107104        return i;
    108 }
     105} // ato
     106
    109107unsigned int ato( const char * ptr ) {
    110108        unsigned int ui;
    111109        if ( sscanf( ptr, "%u", &ui ) == EOF ) {}
    112110        return ui;
    113 }
     111} // ato
     112
    114113long int ato( const char * ptr ) {
    115114        long int li;
    116115        if ( sscanf( ptr, "%ld", &li ) == EOF ) {}
    117116        return li;
    118 }
     117} // ato
     118
    119119unsigned long int ato( const char * ptr ) {
    120120        unsigned long int uli;
    121121        if ( sscanf( ptr, "%lu", &uli ) == EOF ) {}
    122122        return uli;
    123 }
     123} // ato
     124
    124125long long int ato( const char * ptr ) {
    125126        long long int lli;
    126127        if ( sscanf( ptr, "%lld", &lli ) == EOF ) {}
    127128        return lli;
    128 }
     129} // ato
     130
    129131unsigned long long int ato( const char * ptr ) {
    130132        unsigned long long int ulli;
    131133        if ( sscanf( ptr, "%llu", &ulli ) == EOF ) {}
    132134        return ulli;
    133 }
     135} // ato
     136
    134137
    135138float ato( const char * ptr ) {
     
    137140        if ( sscanf( ptr, "%f", &f ) == EOF ) {}
    138141        return f;
    139 }
     142} // ato
     143
    140144double ato( const char * ptr ) {
    141145        double d;
    142146        if ( sscanf( ptr, "%lf", &d ) == EOF ) {}
    143147        return d;
    144 }
     148} // ato
     149
    145150long double ato( const char * ptr ) {
    146151        long double ld;
    147152        if ( sscanf( ptr, "%Lf", &ld ) == EOF ) {}
    148153        return ld;
    149 }
     154} // ato
     155
    150156
    151157float _Complex ato( const char * ptr ) {
     
    153159        if ( sscanf( ptr, "%g%gi", &re, &im ) == EOF ) {}
    154160        return re + im * _Complex_I;
    155 }
     161} // ato
     162
    156163double _Complex ato( const char * ptr ) {
    157164        double re, im;
    158165        if ( sscanf( ptr, "%lf%lfi", &re, &im ) == EOF ) {}
    159166        return re + im * _Complex_I;
    160 }
     167} // ato
     168
    161169long double _Complex ato( const char * ptr ) {
    162170        long double re, im;
    163171        if ( sscanf( ptr, "%Lf%Lfi", &re, &im ) == EOF ) {}
    164172        return re + im * _Complex_I;
    165 }
     173} // ato
     174
    166175
    167176int strto( const char * sptr, char ** eptr, int base ) {
    168177        return (int)strtol( sptr, eptr, base );
    169 }
     178} // strto
     179
    170180unsigned int strto( const char * sptr, char ** eptr, int base ) {
    171181        return (unsigned int)strtoul( sptr, eptr, base );
    172 }
     182} // strto
     183
    173184long int strto( const char * sptr, char ** eptr, int base ) {
    174185        return strtol( sptr, eptr, base );
    175 }
     186} // strto
     187
    176188unsigned long int strto( const char * sptr, char ** eptr, int base ) {
    177189        return strtoul( sptr, eptr, base );
    178 }
     190} // strto
     191
    179192long long int strto( const char * sptr, char ** eptr, int base ) {
    180193        return strtoll( sptr, eptr, base );
    181 }
     194} // strto
     195
    182196unsigned long long int strto( const char * sptr, char ** eptr, int base ) {
    183197        return strtoull( sptr, eptr, base );
    184 }
     198} // strto
     199
    185200
    186201float strto( const char * sptr, char ** eptr ) {
    187202        return strtof( sptr, eptr );
    188 }
     203} // strto
     204
    189205double strto( const char * sptr, char ** eptr ) {
    190206        return strtod( sptr, eptr );
    191 }
     207} // strto
     208
    192209long double strto( const char * sptr, char ** eptr ) {
    193210        return strtold( sptr, eptr );
    194 }
     211} // strto
     212
    195213
    196214float _Complex strto( const char * sptr, char ** eptr ) {
     
    201219        if ( sptr == *eptr ) return 0.0;
    202220        return re + im * _Complex_I;
    203 }
     221} // strto
     222
    204223double _Complex strto( const char * sptr, char ** eptr ) {
    205224        double re, im;
     
    209228        if ( sptr == *eptr ) return 0.0;
    210229        return re + im * _Complex_I;
    211 }
     230} // strto
     231
    212232long double _Complex strto( const char * sptr, char ** eptr ) {
    213233        long double re, im;
     
    217237        if ( sptr == *eptr ) return 0.0;
    218238        return re + im * _Complex_I;
    219 }
     239} // strto
    220240
    221241//---------------------------------------
Note: See TracChangeset for help on using the changeset viewer.