author | Claus Gittinger <cg@exept.de> |
Thu, 28 Apr 2005 19:29:30 +0200 | |
changeset 8874 | f1fdda306d51 |
parent 8183 | 93f8b0cc734f |
child 9073 | cb81da1b2ad5 |
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 |
" |
|
12 |
||
5359
396b77b6f1d2
added #isSortedCollection, #isSortedBy:
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
13 |
"{ Package: 'stx:libbasic' }" |
396b77b6f1d2
added #isSortedCollection, #isSortedBy:
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
14 |
|
1 | 15 |
OrderedCollection subclass:#SortedCollection |
1155 | 16 |
instanceVariableNames:'sortBlock' |
17 |
classVariableNames:'DefaultSortBlock' |
|
18 |
poolDictionaries:'' |
|
19 |
category:'Collections-Sequenceable' |
|
1 | 20 |
! |
21 |
||
77 | 22 |
!SortedCollection class methodsFor:'documentation'! |
1 | 23 |
|
88 | 24 |
copyright |
25 |
" |
|
26 |
COPYRIGHT (c) 1993 by Claus Gittinger |
|
159 | 27 |
All Rights Reserved |
88 | 28 |
|
29 |
This software is furnished under a license and may be used |
|
30 |
only in accordance with the terms of that license and with the |
|
31 |
inclusion of the above copyright notice. This software may not |
|
32 |
be provided or otherwise made available to, or used by, any |
|
33 |
other person. No title to or ownership of the software is |
|
34 |
hereby transferred. |
|
35 |
" |
|
36 |
! |
|
37 |
||
77 | 38 |
documentation |
39 |
" |
|
40 |
I keep my elements sorted. The sort order is defined by a sortblock, |
|
8874 | 41 |
a two-argument block which, when given two elements of the collection, |
42 |
should return true if the element given as first arg has to come before the |
|
77 | 43 |
element given as second arg. |
44 |
||
1156 | 45 |
Equal elements may occur multiple times. |
46 |
||
47 |
SortedCollection uses quickSort to resort and a binary search when adding/removing |
|
8874 | 48 |
elements. |
1156 | 49 |
Because insertion/removal may require that remaining elements have to |
50 |
be shifted within the container, adding many individual elements may be done faster |
|
51 |
by creating a completely new collection from the unsorted elements. |
|
1167 | 52 |
(see examples) |
1156 | 53 |
|
77 | 54 |
Thus a sortBlock of [:a :b | a < b] defines ascending sort-order, |
55 |
while [:a :b | a > b] defines descening order. |
|
56 |
The default sortBlock for SortedCollections is the first one. |
|
1290 | 57 |
|
3251
87cac58a4e57
added VW compatibility warning
Claus Gittinger <cg@exept.de>
parents:
3250
diff
changeset
|
58 |
Compatibility Warning: |
8874 | 59 |
VW seems to use a default sortBlock which compares a<=b, |
60 |
while ST/X uses a<b. |
|
61 |
That means, that elements which are compared MUST understand #< in ST/X |
|
62 |
while the minumum protocol is #<= in VW. |
|
63 |
This may be changed in a future release of ST/X |
|
64 |
(it is not yet, to not confuse existing applications ... |
|
65 |
... be aware, that the sortBlock has an effect on a few algorithms |
|
66 |
found here; especially #indexForInserting is critical.) |
|
67 |
||
3251
87cac58a4e57
added VW compatibility warning
Claus Gittinger <cg@exept.de>
parents:
3250
diff
changeset
|
68 |
|
1290 | 69 |
[author:] |
8874 | 70 |
Claus Gittinger |
77 | 71 |
" |
1167 | 72 |
! |
73 |
||
74 |
examples |
|
75 |
" |
|
8874 | 76 |
when many elements are to be added, it may be better to add them |
1175 | 77 |
all en-bloeque instead of individually. |
1167 | 78 |
The reason is that for each individual #add:, the contents has to be |
1175 | 79 |
shifted, to create an empty slot for the new element. |
1167 | 80 |
|
81 |
timing example: |
|
82 |
||
8874 | 83 |
|o rnd| |
1167 | 84 |
|
8874 | 85 |
o := SortedCollection new. |
86 |
rnd := Random new. |
|
87 |
10000 timesRepeat:[ |
|
88 |
o add:rnd next. |
|
89 |
] |
|
1167 | 90 |
|
1175 | 91 |
takes 1365 ms on a P5 (admitted: this is a fast machine ;-) |
1167 | 92 |
In contrast: |
93 |
||
8874 | 94 |
|o rnd| |
1167 | 95 |
|
8874 | 96 |
o := OrderedCollection new. |
97 |
rnd := Random new. |
|
98 |
10000 timesRepeat:[ |
|
99 |
o add:rnd next. |
|
100 |
]. |
|
101 |
o := o asSortedCollection |
|
1167 | 102 |
|
1175 | 103 |
takes 383 ms on the same machine. |
1173
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
104 |
|
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
105 |
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
|
106 |
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
|
107 |
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
|
108 |
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
|
109 |
on the machine ... (and ST/X version ;-). |
1167 | 110 |
" |
77 | 111 |
! ! |
1 | 112 |
|
113 |
!SortedCollection class methodsFor:'initialization'! |
|
114 |
||
115 |
initialize |
|
8180
7c346c8015f2
Only use #> for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
7554
diff
changeset
|
116 |
"setup the default sortBlock. |
7c346c8015f2
Only use #> for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
7554
diff
changeset
|
117 |
Use #>, since this is the base method in Magnitude." |
1155 | 118 |
|
119 |
"/ only do this once at early startup |
|
913 | 120 |
DefaultSortBlock isNil ifTrue:[ |
8874 | 121 |
DefaultSortBlock := [:a :b | a < b] |
913 | 122 |
] |
1 | 123 |
|
913 | 124 |
" |
125 |
SortedCollection initialize |
|
126 |
" |
|
1155 | 127 |
|
128 |
"Modified: 12.4.1996 / 12:29:27 / cg" |
|
1 | 129 |
! ! |
130 |
||
131 |
!SortedCollection class methodsFor:'instance creation'! |
|
132 |
||
2929 | 133 |
forStrings |
134 |
"this is supposed to return a sortedCollection, which sorts using |
|
135 |
the current locales collating sequence. For now, simply use a |
|
136 |
normal string compare. |
|
137 |
This will change" |
|
138 |
||
139 |
^ self new |
|
140 |
||
141 |
"Modified: 13.9.1997 / 10:18:58 / cg" |
|
142 |
"Created: 13.9.1997 / 10:41:54 / cg" |
|
143 |
! |
|
144 |
||
145 |
forStrings:size |
|
146 |
"this is supposed to return a sortedCollection, which sorts using |
|
147 |
the current locales collating sequence. For now, simply use a |
|
148 |
normal string compare. |
|
149 |
This will change" |
|
150 |
||
151 |
^ self new:size |
|
152 |
||
153 |
"Modified: 13.9.1997 / 10:18:58 / cg" |
|
154 |
! |
|
155 |
||
1 | 156 |
new |
2 | 157 |
"return a new sortedCollection, the sorting is done using |
1155 | 158 |
a compare for a < b, in ascending order" |
2 | 159 |
|
1 | 160 |
^ super new setSortBlock:DefaultSortBlock |
1155 | 161 |
|
162 |
"Modified: 12.4.1996 / 12:28:18 / cg" |
|
1 | 163 |
! |
164 |
||
165 |
new:size |
|
2 | 166 |
"return a new sortedCollection with preallocated size. |
1155 | 167 |
The sorting is done using a compare for a < b, in ascending order" |
2 | 168 |
|
1 | 169 |
^ (super new:size) setSortBlock:DefaultSortBlock |
1155 | 170 |
|
171 |
"Modified: 12.4.1996 / 12:28:22 / cg" |
|
1 | 172 |
! |
173 |
||
174 |
sortBlock:aBlock |
|
1155 | 175 |
"return a new sortedCollection, whe the sort order is defined |
8874 | 176 |
by aBlock. |
1155 | 177 |
This must be a two-argument block which returns true if its arg1 has to come before |
178 |
its arg2 in the collection." |
|
2 | 179 |
|
1 | 180 |
^ super new setSortBlock:aBlock |
1155 | 181 |
|
182 |
"default: |
|
183 |
|s| |
|
184 |
||
185 |
s := SortedCollection new. |
|
186 |
s add:15; add:99; add:3; add:-29; add:17; add:-6. |
|
1422 | 187 |
Transcript showCR:s |
1155 | 188 |
" |
189 |
||
190 |
"sorting by absolute values: |
|
191 |
|s| |
|
192 |
||
193 |
s := SortedCollection sortBlock:[:a :b | a abs < b abs]. |
|
194 |
s add:15; add:99; add:3; add:29; add:17; add:-6. |
|
1422 | 195 |
Transcript showCR:s |
1155 | 196 |
" |
197 |
||
198 |
"default again: |
|
199 |
|s| |
|
200 |
||
201 |
s := SortedCollection new. |
|
202 |
s add:'foo'; add:'Bar'; add:'baz'; add:'hello'; add:'world'; add:'Wow'. |
|
1422 | 203 |
Transcript showCR:s |
1155 | 204 |
" |
205 |
||
206 |
"sorting strings caseless: |
|
207 |
|s| |
|
208 |
||
209 |
s := SortedCollection sortBlock:[:a :b | a asLowercase < b asLowercase]. |
|
210 |
s add:'foo'; add:'Bar'; add:'baz'; add:'hello'; add:'world'; add:'Wow'. |
|
1422 | 211 |
Transcript showCR:s |
1155 | 212 |
" |
213 |
||
214 |
"Modified: 12.4.1996 / 12:26:28 / cg" |
|
329 | 215 |
! |
216 |
||
217 |
withAll:aCollection sortBlock:aBlock |
|
218 |
"initialize from aCollection and set the sort-block" |
|
219 |
||
2248 | 220 |
^ (self sortBlock:aBlock) addAll:aCollection; yourself |
329 | 221 |
|
222 |
" |
|
223 |
SortedCollection withAll:#(1 2 3 4 5 6 7 8 9 0) |
|
8874 | 224 |
sortBlock:[:a :b | a > b] |
1155 | 225 |
" |
226 |
||
227 |
"default: |
|
228 |
|s| |
|
229 |
||
230 |
s := SortedCollection withAll:#(15 99 3 29 17 -6). |
|
1422 | 231 |
Transcript showCR:s |
1155 | 232 |
" |
233 |
||
234 |
"sorting by absolute values: |
|
235 |
|s| |
|
236 |
||
237 |
s := SortedCollection withAll:#(15 99 3 29 17 -6) sortBlock:[:a :b | a abs < b abs]. |
|
1422 | 238 |
Transcript showCR:s |
329 | 239 |
" |
1155 | 240 |
|
241 |
"default again: |
|
242 |
|s| |
|
243 |
||
244 |
s := SortedCollection withAll:#('foo' 'Bar' 'baz' 'hello' 'world' 'Wow'). |
|
1422 | 245 |
Transcript showCR:s |
1155 | 246 |
" |
247 |
||
248 |
"sorting strings caseless: |
|
249 |
|s| |
|
250 |
||
251 |
s := SortedCollection withAll:#('foo' 'Bar' 'baz' 'hello' 'world' 'Wow') sortBlock:[:a :b | a asLowercase < b asLowercase]. |
|
1422 | 252 |
Transcript showCR:s |
1155 | 253 |
" |
254 |
||
255 |
"Modified: 12.4.1996 / 12:28:09 / cg" |
|
1 | 256 |
! ! |
257 |
||
7300 | 258 |
!SortedCollection class methodsFor:'Compatibility-Dolphin'! |
6411 | 259 |
|
260 |
sortBlock:aBlock withAll:aCollection |
|
261 |
^ self withAll:aCollection sortBlock:aBlock |
|
262 |
! ! |
|
263 |
||
1 | 264 |
!SortedCollection methodsFor:'adding & removing'! |
265 |
||
607 | 266 |
add:anObject |
267 |
"add the argument, anObject at the proper place in the |
|
268 |
receiver. Returns the argument, anObject." |
|
2 | 269 |
|
607 | 270 |
|index| |
1 | 271 |
|
607 | 272 |
lastIndex < firstIndex "i.e. self size == 0" ifTrue:[ |
8874 | 273 |
"/ empty - add at the end |
274 |
index := lastIndex. |
|
275 |
(index == contentsArray size) ifTrue:[ |
|
276 |
self makeRoomAtLast. |
|
277 |
index := lastIndex. |
|
278 |
]. |
|
279 |
lastIndex := index := index + 1. |
|
607 | 280 |
] ifFalse:[ |
8874 | 281 |
index := self indexForInserting:anObject. |
282 |
index := self makeRoomAtIndex:index. |
|
607 | 283 |
]. |
3340 | 284 |
contentsArray basicAt:index put:anObject. |
607 | 285 |
^ anObject |
2 | 286 |
|
607 | 287 |
" |
8874 | 288 |
|c| #(7 3 9 10 99) asSortedCollection add:5; yourself |
289 |
#(7 3 9 10 99) asSortedCollection add:1; yourself |
|
290 |
#(7 3 9 10 99) asSortedCollection add:1000; yourself |
|
607 | 291 |
" |
1162
3995101dfa44
changed for new #makeRoomAtIndex:; added comments
Claus Gittinger <cg@exept.de>
parents:
1156
diff
changeset
|
292 |
|
3340 | 293 |
"Modified: / 13.3.1998 / 16:21:52 / cg" |
1 | 294 |
! |
295 |
||
296 |
addAll:aCollection |
|
8874 | 297 |
"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
|
298 |
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
|
299 |
|
5816 | 300 |
|addedCollection| |
1 | 301 |
|
5359
396b77b6f1d2
added #isSortedCollection, #isSortedBy:
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
302 |
(aCollection isSortedCollection |
7554 | 303 |
and:[aCollection sortBlock == sortBlock]) ifTrue:[ |
8874 | 304 |
addedCollection := aCollection |
5030
655a0f0a60c3
use my sortBlock when merge-sorting
Claus Gittinger <cg@exept.de>
parents:
5029
diff
changeset
|
305 |
] ifFalse:[ |
8874 | 306 |
addedCollection := Array withAll:aCollection. |
307 |
addedCollection sort:sortBlock. |
|
5030
655a0f0a60c3
use my sortBlock when merge-sorting
Claus Gittinger <cg@exept.de>
parents:
5029
diff
changeset
|
308 |
|
8874 | 309 |
"/ special case: I am empty - add them en-bloque. |
310 |
"/ (but only if the argument is not a sortedCollection, |
|
311 |
"/ because quicksort degenerates to a veeery slow operation then) |
|
5030
655a0f0a60c3
use my sortBlock when merge-sorting
Claus Gittinger <cg@exept.de>
parents:
5029
diff
changeset
|
312 |
|
8874 | 313 |
self size == 0 ifTrue:[ |
314 |
contentsArray := addedCollection. |
|
315 |
firstIndex := 1. |
|
316 |
lastIndex := addedCollection size. |
|
317 |
^ aCollection |
|
318 |
]. |
|
1 | 319 |
]. |
1173
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
320 |
|
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
321 |
"/ |
5030
655a0f0a60c3
use my sortBlock when merge-sorting
Claus Gittinger <cg@exept.de>
parents:
5029
diff
changeset
|
322 |
"/ 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
|
323 |
"/ |
5030
655a0f0a60c3
use my sortBlock when merge-sorting
Claus Gittinger <cg@exept.de>
parents:
5029
diff
changeset
|
324 |
self mergeSorted:addedCollection. |
1173
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
325 |
^ aCollection |
1 | 326 |
|
77 | 327 |
" |
8874 | 328 |
#(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
|
329 |
|
5028
1f7e943ce46e
use my sortBlock when merge-sorting
Claus Gittinger <cg@exept.de>
parents:
4596
diff
changeset
|
330 |
( #(7 3 9 10 99) asSortedCollection:[:a :b | a > b]) |
8874 | 331 |
addAll:#(77 0 1 16 5); yourself |
5030
655a0f0a60c3
use my sortBlock when merge-sorting
Claus Gittinger <cg@exept.de>
parents:
5029
diff
changeset
|
332 |
|
8874 | 333 |
#(7 3 9 10 99) asSortedCollection |
334 |
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
|
335 |
|
8874 | 336 |
(#(7 3 9 10 99) asSortedCollection:[:a :b | a > b]) |
337 |
addAll:( #(77 0 1 16 5) asSortedCollection:[:a :b | a < b]); yourself |
|
77 | 338 |
" |
1173
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
339 |
|
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
340 |
"Modified: 13.4.1996 / 12:42:15 / cg" |
1 | 341 |
! |
342 |
||
1173
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
343 |
mergeSorted:aSortedCollection |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
344 |
"add all elements of the argument, aSortedCollection to the |
8874 | 345 |
receiver. |
1173
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
346 |
This leads to an error, if the argument is not a sortedCollection. |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
347 |
|
8874 | 348 |
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
|
349 |
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
|
350 |
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
|
351 |
Therefore, adding chunks is better done by sorting the chunk and merging |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
352 |
it in, instead of resorting the receiver." |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
353 |
|
8874 | 354 |
|newContentsArray |
1173
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
355 |
contentsArray2 |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
356 |
destIndex "{ Class: SmallInteger }" |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
357 |
n1 "{ Class: SmallInteger }" |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
358 |
n2 "{ Class: SmallInteger }" |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
359 |
srcIndex1 "{ Class: SmallInteger }" |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
360 |
srcIndex2 "{ Class: SmallInteger }" |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
361 |
last1 "{ Class: SmallInteger }" |
8874 | 362 |
last2 "{ Class: SmallInteger }" |
1173
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
363 |
end1Reached end2Reached |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
364 |
el1 el2 | |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
365 |
|
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
366 |
n1 := self size. |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
367 |
n2 := aSortedCollection size. |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
368 |
|
5381
914e310a69be
Fix #mergeSorted if one of the collections is empty.
Stefan Vogel <sv@exept.de>
parents:
5359
diff
changeset
|
369 |
n1 == 0 ifTrue:[ |
8874 | 370 |
^ aSortedCollection. |
5381
914e310a69be
Fix #mergeSorted if one of the collections is empty.
Stefan Vogel <sv@exept.de>
parents:
5359
diff
changeset
|
371 |
]. |
914e310a69be
Fix #mergeSorted if one of the collections is empty.
Stefan Vogel <sv@exept.de>
parents:
5359
diff
changeset
|
372 |
n2 == 0 ifTrue:[ |
8874 | 373 |
^ self. |
5381
914e310a69be
Fix #mergeSorted if one of the collections is empty.
Stefan Vogel <sv@exept.de>
parents:
5359
diff
changeset
|
374 |
]. |
914e310a69be
Fix #mergeSorted if one of the collections is empty.
Stefan Vogel <sv@exept.de>
parents:
5359
diff
changeset
|
375 |
|
1173
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
376 |
newContentsArray := Array new:(n1 + n2). |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
377 |
destIndex := 1. |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
378 |
|
5359
396b77b6f1d2
added #isSortedCollection, #isSortedBy:
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
379 |
(aSortedCollection isSortedBy:sortBlock) ifTrue:[ |
8874 | 380 |
aSortedCollection isSortedCollection ifTrue:[ |
381 |
contentsArray2 := aSortedCollection contentsArray. |
|
382 |
srcIndex2 := aSortedCollection firstIndex. |
|
383 |
] ifFalse:[ |
|
384 |
contentsArray2 := aSortedCollection. |
|
385 |
srcIndex2 := 1. |
|
386 |
] |
|
5030
655a0f0a60c3
use my sortBlock when merge-sorting
Claus Gittinger <cg@exept.de>
parents:
5029
diff
changeset
|
387 |
] ifFalse:[ |
8874 | 388 |
contentsArray2 := aSortedCollection. |
389 |
srcIndex2 := 1. |
|
5030
655a0f0a60c3
use my sortBlock when merge-sorting
Claus Gittinger <cg@exept.de>
parents:
5029
diff
changeset
|
390 |
]. |
655a0f0a60c3
use my sortBlock when merge-sorting
Claus Gittinger <cg@exept.de>
parents:
5029
diff
changeset
|
391 |
|
1173
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
392 |
last2 := srcIndex2 + n2 -1. |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
393 |
|
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
394 |
srcIndex1 := firstIndex. |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
395 |
last1 := srcIndex1 + n1 -1. |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
396 |
|
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
397 |
(srcIndex1 <= last1 and:[srcIndex2 <= last2]) ifTrue:[ |
8874 | 398 |
el1 := contentsArray at:srcIndex1. |
399 |
el2 := contentsArray2 at:srcIndex2. |
|
400 |
end1Reached := end2Reached := false. |
|
1173
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
401 |
|
8874 | 402 |
[end1Reached or:[end2Reached]] whileFalse:[ |
403 |
(sortBlock value:el1 value:el2) ifTrue:[ |
|
404 |
"/ el1 to come before el2 |
|
405 |
newContentsArray at:destIndex put:el1. destIndex := destIndex + 1. |
|
406 |
srcIndex1 := srcIndex1 + 1. |
|
407 |
srcIndex1 <= last1 ifTrue:[ |
|
408 |
el1 := contentsArray at:srcIndex1. |
|
409 |
] ifFalse:[ |
|
410 |
end1Reached := true |
|
411 |
] |
|
412 |
] ifFalse:[ |
|
413 |
"/ el2 to come before el1 |
|
414 |
newContentsArray at:destIndex put:el2. destIndex := destIndex + 1. |
|
415 |
srcIndex2 := srcIndex2 + 1. |
|
416 |
srcIndex2 <= last2 ifTrue:[ |
|
417 |
el2 := contentsArray2 at:srcIndex2. |
|
418 |
] ifFalse:[ |
|
419 |
end2Reached := true |
|
420 |
] |
|
421 |
] |
|
422 |
] |
|
1173
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
423 |
]. |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
424 |
|
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
425 |
"/ 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
|
426 |
(srcIndex1 <= last1) ifTrue:[ |
8874 | 427 |
newContentsArray |
428 |
replaceFrom:destIndex |
|
429 |
to:(destIndex+(last1-srcIndex1)) |
|
430 |
with:contentsArray |
|
431 |
startingAt:srcIndex1 |
|
1173
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
432 |
]. |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
433 |
|
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
434 |
"/ 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
|
435 |
(srcIndex2 <= last2) ifTrue:[ |
8874 | 436 |
newContentsArray |
437 |
replaceFrom:destIndex |
|
438 |
to:(destIndex+(last2-srcIndex2)) |
|
439 |
with:contentsArray2 |
|
440 |
startingAt:srcIndex2 |
|
1173
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
441 |
]. |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
442 |
|
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
443 |
firstIndex := 1. |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
444 |
lastIndex := n1 + n2. |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
445 |
contentsArray := newContentsArray |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
446 |
|
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
447 |
" |
8874 | 448 |
#(7 3 9 10 99) asSortedCollection |
449 |
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
|
450 |
|
8874 | 451 |
#(7 3 9 10 99) asSortedCollection |
452 |
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
|
453 |
|
8874 | 454 |
#() asSortedCollection |
455 |
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
|
456 |
|
8874 | 457 |
#(7 3 9 10 99) asSortedCollection |
458 |
mergeSorted:#() asSortedCollection |
|
1173
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
459 |
" |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
460 |
|
3250
c4b444774f1c
oops - mergesort cannot simply copy remaining
Claus Gittinger <cg@exept.de>
parents:
2929
diff
changeset
|
461 |
"Modified: / 30.1.1998 / 01:49:42 / cg" |
607 | 462 |
! ! |
463 |
||
5560 | 464 |
!SortedCollection methodsFor:'blocked'! |
465 |
||
466 |
add:newObject after:oldObject |
|
467 |
"catch this - its not allowed for sortedCollections" |
|
468 |
||
469 |
self shouldNotImplement |
|
470 |
! |
|
471 |
||
472 |
add:newObject before:oldObject |
|
473 |
"catch this - its not allowed for sortedCollections" |
|
474 |
||
475 |
self shouldNotImplement |
|
476 |
! |
|
477 |
||
478 |
addFirst:anObject |
|
479 |
"catch this - its not allowed for sortedCollections" |
|
480 |
||
481 |
self shouldNotImplement |
|
482 |
! |
|
483 |
||
484 |
addLast:anObject |
|
485 |
"catch this - its not allowed for sortedCollections" |
|
486 |
||
487 |
self shouldNotImplement |
|
488 |
! ! |
|
489 |
||
607 | 490 |
!SortedCollection methodsFor:'converting'! |
491 |
||
492 |
asSortedCollection |
|
493 |
"return the receiver as a sorted collection" |
|
494 |
||
495 |
"could be an instance of a subclass..." |
|
496 |
self class == SortedCollection ifTrue:[ |
|
8874 | 497 |
sortBlock == DefaultSortBlock ifTrue:[ |
498 |
^ self |
|
499 |
] |
|
1 | 500 |
]. |
607 | 501 |
^ super asSortedCollection |
5029
ae3312dda1dd
avoid creation of a new sortedCollection in #asSortedCollection:
Claus Gittinger <cg@exept.de>
parents:
5028
diff
changeset
|
502 |
! |
ae3312dda1dd
avoid creation of a new sortedCollection in #asSortedCollection:
Claus Gittinger <cg@exept.de>
parents:
5028
diff
changeset
|
503 |
|
ae3312dda1dd
avoid creation of a new sortedCollection in #asSortedCollection:
Claus Gittinger <cg@exept.de>
parents:
5028
diff
changeset
|
504 |
asSortedCollection:aSortBlock |
ae3312dda1dd
avoid creation of a new sortedCollection in #asSortedCollection:
Claus Gittinger <cg@exept.de>
parents:
5028
diff
changeset
|
505 |
"return the receiver as a sorted collection, using aSortBlock. |
ae3312dda1dd
avoid creation of a new sortedCollection in #asSortedCollection:
Claus Gittinger <cg@exept.de>
parents:
5028
diff
changeset
|
506 |
Return the receiver, if its a sortedCollection and the sortBlock |
ae3312dda1dd
avoid creation of a new sortedCollection in #asSortedCollection:
Claus Gittinger <cg@exept.de>
parents:
5028
diff
changeset
|
507 |
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
|
508 |
|
ae3312dda1dd
avoid creation of a new sortedCollection in #asSortedCollection:
Claus Gittinger <cg@exept.de>
parents:
5028
diff
changeset
|
509 |
"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
|
510 |
self class == SortedCollection ifTrue:[ |
8874 | 511 |
"/ if the sortBlock is the same, return the receiver |
512 |
aSortBlock == sortBlock ifTrue:[ |
|
513 |
^ self |
|
514 |
]. |
|
5029
ae3312dda1dd
avoid creation of a new sortedCollection in #asSortedCollection:
Claus Gittinger <cg@exept.de>
parents:
5028
diff
changeset
|
515 |
]. |
5030
655a0f0a60c3
use my sortBlock when merge-sorting
Claus Gittinger <cg@exept.de>
parents:
5029
diff
changeset
|
516 |
^ super asSortedCollection:aSortBlock |
1 | 517 |
! ! |
518 |
||
95 | 519 |
!SortedCollection methodsFor:'copying'! |
520 |
||
607 | 521 |
copyEmpty:size |
522 |
"return a copy of the receiver with no elements, but |
|
523 |
the same size. This method has been be redefined to |
|
524 |
preserve the sortBlock." |
|
525 |
||
526 |
^ (self species new:size) sortBlock:sortBlock |
|
527 |
! |
|
528 |
||
159 | 529 |
postCopyFrom:anOriginal |
530 |
"sent after a copy or when a new collection species has been created. |
|
95 | 531 |
The new collection should have the same sortBlock as the original." |
532 |
||
159 | 533 |
sortBlock := anOriginal sortBlock |
95 | 534 |
|
535 |
" |
|
536 |
#(4 7 1 99 -1 17) asSortedCollection inspect |
|
159 | 537 |
#(4 7 1 99 -1 17) asSortedCollection copy inspect |
95 | 538 |
(#(4 7 1 99 -1 17) asSortedCollection sortBlock:[:a :b | a > b]) inspect |
159 | 539 |
(#(4 7 1 99 -1 17) asSortedCollection sortBlock:[:a :b | a > b]) copy inspect |
540 |
(#(4 7 1 99 -1 17) asSortedCollection select:[:e| e even]) inspect |
|
95 | 541 |
" |
542 |
! ! |
|
543 |
||
216 | 544 |
!SortedCollection methodsFor:'enumerating'! |
184 | 545 |
|
546 |
collect:aBlock |
|
547 |
"evaluate the argument, aBlock for every element in the collection |
|
548 |
and return a collection of the results. Redefined to return an OrderedCollection; |
|
549 |
see X3J20 spec. (SortedCollection>>collect: should return an OrderedCollection)" |
|
550 |
||
551 |
|newCollection |
|
552 |
start "{ Class:SmallInteger }" |
|
553 |
stop "{ Class:SmallInteger }" | |
|
554 |
||
555 |
newCollection := OrderedCollection new:(self size). |
|
556 |
stop := lastIndex. |
|
557 |
start := firstIndex. |
|
558 |
start to:stop do:[:index | |
|
559 |
newCollection add:(aBlock value:(contentsArray at:index)). |
|
560 |
]. |
|
561 |
^ newCollection |
|
562 |
! ! |
|
563 |
||
607 | 564 |
!SortedCollection methodsFor:'instance protocol'! |
565 |
||
5668
0cbaa16dc17c
Inherited #sort: shows a debugger.
Stefan Vogel <sv@exept.de>
parents:
5604
diff
changeset
|
566 |
sort:aSortBlock |
0cbaa16dc17c
Inherited #sort: shows a debugger.
Stefan Vogel <sv@exept.de>
parents:
5604
diff
changeset
|
567 |
"redefined from superclass to change the sortBlock" |
0cbaa16dc17c
Inherited #sort: shows a debugger.
Stefan Vogel <sv@exept.de>
parents:
5604
diff
changeset
|
568 |
|
0cbaa16dc17c
Inherited #sort: shows a debugger.
Stefan Vogel <sv@exept.de>
parents:
5604
diff
changeset
|
569 |
sortBlock ~~ aSortBlock ifTrue:[ |
8874 | 570 |
self sortBlock:aSortBlock. |
5668
0cbaa16dc17c
Inherited #sort: shows a debugger.
Stefan Vogel <sv@exept.de>
parents:
5604
diff
changeset
|
571 |
]. |
0cbaa16dc17c
Inherited #sort: shows a debugger.
Stefan Vogel <sv@exept.de>
parents:
5604
diff
changeset
|
572 |
|
0cbaa16dc17c
Inherited #sort: shows a debugger.
Stefan Vogel <sv@exept.de>
parents:
5604
diff
changeset
|
573 |
! |
0cbaa16dc17c
Inherited #sort: shows a debugger.
Stefan Vogel <sv@exept.de>
parents:
5604
diff
changeset
|
574 |
|
607 | 575 |
sortBlock |
576 |
"return the block used for sorting" |
|
577 |
||
578 |
^ sortBlock |
|
579 |
! |
|
580 |
||
581 |
sortBlock:aSortBlock |
|
8874 | 582 |
"change the sort criteria for a sorted collection, resort the elements of |
607 | 583 |
the collection, and return the receiver. The argument, aSortBlock must |
584 |
be a two-argument block which returns true if its arg1 has to come before |
|
585 |
its arg2 in the collection." |
|
586 |
||
587 |
sortBlock := aSortBlock. |
|
588 |
lastIndex > firstIndex ifTrue:[ |
|
589 |
contentsArray quickSortFrom:firstIndex to:lastIndex sortBlock:aSortBlock |
|
590 |
] |
|
591 |
||
592 |
" |
|
593 |
#(9 8 7 6 5 4 3) asSortedCollection |
|
594 |
#(9 8 7 6 5 4 3) asSortedCollection sortBlock:[:a : b | a < b] |
|
595 |
#(9 8 7 6 5 4 3) asSortedCollection sortBlock:[:a : b | a > b] |
|
596 |
#($f $G $z $Y $o $H) asSortedCollection |
|
597 |
#($f $G $z $Y $o $H) asSortedCollection sortBlock:[:a : b | a asUppercase < b asUppercase] |
|
598 |
" |
|
599 |
! ! |
|
600 |
||
601 |
!SortedCollection methodsFor:'private'! |
|
602 |
||
603 |
indexForInserting:anObject |
|
8874 | 604 |
"search the index at which to insert anObject. |
1162
3995101dfa44
changed for new #makeRoomAtIndex:; added comments
Claus Gittinger <cg@exept.de>
parents:
1156
diff
changeset
|
605 |
Can also be used to search for an existing element |
3995101dfa44
changed for new #makeRoomAtIndex:; added comments
Claus Gittinger <cg@exept.de>
parents:
1156
diff
changeset
|
606 |
by checking if the element at the returned index is the one we look for. |
3995101dfa44
changed for new #makeRoomAtIndex:; added comments
Claus Gittinger <cg@exept.de>
parents:
1156
diff
changeset
|
607 |
Uses a binarySearch since we can depend on the elements being on sorted order. |
607 | 608 |
The returned index is a physical one, for accessing contentsArray." |
609 |
||
610 |
|low "{ Class: SmallInteger}" |
|
611 |
high "{ Class: SmallInteger}" |
|
612 |
middle "{ Class: SmallInteger}" |
|
613 |
element| |
|
614 |
||
615 |
" |
|
1162
3995101dfa44
changed for new #makeRoomAtIndex:; added comments
Claus Gittinger <cg@exept.de>
parents:
1156
diff
changeset
|
616 |
we can of course use a binary search - since the elements are sorted |
607 | 617 |
" |
618 |
low := firstIndex. |
|
619 |
high := lastIndex. |
|
620 |
[low <= high] whileTrue:[ |
|
8874 | 621 |
middle := (low + high) // 2. |
622 |
element := contentsArray at:middle. |
|
623 |
(sortBlock value:element value:anObject) ifTrue:[ |
|
624 |
"middleelement is smaller than object" |
|
625 |
low := middle + 1 |
|
626 |
] ifFalse:[ |
|
627 |
high := middle - 1 |
|
628 |
] |
|
607 | 629 |
]. |
630 |
^ low |
|
631 |
||
632 |
" |
|
8874 | 633 |
#(1 2 3 4 7 99 1313 981989 898989898) asSortedCollection indexForInserting:50 |
634 |
#(1.0 2.0 3 4 7 49.0 51.0 99 1313 981989 898989898) asSortedCollection indexForInserting:50 |
|
607 | 635 |
" |
636 |
||
1162
3995101dfa44
changed for new #makeRoomAtIndex:; added comments
Claus Gittinger <cg@exept.de>
parents:
1156
diff
changeset
|
637 |
"Modified: 12.4.1996 / 13:22:03 / cg" |
607 | 638 |
! |
639 |
||
640 |
setSortBlock: aSortBlock |
|
641 |
"set the sortblock without resorting - private only" |
|
642 |
||
643 |
sortBlock := aSortBlock |
|
644 |
! ! |
|
645 |
||
1173
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
646 |
!SortedCollection methodsFor:'queries'! |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
647 |
|
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
648 |
isSorted |
5359
396b77b6f1d2
added #isSortedCollection, #isSortedBy:
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
649 |
"return true. if my elements are sorted" |
396b77b6f1d2
added #isSortedCollection, #isSortedBy:
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
650 |
|
1173
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
651 |
^ true |
5359
396b77b6f1d2
added #isSortedCollection, #isSortedBy:
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
652 |
! |
396b77b6f1d2
added #isSortedCollection, #isSortedBy:
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
653 |
|
396b77b6f1d2
added #isSortedCollection, #isSortedBy:
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
654 |
isSortedBy:aBlock |
396b77b6f1d2
added #isSortedCollection, #isSortedBy:
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
655 |
"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
|
656 |
|
5359
396b77b6f1d2
added #isSortedCollection, #isSortedBy:
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
657 |
aBlock == sortBlock ifTrue:[^ true]. |
396b77b6f1d2
added #isSortedCollection, #isSortedBy:
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
658 |
^ super isSortedBy:aBlock |
396b77b6f1d2
added #isSortedCollection, #isSortedBy:
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
659 |
|
396b77b6f1d2
added #isSortedCollection, #isSortedBy:
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
660 |
|
396b77b6f1d2
added #isSortedCollection, #isSortedBy:
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
661 |
! |
396b77b6f1d2
added #isSortedCollection, #isSortedBy:
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
662 |
|
396b77b6f1d2
added #isSortedCollection, #isSortedBy:
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
663 |
isSortedCollection |
396b77b6f1d2
added #isSortedCollection, #isSortedBy:
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
664 |
"return true. if I am a sorted collection" |
396b77b6f1d2
added #isSortedCollection, #isSortedBy:
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
665 |
|
396b77b6f1d2
added #isSortedCollection, #isSortedBy:
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
666 |
^ true |
1173
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
667 |
! ! |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
668 |
|
607 | 669 |
!SortedCollection methodsFor:'searching'! |
670 |
||
671 |
after:anObject ifAbsent:exceptionBlock |
|
672 |
"return the element after the argument, anObject; or nil if there is none. |
|
673 |
If anObject is contained multiple times in the collection, return the |
|
674 |
the first element which is non-equal to anObject. |
|
675 |
If the receiver does not contain anObject, return the result from evaluating |
|
676 |
exceptionBlock." |
|
677 |
||
1162
3995101dfa44
changed for new #makeRoomAtIndex:; added comments
Claus Gittinger <cg@exept.de>
parents:
1156
diff
changeset
|
678 |
|index "{ Class: SmallInteger }" |
3995101dfa44
changed for new #makeRoomAtIndex:; added comments
Claus Gittinger <cg@exept.de>
parents:
1156
diff
changeset
|
679 |
last "{ Class: SmallInteger }"| |
607 | 680 |
|
681 |
index := self indexForInserting:anObject. |
|
8874 | 682 |
((index < firstIndex) |
607 | 683 |
or:[(contentsArray at:index) ~= anObject]) ifTrue:[^ exceptionBlock value]. |
684 |
||
8874 | 685 |
"skip multiple occurrences of the same ..." |
607 | 686 |
|
1162
3995101dfa44
changed for new #makeRoomAtIndex:; added comments
Claus Gittinger <cg@exept.de>
parents:
1156
diff
changeset
|
687 |
last := lastIndex. |
3995101dfa44
changed for new #makeRoomAtIndex:; added comments
Claus Gittinger <cg@exept.de>
parents:
1156
diff
changeset
|
688 |
[(index <= last) and:[(contentsArray at:index) = anObject]] whileTrue:[ |
8874 | 689 |
index := index + 1 |
607 | 690 |
]. |
1162
3995101dfa44
changed for new #makeRoomAtIndex:; added comments
Claus Gittinger <cg@exept.de>
parents:
1156
diff
changeset
|
691 |
(index > last) ifTrue:[^ nil]. |
607 | 692 |
^ contentsArray at:index |
1162
3995101dfa44
changed for new #makeRoomAtIndex:; added comments
Claus Gittinger <cg@exept.de>
parents:
1156
diff
changeset
|
693 |
|
3995101dfa44
changed for new #makeRoomAtIndex:; added comments
Claus Gittinger <cg@exept.de>
parents:
1156
diff
changeset
|
694 |
"Modified: 12.4.1996 / 13:20:33 / cg" |
607 | 695 |
! |
696 |
||
697 |
before:anObject ifAbsent:exceptionBlock |
|
698 |
"return the element before the argument, anObject; or nil if there is none. |
|
699 |
If the receiver does not contain anObject, return the result from evaluating |
|
700 |
exceptionBlock." |
|
701 |
||
702 |
|index "{ Class: SmallInteger }"| |
|
703 |
||
704 |
index := self indexForInserting:anObject. |
|
8874 | 705 |
((index <= firstIndex) |
607 | 706 |
or:[(contentsArray at:index) ~= anObject]) ifTrue:[^ exceptionBlock value]. |
707 |
||
708 |
^ contentsArray at:index - 1 |
|
709 |
||
710 |
" |
|
711 |
#(7 3 9 10 99) asSortedCollection before:50 |
|
8874 | 712 |
#(7 3 9 10 99) asSortedCollection before:1 |
713 |
#(7 3 9 10 99) asSortedCollection before:10 |
|
714 |
#(7 3 9 10 99) asSortedCollection before:7 |
|
715 |
#(7 3 9 10 99) asSortedCollection before:99 |
|
716 |
#(7 10 3 10 9 10 10 99) asSortedCollection before:9 |
|
717 |
#(7 10 3 10 9 10 10 99) asSortedCollection before:10 |
|
607 | 718 |
" |
4596 | 719 |
! |
720 |
||
721 |
indexOf:anElement |
|
8874 | 722 |
"Return the index of anElement within the receiver. |
4596 | 723 |
If the receiver does not contain anElement, return 0." |
724 |
||
725 |
^ self indexOf:anElement ifAbsent:0 |
|
726 |
! |
|
727 |
||
728 |
indexOf:anElement ifAbsent:exceptionBlock |
|
8874 | 729 |
"Return the index of anElement within the receiver. |
730 |
If the receiver does not contain anElement, |
|
4596 | 731 |
return the result of evaluating the argument, exceptionBlock." |
732 |
||
8874 | 733 |
|insertionIndex index "{ Class: SmallInteger }" |
4596 | 734 |
obj| |
735 |
||
736 |
firstIndex > lastIndex ifTrue:[ |
|
8874 | 737 |
"/ empty |
738 |
^ exceptionBlock value |
|
4596 | 739 |
]. |
740 |
||
741 |
"/ if I am small, the inherited linear search is faster ... |
|
742 |
(lastIndex - firstIndex) < 20 ifTrue:[ |
|
8874 | 743 |
^ super indexOf:anElement ifAbsent:exceptionBlock |
4596 | 744 |
]. |
745 |
||
746 |
insertionIndex := self indexForInserting:anElement. |
|
747 |
insertionIndex > lastIndex ifTrue:[ |
|
8874 | 748 |
insertionIndex := lastIndex |
4596 | 749 |
] ifFalse:[ |
8874 | 750 |
insertionIndex < firstIndex ifTrue:[ |
751 |
insertionIndex := firstIndex |
|
752 |
] |
|
4596 | 753 |
]. |
754 |
||
755 |
index := insertionIndex. |
|
756 |
[index >= firstIndex |
|
757 |
and:[obj := contentsArray basicAt:index. |
|
8874 | 758 |
anElement = obj ifTrue: [^ index - firstIndex + 1]. |
759 |
[sortBlock value:anElement value:obj]]] |
|
760 |
whileTrue: [index := index - 1]. |
|
4596 | 761 |
|
762 |
index := insertionIndex. |
|
763 |
[index <= lastIndex |
|
764 |
and: [obj := contentsArray basicAt: index. |
|
8874 | 765 |
anElement = obj ifTrue: [^ index - firstIndex + 1]. |
766 |
[sortBlock value:obj value:anElement]]] |
|
767 |
whileTrue: [index := index + 1]. |
|
4596 | 768 |
|
769 |
^exceptionBlock value |
|
770 |
||
771 |
" |
|
772 |
#('aa' 'bb' 'cc' 'dd') asSortedCollection indexOf:'bb' |
|
8874 | 773 |
#('aa' 'bb' 'cc' 'dd' 'aa' 'bb' 'cc' 'dd') asSortedCollection indexOf:'bb' |
4596 | 774 |
|
775 |
|allSyms indices| |
|
8874 | 776 |
allSyms := Symbol allInstances asSortedCollection. |
4596 | 777 |
Time millisecondsToRun:[ |
8874 | 778 |
indices := allSyms collect:[:el | allSyms indexOf:el]. |
4596 | 779 |
]. |
780 |
indices = (1 to:allSyms size) |
|
781 |
" |
|
607 | 782 |
! ! |
783 |
||
1 | 784 |
!SortedCollection methodsFor:'testing'! |
785 |
||
786 |
includes:anObject |
|
787 |
"return true, if the argument, anObject is in the collection. |
|
62 | 788 |
Redefined, since due to being sorted, the inclusion check can |
2 | 789 |
be done with log-n compares i.e. much faster." |
1 | 790 |
|
32 | 791 |
|index "{ Class: SmallInteger }"| |
1 | 792 |
|
77 | 793 |
index := self indexForInserting:anObject. |
32 | 794 |
((index < firstIndex) or:[index > lastIndex]) ifTrue:[^ false]. |
795 |
^ (contentsArray at:index) = anObject |
|
2 | 796 |
|
77 | 797 |
" |
798 |
#(7 3 9 10 99) asSortedCollection includes:50 |
|
799 |
#(7 3 9 10 99) asSortedCollection includes:10 |
|
800 |
" |
|
1 | 801 |
! |
802 |
||
803 |
occurrencesOf:anObject |
|
804 |
"return how many times the argument, anObject is in the collection. |
|
62 | 805 |
Redefined, since due to being sorted, the range of checked objects |
5048 | 806 |
can be limited i.e. it can be done much faster. |
807 |
Uses #= (i.e. equality) compare." |
|
1 | 808 |
|
809 |
|index "{ Class: SmallInteger }" |
|
1162
3995101dfa44
changed for new #makeRoomAtIndex:; added comments
Claus Gittinger <cg@exept.de>
parents:
1156
diff
changeset
|
810 |
tally "{ Class: SmallInteger }" |
3995101dfa44
changed for new #makeRoomAtIndex:; added comments
Claus Gittinger <cg@exept.de>
parents:
1156
diff
changeset
|
811 |
last "{ Class: SmallInteger }" | |
1 | 812 |
|
77 | 813 |
index := self indexForInserting:anObject. |
8874 | 814 |
last := lastIndex. |
1162
3995101dfa44
changed for new #makeRoomAtIndex:; added comments
Claus Gittinger <cg@exept.de>
parents:
1156
diff
changeset
|
815 |
((index < firstIndex) or:[index > last]) ifTrue:[^ 0]. |
3995101dfa44
changed for new #makeRoomAtIndex:; added comments
Claus Gittinger <cg@exept.de>
parents:
1156
diff
changeset
|
816 |
|
3995101dfa44
changed for new #makeRoomAtIndex:; added comments
Claus Gittinger <cg@exept.de>
parents:
1156
diff
changeset
|
817 |
"/ there may be multiple of them; count 'em |
32 | 818 |
|
1 | 819 |
tally := 0. |
1162
3995101dfa44
changed for new #makeRoomAtIndex:; added comments
Claus Gittinger <cg@exept.de>
parents:
1156
diff
changeset
|
820 |
[(index <= last) and:[(contentsArray at:index) = anObject]] whileTrue:[ |
8874 | 821 |
tally := tally + 1. |
822 |
index := index + 1 |
|
1 | 823 |
]. |
824 |
^ tally |
|
2 | 825 |
|
77 | 826 |
" |
827 |
#(7 3 9 10 99) asSortedCollection occurrencesOf:50 |
|
828 |
#(7 3 9 10 99) asSortedCollection occurrencesOf:10 |
|
829 |
#(7 10 3 10 9 10 10 99) asSortedCollection occurrencesOf:10 |
|
830 |
" |
|
1162
3995101dfa44
changed for new #makeRoomAtIndex:; added comments
Claus Gittinger <cg@exept.de>
parents:
1156
diff
changeset
|
831 |
|
3995101dfa44
changed for new #makeRoomAtIndex:; added comments
Claus Gittinger <cg@exept.de>
parents:
1156
diff
changeset
|
832 |
"Modified: 12.4.1996 / 18:48:40 / cg" |
77 | 833 |
! ! |
834 |
||
629 | 835 |
!SortedCollection class methodsFor:'documentation'! |
836 |
||
837 |
version |
|
8874 | 838 |
^ '$Header: /cvs/stx/stx/libbasic/SortedCollection.st,v 1.56 2005-04-28 17:29:30 cg Exp $' |
629 | 839 |
! ! |
7300 | 840 |
|
607 | 841 |
SortedCollection initialize! |