-
Notifications
You must be signed in to change notification settings - Fork 0
/
pqueue.ss
1461 lines (1381 loc) · 45.5 KB
/
pqueue.ss
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
;{{{ Macro
(define-syntax defopt
(syntax-rules ()
[(_ (p x ... [y e]) b1 b2 ...)
(define p
(case-lambda
[(x ...) (p x ... e)]
[(x ... y) b1 b2 ...]))]))
;}}}
;{{{ Skew Heap
(module sh%
(make-sh sh-show sh-empty? sh-merge! sh-push! sh-pop!
sh-node sh-mutate! sh-delete! list->sh)
;{{{ New heap
(defopt (make-sh [p <])
(list p))
;}}}
;{{{ Print tree
(defopt (sh-show sh [tab '(1 3 1)])
(let* ([h #\x2500] [v #\x2502] [u #\x250c] [d #\x2514]
;[h #\-] [v #\|] [u #\/] [d #\\]
[s #\space] [str "~a\x1b;[1m~a\x1b;[m~%"]
[nps (car tab)] [ns (cadr tab)] [nss (caddr tab)]
[sp (make-string (+ nps ns nss) s)] [hh (make-string (1- ns) h)]
[ps (make-string nps s)] [ss (make-string nss s)]
[uh (string-append ps (make-string 1 u) hh ss)]
[dh (string-append ps (make-string 1 d) hh ss)]
[vs (string-append ps (make-string 1 v) (make-string (1- ns) s) ss)])
(let loop ([st (cdr sh)] [lsp ps] [csp ps] [rsp ps])
(unless (null? st)
(loop (cadr st)
(string-append lsp sp)
(string-append lsp uh)
(string-append lsp vs))
(printf str csp (car st))
(loop (cddr st)
(string-append rsp vs)
(string-append rsp dh)
(string-append rsp sp))))))
;}}}
;{{{ Empty test
(define (sh-empty? sh)
(null? (cdr sh)))
;}}}
;{{{ Merge heap
(define ($sh-merge! sh dt)
(let ([lt? (car sh)])
(unless (null? dt)
(set-cdr! sh
(let ([n1 (cons '() (cons '() (cdr sh)))])
(let loop ([n1 n1] [n2 dt])
(let ([nr (cddr n1)])
(set-cdr! (cdr n1) (cadr n1))
(if (null? nr) (set-car! (cdr n1) n2)
(let-values ([(n2 nr)
(if (lt? (car n2) (car nr))
(values n2 nr) (values nr n2))])
(set-car! (cdr n1) n2)
(loop n2 nr)))))
(cadr n1)))))
sh)
(define (sh-merge! s1 s2)
(if (not (equal? (car s1) (car s2)))
(error 'sh-merge! "Incompatible order type"))
($sh-merge! s1 (cdr s2)))
;}}}
;{{{ Push to heap
(define (sh-push! sh x)
($sh-merge! sh (list x '())))
;}}}
;{{{ Pop from heap
(define (sh-pop! sh)
(let ([n (cdr sh)])
(if (null? n)
(error 'sh-pop! "Empty heap"))
(set-cdr! sh (cadr n))
($sh-merge! sh (cddr n))
(car n)))
;}}}
;{{{ Search for a node in heap
(define (sh-node sh p?)
(let loop ([n (cdr sh)])
(if (null? n) #f
(if (p? (car n)) n
(or (loop (cadr n))
(loop (cddr n)))))))
(define ($sh-trace sh p?)
(let loop ([n (cdr sh)])
(if (null? n) '()
(if (p? (car n)) (list n)
(let ([l (loop (cadr n))])
(if (null? l)
(let ([l (loop (cddr n))])
(if (null? l) '()
(cons n l)))
(cons n l)))))))
;}}}
;{{{ Mutate a node in heap
(define ($sh-adjust-increase! tr lt?)
(let* ([mh (car (last-pair tr))] [m (car mh)])
(let loop ([mh mh])
(let ([l (cadr mh)] [r (cddr mh)])
(cond [(cond
[(and (null? l) (null? r)) #f]
[(null? l) r] [(null? r) l]
[else (if (lt? (car r) (car l)) r l)])
=> (lambda (d)
(when (lt? (car d) m)
(set-car! mh (car d))
(set-car! d m)
(loop d)))])))))
(define ($sh-adjust-decrease! tr lt?)
(let* ([l (reverse tr)] [m (caar l)])
(let loop ([l l])
(let ([ll (cdr l)])
(if (and (not (null? ll)) (lt? m (caar ll)))
(begin
(set-car! (car l) (caar ll)) (loop ll))
(set-car! (car l) m))))))
(define (sh-mutate! sh p? mutator)
(let ([tr ($sh-trace sh p?)])
(if (null? tr) #f
(let ([lt? (car sh)] [t0 (car (last-pair tr))])
(cond [(let-values ([(v mutate) (mutator (car t0))])
(let-syntax ([prog1 (syntax-rules ()
[(_ v body ...) (let ([v& v]) body ... v&)])])
(cond
[(not mutate) (prog1
(cond [(lt? (car t0) v) $sh-adjust-increase!]
[(lt? v (car t0)) $sh-adjust-decrease!]
[else #f])
(set-car! t0 v))]
[(procedure? mutate) (prog1
(let ([v0 ((mutate 'accessor) (car t0))]
[lt? (mutate 'lt)])
(cond [(lt? v0 v) $sh-adjust-increase!]
[(lt? v v0) $sh-adjust-decrease!]
[else #f]))
((mutate 'mutator) (car t0) v))]
[(real? mutate)
(cond [(positive? mutate) $sh-adjust-increase!]
[(negative? mutate) $sh-adjust-decrease!]
[else #f])]
[else $sh-adjust-decrease!])))
=> (lambda (f) (f tr lt?))])
sh))))
;}}}
;{{{ Delete a node in heap
(define (sh-delete! sh p?)
(let ([tr ($sh-trace sh p?)])
(if (null? tr) #f
(let ([n (cdr sh)])
(let loop ([l (reverse! tr)])
(let ([ll (cdr l)])
(unless (null? ll)
(set-car! (car l) (caar ll))
(loop ll))))
(set-cdr! sh (cadr n))
($sh-merge! sh (cddr n))
sh))))
;}}}
;{{{ Make heap from list
(defopt (list->sh l [op #f])
(fold-left (lambda (x y) (sh-push! x y))
(if op (make-sh op) (make-sh)) l))
;}}}
)
;}}}
;{{{ Leftist Heap
(module lh%
(make-lh lh-show lh-empty? lh-merge! lh-push! lh-pop!
lh-node lh-mutate! lh-delete! list->lh)
;{{{ Macro
(define-syntax level-left
(syntax-rules ()
[(_ n)
(if (null? (cadr n)) -1
(cdaadr n))]))
(define-syntax level-right
(syntax-rules ()
[(_ n)
(if (null? (cddr n)) -1
(cdaddr n))]))
;}}}
;{{{ New heap
(defopt (make-lh [p <])
(list p))
;}}}
;{{{ Print tree
(defopt (lh-show lh [tab '(1 3 1)])
(let* ([h #\x2500] [v #\x2502] [u #\x250c] [d #\x2514]
;[h #\-] [v #\|] [u #\/] [d #\\]
[s #\space] [str "~a\x1b;[3~d;1m~a\x1b;[m~%"]
[nps (car tab)] [ns (cadr tab)] [nss (caddr tab)]
[sp (make-string (+ nps ns nss) s)] [hh (make-string (1- ns) h)]
[ps (make-string nps s)] [ss (make-string nss s)]
[uh (string-append ps (make-string 1 u) hh ss)]
[dh (string-append ps (make-string 1 d) hh ss)]
[vs (string-append ps (make-string 1 v) (make-string (1- ns) s) ss)])
(let loop ([st (cdr lh)] [lsp ps] [csp ps] [rsp ps])
(unless (null? st)
(loop (cadr st)
(string-append lsp sp)
(string-append lsp uh)
(string-append lsp vs))
(printf str csp
(let ([delta (- (level-left st) (level-right st))])
(cond [(positive? delta) 3] [(zero? delta) 9] [else 0]))
(caar st))
(loop (cddr st)
(string-append rsp vs)
(string-append rsp dh)
(string-append rsp sp))))))
;}}}
;{{{ Empty test
(define (lh-empty? lh)
(null? (cdr lh)))
;}}}
;{{{ Merge heap
(define ($lh-merge! lh dt)
(let ([lt? (car lh)])
(set-cdr! lh
(let loop ([n1 (cdr lh)] [n2 dt])
(cond [(null? n1) n2] [(null? n2) n1] [else
(let-values ([(n1 n2)
(if (lt? (caar n1) (caar n2))
(values n1 n2) (values n2 n1))])
(let* ([v0 (level-right n1)]
[n (loop (cddr n1) n2)]
[v (cdar n)])
(if (< (level-left n1) v)
(begin
(set-cdr! (cdr n1) (cadr n1))
(set-car! (cdr n1) n))
(set-cdr! (cdr n1) n))
(unless (= v0 v)
(set-cdr! (car n1)
(1+ (level-right n1)))))
n1)]))))
lh)
(define (lh-merge! l1 l2)
(if (not (equal? (car l1) (car l2)))
(error 'lh-merge! "Incompatible order type"))
($lh-merge! l1 (cdr l2)))
;}}}
;{{{ Push to heap
(define (lh-push! lh x)
($lh-merge! lh (list (cons x 0) '())))
;}}}
;{{{ Pop from heap
(define (lh-pop! lh)
(let ([n (cdr lh)])
(if (null? n)
(error 'lh-pop! "Empty heap"))
(set-cdr! lh (cadr n))
($lh-merge! lh (cddr n))
(caar n)))
;}}}
;{{{ Search for a node in heap
(define (lh-node lh p?)
(let loop ([n (cdr lh)])
(if (null? n) #f
(if (p? (caar n)) n
(or (loop (cadr n))
(loop (cddr n)))))))
(define ($lh-trace lh p?)
(let loop ([n (cdr lh)])
(if (null? n) '()
(if (p? (caar n)) (list n)
(let ([l (loop (cadr n))])
(if (null? l)
(let ([l (loop (cddr n))])
(if (null? l) '()
(cons n l)))
(cons n l)))))))
;}}}
;{{{ Mutate a node in heap
(define ($lh-adjust-increase! tr lt?)
(let* ([mh (car (last-pair tr))] [m (caar mh)])
(let loop ([mh mh])
(let ([l (cadr mh)] [r (cddr mh)])
(cond [(cond
[(and (null? l) (null? r)) #f]
[(null? l) r] [(null? r) l]
[else (if (lt? (caar r) (caar l)) r l)])
=> (lambda (d)
(when (lt? (caar d) m)
(set-car! (car mh) (caar d))
(set-car! (car d) m)
(loop d)))])))))
(define ($lh-adjust-decrease! tr lt?)
(let* ([l (reverse tr)] [m (caaar l)])
(let loop ([l l])
(let ([ll (cdr l)])
(if (and (not (null? ll)) (lt? m (caaar ll)))
(begin
(set-car! (caar l) (caaar ll)) (loop ll))
(set-car! (caar l) m))))))
(define (lh-mutate! lh p? mutator)
(let ([tr ($lh-trace lh p?)])
(if (null? tr) #f
(let ([lt? (car lh)] [t0 (car (last-pair tr))])
(cond [(let-values ([(v mutate) (mutator (caar t0))])
(let-syntax ([prog1 (syntax-rules ()
[(_ v body ...) (let ([v& v]) body ... v&)])])
(cond
[(not mutate) (prog1
(cond [(lt? (caar t0) v) $lh-adjust-increase!]
[(lt? v (caar t0)) $lh-adjust-decrease!]
[else #f])
(set-car! (car t0) v))]
[(procedure? mutate) (prog1
(let ([v0 ((mutate 'accessor) (caar t0))]
[lt? (mutate 'lt)])
(cond [(lt? v0 v) $lh-adjust-increase!]
[(lt? v v0) $lh-adjust-decrease!]
[else #f]))
((mutate 'mutator) (caar t0) v))]
[(real? mutate)
(cond [(positive? mutate) $lh-adjust-increase!]
[(negative? mutate) $lh-adjust-decrease!]
[else #f])]
[else $lh-adjust-decrease!])))
=> (lambda (f) (f tr lt?))])
lh))))
;}}}
;{{{ Delete a node in heap
(define (lh-delete! lh p?)
(let ([tr ($lh-trace lh p?)])
(if (null? tr) #f
(let ([n (cdr lh)])
(let loop ([l (reverse! tr)])
(let ([ll (cdr l)])
(unless (null? ll)
(set-car! (caar l) (caaar ll))
(loop ll))))
(set-cdr! lh (cadr n))
($lh-merge! lh (cddr n))
lh))))
;}}}
;{{{ Make heap from list
(defopt (list->lh l [op #f])
(fold-left (lambda (x y) (lh-push! x y))
(if op (make-lh op) (make-lh)) l))
;}}}
)
;}}}
;{{{ Binomial Heap
(module bh%
(make-bh bh-show bh-empty? bh-merge! bh-push! bh-pop!
bh-node bh-mutate! bh-delete! list->bh)
;{{{ New heap
(defopt (make-bh [p <])
(list p))
;}}}
;{{{ Print heap
(defopt (bh-show bh [tab '(1 3 1)])
(let* ([h #\x2500] [v #\x2502] [u #\x251c] [d #\x2514]
;[h #\-] [v #\|] [u #\|] [d #\\]
[s #\space] [str "~a\x1b;[1m~a\x1b;[m~%"]
[nps (car tab)] [ns (cadr tab)] [nss (caddr tab)]
[sp (make-string (+ nps ns nss) s)] [hh (make-string (1- ns) h)]
[ps (make-string nps s)] [ss (make-string nss s)]
[uh (string-append ps (make-string 1 u) hh ss)]
[dh (string-append ps (make-string 1 d) hh ss)]
[vs (string-append ps (make-string 1 v) (make-string (1- ns) s) ss)])
(unless (null? (cdr bh))
(let loop ([st (cdr bh)] [par (cons ps ps)] [pdr (cons ps ps)])
(let count ([st st])
(let* ([f (null? (cdr st))] [pr (if f par pdr)])
(printf str (car pr) (caar st))
(unless (null? (cdar st))
(loop (cdar st)
(cons (string-append (cdr pr) dh)
(string-append (cdr pr) sp))
(cons (string-append (cdr pr) uh)
(string-append (cdr pr) vs))))
(unless f (count (cdr st)))))))))
;}}}
;{{{ Empty test
(define (bh-empty? bh)
(null? (cdr bh)))
;}}}
;{{{ Merge heap
(define ($bh-merge! bh dt)
(let ([lt? (car bh)])
(set-cdr! bh
(let loop ([n1 (cdr bh)] [n2 dt] [f #f])
(cond [(null? n1) n2] [(null? n2) n1]
[else
(let ([s1 (length (cdar n1))] [s2 (length (cdar n2))])
(if (= s1 s2)
(cons
(let-values ([(t1 t2)
(if (lt? (caar n1) (caar n2))
(values (car n1) (car n2))
(values (car n2) (car n1)))])
(set-cdr! t1 (cons t2 (cdr t1)))
t1)
(loop (cdr n1) (cdr n2) #f))
(let-values ([(n1 n2)
(if (< s1 s2) (values n2 n1) (values n1 n2))])
(let ([n (loop (cdr n1) n2 #f)])
(if f
(begin (set-cdr! n1 n) n1)
(loop (list (car n1)) n #t))))))]))))
bh)
(define (bh-merge! b1 b2)
(if (not (equal? (car b1) (car b2)))
(error 'bh-merge! "Incompatible order type"))
($bh-merge! b1 (cdr b2)))
;}}}
;{{{ Push to heap
(define (bh-push! bh x)
($bh-merge! bh (list (list x))))
;}}}
;{{{ Pop from heap
(define ($bh-min bh lt?)
(let ([m #f] [mt '()])
(let loop ([last bh])
(let ([tree (cdr last)])
(unless (null? tree)
(let ([v (caar tree)])
(when (or (null? mt) (lt? v m))
(set! mt last) (set! m v)))
(loop tree))))
(values m mt)))
(define (bh-pop! bh)
(let-values ([(m mt) ($bh-min bh (car bh))])
(if (null? mt)
(error 'bh-pop! "Empty heap"))
(let ([d (cdadr mt)])
(set-cdr! mt (cddr mt))
($bh-merge! bh d))
m))
;}}}
;{{{ Search for a node in heap
(define (bh-node bh p?)
(let loop ([n (cdr bh)])
(let count ([n n])
(if (null? n) #f
(if (p? (caar n)) n
(or (loop (cdar n))
(count (cdr n))))))))
(define ($bh-trace bh p?)
(let loop ([last bh])
(let count ([last last])
(let ([n (cdr last)])
(if (null? n) '()
(if (p? (caar n)) (list last)
(let ([l (loop (car n))])
(if (null? l) (count n)
(cons last l)))))))))
;}}}
;{{{ Mutate a node in heap
(define ($bh-adjust-increase! tr lt?)
(let* ([mh (cadar (last-pair tr))] [mm (car mh)])
(let loop ([mh mh])
(let-values ([(m mt) ($bh-min mh lt?)])
(if (and (not (null? mt)) (lt? m mm))
(let ([mt (cadr mt)])
(set-car! mh m) (loop mt))
(set-car! mh mm))))))
(define ($bh-adjust-decrease! tr lt?)
(let* ([l (reverse! (map cadr tr))] [m (caar l)])
(let loop ([l l])
(let ([ll (cdr l)])
(if (and (not (null? ll)) (lt? m (caar ll)))
(begin
(set-car! (car l) (caar ll)) (loop ll))
(set-car! (car l) m))))))
(define (bh-mutate! bh p? mutator)
;; `mutator` is a procedure that takes in an argument `v0`
;; standing for the data to be mutated and returns multi-values
;; `v mutate`.
;; If `mutate` is `#f`, then `v` is treated as the new data.
;; If `mutate` is a procedure, then it takes in a symbol and
;; returns a procedure:
;; * `(mutate 'accessor)` gives the accessor of the priority
;; value of data;
;; * `(mutate 'mutator)` gives the mutator of the priority
;; value of data;
;; * `(mutate 'lt)` gives the less-than predicate of the priority
;; value;
;; and `v` is treated as the new priority value.
;; In the cases below, the data should be mutated by the
;; procedure `mutator`.
;; If `mutate` is a real number, then its sign is used to
;; determine whether the priority value is to be increased or
;; decreased.
;; In other cases, the priority value is viewed to be decreased.
(let ([tr ($bh-trace bh p?)])
(if (null? tr) #f
(let ([lt? (car bh)] [adjust! $bh-adjust-decrease!]
[t0 (cadar (last-pair tr))])
(let-values ([(v mutate) (mutator (car t0))])
(cond
[(not mutate)
(if (lt? (car t0) v)
(set! adjust! $bh-adjust-increase!)
(if (not (lt? v (car t0)))
(set! adjust! #f)))
(set-car! t0 v)]
[(procedure? mutate)
(let ([v0 ((mutate 'accessor) (car t0))]
[lt? (mutate 'lt)])
(if (lt? v0 v)
(set! adjust! $bh-adjust-increase!)
(if (not (lt? v v0))
(set! adjust! #f))))
((mutate 'mutator) (car t0) v)]
[(real? mutate)
(if (positive? mutate)
(set! adjust! $bh-adjust-increase!)
(if (zero? mutate)
(set! adjust! #f)))]))
(if adjust! (adjust! tr lt?))
bh))))
;}}}
;{{{ Delete a node in heap
(define (bh-delete! bh p?)
(let ([tr ($bh-trace bh p?)])
(if (null? tr) #f
(let ([mt (car tr)] [d (cdadar tr)])
(let loop ([l (reverse! (map cadr tr))])
(let ([ll (cdr l)])
(unless (null? ll)
(set-car! (car l) (caar ll))
(loop ll))))
(set-cdr! mt (cddr mt))
($bh-merge! bh d)
bh))))
;}}}
;{{{ Make heap from list
(defopt (list->bh l [op #f])
(fold-left (lambda (x y) (bh-push! x y))
(if op (make-bh op) (make-bh)) l))
;}}}
)
;}}}
;{{{ Fibonacci Heap
(module fh%
(make-fh fh-show fh-empty? fh-merge! fh-push! fh-pop!
fh-node fh-mutate! fh-delete! list->fh)
;{{{ Macro
(define-syntax list*&
(lambda (x)
(syntax-case x ()
[(_ head) #'(list* head)]
[(name head remain ...)
(with-syntax ([& (datum->syntax #'name '&)])
#'(let* ([& (list '())] [head& head]
[remain& (list* remain ...)])
(set-car! & head&)
(set-cdr! & remain&)
&))])))
(define-syntax list&
(lambda (x)
(syntax-case x ()
[(name elems ...)
(datum->syntax #'name (syntax->datum
#'(list*& elems ... '())))])))
(define-syntax cons&
(lambda (x)
(syntax-case x ()
[(name a b)
(datum->syntax #'name (syntax->datum
#'(list*& a b)))])))
(define-syntax make-lr
(syntax-rules (nil)
[(_ nil r)
(set-car! (cddr r) '())]
[(_ l nil)
(set-cdr! (cddr l) '())]
[(_ l r)
(let ([l& l] [r& r])
(set-cdr! (cddr l&) r&)
(set-car! (cddr r&) l&))]))
(define-syntax make-ud
(syntax-rules (nil)
[(_ nil d)
(set-car! (cadr d) '())]
[(_ u nil)
(set-cdr! (cadr u) '())]
[(_ u d)
(let ([u& u] [d& d])
(set-cdr! (cadr u&) d&)
(set-car! (cadr d&) u&))]))
;}}}
;{{{ Reusable vector
(define ($make-rvector n)
(let ([i 0] [vec '#()])
(lambda (operation)
(case operation
[size (lambda (size)
(let ([old-size (vector-length vec)])
(if (and (<= size old-size) (< i (1- n)))
(set! i (1+ i))
(let ([new-size
(if (or (zero? old-size)
(<= size old-size))
size
(let loop ([s old-size])
(if (>= s size) s
(loop (ceiling (* s 3/2))))))])
(set! i 0)
(set! vec (make-vector new-size #f))))))]
[ref (lambda (index)
(let ([l (vector-ref vec index)])
(and l (= i (car l)) (cdr l))))]
[set! (lambda (index v)
(vector-set! vec index (cons i v)))]))))
(define $rvector ($make-rvector 1024))
(define $rvector-size ($rvector 'size))
(define $rvector-ref ($rvector 'ref))
(define $rvector-set! ($rvector 'set!))
;}}}
;{{{ New heap
(defopt (make-fh [p <])
(list (cons p 0)))
;}}}
;{{{ Print heap
(defopt (fh-show fh [tab '(1 3 1)])
(let* ([h #\x2500] [v #\x2502] [u #\x251c] [d #\x2514]
;[h #\-] [v #\|] [u #\|] [d #\\]
[s #\space] [str "~a\x1b;[3~d;1m~a\x1b;[m~%"]
[nps (car tab)] [ns (cadr tab)] [nss (caddr tab)]
[sp (make-string (+ nps ns nss) s)] [hh (make-string (1- ns) h)]
[ps (make-string nps s)] [ss (make-string nss s)]
[uh (string-append ps (make-string 1 u) hh ss)]
[dh (string-append ps (make-string 1 d) hh ss)]
[vs (string-append ps (make-string 1 v) (make-string (1- ns) s) ss)])
(unless (null? (cdr fh))
(let loop ([head (cdr fh)] [par (cons ps ps)] [pdr (cons ps ps)])
(let count ([st head])
(let* ([f (eq? (cdddr st) head)] [pr (if f par pdr)])
(printf str (car pr) (if (cddar st) 3 9) (caar st))
(unless (null? (cdadr st))
(loop (cdadr st)
(cons (string-append (cdr pr) dh)
(string-append (cdr pr) sp))
(cons (string-append (cdr pr) uh)
(string-append (cdr pr) vs))))
(unless f (count (cdddr st)))))))))
;}}}
;{{{ Empty test
(define (fh-empty? fh)
(null? (cdr fh)))
;}}}
;{{{ Merge heap
(define ($fh-merge! fh dt)
(unless (null? dt)
(let ([lt? (caar fh)] [n1 (cdr fh)] [n2 dt])
(if (null? n1) (set-cdr! fh n2)
(let-values ([(n1 n2)
(if (lt? (caar n1) (caar n2)) (values n1 n2)
(begin (set-cdr! fh n2) (values n2 n1)))])
(let ([n1t (caddr n1)] [n2t (caddr n2)])
(make-lr n2t n1)
(make-lr n1t n2))))))
fh)
(define (fh-merge! f1 f2)
(if (not (equal? (caar f1) (caar f2)))
(error 'fh-merge! "Incompatible order type"))
(set-cdr! (car f1) (+ (cdar f1) (cdar f2)))
($fh-merge! f1 (cdr f2)))
;}}}
;{{{ Push to heap
(define (fh-push! fh x)
(set-cdr! (car fh) (1+ (cdar fh)))
($fh-merge! fh (list*& (list* x 0 #f) (list '()) & &)))
;}}}
;{{{ Pop from heap
(define ($fh-extract! mt)
(let ([mtd (cdadr mt)])
(let-values ([(ml mr) (if (eq? (caddr mt) mt)
(values (caddr mtd) mtd)
(values (caddr mt) (cdddr mt)))])
(if (null? mtd)
(make-lr ml mr)
(begin
(make-lr ml mtd)
(let loop ([n mtd])
(make-ud nil n)
(set-cdr! (cdar n) #f)
(if (eq? (cdddr n) mtd)
(make-lr n mr)
(loop (cdddr n))))))
mr)))
(define ($fh-consolidate! fh)
(let ([lt? (caar fh)] [mt (cdr fh)])
($rvector-size (1+ (exact (floor
(log (cdar fh) (/ (1+ (sqrt 5)) 2))))))
(let loop ([n mt])
(let* ([r (cdddr n)] [f (eq? r mt)] [nr (let loop ([n n])
(cond [($rvector-ref (cadar n))
=> (lambda (m)
(let-values ([(n1 n2)
(if (lt? (caar n) (caar m))
(values n m) (values m n))])
(let ([n (cadar n1)])
($rvector-set! n #f)
(set-car! (cdar n1) (1+ n)))
(let ([n1d (cdadr n1)])
(make-ud n1 n2)
(set-cdr! (cdar n2) #f)
(make-lr (caddr n2) (cdddr n2))
(if (eq? n2 mt)
(set! mt (cdddr n2)))
(if (null? n1d)
(make-lr n2 n2)
(begin
(make-lr (caddr n1d) n2)
(make-lr n2 n1d))))
(loop n1)))]
[else ($rvector-set! (cadar n) n) n]))])
(if (and (eq? nr n) (lt? (caar n) (caadr fh)))
(set-cdr! fh n))
(if (not f) (loop r))))))
(define (fh-pop! fh)
(let ([mt (cdr fh)])
(if (null? mt)
(error 'fh-pop! "Empty heap"))
(let ([m (caar mt)] [n (1- (cdar fh))])
(set-cdr! (car fh) n)
(if (zero? n) (set-cdr! fh '())
(begin
(set-cdr! fh ($fh-extract! mt))
($fh-consolidate! fh)))
m)))
;}}}
;{{{ Search for a node in heap
(define (fh-node fh p?)
(let loop ([head (cdr fh)])
(let count ([n head])
(if (null? n) #f
(if (p? (caar n)) n
(or (loop (cdadr n))
(if (eq? (cdddr n) head) #f
(count (cdddr n)))))))))
;}}}
;{{{ Mutate a node in heap
(define ($fh-cut! fh node)
(unless (null? (caadr node))
(let loop ([node node])
(let ([p (caadr node)])
(let ([deg (1- (cadar p))])
(set-car! (cdar p) deg)
(if (eq? (cdadr p) node)
(if (zero? deg)
(make-ud p nil)
(make-ud p (cdddr node))))
(if (positive? deg)
(make-lr (caddr node) (cdddr node))))
(set-cdr! (cdar node) #f)
(make-ud nil node)
(make-lr (cadddr fh) node)
(make-lr node (cdr fh))
(unless (null? (caadr p))
(if (cddar p) (loop p)
(set-cdr! (cdar p) #t)))))))
(define ($fh-adjust-increase! fh node)
(let ([s (cdadr node)] [lt? (caar fh)] [m (caar node)])
(unless (null? s)
(let loop ([n (cdddr s)])
(let ([r (cdddr n)])
(if (lt? (caar n) m)
($fh-cut! fh n))
(if (not (eq? n s))
(loop r)))))
(if (eq? (cdr fh) node)
($fh-consolidate! fh))))
(define ($fh-adjust-decrease! fh node)
(let ([p (caadr node)] [lt? (caar fh)] [m (caar node)])
(unless (null? p)
(if (lt? m (caar p))
($fh-cut! fh node)))
(if (lt? m (caadr fh))
(set-cdr! fh node))))
(define ($fh-mutate! fh node mutator)
(let ([lt? (caar fh)])
(cond [(let-values ([(v mutate) (mutator (caar node))])
(let-syntax ([prog1 (syntax-rules ()
[(_ v body ...) (let ([v& v]) body ... v&)])])
(cond
[(not mutate) (prog1
(cond [(lt? (caar node) v) $fh-adjust-increase!]
[(lt? v (caar node)) $fh-adjust-decrease!]
[else #f])
(set-car! (car node) v))]
[(procedure? mutate) (prog1
(let ([v0 ((mutate 'accessor) (caar node))]
[lt? (mutate 'lt)])
(cond [(lt? v0 v) $fh-adjust-increase!]
[(lt? v v0) $fh-adjust-decrease!]
[else #f]))
((mutate 'mutator) (caar node) v))]
[(real? mutate)
(cond [(positive? mutate) $fh-adjust-increase!]
[(negative? mutate) $fh-adjust-decrease!]
[else #f])]
[else $fh-adjust-decrease!])))
=> (lambda (f) (f fh node))])
fh))
(define (fh-mutate! fh p? mutator)
(let ([node (fh-node fh p?)])
(and node ($fh-mutate! fh node mutator))))
;}}}
;{{{ Delete a node in heap
(define ($fh-delete! fh node)
(if (eq? (cdr fh) node)
(fh-pop! fh)
(begin
($fh-cut! fh node)
(set-cdr! (car fh) (1- (cdar fh)))
($fh-extract! node)))
fh)
(define (fh-delete! fh p?)
(let ([node (fh-node fh p?)])
(and node ($fh-delete! fh node))))
;}}}
;{{{ Make heap from list
(defopt (list->fh l [op #f])
(fold-left (lambda (x y) (fh-push! x y))
(if op (make-fh op) (make-fh)) l))
;}}}
)
;}}}
;{{{ Pairing Heap
(module ph%
(make-ph ph-show ph-empty? ph-merge! ph-push! ph-pop!
ph-node ph-mutate! ph-delete! list->ph)
;{{{ New heap
(defopt (make-ph [p <])
(cons p #f))
;}}}
;{{{ Print heap
(defopt (ph-show ph [tab '(1 3 1)])
(let* ([h #\x2500] [v #\x2502] [u #\x251c] [d #\x2514]
;[h #\-] [v #\|] [u #\|] [d #\\]
[s #\space] [str "~a\x1b;[1m~a\x1b;[m~%"]
[nps (car tab)] [ns (cadr tab)] [nss (caddr tab)]
[sp (make-string (+ nps ns nss) s)] [hh (make-string (1- ns) h)]
[ps (make-string nps s)] [ss (make-string nss s)]
[uh (string-append ps (make-string 1 u) hh ss)]
[dh (string-append ps (make-string 1 d) hh ss)]
[vs (string-append ps (make-string 1 v) (make-string (1- ns) s) ss)])
(when (cdr ph)
(let loop ([st (cdr ph)] [par ps] [pdr ps])
(printf str par (car st))
(let count ([st (cdr st)])
(unless (null? st)
(let-values ([(ar dr) (if (null? (cdr st))
(values dh sp) (values uh vs))])
(loop (car st) (string-append pdr ar) (string-append pdr dr)))
(count (cdr st))))))))
;}}}
;{{{ Empty test
(define (ph-empty? ph)
(not (cdr ph)))
;}}}
;{{{ Merge heap
(define ($ph-pair! cl lt?)
(if (null? cl) #f
(let loop ([cl cl])
(if (null? (cdr cl)) (car cl)
(loop
(let loop ([cl cl] [rl '()])
(cond [(null? cl) rl]
[(null? (cdr cl)) (cons (car cl) rl)]
[else
(let-values ([(a b) (if (lt? (caadr cl) (caar cl))
(values (cadr cl) (car cl))
(values (car cl) (cadr cl)))])
(set-cdr! a (cons b (cdr a)))
(loop (cddr cl) (cons a rl)))])))))))
(define ($ph-merge! ph dt)
(when dt
(let ([n1 (cdr ph)] [n2 dt])
(set-cdr! ph
(if (not n1) n2
($ph-pair! (list n1 n2) (car ph))))))
ph)
(define (ph-merge! p1 p2)
(if (not (equal? (car p1) (car p2)))
(error 'ph-merge! "Incompatible order type"))
($ph-merge! p1 (cdr p2)))
;}}}
;{{{ Push to heap
(define (ph-push! ph x)
($ph-merge! ph (list x)))
;}}}
;{{{ Pop from heap
(define (ph-pop! ph)
(if (not (cdr ph))
(error 'ph-pop! "Empty heap"))
(let ([m (cadr ph)])
(set-cdr! ph ($ph-pair! (cddr ph) (car ph)))
m))
;}}}
;{{{ Search for a node in heap
(define (ph-node ph p?)
(and (cdr ph)
(let loop ([n (cdr ph)])
(if (p? (car n)) n
(let count ([n (cdr n)])
(if (null? n) #f
(or (loop (car n))
(count (cdr n)))))))))
(define ($ph-trace ph p?)
(let ([r (and (cdr ph)
(let loop ([p #f] [b (list #f (cdr ph))] [n (cdr ph)])
(if (p? (car n)) (cons p b)
(let count ([l n])
(if (null? (cdr l)) #f
(or (loop n l (cadr l))
(count (cdr l))))))))])
(if r (values (car r) (cdr r))
(values #f #f))))
;}}}
;{{{ Mutate a node in heap
(define ($ph-adjust-increase! ph p b)
(let* ([n (cadr b)] [v (car n)] [lt? (car ph)])
(set-cdr! ph ($ph-pair!
(let loop ([l n] [r (list (cdr ph))])
(if (null? (cdr l)) r
(if (lt? (caadr l) v)
(let ([n (cadr l)])
(set-cdr! l (cddr l))
(loop l (cons n r)))
(loop (cdr l) r))))
lt?))))
(define ($ph-adjust-decrease! ph p b)
(let ([n (cadr b)])
(when (and p ((car ph) (car n) (car p)))
(set-cdr! b (cddr b))
($ph-merge! ph n)
(void))))
(define (ph-mutate! ph p? mutator)
(let-values ([(p b) ($ph-trace ph p?)])
(and b
(let ([lt? (car ph)] [t0 (cadr b)])
(cond [(let-values ([(v mutate) (mutator (car t0))])
(let-syntax ([prog1 (syntax-rules ()
[(_ v body ...) (let ([v& v]) body ... v&)])])
(cond
[(not mutate) (prog1
(cond [(lt? (car t0) v) $ph-adjust-increase!]
[(lt? v (car t0)) $ph-adjust-decrease!]
[else #f])
(set-car! t0 v))]
[(procedure? mutate) (prog1
(let ([v0 ((mutate 'accessor) (car t0))]
[lt? (mutate 'lt)])
(cond [(lt? v0 v) $ph-adjust-increase!]
[(lt? v v0) $ph-adjust-decrease!]
[else #f]))
((mutate 'mutator) (car t0) v))]
[(real? mutate)
(cond [(positive? mutate) $ph-adjust-increase!]
[(negative? mutate) $ph-adjust-decrease!]
[else #f])]
[else $ph-adjust-decrease!])))
=> (lambda (f) (f ph p b))])
ph))))
;}}}
;{{{ Delete a node in heap
(define (ph-delete! ph p?)
(let-values ([(p b) ($ph-trace ph p?)])