Changeset d0296db6
- Timestamp:
- Oct 29, 2024, 12:45:42 PM (41 hours ago)
- Branches:
- master
- Children:
- 7ef4438
- Parents:
- 33474e6
- Location:
- doc/theses/mike_brooks_MMath
- Files:
-
- 2 added
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
doc/theses/mike_brooks_MMath/array.tex
r33474e6 rd0296db6 260 260 \lstinput{10-15}{hello-accordion.cfa} 261 261 This structure's layout has the starting offset of @studentIds@ varying according to the generic parameter @C@, and the offset of @preferences@ varying according to both generic parameters. 262 For a function that operates on a @School@ structure, the type system handles this memory layout transparently. 263 \lstinput{40-45}{hello-accordion.cfa} 262 263 The school example has the data structure capturing many students' course-preference forms. 264 It has course- and student-level metadata (their respective display names) and a position-based preferecens' matrix. 265 The input files in \VRef[Figure]{f:checkHarness} give example data. 266 267 When a function operates on a @School@ structure, the type system handles its memory layout transparently. 268 \lstinput{30-37}{hello-accordion.cfa} 269 In the running example, this @getPref@ function answers, 270 for the student at position @sIx@, what is the position of its @pref@\textsuperscript{th}-favoured class? 271 264 272 \VRef[Figure]{f:checkHarness} shows the @School@ harness and results with different array sizes. 273 This example program prints the courses in each student's preferred order, all using the looked-up display names. 265 274 Note the declaration of the @school@ variable. 266 275 It is on the stack and its initialization does not use any casting or size arithmetic. … … 277 286 \begin{tabular}{@{}ll@{\hspace{25pt}}l@{}} 278 287 \begin{tabular}{@{}p{3.25in}@{}} 279 \lstinput{ 60-64}{hello-accordion.cfa}288 \lstinput{50-55}{hello-accordion.cfa} 280 289 \vspace*{-3pt} 281 \lstinput{ 73-80}{hello-accordion.cfa}290 \lstinput{90-98}{hello-accordion.cfa} 282 291 \end{tabular} 283 292 & 284 293 \raisebox{0.32\totalheight}{% 285 \lstinput{85-93}{hello-accordion.cfa}286 294 }% 287 295 & 288 \lstinput{95-109}{hello-accordion.cfa}289 296 \end{tabular} 290 \caption{\lstinline{school} Harness and Output} 297 298 TODO: Get Peter's layout help 299 300 \$ cat school1 301 302 \lstinput{}{school1} 303 304 \$ ./a.out < school1 305 306 \lstinput{}{school1.out} 307 308 \$ cat school2 309 310 \lstinput{}{school2} 311 312 \$ ./a.out < school2 313 314 \lstinput{}{school2.out} 315 316 \caption{\lstinline{School} harness, input and output} 291 317 \label{f:checkHarness} 292 318 \end{figure} -
doc/theses/mike_brooks_MMath/programs/hello-accordion.cfa
r33474e6 rd0296db6 2 2 #include <stdlib.hfa> 3 3 #include <array.hfa> 4 #include < locale.h> // setlocale4 #include <string.hfa> 5 5 6 6 … … 8 8 9 9 10 forall( [C], [S] ) $\C{// Class size, Students in class}$10 forall( [C], [S] ) $\C{// num courses, num students}$ 11 11 struct School { 12 @array ( int, C )@ classIds; $\C{// nested VLAs}$13 @array ( int, S )@ studentIds;14 @array ( int, C, S )@preferences; $\C{// multidimensional}$12 @array@( string, C ) course_codes; $\C{// nested VLAs}$ 13 @array@( string, S ) student_ids; 14 @array@( int, C, S ) preferences; $\C{// multidimensional}$ 15 15 }; 16 17 18 19 // TODO: understand (fix?) why these are needed (autogen seems to be failing ... is typeof as struct member nayok?)20 21 forall( [C], [S] )22 void ?{}( School( C, S ) & this ) {}23 24 forall( [C], [S] )25 void ^?{}( School( C, S ) & this ) {}26 16 27 17 … … 39 29 40 30 forall( [C], [S] ) 41 void put( @School( C, S ) & s@, int class, int student, int pref ) { 42 s.classIds[class] = class; $\C{// fields' offsets are dynamic }$ 43 s.studentIds[student] = student; 44 s.preferences[class][student] = pref; 31 int getPref( @School( C, S ) & school@, int is, int pref ) { 32 for ( ic; C ) { 33 int curPref = @school.preferences@[ic][is]; $\C{// offset calculation implicit}$ 34 if ( curPref == pref ) return ic; 35 } 36 assert( false ); 45 37 } 46 38 … … 56 48 57 49 50 int main() { 51 int nc, ns; 52 sin | nc | " courses,"; 53 sin | ns | " students"; 54 @School( nc, ns ) school;@ 55 // ... elided: read data into school 58 56 59 57 60 int main() { 61 int classes, students; 62 sin | classes | students; 63 @School( classes, students ) school;@ 64 // elided: read data into school, calling put 65 { int class, student, preference; 66 // for each student's class/preferences 67 try { 68 for ( ) { 69 sin | class | student | preference; 70 put( school, class, student, preference ); 71 } 72 } catch( end_of_file * ) {} } 73 for ( s; students ) { 74 sout | "student" | s | nonl; 75 for ( c; classes ) { 76 sout | school.preferences[c][s] | nonl; 58 59 60 { string sv; 61 int iv; 62 // headers' row 63 sin | "\nc\\s"; 64 for ( is ; ns ) { 65 // column label 66 sin | sv; 67 school.student_ids[is] = sv; 68 } 69 // body rows 70 for ( ic ; nc ) { 71 // row label 72 sin | sv; 73 school.course_codes[ic] = sv; 74 for ( is ; ns ) { 75 // matrix item 76 sin | iv; 77 school.preferences[ic][is] = iv; 78 } 79 } 80 } 81 82 83 84 85 86 87 88 89 90 for ( is; ns ) { 91 sout | school.student_ids[is] | ": " | nonl; 92 for ( pref; 1 ~= nc ) { 93 int ic = getPref(school, is, pref); 94 sout | school.course_codes[ ic ] | nonl; 77 95 } 78 96 sout | nl; … … 81 99 82 100 83 84 /*85 $\$$ cat school186 2 287 0 0 188 1 0 789 0 1 1290 1 1 1391 $\$$ a.out < school192 student 0 1 793 student 1 12 1394 95 $\$$ cat school296 3 397 0 0 198 1 0 799 2 0 8100 0 1 12101 1 1 13102 2 1 14103 0 2 26104 1 2 27105 2 2 28106 $\$$ a.out < school2107 student 0 1 7 8108 student 1 12 13 14109 student 2 26 27 28110 */111 112 101 // Local Variables: // 113 102 // compile-command: "sed -f sedcmd hello-accordion.cfa > ../build/tmp.cfa; cfa ../build/tmp.cfa -Wall -Wextra" // -
doc/theses/mike_brooks_MMath/programs/school1
r33474e6 rd0296db6 1 2 2 2 0 0 1 3 1 0 7 4 0 1 12 5 1 1 13 1 3 courses, 2 students 2 c\s 90111111 90222222 3 ENGL101 3 2 4 PHYS101 1 3 5 CHEM101 2 1 -
doc/theses/mike_brooks_MMath/programs/school2
r33474e6 rd0296db6 1 3 3 2 0 0 1 3 1 0 7 4 2 0 8 5 0 1 12 6 1 1 13 7 2 1 14 8 0 2 26 9 1 2 27 10 2 2 28 1 5 courses, 3 students 2 c\s 90111111 90222222 90333333 3 ENGL101 3 2 3 4 PHYS101 1 3 4 5 CHEM101 2 1 5 6 PHIL101 4 5 2 7 MATH499 5 4 1
Note: See TracChangeset
for help on using the changeset viewer.