utils/permute.lisp (demonstrate-permutation-network): Optionally show offsets.
authorMark Wooding <mdw@distorted.org.uk>
Sat, 3 Feb 2024 23:34:13 +0000 (23:34 +0000)
committerMark Wooding <mdw@distorted.org.uk>
Sat, 3 Feb 2024 23:35:55 +0000 (23:35 +0000)
Add an option to print a table showing how far each bit has still to
move.  This can be helpful for understanding some permutations.  Others,
not so much.

utils/permute.lisp

index ee81bb3..884068a 100644 (file)
 (defun demonstrate-permutation-network
     (n steps
      &key reference
+         offsets
          (stream *standard-output*))
   "Print, on STREAM, a demonstration of the permutation STEPS.
 
    earliest unpaired `#' marker.  The right hand side shows the arrangement
    of the original input bits.
 
+   If OFFSETS is non-nil, then also print a table of offsets showing how far
+   each bit has yet to move.
+
    If REFERENCE is not nil, then print a final pair of diagrams.  This time,
    the map shows `-' for correct bits and `x' for incorrect ones, with the
    right hand side showing the expected arrangement of input bits.
                                 stream)))
                 (write-string " | " stream)
 
+                (when offsets
+                  (dotimes (j mapwd)
+                    (let ((k (+ i j)))
+                      (when (plusp j) (write-char #\space stream))
+                      (cond ((>= k n)
+                             (format stream "~v@T" (1+ ixwd)))
+                            (t
+                             (format stream "~*~[~2:*~vD~:;~2:*~v@D~]"
+                                     (1+ ixwd)
+                                     (- (aref end-inv (aref v k)) k))))))
+                  (write-string " | " stream))
+
+                (dotimes (j (min mapwd (- n i)))
+                  (let ((k (+ i j)))
+                    (when (plusp j) (write-char #\space stream))
+                    (format stream "~vD" ixwd (aref v k))))
+                (terpri))))
+
        (fresh-line)
        (show-stage 0 0 v)