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