Commits

Robert Smith committed 803d9e5

* Add PERM-INVERSE-EVAL and PERM-INVERSE-EVAL* for efficient
evaluation on inverses.
* Clarify PERM-EVAL* documentation.

Comments (0)

Files changed (3)

 perm-ref : zero-based reference
 perm-eval : one-based (standard) reference
 perm-eval* : one-based (standard) reference with out-of-bounds handling
+perm-inverse-eval : one-based (standard) reference of inverse
+perm-inverse-eval* : one-based (standard) reference of inverse with out-of-bounds handling
 perm-size : the size of the permutation (number of mapped elements)
 perm-length : number of inversions
 perm-even-p |
    #:perm-ref
    #:perm-eval
    #:perm-eval*
+   #:perm-inverse-eval
+   #:perm-inverse-eval*
    #:perm-size
    #:perm-length
    #:perm-even-p
   (aref (perm.spec perm) n))
 
 (defun perm-eval* (perm n)
-  "Evaluate the permutation PERM at index N."
+  "Evaluate the permutation PERM at index N. If N is larger than the
+size of the permutation, return the fixed point."
   (assert (<= 1 n)
           (n)
           "Permutation index of ~D must be greater than 1."
       n
       (aref (perm.spec perm) n)))
 
+(defun perm-inverse-eval (perm n)
+  "Evaluate the inverse of the permutation PERM at index N."
+  (assert (<= 1 n (perm-size perm))
+          (n)
+          "Permutation index of ~D must be within 1 and the length of the ~
+           permutation ~A."
+          n perm)
+  (position n (perm.spec perm)))
+
+(defun perm-inverse-eval* (perm n)
+  "Evaluate the inverse of the permutation PERM at index N. If N is
+larger than the size of the permutation, return the fixed point."
+  (assert (<= 1 n)
+          (n)
+          "Permutation index of ~D must be greater than 1."
+          n)
+  (if (> n (perm-size perm))
+      n
+      (position n (perm.spec perm))))
+
 (defun perm-size (perm)
   "The size of a permutation PERM."
   (1- (length (perm.spec perm))))