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 | //
|
---|
7 | // sum.c -- test resolvers ability to deal with many variables with the same name and to use the minimum number of casts
|
---|
8 | // necessary to disambiguate overloaded variable names.
|
---|
9 | //
|
---|
10 | // Author : Peter A. Buhr
|
---|
11 | // Created On : Wed May 27 17:56:53 2015
|
---|
12 | // Last Modified By : Peter A. Buhr
|
---|
13 | // Last Modified On : Sun Oct 29 10:29:12 2017
|
---|
14 | // Update Count : 260
|
---|
15 | //
|
---|
16 |
|
---|
17 | #include <fstream>
|
---|
18 |
|
---|
19 | void ?{}( int & c, zero_t ) { c = 0; }
|
---|
20 |
|
---|
21 | trait sumable( otype T ) {
|
---|
22 | void ?{}( T &, zero_t ); // constructor from 0 literal
|
---|
23 | T ?+?( T, T ); // assortment of additions
|
---|
24 | T ?+=?( T &, T );
|
---|
25 | T ++?( T & );
|
---|
26 | T ?++( T & );
|
---|
27 | }; // sumable
|
---|
28 |
|
---|
29 | forall( otype T | sumable( T ) ) // use trait
|
---|
30 | T sum( unsigned int n, T a[] ) {
|
---|
31 | T total = 0; // instantiate T, select 0
|
---|
32 | for ( unsigned int i = 0; i < n; i += 1 )
|
---|
33 | total += a[i]; // select +
|
---|
34 | return total;
|
---|
35 | } // sum
|
---|
36 |
|
---|
37 | // Not in prelude.
|
---|
38 | unsigned char ?+?( unsigned char t1, unsigned char t2 ) { return (int)t1 + t2; } // cast forces integer addition, otherwise recursion
|
---|
39 | unsigned char ?+=?( unsigned char & t1, unsigned char t2 ) { t1 = t1 + t2; return t1; }
|
---|
40 | unsigned char ++?( unsigned char & t ) { t += 1; return t; }
|
---|
41 | unsigned char ?++( unsigned char & t ) { unsigned char temp = t; t += 1; return temp; }
|
---|
42 |
|
---|
43 | // Not in prelude.
|
---|
44 | void ?{}( unsigned char & c, zero_t ) { c = 0; }
|
---|
45 | void ?{}( float & f, zero_t ) { f = 0.0; }
|
---|
46 | void ?{}( double & d, zero_t ) { d = 0.0; }
|
---|
47 |
|
---|
48 | int main( void ) {
|
---|
49 | const int low = 5, High = 15, size = High - low;
|
---|
50 |
|
---|
51 | unsigned char s = 0, a[size], v = (char)low;
|
---|
52 | for ( int i = 0; i < size; i += 1, v += 1 ) {
|
---|
53 | s += v;
|
---|
54 | a[i] = v;
|
---|
55 | } // for
|
---|
56 | sout | "sum from" | low | "to" | High | "is"
|
---|
57 | | sum( size, (unsigned char *)a ) | ", check" | (int)s | endl;
|
---|
58 |
|
---|
59 | int s = 0, a[size], v = low;
|
---|
60 | for ( int i = 0; i < size; i += 1, v += 1 ) {
|
---|
61 | s += (int)v;
|
---|
62 | a[i] = (int)v;
|
---|
63 | } // for
|
---|
64 | sout | "sum from" | low | "to" | High | "is"
|
---|
65 | | sum( size, (int *)a ) | ", check" | (int)s | endl;
|
---|
66 |
|
---|
67 | float s = 0.0f, a[size], v = low / 10.0f;
|
---|
68 | for ( int i = 0; i < size; i += 1, v += 0.1f ) {
|
---|
69 | s += (float)v;
|
---|
70 | a[i] = (float)v;
|
---|
71 | } // for
|
---|
72 | sout | "sum from" | low / 10.0f | "to" | High / 10.0f | "is"
|
---|
73 | | sum( size, (float *)a ) | ", check" | (float)s | endl;
|
---|
74 |
|
---|
75 | double s = 0.0, a[size], v = low / 10.0;
|
---|
76 | for ( int i = 0; i < size; i += 1, v += 0.1 ) {
|
---|
77 | s += (double)v;
|
---|
78 | a[i] = (double)v;
|
---|
79 | } // for
|
---|
80 | sout | "sum from" | low / 10.0 | "to" | High / 10.0 | "is"
|
---|
81 | | sum( size, (double *)a ) | ", check" | (double)s | endl;
|
---|
82 |
|
---|
83 | struct S { int i, j; };
|
---|
84 | void ?{}( S & s ) { s.[i, j] = 0; }
|
---|
85 | void ?{}( S & s, int i, int j ) { s.[i,j] = [i, j]; }
|
---|
86 | void ?{}( S & s, zero_t ) { s.[i,j] = 0; }
|
---|
87 | void ?{}( S & s, one_t ) { s.[i,j] = 1; }
|
---|
88 | S ?+?( S t1, S t2 ) { return (S){ t1.i + t2.i, t1.j + t2.j }; }
|
---|
89 | S ?+=?( S & t1, S t2 ) { t1 = t1 + t2; return t1; }
|
---|
90 | S ++?( S & t ) { t += (S){1}; return t; }
|
---|
91 | S ?++( S & t ) { S temp = t; t += (S){1}; return temp; }
|
---|
92 | ofstream * ?|?( ofstream * os, S v ) { return os | v.i | v.j; }
|
---|
93 |
|
---|
94 | S s = (S){0}, a[size], v = { low, low };
|
---|
95 | for ( int i = 0; i < size; i += 1, v += (S){1} ) {
|
---|
96 | s += (S)v;
|
---|
97 | a[i] = (S)v;
|
---|
98 | } // for
|
---|
99 | sout | "sum from" | low | "to" | High | "is"
|
---|
100 | | sum( size, (S *)a ) | ", check" | (S)s | endl;
|
---|
101 | } // main
|
---|
102 |
|
---|
103 | // Local Variables: //
|
---|
104 | // tab-width: 4 //
|
---|
105 | // compile-command: "cfa sum.c" //
|
---|
106 | // End: //
|
---|