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