-
Notifications
You must be signed in to change notification settings - Fork 3
/
duo-symbol.el
1241 lines (1147 loc) · 44.2 KB
/
duo-symbol.el
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-symbol.el --- Symbol part of duo -*- lexical-binding: t; -*-
;;; Commentary:
;; Use symbol 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-sym-push-cons (cons symlist)
"Add CONS at the beginning of SYMLIST. Return list in SYMLIST.
See the docstring of `duo-naive-push' to know why it
uses the list symbol as argument.
Common usage :
\(duo-sym-push-cons cons 'list)
Destructive."
(setcdr cons (symbol-value symlist))
(set symlist cons)
(symbol-value symlist))
(defun duo-sym-add-cons (cons symlist &optional last)
"Store CONS at the end of SYMLIST. Return CONS.
If non nil, LAST is used to speed up the process.
See the docstring of `duo-naive-push' to know why it
uses the list symbol as argument.
Common usage :
\(duo-sym-add-cons cons 'list)
Destructive."
(let* ((list (symbol-value symlist))
(last (if last
last
(duo-last list))))
(when last
(setcdr last cons))
(setcdr cons nil)
(unless list
(set symlist cons))
cons))
(defun duo-sym-push (elem symlist)
"Add ELEM at the beginning of SYMLIST.
Return list in SYMLIST.
See the docstring of `duo-naive-push' to know why it
uses the list symbol as argument.
Common usage :
\(duo-sym-push elem 'list)
Destructive."
(let ((duo (cons elem (symbol-value symlist))))
(set symlist duo)
(symbol-value symlist)))
(defun duo-sym-add (elem symlist &optional last)
"Add ELEM at the end of SYMLIST. Return the new LAST.
If non nil, LAST is used to speed up the process.
See the docstring of `duo-naive-push' to know why it
uses the list symbol as argument.
Common usage :
\(duo-sym-add elem 'list)
Destructive."
(let* ((list (symbol-value symlist))
(last (if last
last
(duo-last list)))
(duo (cons elem nil)))
(when last
(setcdr last duo))
(unless list
(set symlist duo))
duo))
(defun duo-sym-push-new-cons (cons symlist)
"Add CONS at the beginning of SYMLIST if not already there.
Return list in SYMLIST.
See the docstring of `duo-naive-push' to know why it
uses the list symbol as argument.
Common usage :
\(duo-sym-push-new-cons cons 'list)
Destructive."
(let ((list (symbol-value symlist)))
(if (duo-inside cons list)
list
(duo-sym-push-cons cons symlist))))
(defun duo-sym-add-new-cons (cons symlist &optional last)
"Add CONS at the end of SYMLIST if not already there.
Return the new LAST.
If non nil, LAST is used to speed up the process.
See the docstring of `duo-naive-push' to know why it
uses the list symbol as argument.
Common usage :
\(duo-sym-add-new-cons cons 'list)
Destructive."
(unless (duo-inside cons (symbol-value symlist))
(duo-sym-add-cons cons symlist last)))
(defun duo-sym-push-new (elem symlist &optional fn-equal)
"Add ELEM at the beginning of SYMLIST if not already there.
Return list in SYMLIST.
FN-EQUAL takes two arguments and return t if they are considered equals.
FN-EQUAL defaults to `equal'.
See the docstring of `duo-naive-push' to know why it
uses the list symbol as argument.
Common usage :
\(duo-sym-push-new elem 'list)
Destructive."
(let ((list (symbol-value symlist)))
(if (duo-member elem list fn-equal)
list
(duo-sym-push elem symlist))))
(defun duo-sym-add-new (elem symlist &optional last fn-equal)
"Add ELEM at the end of SYMLIST.
Do nothing if ELEM is already present.
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'.
See the docstring of `duo-naive-push' to know why it
uses the list symbol as argument.
Common usage :
\(duo-sym-add-new elem 'list)
Destructive."
(unless (duo-member elem (symbol-value symlist) fn-equal)
(duo-sym-add elem symlist last)))
(defun duo-sym-pop (symlist)
"Remove first element in SYMLIST. Return popped cons.
See the docstring of `duo-naive-pop' to know why it
uses the list symbol as argument.
Common usage :
\(setq popped (duo-sym-pop 'list))
Destructive."
(let* ((list (symbol-value symlist))
(popped list))
(set symlist (cdr list))
(setcdr popped nil)
popped))
(defun duo-sym-drop (symlist)
"Remove last element of SYMLIST.
Return cons of removed element.
See the docstring of `duo-naive-push' to know why it
uses the list symbol as argument.
Common usage :
\(duo-sym-drop 'list)
Destructive."
(let* ((list (symbol-value symlist))
(before-last (duo-last list 2))
(last (cdr before-last)))
(if last
(setcdr before-last nil)
;; One element list
(setq last list)
(set symlist nil))
last))
(defun duo-sym-push-and-truncate (elem symlist &optional num)
"Add ELEM at the beginning of SYMLIST.
Truncate SYMLIST to its first NUM elements.
If NUM is nil, do nothing.
Return list in SYMLIST.
See the docstring of `duo-naive-push' to know why it
uses the list symbol as argument.
Common usage :
\(duo-sym-push-and-truncate elem 'list num)
Destructive."
(duo-sym-push elem symlist)
(duo-truncate (symbol-value symlist) num)
(symbol-value symlist))
(defun duo-sym-add-and-clip (elem symlist &optional num length last)
"Add ELEM at the end of SYMLIST.
Truncate SYMLIST to its last NUM elements.
If NUM is nil, do nothing.
Return the new LAST.
If non nil, LENGTH is used to speed up the process.
See the docstring of `duo-naive-push' to know why it
uses the list symbol as argument.
Common usage :
\(duo-sym-add-and-clip elem 'list num)
Destructive."
(let* ((list (symbol-value symlist))
(length (if length
length
(length list)))
(added (duo-sym-add elem symlist last)))
(when added
(setq length (1+ length)))
(while (> length num)
(duo-sym-pop symlist)
(setq length (1- length)))
added))
(defun duo-sym-push-new-and-truncate (elem symlist &optional num fn-equal)
"Push ELEM to SYMLIST if not there and truncate.
The first NUM elements are kept.
If NUM is nil, do nothing.
Return list in SYMLIST.
FN-EQUAL takes two arguments and return t if they are considered equals.
FN-EQUAL defaults to `equal'.
See the docstring of `duo-naive-push' to know why it
uses the list symbol as argument.
Common usage :
\(duo-sym-push-new-and-truncate elem 'list num)
Destructive."
(let ((list (symbol-value symlist)))
(if (duo-member elem list fn-equal)
list
(duo-sym-push-and-truncate elem symlist num))))
(defun duo-sym-add-new-and-clip (elem symlist &optional num length last fn-equal)
"Add ELEM at the end of SYMLIST if not there.
If NUM is nil, do nothing.
Return the new LAST.
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'.
See the docstring of `duo-naive-push' to know why it
uses the list symbol as argument.
Common usage :
\(duo-sym-add-new-and-clip elem 'list)
Destructive."
(unless (duo-member elem (symbol-value symlist) fn-equal)
(duo-sym-add-and-clip elem symlist num length last)))
;;; Rotate <- ->
;;; ------------------------------
(defun duo-sym-rotate-left (symlist)
"Rotate SYMLIST to the left.
Return list in SYMLIST.
Equivalent to pop first element and add it to the end.
See the docstring of `duo-naive-pop' to know why it
uses the list symbol as argument.
Common usage :
\(duo-sym-rotate-left 'list)
Destructive."
;; Length list > 1
(when (cdr (symbol-value symlist))
(let ((popped (duo-sym-pop symlist)))
(duo-sym-add-cons popped symlist)))
(symbol-value symlist))
(defun duo-sym-rotate-right (symlist)
"Rotate SYMLIST to the right.
Return list in SYMLIST.
Equivalent to drop last element and push it at the beginning.
See the docstring of `duo-naive-push' to know why it
uses the list symbol as argument.
Common usage :
\(duo-sym-rotate-right 'list)
Destructive."
;; Length list > 1
(when (cdr (symbol-value symlist))
(let ((dropped (duo-sym-drop symlist)))
(duo-sym-push-cons dropped symlist)))
(symbol-value symlist))
;;; Roll
;;; ------------------------------
(defun duo-sym-roll-cons-to-beg (cons symlist &optional previous)
"Roll SYMLIST to the left until CONS is at the beginning.
Return list in SYMLIST.
CONS must be a cons in SYMLIST.
If non nil, PREVIOUS is used to speed up the process.
See the docstring of `duo-naive-push' to know why it
uses the list symbol as argument.
Common usage :
\(duo-sym-roll-cons-to-beg cons 'list)
Destructive."
(let* ((list (symbol-value symlist))
(previous (if (and previous (eq cons (cdr previous)))
previous
(duo-previous cons list)))
(last previous))
(when (and cons previous (cdr list))
(while (cdr last)
(setq last (cdr last)))
(setcdr previous nil)
(setcdr last list)
(set symlist cons)))
(symbol-value symlist))
(defun duo-sym-roll-cons-to-end (cons symlist)
"Roll SYMLIST to the right until CONS is at the end.
Return list in SYMLIST.
CONS must be a cons in SYMLIST.
See the docstring of `duo-naive-push' to know why it
uses the list symbol as argument.
Common usage :
\(duo-sym-roll-cons-to-end cons 'list)
Destructive."
(let* ((list (symbol-value symlist))
(next (cdr cons))
(last next))
(when (and cons next (cdr list))
(while (cdr last)
(setq last (cdr last)))
(setcdr cons nil)
(setcdr last list)
(set symlist next)))
(symbol-value symlist))
(defun duo-sym-roll-to-beg (elem symlist &rest restargs)
"Roll SYMLIST to the left until ELEM is at the beginning.
Return list in SYMLIST.
ELEM must be present in SYMLIST.
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'.
See the docstring of `duo-naive-push' to know why it
uses the list symbol as argument.
Common usage :
\(duo-sym-roll-to-beg elem 'list)
Destructive."
(let* ((list (symbol-value symlist))
(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-sym-roll-cons-to-beg duo symlist previous)))
(defun duo-sym-roll-to-end (elem symlist &optional fn-equal)
"Roll SYMLIST to the right until ELEM is at the end.
Return list in SYMLIST.
ELEM must be present in SYMLIST.
FN-EQUAL takes two arguments and return t if they are considered equals.
FN-EQUAL defaults to `equal'.
See the docstring of `duo-naive-push' to know why it
uses the list symbol as argument.
Common usage :
\(duo-sym-roll-to-end elem 'list)
Destructive."
(let* ((list (symbol-value symlist))
(duo (duo-member elem list fn-equal)))
(duo-sym-roll-cons-to-end duo symlist)))
;;; Reverse
;;; ------------------------------
(defun duo-sym-reverse (symlist)
"Reverse SYMLIST. Return list in SYMLIST.
See the docstring of `duo-naive-push' to know why it
uses the list symbol as argument.
Common usage :
\(duo-sym-reverse 'list)
Destructive."
(let* ((list (symbol-value symlist))
(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)
(set symlist newlist)
newlist))
(defun duo-sym-reverse-previous (cons symlist)
"Reverse first part of SYMLIST.
The fist part goes from beginning to CONS included.
Return list in SYMLIST.
See the docstring of `duo-naive-push' to know why it
uses the list symbol as argument.
Common usage :
\(duo-sym-reverse-previous cons 'list)
Destructive."
(let ((next (cdr cons)))
(setcdr cons nil)
(duo-sym-reverse symlist)
(setcdr (duo-last (symbol-value symlist)) next)
(symbol-value symlist)))
(defun duo-sym-reverse-next (cons symlist)
"Reverse second part of SYMLIST, from after CONS to end.
Return list in SYMLIST.
Destructive."
(let ((symnext (make-symbol "next")))
(set symnext (cdr cons))
(setcdr cons nil)
(duo-sym-reverse symnext)
(setcdr cons (symbol-value symnext))
(symbol-value symlist)))
(defun duo-sym-reverse-before (elem symlist &optional fn-equal)
"Reverse first part of SYMLIST.
The first part goes from beginning to ELEM included.
Return list in SYMLIST.
FN-EQUAL takes two arguments and return t if they are considered equals.
FN-EQUAL defaults to `equal'.
See the docstring of `duo-naive-push' to know why it
uses the list symbol as argument.
Common usage :
\(duo-sym-reverse-before elem 'list)
Destructive."
(let ((duo (duo-member elem (symbol-value symlist) fn-equal)))
(duo-sym-reverse-previous duo symlist)))
(defun duo-sym-reverse-after (elem symlist &optional fn-equal)
"Reverse second part of SYMLIST, from after ELEM to end.
Return list in SYMLIST.
FN-EQUAL takes two arguments and return t if they are considered equals.
FN-EQUAL defaults to `equal'.
Destructive."
(let ((duo (duo-member elem (symbol-value symlist) fn-equal)))
(duo-sym-reverse-next duo symlist)))
;;; Insert
;;; ------------------------------------------------------------
;;; Cons Cons
;;; ------------------------------
(defun duo-sym-insert-cons-previous (cons new symlist &optional previous)
"Insert NEW before CONS in SYMLIST. Return NEW.
CONS must be a cons in SYMLIST.
NEW is the cons inserted.
If non nil, PREVIOUS inserted is used to speed up the process.
See the docstring of `duo-naive-push' to know why it
uses the list symbol as argument.
Common usage :
\(duo-sym-insert-cons-previous cons new 'list)
Destructive."
(let ((list (symbol-value symlist)))
(if (eq cons list)
(duo-sym-push-cons new symlist)
(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-sym-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-sym-insert-previous (cons new symlist &optional previous)
"Insert NEW before CONS in SYMLIST. Return cons of NEW.
CONS must be a cons in SYMLIST.
NEW is the value of the element inserted.
If non nil, PREVIOUS inserted is used to speed up the process.
See the docstring of `duo-naive-push' to know why it
uses the list symbol as argument.
Common usage :
\(duo-sym-insert-previous cons new 'list)
Destructive."
(let ((duo (list new)))
(duo-sym-insert-cons-previous cons duo symlist previous)))
(defun duo-sym-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-sym-insert-cons-next cons duo)))
;;; Elem Cons
;;; ------------------------------
(defun duo-sym-insert-cons-before (elem new symlist &rest restargs)
"Insert NEW before ELEM in SYMLIST. 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'.
See the docstring of `duo-naive-push' to know why it
uses the list symbol as argument.
Common usage :
\(duo-sym-insert-cons-before elem new 'list)
Destructive."
(let* ((list (symbol-value symlist))
(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-sym-insert-cons-previous duo new symlist previous)))
(defun duo-sym-insert-cons-after (elem new symlist &optional fn-equal)
"Insert NEW after ELEM in SYMLIST. 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 (symbol-value symlist) fn-equal)))
(duo-sym-insert-cons-next duo new)))
;;; Elem Elem
;;; ------------------------------
(defun duo-sym-insert-before (elem new symlist &rest restargs)
"Insert NEW before ELEM in SYMLIST. 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'.
See the docstring of `duo-naive-push' to know why it
uses the list symbol as argument.
Common usage :
\(duo-sym-insert-before elem new 'list)
Destructive."
(let* ((list (symbol-value symlist))
(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-sym-insert-cons-previous cons-elem cons-new symlist previous)))
(defun duo-sym-insert-after (elem new symlist &optional fn-equal)
"Insert NEW after ELEM in SYMLIST. 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* ((list (symbol-value symlist))
(cons-elem (duo-member elem list fn-equal))
(cons-new (list new)))
(duo-sym-insert-cons-next cons-elem cons-new)))
;;; Remove
;;; ------------------------------------------------------------
(defun duo-sym-remove (cons symlist &optional previous)
"Remove CONS from SYMLIST. Return CONS.
CONS must be a cons in SYMLIST.
If non nil, PREVIOUS removed is used to speed up the process.
See the docstring of `duo-naive-pop' to know why it
uses the list symbol as argument.
Common usage :
\(duo-sym-remove cons 'list)
Destructive."
(let ((list (symbol-value symlist)))
(if (eq cons list)
(duo-sym-pop symlist)
(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))))
(defun duo-sym-delete (elem symlist &rest restargs)
"Delete ELEM from SYMLIST. Return removed cons.
If non nil in RESTARGS :
- PREVIOUS deleted 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'.
See the docstring of `duo-naive-pop' to know why it
uses the list symbol as argument.
Common usage :
\(duo-sym-delete elem 'list)
Destructive."
(let* ((list (symbol-value symlist))
(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-sym-remove duo symlist previous)
nil)))
(defun duo-sym-delete-all (elem symlist &optional fn-equal)
"Delete all elements equals to ELEM from SYMLIST.
FN-EQUAL takes two arguments and return t if they are considered equals.
FN-EQUAL defaults to `equal'.
See the docstring of `duo-naive-pop' to know why it
uses the list symbol as argument.
Common usage :
\(duo-sym-delete-all elem 'list)
Destructive."
(let ((removed)
(sym-removed-list (make-symbol "removed-list"))
(last)
(list)
(duo)
(pre)
(next)
(fn-equal (or fn-equal #'equal)))
(set sym-removed-list nil)
(while (funcall fn-equal elem (car (symbol-value symlist)))
(setq removed (duo-sym-pop symlist))
(setq last (duo-sym-add-cons removed sym-removed-list last)))
(setq list (symbol-value symlist))
(setq duo list)
(setq pre nil)
(while duo
(setq next (cdr duo))
(if (funcall fn-equal elem (car duo))
(progn
(duo-sym-remove duo symlist pre)
(setq removed duo)
(setq last (duo-sym-add-cons removed sym-removed-list last))
(setq pre nil))
(setq pre duo))
(setq duo next))
(symbol-value sym-removed-list)))
;;; Teleport
;;; ------------------------------------------------------------
;;; Cons Cons
;;; ------------------------------
(defun duo-sym-teleport-cons-previous (cons moved symlist
&optional pre-removed pre-inserted)
"Move MOVED before CONS in SYMLIST. Return MOVED.
CONS must be a cons in SYMLIST.
MOVED is the cons of the moved element.
If non nil, PRE-REMOVED and PRE-INSERTED
are used to speed up the process.
See the docstring of `duo-naive-push' to know why it
uses the list symbol as argument.
Common usage :
\(duo-sym-teleport-cons-previous cons moved 'list)
Destructive."
(when (and cons moved (not (eq cons moved)))
(duo-sym-remove moved symlist pre-removed)
(duo-sym-insert-cons-previous cons moved symlist pre-inserted))
moved)
(defun duo-sym-teleport-cons-next (cons moved symlist &optional previous)
"Move MOVED after CONS in SYMLIST. Return MOVED.
CONS must be a cons in SYMLIST.
MOVED is the cons of the moved element.
If non nil, PREVIOUS removed is used to speed up the process.
See the docstring of `duo-naive-push' to know why it
uses the list symbol as argument.
Common usage :
\(duo-sym-teleport-cons-next cons moved 'list)
Destructive."
(when (and cons moved (not (eq cons moved)))
(duo-sym-remove moved symlist previous)
(duo-sym-insert-cons-next cons moved))
moved)
;;; Cons Elem
;;; ------------------------------
(defun duo-sym-teleport-previous (cons moved symlist &rest restargs)
"Move MOVED before CONS in SYMLIST. Return cons of MOVED.
CONS must be a cons in SYMLIST.
MOVED is the value of the moved element.
If non nil in RESTARGS :
- PRE-REMOVED and PRE-INSERTED (in that order) 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'.
See the docstring of `duo-naive-push' to know why it
uses the list symbol as argument.
Common usage :
\(duo-sym-teleport-previous cons moved 'list)
Destructive."
(let* ((list (symbol-value symlist))
(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-sym-teleport-cons-previous cons duo symlist pre-removed pre-inserted)))
(defun duo-sym-teleport-next (cons moved symlist &rest restargs)
"Move MOVED after CONS in SYMLIST. Return cons of MOVED.
CONS must be a cons in SYMLIST.
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'.
See the docstring of `duo-naive-push' to know why it
uses the list symbol as argument.
Common usage :
\(duo-sym-teleport-next cons moved 'list)
Destructive."
(let* ((list (symbol-value symlist))
(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-sym-teleport-cons-next cons duo symlist previous)))
;;; Elem Cons
;;; ------------------------------
(defun duo-sym-teleport-cons-before (elem moved symlist &rest restargs)
"Move MOVED before ELEM in SYMLIST. Return MOVED.
ELEM must be present in list.
MOVED is the cons of the moved element.
If non nil in RESTARGS :
- PRE-REMOVED and PRE-INSERTED (in that order) 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'.
See the docstring of `duo-naive-push' to know why it
uses the list symbol as argument.
Common usage :
\(duo-sym-teleport-cons-before cons moved 'list)
Destructive."
(let* ((list (symbol-value symlist))
(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-sym-teleport-cons-previous duo
moved symlist pre-removed pre-inserted)))
(defun duo-sym-teleport-cons-after (elem moved symlist &rest restargs)
"Move MOVED after ELEM in SYMLIST. Return MOVED.
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'.
See the docstring of `duo-naive-push' to know why it
uses the list symbol as argument.
Common usage :
\(duo-sym-teleport-cons-after cons moved 'list)
Destructive."
(let* ((list (symbol-value symlist))
(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-sym-teleport-cons-next duo moved symlist previous)))
;;; Elem Elem
;;; ------------------------------
(defun duo-sym-teleport-before (elem moved symlist &rest restargs)
"Move MOVED before ELEM in SYMLIST. Return MOVED.
ELEM must be present in list.
MOVED is the value of the moved element.
If non nil in RESTARGS ;
- PRE-REMOVED and PRE-INSERTED (in that order) 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'.
See the docstring of `duo-naive-push' to know why it
uses the list symbol as argument.
Common usage :
\(duo-sym-teleport-before cons moved 'list)
Destructive."
(let* ((list (symbol-value symlist))
(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-sym-teleport-cons-previous elem-cons
moved-cons symlist pre-removed pre-inserted)))
(defun duo-sym-teleport-after (elem moved symlist &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 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'.
See the docstring of `duo-naive-push' to know why it
uses the list symbol as argument.
Common usage :
\(duo-sym-teleport-after elem moved 'list)
Destructive."
(let* ((list (symbol-value symlist))
(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-sym-teleport-cons-next elem-cons moved-cons symlist previous)))
;;; Move
;;; ------------------------------------------------------------
;;; Linear
;;; ------------------------------
(defun duo-sym-move-previous (moved symlist &optional num)
"Move MOVED to NUM previous place in SYMLIST. Return MOVED.
If range is exceeded, move MOVED at the beginning of the list.
MOVED must be a cons in SYMLIST.
NUM defaults to 1.
See the docstring of `duo-naive-push' to know why it
uses the list symbol as argument.
Common usage :
\(duo-sym-move-previous moved 'list)
Destructive."
(let* ((list (symbol-value symlist))
(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-sym-teleport-cons-previous landmark moved symlist pre-rem pre-ins)
moved))
(defun duo-sym-move-next (moved symlist &optional num)
"Move MOVED to NUM next place in SYMLIST. Return MOVED.
If range is exceeded, move MOVED at the end of the list.
MOVED must be a cons in SYMLIST.
NUM defaults to 1.
See the docstring of `duo-naive-push' to know why it
uses the list symbol as argument.
Common usage :
\(duo-sym-move-next moved 'list)
Destructive."
(let* ((list (symbol-value symlist))
(num (or num 1))
(landmark (nthcdr num moved)))
(unless landmark
(setq landmark (duo-last list)))
(duo-sym-teleport-cons-next landmark moved symlist)
moved))
(defun duo-sym-move-before (elem symlist &optional num fn-equal)
"Move ELEM to NUM previous place in SYMLIST. Return MOVED.
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'.
See the docstring of `duo-naive-push' to know why it
uses the list symbol as argument.
Common usage :
\(duo-sym-move-before moved 'list)
Destructive."
(let* ((list (symbol-value symlist))
(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-sym-teleport-cons-previous landmark moved symlist pre-rem pre-ins)
moved))
(defun duo-sym-move-after (elem symlist &optional num fn-equal)
"Move ELEM to NUM next place in SYMLIST. Return MOVED.
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'.
See the docstring of `duo-naive-push' to know why it
uses the list symbol as argument.