Ignore:
Timestamp:
Jul 8, 2024, 9:03:12 AM (10 days ago)
Author:
Peter A. Buhr <pabuhr@…>
Branches:
master
Children:
061b001
Parents:
5a553e2
Message:

update example programs

Location:
doc/theses/mike_brooks_MMath/programs
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • doc/theses/mike_brooks_MMath/programs/bkgd-carray-arrty.c

    r5a553e2 re26a842  
    148148void stx2() { const T x[10];
    149149//            x[5] = 3.14; // bad
    150                         }
     150}
    151151void stx3() { T const x[10];
    152152//            x[5] = 3.14; // bad
    153                         }
     153}
    154154
    155155// Local Variables: //
  • doc/theses/mike_brooks_MMath/programs/hello-accordion.cfa

    r5a553e2 re26a842  
    22#include <stdlib.hfa>
    33#include <array.hfa>
     4#include <locale.h>                                                     // setlocale
    45
    56
     
    78
    89
    9 
    10 
    11 forall( T, [Nclients], [Ncosts] )
    12 struct request {
    13         unsigned int requestor_id;
    14         array( T, Nclients ) impacted_client_ids; // nested VLA
    15         array( float, Ncosts ) cost_contribs; // nested VLA
    16         float total_cost;
     10forall( T, @[NprovTerty]@, @[Nmunicipalities]@ )
     11struct CanadaPop {
     12        array( T, @NprovTerty@ ) provTerty; $\C{// nested VLA}$
     13        array( T, @Nmunicipalities@ ) municipalities; $\C{// nested VLA}$
     14        int total_pt, total_mun;
    1715};
    1816
     
    2018// TODO: understand (fix?) why these are needed (autogen seems to be failing ... is typeof as struct member nayok?)
    2119
    22 forall( T, [Nclients], [Ncosts] )
    23         void ?{}( T &, request( T, Nclients, Ncosts ) & this ) {}
     20forall( T, [NprovTerty], [Nmunicipalities] )
     21        void ?{}( T &, CanadaPop( T, NprovTerty, Nmunicipalities ) & this ) {}
    2422
    25 forall( T &, [Nclients], [Ncosts] )
    26         void ^?{}( request( T, Nclients, Ncosts ) & this ) {}
     23forall( T &, [NprovTerty], [Nmunicipalities] )
     24        void ^?{}( CanadaPop( T, NprovTerty, Nmunicipalities ) & this ) {}
    2725
    2826
     
    3937
    4038
    41 forall( T, [Nclients], [Ncosts] )
    42 void summarize( request( T, Nclients, Ncosts ) & r ) {
    43         r.total_cost = 0;
    44         for( i; Ncosts )
    45                 r.total_cost += r.cost_contribs[i];
    46         // say the cost is per-client, to make output vary
    47         r.total_cost *= Nclients;
     39
     40forall( T, [NprovTerty], [Nmunicipalities] )
     41void check( CanadaPop( T, NprovTerty, Nmunicipalities ) & pop ) with( pop ) {
     42        total_pt = total_mun = 0;
     43        for ( i; NprovTerty ) total_pt += provTerty[i];
     44        for ( i; Nmunicipalities ) total_mun += municipalities[i];
    4845}
    4946
     
    5956
    6057
     58
     59
    6160int main( int argc, char * argv[] ) {
    62         const int ncl = ato( argv[1] );
    63         const int nco = 2;
    64 
    65         request( int, ncl, nco ) r;
    66         r.cost_contribs[0] = 100;
    67         r.cost_contribs[1] = 0.1;
    68 
    69         summarize(r);
    70         sout | "Total cost:" | r.total_cost;
     61        const int npt = ato( argv[1] ), nmun = ato( argv[2] );
     62        @CanadaPop( int, npt, nmun ) pop;@
     63        // read in population numbers
     64        @check( pop );@
     65        sout | setlocale( LC_NUMERIC, getenv( "LANG" ) );
     66        sout | "Total province/territory:" | pop.total_pt;
     67        sout | "Total municipalities:" | pop.total_mun;
    7168}
    7269/*
    73 $\$$ ./a.out 5
    74 Total cost: 500.5
    75 $\$$ ./a.out 6
    76 Total cost: 600.6
     70$\$$ ./a.out  13  3573
     71Total province/territory: 36,991,981
     72Total municipalities: 36,991,981
    7773*/
     74
     75// Local Variables: //
     76// compile-command: "sed -f sedcmd hello-accordion.cfa > ../build/tmp.cfa; cfa ../build/tmp.cfa -Wall -Wextra" //
     77// End: //
  • doc/theses/mike_brooks_MMath/programs/hello-array.cfa

    r5a553e2 re26a842  
    88
    99
    10 forall( [N] ) // array bound
    11 array(bool, N) & f( array(float, N) & a, array(float, N) & b ) {
    12         array(bool, N) & ret = *alloc(); // sizeof used by alloc
    13         for( i; N ) {
    14                 ret[i] = 0.005 > 2 * (abs(a[i] - b[i])) / (abs(a[i]) + abs(b[i]));
     10forall( [@N@] )                                                         $\C{// array dimension}$
     11array( bool, @N@) & f( array( float, @N@ ) & x, array( float, @N@ ) & y ) {
     12        array( bool, @N@ ) & ret = *@alloc@();  $\C{// sizeof ret  used by alloc}$
     13        for ( i; @N@ ) {
     14                ret[i] = 0.005 > 2 * (abs( x[i] - y[i] )) / (abs( x[i]) + abs(y[i] ));
    1515        }
    1616        return ret;
     
    2929
    3030int main( int argc, char * argv[] ) {
    31         int n = ato( argv[1] );
    32         array(float, n) a, b; // VLA
    33         for ( i; n ) {
    34                 a[i] = 3.14 / (i + 1);
    35                 b[i] = a[i] + 0.005 ;
     31        const int @n@ = ato( argv[1] );                 $\C{// deduce conversion type}$
     32        array( float, @n@ ) x, y;                               $\C{// VLAs}$
     33        for ( i; n ) {                                                  $\C{// initialize arrays}$
     34                x[i] = 3.14 / (i + 1);
     35                y[i] = x[i] + 0.005 ;
    3636        }
    37         array(bool, n) & result = f( a, b ); // call
    38         sout | "result: " | nonl;
     37        array( bool, @n@ ) & result = @f( x, y )@; $\C{// call}$
     38        sout | "result: " | nonl;                               $\C{// print result}$
    3939        for ( i; n )
    4040                sout | result[i] | nonl;
    4141        sout | nl;
    42         free( &result ); // free returned storage
     42        free( &result );                                                $\C{// free result storage}$
    4343}
    4444/*
     
    5050
    5151void fred() {
    52         array(float, 10) a;
    53         array(float, 20) b;
    54         f( a, a );
    55         f( b, b );
    56         f( a, b );
     52        array( float, @10@ ) x;
     53        array( float, @20@ ) y;
     54        f( x, x );
     55        f( y, y );
     56//      f( x, y );
    5757}
    5858
    5959#ifdef SHOWERR1
    6060forall( [M], [N] )
    61 void bad( array(float, M) &a, array(float, N) &b ) {
    62         f( a, a ); // ok
    63         f( b, b ); // ok
    64         f( a, b ); // error
     61void bad( array(float, M) &x, array(float, N) &y ) {
     62        f( x, x );              $\C[1.5in]{// ok}$
     63        f( y, y );              $\C{// ok}$
     64        f( x, y );              $\C{// error}\CRT$
    6565}
    6666#endif
     
    6969
    7070forall( [M], [N] )
    71 void bad_fixed( array(float, M) & a, array(float, N) & b ) {
    72         if ( M == N ) {
    73                 f( a, (array(float, M) &)b ); // cast b to matching type
    74         }
     71void bad_fixed( array( float, M ) & x, array( float, N ) & y ) {
     72        if ( M == N )
     73                f( x, @(array( float, M ) &)@y ); $\C{// cast y to matching type}$
    7574}
     75
     76// Local Variables: //
     77// compile-command: "sed -f sedcmd hello-array.cfa > ../build/tmp.cfa; cfa ../build/tmp.cfa -Wall -Wextra" //
     78// End: //
  • doc/theses/mike_brooks_MMath/programs/hello-md.cfa

    r5a553e2 re26a842  
    3939
    4040forall( [N] )
    41 void print1d_cstyle( array(float, N) & c );
     41void print1d_cstyle( array( float, N ) & r ); $\C{// C style}$
    4242
    43 forall( [N], C & | ar( C, float, N ) )
     43forall( [N], C & @| ar( C, float, N )@ ) $\C{// add trait}$
    4444void print1d( C & c );
    4545
     
    5959
    6060forall( [N] )
    61 void print1d_cstyle( array(float, N) & c ) {
    62         for ( i; N ) {
    63                 sout | c[i] | nonl;
    64         }
     61void print1d_cstyle( array( float, N ) & r ) { $\C{// C style}$
     62        for ( i; N ) sout | r[i] | nonl;
    6563        sout | nl;
    6664}
     65
     66
    6767
    6868
     
    9898
    9999
    100 void fill( array(float, 5, 7) & a ) {
     100void fill( array( float, 5, 7 ) & a ) {
    101101        for ( i; (ptrdiff_t) 5 ) {
    102102                for ( j; 7 ) {
     
    116116
    117117
    118 array( float, 5, 7 ) a;
    119 fill(a);
     118array( float, 5, 7 ) m;
     119fill( m );
    120120/*
    121 0.0  0.1  0.2  0.3  0.4  0.5  0.6 
    122 1.0  1.1  1.2  1.3  1.4  1.5  1.6 
    123 2.0  2.1  2.2  2.3  2.4  2.5  2.6 
    124 3.0  3.1  3.2  3.3  3.4  3.5  3.6 
    125 4.0  4.1  4.2  4.3  4.4  4.5  4.6
     121r/c   0     1     2     3     4     5     6
     1220  0.0  0.1  0.2  @0.3@  0.4  0.5  0.6 
     1231  1.0  1.1  1.2  @1.3@  1.4  1.5  1.6 
     1242  @2.0  2.1  2.2  2.3  2.4  2.5  2.6@ 
     1253  3.0  3.1  3.2  @3.3@  3.4  3.5  3.6 
     1264  4.0  4.1  4.2  @4.3@  4.4  4.5  4.6
    126127*/
    127128
     
    137138
    138139
    139 
    140 print1d_cstyle( a[ 2 ] );  // 2.0  2.1  2.2  2.3  2.4  2.5  2.6
     140print1d_cstyle( m[ 2 ] );  $\C{// row 2:  2.0  2.1  2.2  2.3  2.4  2.5  2.6}$
    141141
    142142
    143143
    144144
    145 print1d( a[ 2 ] );  // 2.0  2.1  2.2  2.3  2.4  2.5  2.6
     145print1d( m[ 2 ] );  $\C{// row:  2.0  2.1  2.2  2.3  2.4  2.5  2.6}$
    146146
    147147
    148148
    149149
    150 print1d( a[ 2, all ] );  // 2.0  2.1  2.2  2.3  2.4  2.5  2.6
    151 print1d( a[ all, 3 ] );  // 0.3  1.3  2.3  3.3  4.3
     150print1d( m[ 2, all ] );  $\C{// row 2:  2.0  2.1  2.2  2.3  2.4  2.5  2.6}$
     151print1d( m[ all, 3 ] );  $\C{// column 3:  0.3  1.3  2.3  3.3  4.3}$
    152152
    153153
    154154
    155 print1d_cstyle( a[ 2, all ] );
     155print1d_cstyle( m[ 2, all ] );
    156156
    157157
     
    163163#ifdef SHOW_ERROR_1
    164164
    165 print1d_cstyle( a[ all, 2 ] );  // bad
     165print1d_cstyle( m[ all, 2 ] );  $\C{// bad}$
    166166
    167167#endif
  • doc/theses/mike_brooks_MMath/programs/lst-issues-attach-reduction.hpp

    r5a553e2 re26a842  
    101101class list {
    102102        struct node {
    103                 @LIST_ENTRY(node) links;@
    104                 @El elem;@
     103                LIST_ENTRY(node) links;
     104                El elem;
    105105        };
    106106        LIST_HEAD(Impl, node);
     
    111111        }
    112112        void push_front( const El & src ) {
    113                 node * n = @new node()@;
     113                node * n = new node();
    114114                n->elem = src;
    115115                LIST_INSERT_HEAD(&impl, n, links);
  • doc/theses/mike_brooks_MMath/programs/lst-issues-wrapped-byref.run.cpp

    r5a553e2 re26a842  
    4949for (auto const& cur : reqs)
    5050        printf("{%d %d} ", cur->pri, cur->rqr);
    51 printf("\n");
     51        printf("\n");
    5252
    5353}
Note: See TracChangeset for help on using the changeset viewer.