author | Jan Vrany <jan.vrany@fit.cvut.cz> |
Wed, 16 Jun 2010 21:12:05 +0100 | |
branch | jv |
changeset 17767 | a4a32df3aa5e |
parent 17761 | b0e5971141bc |
child 17772 | 6e0b3c06c364 |
permissions | -rw-r--r-- |
1 | 1 |
" |
5 | 2 |
COPYRIGHT (c) 1989 by Claus Gittinger |
6135 | 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:
5268
diff
changeset
|
12 |
"{ Package: 'stx:libbasic' }" |
396b77b6f1d2
added #isSortedCollection, #isSortedBy:
Claus Gittinger <cg@exept.de>
parents:
5268
diff
changeset
|
13 |
|
1 | 14 |
Object subclass:#Collection |
11331 | 15 |
instanceVariableNames:'' |
16 |
classVariableNames:'InvalidKeySignal EmptyCollectionSignal ValueNotFoundSignal |
|
17 |
NotEnoughElementsSignal RecursiveCollectionStoreStringSignal' |
|
18 |
poolDictionaries:'' |
|
19 |
category:'Collections-Abstract' |
|
1 | 20 |
! |
21 |
||
2097 | 22 |
!Collection class methodsFor:'documentation'! |
88 | 23 |
|
24 |
copyright |
|
25 |
" |
|
26 |
COPYRIGHT (c) 1989 by Claus Gittinger |
|
6135 | 27 |
All Rights Reserved |
1 | 28 |
|
88 | 29 |
This software is furnished under a license and may be used |
30 |
only in accordance with the terms of that license and with the |
|
31 |
inclusion of the above copyright notice. This software may not |
|
32 |
be provided or otherwise made available to, or used by, any |
|
33 |
other person. No title to or ownership of the software is |
|
34 |
hereby transferred. |
|
35 |
" |
|
36 |
! |
|
1 | 37 |
|
88 | 38 |
documentation |
39 |
" |
|
40 |
Abstract superclass for all collections. |
|
41 |
This abstract class provides functionality common to all collections, |
|
42 |
without knowing how the concrete class implements things. Thus, all |
|
43 |
methods found here depend on some basic mechanisms to be defined in the |
|
44 |
concrete class. |
|
45 |
These basic methods are usually defined as #subclassResponsibility here. |
|
46 |
Some methods are also redefined for better performance. |
|
1289 | 47 |
|
1325 | 48 |
Subclasses should at least implement: |
49 |
do: - enumerate elements |
|
50 |
||
51 |
they should implement one of the following set of access messages: |
|
52 |
keyed collections: |
|
53 |
at:ifAbsent: - fetching an element |
|
54 |
at: - fetching an element |
|
55 |
at:put: - storing an element |
|
56 |
||
57 |
unkeyed collections: |
|
58 |
add: - add an element |
|
59 |
remove:ifAbsent: - remove an element |
|
60 |
||
61 |
||
1289 | 62 |
[author:] |
63 |
Claus Gittinger |
|
88 | 64 |
" |
65 |
! ! |
|
1 | 66 |
|
2097 | 67 |
!Collection class methodsFor:'initialization'! |
10 | 68 |
|
69 |
initialize |
|
70 |
"setup the signal" |
|
71 |
||
54 | 72 |
InvalidKeySignal isNil ifTrue:[ |
7092
630807cd320f
Convert Object>>errorSignal -> Error
Stefan Vogel <sv@exept.de>
parents:
7054
diff
changeset
|
73 |
InvalidKeySignal := Error newSignalMayProceed:true. |
3102
2bc81588db6c
sigs are children of NotFoundSignal (ST80 compat)
Claus Gittinger <cg@exept.de>
parents:
3075
diff
changeset
|
74 |
InvalidKeySignal nameClass:self message:#invalidKeySignal. |
2bc81588db6c
sigs are children of NotFoundSignal (ST80 compat)
Claus Gittinger <cg@exept.de>
parents:
3075
diff
changeset
|
75 |
InvalidKeySignal notifierString:'invalid key:'. |
345 | 76 |
|
8510
af60b80bf50e
refer to exception classes instead of signal class vars
Stefan Vogel <sv@exept.de>
parents:
8395
diff
changeset
|
77 |
ValueNotFoundSignal := NotFoundError newSignalMayProceed:true. |
3102
2bc81588db6c
sigs are children of NotFoundSignal (ST80 compat)
Claus Gittinger <cg@exept.de>
parents:
3075
diff
changeset
|
78 |
ValueNotFoundSignal nameClass:self message:#valueNotFoundSignal. |
2bc81588db6c
sigs are children of NotFoundSignal (ST80 compat)
Claus Gittinger <cg@exept.de>
parents:
3075
diff
changeset
|
79 |
ValueNotFoundSignal notifierString:'value not found:'. |
155 | 80 |
|
8510
af60b80bf50e
refer to exception classes instead of signal class vars
Stefan Vogel <sv@exept.de>
parents:
8395
diff
changeset
|
81 |
NotEnoughElementsSignal := NotFoundError newSignalMayProceed:true. |
3102
2bc81588db6c
sigs are children of NotFoundSignal (ST80 compat)
Claus Gittinger <cg@exept.de>
parents:
3075
diff
changeset
|
82 |
NotEnoughElementsSignal nameClass:self message:#notEnoughElementsSignal. |
2bc81588db6c
sigs are children of NotFoundSignal (ST80 compat)
Claus Gittinger <cg@exept.de>
parents:
3075
diff
changeset
|
83 |
NotEnoughElementsSignal notifierString:'not enough elements in collection'. |
348 | 84 |
|
3102
2bc81588db6c
sigs are children of NotFoundSignal (ST80 compat)
Claus Gittinger <cg@exept.de>
parents:
3075
diff
changeset
|
85 |
EmptyCollectionSignal := NotEnoughElementsSignal newSignalMayProceed:true. |
2bc81588db6c
sigs are children of NotFoundSignal (ST80 compat)
Claus Gittinger <cg@exept.de>
parents:
3075
diff
changeset
|
86 |
EmptyCollectionSignal nameClass:self message:#emptyCollectionSignal. |
2bc81588db6c
sigs are children of NotFoundSignal (ST80 compat)
Claus Gittinger <cg@exept.de>
parents:
3075
diff
changeset
|
87 |
EmptyCollectionSignal notifierString:'operation not allowed for empty collections'. |
54 | 88 |
] |
3102
2bc81588db6c
sigs are children of NotFoundSignal (ST80 compat)
Claus Gittinger <cg@exept.de>
parents:
3075
diff
changeset
|
89 |
|
2bc81588db6c
sigs are children of NotFoundSignal (ST80 compat)
Claus Gittinger <cg@exept.de>
parents:
3075
diff
changeset
|
90 |
"Modified: / 8.11.1997 / 19:18:17 / cg" |
10 | 91 |
! ! |
92 |
||
2097 | 93 |
!Collection class methodsFor:'instance creation'! |
155 | 94 |
|
602 | 95 |
new:size withAll:element |
5626 | 96 |
"return a new Collection of size, where all elements are |
602 | 97 |
initialized to element" |
348 | 98 |
|
602 | 99 |
|newCollection| |
92 | 100 |
|
602 | 101 |
newCollection := self new:size. |
102 |
size timesRepeat:[newCollection add:element] |
|
103 |
! |
|
1 | 104 |
|
5626 | 105 |
newFrom:aCollection |
106 |
"Return an instance of me containing the same elements as aCollection." |
|
107 |
||
108 |
^ self withAll:aCollection |
|
109 |
||
110 |
" |
|
111 |
Bag newFrom:#(1 2 3 4 4 5 6 7 7 7 ) |
|
112 |
Set newFrom:#(1 2 3 4 4 5 6 7 7 7 ) |
|
113 |
" |
|
114 |
! |
|
115 |
||
1 | 116 |
with:anObject |
117 |
"return a new Collection with one element:anObject" |
|
118 |
||
119 |
|newCollection| |
|
120 |
||
121 |
newCollection := self new. |
|
122 |
newCollection add:anObject. |
|
123 |
^ newCollection |
|
124 |
! |
|
125 |
||
126 |
with:firstObject with:secondObject |
|
127 |
"return a new Collection with two elements:firstObject and secondObject" |
|
128 |
||
129 |
|newCollection| |
|
130 |
||
131 |
newCollection := self new. |
|
1112 | 132 |
newCollection add:firstObject; add:secondObject. |
1 | 133 |
^ newCollection |
134 |
! |
|
135 |
||
136 |
with:firstObject with:secondObject with:thirdObject |
|
137 |
"return a new Collection with three elements" |
|
138 |
||
139 |
|newCollection| |
|
140 |
||
141 |
newCollection := self new. |
|
1112 | 142 |
newCollection add:firstObject; add:secondObject; add:thirdObject. |
1 | 143 |
^ newCollection |
144 |
! |
|
145 |
||
146 |
with:firstObject with:secondObject with:thirdObject with:fourthObject |
|
147 |
"return a new Collection with four elements" |
|
148 |
||
149 |
|newCollection| |
|
150 |
||
151 |
newCollection := self new. |
|
1112 | 152 |
newCollection add:firstObject; add:secondObject; add:thirdObject; add:fourthObject. |
1 | 153 |
^ newCollection |
154 |
! |
|
155 |
||
2225
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
156 |
with:a1 with:a2 with:a3 with:a4 with:a5 |
345 | 157 |
"return a new Collection with five elements" |
158 |
||
159 |
|newCollection| |
|
160 |
||
161 |
newCollection := self new. |
|
2225
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
162 |
newCollection add:a1; add:a2; add:a3; add:a4; add:a5. |
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
163 |
^ newCollection |
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
164 |
|
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
165 |
"Modified: 22.1.1997 / 19:34:01 / cg" |
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
166 |
! |
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
167 |
|
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
168 |
with:a1 with:a2 with:a3 with:a4 with:a5 with:a6 |
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
169 |
"return a new Collection with size elements" |
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
170 |
|
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
171 |
|newCollection| |
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
172 |
|
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
173 |
newCollection := self new. |
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
174 |
newCollection add:a1; add:a2; add:a3; add:a4; add:a5; add:a6. |
345 | 175 |
^ newCollection |
2225
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
176 |
|
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
177 |
"Created: 22.1.1997 / 19:34:14 / cg" |
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
178 |
! |
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
179 |
|
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
180 |
with:a1 with:a2 with:a3 with:a4 with:a5 with:a6 with:a7 |
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
181 |
"return a new Collection with seven elements" |
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
182 |
|
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
183 |
|newCollection| |
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
184 |
|
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
185 |
newCollection := self new. |
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
186 |
newCollection add:a1; add:a2; add:a3; add:a4; add:a5; add:a6; add:a7. |
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
187 |
^ newCollection |
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
188 |
|
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
189 |
"Created: 22.1.1997 / 19:34:24 / cg" |
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
190 |
! |
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
191 |
|
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
192 |
with:a1 with:a2 with:a3 with:a4 with:a5 with:a6 with:a7 with:a8 |
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
193 |
"return a new Collection with eight elements" |
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
194 |
|
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
195 |
|newCollection| |
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
196 |
|
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
197 |
newCollection := self new. |
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
198 |
newCollection add:a1; add:a2; add:a3; add:a4; add:a5; add:a6; add:a7; add:a8. |
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
199 |
^ newCollection |
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
200 |
|
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
201 |
"Created: 22.1.1997 / 19:34:34 / cg" |
345 | 202 |
! |
203 |
||
1 | 204 |
withAll:aCollection |
205 |
"return a new Collection with all elements taken from the argument, |
|
206 |
aCollection" |
|
207 |
||
208 |
|newCollection| |
|
209 |
||
210 |
newCollection := self new. |
|
211 |
newCollection addAll:aCollection. |
|
212 |
^newCollection |
|
9159 | 213 |
! |
214 |
||
215 |
withSize:n |
|
216 |
"return a new collection which really provides space for n elements. |
|
217 |
Kludges around the stupid definition of OrderedCollection>>new:" |
|
218 |
||
219 |
^ self new:n |
|
220 |
! ! |
|
221 |
||
222 |
!Collection class methodsFor:'Compatibility-Squeak'! |
|
223 |
||
224 |
ofSize:n |
|
225 |
"return a new collection which really provides space for n elements. |
|
226 |
Kludges around the stupid definition of OrderedCollection>>new:" |
|
227 |
||
228 |
^ self withSize:n |
|
1 | 229 |
! ! |
230 |
||
2097 | 231 |
!Collection class methodsFor:'Signal constants'! |
10 | 232 |
|
602 | 233 |
emptyCollectionSignal |
234 |
"return the signal used to report non-allowed operation on empty collections" |
|
345 | 235 |
|
602 | 236 |
^ EmptyCollectionSignal |
308 | 237 |
! |
238 |
||
602 | 239 |
invalidKeySignal |
240 |
"return the signal used to report bad key usage" |
|
308 | 241 |
|
602 | 242 |
^ InvalidKeySignal |
201 | 243 |
! |
244 |
||
602 | 245 |
notEnoughElementsSignal |
246 |
"return the signal used to report attempts for an operation, for which |
|
247 |
there are not enough elements in the collection" |
|
249 | 248 |
|
602 | 249 |
^ NotEnoughElementsSignal |
404 | 250 |
! |
251 |
||
602 | 252 |
valueNotFoundSignal |
253 |
"return the signal used to report a nonexisting element." |
|
404 | 254 |
|
602 | 255 |
^ ValueNotFoundSignal |
256 |
! ! |
|
404 | 257 |
|
17732
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
258 |
!Collection class methodsFor:'misc ui support'! |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
259 |
|
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
260 |
iconInBrowserSymbol |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
261 |
<resource: #programImage> |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
262 |
|
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
263 |
^ #containerClassBrowserIcon |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
264 |
! ! |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
265 |
|
2097 | 266 |
!Collection class methodsFor:'queries'! |
404 | 267 |
|
602 | 268 |
growIsCheap |
269 |
"return true, if this collection can easily grow |
|
270 |
(i.e. without a need for become:). |
|
271 |
Returns true here; this method is redefined in fix-size |
|
272 |
collections" |
|
404 | 273 |
|
602 | 274 |
^ true |
8893 | 275 |
! |
276 |
||
277 |
isAbstract |
|
11231 | 278 |
"Return if this class is an abstract class. |
279 |
True is returned for Collection here; false for subclasses. |
|
280 |
Abstract subclasses must redefine again." |
|
281 |
||
8893 | 282 |
^ self == Collection |
257 | 283 |
! ! |
284 |
||
7261 | 285 |
!Collection methodsFor:'Compatibility-Dolphin'! |
6411 | 286 |
|
287 |
identityIncludes:anObject |
|
7807 | 288 |
"return true, if the argument, anObject is in the collection. |
289 |
Same as #includesIdentical for Dolphin compatibility." |
|
290 |
||
6411 | 291 |
^ self includesIdentical:anObject. |
6869 | 292 |
! |
293 |
||
294 |
includesAnyOf:aCollection |
|
295 |
"same as #includesAny for Dolphin compatibility." |
|
296 |
||
297 |
^ self includesAny:aCollection |
|
298 |
||
299 |
" |
|
300 |
#(1 2 3 4 5 6 7) includesAny:#(1 2 3) |
|
301 |
#('hello' 'there' 'world') includesAny:#('hello' 'world') |
|
302 |
#(1 2 3 4 5 6 7) includesAny:#(7 8 9) |
|
303 |
#(1 2 3 4 5 6 7) includesAny:#(8 9 10) |
|
304 |
" |
|
17735 | 305 |
! |
306 |
||
307 |
symmetricDifference:aCollection |
|
308 |
"return a new set containing all elements, |
|
309 |
which are contained in either the receiver or aCollection, but not in both. |
|
310 |
Same as xor: - for compatibility" |
|
311 |
||
312 |
^ self xor:aCollection |
|
313 |
||
314 |
" |
|
315 |
|c1 c2| |
|
316 |
||
317 |
c1 := #( foo bar baz baloo ). |
|
318 |
c2 := #( foe bar banana baloo ). |
|
319 |
c1 symmetricDifference:c2. |
|
320 |
self assert:(c1 symmetricDifference:c2) asSet = (c2 symmetricDifference:c1) asSet |
|
321 |
" |
|
6411 | 322 |
! ! |
323 |
||
7261 | 324 |
!Collection methodsFor:'Compatibility-Squeak'! |
5752
66031c1d493a
added #withIndexDo: for Squeak compatibility
Claus Gittinger <cg@exept.de>
parents:
5651
diff
changeset
|
325 |
|
9354 | 326 |
addIfNotPresent:anObject |
7011 | 327 |
"Include anObject as one of the receiver's elements, but only if there |
328 |
is no such element already. Anwser anObject." |
|
329 |
||
330 |
(self includes: anObject) ifFalse: [self add: anObject]. |
|
331 |
^ anObject |
|
332 |
! |
|
333 |
||
6149 | 334 |
anyOne |
8858 | 335 |
"return any element from the collection. |
336 |
Report an error if there is none. |
|
6149 | 337 |
Same as #anElement - for Squeak compatibility" |
338 |
||
339 |
^ self anElement |
|
340 |
! |
|
341 |
||
17732
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
342 |
difference: aCollection |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
343 |
"Answer the set-theoretic difference of two collections." |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
344 |
|
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
345 |
^ self reject:[:each | aCollection includes: each] |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
346 |
|
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
347 |
" |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
348 |
#(0 2 4 6 8) difference:#(2 4) |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
349 |
" |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
350 |
! |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
351 |
|
17734
406b1590afe8
Merged with trunk r10476
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17732
diff
changeset
|
352 |
gather:aBlock |
406b1590afe8
Merged with trunk r10476
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17732
diff
changeset
|
353 |
"return an Array, |
406b1590afe8
Merged with trunk r10476
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17732
diff
changeset
|
354 |
containing all elements as returned from applying aBlock to each element if the receiver, |
406b1590afe8
Merged with trunk r10476
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17732
diff
changeset
|
355 |
where the block returns a collection of to-be-added elements. |
406b1590afe8
Merged with trunk r10476
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17732
diff
changeset
|
356 |
This could also be called: collectAllAsArray:" |
406b1590afe8
Merged with trunk r10476
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17732
diff
changeset
|
357 |
|
406b1590afe8
Merged with trunk r10476
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17732
diff
changeset
|
358 |
^ self gather:aBlock as:Array |
406b1590afe8
Merged with trunk r10476
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17732
diff
changeset
|
359 |
|
406b1590afe8
Merged with trunk r10476
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17732
diff
changeset
|
360 |
" |
406b1590afe8
Merged with trunk r10476
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17732
diff
changeset
|
361 |
(Set withAll:#(10 20 30 10 20 40)) gather:[:el | Array with:el with:el * 2] |
406b1590afe8
Merged with trunk r10476
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17732
diff
changeset
|
362 |
" |
406b1590afe8
Merged with trunk r10476
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17732
diff
changeset
|
363 |
! |
406b1590afe8
Merged with trunk r10476
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17732
diff
changeset
|
364 |
|
406b1590afe8
Merged with trunk r10476
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17732
diff
changeset
|
365 |
gather:aBlock as:aClass |
406b1590afe8
Merged with trunk r10476
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17732
diff
changeset
|
366 |
"return an instance of the collection-class aClass, |
406b1590afe8
Merged with trunk r10476
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17732
diff
changeset
|
367 |
containing all elements as returned from applying aBlock to each element if the receiver. |
406b1590afe8
Merged with trunk r10476
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17732
diff
changeset
|
368 |
where the block returns a collection of to-be-added elements. |
406b1590afe8
Merged with trunk r10476
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17732
diff
changeset
|
369 |
This could also be called: collectAll:as:" |
406b1590afe8
Merged with trunk r10476
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17732
diff
changeset
|
370 |
|
406b1590afe8
Merged with trunk r10476
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17732
diff
changeset
|
371 |
^ aClass streamContents:[:s | |
406b1590afe8
Merged with trunk r10476
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17732
diff
changeset
|
372 |
self do: [:ea | s nextPutAll: (aBlock value: ea)] |
406b1590afe8
Merged with trunk r10476
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17732
diff
changeset
|
373 |
] |
406b1590afe8
Merged with trunk r10476
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17732
diff
changeset
|
374 |
|
406b1590afe8
Merged with trunk r10476
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17732
diff
changeset
|
375 |
" |
406b1590afe8
Merged with trunk r10476
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17732
diff
changeset
|
376 |
(Set withAll:#(10 20 30 10 20 40)) gather:[:el | Array with:el with:el * 2] as:OrderedCollection |
406b1590afe8
Merged with trunk r10476
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17732
diff
changeset
|
377 |
(Set withAll:#(10 20 30 10 20 40)) collectAll:[:el | Array with:el with:el * 2] |
406b1590afe8
Merged with trunk r10476
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17732
diff
changeset
|
378 |
" |
406b1590afe8
Merged with trunk r10476
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17732
diff
changeset
|
379 |
! |
406b1590afe8
Merged with trunk r10476
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17732
diff
changeset
|
380 |
|
9354 | 381 |
groupBy:keyBlock having:selectBlock |
17742 | 382 |
"Like in SQL operation - Split the receiver's contents into collections of |
9354 | 383 |
elements for which keyBlock returns the same results, and return those |
384 |
collections allowed by selectBlock. " |
|
385 |
||
9319 | 386 |
|result key| |
17742 | 387 |
|
9319 | 388 |
result := OrderedDictionary new. |
17742 | 389 |
self do:[:e | |
390 |
key := keyBlock value: e. |
|
391 |
(result includesKey: key) ifFalse: [ |
|
392 |
result at: key put: OrderedCollection new |
|
393 |
]. |
|
394 |
(result at: key) add: e |
|
395 |
]. |
|
9319 | 396 |
^ result := result select: selectBlock |
397 |
||
9354 | 398 |
" |
399 |
#(1 2 3 4 5 6 7 8 9) groupBy:[:e | e odd] having:[:a |true] |
|
400 |
" |
|
9319 | 401 |
! |
402 |
||
9065 | 403 |
ifEmpty:ifEmptyValue ifNotEmpty:ifNotEmptyValue |
404 |
|action| |
|
405 |
||
406 |
action := self isEmpty ifTrue:[ ifEmptyValue ] ifFalse:[ ifNotEmptyValue ]. |
|
407 |
||
408 |
action isBlock ifTrue:[ |
|
409 |
action numArgs == 1 ifTrue:[ |
|
410 |
^ action value:self |
|
411 |
] |
|
412 |
]. |
|
413 |
^ action value |
|
414 |
! |
|
415 |
||
416 |
ifEmpty:ifEmptyValue ifNotEmptyDo:ifNotEmptyValue |
|
417 |
^ self ifEmpty:ifEmptyValue ifNotEmpty:ifNotEmptyValue |
|
418 |
! |
|
419 |
||
420 |
ifEmptyDo:ifEmptyValue ifNotEmpty:ifNotEmptyValue |
|
421 |
^ self ifEmpty:ifEmptyValue ifNotEmpty:ifNotEmptyValue |
|
422 |
! |
|
423 |
||
424 |
ifNotEmpty:ifNotEmptyValue |
|
425 |
^ self ifEmpty:nil ifNotEmpty:ifNotEmptyValue |
|
426 |
! |
|
427 |
||
428 |
ifNotEmptyDo:ifNotEmptyValue |
|
429 |
^ self ifEmpty:nil ifNotEmpty:ifNotEmptyValue |
|
430 |
! |
|
431 |
||
432 |
ifNotEmptyDo:ifNotEmptyValue ifEmpty:ifEmptyValue |
|
433 |
^ self ifEmpty:ifEmptyValue ifNotEmpty:ifNotEmptyValue |
|
434 |
! |
|
435 |
||
11280 | 436 |
intersection:aCollection |
437 |
^ self intersect:aCollection |
|
438 |
||
439 |
"Created: / 22-10-2008 / 21:29:27 / cg" |
|
440 |
! |
|
441 |
||
5752
66031c1d493a
added #withIndexDo: for Squeak compatibility
Claus Gittinger <cg@exept.de>
parents:
5651
diff
changeset
|
442 |
withIndexDo:aTwoArgBlock |
66031c1d493a
added #withIndexDo: for Squeak compatibility
Claus Gittinger <cg@exept.de>
parents:
5651
diff
changeset
|
443 |
"evaluate the argument, aBlock for every element in the collection, |
66031c1d493a
added #withIndexDo: for Squeak compatibility
Claus Gittinger <cg@exept.de>
parents:
5651
diff
changeset
|
444 |
passing both element and index as arguments." |
66031c1d493a
added #withIndexDo: for Squeak compatibility
Claus Gittinger <cg@exept.de>
parents:
5651
diff
changeset
|
445 |
|
66031c1d493a
added #withIndexDo: for Squeak compatibility
Claus Gittinger <cg@exept.de>
parents:
5651
diff
changeset
|
446 |
^ self keysAndValuesDo:[:key :index | |
66031c1d493a
added #withIndexDo: for Squeak compatibility
Claus Gittinger <cg@exept.de>
parents:
5651
diff
changeset
|
447 |
aTwoArgBlock value:index value:key |
66031c1d493a
added #withIndexDo: for Squeak compatibility
Claus Gittinger <cg@exept.de>
parents:
5651
diff
changeset
|
448 |
]. |
66031c1d493a
added #withIndexDo: for Squeak compatibility
Claus Gittinger <cg@exept.de>
parents:
5651
diff
changeset
|
449 |
! ! |
66031c1d493a
added #withIndexDo: for Squeak compatibility
Claus Gittinger <cg@exept.de>
parents:
5651
diff
changeset
|
450 |
|
1 | 451 |
!Collection methodsFor:'accessing'! |
452 |
||
602 | 453 |
anElement |
6966 | 454 |
"return any element from the collection, |
455 |
report an error if there is none" |
|
602 | 456 |
|
457 |
self do: [:each | ^ each]. |
|
6511
0ca4f36b16ca
signal an error in #anElement if there is none;
Claus Gittinger <cg@exept.de>
parents:
6506
diff
changeset
|
458 |
self emptyCollectionError. |
602 | 459 |
! |
460 |
||
1369
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1359
diff
changeset
|
461 |
atAll:indexCollection put:anObject |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1359
diff
changeset
|
462 |
"put anObject into all indexes from indexCollection in the receiver. |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1359
diff
changeset
|
463 |
This abstract implementation requires that the receiver supports |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1359
diff
changeset
|
464 |
access via a key (and indexCollection contains valid keys). |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1359
diff
changeset
|
465 |
|
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1359
diff
changeset
|
466 |
Notice: This operation modifies the receiver, NOT a copy; |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1359
diff
changeset
|
467 |
therefore the change may affect all others referencing the receiver." |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1359
diff
changeset
|
468 |
|
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1359
diff
changeset
|
469 |
indexCollection do:[:index | self at:index put:anObject] |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1359
diff
changeset
|
470 |
|
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1359
diff
changeset
|
471 |
" |
3469
2c7f6fc4685a
started args / returns comments.
Claus Gittinger <cg@exept.de>
parents:
3402
diff
changeset
|
472 |
args: indexCollection : <Collection of keys<object> > |
2c7f6fc4685a
started args / returns comments.
Claus Gittinger <cg@exept.de>
parents:
3402
diff
changeset
|
473 |
returns: self |
2c7f6fc4685a
started args / returns comments.
Claus Gittinger <cg@exept.de>
parents:
3402
diff
changeset
|
474 |
" |
2c7f6fc4685a
started args / returns comments.
Claus Gittinger <cg@exept.de>
parents:
3402
diff
changeset
|
475 |
|
2c7f6fc4685a
started args / returns comments.
Claus Gittinger <cg@exept.de>
parents:
3402
diff
changeset
|
476 |
" |
1369
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1359
diff
changeset
|
477 |
(Array new:10) atAll:(1 to:5) put:0 |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1359
diff
changeset
|
478 |
(Array new:10) atAll:#(1 5 6 9) put:0 |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1359
diff
changeset
|
479 |
(Dictionary new) atAll:#(foo bar baz) put:'hello' |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1359
diff
changeset
|
480 |
|
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1359
diff
changeset
|
481 |
raises an error: |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1359
diff
changeset
|
482 |
(Set new) atAll:#(foo bar baz) put:'hello' |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1359
diff
changeset
|
483 |
" |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1359
diff
changeset
|
484 |
|
3469
2c7f6fc4685a
started args / returns comments.
Claus Gittinger <cg@exept.de>
parents:
3402
diff
changeset
|
485 |
"Modified: / 20.5.1998 / 14:50:46 / cg" |
1369
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1359
diff
changeset
|
486 |
! |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1359
diff
changeset
|
487 |
|
4985 | 488 |
fifth |
489 |
"return the fifth element of the collection. |
|
490 |
For unordered collections, this simply returns the fifth |
|
491 |
element when enumerating them. |
|
492 |
This should be redefined in subclasses." |
|
493 |
||
494 |
^ self nth:5 |
|
495 |
||
496 |
" |
|
497 |
#(1 2 3 4 5) fifth |
|
498 |
" |
|
499 |
! |
|
500 |
||
68 | 501 |
first |
502 |
"return the first element of the collection. |
|
4855 | 503 |
For unordered collections, this simply returns the first |
504 |
element when enumerating them. |
|
68 | 505 |
This should be redefined in subclasses." |
506 |
||
507 |
self do:[:e | ^ e]. |
|
508 |
||
509 |
"error if collection is empty" |
|
155 | 510 |
^ self emptyCollectionError |
68 | 511 |
! |
512 |
||
5887 | 513 |
first:n |
514 |
"return the n first elements of the collection. |
|
515 |
Raises an error if there are not enough elements in the receiver. |
|
516 |
For unordered collections, this simply returns the first |
|
517 |
n elements when enumerating them |
|
518 |
(Warning: the contents of the returned collection is not deterministic in this case). |
|
519 |
This should be redefined in subclasses." |
|
520 |
||
521 |
|coll remain| |
|
522 |
||
523 |
n < 0 ifTrue:[self error:'bad (negative) argument']. |
|
524 |
||
525 |
coll := OrderedCollection new. |
|
526 |
remain := n. |
|
527 |
self do:[:e | |
|
528 |
coll add:e. |
|
529 |
remain := remain - 1. |
|
530 |
remain == 0 ifTrue:[ |
|
531 |
^ coll |
|
532 |
]. |
|
533 |
]. |
|
534 |
||
535 |
"error if collection has not enough elements" |
|
536 |
^ self notEnoughElementsError |
|
537 |
||
538 |
" |
|
539 |
#(1 2 3 4 5) first:3 |
|
540 |
#(1 2 3 4 5) asSet first:3 |
|
541 |
" |
|
542 |
! |
|
543 |
||
936
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
544 |
firstIfEmpty:exceptionValue |
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
545 |
"return the first element of the collection. |
10593 | 546 |
If its empty, return the exceptionValue. |
936
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
547 |
(i.e. dont trigger an error as done in #first)" |
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
548 |
|
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
549 |
self isEmpty ifTrue:[^ exceptionValue value]. |
10593 | 550 |
^ self first |
551 |
||
552 |
"Modified: / 04-06-2007 / 22:36:14 / cg" |
|
553 |
! |
|
554 |
||
555 |
firstOrNil |
|
556 |
"return the first element of the collection. |
|
557 |
If its empty, return nil. |
|
558 |
(i.e. dont trigger an error as done in #first)" |
|
559 |
||
560 |
^ self firstIfEmpty:nil |
|
561 |
||
562 |
"Created: / 04-06-2007 / 22:36:07 / cg" |
|
936
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
563 |
! |
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
564 |
|
4985 | 565 |
fourth |
566 |
"return the fourth element of the collection. |
|
567 |
For unordered collections, this simply returns the fourth |
|
568 |
element when enumerating them. |
|
569 |
This should be redefined in subclasses." |
|
570 |
||
571 |
^ self nth:4 |
|
572 |
||
573 |
" |
|
574 |
#(1 2 3 4) fourth |
|
575 |
" |
|
576 |
! |
|
577 |
||
68 | 578 |
last |
579 |
"return the last element of the collection. |
|
580 |
This should be redefined in subclasses." |
|
581 |
||
582 |
|theLastOne any| |
|
583 |
||
584 |
any := false. |
|
585 |
self do:[:e | any := true. theLastOne := e]. |
|
586 |
any ifTrue:[ |
|
6135 | 587 |
^ theLastOne |
68 | 588 |
]. |
589 |
||
590 |
"error if collection is empty" |
|
155 | 591 |
^ self emptyCollectionError |
68 | 592 |
! |
593 |
||
5887 | 594 |
last:n |
595 |
"return the n last elements of the collection. |
|
596 |
Raises an error if there are not enough elements in the receiver. |
|
597 |
For unordered collections, this simply returns the last |
|
598 |
n elements when enumerating them |
|
599 |
(Warning: the contents of the returned collection is not deterministic in this case). |
|
600 |
This should be redefined in subclasses since the implementation here is VERY inefficient." |
|
601 |
||
602 |
|coll remain| |
|
603 |
||
604 |
n < 0 ifTrue:[self error:'bad (negative) argument']. |
|
605 |
||
10798
18d87b8fce50
Clean up code and document differences between #add: and #addLast:
Stefan Vogel <sv@exept.de>
parents:
10795
diff
changeset
|
606 |
coll := OrderedCollection new:n. |
5887 | 607 |
remain := n. |
608 |
self do:[:e | |
|
609 |
remain > 0 ifTrue:[ |
|
610 |
remain := remain - 1. |
|
611 |
] ifFalse:[ |
|
612 |
coll removeFirst. |
|
613 |
]. |
|
10798
18d87b8fce50
Clean up code and document differences between #add: and #addLast:
Stefan Vogel <sv@exept.de>
parents:
10795
diff
changeset
|
614 |
coll add:e. |
5887 | 615 |
]. |
616 |
||
617 |
remain ~~ 0 ifTrue:[ |
|
618 |
"error if collection has not enough elements" |
|
619 |
^ self notEnoughElementsError |
|
620 |
]. |
|
621 |
^ coll |
|
622 |
||
623 |
" |
|
624 |
#(1 2 3 4 5) last:3 |
|
10798
18d87b8fce50
Clean up code and document differences between #add: and #addLast:
Stefan Vogel <sv@exept.de>
parents:
10795
diff
changeset
|
625 |
#(1 2 3 4 5 6 7 8 9 0) asSet last:3 |
5887 | 626 |
'hello world' last:5 |
627 |
" |
|
628 |
! |
|
629 |
||
936
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
630 |
lastIfEmpty:exceptionValue |
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
631 |
"return the last element of the collection. |
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
632 |
If its empty, return the exceptionValue. |
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
633 |
(i.e. dont trigger an error as done in #last)" |
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
634 |
|
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
635 |
self isEmpty ifTrue:[^ exceptionValue value]. |
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
636 |
^ self last |
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
637 |
|
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
638 |
"Modified: 6.2.1996 / 15:27:17 / cg" |
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
639 |
"Created: 6.2.1996 / 15:27:49 / cg" |
4855 | 640 |
! |
641 |
||
9073 | 642 |
median |
643 |
"Return the middle element, or as close as we can get." |
|
644 |
||
645 |
^ self asSortedCollection median |
|
646 |
||
647 |
" |
|
648 |
#(10 35 20 45 30 5) median |
|
649 |
" |
|
650 |
! |
|
651 |
||
4985 | 652 |
nth:n |
653 |
"return the nth element of the collection. |
|
654 |
For unordered collections, this simply returns the nth |
|
655 |
element when enumerating them. |
|
656 |
This should be redefined in subclasses." |
|
657 |
||
658 |
|count| |
|
659 |
||
660 |
count := 1. |
|
661 |
self do:[:e | |
|
662 |
count == n ifTrue:[^ e]. |
|
663 |
count := count + 1 |
|
664 |
]. |
|
665 |
||
5266 | 666 |
"error if collection is smaller" |
667 |
^ self notEnoughElementsError |
|
4985 | 668 |
|
669 |
" |
|
670 |
#(1 2 3 4) nth:3 |
|
5266 | 671 |
#(1 2 3 4) nth:5 |
672 |
||
673 |
#(1 2 3 4) asSet nth:3 |
|
674 |
#(1 2 3 4) asSet nth:5 |
|
4985 | 675 |
" |
676 |
! |
|
677 |
||
17732
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
678 |
order |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
679 |
"report an error that only OrderedXXX's have an order" |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
680 |
|
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
681 |
self shouldNotImplement |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
682 |
! |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
683 |
|
4855 | 684 |
second |
685 |
"return the second element of the collection. |
|
686 |
For unordered collections, this simply returns the second |
|
687 |
element when enumerating them. |
|
7129 | 688 |
This should be redefined in subclasses." |
689 |
||
690 |
^ self nth:2 |
|
4855 | 691 |
|
4856 | 692 |
" |
693 |
#(1 2 3) second |
|
694 |
" |
|
6135 | 695 |
! |
696 |
||
697 |
seventh |
|
698 |
"return the seventh element of the collection. |
|
699 |
For unordered collections, this simply returns the sixth |
|
700 |
element when enumerating them. |
|
701 |
This should be redefined in subclasses." |
|
702 |
||
703 |
^ self nth:7 |
|
704 |
||
705 |
" |
|
706 |
#(1 2 3 4 5 6 7) seventh |
|
707 |
" |
|
708 |
||
709 |
"Created: / 1.11.2001 / 16:43:29 / cg" |
|
4856 | 710 |
! |
711 |
||
4985 | 712 |
sixth |
713 |
"return the sixth element of the collection. |
|
714 |
For unordered collections, this simply returns the sixth |
|
715 |
element when enumerating them. |
|
716 |
This should be redefined in subclasses." |
|
717 |
||
718 |
^ self nth:6 |
|
719 |
||
720 |
" |
|
721 |
#(1 2 3 4 5 6 7) sixth |
|
722 |
" |
|
723 |
! |
|
724 |
||
4856 | 725 |
third |
726 |
"return the third element of the collection. |
|
727 |
For unordered collections, this simply returns the third |
|
728 |
element when enumerating them. |
|
729 |
This should be redefined in subclasses." |
|
730 |
||
4985 | 731 |
^ self nth:3 |
4856 | 732 |
|
733 |
||
734 |
" |
|
735 |
#(1 2 3) third |
|
736 |
" |
|
17747 | 737 |
! |
738 |
||
739 |
values |
|
740 |
"return a collection containing all values of the receiver. |
|
741 |
This is to make value access to an OrderedDictionary compatible with any-Collection" |
|
742 |
||
743 |
|aCollection| |
|
744 |
||
745 |
aCollection := OrderedCollection new. |
|
746 |
self do:[:value| aCollection add:value]. |
|
747 |
^ aCollection |
|
1 | 748 |
! ! |
749 |
||
750 |
!Collection methodsFor:'adding & removing'! |
|
751 |
||
752 |
add:anObject |
|
88 | 753 |
"add the argument, anObject to the receiver. |
754 |
If the receiver is ordered, the position of the new element is undefined |
|
7168
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
755 |
(i.e. don't depend on where it will be put). |
2351 | 756 |
An error is raised here - it is to be implemented by a concrete subclass." |
1 | 757 |
|
758 |
^ self subclassResponsibility |
|
2351 | 759 |
|
760 |
"Modified: 1.2.1997 / 11:57:08 / cg" |
|
1 | 761 |
! |
762 |
||
1373
2c7cab1a4a2e
oops - typo; occurrences has two r's
Claus Gittinger <cg@exept.de>
parents:
1370
diff
changeset
|
763 |
add:newObject withOccurrences:anInteger |
2c7cab1a4a2e
oops - typo; occurrences has two r's
Claus Gittinger <cg@exept.de>
parents:
1370
diff
changeset
|
764 |
"add the argument, anObject anInteger times to the receiver. |
2c7cab1a4a2e
oops - typo; occurrences has two r's
Claus Gittinger <cg@exept.de>
parents:
1370
diff
changeset
|
765 |
Returns the object." |
2c7cab1a4a2e
oops - typo; occurrences has two r's
Claus Gittinger <cg@exept.de>
parents:
1370
diff
changeset
|
766 |
|
2c7cab1a4a2e
oops - typo; occurrences has two r's
Claus Gittinger <cg@exept.de>
parents:
1370
diff
changeset
|
767 |
anInteger timesRepeat:[self add:newObject]. |
2c7cab1a4a2e
oops - typo; occurrences has two r's
Claus Gittinger <cg@exept.de>
parents:
1370
diff
changeset
|
768 |
^ newObject |
2c7cab1a4a2e
oops - typo; occurrences has two r's
Claus Gittinger <cg@exept.de>
parents:
1370
diff
changeset
|
769 |
|
2c7cab1a4a2e
oops - typo; occurrences has two r's
Claus Gittinger <cg@exept.de>
parents:
1370
diff
changeset
|
770 |
"Created: 11.5.1996 / 12:13:48 / cg" |
2c7cab1a4a2e
oops - typo; occurrences has two r's
Claus Gittinger <cg@exept.de>
parents:
1370
diff
changeset
|
771 |
! |
2c7cab1a4a2e
oops - typo; occurrences has two r's
Claus Gittinger <cg@exept.de>
parents:
1370
diff
changeset
|
772 |
|
1 | 773 |
addAll:aCollection |
1163 | 774 |
"add all elements of the argument, aCollection to the receiver. |
7752 | 775 |
Returns the argument, aCollection (sigh)." |
1 | 776 |
|
777 |
aCollection do:[:element | |
|
1163 | 778 |
self add:element |
1 | 779 |
]. |
780 |
^ aCollection |
|
88 | 781 |
|
782 |
" |
|
783 |
#(1 2 3 4) copy addAll:#(5 6 7 8) |
|
784 |
#(1 2 3 4) asOrderedCollection addAll:#(5 6 7 8) |
|
785 |
" |
|
1163 | 786 |
|
787 |
"Modified: 12.4.1996 / 13:29:20 / cg" |
|
1 | 788 |
! |
789 |
||
12 | 790 |
addAllFirst:aCollection |
791 |
"insert all elements of the argument, aCollection at the beginning |
|
1163 | 792 |
of the receiver. Returns the argument, aCollection." |
12 | 793 |
|
794 |
aCollection reverseDo:[:element | |
|
1163 | 795 |
self addFirst:element |
12 | 796 |
]. |
797 |
^ aCollection |
|
798 |
||
799 |
" |
|
800 |
|c| |
|
801 |
c := #(4 3 2 1) asOrderedCollection. |
|
1163 | 802 |
c addAllFirst:#(9 8 7 6 5). |
803 |
c |
|
12 | 804 |
" |
1163 | 805 |
|
806 |
"Modified: 12.4.1996 / 13:30:10 / cg" |
|
12 | 807 |
! |
808 |
||
602 | 809 |
addAllLast:aCollection |
1163 | 810 |
"add all elements of the argument, aCollection to the receiver. |
811 |
Returns the argument, aCollection." |
|
602 | 812 |
|
813 |
aCollection do:[:element | |
|
1163 | 814 |
self addLast:element |
602 | 815 |
]. |
816 |
^ aCollection |
|
817 |
||
818 |
" |
|
819 |
|c| |
|
820 |
c := #(4 3 2 1) asOrderedCollection. |
|
821 |
c addAllLast:#(9 8 7 6 5) |
|
822 |
" |
|
1163 | 823 |
|
824 |
"Modified: 12.4.1996 / 13:30:54 / cg" |
|
602 | 825 |
! |
826 |
||
827 |
addFirst:anObject |
|
828 |
"add the argument, anObject to the receiver. |
|
2351 | 829 |
If the receiver is ordered, the new element will be added at the beginning. |
830 |
An error is raised here - it is to be implemented by a concrete subclass." |
|
1 | 831 |
|
832 |
^ self subclassResponsibility |
|
2351 | 833 |
|
834 |
"Modified: 1.2.1997 / 11:57:19 / cg" |
|
1 | 835 |
! |
836 |
||
602 | 837 |
addLast:anObject |
838 |
"add the argument, anObject to the receiver. |
|
1163 | 839 |
If the receiver is ordered, the new element will be added at the end. |
10798
18d87b8fce50
Clean up code and document differences between #add: and #addLast:
Stefan Vogel <sv@exept.de>
parents:
10795
diff
changeset
|
840 |
Return the argument, anObject. |
18d87b8fce50
Clean up code and document differences between #add: and #addLast:
Stefan Vogel <sv@exept.de>
parents:
10795
diff
changeset
|
841 |
|
18d87b8fce50
Clean up code and document differences between #add: and #addLast:
Stefan Vogel <sv@exept.de>
parents:
10795
diff
changeset
|
842 |
This usually has the same semantics as #add:. |
18d87b8fce50
Clean up code and document differences between #add: and #addLast:
Stefan Vogel <sv@exept.de>
parents:
10795
diff
changeset
|
843 |
OrderedSet and OrderedDictionary redefine this, to move anObject to |
18d87b8fce50
Clean up code and document differences between #add: and #addLast:
Stefan Vogel <sv@exept.de>
parents:
10795
diff
changeset
|
844 |
the end, even if it is already present in the collection." |
602 | 845 |
|
846 |
^ self add:anObject |
|
1163 | 847 |
|
848 |
"Modified: 12.4.1996 / 13:31:12 / cg" |
|
602 | 849 |
! |
850 |
||
3124 | 851 |
contents:aCollection |
3767
bbf560999d90
care for nil arg in #contents:
Claus Gittinger <cg@exept.de>
parents:
3469
diff
changeset
|
852 |
"set my contents from aCollection |
5265 | 853 |
- this may be redefined in a concrete subclass for more performance" |
3124 | 854 |
|
6042
04414166010e
check for bad arg in #contents:
Claus Gittinger <cg@exept.de>
parents:
5887
diff
changeset
|
855 |
aCollection == self ifTrue:[ |
04414166010e
check for bad arg in #contents:
Claus Gittinger <cg@exept.de>
parents:
5887
diff
changeset
|
856 |
"/ self error:'should not happen'. |
04414166010e
check for bad arg in #contents:
Claus Gittinger <cg@exept.de>
parents:
5887
diff
changeset
|
857 |
^ self |
04414166010e
check for bad arg in #contents:
Claus Gittinger <cg@exept.de>
parents:
5887
diff
changeset
|
858 |
]. |
04414166010e
check for bad arg in #contents:
Claus Gittinger <cg@exept.de>
parents:
5887
diff
changeset
|
859 |
|
3767
bbf560999d90
care for nil arg in #contents:
Claus Gittinger <cg@exept.de>
parents:
3469
diff
changeset
|
860 |
self removeAll. |
bbf560999d90
care for nil arg in #contents:
Claus Gittinger <cg@exept.de>
parents:
3469
diff
changeset
|
861 |
aCollection notNil ifTrue:[ |
bbf560999d90
care for nil arg in #contents:
Claus Gittinger <cg@exept.de>
parents:
3469
diff
changeset
|
862 |
self addAll:aCollection |
bbf560999d90
care for nil arg in #contents:
Claus Gittinger <cg@exept.de>
parents:
3469
diff
changeset
|
863 |
] |
3124 | 864 |
|
3767
bbf560999d90
care for nil arg in #contents:
Claus Gittinger <cg@exept.de>
parents:
3469
diff
changeset
|
865 |
"Modified: / 17.8.1998 / 10:18:43 / cg" |
3124 | 866 |
! |
867 |
||
1 | 868 |
remove:anObject |
2351 | 869 |
"search for the first element, which is equal to anObject; |
870 |
if found, remove and return it. |
|
871 |
If not found, report a 'value not found'-error. |
|
872 |
Uses equality compare (=) to search for the occurrence." |
|
1 | 873 |
|
1163 | 874 |
^ self remove:anObject ifAbsent:[self errorValueNotFound:anObject] |
875 |
||
2351 | 876 |
"Modified: 1.2.1997 / 11:58:48 / cg" |
1 | 877 |
! |
878 |
||
602 | 879 |
remove:anObject ifAbsent:exceptionBlock |
2351 | 880 |
"search for the first element, which is equal to anObject; |
881 |
if found, remove and return it. |
|
882 |
If not found, return the the value of the exceptionBlock. |
|
883 |
Uses equality compare (=) to search for the occurrence. |
|
884 |
An error is raised here - it is to be implemented by a concrete subclass." |
|
602 | 885 |
|
886 |
^ self subclassResponsibility |
|
2351 | 887 |
|
888 |
"Modified: 1.2.1997 / 11:56:53 / cg" |
|
602 | 889 |
! |
890 |
||
134 | 891 |
removeAll |
1163 | 892 |
"remove all elements from the receiver. Returns the receiver. |
134 | 893 |
This should be reimplemented in subclasses for better |
894 |
performance." |
|
895 |
||
2383 | 896 |
[self isEmpty] whileFalse:[ |
1163 | 897 |
self removeFirst |
134 | 898 |
]. |
1163 | 899 |
|
2383 | 900 |
"Modified: 12.2.1997 / 12:40:29 / cg" |
134 | 901 |
! |
902 |
||
1 | 903 |
removeAll:aCollection |
360 | 904 |
"remove all elements of the argument, aCollection from the receiver. |
1163 | 905 |
Return the argument, aCollection. |
5267 | 906 |
Raises an error, if some element-to-remove is not in the receiver. |
907 |
(see also: #removeAllFoundIn:, which does not raise an error). |
|
1163 | 908 |
|
360 | 909 |
Notice: for some collections (those not tuned for |
1163 | 910 |
resizing themself) this may be very slow. |
911 |
If the number of removed elements is big compared to to |
|
912 |
the receivers size, it may be better to copy the |
|
913 |
ones which are not to be removed into a new collection." |
|
1 | 914 |
|
915 |
aCollection do:[:element | self remove:element]. |
|
916 |
^ aCollection |
|
1163 | 917 |
|
5268 | 918 |
" |
919 |
|coll| |
|
920 |
||
921 |
coll := #(1 2 3 4 5 6) asSet. |
|
922 |
coll removeAll:#(4 5 6). |
|
923 |
coll |
|
924 |
" |
|
925 |
||
926 |
"raises an error: |
|
927 |
|coll| |
|
928 |
||
929 |
coll := #(1 2 3 4 5 6) asSet. |
|
930 |
coll removeAll:#(4 5 6 7 8). |
|
931 |
coll |
|
932 |
" |
|
933 |
||
934 |
"no error raised: |
|
935 |
|coll| |
|
936 |
||
937 |
coll := #(1 2 3 4 5 6) asSet. |
|
938 |
coll removeAllFoundIn:#(4 5 6 7 8). |
|
939 |
coll |
|
940 |
" |
|
941 |
||
942 |
||
348 | 943 |
! |
944 |
||
5265 | 945 |
removeAllFoundIn:aCollection |
5267 | 946 |
"Remove each element of aCollection, which is present in the receiver |
947 |
from the receiver. |
|
948 |
No error is raised, if some element-to-remove is not in the receiver. |
|
949 |
(see also: #removeAll:, which does raise an error)." |
|
4980 | 950 |
|
951 |
aCollection do:[:each | self remove:each ifAbsent:[]]. |
|
5265 | 952 |
^ aCollection |
5268 | 953 |
|
954 |
" |
|
955 |
|coll| |
|
956 |
||
957 |
coll := #(1 2 3 4 5 6) asSet. |
|
958 |
coll removeAllFoundIn:#(4 5 6 7 8). |
|
959 |
coll |
|
960 |
" |
|
961 |
||
962 |
"raises an error: |
|
963 |
|coll| |
|
964 |
||
965 |
coll := #(1 2 3 4 5 6) asSet. |
|
966 |
coll removeAll:#(4 5 6 7 8). |
|
967 |
coll |
|
968 |
" |
|
969 |
||
4980 | 970 |
! |
971 |
||
17746 | 972 |
removeAllKeys:aCollection |
973 |
"remove all keys of the argument, aCollection from the receiver. |
|
974 |
Raises an error, if some element-to-remove is not in the receiver. |
|
975 |
Notice: only works for keyed collections, such as dictionaries." |
|
976 |
||
977 |
aCollection do:[:element | self removeKey:element]. |
|
978 |
! |
|
979 |
||
5265 | 980 |
removeAllSuchThat:aBlock |
4980 | 981 |
"Apply the condition to each element and remove it if the condition is true. |
5265 | 982 |
Return a collection of removed elements. |
5267 | 983 |
First elements-to-remove are collected, then removed in one operation." |
4980 | 984 |
|
5267 | 985 |
|removedElements| |
4980 | 986 |
|
5267 | 987 |
removedElements := OrderedCollection new. |
988 |
self do:[:element | |
|
4980 | 989 |
(aBlock value:element) ifTrue: [ |
5267 | 990 |
removedElements add:element |
4980 | 991 |
] |
992 |
]. |
|
5267 | 993 |
self removeAll:removedElements. |
994 |
^ removedElements |
|
995 |
||
996 |
" |
|
997 |
|coll| |
|
998 |
||
999 |
coll := #(1 2 3 4 5 6 7 8 9 10) asOrderedCollection. |
|
1000 |
coll removeAllSuchThat:[:el | el even]. |
|
1001 |
coll |
|
1002 |
" |
|
1003 |
" |
|
1004 |
|coll| |
|
1005 |
||
1006 |
coll := #(1 2 3 4 5 6 7 8 9 10) asSet. |
|
1007 |
coll removeAllSuchThat:[:el | el even]. |
|
1008 |
coll |
|
1009 |
" |
|
1010 |
||
4980 | 1011 |
! |
1012 |
||
348 | 1013 |
removeFirst |
1014 |
"remove the first element from the receiver. |
|
1015 |
Return the removed element." |
|
1016 |
||
1561
1f64a9b522c2
at least a suboptimal #removeFirst: can be provided by Collection
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
1017 |
self do:[:element | |
1f64a9b522c2
at least a suboptimal #removeFirst: can be provided by Collection
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
1018 |
self remove:element. |
1f64a9b522c2
at least a suboptimal #removeFirst: can be provided by Collection
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
1019 |
^ element. |
1f64a9b522c2
at least a suboptimal #removeFirst: can be provided by Collection
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
1020 |
]. |
1f64a9b522c2
at least a suboptimal #removeFirst: can be provided by Collection
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
1021 |
^ self emptyCollectionError |
1f64a9b522c2
at least a suboptimal #removeFirst: can be provided by Collection
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
1022 |
|
1f64a9b522c2
at least a suboptimal #removeFirst: can be provided by Collection
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
1023 |
" |
1f64a9b522c2
at least a suboptimal #removeFirst: can be provided by Collection
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
1024 |
(Set with:3 with:2 with:1) removeFirst |
1f64a9b522c2
at least a suboptimal #removeFirst: can be provided by Collection
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
1025 |
" |
1f64a9b522c2
at least a suboptimal #removeFirst: can be provided by Collection
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
1026 |
|
1f64a9b522c2
at least a suboptimal #removeFirst: can be provided by Collection
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
1027 |
"Modified: 28.6.1996 / 18:55:33 / cg" |
348 | 1028 |
! |
1029 |
||
1030 |
removeFirst:n |
|
1031 |
"remove the first n elements from the receiver. |
|
5265 | 1032 |
Return a collection of removed elements. |
360 | 1033 |
Notice: for some collections (those not tuned for |
5265 | 1034 |
resizing themself) this may be very slow." |
348 | 1035 |
|
1036 |
|ret| |
|
1037 |
||
1038 |
self size < n ifTrue:[ |
|
5265 | 1039 |
^ self notEnoughElementsError |
348 | 1040 |
]. |
1041 |
ret := Array new:n. |
|
1042 |
1 to:n do:[:i | |
|
5265 | 1043 |
ret at:i put:(self removeFirst). |
348 | 1044 |
]. |
1045 |
^ ret |
|
1046 |
! |
|
1047 |
||
2351 | 1048 |
removeIdentical:anObject |
1049 |
"search for the first element, which is identical to anObject; |
|
1050 |
if found, remove and return it. |
|
1051 |
If not found, report a 'value not found'-error. |
|
1052 |
Uses identity compare (==) to search for the occurrence." |
|
1053 |
||
1054 |
^ self removeIdentical:anObject ifAbsent:[self errorValueNotFound:anObject] |
|
1055 |
||
1056 |
"Modified: 12.4.1996 / 13:33:30 / cg" |
|
1057 |
"Created: 1.2.1997 / 11:59:10 / cg" |
|
1058 |
! |
|
1059 |
||
1060 |
removeIdentical:anObject ifAbsent:exceptionBlock |
|
1061 |
"search for the first element, which is identical to anObject; |
|
1062 |
if found, remove and return it. |
|
1063 |
If not found, return the the value of the exceptionBlock. |
|
1064 |
Uses identity compare (==) to search for the occurrence. |
|
1065 |
An error is raised here - it is to be implemented by a concrete subclass." |
|
1066 |
||
1067 |
^ self subclassResponsibility |
|
1068 |
||
1069 |
"Created: 1.2.1997 / 11:56:01 / cg" |
|
1070 |
"Modified: 1.2.1997 / 11:56:59 / cg" |
|
1071 |
! |
|
1072 |
||
602 | 1073 |
removeLast |
1074 |
"remove the last element from the receiver. |
|
2351 | 1075 |
Return the removed element. |
1076 |
An error is raised here - it is to be implemented by a concrete subclass." |
|
602 | 1077 |
|
1078 |
^ self subclassResponsibility |
|
2351 | 1079 |
|
1080 |
"Modified: 1.2.1997 / 11:57:53 / cg" |
|
602 | 1081 |
! |
1082 |
||
348 | 1083 |
removeLast:n |
11331 | 1084 |
"remove the last n elements from the receiver collection. |
5265 | 1085 |
Return a collection of removed elements. |
11331 | 1086 |
Notice: for some collections this may be very slow |
1087 |
(those not tuned for resizing themself)." |
|
348 | 1088 |
|
1089 |
|ret| |
|
1090 |
||
1091 |
self size < n ifTrue:[ |
|
5265 | 1092 |
^ self notEnoughElementsError |
348 | 1093 |
]. |
1094 |
ret := Array new:n. |
|
1095 |
n to:1 by:-1 do:[:i | |
|
5265 | 1096 |
ret at:i put:(self removeLast). |
348 | 1097 |
]. |
1098 |
^ ret |
|
1 | 1099 |
! ! |
1100 |
||
8858 | 1101 |
!Collection methodsFor:'bulk operations'! |
1102 |
||
17732
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1103 |
abs |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1104 |
"Absolute value of all elements in the collection" |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1105 |
|
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1106 |
^ self collect: [:a | a abs] |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1107 |
|
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1108 |
" |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1109 |
TestCase assert:( { 1. -2. -3. 4 } abs = { 1. 2. 3. 4 }). |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1110 |
" |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1111 |
! |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1112 |
|
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1113 |
negated |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1114 |
"Negated value of all elements in the collection" |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1115 |
|
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1116 |
^ self collect:[:a | a negated] |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1117 |
|
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1118 |
" |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1119 |
TestCase assert:( { 1. -2. -3. 4 } negated = { -1. 2. 3. -4 }). |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1120 |
" |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1121 |
! |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1122 |
|
11468 | 1123 |
product |
11498 | 1124 |
"multiply up all elements." |
8858 | 1125 |
|
11468 | 1126 |
self emptyCheck. |
1127 |
^ self |
|
11469 | 1128 |
inject:(self first class unity) |
1129 |
into:[:accum :each | accum * each]. |
|
8858 | 1130 |
|
1131 |
" |
|
11468 | 1132 |
TestCase should:[ Array new sum ] raise:Error. |
1133 |
||
1134 |
TestCase assert:( { 1 } product == 1). |
|
1135 |
TestCase assert:( { 6 } product == 6). |
|
1136 |
TestCase assert:( { 1. 2. 3. 4. 5. } product = 5 factorial ) |
|
1137 |
" |
|
1138 |
! |
|
1139 |
||
1140 |
sum |
|
11498 | 1141 |
"sum up all elements; return 0 for an empty collection." |
11468 | 1142 |
|
11489
6009fd847761
sum returns 0 when applied to an empty receiver
Claus Gittinger <cg@exept.de>
parents:
11475
diff
changeset
|
1143 |
self isEmpty ifTrue:[^ 0]. |
6009fd847761
sum returns 0 when applied to an empty receiver
Claus Gittinger <cg@exept.de>
parents:
11475
diff
changeset
|
1144 |
|
11468 | 1145 |
^ self |
1146 |
inject:(self first class zero) |
|
1147 |
into:[:accum :each | accum + each]. |
|
1148 |
||
1149 |
||
1150 |
" |
|
11489
6009fd847761
sum returns 0 when applied to an empty receiver
Claus Gittinger <cg@exept.de>
parents:
11475
diff
changeset
|
1151 |
TestCase assert: ( { } sum = 0 ). |
11468 | 1152 |
TestCase assert: ( { 1 } sum = 1 ). |
1153 |
TestCase assert: ( { 1. 2. 3. 4. } sum = 10 ). |
|
1154 |
TestCase assert: ( (1 to:10) sum = 55 ). |
|
1155 |
TestCase assert: ( 'abc' asByteArray sum = 294 ). |
|
1156 |
TestCase assert: ( { 10 +/- 2. |
|
1157 |
20 +/- 4. |
|
1158 |
100 +/- 10 } sum = (130 +/- 16) ). |
|
1159 |
||
1160 |
TestCase assert: ( { (1 / 9). |
|
1161 |
(1 / 7). |
|
1162 |
} sum = (16 / 63) ). |
|
8858 | 1163 |
" |
11514 | 1164 |
! |
1165 |
||
1166 |
sum:aBlock |
|
1167 |
"for each element in the receiver, evaluate the argument, aBlock |
|
1168 |
and sum up the results. Return the total sum or 0 for an empty collection. |
|
1169 |
Similar to (self collect...) sum, but avoids creation of an intermediate collection." |
|
1170 |
||
1171 |
|sum| |
|
1172 |
||
1173 |
self do:[:element | |
|
1174 |
|thisValue| |
|
1175 |
||
1176 |
thisValue := aBlock value:element. |
|
1177 |
sum isNil ifTrue:[ |
|
1178 |
sum := thisValue |
|
1179 |
] ifFalse:[ |
|
1180 |
sum := sum + thisValue |
|
1181 |
]. |
|
1182 |
]. |
|
1183 |
^ sum ? 0 |
|
1184 |
||
1185 |
" |
|
1186 |
((1 to:10) collect:[:n | n squared]) sum |
|
1187 |
((1 to:10) sum:[:n | n squared]) |
|
1188 |
" |
|
8858 | 1189 |
! ! |
1190 |
||
602 | 1191 |
!Collection methodsFor:'converting'! |
1192 |
||
1193 |
asArray |
|
1194 |
"return a new Array with the collections elements" |
|
1195 |
||
1196 |
|anArray |
|
1197 |
index "{ Class: SmallInteger }" | |
|
1 | 1198 |
|
602 | 1199 |
anArray := Array new:(self size). |
1200 |
index := 1. |
|
1201 |
self do:[:each | |
|
6135 | 1202 |
anArray at:index put:each. |
1203 |
index := index + 1 |
|
602 | 1204 |
]. |
1205 |
^ anArray |
|
1 | 1206 |
! |
1207 |
||
602 | 1208 |
asBag |
1209 |
"return a new Bag with the receiver collections elements" |
|
1 | 1210 |
|
602 | 1211 |
^ self addAllTo:(Bag new) |
1 | 1212 |
! |
1213 |
||
602 | 1214 |
asByteArray |
1215 |
"return a new ByteArray with the collections elements |
|
17732
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1216 |
(which must convert to 8bit integers in the range 0..255)." |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1217 |
|
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1218 |
^ self asIntegerArray:ByteArray |
1 | 1219 |
! |
1220 |
||
5607
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5557
diff
changeset
|
1221 |
asCollection |
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5557
diff
changeset
|
1222 |
"return myself as a Collection. |
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5557
diff
changeset
|
1223 |
I am already a Collection." |
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5557
diff
changeset
|
1224 |
|
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5557
diff
changeset
|
1225 |
^ self |
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5557
diff
changeset
|
1226 |
|
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5557
diff
changeset
|
1227 |
|
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5557
diff
changeset
|
1228 |
! |
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5557
diff
changeset
|
1229 |
|
7906 | 1230 |
asDictionary |
1231 |
"return a new Dictionary with the receiver collections elements" |
|
1232 |
||
1233 |
|d| |
|
1234 |
||
1235 |
d := Dictionary new. |
|
1236 |
self keysAndValuesDo:[:k :v | d at:k put:v]. |
|
1237 |
^ d |
|
1238 |
||
1239 |
" |
|
1240 |
#(10 20 30 40 50 60 70 80 90) asDictionary |
|
1241 |
" |
|
1242 |
! |
|
1243 |
||
602 | 1244 |
asDoubleArray |
1245 |
"return a new DoubleArray with the collections elements |
|
17732
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1246 |
(which must convert to 64bit floats)." |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1247 |
|
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1248 |
^ self asIntegerArray:DoubleArray |
602 | 1249 |
! |
68 | 1250 |
|
11412 | 1251 |
asFlatOrderedCollection |
1252 |
|coll| |
|
1253 |
||
1254 |
coll := OrderedCollection new. |
|
1255 |
self flatDo:[:el | coll add:el]. |
|
1256 |
^ coll. |
|
1257 |
||
1258 |
" |
|
1259 |
#( |
|
1260 |
(1 2 3) |
|
1261 |
4 5 |
|
1262 |
(6) |
|
1263 |
7 |
|
1264 |
(8 (9 10) 11 12 (13 (14 (15) 16)))) asFlatOrderedCollection |
|
1265 |
" |
|
1266 |
! |
|
1267 |
||
602 | 1268 |
asFloatArray |
1269 |
"return a new FloatArray with the collections elements |
|
17732
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1270 |
(which must convert to 32bit floats)." |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1271 |
|
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1272 |
^ self asIntegerArray:FloatArray |
602 | 1273 |
! |
1274 |
||
1275 |
asIdentitySet |
|
1276 |
"return a new IdentitySet with the receiver collections elements" |
|
1277 |
||
10795
3797cbc3fcb4
Do not add nil elements to a Set in #asSet and #asIdentitySet
Stefan Vogel <sv@exept.de>
parents:
10750
diff
changeset
|
1278 |
^ self addAllNonNilElementsTo:(IdentitySet new:self size) |
68 | 1279 |
! |
1280 |
||
17732
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1281 |
asIntegerArray |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1282 |
"return a new IntegerArray with the collection's elements |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1283 |
(which must convert to 32bit integers in the range 0..16rFFFFFFFF)." |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1284 |
|
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1285 |
^ self asIntegerArray:IntegerArray |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1286 |
! |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1287 |
|
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1288 |
asIntegerArray:arrayClass |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1289 |
"return a new Array with the collection's elements" |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1290 |
|
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1291 |
|anIntegerArray |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1292 |
index "{ Class: SmallInteger }" | |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1293 |
|
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1294 |
anIntegerArray := arrayClass new:(self size). |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1295 |
index := 1. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1296 |
self do:[:each | |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1297 |
anIntegerArray at:index put:each asInteger. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1298 |
index := index + 1 |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1299 |
]. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1300 |
^ anIntegerArray |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1301 |
! |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1302 |
|
2167 | 1303 |
asList |
1304 |
"return a new List with the receiver collections elements" |
|
1305 |
||
1306 |
^ self addAllTo:(List new:self size) |
|
1307 |
||
1308 |
"Created: 14.2.1997 / 16:25:23 / cg" |
|
1309 |
! |
|
1310 |
||
8050 | 1311 |
asLongIntegerArray |
1312 |
"return a new LongIntegerArray with the collections elements |
|
17732
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1313 |
(which must convert to 64bit integers in the range 0..16rFFFFFFFFFFFFFFFF)." |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1314 |
|
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1315 |
^ self asIntegerArray:LongIntegerArray |
8050 | 1316 |
! |
1317 |
||
602 | 1318 |
asOrderedCollection |
1319 |
"return a new OrderedCollection with the receiver collections elements" |
|
1320 |
||
1321 |
^ self addAllTo:(OrderedCollection new:self size) |
|
1322 |
! |
|
1323 |
||
11469 | 1324 |
asRunArray |
1325 |
"return a new RunArray with the collections elements" |
|
1326 |
||
1327 |
^ RunArray from:self. |
|
1328 |
||
1329 |
"/ |runs lastElement occurrences| |
|
1330 |
"/ |
|
1331 |
"/ runs := RunArray new. |
|
1332 |
"/ occurrences := 0. |
|
1333 |
"/ self do:[:each | |
|
1334 |
"/ each == lastElement ifTrue:[ |
|
1335 |
"/ occurrences := occurrences + 1 |
|
1336 |
"/ ] ifFalse:[ |
|
1337 |
"/ runs add:lastElement withOccurrences:occurrences. |
|
1338 |
"/ occurrences := 1. |
|
1339 |
"/ lastElement := each |
|
1340 |
"/ ]. |
|
1341 |
"/ ]. |
|
1342 |
"/ occurrences ~~ 0 ifTrue:[ |
|
1343 |
"/ runs add:lastElement withOccurrences:occurrences |
|
1344 |
"/ ]. |
|
1345 |
"/ ^ runs |
|
1394 | 1346 |
|
1347 |
" |
|
11469 | 1348 |
#(1 2 3 3 3 4 4 4 4 5 6 7) asRunArray |
1394 | 1349 |
" |
11469 | 1350 |
|
1351 |
"Modified: / 7.4.1998 / 09:50:54 / cg" |
|
1394 | 1352 |
! |
1353 |
||
5607
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5557
diff
changeset
|
1354 |
asSequenceableCollection |
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5557
diff
changeset
|
1355 |
"return myself as a SequenceableCollection. |
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5557
diff
changeset
|
1356 |
I am already a Collection, but not sequenceable." |
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5557
diff
changeset
|
1357 |
|
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5557
diff
changeset
|
1358 |
^ self asArray |
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5557
diff
changeset
|
1359 |
|
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5557
diff
changeset
|
1360 |
|
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5557
diff
changeset
|
1361 |
! |
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5557
diff
changeset
|
1362 |
|
602 | 1363 |
asSet |
1364 |
"return a new Set with the receiver collections elements" |
|
1365 |
||
10795
3797cbc3fcb4
Do not add nil elements to a Set in #asSet and #asIdentitySet
Stefan Vogel <sv@exept.de>
parents:
10750
diff
changeset
|
1366 |
^ self addAllNonNilElementsTo:(Set new:self size) |
602 | 1367 |
! |
1368 |
||
9353 | 1369 |
asSharedCollection |
1370 |
"return a shared collection on the receiver. |
|
1371 |
This implements synchronized access to me." |
|
1372 |
||
1373 |
^ SharedCollection for:self. |
|
1374 |
! |
|
1375 |
||
602 | 1376 |
asSortedCollection |
1377 |
"return a new SortedCollection with the receiver collections elements" |
|
1 | 1378 |
|
602 | 1379 |
|aSortedCollection| |
1380 |
||
1381 |
aSortedCollection := SortedCollection new:self size. |
|
1382 |
aSortedCollection addAll:self. |
|
1383 |
^ aSortedCollection |
|
1384 |
! |
|
1385 |
||
1386 |
asSortedCollection:sortBlock |
|
1387 |
"return a new SortedCollection with the receiver collections elements, |
|
1388 |
using sortBlock for comparing" |
|
1389 |
||
1390 |
|aSortedCollection| |
|
1391 |
||
1392 |
aSortedCollection := SortedCollection sortBlock:sortBlock. |
|
1393 |
aSortedCollection addAll:self. |
|
1394 |
^ aSortedCollection |
|
1 | 1395 |
! |
1396 |
||
2919 | 1397 |
asSortedStrings |
1398 |
"Create & return a SortedCollection that sorts the receivers |
|
1399 |
elements according to the locales collating policy. |
|
1400 |
This is currently not really support - strings are sorted |
|
1401 |
without caring for the locale." |
|
1402 |
||
1403 |
|aSortedCollection| |
|
1404 |
||
1405 |
aSortedCollection := SortedCollection forStrings:self size. |
|
1406 |
aSortedCollection addAll:self. |
|
1407 |
^ aSortedCollection |
|
1408 |
||
1409 |
"Created: 13.9.1997 / 09:36:22 / cg" |
|
1410 |
"Modified: 13.9.1997 / 09:43:00 / cg" |
|
1411 |
! |
|
1412 |
||
1413 |
asSortedStrings:sortBlock |
|
1414 |
"Create & return a SortedCollection that sorts the receivers |
|
3061 | 1415 |
elements using sortBlock and according to the locales collating policy, |
1416 |
which is passed as first arg to sortBlock. |
|
2919 | 1417 |
This is currently not really support - strings are sorted |
1418 |
without caring for the locale." |
|
1419 |
||
1420 |
|aSortedCollection| |
|
1421 |
||
1422 |
aSortedCollection := SortedCollection forStrings:self size. |
|
1423 |
aSortedCollection sortBlock:sortBlock. |
|
1424 |
aSortedCollection addAll:self. |
|
1425 |
^ aSortedCollection |
|
1426 |
||
3061 | 1427 |
"Created: / 13.9.1997 / 09:36:45 / cg" |
1428 |
"Modified: / 27.10.1997 / 16:39:48 / cg" |
|
2919 | 1429 |
! |
1430 |
||
1431 |
asSortedStrings:sortBlock with:aCollationPolicy |
|
1432 |
"Create & return a SortedCollection that sorts the receivers |
|
1433 |
elements using sortBlock and according to the specified locales collating policy. |
|
1434 |
This is currently not really support - strings are sorted |
|
1435 |
without caring for the locale." |
|
1436 |
||
1437 |
|aSortedCollection| |
|
1438 |
||
1439 |
aSortedCollection := SortedCollection forStrings:self size collatedBy:aCollationPolicy. |
|
1440 |
aSortedCollection sortBlock:sortBlock. |
|
1441 |
aSortedCollection addAll:self. |
|
1442 |
^ aSortedCollection |
|
1443 |
||
1444 |
"Created: 13.9.1997 / 09:37:21 / cg" |
|
1445 |
"Modified: 13.9.1997 / 09:45:27 / cg" |
|
1446 |
! |
|
1447 |
||
1448 |
asSortedStringsWith: aCollationPolicy |
|
1449 |
"Create & return a SortedCollection that sorts the receivers |
|
1450 |
elements according to the specified locales collating policy. |
|
1451 |
This is currently not really support - strings are sorted |
|
1452 |
without caring for the locale." |
|
1453 |
||
1454 |
|aSortedCollection| |
|
1455 |
||
1456 |
aSortedCollection := SortedCollection forStrings:self size collatedBy:aCollationPolicy. |
|
1457 |
aSortedCollection addAll:self. |
|
1458 |
^ aSortedCollection |
|
1459 |
||
1460 |
"Created: 13.9.1997 / 09:37:50 / cg" |
|
1461 |
"Modified: 13.9.1997 / 09:44:08 / cg" |
|
1462 |
! |
|
1463 |
||
602 | 1464 |
asString |
11708
43d1d6cf1ba3
fixed asString for non-8-bit characters
Claus Gittinger <cg@exept.de>
parents:
11706
diff
changeset
|
1465 |
"return a String with the collection's elements |
602 | 1466 |
(which must convert to characters)" |
1467 |
||
11708
43d1d6cf1ba3
fixed asString for non-8-bit characters
Claus Gittinger <cg@exept.de>
parents:
11706
diff
changeset
|
1468 |
|string |
43d1d6cf1ba3
fixed asString for non-8-bit characters
Claus Gittinger <cg@exept.de>
parents:
11706
diff
changeset
|
1469 |
index "{ Class: SmallInteger }" |
43d1d6cf1ba3
fixed asString for non-8-bit characters
Claus Gittinger <cg@exept.de>
parents:
11706
diff
changeset
|
1470 |
char bitsPerCharacter| |
43d1d6cf1ba3
fixed asString for non-8-bit characters
Claus Gittinger <cg@exept.de>
parents:
11706
diff
changeset
|
1471 |
|
43d1d6cf1ba3
fixed asString for non-8-bit characters
Claus Gittinger <cg@exept.de>
parents:
11706
diff
changeset
|
1472 |
string := String new:(self size). |
43d1d6cf1ba3
fixed asString for non-8-bit characters
Claus Gittinger <cg@exept.de>
parents:
11706
diff
changeset
|
1473 |
bitsPerCharacter := string bitsPerCharacter. |
43d1d6cf1ba3
fixed asString for non-8-bit characters
Claus Gittinger <cg@exept.de>
parents:
11706
diff
changeset
|
1474 |
|
602 | 1475 |
index := 1. |
1476 |
self do:[:each | |
|
11708
43d1d6cf1ba3
fixed asString for non-8-bit characters
Claus Gittinger <cg@exept.de>
parents:
11706
diff
changeset
|
1477 |
char := each asCharacter. |
43d1d6cf1ba3
fixed asString for non-8-bit characters
Claus Gittinger <cg@exept.de>
parents:
11706
diff
changeset
|
1478 |
char bitsPerCharacter > bitsPerCharacter ifTrue:[ |
43d1d6cf1ba3
fixed asString for non-8-bit characters
Claus Gittinger <cg@exept.de>
parents:
11706
diff
changeset
|
1479 |
char bitsPerCharacter == 16 ifTrue:[ |
43d1d6cf1ba3
fixed asString for non-8-bit characters
Claus Gittinger <cg@exept.de>
parents:
11706
diff
changeset
|
1480 |
string := Unicode16String fromString:string. |
43d1d6cf1ba3
fixed asString for non-8-bit characters
Claus Gittinger <cg@exept.de>
parents:
11706
diff
changeset
|
1481 |
] ifFalse:[ |
43d1d6cf1ba3
fixed asString for non-8-bit characters
Claus Gittinger <cg@exept.de>
parents:
11706
diff
changeset
|
1482 |
string := Unicode32String fromString:string. |
43d1d6cf1ba3
fixed asString for non-8-bit characters
Claus Gittinger <cg@exept.de>
parents:
11706
diff
changeset
|
1483 |
]. |
43d1d6cf1ba3
fixed asString for non-8-bit characters
Claus Gittinger <cg@exept.de>
parents:
11706
diff
changeset
|
1484 |
]. |
43d1d6cf1ba3
fixed asString for non-8-bit characters
Claus Gittinger <cg@exept.de>
parents:
11706
diff
changeset
|
1485 |
string at:index put:char. |
6135 | 1486 |
index := index + 1 |
360 | 1487 |
]. |
11708
43d1d6cf1ba3
fixed asString for non-8-bit characters
Claus Gittinger <cg@exept.de>
parents:
11706
diff
changeset
|
1488 |
^ string |
43d1d6cf1ba3
fixed asString for non-8-bit characters
Claus Gittinger <cg@exept.de>
parents:
11706
diff
changeset
|
1489 |
|
43d1d6cf1ba3
fixed asString for non-8-bit characters
Claus Gittinger <cg@exept.de>
parents:
11706
diff
changeset
|
1490 |
" |
43d1d6cf1ba3
fixed asString for non-8-bit characters
Claus Gittinger <cg@exept.de>
parents:
11706
diff
changeset
|
1491 |
#(80 81 82) asString |
43d1d6cf1ba3
fixed asString for non-8-bit characters
Claus Gittinger <cg@exept.de>
parents:
11706
diff
changeset
|
1492 |
#(16r8000 16r8001 16r8002) asString |
43d1d6cf1ba3
fixed asString for non-8-bit characters
Claus Gittinger <cg@exept.de>
parents:
11706
diff
changeset
|
1493 |
#(16r800000 16r800001 16r800002) asString |
43d1d6cf1ba3
fixed asString for non-8-bit characters
Claus Gittinger <cg@exept.de>
parents:
11706
diff
changeset
|
1494 |
" |
602 | 1495 |
! |
1496 |
||
1497 |
asStringCollection |
|
1413 | 1498 |
"return a new string collection containing the elements; |
1499 |
these ought to be strings. (i.e. String or Text instances)" |
|
602 | 1500 |
|
1501 |
^ self addAllTo:(StringCollection new) |
|
1413 | 1502 |
|
1503 |
"Modified: 18.5.1996 / 13:54:34 / cg" |
|
360 | 1504 |
! |
1505 |
||
8160 | 1506 |
asUnicodeString |
1507 |
"return a String with the collections elements |
|
1508 |
(which must convert to characters)" |
|
1509 |
||
1510 |
|aString |
|
1511 |
index "{ Class: SmallInteger }" | |
|
1512 |
||
1513 |
aString := UnicodeString uninitializedNew:self size. |
|
1514 |
index := 1. |
|
1515 |
self do:[:each | |
|
1516 |
aString at:index put:each asCharacter. |
|
1517 |
index := index + 1 |
|
1518 |
]. |
|
1519 |
^ aString |
|
1520 |
||
1521 |
" |
|
1522 |
#(16r440 16r443 16r441 16r441 16r43A 16r438 16r439 16r20 16r44F 16r437 16r44B 16r43A) asUnicodeString |
|
1523 |
" |
|
1524 |
! |
|
1525 |
||
7885 | 1526 |
asWordArray |
1527 |
"return a new WordArray with the collections elements |
|
1528 |
(which must convert to integers in the range 0..16rFFFF)." |
|
1529 |
||
1530 |
|aWordArray |
|
1531 |
index "{ Class: SmallInteger }" | |
|
1532 |
||
1533 |
aWordArray := WordArray new:(self size). |
|
1534 |
index := 1. |
|
1535 |
self do:[:each | |
|
1536 |
aWordArray at:index put:each asInteger. |
|
1537 |
index := index + 1 |
|
1538 |
]. |
|
1539 |
^ aWordArray |
|
1540 |
! |
|
1541 |
||
17732
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1542 |
copyAs:collectionClass |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1543 |
"return a new instance of collectionClass with the receiver collection's elements. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1544 |
This is similar to copy as:collectionClass, to ensure that we get a new |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1545 |
(unshared) collection, but avoids the copy if the receiver is not already an |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1546 |
instance of collectionClass." |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1547 |
|
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1548 |
|newColl| |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1549 |
|
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1550 |
newColl := self as:collectionClass. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1551 |
newColl == self ifTrue:[ |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1552 |
^ self copy |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1553 |
]. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1554 |
^ newColl. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1555 |
|
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1556 |
" |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1557 |
|coll1 coll2| |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1558 |
|
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1559 |
coll1 := #(1 2 3 4 5). |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1560 |
coll2 := coll1 copyAs:Array. '-- will generate a copy'. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1561 |
self assert:(coll1 ~~ coll2). |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1562 |
self assert:(coll1 = coll2). |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1563 |
|
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1564 |
coll1 := #(1 2 3 4 5). |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1565 |
coll2 := coll1 copyAs:OrderedCollection. '-- will generate an OC'. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1566 |
self assert:(coll1 ~~ coll2). |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1567 |
self assert:(coll1 isSameSequenceAs: coll2). |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1568 |
|
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1569 |
coll1 := #(1 2 3 4 5) asOrderedCollection. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1570 |
coll2 := coll1 copyAs:OrderedCollection. '-- will generate a copy'. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1571 |
self assert:(coll1 ~~ coll2). |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1572 |
self assert:(coll1 = coll2). |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1573 |
" |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1574 |
! |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1575 |
|
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1576 |
copyAsOrderedCollection |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1577 |
"return a new OrderedCollection with the receiver collection's elements. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1578 |
This is similar to copy asOrderedCollection, to ensure that we get a new |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1579 |
(unshared) collection, but avoids the copy if the receiver is not already an |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1580 |
OrderedCollection." |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1581 |
|
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1582 |
|newColl| |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1583 |
|
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1584 |
newColl := self asOrderedCollection. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1585 |
newColl == self ifTrue:[ |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1586 |
^ self copy |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1587 |
]. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1588 |
^ newColl. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1589 |
! |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1590 |
|
7905 | 1591 |
keysAndValues |
1592 |
|assocs| |
|
1593 |
||
1594 |
assocs := OrderedCollection new. |
|
1595 |
self keysAndValuesDo:[:k :v | |
|
1596 |
assocs add:(k -> v). |
|
1597 |
]. |
|
1598 |
^ assocs. |
|
1599 |
||
1600 |
" |
|
1601 |
#(10 20 30 40 50 60) keysAndValues |
|
1602 |
" |
|
1603 |
! |
|
1604 |
||
9410 | 1605 |
literalArrayEncoding |
1606 |
"encode myself as an array literal, from which a copy of the receiver |
|
1607 |
can be reconstructed with #decodeAsLiteralArray." |
|
1608 |
||
1609 |
|encoding idx| |
|
1610 |
||
1611 |
encoding := Array new:self size + 1. |
|
1612 |
encoding at:1 put:self class name. |
|
1613 |
idx := 2. |
|
1614 |
self do:[:element| |
|
11315
9a6ca3147b69
#showWhereWeCameFrom - show more stack backtrace
Stefan Vogel <sv@exept.de>
parents:
11309
diff
changeset
|
1615 |
encoding at:idx put:element literalArrayEncoding. |
9a6ca3147b69
#showWhereWeCameFrom - show more stack backtrace
Stefan Vogel <sv@exept.de>
parents:
11309
diff
changeset
|
1616 |
idx := idx + 1. |
9410 | 1617 |
]. |
1618 |
^ encoding |
|
1619 |
||
1620 |
||
1621 |
" |
|
1622 |
(Array with:(Color red:50 green:50 blue:50) |
|
11315
9a6ca3147b69
#showWhereWeCameFrom - show more stack backtrace
Stefan Vogel <sv@exept.de>
parents:
11309
diff
changeset
|
1623 |
with:(1 @ 2) |
9410 | 1624 |
) literalArrayEncoding decodeAsLiteralArray |
1625 |
" |
|
1626 |
||
1627 |
"Modified: 22.4.1996 / 13:00:56 / cg" |
|
1628 |
! |
|
1629 |
||
602 | 1630 |
readStream |
1631 |
"return a stream for reading from the receiver" |
|
1632 |
||
1633 |
^ ReadStream on:self |
|
88 | 1634 |
|
1635 |
" |
|
602 | 1636 |
|s| |
1637 |
||
1638 |
s := 'hello world' readStream. |
|
1639 |
s next:5. |
|
1640 |
s next. |
|
1641 |
(s next:5) inspect |
|
88 | 1642 |
" |
1 | 1643 |
! |
1644 |
||
8140
7cc7ac5f29ca
Define #xxxStreamOrNil methods.
Stefan Vogel <sv@exept.de>
parents:
8050
diff
changeset
|
1645 |
readStreamOrNil |
7cc7ac5f29ca
Define #xxxStreamOrNil methods.
Stefan Vogel <sv@exept.de>
parents:
8050
diff
changeset
|
1646 |
"return a stream for reading from the receiver. |
7cc7ac5f29ca
Define #xxxStreamOrNil methods.
Stefan Vogel <sv@exept.de>
parents:
8050
diff
changeset
|
1647 |
This has been defined for protocol compatibility with FileName, |
7cc7ac5f29ca
Define #xxxStreamOrNil methods.
Stefan Vogel <sv@exept.de>
parents:
8050
diff
changeset
|
1648 |
but nil is never returned here" |
7cc7ac5f29ca
Define #xxxStreamOrNil methods.
Stefan Vogel <sv@exept.de>
parents:
8050
diff
changeset
|
1649 |
|
7cc7ac5f29ca
Define #xxxStreamOrNil methods.
Stefan Vogel <sv@exept.de>
parents:
8050
diff
changeset
|
1650 |
^ self readStream |
7cc7ac5f29ca
Define #xxxStreamOrNil methods.
Stefan Vogel <sv@exept.de>
parents:
8050
diff
changeset
|
1651 |
! |
7cc7ac5f29ca
Define #xxxStreamOrNil methods.
Stefan Vogel <sv@exept.de>
parents:
8050
diff
changeset
|
1652 |
|
8631 | 1653 |
readWriteStream |
1654 |
"return a stream for reading and writing from/to the receiver" |
|
1655 |
||
1656 |
^ ReadWriteStream with:self |
|
1657 |
||
1658 |
" |
|
11436 | 1659 |
'hello world' readWriteStream |
1660 |
nextPutAll:'+Foo'; |
|
1661 |
contents |
|
1662 |
||
1663 |
'hello world' readWriteStream |
|
1664 |
setToEnd; |
|
1665 |
nextPutAll:'+Foo'; |
|
1666 |
contents |
|
8631 | 1667 |
" |
1668 |
! |
|
1669 |
||
602 | 1670 |
writeStream |
1671 |
"return a stream for writing onto the receiver" |
|
1672 |
||
1673 |
^ WriteStream on:self |
|
2 | 1674 |
|
602 | 1675 |
" |
1676 |
|s| |
|
1677 |
||
11315
9a6ca3147b69
#showWhereWeCameFrom - show more stack backtrace
Stefan Vogel <sv@exept.de>
parents:
11309
diff
changeset
|
1678 |
s := #() writeStream. |
602 | 1679 |
s nextPut:1. |
1680 |
s nextPut:2. |
|
1681 |
s nextPut:3. |
|
1682 |
s contents inspect |
|
1683 |
" |
|
88 | 1684 |
|
1685 |
" |
|
602 | 1686 |
|s| |
2 | 1687 |
|
602 | 1688 |
s := OrderedCollection new writeStream. |
1689 |
s nextPut:1. |
|
1690 |
s nextPut:2. |
|
1691 |
s nextPut:3. |
|
1692 |
s contents inspect |
|
1693 |
" |
|
8140
7cc7ac5f29ca
Define #xxxStreamOrNil methods.
Stefan Vogel <sv@exept.de>
parents:
8050
diff
changeset
|
1694 |
! |
7cc7ac5f29ca
Define #xxxStreamOrNil methods.
Stefan Vogel <sv@exept.de>
parents:
8050
diff
changeset
|
1695 |
|
7cc7ac5f29ca
Define #xxxStreamOrNil methods.
Stefan Vogel <sv@exept.de>
parents:
8050
diff
changeset
|
1696 |
writeStreamOrNil |
7cc7ac5f29ca
Define #xxxStreamOrNil methods.
Stefan Vogel <sv@exept.de>
parents:
8050
diff
changeset
|
1697 |
"return a stream for writing onto the receiver. |
7cc7ac5f29ca
Define #xxxStreamOrNil methods.
Stefan Vogel <sv@exept.de>
parents:
8050
diff
changeset
|
1698 |
This has been defined for protocol compatibility with FileName, |
7cc7ac5f29ca
Define #xxxStreamOrNil methods.
Stefan Vogel <sv@exept.de>
parents:
8050
diff
changeset
|
1699 |
but nil is never returned here" |
7cc7ac5f29ca
Define #xxxStreamOrNil methods.
Stefan Vogel <sv@exept.de>
parents:
8050
diff
changeset
|
1700 |
|
7cc7ac5f29ca
Define #xxxStreamOrNil methods.
Stefan Vogel <sv@exept.de>
parents:
8050
diff
changeset
|
1701 |
^ self writeStream |
602 | 1702 |
! ! |
1 | 1703 |
|
602 | 1704 |
!Collection methodsFor:'copying'! |
1705 |
||
3381
e1c12fa92fbf
moved #postCopyFrom: from object; redefined copy to send this instead
Claus Gittinger <cg@exept.de>
parents:
3367
diff
changeset
|
1706 |
copy |
e1c12fa92fbf
moved #postCopyFrom: from object; redefined copy to send this instead
Claus Gittinger <cg@exept.de>
parents:
3367
diff
changeset
|
1707 |
"return a copy of the receiver. |
7168
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
1708 |
Redefined to pass the original as argument to the postCopyFrom method." |
3381
e1c12fa92fbf
moved #postCopyFrom: from object; redefined copy to send this instead
Claus Gittinger <cg@exept.de>
parents:
3367
diff
changeset
|
1709 |
|
e1c12fa92fbf
moved #postCopyFrom: from object; redefined copy to send this instead
Claus Gittinger <cg@exept.de>
parents:
3367
diff
changeset
|
1710 |
^ self shallowCopy postCopyFrom:self |
e1c12fa92fbf
moved #postCopyFrom: from object; redefined copy to send this instead
Claus Gittinger <cg@exept.de>
parents:
3367
diff
changeset
|
1711 |
|
e1c12fa92fbf
moved #postCopyFrom: from object; redefined copy to send this instead
Claus Gittinger <cg@exept.de>
parents:
3367
diff
changeset
|
1712 |
"Created: / 19.4.1998 / 20:02:53 / cg" |
e1c12fa92fbf
moved #postCopyFrom: from object; redefined copy to send this instead
Claus Gittinger <cg@exept.de>
parents:
3367
diff
changeset
|
1713 |
! |
e1c12fa92fbf
moved #postCopyFrom: from object; redefined copy to send this instead
Claus Gittinger <cg@exept.de>
parents:
3367
diff
changeset
|
1714 |
|
602 | 1715 |
copyEmpty |
1716 |
"return a copy of the receiver with no elements. |
|
1717 |
This is used by copying and enumeration methods |
|
1718 |
to get a new instance which is similar to the receiver." |
|
1719 |
||
1720 |
^ self species new |
|
1 | 1721 |
! |
1722 |
||
602 | 1723 |
copyEmpty:size |
1724 |
"return a copy of the receiver with no elements, but space for |
|
1725 |
size elements. This is used by copying and enumeration methods |
|
1726 |
to get a new instance which is similar to the receiver. |
|
1727 |
This method should be redefined in subclasses with instance |
|
1728 |
variables, which should be put into the copy too. |
|
1729 |
For example, SortedCollection has to copy its sortBlock into the |
|
1730 |
new collection." |
|
1 | 1731 |
|
602 | 1732 |
^ self species new:size |
360 | 1733 |
! |
1734 |
||
602 | 1735 |
copyEmptyAndGrow:size |
1736 |
"return a copy of the receiver with size nil elements. |
|
1737 |
This is used by copying and enumeration methods |
|
1738 |
to get a new instance which is similar to the receiver." |
|
155 | 1739 |
|
602 | 1740 |
^ (self copyEmpty:size) grow:size |
3381
e1c12fa92fbf
moved #postCopyFrom: from object; redefined copy to send this instead
Claus Gittinger <cg@exept.de>
parents:
3367
diff
changeset
|
1741 |
! |
e1c12fa92fbf
moved #postCopyFrom: from object; redefined copy to send this instead
Claus Gittinger <cg@exept.de>
parents:
3367
diff
changeset
|
1742 |
|
e1c12fa92fbf
moved #postCopyFrom: from object; redefined copy to send this instead
Claus Gittinger <cg@exept.de>
parents:
3367
diff
changeset
|
1743 |
postCopyFrom:original |
e1c12fa92fbf
moved #postCopyFrom: from object; redefined copy to send this instead
Claus Gittinger <cg@exept.de>
parents:
3367
diff
changeset
|
1744 |
"sent to a freshly copied object to give it a chance to adjust things. |
e1c12fa92fbf
moved #postCopyFrom: from object; redefined copy to send this instead
Claus Gittinger <cg@exept.de>
parents:
3367
diff
changeset
|
1745 |
Notice, that for Sets/Dicts etc. a rehash is not needed, since the copy |
e1c12fa92fbf
moved #postCopyFrom: from object; redefined copy to send this instead
Claus Gittinger <cg@exept.de>
parents:
3367
diff
changeset
|
1746 |
will have the same hash key as the receiver (as long as ST/X provides the |
e1c12fa92fbf
moved #postCopyFrom: from object; redefined copy to send this instead
Claus Gittinger <cg@exept.de>
parents:
3367
diff
changeset
|
1747 |
setHash: functionality)." |
e1c12fa92fbf
moved #postCopyFrom: from object; redefined copy to send this instead
Claus Gittinger <cg@exept.de>
parents:
3367
diff
changeset
|
1748 |
|
e1c12fa92fbf
moved #postCopyFrom: from object; redefined copy to send this instead
Claus Gittinger <cg@exept.de>
parents:
3367
diff
changeset
|
1749 |
"for ST-80 compatibility, we try postCopy here ..." |
e1c12fa92fbf
moved #postCopyFrom: from object; redefined copy to send this instead
Claus Gittinger <cg@exept.de>
parents:
3367
diff
changeset
|
1750 |
^ self postCopy |
e1c12fa92fbf
moved #postCopyFrom: from object; redefined copy to send this instead
Claus Gittinger <cg@exept.de>
parents:
3367
diff
changeset
|
1751 |
|
e1c12fa92fbf
moved #postCopyFrom: from object; redefined copy to send this instead
Claus Gittinger <cg@exept.de>
parents:
3367
diff
changeset
|
1752 |
"Created: / 19.4.1998 / 19:59:42 / cg" |
e1c12fa92fbf
moved #postCopyFrom: from object; redefined copy to send this instead
Claus Gittinger <cg@exept.de>
parents:
3367
diff
changeset
|
1753 |
"Modified: / 19.4.1998 / 20:03:57 / cg" |
1 | 1754 |
! ! |
1755 |
||
1756 |
!Collection methodsFor:'enumerating'! |
|
1757 |
||
10795
3797cbc3fcb4
Do not add nil elements to a Set in #asSet and #asIdentitySet
Stefan Vogel <sv@exept.de>
parents:
10750
diff
changeset
|
1758 |
addAllNonNilElementsTo:aCollection |
3797cbc3fcb4
Do not add nil elements to a Set in #asSet and #asIdentitySet
Stefan Vogel <sv@exept.de>
parents:
10750
diff
changeset
|
1759 |
"add all nonNil elements of the receiver to aCollection. |
3797cbc3fcb4
Do not add nil elements to a Set in #asSet and #asIdentitySet
Stefan Vogel <sv@exept.de>
parents:
10750
diff
changeset
|
1760 |
Return aCollection." |
3797cbc3fcb4
Do not add nil elements to a Set in #asSet and #asIdentitySet
Stefan Vogel <sv@exept.de>
parents:
10750
diff
changeset
|
1761 |
|
3797cbc3fcb4
Do not add nil elements to a Set in #asSet and #asIdentitySet
Stefan Vogel <sv@exept.de>
parents:
10750
diff
changeset
|
1762 |
self do:[:each | each notNil ifTrue:[aCollection add:each]]. |
3797cbc3fcb4
Do not add nil elements to a Set in #asSet and #asIdentitySet
Stefan Vogel <sv@exept.de>
parents:
10750
diff
changeset
|
1763 |
^ aCollection |
3797cbc3fcb4
Do not add nil elements to a Set in #asSet and #asIdentitySet
Stefan Vogel <sv@exept.de>
parents:
10750
diff
changeset
|
1764 |
|
3797cbc3fcb4
Do not add nil elements to a Set in #asSet and #asIdentitySet
Stefan Vogel <sv@exept.de>
parents:
10750
diff
changeset
|
1765 |
" |
3797cbc3fcb4
Do not add nil elements to a Set in #asSet and #asIdentitySet
Stefan Vogel <sv@exept.de>
parents:
10750
diff
changeset
|
1766 |
#(1 2 3 4 5 1 2 3 4 5 nil) asOrderedCollection addAllNonNilElementsTo:Set new |
3797cbc3fcb4
Do not add nil elements to a Set in #asSet and #asIdentitySet
Stefan Vogel <sv@exept.de>
parents:
10750
diff
changeset
|
1767 |
" |
3797cbc3fcb4
Do not add nil elements to a Set in #asSet and #asIdentitySet
Stefan Vogel <sv@exept.de>
parents:
10750
diff
changeset
|
1768 |
! |
3797cbc3fcb4
Do not add nil elements to a Set in #asSet and #asIdentitySet
Stefan Vogel <sv@exept.de>
parents:
10750
diff
changeset
|
1769 |
|
602 | 1770 |
addAllTo:aCollection |
1771 |
"add all elements of the receiver, to aCollection. |
|
1772 |
Return aCollection." |
|
77 | 1773 |
|
602 | 1774 |
self do:[:each | aCollection add:each]. |
1775 |
^ aCollection |
|
5254 | 1776 |
|
1777 |
" |
|
1778 |
#(1 2 3 4 5 1 2 3 4 5) addAllTo:Set new |
|
1779 |
" |
|
1780 |
||
1781 |
"Modified: / 11.2.2000 / 11:22:14 / cg" |
|
77 | 1782 |
! |
1783 |
||
1 | 1784 |
collect:aBlock |
1785 |
"for each element in the receiver, evaluate the argument, aBlock |
|
1786 |
and return a new collection with the results" |
|
1787 |
||
1788 |
|newCollection| |
|
1789 |
||
201 | 1790 |
newCollection := self species new. |
1791 |
self do:[:element | newCollection add:(aBlock value:element)]. |
|
1 | 1792 |
^ newCollection |
68 | 1793 |
|
1794 |
" |
|
1795 |
#(1 2 3 4) collect:[:n | n * 2] |
|
1796 |
" |
|
1 | 1797 |
! |
1798 |
||
17732
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1799 |
collect:aBlock as:aClass |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1800 |
"like collect, but use an instance of aClass to collect the results. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1801 |
Also avoids the need for an extra intermediate collection which is created with |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1802 |
the standard coding: 'self asXXXX collect:[...]" |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1803 |
|
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1804 |
|newCollection |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1805 |
idx "{ Class:SmallInteger }" |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1806 |
sz "{ Class:SmallInteger }"| |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1807 |
|
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1808 |
sz := self size. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1809 |
newCollection := (aClass new:sz) grow:sz. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1810 |
newCollection isSequenceable ifTrue:[ |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1811 |
idx := 1. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1812 |
self do:[:el | |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1813 |
newCollection at:idx put:(aBlock value:el). |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1814 |
idx := idx + 1. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1815 |
]. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1816 |
] ifFalse:[ |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1817 |
self do:[:el | |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1818 |
newCollection add:(aBlock value:el). |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1819 |
]. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1820 |
]. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1821 |
^ newCollection |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1822 |
|
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1823 |
" |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1824 |
#(one two three four five six) collect:[:element | element asUppercase] as:OrderedCollection |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1825 |
'abcdef' collect:[:char | char digitValue] as:ByteArray |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1826 |
" |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1827 |
! |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1828 |
|
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1829 |
collect:collectBlock thenReject:rejectBlock |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1830 |
"combination of collect followed by reject. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1831 |
May be redefined by some subclasses for optimal performance |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1832 |
(avoiding the creation of intermediate garbage)" |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1833 |
|
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1834 |
^ (self collect:collectBlock) select:[:el | (rejectBlock value:el) not] |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1835 |
|
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1836 |
" |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1837 |
#(1 2 3 4 5 6 7) collect:[:i | i * 2] thenReject:[:i | i > 10] |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1838 |
" |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1839 |
! |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1840 |
|
4890 | 1841 |
collect:collectBlock thenSelect:selectBlock |
1842 |
"combination of collect followed by select. |
|
1843 |
May be redefined by some subclasses for optimal performance |
|
7164 | 1844 |
(avoiding the creation of intermediate garbage)" |
4890 | 1845 |
|
4892 | 1846 |
^ (self collect:collectBlock) select:selectBlock |
1847 |
||
1848 |
" |
|
1849 |
#(1 2 3 4 5 6 7) collect:[:i | i * 2] thenSelect:[:i | i < 10] |
|
1850 |
" |
|
4890 | 1851 |
! |
1852 |
||
6867 | 1853 |
collectAll:aBlock |
1854 |
"for each element in the receiver, evaluate the argument, aBlock. |
|
9315 | 1855 |
The block is supposed to return a collection, whose elements are collected. |
8608 | 1856 |
The species of the returned collection is that of the first returned |
1857 |
partial result." |
|
6867 | 1858 |
|
1859 |
|result| |
|
1860 |
||
1861 |
self do:[:element | |
|
1862 |
|individualResult| |
|
1863 |
||
1864 |
individualResult := aBlock value:element. |
|
1865 |
result isNil ifTrue:[ |
|
1866 |
result := individualResult speciesForAdding new. |
|
1867 |
]. |
|
1868 |
result addAll:individualResult. |
|
1869 |
]. |
|
1870 |
||
8609
daa0d6682135
collectAll: now returns an empty collection if
Claus Gittinger <cg@exept.de>
parents:
8608
diff
changeset
|
1871 |
^ result ? #() |
6867 | 1872 |
|
1873 |
" |
|
1874 |
#(1 2 3 4) collectAll:[:n | Array new:n withAll:n ] |
|
7629 | 1875 |
#(1 2 3 4) collectAll:[:n | Array with:n with:n squared ] |
8607 | 1876 |
#(1 2 3 4) collectAll:[:n | 1 to:n ] |
1877 |
(Array with:Point with:Rectangle) collectAll:[:c | c instVarNames ] |
|
7629 | 1878 |
" |
6867 | 1879 |
! |
1880 |
||
602 | 1881 |
count:aBlock |
1882 |
"count elements, for which aBlock returns true. |
|
1883 |
Return the sum." |
|
1884 |
||
7336 | 1885 |
|count| |
1886 |
||
1887 |
count := 0. |
|
1888 |
self do:[:element | |
|
1889 |
(aBlock value:element) ifTrue:[count := count + 1] |
|
1890 |
]. |
|
1891 |
^ count |
|
77 | 1892 |
|
1893 |
" |
|
602 | 1894 |
#(1 2 3 4 6 8 10) count:[:a | a even] |
7336 | 1895 |
#(1 nil nil nil 2 3 nil 4 5) count:[:a | a isNil] |
77 | 1896 |
" |
1897 |
! |
|
1898 |
||
1 | 1899 |
detect:aBlock |
1900 |
"evaluate the argument, aBlock for each element in the receiver until |
|
1901 |
the block returns true; in this case return the element which caused |
|
1902 |
the true evaluation. |
|
345 | 1903 |
If none of the evaluations returns true, report an error" |
1 | 1904 |
|
1905 |
^ self detect:aBlock ifNone:[self errorNotFound] |
|
68 | 1906 |
|
1907 |
" |
|
1908 |
#(1 2 3 4) detect:[:n | n odd] |
|
1909 |
#(2 4 6 8) detect:[:n | n odd] |
|
1910 |
" |
|
1 | 1911 |
! |
1912 |
||
8750 | 1913 |
detect:generatorBlock forWhich:testBlock ifNone:exceptionBlock |
1914 |
"evaluate generatorBlock for each element in the receiver until |
|
1915 |
testBlock returns true for it; |
|
1916 |
in this case return the value from generatorBlock, which caused the true evaluation. |
|
1917 |
If none of the test evaluations returns true, return the result of the |
|
1918 |
evaluation of the exceptionBlock" |
|
1919 |
||
1920 |
self do:[:each | |
|
1921 |
|val| |
|
1922 |
||
1923 |
val := generatorBlock value:each. |
|
1924 |
(testBlock value:val) ifTrue:[^ val]. |
|
1925 |
]. |
|
1926 |
^ exceptionBlock value |
|
1927 |
||
1928 |
" |
|
1929 |
#(2 3 4) detect:[:n | n squared] forWhich:[:nsq | nsq odd] ifNone:['sorry'] |
|
1930 |
#( 2 4 ) detect:[:n | n squared] forWhich:[:nsq | nsq odd] ifNone:['sorry'] |
|
8751 | 1931 |
|
1932 |
#( 'st' 'c' 'java' ) |
|
1933 |
detect:[:ext | 'Foo' asFilename withSuffix:ext] |
|
1934 |
forWhich:[:fn | fn exists] |
|
1935 |
ifNone:nil |
|
8750 | 1936 |
" |
1937 |
! |
|
1938 |
||
9842 | 1939 |
detect:aOneArgBlock ifNone:exceptionBlock |
1 | 1940 |
"evaluate the argument, aBlock for each element in the receiver until |
1941 |
the block returns true; in this case return the element which caused |
|
1942 |
the true evaluation. |
|
1943 |
If none of the evaluations returns true, return the result of the |
|
1944 |
evaluation of the exceptionBlock" |
|
1945 |
||
1946 |
self do:[:each | |
|
9842 | 1947 |
(aOneArgBlock value:each) ifTrue:[^ each]. |
1 | 1948 |
]. |
1949 |
^ exceptionBlock value |
|
68 | 1950 |
|
1951 |
" |
|
1952 |
#(1 2 3 4) detect:[:n | n odd] ifNone:['sorry'] |
|
1953 |
#(2 4 6 8) detect:[:n | n odd] ifNone:['sorry'] |
|
1954 |
" |
|
9842 | 1955 |
|
1956 |
"Modified: / 13-09-2006 / 11:17:42 / cg" |
|
1 | 1957 |
! |
1958 |
||
421 | 1959 |
detectLast:aBlock |
1960 |
"evaluate the argument, aBlock for each element in the receiver until |
|
1961 |
the block returns true; in this case return the element which caused |
|
1962 |
the true evaluation. The elements are processed in reverse order. |
|
1963 |
If none of the evaluations returns true, report an error" |
|
1964 |
||
1965 |
^ self detectLast:aBlock ifNone:[self errorNotFound] |
|
1966 |
||
1967 |
" |
|
1968 |
#(1 2 3 4) detectLast:[:n | n odd] |
|
1969 |
#(2 4 6 8) detectLast:[:n | n odd] |
|
1970 |
" |
|
1971 |
! |
|
1972 |
||
1973 |
detectLast:aBlock ifNone:exceptionBlock |
|
1974 |
"evaluate the argument, aBlock for each element in the receiver until |
|
1975 |
the block returns true; in this case return the element which caused |
|
1976 |
the true evaluation. The elements are processed in reverse order. |
|
1977 |
If none of the evaluations returns true, return the result of the |
|
1978 |
evaluation of the exceptionBlock" |
|
1979 |
||
1980 |
self reverseDo:[:each | |
|
6135 | 1981 |
(aBlock value:each) ifTrue:[^ each]. |
421 | 1982 |
]. |
1983 |
^ exceptionBlock value |
|
1984 |
||
1985 |
" |
|
1986 |
#(1 2 3 4) detectLast:[:n | n odd] ifNone:['sorry'] |
|
1987 |
#(2 4 6 8) detectLast:[:n | n odd] ifNone:['sorry'] |
|
1988 |
" |
|
1989 |
! |
|
1990 |
||
602 | 1991 |
do:aBlock |
1992 |
"evaluate the argument, aBlock for each element" |
|
1993 |
||
1994 |
^ self subclassResponsibility |
|
1995 |
! |
|
1996 |
||
4394 | 1997 |
do:aBlock inBetweenDo:betweenBlock |
1998 |
"evaluate the argument, aBlock for each element. |
|
1999 |
Between elements (i.e. after each except for the last), |
|
2000 |
evaluate betweenBlock. |
|
5254 | 2001 |
This is a utility helper for collection printers |
2002 |
(for example, to print a space between elements)." |
|
4394 | 2003 |
|
8359 | 2004 |
<resource: #obsolete> |
6052
a7c0dda5e0bc
do:inBetweenDo: -> do:separatedBy: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6042
diff
changeset
|
2005 |
self obsoleteMethodWarning:'use #do:separatedBy:'. |
a7c0dda5e0bc
do:inBetweenDo: -> do:separatedBy: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6042
diff
changeset
|
2006 |
^ self do:aBlock separatedBy:betweenBlock |
a7c0dda5e0bc
do:inBetweenDo: -> do:separatedBy: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6042
diff
changeset
|
2007 |
|
a7c0dda5e0bc
do:inBetweenDo: -> do:separatedBy: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6042
diff
changeset
|
2008 |
" |
a7c0dda5e0bc
do:inBetweenDo: -> do:separatedBy: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6042
diff
changeset
|
2009 |
#(1 2 3 4) do:[:el | Transcript show:el] |
a7c0dda5e0bc
do:inBetweenDo: -> do:separatedBy: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6042
diff
changeset
|
2010 |
inBetweenDo:[ Transcript show:'-'] |
a7c0dda5e0bc
do:inBetweenDo: -> do:separatedBy: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6042
diff
changeset
|
2011 |
|
a7c0dda5e0bc
do:inBetweenDo: -> do:separatedBy: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6042
diff
changeset
|
2012 |
(Dictionary with:(1->'one') with:(2->'two')) |
a7c0dda5e0bc
do:inBetweenDo: -> do:separatedBy: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6042
diff
changeset
|
2013 |
do:[:el | Transcript showCR:el printString] |
a7c0dda5e0bc
do:inBetweenDo: -> do:separatedBy: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6042
diff
changeset
|
2014 |
inBetweenDo:[ Transcript showCR:'----'] |
a7c0dda5e0bc
do:inBetweenDo: -> do:separatedBy: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6042
diff
changeset
|
2015 |
|
a7c0dda5e0bc
do:inBetweenDo: -> do:separatedBy: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6042
diff
changeset
|
2016 |
(Dictionary with:(1->'one') with:(2->'two')) |
a7c0dda5e0bc
do:inBetweenDo: -> do:separatedBy: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6042
diff
changeset
|
2017 |
associations |
a7c0dda5e0bc
do:inBetweenDo: -> do:separatedBy: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6042
diff
changeset
|
2018 |
do:[:el | Transcript showCR:el printString] |
a7c0dda5e0bc
do:inBetweenDo: -> do:separatedBy: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6042
diff
changeset
|
2019 |
inBetweenDo:[ Transcript showCR:'----'] |
a7c0dda5e0bc
do:inBetweenDo: -> do:separatedBy: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6042
diff
changeset
|
2020 |
|
a7c0dda5e0bc
do:inBetweenDo: -> do:separatedBy: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6042
diff
changeset
|
2021 |
" |
a7c0dda5e0bc
do:inBetweenDo: -> do:separatedBy: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6042
diff
changeset
|
2022 |
|
a7c0dda5e0bc
do:inBetweenDo: -> do:separatedBy: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6042
diff
changeset
|
2023 |
"Modified: / 11.2.2000 / 11:23:15 / cg" |
a7c0dda5e0bc
do:inBetweenDo: -> do:separatedBy: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6042
diff
changeset
|
2024 |
! |
a7c0dda5e0bc
do:inBetweenDo: -> do:separatedBy: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6042
diff
changeset
|
2025 |
|
a7c0dda5e0bc
do:inBetweenDo: -> do:separatedBy: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6042
diff
changeset
|
2026 |
do:aBlock separatedBy:betweenBlock |
a7c0dda5e0bc
do:inBetweenDo: -> do:separatedBy: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6042
diff
changeset
|
2027 |
"evaluate the argument, aBlock for each element. |
a7c0dda5e0bc
do:inBetweenDo: -> do:separatedBy: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6042
diff
changeset
|
2028 |
Between elements (i.e. after each except for the last), |
a7c0dda5e0bc
do:inBetweenDo: -> do:separatedBy: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6042
diff
changeset
|
2029 |
evaluate betweenBlock. |
a7c0dda5e0bc
do:inBetweenDo: -> do:separatedBy: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6042
diff
changeset
|
2030 |
This is a utility helper for collection printers |
a7c0dda5e0bc
do:inBetweenDo: -> do:separatedBy: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6042
diff
changeset
|
2031 |
(for example, to print a space between elements)." |
a7c0dda5e0bc
do:inBetweenDo: -> do:separatedBy: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6042
diff
changeset
|
2032 |
|
4394 | 2033 |
|first| |
2034 |
||
2035 |
first := true. |
|
2036 |
self do:[:element | |
|
2037 |
first ifTrue:[ |
|
2038 |
first := false |
|
2039 |
] ifFalse:[ |
|
2040 |
betweenBlock value. |
|
2041 |
]. |
|
2042 |
aBlock value:element |
|
2043 |
]. |
|
2044 |
||
2045 |
" |
|
2046 |
#(1 2 3 4) do:[:el | Transcript show:el] |
|
6052
a7c0dda5e0bc
do:inBetweenDo: -> do:separatedBy: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6042
diff
changeset
|
2047 |
separatedBy:[ Transcript show:'-'] |
5619 | 2048 |
|
2049 |
(Dictionary with:(1->'one') with:(2->'two')) |
|
2050 |
do:[:el | Transcript showCR:el printString] |
|
6052
a7c0dda5e0bc
do:inBetweenDo: -> do:separatedBy: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6042
diff
changeset
|
2051 |
separatedBy:[ Transcript showCR:'----'] |
5619 | 2052 |
|
2053 |
(Dictionary with:(1->'one') with:(2->'two')) |
|
2054 |
associations |
|
2055 |
do:[:el | Transcript showCR:el printString] |
|
6052
a7c0dda5e0bc
do:inBetweenDo: -> do:separatedBy: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6042
diff
changeset
|
2056 |
separatedBy:[ Transcript showCR:'----'] |
5619 | 2057 |
|
4394 | 2058 |
" |
5254 | 2059 |
|
2060 |
"Modified: / 11.2.2000 / 11:23:15 / cg" |
|
4394 | 2061 |
! |
2062 |
||
1212 | 2063 |
doWithExit:aBlock |
2064 |
"evaluate the argument, aBlock for each element. |
|
2065 |
Passes an additional exit object, which can be used to leave |
|
2066 |
the loop early, by sending it a #value: message. |
|
2067 |
Returns nil or the value passed to the exit>>value: message. |
|
2068 |
||
8662 | 2069 |
Notice, that this is different to a return statement in the block, |
2070 |
which returns from the enclosed method, NOT only from the block." |
|
2071 |
||
2072 |
|exit| |
|
2073 |
||
2074 |
exit := [:exitValue | ^exitValue]. |
|
2075 |
self do:[:el | |
|
2076 |
aBlock value:el value:exit |
|
2077 |
]. |
|
2078 |
^ nil |
|
1212 | 2079 |
|
2080 |
"Example: |
|
2081 |
search for the first element which is >= 99; return it. |
|
2082 |
remaining elements are not processed: |
|
2083 |
||
2084 |
#(1 2 3 4 999 5 6 7 8 9) |
|
2085 |
doWithExit:[:element :exit | |
|
1422 | 2086 |
Transcript showCR:element. |
8662 | 2087 |
element >= 99 ifTrue:[exit value:element]] |
1212 | 2088 |
|
2089 |
#(1 2 3 4 5 6 7 8 9) |
|
2090 |
doWithExit:[:element :exit | |
|
1422 | 2091 |
Transcript showCR:element. |
1212 | 2092 |
element >= 99 ifTrue:[exit value:element]] |
2093 |
" |
|
2094 |
||
2095 |
"to demonstrate the difference to returning from the block: |
|
2096 |
this works: |
|
2097 |
||
2098 |
|el| |
|
2099 |
||
2100 |
el := #(1 2 3 4 999 5 6 7 8 9) |
|
2101 |
doWithExit:[:element :exit | |
|
2102 |
element >= 99 ifTrue:[exit value:element]]. |
|
1422 | 2103 |
Transcript showCR:el. |
1212 | 2104 |
|
2105 |
||
1422 | 2106 |
this does NOT work as expected by a newComer ;-) (the showCR is not reached): |
1212 | 2107 |
|
2108 |
|el| |
|
2109 |
||
2110 |
el := #(1 2 3 4 999 5 6 7 8 9) |
|
2111 |
do:[:element | |
|
2112 |
element >= 99 ifTrue:[^ element]]. |
|
1422 | 2113 |
Transcript showCR:el. |
1212 | 2114 |
" |
2115 |
||
2116 |
"Modified: 18.4.1996 / 14:16:59 / cg" |
|
2117 |
! |
|
2118 |
||
11412 | 2119 |
flatDo:aBlock |
2120 |
"for each element of the collection, if its a scalar, evaluate aBlock for it; |
|
2121 |
otherwise, recursively invoke flatDo on the collection." |
|
2122 |
||
2123 |
self do:[:each | |
|
2124 |
(each isNonByteCollection) ifTrue:[ |
|
2125 |
each flatDo:aBlock |
|
2126 |
] ifFalse:[ |
|
2127 |
aBlock value:each |
|
2128 |
]. |
|
2129 |
]. |
|
2130 |
||
2131 |
" |
|
2132 |
#( |
|
2133 |
(1 2 3) |
|
2134 |
4 5 |
|
2135 |
(6) |
|
2136 |
7 |
|
2137 |
(8 (9 10) 11 12 (13 (14 (15) 16)))) flatDo:[:el | Transcript showCR:el] |
|
2138 |
" |
|
2139 |
! |
|
2140 |
||
1 | 2141 |
inject:thisValue into:binaryBlock |
1262 | 2142 |
"starting with thisValue for value, pass this value and each element |
2143 |
to binaryBlock, replacing value with the result returned from the block |
|
2144 |
in the next iteration." |
|
2145 |
||
1 | 2146 |
|nextValue| |
2147 |
||
2148 |
nextValue := thisValue. |
|
2149 |
self do: [:each | nextValue := binaryBlock value:nextValue value:each]. |
|
2150 |
^ nextValue |
|
68 | 2151 |
|
1262 | 2152 |
"sum up the elements of a collection: |
2153 |
||
68 | 2154 |
#(1 2 3 4) inject:0 into:[:accu :element | accu + element] |
2155 |
(1 to:10) inject:0 into:[:accu :element | accu + element] |
|
1262 | 2156 |
|
2157 |
find the minimum: |
|
2158 |
||
2159 |
|coll| |
|
2160 |
coll := #(1 99 -15 20 100). |
|
2161 |
coll inject:(coll first) into:[:minSoFar :element | minSoFar min:element] |
|
68 | 2162 |
" |
1262 | 2163 |
|
2164 |
"Modified: 23.4.1996 / 13:47:06 / cg" |
|
1 | 2165 |
! |
2166 |
||
602 | 2167 |
keysAndValuesDo:aTwoArgBlock |
2168 |
"evaluate the argument, aBlock for every element in the collection, |
|
2169 |
passing both index and element as arguments." |
|
2170 |
||
2171 |
^ self errorNotKeyed |
|
2172 |
! |
|
2173 |
||
1359 | 2174 |
keysAndValuesReverseDo:aTwoArgBlock |
2175 |
"evaluate the argument, aBlock in reverse order for every element in the collection, |
|
2176 |
passing both index and element as arguments." |
|
2177 |
||
2178 |
^ self errorNotKeyed |
|
2179 |
||
2180 |
"Created: 9.5.1996 / 00:58:24 / cg" |
|
2181 |
! |
|
2182 |
||
9076
e7957fd220c8
+keysAndValuesSelect:thenCollect:
Claus Gittinger <cg@exept.de>
parents:
9073
diff
changeset
|
2183 |
keysAndValuesSelect:selectBlockWith2Args thenCollect:collectBlockWith2Args |
e7957fd220c8
+keysAndValuesSelect:thenCollect:
Claus Gittinger <cg@exept.de>
parents:
9073
diff
changeset
|
2184 |
|collected| |
e7957fd220c8
+keysAndValuesSelect:thenCollect:
Claus Gittinger <cg@exept.de>
parents:
9073
diff
changeset
|
2185 |
|
e7957fd220c8
+keysAndValuesSelect:thenCollect:
Claus Gittinger <cg@exept.de>
parents:
9073
diff
changeset
|
2186 |
collected := OrderedCollection new. |
e7957fd220c8
+keysAndValuesSelect:thenCollect:
Claus Gittinger <cg@exept.de>
parents:
9073
diff
changeset
|
2187 |
self keysAndValuesDo:[:eachKey :eachValue | |
e7957fd220c8
+keysAndValuesSelect:thenCollect:
Claus Gittinger <cg@exept.de>
parents:
9073
diff
changeset
|
2188 |
(selectBlockWith2Args value:eachKey value:eachValue) ifTrue:[ |
e7957fd220c8
+keysAndValuesSelect:thenCollect:
Claus Gittinger <cg@exept.de>
parents:
9073
diff
changeset
|
2189 |
collected add:(collectBlockWith2Args value:eachKey value:eachValue) |
e7957fd220c8
+keysAndValuesSelect:thenCollect:
Claus Gittinger <cg@exept.de>
parents:
9073
diff
changeset
|
2190 |
]. |
e7957fd220c8
+keysAndValuesSelect:thenCollect:
Claus Gittinger <cg@exept.de>
parents:
9073
diff
changeset
|
2191 |
]. |
e7957fd220c8
+keysAndValuesSelect:thenCollect:
Claus Gittinger <cg@exept.de>
parents:
9073
diff
changeset
|
2192 |
^ collected |
9077
327b64542a83
+keysAndValuesSelect:thenCollect:
Claus Gittinger <cg@exept.de>
parents:
9076
diff
changeset
|
2193 |
|
327b64542a83
+keysAndValuesSelect:thenCollect:
Claus Gittinger <cg@exept.de>
parents:
9076
diff
changeset
|
2194 |
" |
327b64542a83
+keysAndValuesSelect:thenCollect:
Claus Gittinger <cg@exept.de>
parents:
9076
diff
changeset
|
2195 |
#(10 20 30 40) keysAndValuesSelect:[:idx :val | idx > 2] thenCollect:[:idx :val | idx->val] |
327b64542a83
+keysAndValuesSelect:thenCollect:
Claus Gittinger <cg@exept.de>
parents:
9076
diff
changeset
|
2196 |
" |
9076
e7957fd220c8
+keysAndValuesSelect:thenCollect:
Claus Gittinger <cg@exept.de>
parents:
9073
diff
changeset
|
2197 |
! |
e7957fd220c8
+keysAndValuesSelect:thenCollect:
Claus Gittinger <cg@exept.de>
parents:
9073
diff
changeset
|
2198 |
|
7454 | 2199 |
map:selector |
2200 |
"for lisp fans - similar to collect" |
|
2201 |
||
2202 |
^ self collect:[:eachElement | eachElement perform:selector]. |
|
2203 |
||
2204 |
" |
|
2205 |
#(1 2 3 4) map:#negated |
|
2206 |
" |
|
2207 |
! |
|
2208 |
||
2209 |
map:selector with:arg |
|
2210 |
"for lisp fans - similar to collect" |
|
2211 |
||
2212 |
^ self collect:[:eachElement | eachElement perform:selector with:arg]. |
|
2213 |
||
2214 |
" |
|
2215 |
#(1 2 3 4) map:#+ with:1 |
|
2216 |
" |
|
2217 |
! |
|
2218 |
||
8863 | 2219 |
nonNilElementsDo:aBlock |
2220 |
"evaluate the argument, aBlock for every non-nil element in the collection." |
|
2221 |
||
2222 |
self do:[:each | |
|
2223 |
each notNil ifTrue:[ |
|
2224 |
aBlock value:each. |
|
2225 |
] |
|
2226 |
] |
|
2227 |
! |
|
2228 |
||
10750 | 2229 |
pairsDo:aTwoArgBlock |
2230 |
"evaluate the argument, aTwoArgBlock for every element in the collection, |
|
602 | 2231 |
which is supposed to consist of 2-element collections. |
17734
406b1590afe8
Merged with trunk r10476
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17732
diff
changeset
|
2232 |
The block is called with 2 arguments for each collection in the receiver. |
406b1590afe8
Merged with trunk r10476
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17732
diff
changeset
|
2233 |
CONFUSION ATTACK: |
406b1590afe8
Merged with trunk r10476
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17732
diff
changeset
|
2234 |
this is different from pairWiseDo:. |
406b1590afe8
Merged with trunk r10476
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17732
diff
changeset
|
2235 |
but the Squeak-pairsDo: does the same as our pairWiseDo: |
406b1590afe8
Merged with trunk r10476
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17732
diff
changeset
|
2236 |
(sigh: but we were first, so they should have adapted...)" |
602 | 2237 |
|
2238 |
self do:[:aPair | |
|
10750 | 2239 |
aTwoArgBlock value:(aPair at:1) value:(aPair at:2). |
602 | 2240 |
] |
10750 | 2241 |
|
602 | 2242 |
" |
6555 | 2243 |
#( |
2244 |
(1 one) |
|
2245 |
(2 two) |
|
2246 |
(3 three) |
|
2247 |
(4 four) |
|
2248 |
(5 five) |
|
2249 |
(6 six) |
|
2250 |
) pairsDo: |
|
2251 |
[:num :sym | |
|
2252 |
Transcript show:num; show:' is: '; showCR:sym] |
|
602 | 2253 |
|
2254 |
#( (1 1) (1 2) (1 3) (1 4) (1 5)) |
|
1422 | 2255 |
pairsDo:[:x :y | Transcript showCR:x@y] |
602 | 2256 |
" |
10750 | 2257 |
|
2258 |
"Modified: / 20-10-2007 / 17:17:50 / cg" |
|
602 | 2259 |
! |
2260 |
||
1 | 2261 |
reject:aBlock |
2262 |
"return a new collection with all elements from the receiver, for which |
|
2263 |
the argument aBlock evaluates to false" |
|
2264 |
||
17732
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
2265 |
^ self select:[:element | (aBlock value:element) not] |
68 | 2266 |
|
2267 |
" |
|
2268 |
#(1 2 3 4) reject:[:e | e odd] |
|
2269 |
(1 to:10) reject:[:e | e even] |
|
2270 |
" |
|
1 | 2271 |
! |
2272 |
||
17732
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
2273 |
reject:rejectBlock thenCollect:collectBlock |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
2274 |
"combination of reject followed by collect. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
2275 |
May be redefined by some subclasses for optimal performance |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
2276 |
(avoiding the creation of intermediate garbage)" |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
2277 |
|
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
2278 |
^ (self select:[:el | (rejectBlock value:el) not]) collect:collectBlock |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
2279 |
|
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
2280 |
" |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
2281 |
#(1 2 3 4 5 6 7) reject:[:i | i even] thenCollect:[:i | i * 2] |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
2282 |
" |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
2283 |
! |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
2284 |
|
602 | 2285 |
reverseDo:aBlock |
2286 |
"evaluate the argument, aBlock for each element in reverse order." |
|
2287 |
||
2288 |
"it could be defined in terms of do: - but very inefficient. |
|
2289 |
Better force programmer to define a better version ..." |
|
2290 |
||
2291 |
^ self subclassResponsibility |
|
2292 |
! |
|
2293 |
||
1 | 2294 |
select:aBlock |
2295 |
"return a new collection with all elements from the receiver, for which |
|
5265 | 2296 |
the argument aBlock evaluates to true. |
2297 |
See also: #removeAllFoundIn: and #removeAllSuchThat:" |
|
1 | 2298 |
|
2299 |
|newCollection| |
|
2300 |
||
201 | 2301 |
newCollection := self species new. |
1 | 2302 |
self do:[:each | |
5265 | 2303 |
(aBlock value:each) ifTrue:[newCollection add:each]. |
1 | 2304 |
]. |
2305 |
^ newCollection |
|
68 | 2306 |
|
2307 |
" |
|
2308 |
#(1 2 3 4) select:[:e | e odd] |
|
2309 |
(1 to:10) select:[:e | e even] |
|
2310 |
" |
|
2311 |
! |
|
2312 |
||
2313 |
select:aBlock ifNone:exceptionBlock |
|
2314 |
"try a new collection with all elements from the receiver, for which |
|
2315 |
the argument aBlock evaluates to true. If none of the elements passes |
|
5265 | 2316 |
the check of aBlock, return the result of evaluating exceptionBlock. |
2317 |
See also: #removeAllFoundIn: and #removeAllSuchThat:" |
|
68 | 2318 |
|
2319 |
|newCollection| |
|
2320 |
||
2321 |
newCollection := self select:aBlock. |
|
2322 |
newCollection isEmpty ifTrue:[^ exceptionBlock value]. |
|
2323 |
^ newCollection |
|
2324 |
||
2325 |
" |
|
2326 |
#(1 2 3 4) select:[:e | e > 10] ifNone:['sorry'] |
|
2327 |
" |
|
12 | 2328 |
! |
2329 |
||
4892 | 2330 |
select:selectBlock thenCollect:collectBlock |
2331 |
"combination of select followed by collect. |
|
2332 |
May be redefined by some subclasses for optimal performance |
|
5844 | 2333 |
(avoiding the creation of intermediate garbage)" |
4892 | 2334 |
|
2335 |
^ (self select:selectBlock) collect:collectBlock |
|
2336 |
||
2337 |
" |
|
2338 |
#(1 2 3 4 5 6 7) select:[:i | i even] thenCollect:[:i | i * 2] |
|
2339 |
" |
|
2340 |
! |
|
2341 |
||
6695 | 2342 |
select:selectBlock thenDo:doBlock |
2343 |
"combination of select followed by do |
|
2344 |
Avoids the creation of intermediate garbage" |
|
2345 |
||
2346 |
self do:[:eachElement | |
|
2347 |
(selectBlock value:eachElement) ifTrue:[ |
|
2348 |
doBlock value:eachElement |
|
2349 |
] |
|
2350 |
]. |
|
2351 |
||
2352 |
" |
|
2353 |
#(1 2 3 4 5 6 7) select:[:i | i even] thenDo:[:i | Transcript showCR:i] |
|
2354 |
" |
|
2355 |
! |
|
2356 |
||
362 | 2357 |
triplesDo:aBlock |
2358 |
"evaluate the argument, aBlock for every element in the collection, |
|
2359 |
which is supposed to consist of 3-element collections. |
|
2360 |
The block is called with 3 arguments for each collection in the receiver." |
|
2361 |
||
2362 |
self do:[:aTriple | |
|
2639 | 2363 |
aBlock value:(aTriple at:1) value:(aTriple at:2) value:(aTriple at:3). |
362 | 2364 |
] |
2365 |
" |
|
6555 | 2366 |
#( |
2367 |
(1 one eins) |
|
2368 |
(2 two zwei) |
|
2369 |
(3 three drei) |
|
2370 |
(4 four vier) |
|
2371 |
(5 five #'fuenf') |
|
2372 |
(6 six sechs) |
|
2373 |
) |
|
2374 |
triplesDo:[:num :sym1 :sym2 | |
|
2375 |
Transcript show:num; space; show:sym1; space; showCR:sym2 |
|
2376 |
] |
|
362 | 2377 |
" |
2639 | 2378 |
|
2379 |
"Modified: 10.5.1997 / 14:15:43 / cg" |
|
1 | 2380 |
! |
2381 |
||
602 | 2382 |
with:aCollection collect:aTwoArgBlock |
2383 |
"evaluate the argument, aBlock for successive elements from |
|
2384 |
each the receiver and the argument, aSequenceableCollection; |
|
2385 |
The second argument, aBlock must be a two-argument block, which is |
|
2386 |
evaluated for each element-pair. |
|
2387 |
Collect the results and return a collection containing them. |
|
2388 |
This method fails if neither the receiver nor aCollection is |
|
2389 |
a sequenceable collection (i.e. implements numeric key access)." |
|
260 | 2390 |
|
17747 | 2391 |
|newCollection| |
1 | 2392 |
|
6547
86e092527dd1
+#speciesForAdding; better #collect:with: for fixed size collections
Claus Gittinger <cg@exept.de>
parents:
6511
diff
changeset
|
2393 |
newCollection := self speciesForAdding new. |
17747 | 2394 |
self with:aCollection do:[:el1 :el2 | |
2395 |
newCollection add:(aTwoArgBlock value:el1 value:el2). |
|
602 | 2396 |
]. |
6547
86e092527dd1
+#speciesForAdding; better #collect:with: for fixed size collections
Claus Gittinger <cg@exept.de>
parents:
6511
diff
changeset
|
2397 |
^ newCollection as:self species |
201 | 2398 |
|
2399 |
" |
|
6547
86e092527dd1
+#speciesForAdding; better #collect:with: for fixed size collections
Claus Gittinger <cg@exept.de>
parents:
6511
diff
changeset
|
2400 |
(1 to:3) with:#(one two three) collect:[:num :sym | (num->sym)] |
17747 | 2401 |
#(1 2 3) with:#(10 20 30) collect:[:x :y | (x@y)] |
2402 |
" |
|
2403 |
! |
|
2404 |
||
2405 |
with:aCollection count:aTwoArgBlock |
|
2406 |
"evaluate the argument, aBlock for successive elements from |
|
2407 |
each the receiver and the argument, aSequenceableCollection. |
|
2408 |
Count, how often the second argument, aTwoArgBlock returns true. |
|
2409 |
This method fails if neither the receiver nor aCollection is |
|
2410 |
a sequenceable collection (i.e. implements numeric key access)." |
|
2411 |
||
2412 |
|count "{ Class: SmallInteger }"| |
|
2413 |
||
2414 |
count := 0. |
|
2415 |
self with:aCollection do:[:el1 :el2 | |
|
2416 |
(aTwoArgBlock value:el1 value:el2) ifTrue:[ |
|
2417 |
count := count + 1 |
|
2418 |
] |
|
2419 |
]. |
|
2420 |
^ count |
|
2421 |
||
2422 |
" |
|
2423 |
(1 to:3) with:#(1 3 3) count:[:n1 :n2 | n1 = n2] |
|
201 | 2424 |
" |
2 | 2425 |
! |
2426 |
||
602 | 2427 |
with:aCollection do:aTwoArgBlock |
2428 |
"evaluate the argument, aBlock for successive elements from |
|
2429 |
each the receiver and the argument, aSequenceableCollection. |
|
2430 |
The second argument, aBlock must be a two-argument block. |
|
2431 |
This method fails if neither the receiver nor aCollection is |
|
2432 |
a sequenceable collection (i.e. implements numeric key access)." |
|
2433 |
||
2434 |
|index "{ Class: SmallInteger }"| |
|
2 | 2435 |
|
602 | 2436 |
index := 1. |
2437 |
aCollection isSequenceable ifFalse:[ |
|
6135 | 2438 |
self isSequenceable ifFalse:[ |
2439 |
^ self error:'neither collection is sequenceable'. |
|
2440 |
]. |
|
2441 |
aCollection do:[:element | |
|
2442 |
aTwoArgBlock value:(self at:index) value:element. |
|
2443 |
index := index + 1 |
|
2444 |
] |
|
602 | 2445 |
] ifTrue:[ |
6135 | 2446 |
self do:[:element | |
2447 |
aTwoArgBlock value:element value:(aCollection at:index). |
|
2448 |
index := index + 1 |
|
2449 |
] |
|
602 | 2450 |
] |
201 | 2451 |
|
2452 |
" |
|
602 | 2453 |
(1 to:3) with:#(one two three) do:[:num :sym | |
6135 | 2454 |
Transcript showCR:(num->sym) |
602 | 2455 |
] |
2456 |
(1 to:3) with:#(one two three) asSet do:[:num :sym | |
|
6135 | 2457 |
Transcript showCR:(num->sym) |
602 | 2458 |
]. |
2459 |
(1 to:3) asSet with:#(one two three) do:[:num :sym | |
|
6135 | 2460 |
Transcript showCR:(num->sym) |
602 | 2461 |
] |
201 | 2462 |
" |
1 | 2463 |
! ! |
2464 |
||
5874 | 2465 |
!Collection methodsFor:'enumerating-tests'! |
2466 |
||
2467 |
allSatisfy:aBlock |
|
2468 |
"evaluate aBlock for each of the receiver's elements. |
|
2469 |
Return true, if aBlock returns true for all elements, false otherwise |
|
2470 |
(i.e. false if any element fails to satisfy the block-condition). |
|
2471 |
This is an ANSI renomer of #conform:" |
|
2472 |
||
2473 |
^ self conform:aBlock. |
|
2474 |
||
2475 |
" |
|
2476 |
#(1 2 3 4 5) allSatisfy:[:el | el odd] |
|
2477 |
#(2 4 6 8 10) allSatisfy:[:el | el odd] |
|
2478 |
#(2 4 6 8 10) allSatisfy:[:el | el even] |
|
2479 |
" |
|
2480 |
||
2481 |
! |
|
2482 |
||
2483 |
anySatisfy:aBlock |
|
2484 |
"evaluate aBlock for each of the receiver's elements. |
|
2485 |
Return true, if aBlock ever returns true, false otherwise |
|
2486 |
(i.e. if any element satisfies the block-condition). |
|
2487 |
This is an ANSI renomer of #contains:" |
|
2488 |
||
2489 |
^ self contains:aBlock. |
|
2490 |
||
2491 |
" |
|
2492 |
#(1 2 3 4 5) anySatisfy:[:el | el odd] |
|
2493 |
#(2 4 6 8 10) anySatisfy:[:el | el odd] |
|
2494 |
" |
|
2495 |
||
2496 |
! |
|
2497 |
||
9842 | 2498 |
conform:aOneArgBlock |
5874 | 2499 |
"return true, if every element conforms to some condition. |
2500 |
I.e. return false, if aBlock returns false for any element; |
|
2501 |
true otherwise." |
|
2502 |
||
9842 | 2503 |
self do:[:element | |
2504 |
(aOneArgBlock value:element) ifFalse:[^ false] |
|
2505 |
]. |
|
5874 | 2506 |
^ true |
9842 | 2507 |
|
2508 |
" |
|
2509 |
#(1 2 3 4 5) conform:[:el | el even] |
|
2510 |
#(2 4 6 8 10) conform:[:el | el even] |
|
2511 |
" |
|
2512 |
||
2513 |
"Modified: / 13-09-2006 / 11:19:03 / cg" |
|
5874 | 2514 |
! |
2515 |
||
9842 | 2516 |
contains:aOneArgBlock |
2517 |
"evaluate aOneArgBlock for each of the receiver's elements. |
|
5874 | 2518 |
Return true, if aBlock ever returns true, false otherwise." |
2519 |
||
9842 | 2520 |
self do:[:element | |
2521 |
(aOneArgBlock value:element) ifTrue:[^ true] |
|
2522 |
]. |
|
2523 |
^ false |
|
5874 | 2524 |
|
2525 |
" |
|
2526 |
#(1 2 3 4 5) contains:[:el | el odd] |
|
2527 |
#(2 4 6 8 10) contains:[:el | el odd] |
|
2528 |
" |
|
2529 |
||
9842 | 2530 |
"Modified: / 13-09-2006 / 11:18:36 / cg" |
8577 | 2531 |
! |
2532 |
||
2533 |
noneSatisfy:aBlock |
|
2534 |
"evaluate aBlock for each of the receiver's elements. |
|
2535 |
Return true, if aBlock returns false for all elements, false otherwise |
|
2536 |
(i.e. false if any element satisfies the block-condition)." |
|
2537 |
||
9843 | 2538 |
^ (self contains:aBlock) not |
8577 | 2539 |
|
2540 |
" |
|
2541 |
#(1 2 3 4 5) noneSatisfy:[:el | el odd] |
|
2542 |
#(2 4 6 8 10) noneSatisfy:[:el | el odd] |
|
2543 |
#(2 4 6 8 10) noneSatisfy:[:el | el even] |
|
2544 |
" |
|
9843 | 2545 |
|
2546 |
"Modified: / 13-09-2006 / 11:19:57 / cg" |
|
5874 | 2547 |
! ! |
2548 |
||
602 | 2549 |
!Collection methodsFor:'error handling'! |
2550 |
||
2551 |
emptyCheck |
|
2552 |
"check if the receiver is empty; report an error if so" |
|
2553 |
||
11475
f7e1a355df17
changed #emptyCheck (is isEmpty instead of size == 0)
Claus Gittinger <cg@exept.de>
parents:
11470
diff
changeset
|
2554 |
self isEmpty ifTrue:[ |
6135 | 2555 |
^ self emptyCollectionError. |
602 | 2556 |
]. |
2557 |
! |
|
2558 |
||
2559 |
emptyCollectionError |
|
2560 |
"report an error that the operation is not allowed for |
|
2561 |
empty collections" |
|
2562 |
||
2563 |
^ EmptyCollectionSignal raise |
|
2564 |
! |
|
2565 |
||
2566 |
errorInvalidKey:aKey |
|
2567 |
"report an error that the given key was invalid" |
|
2568 |
||
2569 |
^ InvalidKeySignal raiseRequestWith:aKey |
|
2570 |
! |
|
2571 |
||
2572 |
errorNotKeyed |
|
2573 |
"report an error that keyed access methods are not allowed" |
|
2574 |
||
2575 |
^ self error:(self class name, 's do not respond to keyed accessing messages') |
|
2576 |
! |
|
2577 |
||
2578 |
errorValueNotFound:anObject |
|
2579 |
"report an error that an object was not found in the collection" |
|
2580 |
||
6757 | 2581 |
^ ValueNotFoundSignal raiseRequestWith:anObject |
3075 | 2582 |
|
2583 |
"Modified: / 30.10.1997 / 15:52:18 / cg" |
|
602 | 2584 |
! |
2585 |
||
2586 |
notEnoughElementsError |
|
2587 |
"report an error that the operation is not allowed, |
|
2588 |
since not enough elements are in the collection" |
|
2589 |
||
2590 |
^ NotEnoughElementsSignal raise |
|
2591 |
! ! |
|
2592 |
||
2593 |
!Collection methodsFor:'growing'! |
|
2594 |
||
9309
d1fe8655bb97
move #changeCapacityTo: from Set to Collection
Stefan Vogel <sv@exept.de>
parents:
9247
diff
changeset
|
2595 |
changeCapacityTo:newSize |
d1fe8655bb97
move #changeCapacityTo: from Set to Collection
Stefan Vogel <sv@exept.de>
parents:
9247
diff
changeset
|
2596 |
newSize > self capacity ifTrue:[ |
11315
9a6ca3147b69
#showWhereWeCameFrom - show more stack backtrace
Stefan Vogel <sv@exept.de>
parents:
11309
diff
changeset
|
2597 |
self grow:newSize |
9309
d1fe8655bb97
move #changeCapacityTo: from Set to Collection
Stefan Vogel <sv@exept.de>
parents:
9247
diff
changeset
|
2598 |
]. |
d1fe8655bb97
move #changeCapacityTo: from Set to Collection
Stefan Vogel <sv@exept.de>
parents:
9247
diff
changeset
|
2599 |
|
d1fe8655bb97
move #changeCapacityTo: from Set to Collection
Stefan Vogel <sv@exept.de>
parents:
9247
diff
changeset
|
2600 |
"Created: / 30.10.2001 / 17:56:50 / cg" |
d1fe8655bb97
move #changeCapacityTo: from Set to Collection
Stefan Vogel <sv@exept.de>
parents:
9247
diff
changeset
|
2601 |
! |
d1fe8655bb97
move #changeCapacityTo: from Set to Collection
Stefan Vogel <sv@exept.de>
parents:
9247
diff
changeset
|
2602 |
|
602 | 2603 |
grow |
2604 |
"make the receiver larger" |
|
2605 |
||
2606 |
self grow:(self size + self growSize) |
|
2607 |
! |
|
2608 |
||
2609 |
grow:howBig |
|
2610 |
"change the receivers size" |
|
2611 |
||
2612 |
^ self subclassResponsibility |
|
2613 |
! |
|
2614 |
||
2615 |
growSize |
|
2616 |
"return a suitable size increment for growing. |
|
2617 |
The default returned here may be (and is) redefined in subclasses." |
|
2618 |
||
2619 |
^ self size max:2 |
|
2620 |
! ! |
|
2621 |
||
10092
70a5cb4e5df0
code cleanup: use #contains/#conform instead of explicit loop
Claus Gittinger <cg@exept.de>
parents:
9919
diff
changeset
|
2622 |
|
11412 | 2623 |
!Collection methodsFor:'operations'! |
2624 |
||
2625 |
decrementAt:aKey |
|
2626 |
self at:aKey put:(self at:aKey) - 1. |
|
2627 |
! |
|
2628 |
||
2629 |
incrementAt:aKey |
|
2630 |
self at:aKey put:(self at:aKey) + 1. |
|
2631 |
! ! |
|
2632 |
||
1 | 2633 |
!Collection methodsFor:'printing & storing'! |
2634 |
||
602 | 2635 |
displayString |
2636 |
"return a printed representation of the receiver for display in inspectors etc." |
|
2637 |
||
6511
0ca4f36b16ca
signal an error in #anElement if there is none;
Claus Gittinger <cg@exept.de>
parents:
6506
diff
changeset
|
2638 |
|thisString buffer noneYet total limit| |
602 | 2639 |
|
2640 |
thisContext isRecursive ifTrue:[ |
|
3191
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2641 |
'Collection [error]: displayString of self referencing collection.' errorPrintCR. |
1413 | 2642 |
^ '#("recursive")' |
602 | 2643 |
]. |
2644 |
||
8222
f2c454a9a038
replaced '' writeStream by String writeStream
Claus Gittinger <cg@exept.de>
parents:
8182
diff
changeset
|
2645 |
buffer := String writeStream. |
6511
0ca4f36b16ca
signal an error in #anElement if there is none;
Claus Gittinger <cg@exept.de>
parents:
6506
diff
changeset
|
2646 |
buffer nextPutAll:(self displayStringName); nextPutAll:'('. |
602 | 2647 |
noneYet := true. |
2648 |
total := 0. |
|
2649 |
limit := self maxPrint. |
|
2650 |
||
3191
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2651 |
self printElementsDo: [:element | |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2652 |
thisString := element displayString. |
1413 | 2653 |
noneYet ifTrue:[ |
2654 |
noneYet := false. |
|
2655 |
] ifFalse:[ |
|
6511
0ca4f36b16ca
signal an error in #anElement if there is none;
Claus Gittinger <cg@exept.de>
parents:
6506
diff
changeset
|
2656 |
buffer space |
1413 | 2657 |
]. |
6511
0ca4f36b16ca
signal an error in #anElement if there is none;
Claus Gittinger <cg@exept.de>
parents:
6506
diff
changeset
|
2658 |
buffer nextPutAll:thisString. |
1413 | 2659 |
total := total + 1. |
2660 |
(total > limit) ifTrue:[ |
|
6511
0ca4f36b16ca
signal an error in #anElement if there is none;
Claus Gittinger <cg@exept.de>
parents:
6506
diff
changeset
|
2661 |
buffer nextPutAll:'... )'. |
0ca4f36b16ca
signal an error in #anElement if there is none;
Claus Gittinger <cg@exept.de>
parents:
6506
diff
changeset
|
2662 |
^ buffer contents |
1413 | 2663 |
] |
602 | 2664 |
]. |
6511
0ca4f36b16ca
signal an error in #anElement if there is none;
Claus Gittinger <cg@exept.de>
parents:
6506
diff
changeset
|
2665 |
buffer nextPutAll:')'. |
0ca4f36b16ca
signal an error in #anElement if there is none;
Claus Gittinger <cg@exept.de>
parents:
6506
diff
changeset
|
2666 |
^ buffer contents |
1413 | 2667 |
|
3191
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2668 |
" |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2669 |
#(1 2 3 'hello' $a) asOrderedCollection displayString |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2670 |
|
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2671 |
(Dictionary new at:#hello put:'world'; |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2672 |
at:#foo put:'bar'; yourself) displayString |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2673 |
" |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2674 |
|
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2675 |
"Modified: / 20.1.1998 / 14:11:03 / stefan" |
3964
a3129832fe16
allow redefinition of the displayed class name (displayString)
Claus Gittinger <cg@exept.de>
parents:
3767
diff
changeset
|
2676 |
"Modified: / 2.2.1999 / 22:39:44 / cg" |
a3129832fe16
allow redefinition of the displayed class name (displayString)
Claus Gittinger <cg@exept.de>
parents:
3767
diff
changeset
|
2677 |
! |
a3129832fe16
allow redefinition of the displayed class name (displayString)
Claus Gittinger <cg@exept.de>
parents:
3767
diff
changeset
|
2678 |
|
a3129832fe16
allow redefinition of the displayed class name (displayString)
Claus Gittinger <cg@exept.de>
parents:
3767
diff
changeset
|
2679 |
displayStringName |
a3129832fe16
allow redefinition of the displayed class name (displayString)
Claus Gittinger <cg@exept.de>
parents:
3767
diff
changeset
|
2680 |
"redefinable helper for displayString" |
a3129832fe16
allow redefinition of the displayed class name (displayString)
Claus Gittinger <cg@exept.de>
parents:
3767
diff
changeset
|
2681 |
|
a3129832fe16
allow redefinition of the displayed class name (displayString)
Claus Gittinger <cg@exept.de>
parents:
3767
diff
changeset
|
2682 |
^ (self class name) |
a3129832fe16
allow redefinition of the displayed class name (displayString)
Claus Gittinger <cg@exept.de>
parents:
3767
diff
changeset
|
2683 |
|
a3129832fe16
allow redefinition of the displayed class name (displayString)
Claus Gittinger <cg@exept.de>
parents:
3767
diff
changeset
|
2684 |
"Created: / 2.2.1999 / 22:39:33 / cg" |
3191
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2685 |
! |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2686 |
|
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2687 |
maxPrint |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2688 |
"the print-limit; printOn: will try to not produce more output |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2689 |
than the limit defined here." |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2690 |
|
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2691 |
^ 5000 |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2692 |
! |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2693 |
|
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2694 |
printElementsDo:aBlock |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2695 |
"perform aBlock (1 arg) for all elements. |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2696 |
Used in #printOn:. |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2697 |
Subclasses (e.g. Dictionary) may redefine this." |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2698 |
|
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2699 |
^ self do:aBlock |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2700 |
|
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2701 |
"Created: / 20.1.1998 / 14:11:02 / stefan" |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2702 |
! |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2703 |
|
11599 | 2704 |
printElementsOn:aStream |
3191
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2705 |
"append a user readable representation of the receiver to aStream. |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2706 |
The text appended is not meant to be read back for reconstruction of |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2707 |
the receiver. Also, this method limits the size of generated string. |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2708 |
" |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2709 |
|
3402 | 2710 |
|limit firstOne s| |
3191
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2711 |
|
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2712 |
thisContext isRecursive ifTrue:[ |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2713 |
'Collection [error]: printOn: of self referencing collection.' errorPrintCR. |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2714 |
aStream nextPutAll:'#("recursive")'. |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2715 |
^ self |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2716 |
]. |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2717 |
|
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2718 |
aStream nextPut:$(. |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2719 |
firstOne := true. |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2720 |
|
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2721 |
" |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2722 |
if aStream is not positionable, create an temporary positionable stream |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2723 |
(needed for limit calculation) |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2724 |
" |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2725 |
aStream isPositionable ifTrue:[ |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2726 |
s := aStream. |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2727 |
] ifFalse:[ |
7685
535a69a7cd69
String new -> uninitializedNew: / basicNew:
Claus Gittinger <cg@exept.de>
parents:
7629
diff
changeset
|
2728 |
s := WriteStream on:(String uninitializedNew:50). |
3191
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2729 |
]. |
7054
685d359f9847
code rewritten to be independent of stream zero-base
Claus Gittinger <cg@exept.de>
parents:
7011
diff
changeset
|
2730 |
limit := s position1Based + self maxPrint. |
3191
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2731 |
|
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2732 |
self printElementsDo:[:element | |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2733 |
firstOne ifFalse:[ |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2734 |
s space |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2735 |
] ifTrue:[ |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2736 |
firstOne := false |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2737 |
]. |
7054
685d359f9847
code rewritten to be independent of stream zero-base
Claus Gittinger <cg@exept.de>
parents:
7011
diff
changeset
|
2738 |
(s position1Based >= limit) ifTrue:[ |
3191
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2739 |
s ~~ aStream ifTrue:[ |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2740 |
aStream nextPutAll:(s contents). |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2741 |
]. |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2742 |
aStream nextPutAll:'...etc...)'. |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2743 |
^ self |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2744 |
] ifFalse:[ |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2745 |
element printOn:s. |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2746 |
]. |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2747 |
]. |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2748 |
s ~~ aStream ifTrue:[ |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2749 |
aStream nextPutAll:(s contents). |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2750 |
]. |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2751 |
aStream nextPut:$) |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2752 |
|
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2753 |
" |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2754 |
#(1 2 3 'hello' $a) printOn:Transcript |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2755 |
(Array new:100000) printOn:Transcript |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2756 |
(Array new:100000) printOn:Stdout |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2757 |
(Array new:100000) printString size |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2758 |
(Dictionary new at:#hello put:'world'; |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2759 |
at:#foo put:'bar'; yourself) printOn:Transcript |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2760 |
" |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2761 |
" |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2762 |
|a| |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2763 |
a := Array new:3. |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2764 |
a at:2 put:a. |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2765 |
a printOn:Transcript |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2766 |
" |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2767 |
|
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2768 |
"Modified: / 28.1.1997 / 00:39:17 / cg" |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2769 |
"Modified: / 20.1.1998 / 14:11:03 / stefan" |
602 | 2770 |
! |
2771 |
||
11599 | 2772 |
printOn:aStream |
2773 |
"append a user readable representation of the receiver to aStream. |
|
2774 |
The text appended is not meant to be read back for reconstruction of |
|
2775 |
the receiver. Also, this method limits the size of generated string. |
|
2776 |
" |
|
2777 |
||
2778 |
thisContext isRecursive ifTrue:[ |
|
2779 |
'Collection [error]: printOn: of self referencing collection.' errorPrintCR. |
|
2780 |
aStream nextPutAll:'#("recursive")'. |
|
2781 |
^ self |
|
2782 |
]. |
|
2783 |
||
2784 |
aStream nextPutAll:self class name. |
|
2785 |
self printElementsOn:aStream. |
|
2786 |
||
2787 |
" |
|
2788 |
#(1 2 3 'hello' $a) printOn:Transcript |
|
2789 |
(Array new:100000) printOn:Transcript |
|
2790 |
(Array new:100000) printOn:Stdout |
|
2791 |
(Array new:100000) printString size |
|
2792 |
(Dictionary new at:#hello put:'world'; |
|
2793 |
at:#foo put:'bar'; yourself) printOn:Transcript |
|
2794 |
" |
|
2795 |
" |
|
2796 |
|a| |
|
2797 |
a := Array new:3. |
|
2798 |
a at:2 put:a. |
|
2799 |
a printOn:Transcript |
|
2800 |
" |
|
2801 |
||
2802 |
"Modified: / 28.1.1997 / 00:39:17 / cg" |
|
2803 |
"Modified: / 20.1.1998 / 14:11:03 / stefan" |
|
2804 |
! |
|
2805 |
||
1 | 2806 |
storeOn:aStream |
88 | 2807 |
"output a printed representation onto the argument, aStream. |
2808 |
The text can be re-read to reconstruct (a copy of) the receiver. |
|
2809 |
Recursive (i.e. cyclic) collections cannot be stored correctly |
|
2810 |
(use storeBinaryOn: to handle those)." |
|
1 | 2811 |
|
5254 | 2812 |
|notEmpty| |
1 | 2813 |
|
44 | 2814 |
thisContext isRecursive ifTrue:[ |
5498 | 2815 |
Object recursiveStoreStringSignal raiseRequestWith:self. |
2289 | 2816 |
'Collection [error]: storeOn: of self referencing collection.' errorPrintCR. |
1413 | 2817 |
aStream nextPutAll:'#recursive'. |
2818 |
^ self |
|
44 | 2819 |
]. |
2820 |
||
2821 |
aStream nextPutAll:'('. |
|
1 | 2822 |
aStream nextPutAll:(self class name). |
44 | 2823 |
aStream nextPutAll:' new'. |
5254 | 2824 |
notEmpty := false. |
1 | 2825 |
self do:[:element | |
1413 | 2826 |
aStream nextPutAll:' add:'. |
2827 |
element storeOn:aStream. |
|
2828 |
aStream nextPutAll:';'. |
|
5254 | 2829 |
notEmpty := true |
1 | 2830 |
]. |
5254 | 2831 |
notEmpty ifTrue:[aStream nextPutAll:' yourself']. |
1 | 2832 |
aStream nextPut:$) |
44 | 2833 |
|
88 | 2834 |
" |
2835 |
OrderedCollection new storeOn:Transcript |
|
2836 |
(1 to:10) storeOn:Transcript |
|
2837 |
(Set new add:1; add:'hello'; yourself) storeOn:Transcript |
|
2838 |
" |
|
293 | 2839 |
" |
2840 |
|s| |
|
2841 |
||
2842 |
s := Set new. |
|
2843 |
s add:1; add:'hello'; add:s. |
|
2844 |
s storeOn:Transcript |
|
2845 |
" |
|
1413 | 2846 |
|
5254 | 2847 |
"Modified: / 11.2.2000 / 11:24:56 / cg" |
1 | 2848 |
! ! |
602 | 2849 |
|
2850 |
!Collection methodsFor:'queries'! |
|
2851 |
||
5359
396b77b6f1d2
added #isSortedCollection, #isSortedBy:
Claus Gittinger <cg@exept.de>
parents:
5268
diff
changeset
|
2852 |
defaultElement |
396b77b6f1d2
added #isSortedCollection, #isSortedBy:
Claus Gittinger <cg@exept.de>
parents:
5268
diff
changeset
|
2853 |
^ nil |
396b77b6f1d2
added #isSortedCollection, #isSortedBy:
Claus Gittinger <cg@exept.de>
parents:
5268
diff
changeset
|
2854 |
! |
396b77b6f1d2
added #isSortedCollection, #isSortedBy:
Claus Gittinger <cg@exept.de>
parents:
5268
diff
changeset
|
2855 |
|
9919
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
2856 |
largest:n |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
2857 |
"return the n largest elements" |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
2858 |
|
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
2859 |
|mySize loopAction actionForFirstN actionForRest |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
2860 |
nLargest sz_nLargest minInLargest| |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
2861 |
|
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
2862 |
mySize := self size. |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
2863 |
n > mySize ifTrue:[ |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
2864 |
self notEnoughElementsError |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
2865 |
]. |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
2866 |
|
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
2867 |
(n < 50) ifTrue:[ |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
2868 |
n == 1 ifTrue:[ ^ Array with:self max ]. |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
2869 |
n == 2 ifTrue:[ |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
2870 |
|l1 l2| |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
2871 |
|
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
2872 |
loopAction := [:el | l1 := el. |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
2873 |
loopAction := |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
2874 |
[:el | |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
2875 |
el > l1 ifTrue:[ |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
2876 |
l2 := l1. l1 := el |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
2877 |
] ifFalse:[ |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
2878 |
l2 := el |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
2879 |
]. |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
2880 |
loopAction := actionForRest |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
2881 |
] |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
2882 |
]. |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
2883 |
actionForRest := [:el | |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
2884 |
el > l2 ifTrue:[ |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
2885 |
el > l1 ifTrue:[ |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
2886 |
l2 := l1. l1 := el |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
2887 |
] ifFalse:[ |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
2888 |
l2 := el |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
2889 |
]. |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
2890 |
]. |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
2891 |
]. |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
2892 |
|
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
2893 |
self do:[:el | loopAction value:el ]. |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
2894 |
^ Array with:l2 with:l1 |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
2895 |
]. |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
2896 |
|
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
2897 |
nLargest := SortedCollection new:n. |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
2898 |
sz_nLargest := 0. |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
2899 |
actionForFirstN := |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
2900 |
[:el | |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
2901 |
nLargest add:el. |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
2902 |
sz_nLargest := sz_nLargest + 1. |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
2903 |
sz_nLargest == n ifTrue:[ |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
2904 |
loopAction := actionForRest. |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
2905 |
minInLargest := nLargest min. |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
2906 |
]. |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
2907 |
]. |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
2908 |
actionForRest := |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
2909 |
[:el | |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
2910 |
el > minInLargest ifTrue:[ |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
2911 |
nLargest removeFirst. |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
2912 |
nLargest add:el. |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
2913 |
minInLargest := nLargest min |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
2914 |
]. |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
2915 |
]. |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
2916 |
|
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
2917 |
loopAction := actionForFirstN. |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
2918 |
self do:[:el | loopAction value:el ]. |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
2919 |
^ nLargest |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
2920 |
]. |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
2921 |
|
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
2922 |
^ self asSortedCollection copyTo:n |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
2923 |
|
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
2924 |
" |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
2925 |
#(10 35 20 45 30 5) largest:1 |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
2926 |
#(10 35 20 45 30 5) largest:2 |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
2927 |
#(10 35 20 45 30 5) largest:3 |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
2928 |
#(10 35 20 45 30 5) largest:5 |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
2929 |
#(10 35 20 45 30 5) largest:6 |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
2930 |
#(10 35 20 45 30 5) largest:8 |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
2931 |
" |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
2932 |
|
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
2933 |
" |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
2934 |
|t1 t2 data| |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
2935 |
|
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
2936 |
data := (1 to:10000) collect:[:i | Random nextInteger ]. |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
2937 |
t1 := Time millisecondsToRun:[ |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
2938 |
40 timesRepeat:[ |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
2939 |
data asSortedCollection at:3 |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
2940 |
] |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
2941 |
]. |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
2942 |
t2 := Time millisecondsToRun:[ |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
2943 |
40 timesRepeat:[ |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
2944 |
data largest:3 |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
2945 |
]. |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
2946 |
]. |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
2947 |
Transcript show:'asSorted-at -> '; show:t1; showCR:'ms'. |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
2948 |
Transcript show:'largest -> '; show:t2; showCR:'ms'. |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
2949 |
" |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
2950 |
! |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
2951 |
|
602 | 2952 |
longestCommonPrefix |
1246 | 2953 |
"return the longest common prefix of my elements. |
2954 |
Typically used with string collections." |
|
2955 |
||
5532 | 2956 |
^ self longestCommonPrefixIgnoreCase:false |
2957 |
||
2958 |
" |
|
2959 |
#('Array' 'ArrayedCollection' 'ArrayOfFoo') longestCommonPrefix |
|
2960 |
#('Arra' 'ArrayedCollection' 'ArrayOfFoo') longestCommonPrefix |
|
2961 |
#('Arra' 'b' 'c') longestCommonPrefix |
|
2962 |
#( (1 2 3 4) (1 2 3) (1 2 3 7) (1 2 3 9 10 11)) longestCommonPrefix |
|
2963 |
" |
|
2964 |
||
2965 |
"Modified: 2.3.1997 / 00:21:41 / cg" |
|
2966 |
! |
|
2967 |
||
2968 |
longestCommonPrefixIgnoreCase:ignoreCase |
|
6505
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
2969 |
"return the longest common prefix of my elements (which must be sequenceableCollections). |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
2970 |
Typically used with string collections, |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
2971 |
especially with completion of selectors or filenames." |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
2972 |
|
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
2973 |
|longest| |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
2974 |
|
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
2975 |
self do:[:eachCollection | |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
2976 |
longest isNil ifTrue:[ |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
2977 |
longest := eachCollection |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
2978 |
] ifFalse:[ |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
2979 |
longest := longest commonPrefixWith:eachCollection ignoreCase:ignoreCase |
1246 | 2980 |
] |
602 | 2981 |
]. |
6505
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
2982 |
^ longest. |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
2983 |
|
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
2984 |
"/ |longest try allMatching matchLen| |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
2985 |
"/ |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
2986 |
"/ " |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
2987 |
"/ find the longest common prefix |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
2988 |
"/ " |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
2989 |
"/ matchLen := 0. |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
2990 |
"/ self do:[:aName | |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
2991 |
"/ aName size > matchLen ifTrue:[ |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
2992 |
"/ matchLen := aName size. |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
2993 |
"/ try := aName |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
2994 |
"/ ] |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
2995 |
"/ ]. |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
2996 |
"/ |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
2997 |
"/ allMatching := true. |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
2998 |
"/ |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
2999 |
"/ [true] whileTrue:[ |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3000 |
"/ allMatching := true. |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3001 |
"/ self do:[:aName | |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3002 |
"/ ((ignoreCase not and:[aName startsWith:try]) |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3003 |
"/ or:[ignoreCase and:[aName asLowercase startsWith:try asLowercase]]) ifFalse:[ |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3004 |
"/ allMatching := false |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3005 |
"/ ] |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3006 |
"/ ]. |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3007 |
"/ allMatching ifTrue:[ |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3008 |
"/ ^ try |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3009 |
"/ ]. |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3010 |
"/ matchLen := matchLen - 1. |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3011 |
"/ matchLen == 0 ifTrue:[^ '']. |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3012 |
"/ try := try copyTo:matchLen. |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3013 |
"/ ]. |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3014 |
"/ ^ try |
602 | 3015 |
|
3016 |
" |
|
5532 | 3017 |
#('Array' 'arrayedCollection' 'ARRAYOfFoo') longestCommonPrefixIgnoreCase:true |
3018 |
#('Array' 'arrayedCollection' 'ARRAYOfFoo') longestCommonPrefixIgnoreCase:false |
|
3019 |
#('Array' 'ArayedCollection' 'ARRAYOfFoo') longestCommonPrefixIgnoreCase:false |
|
6505
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3020 |
#('AAA' 'A11' 'AA2') longestCommonPrefixIgnoreCase:false |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3021 |
#('AAA' 'BBB' 'CCC') longestCommonPrefixIgnoreCase:false |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3022 |
" |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3023 |
! |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3024 |
|
6506 | 3025 |
longestCommonSuffix |
3026 |
"return the longest common suffix (tail) of my elements. |
|
3027 |
Typically used with string collections." |
|
3028 |
||
3029 |
^ self longestCommonSuffixIgnoreCase:false |
|
3030 |
! |
|
3031 |
||
6505
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3032 |
longestCommonSuffixIgnoreCase:ignoreCase |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3033 |
"return the longest common suffix (tail) of my elements |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3034 |
(which must be sequenceableCollections)." |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3035 |
|
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3036 |
|longest| |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3037 |
|
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3038 |
self do:[:eachCollection | |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3039 |
longest isNil ifTrue:[ |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3040 |
longest := eachCollection |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3041 |
] ifFalse:[ |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3042 |
longest := longest commonSuffixWith:eachCollection ignoreCase:ignoreCase |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3043 |
] |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3044 |
]. |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3045 |
^ longest. |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3046 |
|
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3047 |
" |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3048 |
#('Array' 'ByteArray' 'BigArray') longestCommonSuffixIgnoreCase:true |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3049 |
#('AAA' 'BBBAA' 'CCCAAAA') longestCommonSuffixIgnoreCase:false |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3050 |
#('AAA' 'BBB' 'CCC') longestCommonSuffixIgnoreCase:false |
602 | 3051 |
" |
5557 | 3052 |
! |
3053 |
||
7821 | 3054 |
max |
3055 |
"return the maximum value in the receiver collection, |
|
8182
e99d0f95c925
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8178
diff
changeset
|
3056 |
using #< to compare elements. |
7821 | 3057 |
Raises an error, if the receiver is empty." |
3058 |
||
3059 |
self emptyCheck. |
|
3060 |
||
3061 |
^ self inject:nil |
|
3062 |
into:[:maxSoFar :this | |
|
3063 |
(maxSoFar isNil |
|
8182
e99d0f95c925
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8178
diff
changeset
|
3064 |
or:[maxSoFar < this]) ifTrue:[this] |
7821 | 3065 |
ifFalse:[maxSoFar]] |
3066 |
||
3067 |
" |
|
3068 |
#(15 1 -9 10 5) max |
|
3069 |
(1 to:15) max |
|
3070 |
(-1 to:-15 by:-1) max |
|
3071 |
(-1 to:-15 by:-4) max |
|
3072 |
(0 to:15 by:4) max |
|
3073 |
" |
|
3074 |
||
3075 |
"Modified: 8.1.1997 / 22:09:37 / cg" |
|
3076 |
! |
|
3077 |
||
3078 |
min |
|
3079 |
"return the minimum value in the receiver collection, |
|
3080 |
using < to compare elements. |
|
3081 |
Raises an error, if the receiver is empty." |
|
3082 |
||
3083 |
self emptyCheck. |
|
3084 |
||
3085 |
^ self inject:nil |
|
3086 |
into:[:minSoFar :this | |
|
3087 |
(minSoFar isNil |
|
3088 |
or:[this < minSoFar]) ifTrue:[this] |
|
3089 |
ifFalse:[minSoFar]] |
|
3090 |
||
3091 |
" |
|
3092 |
#(15 1 -9 10 5) min |
|
3093 |
(1 to:15) min |
|
3094 |
(-1 to:-15 by:-1) min |
|
3095 |
(-1 to:-15 by:-4) min |
|
3096 |
" |
|
3097 |
||
3098 |
"Modified: 8.1.1997 / 22:09:49 / cg" |
|
3099 |
! |
|
3100 |
||
3101 |
minMax |
|
3102 |
"return the minimum and maximum values in the receiver collection |
|
8182
e99d0f95c925
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8178
diff
changeset
|
3103 |
as a two element array, using #< to compare elements. |
7821 | 3104 |
Raises an error, if the receiver is empty." |
3105 |
||
3106 |
|min max| |
|
3107 |
||
3108 |
self emptyCheck. |
|
3109 |
||
3110 |
min := max := nil. |
|
3111 |
self do:[:each | |
|
3112 |
min isNil ifTrue:[ |
|
3113 |
min := max := each |
|
3114 |
] ifFalse:[ |
|
8182
e99d0f95c925
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8178
diff
changeset
|
3115 |
max < each ifTrue:[ |
7821 | 3116 |
max := each |
3117 |
] ifFalse:[ |
|
8182
e99d0f95c925
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8178
diff
changeset
|
3118 |
each < min ifTrue:[ |
7821 | 3119 |
min := each |
3120 |
] |
|
3121 |
] |
|
3122 |
] |
|
3123 |
]. |
|
3124 |
^ Array with:min with:max |
|
3125 |
||
3126 |
" |
|
3127 |
#(15 1 -9 10 5) minMax |
|
3128 |
(1 to:15) minMax |
|
3129 |
(-1 to:-15 by:-1) minMax |
|
3130 |
(-1 to:-15 by:-4) minMax |
|
3131 |
(0 to:15 by:4) minMax |
|
3132 |
" |
|
3133 |
! |
|
3134 |
||
9919
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3135 |
nthLargest:n |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3136 |
"return the n-largest element" |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3137 |
|
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3138 |
^ (self largest:n) first |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3139 |
|
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3140 |
" |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3141 |
#(10 35 20 45 30 5) nthLargest:1 |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3142 |
#(10 35 20 45 30 5) nthLargest:2 |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3143 |
#(10 35 20 45 30 5) nthLargest:3 |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3144 |
#(10 35 20 45 30 5) nthLargest:5 |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3145 |
#(10 35 20 45 30 5) nthLargest:6 |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3146 |
#(10 35 20 45 30 5) nthLargest:8 |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3147 |
" |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3148 |
|
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3149 |
" |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3150 |
|t1 t2 data| |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3151 |
|
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3152 |
data := (1 to:10000) collect:[:i | Random nextInteger ]. |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3153 |
t1 := Time millisecondsToRun:[ |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3154 |
40 timesRepeat:[ |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3155 |
data asSortedCollection at:6 |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3156 |
] |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3157 |
]. |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3158 |
t2 := Time millisecondsToRun:[ |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3159 |
40 timesRepeat:[ |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3160 |
data nthLargest:6 |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3161 |
]. |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3162 |
]. |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3163 |
Transcript show:'asSorted-at -> '; show:t1; showCR:'ms'. |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3164 |
Transcript show:'nthMost -> '; show:t2; showCR:'ms'. |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3165 |
" |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3166 |
! |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3167 |
|
5557 | 3168 |
size |
3169 |
"return the number of elements in the receiver. |
|
3170 |
This is usually redefined in subclasses for more performance." |
|
3171 |
||
3172 |
|count "{ Class: SmallInteger }" | |
|
3173 |
||
3174 |
count := 0. |
|
3175 |
self do:[:element | |
|
6135 | 3176 |
count := count + 1 |
5557 | 3177 |
]. |
3178 |
^ count |
|
6547
86e092527dd1
+#speciesForAdding; better #collect:with: for fixed size collections
Claus Gittinger <cg@exept.de>
parents:
6511
diff
changeset
|
3179 |
! |
86e092527dd1
+#speciesForAdding; better #collect:with: for fixed size collections
Claus Gittinger <cg@exept.de>
parents:
6511
diff
changeset
|
3180 |
|
86e092527dd1
+#speciesForAdding; better #collect:with: for fixed size collections
Claus Gittinger <cg@exept.de>
parents:
6511
diff
changeset
|
3181 |
speciesForAdding |
86e092527dd1
+#speciesForAdding; better #collect:with: for fixed size collections
Claus Gittinger <cg@exept.de>
parents:
6511
diff
changeset
|
3182 |
"like species, but redefined for collections which cannot grow easily. |
86e092527dd1
+#speciesForAdding; better #collect:with: for fixed size collections
Claus Gittinger <cg@exept.de>
parents:
6511
diff
changeset
|
3183 |
Used by functions which create a growing collection (see collect:with:, for example)" |
86e092527dd1
+#speciesForAdding; better #collect:with: for fixed size collections
Claus Gittinger <cg@exept.de>
parents:
6511
diff
changeset
|
3184 |
|
86e092527dd1
+#speciesForAdding; better #collect:with: for fixed size collections
Claus Gittinger <cg@exept.de>
parents:
6511
diff
changeset
|
3185 |
^ self species |
602 | 3186 |
! ! |
3187 |
||
11706 | 3188 |
!Collection methodsFor:'searching'! |
3189 |
||
3190 |
findFirst:aBlock |
|
3191 |
"find the index of the first element, for which evaluation of the argument, aBlock |
|
3192 |
returns true; return its index or 0 if none detected. |
|
3193 |
This is much like #detect, however, here an INDEX is returned, |
|
3194 |
while #detect returns the element." |
|
3195 |
||
3196 |
^ self findFirst:aBlock ifNone:0 |
|
3197 |
||
3198 |
" |
|
3199 |
#(1 2 3 4 5 6) findFirst:[:x | (x >= 3)] |
|
3200 |
#(1 2 3 4 5 6) findFirst:[:x | (x >= 3) and:[x even]] |
|
3201 |
#(1 2 3 4 5 6) findFirst:[:x | (x >= 8)] |
|
3202 |
'one.two.three' findFirst:[:c | (c == $.)] |
|
3203 |
'__one.two.three' findFirst:[:c | (c ~= $_)] |
|
3204 |
'one.two.three' findFirst:[:c | (c ~= $_)] |
|
3205 |
" |
|
3206 |
||
3207 |
"Modified: / 21.10.1998 / 18:48:10 / cg" |
|
3208 |
! ! |
|
3209 |
||
7168
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3210 |
!Collection methodsFor:'set operations'! |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3211 |
|
7169 | 3212 |
\ aCollection |
8606 | 3213 |
"return a new set containing all elements of the receiver, |
3214 |
which are NOT also contained in the aCollection |
|
7168
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3215 |
For large collections you better use a Set for aCollection" |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3216 |
|
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3217 |
|
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3218 |
|newCollection| |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3219 |
|
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3220 |
newCollection := self speciesForAdding new. |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3221 |
self do:[:element | |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3222 |
(aCollection includes:element) ifFalse:[ |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3223 |
newCollection add:element |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3224 |
] |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3225 |
]. |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3226 |
^ newCollection |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3227 |
|
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3228 |
" |
7169 | 3229 |
#(0 1 2 3 4 5 6 7 8 9) \ #(1 2 3) asSet |
3230 |
#(0 1 2 3 4 5 6 7 8 9) \ #(1 2 3) |
|
3231 |
('hello' \ 'l') asString |
|
7168
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3232 |
" |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3233 |
! |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3234 |
|
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3235 |
intersect:aCollection |
8606 | 3236 |
"return a new set containing all elements of the receiver, |
3237 |
which are also contained in the argument collection. |
|
7168
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3238 |
For large collections you better use a Set for self" |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3239 |
|
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3240 |
|newCollection| |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3241 |
|
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3242 |
newCollection := self speciesForAdding new. |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3243 |
aCollection do:[:element | |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3244 |
(self includes:element) ifTrue:[ |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3245 |
newCollection add:element |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3246 |
] |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3247 |
]. |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3248 |
^ newCollection |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3249 |
|
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3250 |
" |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3251 |
#(0 1 2 3 4 5 6 7 8 9) asSet intersect:#(1 2 3 11) |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3252 |
#(0 1 2 3 4 5 6 7 8 9) intersect:#(1 2 3 11) |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3253 |
" |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3254 |
! |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3255 |
|
7169 | 3256 |
union:aCollection |
3257 |
"return a new set containing all elements of the receiver |
|
3258 |
plus those of the aCollection" |
|
3259 |
||
3260 |
|newCollection| |
|
3261 |
||
3262 |
newCollection := self speciesForAdding new. |
|
3263 |
newCollection addAll:self. |
|
3264 |
newCollection addAll:aCollection. |
|
3265 |
^ newCollection |
|
3266 |
||
3267 |
" |
|
3268 |
#(0 2 4 6 8) union:#(1 3 5 7) |
|
11384 | 3269 |
#(0 2 4 6 8) union:#(0 1 3 5 7) |
7169 | 3270 |
" |
3271 |
! |
|
3272 |
||
7168
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3273 |
xor:aCollection |
8606 | 3274 |
"return a new set containing all elements, |
3275 |
which are contained in either the receiver or aCollection, but not in both. |
|
3276 |
||
3277 |
For large collections you better use Sets for both self and aCollection" |
|
7168
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3278 |
|
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3279 |
|newCollection| |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3280 |
|
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3281 |
newCollection := self speciesForAdding new. |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3282 |
aCollection do:[:element | |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3283 |
(self includes:element) ifFalse:[ |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3284 |
newCollection add:element |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3285 |
] |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3286 |
]. |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3287 |
self do:[:element| |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3288 |
(aCollection includes:element) ifFalse:[ |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3289 |
newCollection add:element. |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3290 |
]. |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3291 |
]. |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3292 |
|
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3293 |
^ newCollection |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3294 |
|
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3295 |
" |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3296 |
#(0 1 2 3 4 5 6 7 8 9) xor:#(1 2 3 11) |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3297 |
" |
17735 | 3298 |
|
3299 |
" |
|
3300 |
|c1 c2| |
|
3301 |
||
3302 |
c1 := #( foo bar baz baloo ). |
|
3303 |
c2 := #( foe bar banana baloo ). |
|
3304 |
c1 symmetricDifference:c2. |
|
3305 |
self assert:(c1 symmetricDifference:c2) asSet = (c2 symmetricDifference:c1) asSet |
|
3306 |
" |
|
7168
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3307 |
! ! |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3308 |
|
10811 | 3309 |
!Collection methodsFor:'sorting & reordering'! |
3310 |
||
3311 |
topologicalSort |
|
3312 |
"Sort a partial ordered collection. |
|
3313 |
The receiver consists of tupels defining a partial order. |
|
3314 |
Use the algorithm by R. E. Tarjan from 1972. |
|
3315 |
Answer an OrderedCollection containing the sorted items" |
|
3316 |
||
3317 |
|graph roots sorted count| |
|
3318 |
||
3319 |
"create a graph. |
|
3320 |
For each node there is an entry containing the number of references |
|
3321 |
and a list of arcs to other nodes" |
|
3322 |
||
3323 |
graph := Dictionary new. |
|
3324 |
||
3325 |
self do:[:eachTuple| |
|
3326 |
|node1| |
|
3327 |
node1 := eachTuple first. |
|
3328 |
graph at:node1 ifAbsentPut:[OrderedCollection with:0]. |
|
3329 |
1 to:(eachTuple size-1) do:[:i| |
|
3330 |
|node2 n2| |
|
3331 |
||
3332 |
node1 := eachTuple at:i. |
|
3333 |
node2 := eachTuple at:i+1. |
|
3334 |
"add the node" |
|
3335 |
graph at:node2 ifAbsentPut:[OrderedCollection with:0]. |
|
3336 |
"add an arc from node1 to node2" |
|
3337 |
(graph at:node1) add:node2. |
|
3338 |
"increment count on incoming arcs of node2" |
|
3339 |
n2 := graph at:node2. |
|
3340 |
n2 at:1 put:(n2 first + 1). |
|
3341 |
]. |
|
3342 |
]. |
|
3343 |
||
3344 |
"now find the root nodes (having zero incoming arcs)" |
|
3345 |
roots := OrderedCollection new. |
|
3346 |
graph keysAndValuesDo:[:eachKey :eachValue| |
|
3347 |
eachValue first = 0 ifTrue:[ |
|
3348 |
roots add:eachKey |
|
3349 |
]. |
|
3350 |
]. |
|
3351 |
||
3352 |
"for each root, out it to the sorted list and remove it from the graph. |
|
3353 |
This may cause referenced nodes to be moved to the root" |
|
3354 |
||
3355 |
sorted := OrderedCollection new:graph size. |
|
3356 |
[roots notEmpty] whileTrue:[ |
|
3357 |
|root eachEntry| |
|
3358 |
||
3359 |
root := roots removeFirst. |
|
3360 |
sorted add:root. |
|
3361 |
eachEntry := graph removeKey:root. |
|
3362 |
2 to:eachEntry size do:[:i| |
|
3363 |
|eachChild eachChildGraph| |
|
3364 |
eachChild := eachEntry at:i. |
|
3365 |
eachChildGraph := graph at:eachChild. |
|
3366 |
count := eachChildGraph at:1. |
|
3367 |
eachChildGraph at:1 put:(count - 1). |
|
3368 |
count = 1 ifTrue:[ |
|
3369 |
roots add:eachChild. |
|
3370 |
]. |
|
3371 |
]. |
|
3372 |
]. |
|
3373 |
||
3374 |
graph notEmpty ifTrue:[ |
|
3375 |
self error:'cycle in ordering' mayProceed:true. |
|
3376 |
]. |
|
3377 |
||
3378 |
^ sorted |
|
3379 |
||
3380 |
" |
|
3381 |
#((1 2) (1 3) (3 2)) topologicalSort |
|
11384 | 3382 |
#((1 2) (1 3) (3 2) (4)) topologicalSort |
3383 |
||
3384 |
with cycles: |
|
10811 | 3385 |
#((1 2) (1 3) (3 1)) topologicalSort |
11384 | 3386 |
#((1 2) (2 3) (3 1)) topologicalSort |
3387 |
#((1 2) (2 3) (3 4) (4 1)) topologicalSort |
|
11385
c3d908e81ec7
changed #topologicalSort - comment
Stefan Vogel <sv@exept.de>
parents:
11384
diff
changeset
|
3388 |
#((1 1) (1 2) (1 3) (3 2) (4)) topologicalSort |
10811 | 3389 |
|
3390 |
(Smalltalk allClasses collect:[:eachClass| |
|
3391 |
Array with:eachClass superclass with:eachClass |
|
3392 |
]) topologicalSort |
|
3393 |
" |
|
3394 |
! ! |
|
3395 |
||
602 | 3396 |
!Collection methodsFor:'testing'! |
3397 |
||
3398 |
capacity |
|
3399 |
"return the number of elements, that the receiver is |
|
3400 |
prepared to take. For most collections, this is the actual |
|
3401 |
size. However, some have more space preallocated to allow |
|
3402 |
for faster adding of elements. |
|
3403 |
Not used by the system; added for ST-80 compatibility." |
|
3404 |
||
3405 |
^ self size |
|
3406 |
! |
|
3407 |
||
3408 |
includes:anElement |
|
3409 |
"return true, if an object equal to the argument, anObject is in the list. |
|
3410 |
This compares using #= (i.e. it does not look for the object itself, |
|
3411 |
instead, one that compares equal). |
|
7807 | 3412 |
See #includesIdentical: when identity is asked for. |
3413 |
This is a *very* slow fallback - many subclasses redefine this for performance." |
|
602 | 3414 |
|
3415 |
self do:[:element | |
|
6135 | 3416 |
(anElement = element) ifTrue:[^ true]. |
602 | 3417 |
]. |
3418 |
^ false |
|
3419 |
! |
|
3420 |
||
3421 |
includesAll:aCollection |
|
3422 |
"return true, if the the receiver includes all elements of |
|
3423 |
the argument, aCollection; false if any is missing. |
|
3424 |
Notice: this method has O-square runtime behavior and may be |
|
10092
70a5cb4e5df0
code cleanup: use #contains/#conform instead of explicit loop
Claus Gittinger <cg@exept.de>
parents:
9919
diff
changeset
|
3425 |
slow for big receivers/args. |
70a5cb4e5df0
code cleanup: use #contains/#conform instead of explicit loop
Claus Gittinger <cg@exept.de>
parents:
9919
diff
changeset
|
3426 |
Think about using a Set, or Dictionary." |
70a5cb4e5df0
code cleanup: use #contains/#conform instead of explicit loop
Claus Gittinger <cg@exept.de>
parents:
9919
diff
changeset
|
3427 |
|
70a5cb4e5df0
code cleanup: use #contains/#conform instead of explicit loop
Claus Gittinger <cg@exept.de>
parents:
9919
diff
changeset
|
3428 |
^ aCollection conform:[:element | (self includes:element)] |
602 | 3429 |
|
3430 |
" |
|
3431 |
#(1 2 3 4 5 6 7) includesAll:#(1 2 3) |
|
3432 |
#('hello' 'there' 'world') includesAll:#('hello' 'world') |
|
3433 |
#(1 2 3 4 5 6 7) includesAll:#(7 8 9) |
|
3434 |
" |
|
10092
70a5cb4e5df0
code cleanup: use #contains/#conform instead of explicit loop
Claus Gittinger <cg@exept.de>
parents:
9919
diff
changeset
|
3435 |
|
70a5cb4e5df0
code cleanup: use #contains/#conform instead of explicit loop
Claus Gittinger <cg@exept.de>
parents:
9919
diff
changeset
|
3436 |
"Modified: / 13-10-2006 / 12:54:50 / cg" |
602 | 3437 |
! |
3438 |
||
3439 |
includesAny:aCollection |
|
3440 |
"return true, if the the receiver includes any elements of |
|
3441 |
the argument, aCollection; false if it includes none. |
|
7807 | 3442 |
Notice: this method has O^2(N) runtime behavior and may be |
3443 |
slow for big receivers/args. |
|
3444 |
Think about using a Set or Dictionary. |
|
3445 |
Some speedup is also possible, by arranging highly |
|
6135 | 3446 |
probable elements towards the beginning of aCollection, |
3447 |
to avoid useless searches." |
|
602 | 3448 |
|
3449 |
aCollection do:[:element | |
|
6135 | 3450 |
(self includes:element) ifTrue:[^ true]. |
602 | 3451 |
]. |
3452 |
^ false |
|
3453 |
||
3454 |
" |
|
3455 |
#(1 2 3 4 5 6 7) includesAny:#(1 2 3) |
|
3456 |
#('hello' 'there' 'world') includesAny:#('hello' 'world') |
|
3457 |
#(1 2 3 4 5 6 7) includesAny:#(7 8 9) |
|
3458 |
#(1 2 3 4 5 6 7) includesAny:#(8 9 10) |
|
3459 |
" |
|
3460 |
! |
|
3461 |
||
10504 | 3462 |
includesAnyIdentical:aCollection |
3463 |
"return true, if the the receiver includes any elements of |
|
3464 |
the argument, aCollection; false if it includes none. |
|
3465 |
Use identity compare for comparing. |
|
3466 |
Notice: this method has O^2(N) runtime behavior and may be |
|
3467 |
slow for big receivers/args. |
|
3468 |
Think about using a Set or Dictionary. |
|
3469 |
Some speedup is also possible, by arranging highly |
|
3470 |
probable elements towards the beginning of aCollection, |
|
3471 |
to avoid useless searches." |
|
3472 |
||
3473 |
aCollection do:[:element | |
|
3474 |
(self includesIdentical:element) ifTrue:[^ true]. |
|
3475 |
]. |
|
3476 |
^ false |
|
3477 |
||
3478 |
" |
|
3479 |
#(1 2 3 4 5 6 7) includesAnyIdentical:#(1 2 3) |
|
3480 |
#('hello' 'there' 'world') includesAnyIdentical:#('hello' 'world') |
|
3481 |
#(1 2 3 4 5 6 7) includesAnyIdentical:#(7 8 9) |
|
3482 |
#(1 2 3 4 5 6 7) includesAnyIdentical:#(8 9 10) |
|
3483 |
" |
|
3484 |
! |
|
3485 |
||
602 | 3486 |
includesIdentical:anElement |
7807 | 3487 |
"return true, if the argument, anObject is in the collection. |
602 | 3488 |
This compares using #== (i.e. object identity). |
7807 | 3489 |
See #includes: when equality is asked for. |
3490 |
This is a *very* slow fallback - many subclasses redefine this for performance." |
|
602 | 3491 |
|
3492 |
self do:[:element | |
|
6135 | 3493 |
(anElement == element) ifTrue:[^ true]. |
602 | 3494 |
]. |
3495 |
^ false |
|
3496 |
! |
|
3497 |
||
9005 | 3498 |
isCollection |
3499 |
"return true, if the receiver is some kind of collection; |
|
3500 |
true is returned here - the method is redefined from Object." |
|
3501 |
||
3502 |
^ true |
|
3503 |
! |
|
3504 |
||
602 | 3505 |
isEmpty |
3506 |
"return true, if the receiver is empty" |
|
3507 |
||
3508 |
^ self size == 0 |
|
3509 |
! |
|
3510 |
||
6181 | 3511 |
isEmptyOrNil |
8833 | 3512 |
"return true if I am nil or an empty collection - true here, if the receivers size is 0, |
3513 |
(from Sqeak)" |
|
6181 | 3514 |
|
3515 |
^ self isEmpty |
|
3516 |
||
3517 |
"Created: / 13.11.2001 / 13:17:12 / cg" |
|
6182 | 3518 |
"Modified: / 13.11.2001 / 13:28:35 / cg" |
6181 | 3519 |
! |
3520 |
||
6068
1d5ca1175f3f
added #isNilOrEmptyCollection
Claus Gittinger <cg@exept.de>
parents:
6052
diff
changeset
|
3521 |
isNilOrEmptyCollection |
8833 | 3522 |
"return true if I am nil or an empty collection - false here. |
3523 |
Obsolete, use isEmptyOrNil." |
|
3524 |
||
3525 |
<resource:#obsolete> |
|
6068
1d5ca1175f3f
added #isNilOrEmptyCollection
Claus Gittinger <cg@exept.de>
parents:
6052
diff
changeset
|
3526 |
|
1d5ca1175f3f
added #isNilOrEmptyCollection
Claus Gittinger <cg@exept.de>
parents:
6052
diff
changeset
|
3527 |
^ self isEmpty |
6182 | 3528 |
|
3529 |
"Modified: / 13.11.2001 / 13:27:55 / cg" |
|
6068
1d5ca1175f3f
added #isNilOrEmptyCollection
Claus Gittinger <cg@exept.de>
parents:
6052
diff
changeset
|
3530 |
! |
1d5ca1175f3f
added #isNilOrEmptyCollection
Claus Gittinger <cg@exept.de>
parents:
6052
diff
changeset
|
3531 |
|
9005 | 3532 |
isNonByteCollection |
3533 |
"return true, if the receiver is some kind of collection, but not a String, ByteArray etc.; |
|
3534 |
true is returned here - the method is redefined from Object." |
|
3535 |
||
3536 |
^ true |
|
3537 |
! |
|
3538 |
||
3539 |
isSorted |
|
3540 |
"return true, if the receiver is sorted. |
|
3541 |
Collections which hold their elements in sorted order |
|
3542 |
should return true. Some algorithms (quicksort) degenerate when |
|
3543 |
operating on sorted collections and can be avoided if this information |
|
3544 |
is given. The default returned here (false) should not hurt. |
|
3545 |
I.e. you should NEVER depend on that in your application." |
|
3546 |
||
3547 |
^ false |
|
3548 |
||
3549 |
"Created: 13.4.1996 / 12:35:55 / cg" |
|
3550 |
! |
|
3551 |
||
3552 |
isSortedBy:aBlock |
|
3553 |
"return true, if my elements are sorted (already) by the given criterion (sortBlock). |
|
3554 |
Collections which hold their elements in sorted order |
|
3555 |
should return true. Some algorithms (quicksort) degenerate when |
|
3556 |
operating on sorted collections and can be avoided if this information |
|
3557 |
is given. The default returned here (false) should not hurt. |
|
3558 |
I.e. you should NEVER depend on that in your application." |
|
3559 |
||
3560 |
^ false |
|
3561 |
||
3562 |
! |
|
3563 |
||
3564 |
isSortedCollection |
|
3565 |
"return true, if the receiver is a sortedCollection." |
|
3566 |
||
3567 |
^ false |
|
3568 |
||
3569 |
! |
|
3570 |
||
9247 | 3571 |
isWeakCollection |
3572 |
"return true, if the receiver has weak references to its elements." |
|
3573 |
||
3574 |
^ false |
|
3575 |
! |
|
3576 |
||
602 | 3577 |
notEmpty |
3578 |
"return true, if the receiver is not empty" |
|
3579 |
||
3580 |
^ self isEmpty not |
|
3581 |
! |
|
3582 |
||
6932 | 3583 |
notEmptyOrNil |
3584 |
"Squeak compatibility: |
|
3585 |
return true if I am neither nil nor an empty collection." |
|
3586 |
||
3587 |
^ self notEmpty |
|
3588 |
! |
|
3589 |
||
602 | 3590 |
occurrencesOf:anElement |
3591 |
"return the number of occurrences of the argument, anElement in |
|
5048 | 3592 |
the receiver. Uses #= (i.e. equality) compare." |
602 | 3593 |
|
3594 |
|count "{ Class: SmallInteger }" | |
|
3595 |
||
3596 |
count := 0. |
|
3597 |
self do:[:element | |
|
5048 | 3598 |
(anElement = element) ifTrue:[ |
3599 |
count := count + 1 |
|
3600 |
]. |
|
602 | 3601 |
]. |
3602 |
^ count |
|
8274 | 3603 |
! |
3604 |
||
3605 |
occurrencesOfAny:aCollectionOfElements |
|
3606 |
"return the number of occurrences of any in aCollectionOfElements in the receiver. |
|
3607 |
Uses #= (i.e. equality) compare. |
|
3608 |
Should be redefined in subclass(es) if ever used heavily." |
|
3609 |
||
3610 |
|count "{ Class: SmallInteger }" | |
|
3611 |
||
3612 |
count := 0. |
|
8275 | 3613 |
aCollectionOfElements do:[:element | |
3614 |
count := count + (self occurrencesOf:element) |
|
8274 | 3615 |
]. |
3616 |
^ count |
|
3617 |
||
3618 |
" |
|
3619 |
#(1 4 6 8 4 1) occurrencesOfAny:#(1 4) |
|
3620 |
#(1 4 6 8 4 1) occurrencesOfAny:#(2 5) |
|
3621 |
'hello world' occurrencesOfAny:'hel' |
|
3622 |
" |
|
602 | 3623 |
! ! |
3624 |
||
4680 | 3625 |
!Collection methodsFor:'tracing'! |
3626 |
||
3627 |
traceInto:aRequestor level:level from:referrer |
|
3628 |
"double dispatch into tracer, passing my type implicitely in the selector" |
|
3629 |
||
3630 |
^ aRequestor traceCollection:self level:level from:referrer |
|
3631 |
||
3632 |
||
3633 |
! ! |
|
3634 |
||
8395
6bd97113cb4c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8359
diff
changeset
|
3635 |
!Collection methodsFor:'visiting'! |
6bd97113cb4c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8359
diff
changeset
|
3636 |
|
6bd97113cb4c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8359
diff
changeset
|
3637 |
acceptVisitor:aVisitor with:aParameter |
6bd97113cb4c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8359
diff
changeset
|
3638 |
|
6bd97113cb4c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8359
diff
changeset
|
3639 |
^ aVisitor visitCollection:self with:aParameter |
6bd97113cb4c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8359
diff
changeset
|
3640 |
! ! |
6bd97113cb4c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8359
diff
changeset
|
3641 |
|
2097 | 3642 |
!Collection class methodsFor:'documentation'! |
628 | 3643 |
|
3644 |
version |
|
17767 | 3645 |
^ '$Id: Collection.st 10527 2010-06-16 20:12:05Z vranyj1 $' |
17732
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3646 |
! |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3647 |
|
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3648 |
version_CVS |
17767 | 3649 |
^ 'Header: /cvs/stx/stx/libbasic/Collection.st,v 1.241 2010-01-29 16:41:43 cg Exp ยง' |
17761
b0e5971141bc
Added Lookup and BuiltinLookup classes
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17747
diff
changeset
|
3650 |
! |
b0e5971141bc
Added Lookup and BuiltinLookup classes
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17747
diff
changeset
|
3651 |
|
b0e5971141bc
Added Lookup and BuiltinLookup classes
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17747
diff
changeset
|
3652 |
version_SVN |
17767 | 3653 |
^ '$Id: Collection.st 10527 2010-06-16 20:12:05Z vranyj1 $' |
628 | 3654 |
! ! |
6867 | 3655 |
|
602 | 3656 |
Collection initialize! |
17767 | 3657 |