source: src/examples/sum.c@ 3cfe27f

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors ctor deferred_resn demangler enum forall-pointer-decay gc_noraii jacob/cs343-translation jenkins-sandbox memory new-ast new-ast-unique-expr new-env no_list persistent-indexer pthread-emulation qualifiedEnum resolv-new string with_gc
Last change on this file since 3cfe27f was b63e376, checked in by Peter A. Buhr <pabuhr@…>, 10 years ago

update refrat with new keywords, update examples, update separators for quoted strings

  • Property mode set to 100644
File size: 2.8 KB
Line 
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 --
8//
9// Author : Peter A. Buhr
10// Created On : Wed May 27 17:56:53 2015
11// Last Modified By : Peter A. Buhr
12// Last Modified On : Fri Mar 4 15:06:47 2016
13// Update Count : 196
14//
15
16#include <fstream>
17
18trait sumable( otype T ) {
19 const T 0;
20 T ?+?( T, T );
21 T ?+=?( T *, T );
22 T ++?( T * );
23 T ?++( T * );
24}; // sumable
25
26forall( otype T | sumable( T ) )
27T sum( unsigned int n, T a[] ) {
28 T total = 0; // instantiate T, select 0
29 for ( unsigned int i = 0; i < n; i += 1 )
30 total += a[i]; // select +
31 return total;
32} // sum
33
34// Required to satisfy sumable as char does not have addition.
35const char 0;
36char ?+?( char t1, char t2 ) { return (int)t1 + t2; } // cast forces integer addition, otherwise recursion
37char ?+=?( char *t1, char t2 ) { *t1 = *t1 + t2; return *t1; }
38char ++?( char *t ) { *t += 1; return *t; }
39char ?++( char *t ) { char temp = *t; *t += 1; return temp; }
40
41int main( void ) {
42 const int low = 5, High = 15, size = High - low;
43
44 char s = 0, a[size], v = low;
45 for ( int i = 0; i < size; i += 1, v += 1 ) {
46 s += v;
47 a[i] = v;
48 } // for
49 sout | "sum from" | low | "to" | High | "is"
50 | (int)sum( size, a ) | "" | ", check" | (int)s | endl;
51
52 int s = 0, a[size], v = low;
53 for ( int i = 0; i < size; i += 1, v += 1 ) {
54 s += (int)v;
55 a[i] = (int)v;
56 } // for
57 sout | "sum from" | low | "to" | High | "is"
58 | sum( size, (int *)a ) | "" | ", check" | (int)s | endl;
59
60 float s = 0.0, a[size], v = low / 10.0;
61 for ( int i = 0; i < size; i += 1, v += 0.1f ) {
62 s += (float)v;
63 a[i] = (float)v;
64 } // for
65 sout | "sum from" | low / 10.0 | "to" | High / 10.0 | "is"
66 | sum( size, (float *)a ) | "" | ", check" | (float)s | endl;
67
68 double s = 0, a[size], v = low / 10.0;
69 for ( int i = 0; i < size; i += 1, v += 0.1 ) {
70 s += (double)v;
71 a[i] = (double)v;
72 } // for
73 sout | "sum from" | low / 10.0 | "to" | High / 10.0 | "is"
74 | sum( size, (double *)a ) | "" | ", check" | (double)s | endl;
75
76 struct S { int i, j; } 0 = { 0, 0 }, 1 = { 1, 1 };
77 S ?+?( S t1, S t2 ) { S s = { t1.i + t2.i, t1.j + t2.j }; return s; }
78 S ?+=?( S *t1, S t2 ) { *t1 = *t1 + t2; return *t1; }
79 S ++?( S *t ) { *t += 1; return *t; }
80 S ?++( S *t ) { S temp = *t; *t += 1; return temp; }
81 ofstream * ?|?( ofstream * os, S v ) { return os | v.i | v.j; }
82
83 S s = 0, a[size], v = { low, low };
84 for ( int i = 0; i < size; i += 1, v += (S)1 ) {
85 s += (S)v;
86 a[i] = (S)v;
87 } // for
88 sout | "sum from" | low | "to" | High | "is"
89 | sum( size, (S *)a ) | "" | ", check" | (S)s | endl;
90} // main
91
92// Local Variables: //
93// tab-width: 4 //
94// compile-command: "cfa sum.c" //
95// End: //
Note: See TracBrowser for help on using the repository browser.