author | Claus Gittinger <cg@exept.de> |
Fri, 24 Jan 1997 19:16:48 +0100 | |
changeset 2256 | ffd565c6e084 |
parent 2248 | b11a16f51048 |
child 2929 | ca74fdc386cc |
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 |
||
13 |
OrderedCollection subclass:#SortedCollection |
|
1155 | 14 |
instanceVariableNames:'sortBlock' |
15 |
classVariableNames:'DefaultSortBlock' |
|
16 |
poolDictionaries:'' |
|
17 |
category:'Collections-Sequenceable' |
|
1 | 18 |
! |
19 |
||
77 | 20 |
!SortedCollection class methodsFor:'documentation'! |
1 | 21 |
|
88 | 22 |
copyright |
23 |
" |
|
24 |
COPYRIGHT (c) 1993 by Claus Gittinger |
|
159 | 25 |
All Rights Reserved |
88 | 26 |
|
27 |
This software is furnished under a license and may be used |
|
28 |
only in accordance with the terms of that license and with the |
|
29 |
inclusion of the above copyright notice. This software may not |
|
30 |
be provided or otherwise made available to, or used by, any |
|
31 |
other person. No title to or ownership of the software is |
|
32 |
hereby transferred. |
|
33 |
" |
|
34 |
! |
|
35 |
||
77 | 36 |
documentation |
37 |
" |
|
38 |
I keep my elements sorted. The sort order is defined by a sortblock, |
|
39 |
a two-argument block which, when given two elements of the collection, |
|
40 |
should return true if the element given as first arg has to come before the |
|
41 |
element given as second arg. |
|
42 |
||
1156 | 43 |
Equal elements may occur multiple times. |
44 |
||
45 |
SortedCollection uses quickSort to resort and a binary search when adding/removing |
|
46 |
elements. |
|
47 |
Because insertion/removal may require that remaining elements have to |
|
48 |
be shifted within the container, adding many individual elements may be done faster |
|
49 |
by creating a completely new collection from the unsorted elements. |
|
1167 | 50 |
(see examples) |
1156 | 51 |
|
77 | 52 |
Thus a sortBlock of [:a :b | a < b] defines ascending sort-order, |
53 |
while [:a :b | a > b] defines descening order. |
|
54 |
The default sortBlock for SortedCollections is the first one. |
|
1290 | 55 |
|
56 |
[author:] |
|
57 |
Claus Gittinger |
|
77 | 58 |
" |
1167 | 59 |
! |
60 |
||
61 |
examples |
|
62 |
" |
|
1175 | 63 |
when many elements are to be added, it may be better to add them |
64 |
all en-bloeque instead of individually. |
|
1167 | 65 |
The reason is that for each individual #add:, the contents has to be |
1175 | 66 |
shifted, to create an empty slot for the new element. |
1167 | 67 |
|
68 |
timing example: |
|
69 |
||
70 |
|o rnd| |
|
71 |
||
72 |
o := SortedCollection new. |
|
73 |
rnd := Random new. |
|
1175 | 74 |
10000 timesRepeat:[ |
1167 | 75 |
o add:rnd next. |
1173
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
76 |
] |
1167 | 77 |
|
1175 | 78 |
takes 1365 ms on a P5 (admitted: this is a fast machine ;-) |
1167 | 79 |
In contrast: |
80 |
||
81 |
|o rnd| |
|
82 |
||
83 |
o := OrderedCollection new. |
|
84 |
rnd := Random new. |
|
1175 | 85 |
10000 timesRepeat:[ |
1167 | 86 |
o add:rnd next. |
87 |
]. |
|
88 |
o := o asSortedCollection |
|
89 |
||
1175 | 90 |
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
|
91 |
|
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
92 |
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
|
93 |
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
|
94 |
the number of added items is more than some threshold number. |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
95 |
Building some big collection: |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
96 |
|
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
97 |
|o rnd c| |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
98 |
|
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
99 |
o := OrderedCollection new. |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
100 |
rnd := Random new. |
1175 | 101 |
10000 timesRepeat:[ |
1173
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
102 |
o add:rnd next. |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
103 |
]. |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
104 |
o := o asSortedCollection. |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
105 |
|
1175 | 106 |
adding 1000 elements individually to it: |
1173
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
107 |
|
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
108 |
1000 timesRepeat:[ |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
109 |
o add:rnd next. |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
110 |
]. |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
111 |
|
1175 | 112 |
takes 258 ms |
1173
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
113 |
(because the elements have to be shifted around for each added element); |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
114 |
|
1175 | 115 |
while adding them en-bloque with: |
1173
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
116 |
|
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
117 |
c := OrderedCollection new. |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
118 |
1000 timesRepeat:[ |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
119 |
c add:rnd next. |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
120 |
]. |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
121 |
o addAll:o |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
122 |
|
1175 | 123 |
takes 71 ms |
1167 | 124 |
" |
77 | 125 |
! ! |
1 | 126 |
|
127 |
!SortedCollection class methodsFor:'initialization'! |
|
128 |
||
129 |
initialize |
|
1155 | 130 |
"setup the default sortBlock." |
131 |
||
132 |
"/ only do this once at early startup |
|
913 | 133 |
DefaultSortBlock isNil ifTrue:[ |
1155 | 134 |
DefaultSortBlock := [:a :b | a < b ] |
913 | 135 |
] |
1 | 136 |
|
913 | 137 |
" |
138 |
SortedCollection initialize |
|
139 |
" |
|
1155 | 140 |
|
141 |
"Modified: 12.4.1996 / 12:29:27 / cg" |
|
1 | 142 |
! ! |
143 |
||
144 |
!SortedCollection class methodsFor:'instance creation'! |
|
145 |
||
146 |
new |
|
2 | 147 |
"return a new sortedCollection, the sorting is done using |
1155 | 148 |
a compare for a < b, in ascending order" |
2 | 149 |
|
1 | 150 |
^ super new setSortBlock:DefaultSortBlock |
1155 | 151 |
|
152 |
"Modified: 12.4.1996 / 12:28:18 / cg" |
|
1 | 153 |
! |
154 |
||
155 |
new:size |
|
2 | 156 |
"return a new sortedCollection with preallocated size. |
1155 | 157 |
The sorting is done using a compare for a < b, in ascending order" |
2 | 158 |
|
1 | 159 |
^ (super new:size) setSortBlock:DefaultSortBlock |
1155 | 160 |
|
161 |
"Modified: 12.4.1996 / 12:28:22 / cg" |
|
1 | 162 |
! |
163 |
||
164 |
sortBlock:aBlock |
|
1155 | 165 |
"return a new sortedCollection, whe the sort order is defined |
166 |
by aBlock. |
|
167 |
This must be a two-argument block which returns true if its arg1 has to come before |
|
168 |
its arg2 in the collection." |
|
2 | 169 |
|
1 | 170 |
^ super new setSortBlock:aBlock |
1155 | 171 |
|
172 |
"default: |
|
173 |
|s| |
|
174 |
||
175 |
s := SortedCollection new. |
|
176 |
s add:15; add:99; add:3; add:-29; add:17; add:-6. |
|
1422 | 177 |
Transcript showCR:s |
1155 | 178 |
" |
179 |
||
180 |
"sorting by absolute values: |
|
181 |
|s| |
|
182 |
||
183 |
s := SortedCollection sortBlock:[:a :b | a abs < b abs]. |
|
184 |
s add:15; add:99; add:3; add:29; add:17; add:-6. |
|
1422 | 185 |
Transcript showCR:s |
1155 | 186 |
" |
187 |
||
188 |
"default again: |
|
189 |
|s| |
|
190 |
||
191 |
s := SortedCollection new. |
|
192 |
s add:'foo'; add:'Bar'; add:'baz'; add:'hello'; add:'world'; add:'Wow'. |
|
1422 | 193 |
Transcript showCR:s |
1155 | 194 |
" |
195 |
||
196 |
"sorting strings caseless: |
|
197 |
|s| |
|
198 |
||
199 |
s := SortedCollection sortBlock:[:a :b | a asLowercase < b asLowercase]. |
|
200 |
s add:'foo'; add:'Bar'; add:'baz'; add:'hello'; add:'world'; add:'Wow'. |
|
1422 | 201 |
Transcript showCR:s |
1155 | 202 |
" |
203 |
||
204 |
"Modified: 12.4.1996 / 12:26:28 / cg" |
|
329 | 205 |
! |
206 |
||
207 |
withAll:aCollection sortBlock:aBlock |
|
208 |
"initialize from aCollection and set the sort-block" |
|
209 |
||
2248 | 210 |
^ (self sortBlock:aBlock) addAll:aCollection; yourself |
329 | 211 |
|
212 |
" |
|
213 |
SortedCollection withAll:#(1 2 3 4 5 6 7 8 9 0) |
|
1155 | 214 |
sortBlock:[:a :b | a > b] |
215 |
" |
|
216 |
||
217 |
"default: |
|
218 |
|s| |
|
219 |
||
220 |
s := SortedCollection withAll:#(15 99 3 29 17 -6). |
|
1422 | 221 |
Transcript showCR:s |
1155 | 222 |
" |
223 |
||
224 |
"sorting by absolute values: |
|
225 |
|s| |
|
226 |
||
227 |
s := SortedCollection withAll:#(15 99 3 29 17 -6) sortBlock:[:a :b | a abs < b abs]. |
|
1422 | 228 |
Transcript showCR:s |
329 | 229 |
" |
1155 | 230 |
|
231 |
"default again: |
|
232 |
|s| |
|
233 |
||
234 |
s := SortedCollection withAll:#('foo' 'Bar' 'baz' 'hello' 'world' 'Wow'). |
|
1422 | 235 |
Transcript showCR:s |
1155 | 236 |
" |
237 |
||
238 |
"sorting strings caseless: |
|
239 |
|s| |
|
240 |
||
241 |
s := SortedCollection withAll:#('foo' 'Bar' 'baz' 'hello' 'world' 'Wow') sortBlock:[:a :b | a asLowercase < b asLowercase]. |
|
1422 | 242 |
Transcript showCR:s |
1155 | 243 |
" |
244 |
||
245 |
"Modified: 12.4.1996 / 12:28:09 / cg" |
|
1 | 246 |
! ! |
247 |
||
248 |
!SortedCollection methodsFor:'adding & removing'! |
|
249 |
||
607 | 250 |
add:anObject |
251 |
"add the argument, anObject at the proper place in the |
|
252 |
receiver. Returns the argument, anObject." |
|
2 | 253 |
|
607 | 254 |
|index| |
1 | 255 |
|
607 | 256 |
lastIndex < firstIndex "i.e. self size == 0" ifTrue:[ |
1162
3995101dfa44
changed for new #makeRoomAtIndex:; added comments
Claus Gittinger <cg@exept.de>
parents:
1156
diff
changeset
|
257 |
super add:anObject |
607 | 258 |
] ifFalse:[ |
1162
3995101dfa44
changed for new #makeRoomAtIndex:; added comments
Claus Gittinger <cg@exept.de>
parents:
1156
diff
changeset
|
259 |
index := self indexForInserting:anObject. |
3995101dfa44
changed for new #makeRoomAtIndex:; added comments
Claus Gittinger <cg@exept.de>
parents:
1156
diff
changeset
|
260 |
index := self makeRoomAtIndex:index. |
3995101dfa44
changed for new #makeRoomAtIndex:; added comments
Claus Gittinger <cg@exept.de>
parents:
1156
diff
changeset
|
261 |
contentsArray at:index put:anObject |
607 | 262 |
]. |
263 |
^ anObject |
|
2 | 264 |
|
607 | 265 |
" |
266 |
|c| #(7 3 9 10 99) asSortedCollection add:5; yourself |
|
267 |
#(7 3 9 10 99) asSortedCollection add:1; yourself |
|
268 |
#(7 3 9 10 99) asSortedCollection add:1000; yourself |
|
269 |
" |
|
1162
3995101dfa44
changed for new #makeRoomAtIndex:; added comments
Claus Gittinger <cg@exept.de>
parents:
1156
diff
changeset
|
270 |
|
3995101dfa44
changed for new #makeRoomAtIndex:; added comments
Claus Gittinger <cg@exept.de>
parents:
1156
diff
changeset
|
271 |
"Modified: 12.4.1996 / 16:47:41 / cg" |
1 | 272 |
! |
273 |
||
274 |
add:newObject after:oldObject |
|
2 | 275 |
"catch this - its not allowed for sortedCollections" |
276 |
||
1 | 277 |
self shouldNotImplement |
278 |
! |
|
279 |
||
280 |
add:newObject before:oldObject |
|
2 | 281 |
"catch this - its not allowed for sortedCollections" |
282 |
||
1 | 283 |
self shouldNotImplement |
284 |
! |
|
285 |
||
286 |
addAll:aCollection |
|
287 |
"add all elements of the argument, aCollection to the |
|
1173
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
288 |
receiver. Returns the argument, aCollection." |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
289 |
|
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
290 |
|newContents| |
1 | 291 |
|
1173
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
292 |
"/ special case: I am empty - add them en-bloque. |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
293 |
"/ (but only 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
|
294 |
"/ because quicksort degenerates to a veeery slow operation then) |
1 | 295 |
|
1173
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
296 |
(self size == 0 and:[aCollection isSorted not]) ifTrue:[ |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
297 |
newContents := Array withAll:(aCollection asArray). |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
298 |
newContents == aCollection ifTrue:[ |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
299 |
newContents := newContents shallowCopy |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
300 |
]. |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
301 |
newContents sort:sortBlock. |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
302 |
contentsArray := newContents. |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
303 |
firstIndex := 1. |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
304 |
lastIndex := newContents size. |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
305 |
^ aCollection |
1 | 306 |
]. |
1173
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
307 |
|
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
308 |
"/ |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
309 |
"/ do a mergeSort |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
310 |
"/ |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
311 |
self mergeSorted:(aCollection asSortedCollection). |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
312 |
^ aCollection |
1 | 313 |
|
77 | 314 |
" |
1173
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
315 |
#(7 3 9 10 99) asSortedCollection addAll:#(77 0 1 16 5); yourself |
77 | 316 |
" |
1173
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
317 |
|
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
318 |
"Modified: 13.4.1996 / 12:42:15 / cg" |
1 | 319 |
! |
320 |
||
607 | 321 |
addFirst:anObject |
322 |
"catch this - its not allowed for sortedCollections" |
|
323 |
||
324 |
self shouldNotImplement |
|
325 |
! |
|
1 | 326 |
|
607 | 327 |
addLast:anObject |
328 |
"catch this - its not allowed for sortedCollections" |
|
329 |
||
330 |
self shouldNotImplement |
|
331 |
! |
|
1 | 332 |
|
607 | 333 |
at:index put:anObject |
334 |
"catch this - its not allowed for sortedCollections" |
|
335 |
||
336 |
self shouldNotImplement |
|
1173
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
337 |
! |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
338 |
|
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
339 |
mergeSorted:aSortedCollection |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
340 |
"add all elements of the argument, aSortedCollection to the |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
341 |
receiver. |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
342 |
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
|
343 |
|
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
344 |
This should be used when a number of elements is to be added. |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
345 |
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
|
346 |
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
|
347 |
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
|
348 |
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
|
349 |
|
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
350 |
|newContentsArray |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
351 |
contentsArray2 |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
352 |
destIndex "{ Class: SmallInteger }" |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
353 |
n1 "{ Class: SmallInteger }" |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
354 |
n2 "{ Class: SmallInteger }" |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
355 |
srcIndex1 "{ Class: SmallInteger }" |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
356 |
srcIndex2 "{ Class: SmallInteger }" |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
357 |
last1 "{ Class: SmallInteger }" |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
358 |
last2 "{ Class: SmallInteger }" |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
359 |
end1Reached end2Reached |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
360 |
el1 el2 | |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
361 |
|
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
362 |
n1 := self size. |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
363 |
n2 := aSortedCollection size. |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
364 |
|
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
365 |
newContentsArray := Array new:(n1 + n2). |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
366 |
destIndex := 1. |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
367 |
|
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
368 |
contentsArray2 := aSortedCollection contentsArray. |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
369 |
srcIndex2 := aSortedCollection firstIndex. |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
370 |
last2 := srcIndex2 + n2 -1. |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
371 |
|
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
372 |
srcIndex1 := firstIndex. |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
373 |
last1 := srcIndex1 + n1 -1. |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
374 |
|
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
375 |
(srcIndex1 <= last1 and:[srcIndex2 <= last2]) ifTrue:[ |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
376 |
el1 := contentsArray at:srcIndex1. |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
377 |
el2 := contentsArray2 at:srcIndex2. |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
378 |
end1Reached := end2Reached := false. |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
379 |
|
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
380 |
[end1Reached or:[end2Reached]] whileFalse:[ |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
381 |
(sortBlock value:el1 value:el2) ifTrue:[ |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
382 |
"/ el1 to come before el2 |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
383 |
newContentsArray at:destIndex put:el1. destIndex := destIndex + 1. |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
384 |
srcIndex1 := srcIndex1 + 1. |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
385 |
srcIndex1 <= last1 ifTrue:[ |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
386 |
el1 := contentsArray at:srcIndex1. |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
387 |
] ifFalse:[ |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
388 |
end1Reached := true |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
389 |
] |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
390 |
] ifFalse:[ |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
391 |
"/ el2 to come before el1 |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
392 |
newContentsArray at:destIndex put:el2. destIndex := destIndex + 1. |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
393 |
srcIndex2 := srcIndex2 + 1. |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
394 |
srcIndex2 <= last2 ifTrue:[ |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
395 |
el2 := contentsArray2 at:srcIndex2. |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
396 |
] ifFalse:[ |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
397 |
end2Reached := true |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
398 |
] |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
399 |
] |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
400 |
] |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
401 |
]. |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
402 |
|
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
403 |
"/ 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
|
404 |
(srcIndex1 <= last1) ifTrue:[ |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
405 |
newContentsArray |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
406 |
replaceFrom:destIndex |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
407 |
with:contentsArray |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
408 |
startingAt:srcIndex1 |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
409 |
]. |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
410 |
|
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
411 |
"/ 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
|
412 |
(srcIndex2 <= last2) ifTrue:[ |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
413 |
newContentsArray |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
414 |
replaceFrom:destIndex |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
415 |
with:contentsArray2 |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
416 |
startingAt:srcIndex2 |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
417 |
]. |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
418 |
|
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
419 |
firstIndex := 1. |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
420 |
lastIndex := n1 + n2. |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
421 |
contentsArray := newContentsArray |
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 |
" |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
424 |
#(7 3 9 10 99) asSortedCollection |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
425 |
mergeSorted:#(77 0 1 16 5) asSortedCollection |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
426 |
|
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
427 |
#(7 3 9 10 99) asSortedCollection |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
428 |
mergeSorted:#(77 0 3 1 98 99 16 5) asSortedCollection |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
429 |
|
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
430 |
#() asSortedCollection |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
431 |
mergeSorted:#(77 0 3 1 98 99 16 5) asSortedCollection |
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 |
#(7 3 9 10 99) asSortedCollection |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
434 |
mergeSorted:#() asSortedCollection |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
435 |
" |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
436 |
|
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
437 |
"Modified: 13.4.1996 / 12:48:34 / cg" |
607 | 438 |
! ! |
439 |
||
440 |
!SortedCollection methodsFor:'converting'! |
|
441 |
||
442 |
asSortedCollection |
|
443 |
"return the receiver as a sorted collection" |
|
444 |
||
445 |
"could be an instance of a subclass..." |
|
446 |
self class == SortedCollection ifTrue:[ |
|
447 |
^ self |
|
1 | 448 |
]. |
607 | 449 |
^ super asSortedCollection |
1 | 450 |
! ! |
451 |
||
95 | 452 |
!SortedCollection methodsFor:'copying'! |
453 |
||
607 | 454 |
copyEmpty:size |
455 |
"return a copy of the receiver with no elements, but |
|
456 |
the same size. This method has been be redefined to |
|
457 |
preserve the sortBlock." |
|
458 |
||
459 |
^ (self species new:size) sortBlock:sortBlock |
|
460 |
! |
|
461 |
||
159 | 462 |
postCopyFrom:anOriginal |
463 |
"sent after a copy or when a new collection species has been created. |
|
95 | 464 |
The new collection should have the same sortBlock as the original." |
465 |
||
159 | 466 |
sortBlock := anOriginal sortBlock |
95 | 467 |
|
468 |
" |
|
469 |
#(4 7 1 99 -1 17) asSortedCollection inspect |
|
159 | 470 |
#(4 7 1 99 -1 17) asSortedCollection copy inspect |
95 | 471 |
(#(4 7 1 99 -1 17) asSortedCollection sortBlock:[:a :b | a > b]) inspect |
159 | 472 |
(#(4 7 1 99 -1 17) asSortedCollection sortBlock:[:a :b | a > b]) copy inspect |
473 |
(#(4 7 1 99 -1 17) asSortedCollection select:[:e| e even]) inspect |
|
95 | 474 |
" |
475 |
! ! |
|
476 |
||
216 | 477 |
!SortedCollection methodsFor:'enumerating'! |
184 | 478 |
|
479 |
collect:aBlock |
|
480 |
"evaluate the argument, aBlock for every element in the collection |
|
481 |
and return a collection of the results. Redefined to return an OrderedCollection; |
|
482 |
see X3J20 spec. (SortedCollection>>collect: should return an OrderedCollection)" |
|
483 |
||
484 |
|newCollection |
|
485 |
start "{ Class:SmallInteger }" |
|
486 |
stop "{ Class:SmallInteger }" | |
|
487 |
||
488 |
newCollection := OrderedCollection new:(self size). |
|
489 |
stop := lastIndex. |
|
490 |
start := firstIndex. |
|
491 |
start to:stop do:[:index | |
|
492 |
newCollection add:(aBlock value:(contentsArray at:index)). |
|
493 |
]. |
|
494 |
^ newCollection |
|
495 |
! ! |
|
496 |
||
607 | 497 |
!SortedCollection methodsFor:'instance protocol'! |
498 |
||
499 |
sortBlock |
|
500 |
"return the block used for sorting" |
|
501 |
||
502 |
^ sortBlock |
|
503 |
! |
|
504 |
||
505 |
sortBlock:aSortBlock |
|
506 |
"change the sort criteria for a sorted collection, resort the elements of |
|
507 |
the collection, and return the receiver. The argument, aSortBlock must |
|
508 |
be a two-argument block which returns true if its arg1 has to come before |
|
509 |
its arg2 in the collection." |
|
510 |
||
511 |
sortBlock := aSortBlock. |
|
512 |
lastIndex > firstIndex ifTrue:[ |
|
513 |
contentsArray quickSortFrom:firstIndex to:lastIndex sortBlock:aSortBlock |
|
514 |
] |
|
515 |
||
516 |
" |
|
517 |
#(9 8 7 6 5 4 3) asSortedCollection |
|
518 |
#(9 8 7 6 5 4 3) asSortedCollection sortBlock:[:a : b | a < b] |
|
519 |
#(9 8 7 6 5 4 3) asSortedCollection sortBlock:[:a : b | a > b] |
|
520 |
#($f $G $z $Y $o $H) asSortedCollection |
|
521 |
#($f $G $z $Y $o $H) asSortedCollection sortBlock:[:a : b | a asUppercase < b asUppercase] |
|
522 |
" |
|
523 |
! ! |
|
524 |
||
525 |
!SortedCollection methodsFor:'private'! |
|
526 |
||
527 |
indexForInserting:anObject |
|
1162
3995101dfa44
changed for new #makeRoomAtIndex:; added comments
Claus Gittinger <cg@exept.de>
parents:
1156
diff
changeset
|
528 |
"search the index at which to insert anObject. |
3995101dfa44
changed for new #makeRoomAtIndex:; added comments
Claus Gittinger <cg@exept.de>
parents:
1156
diff
changeset
|
529 |
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
|
530 |
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
|
531 |
Uses a binarySearch since we can depend on the elements being on sorted order. |
607 | 532 |
The returned index is a physical one, for accessing contentsArray." |
533 |
||
534 |
|low "{ Class: SmallInteger}" |
|
535 |
high "{ Class: SmallInteger}" |
|
536 |
middle "{ Class: SmallInteger}" |
|
537 |
element| |
|
538 |
||
539 |
" |
|
1162
3995101dfa44
changed for new #makeRoomAtIndex:; added comments
Claus Gittinger <cg@exept.de>
parents:
1156
diff
changeset
|
540 |
we can of course use a binary search - since the elements are sorted |
607 | 541 |
" |
542 |
low := firstIndex. |
|
543 |
high := lastIndex. |
|
544 |
[low <= high] whileTrue:[ |
|
1162
3995101dfa44
changed for new #makeRoomAtIndex:; added comments
Claus Gittinger <cg@exept.de>
parents:
1156
diff
changeset
|
545 |
middle := (low + high) // 2. |
3995101dfa44
changed for new #makeRoomAtIndex:; added comments
Claus Gittinger <cg@exept.de>
parents:
1156
diff
changeset
|
546 |
element := contentsArray at:middle. |
3995101dfa44
changed for new #makeRoomAtIndex:; added comments
Claus Gittinger <cg@exept.de>
parents:
1156
diff
changeset
|
547 |
(sortBlock value:element value:anObject) ifTrue:[ |
3995101dfa44
changed for new #makeRoomAtIndex:; added comments
Claus Gittinger <cg@exept.de>
parents:
1156
diff
changeset
|
548 |
"middleelement is smaller than object" |
3995101dfa44
changed for new #makeRoomAtIndex:; added comments
Claus Gittinger <cg@exept.de>
parents:
1156
diff
changeset
|
549 |
low := middle + 1 |
3995101dfa44
changed for new #makeRoomAtIndex:; added comments
Claus Gittinger <cg@exept.de>
parents:
1156
diff
changeset
|
550 |
] ifFalse:[ |
3995101dfa44
changed for new #makeRoomAtIndex:; added comments
Claus Gittinger <cg@exept.de>
parents:
1156
diff
changeset
|
551 |
high := middle - 1 |
3995101dfa44
changed for new #makeRoomAtIndex:; added comments
Claus Gittinger <cg@exept.de>
parents:
1156
diff
changeset
|
552 |
] |
607 | 553 |
]. |
554 |
^ low |
|
555 |
||
556 |
" |
|
557 |
#(1 2 3 4 7 99 1313 981989 898989898) asSortedCollection indexForInserting:50 |
|
558 |
#(1.0 2.0 3 4 7 49.0 51.0 99 1313 981989 898989898) asSortedCollection indexForInserting:50 |
|
559 |
" |
|
560 |
||
1162
3995101dfa44
changed for new #makeRoomAtIndex:; added comments
Claus Gittinger <cg@exept.de>
parents:
1156
diff
changeset
|
561 |
"Modified: 12.4.1996 / 13:22:03 / cg" |
607 | 562 |
! |
563 |
||
564 |
setSortBlock: aSortBlock |
|
565 |
"set the sortblock without resorting - private only" |
|
566 |
||
567 |
sortBlock := aSortBlock |
|
568 |
! ! |
|
569 |
||
1173
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
570 |
!SortedCollection methodsFor:'queries'! |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
571 |
|
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
572 |
isSorted |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
573 |
^ true |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
574 |
|
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
575 |
"Created: 13.4.1996 / 12:36:29 / cg" |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
576 |
! ! |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
577 |
|
607 | 578 |
!SortedCollection methodsFor:'searching'! |
579 |
||
580 |
after:anObject ifAbsent:exceptionBlock |
|
581 |
"return the element after the argument, anObject; or nil if there is none. |
|
582 |
If anObject is contained multiple times in the collection, return the |
|
583 |
the first element which is non-equal to anObject. |
|
584 |
If the receiver does not contain anObject, return the result from evaluating |
|
585 |
exceptionBlock." |
|
586 |
||
1162
3995101dfa44
changed for new #makeRoomAtIndex:; added comments
Claus Gittinger <cg@exept.de>
parents:
1156
diff
changeset
|
587 |
|index "{ Class: SmallInteger }" |
3995101dfa44
changed for new #makeRoomAtIndex:; added comments
Claus Gittinger <cg@exept.de>
parents:
1156
diff
changeset
|
588 |
last "{ Class: SmallInteger }"| |
607 | 589 |
|
590 |
index := self indexForInserting:anObject. |
|
591 |
((index < firstIndex) |
|
592 |
or:[(contentsArray at:index) ~= anObject]) ifTrue:[^ exceptionBlock value]. |
|
593 |
||
594 |
"skip multiple occurences of the same ..." |
|
595 |
||
1162
3995101dfa44
changed for new #makeRoomAtIndex:; added comments
Claus Gittinger <cg@exept.de>
parents:
1156
diff
changeset
|
596 |
last := lastIndex. |
3995101dfa44
changed for new #makeRoomAtIndex:; added comments
Claus Gittinger <cg@exept.de>
parents:
1156
diff
changeset
|
597 |
[(index <= last) and:[(contentsArray at:index) = anObject]] whileTrue:[ |
3995101dfa44
changed for new #makeRoomAtIndex:; added comments
Claus Gittinger <cg@exept.de>
parents:
1156
diff
changeset
|
598 |
index := index + 1 |
607 | 599 |
]. |
1162
3995101dfa44
changed for new #makeRoomAtIndex:; added comments
Claus Gittinger <cg@exept.de>
parents:
1156
diff
changeset
|
600 |
(index > last) ifTrue:[^ nil]. |
607 | 601 |
^ contentsArray at:index |
1162
3995101dfa44
changed for new #makeRoomAtIndex:; added comments
Claus Gittinger <cg@exept.de>
parents:
1156
diff
changeset
|
602 |
|
3995101dfa44
changed for new #makeRoomAtIndex:; added comments
Claus Gittinger <cg@exept.de>
parents:
1156
diff
changeset
|
603 |
"Modified: 12.4.1996 / 13:20:33 / cg" |
607 | 604 |
! |
605 |
||
606 |
before:anObject ifAbsent:exceptionBlock |
|
607 |
"return the element before the argument, anObject; or nil if there is none. |
|
608 |
If the receiver does not contain anObject, return the result from evaluating |
|
609 |
exceptionBlock." |
|
610 |
||
611 |
|index "{ Class: SmallInteger }"| |
|
612 |
||
613 |
index := self indexForInserting:anObject. |
|
614 |
((index <= firstIndex) |
|
615 |
or:[(contentsArray at:index) ~= anObject]) ifTrue:[^ exceptionBlock value]. |
|
616 |
||
617 |
^ contentsArray at:index - 1 |
|
618 |
||
619 |
" |
|
620 |
#(7 3 9 10 99) asSortedCollection before:50 |
|
621 |
#(7 3 9 10 99) asSortedCollection before:1 |
|
622 |
#(7 3 9 10 99) asSortedCollection before:10 |
|
623 |
#(7 3 9 10 99) asSortedCollection before:7 |
|
624 |
#(7 3 9 10 99) asSortedCollection before:99 |
|
625 |
#(7 10 3 10 9 10 10 99) asSortedCollection before:9 |
|
626 |
#(7 10 3 10 9 10 10 99) asSortedCollection before:10 |
|
627 |
" |
|
628 |
! ! |
|
629 |
||
1 | 630 |
!SortedCollection methodsFor:'testing'! |
631 |
||
632 |
includes:anObject |
|
633 |
"return true, if the argument, anObject is in the collection. |
|
62 | 634 |
Redefined, since due to being sorted, the inclusion check can |
2 | 635 |
be done with log-n compares i.e. much faster." |
1 | 636 |
|
32 | 637 |
|index "{ Class: SmallInteger }"| |
1 | 638 |
|
77 | 639 |
index := self indexForInserting:anObject. |
32 | 640 |
((index < firstIndex) or:[index > lastIndex]) ifTrue:[^ false]. |
641 |
^ (contentsArray at:index) = anObject |
|
2 | 642 |
|
77 | 643 |
" |
644 |
#(7 3 9 10 99) asSortedCollection includes:50 |
|
645 |
#(7 3 9 10 99) asSortedCollection includes:10 |
|
646 |
" |
|
1 | 647 |
! |
648 |
||
649 |
occurrencesOf:anObject |
|
650 |
"return how many times the argument, anObject is in the collection. |
|
62 | 651 |
Redefined, since due to being sorted, the range of checked objects |
2 | 652 |
can be limited i.e. it can be done much faster." |
1 | 653 |
|
654 |
|index "{ Class: SmallInteger }" |
|
1162
3995101dfa44
changed for new #makeRoomAtIndex:; added comments
Claus Gittinger <cg@exept.de>
parents:
1156
diff
changeset
|
655 |
tally "{ Class: SmallInteger }" |
3995101dfa44
changed for new #makeRoomAtIndex:; added comments
Claus Gittinger <cg@exept.de>
parents:
1156
diff
changeset
|
656 |
last "{ Class: SmallInteger }" | |
1 | 657 |
|
77 | 658 |
index := self indexForInserting:anObject. |
1162
3995101dfa44
changed for new #makeRoomAtIndex:; added comments
Claus Gittinger <cg@exept.de>
parents:
1156
diff
changeset
|
659 |
last := lastIndex. |
3995101dfa44
changed for new #makeRoomAtIndex:; added comments
Claus Gittinger <cg@exept.de>
parents:
1156
diff
changeset
|
660 |
((index < firstIndex) or:[index > last]) ifTrue:[^ 0]. |
3995101dfa44
changed for new #makeRoomAtIndex:; added comments
Claus Gittinger <cg@exept.de>
parents:
1156
diff
changeset
|
661 |
|
3995101dfa44
changed for new #makeRoomAtIndex:; added comments
Claus Gittinger <cg@exept.de>
parents:
1156
diff
changeset
|
662 |
"/ there may be multiple of them; count 'em |
32 | 663 |
|
1 | 664 |
tally := 0. |
1162
3995101dfa44
changed for new #makeRoomAtIndex:; added comments
Claus Gittinger <cg@exept.de>
parents:
1156
diff
changeset
|
665 |
[(index <= last) and:[(contentsArray at:index) = anObject]] whileTrue:[ |
3995101dfa44
changed for new #makeRoomAtIndex:; added comments
Claus Gittinger <cg@exept.de>
parents:
1156
diff
changeset
|
666 |
tally := tally + 1. |
3995101dfa44
changed for new #makeRoomAtIndex:; added comments
Claus Gittinger <cg@exept.de>
parents:
1156
diff
changeset
|
667 |
index := index + 1 |
1 | 668 |
]. |
669 |
^ tally |
|
2 | 670 |
|
77 | 671 |
" |
672 |
#(7 3 9 10 99) asSortedCollection occurrencesOf:50 |
|
673 |
#(7 3 9 10 99) asSortedCollection occurrencesOf:10 |
|
674 |
#(7 10 3 10 9 10 10 99) asSortedCollection occurrencesOf:10 |
|
675 |
" |
|
1162
3995101dfa44
changed for new #makeRoomAtIndex:; added comments
Claus Gittinger <cg@exept.de>
parents:
1156
diff
changeset
|
676 |
|
3995101dfa44
changed for new #makeRoomAtIndex:; added comments
Claus Gittinger <cg@exept.de>
parents:
1156
diff
changeset
|
677 |
"Modified: 12.4.1996 / 18:48:40 / cg" |
77 | 678 |
! ! |
679 |
||
629 | 680 |
!SortedCollection class methodsFor:'documentation'! |
681 |
||
682 |
version |
|
2248 | 683 |
^ '$Header: /cvs/stx/stx/libbasic/Attic/SortColl.st,v 1.34 1997-01-23 16:41:10 ca Exp $' |
629 | 684 |
! ! |
607 | 685 |
SortedCollection initialize! |