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