2 ;;; Copyright (c) 2005--2007, by A.J. Rossini <blindglobe@gmail.com>
3 ;;; See COPYRIGHT file for any additional restrictions (BSD license).
4 ;;; Since 1991, ANSI was finally finished. Edited for ANSI Common Lisp.
6 ;;;; fastmap -- Fast version of MAP
8 ;;;; Copyright (c) 1991, by Luke Tierney. Permission is granted for
11 ;;;; The FASTMAP function is a version of MAP that is assumed to
13 ;;;; a) be reasonable fast on any combination of lists and vectors
16 ;;;; b) not hang if at least one of its arguments is not a circular
19 ;;;; This function is the core of the vectorized arithmetic system, so it
20 ;;;; may be worth optimizing for each CL implementation. I tried to improve
21 ;;;; it for (A)KCL but have not yet been able to obtain a significant
22 ;;;; increase in speed.
28 (defpackage :lisp-stat-fastmap
32 (in-package :lisp-stat-fastmap
)
40 Returns NIL unless X is a list or vector."
41 (or (listp x
) (vectorp x
)))
43 (defun cdr-lists (args)
44 (do ((list args
(cdr list
)))
46 (if (consp (car list
)) (rplaca list
(cdar list
)))))
48 (defun get-result-size (args)
49 (macrolet ((any-nulls (ls) `(dolist (x ,ls
) (if (null x
) (return t
)))))
55 (setf n
(if n
(min n
(length x
)) (length x
)))))
60 (if (or (<= n m
) (any-nulls lists
)) (return))
67 (if (any-nulls lists
) (return))
73 (defun fastmap (type fcn
&rest args
)
74 (cond ((and (eq type
'list
) (every #'listp args
))
75 (apply #'mapcar fcn args
))
76 ((and (eq type
'vector
) (every #'vectorp args
))
77 (apply #'map
'vector fcn args
))
78 ((every #'sequencep args
)
79 (let* ((n (get-result-size args
))
80 (result (make-sequence type n
))
81 (farg (make-list (length args
))))
83 (macrolet ((fill-arglist ()
84 `(do ((f farg
(cdr f
))
96 (rplaca r
(apply fcn farg
))
102 (setf (aref result i
) (apply fcn farg
))
103 (cdr-lists args
))))))
104 (t (error "not all sequences"))))