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 (:import-from
:lisp-stat-fastmap fastmap
)
23 (:shadowing-import-from
:lisp-stat-object-system
25 call-next-method call-method
)
26 (:export compound-data-p
*compound-data-proto
*
28 compound-data-seq compound-data-length
29 element-list element-seq
31 recursive-map-elements map-elements repeat
33 get-next-element make-next-element set-next-element
34 sequencep iseq ordered-nneg-seq
35 select split-list which
38 (in-package :lisp-stat-compound-data
)
40 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
42 ;;; Internal Support Functions
44 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
47 "Predicate to determine if argument is compound. Most common
48 non-compound types are checked first."
49 (declare (inline numberp symbolp stringp consp arrayp array-total-size
))
50 (cond ((or (numberp x
) (symbolp x
) (stringp x
)) nil
)
51 ((or (consp x
) (and (arrayp x
) (< 0 (array-total-size x
)))) t
)
52 (t (compound-object-p x
))))
54 (defun find-compound-data (list)
55 "Returns first compound data item in LIST or NIL if there is none."
56 (dolist (x list
) (if (cmpndp x
) (return x
))))
58 (defun any-compound-elements (seq)
59 "Checks for a compound element."
60 (cond ((consp seq
) (dolist (x seq
) (if (cmpndp x
) (return x
))))
62 (let ((n (length seq
)))
66 (let ((x (aref seq i
)))
67 (if (cmpndp x
) (return x
))))))
68 (t (error "argument must be a list or vector"))))
70 (defun compound-data-sequence (x)
71 "Returns sequence of data values for X."
72 (declare (inline consp vectorp arrayp make-array array-total-size
))
74 ((or (consp x
) (vectorp x
)) x
)
75 ((arrayp x
) (make-array (array-total-size x
) :displaced-to x
))
76 (t (send x
:data-seq
))))
78 (defmacro sequence-type
(x) `(if (consp ,x
) 'list
'vector
))
80 (defun make-compound-data (shape sequence
)
81 "Construct a compound data item to match the shape of the first
83 (let ((n (length (compound-data-sequence shape
))))
84 (if (/= n
(length sequence
)) (error "compound data not the same shape"))
86 ((consp shape
) (if (consp sequence
) sequence
(coerce sequence
'list
)))
88 (if (vectorp sequence
) sequence
(coerce sequence
'vector
)))
90 (make-array (array-dimensions shape
)
91 :displaced-to
(coerce sequence
'vector
)))
92 (t (send shape
:make-data sequence
)))))
94 (defun make-circle (x)
95 "Make a circular list of one element."
96 (declare (inline cons rplacd
))
97 (let ((x (cons x nil
)))
101 (defun check-compound (x)
102 "Signals an error if X is not compound."
103 (if (not (cmpndp x
)) (error "not a compound data item - ~a" x
)))
105 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
107 ;;; MAP-ELEMENTS function
108 ;;; Applies a function to arguments. If all arguments are simple (i. e.
109 ;;; not compound) then MAP-ELEMENTS acts like funcall. Otherwise all
110 ;;; compound arguments must be of the same shape and simple arguments
111 ;;; are treated as if they were compound arguments of the appropriate
112 ;;; shape. This is implemented by replacin all simple arguments by
113 ;;; circular lists of one element.
115 ;;; This implementation uses FASTMAP, a version of MAP that is assumed
118 ;;; a) work reasonable fast on any combination of lists and vectors
121 ;;; b) not hang if at least one of its arguments is not a circular
124 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
126 (defun fixup-map-elements-arglist (args)
127 (do* ((args args
(rest args
))
128 (x (car args
) (car args
)))
130 (declare (inline car
))
132 (if (cmpndp x
) (compound-data-sequence x
) (make-circle x
)))))
134 (defun map-elements (fcn &rest args
)
135 "Args: (fcn &rest args)
136 Applies FCN elementwise. If no arguments are compound MAP-ELEMENTS
137 acts like FUNCALL. Compound arguments must all be the same shape. Non
138 compound arguments, in the presence of compound ones, are treated as
139 if they were of the same shape as the compound items with constant data
141 (let ((first-compound (find-compound-data args
)))
142 (cond ((null first-compound
) (apply fcn args
))
143 (t (fixup-map-elements-arglist args
)
144 (let* ((seq (compound-data-sequence first-compound
))
145 (type (sequence-type seq
)))
146 (make-compound-data first-compound
147 (apply #'fastmap type fcn args
)))))))
149 (defun recursive-map-elements (base-fcn fcn
&rest args
)
150 "Args: (base-fcn fcn &rest args)
151 The same idea as MAP-ELEMENTS, except arguments are in a list and the
152 base and recursive cases can use different functions. Modified to check
153 for second level of compounding and use base-fcn if there is none."
154 (let ((first-compound (find-compound-data args
)))
155 (cond ((null first-compound
) (apply base-fcn args
))
156 (t (fixup-map-elements-arglist args
)
157 (let* ((seq (compound-data-sequence first-compound
))
158 (type (sequence-type seq
))
159 (f (if (any-compound-elements seq
) fcn base-fcn
)))
160 (make-compound-data first-compound
161 (apply #'fastmap type f args
)))))))
164 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
166 ;;;; Public Predicate and Accessor Functions
168 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
170 (defun compound-data-p (x)
172 Returns T if X is a compound data item, NIL otherwise."
175 (defun compound-data-seq (x)
177 Returns data sequence in X."
179 (compound-data-sequence x
))
181 (defun compound-data-length (x)
183 Returns length of data sequence in X."
185 (length (compound-data-sequence x
)))
187 (defun compound-data-shape (x)
188 "Needed but undefined??"
192 (defun element-list (x)
195 (let ((x (concatenate 'list
(compound-data-seq x
)))) ; copies sequence
197 ((any-compound-elements x
)
198 (do ((next x
(rest next
)))
200 (setf (first next
) (element-list (first next
))))
201 (do ((result (first x
))
202 (last (last (first x
)))
203 (next (rest x
) (rest next
)))
204 ((not (consp next
)) result
)
205 (setf (rest last
) (first next
))
206 (setf last
(last (first next
)))))
210 (defun element-seq (x)
212 Returns sequence of the elements of compound item X."
214 (let ((seq (compound-data-seq x
)))
215 (if (any-compound-elements seq
) (element-list seq
) seq
)))
217 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
219 ;;;; Compound Data Objects
221 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
223 (defproto *compound-data-proto
*)
225 (defmeth *compound-data-proto
* :data-length
(&rest args
) nil
)
226 (defmeth *compound-data-proto
* :data-seq
(&rest args
) nil
)
227 (defmeth *compound-data-proto
* :make-data
(&rest args
) nil
)
228 (defmeth *compound-data-proto
* :select-data
(&rest args
) nil
)
230 (defun compound-object-p (x) (kind-of-p x
*compound-data-proto
*))
234 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
236 ;;;; Sorting Functions
238 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
242 Returns a sequence with the numbers or strings in the sequence X in order."
243 (flet ((less (x y
) (if (numberp x
) (< x y
) (string-lessp x y
))))
244 (stable-sort (copy-seq (compound-data-seq x
)) #'less
)))
248 Returns a sequence of the indices of elements in the sequence of numbers
249 or strings X in order."
250 (let* ((seq (compound-data-seq x
))
251 (type (if (consp seq
) 'list
'vector
))
253 (flet ((entry (x) (setf i
(+ i
1)) (list x i
))
257 (if (numberp x
) (< x y
) (string-lessp x y
)))))
258 (let ((sorted-seq (stable-sort (map type
#'entry seq
) #'less
)))
259 (map type
#'second sorted-seq
)))))
261 ;; this isn't destructive -- do we document destructive only, or any
265 Returns a sequence with the elements of the list or array of numbers or
266 strings X replaced by their ranks."
267 (let ((ranked-seq (order (order x
))))
269 ;; compound-data-shape is undefined?
270 (compound-data-shape x
) ranked-seq
)))
280 Repeats VALS. If TIMES is a number and VALS is a non-null, non-array atom,
281 a list of length TIMES with all elements eq to VALS is returned. If VALS
282 is a list and TIMES is a number then VALS is appended TIMES times. If
283 TIMES is a list of numbers then VALS must be a list of equal length and
284 the simpler version of repeat is mapped down the two lists.
285 Examples: (repeat 2 5) returns (2 2 2 2 2)
286 (repeat '(1 2) 3) returns (1 2 1 2 1 2)
287 (repeat '(4 5 6) '(1 2 3)) returns (4 5 5 6 6 6)
288 (repeat '((4) (5 6)) '(2 3)) returns (4 4 5 6 5 6 5 6)"
289 (cond ((compound-data-p b
)
290 (let* ((reps (coerce (compound-data-seq (map-elements #'repeat a b
))
292 (result (first reps
))
293 (tail (last (first reps
))))
294 (dolist (next (rest reps
) result
)
296 (setf (rest tail
) next
)
297 (setf tail
(last next
))))))
298 (t (let* ((a (if (compound-data-p a
)
299 (coerce (compound-data-seq a
) 'list
)
302 (dotimes (i b result
)
303 (let ((next (copy-list a
)))
304 (if result
(setf (rest (last next
)) result
))
305 (setf result next
)))))))
312 Returns a list of the indices where elements of sequence X are not NIL."
313 (let ((x (list (compound-data-seq x
)))
316 (flet ((add-result (x)
317 (if result
(setf (rest tail
) (list x
)) (setf result
(list x
)))
318 (setf tail
(if tail
(rest tail
) result
)))
319 (get-next-element (seq-list i
)
320 (cond ((consp (first seq-list
))
321 (let ((elem (first (first seq-list
))))
322 (setf (first seq-list
) (rest (first seq-list
)))
324 (t (aref (first seq-list
) i
)))))
325 (let ((n (length (first x
))))
326 (dotimes (i n result
)
327 (if (get-next-element x i
) (add-result i
)))))))
330 ;;; Sequences are part of ANSI CL, being a supertype of vector and
331 ;;; list (ordered set of things).
333 ;;; Need to use the interenal structure when possible -- silly to be
334 ;;; redundant! However, this means we need to understand what
335 ;;; sequences were intending to do, which I'm not clear on yet.
337 ;;; The original ordering, object-wise, was to have compound
338 ;;; functionality passed into sequences, into other data sources.
339 ;;; However, at this point, we will see about inverting this and
340 ;;; having basic data types pushed through compound, to simplify
343 ;;; Type Checking Functions
345 (defun check-sequence (a)
346 ;; FIXME:AJR: does this handle consp as well? (Luke had an "or"
348 (if (not (typep a
'sequence
))
349 (error "not a sequence - ~s" a
)))
351 ;;; Sequence Element Access
354 ;;; (elt x i) -- NOT. This is more like "pop".
355 (defun get-next-element (x i
)
356 "Get element i from seq x. FIXME: not really??"
357 (let ((myseq (first x
)))
359 (let ((elem (first myseq
)))
360 (setf (first x
) (rest myseq
))
364 ;;; (setf (elt x i) v)
365 (defun set-next-element (x i v
)
366 (let ((seq (first x
)))
369 (setf (first x
) (rest seq
)))
370 (t (setf (aref seq i
) v
)))))
372 (defun make-next-element (x) (list x
))
375 ;;; Sequence Functions
378 ;; to prevent breakage.
379 (defmacro sequencep
(x)
382 (defun iseq (a &optional b
)
383 "Args: (n &optional m)
384 Generate a sequence of consecutive integers from a to b.
385 With one argumant returns a list of consecutive integers from 0 to N - 1.
386 With two returns a list of consecutive integers from N to M.
387 Examples: (iseq 4) returns (0 1 2 3)
388 (iseq 3 7) returns (3 4 5 6 7)
389 (iseq 3 -3) returns (3 2 1 0 -1 -2 -3)"
391 (let ((n (+ 1 (abs (- b a
))))
394 (setq x
(cons (if (< a b
) (- b i
) (+ b i
)) x
))))
397 ((< a
0) (iseq (+ a
1) 0))
398 ((< 0 a
) (iseq 0 (- a
1))))))
400 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
402 ;;;; Subset Selection and Mutation Functions
404 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
407 (defun old-rowmajor-index (index indices dim olddim
)
408 "translate row major index in resulting subarray to row major index
409 in the original array."
410 (declare (fixnum index
))
411 (let ((rank (length dim
))
415 (declare (fixnum rank face oldface
))
419 (setf face
(* face
(aref dim i
)))
420 (setf oldface
(* oldface
(aref olddim i
))))
424 (setf face
(/ face
(aref dim i
)))
425 (setf oldface
(/ oldface
(aref olddim i
)))
427 (* oldface
(aref (aref indices i
) (floor (/ index face
))))) ;;*** is this floor really needed???
428 (setf index
(rem index face
)))
431 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
433 ;;;; Subset Selection and Mutation Functions
435 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
437 (defun subarray-select (a indexlist
&optional
(values nil set_values
))
438 "extract or set subarray for the indices from a displaced array."
450 (declare (fixnum rank n
))
452 (if (or (sequencep a
) (not (arrayp a
))) (error "not an array - ~a" a
))
453 (if (not (listp indexlist
)) (error "bad index list - ~a" indices
))
454 (if (/= (length indexlist
) (array-rank a
))
455 (error "wrong number of indices"))
457 (setf indices
(coerce indexlist
'vector
))
459 (setf olddim
(coerce (array-dimensions a
) 'vector
))
461 ;; compute the result dimension vector and fix up the indices
462 (setf rank
(array-rank a
))
463 (setf dim
(make-array rank
))
466 (setf index
(aref indices i
))
467 (setf n
(aref olddim i
))
468 (setf index
(if (fixnump index
) (vector index
) (coerce index
'vector
)))
469 (setf k
(length index
))
472 (if (<= n
(check-nonneg-fixnum (aref index j
)))
473 (error "index out of bounds - ~a" (aref index j
)))
474 (setf (aref indices i
) index
))
475 (setf (aref dim i
) (length index
)))
477 ;; set up the result or check the values
478 (let ((dim-list (coerce dim
'list
)))
482 ((compound-data-p values
)
483 (if (or (not (arrayp values
)) (/= rank
(array-rank values
)))
484 (error "bad values array - ~a" values
))
485 (setf vdim
(coerce (array-dimensions values
) 'vector
))
488 (if (/= (aref vdim i
) (aref dim i
))
489 (error "bad value array dimensions - ~a" values
)))
490 (setf result values
))
491 (t (setf result
(make-array dim-list
:initial-element values
)))))
492 (t (setf result
(make-array dim-list
)))))
494 ;; compute the result or set the values
495 (setf data
(compound-data-seq a
))
496 (setf result_data
(compound-data-seq result
))
497 (setf n
(length result_data
))
500 (setf k
(old-rowmajor-index i indices dim olddim
))
501 (if (or (> 0 k
) (>= k
(length data
))) (error "index out of range"))
503 (setf (aref data k
) (aref result_data i
))
504 (setf (aref result_data i
) (aref data k
))))
509 ;;;; is x an ordered sequence of nonnegative positive integers?
510 (defun ordered-nneg-seq(x)
511 ;; FIXME -- sbcl warning about unreachable code, might be a logic error here.
514 (cx (make-next-element x
))
517 (let ((elem (check-nonneg-fixnum (get-next-element cx i
))))
518 (if (> m elem
) (return nil
) (setf m elem
)))))))
520 ;;;; select or set the subsequence corresponding to the specified indices
521 (defun sequence-select(x indices
&optional
(values nil set-values
))
522 ;; FIXME -- sbcl warning about unreachable code, might be a logic error here.
528 (declare (fixnum rlen dlen vlen
))
530 ;; Check the input data
532 (check-sequence indices
)
533 (if set-values
(check-sequence values
))
535 ;; Find the data sizes
536 (setf data
(if (ordered-nneg-seq indices
) x
(coerce x
'vector
)))
537 (setf dlen
(length data
))
538 (setf rlen
(length indices
))
540 (setf vlen
(length values
))
541 (if (/= vlen rlen
) (error "value and index sequences do not match")))
543 ;; set up the result/value sequence
547 (make-sequence (if (listp x
) 'list
'vector
) rlen
)))
549 ;; get or set the sequence elements
552 (cr (make-next-element result
))
553 (ci (make-next-element indices
))
558 (declare (fixnum i j index
))
559 (setf index
(get-next-element ci i
))
560 (if (<= dlen index
) (error "index out of range - ~a" index
))
561 (let ((elem (get-next-element cr i
)))
568 ((not (and (< j index
) (consp nextx
))))
570 (setf nextx
(rest nextx
)))
571 (setf (first nextx
) elem
))
572 (t (setf (aref x index
) elem
)))))
574 (cr (make-next-element result
))
575 (ci (make-next-element indices
))
581 (declare (fixnum i j index
))
582 (setf index
(get-next-element ci i
))
583 (if (<= dlen index
) (error "index out of range - ~a" index
))
585 ((listp data
) ;; indices must be ordered
587 ((not (and (< j index
) (consp nextx
))))
589 (setf nextx
(rest nextx
)))
590 (setf elem
(first nextx
)))
591 (t (setf elem
(aref data index
))))
592 (set-next-element cr i elem
)))
600 (defun select (x &rest args
)
601 "Args: (a &rest indices)
602 A can be a list or an array. If A is a list and INDICES is a single number
603 then the appropriate element of A is returned. If is a list and INDICES is
604 a list of numbers then the sublist of the corresponding elements is returned.
605 If A in an array then the number of INDICES must match the ARRAY-RANK of A.
606 If each index is a number then the appropriate array element is returned.
607 Otherwise the INDICES must all be lists of numbers and the corresponding
608 submatrix of A is returned. SELECT can be used in setf."
610 ((every #'fixnump args
)
611 (if (listp x
) (nth (first args
) x
) (apply #'aref x args
)))
612 ((sequencep x
) (sequence-select x
(first args
)))
613 (t (subarray-select x args
))))
616 ;; Built in SET-SELECT (SETF method for SELECT)
617 (defun set-select (x &rest args
)
618 (let ((indices (butlast args
))
619 (values (first (last args
))))
622 (if (not (consp indices
)) (error "bad indices - ~a" indices
))
623 (let* ((indices (first indices
))
624 (i-list (if (fixnump indices
) (list indices
) indices
))
625 (v-list (if (fixnump indices
) (list values
) values
)))
626 (sequence-select x i-list v-list
)))
628 (subarray-select x indices values
))
629 (t (error "bad argument type - ~a" x
)))
632 (defsetf select set-select
)
635 ;;;; Basic Sequence Operations
638 (defun difference (x)
640 Returns differences for a sequence X."
641 (let ((n (length x
)))
642 (- (select x
(iseq 1 (1- n
))) (select x
(iseq 0 (- n
2))))))
644 (defun rseq (a b num
)
646 Returns a list of NUM equally spaced points starting at A and ending at B."
647 (+ a
(* (values-list (iseq 0 (1- num
))) (/ (float (- b a
)) (1- num
)))))
651 (defun split-list (x n
)
653 Returns a list of COLS lists of equal length of the elements of LIST.
654 Example: (split-list '(1 2 3 4 5 6) 2) returns ((1 2 3) (4 5 6))"
656 (if (/= (rem (length x
) n
) 0) (error "length not divisible by ~a" n
))
657 (flet ((next-split ()
660 (dotimes (i n result
)
662 (let ((c-elem (list (first x
))))
667 (setf (rest end
) c-elem
)
668 (setf end
(rest end
)))))
669 (setf x
(rest x
))))))
672 (k (/ (length x
) n
)))
674 (dotimes (i k result
)
676 (let ((c-sub (list (next-split))))
681 (setf (rest end
) c-sub
)
682 (setf end
(rest end
)))))))))