[af0c8da] | 1 | // |
---|
| 2 | // Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo |
---|
| 3 | // |
---|
| 4 | // The contents of this file are covered under the licence agreement in the |
---|
| 5 | // file "LICENCE" distributed with Cforall. |
---|
| 6 | // |
---|
[dc8511c] | 7 | // sum.cfa -- test resolvers ability to deal with many variables with the same name and to use the minimum number of |
---|
| 8 | // casts necessary to disambiguate overloaded variable names. |
---|
[af0c8da] | 9 | // |
---|
| 10 | // Author : Peter A. Buhr |
---|
| 11 | // Created On : Wed May 27 17:56:53 2015 |
---|
| 12 | // Last Modified By : Peter A. Buhr |
---|
[ef346f7c] | 13 | // Last Modified On : Sun Dec 23 23:00:38 2018 |
---|
| 14 | // Update Count : 287 |
---|
[af0c8da] | 15 | // |
---|
| 16 | |
---|
[73abe95] | 17 | #include <fstream.hfa> |
---|
| 18 | #include <stdlib.hfa> |
---|
[af0c8da] | 19 | |
---|
[7d69095] | 20 | void ?{}( int & c, zero_t ) { c = 0; } // not in prelude |
---|
[af0c8da] | 21 | |
---|
| 22 | trait sumable( otype T ) { |
---|
[7d69095] | 23 | void ?{}( T &, zero_t ); // 0 literal constructor |
---|
[af0c8da] | 24 | T ?+?( T, T ); // assortment of additions |
---|
| 25 | T ?+=?( T &, T ); |
---|
| 26 | T ++?( T & ); |
---|
| 27 | T ?++( T & ); |
---|
| 28 | }; // sumable |
---|
| 29 | |
---|
| 30 | forall( otype T | sumable( T ) ) // use trait |
---|
[7d69095] | 31 | T sum( size_t size, T a[] ) { |
---|
| 32 | T total = 0; // initialize by 0 constructor |
---|
| 33 | for ( size_t i = 0; i < size; i += 1 ) |
---|
[2b9ddf2] | 34 | total += a[i]; // select appropriate + |
---|
[af0c8da] | 35 | return total; |
---|
| 36 | } // sum |
---|
| 37 | |
---|
| 38 | // Not in prelude. |
---|
| 39 | unsigned char ?+?( unsigned char t1, unsigned char t2 ) { return (int)t1 + t2; } // cast forces integer addition, otherwise recursion |
---|
| 40 | unsigned char ?+=?( unsigned char & t1, unsigned char t2 ) { t1 = t1 + t2; return t1; } |
---|
| 41 | unsigned char ++?( unsigned char & t ) { t += 1; return t; } |
---|
| 42 | unsigned char ?++( unsigned char & t ) { unsigned char temp = t; t += 1; return temp; } |
---|
| 43 | |
---|
| 44 | // Not in prelude. |
---|
| 45 | void ?{}( unsigned char & c, zero_t ) { c = 0; } |
---|
| 46 | void ?{}( float & f, zero_t ) { f = 0.0; } |
---|
| 47 | void ?{}( double & d, zero_t ) { d = 0.0; } |
---|
| 48 | |
---|
| 49 | int main( void ) { |
---|
| 50 | const int low = 5, High = 15, size = High - low; |
---|
| 51 | |
---|
| 52 | unsigned char s = 0, a[size], v = (char)low; |
---|
| 53 | for ( int i = 0; i < size; i += 1, v += 1 ) { |
---|
| 54 | s += v; |
---|
| 55 | a[i] = v; |
---|
| 56 | } // for |
---|
| 57 | sout | "sum from" | low | "to" | High | "is" |
---|
[200fcb3] | 58 | | sum( size, (unsigned char *)a ) | ", check" | (int)s; |
---|
[af0c8da] | 59 | |
---|
| 60 | int s = 0, a[size], v = low; |
---|
| 61 | for ( int i = 0; i < size; i += 1, v += 1 ) { |
---|
| 62 | s += (int)v; |
---|
| 63 | a[i] = (int)v; |
---|
| 64 | } // for |
---|
| 65 | sout | "sum from" | low | "to" | High | "is" |
---|
[200fcb3] | 66 | | sum( size, (int *)a ) | ", check" | (int)s; |
---|
[af0c8da] | 67 | |
---|
| 68 | float s = 0.0f, a[size], v = low / 10.0f; |
---|
| 69 | for ( int i = 0; i < size; i += 1, v += 0.1f ) { |
---|
| 70 | s += (float)v; |
---|
| 71 | a[i] = (float)v; |
---|
| 72 | } // for |
---|
| 73 | sout | "sum from" | low / 10.0f | "to" | High / 10.0f | "is" |
---|
[200fcb3] | 74 | | sum( size, (float *)a ) | ", check" | (float)s; |
---|
[af0c8da] | 75 | |
---|
| 76 | double s = 0.0, a[size], v = low / 10.0; |
---|
| 77 | for ( int i = 0; i < size; i += 1, v += 0.1 ) { |
---|
| 78 | s += (double)v; |
---|
| 79 | a[i] = (double)v; |
---|
| 80 | } // for |
---|
| 81 | sout | "sum from" | low / 10.0 | "to" | High / 10.0 | "is" |
---|
[200fcb3] | 82 | | sum( size, (double *)a ) | ", check" | (double)s; |
---|
[af0c8da] | 83 | |
---|
| 84 | struct S { int i, j; }; |
---|
| 85 | void ?{}( S & s ) { s.[i, j] = 0; } |
---|
[c0b4db0] | 86 | void ?{}( S & s, int i ) { s.[i, j] = [i, 0]; } |
---|
| 87 | void ?{}( S & s, int i, int j ) { s.[i, j] = [i, j]; } |
---|
| 88 | void ?{}( S & s, zero_t ) { s.[i, j] = 0; } |
---|
| 89 | void ?{}( S & s, one_t ) { s.[i, j] = 1; } |
---|
[af0c8da] | 90 | S ?+?( S t1, S t2 ) { return (S){ t1.i + t2.i, t1.j + t2.j }; } |
---|
| 91 | S ?+=?( S & t1, S t2 ) { t1 = t1 + t2; return t1; } |
---|
| 92 | S ++?( S & t ) { t += (S){1}; return t; } |
---|
| 93 | S ?++( S & t ) { S temp = t; t += (S){1}; return temp; } |
---|
[09687aa] | 94 | ofstream & ?|?( ofstream & os, S v ) { return os | v.i | v.j; } |
---|
[ef346f7c] | 95 | void ?|?( ofstream & os, S v ) { (ofstream &)(os | v); nl( os ); } |
---|
[af0c8da] | 96 | |
---|
| 97 | S s = (S){0}, a[size], v = { low, low }; |
---|
| 98 | for ( int i = 0; i < size; i += 1, v += (S){1} ) { |
---|
| 99 | s += (S)v; |
---|
| 100 | a[i] = (S)v; |
---|
| 101 | } // for |
---|
| 102 | sout | "sum from" | low | "to" | High | "is" |
---|
[200fcb3] | 103 | | sum( size, (S *)a ) | ", check" | (S)s; |
---|
[2b9ddf2] | 104 | |
---|
| 105 | forall( otype Impl | sumable( Impl ) ) |
---|
| 106 | struct GS { |
---|
| 107 | Impl * x, * y; |
---|
| 108 | }; |
---|
| 109 | GS(int) gs; |
---|
| 110 | gs.x = anew( size ); // create array storage for field |
---|
| 111 | s = 0; v = low; |
---|
| 112 | for ( int i = 0; i < size; i += 1, v += 1 ) { |
---|
| 113 | s += (int)v; |
---|
[7d69095] | 114 | gs.x[i] = (int)v; // set field array in generic type |
---|
[2b9ddf2] | 115 | } // for |
---|
| 116 | sout | "sum from" | low | "to" | High | "is" |
---|
[200fcb3] | 117 | | sum( size, gs.x ) | ", check" | (int)s; // add field array in generic type |
---|
[b3763ca] | 118 | delete( gs.x ); |
---|
[af0c8da] | 119 | } // main |
---|
| 120 | |
---|
| 121 | // Local Variables: // |
---|
| 122 | // tab-width: 4 // |
---|
[dc8511c] | 123 | // compile-command: "cfa sum.cfa" // |
---|
[af0c8da] | 124 | // End: // |
---|