1880
|
1 |
"{ Package: 'stx:libbasic2' }"
|
|
2 |
|
|
3 |
SequenceableCollection subclass:#Heap
|
|
4 |
instanceVariableNames:'array tally sortBlock'
|
|
5 |
classVariableNames:''
|
|
6 |
poolDictionaries:''
|
|
7 |
category:'Collections-Sequenceable'
|
|
8 |
!
|
|
9 |
|
2353
|
10 |
Heap comment:'Class Heap implements a special data structure commonly referred to as ''heap''. Heaps are more efficient than SortedCollections if:
|
|
11 |
a) Elements are only removed at the beginning
|
|
12 |
b) Elements are added with arbitrary sort order.
|
|
13 |
The sort time for a heap is O(n log n) in all cases.
|
|
14 |
Instance variables:
|
|
15 |
array <Array> the data repository
|
|
16 |
tally <Integer> the number of elements in the heap
|
|
17 |
sortBlock <Block|nil> a two-argument block defining the sort order,
|
|
18 |
or nil in which case the default sort order is
|
|
19 |
[:element1 :element2| element1 <= element2]'
|
1880
|
20 |
!
|
|
21 |
|
|
22 |
|
|
23 |
!Heap class methodsFor:'instance creation'!
|
|
24 |
|
|
25 |
new
|
|
26 |
^self new: 10
|
|
27 |
!
|
|
28 |
|
|
29 |
new: n
|
|
30 |
^super new setCollection: (Array new: n)
|
|
31 |
!
|
|
32 |
|
|
33 |
sortBlock: aBlock
|
|
34 |
"Create a new heap sorted by the given block"
|
|
35 |
^self new sortBlock: aBlock
|
|
36 |
!
|
|
37 |
|
|
38 |
withAll: aCollection
|
|
39 |
"Create a new heap with all the elements from aCollection"
|
|
40 |
^(self basicNew)
|
|
41 |
setCollection: aCollection asArray copy tally: aCollection size;
|
|
42 |
reSort;
|
|
43 |
yourself
|
|
44 |
!
|
|
45 |
|
|
46 |
withAll: aCollection sortBlock: sortBlock
|
|
47 |
"Create a new heap with all the elements from aCollection"
|
|
48 |
^(self basicNew)
|
|
49 |
setCollection: aCollection asArray copy tally: aCollection size;
|
|
50 |
sortBlock: sortBlock;
|
|
51 |
yourself
|
|
52 |
! !
|
|
53 |
|
|
54 |
!Heap class methodsFor:'examples'!
|
|
55 |
|
|
56 |
documentation
|
|
57 |
"
|
|
58 |
Class Heap implements a special data structure commonly referred to as 'heap'.
|
|
59 |
Heaps are more efficient than SortedCollections if:
|
|
60 |
a) Elements are only removed at the beginning
|
|
61 |
b) Elements are added with arbitrary sort order.
|
|
62 |
|
|
63 |
The sort time for a heap is O(n log n) in all cases.
|
|
64 |
Instance variables:
|
|
65 |
array <Array> the data repository
|
|
66 |
tally <Integer> the number of elements in the heap
|
|
67 |
sortBlock <Block|nil> a two-argument block defining the sort order,
|
|
68 |
or nil in which case the default sort order is
|
|
69 |
[:element1 :element2| element1 <= element2]
|
|
70 |
"
|
|
71 |
|
|
72 |
"Created: / 31-05-2007 / 14:53:44 / cg"
|
|
73 |
!
|
|
74 |
|
|
75 |
examples
|
|
76 |
"
|
|
77 |
Create a sorted collection of numbers, remove the elements
|
|
78 |
sequentially and add new objects randomly.
|
|
79 |
Note: This is the kind of benchmark a heap is designed for.
|
|
80 |
[exBegin]
|
|
81 |
| n rnd array time sorted |
|
2353
|
82 |
n := 50000.
|
1880
|
83 |
rnd := Random new.
|
|
84 |
array := (1 to: n) collect:[:i| rnd next].
|
|
85 |
|
|
86 |
time := Time millisecondsToRun:[
|
|
87 |
sorted := Heap withAll: array.
|
|
88 |
1 to: n do:[:i|
|
|
89 |
sorted removeFirst.
|
|
90 |
sorted add: rnd next].
|
|
91 |
].
|
|
92 |
Transcript showCR:'Time for Heap: ', time printString,' msecs'.
|
|
93 |
|
|
94 |
time := Time millisecondsToRun:[
|
|
95 |
sorted := SortedCollection withAll: array.
|
|
96 |
1 to: n do:[:i|
|
|
97 |
sorted removeFirst.
|
|
98 |
sorted add: rnd next].
|
|
99 |
].
|
|
100 |
Transcript showCR:'Time for SortedCollection: ', time printString,' msecs'.
|
|
101 |
[exEnd]
|
|
102 |
|
|
103 |
Sort a random collection of Floats and compare the results with
|
|
104 |
SortedCollection (using the quick-sort algorithm) and
|
|
105 |
[exBegin]
|
|
106 |
| n rnd array out time sorted |
|
2353
|
107 |
n := 40000.
|
1880
|
108 |
rnd := Random new.
|
|
109 |
array := (1 to: n) collect:[:i| rnd next].
|
|
110 |
|
|
111 |
out := Array new: n.
|
|
112 |
time := Time millisecondsToRun:[
|
|
113 |
sorted := Heap withAll: array.
|
|
114 |
1 to: n do:[:i| sorted removeFirst].
|
|
115 |
].
|
|
116 |
Transcript showCR:'Time for heap-sort: ', time printString,' msecs'.
|
|
117 |
|
|
118 |
time := Time millisecondsToRun:[
|
|
119 |
sorted := SortedCollection withAll: array.
|
|
120 |
].
|
|
121 |
Transcript showCR:'Time for quick-sort: ', time printString,' msecs'.
|
|
122 |
[exEnd]
|
|
123 |
"
|
|
124 |
|
|
125 |
"Created: / 31-05-2007 / 14:46:59 / cg"
|
|
126 |
! !
|
|
127 |
|
|
128 |
!Heap methodsFor:'accessing'!
|
|
129 |
|
|
130 |
at: index
|
|
131 |
"Return the element at the given position within the receiver"
|
|
132 |
(index < 1 or:[index > tally]) ifTrue:[^self subscriptBoundsError: "errorSubscriptBounds:" index].
|
|
133 |
^array at: index
|
|
134 |
|
|
135 |
"Modified: / 31-05-2007 / 14:44:58 / cg"
|
|
136 |
!
|
|
137 |
|
|
138 |
at: index put: newObject
|
|
139 |
"Heaps are accessed with #add: not #at:put:"
|
|
140 |
^self shouldNotImplement
|
|
141 |
!
|
|
142 |
|
|
143 |
first
|
2806
|
144 |
"Return the first element in the receiver"
|
|
145 |
|
|
146 |
self isEmpty ifTrue:[
|
|
147 |
^ self emptyCollectionError.
|
|
148 |
].
|
|
149 |
^ array at: 1
|
|
150 |
|
|
151 |
"
|
|
152 |
Heap new first
|
|
153 |
"
|
1880
|
154 |
!
|
|
155 |
|
|
156 |
firstOrNil
|
|
157 |
tally = 0 ifTrue:[^nil] ifFalse:[^array at: 1]
|
|
158 |
!
|
|
159 |
|
|
160 |
reSort
|
|
161 |
"Resort the entire heap"
|
|
162 |
self isEmpty ifTrue:[^self].
|
|
163 |
tally // 2 to: 1 by: -1 do:[:i| self downHeap: i].
|
|
164 |
!
|
|
165 |
|
|
166 |
size
|
|
167 |
"Answer how many elements the receiver contains."
|
|
168 |
|
|
169 |
^ tally
|
|
170 |
!
|
|
171 |
|
|
172 |
sortBlock
|
|
173 |
^sortBlock
|
|
174 |
!
|
|
175 |
|
|
176 |
sortBlock: aBlock
|
|
177 |
sortBlock := aBlock.
|
|
178 |
sortBlock fixTemps.
|
|
179 |
self reSort.
|
|
180 |
! !
|
|
181 |
|
|
182 |
!Heap methodsFor:'adding'!
|
|
183 |
|
|
184 |
add: anObject
|
|
185 |
"Include newObject as one of the receiver's elements. Answer newObject."
|
|
186 |
tally = array size ifTrue:[self grow].
|
|
187 |
array at: (tally := tally + 1) put: anObject.
|
|
188 |
self upHeap: tally.
|
|
189 |
^anObject
|
|
190 |
! !
|
|
191 |
|
|
192 |
!Heap methodsFor:'comparing'!
|
|
193 |
|
|
194 |
= anObject
|
|
195 |
|
|
196 |
^ self == anObject
|
|
197 |
ifTrue: [true]
|
|
198 |
ifFalse: [anObject isHeap
|
|
199 |
ifTrue: [sortBlock = anObject sortBlock and: [super = anObject]]
|
|
200 |
ifFalse: [super = anObject]]
|
|
201 |
! !
|
|
202 |
|
|
203 |
!Heap methodsFor:'enumerating'!
|
|
204 |
|
|
205 |
do: aBlock
|
|
206 |
"Evaluate aBlock with each of the receiver's elements as the argument."
|
|
207 |
1 to: tally do:[:i| aBlock value: (array at: i)]
|
|
208 |
! !
|
|
209 |
|
|
210 |
!Heap methodsFor:'growing'!
|
|
211 |
|
|
212 |
grow
|
|
213 |
"Become larger."
|
|
214 |
self growTo: self size + self growSize.
|
|
215 |
!
|
|
216 |
|
|
217 |
growSize
|
|
218 |
"Return the size by which the receiver should grow if there are no empty slots left."
|
|
219 |
^array size max: 5
|
|
220 |
!
|
|
221 |
|
|
222 |
growTo: newSize
|
|
223 |
"Grow to the requested size."
|
|
224 |
| newArray |
|
|
225 |
newArray := Array new: (newSize max: tally).
|
|
226 |
newArray replaceFrom: 1 to: array size with: array startingAt: 1.
|
|
227 |
array := newArray
|
|
228 |
!
|
|
229 |
|
|
230 |
trim
|
|
231 |
"Remove any empty slots in the receiver."
|
|
232 |
self growTo: self size.
|
|
233 |
! !
|
|
234 |
|
|
235 |
!Heap methodsFor:'private'!
|
|
236 |
|
|
237 |
array
|
|
238 |
^array
|
|
239 |
!
|
|
240 |
|
|
241 |
privateRemoveAt: index
|
|
242 |
"Remove the element at the given index and make sure the sorting order is okay"
|
|
243 |
| removed |
|
|
244 |
removed := array at: index.
|
|
245 |
array at: index put: (array at: tally).
|
|
246 |
array at: tally put: nil.
|
|
247 |
tally := tally - 1.
|
|
248 |
index > tally ifFalse:[
|
|
249 |
"Use #downHeapSingle: since only one element has been removed"
|
|
250 |
self downHeapSingle: index].
|
|
251 |
^removed
|
|
252 |
!
|
|
253 |
|
|
254 |
setCollection: aCollection
|
|
255 |
array := aCollection.
|
|
256 |
tally := 0.
|
|
257 |
!
|
|
258 |
|
|
259 |
setCollection: aCollection tally: newTally
|
|
260 |
array := aCollection.
|
|
261 |
tally := newTally.
|
|
262 |
!
|
|
263 |
|
|
264 |
species
|
|
265 |
^ Array
|
|
266 |
! !
|
|
267 |
|
|
268 |
!Heap methodsFor:'private-heap'!
|
|
269 |
|
|
270 |
downHeap: anIndex
|
|
271 |
"Check the heap downwards for correctness starting at anIndex.
|
|
272 |
Everything above (i.e. left of) anIndex is ok."
|
|
273 |
| value k n j |
|
|
274 |
anIndex = 0 ifTrue:[^self].
|
|
275 |
n := tally bitShift: -1.
|
|
276 |
k := anIndex.
|
|
277 |
value := array at: anIndex.
|
|
278 |
[k <= n] whileTrue:[
|
|
279 |
j := k + k.
|
|
280 |
"use max(j,j+1)"
|
|
281 |
(j < tally and:[self sorts: (array at: j+1) before: (array at: j)])
|
|
282 |
ifTrue:[ j := j + 1].
|
|
283 |
"check if position k is ok"
|
|
284 |
(self sorts: value before: (array at: j))
|
|
285 |
ifTrue:[ "yes -> break loop"
|
|
286 |
n := k - 1]
|
|
287 |
ifFalse:[ "no -> make room at j by moving j-th element to k-th position"
|
|
288 |
array at: k put: (array at: j).
|
|
289 |
"and try again with j"
|
|
290 |
k := j]].
|
|
291 |
array at: k put: value.
|
|
292 |
!
|
|
293 |
|
|
294 |
downHeapSingle: anIndex
|
|
295 |
"This version is optimized for the case when only one element in the receiver can be at a wrong position. It avoids one comparison at each node when travelling down the heap and checks the heap upwards after the element is at a bottom position. Since the probability for being at the bottom of the heap is much larger than for being somewhere in the middle this version should be faster."
|
|
296 |
| value k n j |
|
|
297 |
anIndex = 0 ifTrue:[^self].
|
|
298 |
n := tally bitShift: -1.
|
|
299 |
k := anIndex.
|
|
300 |
value := array at: anIndex.
|
|
301 |
[k <= n] whileTrue:[
|
|
302 |
j := k + k.
|
|
303 |
"use max(j,j+1)"
|
|
304 |
(j < tally and:[self sorts: (array at: j+1) before: (array at: j)])
|
|
305 |
ifTrue:[ j := j + 1].
|
|
306 |
array at: k put: (array at: j).
|
|
307 |
"and try again with j"
|
|
308 |
k := j].
|
|
309 |
array at: k put: value.
|
|
310 |
self upHeap: k
|
|
311 |
!
|
|
312 |
|
|
313 |
upHeap: anIndex
|
|
314 |
"Check the heap upwards for correctness starting at anIndex.
|
|
315 |
Everything below anIndex is ok."
|
|
316 |
| value k kDiv2 tmp |
|
|
317 |
anIndex = 0 ifTrue:[^self].
|
|
318 |
k := anIndex.
|
|
319 |
value := array at: anIndex.
|
|
320 |
[ (k > 1) and:[self sorts: value before: (tmp := array at: (kDiv2 := k bitShift: -1))] ]
|
|
321 |
whileTrue:[
|
|
322 |
array at: k put: tmp.
|
|
323 |
k := kDiv2].
|
|
324 |
array at: k put: value.
|
|
325 |
! !
|
|
326 |
|
|
327 |
!Heap methodsFor:'removing'!
|
|
328 |
|
|
329 |
remove: oldObject ifAbsent: aBlock
|
|
330 |
"Remove oldObject as one of the receiver's elements. If several of the
|
|
331 |
elements are equal to oldObject, only one is removed. If no element is
|
|
332 |
equal to oldObject, answer the result of evaluating anExceptionBlock.
|
|
333 |
Otherwise, answer the argument, oldObject."
|
|
334 |
1 to: tally do:[:i|
|
|
335 |
(array at: i) = oldObject ifTrue:[^self privateRemoveAt: i]].
|
|
336 |
^aBlock value
|
|
337 |
!
|
|
338 |
|
|
339 |
removeAt: index
|
|
340 |
"Remove the element at given position"
|
|
341 |
(index < 1 or:[index > tally]) ifTrue:[^self subscriptBoundsError: "errorSubscriptBounds:" index].
|
|
342 |
^self privateRemoveAt: index
|
|
343 |
|
|
344 |
"Modified: / 31-05-2007 / 14:45:42 / cg"
|
|
345 |
!
|
|
346 |
|
|
347 |
removeFirst
|
|
348 |
"Remove the first element from the receiver"
|
|
349 |
^self removeAt: 1
|
|
350 |
! !
|
|
351 |
|
|
352 |
!Heap methodsFor:'testing'!
|
|
353 |
|
|
354 |
isEmpty
|
|
355 |
"Answer whether the receiver contains any elements."
|
|
356 |
^tally = 0
|
|
357 |
!
|
|
358 |
|
|
359 |
isHeap
|
|
360 |
|
|
361 |
^ true
|
|
362 |
!
|
|
363 |
|
|
364 |
sorts: element1 before: element2
|
|
365 |
"Return true if element1 should be sorted before element2.
|
|
366 |
This method defines the sort order in the receiver"
|
|
367 |
^sortBlock == nil
|
|
368 |
ifTrue:[element1 <= element2]
|
|
369 |
ifFalse:[sortBlock value: element1 value: element2].
|
|
370 |
! !
|
|
371 |
|
|
372 |
!Heap class methodsFor:'documentation'!
|
|
373 |
|
|
374 |
version
|
2806
|
375 |
^ '$Header: /cvs/stx/stx/libbasic2/Heap.st,v 1.3 2012-08-13 11:15:33 stefan Exp $'
|
2353
|
376 |
!
|
|
377 |
|
|
378 |
version_CVS
|
2806
|
379 |
^ '$Header: /cvs/stx/stx/libbasic2/Heap.st,v 1.3 2012-08-13 11:15:33 stefan Exp $'
|
1880
|
380 |
! !
|