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 ;;; compound -- Compound data and element-wise mapping functions
8 ;;; Copyright (c) 1991, by Luke Tierney. Permission is granted for
18 (defpackage :lisp-stat-compound-data
20 :lisp-stat-object-system
22 (:shadowing-import-from
:lisp-stat-object-system
24 call-next-method call-method
)
25 (:export compound-data-p
*compound-data-proto
*
27 compound-data-seq compound-data-length
28 element-list element-seq
30 recursive-map-elements map-elements repeat
32 get-next-element make-next-element set-next-element
33 sequencep iseq ordered-nneg-seq
34 select split-list which
37 (in-package :lisp-stat-compound-data
)
39 ;;; Sequences are part of ANSI CL, being a supertype of vector and
40 ;;; list (ordered set of things).
42 ;;; Need to use the interenal structure when possible -- silly to be
43 ;;; redundant! However, this means we need to understand what
44 ;;; sequences were intending to do, which I'm not clear on yet.
46 ;;; The original ordering, object-wise, was to have compound
47 ;;; functionality passed into sequences, into other data sources.
48 ;;; However, at this point, we will see about inverting this and
49 ;;; having basic data types pushed through compound, to simplify
50 ;;; packaging. In this vein, we have created a compound package to
51 ;;; contain the compound data and sequence structures. Probably need
52 ;;; to clean this up even more.
55 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
57 ;;; Internal Support Functions
59 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
62 "Predicate to determine if argument is compound. Most common
63 non-compound types are checked first."
64 (declare (inline numberp symbolp stringp consp arrayp array-total-size
))
65 (cond ((or (numberp x
) (symbolp x
) (stringp x
)) nil
)
66 ((or (consp x
) (and (arrayp x
) (< 0 (array-total-size x
)))) t
)
67 (t (compound-object-p x
))))
69 (defun find-compound-data (list)
70 "Returns first compound data item in LIST or NIL if there is none."
71 (dolist (x list
) (if (cmpndp x
) (return x
))))
73 (defun any-compound-elements (seq)
74 "Checks for a compound element."
75 (cond ((consp seq
) (dolist (x seq
) (if (cmpndp x
) (return x
))))
77 (let ((n (length seq
)))
81 (let ((x (aref seq i
)))
82 (if (cmpndp x
) (return x
))))))
83 (t (error "argument must be a list or vector"))))
85 (defun compound-data-sequence (x)
86 "Returns sequence of data values for X."
87 (declare (inline consp vectorp arrayp make-array array-total-size
))
89 ((or (consp x
) (vectorp x
)) x
)
90 ((arrayp x
) (make-array (array-total-size x
) :displaced-to x
))
91 (t (send x
:data-seq
))))
93 (defmacro sequence-type
(x) `(if (consp ,x
) 'list
'vector
))
95 (defun make-compound-data (shape sequence
)
96 "Construct a compound data item to match the shape of the first
98 (let ((n (length (compound-data-sequence shape
))))
99 (if (/= n
(length sequence
)) (error "compound data not the same shape"))
101 ((consp shape
) (if (consp sequence
) sequence
(coerce sequence
'list
)))
103 (if (vectorp sequence
) sequence
(coerce sequence
'vector
)))
105 (make-array (array-dimensions shape
)
106 :displaced-to
(coerce sequence
'vector
)))
107 (t (send shape
:make-data sequence
)))))
109 (defun make-circle (x)
110 "Make a circular list of one element."
111 (declare (inline cons rplacd
))
112 (let ((x (cons x nil
)))
116 (defun check-compound (x)
117 "Signals an error if X is not compound."
118 (if (not (cmpndp x
)) (error "not a compound data item - ~a" x
)))
120 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
122 ;;; MAP-ELEMENTS function
123 ;;; Applies a function to arguments. If all arguments are simple (i. e.
124 ;;; not compound) then MAP-ELEMENTS acts like funcall. Otherwise all
125 ;;; compound arguments must be of the same shape and simple arguments
126 ;;; are treated as if they were compound arguments of the appropriate
127 ;;; shape. This is implemented by replacin all simple arguments by
128 ;;; circular lists of one element.
130 ;;; This implementation uses FASTMAP, a version of MAP that is assumed
133 ;;; a) work reasonable fast on any combination of lists and vectors
136 ;;; b) not hang if at least one of its arguments is not a circular
139 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
141 (defun fixup-map-elements-arglist (args)
142 (do* ((args args
(rest args
))
143 (x (car args
) (car args
)))
145 (declare (inline car
))
147 (if (cmpndp x
) (compound-data-sequence x
) (make-circle x
)))))
149 (defun map-elements (fcn &rest args
)
150 "Args: (fcn &rest args)
151 Applies FCN elementwise. If no arguments are compound MAP-ELEMENTS
152 acts like FUNCALL. Compound arguments must all be the same shape. Non
153 compound arguments, in the presence of compound ones, are treated as
154 if they were of the same shape as the compound items with constant data
156 (let ((first-compound (find-compound-data args
)))
157 (cond ((null first-compound
) (apply fcn args
))
158 (t (fixup-map-elements-arglist args
)
159 (let* ((seq (compound-data-sequence first-compound
))
160 (type (sequence-type seq
)))
161 (make-compound-data first-compound
162 (apply #'map type fcn args
)))))))
164 (defun recursive-map-elements (base-fcn fcn
&rest args
)
165 "Args: (base-fcn fcn &rest args)
166 The same idea as MAP-ELEMENTS, except arguments are in a list and the
167 base and recursive cases can use different functions. Modified to check
168 for second level of compounding and use base-fcn if there is none."
169 (let ((first-compound (find-compound-data args
)))
170 (cond ((null first-compound
) (apply base-fcn args
))
171 (t (fixup-map-elements-arglist args
)
172 (let* ((seq (compound-data-sequence first-compound
))
173 (type (sequence-type seq
))
174 (f (if (any-compound-elements seq
) fcn base-fcn
)))
175 (make-compound-data first-compound
176 (apply #'map type f args
)))))))
179 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
181 ;;;; Public Predicate and Accessor Functions
183 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
185 (defun compound-data-p (x)
187 Returns T if X is a compound data item, NIL otherwise."
190 (defun compound-data-seq (x)
192 Returns data sequence in X."
194 (compound-data-sequence x
))
196 (defun compound-data-length (x)
198 Returns length of data sequence in X."
200 (length (compound-data-sequence x
)))
202 (defun compound-data-shape (x)
203 "Needed but undefined??"
207 (defun element-list (x)
210 (let ((x (concatenate 'list
(compound-data-seq x
)))) ; copies sequence
212 ((any-compound-elements x
)
213 (do ((next x
(rest next
)))
215 (setf (first next
) (element-list (first next
))))
216 (do ((result (first x
))
217 (last (last (first x
)))
218 (next (rest x
) (rest next
)))
219 ((not (consp next
)) result
)
220 (setf (rest last
) (first next
))
221 (setf last
(last (first next
)))))
225 (defun element-seq (x)
227 Returns sequence of the elements of compound item X."
229 (let ((seq (compound-data-seq x
)))
230 (if (any-compound-elements seq
) (element-list seq
) seq
)))
232 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
234 ;;;; Compound Data Objects
236 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
238 (defvar *compound-data-proto
*)
239 (defproto *compound-data-proto
*)
241 ;;; FIXME: These need to be defined!!
242 (defmeth *compound-data-proto
* :data-length
(&rest args
) nil
)
243 (defmeth *compound-data-proto
* :data-seq
(&rest args
) nil
)
244 (defmeth *compound-data-proto
* :make-data
(&rest args
) nil
)
245 (defmeth *compound-data-proto
* :select-data
(&rest args
) nil
)
247 (defun compound-object-p (x) (kind-of-p x
*compound-data-proto
*))
251 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
253 ;;;; Sorting Functions
255 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
259 Returns a sequence with the numbers or strings in the sequence X in order."
260 (flet ((less (x y
) (if (numberp x
) (< x y
) (string-lessp x y
))))
261 (stable-sort (copy-seq (compound-data-seq x
)) #'less
)))
265 Returns a sequence of the indices of elements in the sequence of numbers
266 or strings X in order."
267 (let* ((seq (compound-data-seq x
))
268 (type (if (consp seq
) 'list
'vector
))
270 (flet ((entry (x) (setf i
(+ i
1)) (list x i
))
274 (if (numberp x
) (< x y
) (string-lessp x y
)))))
275 (let ((sorted-seq (stable-sort (map type
#'entry seq
) #'less
)))
276 (map type
#'second sorted-seq
)))))
278 ;; this isn't destructive -- do we document destructive only, or any
282 Returns a sequence with the elements of the list or array of numbers or
283 strings X replaced by their ranks."
284 (let ((ranked-seq (order (order x
))))
286 ;; compound-data-shape is undefined?
287 (compound-data-shape x
) ranked-seq
)))
297 Repeats VALS. If TIMES is a number and VALS is a non-null, non-array atom,
298 a list of length TIMES with all elements eq to VALS is returned. If VALS
299 is a list and TIMES is a number then VALS is appended TIMES times. If
300 TIMES is a list of numbers then VALS must be a list of equal length and
301 the simpler version of repeat is mapped down the two lists.
302 Examples: (repeat 2 5) returns (2 2 2 2 2)
303 (repeat '(1 2) 3) returns (1 2 1 2 1 2)
304 (repeat '(4 5 6) '(1 2 3)) returns (4 5 5 6 6 6)
305 (repeat '((4) (5 6)) '(2 3)) returns (4 4 5 6 5 6 5 6)"
306 (cond ((compound-data-p b
)
307 (let* ((reps (coerce (compound-data-seq (map-elements #'repeat a b
))
309 (result (first reps
))
310 (tail (last (first reps
))))
311 (dolist (next (rest reps
) result
)
313 (setf (rest tail
) next
)
314 (setf tail
(last next
))))))
315 (t (let* ((a (if (compound-data-p a
)
316 (coerce (compound-data-seq a
) 'list
)
319 (dotimes (i b result
)
320 (let ((next (copy-list a
)))
321 (if result
(setf (rest (last next
)) result
))
322 (setf result next
)))))))
329 Returns a list of the indices where elements of sequence X are not NIL."
330 (let ((x (list (compound-data-seq x
)))
333 (flet ((add-result (x)
334 (if result
(setf (rest tail
) (list x
)) (setf result
(list x
)))
335 (setf tail
(if tail
(rest tail
) result
)))
336 (get-next-element (seq-list i
)
337 (cond ((consp (first seq-list
))
338 (let ((elem (first (first seq-list
))))
339 (setf (first seq-list
) (rest (first seq-list
)))
341 (t (aref (first seq-list
) i
)))))
342 (let ((n (length (first x
))))
343 (dotimes (i n result
)
344 (if (get-next-element x i
) (add-result i
)))))))
346 ;;; Type Checking Functions
348 (defun check-sequence (a)
349 ;; FIXME:AJR: does this handle consp as well? (Luke had an "or"
351 (if (not (typep a
'sequence
))
352 (error "not a sequence - ~s" a
)))
354 ;;; Sequence Element Access
357 ;;; (elt x i) -- NOT. This is more like "pop".
358 (defun get-next-element (x i
)
359 "Get element i from seq x. FIXME: not really??"
360 (let ((myseq (first x
)))
362 (let ((elem (first myseq
)))
363 (setf (first x
) (rest myseq
))
367 ;;; (setf (elt x i) v)
368 (defun set-next-element (x i v
)
369 (let ((seq (first x
)))
372 (setf (first x
) (rest seq
)))
373 (t (setf (aref seq i
) v
)))))
375 (defun make-next-element (x) (list x
))
378 ;;; Sequence Functions
381 ;; to prevent breakage.
382 (defmacro sequencep
(x)
385 (defun iseq (a &optional b
)
386 "Args: (n &optional m)
387 Generate a sequence of consecutive integers from a to b.
388 With one argumant returns a list of consecutive integers from 0 to N - 1.
389 With two returns a list of consecutive integers from N to M.
390 Examples: (iseq 4) returns (0 1 2 3)
391 (iseq 3 7) returns (3 4 5 6 7)
392 (iseq 3 -3) returns (3 2 1 0 -1 -2 -3)"
394 (let ((n (+ 1 (abs (- b a
))))
397 (setq x
(cons (if (< a b
) (- b i
) (+ b i
)) x
))))
400 ((< a
0) (iseq (+ a
1) 0))
401 ((< 0 a
) (iseq 0 (- a
1))))))
403 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
405 ;;;; Subset Selection and Mutation Functions
407 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
410 (defun old-rowmajor-index (index indices dim olddim
)
411 "translate row major index in resulting subarray to row major index
412 in the original array."
413 (declare (fixnum index
))
414 (let ((rank (length dim
))
418 (declare (fixnum rank face oldface
))
422 (setf face
(* face
(aref dim i
)))
423 (setf oldface
(* oldface
(aref olddim i
))))
427 (setf face
(/ face
(aref dim i
)))
428 (setf oldface
(/ oldface
(aref olddim i
)))
430 (* oldface
(aref (aref indices i
) (floor (/ index face
))))) ;;*** is this floor really needed???
431 (setf index
(rem index face
)))
434 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
436 ;;;; Subset Selection and Mutation Functions
438 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
440 (defun subarray-select (a indexlist
&optional
(values nil set_values
))
441 "extract or set subarray for the indices from a displaced array."
453 (declare (fixnum rank n
))
455 (if (or (sequencep a
) (not (arrayp a
))) (error "not an array - ~a" a
))
456 (if (not (listp indexlist
)) (error "bad index list - ~a" indices
))
457 (if (/= (length indexlist
) (array-rank a
))
458 (error "wrong number of indices"))
460 (setf indices
(coerce indexlist
'vector
))
462 (setf olddim
(coerce (array-dimensions a
) 'vector
))
464 ;; compute the result dimension vector and fix up the indices
465 (setf rank
(array-rank a
))
466 (setf dim
(make-array rank
))
469 (setf index
(aref indices i
))
470 (setf n
(aref olddim i
))
471 (setf index
(if (fixnump index
) (vector index
) (coerce index
'vector
)))
472 (setf k
(length index
))
475 (if (<= n
(check-nonneg-fixnum (aref index j
)))
476 (error "index out of bounds - ~a" (aref index j
)))
477 (setf (aref indices i
) index
))
478 (setf (aref dim i
) (length index
)))
480 ;; set up the result or check the values
481 (let ((dim-list (coerce dim
'list
)))
485 ((compound-data-p values
)
486 (if (or (not (arrayp values
)) (/= rank
(array-rank values
)))
487 (error "bad values array - ~a" values
))
488 (setf vdim
(coerce (array-dimensions values
) 'vector
))
491 (if (/= (aref vdim i
) (aref dim i
))
492 (error "bad value array dimensions - ~a" values
)))
493 (setf result values
))
494 (t (setf result
(make-array dim-list
:initial-element values
)))))
495 (t (setf result
(make-array dim-list
)))))
497 ;; compute the result or set the values
498 (setf data
(compound-data-seq a
))
499 (setf result_data
(compound-data-seq result
))
500 (setf n
(length result_data
))
503 (setf k
(old-rowmajor-index i indices dim olddim
))
504 (if (or (> 0 k
) (>= k
(length data
))) (error "index out of range"))
506 (setf (aref data k
) (aref result_data i
))
507 (setf (aref result_data i
) (aref data k
))))
512 ;;;; is x an ordered sequence of nonnegative positive integers?
513 (defun ordered-nneg-seq(x)
514 ;; FIXME -- sbcl warning about unreachable code, might be a logic error here.
517 (cx (make-next-element x
))
520 (let ((elem (check-nonneg-fixnum (get-next-element cx i
))))
521 (if (> m elem
) (return nil
) (setf m elem
)))))))
523 ;;;; select or set the subsequence corresponding to the specified indices
524 (defun sequence-select(x indices
&optional
(values nil set-values
))
525 ;; FIXME -- sbcl warning about unreachable code, might be a logic error here.
531 (declare (fixnum rlen dlen vlen
))
533 ;; Check the input data
535 (check-sequence indices
)
536 (if set-values
(check-sequence values
))
538 ;; Find the data sizes
539 (setf data
(if (ordered-nneg-seq indices
) x
(coerce x
'vector
)))
540 (setf dlen
(length data
))
541 (setf rlen
(length indices
))
543 (setf vlen
(length values
))
544 (if (/= vlen rlen
) (error "value and index sequences do not match")))
546 ;; set up the result/value sequence
550 (make-sequence (if (listp x
) 'list
'vector
) rlen
)))
552 ;; get or set the sequence elements
555 (cr (make-next-element result
))
556 (ci (make-next-element indices
))
561 (declare (fixnum i j index
))
562 (setf index
(get-next-element ci i
))
563 (if (<= dlen index
) (error "index out of range - ~a" index
))
564 (let ((elem (get-next-element cr i
)))
571 ((not (and (< j index
) (consp nextx
))))
573 (setf nextx
(rest nextx
)))
574 (setf (first nextx
) elem
))
575 (t (setf (aref x index
) elem
)))))
577 (cr (make-next-element result
))
578 (ci (make-next-element indices
))
584 (declare (fixnum i j index
))
585 (setf index
(get-next-element ci i
))
586 (if (<= dlen index
) (error "index out of range - ~a" index
))
588 ((listp data
) ;; indices must be ordered
590 ((not (and (< j index
) (consp nextx
))))
592 (setf nextx
(rest nextx
)))
593 (setf elem
(first nextx
)))
594 (t (setf elem
(aref data index
))))
595 (set-next-element cr i elem
)))
603 (defun select (x &rest args
)
604 "Args: (a &rest indices)
605 A can be a list or an array. If A is a list and INDICES is a single number
606 then the appropriate element of A is returned. If is a list and INDICES is
607 a list of numbers then the sublist of the corresponding elements is returned.
608 If A in an array then the number of INDICES must match the ARRAY-RANK of A.
609 If each index is a number then the appropriate array element is returned.
610 Otherwise the INDICES must all be lists of numbers and the corresponding
611 submatrix of A is returned. SELECT can be used in setf."
613 ((every #'fixnump args
)
614 (if (listp x
) (nth (first args
) x
) (apply #'aref x args
)))
615 ((sequencep x
) (sequence-select x
(first args
)))
616 (t (subarray-select x args
))))
619 ;; Built in SET-SELECT (SETF method for SELECT)
620 ;; FIXME: This should be done cleaner, check the spec, something like
621 ;; (defun (setf select) (x &rest args)...)
622 (defun set-select (x &rest args
)
623 (let ((indices (butlast args
))
624 (values (first (last args
))))
627 (if (not (consp indices
)) (error "bad indices - ~a" indices
))
628 (let* ((indices (first indices
))
629 (i-list (if (fixnump indices
) (list indices
) indices
))
630 (v-list (if (fixnump indices
) (list values
) values
)))
631 (sequence-select x i-list v-list
)))
633 (subarray-select x indices values
))
634 (t (error "bad argument type - ~a" x
)))
637 (defsetf select set-select
)
640 ;;;; Basic Sequence Operations
643 (defun difference (x)
645 Returns differences for a sequence X."
646 (let ((n (length x
)))
647 (- (select x
(iseq 1 (1- n
))) (select x
(iseq 0 (- n
2))))))
649 (defun rseq (a b num
)
651 Returns a list of NUM equally spaced points starting at A and ending at B."
652 (+ a
(* (values-list (iseq 0 (1- num
))) (/ (float (- b a
)) (1- num
)))))
656 (defun split-list (x n
)
658 Returns a list of COLS lists of equal length of the elements of LIST.
659 Example: (split-list '(1 2 3 4 5 6) 2) returns ((1 2 3) (4 5 6))"
661 (if (/= (rem (length x
) n
) 0) (error "length not divisible by ~a" n
))
662 (flet ((next-split ()
665 (dotimes (i n result
)
667 (let ((c-elem (list (first x
))))
672 (setf (rest end
) c-elem
)
673 (setf end
(rest end
)))))
674 (setf x
(rest x
))))))
677 (k (/ (length x
) n
)))
679 (dotimes (i k result
)
681 (let ((c-sub (list (next-split))))
686 (setf (rest end
) c-sub
)
687 (setf end
(rest end
)))))))))