-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsequence-ops-internal.lisp
286 lines (218 loc) · 8.36 KB
/
sequence-ops-internal.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
;;; -*- Mode: LISP; Syntax: Common-lisp; Package: (*SIM-I COMMON-LISP-GLOBAL); Base: 10 -*-
(in-package :*sim-i)
;;;> *+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+
;;;>
;;;> The Thinking Machines *Lisp Simulator is in the public domain.
;;;> You are free to do whatever you like with it, including but
;;;> not limited to distributing, modifying, and copying.
;;;>
;;;> *+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+
;;; Author: JP Massar.
(defun check-zero-length-sequence-start-and-end (start end)
(*nocompile
(if (not (*and (and!! (zerop!! start) (zerop!! end))))
(error "The start and end indices for a zero length sequence must be zero everywhere")
)))
(defun proper-start-and-end-check (start end sequence-length function)
(*let (pvar-sequence-length)
(if (numberp sequence-length)
(*set pvar-sequence-length (!! (the fixnum sequence-length)))
(*set pvar-sequence-length sequence-length)
)
(flet
((check-range (pvar parameter-name)
(assert (*and (and!! (integerp!! pvar) (>=!! pvar (!! 0)))) ()
"The ~S parameter to function ~S is somewhere negative or contains a non-integer value"
parameter-name function
)
(assert (*and (<=!! pvar pvar-sequence-length)) ()
"The ~S parameter to function ~S somewhere exceeds the length of the sequence (~D)"
parameter-name function (*max pvar-sequence-length)
)))
(check-range start 'start)
(check-range end 'end)
(assert (*and (<=!! start end)) ()
"The start parameter to function ~S is somewhere greater than the end parameter"
function
)
)))
(defun with-start-and-end-checked-and-coerced (start end length function-name function-of-start-and-end)
(safety-check
(proper-start-and-end-check start end length function-name)
)
(funcall function-of-start-and-end start end)
)
(defmacro without-void-pvar-list (pvar-list-symbol &body body)
(assert (symbolp pvar-list-symbol))
(let ((void-pvar-symbol (gensym "VOID-PVAR-")))
`(let ((,void-pvar-symbol (find-if 'void-pvar-p ,pvar-list-symbol)))
(if (and ,void-pvar-symbol (*or t!!))
(sequence-pvar-check ,void-pvar-symbol nil)
(progn ,@body)
))))
(*defun *map-vectors-nocheck (function start end backwards? function-expects-index-as-first-argument? &rest vector-pvars)
;; Apply function, which should be a function taking (length array-pvars)
;; pvar arguments and not returning any value of significance, to
;; each array element of each array pvar element of array-pvars.
;; If function-expects-index-as-first-argument? is true, then the
;; function will get as its first argument the index of the elements
;; it is being called with, and then the elements.
;; If there are no arrays there is nothing to do.
(when (null vector-pvars) (return-from *map-vectors-nocheck nil))
;; If there are no processors active there is nothing to do.
(when (not (*or t!!)) (return-from *map-vectors-nocheck nil))
;; This is a hack to allow us to either pass in a precreated list of vectors
;; or many different vectors not in a list.
(let ((is-really-&rest (listp (car vector-pvars))))
(when is-really-&rest (setq vector-pvars (car vector-pvars)))
(let ((start-and-end-constant (and (pvar-constant-value start) (pvar-constant-value end)))
(min-index (if (pvar-constant-value start) (pvar-constant-value start) (*min start)))
(max-index (if (pvar-constant-value end) (pvar-constant-value end) (*max end)))
element-pvar-list
current-index
)
(flet
((call-function (index)
(if function-expects-index-as-first-argument?
(apply function index element-pvar-list)
(apply function element-pvar-list)
))
(decf-pvars ()
(decf current-index)
(setq element-pvar-list
(mapcar
#'(lambda (vector-pvar) (alias!! (aref!! vector-pvar (!! current-index))))
vector-pvars
)))
(incf-pvars ()
(incf current-index)
(setq element-pvar-list
(mapcar
#'(lambda (vector-pvar) (alias!! (aref!! vector-pvar (!! current-index))))
vector-pvars
))))
;; Set up the current-index initially.
(if backwards?
(setq current-index max-index)
(setq current-index (1- min-index))
)
;; Call the function on the vector elements,
;; and proceed on to the next/previous set of elements.
(let ((*interpreter-safety* 0))
(if backwards?
(if start-and-end-constant
(do ((index (1- max-index) (decf index)))
((< index min-index))
(decf-pvars)
(call-function index)
)
(do ((index (1- max-index) (decf index)))
((< index max-index))
(decf-pvars)
(*when (and!! (>=!! (!! (the fixnum index)) start) (<!! (!! (the fixnum index)) end))
(call-function index)
)
))
(if start-and-end-constant
(do ((index min-index (incf index)))
((>= index max-index))
(incf-pvars)
(call-function index)
)
(do ((index min-index (incf index)))
((>= index max-index))
(incf-pvars)
(*when (and!! (>=!! (!! (the fixnum index)) start) (<!! (!! (the fixnum index)) end))
(call-function index)
)
))))
))))
(defun copy-seq!!-internal (sequence)
(*let (result)
(*set result sequence)
result
))
(defun *nreverse-internal (sequence)
(let ((vector-length (array-pvar-total-size sequence)))
(*let (temp)
(dotimes (j (floor (/ vector-length 2)))
(let ((other-index (- vector-length (1+ j))))
(*set temp (aref!! sequence (!! j)))
(*setf (aref!! sequence (!! j)) (aref!! sequence (!! other-index)))
(*setf (aref!! sequence (!! other-index)) temp)
))))
sequence
)
(defun reverse!!-internal (sequence)
(*let ((result sequence))
(*nreverse-internal result)
result
))
(defun subseq!!-internal (sequence start end)
(let* ((vlength (array-pvar-total-size sequence))
(element-type (pvar-array-element-type sequence))
result-size
min-index
(start-constant (pvar-constant-value start))
(end-constant (pvar-constant-value end))
(start-and-end-constant (and start-constant end-constant))
)
;; Check the start and end parameters against
;; the length of the sequence and each other.
(with-start-and-end-checked-and-coerced
start end vlength 'subseq!!
#'(lambda (start end)
;; Subseq!! insists that all the subsequence lengths specified
;; must be the same. Check this and set result-size to
;; how long a vector we must allocate for the result.
(if start-and-end-constant
(setq result-size (- (pvar-constant-value end) (pvar-constant-value start)))
(progn
(setq result-size (*min (-!! end start)))
(if (null result-size) (setq result-size 0))
(assert (*and (=!! (!! (the fixnum result-size)) (-!! end start))) ()
"The subsequences must all be the same length."
)))
;; Allocate an array pvar as our result.
(let ((dest (make-array-pvar
:stack
`(pvar (array ,(cadr element-type) (,result-size)))
)))
(setf (pvar-lvalue? dest) t)
;; Now copy the data from the original sequence pvar
;; to our result pvar. Do this by selecting all
;; the processors with a given start index, and copying
;; the proper number of elements into the result
;; pvar starting at the start index. Then turn
;; off all the processors with that index and do
;; the same for the next index. Exit when we have
;; exhausted the possible start indices.
(when (plusp result-size)
(*let ((selected-set t!!))
(with-css-saved
(block while-more-indices
(loop
(*when selected-set
(unless (*or t!!) (return-from while-more-indices nil))
(setq min-index (*min start))
(*when (=!! (!! (the fixnum min-index)) start)
(*set selected-set nil!!)
(dotimes (j result-size)
(*setf (aref!! dest (!! j)) (aref!! sequence (+!! start (!! j))))
))))))))
(setf (pvar-lvalue? dest) nil)
dest
)))))
(defun *fill-internal (sequence item start end)
(let ((vlength (array-pvar-total-size sequence)))
(when (zerop vlength)
(check-zero-length-sequence-start-and-end start end)
(return-from *fill-internal sequence)
)
(*when (not!! (zerop!! (-!! end start)))
(*map-vectors-nocheck
#'(lambda (dest) (*compile () (*set dest item)))
start end nil nil sequence
))
sequence
))