-
Notifications
You must be signed in to change notification settings - Fork 3
/
sequence-ops.lisp
892 lines (681 loc) · 26.6 KB
/
sequence-ops.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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
;;; -*- 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.
;;;> Bugs, comments and revisions due to porting can be sent to:
;;;> [email protected]. Other than to Thinking Machines'
;;;> customers, no promise of support is intended or implied.
;;;>
;;;> *+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+
;;; Author: JP Massar.
;;;; **************************************************************************
;;;; THIS CODE IS SOURCE COMPATIBLE BETWEEN THE *LISP SIMULATOR AND THE
;;;; *LISP INTERPRETER. DO NOT CHANGE IT UNLESS YOU KNOW WHAT YOU ARE
;;;; DOING AND CAN MAINTAIN THIS SOURCE COMPATIBILITY.
;;;; ***************************************************************************
(defun sequence-pvar-check (pvar function-name)
(assert (vector-pvar-p pvar) ()
"The sequence-pvar argument ~S, to function ~S, is not a sequence pvar" pvar function-name
))
(defun sequence-function-pvar-args-check (function-name sequence &rest pvars)
(new-vp-pvar-check sequence function-name)
(new-multiple-pvar-check pvars function-name)
(sequence-pvar-check sequence function-name)
)
(defmacro without-void-sequence-pvar (sequence-pvar function-name &body body)
(assert (symbolp sequence-pvar))
`(if (void-pvar-p ,sequence-pvar)
(if (*or t!!) (sequence-pvar-check ,sequence-pvar ',function-name) ,sequence-pvar)
(progn
(safety-check (sequence-pvar-check ,sequence-pvar ',function-name))
,@body
)))
(defmacro without-void-sequence (sequence function-name &body body)
(assert (symbolp sequence))
`(progn
(safety-check (new-vp-pvar-check ,sequence ',function-name))
(without-void-sequence-pvar
,sequence ,function-name
,@body
)))
(defun copy-seq!! (sequence)
(simple-pvar-argument!! sequence)
(without-void-sequence sequence copy-seq!!
(copy-seq!!-internal sequence)
))
(defun fast-length!! (sequence) (!! (array-pvar-total-size sequence)))
(defun length!! (sequence)
(simple-pvar-argument!! sequence)
(without-void-sequence sequence length!!
(fast-length!! sequence)
))
(*defun *nreverse (sequence)
(without-void-sequence sequence *nreverse
(*nreverse-internal sequence)
))
(defun reverse!! (sequence)
(simple-pvar-argument!! sequence)
(without-void-sequence sequence reverse!!
(reverse!!-internal sequence)
))
(defun subseq!! (sequence start &optional (end (length!! sequence)))
(simple-pvar-argument!! sequence start &opt end)
(without-void-sequence sequence subseq!!
(without-void-pvars (start end)
(subseq!!-internal sequence start end)
)))
(*proclaim '(ftype (function (t pvar &rest pvar) (pvar boolean)) some!! every!! notany!! notevery!!))
(defun find-shortest-length (sequence-pvar sequence-pvars)
(let ((shortest-length (array-pvar-total-size sequence-pvar)))
(dolist (pvar sequence-pvars)
(setq shortest-length (min shortest-length (array-pvar-total-size pvar)))
)
shortest-length
))
(defun-wcefi some!! (predicate sequence &rest more-sequences)
(simple-pvar-argument!! sequence &rest more-sequences)
(safety-check
(new-multiple-pvar-check more-sequences 'some!!)
)
(without-void-sequence sequence some!!
(without-void-pvar-list more-sequences
(let ((shortest-length (find-shortest-length sequence more-sequences)))
(let!! ((result nil!!))
(declare (type boolean-pvar result))
(*map-vectors-nocheck
#'(lambda (&rest element-pvars)
(*when (not!! result)
(*set result (or!! result (the (pvar boolean) (apply predicate element-pvars))))
))
(!! 0)
(!! shortest-length)
nil nil
(cons sequence more-sequences)
)
result
)))))
(defun-wcefi every!! (predicate sequence &rest more-sequences)
(simple-pvar-argument!! sequence &rest more-sequences)
(safety-check
(new-multiple-pvar-check more-sequences 'every!!)
)
(without-void-sequence sequence every!!
(without-void-pvar-list more-sequences
(let ((shortest-length (find-shortest-length sequence more-sequences)))
(let!! ((result t!!))
(declare (type boolean-pvar result))
(*map-vectors-nocheck
#'(lambda (&rest element-pvars)
(*when result
(declare (return-pvar-p nil))
(*set result (and!! result (the (pvar boolean) (apply predicate element-pvars))))
))
(!! 0)
(!! shortest-length)
nil nil
(cons sequence more-sequences)
)
result
)))))
(defun-wcefi notany!! (predicate sequence &rest more-sequences)
(simple-pvar-argument!! sequence &rest more-sequences)
(safety-check
(new-multiple-pvar-check more-sequences 'notany!!)
)
(without-void-sequence sequence notany!!
(without-void-pvar-list more-sequences
(let ((shortest-length (find-shortest-length sequence more-sequences)))
(let!! ((result nil!!))
(declare (type boolean-pvar result))
(*map-vectors-nocheck
#'(lambda (&rest element-pvars)
(*when (not!! result)
(declare (return-pvar-p nil))
(*set result (or!! result (the (pvar boolean) (apply predicate element-pvars))))
))
(!! 0)
(!! shortest-length)
nil nil
(cons sequence more-sequences)
)
(*set result (not!! result))
result
)))))
(defun-wcefi notevery!! (predicate sequence &rest more-sequences)
(simple-pvar-argument!! sequence &rest more-sequences)
(safety-check
(new-multiple-pvar-check more-sequences 'notevery!!)
)
(without-void-sequence sequence notevery!!
(without-void-pvar-list more-sequences
(let ((shortest-length (find-shortest-length sequence more-sequences)))
(let!! ((result t!!))
(declare (type boolean-pvar result))
(*map-vectors-nocheck
#'(lambda (&rest element-pvars)
(*when result
(declare (return-pvar-p nil))
(*set result (and!! result (the (pvar boolean) (apply predicate element-pvars))))
))
(!! 0)
(!! shortest-length)
nil nil
(cons sequence more-sequences)
)
(*set result (not!! result))
result
)))))
(defun-wcefi reduce!!
(function sequence &key (from-end nil) (start (!! 0)) (end (length!! sequence)) (initial-value nil))
(simple-pvar-argument!! sequence &opt start end initial-value)
(safety-check
(new-two-pvar-check start end 'reduce!!)
(if initial-value (new-vp-pvar-check initial-value 'reduce!!))
)
(without-void-sequence sequence reduce!!
(without-void-pvars (start end)
(let ((vlength (array-pvar-total-size sequence)))
(when (zerop vlength)
(check-zero-length-sequence-start-and-end start end)
(return-from reduce!!
(if initial-value initial-value (funcall function))
))
(with-start-and-end-checked-and-coerced
start end vlength 'reduce!!
#'(lambda (start end)
(*locally
(declare (type (field-pvar *) start end))
(let!! (result)
(declare (type (pvar *) result))
nil
(*compile ()
(*let ((reduction-length (-!! end start)))
(declare (type (pvar (unsigned-byte 32)) reduction-length))
(declare (return-pvar-p nil))
;; From CLtL, page 251
;; If an :initial-value argument is given, it is logically
;; placed before the subsequence (after it if :from-end is
;; true) and included in the reduction operation.
(*cond
;; From CLtL, page 251.
;; If the subsequence is empty and an :initial-value argument
;; is given, then the :initial-value argument is returned and
;; function is not called. If no :initial-value is given, then
;; the function is called with zero arguments.
((zerop!! reduction-length)
(when (*or t!!)
(*nocompile
(if initial-value
(*set result initial-value)
(*set result (funcall function))
))))
;; From CLtL, page 251
;; If the subsequence contains exactly one element and no
;; :initial-value is given, then that element is returned
;; and function is not called.
((=!! (!! 1) reduction-length)
(when (*or t!!)
(*nocompile
(if initial-value
(if from-end
(*set result (funcall function (aref!! sequence start) initial-value))
(*set result (funcall function initial-value (aref!! sequence start)))
)
(*set result (aref!! sequence start))
))))
(t!!
(when (*or t!!)
(if initial-value
(*nocompile
(*set result initial-value)
(*map-vectors-nocheck
#'(lambda (element)
(if from-end
(*set result (funcall function element result))
(*set result (funcall function result element))
))
start end from-end nil sequence
))
(*let ((first t!!))
(declare (type (pvar boolean) first))
(declare (return-pvar-p nil))
(*map-vectors-nocheck
#'(lambda (element)
(*if first
(progn
(*nocompile (*set result element))
(*set first nil!!)
)
(*nocompile
(if from-end
(*set result (funcall function element result))
(*set result (funcall function result element))
))))
start end from-end nil sequence
)))))
)))
result
))))))))
(*defun *fill (sequence item &key (start (!! 0)) (end (length!! sequence)))
(simple-pvar-argument!! (item start end))
(safety-check
(new-two-pvar-check start end '*fill)
(new-vp-pvar-check item '*fill)
)
(without-void-sequence sequence *fill
(without-void-pvars (item start end)
(*fill-internal sequence item start end)
)))
(defmacro check-test-test-not (test test-not)
`(if (null ,test)
(if (null ,test-not)
(setq ,test 'eql!!)
)
(if ,test-not (error "You cannot specify :test and :test-not"))
))
(defun-wcefi internal-substitute!! (newitem olditem sequence start end from-end test test-not key count)
(without-void-sequence-pvar sequence substitute!!
(without-void-pvars (newitem start end)
(let ((vlength (array-pvar-total-size sequence)))
(when (zerop vlength)
(check-zero-length-sequence-start-and-end start end)
(return-from internal-substitute!! sequence)
)
(with-start-and-end-checked-and-coerced
start end vlength 'substitute!!
#'(lambda (start end)
(*compile ()
(*let (found (number-substituted (!! 0)))
(declare (type (pvar boolean) found))
(declare (type (pvar (unsigned-byte 32)) number-substituted))
(declare (return-pvar-p nil))
(*map-vectors-nocheck
#'(lambda (element)
(*set found nil!!)
(if test
(if olditem
(*set found (the (pvar boolean) (funcall test olditem (funcall key element))))
(*set found (the (pvar boolean) (funcall test (funcall key element))))
)
(if olditem
(*set found (not!! (the (pvar boolean) (funcall test-not olditem (funcall key element)))))
(*set found (not!! (the (pvar boolean) (funcall test-not (funcall key element)))))
))
(*when found
(declare (return-pvar-p nil))
(if count
(*nocompile
(*when (<!! number-substituted count)
(declare (return-pvar-p nil))
(*set element newitem)
)
(*compile () (*set number-substituted (1+!! number-substituted)))
)
(*nocompile (*set element newitem))
)))
start end from-end nil sequence
)
)))))))
sequence
)
(defun nsubstitute!!
(newitem olditem sequence
&key
(from-end nil) (test nil) (test-not nil)
(start (!! 0)) (end (length!! sequence))
(count nil) (key #'identity)
)
(simple-pvar-argument!! newitem olditem &opt from-end start end count)
(safety-check
(sequence-function-pvar-args-check 'nsubstitute!! sequence newitem olditem start end)
(if count (new-vp-pvar-check count 'nsubstitute!!))
)
(check-test-test-not test test-not)
(internal-substitute!! newitem olditem sequence start end from-end test test-not key count)
)
(defun nsubstitute-if!!
(newitem test sequence
&key (from-end nil) (start (!! 0)) (end (length!! sequence)) (key #'identity) (count nil)
)
(simple-pvar-argument!! newitem &opt from-end start end count)
(safety-check
(sequence-function-pvar-args-check 'nsubstitute-if!! sequence newitem start end)
(if count (new-vp-pvar-check count 'nsubstitute-if!!))
)
(internal-substitute!! newitem nil sequence start end from-end test nil key count)
)
(defun nsubstitute-if-not!!
(newitem test sequence
&key (from-end nil) (start (!! 0)) (end (length!! sequence)) (key #'identity) (count nil)
)
(simple-pvar-argument!! newitem &opt from-end start end count)
(safety-check
(sequence-function-pvar-args-check 'nsubstitute-if-not!! sequence newitem start end)
(if count (new-vp-pvar-check count 'nsubstitute-if-not!!))
)
(internal-substitute!! newitem nil sequence start end from-end nil test key count)
)
(defun substitute!!
(newitem olditem sequence
&key
(from-end nil) (test nil) (test-not nil)
(start (!! 0)) (end (length!! sequence))
(count nil) (key #'identity)
)
(simple-pvar-argument!! sequence)
(nsubstitute!! newitem olditem (copy-seq!! sequence)
:from-end from-end :test test :test-not test-not :start start :end end :count count :key key
))
(defun substitute-if!!
(newitem test sequence
&key (from-end nil) (start (!! 0)) (end (length!! sequence)) (key #'identity) (count nil)
)
(simple-pvar-argument!! sequence)
(nsubstitute-if!! newitem test (copy-seq!! sequence)
:from-end from-end :start start :end end :count count :key key
))
(defun substitute-if-not!!
(newitem test sequence
&key (from-end nil) (start (!! 0)) (end (length!! sequence)) (key #'identity) (count nil)
)
(simple-pvar-argument!! sequence)
(nsubstitute-if-not!! newitem test (copy-seq!! sequence)
:from-end from-end :start start :end end :count count :key key
))
(defun-wcefi internal-find!! (sequence start end item from-end test test-not key return-value-if-not-found)
(without-void-sequence-pvar sequence find!!
(without-void-pvars (start end)
(let ((vlength (array-pvar-total-size sequence)))
(when (zerop vlength)
(check-zero-length-sequence-start-and-end start end)
(return-from internal-find!! (if return-value-if-not-found return-value-if-not-found nil!!))
)
(with-start-and-end-checked-and-coerced
start end vlength 'find!!
#'(lambda (start end)
(*compile ()
(let!! ((found nil!!))
(declare (type (pvar boolean) found))
(*let (result)
(*map-vectors-nocheck
#'(lambda (element)
(*when (not!! found)
(if test
(if item
(*set found (the (pvar boolean) (funcall test item (funcall key element))))
(*set found (the (pvar boolean) (funcall test (funcall key element))))
)
(if item
(*set found (not!! (the (pvar boolean) (funcall test-not item (funcall key element)))))
(*set found (not!! (the (pvar boolean) (funcall test-not (funcall key element)))))
))
(*when found (*nocompile (*set result element)))
))
start end from-end nil sequence
)
(if (*and found)
result
(*nocompile
(if return-value-if-not-found
(if!! found result return-value-if-not-found)
(if!! found result nil!!)
)))
)))))))))
(defun find!! (item sequence
&key
(from-end nil) (test nil) (test-not nil)
(start (!! 0)) (end (length!! sequence))
(key #'identity) (return-value-if-not-found nil)
)
(simple-pvar-argument!! item sequence &opt from-end start end return-value-if-not-found)
(safety-check
(sequence-function-pvar-args-check 'find!! sequence item start end)
(if return-value-if-not-found (new-vp-pvar-check return-value-if-not-found 'find!!))
)
(check-test-test-not test test-not)
(internal-find!! sequence start end item from-end test test-not key return-value-if-not-found)
)
(defun find-if!! (test sequence
&key (from-end nil) (start (!! 0)) (end (length!! sequence)) (key #'identity) (return-value-if-not-found nil))
(simple-pvar-argument!! sequence &opt from-end start end return-value-if-not-found)
(safety-check
(sequence-function-pvar-args-check 'find-if!! sequence start end)
(if return-value-if-not-found (new-vp-pvar-check return-value-if-not-found 'find-if!!))
)
(internal-find!! sequence start end nil from-end test nil key return-value-if-not-found)
)
(defun find-if-not!!
(test sequence
&key
(from-end nil) (start (!! 0)) (end (length!! sequence))
(key #'identity) (return-value-if-not-found nil)
)
(simple-pvar-argument!! sequence &opt from-end start end return-value-if-not-found)
(safety-check
(sequence-function-pvar-args-check 'find-if-not!! sequence start end)
(if return-value-if-not-found (new-vp-pvar-check return-value-if-not-found 'find-if-not!!!!))
)
(internal-find!! sequence start end nil from-end nil test key return-value-if-not-found)
)
(*proclaim '(ftype (function (t t &rest t) (pvar (signed-byte *))) position!! position-if!! position-if-not!!))
(*proclaim '(ftype (function (t t &rest t) (pvar (unsigned-byte *))) count!! count-if!! count-if-not!!))
;;; Position!! returns -1 in processors in which the
;;; item was not found. This is in violation of CLtL,
;;; which has position return NIL. Tough cookies.
(defun-wcefi internal-position!! (sequence start end item from-end test test-not key)
(without-void-sequence-pvar sequence position!!
(without-void-pvars (start end)
(let ((vlength (array-pvar-total-size sequence)))
(when (zerop vlength)
(check-zero-length-sequence-start-and-end start end)
(return-from internal-position!! (!! -1))
)
(with-start-and-end-checked-and-coerced
start end vlength 'position!!
#'(lambda (start end)
(*compile ()
(let!! (position (found nil!!))
(declare (type (pvar boolean) found))
(declare (type (pvar (signed-byte 32)) position))
(*map-vectors-nocheck
#'(lambda (index element)
(*when (not!! found)
(if test
(if item
(*set found (the (pvar boolean) (funcall test item (funcall key element))))
(*set found (the (pvar boolean) (funcall test (funcall key element))))
)
(if item
(*set found (not!! (the (pvar boolean) (funcall test-not item (funcall key element)))))
(*set found (not!! (the (pvar boolean) (funcall test-not (funcall key element)))))
))
(*when found (*set position (!! (the fixnum index))))
))
start end from-end t sequence
)
(*when (not!! found) (declare (return-pvar-p nil)) (*set position (!! -1)))
position
))))))))
(defun position!! (item sequence
&key (from-end nil) (test nil) (test-not nil) (start (!! 0)) (end (length!! sequence)) (key #'identity)
)
(simple-pvar-argument!! item sequence &opt from-end start end)
(safety-check
(sequence-function-pvar-args-check 'position!! sequence item start end)
)
(check-test-test-not test test-not)
(internal-position!! sequence start end item from-end test test-not key)
)
(defun position-if!! (test sequence &key (from-end nil) (start (!! 0)) (end (length!! sequence)) (key #'identity))
(simple-pvar-argument!! sequence &opt from-end start end)
(safety-check
(sequence-function-pvar-args-check 'position-if!! sequence start end)
)
(internal-position!! sequence start end nil from-end test nil key)
)
(defun position-if-not!! (test sequence &key (from-end nil) (start (!! 0)) (end (length!! sequence)) (key #'identity))
(simple-pvar-argument!! sequence &opt from-end start end)
(safety-check
(sequence-function-pvar-args-check 'position-if-not!! sequence start end)
)
(internal-position!! sequence start end nil from-end nil test key)
)
(defun-wcefi internal-count!! (sequence start end item test test-not key)
(without-void-sequence-pvar sequence count!!
(without-void-pvars (start end)
(let ((vlength (array-pvar-total-size sequence)))
(when (zerop vlength)
(check-zero-length-sequence-start-and-end start end)
(return-from internal-count!! (!! 0))
)
(with-start-and-end-checked-and-coerced
start end vlength 'count!!
#'(lambda (start end)
(*compile ()
(let!! ((count (!! 0)) found)
(declare (type (pvar (unsigned-byte 32)) count))
(declare (type (pvar boolean) found))
(*map-vectors-nocheck
#'(lambda (element)
(if test
(if item
(*set found (the (pvar boolean) (funcall test item (funcall key element))))
(*set found (the (pvar boolean) (funcall test (funcall key element))))
)
(if item
(*set found (not!! (the (pvar boolean) (funcall test-not item (funcall key element)))))
(*set found (not!! (the (pvar boolean) (funcall test-not (funcall key element)))))
))
(*when found (declare (return-pvar-p nil)) (*set count (+!! (!! 1) count)))
)
start end nil nil sequence
)
count
))))))))
(defun count!! (item sequence
&key (from-end nil) (test nil) (test-not nil) (start (!! 0)) (end (length!! sequence)) (key #'identity)
)
(declare (ignore from-end))
(simple-pvar-argument!! item sequence
; from-end ;;; Uncomment when no longer ignored
start end)
(safety-check
(sequence-function-pvar-args-check 'count!! sequence item start end)
)
(check-test-test-not test test-not)
(internal-count!! sequence start end item test test-not key)
)
(defun count-if!! (test sequence &key (from-end nil) (start (!! 0)) (end (length!! sequence)) (key #'identity))
(declare (ignore from-end))
(simple-pvar-argument!! sequence
; from-end ;;; Uncomment when no longer ignored
start end)
(safety-check (sequence-function-pvar-args-check 'count-if!! sequence start end))
(internal-count!! sequence start end nil test nil key)
)
(defun count-if-not!! (test sequence &key (from-end nil) (start (!! 0)) (end (length!! sequence)) (key #'identity))
(declare (ignore from-end))
(simple-pvar-argument!! sequence
; from-end ;;; Uncomment when no longer ignored
start end)
(sequence-function-pvar-args-check 'count-if-not!! sequence start end)
(internal-count!! sequence start end nil nil test key)
)
;;; Tests for the sequence functions.
(defun-wco test-sequence-functions ()
(*nocompile
(flet
((assert-equalp (a1 a2 tag)
(let ((*print-array* t))
(assert (*and (equalp!! a1 a2)) ()
"The arrays for tag ~S are not equalp. (PREF A1 0): ~A, (PREF A2 0): ~A"
tag (pref a1 0) (pref a2 0)
)))
(assert-true (pvar tag)
(assert (*and pvar) () "The pvar for tag ~S is not everywhere true." tag)
)
(note (name) (format t "~S, " name))
)
(macrolet
((test-equalp (a1 a2) `(*let () (assert-equalp ,a1 ,a2 ',(car a1))))
(test-true (x) `(*let () (assert-true ,x ',(car x))))
)
(*let ((sequence (!! '#(0 1 2 3 4 5))))
(declare (type (pvar (array (unsigned-byte 8) (6))) sequence))
(format t "~%Testing sequence functions.~%")
;; subseq
(test-equalp (subseq!! sequence (!! 1)) (!! '#(1 2 3 4 5)))
(test-equalp (subseq!! sequence (!! 0) (!! 2)) (!! '#(0 1)))
(test-equalp (subseq!! sequence (!! 2) (!! 5)) (!! '#(2 3 4)))
(note 'subseq!!)
;; copy-seq
(test-equalp (copy-seq!! sequence) sequence)
(note 'copy-seq!!)
;; reverse
(test-equalp (reverse!! sequence) (!! '#(5 4 3 2 1 0)))
(test-equalp (reverse!! (subseq!! sequence (!! 3))) (!! '#(5 4 3)))
(test-equalp (reverse!! (subseq!! sequence (!! 2) (!! 3))) (!! '#(2)))
(note 'reverse!!)
;; some
(test-true (some!! 'numberp!! sequence))
(test-true (some!! 'oddp!! sequence))
(test-true (some!! #'(lambda (x) (=!! x (!! 4))) sequence))
(note 'some!!)
;; every
(test-true (every!! 'numberp!! sequence))
(test-true (every!! 'plusp!! (subseq!! sequence (!! 1))))
(test-true (every!! #'(lambda (x) (<!! x (!! 20))) sequence))
(note 'every!!)
;; notany
(test-true (notany!! 'characterp!! sequence))
(test-true (notany!! 'minusp!! sequence))
(test-true (notany!! #'(lambda (x) (>!! x (!! 20))) sequence))
(note 'notany!!)
;; notevery
(test-true (notevery!! 'oddp!! sequence))
(test-true (notevery!! 'plusp!! sequence))
(test-true (notevery!! #'(lambda (x) (=!! x (!! 3))) sequence))
(note 'notevery!!)
;; reduce
(test-true (=!! (!! 0) (reduce!! 'min!! sequence :initial-value (!! 20))))
(test-true (=!! (!! 3) (reduce!! '+!! sequence :start (!! 0) :end (!! 3))))
(test-true (=!! (!! 24) (reduce!! '*!! sequence :start (!! 2) :end (!! 5))))
(test-true (=!! (!! -15) (reduce!! '-!! sequence)))
(test-true (=!! (!! -3) (reduce!! '-!! sequence :from-end t)))
(note 'reduce!!)
;; *fill
(*fill sequence (!! 0))
(test-true (every!! 'zerop!! sequence))
(*fill sequence (!! 2) :start (!! 1) :end (!! 3))
(test-true (every!! #'(lambda (x) (=!! x (!! 2))) (subseq!! sequence (!! 1) (!! 3))))
(*set sequence (!! '#(0 1 2 3 4 5)))
(note '*fill)
;; substitute
(test-equalp (substitute!! (!! 8) (!! 0) sequence) (!! '#(8 1 2 3 4 5)))
(test-equalp (substitute!! (!! 8) (!! 3) sequence :test '>!!) (!! '#(8 8 8 3 4 5)))
(test-equalp (substitute-if!! (!! 8) 'oddp!! sequence :start (!! 2)) (!! '#(0 1 2 8 4 8)))
(test-equalp (substitute-if-not!! (!! 8) #'(lambda (x) (not!! (plusp!! x))) sequence :end (!! 3)) (!! '#(0 8 8 3 4 5)))
(note 'substitute!!)
;; find
(test-true (=!! (find!! t!! sequence :key #'oddp!!) (!! 1)))
(test-true (=!! (find!! t!! sequence :key #'oddp!! :from-end t) (!! 5)))
(test-true (=!! (find-if!! #'oddp!! sequence :start (!! 2)) (!! 3)))
(test-true (=!! (find-if!! #'oddp!! sequence :from-end t :end (!! 4)) (!! 3)))
(note 'find!!)
;; position
(test-true (=!! (position!! (!! 3) sequence) (!! 3)))
(test-true (=!! (position!! t!! sequence :key #'oddp!!) (!! 1)))
(test-true (=!! (position-if!! #'oddp!! sequence :from-end t) (!! 5)))
(test-true (=!! (position-if!! #'minusp!! sequence :from-end t :key #'(lambda (x) (-!! x (!! 3)))) (!! 2)))
(note 'position!!)
;; count
(test-true (=!! (count!! (!! 2) sequence) (!! 1)))
(test-true (=!! (count!! (!! -1) sequence) (!! 0)))
(test-true (=!! (count-if!! #'oddp!! sequence) (!! 3)))
(test-true (=!! (count-if-not!! #'zerop!! sequence :start (!! 2) :end (!! 4) :from-end t :key #'1-!!) (!! 2)))
(note 'count!!)
(format t "~%Finished simple sequence tests~%")
)))))