Heap.st
changeset 5386 5165431572dd
parent 5206 dcadf2efcee0
equal deleted inserted replaced
5385:a54bf0f05ff5 5386:5165431572dd
     1 "{ Encoding: utf8 }"
     1 "{ Encoding: utf8 }"
     2 
     2 
       
     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 "
     3 "{ Package: 'stx:libbasic2' }"
    14 "{ Package: 'stx:libbasic2' }"
     4 
    15 
     5 "{ NameSpace: Smalltalk }"
    16 "{ NameSpace: Smalltalk }"
     6 
    17 
     7 SequenceableCollection subclass:#Heap
    18 SequenceableCollection subclass:#Heap
    21 	sortBlock	<Block|nil>	a two-argument block defining the sort order,
    32 	sortBlock	<Block|nil>	a two-argument block defining the sort order,
    22 							or nil in which case the default sort order is
    33 							or nil in which case the default sort order is
    23 								[:element1 :element2| element1 <= element2]'
    34 								[:element1 :element2| element1 <= element2]'
    24 !
    35 !
    25 
    36 
    26 
    37 !Heap class methodsFor:'documentation'!
    27 !Heap class methodsFor:'instance creation'!
    38 
    28 
    39 copyright
    29 new
    40 "
    30 	^self new: 10
    41  COPYRIGHT (c) 2018 by eXept Software AG
    31 !
    42               All Rights Reserved
    32 
    43 
    33 new: n
    44  This software is furnished under a license and may be used
    34 	^super new setCollection: (Array new: n)
    45  only in accordance with the terms of that license and with the
    35 !
    46  inclusion of the above copyright notice.   This software may not
    36 
    47  be provided or otherwise made available to, or used by, any
    37 sortBlock: aBlock
    48  other person.  No title to or ownership of the software is
    38 	"Create a new heap sorted by the given block"
    49  hereby transferred.
    39 	^self new sortBlock: aBlock
    50 "
    40 !
    51 !
    41 
       
    42 withAll: aCollection
       
    43         "Create a new heap with all the elements from aCollection"
       
    44         ^(self basicNew)
       
    45                 setCollection: aCollection asNewArray tally: aCollection size;
       
    46                 reSort;
       
    47                 yourself
       
    48 !
       
    49 
       
    50 withAll: aCollection sortBlock: sortBlock
       
    51         "Create a new heap with all the elements from aCollection"
       
    52         ^(self basicNew)
       
    53                 setCollection: aCollection asNewArray tally: aCollection size;
       
    54                 sortBlock: sortBlock;
       
    55                 yourself
       
    56 ! !
       
    57 
       
    58 !Heap class methodsFor:'examples'!
       
    59 
    52 
    60 documentation
    53 documentation
    61 "
    54 "
    62     Class Heap implements a special data structure commonly referred to as 'heap'.
    55     Class Heap implements a special data structure commonly referred to as 'heap'.
    63     Heaps are more efficient than SortedCollections if:
    56     Heaps are more efficient than SortedCollections if:
   125         Transcript showCR:'Time for quick-sort: ', time printString,' msecs'.
   118         Transcript showCR:'Time for quick-sort: ', time printString,' msecs'.
   126                                                                              [exEnd]
   119                                                                              [exEnd]
   127 "
   120 "
   128 
   121 
   129     "Created: / 31-05-2007 / 14:46:59 / cg"
   122     "Created: / 31-05-2007 / 14:46:59 / cg"
       
   123 ! !
       
   124 
       
   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
   130 ! !
   154 ! !
   131 
   155 
   132 !Heap methodsFor:'accessing'!
   156 !Heap methodsFor:'accessing'!
   133 
   157 
   134 at: index
   158 at: index