| 1 |
;;;; -*- Mode: Lisp; Syntax: Common-Lisp; indent-tabs-mode: nil; Package: SYSTEM -*- |
| 2 |
;;;; vim: set filetype=lisp tabstop=8 shiftwidth=2 expandtab: |
| 3 |
|
| 4 |
;;;; |
| 5 |
;;;; Copyright (c) 2010, Juan Jose Garcia-Ripoll |
| 6 |
;;;; |
| 7 |
;;;; See file 'LICENSE' for the copyright details. |
| 8 |
;;;; |
| 9 |
;;;; SEQMACROS -- Macros that are used to expand sequence routines |
| 10 |
;;;; |
| 11 |
|
| 12 |
(in-package "SYSTEM") |
| 13 |
|
| 14 |
(defmacro with-count ((count &optional (value count) &key (output nil output-p)) |
| 15 |
&body body) |
| 16 |
(setf body `(locally ,@body)) |
| 17 |
`(let ((,count (sequence-count ,value))) |
| 18 |
(declare (fixnum ,count)) |
| 19 |
,(if output-p |
| 20 |
`(if (plusp ,count) |
| 21 |
,body |
| 22 |
,output) |
| 23 |
body))) |
| 24 |
|
| 25 |
(defmacro with-predicate ((predicate) &body body) |
| 26 |
`(let ((,predicate (si::coerce-to-function ,predicate))) |
| 27 |
(declare (function ,predicate)) |
| 28 |
(macrolet ((,predicate (&rest args) |
| 29 |
`(locally (declare (optimize (safety 0) (speed 3))) |
| 30 |
(funcall ,',predicate ,@args)))) |
| 31 |
,@body))) |
| 32 |
|
| 33 |
(defmacro with-key ((akey) &body body) |
| 34 |
`(let ((,akey (if ,akey (si::coerce-to-function ,akey) #'identity))) |
| 35 |
(declare (function ,akey)) |
| 36 |
(macrolet ((,akey (value) |
| 37 |
`(locally (declare (optimize (safety 0) (speed 3))) |
| 38 |
(funcall ,',akey ,value)))) |
| 39 |
,@body))) |
| 40 |
|
| 41 |
(defmacro with-tests (&whole whole (test test-not &optional key) &body body) |
| 42 |
(ext::with-unique-names (%test %test-not %test-fn) |
| 43 |
`(let* ((,%test ,test) |
| 44 |
(,%test-not ,test-not) |
| 45 |
(,%test-fn (if ,%test |
| 46 |
(progn (when ,%test-not (test-error)) |
| 47 |
(si::coerce-to-function ,%test)) |
| 48 |
(if ,%test-not |
| 49 |
(si::coerce-to-function ,%test-not) |
| 50 |
#'eql)))) |
| 51 |
(declare (function ,%test-fn)) |
| 52 |
(macrolet ((compare (v1 v2) |
| 53 |
`(locally (declare (optimize (safety 0) (speed 3))) |
| 54 |
(if ,',%test-not |
| 55 |
(not (funcall ,',%test-fn ,v1 ,v2)) |
| 56 |
(funcall ,',%test-fn ,v1 ,v2))))) |
| 57 |
,@(if key `((with-key (,key) ,@body)) body))))) |
| 58 |
|
| 59 |
(defmacro with-start-end ((start end seq &optional (length (gensym) length-p)) |
| 60 |
&body body) |
| 61 |
`(multiple-value-bind (,start ,end ,length) |
| 62 |
(sequence-start-end 'subseq ,seq ,start ,end) |
| 63 |
(declare (fixnum ,start ,end ,length) |
| 64 |
,@(unless length-p `((ignorable ,length)))) |
| 65 |
,@body)) |
| 66 |
|
| 67 |
(defmacro reckless (&body body) |
| 68 |
`(locally (declare (optimize (safety 0) (speed 3) (debug 0))) ,@body)) |
| 69 |
|
| 70 |
(defmacro do-vector ((elt vector start end |
| 71 |
&key from-end output setter (index (gensym))) |
| 72 |
&body body) |
| 73 |
(with-unique-names (%vector %count) |
| 74 |
(when setter |
| 75 |
(setf body `((macrolet ((,setter (value) |
| 76 |
`(reckless (si::aset ,',%vector |
| 77 |
,',index |
| 78 |
,value)))) |
| 79 |
,@body)))) |
| 80 |
(if from-end |
| 81 |
`(do* ((,%vector ,vector) |
| 82 |
(,index ,end) |
| 83 |
(,%count ,start)) |
| 84 |
((= ,index ,%count) ,output) |
| 85 |
(declare (fixnum ,index ,%count) |
| 86 |
(vector ,%vector)) |
| 87 |
(let ((,elt (reckless (aref ,%vector (setf ,index (1- ,index)))))) |
| 88 |
,@body)) |
| 89 |
`(do* ((,%vector ,vector) |
| 90 |
(,index ,start (1+ ,index)) |
| 91 |
(,%count ,end)) |
| 92 |
((= ,index ,%count) ,output) |
| 93 |
(declare (fixnum ,index ,%count) |
| 94 |
(vector ,%vector)) |
| 95 |
(let ((,elt (reckless (aref ,%vector ,index)))) |
| 96 |
,@body))))) |
| 97 |
|
| 98 |
(defmacro do-sublist ((elt list start end &key output |
| 99 |
setter (index (gensym))) |
| 100 |
&body body) |
| 101 |
(with-unique-names (%sublist %count) |
| 102 |
(when setter |
| 103 |
(setf body `((macrolet ((,setter (value) |
| 104 |
`(reckless (rplaca ,',%sublist ,value)))) |
| 105 |
,@body)))) |
| 106 |
`(do* ((,index ,start (1+ ,index)) |
| 107 |
(,%sublist (nthcdr ,index ,list) (cdr ,%sublist)) |
| 108 |
(,%count (- ,end ,index) (1- ,%count))) |
| 109 |
((<= ,%count 0) ,output) |
| 110 |
(declare (fixnum ,index ,%count) |
| 111 |
(list ,%sublist)) |
| 112 |
(let ((,elt (car ,%sublist))) |
| 113 |
,@body)))) |
| 114 |
|
| 115 |
(defmacro do-sequence ((elt sequence start end &rest args |
| 116 |
&key setter index output specialize) |
| 117 |
&body body) |
| 118 |
(if specialize |
| 119 |
(with-unique-names (%sequence) |
| 120 |
(setf args (copy-list args)) |
| 121 |
(remf args :specialize) |
| 122 |
(setf args (list* elt %sequence start end args)) |
| 123 |
`(let ((,%sequence ,sequence)) |
| 124 |
(if (listp ,%sequence) |
| 125 |
(do-sublist ,args ,@body) |
| 126 |
(do-vector ,args ,@body)))) |
| 127 |
(with-unique-names (%sequence %start %i %count) |
| 128 |
`(do* ((,%sequence ,sequence) |
| 129 |
(,index ,start (1+ ,index)) |
| 130 |
(,%i (make-seq-iterator ,%sequence ,index) |
| 131 |
(seq-iterator-next ,%sequence ,%i)) |
| 132 |
(,%count (- ,end ,start) (1- ,%count))) |
| 133 |
((or (null ,%i) (not (plusp ,%count))) ,output) |
| 134 |
(let ((,elt (seq-iterator-ref ,%sequence ,%i))) |
| 135 |
,@body))))) |
| 136 |
|
| 137 |
(defmacro do-sequences ((elt-list seq-list &key output) &body body) |
| 138 |
(with-unique-names (%iterators %sequences) |
| 139 |
`(do* ((,%sequences ,seq-list) |
| 140 |
(,%iterators (mapcar #'make-seq-iterator ,%sequences)) |
| 141 |
(,elt-list (copy-list ,%sequences))) |
| 142 |
((null (setf ,elt-list |
| 143 |
(seq-iterator-list-pop ,elt-list |
| 144 |
,%sequences |
| 145 |
,%iterators))) |
| 146 |
,@(and output (list output))) |
| 147 |
,@body))) |