Subsections

Overlapping slices

What happens if two trimmed multiples overlap? For example, consider the program

   PROGRAM slices CONTEXT VOID
   USE standard
   BEGIN
      [4]INT r;

      PROC res = VOID:
      FOR n FROM LWB r TO UPB r
      DO r[n]:=n OD;

      PROC mpr = ([]INT m)VOID:
      (
         FOR i FROM LWB m TO UPB m
         DO
            print((whole(m[i],0),blank))
         OD;
         print(newline)
      ); #mpr#

      res;
      print("Original contents:");  mpr(r);

      r[:UPB r-1]:=r[1+LWB r:];
      print((newline,"r[:3]:=r[2:]",newline,
             "Compiler does it: "));  mpr(r);
      res;

      FOR i FROM LWB r TO UPB r-1
      DO r[i]:=r[i+1] OD;
      print("Forwards loop:    "); mpr(r);
      res;

      FOR i FROM UPB r-1 BY -1 TO LWB r
      DO r[i]:=r[i+1] OD;
      print("Backwards loop:   "); mpr(r);
      res;  r[1+LWB r:]:=r[:UPB r-1];
      print((newline,"r[2:]:=r[:3]",newline,
             "Compiler does it: "));  mpr(r);
      res;

      FOR i FROM 1+LWB r TO UPB r
      DO r[i]:=r[i-1] OD;
      print("Forwards loop:    "); mpr(r);
      res;

      FOR i FROM UPB r BY -1 TO 1+LWB r
      DO r[i]:=r[i-1] OD;
      print("Backwards loop:   "); mpr(r)
   END
   FINISH

When compiled and executed, the program gives the following output:

   Original contents:1 2 3 4 
   
   r[:3]:=r[2:]
   Compiler does it: 2 3 4 4 
   Forwards loop:    2 3 4 4 
   Backwards loop:   4 4 4 4 
   
   r[2:]:=r[:3]
   Compiler does it: 1 1 2 3 
   Forwards loop:    1 1 1 1 
   Backwards loop:   1 1 2 3

Notice that lines 5 and 8 of the results are definitely wrong, but that the compiler gets it right both times. The lesson is, do not worry about overlapping multiples: the compiler will ensure you get the effect you want.

A different matter is when you want to replace a column of a square multiple with a row. Here, the overlap is more of a “crossoverlap”. In this case you need to be careful--see the next exercise.


Exercises

11.6
Given a square two-dimensional multiple of integers, write a procedure which uses trimmers (not necessarily overlapping) to convert its columns to rows and its rows to columns. For example:
   ((1,2,3),         ((1,4,7),
    (4,5,6),   =>     (2,5,8),
    (7,8,9))          (3,6,9))
Your procedure should cater for any size of square multiple. Ans[*]


Sian Mountbatten 2012-01-19