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