Robert Smith avatar 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))))
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.