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