-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathduo-return.el
More file actions
1247 lines (1153 loc) · 45.9 KB
/
Copy pathduo-return.el
File metadata and controls
1247 lines (1153 loc) · 45.9 KB
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
;;; duo-return.el --- Return part of duo -*- lexical-binding: t; -*-
;;; Commentary:
;; Use return value to alter list
;; Copyright (C) 2019 Chimay
;;; License:
;;; ----------------------------------------------------------------------
;; This file is not part of Emacs.
;; This program is free software; you can redistribute it and/or modify it
;; under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 2, or (at your option)
;; any later version.
;; This program is distributed in the hope that it will be useful, but
;; WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
;; General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program; see the file COPYING. If not, write to the
;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
;; Boston, MA 02111-1307, USA.
;;; Code:
;;; ----------------------------------------------------------------------
;;; Require
;;; ------------------------------------------------------------
(eval-when-compile
(require 'duo-common))
(declare-function duo-< "duo-common")
(declare-function duo-type-of "duo-common")
(declare-function duo-last "duo-common")
(declare-function duo-inside "duo-common")
(declare-function duo-member "duo-common")
(declare-function duo-truncate "duo-common")
(declare-function duo-previous "duo-common")
(declare-function duo-before "duo-common")
(declare-function duo-circ-previous "duo-common")
(declare-function duo-circ-next "duo-common")
(declare-function duo-circ-before "duo-common")
(declare-function duo-assoc "duo-common")
(declare-function duo-partition "duo-common")
;;; Stack & Queue
;;; ------------------------------------------------------------
(defun duo-return-push-cons (cons list)
"Add CONS at the beginning of LIST. Return LIST.
The actual new list must be recovered using the returned list.
See the docstring of `duo-naive-push' to know why.
Common usage :
\(setq list (duo-return-push-cons cons list))
Destructive."
(setcdr cons list)
cons)
(defun duo-return-add-cons (cons list &optional last)
"Store CONS at the end of LIST. Return CONS.
If non nil, LAST is used to speed up the process.
Destructive."
(let ((last (if last
last
(duo-last list))))
(when last
(setcdr last cons))
(setcdr cons nil)
cons))
(defun duo-return-push (elem list)
"Add ELEM at the beginning of LIST. Return LIST.
The actual new list must be recovered using the returned list.
See the docstring of `duo-naive-push' to know why.
Common usage :
\(setq list (duo-return-push elem list))
Destructive."
(cons elem list))
(defun duo-return-add (elem list &optional last)
"Add ELEM at the end of LIST. Return the new LAST.
If non nil, LAST is used to speed up the process.
Destructive."
(let ((last (if last
last
(duo-last list)))
(duo (cons elem nil)))
(when last
(setcdr last duo))
duo))
(defun duo-return-push-new-cons (cons list)
"Add CONS at the beginning of LIST if not already there. Return LIST.
The actual new list must be recovered using the returned list.
See the docstring of `duo-naive-push' to know why.
Common usage :
\(setq list (duo-return-push-new elem list))
Destructive."
(if (duo-inside cons list)
list
(duo-return-push-cons cons list)))
(defun duo-return-add-new-cons (cons list &optional last)
"Add CONS at the end of LIST if not already there. Return the new LAST.
If non nil, LAST is used to speed up the process.
Destructive."
(unless (duo-inside cons list)
(duo-return-add-cons cons list last)))
(defun duo-return-push-new (elem list &optional fn-equal)
"Add ELEM at the beginning of LIST if not already there. Return LIST.
FN-EQUAL takes two arguments and return t if they are considered equals.
FN-EQUAL defaults to `equal'.
The actual new list must be recovered using the returned list.
See the docstring of `duo-naive-push' to know why.
Common usage :
\(setq list (duo-return-push-new elem list))
Destructive."
(if (duo-member elem list fn-equal)
list
(duo-return-push elem list)))
(defun duo-return-add-new (elem list &optional last fn-equal)
"Add ELEM at the end of LIST if not already there.
Return the new LAST.
If non nil, LAST is used to speed up the process.
FN-EQUAL takes two arguments and return t if they are considered equals.
FN-EQUAL defaults to `equal'.
Destructive."
(unless (duo-member elem list fn-equal)
(duo-return-add elem list last)))
(defun duo-return-pop (list)
"Remove the first element of LIST. Return (popped-cons . new-list)
The actual new list must be recovered using the returned structure.
See the docstring of `duo-naive-pop' to know why.
Common usage :
\(setq pair (duo-return-pop list))
\(setq popped (car pair))
\(setq list (cdr pair))"
(let ((popped list)
(newlist (cdr list)))
(setcdr popped nil)
(cons popped newlist)))
(defun duo-return-drop (list)
"Remove last element of LIST. Return cons of removed element.
If the list had only one element before the operation,
it must be manually set to nil after that.
See the docstring of `duo-naive-pop' to know why.
Common usage :
\(setq dropped (duo-return-drop list))
\(when (eq dropped list)
(setq list nil))
Destructive."
(let* ((before-last (duo-last list 2))
(last (cdr before-last)))
(if last
(setcdr before-last nil)
(setq last list))
last))
(defun duo-return-push-and-truncate (elem list &optional num)
"Add ELEM at the beginning of LIST. Truncate LIST to its first NUM elements.
If NUM is nil, do nothing.
Return LIST.
The actual new list must be recovered using the returned list.
See the docstring of `duo-naive-push' to know why.
Common usage :
\(setq list (duo-return-push-and-truncate elem list))
Destructive."
(let ((newlist list))
(setq newlist (duo-return-push elem list))
(duo-truncate newlist num)
newlist))
(defun duo-return-add-and-clip (elem list &optional num length last)
"Add ELEM at the end of LIST. Truncate LIST to its last NUM elements.
If NUM is nil, do nothing.
Return (new LAST . LIST).
If non nil, LENGTH and LAST are used to speed up the process.
The actual new list must be recovered using the returned structure.
See the docstring of `duo-naive-push' to know why.
Common usage :
\(setq pair (duo-return-add-and-clip elem list num))
\(setq added (car pair))
\(setq list (cdr pair))
Destructive."
(let ((length (if length
length
(length list)))
(added (duo-return-add elem list last)))
(when added
(setq length (1+ length)))
(while (> length num)
(setq list (cdr (duo-return-pop list)))
(setq length (1- length)))
(cons added list)))
(defun duo-return-push-new-and-truncate (elem list &optional num fn-equal)
"Push ELEM to LIST if not there and truncate to NUM elements.
If NUM is nil, do nothing.
Return LIST.
FN-EQUAL takes two arguments and return t if they are considered equals.
FN-EQUAL defaults to `equal'.
The actual new list must be recovered using the returned list.
See the docstring of `duo-naive-push' to know why.
Common usage :
\(setq list (duo-return-push-new-and-truncate elem list))
Destructive."
(if (duo-member elem list fn-equal)
list
(duo-return-push-and-truncate elem list num)))
(defun duo-return-add-new-and-clip (elem list &optional num length last fn-equal)
"Add ELEM to LIST if not there and truncate to NUM elements.
If NUM is nil, do nothing.
Return (new LAST . LIST).
If non nil, LENGTH and LAST are used to speed up the process.
FN-EQUAL takes two arguments and return t if they are considered equals.
FN-EQUAL defaults to `equal'.
Destructive."
(unless (duo-member elem list fn-equal)
(duo-return-add-and-clip elem list num length last)))
;;; Rotate <- ->
;;; ------------------------------------------------------------
(defun duo-return-rotate-left (list)
"Rotate LIST to the left. Return LIST.
Equivalent to pop first element and add it to the end.
The actual new list must be recovered using the returned list.
See the docstring of `duo-naive-pop' to know why.
Common usage :
\(setq list (duo-rotate-left list))
Destructive."
;; Length list > 1
(if (cdr list)
(let* ((pair (duo-return-pop list))
(duo (car pair))
(newlist (cdr pair)))
(duo-return-add-cons duo newlist)
newlist)
list))
(defun duo-return-rotate-right (list)
"Rotate LIST to the right. Return LIST.
Equivalent to drop last element and push it at the beginning.
The actual new list must be recovered using the returned list.
See the docstring of `duo-naive-push' to know why.
Common usage :
\(setq list (duo-rotate-right list))
Destructive."
;; Length list > 1
(if (cdr list)
(let ((duo (duo-return-drop list)))
(duo-return-push-cons duo list))
list))
;;; Roll
;;; ------------------------------------------------------------
(defun duo-return-roll-cons-to-beg (cons list &optional previous)
"Roll LIST to the left until CONS is at the beginning. Return LIST.
CONS must be a cons in LIST.
If non nil, PREVIOUS is used to speed up the process.
The actual new list must be recovered using the returned list.
See the docstring of `duo-naive-push' to know why.
Common usage :
\(setq list (duo-return-roll-cons-to-beg cons list))
Destructive."
(let* ((previous (if (and previous (eq cons (cdr previous)))
previous
(duo-previous cons list)))
(last previous))
(if (and cons previous (cdr list))
(progn
(while (cdr last)
(setq last (cdr last)))
(setcdr previous nil)
(setcdr last list)
cons)
list)))
(defun duo-return-roll-cons-to-end (cons list)
"Roll LIST to the right until CONS is at the end. Return LIST.
CONS must be a cons in LIST.
The actual new list must be recovered using the returned list.
See the docstring of `duo-naive-push' to know why.
Common usage :
\(setq list (duo-return-roll-cons-to-end cons list))
Destructive."
(let* ((next (cdr cons))
(last next))
(if (and cons next (cdr list))
(progn
(while (cdr last)
(setq last (cdr last)))
(setcdr cons nil)
(setcdr last list)
next)
list)))
(defun duo-return-roll-to-beg (elem list &rest restargs)
"Roll LIST to the left until ELEM is at the beginning. Return LIST.
ELEM must be present in LIST.
If non nil in RESTARGS :
- PREVIOUS is used to speed up the process.
- FN-EQUAL takes two arguments and return t if they are considered equals.
- FN-EQUAL defaults to `equal'.
The actual new list must be recovered using the returned list.
See the docstring of `duo-naive-push' to know why.
Common usage :
\(setq list (duo-roll-to-beg elem list))
Destructive."
(let* ((argassoc (duo-partition restargs #'duo-type-of))
(fn-equal (or (car (cdr (car (duo-assoc "function" argassoc))))
#'equal))
(previous (or (car (cdr (car (duo-assoc "cons" argassoc))))
(duo-before elem list 1 fn-equal)))
(duo (if previous
(cdr previous)
list)))
(duo-return-roll-cons-to-beg duo list previous)))
(defun duo-return-roll-to-end (elem list &optional fn-equal)
"Roll LIST to the right until ELEM is at the end. Return LIST.
ELEM must be present in LIST.
FN-EQUAL takes two arguments and return t if they are considered equals.
FN-EQUAL defaults to `equal'.
The actual new list must be recovered using the returned list.
See the docstring of `duo-naive-push' to know why.
Common usage :
\(setq list (duo-roll-to-end elem list))
Destructive."
(let ((duo (duo-member elem list fn-equal)))
(duo-return-roll-cons-to-end duo list)))
;;; Reverse
;;; ------------------------------------------------------------
(defun duo-return-reverse (list)
"Reverse LIST. Return LIST.
The actual new list must be recovered using the returned list.
See the docstring of `duo-naive-push' to know why.
Common usage :
\(setq list (duo-return-reverse list))
Destructive."
(let* ((newlist (duo-last list))
(current newlist)
(previous (duo-previous current list)))
(while previous
(setcdr current previous)
(setq current previous)
(setq previous (duo-previous current list)))
(setcdr current nil)
newlist))
(defun duo-return-reverse-previous (cons list)
"Reverse first part of LIST, from beginning to CONS included.
The actual new list must be recovered using the returned list.
See the docstring of `duo-naive-push' to know why.
Common usage :
\(setq list (duo-return-reverse-previous cons list))
Destructive."
(let ((next (cdr cons))
(newlist))
(setcdr cons nil)
(setq newlist (duo-return-reverse list))
(setcdr (duo-last newlist) next)
newlist))
(defun duo-return-reverse-next (cons list)
"Reverse second part of LIST, starting just after CONS to end.
Return LIST.
Destructive."
(let ((next (cdr cons))
(reversed))
(setcdr cons nil)
(setq reversed (duo-return-reverse next))
(setcdr cons reversed)
list))
(defun duo-return-reverse-before (elem list &optional fn-equal)
"Reverse first part of LIST, from beginning to ELEM included.
FN-EQUAL takes two arguments and return t if they are considered equals.
FN-EQUAL defaults to `equal'.
The actual new list must be recovered using the returned list.
See the docstring of `duo-naive-push' to know why.
Common usage :
\(setq list (duo-return-reverse-before elem list))
Destructive."
(let ((duo (duo-member elem list fn-equal)))
(duo-return-reverse-previous duo list)))
(defun duo-return-reverse-after (elem list &optional fn-equal)
"Reverse second part of LIST, starting just after ELEM to end.
Return LIST.
FN-EQUAL takes two arguments and return t if they are considered equals.
FN-EQUAL defaults to `equal'.
Destructive."
(let ((duo (duo-member elem list fn-equal)))
(duo-return-reverse-next duo list)))
;;; Insert
;;; ------------------------------------------------------------
;;; Cons Cons
;;; ------------------------------
(defun duo-return-insert-cons-previous (cons new list &optional previous)
"Insert NEW before CONS in LIST. Return NEW.
CONS must be a cons in LIST.
NEW is the cons inserted.
If non nil, PREVIOUS inserted is used to speed up the process.
If the new cons is inserted at the beginning of the list,
the actual new list must be recovered using new LIST = NEW.
See the docstring of `duo-naive-push' to know why.
Common usage :
\(setq return (duo-return-insert-cons-previous cons new list))
\(when (eq (cdr return) list)
(setq list return))
Destructive."
(if (eq cons list)
(duo-return-push-cons new list)
(let ((previous (if (and previous
(eq cons (cdr previous))
(not (eq previous new)))
previous
(duo-previous cons list))))
(when (and cons new previous)
(setcdr new (cdr previous))
(setcdr previous new))
new)))
(defun duo-return-insert-cons-next (cons new)
"Insert NEW after CONS in list. Return NEW.
CONS must be a cons in LIST.
NEW is the cons inserted.
Destructive."
(when (and cons new)
(setcdr new (cdr cons))
(setcdr cons new))
new)
;;; Cons Elem
;;; ------------------------------
(defun duo-return-insert-previous (cons new list &optional previous)
"Insert NEW before CONS in LIST. Return cons of NEW.
CONS must be a cons in LIST.
NEW is the value of the element inserted.
If non nil, PREVIOUS inserted is used to speed up the process.
If the new cons is inserted at the beginning of the list,
the actual new list must be recovered using new LIST = NEW.
See the docstring of `duo-naive-push' to know why.
Common usage :
\(setq return (duo-insert-previous cons new list))
\(when (eq (cdr return) list)
(setq list return))
Destructive."
(let ((duo (list new)))
(duo-return-insert-cons-previous cons duo list previous)))
(defun duo-return-insert-next (cons new)
"Insert NEW after CONS in list. Return cons of NEW.
CONS must be a cons in LIST.
NEW is the value of the element inserted.
Destructive."
(let ((duo (list new)))
(duo-return-insert-cons-next cons duo)))
;;; Elem Cons
;;; ------------------------------
(defun duo-return-insert-cons-before (elem new list &rest restargs)
"Insert NEW before ELEM in LIST. Return NEW.
ELEM must be present in LIST.
NEW is the cons inserted.
If non nil in RESTARGS :
- PREVIOUS inserted is used to speed up the process.
- FN-EQUAL takes two arguments and return t if they are considered equals.
- FN-EQUAL defaults to `equal'.
If the new cons is inserted at the beginning of the list,
the actual new list must be recovered using new LIST = NEW.
See the docstring of `duo-naive-push' to know why.
Common usage :
\(setq return (duo-insert-cons-before cons new list))
\(when (eq (cdr return) list)
(setq list return))
Destructive."
(let* ((argassoc (duo-partition restargs #'duo-type-of))
(fn-equal (or (car (cdr (car (duo-assoc "function" argassoc))))
#'equal))
(arg-pre (car (cdr (car (duo-assoc "cons" argassoc)))))
(previous (if (and arg-pre
(funcall fn-equal elem (car (cdr arg-pre))))
arg-pre
(duo-before elem list 1 fn-equal)))
(duo (if (funcall fn-equal elem (car list))
list
(cdr previous))))
(duo-return-insert-cons-previous duo new list previous)))
(defun duo-return-insert-cons-after (elem new list &optional fn-equal)
"Insert NEW after ELEM in LIST. Return NEW.
ELEM must be present in LIST.
NEW is the cons inserted.
FN-EQUAL takes two arguments and return t if they are considered equals.
FN-EQUAL defaults to `equal'.
Destructive."
(let ((duo (duo-member elem list fn-equal)))
(duo-return-insert-cons-next duo new)))
;;; Elem Elem
;;; ------------------------------
(defun duo-return-insert-before (elem new list &rest restargs)
"Insert NEW before ELEM in LIST. Return cons of NEW.
ELEM must be present in LIST.
NEW is the value of the element inserted.
If non nil in RESTARGS :
- PREVIOUS inserted is used to speed up the process.
- FN-EQUAL takes two arguments and return t if they are considered equals.
- FN-EQUAL defaults to `equal'.
If the new cons is inserted at the beginning of the list,
the actual new list must be recovered using new LIST = NEW.
See the docstring of `duo-naive-push' to know why.
Common usage :
\(setq return (duo-insert-before cons new list))
\(when (eq (cdr return) list)
(setq list return))
Destructive."
(let* ((argassoc (duo-partition restargs #'duo-type-of))
(fn-equal (or (car (cdr (car (duo-assoc "function" argassoc))))
#'equal))
(arg-pre (car (cdr (car (duo-assoc "cons" argassoc)))))
(previous (if (and arg-pre
(funcall fn-equal elem (car (cdr arg-pre))))
arg-pre
(duo-before elem list 1 fn-equal)))
(cons-elem (if (funcall fn-equal elem (car list))
list
(cdr previous)))
(cons-new (list new)))
(duo-return-insert-cons-previous cons-elem cons-new list previous)))
(defun duo-return-insert-after (elem new list &optional fn-equal)
"Insert NEW after ELEM in LIST. Return cons of NEW.
ELEM must be present in LIST.
NEW is the value of the element inserted.
FN-EQUAL takes two arguments and return t if they are considered equals.
FN-EQUAL defaults to `equal'.
Destructive."
(let ((cons-elem (duo-member elem list fn-equal))
(cons-new (list new)))
(duo-return-insert-cons-next cons-elem cons-new)))
;;; Remove
;;; ------------------------------------------------------------
(defun duo-return-remove (cons list &optional previous)
"Remove CONS from LIST. Return (CONS . LIST).
CONS must be a cons in LIST.
If non nil, PREVIOUS removed is used to speed up the process.
The actual new list must be recovered using the returned structure.
See the docstring of `duo-naive-pop' to know why.
Common usage :
\(setq pair (duo-return-remove cons list))
\(setq removed (car pair))
\(setq list (cdr pair))
Destructive."
(if (eq cons list)
(duo-return-pop list)
(let ((previous (if (and previous
(eq cons (cdr previous))
(not (eq previous cons)))
previous
(duo-previous cons list))))
(when (and cons previous)
(setcdr previous (cdr cons))
(setcdr cons nil))
(cons cons list))))
(defun duo-return-delete (elem list &rest restargs)
"Delete ELEM from LIST. Return (removed-cons . LIST).
FN-EQUAL takes two arguments and return t if they are considered equals.
FN-EQUAL defaults to `equal'.
If non nil in RESTARGS :
- PREVIOUS removed is used to speed up the process.
- FN-EQUAL takes two arguments and return t if they are considered equals.
- FN-EQUAL defaults to `equal'.
The actual new list must be recovered using the returned structure.
See the docstring of `duo-naive-pop' to know why.
Common usage :
\(setq pair (duo-delete elem list))
\(setq removed (car pair))
\(setq list (cdr pair))
Destructive."
(let* ((argassoc (duo-partition restargs #'duo-type-of))
(fn-equal (or (car (cdr (car (duo-assoc "function" argassoc))))
#'equal))
(arg-pre (car (cdr (car (duo-assoc "cons" argassoc)))))
(previous (if (and arg-pre
(funcall fn-equal elem (car (cdr arg-pre))))
arg-pre
(duo-before elem list 1 fn-equal)))
(duo (if (funcall fn-equal elem (car list))
list
(cdr previous))))
(if (and duo
list)
(duo-return-remove duo list previous)
(cons nil list))))
(defun duo-return-delete-all (elem list &optional fn-equal)
"Delete all elements equals to ELEM from LIST.
Return (list-of-removed-cons . LIST).
FN-EQUAL takes two arguments and return t if they are considered equals.
FN-EQUAL defaults to `equal'.
The actual new list must be recovered using the returned structure.
See the docstring of `duo-naive-pop' to know why.
Common usage :
\(setq pair (duo-delete-all elem list))
\(setq removed-list (car pair))
\(setq list (cdr pair))
Destructive."
(let ((newlist list)
(pair)
(removed)
(removed-list)
(last)
(duo)
(pre)
(next)
(fn-equal (or fn-equal #'equal)))
(while (funcall fn-equal elem (car newlist))
(setq pair (duo-return-pop newlist))
(setq removed (car pair))
(setq newlist (cdr pair))
(if removed-list
(setq last (duo-return-add-cons removed removed-list last))
(setq removed-list removed)
(setq last removed)))
(setq duo newlist)
(setq pre nil)
(while duo
(setq next (cdr duo))
(if (funcall fn-equal elem (car duo))
(progn
(setq newlist (cdr (duo-return-remove duo newlist pre)))
(setq removed duo)
(if removed-list
(setq last (duo-return-add-cons removed removed-list last))
(setq removed-list removed)
(setq last removed))
(setq pre nil))
(setq pre duo))
(setq duo next))
(cons removed-list newlist)))
;;; Teleport
;;; ------------------------------------------------------------
;;; Cons Cons
;;; ------------------------------
(defun duo-return-teleport-cons-previous (cons moved list &optional
pre-removed pre-inserted)
"Move MOVED before CONS in LIST. Return (MOVED . LIST).
CONS must be a cons in LIST.
MOVED is the cons of the moved element.
If non nil, PRE-REMOVED and PRE-INSERTED
are used to speed up the process.
The actual new list must be recovered using the returned structure.
See the docstring of `duo-naive-push' to know why.
Common usage :
\(setq pair (duo-return-teleport-cons-previous cons moved list))
\(setq moved (car pair))
\(setq list (cdr pair))
Destructive."
(let ((newlist list)
(return))
(when (and cons moved (not (eq cons moved)))
(setq newlist (cdr (duo-return-remove moved list pre-removed)))
(setq return (duo-return-insert-cons-previous cons moved newlist
pre-inserted))
(when (eq (cdr return) newlist)
(setq newlist return)))
(cons moved newlist)))
(defun duo-return-teleport-cons-next (cons moved list &optional previous)
"Move MOVED after CONS in LIST. Return (MOVED . LIST).
CONS must be a cons in LIST.
MOVED is the cons of the moved element.
If non nil, PREVIOUS removed is used to speed up the process.
The actual new list must be recovered using the returned structure.
See the docstring of `duo-naive-pop' to know why.
Common usage :
\(setq pair (duo-return-teleport-cons-next cons moved list))
\(setq moved (car pair))
\(setq list (cdr pair))
Destructive."
(let ((newlist list))
(when (and cons moved (not (eq cons moved)))
(setq newlist (cdr (duo-return-remove moved list previous)))
(duo-return-insert-cons-next cons moved))
(cons moved newlist)))
;;; Cons Elem
;;; ------------------------------
(defun duo-return-teleport-previous (cons moved list &rest restargs)
"Move MOVED before CONS in LIST. Return (cons of MOVED . LIST).
CONS must be a cons in LIST.
MOVED is the value of the moved element.
If non nil :
- PRE-REMOVED and PRE-INSERTED are used to speed up the process.
- FN-EQUAL takes two arguments and return t if they are considered equals.
- FN-EQUAL defaults to `equal'.
The actual new list must be recovered using the returned structure.
See the docstring of `duo-naive-push' to know why.
Common usage :
\(setq pair (duo-teleport-previous cons moved list))
\(setq cons-moved (car pair))
\(setq list (cdr pair))
Destructive."
(let* ((argassoc (duo-partition restargs #'duo-type-of))
(fn-equal (or (car (cdr (car (duo-assoc "function" argassoc))))
#'equal))
(arg-pre-removed (car (cdr (car (duo-assoc "cons" argassoc)))))
(pre-removed (if (and
arg-pre-removed
(funcall fn-equal moved (car (cdr arg-pre-removed))))
arg-pre-removed
(duo-before moved list 1 fn-equal)))
(pre-inserted (car (nthcdr 2 (car (duo-assoc "cons" argassoc)))))
(duo (if pre-removed
(cdr pre-removed)
(duo-member moved list fn-equal))))
(duo-return-teleport-cons-previous cons duo list
pre-removed pre-inserted)))
(defun duo-return-teleport-next (cons moved list &rest restargs)
"Move MOVED after CONS in LIST. Return (cons of MOVED . LIST).
CONS must be a cons in LIST.
MOVED is the value of the moved element.
If non nil in RESTARGS :
- PREVIOUS removed is used to speed up the process.
- FN-EQUAL takes two arguments and return t if they are considered equals.
- FN-EQUAL defaults to `equal'.
The actual new list must be recovered using the returned structure.
See the docstring of `duo-naive-pop' to know why.
Common usage :
\(setq pair (duo-teleport-next cons moved list))
\(setq cons-moved (car pair))
\(setq list (cdr pair))
Destructive."
(let* ((argassoc (duo-partition restargs #'duo-type-of))
(fn-equal (or (car (cdr (car (duo-assoc "function" argassoc))))
#'equal))
(arg-pre (car (cdr (car (duo-assoc "cons" argassoc)))))
(previous (if (and
arg-pre
(funcall fn-equal moved (car (cdr arg-pre))))
arg-pre
(duo-before moved list 1 fn-equal)))
(duo (if previous
(cdr previous)
(duo-member moved list fn-equal))))
(duo-return-teleport-cons-next cons duo list previous)))
;;; Elem Cons
;;; ------------------------------
(defun duo-return-teleport-cons-before (elem moved list &rest restargs)
"Move MOVED before ELEM in LIST. Return (MOVED . LIST).
ELEM must be present in list.
MOVED is the cons of the moved element.
If non nil in RESTARGS :
- PRE-REMOVED and PRE-INSERTED are used to speed up the process.
- FN-EQUAL takes two arguments and return t if they are considered equals.
- FN-EQUAL defaults to `equal'.
The actual new list must be recovered using the returned structure.
See the docstring of `duo-naive-push' to know why.
Common usage :
\(setq pair (duo-teleport-before elem moved list))
\(setq moved (car pair))
\(setq list (cdr pair))
Destructive."
(let* ((argassoc (duo-partition restargs #'duo-type-of))
(fn-equal (or (car (cdr (car (duo-assoc "function" argassoc))))
#'equal))
(pre-removed (car (cdr (car (duo-assoc "cons" argassoc)))))
(arg-pre-inserted (car (nthcdr 2 (car (duo-assoc "cons" argassoc)))))
(pre-inserted (if (and
arg-pre-inserted
(funcall fn-equal elem (car (cdr arg-pre-inserted))))
arg-pre-inserted
(duo-before elem list 1 fn-equal)))
(duo (if pre-inserted
(cdr pre-inserted)
(duo-member elem list fn-equal))))
(duo-return-teleport-cons-previous duo moved list
pre-removed pre-inserted)))
(defun duo-return-teleport-cons-after (elem moved list &rest restargs)
"Move MOVED after ELEM in LIST. Return (MOVED . LIST).
ELEM must be present in list.
MOVED is the cons of the moved element.
If non nil in RESTARGS :
- PREVIOUS removed is used to speed up the process.
- FN-EQUAL takes two arguments and return t if they are considered equals.
- FN-EQUAL defaults to `equal'.
The actual new list must be recovered using the returned structure.
See the docstring of `duo-naive-pop' to know why.
Common usage :
\(setq pair (duo-teleport-after elem moved list))
\(setq moved (car pair))
\(setq list (cdr pair))
Destructive."
(let* ((argassoc (duo-partition restargs #'duo-type-of))
(fn-equal (or (car (cdr (car (duo-assoc "function" argassoc))))
#'equal))
(previous (car (cdr (car (duo-assoc "cons" argassoc)))))
(duo (duo-member elem list fn-equal)))
(duo-return-teleport-cons-next duo moved list previous)))
;;; Elem Elem
;;; ------------------------------
(defun duo-return-teleport-before (elem moved list &rest restargs)
"Move MOVED before ELEM in LIST. Return (cons of MOVED . LIST).
ELEM must be present in list.
MOVED is the value of the moved element.
If non nil in RESTARGS :
- PRE-REMOVED and PRE-INSERTED are used to speed up the process.
- FN-EQUAL takes two arguments and return t if they are considered equals.
- FN-EQUAL defaults to `equal'.
The actual new list must be recovered using the returned structure.
See the docstring of `duo-naive-push' to know why.
Common usage :
\(setq pair (duo-teleport-before elem moved list))
\(setq cons-moved (car pair))
\(setq list (cdr pair))
Destructive."
(let* ((argassoc (duo-partition restargs #'duo-type-of))
(fn-equal (or (car (cdr (car (duo-assoc "function" argassoc))))
#'equal))
(arg-pre-removed (car (cdr (car (duo-assoc "cons" argassoc)))))
(pre-removed (if (and
arg-pre-removed
(funcall fn-equal moved (car (cdr arg-pre-removed))))
arg-pre-removed
(duo-before moved list 1 fn-equal)))
(arg-pre-inserted (car (nthcdr 2 (car (duo-assoc "cons" argassoc)))))
(pre-inserted (if (and
arg-pre-inserted
(funcall fn-equal elem (car (cdr arg-pre-inserted))))
arg-pre-inserted
(duo-before elem list 1 fn-equal)))
(elem-cons (if pre-inserted
(cdr pre-inserted)
(duo-member elem list fn-equal)))
(moved-cons (if pre-removed
(cdr pre-removed)
(duo-member moved list fn-equal))))
(duo-return-teleport-cons-previous elem-cons moved-cons list
pre-removed pre-inserted)))
(defun duo-return-teleport-after (elem moved list &rest restargs)
"Move MOVED after ELEM in LIST. Return (cons of MOVED . LIST).
ELEM must be present in list.
MOVED is the value of the moved element.
If non nil RESTARGS :
- PREVIOUS removed is used to speed up the process.
- FN-EQUAL takes two arguments and return t if they are considered equals.
- FN-EQUAL defaults to `equal'.
The actual new list must be recovered using the returned structure.
See the docstring of `duo-naive-pop' to know why.
Common usage :
\(setq pair (duo-teleport-after elem moved list))
\(setq cons-moved (car pair))
\(setq list (cdr pair))
Destructive."
(let* ((argassoc (duo-partition restargs #'duo-type-of))
(fn-equal (or (car (cdr (car (duo-assoc "function" argassoc))))
#'equal))
(arg-pre (car (cdr (car (duo-assoc "cons" argassoc)))))
(previous (if (and
arg-pre
(funcall fn-equal moved (car (cdr arg-pre))))
arg-pre
(duo-before moved list 1 fn-equal)))
(elem-cons (duo-member elem list fn-equal))
(moved-cons (if previous
(cdr previous)
(duo-member moved list fn-equal))))
(duo-return-teleport-cons-next elem-cons moved-cons list previous)))
;;; Move
;;; ------------------------------------------------------------
;;; Linear
;;; ------------------------------
(defun duo-return-move-previous (moved list &optional num)
"Move MOVED to NUM previous place in LIST. Return (MOVED . LIST).
If range is exceeded, move MOVED at the beginning of the list.
MOVED must be a cons in LIST.
NUM defaults to 1.
The actual new list must be recovered using the returned structure.
See the docstring of `duo-naive-push' to know why.
Common usage :
\(setq pair (duo-move-previous moved list))
\(setq moved (car pair))
\(setq list (cdr pair))
Destructive."
(let* ((num (or num 1))
(pre-ins (duo-previous moved list (1+ num)))
(landmark (if pre-ins
(cdr pre-ins)
list))
(pre-rem (if pre-ins
(nthcdr num pre-ins)
(duo-previous moved list))))
(duo-return-teleport-cons-previous landmark moved list pre-rem pre-ins)))
(defun duo-return-move-next (moved list &optional num)
"Move MOVED to NUM next place in LIST. Return (MOVED . LIST).
If range is exceeded, move MOVED at the end of the list.
MOVED must be a cons in LIST.
NUM defaults to 1.
The actual new list must be recovered using the returned structure.
See the docstring of `duo-naive-push' to know why.
Common usage :
\(setq pair (duo-move-next moved list))
\(setq moved (car pair))
\(setq list (cdr pair))
Destructive."
(let* ((num (or num 1))
(landmark (nthcdr num moved)))
(unless landmark
(setq landmark (duo-last list)))
(duo-return-teleport-cons-next landmark moved list)))
(defun duo-return-move-before (elem list &optional num fn-equal)
"Move ELEM to NUM previous place in LIST. Return (MOVED . LIST).
If range is exceeded, move ELEM at the beginning of the list.
MOVED is the moved value.
NUM defaults to 1.
FN-EQUAL takes two arguments and return t if they are considered equals.
FN-EQUAL defaults to `equal'.
The actual new list must be recovered using the returned structure.
See the docstring of `duo-naive-push' to know why.
Common usage :
\(setq pair (duo-move-before elem list))
\(setq cons-moved (car pair))
\(setq list (cdr pair))
Destructive."
(let* ((num (or num 1))
(pre-ins (duo-before elem list (1+ num) fn-equal))
(landmark (if pre-ins
(cdr pre-ins)
list))
(pre-rem (if pre-ins
(nthcdr num pre-ins)
(duo-before elem list 1 fn-equal)))
(moved (if pre-rem
(cdr pre-rem)
(duo-member elem list fn-equal))))
(duo-return-teleport-cons-previous landmark moved list pre-rem pre-ins)))
(defun duo-return-move-after (elem list &optional num fn-equal)
"Move ELEM to NUM next place in LIST. Return (MOVED . LIST).
If range is exceeded, move MOVED at the end of the list.
MOVED is the moved value.
NUM defaults to 1.
FN-EQUAL takes two arguments and return t if they are considered equals.
FN-EQUAL defaults to `equal'.
The actual new list must be recovered using the returned structure.
See the docstring of `duo-naive-push' to know why.
Common usage :
\(setq pair (duo-move-after elem list))
\(setq cons-moved (car pair))
\(setq list (cdr pair))
Destructive."
(let* ((num (or num 1))
(moved (duo-member elem list fn-equal))
(landmark (nthcdr num moved)))
(unless landmark
(setq landmark (duo-last list)))
(duo-return-teleport-cons-next landmark moved list)))