-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlist.lisp
1306 lines (1188 loc) · 53.3 KB
/
list.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
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;;; Copyright 2020 Google LLC
;;;
;;; Use of this source code is governed by an MIT-style
;;; license that can be found in the LICENSE file or at
;;; https://opensource.org/licenses/MIT.
;;; Utilities related to lists and conses.
;;; The package is designed to be used by other packages.
;;;
(ace.core:defpackage* #:ace.core.list
(:use #:common-lisp
#:ace.core.once-only
#:ace.core.defun
#:ace.core.collect)
(:import-from #:ace.core.macro
ace.core.macro:find-type-declaration
ace.core.macro:with-gensyms
ace.core.macro:eval*
ace.core.macro:lexenv
ace.core.macro:gensym*
ace.core.macro:gensymp
ace.core.macro:eval-always)
(:shadow cl:find
cl:find-if
cl:find-if-not
cl:position
cl:position-if
cl:position-if-not
cl:remove
cl:remove-if
cl:remove-if-not
cl:delete
cl:delete-if
cl:delete-if-not
cl:length)
(:export
#:length
#:length=
#:elements-eq
#:unordered-equal
#:undot
#:match
#:copy-if #:copy-if-not
#:find #:find-if #:find-if-not
#:position #:position-if #:position-if-not
#:remove #:remove-if #:remove-if-not
#:delete #:delete-if #:delete-if-not
#:delete-adjacent
#:removef
#:removef-if
#:removef-if-not
#:deletef
#:deletef-if
#:deletef-if-not
#:partition
#:npartition
#:dolist*
#:car-or-atom
#:mappend
#:intersectp
#:push*
#:npush*
#:appendf
#:nconcf
#:nreversef
#:unionf
#:intersectionf))
(cl:in-package #:ace.core.list)
(deftype position () "Position on the list." `(integer 0 ,array-dimension-limit))
(deftype position* () "Position on the list or NIL." '(or position null))
(deftype item-count () "Count of elements." `(integer 0 ,array-dimension-limit))
(deftype item-count* () "Count of elements or NIL." '(or item-count null))
(deftype function$ () "Function or symbol" '(or function symbol))
(defun* length (list)
"Returns the length of the LIST.
No precautions are made as to if the LIST is circular."
(declare (self inline foldable (list) position))
(cl:length list))
(defun* length= (list1 list2)
"Return true if the lengths of LIST1 and LIST2 are equal."
(declare (self inline (list list) boolean))
(do ((l1 list1 (cdr l1))
(l2 list2 (cdr l2)))
((not (and l1 l2))
(not (or l1 l2)))))
(defun* elements-eq (list1 list2)
"True if LIST1 and LIST2 are equal lists - with elements compared using EQ.
The lists need to be in equal length for the function to return true.
The function will also return true if the lists or their sub-lists compare EQ."
(declare (self inline (list list) boolean))
(do ((l1 list1 (cdr l1))
(l2 list2 (cdr l2)))
((eq l1 l2) t)
(unless (eq (car l1) (car l2))
(return nil))))
(defun* unordered-equal (list1 list2 &key (test #'eql) (key #'identity))
"True if LIST1 and LIST2 are the same up to permutation of elements.
KEY extracts a value to compare, and TEST determines how values are compared.
TEST must be one of EQ, EQL, EQUAL, or EQUALP."
(declare (self inline (list list &key function function) boolean))
(let* ((size (+ (length list1) (length list2)))
(ht (make-hash-table :size size :test test)))
(dolist (e list1) (incf (gethash (funcall key e) ht 0)))
(dolist (e list2) (decf (gethash (funcall key e) ht 0)))
(loop :for count :being :each :hash-value :of ht
:always (zerop count))))
(defun* undot (list)
"Return LIST. If LIST is dotted,
return a copy that is a proper list."
(declare (self inline (list) list))
(let ((last (last list)))
(if (cdr last)
(nconc (butlast list) (list (car last) (cdr last)))
list)))
(defun* match (list1 list2 &key
(test #'eql) (key #'identity)
(start1 0) (start2 0))
"Compares elements of LIST1 and LIST2 using TEST function over KEY values.
Parameters START1 and START2 determine where the comparison starts on each of
the lists, respectively.
True if there is a match between the lists.
The second and third value are sub-list at position where the
mismatch (or match) was determined.
The LIST:MATCH will short the comparison if the lists or their sub-list are EQ.
In this case (values T <sub-LIST1> <sub-LIST2>) are returned.
"
(declare (self inline (list list &key function function) boolean t t))
(do ((l1 (nthcdr start1 list1) (cdr l1))
(l2 (nthcdr start2 list2) (cdr l2)))
((eq l1 l2) (values t l1 l2))
(unless (and l1 l2 (funcall test
(funcall key (car l1))
(funcall key (car l2))))
(return (values nil l1 l2)))))
(define-modify-macro appendf (&rest more-lists) append
"Modify macro that appends the first argument (PLACE) and MORE-LISTS together
and assigns the result to PLACE.")
(define-modify-macro nconcf (&rest more-lists) nconc
"Modify macro that concatenates the first argument (PLACE) and MORE-LISTS together
and assigns the result to PLACE. The operation may be destructive for all arguments.")
(define-modify-macro nreversef () nreverse
"Modify macro that reverses the first argument (PLACE) and assigns the result to it.")
(define-modify-macro intersectionf (list2 &rest args) intersection
"Modify macro that creates an intersection of the first argument (PLACE) and LIST2
and assigns the result to PLACE.")
(define-modify-macro unionf (list2 &rest args) union
"Modify macro that creates a union of the first argument (PLACE) and LIST2
and assigns the result to PLACE.")
;;;
;;; FIND(-*) and POSITION(-*) functions with implicit LIST type declaration.
;;;
(defconstant +find-constant-list-unfold-length+ 7
"Constant list up to this length are unfolded instead of creating a LOOP.")
(eval-always
(defun %find-if-form (whole predicate list &rest args &key start end from-end key)
(once-only ((predicate `(coerce ,predicate 'function))
list
&rest args &key start end from-end
(key `(coerce (or ,key #'identity) 'function)))
(declare (list list) (position start) (position* end)
(function predicate key)
(inline predicate key)
(dynamic-extent predicate key))
;; Use a LOOP
(let* ((when/unless (ecase (first whole)
((find position find-if position-if) 'when)
((find-if-not position-if-not) 'unless)))
(posp (cl:find (first whole) '(position position-if position-if-not) :test #'eq))
(%pos (and (or posp end) (gensym* :pos)))
(%item (gensym "ITEM-"))
;; just for aesthetics.
(key (unless (eq key '#'identity) key))
(%result (and from-end (gensym* :result)))
(env (lexenv))
(unfold
(and (constantp list env)
(constantp start env)
(constantp end env)
(not from-end)))
(start-value (and unfold (if start (eval* start) 0)))
(end-value (and unfold end (eval* end)))
(list-items (and unfold list (eval* list)))
(list-length (and unfold (length list-items)))
(block (and unfold (gensym* :block))))
(if (and list-length (<= list-length +find-constant-list-unfold-length+))
;; Unfold the list.
`(block ,block
,@(loop
:for item :in (nthcdr start-value list-items)
:for pos :from (or start-value 0)
:while (or (null end-value) (< pos end-value))
:collect
`(,when/unless
(funcall ,predicate
,(if key `(funcall ,key ',item) `(quote ,item)))
(return-from ,block ,(if posp pos `(quote ,item))))))
;; Use LOOP
`(loop
,@(when %result `(:with ,%result = nil))
,@(when %pos `(:for ,%pos :of-type position :from ,(or start 0)))
,@(cond ((gensymp end) `(:while (or (null ,end) (< ,%pos ,end))))
(end `(:while (< ,%pos ,end))))
:for ,%item :in ,(if start `(nthcdr ,start ,list) list)
:do
(,when/unless
(funcall ,predicate ,(if key `(funcall ,key ,%item) %item))
,@(when %result `((setf ,%result ,(if posp %pos %item))))
,@(case from-end
((t) nil)
((nil) `((return ,(or %result (if posp %pos %item)))))
(t `((unless ,from-end (return ,%result))))))
;; FINALLY
,@(when %result `(:finally (return ,%result)))))))))
(eval-always
(defun %remove-if-form (whole predicate list &rest args &key start end from-end count key)
(once-only ((predicate `(coerce ,predicate 'function))
list
&rest args &key start end from-end count
(key `(coerce (or ,key #'identity) 'function)))
(declare (list list) (position start) (position* end)
(type item-count* count)
(function predicate key)
(inline predicate key)
(dynamic-extent predicate key))
;; Use a LOOP
(let* ((count-function
(ecase (first whole)
((delete delete-if delete-if-not) 'delete-count)
((remove remove-if remove-if-not) 'remove-count)
((copy-if copy-if-not) 'copy-if-count)))
(simple-function
(ecase (first whole)
((delete delete-if delete-if-not) '%%delete)
((remove remove-if remove-if-not) '%%remove)
((copy-if copy-if-not) '%%copy-if)))
(%item (gensym* :item))
;; just for aesthetics.
(key (unless (eq key '#'identity) key))
(env (lexenv))
(pred/key
(cond
((member (first whole) '(remove-if-not delete-if-not copy-if-not))
`(lambda (,%item)
(not (funcall
,predicate ,(if key `(funcall ,key ,%item) %item)))))
(key
`(lambda (,%item)
(funcall ,predicate (funcall ,key ,%item))))
(t
predicate))))
;; Need a dynamic extent FLET here.
(once-only (pred/key)
(declare (function pred/key)
(inline pred/key)
(dynamic-extent pred/key))
(if (and count (or (not (constantp count env)) (eval* count env)))
`(,count-function ,pred/key ,list ,start ,end ,count ,from-end)
`(,simple-function ,pred/key ,list (or ,start 0) ,end)))))))
(eval-always
(defun %find/remove-form (whole item list &rest args
&key start end from-end key test test-not count)
(once-only (item list
&rest args
&key start end from-end key test test-not count)
(declare (type (or function null) key test test-not)
(inline key test test-not)
(dynamic-extent key test test-not))
(let* ((x item)
(y (gensym "Y-"))
(predicate
(cond ((and test test-not (not (gensymp test)) (not (gensymp test-not)))
(warn "Both TEST and TEST-NOT specified in ~A." whole) ; NOLINT
`(error "Both TEST and TEST-NOT used in ~A." ',(first whole)))
((or (gensymp test) (gensymp test-not))
(let ((%test (if (gensymp test) test (and test t)))
(%test-not (if (gensymp test-not) test-not (and test-not t))))
`(cond ((and ,%test ,%test-not)
(error "Both TEST and TEST-NOT used in ~A." ',(first whole)))
(,%test (lambda (,y) (funcall ,test ,x ,y)))
(,%test-not (lambda (,y) (not (funcall ,test-not ,x ,y))))
(t (lambda (,y) (eql ,x ,y))))))
(test-not `(lambda (,y) (not (funcall ,test-not ,x ,y))))
(test `(lambda (,y) (funcall ,test ,x ,y)))
(t `(lambda (,y) (eql ,x ,y))))))
(cond ((member (car whole) '(delete remove))
(%remove-if-form
whole predicate list
:start start :end end
:from-end from-end :key key :count count))
((member (car whole) '(find position))
(when count
(warn "~A does not accept a COUNT argument." (car whole))) ; NOLINT
(%find-if-form
whole predicate list
:start start :end end
:from-end from-end :key key)))))))
(defun* find (item list &key from-end (start 0) end key test test-not)
"Find ITEM in a LIST. This is only marginally faster than CL:FIND
because it assumes that the sequence is a LIST and KEY, TEST, TEST-NOT are functions.
Parameters: FROM-END, START, END, KEY, TEST, TEST-NOT - as in CL:FIND."
(declare (self (t list &key
(:key function$)
(:test function$)
(:test-not function$))
t))
(cl:find item (the list list)
:from-end from-end :start start :end end :key key
:test test :test-not test-not))
(define-compiler-macro find (&whole whole &rest args)
(apply #'%find/remove-form whole args))
(defun* find-if (predicate list &key from-end (start 0) end key)
"Find an item in a LIST that matches the PREDICATE.
This is only marginally faster than CL:FIND-IF
because it assumes that PREDICATE is a function and the sequence is a LIST.
Parameters: FROM-END, START, END, KEY - as in CL:FIND-IF."
(declare (self (function$ list &key (:key function$)) t))
(cl:find-if
predicate (the list list) :from-end from-end :start start :end end :key key))
(define-compiler-macro find-if (&whole whole &rest args)
(apply #'%find-if-form whole args))
(defun* find-if-not (predicate list &key from-end (start 0) end key)
"Find an item in a LIST that matches the PREDICATE.
This is only marginally faster than CL:FIND-IF-NOT
because it assumes that PREDICATE is a function and the sequence is a LIST.
Parameters: FROM-END, START, END, KEY - as in CL:FIND-IF-NOT."
(declare (self (function$ list &key (:key function$)) t))
(cl:find-if-not
predicate (the list list) :from-end from-end :start start :end end :key key))
(define-compiler-macro find-if-not (&whole whole &rest args)
(apply #'%find-if-form whole args))
(defun* position (item list &key from-end (start 0) end key test test-not)
"Find the position of the ITEM in a LIST. This is only marginally faster than CL:POSITION
because it assumes that the sequence is a LIST and KEY, TEST, TEST-NOT are functions.
Parameters: FROM-END, START, END, KEY, TEST, TEST-NOT - as in CL:POSITION."
(declare (self (t list &key
(:key function$)
(:test function$)
(:test-not function$))
position*))
(cl:position item (the list list)
:from-end from-end :start start :end end
:key key :test test :test-not test-not))
(define-compiler-macro position (&whole whole &rest args)
(apply #'%find/remove-form whole args))
(defun* position-if (predicate list &key from-end (start 0) end key)
"Find the position of an ITEM in a LIST for which the PREDICATE is true.
This is only marginally faster than CL:POSITION-IF because it assumes
that the sequence is a LIST and KEY, TEST, TEST-NOT are functions.
Parameters: FROM-END, START, END, KEY - as in CL:POSITION-IF."
(declare (self (function$ list &key (:key function$)) position*))
(cl:position-if predicate (the list list)
:from-end from-end :start start :end end :key key))
(define-compiler-macro position-if (&whole whole &rest args)
(apply #'%find-if-form whole args))
(defun* position-if-not (predicate list &key from-end (start 0) end key)
"Find the position of an ITEM in a LIST for which the PREDICATE is NIL.
This is only marginally faster than CL:POSITION-IF because it assumes
that the sequence is a LIST and KEY, TEST, TEST-NOT are functions.
Parameters: FROM-END, START, END, KEY - as in CL:POSITION-IF."
(declare (self (function$ list &key (:key function$)) position*))
(cl:position-if-not
predicate (the list list) :from-end from-end :start start :end end :key key))
(define-compiler-macro position-if-not (&whole whole &rest args)
(apply #'%find-if-form whole args))
;;;
;;; REMOVE(-*) and DELETE(-*) functions with implicit LIST type declaration.
;;;
;;; (cl:remove item sequence
;;; &key from-end (test #'eql) test-not (start 0) end count key)
;;;
;;; (cl:remove-if predicate sequence &key from-end (start 0) end count key)
;;;
;;; (cl:delete item sequence
;;; &key from-end (test #'eql) test-not (start 0) end count key)
;;;
;;; (cl:delete-if predicate sequence &key from-end (start 0) end count key)
(defun* %%delete (pred list start end)
(declare (self (function list position position*) list))
(cond
((null end)
(let ((handle (cons nil list)))
(declare (dynamic-extent handle))
(do* ((previous (nthcdr start handle))
(current (cdr previous) (cdr current)))
((null current)
(cdr handle))
(if (funcall pred (car current))
(rplacd previous (cdr current))
(pop previous)))))
((< start end)
(let ((handle (cons nil list)))
(declare (dynamic-extent handle))
(do* ((previous (nthcdr start handle))
(current (cdr previous) (cdr current))
(index start (1+ index)))
((or (null current) (= index end))
(cdr handle))
(declare (position index))
(if (funcall pred (car current))
(rplacd previous (cdr current))
(pop previous)))))
(list)))
;; TODO(czak, dougk): Make SBCL inline stuff properly.
(define-compiler-macro %%delete (&whole w pred list start end &environment env)
(if (and (constantp end env) (null (eval* end env)))
;; inline
(with-gensyms (%handle %prev %cur)
(once-only (pred start)
`(let ((,%handle (cons nil ,list)))
(declare (dynamic-extent ,%handle))
(do* ((,%prev ,(if (member start '(nil 0))
%handle
`(nthcdr ,start ,%handle)))
(,%cur (cdr ,%prev) (cdr ,%cur)))
((null ,%cur)
(cdr ,%handle))
(if (funcall ,pred (car ,%cur))
(rplacd ,%prev (cdr ,%cur))
(pop ,%prev))))))
w))
(defun* %%delete-count (pred list start end count)
(declare (self inline (function list position position item-count) list))
(let ((handle (cons nil list)))
(declare (position start end) (dynamic-extent handle))
(do* ((previous (nthcdr start handle))
(current (cdr previous) (cdr current))
(index start (1+ index)))
;; Assume (<= end len) ->
;; no need to check for (null current) here.
((or (= index end) (zerop count))
(cdr handle))
(declare (position index))
(cond ((funcall pred (car current))
(rplacd previous (cdr current))
(decf count))
(t
(pop previous))))))
(defun* delete-count (predicate list start end count &optional from-end)
"Delete items matching PREDICATE from the LIST.
The LIST will be modified destructively.
Parameters:
PREDICATE - a boolean function.
LIST - the original list that will be modified.
START - index on the list where deleting starts (default 0).
END - index on the list before which the deleting stops (default end of list).
COUNT - number of items to delete.
FROM-END - if true the list will be reversed twice.
"
(declare
(self (function list position* position* item-count* &optional t) list))
(cond
((null list) nil)
((null count)
(%%delete predicate list (or start 0) end))
((zerop count) list)
((let* ((start (or start 0))
(len (length list))
(end (if end (min end len) len)))
(declare (position start end) (item-count len))
(cond
((>= start end) list)
((>= count (- end start))
;; Just simply call %%remove
(%%delete predicate list start end))
((not from-end)
(%%delete-count predicate list start end count))
(t
;; Need to reverse the list twice.
(nreverse
(%%delete-count
predicate (nreverse list)
(- len end) (- len start) count))))))))
;; remove
(defun* %%remove (pred list start end)
(declare (self (function list position position*) list))
(cond ((null end)
(loop
:for items. :on (nthcdr start list)
:when (funcall pred (car items.))
:do (return
(with-collected-values ((collect :mode :nconc))
(collect (ldiff list items.)) ; copy
(loop
:with rem. = (cdr items.)
:for items. :on rem.
:do (when (funcall pred (car items.))
(collect (ldiff rem. items.)) ; copy
(setf rem. (cdr items.)))
:finally (collect rem.)))) ; tail
:finally (return list)))
((< start end)
(loop
:for items. :on (nthcdr start list)
:for i :of-type position :from start :below end
:when (funcall pred (car items.))
:do (return
(with-collected-values ((collect :mode :nconc))
(collect (subseq list 0 i)) ; copy
(loop
:with rem. = (cdr items.)
:for items. :on rem.
:for j :of-type position :from (1+ i) :below end
:do (when (funcall pred (car items.))
(collect (ldiff rem. items.)) ; copy
(setf rem. (cdr items.)))
:finally (collect rem.)))) ; tail
:finally (return list)))
(list)))
;; TODO(czak, dougk): Make SBCL inline stuff properly.
(define-compiler-macro %%remove (&whole w pred list start end &environment env)
(if (and (constantp end env) (null (eval* end env)))
;; inline
(with-gensyms (%items. %rem. %collect)
(once-only (pred list start)
`(loop
:for ,%items. :on ,(if (member start '(nil 0))
list
`(nthcdr ,start ,list))
:when (funcall ,pred (car ,%items.))
:do (return
(with-collected-values ((,%collect :mode :nconc))
(,%collect (ldiff ,list ,%items.)) ; copy
(loop
:with ,%rem. = (cdr ,%items.)
:for ,%items. :on ,%rem.
:do (when (funcall ,pred (car ,%items.))
(,%collect (ldiff ,%rem. ,%items.)) ; copy
(setf ,%rem. (cdr ,%items.)))
:finally (,%collect ,%rem.)))) ; tail
:finally (return ,list))))
w))
(defun* %%remove-count (pred list start end count)
(declare (self inline (function list position position item-count) list))
(loop
:for items. :on (nthcdr start list)
:for i :of-type position :from start :below end
:when (funcall pred (car items.))
:do (decf count)
(return
(with-collected-values ((collect :mode :nconc))
(collect (subseq list 0 i)) ; copy
(loop
:with rem. = (cdr items.)
:while (plusp count)
:for items. :on rem.
:for j :of-type position :from (1+ i) :below end
:do (when (funcall pred (car items.))
(collect (ldiff rem. items.)) ; copy
(setf rem. (cdr items.))
(decf count))
:finally (collect rem.)))) ; tail
:finally (return list)))
(defun* remove-count (predicate list start end count &optional from-end)
"Remove items matching PREDICATE from the LIST.
The LIST will not be modified, but its tail maybe reused in the new list.
Parameters:
PREDICATE - a boolean function.
LIST - the original list that will be modified.
START - index on the list where removing starts (default 0).
END - index on the list before which the removing stops (default end of list).
COUNT - number of items to remove.
FROM-END - if true the list will be reversed twice.
"
(declare
(self (function list position* position* item-count* &optional t) list))
(cond
((null list) nil)
((null count)
(%%remove predicate list (or start 0) end))
((zerop count) list)
((let* ((start (or start 0))
(len (length list))
(end (if end (min end len) len)))
(declare (position start end) (item-count len))
(cond
((>= start end) list)
((>= count (- end start))
;; Just simply call %%remove
(%%remove predicate list start end))
((not from-end)
(%%remove-count predicate list start end count))
(t
;; Need to reverse the list twice.
(nreverse
(%%delete-count
predicate (reverse list) ; copy
(- len end) (- len start) count))))))))
(defun* remove-if (predicate list &key from-end (start 0) end key count)
"Remove any item in the LIST that matches the PREDICATE.
This operation is not destructive to the LIST.
Unlike CL:REMOVE-IF, this will return the original LIST unless something was
actually removed from it. An unchanged tail of the LIST, maybe also be returned.
Parameters: FROM-END, START, END, KEY, COUNT - as in CL:REMOVE-IF.
"
(declare (self (function$ list &key (:key function$)) t))
(let ((predicate (coerce predicate 'function))
(key (and key (coerce key 'function))))
(labels ((pred (x) (funcall predicate x))
(pred/key (x) (funcall #'pred (funcall key x))))
(declare (dynamic-extent #'pred #'pred/key) (inline pred pred/key))
(remove-count (if key #'pred/key #'pred) list start end count from-end))))
(define-compiler-macro remove-if (&whole whole &rest args)
(apply #'%remove-if-form whole args))
(defun* remove-if-not (predicate list &key from-end (start 0) end key count)
"Remove any item in the LIST that does not match the PREDICATE.
This operation is not destructive to the LIST.
Unlike CL:REMOVE-IF-NOT, this will return the original LIST unless something was
actually removed from it. An unchanged tail of the LIST, maybe also be returned.
Parameters: FROM-END, START, END, KEY, COUNT - as in CL:REMOVE-IF-NOT.
"
(declare (self (function$ list &key (:key function$)) t))
(let ((predicate (coerce predicate 'function))
(key (and key (coerce key 'function))))
(flet ((predicate (x) (not (funcall predicate x))))
(declare (inline predicate) (dynamic-extent #'predicate))
(remove-if
#'predicate list
:from-end from-end :start start :end end :key key :count count))))
(define-compiler-macro remove-if-not (&whole whole &rest args)
(apply #'%remove-if-form whole args))
(defun* remove (item list &key from-end (start 0) end key test test-not count)
"Remove any item in the LIST that match the ITEM using KEY, TEST, and TEST-NOT
functions for comparison. This operation is not destructive to the LIST.
Unlike CL:REMOVE, this will return the original LIST unless something was
actually removed from it. An unchanged tail of the LIST, maybe also be returned.
Parameters: FROM-END, START, END, TEST, TEST-NOT, KEY, COUNT - as in CL:REMOVE.
"
(declare (self (t list &key
(:key function$)
(:test function$)
(:test-not function$))
t))
(let ((key (and key (coerce key 'function)))
(test (and test (coerce test 'function)))
(test-not (and test-not (coerce test-not 'function))))
(flet ((p1 (y) (not (funcall test-not item y)))
(p2 (y) (funcall test item y))
(p3 (y) (eql item y)))
(declare (inline p1 p2 p3) (dynamic-extent #'p1 #'p2 #'p3))
(remove-if
(cond (test-not
(when test (error "Both TEST and TEST-NOT used in ~A." 'remove))
#'p1)
(test #'p2)
(t #'p3))
list
:from-end from-end :start start :end end
:key key :count count))))
(define-compiler-macro remove (&whole whole &rest args)
(apply #'%find/remove-form whole args))
;; delete
(defun* delete-if (predicate list &key from-end (start 0) end key count)
"Delete any item in the LIST that matches the PREDICATE.
This operation is destructive to the LIST unless nothing was deleted.
Parameters: FROM-END, START, END, KEY, COUNT - as in CL:DELETE-IF.
"
(declare (self (function$ list &key (:key function$)) t))
(let ((predicate (coerce predicate 'function))
(key (and key (coerce key 'function))))
(labels ((pred (x) (funcall predicate x))
(pred/key (x) (funcall #'pred (funcall key x))))
(declare (dynamic-extent #'pred #'pred/key) (inline pred pred/key))
(delete-count (if key #'pred/key #'pred) list start end count from-end))))
(define-compiler-macro delete-if (&whole whole &rest args)
(apply #'%remove-if-form whole args))
(defun* delete-if-not (predicate list &key from-end (start 0) end key count)
"Delete any item in the LIST that does not match the PREDICATE.
This operation is destructive to the LIST unless nothing was deleted.
Parameters: FROM-END, START, END, KEY, COUNT - as in CL:DELETE-IF-NOT.
"
(declare (self (function$ list &key (:key function$)) t))
(let ((predicate (coerce predicate 'function))
(key (and key (coerce key 'function))))
(flet ((predicate (x) (not (funcall predicate x))))
(declare (inline predicate) (dynamic-extent #'predicate))
(delete-if
#'predicate list
:from-end from-end :start start :end end :key key :count count))))
(define-compiler-macro delete-if-not (&whole whole &rest args)
(apply #'%remove-if-form whole args))
(defun* delete (item list &key from-end (start 0) end key test test-not count)
"Delete any item in the LIST that match the ITEM using KEY, TEST, and TEST-NOT
functions for comparison.
This operation is destructive to the LIST unless nothing was deleted.
Parameters: FROM-END, START, END, TEST, TEST-NOT, KEY, COUNT - as in CL:REMOVE.
"
(declare (self (t list &key
(:key function$)
(:test function$)
(:test-not function$))
t))
(let ((key (and key (coerce key 'function)))
(test (and test (coerce test 'function)))
(test-not (and test-not (coerce test-not 'function))))
(flet ((p1 (y) (not (funcall test-not item y)))
(p2 (y) (funcall test item y))
(p3 (y) (eql item y)))
(declare (inline p1 p2 p3) (dynamic-extent #'p1 #'p2 #'p3))
(delete-if
(cond (test-not
(when test (error "Both TEST and TEST-NOT used in ~A." 'delete))
#'p1)
(test #'p2)
(t #'p3))
list
:from-end from-end :start start :end end
:key key :count count))))
(define-compiler-macro delete (&whole whole &rest args)
(apply #'%find/remove-form whole args))
;;; DELETE-ADJACENT
(defun* %%delete-adjacent (list test start)
(declare (self inline (list function position) list))
(loop
:with rest = (nthcdr start list)
:while (cdr rest)
:do
(if (funcall test (first rest) (second rest))
(pop (cdr rest))
(pop rest)))
list)
#-opt (declaim (notinline %%delete-adjacent))
(defun* %%delete-adjacent-end (list test start end)
(declare (self (list function position position) list))
(loop
:with rest = (nthcdr start list)
:with end-1 fixnum = (1- end)
:while (and (cdr rest) (< start end-1))
:do
(if (funcall test (first rest) (second rest))
(pop (cdr rest))
(pop rest))
(incf start))
list)
(defun* %%delete-adjacent-keyed (list test key start end)
(declare (self (list function function position position*) list))
(cond
(end
(loop
:with rest = (nthcdr start list)
:with end-1 fixnum = (1- end)
:while (and (cdr rest) (< start end-1))
:with head = (funcall key (first rest))
:do
(if (funcall test head (setf head (funcall key (second rest))))
(pop (cdr rest))
(pop rest))
(incf start)))
(t
(loop
:with rest = (nthcdr start list)
:while (cdr rest)
:with head = (funcall key (first rest))
:do
(if (funcall test head (setf head (funcall key (second rest))))
(pop (cdr rest))
(pop rest)))))
list)
(defun* delete-adjacent (list &key (test #'eql) key (start 0) end from-end)
"Delete adjacent items from the LIST passing the TEST predicate.
The list will be modified destructively.
If FROM-END is NIL, the first of the duplicated items is retained.
Otherwise, the last of the duplicates in the sequence is retained.
Only elements within (start, end) range can be deleted, if FROM-END is NIL.
If FROM-END is true, elements within [start, end-1) are conditionally deleted.
KEY, if present, will be applied only to elements within the [start, end) range.
Parameters:
LIST - the original list that will be modified.
TEST - a boolean function of two parameters.
KEY - a function applied to each element of the list.
START - index on the list where removing starts (default 0).
END - index on the list before which the removing stops (default end of list).
FROM-END - if true the list will be reversed twice and the last of the
duplicated items in the sequence is retained.
"
;; TODO(czak): COUNT - number of items to remove.
(declare (self inline
(list &key
(:test function$)
(:key function$)
(:start position)
(:end position*)
(:from-end t))
list))
(flet ((dedup (list start end)
(let ((test (coerce test 'function)))
(cond
(key (%%delete-adjacent-keyed
list test (coerce key 'function) start end))
(end (%%delete-adjacent-end list test start end))
(t (%%delete-adjacent list test start))))))
(declare (inline dedup))
(cond
(from-end
(let* ((len nil)
(%start (if end (max 0 (- (setf len (length list)) end)) 0))
(%end (and (> start 0) (max 0 (- (or len (length list)) start)))))
(declare (position %start) (type (or null fixnum) len %end))
(nreverse (dedup (nreverse list) %start %end))))
(t
(dedup list start end)))))
;;;
;;; COPY-IF, COPY-IF-NOT
;;;
(defun* %%copy-if (pred list start end)
(declare (self (function list position position*) list))
(cond ((null end)
(loop :for item :in (nthcdr start list)
:when (funcall pred item)
:collect item))
((< start end)
(loop :for item :in (nthcdr start list)
:for i :of-type position :from start :below end
:when (funcall pred item)
:collect item))))
;; TODO(czak, dougk): Make SBCL inline stuff properly.
(define-compiler-macro %%copy-if
(&whole w pred list start end &environment env)
(if (and (constantp end env) (null (eval* end env)))
;; inline
(with-gensyms (%item)
(once-only (pred list start)
`(loop :for ,%item
:in ,(if (member start '(nil 0))
list
`(nthcdr ,start ,list))
:when (funcall ,pred ,%item)
:collect ,%item)))
w))
(defun* %%copy-if-count (pred list start end count)
(declare (self inline (function list position position item-count) list))
(loop
:while (plusp count)
:for item :in (nthcdr start list)
:for i :of-type position :from start :below end
:when (funcall pred item)
:do (decf count)
:and :collect item))
(defun* copy-if-count (predicate list start end count &optional from-end)
"Copy items matching PREDICATE from the LIST.
The LIST will not be modified and a new copy is returned.
Parameters:
PREDICATE - a boolean function.
LIST - the original list that will not be modified.
START - index on the list where copy starts (default 0).
END - index on the list before which the copy stops (default end of list).
COUNT - number of items to copy.
FROM-END - if true the list will be reversed twice.
"
(declare
(self (function list position* position* item-count* &optional t) list))
(cond
((null list) nil)
((null count)
(%%copy-if predicate list (or start 0) end))
((zerop count) nil)
((let* ((start (or start 0))
(len (length list))
(end (if end (min end len) len)))
(declare (position start end) (item-count len))
(cond
((>= start end) nil)
((>= count (- end start))
;; Just simply call %%copy-if
(%%copy-if predicate list start end))
((not from-end)
(%%copy-if-count predicate list start end count))
(t
;; Need to reverse the list twice.
(nreverse
(%%copy-if-count
predicate (nreverse (subseq list start end))
0 (- end start) count))))))))
(defun* copy-if (predicate list &key from-end (start 0) end key count)
"Copy any item in the LIST that matches the PREDICATE and return a new
fresh list.
This operation is not destructive to the LIST.
Unlike LIST:REMOVE-IF-NOT this will actually copy every cons in the list.
Parameters:
PREDICATE - a boolean function.
LIST - the original list that will not be modified.
START - index on the list where copy starts (default 0).
END - index on the list before which the copy stops (default end of list).
COUNT - number of items to copy.
FROM-END - if true the list will be reversed twice.
"
(declare (self (function$ list &key (:key function$)) t))
(let ((predicate (coerce predicate 'function))
(key (and key (coerce key 'function))))
(labels ((pred (x) (funcall predicate x))
(pred/key (x) (funcall #'pred (funcall key x))))
(declare (dynamic-extent #'pred #'pred/key) (inline pred pred/key))
(copy-if-count
(if key #'pred/key #'pred)
list start end count from-end))))
(define-compiler-macro copy-if (&whole whole &rest args)
(apply #'%remove-if-form whole args))
(defun* copy-if-not (predicate list &key from-end (start 0) end key count)
"Copy any item in the LIST that does not match the PREDICATE and return
a new fresh list.
This operation is not destructive to the LIST.
Unlike LIST:REMOVE-IF this will actually copy every cons in the list.
Parameters:
PREDICATE - a boolean function.
LIST - the original list that will not be modified.
START - index on the list where copy starts (default 0).
END - index on the list before which the copy stops (default end of list).
COUNT - number of items to copy.
FROM-END - if true the list will be reversed twice.
"
(declare (self (function$ list &key (:key function$)) t))
(let ((predicate (coerce predicate 'function)))
(flet ((predicate (x) (not (funcall predicate x))))
(declare (inline predicate) (dynamic-extent #'predicate))
(copy-if