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 ;;;; Copyright (c) 1991, by Luke Tierney. Permission is granted for
7 ;;;; unrestricted use. (though Luke never had this file).
15 (defpackage :lisp-stat-sequence
17 (:export check-sequence
18 get-next-element make-next-element set-next-element
28 (in-package :lisp-stat-sequence
)
30 ;;; Sequences are part of ANSI CL, being a supertype of vector and
31 ;;; list (ordered set of things).
33 ;;; Need to use the interenal structure when possible -- silly to be
34 ;;; redundant! However, this means we need to understand what
35 ;;; sequences were intending to do, which I'm not clear on yet.
37 ;;; The original ordering, object-wise, was to have compound
38 ;;; functionality passed into sequences, into other data sources.
39 ;;; However, at this point, we will see about inverting this and
40 ;;; having basic data types pushed through compound, to simplify
43 ;;; Type Checking Functions
45 (defun check-sequence (a)
46 ;; FIXME:AJR: does this handle consp as well? (Luke had an "or"
48 (if (not (typep a
'sequence
))
49 (error "not a sequence - ~s" a
)))
51 ;;; Sequence Element Access
54 ;;; (elt x i) -- NOT. This is more like "pop".
55 (defun get-next-element (x i
)
56 "Get element i from seq x. FIXME: not really??"
57 (let ((myseq (first x
)))
59 (let ((elem (first myseq
)))
60 (setf (first x
) (rest myseq
))
64 ;;; (setf (elt x i) v)
65 (defun set-next-element (x i v
)
66 (let ((seq (first x
)))
69 (setf (first x
) (rest seq
)))
70 (t (setf (aref seq i
) v
)))))
72 (defun make-next-element (x) (list x
))
75 ;;; Sequence Functions
78 ;; to prevent breakage.
79 (defmacro sequencep
(x)
82 (defun iseq (a &optional b
)
83 "Args: (n &optional m)
84 Generate a sequence of consecutive integers from a to b.
85 With one argumant returns a list of consecutive integers from 0 to N - 1.
86 With two returns a list of consecutive integers from N to M.
87 Examples: (iseq 4) returns (0 1 2 3)
88 (iseq 3 7) returns (3 4 5 6 7)
89 (iseq 3 -3) returns (3 2 1 0 -1 -2 -3)"
91 (let ((n (+ 1 (abs (- b a
))))
94 (setq x
(cons (if (< a b
) (- b i
) (+ b i
)) x
))))
97 ((< a
0) (iseq (+ a
1) 0))
98 ((< 0 a
) (iseq 0 (- a
1))))))
100 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
102 ;;;; Subset Selection and Mutation Functions
104 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
106 ;;;; is x an ordered sequence of nonnegative positive integers?
107 (defun ordered-nneg-seq(x)
108 ;; FIXME -- sbcl warning about unreachable code, might be a logic error here.
111 (cx (make-next-element x
))
114 (let ((elem (check-nonneg-fixnum (get-next-element cx i
))))
115 (if (> m elem
) (return nil
) (setf m elem
)))))))
117 ;;;; select or set the subsequence corresponding to the specified indices
118 (defun sequence-select(x indices
&optional
(values nil set-values
))
119 ;; FIXME -- sbcl warning about unreachable code, might be a logic error here.
125 (declare (fixnum rlen dlen vlen
))
127 ;; Check the input data
129 (check-sequence indices
)
130 (if set-values
(check-sequence values
))
132 ;; Find the data sizes
133 (setf data
(if (ordered-nneg-seq indices
) x
(coerce x
'vector
)))
134 (setf dlen
(length data
))
135 (setf rlen
(length indices
))
137 (setf vlen
(length values
))
138 (if (/= vlen rlen
) (error "value and index sequences do not match")))
140 ;; set up the result/value sequence
144 (make-sequence (if (listp x
) 'list
'vector
) rlen
)))
146 ;; get or set the sequence elements
149 (cr (make-next-element result
))
150 (ci (make-next-element indices
))
155 (declare (fixnum i j index
))
156 (setf index
(get-next-element ci i
))
157 (if (<= dlen index
) (error "index out of range - ~a" index
))
158 (let ((elem (get-next-element cr i
)))
165 ((not (and (< j index
) (consp nextx
))))
167 (setf nextx
(rest nextx
)))
168 (setf (first nextx
) elem
))
169 (t (setf (aref x index
) elem
)))))
171 (cr (make-next-element result
))
172 (ci (make-next-element indices
))
178 (declare (fixnum i j index
))
179 (setf index
(get-next-element ci i
))
180 (if (<= dlen index
) (error "index out of range - ~a" index
))
182 ((listp data
) ;; indices must be ordered
184 ((not (and (< j index
) (consp nextx
))))
186 (setf nextx
(rest nextx
)))
187 (setf elem
(first nextx
)))
188 (t (setf elem
(aref data index
))))
189 (set-next-element cr i elem
)))
197 (defun select (x &rest args
)
198 "Args: (a &rest indices)
199 A can be a list or an array. If A is a list and INDICES is a single number
200 then the appropriate element of A is returned. If is a list and INDICES is
201 a list of numbers then the sublist of the corresponding elements is returned.
202 If A in an array then the number of INDICES must match the ARRAY-RANK of A.
203 If each index is a number then the appropriate array element is returned.
204 Otherwise the INDICES must all be lists of numbers and the corresponding
205 submatrix of A is returned. SELECT can be used in setf."
207 ((every #'fixnump args
)
208 (if (listp x
) (nth (first args
) x
) (apply #'aref x args
)))
209 ((sequencep x
) (sequence-select x
(first args
)))
210 (t (subarray-select x args
))))
213 ;; Built in SET-SELECT (SETF method for SELECT)
214 (defun set-select (x &rest args
)
215 (let ((indices (butlast args
))
216 (values (first (last args
))))
219 (if (not (consp indices
)) (error "bad indices - ~a" indices
))
220 (let* ((indices (first indices
))
221 (i-list (if (fixnump indices
) (list indices
) indices
))
222 (v-list (if (fixnump indices
) (list values
) values
)))
223 (sequence-select x i-list v-list
)))
225 (subarray-select x indices values
))
226 (t (error "bad argument type - ~a" x
)))
229 (defsetf select set-select
)
232 ;;;; Basic Sequence Operations
235 (defun difference (x)
237 Returns differences for a sequence X."
238 (let ((n (length x
)))
239 (- (select x
(iseq 1 (1- n
))) (select x
(iseq 0 (- n
2))))))
241 (defun rseq (a b num
)
243 Returns a list of NUM equally spaced points starting at A and ending at B."
244 (+ a
(* (values-list (iseq 0 (1- num
))) (/ (float (- b a
)) (1- num
)))))