[b6ad601] | 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 | // loopctrl.cfa -- |
---|
[b6ad601] | 8 | // |
---|
| 9 | // Author : Peter A. Buhr |
---|
| 10 | // Created On : Wed Aug 8 18:32:59 2018 |
---|
| 11 | // Last Modified By : Peter A. Buhr |
---|
[8789ae4] | 12 | // Last Modified On : Fri Aug 2 08:42:55 2024 |
---|
| 13 | // Update Count : 185 |
---|
[b6ad601] | 14 | // |
---|
| 15 | |
---|
| 16 | #include <fstream.hfa> |
---|
[8789ae4] | 17 | #include <enum.hfa> |
---|
[b6ad601] | 18 | |
---|
[091ccdb] | 19 | void fred() { |
---|
| 20 | // Test all possible loop syntax. |
---|
| 21 | |
---|
[c48b61c] | 22 | int s = 0, c = 10, i = 2; |
---|
| 23 | |
---|
| 24 | for ( c ) { sout | "A"; } sout | nl; |
---|
| 25 | for ( ~= c ) { sout | "B"; } sout | nl; |
---|
| 26 | for ( -~ c ) { sout | "C"; } sout | nl; |
---|
| 27 | for ( -~= c ) { sout | "D"; } sout | nl; |
---|
| 28 | |
---|
| 29 | for ( s ~ c ) { sout | "A"; } sout | nl; |
---|
| 30 | for ( s ~= c ) { sout | "B"; } sout | nl; |
---|
| 31 | for ( s -~ c ) { sout | "C"; } sout | nl; |
---|
| 32 | for ( s -~= c ) { sout | "D"; } sout | nl; |
---|
| 33 | |
---|
| 34 | for ( s ~ c ~ i ) { sout | "A"; } sout | nl; |
---|
| 35 | for ( s ~= c ~ i ) { sout | "B"; } sout | nl; |
---|
| 36 | for ( s -~ c ~ i ) { sout | "C"; } sout | nl; |
---|
| 37 | for ( s -~= c ~ i ) { sout | "D"; } sout | nl; |
---|
| 38 | |
---|
| 39 | for ( j; c ) { sout | j; } sout | nl; |
---|
| 40 | for ( j; ~= c ) { sout | j; } sout | nl; |
---|
| 41 | for ( j; -~ c ) { sout | j; } sout | nl; |
---|
| 42 | for ( j; -~= c ) { sout | j; } sout | nl; |
---|
| 43 | |
---|
| 44 | for ( j; s ~ c ) { sout | j; } sout | nl; |
---|
| 45 | for ( j; s ~= c ) { sout | j; } sout | nl; |
---|
| 46 | for ( j; s -~ c ) { sout | j; } sout | nl; |
---|
| 47 | for ( j; s -~= c ) { sout | j; } sout | nl; |
---|
| 48 | |
---|
| 49 | for ( j; s ~ c ~ i ) { sout | j; } sout | nl; |
---|
| 50 | for ( j; s ~= c ~ i ) { sout | j; } sout | nl; |
---|
| 51 | for ( j; s -~ c ~ i ) { sout | j; } sout | nl; |
---|
| 52 | for ( j; s -~= c ~ i ) { sout | j; } sout | nl; |
---|
[091ccdb] | 53 | |
---|
| 54 | // CANNOT DIRECTLY INITIALIZE INDEX VARIABLE, ONLY SINGLE LOOP INDEX VARIABLE IN DECLARATION |
---|
| 55 | |
---|
[c48b61c] | 56 | for ( j; c ) { sout | j; } sout | nl; |
---|
| 57 | for ( int j; c ) { sout | j; } sout | nl; |
---|
| 58 | for ( int j; ~= c ) { sout | j; } sout | nl; |
---|
| 59 | for ( int j; -~ c ) { sout | j; } sout | nl; |
---|
| 60 | for ( int j; -~= c ) { sout | j; } sout | nl; |
---|
| 61 | |
---|
| 62 | for ( int j; s ~ c ) { sout | j; } sout | nl; |
---|
| 63 | for ( int j; s ~= c ) { sout | j; } sout | nl; |
---|
| 64 | for ( int j; s -~ c ) { sout | j; } sout | nl; |
---|
| 65 | for ( int j; s -~= c ) { sout | j; } sout | nl; |
---|
| 66 | |
---|
| 67 | for ( int j; s ~ c ~ i ) { sout | j; } sout | nl; |
---|
| 68 | for ( int j; s ~= c ~ i ) { sout | j; } sout | nl; |
---|
| 69 | for ( int j; s -~ c ~ i ) { sout | j; } sout | nl; |
---|
| 70 | for ( int j; s -~= c ~ i ) { sout | j; } sout | nl; |
---|
| 71 | |
---|
| 72 | for ( j; s ~ @ ) { if ( j == 10 ) break; sout | j; } sout | nl; |
---|
[283876d] | 73 | for ( j; @ -~ s ) { if ( j == -10 ) break; sout | j; } sout | nl; |
---|
[c48b61c] | 74 | for ( j; s ~ @ ~ i ) { if ( j == 10 ) break; sout | j; } sout | nl; |
---|
[283876d] | 75 | for ( j; @ -~ s ~ i ) { if ( j == -10 ) break; sout | j; } sout | nl; |
---|
[c48b61c] | 76 | for ( j; s ~ @ ~ @ ) { if ( j == 10 ) break; sout | j; j += 1; } sout | nl; |
---|
| 77 | |
---|
| 78 | for ( int j; s ~ @ ) { if ( j == 10 ) break; sout | j; j += 1; } sout | nl; |
---|
[283876d] | 79 | for ( int j; @ -~ s ) { if ( j == -10 ) break; sout | j; j -= 1; } sout | nl; |
---|
[c48b61c] | 80 | for ( int j; s ~ @ ~ i ) { if ( j == 10 ) break; sout | j; } sout | nl; |
---|
[283876d] | 81 | for ( int j; @ -~ s ~ i ) { if ( j == -10 ) break; sout | j; } sout | nl; |
---|
[c48b61c] | 82 | for ( int j; s ~ @ ~ @ ) { if ( j == 10 ) break; sout | j; j += 1; } sout | nl; |
---|
[091ccdb] | 83 | |
---|
[8789ae4] | 84 | enum(int) E { A, B, C, D }; |
---|
| 85 | // for ( E e; A ~= C ) { sout | e; } sout | nl; |
---|
| 86 | // for ( e; A ~= D ) { sout | e; } sout | nl; |
---|
| 87 | // for ( e; A -~= D ~ 2 ) { sout | e; } sout | nl; |
---|
| 88 | for ( e; E ) { sout | e; } sout | nl; |
---|
| 89 | for ( e; -~= E ) { sout | e; } sout | nl; |
---|
[091ccdb] | 90 | } |
---|
| 91 | |
---|
[b6ad601] | 92 | struct S { int i, j; }; |
---|
| 93 | void ?{}( S & s ) { s.[i, j] = 0; } |
---|
| 94 | void ?{}( S & s, int i ) { s.[i, j] = [i, 0]; } |
---|
| 95 | void ?{}( S & s, int i, int j ) { s.[i, j] = [i, j]; } |
---|
| 96 | void ?{}( S & s, zero_t ) { s.[i, j] = 0; } |
---|
| 97 | void ?{}( S & s, one_t ) { s.[i, j] = 1; } |
---|
[8789ae4] | 98 | int ?<?( S s1, S s2 ) { return s1.i < s2.i || s1.j < s2.j; } |
---|
| 99 | int ?<=?( S s1, S s2 ) { return s1.i <= s2.i || s1.j <= s2.j; } |
---|
| 100 | int ?>?( S s1, S s2 ) { return s1.i > s2.i || s1.j > s2.j; } |
---|
| 101 | int ?>=?( S s1, S s2 ) { return s1.i >= s2.i || s1.j >= s2.j; } |
---|
| 102 | S ?+=?( S & s1, S s2 ) { s1.i += s2.i; s1.j += s2.j; return s1; } |
---|
| 103 | S ?+=?( S & s, one_t ) { s.i += 1; s.j += 1; return s; } |
---|
| 104 | S ?-=?( S & s1, S s2 ) { s1.i -= s2.i; s1.j -= s2.j; return s1; } |
---|
| 105 | S ?-=?( S & s, one_t ) { s.i -= 1; s.j -= 1; return s; } |
---|
| 106 | ofstream & ?|?( ofstream & os, S s ) { return os | '(' | s.i | s.j | ')'; } |
---|
| 107 | void & ?|?( ofstream & os, S s ) { (ofstream &)(os | s); ends( os ); } |
---|
[b6ad601] | 108 | |
---|
| 109 | int main() { |
---|
[091ccdb] | 110 | // Test some loop options. |
---|
| 111 | |
---|
[200fcb3] | 112 | sout | nlOff; // turn off auto newline |
---|
| 113 | while () { sout | "empty"; break; } sout | nl; |
---|
| 114 | do { sout | "empty"; break; } while (); sout | nl; |
---|
[5ea5b28] | 115 | for () { sout | "empty"; break; } sout | nl | nl; |
---|
[b6ad601] | 116 | |
---|
[200fcb3] | 117 | for ( 0 ) { sout | "A"; } sout | "zero" | nl; |
---|
| 118 | for ( 1 ) { sout | "A"; } sout | nl; |
---|
| 119 | for ( 10 ) { sout | "A"; } sout | nl; |
---|
[5c98a25] | 120 | for ( ~= 10 ) { sout | "A"; } sout | nl; |
---|
[200fcb3] | 121 | for ( 1 ~= 10 ~ 2 ) { sout | "B"; } sout | nl; |
---|
[c48b61c] | 122 | for ( 1 -~= 10 ~ 2 ) { sout | "C"; } sout | nl; |
---|
[200fcb3] | 123 | for ( 0.5 ~ 5.5 ) { sout | "D"; } sout | nl; |
---|
[c48b61c] | 124 | for ( 0.5 -~ 5.5 ) { sout | "E"; } sout | nl | nl; |
---|
[b6ad601] | 125 | |
---|
[200fcb3] | 126 | for ( i; 10 ) { sout | i; } sout | nl; |
---|
[5c98a25] | 127 | for ( i; ~= 10 ) { sout | i; } sout | nl; |
---|
[200fcb3] | 128 | for ( i; 1 ~= 10 ~ 2 ) { sout | i; } sout | nl; |
---|
[c48b61c] | 129 | for ( i; 1 -~= 10 ~ 2 ) { sout | i; } sout | nl; |
---|
[200fcb3] | 130 | for ( i; 0.5 ~ 5.5 ) { sout | i; } sout | nl; |
---|
[c48b61c] | 131 | for ( i; 0.5 -~ 5.5 ) { sout | i; } sout | nl; |
---|
[200fcb3] | 132 | for ( ui; 2u ~= 10u ~ 2u ) { sout | ui; } sout | nl; |
---|
[c48b61c] | 133 | for ( ui; 2u -~= 10u ~ 2u ) { sout | ui; } sout | nl | nl; |
---|
[b6ad601] | 134 | |
---|
[67d4e37] | 135 | // @ means do nothing |
---|
| 136 | for ( i; 1 ~ @ ) { |
---|
| 137 | if ( i > 10 ) break; |
---|
| 138 | sout | i; |
---|
| 139 | } sout | nl; |
---|
[283876d] | 140 | for ( i; @ -~ 10 ) { |
---|
[67d4e37] | 141 | if ( i < 0 ) break; |
---|
| 142 | sout | i; |
---|
| 143 | } sout | nl; |
---|
[1b54b54] | 144 | for ( i; 2 ~ @ ~ 2 ) { |
---|
| 145 | if ( i > 10 ) break; |
---|
| 146 | sout | i; |
---|
| 147 | } sout | nl; |
---|
| 148 | for ( i; 2.1 ~ @ ~ @ ) { |
---|
| 149 | if ( i > 10.5 ) break; |
---|
| 150 | sout | i; |
---|
| 151 | i += 1.7; |
---|
| 152 | } sout | nl; |
---|
[283876d] | 153 | for ( i; @ -~ 10 ~ 2 ) { |
---|
[1b54b54] | 154 | if ( i < 0 ) break; |
---|
| 155 | sout | i; |
---|
| 156 | } sout | nl; |
---|
| 157 | for ( i; 12.1 ~ @ ~ @ ) { |
---|
| 158 | if ( i < 2.5 ) break; |
---|
| 159 | sout | i; |
---|
| 160 | i -= 1.7; |
---|
| 161 | } sout | nl | nl; |
---|
| 162 | |
---|
[61dafb8] | 163 | enum { N = 10 }; |
---|
[200fcb3] | 164 | for ( N ) { sout | "N"; } sout | nl; |
---|
| 165 | for ( i; N ) { sout | i; } sout | nl; |
---|
[283876d] | 166 | for ( i; ~= N ) { sout | i; } sout | nl; |
---|
[c48b61c] | 167 | for ( i; -~= N ) { sout | i; } sout | nl | nl; |
---|
[61dafb8] | 168 | |
---|
[dc8511c] | 169 | const int start = 3, comp = 10, inc = 2; |
---|
[200fcb3] | 170 | for ( i; start ~ comp ~ inc + 1 ) { sout | i; } sout | nl | nl; |
---|
[b6ad601] | 171 | |
---|
[67d4e37] | 172 | for ( i; 10 : j; -5 ~ @ ) { sout | i | j; } sout | nl; |
---|
[283876d] | 173 | for ( i; 10 : j; @ -~ -5 ) { sout | i | j; } sout | nl; |
---|
[67d4e37] | 174 | for ( i; 10 : j; -5 ~ @ ~ 2 ) { sout | i | j; } sout | nl; |
---|
[283876d] | 175 | for ( i; 10 : j; @ -~ -5 ~ 2 ) { sout | i | j; } sout | nl | nl; |
---|
[67d4e37] | 176 | |
---|
| 177 | for ( j; -5 ~ @ : i; 10 ) { sout | i | j; } sout | nl; |
---|
[283876d] | 178 | for ( j; @ -~ -5 : i; 10 ) { sout | i | j; } sout | nl; |
---|
[67d4e37] | 179 | for ( j; -5 ~ @ ~ 2 : i; 10 ) { sout | i | j; } sout | nl; |
---|
[283876d] | 180 | for ( j; @ -~ -5 ~ 2 : i; 10 ) { sout | i | j; } sout | nl | nl; |
---|
[67d4e37] | 181 | |
---|
[283876d] | 182 | for ( j; @ -~ -5 ~ 2 : i; 10 : k; 1.5 ~ @ ) { sout | i | j | k; } sout | nl; |
---|
| 183 | for ( j; @ -~ -5 ~ 2 : k; 1.5 ~ @ : i; 10 ) { sout | i | j | k; } sout | nl; |
---|
| 184 | for ( k; 1.5 ~ @ : j; @ -~ -5 ~ 2 : i; 10 ) { sout | i | j | k; } sout | nl; |
---|
[c48b61c] | 185 | |
---|
[8789ae4] | 186 | for ( S s = (S){0}; s < (S){10,10}; s += (S){1} ) { sout | s; } sout | nl; // 0 does not work |
---|
[ff36907] | 187 | for ( s; (S){10,10} ) { sout | s; } sout | nl; |
---|
| 188 | sout | nl; |
---|
| 189 | for ( s; (S){0} ~ (S){10,10} ) { sout | s; } sout | nl; |
---|
| 190 | for ( s; (S){0} ~ (S){10,10} ~ (S){1} ) { sout | s; } sout | nl; |
---|
| 191 | for ( s; (S){0} ~= (S){10,10} ) { sout | s; } sout | nl; |
---|
| 192 | for ( s; (S){0} ~= (S){10,10} ~ (S){1} ) { sout | s; } sout | nl; |
---|
| 193 | sout | nl; |
---|
| 194 | for ( s; (S){0} -~ (S){10,10} ) { sout | s; } sout | nl; |
---|
| 195 | for ( s; (S){0} -~ (S){10,10} ~ (S){1} ) { sout | s; } sout | nl; |
---|
| 196 | for ( s; (S){0} -~= (S){10,10} ) { sout | s; } sout | nl; |
---|
| 197 | for ( s; (S){0} -~= (S){10,10} ~ (S){1} ) { sout | s; } sout | nl | nl; |
---|
| 198 | |
---|
[8789ae4] | 199 | int i = 10; |
---|
| 200 | double d = 10.; |
---|
| 201 | char c = 'e'; |
---|
| 202 | S s = { 7 }; |
---|
| 203 | |
---|
| 204 | for ( anon; 3 ~ i ) sout | anon; sout | nl; |
---|
| 205 | for ( anon; 3 ~ i ) sout | anon; sout | nl; |
---|
| 206 | for ( anon; 3 -~ i ) sout | anon; sout | nl; |
---|
| 207 | for ( anon; 3.5 ~ d ) sout | anon; sout | nl; |
---|
| 208 | for ( anon; 'a' ~= c ) sout | anon; sout | nl; |
---|
| 209 | for ( anon; 'a' -~= c ) sout | anon; sout | nl; |
---|
| 210 | for ( anon; (S){0} ~ s ) sout | anon; sout | nl; // 0 does not work |
---|
| 211 | for ( anon; (S){1} ~ s ) sout | anon; sout | nl; // 1 does not work |
---|
| 212 | for ( anon; (S){3} ~ s ) sout | anon; sout | nl; |
---|
| 213 | for ( anon; (S){3} -~ s ) sout | anon; sout | nl | nl; |
---|
| 214 | |
---|
[c48b61c] | 215 | fred(); |
---|
[b6ad601] | 216 | } |
---|
| 217 | |
---|
| 218 | // Local Variables: // |
---|
| 219 | // tab-width: 4 // |
---|
[dc8511c] | 220 | // compile-command: "cfa loopctrl.cfa" // |
---|
[b6ad601] | 221 | // End: // |
---|