author | Jan Vrany <jan.vrany@fit.cvut.cz> |
Mon, 15 Jun 2009 20:55:05 +0100 | |
branch | jv |
changeset 17711 | 39faaaf888b4 |
parent 11478 | 0cdba0734b9f |
child 17732 | a1892eeca6c0 |
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 |
||
11478
0cdba0734b9f
binary search algorithm made publicly usable
Claus Gittinger <cg@exept.de>
parents:
11286
diff
changeset
|
327 |
!SortedCollection class methodsFor:'helpers'! |
0cdba0734b9f
binary search algorithm made publicly usable
Claus Gittinger <cg@exept.de>
parents:
11286
diff
changeset
|
328 |
|
0cdba0734b9f
binary search algorithm made publicly usable
Claus Gittinger <cg@exept.de>
parents:
11286
diff
changeset
|
329 |
binarySearch:aSortedArray for:anObject |
0cdba0734b9f
binary search algorithm made publicly usable
Claus Gittinger <cg@exept.de>
parents:
11286
diff
changeset
|
330 |
"search for an existing element; return true if found. |
0cdba0734b9f
binary search algorithm made publicly usable
Claus Gittinger <cg@exept.de>
parents:
11286
diff
changeset
|
331 |
by checking if the element at the returned index is the one we look for. |
0cdba0734b9f
binary search algorithm made publicly usable
Claus Gittinger <cg@exept.de>
parents:
11286
diff
changeset
|
332 |
Uses a binarySearch since we can depend on the elements being on sorted order. |
0cdba0734b9f
binary search algorithm made publicly usable
Claus Gittinger <cg@exept.de>
parents:
11286
diff
changeset
|
333 |
The returned index is a physical one, for accessing contentsArray. |
0cdba0734b9f
binary search algorithm made publicly usable
Claus Gittinger <cg@exept.de>
parents:
11286
diff
changeset
|
334 |
This is only to be used for arrays which are known to be sorted." |
0cdba0734b9f
binary search algorithm made publicly usable
Claus Gittinger <cg@exept.de>
parents:
11286
diff
changeset
|
335 |
|
0cdba0734b9f
binary search algorithm made publicly usable
Claus Gittinger <cg@exept.de>
parents:
11286
diff
changeset
|
336 |
|idx| |
0cdba0734b9f
binary search algorithm made publicly usable
Claus Gittinger <cg@exept.de>
parents:
11286
diff
changeset
|
337 |
|
0cdba0734b9f
binary search algorithm made publicly usable
Claus Gittinger <cg@exept.de>
parents:
11286
diff
changeset
|
338 |
idx := self |
0cdba0734b9f
binary search algorithm made publicly usable
Claus Gittinger <cg@exept.de>
parents:
11286
diff
changeset
|
339 |
binarySearch:aSortedArray |
0cdba0734b9f
binary search algorithm made publicly usable
Claus Gittinger <cg@exept.de>
parents:
11286
diff
changeset
|
340 |
forIndexOf:anObject. |
0cdba0734b9f
binary search algorithm made publicly usable
Claus Gittinger <cg@exept.de>
parents:
11286
diff
changeset
|
341 |
^ idx <= aSortedArray size |
0cdba0734b9f
binary search algorithm made publicly usable
Claus Gittinger <cg@exept.de>
parents:
11286
diff
changeset
|
342 |
and:[ (aSortedArray at:idx) = anObject ]. |
0cdba0734b9f
binary search algorithm made publicly usable
Claus Gittinger <cg@exept.de>
parents:
11286
diff
changeset
|
343 |
|
0cdba0734b9f
binary search algorithm made publicly usable
Claus Gittinger <cg@exept.de>
parents:
11286
diff
changeset
|
344 |
" |
0cdba0734b9f
binary search algorithm made publicly usable
Claus Gittinger <cg@exept.de>
parents:
11286
diff
changeset
|
345 |
SortedCollection |
0cdba0734b9f
binary search algorithm made publicly usable
Claus Gittinger <cg@exept.de>
parents:
11286
diff
changeset
|
346 |
binarySearch:#(1 2 3 4 7 99 1313 981989 898989898) |
0cdba0734b9f
binary search algorithm made publicly usable
Claus Gittinger <cg@exept.de>
parents:
11286
diff
changeset
|
347 |
for:898989898 |
0cdba0734b9f
binary search algorithm made publicly usable
Claus Gittinger <cg@exept.de>
parents:
11286
diff
changeset
|
348 |
" |
0cdba0734b9f
binary search algorithm made publicly usable
Claus Gittinger <cg@exept.de>
parents:
11286
diff
changeset
|
349 |
! |
0cdba0734b9f
binary search algorithm made publicly usable
Claus Gittinger <cg@exept.de>
parents:
11286
diff
changeset
|
350 |
|
0cdba0734b9f
binary search algorithm made publicly usable
Claus Gittinger <cg@exept.de>
parents:
11286
diff
changeset
|
351 |
binarySearch:aSortedArray forIndexOf:anObject |
0cdba0734b9f
binary search algorithm made publicly usable
Claus Gittinger <cg@exept.de>
parents:
11286
diff
changeset
|
352 |
"search the index at which to insert anObject. |
0cdba0734b9f
binary search algorithm made publicly usable
Claus Gittinger <cg@exept.de>
parents:
11286
diff
changeset
|
353 |
Can also be used to search for an existing element |
0cdba0734b9f
binary search algorithm made publicly usable
Claus Gittinger <cg@exept.de>
parents:
11286
diff
changeset
|
354 |
by checking if the element at the returned index is the one we look for. |
0cdba0734b9f
binary search algorithm made publicly usable
Claus Gittinger <cg@exept.de>
parents:
11286
diff
changeset
|
355 |
Uses a binarySearch since we can depend on the elements being on sorted order. |
0cdba0734b9f
binary search algorithm made publicly usable
Claus Gittinger <cg@exept.de>
parents:
11286
diff
changeset
|
356 |
The returned index is a physical one, for accessing contentsArray. |
0cdba0734b9f
binary search algorithm made publicly usable
Claus Gittinger <cg@exept.de>
parents:
11286
diff
changeset
|
357 |
This is only to be used for arrays which are known to be sorted." |
0cdba0734b9f
binary search algorithm made publicly usable
Claus Gittinger <cg@exept.de>
parents:
11286
diff
changeset
|
358 |
|
0cdba0734b9f
binary search algorithm made publicly usable
Claus Gittinger <cg@exept.de>
parents:
11286
diff
changeset
|
359 |
^ self |
0cdba0734b9f
binary search algorithm made publicly usable
Claus Gittinger <cg@exept.de>
parents:
11286
diff
changeset
|
360 |
binarySearch:aSortedArray |
0cdba0734b9f
binary search algorithm made publicly usable
Claus Gittinger <cg@exept.de>
parents:
11286
diff
changeset
|
361 |
from:1 to:(aSortedArray size) |
0cdba0734b9f
binary search algorithm made publicly usable
Claus Gittinger <cg@exept.de>
parents:
11286
diff
changeset
|
362 |
forIndexOf:anObject |
0cdba0734b9f
binary search algorithm made publicly usable
Claus Gittinger <cg@exept.de>
parents:
11286
diff
changeset
|
363 |
usingSortBlock:[:a :b | a < b ] |
0cdba0734b9f
binary search algorithm made publicly usable
Claus Gittinger <cg@exept.de>
parents:
11286
diff
changeset
|
364 |
|
0cdba0734b9f
binary search algorithm made publicly usable
Claus Gittinger <cg@exept.de>
parents:
11286
diff
changeset
|
365 |
" |
0cdba0734b9f
binary search algorithm made publicly usable
Claus Gittinger <cg@exept.de>
parents:
11286
diff
changeset
|
366 |
SortedCollection |
0cdba0734b9f
binary search algorithm made publicly usable
Claus Gittinger <cg@exept.de>
parents:
11286
diff
changeset
|
367 |
binarySearch:#(1 2 3 4 7 99 1313 981989 898989898) |
0cdba0734b9f
binary search algorithm made publicly usable
Claus Gittinger <cg@exept.de>
parents:
11286
diff
changeset
|
368 |
from:1 to:9 |
0cdba0734b9f
binary search algorithm made publicly usable
Claus Gittinger <cg@exept.de>
parents:
11286
diff
changeset
|
369 |
forIndexOf:99 |
0cdba0734b9f
binary search algorithm made publicly usable
Claus Gittinger <cg@exept.de>
parents:
11286
diff
changeset
|
370 |
usingSortBlock:[:a :b | a < b ] |
0cdba0734b9f
binary search algorithm made publicly usable
Claus Gittinger <cg@exept.de>
parents:
11286
diff
changeset
|
371 |
" |
0cdba0734b9f
binary search algorithm made publicly usable
Claus Gittinger <cg@exept.de>
parents:
11286
diff
changeset
|
372 |
|
0cdba0734b9f
binary search algorithm made publicly usable
Claus Gittinger <cg@exept.de>
parents:
11286
diff
changeset
|
373 |
"Modified: 12.4.1996 / 13:22:03 / cg" |
0cdba0734b9f
binary search algorithm made publicly usable
Claus Gittinger <cg@exept.de>
parents:
11286
diff
changeset
|
374 |
! |
0cdba0734b9f
binary search algorithm made publicly usable
Claus Gittinger <cg@exept.de>
parents:
11286
diff
changeset
|
375 |
|
0cdba0734b9f
binary search algorithm made publicly usable
Claus Gittinger <cg@exept.de>
parents:
11286
diff
changeset
|
376 |
binarySearch:aSortedArray from:firstIndex to:lastIndex forIndexOf:anObject usingSortBlock:sortBlock |
0cdba0734b9f
binary search algorithm made publicly usable
Claus Gittinger <cg@exept.de>
parents:
11286
diff
changeset
|
377 |
"search the index at which to insert anObject. |
0cdba0734b9f
binary search algorithm made publicly usable
Claus Gittinger <cg@exept.de>
parents:
11286
diff
changeset
|
378 |
Can also be used to search for an existing element |
0cdba0734b9f
binary search algorithm made publicly usable
Claus Gittinger <cg@exept.de>
parents:
11286
diff
changeset
|
379 |
by checking if the element at the returned index is the one we look for. |
0cdba0734b9f
binary search algorithm made publicly usable
Claus Gittinger <cg@exept.de>
parents:
11286
diff
changeset
|
380 |
Uses a binarySearch since we can depend on the elements being on sorted order. |
0cdba0734b9f
binary search algorithm made publicly usable
Claus Gittinger <cg@exept.de>
parents:
11286
diff
changeset
|
381 |
The returned index is a physical one, for accessing contentsArray. |
0cdba0734b9f
binary search algorithm made publicly usable
Claus Gittinger <cg@exept.de>
parents:
11286
diff
changeset
|
382 |
This is only to be used for arrays which are known to be sorted." |
0cdba0734b9f
binary search algorithm made publicly usable
Claus Gittinger <cg@exept.de>
parents:
11286
diff
changeset
|
383 |
|
0cdba0734b9f
binary search algorithm made publicly usable
Claus Gittinger <cg@exept.de>
parents:
11286
diff
changeset
|
384 |
|low "{ Class: SmallInteger}" |
0cdba0734b9f
binary search algorithm made publicly usable
Claus Gittinger <cg@exept.de>
parents:
11286
diff
changeset
|
385 |
high "{ Class: SmallInteger}" |
0cdba0734b9f
binary search algorithm made publicly usable
Claus Gittinger <cg@exept.de>
parents:
11286
diff
changeset
|
386 |
middle "{ Class: SmallInteger}" |
0cdba0734b9f
binary search algorithm made publicly usable
Claus Gittinger <cg@exept.de>
parents:
11286
diff
changeset
|
387 |
element| |
0cdba0734b9f
binary search algorithm made publicly usable
Claus Gittinger <cg@exept.de>
parents:
11286
diff
changeset
|
388 |
|
0cdba0734b9f
binary search algorithm made publicly usable
Claus Gittinger <cg@exept.de>
parents:
11286
diff
changeset
|
389 |
" |
0cdba0734b9f
binary search algorithm made publicly usable
Claus Gittinger <cg@exept.de>
parents:
11286
diff
changeset
|
390 |
we can of course use a binary search - since the elements are sorted |
0cdba0734b9f
binary search algorithm made publicly usable
Claus Gittinger <cg@exept.de>
parents:
11286
diff
changeset
|
391 |
" |
0cdba0734b9f
binary search algorithm made publicly usable
Claus Gittinger <cg@exept.de>
parents:
11286
diff
changeset
|
392 |
low := firstIndex. |
0cdba0734b9f
binary search algorithm made publicly usable
Claus Gittinger <cg@exept.de>
parents:
11286
diff
changeset
|
393 |
high := lastIndex. |
0cdba0734b9f
binary search algorithm made publicly usable
Claus Gittinger <cg@exept.de>
parents:
11286
diff
changeset
|
394 |
[low > high] whileFalse:[ |
0cdba0734b9f
binary search algorithm made publicly usable
Claus Gittinger <cg@exept.de>
parents:
11286
diff
changeset
|
395 |
middle := (low + high) // 2. |
0cdba0734b9f
binary search algorithm made publicly usable
Claus Gittinger <cg@exept.de>
parents:
11286
diff
changeset
|
396 |
element := aSortedArray at:middle. |
0cdba0734b9f
binary search algorithm made publicly usable
Claus Gittinger <cg@exept.de>
parents:
11286
diff
changeset
|
397 |
(sortBlock value:element value:anObject) ifTrue:[ |
0cdba0734b9f
binary search algorithm made publicly usable
Claus Gittinger <cg@exept.de>
parents:
11286
diff
changeset
|
398 |
"middleelement is smaller than object" |
0cdba0734b9f
binary search algorithm made publicly usable
Claus Gittinger <cg@exept.de>
parents:
11286
diff
changeset
|
399 |
low := middle + 1 |
0cdba0734b9f
binary search algorithm made publicly usable
Claus Gittinger <cg@exept.de>
parents:
11286
diff
changeset
|
400 |
] ifFalse:[ |
0cdba0734b9f
binary search algorithm made publicly usable
Claus Gittinger <cg@exept.de>
parents:
11286
diff
changeset
|
401 |
high := middle - 1 |
0cdba0734b9f
binary search algorithm made publicly usable
Claus Gittinger <cg@exept.de>
parents:
11286
diff
changeset
|
402 |
] |
0cdba0734b9f
binary search algorithm made publicly usable
Claus Gittinger <cg@exept.de>
parents:
11286
diff
changeset
|
403 |
]. |
0cdba0734b9f
binary search algorithm made publicly usable
Claus Gittinger <cg@exept.de>
parents:
11286
diff
changeset
|
404 |
^ low |
0cdba0734b9f
binary search algorithm made publicly usable
Claus Gittinger <cg@exept.de>
parents:
11286
diff
changeset
|
405 |
|
0cdba0734b9f
binary search algorithm made publicly usable
Claus Gittinger <cg@exept.de>
parents:
11286
diff
changeset
|
406 |
" |
0cdba0734b9f
binary search algorithm made publicly usable
Claus Gittinger <cg@exept.de>
parents:
11286
diff
changeset
|
407 |
SortedCollection |
0cdba0734b9f
binary search algorithm made publicly usable
Claus Gittinger <cg@exept.de>
parents:
11286
diff
changeset
|
408 |
binarySearch:#(1 2 3 4 7 99 1313 981989 898989898) |
0cdba0734b9f
binary search algorithm made publicly usable
Claus Gittinger <cg@exept.de>
parents:
11286
diff
changeset
|
409 |
from:1 to:9 |
0cdba0734b9f
binary search algorithm made publicly usable
Claus Gittinger <cg@exept.de>
parents:
11286
diff
changeset
|
410 |
forIndexOf:99 |
0cdba0734b9f
binary search algorithm made publicly usable
Claus Gittinger <cg@exept.de>
parents:
11286
diff
changeset
|
411 |
usingSortBlock:[:a :b | a < b ] |
0cdba0734b9f
binary search algorithm made publicly usable
Claus Gittinger <cg@exept.de>
parents:
11286
diff
changeset
|
412 |
" |
0cdba0734b9f
binary search algorithm made publicly usable
Claus Gittinger <cg@exept.de>
parents:
11286
diff
changeset
|
413 |
|
0cdba0734b9f
binary search algorithm made publicly usable
Claus Gittinger <cg@exept.de>
parents:
11286
diff
changeset
|
414 |
"Modified: 12.4.1996 / 13:22:03 / cg" |
0cdba0734b9f
binary search algorithm made publicly usable
Claus Gittinger <cg@exept.de>
parents:
11286
diff
changeset
|
415 |
! ! |
0cdba0734b9f
binary search algorithm made publicly usable
Claus Gittinger <cg@exept.de>
parents:
11286
diff
changeset
|
416 |
|
9073 | 417 |
!SortedCollection methodsFor:'accessing'! |
418 |
||
9918
1947f22681ef
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9917
diff
changeset
|
419 |
largest:n |
1947f22681ef
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9917
diff
changeset
|
420 |
"return the n largest elements" |
1947f22681ef
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9917
diff
changeset
|
421 |
|
1947f22681ef
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9917
diff
changeset
|
422 |
sortBlock == DefaultSortBlock ifTrue:[ |
9920 | 423 |
^ self copyFrom:(self size-n+1) |
9918
1947f22681ef
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9917
diff
changeset
|
424 |
]. |
1947f22681ef
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9917
diff
changeset
|
425 |
^ super largest:n |
1947f22681ef
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9917
diff
changeset
|
426 |
|
1947f22681ef
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9917
diff
changeset
|
427 |
" |
1947f22681ef
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9917
diff
changeset
|
428 |
#(10 35 20 45 30 5) asSortedCollection largest:3 |
1947f22681ef
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9917
diff
changeset
|
429 |
" |
1947f22681ef
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9917
diff
changeset
|
430 |
! |
1947f22681ef
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9917
diff
changeset
|
431 |
|
9916
5c76098e0377
min and max are easy with sorted collections.
Claus Gittinger <cg@exept.de>
parents:
9074
diff
changeset
|
432 |
max |
5c76098e0377
min and max are easy with sorted collections.
Claus Gittinger <cg@exept.de>
parents:
9074
diff
changeset
|
433 |
"Return the largest element." |
5c76098e0377
min and max are easy with sorted collections.
Claus Gittinger <cg@exept.de>
parents:
9074
diff
changeset
|
434 |
|
9917
278547f0d52e
... but only with the default sort block.
Claus Gittinger <cg@exept.de>
parents:
9916
diff
changeset
|
435 |
sortBlock == DefaultSortBlock ifTrue:[ |
278547f0d52e
... but only with the default sort block.
Claus Gittinger <cg@exept.de>
parents:
9916
diff
changeset
|
436 |
^ self last |
278547f0d52e
... but only with the default sort block.
Claus Gittinger <cg@exept.de>
parents:
9916
diff
changeset
|
437 |
]. |
278547f0d52e
... but only with the default sort block.
Claus Gittinger <cg@exept.de>
parents:
9916
diff
changeset
|
438 |
^ super max |
9916
5c76098e0377
min and max are easy with sorted collections.
Claus Gittinger <cg@exept.de>
parents:
9074
diff
changeset
|
439 |
|
5c76098e0377
min and max are easy with sorted collections.
Claus Gittinger <cg@exept.de>
parents:
9074
diff
changeset
|
440 |
" |
5c76098e0377
min and max are easy with sorted collections.
Claus Gittinger <cg@exept.de>
parents:
9074
diff
changeset
|
441 |
#(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
|
442 |
" |
5c76098e0377
min and max are easy with sorted collections.
Claus Gittinger <cg@exept.de>
parents:
9074
diff
changeset
|
443 |
! |
5c76098e0377
min and max are easy with sorted collections.
Claus Gittinger <cg@exept.de>
parents:
9074
diff
changeset
|
444 |
|
9073 | 445 |
median |
446 |
"Return the middle element, or as close as we can get." |
|
447 |
||
448 |
^ self at:(self size + 1 // 2) |
|
449 |
||
450 |
" |
|
451 |
#(10 35 20 45 30 5) asSortedCollection median |
|
452 |
#(10 35 20 45 30 5) median |
|
453 |
" |
|
9916
5c76098e0377
min and max are easy with sorted collections.
Claus Gittinger <cg@exept.de>
parents:
9074
diff
changeset
|
454 |
! |
5c76098e0377
min and max are easy with sorted collections.
Claus Gittinger <cg@exept.de>
parents:
9074
diff
changeset
|
455 |
|
5c76098e0377
min and max are easy with sorted collections.
Claus Gittinger <cg@exept.de>
parents:
9074
diff
changeset
|
456 |
min |
5c76098e0377
min and max are easy with sorted collections.
Claus Gittinger <cg@exept.de>
parents:
9074
diff
changeset
|
457 |
"Return the smallest element." |
5c76098e0377
min and max are easy with sorted collections.
Claus Gittinger <cg@exept.de>
parents:
9074
diff
changeset
|
458 |
|
9917
278547f0d52e
... but only with the default sort block.
Claus Gittinger <cg@exept.de>
parents:
9916
diff
changeset
|
459 |
sortBlock == DefaultSortBlock ifTrue:[ |
278547f0d52e
... but only with the default sort block.
Claus Gittinger <cg@exept.de>
parents:
9916
diff
changeset
|
460 |
^ self first |
278547f0d52e
... but only with the default sort block.
Claus Gittinger <cg@exept.de>
parents:
9916
diff
changeset
|
461 |
]. |
278547f0d52e
... but only with the default sort block.
Claus Gittinger <cg@exept.de>
parents:
9916
diff
changeset
|
462 |
^ super min |
9916
5c76098e0377
min and max are easy with sorted collections.
Claus Gittinger <cg@exept.de>
parents:
9074
diff
changeset
|
463 |
|
5c76098e0377
min and max are easy with sorted collections.
Claus Gittinger <cg@exept.de>
parents:
9074
diff
changeset
|
464 |
" |
5c76098e0377
min and max are easy with sorted collections.
Claus Gittinger <cg@exept.de>
parents:
9074
diff
changeset
|
465 |
#(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
|
466 |
" |
9073 | 467 |
! ! |
468 |
||
1 | 469 |
!SortedCollection methodsFor:'adding & removing'! |
470 |
||
607 | 471 |
add:anObject |
11268
7ce883e9a42e
tuned add: if the elements are added in sorted order
Claus Gittinger <cg@exept.de>
parents:
9920
diff
changeset
|
472 |
"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
|
473 |
Returns the argument, anObject (sigh)." |
2 | 474 |
|
11268
7ce883e9a42e
tuned add: if the elements are added in sorted order
Claus Gittinger <cg@exept.de>
parents:
9920
diff
changeset
|
475 |
|index lastElement| |
1 | 476 |
|
11274
a6f648b1d291
tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents:
11268
diff
changeset
|
477 |
"/ |
a6f648b1d291
tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents:
11268
diff
changeset
|
478 |
"/ cg: the original code was simply: |
a6f648b1d291
tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents:
11268
diff
changeset
|
479 |
"/ |
a6f648b1d291
tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents:
11268
diff
changeset
|
480 |
"/ index := self indexForInserting:anObject. |
a6f648b1d291
tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents:
11268
diff
changeset
|
481 |
"/ index := self makeRoomAtIndex:index. |
a6f648b1d291
tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents:
11268
diff
changeset
|
482 |
"/ contentsArray basicAt:index put:anObject. |
a6f648b1d291
tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents:
11268
diff
changeset
|
483 |
"/ |
a6f648b1d291
tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents:
11268
diff
changeset
|
484 |
"/ which was nice and simple to understand. |
a6f648b1d291
tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents:
11268
diff
changeset
|
485 |
"/ 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
|
486 |
"/ which often happens. |
a6f648b1d291
tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents:
11268
diff
changeset
|
487 |
"/ 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
|
488 |
|
11268
7ce883e9a42e
tuned add: if the elements are added in sorted order
Claus Gittinger <cg@exept.de>
parents:
9920
diff
changeset
|
489 |
(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
|
490 |
or:[ |
7ce883e9a42e
tuned add: if the elements are added in sorted order
Claus Gittinger <cg@exept.de>
parents:
9920
diff
changeset
|
491 |
lastElement := contentsArray at:lastIndex. |
7ce883e9a42e
tuned add: if the elements are added in sorted order
Claus Gittinger <cg@exept.de>
parents:
9920
diff
changeset
|
492 |
(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
|
493 |
]) ifTrue:[ |
11274
a6f648b1d291
tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents:
11268
diff
changeset
|
494 |
"/ 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
|
495 |
index := lastIndex. |
7ce883e9a42e
tuned add: if the elements are added in sorted order
Claus Gittinger <cg@exept.de>
parents:
9920
diff
changeset
|
496 |
(index == contentsArray size) ifTrue:[ |
7ce883e9a42e
tuned add: if the elements are added in sorted order
Claus Gittinger <cg@exept.de>
parents:
9920
diff
changeset
|
497 |
self makeRoomAtLast. |
7ce883e9a42e
tuned add: if the elements are added in sorted order
Claus Gittinger <cg@exept.de>
parents:
9920
diff
changeset
|
498 |
index := lastIndex. |
7ce883e9a42e
tuned add: if the elements are added in sorted order
Claus Gittinger <cg@exept.de>
parents:
9920
diff
changeset
|
499 |
]. |
7ce883e9a42e
tuned add: if the elements are added in sorted order
Claus Gittinger <cg@exept.de>
parents:
9920
diff
changeset
|
500 |
lastIndex := index := index + 1. |
607 | 501 |
] ifFalse:[ |
11268
7ce883e9a42e
tuned add: if the elements are added in sorted order
Claus Gittinger <cg@exept.de>
parents:
9920
diff
changeset
|
502 |
index := self indexForInserting:anObject. |
11274
a6f648b1d291
tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents:
11268
diff
changeset
|
503 |
(index == firstIndex) ifTrue:[ |
a6f648b1d291
tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents:
11268
diff
changeset
|
504 |
(firstIndex == 1) ifTrue:[ |
a6f648b1d291
tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents:
11268
diff
changeset
|
505 |
self makeRoomAtFront. |
a6f648b1d291
tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents:
11268
diff
changeset
|
506 |
]. |
a6f648b1d291
tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents:
11268
diff
changeset
|
507 |
index := firstIndex := firstIndex - 1. |
a6f648b1d291
tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents:
11268
diff
changeset
|
508 |
] ifFalse:[ |
a6f648b1d291
tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents:
11268
diff
changeset
|
509 |
index := self makeRoomAtIndex:index. |
a6f648b1d291
tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents:
11268
diff
changeset
|
510 |
]. |
607 | 511 |
]. |
3340 | 512 |
contentsArray basicAt:index put:anObject. |
607 | 513 |
^ anObject |
2 | 514 |
|
607 | 515 |
" |
11268
7ce883e9a42e
tuned add: if the elements are added in sorted order
Claus Gittinger <cg@exept.de>
parents:
9920
diff
changeset
|
516 |
#(7 3 9 10 99) asSortedCollection add:5; yourself |
8874 | 517 |
#(7 3 9 10 99) asSortedCollection add:1; yourself |
518 |
#(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
|
519 |
#(7 3 9 10 99) asSortedCollection add:1; yourself |
607 | 520 |
" |
1162
3995101dfa44
changed for new #makeRoomAtIndex:; added comments
Claus Gittinger <cg@exept.de>
parents:
1156
diff
changeset
|
521 |
|
11276 | 522 |
"Modified: / 22-10-2008 / 17:09:39 / cg" |
1 | 523 |
! |
524 |
||
525 |
addAll:aCollection |
|
8874 | 526 |
"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
|
527 |
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
|
528 |
|
11286 | 529 |
|addedCollection mySize numAdded| |
530 |
||
531 |
numAdded := aCollection size. |
|
532 |
mySize := self size. |
|
1 | 533 |
|
5359
396b77b6f1d2
added #isSortedCollection, #isSortedBy:
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
534 |
(aCollection isSortedCollection |
7554 | 535 |
and:[aCollection sortBlock == sortBlock]) ifTrue:[ |
11286 | 536 |
"/ not helping, really |
537 |
"/ mySize == 0 ifTrue:[ |
|
538 |
"/ "/ special case: I am empty - add them en-bloque. |
|
539 |
"/ contentsArray size >= numAdded ifTrue:[ |
|
540 |
"/ contentsArray replaceFrom:1 with:aCollection. |
|
541 |
"/ firstIndex := 1. |
|
542 |
"/ lastIndex := numAdded. |
|
543 |
"/ ^ self. |
|
544 |
"/ ]. |
|
545 |
"/ ]. |
|
546 |
addedCollection := aCollection. |
|
5030
655a0f0a60c3
use my sortBlock when merge-sorting
Claus Gittinger <cg@exept.de>
parents:
5029
diff
changeset
|
547 |
] ifFalse:[ |
11286 | 548 |
addedCollection := Array withAll:aCollection. |
549 |
addedCollection sort:sortBlock. |
|
5030
655a0f0a60c3
use my sortBlock when merge-sorting
Claus Gittinger <cg@exept.de>
parents:
5029
diff
changeset
|
550 |
|
11286 | 551 |
mySize == 0 ifTrue:[ |
552 |
"/ special case: I am empty - add them en-bloque. |
|
553 |
contentsArray := addedCollection. |
|
554 |
firstIndex := 1. |
|
555 |
lastIndex := numAdded. |
|
556 |
^ aCollection |
|
557 |
]. |
|
1 | 558 |
]. |
1173
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
559 |
|
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
560 |
"/ |
5030
655a0f0a60c3
use my sortBlock when merge-sorting
Claus Gittinger <cg@exept.de>
parents:
5029
diff
changeset
|
561 |
"/ 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
|
562 |
"/ |
5030
655a0f0a60c3
use my sortBlock when merge-sorting
Claus Gittinger <cg@exept.de>
parents:
5029
diff
changeset
|
563 |
self mergeSorted:addedCollection. |
1173
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
564 |
^ aCollection |
1 | 565 |
|
77 | 566 |
" |
8874 | 567 |
#(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
|
568 |
|
5028
1f7e943ce46e
use my sortBlock when merge-sorting
Claus Gittinger <cg@exept.de>
parents:
4596
diff
changeset
|
569 |
( #(7 3 9 10 99) asSortedCollection:[:a :b | a > b]) |
11286 | 570 |
addAll:#(77 0 1 16 5); yourself |
5030
655a0f0a60c3
use my sortBlock when merge-sorting
Claus Gittinger <cg@exept.de>
parents:
5029
diff
changeset
|
571 |
|
8874 | 572 |
#(7 3 9 10 99) asSortedCollection |
11286 | 573 |
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
|
574 |
|
8874 | 575 |
(#(7 3 9 10 99) asSortedCollection:[:a :b | a > b]) |
11286 | 576 |
addAll:( #(77 0 1 16 5) asSortedCollection:[:a :b | a < b]); yourself |
577 |
" |
|
578 |
||
579 |
" |
|
580 |
|x e| |
|
581 |
||
582 |
e := (1 to:100) asSortedCollection. |
|
583 |
TimeDuration toRun:[ |
|
584 |
10000 timesRepeat:[ |
|
585 |
x := SortedCollection new:100. |
|
586 |
x addAll:e. |
|
587 |
]. |
|
588 |
]. |
|
77 | 589 |
" |
1173
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
590 |
|
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
591 |
"Modified: 13.4.1996 / 12:42:15 / cg" |
1 | 592 |
! |
593 |
||
1173
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
594 |
mergeSorted:aSortedCollection |
11286 | 595 |
"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
|
596 |
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
|
597 |
|
8874 | 598 |
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
|
599 |
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
|
600 |
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
|
601 |
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
|
602 |
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
|
603 |
|
8874 | 604 |
|newContentsArray |
1173
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
605 |
contentsArray2 |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
606 |
destIndex "{ Class: SmallInteger }" |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
607 |
n1 "{ Class: SmallInteger }" |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
608 |
n2 "{ Class: SmallInteger }" |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
609 |
srcIndex1 "{ Class: SmallInteger }" |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
610 |
srcIndex2 "{ Class: SmallInteger }" |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
611 |
last1 "{ Class: SmallInteger }" |
8874 | 612 |
last2 "{ Class: SmallInteger }" |
1173
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
613 |
end1Reached end2Reached |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
614 |
el1 el2 | |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
615 |
|
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
616 |
n1 := self size. |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
617 |
n2 := aSortedCollection size. |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
618 |
|
5381
914e310a69be
Fix #mergeSorted if one of the collections is empty.
Stefan Vogel <sv@exept.de>
parents:
5359
diff
changeset
|
619 |
n1 == 0 ifTrue:[ |
11286 | 620 |
^ aSortedCollection. |
5381
914e310a69be
Fix #mergeSorted if one of the collections is empty.
Stefan Vogel <sv@exept.de>
parents:
5359
diff
changeset
|
621 |
]. |
914e310a69be
Fix #mergeSorted if one of the collections is empty.
Stefan Vogel <sv@exept.de>
parents:
5359
diff
changeset
|
622 |
n2 == 0 ifTrue:[ |
11286 | 623 |
^ self. |
5381
914e310a69be
Fix #mergeSorted if one of the collections is empty.
Stefan Vogel <sv@exept.de>
parents:
5359
diff
changeset
|
624 |
]. |
914e310a69be
Fix #mergeSorted if one of the collections is empty.
Stefan Vogel <sv@exept.de>
parents:
5359
diff
changeset
|
625 |
|
1173
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
626 |
newContentsArray := Array new:(n1 + n2). |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
627 |
destIndex := 1. |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
628 |
|
5359
396b77b6f1d2
added #isSortedCollection, #isSortedBy:
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
629 |
(aSortedCollection isSortedBy:sortBlock) ifTrue:[ |
11286 | 630 |
aSortedCollection isSortedCollection ifTrue:[ |
631 |
contentsArray2 := aSortedCollection contentsArray. |
|
632 |
srcIndex2 := aSortedCollection firstIndex. |
|
633 |
] ifFalse:[ |
|
634 |
contentsArray2 := aSortedCollection. |
|
635 |
srcIndex2 := 1. |
|
636 |
] |
|
5030
655a0f0a60c3
use my sortBlock when merge-sorting
Claus Gittinger <cg@exept.de>
parents:
5029
diff
changeset
|
637 |
] ifFalse:[ |
11286 | 638 |
contentsArray2 := aSortedCollection. |
639 |
srcIndex2 := 1. |
|
5030
655a0f0a60c3
use my sortBlock when merge-sorting
Claus Gittinger <cg@exept.de>
parents:
5029
diff
changeset
|
640 |
]. |
655a0f0a60c3
use my sortBlock when merge-sorting
Claus Gittinger <cg@exept.de>
parents:
5029
diff
changeset
|
641 |
|
1173
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
642 |
last2 := srcIndex2 + n2 -1. |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
643 |
|
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
644 |
srcIndex1 := firstIndex. |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
645 |
last1 := srcIndex1 + n1 -1. |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
646 |
|
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
647 |
(srcIndex1 <= last1 and:[srcIndex2 <= last2]) ifTrue:[ |
11286 | 648 |
el1 := contentsArray at:srcIndex1. |
649 |
el2 := contentsArray2 at:srcIndex2. |
|
650 |
end1Reached := end2Reached := false. |
|
1173
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
651 |
|
11286 | 652 |
[end1Reached or:[end2Reached]] whileFalse:[ |
653 |
(sortBlock value:el1 value:el2) ifTrue:[ |
|
654 |
"/ el1 to come before el2 |
|
655 |
newContentsArray at:destIndex put:el1. destIndex := destIndex + 1. |
|
656 |
srcIndex1 := srcIndex1 + 1. |
|
657 |
srcIndex1 <= last1 ifTrue:[ |
|
658 |
el1 := contentsArray at:srcIndex1. |
|
659 |
] ifFalse:[ |
|
660 |
end1Reached := true |
|
661 |
] |
|
662 |
] ifFalse:[ |
|
663 |
"/ el2 to come before el1 |
|
664 |
newContentsArray at:destIndex put:el2. destIndex := destIndex + 1. |
|
665 |
srcIndex2 := srcIndex2 + 1. |
|
666 |
srcIndex2 <= last2 ifTrue:[ |
|
667 |
el2 := contentsArray2 at:srcIndex2. |
|
668 |
] ifFalse:[ |
|
669 |
end2Reached := true |
|
670 |
] |
|
671 |
] |
|
672 |
] |
|
1173
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
673 |
]. |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
674 |
|
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
675 |
"/ 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
|
676 |
(srcIndex1 <= last1) ifTrue:[ |
11286 | 677 |
newContentsArray |
678 |
replaceFrom:destIndex |
|
679 |
to:(destIndex+(last1-srcIndex1)) |
|
680 |
with:contentsArray |
|
681 |
startingAt:srcIndex1 |
|
1173
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
682 |
]. |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
683 |
|
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
684 |
"/ 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
|
685 |
(srcIndex2 <= last2) ifTrue:[ |
11286 | 686 |
newContentsArray |
687 |
replaceFrom:destIndex |
|
688 |
to:(destIndex+(last2-srcIndex2)) |
|
689 |
with:contentsArray2 |
|
690 |
startingAt:srcIndex2 |
|
1173
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
691 |
]. |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
692 |
|
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
693 |
firstIndex := 1. |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
694 |
lastIndex := n1 + n2. |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
695 |
contentsArray := newContentsArray |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
696 |
|
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
697 |
" |
8874 | 698 |
#(7 3 9 10 99) asSortedCollection |
11286 | 699 |
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
|
700 |
|
8874 | 701 |
#(7 3 9 10 99) asSortedCollection |
11286 | 702 |
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
|
703 |
|
8874 | 704 |
#() asSortedCollection |
11286 | 705 |
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
|
706 |
|
8874 | 707 |
#(7 3 9 10 99) asSortedCollection |
11286 | 708 |
mergeSorted:#() asSortedCollection |
1173
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
709 |
" |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
710 |
|
3250
c4b444774f1c
oops - mergesort cannot simply copy remaining
Claus Gittinger <cg@exept.de>
parents:
2929
diff
changeset
|
711 |
"Modified: / 30.1.1998 / 01:49:42 / cg" |
607 | 712 |
! ! |
713 |
||
5560 | 714 |
!SortedCollection methodsFor:'blocked'! |
715 |
||
716 |
add:newObject after:oldObject |
|
717 |
"catch this - its not allowed for sortedCollections" |
|
718 |
||
719 |
self shouldNotImplement |
|
720 |
! |
|
721 |
||
722 |
add:newObject before:oldObject |
|
723 |
"catch this - its not allowed for sortedCollections" |
|
724 |
||
725 |
self shouldNotImplement |
|
726 |
! |
|
727 |
||
728 |
addFirst:anObject |
|
729 |
"catch this - its not allowed for sortedCollections" |
|
730 |
||
731 |
self shouldNotImplement |
|
732 |
! |
|
733 |
||
734 |
addLast:anObject |
|
735 |
"catch this - its not allowed for sortedCollections" |
|
736 |
||
737 |
self shouldNotImplement |
|
738 |
! ! |
|
739 |
||
607 | 740 |
!SortedCollection methodsFor:'converting'! |
741 |
||
742 |
asSortedCollection |
|
743 |
"return the receiver as a sorted collection" |
|
744 |
||
745 |
"could be an instance of a subclass..." |
|
746 |
self class == SortedCollection ifTrue:[ |
|
8874 | 747 |
sortBlock == DefaultSortBlock ifTrue:[ |
748 |
^ self |
|
749 |
] |
|
1 | 750 |
]. |
607 | 751 |
^ super asSortedCollection |
5029
ae3312dda1dd
avoid creation of a new sortedCollection in #asSortedCollection:
Claus Gittinger <cg@exept.de>
parents:
5028
diff
changeset
|
752 |
! |
ae3312dda1dd
avoid creation of a new sortedCollection in #asSortedCollection:
Claus Gittinger <cg@exept.de>
parents:
5028
diff
changeset
|
753 |
|
ae3312dda1dd
avoid creation of a new sortedCollection in #asSortedCollection:
Claus Gittinger <cg@exept.de>
parents:
5028
diff
changeset
|
754 |
asSortedCollection:aSortBlock |
ae3312dda1dd
avoid creation of a new sortedCollection in #asSortedCollection:
Claus Gittinger <cg@exept.de>
parents:
5028
diff
changeset
|
755 |
"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
|
756 |
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
|
757 |
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
|
758 |
|
ae3312dda1dd
avoid creation of a new sortedCollection in #asSortedCollection:
Claus Gittinger <cg@exept.de>
parents:
5028
diff
changeset
|
759 |
"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
|
760 |
self class == SortedCollection ifTrue:[ |
8874 | 761 |
"/ if the sortBlock is the same, return the receiver |
762 |
aSortBlock == sortBlock ifTrue:[ |
|
763 |
^ self |
|
764 |
]. |
|
5029
ae3312dda1dd
avoid creation of a new sortedCollection in #asSortedCollection:
Claus Gittinger <cg@exept.de>
parents:
5028
diff
changeset
|
765 |
]. |
5030
655a0f0a60c3
use my sortBlock when merge-sorting
Claus Gittinger <cg@exept.de>
parents:
5029
diff
changeset
|
766 |
^ super asSortedCollection:aSortBlock |
1 | 767 |
! ! |
768 |
||
95 | 769 |
!SortedCollection methodsFor:'copying'! |
770 |
||
607 | 771 |
copyEmpty:size |
772 |
"return a copy of the receiver with no elements, but |
|
773 |
the same size. This method has been be redefined to |
|
774 |
preserve the sortBlock." |
|
775 |
||
776 |
^ (self species new:size) sortBlock:sortBlock |
|
777 |
! |
|
778 |
||
159 | 779 |
postCopyFrom:anOriginal |
780 |
"sent after a copy or when a new collection species has been created. |
|
95 | 781 |
The new collection should have the same sortBlock as the original." |
782 |
||
159 | 783 |
sortBlock := anOriginal sortBlock |
95 | 784 |
|
785 |
" |
|
786 |
#(4 7 1 99 -1 17) asSortedCollection inspect |
|
159 | 787 |
#(4 7 1 99 -1 17) asSortedCollection copy inspect |
95 | 788 |
(#(4 7 1 99 -1 17) asSortedCollection sortBlock:[:a :b | a > b]) inspect |
159 | 789 |
(#(4 7 1 99 -1 17) asSortedCollection sortBlock:[:a :b | a > b]) copy inspect |
790 |
(#(4 7 1 99 -1 17) asSortedCollection select:[:e| e even]) inspect |
|
95 | 791 |
" |
792 |
! ! |
|
793 |
||
216 | 794 |
!SortedCollection methodsFor:'enumerating'! |
184 | 795 |
|
796 |
collect:aBlock |
|
797 |
"evaluate the argument, aBlock for every element in the collection |
|
798 |
and return a collection of the results. Redefined to return an OrderedCollection; |
|
799 |
see X3J20 spec. (SortedCollection>>collect: should return an OrderedCollection)" |
|
800 |
||
801 |
|newCollection |
|
802 |
start "{ Class:SmallInteger }" |
|
803 |
stop "{ Class:SmallInteger }" | |
|
804 |
||
805 |
newCollection := OrderedCollection new:(self size). |
|
806 |
stop := lastIndex. |
|
807 |
start := firstIndex. |
|
808 |
start to:stop do:[:index | |
|
809 |
newCollection add:(aBlock value:(contentsArray at:index)). |
|
810 |
]. |
|
811 |
^ newCollection |
|
812 |
! ! |
|
813 |
||
607 | 814 |
!SortedCollection methodsFor:'instance protocol'! |
815 |
||
5668
0cbaa16dc17c
Inherited #sort: shows a debugger.
Stefan Vogel <sv@exept.de>
parents:
5604
diff
changeset
|
816 |
sort:aSortBlock |
0cbaa16dc17c
Inherited #sort: shows a debugger.
Stefan Vogel <sv@exept.de>
parents:
5604
diff
changeset
|
817 |
"redefined from superclass to change the sortBlock" |
0cbaa16dc17c
Inherited #sort: shows a debugger.
Stefan Vogel <sv@exept.de>
parents:
5604
diff
changeset
|
818 |
|
0cbaa16dc17c
Inherited #sort: shows a debugger.
Stefan Vogel <sv@exept.de>
parents:
5604
diff
changeset
|
819 |
sortBlock ~~ aSortBlock ifTrue:[ |
8874 | 820 |
self sortBlock:aSortBlock. |
5668
0cbaa16dc17c
Inherited #sort: shows a debugger.
Stefan Vogel <sv@exept.de>
parents:
5604
diff
changeset
|
821 |
]. |
0cbaa16dc17c
Inherited #sort: shows a debugger.
Stefan Vogel <sv@exept.de>
parents:
5604
diff
changeset
|
822 |
|
0cbaa16dc17c
Inherited #sort: shows a debugger.
Stefan Vogel <sv@exept.de>
parents:
5604
diff
changeset
|
823 |
! |
0cbaa16dc17c
Inherited #sort: shows a debugger.
Stefan Vogel <sv@exept.de>
parents:
5604
diff
changeset
|
824 |
|
607 | 825 |
sortBlock |
826 |
"return the block used for sorting" |
|
827 |
||
828 |
^ sortBlock |
|
829 |
! |
|
830 |
||
831 |
sortBlock:aSortBlock |
|
8874 | 832 |
"change the sort criteria for a sorted collection, resort the elements of |
607 | 833 |
the collection, and return the receiver. The argument, aSortBlock must |
834 |
be a two-argument block which returns true if its arg1 has to come before |
|
835 |
its arg2 in the collection." |
|
836 |
||
837 |
sortBlock := aSortBlock. |
|
838 |
lastIndex > firstIndex ifTrue:[ |
|
839 |
contentsArray quickSortFrom:firstIndex to:lastIndex sortBlock:aSortBlock |
|
840 |
] |
|
841 |
||
842 |
" |
|
843 |
#(9 8 7 6 5 4 3) asSortedCollection |
|
844 |
#(9 8 7 6 5 4 3) asSortedCollection sortBlock:[:a : b | a < b] |
|
845 |
#(9 8 7 6 5 4 3) asSortedCollection sortBlock:[:a : b | a > b] |
|
846 |
#($f $G $z $Y $o $H) asSortedCollection |
|
847 |
#($f $G $z $Y $o $H) asSortedCollection sortBlock:[:a : b | a asUppercase < b asUppercase] |
|
848 |
" |
|
849 |
! ! |
|
850 |
||
851 |
!SortedCollection methodsFor:'private'! |
|
852 |
||
853 |
indexForInserting:anObject |
|
8874 | 854 |
"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
|
855 |
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
|
856 |
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
|
857 |
Uses a binarySearch since we can depend on the elements being on sorted order. |
607 | 858 |
The returned index is a physical one, for accessing contentsArray." |
859 |
||
11478
0cdba0734b9f
binary search algorithm made publicly usable
Claus Gittinger <cg@exept.de>
parents:
11286
diff
changeset
|
860 |
^ self class |
0cdba0734b9f
binary search algorithm made publicly usable
Claus Gittinger <cg@exept.de>
parents:
11286
diff
changeset
|
861 |
binarySearch:contentsArray |
0cdba0734b9f
binary search algorithm made publicly usable
Claus Gittinger <cg@exept.de>
parents:
11286
diff
changeset
|
862 |
from:firstIndex to:lastIndex |
0cdba0734b9f
binary search algorithm made publicly usable
Claus Gittinger <cg@exept.de>
parents:
11286
diff
changeset
|
863 |
forIndexOf:anObject usingSortBlock:sortBlock |
607 | 864 |
|
865 |
" |
|
8874 | 866 |
#(1 2 3 4 7 99 1313 981989 898989898) asSortedCollection indexForInserting:50 |
11478
0cdba0734b9f
binary search algorithm made publicly usable
Claus Gittinger <cg@exept.de>
parents:
11286
diff
changeset
|
867 |
|
8874 | 868 |
#(1.0 2.0 3 4 7 49.0 51.0 99 1313 981989 898989898) asSortedCollection indexForInserting:50 |
11478
0cdba0734b9f
binary search algorithm made publicly usable
Claus Gittinger <cg@exept.de>
parents:
11286
diff
changeset
|
869 |
|
607 | 870 |
" |
871 |
||
1162
3995101dfa44
changed for new #makeRoomAtIndex:; added comments
Claus Gittinger <cg@exept.de>
parents:
1156
diff
changeset
|
872 |
"Modified: 12.4.1996 / 13:22:03 / cg" |
607 | 873 |
! |
874 |
||
875 |
setSortBlock: aSortBlock |
|
876 |
"set the sortblock without resorting - private only" |
|
877 |
||
878 |
sortBlock := aSortBlock |
|
879 |
! ! |
|
880 |
||
1173
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
881 |
!SortedCollection methodsFor:'queries'! |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
882 |
|
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
883 |
isSorted |
5359
396b77b6f1d2
added #isSortedCollection, #isSortedBy:
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
884 |
"return true. if my elements are sorted" |
396b77b6f1d2
added #isSortedCollection, #isSortedBy:
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
885 |
|
1173
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
886 |
^ true |
5359
396b77b6f1d2
added #isSortedCollection, #isSortedBy:
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
887 |
! |
396b77b6f1d2
added #isSortedCollection, #isSortedBy:
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
888 |
|
396b77b6f1d2
added #isSortedCollection, #isSortedBy:
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
889 |
isSortedBy:aBlock |
396b77b6f1d2
added #isSortedCollection, #isSortedBy:
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
890 |
"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
|
891 |
|
5359
396b77b6f1d2
added #isSortedCollection, #isSortedBy:
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
892 |
aBlock == sortBlock ifTrue:[^ true]. |
396b77b6f1d2
added #isSortedCollection, #isSortedBy:
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
893 |
^ super isSortedBy:aBlock |
396b77b6f1d2
added #isSortedCollection, #isSortedBy:
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
894 |
|
396b77b6f1d2
added #isSortedCollection, #isSortedBy:
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
895 |
|
396b77b6f1d2
added #isSortedCollection, #isSortedBy:
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
896 |
! |
396b77b6f1d2
added #isSortedCollection, #isSortedBy:
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
897 |
|
396b77b6f1d2
added #isSortedCollection, #isSortedBy:
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
898 |
isSortedCollection |
396b77b6f1d2
added #isSortedCollection, #isSortedBy:
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
899 |
"return true. if I am a sorted collection" |
396b77b6f1d2
added #isSortedCollection, #isSortedBy:
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
900 |
|
396b77b6f1d2
added #isSortedCollection, #isSortedBy:
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
901 |
^ true |
1173
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
902 |
! ! |
1d831f2c0d44
added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents:
1167
diff
changeset
|
903 |
|
607 | 904 |
!SortedCollection methodsFor:'searching'! |
905 |
||
906 |
after:anObject ifAbsent:exceptionBlock |
|
907 |
"return the element after the argument, anObject; or nil if there is none. |
|
908 |
If anObject is contained multiple times in the collection, return the |
|
909 |
the first element which is non-equal to anObject. |
|
910 |
If the receiver does not contain anObject, return the result from evaluating |
|
911 |
exceptionBlock." |
|
912 |
||
1162
3995101dfa44
changed for new #makeRoomAtIndex:; added comments
Claus Gittinger <cg@exept.de>
parents:
1156
diff
changeset
|
913 |
|index "{ Class: SmallInteger }" |
3995101dfa44
changed for new #makeRoomAtIndex:; added comments
Claus Gittinger <cg@exept.de>
parents:
1156
diff
changeset
|
914 |
last "{ Class: SmallInteger }"| |
607 | 915 |
|
916 |
index := self indexForInserting:anObject. |
|
8874 | 917 |
((index < firstIndex) |
607 | 918 |
or:[(contentsArray at:index) ~= anObject]) ifTrue:[^ exceptionBlock value]. |
919 |
||
8874 | 920 |
"skip multiple occurrences of the same ..." |
607 | 921 |
|
1162
3995101dfa44
changed for new #makeRoomAtIndex:; added comments
Claus Gittinger <cg@exept.de>
parents:
1156
diff
changeset
|
922 |
last := lastIndex. |
3995101dfa44
changed for new #makeRoomAtIndex:; added comments
Claus Gittinger <cg@exept.de>
parents:
1156
diff
changeset
|
923 |
[(index <= last) and:[(contentsArray at:index) = anObject]] whileTrue:[ |
8874 | 924 |
index := index + 1 |
607 | 925 |
]. |
1162
3995101dfa44
changed for new #makeRoomAtIndex:; added comments
Claus Gittinger <cg@exept.de>
parents:
1156
diff
changeset
|
926 |
(index > last) ifTrue:[^ nil]. |
607 | 927 |
^ contentsArray at:index |
1162
3995101dfa44
changed for new #makeRoomAtIndex:; added comments
Claus Gittinger <cg@exept.de>
parents:
1156
diff
changeset
|
928 |
|
3995101dfa44
changed for new #makeRoomAtIndex:; added comments
Claus Gittinger <cg@exept.de>
parents:
1156
diff
changeset
|
929 |
"Modified: 12.4.1996 / 13:20:33 / cg" |
607 | 930 |
! |
931 |
||
932 |
before:anObject ifAbsent:exceptionBlock |
|
933 |
"return the element before the argument, anObject; or nil if there is none. |
|
934 |
If the receiver does not contain anObject, return the result from evaluating |
|
935 |
exceptionBlock." |
|
936 |
||
937 |
|index "{ Class: SmallInteger }"| |
|
938 |
||
939 |
index := self indexForInserting:anObject. |
|
8874 | 940 |
((index <= firstIndex) |
607 | 941 |
or:[(contentsArray at:index) ~= anObject]) ifTrue:[^ exceptionBlock value]. |
942 |
||
943 |
^ contentsArray at:index - 1 |
|
944 |
||
945 |
" |
|
946 |
#(7 3 9 10 99) asSortedCollection before:50 |
|
8874 | 947 |
#(7 3 9 10 99) asSortedCollection before:1 |
948 |
#(7 3 9 10 99) asSortedCollection before:10 |
|
949 |
#(7 3 9 10 99) asSortedCollection before:7 |
|
950 |
#(7 3 9 10 99) asSortedCollection before:99 |
|
951 |
#(7 10 3 10 9 10 10 99) asSortedCollection before:9 |
|
952 |
#(7 10 3 10 9 10 10 99) asSortedCollection before:10 |
|
607 | 953 |
" |
4596 | 954 |
! |
955 |
||
956 |
indexOf:anElement |
|
8874 | 957 |
"Return the index of anElement within the receiver. |
4596 | 958 |
If the receiver does not contain anElement, return 0." |
959 |
||
960 |
^ self indexOf:anElement ifAbsent:0 |
|
961 |
! |
|
962 |
||
963 |
indexOf:anElement ifAbsent:exceptionBlock |
|
8874 | 964 |
"Return the index of anElement within the receiver. |
965 |
If the receiver does not contain anElement, |
|
4596 | 966 |
return the result of evaluating the argument, exceptionBlock." |
967 |
||
8874 | 968 |
|insertionIndex index "{ Class: SmallInteger }" |
4596 | 969 |
obj| |
970 |
||
971 |
firstIndex > lastIndex ifTrue:[ |
|
8874 | 972 |
"/ empty |
973 |
^ exceptionBlock value |
|
4596 | 974 |
]. |
975 |
||
976 |
"/ if I am small, the inherited linear search is faster ... |
|
977 |
(lastIndex - firstIndex) < 20 ifTrue:[ |
|
8874 | 978 |
^ super indexOf:anElement ifAbsent:exceptionBlock |
4596 | 979 |
]. |
980 |
||
981 |
insertionIndex := self indexForInserting:anElement. |
|
982 |
insertionIndex > lastIndex ifTrue:[ |
|
8874 | 983 |
insertionIndex := lastIndex |
4596 | 984 |
] ifFalse:[ |
8874 | 985 |
insertionIndex < firstIndex ifTrue:[ |
986 |
insertionIndex := firstIndex |
|
987 |
] |
|
4596 | 988 |
]. |
989 |
||
990 |
index := insertionIndex. |
|
991 |
[index >= firstIndex |
|
992 |
and:[obj := contentsArray basicAt:index. |
|
8874 | 993 |
anElement = obj ifTrue: [^ index - firstIndex + 1]. |
994 |
[sortBlock value:anElement value:obj]]] |
|
995 |
whileTrue: [index := index - 1]. |
|
4596 | 996 |
|
997 |
index := insertionIndex. |
|
998 |
[index <= lastIndex |
|
999 |
and: [obj := contentsArray basicAt: index. |
|
8874 | 1000 |
anElement = obj ifTrue: [^ index - firstIndex + 1]. |
1001 |
[sortBlock value:obj value:anElement]]] |
|
1002 |
whileTrue: [index := index + 1]. |
|
4596 | 1003 |
|
1004 |
^exceptionBlock value |
|
1005 |
||
1006 |
" |
|
1007 |
#('aa' 'bb' 'cc' 'dd') asSortedCollection indexOf:'bb' |
|
8874 | 1008 |
#('aa' 'bb' 'cc' 'dd' 'aa' 'bb' 'cc' 'dd') asSortedCollection indexOf:'bb' |
4596 | 1009 |
|
1010 |
|allSyms indices| |
|
8874 | 1011 |
allSyms := Symbol allInstances asSortedCollection. |
4596 | 1012 |
Time millisecondsToRun:[ |
8874 | 1013 |
indices := allSyms collect:[:el | allSyms indexOf:el]. |
4596 | 1014 |
]. |
1015 |
indices = (1 to:allSyms size) |
|
1016 |
" |
|
607 | 1017 |
! ! |
1018 |
||
1 | 1019 |
!SortedCollection methodsFor:'testing'! |
1020 |
||
1021 |
includes:anObject |
|
1022 |
"return true, if the argument, anObject is in the collection. |
|
62 | 1023 |
Redefined, since due to being sorted, the inclusion check can |
2 | 1024 |
be done with log-n compares i.e. much faster." |
1 | 1025 |
|
32 | 1026 |
|index "{ Class: SmallInteger }"| |
1 | 1027 |
|
77 | 1028 |
index := self indexForInserting:anObject. |
32 | 1029 |
((index < firstIndex) or:[index > lastIndex]) ifTrue:[^ false]. |
1030 |
^ (contentsArray at:index) = anObject |
|
2 | 1031 |
|
77 | 1032 |
" |
1033 |
#(7 3 9 10 99) asSortedCollection includes:50 |
|
1034 |
#(7 3 9 10 99) asSortedCollection includes:10 |
|
1035 |
" |
|
1 | 1036 |
! |
1037 |
||
1038 |
occurrencesOf:anObject |
|
1039 |
"return how many times the argument, anObject is in the collection. |
|
62 | 1040 |
Redefined, since due to being sorted, the range of checked objects |
5048 | 1041 |
can be limited i.e. it can be done much faster. |
1042 |
Uses #= (i.e. equality) compare." |
|
1 | 1043 |
|
1044 |
|index "{ Class: SmallInteger }" |
|
1162
3995101dfa44
changed for new #makeRoomAtIndex:; added comments
Claus Gittinger <cg@exept.de>
parents:
1156
diff
changeset
|
1045 |
tally "{ Class: SmallInteger }" |
3995101dfa44
changed for new #makeRoomAtIndex:; added comments
Claus Gittinger <cg@exept.de>
parents:
1156
diff
changeset
|
1046 |
last "{ Class: SmallInteger }" | |
1 | 1047 |
|
77 | 1048 |
index := self indexForInserting:anObject. |
8874 | 1049 |
last := lastIndex. |
1162
3995101dfa44
changed for new #makeRoomAtIndex:; added comments
Claus Gittinger <cg@exept.de>
parents:
1156
diff
changeset
|
1050 |
((index < firstIndex) or:[index > last]) ifTrue:[^ 0]. |
3995101dfa44
changed for new #makeRoomAtIndex:; added comments
Claus Gittinger <cg@exept.de>
parents:
1156
diff
changeset
|
1051 |
|
3995101dfa44
changed for new #makeRoomAtIndex:; added comments
Claus Gittinger <cg@exept.de>
parents:
1156
diff
changeset
|
1052 |
"/ there may be multiple of them; count 'em |
32 | 1053 |
|
1 | 1054 |
tally := 0. |
1162
3995101dfa44
changed for new #makeRoomAtIndex:; added comments
Claus Gittinger <cg@exept.de>
parents:
1156
diff
changeset
|
1055 |
[(index <= last) and:[(contentsArray at:index) = anObject]] whileTrue:[ |
8874 | 1056 |
tally := tally + 1. |
1057 |
index := index + 1 |
|
1 | 1058 |
]. |
1059 |
^ tally |
|
2 | 1060 |
|
77 | 1061 |
" |
1062 |
#(7 3 9 10 99) asSortedCollection occurrencesOf:50 |
|
1063 |
#(7 3 9 10 99) asSortedCollection occurrencesOf:10 |
|
1064 |
#(7 10 3 10 9 10 10 99) asSortedCollection occurrencesOf:10 |
|
1065 |
" |
|
1162
3995101dfa44
changed for new #makeRoomAtIndex:; added comments
Claus Gittinger <cg@exept.de>
parents:
1156
diff
changeset
|
1066 |
|
3995101dfa44
changed for new #makeRoomAtIndex:; added comments
Claus Gittinger <cg@exept.de>
parents:
1156
diff
changeset
|
1067 |
"Modified: 12.4.1996 / 18:48:40 / cg" |
77 | 1068 |
! ! |
1069 |
||
629 | 1070 |
!SortedCollection class methodsFor:'documentation'! |
1071 |
||
1072 |
version |
|
17711 | 1073 |
^ '$Id: SortedCollection.st 10447 2009-06-14 13:09:55Z vranyj1 $' |
629 | 1074 |
! ! |
7300 | 1075 |
|
607 | 1076 |
SortedCollection initialize! |