home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
vis-ftp.cs.umass.edu
/
vis-ftp.cs.umass.edu.tar
/
vis-ftp.cs.umass.edu
/
pub
/
Software
/
ASCENDER
/
ascendMar8.tar
/
UMass
/
ISR
/
lispm.lisp
< prev
next >
Wrap
Text File
|
1995-04-11
|
8KB
|
230 lines
; -*-lisp-mode-*-
;--------------------------------------------------------
; LISPM.LISP - lisp machine extras
; Robert Heller Created on Wed Oct 2 08:59:01 1991
; Last mod -
;--------------------------------------------------------
; Contents:
;--------------------------------------------------------
; (c) Copyright 1986 by The University of Massachusetts
;--------------------------------------------------------
(in-package "LISPM")
(shadow '(find find-if find-if-not) (find-package 'lispm))
;; FIND FIND-IF and FIND-IF-NOT
(eval-when (compile)
;; in LR processing, the leftmost item satisfying the test is returned. The search loop
;; is exited immediately upon finding such an item.
(Defmacro WITH-FIND-LIST-BINDINGS (&BODY body)
`(LET* (thing-found
pos
(start (IF start (MAX 0 start) 0))
(len (LENGTH list))
(end (IF end (MIN end len) len))
(list (NTHCDR start list)))
(PROGN . ,body)
(VALUES thing-found pos)))
(Defmacro FIND-LIST-LR-BODY (loopvar pred)
`(DO ((,loopvar list (CDR ,loopvar))
(loopcnt start (1+ loopcnt)))
((>= loopcnt end))
(WHEN ,pred
(SETQ thing-found (CAR ,loopvar)
pos loopcnt)
(RETURN))
))
(Defmacro FIND-LIST-LR-MACRO ()
`(COND (key
(IF test-not
(Find-LIST-LR-BODY z (NOT (FUNCALL test-not item (FUNCALL key (CAR z)))))
(Find-LIST-LR-BODY z (FUNCALL test item (FUNCALL key (CAR z))))))
(test-not
(Find-LIST-LR-BODY z (NOT (FUNCALL test-not item (CAR z)))))
(t
(Find-LIST-LR-BODY z (FUNCALL test item (CAR z))))))
(Defmacro FIND-IF-LIST-LR-MACRO ()
`(IF key
(Find-LIST-LR-BODY z (FUNCALL pred (FUNCALL key (CAR z))))
(Find-LIST-LR-BODY z (FUNCALL pred (CAR z)))))
(Defmacro FIND-IF-NOT-LIST-LR-MACRO ()
`(IF key
(Find-LIST-LR-BODY z (NOT (FUNCALL pred (FUNCALL key (CAR z)))))
(Find-LIST-LR-BODY z (NOT (FUNCALL pred (CAR z))))))
)
(eval-when (compile)
;; in RL processing, the rightmost item satisfying the test is sought. The list, however,
;; is searched left-to-right and instead of exiting when finding the item, we simply record
;; the item and its position. The values returned then correspond to the rightmost such item
;; in the list.
(Defmacro FIND-LIST-RL-BODY (loopvar pred)
`(DO ((,loopvar list (CDR ,loopvar))
(loopcnt start (1+ loopcnt)))
((>= loopcnt end))
(WHEN ,pred
(SETQ thing-found (CAR ,loopvar)
pos loopcnt)
)))
(Defmacro FIND-LIST-RL-MACRO ()
`(COND (key
(IF test-not
(Find-LIST-RL-BODY z (NOT (FUNCALL test-not item (FUNCALL key (CAR z)))))
(Find-LIST-RL-BODY z (FUNCALL test item (FUNCALL key (CAR z))))))
(test-not
(Find-LIST-RL-BODY z (NOT (FUNCALL test-not item (CAR z)))))
(t
(Find-LIST-RL-BODY z (FUNCALL test item (CAR z))))))
(Defmacro FIND-IF-LIST-RL-MACRO ()
`(IF key
(Find-LIST-RL-BODY z (FUNCALL pred (FUNCALL key (CAR z))))
(Find-LIST-RL-BODY z (FUNCALL pred (CAR z)))))
(Defmacro FIND-IF-NOT-LIST-RL-MACRO ()
`(IF key
(Find-LIST-RL-BODY z (NOT (FUNCALL pred (FUNCALL key (CAR z)))))
(Find-LIST-RL-BODY z (NOT (FUNCALL pred (CAR z))))))
)
(Defun FIND-LIST (item list &OPTIONAL (test #'EQL) key test-not start end from-end)
(WITH-Find-LIST-BINDINGS
(IF from-end (FIND-LIST-RL-MACRO) (FIND-LIST-LR-MACRO))))
(Defun FIND-IF-LIST (pred list &OPTIONAL key start end from-end)
(WITH-FIND-LIST-BINDINGS
(IF from-end (FIND-IF-LIST-RL-MACRO) (FIND-IF-LIST-LR-MACRO))))
(Defun FIND-IF-NOT-LIST (pred list &OPTIONAL key start end from-end)
(WITH-FIND-LIST-BINDINGS
(IF from-end (FIND-IF-NOT-LIST-RL-MACRO)(FIND-IF-NOT-LIST-LR-MACRO))))
(eval-when (compile)
(Defmacro WITH-FIND-VECTOR-BINDINGS (&BODY body)
`(LET* (thing-found
pos
(start (IF start (MAX 0 start) 0))
(len (LENGTH vector))
(end (IF end (MIN end len) len)))
(PROGN . ,body)
(VALUES thing-found pos)))
(Defmacro FIND-VECTOR-LR-BODY (loopvar pred)
`(DO ((,loopvar start (1+ ,loopvar)))
((>= ,loopvar end))
(WHEN ,pred
(SETQ thing-found (AREF vector ,loopvar)
pos ,loopvar)
(RETURN))
))
(Defmacro FIND-VECTOR-LR-MACRO ()
`(COND (key
(IF test-not
(FIND-VECTOR-LR-BODY z (NOT (FUNCALL test-not item (FUNCALL key (AREF vector Z)))))
(FIND-VECTOR-LR-BODY z (FUNCALL test item (FUNCALL key (AREF vector Z))))))
(test-not
(FIND-VECTOR-LR-BODY z (NOT (FUNCALL test-not item (AREF vector Z)))))
(t
(FIND-VECTOR-LR-BODY z (FUNCALL test item (AREF vector Z))))))
(Defmacro FIND-IF-VECTOR-LR-MACRO ()
`(IF key
(FIND-VECTOR-LR-BODY z (FUNCALL pred (FUNCALL key (AREF vector Z))))
(FIND-VECTOR-LR-BODY z (FUNCALL pred (AREF vector Z)))))
(Defmacro FIND-IF-NOT-VECTOR-LR-MACRO ()
`(IF key
(FIND-VECTOR-LR-BODY z (NOT (FUNCALL pred (FUNCALL key (AREF vector Z)))))
(FIND-VECTOR-LR-BODY z (NOT (FUNCALL pred (AREF vector Z))))))
)
(eval-when(compile)
(Defmacro FIND-VECTOR-RL-BODY (loopvar pred)
`(DO ((,loopvar (1- end) (1- ,loopvar)))
((< ,loopvar start))
(WHEN ,pred
(SETQ thing-found (aref vector ,loopvar)
pos ,loopvar)
(RETURN))
))
(Defmacro FIND-VECTOR-RL-MACRO ()
`(COND (key
(IF test-not
(FIND-VECTOR-RL-BODY z (NOT (FUNCALL test-not item (FUNCALL key (AREF vector Z)))))
(FIND-VECTOR-RL-BODY z (FUNCALL test item (FUNCALL key (AREF vector Z))))))
(test-not
(FIND-VECTOR-RL-BODY z (NOT (FUNCALL test-not item (AREF vector Z)))))
(t
(FIND-VECTOR-RL-BODY z (FUNCALL test item (AREF vector Z))))))
(Defmacro FIND-IF-VECTOR-RL-MACRO ()
`(IF key
(FIND-VECTOR-RL-BODY z (FUNCALL pred (FUNCALL key (AREF vector Z))))
(FIND-VECTOR-RL-BODY z (FUNCALL pred (AREF vector Z)))))
(Defmacro FIND-IF-NOT-VECTOR-RL-MACRO ()
`(IF key
(FIND-VECTOR-RL-BODY z (NOT (FUNCALL pred (FUNCALL key (AREF vector Z)))))
(FIND-VECTOR-RL-BODY z (NOT (FUNCALL pred (AREF vector Z))))))
)
(Defun FIND-VECTOR (item vector &OPTIONAL (test #'EQL) key test-not start end from-end)
(WITH-Find-VECTOR-BINDINGS
(IF from-end
(FIND-VECTOR-RL-MACRO)
(FIND-VECTOR-LR-MACRO))))
(Defun FIND-IF-VECTOR (pred vector &OPTIONAL key start end from-end)
(WITH-FIND-VECTOR-BINDINGS
(IF from-end
(FIND-IF-VECTOR-RL-MACRO)
(FIND-IF-VECTOR-LR-MACRO))))
(Defun FIND-IF-NOT-VECTOR (pred vector &OPTIONAL key start end from-end)
(WITH-FIND-VECTOR-BINDINGS
(IF from-end
(FIND-IF-NOT-VECTOR-RL-MACRO)
(FIND-IF-NOT-VECTOR-LR-MACRO))))
(Defun FIND* (item sequence &OPTIONAL (test #'EQL) key test-not start end from-end)
(IF (ARRAYP sequence)
(FIND-VECTOR item sequence test key test-not start end from-end)
(FIND-LIST item sequence test key test-not start end from-end)))
(Defun FIND (item sequence &KEY key (test #'EQL) test-not start end from-end)
"Return first element of SEQUENCE that matches ITEM. Also returns the position
of the item."
(FIND* item sequence test key test-not start end from-end))
(Defun FIND-IF* (predicate sequence &OPTIONAL key start end from-end)
(IF (ARRAYP sequence)
(FIND-IF-VECTOR predicate sequence key start end from-end)
(FIND-IF-LIST predicate sequence key start end from-end)))
(Defun FIND-IF (predicate sequence &KEY key start end from-end)
"Return the first element of SEQUENCE that satisfies PREDICATE"
(FIND-IF* predicate sequence key start end from-end))
(Defun FIND-IF-NOT* (predicate sequence &OPTIONAL key start end from-end)
(IF (ARRAYP sequence)
(FIND-IF-NOT-VECTOR predicate sequence key start end from-end)
(FIND-IF-NOT-LIST predicate sequence key start end from-end)))
(Defun FIND-IF-NOT (predicate sequence &KEY key start end from-end)
"Return the first element of SEQUENCE that doesn't satisfy PREDICATE"
(FIND-IF-NOT* predicate sequence key start end from-end))