This repository has been archived by the owner on May 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsplay-heap.lisp
177 lines (157 loc) · 6.73 KB
/
splay-heap.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
;;;; splay heap implementation translated from Okasaki's Purely
;;;; Functional Data Structures.
(in-package :net.mwatters.heap)
(define-struct-like-class splay-heap (tree-based-heap)
;; cached element with minimum priority:
min)
;; note: want these as auto-defined accessors, but need to extend
;; struct-like-classes impl (or just use structs).
(defmacro splay-heap-root (h)
`(tree-based-heap-root ,h))
(defmacro splay-heap-predicate (h)
`(tree-based-heap-predicate ,h))
(defmacro with-splay-pivoting ((left elt right) node
pred pivot
&key empty smaller otherwise)
"helper macro for expressing splay tree pivoting logic. if NODE is
empty \(nil\), return the value of EMPTY. otherwise, bind the vars
denoted by LEFT, ELT, and RIGHT to the corresponding values of NODE.
if the value of ELT is less than the value of PIVOT according to PRED,
return the value of SMALLER. otherwise return the value of
OTHERWISE."
(with-gensyms (n p)
`(let ((,n ,node)
(,p ,pivot))
(symbol-macrolet
((,left (tree-node-left ,n))
(,elt (tree-node-element ,n))
(,right (tree-node-right ,n)))
(cond
((not ,n)
,empty)
((funcall ,pred ,elt ,p)
,smaller)
(t
,otherwise))))))
(defun splay-heap-partition (root pred pivot)
"return two splay trees containing all elements in the tree-node
ROOT less than and greater than or equal to PIVOT according to PRED,
respectively."
(with-splay-pivoting (a x b) root
pred pivot
:empty (cons nil nil)
;; X is smaller than the pivot (A, X will be in left subtree):
:smaller (with-splay-pivoting (b1 y b2) b
pred pivot
:empty (cons root nil)
;; Y is smaller than the pivot (B1, Y will be in left
;; subtree, split up B2):
:smaller (destructuring-bind (smaller . bigger)
(splay-heap-partition b2 pred pivot)
(cons (make-tree-node
:left (make-tree-node
:left a
:element x
:right b1)
:element y
:right smaller)
bigger))
;; Y is gte the pivot (Y, B2 will be in right subtree,
;; split up B1):
:otherwise (destructuring-bind (smaller . bigger)
(splay-heap-partition b1 pred pivot)
(cons (make-tree-node
:left a
:element x
:right smaller)
(make-tree-node
:left bigger
:element y
:right b2))))
;; X is gte the pivot (X, B will be in right subtree):
:otherwise (with-splay-pivoting (a1 y a2) a
pred pivot
:empty (cons nil root)
;; Y is smaller than the pivot (A1, Y will be in left
;; subtree, split up A2):
:smaller (destructuring-bind (smaller . bigger)
(splay-heap-partition a2 pred pivot)
(cons (make-tree-node
:left a1
:element y
:right smaller)
(make-tree-node
:left bigger
:element x
:right b)))
;; Y is gte the pivot (Y, A2 will be in right
;; subtree, split up A1):
:otherwise (destructuring-bind (smaller . bigger)
(splay-heap-partition a1 pred pivot)
(cons smaller
(make-tree-node
:left bigger
:element y
:right (make-tree-node
:left a2
:element x
:right b)))))))
(defmethod tree-based-heap-insert ((h splay-heap) e)
(let ((r (splay-heap-root h))
(p (splay-heap-predicate h)))
(destructuring-bind (smaller . bigger)
(splay-heap-partition r p e)
(setf (splay-heap-root h) (make-tree-node
:left smaller
:element e
:right bigger))
;; record new minimum element:
(when (or (not (splay-heap-min h))
(funcall p e (splay-heap-min h))
;; if the new element has the same priority as the
;; current minimum, it will be the new root element:
(and (not (funcall p e (splay-heap-min h)))
(not (funcall p (splay-heap-min h) e))))
(setf (splay-heap-min h) e))
h)))
(defun splay-heap-find-min (n)
(when n
(let ((left (tree-node-left n)))
(if left
(splay-heap-find-min left)
(tree-node-element n)))))
(defun splay-heap-remove-min (n)
(when n
(let ((left (tree-node-left n)))
(if (not left)
(tree-node-right n)
(symbol-macrolet
((a (tree-node-left left))
(x (tree-node-element left))
(b (tree-node-right left))
(y (tree-node-element n))
(c (tree-node-right n)))
(if (not a)
(make-tree-node :left b
:element y
:right c)
(make-tree-node :left (splay-heap-remove-min a)
:element x
:right (make-tree-node :left b
:element y
:right c))))))))
(defmethod heap-next ((h splay-heap))
(let* ((saved-min (splay-heap-min h))
(n (if saved-min
saved-min
(splay-heap-find-min (splay-heap-root h)))))
(unless saved-min
(setf (splay-heap-min h) n))
(values (pqueue-node-data n)
(pqueue-node-priority n)
nil)))
(defmethod heap-remove-min ((h splay-heap))
(setf
(splay-heap-root h) (splay-heap-remove-min (splay-heap-root h))
(splay-heap-min h) (splay-heap-find-min (splay-heap-root h)))
h)