author | Claus Gittinger <cg@exept.de> |
Wed, 10 Apr 2019 06:08:33 +0200 | |
changeset 24050 | f39b5494bc52 |
parent 23703 | a5b7669034e8 |
child 24627 | 73fa91544f88 |
permissions | -rw-r--r-- |
1 | 1 |
" |
2 |
COPYRIGHT (c) 1993 by Claus Gittinger |
|
159 | 3 |
All Rights Reserved |
1 | 4 |
|
5 |
This software is furnished under a license and may be used |
|
6 |
only in accordance with the terms of that license and with the |
|
7 |
inclusion of the above copyright notice. This software may not |
|
8 |
be provided or otherwise made available to, or used by, any |
|
9 |
other person. No title to or ownership of the software is |
|
10 |
hereby transferred. |
|
11 |
" |
|
5359
396b77b6f1d2
added #isSortedCollection, #isSortedBy:
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
12 |
"{ Package: 'stx:libbasic' }" |
396b77b6f1d2
added #isSortedCollection, #isSortedBy:
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
13 |
|
18508 | 14 |
"{ NameSpace: Smalltalk }" |
15 |
||
1 | 16 |
OrderedCollection subclass:#SortedCollection |
1155 | 17 |
instanceVariableNames:'sortBlock' |
18 |
classVariableNames:'DefaultSortBlock' |
|
19 |
poolDictionaries:'' |
|
20 |
category:'Collections-Sequenceable' |
|
1 | 21 |
! |
22 |
||
77 | 23 |
!SortedCollection class methodsFor:'documentation'! |
1 | 24 |
|
88 | 25 |
copyright |
26 |
" |
|
27 |
COPYRIGHT (c) 1993 by Claus Gittinger |
|
159 | 28 |
All Rights Reserved |
88 | 29 |
|
30 |
This software is furnished under a license and may be used |
|
31 |
only in accordance with the terms of that license and with the |
|
32 |
inclusion of the above copyright notice. This software may not |
|
33 |
be provided or otherwise made available to, or used by, any |
|
34 |
other person. No title to or ownership of the software is |
|
35 |
hereby transferred. |
|
36 |
" |
|
37 |
! |
|
38 |
||
77 | 39 |
documentation |
40 |
" |
|
41 |
I keep my elements sorted. The sort order is defined by a sortblock, |
|
8874 | 42 |
a two-argument block which, when given two elements of the collection, |
43 |
should return true if the element given as first arg has to come before the |
|
77 | 44 |
element given as second arg. |
45 |
||
1156 | 46 |
Equal elements may occur multiple times. |
47 |
||
11274
a6f648b1d291
tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents:
11268
diff
changeset
|
48 |
SortedCollection uses quickSort to resort and a binary search when adding/removing elements. |
1156 | 49 |
Because insertion/removal may require that remaining elements have to |
20634 | 50 |
be shifted within the container, adding many individual elements will be faster above a |
22336 | 51 |
particular number of added elements by creating a completely new collection from the |
20634 | 52 |
unsorted elements. |
1167 | 53 |
(see examples) |
1156 | 54 |
|
20634 | 55 |
A sortblock, given arguments a,b should return true, if a is to be shown before b. |
11274
a6f648b1d291
tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents:
11268
diff
changeset
|
56 |
A sortBlock of [:a :b | a < b] defines ascending sort-order, |
77 | 57 |
while [:a :b | a > b] defines descening order. |
20634 | 58 |
The default sortBlock for SortedCollections is the first one (i.e. sorting in ascending order). |
1290 | 59 |
|
3251
87cac58a4e57
added VW compatibility warning
Claus Gittinger <cg@exept.de>
parents:
3250
diff
changeset
|
60 |
Compatibility Warning: |
22336 | 61 |
VW seems to use a default sortBlock which compares a<=b, whereas ST/X uses a<b. |
11274
a6f648b1d291
tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents:
11268
diff
changeset
|
62 |
That means, that elements which are compared MUST understand #< in ST/X |
a6f648b1d291
tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents:
11268
diff
changeset
|
63 |
while the minumum protocol is #<= in VW. |
a6f648b1d291
tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents:
11268
diff
changeset
|
64 |
This may be changed in a future release of ST/X |
a6f648b1d291
tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents:
11268
diff
changeset
|
65 |
(it is not yet, to not confuse existing applications ... |
a6f648b1d291
tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents:
11268
diff
changeset
|
66 |
... be aware, that the sortBlock has an effect on a few algorithms |
a6f648b1d291
tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents:
11268
diff
changeset
|
67 |
found here; especially #indexForInserting is critical.) |
8874 | 68 |
|
14304 | 69 |
[caveat:] |
70 |
a balanced tree may be a better choice, as inserting may be slow when |
|
22336 | 71 |
elements have to be shifted. See the performance measurements in AATree. |
3251
87cac58a4e57
added VW compatibility warning
Claus Gittinger <cg@exept.de>
parents:
3250
diff
changeset
|
72 |
|
1290 | 73 |
[author:] |
11274
a6f648b1d291
tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents:
11268
diff
changeset
|
74 |
Claus Gittinger |
77 | 75 |
" |
1167 | 76 |
! |
77 |
||
78 |
examples |
|
79 |
" |
|
8874 | 80 |
when many elements are to be added, it may be better to add them |
1175 | 81 |
all en-bloeque instead of individually. |
1167 | 82 |
The reason is that for each individual #add:, the contents has to be |
1175 | 83 |
shifted, to create an empty slot for the new element. |
1167 | 84 |
|
85 |
timing example: |
|
86 |
||
9074 | 87 |
|o rnd| |
1167 | 88 |
|
9074 | 89 |
o := SortedCollection new. |
90 |
rnd := Random new. |
|
91 |
10000 timesRepeat:[ |
|
92 |
o add:rnd next. |
|
93 |
] |
|
1167 | 94 |
|
1175 | 95 |
takes 1365 ms on a P5 (admitted: this is a fast machine ;-) |
22336 | 96 |
Times are a'changing: |
11274
a6f648b1d291
tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents:
11268
diff
changeset
|
97 |
Just came around 2005: on my 1.5Ghz laptop, this now takes 260ms... |
a6f648b1d291
tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents:
11268
diff
changeset
|
98 |
an another comment 2008: on a 1.8Ghz laptop, it takes now 105ms... |
9074 | 99 |
|
1167 | 100 |
In contrast: |
101 |
||
9074 | 102 |
|o rnd| |
1167 | 103 |
|
9074 | 104 |
o := OrderedCollection new. |
105 |
rnd := Random new. |
|
106 |
10000 timesRepeat:[ |
|
107 |
o add:rnd next. |
|
108 |
]. |
|
109 |
o := o asSortedCollection |
|
1167 | 110 |
|
1175 | 111 |
takes 383 ms on the same machine. |
11274
a6f648b1d291
tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents:
11268
diff
changeset
|
112 |
2005: on my 1.5Ghz laptop, this now takes 100ms |
a6f648b1d291
tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents:
11268
diff
changeset
|
113 |
2008: on a 1.8Ghz laptop, this now takes 47ms |
1173
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
114 |
|
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
115 |
If you already have a big sortedCollection at hand, adding multiple |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
116 |
items may be better done with #addAll:, which resorts all elements, if |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
117 |
the number of added items is more than some threshold number. |
3251
87cac58a4e57
added VW compatibility warning
Claus Gittinger <cg@exept.de>
parents:
3250
diff
changeset
|
118 |
However, the break-even point where bulk-adding is faster depends |
87cac58a4e57
added VW compatibility warning
Claus Gittinger <cg@exept.de>
parents:
3250
diff
changeset
|
119 |
on the machine ... (and ST/X version ;-). |
11268
7ce883e9a42e
tuned add: if the elements are added in sorted order
Claus Gittinger <cg@exept.de>
parents:
9920
diff
changeset
|
120 |
|
7ce883e9a42e
tuned add: if the elements are added in sorted order
Claus Gittinger <cg@exept.de>
parents:
9920
diff
changeset
|
121 |
|
11274
a6f648b1d291
tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents:
11268
diff
changeset
|
122 |
adding elements in order: |
a6f648b1d291
tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents:
11268
diff
changeset
|
123 |
|
a6f648b1d291
tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents:
11268
diff
changeset
|
124 |
|c| |
11268
7ce883e9a42e
tuned add: if the elements are added in sorted order
Claus Gittinger <cg@exept.de>
parents:
9920
diff
changeset
|
125 |
Time millisecondsToRun:[ |
7ce883e9a42e
tuned add: if the elements are added in sorted order
Claus Gittinger <cg@exept.de>
parents:
9920
diff
changeset
|
126 |
10 timesRepeat:[ |
7ce883e9a42e
tuned add: if the elements are added in sorted order
Claus Gittinger <cg@exept.de>
parents:
9920
diff
changeset
|
127 |
|c| |
7ce883e9a42e
tuned add: if the elements are added in sorted order
Claus Gittinger <cg@exept.de>
parents:
9920
diff
changeset
|
128 |
c := SortedCollection new. |
7ce883e9a42e
tuned add: if the elements are added in sorted order
Claus Gittinger <cg@exept.de>
parents:
9920
diff
changeset
|
129 |
(1 to:100000) do:[:e | c add:e]. |
7ce883e9a42e
tuned add: if the elements are added in sorted order
Claus Gittinger <cg@exept.de>
parents:
9920
diff
changeset
|
130 |
] |
22336 | 131 |
]. |
11274
a6f648b1d291
tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents:
11268
diff
changeset
|
132 |
|
a6f648b1d291
tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents:
11268
diff
changeset
|
133 |
(5.4.1: 2031 2187) |
a6f648b1d291
tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents:
11268
diff
changeset
|
134 |
(5.4.3: 484 516) |
a6f648b1d291
tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents:
11268
diff
changeset
|
135 |
|
a6f648b1d291
tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents:
11268
diff
changeset
|
136 |
|c| |
a6f648b1d291
tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents:
11268
diff
changeset
|
137 |
c := SortedCollection new. |
a6f648b1d291
tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents:
11268
diff
changeset
|
138 |
(1 to:100000) do:[:e | c add:e]. |
a6f648b1d291
tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents:
11268
diff
changeset
|
139 |
self assert:(c asBag = (1 to:100000) asBag). |
a6f648b1d291
tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents:
11268
diff
changeset
|
140 |
|
a6f648b1d291
tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents:
11268
diff
changeset
|
141 |
adding elements in reverse order: |
a6f648b1d291
tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents:
11268
diff
changeset
|
142 |
|
a6f648b1d291
tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents:
11268
diff
changeset
|
143 |
Time millisecondsToRun:[ |
a6f648b1d291
tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents:
11268
diff
changeset
|
144 |
10 timesRepeat:[ |
a6f648b1d291
tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents:
11268
diff
changeset
|
145 |
|c| |
a6f648b1d291
tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents:
11268
diff
changeset
|
146 |
c := SortedCollection new. |
a6f648b1d291
tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents:
11268
diff
changeset
|
147 |
(1 to:100000) reverseDo:[:e | c add:e]. |
a6f648b1d291
tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents:
11268
diff
changeset
|
148 |
] |
22336 | 149 |
]. |
11274
a6f648b1d291
tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents:
11268
diff
changeset
|
150 |
|
a6f648b1d291
tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents:
11268
diff
changeset
|
151 |
(5.4.1: 201969) |
a6f648b1d291
tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents:
11268
diff
changeset
|
152 |
(5.4.3: 1609 1766) |
a6f648b1d291
tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents:
11268
diff
changeset
|
153 |
|
a6f648b1d291
tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents:
11268
diff
changeset
|
154 |
|c| |
a6f648b1d291
tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents:
11268
diff
changeset
|
155 |
c := SortedCollection new. |
a6f648b1d291
tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents:
11268
diff
changeset
|
156 |
(1 to:100000) reverseDo:[:e | c add:e]. |
a6f648b1d291
tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents:
11268
diff
changeset
|
157 |
self assert:(c asBag = (1 to:100000) asBag). |
a6f648b1d291
tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents:
11268
diff
changeset
|
158 |
|
a6f648b1d291
tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents:
11268
diff
changeset
|
159 |
adding elements in random order: |
a6f648b1d291
tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents:
11268
diff
changeset
|
160 |
|
a6f648b1d291
tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents:
11268
diff
changeset
|
161 |
|toAdd| |
a6f648b1d291
tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents:
11268
diff
changeset
|
162 |
|
a6f648b1d291
tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents:
11268
diff
changeset
|
163 |
toAdd := (1 to:100000) asOrderedCollection randomShuffle. |
a6f648b1d291
tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents:
11268
diff
changeset
|
164 |
|
a6f648b1d291
tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents:
11268
diff
changeset
|
165 |
Time millisecondsToRun:[ |
a6f648b1d291
tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents:
11268
diff
changeset
|
166 |
10 timesRepeat:[ |
a6f648b1d291
tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents:
11268
diff
changeset
|
167 |
|c| |
a6f648b1d291
tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents:
11268
diff
changeset
|
168 |
c := SortedCollection new. |
a6f648b1d291
tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents:
11268
diff
changeset
|
169 |
toAdd do:[:e | c add:e]. |
a6f648b1d291
tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents:
11268
diff
changeset
|
170 |
] |
a6f648b1d291
tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents:
11268
diff
changeset
|
171 |
]. |
a6f648b1d291
tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents:
11268
diff
changeset
|
172 |
|
a6f648b1d291
tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents:
11268
diff
changeset
|
173 |
(5.4.1: 108484) |
a6f648b1d291
tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents:
11268
diff
changeset
|
174 |
(5.4.3: 75734) |
a6f648b1d291
tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents:
11268
diff
changeset
|
175 |
|
a6f648b1d291
tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents:
11268
diff
changeset
|
176 |
|c| |
a6f648b1d291
tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents:
11268
diff
changeset
|
177 |
c := SortedCollection new. |
a6f648b1d291
tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents:
11268
diff
changeset
|
178 |
(1 to:100000) asOrderedCollection randomShuffle do:[:e | c add:e]. |
a6f648b1d291
tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents:
11268
diff
changeset
|
179 |
self assert:(c asBag = (1 to:100000) asBag). |
1167 | 180 |
" |
77 | 181 |
! ! |
1 | 182 |
|
183 |
!SortedCollection class methodsFor:'initialization'! |
|
184 |
||
185 |
initialize |
|
8180
7c346c8015f2
Only use #> for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
7554
diff
changeset
|
186 |
"setup the default sortBlock. |
12034 | 187 |
Use #<, since this is the base method in Magnitude." |
1155 | 188 |
|
189 |
"/ only do this once at early startup |
|
913 | 190 |
DefaultSortBlock isNil ifTrue:[ |
12034 | 191 |
DefaultSortBlock := [:a :b | a < b] |
913 | 192 |
] |
1 | 193 |
|
913 | 194 |
" |
195 |
SortedCollection initialize |
|
196 |
" |
|
1155 | 197 |
|
198 |
"Modified: 12.4.1996 / 12:29:27 / cg" |
|
1 | 199 |
! ! |
200 |
||
201 |
!SortedCollection class methodsFor:'instance creation'! |
|
202 |
||
2929 | 203 |
forStrings |
204 |
"this is supposed to return a sortedCollection, which sorts using |
|
205 |
the current locales collating sequence. For now, simply use a |
|
206 |
normal string compare. |
|
207 |
This will change" |
|
208 |
||
209 |
^ self new |
|
210 |
||
211 |
"Modified: 13.9.1997 / 10:18:58 / cg" |
|
212 |
"Created: 13.9.1997 / 10:41:54 / cg" |
|
213 |
! |
|
214 |
||
215 |
forStrings:size |
|
216 |
"this is supposed to return a sortedCollection, which sorts using |
|
217 |
the current locales collating sequence. For now, simply use a |
|
218 |
normal string compare. |
|
219 |
This will change" |
|
220 |
||
221 |
^ self new:size |
|
222 |
||
223 |
"Modified: 13.9.1997 / 10:18:58 / cg" |
|
224 |
! |
|
225 |
||
21733 | 226 |
forStrings:size collatedBy:aCollationPolicy |
227 |
"Create & return a SortedCollection that sorts the receiver's |
|
228 |
elements according to the specified locales collating policy. |
|
229 |
This is currently not really supported - strings are sorted |
|
230 |
without caring for the locale." |
|
231 |
||
232 |
^ self forStrings:size. |
|
233 |
||
234 |
"Created: / 28-04-2017 / 13:41:40 / stefan" |
|
235 |
! |
|
236 |
||
1 | 237 |
new |
2 | 238 |
"return a new sortedCollection, the sorting is done using |
1155 | 239 |
a compare for a < b, in ascending order" |
2 | 240 |
|
1 | 241 |
^ super new setSortBlock:DefaultSortBlock |
1155 | 242 |
|
243 |
"Modified: 12.4.1996 / 12:28:18 / cg" |
|
1 | 244 |
! |
245 |
||
246 |
new:size |
|
2 | 247 |
"return a new sortedCollection with preallocated size. |
1155 | 248 |
The sorting is done using a compare for a < b, in ascending order" |
2 | 249 |
|
1 | 250 |
^ (super new:size) setSortBlock:DefaultSortBlock |
1155 | 251 |
|
252 |
"Modified: 12.4.1996 / 12:28:22 / cg" |
|
1 | 253 |
! |
254 |
||
255 |
sortBlock:aBlock |
|
1155 | 256 |
"return a new sortedCollection, whe the sort order is defined |
8874 | 257 |
by aBlock. |
1155 | 258 |
This must be a two-argument block which returns true if its arg1 has to come before |
259 |
its arg2 in the collection." |
|
2 | 260 |
|
1 | 261 |
^ super new setSortBlock:aBlock |
1155 | 262 |
|
263 |
"default: |
|
264 |
|s| |
|
265 |
||
266 |
s := SortedCollection new. |
|
267 |
s add:15; add:99; add:3; add:-29; add:17; add:-6. |
|
1422 | 268 |
Transcript showCR:s |
1155 | 269 |
" |
270 |
||
271 |
"sorting by absolute values: |
|
272 |
|s| |
|
273 |
||
274 |
s := SortedCollection sortBlock:[:a :b | a abs < b abs]. |
|
275 |
s add:15; add:99; add:3; add:29; add:17; add:-6. |
|
1422 | 276 |
Transcript showCR:s |
1155 | 277 |
" |
278 |
||
279 |
"default again: |
|
280 |
|s| |
|
281 |
||
282 |
s := SortedCollection new. |
|
283 |
s add:'foo'; add:'Bar'; add:'baz'; add:'hello'; add:'world'; add:'Wow'. |
|
1422 | 284 |
Transcript showCR:s |
1155 | 285 |
" |
286 |
||
287 |
"sorting strings caseless: |
|
288 |
|s| |
|
289 |
||
290 |
s := SortedCollection sortBlock:[:a :b | a asLowercase < b asLowercase]. |
|
291 |
s add:'foo'; add:'Bar'; add:'baz'; add:'hello'; add:'world'; add:'Wow'. |
|
1422 | 292 |
Transcript showCR:s |
1155 | 293 |
" |
294 |
||
295 |
"Modified: 12.4.1996 / 12:26:28 / cg" |
|
329 | 296 |
! |
297 |
||
298 |
withAll:aCollection sortBlock:aBlock |
|
299 |
"initialize from aCollection and set the sort-block" |
|
300 |
||
2248 | 301 |
^ (self sortBlock:aBlock) addAll:aCollection; yourself |
329 | 302 |
|
303 |
" |
|
304 |
SortedCollection withAll:#(1 2 3 4 5 6 7 8 9 0) |
|
8874 | 305 |
sortBlock:[:a :b | a > b] |
1155 | 306 |
" |
307 |
||
308 |
"default: |
|
309 |
|s| |
|
310 |
||
311 |
s := SortedCollection withAll:#(15 99 3 29 17 -6). |
|
1422 | 312 |
Transcript showCR:s |
1155 | 313 |
" |
314 |
||
315 |
"sorting by absolute values: |
|
316 |
|s| |
|
317 |
||
318 |
s := SortedCollection withAll:#(15 99 3 29 17 -6) sortBlock:[:a :b | a abs < b abs]. |
|
1422 | 319 |
Transcript showCR:s |
329 | 320 |
" |
1155 | 321 |
|
322 |
"default again: |
|
323 |
|s| |
|
324 |
||
325 |
s := SortedCollection withAll:#('foo' 'Bar' 'baz' 'hello' 'world' 'Wow'). |
|
1422 | 326 |
Transcript showCR:s |
1155 | 327 |
" |
328 |
||
329 |
"sorting strings caseless: |
|
330 |
|s| |
|
331 |
||
332 |
s := SortedCollection withAll:#('foo' 'Bar' 'baz' 'hello' 'world' 'Wow') sortBlock:[:a :b | a asLowercase < b asLowercase]. |
|
1422 | 333 |
Transcript showCR:s |
1155 | 334 |
" |
335 |
||
336 |
"Modified: 12.4.1996 / 12:28:09 / cg" |
|
1 | 337 |
! ! |
338 |
||
7300 | 339 |
!SortedCollection class methodsFor:'Compatibility-Dolphin'! |
6411 | 340 |
|
341 |
sortBlock:aBlock withAll:aCollection |
|
342 |
^ self withAll:aCollection sortBlock:aBlock |
|
343 |
! ! |
|
344 |
||
1 | 345 |
!SortedCollection methodsFor:'adding & removing'! |
346 |
||
607 | 347 |
add:anObject |
11268
7ce883e9a42e
tuned add: if the elements are added in sorted order
Claus Gittinger <cg@exept.de>
parents:
9920
diff
changeset
|
348 |
"add the argument, anObject at the proper place in the receiver. |
7ce883e9a42e
tuned add: if the elements are added in sorted order
Claus Gittinger <cg@exept.de>
parents:
9920
diff
changeset
|
349 |
Returns the argument, anObject (sigh)." |
2 | 350 |
|
11268
7ce883e9a42e
tuned add: if the elements are added in sorted order
Claus Gittinger <cg@exept.de>
parents:
9920
diff
changeset
|
351 |
|index lastElement| |
1 | 352 |
|
11274
a6f648b1d291
tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents:
11268
diff
changeset
|
353 |
"/ |
a6f648b1d291
tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents:
11268
diff
changeset
|
354 |
"/ cg: the original code was simply: |
a6f648b1d291
tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents:
11268
diff
changeset
|
355 |
"/ |
a6f648b1d291
tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents:
11268
diff
changeset
|
356 |
"/ index := self indexForInserting:anObject. |
a6f648b1d291
tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents:
11268
diff
changeset
|
357 |
"/ index := self makeRoomAtIndex:index. |
a6f648b1d291
tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents:
11268
diff
changeset
|
358 |
"/ contentsArray basicAt:index put:anObject. |
a6f648b1d291
tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents:
11268
diff
changeset
|
359 |
"/ |
a6f648b1d291
tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents:
11268
diff
changeset
|
360 |
"/ which was nice and simple to understand. |
a6f648b1d291
tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents:
11268
diff
changeset
|
361 |
"/ However, the code below is 5 times faster, if elements are added in an already ordered fashion, |
a6f648b1d291
tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents:
11268
diff
changeset
|
362 |
"/ which often happens. |
a6f648b1d291
tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents:
11268
diff
changeset
|
363 |
"/ so we allow the code to be a bit more complicated... |
a6f648b1d291
tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents:
11268
diff
changeset
|
364 |
|
11268
7ce883e9a42e
tuned add: if the elements are added in sorted order
Claus Gittinger <cg@exept.de>
parents:
9920
diff
changeset
|
365 |
(lastIndex < firstIndex "i.e. self size == 0" |
7ce883e9a42e
tuned add: if the elements are added in sorted order
Claus Gittinger <cg@exept.de>
parents:
9920
diff
changeset
|
366 |
or:[ |
18508 | 367 |
lastElement := contentsArray basicAt:lastIndex. |
11268
7ce883e9a42e
tuned add: if the elements are added in sorted order
Claus Gittinger <cg@exept.de>
parents:
9920
diff
changeset
|
368 |
(sortBlock value:lastElement value:anObject) |
7ce883e9a42e
tuned add: if the elements are added in sorted order
Claus Gittinger <cg@exept.de>
parents:
9920
diff
changeset
|
369 |
]) ifTrue:[ |
11274
a6f648b1d291
tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents:
11268
diff
changeset
|
370 |
"/ empty or lastElement is smaller then newElement; add at the end |
11268
7ce883e9a42e
tuned add: if the elements are added in sorted order
Claus Gittinger <cg@exept.de>
parents:
9920
diff
changeset
|
371 |
index := lastIndex. |
7ce883e9a42e
tuned add: if the elements are added in sorted order
Claus Gittinger <cg@exept.de>
parents:
9920
diff
changeset
|
372 |
(index == contentsArray size) ifTrue:[ |
7ce883e9a42e
tuned add: if the elements are added in sorted order
Claus Gittinger <cg@exept.de>
parents:
9920
diff
changeset
|
373 |
self makeRoomAtLast. |
7ce883e9a42e
tuned add: if the elements are added in sorted order
Claus Gittinger <cg@exept.de>
parents:
9920
diff
changeset
|
374 |
index := lastIndex. |
7ce883e9a42e
tuned add: if the elements are added in sorted order
Claus Gittinger <cg@exept.de>
parents:
9920
diff
changeset
|
375 |
]. |
7ce883e9a42e
tuned add: if the elements are added in sorted order
Claus Gittinger <cg@exept.de>
parents:
9920
diff
changeset
|
376 |
lastIndex := index := index + 1. |
607 | 377 |
] ifFalse:[ |
11268
7ce883e9a42e
tuned add: if the elements are added in sorted order
Claus Gittinger <cg@exept.de>
parents:
9920
diff
changeset
|
378 |
index := self indexForInserting:anObject. |
11274
a6f648b1d291
tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents:
11268
diff
changeset
|
379 |
(index == firstIndex) ifTrue:[ |
a6f648b1d291
tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents:
11268
diff
changeset
|
380 |
(firstIndex == 1) ifTrue:[ |
a6f648b1d291
tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents:
11268
diff
changeset
|
381 |
self makeRoomAtFront. |
a6f648b1d291
tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents:
11268
diff
changeset
|
382 |
]. |
a6f648b1d291
tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents:
11268
diff
changeset
|
383 |
index := firstIndex := firstIndex - 1. |
a6f648b1d291
tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents:
11268
diff
changeset
|
384 |
] ifFalse:[ |
a6f648b1d291
tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents:
11268
diff
changeset
|
385 |
index := self makeRoomAtIndex:index. |
a6f648b1d291
tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents:
11268
diff
changeset
|
386 |
]. |
607 | 387 |
]. |
3340 | 388 |
contentsArray basicAt:index put:anObject. |
607 | 389 |
^ anObject |
2 | 390 |
|
607 | 391 |
" |
11268
7ce883e9a42e
tuned add: if the elements are added in sorted order
Claus Gittinger <cg@exept.de>
parents:
9920
diff
changeset
|
392 |
#(7 3 9 10 99) asSortedCollection add:5; yourself |
8874 | 393 |
#(7 3 9 10 99) asSortedCollection add:1; yourself |
394 |
#(7 3 9 10 99) asSortedCollection add:1000; yourself |
|
11274
a6f648b1d291
tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents:
11268
diff
changeset
|
395 |
#(7 3 9 10 99) asSortedCollection add:1; yourself |
607 | 396 |
" |
1162
3995101dfa44
changed for new #makeRoomAtIndex:; added comments
Claus Gittinger <cg@exept.de>
parents:
1156
diff
changeset
|
397 |
|
11276 | 398 |
"Modified: / 22-10-2008 / 17:09:39 / cg" |
1 | 399 |
! |
400 |
||
401 |
addAll:aCollection |
|
8874 | 402 |
"add all elements of the argument, aCollection to the receiver. |
5028
1f7e943ce46e
use my sortBlock when merge-sorting
Claus Gittinger <cg@exept.de>
parents:
4596
diff
changeset
|
403 |
Returns the argument, aCollection (sigh)." |
1173
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
404 |
|
20007 | 405 |
|addedCollection| |
1 | 406 |
|
5359
396b77b6f1d2
added #isSortedCollection, #isSortedBy:
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
407 |
(aCollection isSortedCollection |
7554 | 408 |
and:[aCollection sortBlock == sortBlock]) ifTrue:[ |
11286 | 409 |
addedCollection := aCollection. |
5030
655a0f0a60c3
use my sortBlock when merge-sorting
Claus Gittinger <cg@exept.de>
parents:
5029
diff
changeset
|
410 |
] ifFalse:[ |
11286 | 411 |
addedCollection := Array withAll:aCollection. |
14101 | 412 |
addedCollection stableSort:sortBlock. |
5030
655a0f0a60c3
use my sortBlock when merge-sorting
Claus Gittinger <cg@exept.de>
parents:
5029
diff
changeset
|
413 |
|
20007 | 414 |
self size == 0 ifTrue:[ |
11286 | 415 |
"/ special case: I am empty - add them en-bloque. |
416 |
contentsArray := addedCollection. |
|
417 |
firstIndex := 1. |
|
20007 | 418 |
lastIndex := aCollection size. |
11286 | 419 |
^ aCollection |
420 |
]. |
|
1 | 421 |
]. |
1173
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
422 |
|
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
423 |
"/ |
5030
655a0f0a60c3
use my sortBlock when merge-sorting
Claus Gittinger <cg@exept.de>
parents:
5029
diff
changeset
|
424 |
"/ do a mergeSort with the sortedContents |
1173
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
425 |
"/ |
5030
655a0f0a60c3
use my sortBlock when merge-sorting
Claus Gittinger <cg@exept.de>
parents:
5029
diff
changeset
|
426 |
self mergeSorted:addedCollection. |
1173
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
427 |
^ aCollection |
1 | 428 |
|
77 | 429 |
" |
8874 | 430 |
#(7 3 9 10 99) asSortedCollection addAll:#(77 0 1 16 5); yourself |
5030
655a0f0a60c3
use my sortBlock when merge-sorting
Claus Gittinger <cg@exept.de>
parents:
5029
diff
changeset
|
431 |
|
5028
1f7e943ce46e
use my sortBlock when merge-sorting
Claus Gittinger <cg@exept.de>
parents:
4596
diff
changeset
|
432 |
( #(7 3 9 10 99) asSortedCollection:[:a :b | a > b]) |
11286 | 433 |
addAll:#(77 0 1 16 5); yourself |
5030
655a0f0a60c3
use my sortBlock when merge-sorting
Claus Gittinger <cg@exept.de>
parents:
5029
diff
changeset
|
434 |
|
8874 | 435 |
#(7 3 9 10 99) asSortedCollection |
11286 | 436 |
addAll:( #(77 0 1 16 5) asSortedCollection:[:a :b | a > b]); yourself |
5030
655a0f0a60c3
use my sortBlock when merge-sorting
Claus Gittinger <cg@exept.de>
parents:
5029
diff
changeset
|
437 |
|
8874 | 438 |
(#(7 3 9 10 99) asSortedCollection:[:a :b | a > b]) |
11286 | 439 |
addAll:( #(77 0 1 16 5) asSortedCollection:[:a :b | a < b]); yourself |
440 |
" |
|
441 |
||
442 |
" |
|
443 |
|x e| |
|
444 |
||
445 |
e := (1 to:100) asSortedCollection. |
|
446 |
TimeDuration toRun:[ |
|
447 |
10000 timesRepeat:[ |
|
448 |
x := SortedCollection new:100. |
|
449 |
x addAll:e. |
|
450 |
]. |
|
451 |
]. |
|
77 | 452 |
" |
1173
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
453 |
|
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
454 |
"Modified: 13.4.1996 / 12:42:15 / cg" |
1 | 455 |
! |
456 |
||
1173
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
457 |
mergeSorted:aSortedCollection |
11286 | 458 |
"add all elements of the argument, aSortedCollection to the receiver. |
12038 | 459 |
This leads to an error, if the argument is not sorted. |
1173
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
460 |
|
8874 | 461 |
This should be used when a number of elements is to be added. |
1173
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
462 |
Notice, that quicksort degenerates to a veeery slow bubble-like |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
463 |
sort when a collection of already sorted elements is given to it. |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
464 |
Therefore, adding chunks is better done by sorting the chunk and merging |
14310 | 465 |
it in, instead of resorting the receiver. |
466 |
||
467 |
aSortedCollection must be sorted by the same sortBlock as myself!!" |
|
1173
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
468 |
|
8874 | 469 |
|newContentsArray |
1173
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
470 |
contentsArray2 |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
471 |
destIndex "{ Class: SmallInteger }" |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
472 |
n1 "{ Class: SmallInteger }" |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
473 |
n2 "{ Class: SmallInteger }" |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
474 |
srcIndex1 "{ Class: SmallInteger }" |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
475 |
srcIndex2 "{ Class: SmallInteger }" |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
476 |
last1 "{ Class: SmallInteger }" |
8874 | 477 |
last2 "{ Class: SmallInteger }" |
1173
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
478 |
end1Reached end2Reached |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
479 |
el1 el2 | |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
480 |
|
14310 | 481 |
n2 := aSortedCollection size. |
482 |
n2 == 0 ifTrue:[ |
|
483 |
^ self. |
|
484 |
]. |
|
485 |
||
486 |
aSortedCollection isSortedCollection ifTrue:[ |
|
487 |
contentsArray2 := aSortedCollection contentsArray. |
|
488 |
srcIndex2 := aSortedCollection firstIndex. |
|
489 |
] ifFalse:[ |
|
490 |
contentsArray2 := aSortedCollection. |
|
491 |
srcIndex2 := 1. |
|
492 |
]. |
|
493 |
||
1173
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
494 |
n1 := self size. |
5381
914e310a69be
Fix #mergeSorted if one of the collections is empty.
Stefan Vogel <sv@exept.de>
parents:
5359
diff
changeset
|
495 |
n1 == 0 ifTrue:[ |
14310 | 496 |
firstIndex := srcIndex2. |
497 |
lastIndex := firstIndex + n2 - 1. |
|
498 |
contentsArray := contentsArray2 copy. |
|
11286 | 499 |
^ self. |
5381
914e310a69be
Fix #mergeSorted if one of the collections is empty.
Stefan Vogel <sv@exept.de>
parents:
5359
diff
changeset
|
500 |
]. |
914e310a69be
Fix #mergeSorted if one of the collections is empty.
Stefan Vogel <sv@exept.de>
parents:
5359
diff
changeset
|
501 |
|
23247 | 502 |
newContentsArray := self containerClass new:(n1 + n2). |
1173
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
503 |
destIndex := 1. |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
504 |
|
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
505 |
last2 := srcIndex2 + n2 -1. |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
506 |
|
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
507 |
srcIndex1 := firstIndex. |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
508 |
last1 := srcIndex1 + n1 -1. |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
509 |
|
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
510 |
(srcIndex1 <= last1 and:[srcIndex2 <= last2]) ifTrue:[ |
18508 | 511 |
el1 := contentsArray basicAt:srcIndex1. |
512 |
el2 := contentsArray2 basicAt:srcIndex2. |
|
11286 | 513 |
end1Reached := end2Reached := false. |
1173
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
514 |
|
11286 | 515 |
[end1Reached or:[end2Reached]] whileFalse:[ |
516 |
(sortBlock value:el1 value:el2) ifTrue:[ |
|
517 |
"/ el1 to come before el2 |
|
18508 | 518 |
newContentsArray basicAt:destIndex put:el1. destIndex := destIndex + 1. |
11286 | 519 |
srcIndex1 := srcIndex1 + 1. |
520 |
srcIndex1 <= last1 ifTrue:[ |
|
18508 | 521 |
el1 := contentsArray basicAt:srcIndex1. |
11286 | 522 |
] ifFalse:[ |
523 |
end1Reached := true |
|
524 |
] |
|
525 |
] ifFalse:[ |
|
526 |
"/ el2 to come before el1 |
|
18508 | 527 |
newContentsArray basicAt:destIndex put:el2. destIndex := destIndex + 1. |
11286 | 528 |
srcIndex2 := srcIndex2 + 1. |
529 |
srcIndex2 <= last2 ifTrue:[ |
|
18508 | 530 |
el2 := contentsArray2 basicAt:srcIndex2. |
11286 | 531 |
] ifFalse:[ |
532 |
end2Reached := true |
|
533 |
] |
|
534 |
] |
|
535 |
] |
|
1173
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
536 |
]. |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
537 |
|
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
538 |
"/ fill up with remaining elements from source1 |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
539 |
(srcIndex1 <= last1) ifTrue:[ |
11286 | 540 |
newContentsArray |
541 |
replaceFrom:destIndex |
|
542 |
to:(destIndex+(last1-srcIndex1)) |
|
543 |
with:contentsArray |
|
544 |
startingAt:srcIndex1 |
|
1173
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
545 |
]. |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
546 |
|
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
547 |
"/ fill up with remaining elements from source2 |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
548 |
(srcIndex2 <= last2) ifTrue:[ |
11286 | 549 |
newContentsArray |
550 |
replaceFrom:destIndex |
|
551 |
to:(destIndex+(last2-srcIndex2)) |
|
552 |
with:contentsArray2 |
|
553 |
startingAt:srcIndex2 |
|
1173
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
554 |
]. |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
555 |
|
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
556 |
firstIndex := 1. |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
557 |
lastIndex := n1 + n2. |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
558 |
contentsArray := newContentsArray |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
559 |
|
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
560 |
" |
8874 | 561 |
#(7 3 9 10 99) asSortedCollection |
11286 | 562 |
mergeSorted:#(77 0 1 16 5) asSortedCollection |
1173
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
563 |
|
8874 | 564 |
#(7 3 9 10 99) asSortedCollection |
11286 | 565 |
mergeSorted:#(77 0 3 1 98 99 16 5) asSortedCollection |
1173
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
566 |
|
8874 | 567 |
#() asSortedCollection |
11286 | 568 |
mergeSorted:#(77 0 3 1 98 99 16 5) asSortedCollection |
1173
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
569 |
|
8874 | 570 |
#(7 3 9 10 99) asSortedCollection |
11286 | 571 |
mergeSorted:#() asSortedCollection |
1173
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
572 |
" |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
573 |
|
23247 | 574 |
"Modified: / 30-01-1998 / 01:49:42 / cg" |
575 |
"Modified: / 30-07-2018 / 12:09:10 / Claus Gittinger" |
|
607 | 576 |
! ! |
577 |
||
5560 | 578 |
!SortedCollection methodsFor:'blocked'! |
579 |
||
580 |
add:newObject after:oldObject |
|
581 |
"catch this - its not allowed for sortedCollections" |
|
582 |
||
583 |
self shouldNotImplement |
|
584 |
! |
|
585 |
||
586 |
add:newObject before:oldObject |
|
587 |
"catch this - its not allowed for sortedCollections" |
|
588 |
||
589 |
self shouldNotImplement |
|
590 |
! |
|
591 |
||
592 |
addFirst:anObject |
|
593 |
"catch this - its not allowed for sortedCollections" |
|
594 |
||
595 |
self shouldNotImplement |
|
596 |
! |
|
597 |
||
598 |
addLast:anObject |
|
599 |
"catch this - its not allowed for sortedCollections" |
|
600 |
||
601 |
self shouldNotImplement |
|
602 |
! ! |
|
603 |
||
607 | 604 |
!SortedCollection methodsFor:'converting'! |
605 |
||
606 |
asSortedCollection |
|
607 |
"return the receiver as a sorted collection" |
|
608 |
||
609 |
"could be an instance of a subclass..." |
|
610 |
self class == SortedCollection ifTrue:[ |
|
8874 | 611 |
sortBlock == DefaultSortBlock ifTrue:[ |
612 |
^ self |
|
613 |
] |
|
1 | 614 |
]. |
607 | 615 |
^ super asSortedCollection |
5029
ae3312dda1dd
avoid creation of a new sortedCollection in #asSortedCollection:
Claus Gittinger <cg@exept.de>
parents:
5028
diff
changeset
|
616 |
! |
ae3312dda1dd
avoid creation of a new sortedCollection in #asSortedCollection:
Claus Gittinger <cg@exept.de>
parents:
5028
diff
changeset
|
617 |
|
ae3312dda1dd
avoid creation of a new sortedCollection in #asSortedCollection:
Claus Gittinger <cg@exept.de>
parents:
5028
diff
changeset
|
618 |
asSortedCollection:aSortBlock |
ae3312dda1dd
avoid creation of a new sortedCollection in #asSortedCollection:
Claus Gittinger <cg@exept.de>
parents:
5028
diff
changeset
|
619 |
"return the receiver as a sorted collection, using aSortBlock. |
21438 | 620 |
Return the receiver, if it's a sortedCollection and the sortBlock |
5029
ae3312dda1dd
avoid creation of a new sortedCollection in #asSortedCollection:
Claus Gittinger <cg@exept.de>
parents:
5028
diff
changeset
|
621 |
is the same as the argument-sortBlock" |
ae3312dda1dd
avoid creation of a new sortedCollection in #asSortedCollection:
Claus Gittinger <cg@exept.de>
parents:
5028
diff
changeset
|
622 |
|
ae3312dda1dd
avoid creation of a new sortedCollection in #asSortedCollection:
Claus Gittinger <cg@exept.de>
parents:
5028
diff
changeset
|
623 |
"could be an instance of a subclass..." |
ae3312dda1dd
avoid creation of a new sortedCollection in #asSortedCollection:
Claus Gittinger <cg@exept.de>
parents:
5028
diff
changeset
|
624 |
self class == SortedCollection ifTrue:[ |
21438 | 625 |
"/ if the sortBlock is the same, return the receiver |
626 |
aSortBlock == sortBlock ifTrue:[ |
|
627 |
^ self |
|
628 |
]. |
|
5029
ae3312dda1dd
avoid creation of a new sortedCollection in #asSortedCollection:
Claus Gittinger <cg@exept.de>
parents:
5028
diff
changeset
|
629 |
]. |
5030
655a0f0a60c3
use my sortBlock when merge-sorting
Claus Gittinger <cg@exept.de>
parents:
5029
diff
changeset
|
630 |
^ super asSortedCollection:aSortBlock |
21438 | 631 |
|
632 |
"Modified (comment): / 13-02-2017 / 20:31:12 / cg" |
|
1 | 633 |
! ! |
634 |
||
95 | 635 |
!SortedCollection methodsFor:'copying'! |
636 |
||
607 | 637 |
copyEmpty:size |
638 |
"return a copy of the receiver with no elements, but |
|
639 |
the same size. This method has been be redefined to |
|
640 |
preserve the sortBlock." |
|
641 |
||
14110 | 642 |
^ (super copyEmpty:size) sortBlock:sortBlock |
607 | 643 |
! |
644 |
||
159 | 645 |
postCopyFrom:anOriginal |
646 |
"sent after a copy or when a new collection species has been created. |
|
95 | 647 |
The new collection should have the same sortBlock as the original." |
648 |
||
159 | 649 |
sortBlock := anOriginal sortBlock |
95 | 650 |
|
651 |
" |
|
652 |
#(4 7 1 99 -1 17) asSortedCollection inspect |
|
159 | 653 |
#(4 7 1 99 -1 17) asSortedCollection copy inspect |
95 | 654 |
(#(4 7 1 99 -1 17) asSortedCollection sortBlock:[:a :b | a > b]) inspect |
159 | 655 |
(#(4 7 1 99 -1 17) asSortedCollection sortBlock:[:a :b | a > b]) copy inspect |
656 |
(#(4 7 1 99 -1 17) asSortedCollection select:[:e| e even]) inspect |
|
95 | 657 |
" |
658 |
! ! |
|
659 |
||
216 | 660 |
!SortedCollection methodsFor:'enumerating'! |
184 | 661 |
|
662 |
collect:aBlock |
|
663 |
"evaluate the argument, aBlock for every element in the collection |
|
664 |
and return a collection of the results. Redefined to return an OrderedCollection; |
|
665 |
see X3J20 spec. (SortedCollection>>collect: should return an OrderedCollection)" |
|
666 |
||
667 |
|newCollection |
|
668 |
start "{ Class:SmallInteger }" |
|
669 |
stop "{ Class:SmallInteger }" | |
|
670 |
||
18508 | 671 |
newCollection := self speciesForCollecting new:(self size). |
184 | 672 |
stop := lastIndex. |
673 |
start := firstIndex. |
|
674 |
start to:stop do:[:index | |
|
18508 | 675 |
newCollection add:(aBlock value:(contentsArray basicAt:index)). |
184 | 676 |
]. |
677 |
^ newCollection |
|
678 |
! ! |
|
679 |
||
607 | 680 |
!SortedCollection methodsFor:'instance protocol'! |
681 |
||
5668
0cbaa16dc17c
Inherited #sort: shows a debugger.
Stefan Vogel <sv@exept.de>
parents:
5604
diff
changeset
|
682 |
sort:aSortBlock |
0cbaa16dc17c
Inherited #sort: shows a debugger.
Stefan Vogel <sv@exept.de>
parents:
5604
diff
changeset
|
683 |
"redefined from superclass to change the sortBlock" |
0cbaa16dc17c
Inherited #sort: shows a debugger.
Stefan Vogel <sv@exept.de>
parents:
5604
diff
changeset
|
684 |
|
0cbaa16dc17c
Inherited #sort: shows a debugger.
Stefan Vogel <sv@exept.de>
parents:
5604
diff
changeset
|
685 |
sortBlock ~~ aSortBlock ifTrue:[ |
8874 | 686 |
self sortBlock:aSortBlock. |
5668
0cbaa16dc17c
Inherited #sort: shows a debugger.
Stefan Vogel <sv@exept.de>
parents:
5604
diff
changeset
|
687 |
]. |
0cbaa16dc17c
Inherited #sort: shows a debugger.
Stefan Vogel <sv@exept.de>
parents:
5604
diff
changeset
|
688 |
|
0cbaa16dc17c
Inherited #sort: shows a debugger.
Stefan Vogel <sv@exept.de>
parents:
5604
diff
changeset
|
689 |
! |
0cbaa16dc17c
Inherited #sort: shows a debugger.
Stefan Vogel <sv@exept.de>
parents:
5604
diff
changeset
|
690 |
|
607 | 691 |
sortBlock |
692 |
"return the block used for sorting" |
|
693 |
||
694 |
^ sortBlock |
|
695 |
! |
|
696 |
||
697 |
sortBlock:aSortBlock |
|
8874 | 698 |
"change the sort criteria for a sorted collection, resort the elements of |
607 | 699 |
the collection, and return the receiver. The argument, aSortBlock must |
700 |
be a two-argument block which returns true if its arg1 has to come before |
|
701 |
its arg2 in the collection." |
|
702 |
||
703 |
sortBlock := aSortBlock. |
|
704 |
lastIndex > firstIndex ifTrue:[ |
|
705 |
contentsArray quickSortFrom:firstIndex to:lastIndex sortBlock:aSortBlock |
|
706 |
] |
|
707 |
||
708 |
" |
|
709 |
#(9 8 7 6 5 4 3) asSortedCollection |
|
710 |
#(9 8 7 6 5 4 3) asSortedCollection sortBlock:[:a : b | a < b] |
|
711 |
#(9 8 7 6 5 4 3) asSortedCollection sortBlock:[:a : b | a > b] |
|
712 |
#($f $G $z $Y $o $H) asSortedCollection |
|
713 |
#($f $G $z $Y $o $H) asSortedCollection sortBlock:[:a : b | a asUppercase < b asUppercase] |
|
714 |
" |
|
715 |
! ! |
|
716 |
||
717 |
!SortedCollection methodsFor:'private'! |
|
718 |
||
18508 | 719 |
indexForInserting:anObject |
720 |
"search the index at which to insert anObject. |
|
721 |
Can also be used to search for an existing element |
|
722 |
by checking if the element at the returned index is the one we look for. |
|
723 |
Uses a binarySearch since we can depend on the elements being in sorted order. |
|
724 |
The returned index is a physical one, for accessing contentsArray." |
|
725 |
||
726 |
|low "{ Class: SmallInteger}" |
|
727 |
high "{ Class: SmallInteger}" |
|
728 |
middle "{ Class: SmallInteger}" |
|
729 |
element| |
|
730 |
||
731 |
" |
|
732 |
we can of course use a binary search - since the elements are sorted |
|
733 |
" |
|
734 |
low := firstIndex. |
|
735 |
high := lastIndex. |
|
736 |
[low > high] whileFalse:[ |
|
737 |
middle := (low + high) // 2. |
|
738 |
element := contentsArray basicAt:middle. |
|
739 |
(sortBlock value:element value:anObject) ifTrue:[ |
|
740 |
"middleelement is smaller than object" |
|
741 |
low := middle + 1 |
|
742 |
] ifFalse:[ |
|
743 |
high := middle - 1 |
|
744 |
] |
|
745 |
]. |
|
746 |
^ low |
|
747 |
||
748 |
" |
|
749 |
#(1 2 3 4 7 99 1313 981989 898989898) asSortedCollection indexForInserting:50 |
|
750 |
||
751 |
#(1.0 2.0 3 4 7 49.0 51.0 99 1313 981989 898989898) asSortedCollection indexForInserting:50 |
|
752 |
||
753 |
" |
|
754 |
||
755 |
"Modified: 12.4.1996 / 13:22:03 / cg" |
|
756 |
! |
|
757 |
||
607 | 758 |
setSortBlock: aSortBlock |
759 |
"set the sortblock without resorting - private only" |
|
760 |
||
761 |
sortBlock := aSortBlock |
|
762 |
! ! |
|
763 |
||
1173
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
764 |
!SortedCollection methodsFor:'queries'! |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
765 |
|
23703 | 766 |
includes:anObject |
767 |
"return true, if the argument, anObject is in the collection. |
|
768 |
Redefined, since due to being sorted, the inclusion check can |
|
769 |
be done with log-n compares i.e. much faster." |
|
770 |
||
771 |
|index "{ Class: SmallInteger }" |
|
772 |
initialIndex "{ Class: SmallInteger }" |
|
773 |
element| |
|
774 |
||
775 |
"/ if I am small, the inherited linear search is faster ... |
|
776 |
(lastIndex - firstIndex) < 20 ifTrue:[ |
|
777 |
firstIndex > lastIndex ifTrue:[ |
|
778 |
"/ empty |
|
779 |
^ false |
|
780 |
]. |
|
781 |
^ super includes:anObject. |
|
782 |
]. |
|
783 |
||
784 |
initialIndex := self indexForInserting:anObject. |
|
785 |
((initialIndex < firstIndex) or:[initialIndex > lastIndex]) ifTrue:[^ false]. |
|
786 |
(contentsArray basicAt:initialIndex) = anObject ifTrue:[ |
|
787 |
"the simple case - plain objects" |
|
788 |
^ true. |
|
789 |
]. |
|
790 |
||
791 |
"the complex case: the collection may include elements, which are odered only by |
|
792 |
a single component (e.g. Associations by key). So we have to test all |
|
793 |
previous and next elements having the same component" |
|
794 |
||
795 |
"for previous elements: while element is not smaller and not larger than anObject ... compare" |
|
796 |
index := initialIndex - 1. |
|
797 |
[index >= firstIndex |
|
798 |
and:[ |
|
799 |
element := contentsArray basicAt:index. |
|
800 |
((sortBlock value:element value:anObject) or:[sortBlock value:anObject value:element]) not] |
|
801 |
] whileTrue:[ |
|
802 |
element = anObject ifTrue:[ |
|
803 |
^ true. |
|
804 |
]. |
|
805 |
index := index - 1. |
|
806 |
]. |
|
807 |
||
808 |
"for next elements: while element is not smaller and not larger than anObject ... compare" |
|
809 |
index := initialIndex + 1. |
|
810 |
[index <= lastIndex |
|
811 |
and:[ |
|
812 |
element := contentsArray basicAt:index. |
|
813 |
((sortBlock value:element value:anObject) or:[sortBlock value:anObject value:element]) not] |
|
814 |
] whileTrue:[ |
|
815 |
element = anObject ifTrue:[ |
|
816 |
^ true. |
|
817 |
]. |
|
818 |
index := index + 1. |
|
819 |
]. |
|
820 |
||
821 |
^ false. |
|
822 |
||
823 |
" |
|
824 |
#(7 3 9 10 99) asSortedCollection includes:50 |
|
825 |
#(7 3 9 10 99) asSortedCollection includes:10 |
|
826 |
" |
|
827 |
! |
|
828 |
||
1173
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
829 |
isSorted |
5359
396b77b6f1d2
added #isSortedCollection, #isSortedBy:
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
830 |
"return true. if my elements are sorted" |
396b77b6f1d2
added #isSortedCollection, #isSortedBy:
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
831 |
|
1173
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
832 |
^ true |
5359
396b77b6f1d2
added #isSortedCollection, #isSortedBy:
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
833 |
! |
396b77b6f1d2
added #isSortedCollection, #isSortedBy:
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
834 |
|
396b77b6f1d2
added #isSortedCollection, #isSortedBy:
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
835 |
isSortedBy:aBlock |
396b77b6f1d2
added #isSortedCollection, #isSortedBy:
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
836 |
"return true, if my elements are sorted (already) by the given criterion (sortBlock)." |
1173
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
837 |
|
5359
396b77b6f1d2
added #isSortedCollection, #isSortedBy:
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
838 |
aBlock == sortBlock ifTrue:[^ true]. |
396b77b6f1d2
added #isSortedCollection, #isSortedBy:
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
839 |
^ super isSortedBy:aBlock |
396b77b6f1d2
added #isSortedCollection, #isSortedBy:
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
840 |
|
396b77b6f1d2
added #isSortedCollection, #isSortedBy:
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
841 |
|
396b77b6f1d2
added #isSortedCollection, #isSortedBy:
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
842 |
! |
396b77b6f1d2
added #isSortedCollection, #isSortedBy:
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
843 |
|
396b77b6f1d2
added #isSortedCollection, #isSortedBy:
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
844 |
isSortedCollection |
396b77b6f1d2
added #isSortedCollection, #isSortedBy:
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
845 |
"return true. if I am a sorted collection" |
396b77b6f1d2
added #isSortedCollection, #isSortedBy:
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
846 |
|
396b77b6f1d2
added #isSortedCollection, #isSortedBy:
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
847 |
^ true |
18508 | 848 |
! |
849 |
||
23703 | 850 |
occurrencesOf:anObject |
851 |
"return how many times the argument, anObject is in the collection. |
|
852 |
Redefined, since due to being sorted, the range of checked objects |
|
853 |
can be limited i.e. it can be done much faster. |
|
854 |
Uses #= (i.e. equality) compare." |
|
855 |
||
856 |
|index "{ Class: SmallInteger }" |
|
857 |
tally "{ Class: SmallInteger }" |
|
858 |
last "{ Class: SmallInteger }" | |
|
859 |
||
860 |
index := self indexOf:anObject. |
|
861 |
index == 0 ifTrue:[ |
|
862 |
^ 0 |
|
863 |
]. |
|
864 |
||
865 |
index := index + firstIndex - 1. |
|
866 |
last := lastIndex. |
|
867 |
||
868 |
"/ there may be multiple of them; count 'em |
|
869 |
||
870 |
tally := 0. |
|
871 |
[(index <= last) and:[(contentsArray basicAt:index) = anObject]] whileTrue:[ |
|
872 |
tally := tally + 1. |
|
873 |
index := index + 1 |
|
874 |
]. |
|
875 |
^ tally |
|
876 |
||
877 |
" |
|
878 |
#(7 3 9 10 99) asSortedCollection occurrencesOf:50 |
|
879 |
#(7 3 9 10 99) asSortedCollection occurrencesOf:10 |
|
880 |
#(7 10 3 10 9 10 10 10 10 10 10 10 10 99) asSortedCollection occurrencesOf:10 |
|
881 |
" |
|
882 |
||
883 |
"Modified: 12.4.1996 / 18:48:40 / cg" |
|
884 |
! |
|
885 |
||
18508 | 886 |
speciesForCollecting |
887 |
"Redefined to return an OrderedCollection; |
|
888 |
see X3J20 spec. (SortedCollection>>collect: should return an OrderedCollection)" |
|
889 |
||
890 |
^ OrderedCollection |
|
1173
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
891 |
! ! |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
892 |
|
607 | 893 |
!SortedCollection methodsFor:'searching'! |
894 |
||
895 |
after:anObject ifAbsent:exceptionBlock |
|
896 |
"return the element after the argument, anObject; or nil if there is none. |
|
897 |
If anObject is contained multiple times in the collection, return the |
|
898 |
the first element which is non-equal to anObject. |
|
899 |
If the receiver does not contain anObject, return the result from evaluating |
|
900 |
exceptionBlock." |
|
901 |
||
1162
3995101dfa44
changed for new #makeRoomAtIndex:; added comments
Claus Gittinger <cg@exept.de>
parents:
1156
diff
changeset
|
902 |
|index "{ Class: SmallInteger }" |
3995101dfa44
changed for new #makeRoomAtIndex:; added comments
Claus Gittinger <cg@exept.de>
parents:
1156
diff
changeset
|
903 |
last "{ Class: SmallInteger }"| |
607 | 904 |
|
18508 | 905 |
index := self indexOf:anObject. |
906 |
index == 0 ifTrue:[ |
|
907 |
^ exceptionBlock value. |
|
908 |
]. |
|
607 | 909 |
|
8874 | 910 |
"skip multiple occurrences of the same ..." |
18508 | 911 |
index := index + firstIndex - 1. |
1162
3995101dfa44
changed for new #makeRoomAtIndex:; added comments
Claus Gittinger <cg@exept.de>
parents:
1156
diff
changeset
|
912 |
last := lastIndex. |
18508 | 913 |
[(index <= last) and:[(contentsArray basicAt:index) = anObject]] whileTrue:[ |
914 |
index := index + 1 |
|
607 | 915 |
]. |
18508 | 916 |
(index > last) ifTrue:[^ exceptionBlock value]. |
917 |
^ contentsArray basicAt:index |
|
1162
3995101dfa44
changed for new #makeRoomAtIndex:; added comments
Claus Gittinger <cg@exept.de>
parents:
1156
diff
changeset
|
918 |
|
18508 | 919 |
" |
920 |
#(7 3 9 10 99) asSortedCollection after:50 |
|
921 |
#(7 3 9 10 99) asSortedCollection after:3 |
|
922 |
#(7 3 9 10 99) asSortedCollection after:1 |
|
923 |
#(7 3 9 10 99) asSortedCollection after:10 |
|
924 |
#(7 3 9 10 99) asSortedCollection after:7 |
|
925 |
#(7 3 9 10 99) asSortedCollection after:99 |
|
926 |
#(7 10 3 10 9 10 10 99) asSortedCollection after:9 |
|
927 |
#(7 10 3 10 9 10 10 99) asSortedCollection after:10 |
|
928 |
" |
|
607 | 929 |
! |
930 |
||
931 |
before:anObject ifAbsent:exceptionBlock |
|
932 |
"return the element before the argument, anObject; or nil if there is none. |
|
933 |
If the receiver does not contain anObject, return the result from evaluating |
|
934 |
exceptionBlock." |
|
935 |
||
936 |
|index "{ Class: SmallInteger }"| |
|
937 |
||
18508 | 938 |
index := self indexOf:anObject. |
939 |
index <= 1 ifTrue:[ |
|
940 |
^ exceptionBlock value. |
|
941 |
]. |
|
942 |
^ contentsArray basicAt:firstIndex + index - 2 |
|
607 | 943 |
|
944 |
" |
|
945 |
#(7 3 9 10 99) asSortedCollection before:50 |
|
18508 | 946 |
#(7 3 9 10 99) asSortedCollection before:3 |
8874 | 947 |
#(7 3 9 10 99) asSortedCollection before:1 |
948 |
#(7 3 9 10 99) asSortedCollection before:10 |
|
949 |
#(7 3 9 10 99) asSortedCollection before:7 |
|
950 |
#(7 3 9 10 99) asSortedCollection before:99 |
|
951 |
#(7 10 3 10 9 10 10 99) asSortedCollection before:9 |
|
952 |
#(7 10 3 10 9 10 10 99) asSortedCollection before:10 |
|
607 | 953 |
" |
4596 | 954 |
! |
955 |
||
18508 | 956 |
indexOf:anObject |
22430 | 957 |
"return the index of the anObject or 0 if anObject is not in the collection. |
958 |
Redefined, since due to being sorted, |
|
959 |
this can be done with log-n compares i.e. much faster." |
|
18508 | 960 |
|
961 |
|index "{ Class: SmallInteger }" |
|
962 |
initialIndex "{ Class: SmallInteger }" |
|
963 |
element| |
|
964 |
||
965 |
"/ if I am small, the inherited linear search is faster ... |
|
966 |
(lastIndex - firstIndex) < 20 ifTrue:[ |
|
967 |
firstIndex > lastIndex ifTrue:[ |
|
968 |
"/ empty |
|
969 |
^ 0 |
|
970 |
]. |
|
971 |
^ super indexOf:anObject. |
|
972 |
]. |
|
973 |
||
974 |
initialIndex := self indexForInserting:anObject. |
|
975 |
initialIndex > lastIndex ifTrue:[ |
|
976 |
initialIndex := lastIndex |
|
977 |
] ifFalse:[ |
|
978 |
initialIndex < firstIndex ifTrue:[ |
|
979 |
initialIndex := firstIndex |
|
980 |
] |
|
981 |
]. |
|
14310 | 982 |
|
19129 | 983 |
"the complex case: the collection may include elements, which are ordered only by |
18508 | 984 |
a single component (e.g. Associations by key). So we have to test all |
985 |
previous and next elements having the same component" |
|
986 |
||
987 |
"for previous elements: while element is not smaller and not larger than anObject ... compare" |
|
988 |
index := initialIndex. |
|
989 |
[index >= firstIndex |
|
990 |
and:[ |
|
991 |
element := contentsArray basicAt:index. |
|
992 |
((sortBlock value:element value:anObject) or:[sortBlock value:anObject value:element]) not] |
|
993 |
] whileTrue:[ |
|
994 |
element = anObject ifTrue:[ |
|
995 |
^ index - firstIndex + 1. |
|
996 |
]. |
|
997 |
index := index - 1. |
|
998 |
]. |
|
999 |
||
1000 |
"for next elements: while element is not smaller and not larger than anObject ... compare" |
|
1001 |
index := initialIndex. |
|
1002 |
[index <= lastIndex |
|
1003 |
and:[ |
|
1004 |
element := contentsArray basicAt:index. |
|
1005 |
((sortBlock value:element value:anObject) or:[sortBlock value:anObject value:element]) not] |
|
1006 |
] whileTrue:[ |
|
1007 |
element = anObject ifTrue:[ |
|
1008 |
^ index - firstIndex + 1. |
|
1009 |
]. |
|
1010 |
index := index + 1. |
|
1011 |
]. |
|
1012 |
||
1013 |
^ 0. |
|
14310 | 1014 |
|
1015 |
" |
|
18508 | 1016 |
#(7 3 9 10 99) asSortedCollection indexOf:50 |
1017 |
#(7 3 9 10 99) asSortedCollection indexOf:10 |
|
1018 |
||
1019 |
#('aa' 'bb' 'cc' 'dd') asSortedCollection indexOf:'bb' |
|
1020 |
#('aa' 'bb' 'cc' 'dd' 'aa' 'bb' 'cc' 'dd') asSortedCollection indexOf:'bb' |
|
14310 | 1021 |
|
18508 | 1022 |
|allSyms indices| |
1023 |
allSyms := Symbol allInstances asSortedCollection. |
|
1024 |
Time millisecondsToRun:[ |
|
1025 |
indices := allSyms collect:[:el | allSyms indexOf:el]. |
|
1026 |
]. |
|
1027 |
indices = (1 to:allSyms size) |
|
14310 | 1028 |
" |
4596 | 1029 |
! |
1030 |
||
1031 |
indexOf:anElement ifAbsent:exceptionBlock |
|
8874 | 1032 |
"Return the index of anElement within the receiver. |
1033 |
If the receiver does not contain anElement, |
|
4596 | 1034 |
return the result of evaluating the argument, exceptionBlock." |
1035 |
||
18508 | 1036 |
|idx| |
4596 | 1037 |
|
18508 | 1038 |
idx := self indexOf:anElement. |
1039 |
idx == 0 ifTrue:[^ exceptionBlock value]. |
|
1040 |
^ idx. |
|
16002 | 1041 |
! |
1042 |
||
1043 |
largest:n |
|
1044 |
"return a collection containing the n largest elements, largest last. |
|
1045 |
Raises an exception, if the receiver does not contain at least n elements" |
|
1046 |
||
1047 |
|mySize| |
|
1048 |
||
1049 |
mySize := self size. |
|
1050 |
n > mySize ifTrue:[ |
|
1051 |
self notEnoughElementsError |
|
1052 |
]. |
|
1053 |
sortBlock == DefaultSortBlock ifTrue:[ |
|
1054 |
^ self copyFrom:(mySize-n+1) |
|
1055 |
]. |
|
1056 |
"/ do not trust the sortblock to sort small-to-large |
|
1057 |
^ super largest:n |
|
1058 |
||
1059 |
" |
|
1060 |
#(10 35 20 45 30 5) asSortedCollection largest:3 |
|
1061 |
(#(10 35 20 45 30 5) asSortedCollection:[:a :b | a > b]) largest:3 |
|
1062 |
||
1063 |
#(10 35 20 45 30 5) asSortedCollection largest:6 |
|
1064 |
#(10 35 20 45 30 5) asSortedCollection largest:7 |
|
1065 |
" |
|
1066 |
! |
|
1067 |
||
1068 |
max |
|
19074 | 1069 |
"return the maximum value in the receiver collection, |
1070 |
redefined, since this can be easily computed. |
|
1071 |
Raises an error, if the receiver is empty." |
|
16002 | 1072 |
|
1073 |
sortBlock == DefaultSortBlock ifTrue:[ |
|
1074 |
^ self last |
|
1075 |
]. |
|
1076 |
^ super max |
|
1077 |
||
1078 |
" |
|
1079 |
#(10 35 20 45 30 5) asSortedCollection max |
|
19075 | 1080 |
#(10 35 20 45 30 5) max |
19074 | 1081 |
#() asSortedCollection max |
16002 | 1082 |
" |
1083 |
! |
|
1084 |
||
1085 |
min |
|
19074 | 1086 |
"return the minimum value in the receiver collection, |
1087 |
redefined, since this can be easily computed. |
|
1088 |
Raises an error, if the receiver is empty." |
|
16002 | 1089 |
|
1090 |
sortBlock == DefaultSortBlock ifTrue:[ |
|
1091 |
^ self first |
|
1092 |
]. |
|
1093 |
^ super min |
|
1094 |
||
1095 |
" |
|
1096 |
#(10 35 20 45 30 5) asSortedCollection min |
|
19075 | 1097 |
#(10 35 20 45 30 5) min |
1098 |
" |
|
1099 |
! |
|
1100 |
||
1101 |
minMax |
|
1102 |
"return the minimum and maximum values in the receiver collection |
|
1103 |
as a two element array, using #< to compare elements. |
|
1104 |
Raises an error, if the receiver is empty." |
|
1105 |
||
1106 |
"return the minimum value in the receiver collection, |
|
1107 |
redefined, since this can be easily computed. |
|
1108 |
Raises an error, if the receiver is empty." |
|
1109 |
||
1110 |
sortBlock == DefaultSortBlock ifTrue:[ |
|
1111 |
^ { self first . self last } |
|
1112 |
]. |
|
1113 |
^ super minMax |
|
1114 |
||
1115 |
" |
|
1116 |
#(10 35 20 45 30 5) asSortedCollection minMax |
|
1117 |
#(10 35 20 45 30 5) minMax |
|
16002 | 1118 |
" |
1119 |
! |
|
1120 |
||
1121 |
smallest:n |
|
1122 |
"return a collection containing the n smallest elements, largest last. |
|
1123 |
Raises an exception, if the receiver does not contain at least n elements" |
|
1124 |
||
1125 |
|mySize| |
|
1126 |
||
1127 |
mySize := self size. |
|
1128 |
n > mySize ifTrue:[ |
|
1129 |
self notEnoughElementsError |
|
1130 |
]. |
|
1131 |
sortBlock == DefaultSortBlock ifTrue:[ |
|
1132 |
^ self copyTo:n |
|
1133 |
]. |
|
1134 |
"/ do not trust the sortblock to sort small-to-large |
|
1135 |
^ super smallest:n |
|
1136 |
||
1137 |
" |
|
1138 |
#(10 35 20 45 30 5) asSortedCollection smallest:3 |
|
1139 |
(#(10 35 20 45 30 5) asSortedCollection:[:a :b | a > b]) smallest:3 |
|
1140 |
||
1141 |
#(10 35 20 45 30 5) asSortedCollection smallest:6 |
|
1142 |
#(10 35 20 45 30 5) asSortedCollection smallest:7 |
|
1143 |
" |
|
1144 |
! ! |
|
1145 |
||
1146 |
!SortedCollection methodsFor:'statistical functions'! |
|
1147 |
||
1148 |
median |
|
1149 |
"Return the middle element, or as close as we can get." |
|
1150 |
||
18508 | 1151 |
^ self basicAt:(self size + 1 // 2) |
16002 | 1152 |
|
1153 |
" |
|
1154 |
#(10 35 20 45 30 5) asSortedCollection median |
|
1155 |
#(10 35 20 45 30 5) median |
|
1156 |
" |
|
607 | 1157 |
! ! |
1158 |
||
629 | 1159 |
!SortedCollection class methodsFor:'documentation'! |
1160 |
||
1161 |
version |
|
18508 | 1162 |
^ '$Header$' |
14309
602e61cb64a3
Remove now unused helper method
Stefan Vogel <sv@exept.de>
parents:
14308
diff
changeset
|
1163 |
! |
602e61cb64a3
Remove now unused helper method
Stefan Vogel <sv@exept.de>
parents:
14308
diff
changeset
|
1164 |
|
602e61cb64a3
Remove now unused helper method
Stefan Vogel <sv@exept.de>
parents:
14308
diff
changeset
|
1165 |
version_CVS |
18508 | 1166 |
^ '$Header$' |
629 | 1167 |
! ! |
7300 | 1168 |
|
16000 | 1169 |
|
607 | 1170 |
SortedCollection initialize! |