Changeset d0296db6 for doc/theses


Ignore:
Timestamp:
Oct 29, 2024, 12:45:42 PM (7 weeks ago)
Author:
Michael Brooks <mlbrooks@…>
Branches:
master
Children:
7ef4438
Parents:
33474e6
Message:

Thesis, array, upgrade accordion demo to use data in all arrays

Location:
doc/theses/mike_brooks_MMath
Files:
2 added
4 edited

Legend:

Unmodified
Added
Removed
  • doc/theses/mike_brooks_MMath/array.tex

    r33474e6 rd0296db6  
    260260\lstinput{10-15}{hello-accordion.cfa}
    261261This 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
     263The school example has the data structure capturing many students' course-preference forms.
     264It has course- and student-level metadata (their respective display names) and a position-based preferecens' matrix.
     265The input files in \VRef[Figure]{f:checkHarness} give example data.
     266
     267When a function operates on a @School@ structure, the type system handles its memory layout transparently.
     268\lstinput{30-37}{hello-accordion.cfa}
     269In the running example, this @getPref@ function answers,
     270for the student at position @sIx@, what is the position of its @pref@\textsuperscript{th}-favoured class?
     271
    264272\VRef[Figure]{f:checkHarness} shows the @School@ harness and results with different array sizes.
     273This example program prints the courses in each student's preferred order, all using the looked-up display names.
    265274Note the declaration of the @school@ variable.
    266275It is on the stack and its initialization does not use any casting or size arithmetic.
     
    277286\begin{tabular}{@{}ll@{\hspace{25pt}}l@{}}
    278287\begin{tabular}{@{}p{3.25in}@{}}
    279 \lstinput{60-64}{hello-accordion.cfa}
     288\lstinput{50-55}{hello-accordion.cfa}
    280289\vspace*{-3pt}
    281 \lstinput{73-80}{hello-accordion.cfa}
     290\lstinput{90-98}{hello-accordion.cfa}
    282291\end{tabular}
    283292&
    284293\raisebox{0.32\totalheight}{%
    285 \lstinput{85-93}{hello-accordion.cfa}
    286294}%
    287295&
    288 \lstinput{95-109}{hello-accordion.cfa}
    289296\end{tabular}
    290 \caption{\lstinline{school} Harness and Output}
     297
     298TODO: 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}
    291317\label{f:checkHarness}
    292318\end{figure}
  • doc/theses/mike_brooks_MMath/programs/hello-accordion.cfa

    r33474e6 rd0296db6  
    22#include <stdlib.hfa>
    33#include <array.hfa>
    4 #include <locale.h>                                                     // setlocale
     4#include <string.hfa>
    55
    66
     
    88
    99
    10 forall( [C], [S] ) $\C{// Class size, Students in class}$
     10forall( [C], [S] ) $\C{// num courses, num students}$
    1111struct 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}$
    1515};
    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 ) {}
    2616
    2717
     
    3929
    4030forall( [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;
     31int 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 );
    4537}
    4638
     
    5648
    5749
     50int main() {
     51        int nc, ns;
     52        sin | nc | " courses,";
     53        sin | ns | " students";
     54        @School( nc, ns ) school;@
     55        // ... elided: read data into school
    5856
    5957
    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;
    7795                }
    7896                sout | nl;
     
    8199
    82100
    83 
    84 /*
    85 $\$$ cat school1
    86 2 2
    87 0 0 1
    88 1 0 7
    89 0 1 12
    90 1 1 13
    91 $\$$ a.out < school1
    92 student 0 1 7
    93 student 1 12 13
    94 
    95 $\$$ cat school2
    96 3 3
    97 0 0 1
    98 1 0 7
    99 2 0 8
    100 0 1 12
    101 1 1 13
    102 2 1 14
    103 0 2 26
    104 1 2 27
    105 2 2 28
    106 $\$$ a.out < school2
    107 student 0 1 7 8
    108 student 1 12 13 14
    109 student 2 26 27 28
    110 */
    111 
    112101// Local Variables: //
    113102// 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
     13 courses, 2 students
     2c\s     90111111 90222222
     3ENGL101        3        2
     4PHYS101        1        3
     5CHEM101        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
     15 courses, 3 students
     2c\s     90111111 90222222 90333333
     3ENGL101        3        2        3
     4PHYS101        1        3        4
     5CHEM101        2        1        5
     6PHIL101        4        5        2
     7MATH499        5        4        1
Note: See TracChangeset for help on using the changeset viewer.