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