-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpairing-heap-listbased.lisp
173 lines (149 loc) · 5.48 KB
/
pairing-heap-listbased.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
;;;; MINHEAP is by Stephan Frank <[email protected]>, 2007-2012.
;;;;
;;;; Permission is hereby granted, free of charge, to any person obtaining
;;;; a copy of this software and associated documentation files (the
;;;; "Software"), to deal in the Software without restriction, including
;;;; without limitation the rights to use, copy, modify, merge, publish,
;;;; distribute, sublicense, and/or sell copies of the Software, and to
;;;; permit persons to whom the Software is furnished to do so, subject to
;;;; the following conditions:
;;;;
;;;; The above copyright notice and this permission notice shall be included
;;;; in all copies or substantial portions of the Software.
;;;;
;;;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
;;;; EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
;;;; MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
;;;; IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
;;;; CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
;;;; TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
;;;; SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
(defpackage :pairing-heap-list (:use :cl)
(:export
#:pairing-heap
#:clear-heap
#:empty-p
#:insert
#:peek-min
#:extract-min
#:extract-node
#:heap-size
#:decrease-key
#:meld
))
(in-package :pairing-heap-list)
;;;; Variant of pairing-heap.lisp where the children of a node are
;;;; implemented as a list. The code is a lot more readable, however
;;;; due to the consing in attach-child this variant is about three
;;;; times slower than the more awkward implementation in
;;;; pairing-heap.lisp.
(defstruct (node (:constructor %make-node (key data)))
(key 0 :type fixnum)
(data nil)
(children nil :type list)
(parent nil :type (or null node)))
(defclass pairing-heap ()
((size :initform 0 :initarg :size
:type (integer 0 *))
(root :initform nil :initarg :root
:type (or null node))))
(defmethod print-object ((obj pairing-heap) stream)
(print-unreadable-object (obj stream :type t :identity t)
(format stream "~4I~:_size: ~A~:_" (slot-value obj 'size))))
(declaim (inline heap-size))
(defun heap-size (heap)
(slot-value heap 'size))
(defun empty-p (heap)
(zerop (slot-value heap 'size)))
(defun clear-heap (heap)
(setf (slot-value heap 'size) 0
(slot-value heap 'root) nil)
heap)
(defun peek-min (heap)
(let ((node (slot-value heap 'root)))
(assert node)
(values (node-data node)
(node-key node))))
(defun extract-min (heap)
(let ((node (slot-value heap 'root)))
(assert node)
(setf (slot-value heap 'root) (combine-siblings node))
(decf (slot-value heap 'size))
(values (node-data node)
(node-key node))))
(defun insert (heap key data)
(let ((node (%make-node key data)))
(incf (slot-value heap 'size))
(setf (slot-value heap 'root)
(if (slot-value heap 'root)
(link (slot-value heap 'root) node)
node))
node))
(defun decrease-key (heap node key)
(if (< (node-key node) key)
(error "Cannot decrease key: new key greater than current key.")
(setf (node-key node) key))
(unless (eq node (slot-value heap 'root))
(setf (slot-value heap 'root)
(link (slot-value heap 'root)
(cut-parent node))))
node)
(defun extract-node (heap node)
(let ((root (slot-value heap 'root)))
(cond
((eq root node)
(extract-min heap))
(t
(cut-parent node)
(let ((tmp (make-instance 'pairing-heap
:size 1
:root node)))
;(declare (dynamic-extent tmp))
(prog1 (extract-min tmp)
(meld heap tmp)))))))
(defun meld (heap-a heap-b)
"Melds HEAP-A and HEAP-B into HEAP-A and returns it. HEAP-B will be
empty after this operation but may be used further."
(incf (slot-value heap-a 'size) (slot-value heap-b 'size))
(setf (slot-value heap-a 'root)
(link (slot-value heap-a 'root)
(slot-value heap-b 'root)))
(clear-heap heap-b)
heap-a)
;;; internal structure maintaining functions
;(declaim (inline attach-child link))
(defun attach-child (parent child)
(push child (node-children parent))
(setf (node-parent child) parent))
(defun link (node-a node-b)
(cond
;((null node-a) node-b)
;((null node-b) node-a)
((< (node-key node-a) (node-key node-b))
(attach-child node-a node-b))
(t (attach-child node-b node-a))))
(defun combine-siblings (parent)
(link-children (pair-children parent)))
(defun pair-children (parent)
(let ((children (node-children parent)))
(setf (node-children parent) nil)
(loop for childs on children by #'cddr
while childs
do (if (cdr childs)
(attach-child parent (link (car childs)
(cadr childs)))
(attach-child parent (car childs)))
finally (return parent))))
(defun link-children (parent)
(when (node-children parent)
(loop for root = (car (node-children parent)) then (link root c)
for c in (cdr (node-children parent))
finally (setf (node-children parent) (list root))
(return root))))
(defun cut-parent (node)
(let ((parent (node-parent node)))
(when parent
(setf (node-children parent)
(delete node (node-children parent) :test #'eq :count 1)))
(setf (node-parent node) nil))
node)