1 (in-package :alexandria
)
3 ;; Make these inlinable by declaiming them INLINE here and some of them
4 ;; NOTINLINE at the end of the file. Exclude functions that have a compiler
5 ;; macro, because inlining seems to cancel compiler macros (at least on SBCL).
6 (declaim (inline copy-sequence sequence-of-length-p
))
8 (defun rotate-tail-to-head (sequence n
)
9 (declare (type (integer 1) n
))
11 (let ((m (mod n
(list-length sequence
))))
12 (if (null (cdr sequence
))
14 (let* ((tail (last sequence
(+ m
1)))
17 (nconc last sequence
))))
18 (let* ((len (length sequence
))
20 (tail (subseq sequence
(- len m
))))
21 (replace sequence sequence
:start1 m
:start2
0)
22 (replace sequence tail
)
25 (defun rotate-head-to-tail (sequence n
)
26 (declare (type (integer 1) n
))
28 (let ((m (mod (1- n
) (list-length sequence
))))
29 (if (null (cdr sequence
))
31 (let* ((headtail (nthcdr m sequence
))
32 (tail (cdr headtail
)))
33 (setf (cdr headtail
) nil
)
34 (nconc tail sequence
))))
35 (let* ((len (length sequence
))
37 (head (subseq sequence
0 m
)))
38 (replace sequence sequence
:start1
0 :start2 m
)
39 (replace sequence head
:start1
(- len m
))
42 (defun rotate (sequence &optional
(n 1))
43 "Returns a sequence of the same type as SEQUENCE, with the elements of
44 SEQUENCE rotated by N: N elements are moved from the end of the sequence to
45 the front if N is positive, and -N elements moved from the front to the end if
46 N is negative. SEQUENCE must be a proper sequence. N must be an integer,
47 defaulting to 1. If absolute value of N is greater then the length of the
48 sequence, the results are identical to calling ROTATE with (* (SIGNUM N) (MOD
49 N (LENGTH SEQUENCE))). The original sequence may be destructively altered, and
50 result sequence may share structure with it."
52 (rotate-tail-to-head sequence n
)
54 (rotate-head-to-tail sequence
(- n
))
57 (defun shuffle (sequence &key
(start 0) end
)
58 "Returns a random permutation of SEQUENCE bounded by START and END.
59 Permuted sequence may share storage with the original one. Signals an
60 error if SEQUENCE is not a proper sequence."
61 (declare (fixnum start
) (type (or fixnum null
) end
))
64 (let* ((end (or end
(list-length sequence
)))
66 (do ((tail (nthcdr start sequence
) (cdr tail
)))
68 (rotatef (car tail
) (car (nthcdr (random n
) tail
)))
71 (let ((end (or end
(length sequence
))))
72 (loop for i from
(- end
1) downto start
73 do
(rotatef (aref sequence i
) (aref sequence
(random (+ i
1)))))))
75 (let ((end (or end
(length sequence
))))
76 (loop for i from
(- end
1) downto start
77 do
(rotatef (elt sequence i
) (elt sequence
(random (+ i
1))))))))
80 (defun random-elt (sequence &key
(start 0) end
)
81 "Returns a random element from SEQUENCE bounded by START and END. Signals an
82 error if the SEQUENCE is not a proper sequence."
83 (declare (sequence sequence
) (fixnum start
) (type (or fixnum null
) end
))
84 (let ((i (+ start
(random (- (or end
(if (listp sequence
)
85 (list-length sequence
)
90 (declaim (inline remove
/swapped-arguments
))
91 (defun remove/swapped-arguments
(sequence item
&rest keyword-arguments
)
92 (apply #'remove item sequence keyword-arguments
))
94 (define-modify-macro removef
(item &rest remove-keywords
)
95 remove
/swapped-arguments
96 "Modify-macro for REMOVE. Sets place designated by the first argument to
97 the result of calling REMOVE with ITEM, place, and the REMOVE-KEYWORDS.")
99 (declaim (inline delete
/swapped-arguments
))
100 (defun delete/swapped-arguments
(sequence item
&rest keyword-arguments
)
101 (apply #'delete item sequence keyword-arguments
))
103 (define-modify-macro deletef
(item &rest remove-keywords
)
104 delete
/swapped-arguments
105 "Modify-macro for DELETE. Sets place designated by the first argument to
106 the result of calling DELETE with ITEM, place, and the REMOVE-KEYWORDS.")
108 (deftype proper-sequence
()
109 "Type designator for proper sequences, that is proper lists and sequences
112 (and (not list
) sequence
)))
114 (defun emptyp (sequence)
115 "Returns true if SEQUENCE is an empty sequence. Signals an error if SEQUENCE
118 (list (null sequence
))
119 (sequence (zerop (length sequence
)))))
121 (defun sequence-of-length-p (sequence length
)
122 "Return true if SEQUENCE is a sequence of length LENGTH. Signals an error if
123 SEQUENCE is not a sequence. Returns FALSE for circular lists."
124 (declare (type array-index length
)
131 (let ((n (1- length
)))
133 (let ((tail (nthcdr n sequence
)))
135 (null (cdr tail
)))))))
137 (= length
(length sequence
)))
139 (= length
(length sequence
)))))
141 (defun copy-sequence (type sequence
)
142 "Returns a fresh sequence of TYPE, which has the same elements as
144 (if (typep sequence type
)
146 (coerce sequence type
)))
148 (defun first-elt (sequence)
149 "Returns the first element of SEQUENCE. Signals a type-error if SEQUENCE is
150 not a sequence, or is an empty sequence."
151 ;; Can't just directly use ELT, as it is not guaranteed to signal the
153 (cond ((consp sequence
)
155 ((and (typep sequence
'(and sequence
(not list
))) (plusp (length sequence
)))
160 :expected-type
'(and sequence
(not (satisfies emptyp
)))))))
162 (defun (setf first-elt
) (object sequence
)
163 "Sets the first element of SEQUENCE. Signals a type-error if SEQUENCE is
164 not a sequence, is an empty sequence, or if OBJECT cannot be stored in SEQUENCE."
165 ;; Can't just directly use ELT, as it is not guaranteed to signal the
167 (cond ((consp sequence
)
168 (setf (car sequence
) object
))
169 ((and (typep sequence
'(and sequence
(not list
)))
170 (plusp (length sequence
)))
171 (setf (elt sequence
0) object
))
175 :expected-type
'(and sequence
(not (satisfies emptyp
)))))))
177 (defun last-elt (sequence)
178 "Returns the last element of SEQUENCE. Signals a type-error if SEQUENCE is
179 not a proper sequence, or is an empty sequence."
180 ;; Can't just directly use ELT, as it is not guaranteed to signal the
183 (cond ((consp sequence
)
185 ((and (typep sequence
'(and sequence
(not list
))) (plusp (setf len
(length sequence
))))
186 (elt sequence
(1- len
)))
190 :expected-type
'(and proper-sequence
(not (satisfies emptyp
))))))))
192 (defun (setf last-elt
) (object sequence
)
193 "Sets the last element of SEQUENCE. Signals a type-error if SEQUENCE is not a proper
194 sequence, is an empty sequence, or if OBJECT cannot be stored in SEQUENCE."
196 (cond ((consp sequence
)
197 (setf (lastcar sequence
) object
))
198 ((and (typep sequence
'(and sequence
(not list
))) (plusp (setf len
(length sequence
))))
199 (setf (elt sequence
(1- len
)) object
))
203 :expected-type
'(and proper-sequence
(not (satisfies emptyp
))))))))
205 (defun starts-with-subseq (prefix sequence
&rest args
&key
(return-suffix nil
) &allow-other-keys
)
206 "Test whether the first elements of SEQUENCE are the same (as per TEST) as the elements of PREFIX.
208 If RETURN-SUFFIX is T the functions returns, as a second value, a
209 displaced array pointing to the sequence after PREFIX."
210 (remove-from-plistf args
:return-suffix
)
211 (let ((sequence-length (length sequence
))
212 (prefix-length (length prefix
)))
213 (if (<= prefix-length sequence-length
)
214 (let ((mismatch (apply #'mismatch sequence prefix args
)))
216 (if (< mismatch prefix-length
)
218 (values t
(when return-suffix
219 (make-array (- sequence-length mismatch
)
220 :element-type
(array-element-type sequence
)
221 :displaced-to sequence
222 :displaced-index-offset prefix-length
224 (values t
(when return-suffix
225 (make-array 0 :element-type
(array-element-type sequence
)
229 (defun ends-with-subseq (suffix sequence
&key
(test #'eql
))
230 "Test whether SEQUENCE ends with SUFFIX. In other words: return true if
231 the last (length SUFFIX) elements of SEQUENCE are equal to SUFFIX."
232 (let ((sequence-length (length sequence
))
233 (suffix-length (length suffix
)))
234 (when (< sequence-length suffix-length
)
235 ;; if SEQUENCE is shorter than SUFFIX, then SEQUENCE can't end with SUFFIX.
236 (return-from ends-with-subseq nil
))
237 (loop for sequence-index from
(- sequence-length suffix-length
) below sequence-length
238 for suffix-index from
0 below suffix-length
239 when
(not (funcall test
(elt sequence sequence-index
) (elt suffix suffix-index
)))
240 do
(return-from ends-with-subseq nil
)
241 finally
(return t
))))
243 (defun starts-with (object sequence
&key
(test #'eql
) (key #'identity
))
244 "Returns true if SEQUENCE is a sequence whose first element is EQL to OBJECT.
245 Returns NIL if the SEQUENCE is not a sequence or is an empty sequence."
249 (cons (car sequence
))
251 (if (plusp (length sequence
))
253 (return-from starts-with nil
)))
255 (return-from starts-with nil
))))
258 (defun ends-with (object sequence
&key
(test #'eql
) (key #'identity
))
259 "Returns true if SEQUENCE is a sequence whose last element is EQL to OBJECT.
260 Returns NIL if the SEQUENCE is not a sequence or is an empty sequence. Signals
261 an error if SEQUENCE is an improper list."
266 ;; signals for improper lists
269 ;; Can't use last-elt, as that signals an error
270 ;; for empty sequences
271 (let ((len (length sequence
)))
273 (elt sequence
(1- len
))
274 (return-from ends-with nil
))))
276 (return-from ends-with nil
))))
279 (defun map-combinations (function sequence
&key
(start 0) end length
(copy t
))
280 "Calls FUNCTION with each combination of LENGTH constructable from the
281 elements of the subsequence of SEQUENCE delimited by START and END. START
282 defaults to 0, END to length of SEQUENCE, and LENGTH to the length of the
283 delimited subsequence. (So unless LENGTH is specified there is only a single
284 combination, which has the same elements as the delimited subsequence.) If
285 COPY is true (the default) each combination is freshly allocated. If COPY is
286 false all combinations are EQ to each other, in which case consequences are
287 specified if a combination is modified by FUNCTION."
288 (let* ((end (or end
(length sequence
)))
290 (length (or length size
))
291 (combination (subseq sequence
0 length
))
292 (function (ensure-function function
)))
294 (funcall function combination
)
296 (funcall function
(if copy
297 (copy-seq combination
)
300 ;; When dealing with lists we prefer walking back and
301 ;; forth instead of using indexes.
303 (labels ((combine-list (c-tail o-tail
)
306 (do ((tail o-tail
(cdr tail
)))
308 (setf (car c-tail
) (car tail
))
309 (combine-list (cdr c-tail
) (cdr tail
))))))
310 (combine-list combination
(nthcdr start sequence
))))
312 (labels ((combine (count start
)
315 (loop for i from start below end
316 do
(let ((j (- count
1)))
317 (setf (aref combination j
) (aref sequence i
))
318 (combine j
(+ i
1)))))))
319 (combine length start
)))
321 (labels ((combine (count start
)
324 (loop for i from start below end
325 do
(let ((j (- count
1)))
326 (setf (elt combination j
) (elt sequence i
))
327 (combine j
(+ i
1)))))))
328 (combine length start
)))))))
331 (defun map-permutations (function sequence
&key
(start 0) end length
(copy t
))
332 "Calls function with each permutation of LENGTH constructable
333 from the subsequence of SEQUENCE delimited by START and END. START
334 defaults to 0, END to length of the sequence, and LENGTH to the
335 length of the delimited subsequence."
336 (let* ((end (or end
(length sequence
)))
338 (length (or length size
)))
339 (labels ((permute (seq n
)
342 (funcall function
(if copy
346 (loop for i from
0 upto n-1
349 (rotatef (elt seq
0) (elt seq n-1
))
350 (rotatef (elt seq i
) (elt seq n-1
))))))))
351 (permute-sequence (seq)
352 (permute seq length
)))
354 ;; Things are simple if we need to just permute the
355 ;; full START-END range.
356 (permute-sequence (subseq sequence start end
))
357 ;; Otherwise we need to generate all the combinations
358 ;; of LENGTH in the START-END range, and then permute
359 ;; a copy of the result: can't permute the combination
360 ;; directly, as they share structure with each other.
361 (let ((permutation (subseq sequence
0 length
)))
362 (flet ((permute-combination (combination)
363 (permute-sequence (replace permutation combination
))))
364 (declare (dynamic-extent #'permute-combination
))
365 (map-combinations #'permute-combination sequence
371 (defun map-derangements (function sequence
&key
(start 0) end
(copy t
))
372 "Calls FUNCTION with each derangement of the subsequence of SEQUENCE denoted
373 by the bounding index designators START and END. Derangement is a permutation
374 of the sequence where no element remains in place. SEQUENCE is not modified,
375 but individual derangements are EQ to each other. Consequences are unspecified
376 if calling FUNCTION modifies either the derangement or SEQUENCE."
377 (let* ((end (or end
(length sequence
)))
379 ;; We don't really care about the elements here.
380 (derangement (subseq sequence
0 size
))
381 ;; Bitvector that has 1 for elements that have been deranged.
382 (mask (make-array size
:element-type
'bit
:initial-element
0)))
383 (declare (dynamic-extent mask
))
385 (labels ((derange (place n
)
386 ;; Perform one recursive step in deranging the
387 ;; sequence: PLACE is index of the original sequence
388 ;; to derange to another index, and N is the number of
389 ;; indexes not yet deranged.
391 (funcall function
(if copy
392 (copy-seq derangement
)
394 ;; Itarate over the indexes I of the subsequence to
395 ;; derange: if I != PLACE and I has not yet been
396 ;; deranged by an earlier call put the element from
397 ;; PLACE to I, mark I as deranged, and recurse,
398 ;; finally removing the mark.
399 (loop for i from
0 below size
401 (unless (or (= place
(+ i start
)) (not (zerop (bit mask i
))))
402 (setf (elt derangement i
) (elt sequence place
)
404 (derange (1+ place
) (1- n
))
405 (setf (bit mask i
) 0))))))
409 (declaim (notinline sequence-of-length-p
))