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