author | Claus Gittinger <cg@exept.de> |
Thu, 25 Apr 1996 18:20:46 +0200 | |
changeset 1290 | 15ba3221b89b |
parent 1289 | 3abde2c376de |
child 1325 | 9446f8ea6941 |
permissions | -rw-r--r-- |
1 | 1 |
" |
5 | 2 |
COPYRIGHT (c) 1989 by Claus Gittinger |
155 | 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 |
" |
|
12 |
||
13 |
Object subclass:#Collection |
|
936
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
14 |
instanceVariableNames:'' |
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
15 |
classVariableNames:'InvalidKeySignal EmptyCollectionSignal ValueNotFoundSignal |
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
16 |
NotEnoughElementsSignal' |
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
17 |
poolDictionaries:'' |
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
18 |
category:'Collections-Abstract' |
1 | 19 |
! |
20 |
||
88 | 21 |
!Collection class methodsFor:'documentation'! |
22 |
||
23 |
copyright |
|
24 |
" |
|
25 |
COPYRIGHT (c) 1989 by Claus Gittinger |
|
155 | 26 |
All Rights Reserved |
1 | 27 |
|
88 | 28 |
This software is furnished under a license and may be used |
29 |
only in accordance with the terms of that license and with the |
|
30 |
inclusion of the above copyright notice. This software may not |
|
31 |
be provided or otherwise made available to, or used by, any |
|
32 |
other person. No title to or ownership of the software is |
|
33 |
hereby transferred. |
|
34 |
" |
|
35 |
! |
|
1 | 36 |
|
88 | 37 |
documentation |
38 |
" |
|
39 |
Abstract superclass for all collections. |
|
40 |
This abstract class provides functionality common to all collections, |
|
41 |
without knowing how the concrete class implements things. Thus, all |
|
42 |
methods found here depend on some basic mechanisms to be defined in the |
|
43 |
concrete class. |
|
44 |
These basic methods are usually defined as #subclassResponsibility here. |
|
45 |
Some methods are also redefined for better performance. |
|
1289 | 46 |
|
47 |
[author:] |
|
48 |
Claus Gittinger |
|
88 | 49 |
" |
50 |
! ! |
|
1 | 51 |
|
10 | 52 |
!Collection class methodsFor:'initialization'! |
53 |
||
54 |
initialize |
|
55 |
"setup the signal" |
|
56 |
||
54 | 57 |
InvalidKeySignal isNil ifTrue:[ |
302 | 58 |
InvalidKeySignal := ErrorSignal newSignalMayProceed:true. |
155 | 59 |
InvalidKeySignal nameClass:self message:#invalidKeySignal. |
345 | 60 |
InvalidKeySignal notifierString:'invalid key:'. |
61 |
||
62 |
ValueNotFoundSignal := ErrorSignal newSignalMayProceed:true. |
|
63 |
ValueNotFoundSignal nameClass:self message:#valueNotFoundSignal. |
|
64 |
ValueNotFoundSignal notifierString:'value not found:'. |
|
155 | 65 |
|
348 | 66 |
NotEnoughElementsSignal := ErrorSignal newSignalMayProceed:true. |
67 |
NotEnoughElementsSignal nameClass:self message:#notEnoughElementsSignal. |
|
68 |
NotEnoughElementsSignal notifierString:'not enough elements in collection'. |
|
69 |
||
70 |
EmptyCollectionSignal := NotEnoughElementsSignal newSignalMayProceed:true. |
|
155 | 71 |
EmptyCollectionSignal nameClass:self message:#emptyCollectionSignal. |
72 |
EmptyCollectionSignal notifierString:'operation not allowed for empty collections'. |
|
54 | 73 |
] |
10 | 74 |
! ! |
75 |
||
602 | 76 |
!Collection class methodsFor:'instance creation'! |
155 | 77 |
|
602 | 78 |
new:size withAll:element |
79 |
"return a new COllection of size, where all elements are |
|
80 |
initialized to element" |
|
348 | 81 |
|
602 | 82 |
|newCollection| |
92 | 83 |
|
602 | 84 |
newCollection := self new:size. |
85 |
size timesRepeat:[newCollection add:element] |
|
86 |
! |
|
1 | 87 |
|
88 |
with:anObject |
|
89 |
"return a new Collection with one element:anObject" |
|
90 |
||
91 |
|newCollection| |
|
92 |
||
93 |
newCollection := self new. |
|
94 |
newCollection add:anObject. |
|
95 |
^ newCollection |
|
96 |
! |
|
97 |
||
98 |
with:firstObject with:secondObject |
|
99 |
"return a new Collection with two elements:firstObject and secondObject" |
|
100 |
||
101 |
|newCollection| |
|
102 |
||
103 |
newCollection := self new. |
|
1112 | 104 |
newCollection add:firstObject; add:secondObject. |
1 | 105 |
^ newCollection |
106 |
! |
|
107 |
||
108 |
with:firstObject with:secondObject with:thirdObject |
|
109 |
"return a new Collection with three elements" |
|
110 |
||
111 |
|newCollection| |
|
112 |
||
113 |
newCollection := self new. |
|
1112 | 114 |
newCollection add:firstObject; add:secondObject; add:thirdObject. |
1 | 115 |
^ newCollection |
116 |
! |
|
117 |
||
118 |
with:firstObject with:secondObject with:thirdObject with:fourthObject |
|
119 |
"return a new Collection with four elements" |
|
120 |
||
121 |
|newCollection| |
|
122 |
||
123 |
newCollection := self new. |
|
1112 | 124 |
newCollection add:firstObject; add:secondObject; add:thirdObject; add:fourthObject. |
1 | 125 |
^ newCollection |
126 |
! |
|
127 |
||
345 | 128 |
with:one with:two with:three with:four with:five |
129 |
"return a new Collection with five elements" |
|
130 |
||
131 |
|newCollection| |
|
132 |
||
133 |
newCollection := self new. |
|
1112 | 134 |
newCollection add:one; add:two; add:three; add:four; add:five. |
345 | 135 |
^ newCollection |
136 |
! |
|
137 |
||
1 | 138 |
withAll:aCollection |
139 |
"return a new Collection with all elements taken from the argument, |
|
140 |
aCollection" |
|
141 |
||
142 |
|newCollection| |
|
143 |
||
144 |
newCollection := self new. |
|
145 |
newCollection addAll:aCollection. |
|
146 |
^newCollection |
|
147 |
! ! |
|
148 |
||
602 | 149 |
!Collection class methodsFor:'Signal constants'! |
10 | 150 |
|
602 | 151 |
emptyCollectionSignal |
152 |
"return the signal used to report non-allowed operation on empty collections" |
|
345 | 153 |
|
602 | 154 |
^ EmptyCollectionSignal |
308 | 155 |
! |
156 |
||
602 | 157 |
invalidKeySignal |
158 |
"return the signal used to report bad key usage" |
|
308 | 159 |
|
602 | 160 |
^ InvalidKeySignal |
201 | 161 |
! |
162 |
||
602 | 163 |
notEnoughElementsSignal |
164 |
"return the signal used to report attempts for an operation, for which |
|
165 |
there are not enough elements in the collection" |
|
249 | 166 |
|
602 | 167 |
^ NotEnoughElementsSignal |
404 | 168 |
! |
169 |
||
602 | 170 |
valueNotFoundSignal |
171 |
"return the signal used to report a nonexisting element." |
|
404 | 172 |
|
602 | 173 |
^ ValueNotFoundSignal |
174 |
! ! |
|
404 | 175 |
|
602 | 176 |
!Collection class methodsFor:'queries'! |
404 | 177 |
|
602 | 178 |
growIsCheap |
179 |
"return true, if this collection can easily grow |
|
180 |
(i.e. without a need for become:). |
|
181 |
Returns true here; this method is redefined in fix-size |
|
182 |
collections" |
|
404 | 183 |
|
602 | 184 |
^ true |
257 | 185 |
! ! |
186 |
||
1 | 187 |
!Collection methodsFor:'accessing'! |
188 |
||
602 | 189 |
anElement |
190 |
"return any element from the collection, or nil if there is none" |
|
191 |
||
192 |
self do: [:each | ^ each]. |
|
193 |
^ nil |
|
194 |
! |
|
195 |
||
68 | 196 |
first |
197 |
"return the first element of the collection. |
|
198 |
This should be redefined in subclasses." |
|
199 |
||
200 |
self do:[:e | ^ e]. |
|
201 |
||
202 |
"error if collection is empty" |
|
155 | 203 |
^ self emptyCollectionError |
68 | 204 |
! |
205 |
||
936
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
206 |
firstIfEmpty:exceptionValue |
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
207 |
"return the first element of the collection. |
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
208 |
If its empty, return the excveptionValue. |
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
209 |
(i.e. dont trigger an error as done in #first)" |
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
210 |
|
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
211 |
self isEmpty ifTrue:[^ exceptionValue value]. |
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
212 |
^ self first |
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
213 |
|
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
214 |
"Modified: 6.2.1996 / 15:27:17 / cg" |
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
215 |
! |
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
216 |
|
68 | 217 |
last |
218 |
"return the last element of the collection. |
|
219 |
This should be redefined in subclasses." |
|
220 |
||
221 |
|theLastOne any| |
|
222 |
||
223 |
any := false. |
|
224 |
self do:[:e | any := true. theLastOne := e]. |
|
225 |
any ifTrue:[ |
|
155 | 226 |
^ theLastOne |
68 | 227 |
]. |
228 |
||
229 |
"error if collection is empty" |
|
155 | 230 |
^ self emptyCollectionError |
68 | 231 |
! |
232 |
||
936
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
233 |
lastIfEmpty:exceptionValue |
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
234 |
"return the last element of the collection. |
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
235 |
If its empty, return the exceptionValue. |
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
236 |
(i.e. dont trigger an error as done in #last)" |
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
237 |
|
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
238 |
self isEmpty ifTrue:[^ exceptionValue value]. |
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
239 |
^ self last |
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
240 |
|
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
241 |
"Modified: 6.2.1996 / 15:27:17 / cg" |
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
242 |
"Created: 6.2.1996 / 15:27:49 / cg" |
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
243 |
! |
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
244 |
|
328 | 245 |
upTo:anElement |
246 |
"return a new collection consisting of the receivers elements upTo |
|
247 |
(but excluding) anElement. |
|
248 |
If anElement is not in the receiver, the returned collection |
|
249 |
will consist of all elements of the receiver" |
|
250 |
||
251 |
|s| |
|
252 |
||
253 |
s := ReadStream on:self. |
|
254 |
^ s upTo:anElement |
|
255 |
||
256 |
" |
|
257 |
'test.text' upTo:$. |
|
258 |
#(1 2 3 4 5) upTo:4 |
|
259 |
" |
|
1 | 260 |
! ! |
261 |
||
262 |
!Collection methodsFor:'adding & removing'! |
|
263 |
||
264 |
add:anObject |
|
88 | 265 |
"add the argument, anObject to the receiver. |
266 |
If the receiver is ordered, the position of the new element is undefined |
|
267 |
(i.e. dont depend on where it will be put)." |
|
1 | 268 |
|
269 |
^ self subclassResponsibility |
|
270 |
! |
|
271 |
||
272 |
addAll:aCollection |
|
1163 | 273 |
"add all elements of the argument, aCollection to the receiver. |
274 |
Returns the argument, aCollection." |
|
1 | 275 |
|
276 |
aCollection do:[:element | |
|
1163 | 277 |
self add:element |
1 | 278 |
]. |
279 |
^ aCollection |
|
88 | 280 |
|
281 |
" |
|
282 |
#(1 2 3 4) copy addAll:#(5 6 7 8) |
|
283 |
#(1 2 3 4) asOrderedCollection addAll:#(5 6 7 8) |
|
284 |
" |
|
1163 | 285 |
|
286 |
"Modified: 12.4.1996 / 13:29:20 / cg" |
|
1 | 287 |
! |
288 |
||
12 | 289 |
addAllFirst:aCollection |
290 |
"insert all elements of the argument, aCollection at the beginning |
|
1163 | 291 |
of the receiver. Returns the argument, aCollection." |
12 | 292 |
|
293 |
aCollection reverseDo:[:element | |
|
1163 | 294 |
self addFirst:element |
12 | 295 |
]. |
296 |
^ aCollection |
|
297 |
||
298 |
" |
|
299 |
|c| |
|
300 |
c := #(4 3 2 1) asOrderedCollection. |
|
1163 | 301 |
c addAllFirst:#(9 8 7 6 5). |
302 |
c |
|
12 | 303 |
" |
1163 | 304 |
|
305 |
"Modified: 12.4.1996 / 13:30:10 / cg" |
|
12 | 306 |
! |
307 |
||
602 | 308 |
addAllLast:aCollection |
1163 | 309 |
"add all elements of the argument, aCollection to the receiver. |
310 |
Returns the argument, aCollection." |
|
602 | 311 |
|
312 |
aCollection do:[:element | |
|
1163 | 313 |
self addLast:element |
602 | 314 |
]. |
315 |
^ aCollection |
|
316 |
||
317 |
" |
|
318 |
|c| |
|
319 |
c := #(4 3 2 1) asOrderedCollection. |
|
320 |
c addAllLast:#(9 8 7 6 5) |
|
321 |
" |
|
1163 | 322 |
|
323 |
"Modified: 12.4.1996 / 13:30:54 / cg" |
|
602 | 324 |
! |
325 |
||
326 |
addFirst:anObject |
|
327 |
"add the argument, anObject to the receiver. |
|
328 |
If the receiver is ordered, the new element will be added at the beginning." |
|
1 | 329 |
|
330 |
^ self subclassResponsibility |
|
331 |
! |
|
332 |
||
602 | 333 |
addLast:anObject |
334 |
"add the argument, anObject to the receiver. |
|
1163 | 335 |
If the receiver is ordered, the new element will be added at the end. |
336 |
Return the argument, anObject." |
|
602 | 337 |
|
338 |
^ self add:anObject |
|
1163 | 339 |
|
340 |
"Modified: 12.4.1996 / 13:31:12 / cg" |
|
602 | 341 |
! |
342 |
||
1 | 343 |
remove:anObject |
348 | 344 |
"remove the (first occurrence of) argument, anObject from the receiver, |
345 |
and return it. |
|
346 |
If it was not in the collection, raise an error." |
|
1 | 347 |
|
1163 | 348 |
^ self remove:anObject ifAbsent:[self errorValueNotFound:anObject] |
349 |
||
350 |
"Modified: 12.4.1996 / 13:33:30 / cg" |
|
1 | 351 |
! |
352 |
||
602 | 353 |
remove:anObject ifAbsent:exceptionBlock |
354 |
"remove the (first occurrence of) argument, anObject from the receiver, |
|
355 |
and return it. |
|
356 |
If it was not in the collection, return the the value of the exceptionBlock" |
|
357 |
||
358 |
^ self subclassResponsibility |
|
359 |
! |
|
360 |
||
134 | 361 |
removeAll |
1163 | 362 |
"remove all elements from the receiver. Returns the receiver. |
134 | 363 |
This should be reimplemented in subclasses for better |
364 |
performance." |
|
365 |
||
366 |
[self notEmpty] whileTrue:[ |
|
1163 | 367 |
self removeFirst |
134 | 368 |
]. |
1163 | 369 |
|
370 |
"Modified: 12.4.1996 / 13:34:39 / cg" |
|
134 | 371 |
! |
372 |
||
1 | 373 |
removeAll:aCollection |
360 | 374 |
"remove all elements of the argument, aCollection from the receiver. |
1163 | 375 |
Return the argument, aCollection. |
376 |
||
360 | 377 |
Notice: for some collections (those not tuned for |
1163 | 378 |
resizing themself) this may be very slow. |
379 |
If the number of removed elements is big compared to to |
|
380 |
the receivers size, it may be better to copy the |
|
381 |
ones which are not to be removed into a new collection." |
|
1 | 382 |
|
383 |
aCollection do:[:element | self remove:element]. |
|
384 |
^ aCollection |
|
1163 | 385 |
|
386 |
"Modified: 12.4.1996 / 13:35:48 / cg" |
|
348 | 387 |
! |
388 |
||
389 |
removeFirst |
|
390 |
"remove the first element from the receiver. |
|
391 |
Return the removed element." |
|
392 |
||
393 |
^ self subclassResponsibility |
|
394 |
! |
|
395 |
||
396 |
removeFirst:n |
|
397 |
"remove the first n elements from the receiver. |
|
360 | 398 |
Return an array filled with the removed elements. |
399 |
Notice: for some collections (those not tuned for |
|
400 |
resizing themself) this may be very slow." |
|
348 | 401 |
|
402 |
|ret| |
|
403 |
||
404 |
self size < n ifTrue:[ |
|
405 |
^ self notEnoughElementsError |
|
406 |
]. |
|
407 |
ret := Array new:n. |
|
408 |
1 to:n do:[:i | |
|
409 |
ret at:i put:(self removeFirst). |
|
410 |
]. |
|
411 |
^ ret |
|
412 |
! |
|
413 |
||
602 | 414 |
removeLast |
415 |
"remove the last element from the receiver. |
|
416 |
Return the removed element." |
|
417 |
||
418 |
^ self subclassResponsibility |
|
419 |
! |
|
420 |
||
348 | 421 |
removeLast:n |
422 |
"remove the last n elements from the receiver. |
|
360 | 423 |
Return an array filled with the removed elements. |
424 |
Notice: for some collections (those not tuned for |
|
425 |
resizing themself) this may be very slow." |
|
348 | 426 |
|
427 |
|ret| |
|
428 |
||
429 |
self size < n ifTrue:[ |
|
430 |
^ self notEnoughElementsError |
|
431 |
]. |
|
432 |
ret := Array new:n. |
|
433 |
n to:1 by:-1 do:[:i | |
|
434 |
ret at:i put:(self removeLast). |
|
435 |
]. |
|
436 |
^ ret |
|
1 | 437 |
! ! |
438 |
||
602 | 439 |
!Collection methodsFor:'converting'! |
440 |
||
441 |
asArray |
|
442 |
"return a new Array with the collections elements" |
|
443 |
||
444 |
|anArray |
|
445 |
index "{ Class: SmallInteger }" | |
|
1 | 446 |
|
602 | 447 |
anArray := Array new:(self size). |
448 |
index := 1. |
|
449 |
self do:[:each | |
|
450 |
anArray at:index put:each. |
|
451 |
index := index + 1 |
|
452 |
]. |
|
453 |
^ anArray |
|
1 | 454 |
! |
455 |
||
602 | 456 |
asBag |
457 |
"return a new Bag with the receiver collections elements" |
|
1 | 458 |
|
602 | 459 |
^ self addAllTo:(Bag new) |
1 | 460 |
! |
461 |
||
602 | 462 |
asByteArray |
463 |
"return a new ByteArray with the collections elements |
|
464 |
(which must convert to integers in the range 0..255)." |
|
1 | 465 |
|
602 | 466 |
|aByteArray |
467 |
index "{ Class: SmallInteger }" | |
|
1 | 468 |
|
602 | 469 |
aByteArray := ByteArray new:(self size). |
470 |
index := 1. |
|
471 |
self do:[:each | |
|
472 |
aByteArray at:index put:each asInteger. |
|
473 |
index := index + 1 |
|
474 |
]. |
|
475 |
^ aByteArray |
|
1 | 476 |
! |
477 |
||
602 | 478 |
asDoubleArray |
479 |
"return a new DoubleArray with the collections elements |
|
480 |
(which must convert to floats)." |
|
481 |
||
482 |
|aDoubleArray |
|
483 |
index "{ Class: SmallInteger }" | |
|
484 |
||
485 |
aDoubleArray := DoubleArray new:(self size). |
|
486 |
index := 1. |
|
487 |
self do:[:each | |
|
488 |
aDoubleArray at:index put:each asFloat. |
|
489 |
index := index + 1 |
|
490 |
]. |
|
491 |
^ aDoubleArray |
|
492 |
! |
|
68 | 493 |
|
602 | 494 |
asFloatArray |
495 |
"return a new FloatArray with the collections elements |
|
496 |
(which must convert to floats)." |
|
497 |
||
498 |
|aFloatArray |
|
499 |
index "{ Class: SmallInteger }" | |
|
500 |
||
501 |
aFloatArray := FloatArray new:(self size). |
|
502 |
index := 1. |
|
503 |
self do:[:each | |
|
504 |
aFloatArray at:index put:each asFloat. |
|
505 |
index := index + 1 |
|
506 |
]. |
|
507 |
^ aFloatArray |
|
508 |
! |
|
509 |
||
510 |
asIdentitySet |
|
511 |
"return a new IdentitySet with the receiver collections elements" |
|
512 |
||
513 |
^ self addAllTo:(IdentitySet new:self size) |
|
68 | 514 |
! |
515 |
||
602 | 516 |
asOrderedCollection |
517 |
"return a new OrderedCollection with the receiver collections elements" |
|
518 |
||
519 |
^ self addAllTo:(OrderedCollection new:self size) |
|
520 |
! |
|
521 |
||
522 |
asSet |
|
523 |
"return a new Set with the receiver collections elements" |
|
524 |
||
525 |
^ self addAllTo:(Set new:self size) |
|
526 |
! |
|
527 |
||
528 |
asSortedCollection |
|
529 |
"return a new SortedCollection with the receiver collections elements" |
|
1 | 530 |
|
602 | 531 |
|aSortedCollection| |
532 |
||
533 |
aSortedCollection := SortedCollection new:self size. |
|
534 |
aSortedCollection addAll:self. |
|
535 |
^ aSortedCollection |
|
536 |
! |
|
537 |
||
538 |
asSortedCollection:sortBlock |
|
539 |
"return a new SortedCollection with the receiver collections elements, |
|
540 |
using sortBlock for comparing" |
|
541 |
||
542 |
|aSortedCollection| |
|
543 |
||
544 |
aSortedCollection := SortedCollection sortBlock:sortBlock. |
|
545 |
aSortedCollection addAll:self. |
|
546 |
^ aSortedCollection |
|
1 | 547 |
! |
548 |
||
602 | 549 |
asString |
550 |
"return a String with the collections elements |
|
551 |
(which must convert to characters)" |
|
552 |
||
553 |
|aString |
|
554 |
index "{ Class: SmallInteger }" | |
|
360 | 555 |
|
602 | 556 |
aString := String new:(self size). |
557 |
index := 1. |
|
558 |
self do:[:each | |
|
559 |
aString at:index put:each asCharacter. |
|
560 |
index := index + 1 |
|
360 | 561 |
]. |
602 | 562 |
^ aString |
563 |
! |
|
564 |
||
565 |
asStringCollection |
|
566 |
"return a new Text-object (collection of lines) with the elements printstings" |
|
567 |
||
568 |
^ self addAllTo:(StringCollection new) |
|
360 | 569 |
! |
570 |
||
602 | 571 |
asText |
572 |
"return a Text-object (collection of lines) from myself. |
|
573 |
BIG warning: asText is totally misnamed here |
|
574 |
- ST/X's asText has nothing to do with PP's asText. |
|
575 |
Therefore it will be removed/renamed soon. |
|
576 |
Please use #asStringCollection." |
|
1 | 577 |
|
602 | 578 |
self obsoleteMethodWarning:'use #asStringCollection; #asText will change its meaning'. |
579 |
||
580 |
^ self asStringCollection |
|
581 |
! |
|
582 |
||
583 |
readStream |
|
584 |
"return a stream for reading from the receiver" |
|
585 |
||
586 |
^ ReadStream on:self |
|
88 | 587 |
|
588 |
" |
|
602 | 589 |
|s| |
590 |
||
591 |
s := 'hello world' readStream. |
|
592 |
s next:5. |
|
593 |
s next. |
|
594 |
(s next:5) inspect |
|
88 | 595 |
" |
1 | 596 |
! |
597 |
||
602 | 598 |
writeStream |
599 |
"return a stream for writing onto the receiver" |
|
600 |
||
601 |
^ WriteStream on:self |
|
2 | 602 |
|
602 | 603 |
" |
604 |
|s| |
|
605 |
||
606 |
s := #() copy writeStream. |
|
607 |
s nextPut:1. |
|
608 |
s nextPut:2. |
|
609 |
s nextPut:3. |
|
610 |
s contents inspect |
|
611 |
" |
|
88 | 612 |
|
613 |
" |
|
602 | 614 |
|s| |
2 | 615 |
|
602 | 616 |
s := OrderedCollection new writeStream. |
617 |
s nextPut:1. |
|
618 |
s nextPut:2. |
|
619 |
s nextPut:3. |
|
620 |
s contents inspect |
|
621 |
" |
|
622 |
! ! |
|
1 | 623 |
|
602 | 624 |
!Collection methodsFor:'copying'! |
625 |
||
626 |
copyEmpty |
|
627 |
"return a copy of the receiver with no elements. |
|
628 |
This is used by copying and enumeration methods |
|
629 |
to get a new instance which is similar to the receiver." |
|
630 |
||
631 |
^ self species new |
|
1 | 632 |
! |
633 |
||
602 | 634 |
copyEmpty:size |
635 |
"return a copy of the receiver with no elements, but space for |
|
636 |
size elements. This is used by copying and enumeration methods |
|
637 |
to get a new instance which is similar to the receiver. |
|
638 |
This method should be redefined in subclasses with instance |
|
639 |
variables, which should be put into the copy too. |
|
640 |
For example, SortedCollection has to copy its sortBlock into the |
|
641 |
new collection." |
|
1 | 642 |
|
602 | 643 |
^ self species new:size |
360 | 644 |
! |
645 |
||
602 | 646 |
copyEmptyAndGrow:size |
647 |
"return a copy of the receiver with size nil elements. |
|
648 |
This is used by copying and enumeration methods |
|
649 |
to get a new instance which is similar to the receiver." |
|
155 | 650 |
|
602 | 651 |
^ (self copyEmpty:size) grow:size |
1 | 652 |
! ! |
653 |
||
654 |
!Collection methodsFor:'enumerating'! |
|
655 |
||
602 | 656 |
addAllTo:aCollection |
657 |
"add all elements of the receiver, to aCollection. |
|
658 |
Return aCollection." |
|
77 | 659 |
|
602 | 660 |
self do:[:each | aCollection add:each]. |
661 |
^ aCollection |
|
77 | 662 |
! |
663 |
||
1 | 664 |
collect:aBlock |
665 |
"for each element in the receiver, evaluate the argument, aBlock |
|
666 |
and return a new collection with the results" |
|
667 |
||
668 |
|newCollection| |
|
669 |
||
201 | 670 |
newCollection := self species new. |
671 |
self do:[:element | newCollection add:(aBlock value:element)]. |
|
1 | 672 |
^ newCollection |
68 | 673 |
|
674 |
" |
|
675 |
#(1 2 3 4) collect:[:n | n * 2] |
|
676 |
" |
|
1 | 677 |
! |
678 |
||
602 | 679 |
conform:aBlock |
680 |
"return true, if every element conforms to some condition. |
|
681 |
I.e. return false, if aBlock returns false for any element; |
|
682 |
true otherwise." |
|
77 | 683 |
|
602 | 684 |
self do:[:element | (aBlock value:element) ifFalse:[^ false]]. |
685 |
^ true |
|
686 |
! |
|
77 | 687 |
|
602 | 688 |
count:aBlock |
689 |
"count elements, for which aBlock returns true. |
|
690 |
Return the sum." |
|
691 |
||
692 |
^ self inject:0 into:[:sumSoFar :element | |
|
693 |
(aBlock value:element) ifTrue:[sumSoFar + 1] |
|
694 |
ifFalse:[sumSoFar] |
|
695 |
] |
|
77 | 696 |
|
697 |
" |
|
602 | 698 |
#(1 2 3 4 6 8 10) count:[:a | a even] |
699 |
#(1 nil nil nil 2 3 nil 4 5) count:[:a | a isNil] |
|
77 | 700 |
" |
701 |
! |
|
702 |
||
1 | 703 |
detect:aBlock |
704 |
"evaluate the argument, aBlock for each element in the receiver until |
|
705 |
the block returns true; in this case return the element which caused |
|
706 |
the true evaluation. |
|
345 | 707 |
If none of the evaluations returns true, report an error" |
1 | 708 |
|
709 |
^ self detect:aBlock ifNone:[self errorNotFound] |
|
68 | 710 |
|
711 |
" |
|
712 |
#(1 2 3 4) detect:[:n | n odd] |
|
713 |
#(2 4 6 8) detect:[:n | n odd] |
|
714 |
" |
|
1 | 715 |
! |
716 |
||
717 |
detect:aBlock ifNone:exceptionBlock |
|
718 |
"evaluate the argument, aBlock for each element in the receiver until |
|
719 |
the block returns true; in this case return the element which caused |
|
720 |
the true evaluation. |
|
721 |
If none of the evaluations returns true, return the result of the |
|
722 |
evaluation of the exceptionBlock" |
|
723 |
||
724 |
self do:[:each | |
|
155 | 725 |
(aBlock value:each) ifTrue:[^ each]. |
1 | 726 |
]. |
727 |
^ exceptionBlock value |
|
68 | 728 |
|
729 |
" |
|
730 |
#(1 2 3 4) detect:[:n | n odd] ifNone:['sorry'] |
|
731 |
#(2 4 6 8) detect:[:n | n odd] ifNone:['sorry'] |
|
732 |
" |
|
1 | 733 |
! |
734 |
||
421 | 735 |
detectLast:aBlock |
736 |
"evaluate the argument, aBlock for each element in the receiver until |
|
737 |
the block returns true; in this case return the element which caused |
|
738 |
the true evaluation. The elements are processed in reverse order. |
|
739 |
If none of the evaluations returns true, report an error" |
|
740 |
||
741 |
^ self detectLast:aBlock ifNone:[self errorNotFound] |
|
742 |
||
743 |
" |
|
744 |
#(1 2 3 4) detectLast:[:n | n odd] |
|
745 |
#(2 4 6 8) detectLast:[:n | n odd] |
|
746 |
" |
|
747 |
! |
|
748 |
||
749 |
detectLast:aBlock ifNone:exceptionBlock |
|
750 |
"evaluate the argument, aBlock for each element in the receiver until |
|
751 |
the block returns true; in this case return the element which caused |
|
752 |
the true evaluation. The elements are processed in reverse order. |
|
753 |
If none of the evaluations returns true, return the result of the |
|
754 |
evaluation of the exceptionBlock" |
|
755 |
||
756 |
self reverseDo:[:each | |
|
757 |
(aBlock value:each) ifTrue:[^ each]. |
|
758 |
]. |
|
759 |
^ exceptionBlock value |
|
760 |
||
761 |
" |
|
762 |
#(1 2 3 4) detectLast:[:n | n odd] ifNone:['sorry'] |
|
763 |
#(2 4 6 8) detectLast:[:n | n odd] ifNone:['sorry'] |
|
764 |
" |
|
765 |
! |
|
766 |
||
602 | 767 |
do:aBlock |
768 |
"evaluate the argument, aBlock for each element" |
|
769 |
||
770 |
^ self subclassResponsibility |
|
771 |
! |
|
772 |
||
1212 | 773 |
doWithExit:aBlock |
774 |
"evaluate the argument, aBlock for each element. |
|
775 |
Passes an additional exit object, which can be used to leave |
|
776 |
the loop early, by sending it a #value: message. |
|
777 |
Returns nil or the value passed to the exit>>value: message. |
|
778 |
||
779 |
Notice, that this is different to a return from the block, which |
|
780 |
returns from the enclosed method, NOT only from the block." |
|
781 |
||
782 |
^ [:exit | |
|
783 |
self do:[:el | |
|
784 |
aBlock value:el value:exit |
|
785 |
]. |
|
786 |
nil |
|
787 |
] valueWithExit. |
|
788 |
||
789 |
"Example: |
|
790 |
search for the first element which is >= 99; return it. |
|
791 |
remaining elements are not processed: |
|
792 |
||
793 |
#(1 2 3 4 999 5 6 7 8 9) |
|
794 |
doWithExit:[:element :exit | |
|
795 |
Transcript showCr:element. |
|
796 |
element >= 99 ifTrue:[exit value:element]] |
|
797 |
||
798 |
#(1 2 3 4 5 6 7 8 9) |
|
799 |
doWithExit:[:element :exit | |
|
800 |
Transcript showCr:element. |
|
801 |
element >= 99 ifTrue:[exit value:element]] |
|
802 |
" |
|
803 |
||
804 |
"to demonstrate the difference to returning from the block: |
|
805 |
this works: |
|
806 |
||
807 |
|el| |
|
808 |
||
809 |
el := #(1 2 3 4 999 5 6 7 8 9) |
|
810 |
doWithExit:[:element :exit | |
|
811 |
element >= 99 ifTrue:[exit value:element]]. |
|
812 |
Transcript showCr:el. |
|
813 |
||
814 |
||
815 |
this does NOT work as expected by a newComer ;-) (the showCr is not reached): |
|
816 |
||
817 |
|el| |
|
818 |
||
819 |
el := #(1 2 3 4 999 5 6 7 8 9) |
|
820 |
do:[:element | |
|
821 |
element >= 99 ifTrue:[^ element]]. |
|
822 |
Transcript showCr:el. |
|
823 |
" |
|
824 |
||
825 |
"Modified: 18.4.1996 / 14:16:59 / cg" |
|
826 |
! |
|
827 |
||
1 | 828 |
inject:thisValue into:binaryBlock |
1262 | 829 |
"starting with thisValue for value, pass this value and each element |
830 |
to binaryBlock, replacing value with the result returned from the block |
|
831 |
in the next iteration." |
|
832 |
||
1 | 833 |
|nextValue| |
834 |
||
835 |
nextValue := thisValue. |
|
836 |
self do: [:each | nextValue := binaryBlock value:nextValue value:each]. |
|
837 |
^ nextValue |
|
68 | 838 |
|
1262 | 839 |
"sum up the elements of a collection: |
840 |
||
68 | 841 |
#(1 2 3 4) inject:0 into:[:accu :element | accu + element] |
842 |
(1 to:10) inject:0 into:[:accu :element | accu + element] |
|
1262 | 843 |
|
844 |
find the minimum: |
|
845 |
||
846 |
|coll| |
|
847 |
coll := #(1 99 -15 20 100). |
|
848 |
coll inject:(coll first) into:[:minSoFar :element | minSoFar min:element] |
|
68 | 849 |
" |
1262 | 850 |
|
851 |
"Modified: 23.4.1996 / 13:47:06 / cg" |
|
1 | 852 |
! |
853 |
||
602 | 854 |
keysAndValuesDo:aTwoArgBlock |
855 |
"evaluate the argument, aBlock for every element in the collection, |
|
856 |
passing both index and element as arguments." |
|
857 |
||
858 |
^ self errorNotKeyed |
|
859 |
! |
|
860 |
||
861 |
pairsDo:aBlock |
|
862 |
"evaluate the argument, aBlock for every element in the collection, |
|
863 |
which is supposed to consist of 2-element collections. |
|
864 |
The block is called with 2 arguments for each collection in the receiver." |
|
865 |
||
866 |
self do:[:aPair | |
|
867 |
aBlock value:(aPair at:1) value:(aPair at:2). |
|
868 |
] |
|
869 |
" |
|
870 |
#( (1 one) (2 two) (3 three) (4 four) (5 five) (6 six)) |
|
871 |
pairsDo:[:num :sym | Transcript show:num; show:' is: '; showCr:sym] |
|
872 |
||
873 |
||
874 |
#( (1 1) (1 2) (1 3) (1 4) (1 5)) |
|
875 |
pairsDo:[:x :y | Transcript showCr:x@y] |
|
876 |
" |
|
877 |
! |
|
878 |
||
1 | 879 |
reject:aBlock |
880 |
"return a new collection with all elements from the receiver, for which |
|
881 |
the argument aBlock evaluates to false" |
|
882 |
||
883 |
^ self select:[:element | (aBlock value:element) == false] |
|
68 | 884 |
|
885 |
" |
|
886 |
#(1 2 3 4) reject:[:e | e odd] |
|
887 |
(1 to:10) reject:[:e | e even] |
|
888 |
" |
|
1 | 889 |
! |
890 |
||
602 | 891 |
reverseDo:aBlock |
892 |
"evaluate the argument, aBlock for each element in reverse order." |
|
893 |
||
894 |
"it could be defined in terms of do: - but very inefficient. |
|
895 |
Better force programmer to define a better version ..." |
|
896 |
||
897 |
^ self subclassResponsibility |
|
898 |
! |
|
899 |
||
1 | 900 |
select:aBlock |
901 |
"return a new collection with all elements from the receiver, for which |
|
902 |
the argument aBlock evaluates to true" |
|
903 |
||
904 |
|newCollection| |
|
905 |
||
201 | 906 |
newCollection := self species new. |
1 | 907 |
self do:[:each | |
155 | 908 |
(aBlock value:each) ifTrue:[newCollection add:each]. |
1 | 909 |
]. |
910 |
^ newCollection |
|
68 | 911 |
|
912 |
" |
|
913 |
#(1 2 3 4) select:[:e | e odd] |
|
914 |
(1 to:10) select:[:e | e even] |
|
915 |
" |
|
916 |
! |
|
917 |
||
918 |
select:aBlock ifNone:exceptionBlock |
|
919 |
"try a new collection with all elements from the receiver, for which |
|
920 |
the argument aBlock evaluates to true. If none of the elements passes |
|
921 |
the check of aBlock, return the result of evaluating exceptionBlock." |
|
922 |
||
923 |
|newCollection| |
|
924 |
||
925 |
newCollection := self select:aBlock. |
|
926 |
newCollection isEmpty ifTrue:[^ exceptionBlock value]. |
|
927 |
^ newCollection |
|
928 |
||
929 |
" |
|
930 |
#(1 2 3 4) select:[:e | e > 10] ifNone:['sorry'] |
|
931 |
" |
|
12 | 932 |
! |
933 |
||
362 | 934 |
triplesDo:aBlock |
935 |
"evaluate the argument, aBlock for every element in the collection, |
|
936 |
which is supposed to consist of 3-element collections. |
|
937 |
The block is called with 3 arguments for each collection in the receiver." |
|
938 |
||
939 |
self do:[:aTriple | |
|
940 |
aBlock value:(aTriple at:1) value:(aTriple at:2) value:(aTriple at:3). |
|
941 |
] |
|
942 |
" |
|
943 |
#( (1 one eins) (2 two zwei) (3 three drei) (4 four vier) (5 five #'fünf') (6 six sechs)) |
|
944 |
triplesDo:[:num :sym1 :sym2 | Transcript show:num; space; show:sym1; space; showCr:sym2] |
|
945 |
" |
|
1 | 946 |
! |
947 |
||
602 | 948 |
with:aCollection collect:aTwoArgBlock |
949 |
"evaluate the argument, aBlock for successive elements from |
|
950 |
each the receiver and the argument, aSequenceableCollection; |
|
951 |
The second argument, aBlock must be a two-argument block, which is |
|
952 |
evaluated for each element-pair. |
|
953 |
Collect the results and return a collection containing them. |
|
954 |
This method fails if neither the receiver nor aCollection is |
|
955 |
a sequenceable collection (i.e. implements numeric key access)." |
|
260 | 956 |
|
602 | 957 |
|index "{ Class: SmallInteger }" |
958 |
newCollection| |
|
1 | 959 |
|
602 | 960 |
newCollection := self species new. |
961 |
index := 1. |
|
962 |
aCollection isSequenceable ifFalse:[ |
|
963 |
self isSequenceable ifFalse:[ |
|
964 |
^ self error:'neither collection is sequenceable'. |
|
965 |
]. |
|
966 |
aCollection do:[:element | |
|
967 |
newCollection add:(aTwoArgBlock value:(self at:index) |
|
968 |
value:element). |
|
969 |
index := index + 1 |
|
970 |
] |
|
971 |
] ifTrue:[ |
|
972 |
self do:[:element | |
|
973 |
newCollection add:(aTwoArgBlock value:element |
|
974 |
value:(aCollection at:index)). |
|
975 |
index := index + 1 |
|
976 |
] |
|
977 |
]. |
|
978 |
^ newCollection |
|
201 | 979 |
|
980 |
" |
|
602 | 981 |
(1 to:3) with:#(one two three) collect:[:num :sym | (num->sym)] |
982 |
#(1 2 3) with:#(10 20 30) collect:[:x :y | (x@y)] |
|
201 | 983 |
" |
2 | 984 |
! |
985 |
||
602 | 986 |
with:aCollection do:aTwoArgBlock |
987 |
"evaluate the argument, aBlock for successive elements from |
|
988 |
each the receiver and the argument, aSequenceableCollection. |
|
989 |
The second argument, aBlock must be a two-argument block. |
|
990 |
This method fails if neither the receiver nor aCollection is |
|
991 |
a sequenceable collection (i.e. implements numeric key access)." |
|
992 |
||
993 |
|index "{ Class: SmallInteger }"| |
|
2 | 994 |
|
602 | 995 |
index := 1. |
996 |
aCollection isSequenceable ifFalse:[ |
|
997 |
self isSequenceable ifFalse:[ |
|
998 |
^ self error:'neither collection is sequenceable'. |
|
999 |
]. |
|
1000 |
aCollection do:[:element | |
|
1001 |
aTwoArgBlock value:(self at:index) value:element. |
|
1002 |
index := index + 1 |
|
1003 |
] |
|
1004 |
] ifTrue:[ |
|
1005 |
self do:[:element | |
|
1006 |
aTwoArgBlock value:element value:(aCollection at:index). |
|
1007 |
index := index + 1 |
|
1008 |
] |
|
1009 |
] |
|
201 | 1010 |
|
1011 |
" |
|
602 | 1012 |
(1 to:3) with:#(one two three) do:[:num :sym | |
1013 |
Transcript showCr:(num->sym) |
|
1014 |
] |
|
1015 |
(1 to:3) with:#(one two three) asSet do:[:num :sym | |
|
1016 |
Transcript showCr:(num->sym) |
|
1017 |
]. |
|
1018 |
(1 to:3) asSet with:#(one two three) do:[:num :sym | |
|
1019 |
Transcript showCr:(num->sym) |
|
1020 |
] |
|
201 | 1021 |
" |
1 | 1022 |
! ! |
1023 |
||
602 | 1024 |
!Collection methodsFor:'error handling'! |
1025 |
||
1026 |
emptyCheck |
|
1027 |
"check if the receiver is empty; report an error if so" |
|
1028 |
||
1029 |
self size == 0 ifTrue:[ |
|
1030 |
^ self emptyCollectionError. |
|
1031 |
]. |
|
1032 |
! |
|
1033 |
||
1034 |
emptyCollectionError |
|
1035 |
"report an error that the operation is not allowed for |
|
1036 |
empty collections" |
|
1037 |
||
1038 |
^ EmptyCollectionSignal raise |
|
1039 |
! |
|
1040 |
||
1041 |
errorInvalidKey:aKey |
|
1042 |
"report an error that the given key was invalid" |
|
1043 |
||
1044 |
^ InvalidKeySignal raiseRequestWith:aKey |
|
1045 |
! |
|
1046 |
||
1047 |
errorNotKeyed |
|
1048 |
"report an error that keyed access methods are not allowed" |
|
1049 |
||
1050 |
^ self error:(self class name, 's do not respond to keyed accessing messages') |
|
1051 |
! |
|
1052 |
||
1053 |
errorValueNotFound:anObject |
|
1054 |
"report an error that an object was not found in the collection" |
|
1055 |
||
1056 |
^ ValueNotFoundSignal raiseRequestWith:anObject |
|
1057 |
! |
|
1058 |
||
1059 |
notEnoughElementsError |
|
1060 |
"report an error that the operation is not allowed, |
|
1061 |
since not enough elements are in the collection" |
|
1062 |
||
1063 |
^ NotEnoughElementsSignal raise |
|
1064 |
! ! |
|
1065 |
||
1066 |
!Collection methodsFor:'growing'! |
|
1067 |
||
1068 |
grow |
|
1069 |
"make the receiver larger" |
|
1070 |
||
1071 |
self grow:(self size + self growSize) |
|
1072 |
! |
|
1073 |
||
1074 |
grow:howBig |
|
1075 |
"change the receivers size" |
|
1076 |
||
1077 |
^ self subclassResponsibility |
|
1078 |
! |
|
1079 |
||
1080 |
growSize |
|
1081 |
"return a suitable size increment for growing. |
|
1082 |
The default returned here may be (and is) redefined in subclasses." |
|
1083 |
||
1084 |
^ self size max:2 |
|
1085 |
! ! |
|
1086 |
||
1 | 1087 |
!Collection methodsFor:'printing & storing'! |
1088 |
||
602 | 1089 |
displayString |
1090 |
"return a printed representation of the receiver for display in inspectors etc." |
|
1091 |
||
1092 |
^ self printOrDisplayStringUsing:#displayString |
|
1093 |
! |
|
1094 |
||
1 | 1095 |
maxPrint |
169 | 1096 |
"the print-limit; printOn: will try to not produce more output |
88 | 1097 |
than the limit defined here." |
1098 |
||
1 | 1099 |
^ 5000 |
1100 |
! |
|
1101 |
||
1102 |
printOn:aStream |
|
88 | 1103 |
"append a user readable representation of the receiver to aStream. |
1104 |
The text appended is not meant to be read back for reconstruction of |
|
169 | 1105 |
the receiver. Also, this method limits the size of generated string. |
88 | 1106 |
" |
1107 |
||
169 | 1108 |
|limit firstOne string| |
1 | 1109 |
|
44 | 1110 |
thisContext isRecursive ifTrue:[ |
155 | 1111 |
Transcript showCr:'Error: printOn: of self referencing collection.'. |
1112 |
aStream nextPutAll:'#("recursive")'. |
|
1113 |
^ self |
|
44 | 1114 |
]. |
1115 |
||
1116 |
limit := self maxPrint. |
|
1 | 1117 |
aStream nextPutAll:self class name. |
1118 |
aStream nextPut:$(. |
|
1119 |
firstOne := true. |
|
169 | 1120 |
|
1 | 1121 |
self do:[:element | |
169 | 1122 |
firstOne ifFalse:[ |
1123 |
aStream space |
|
1124 |
] ifTrue:[ |
|
1125 |
firstOne := false |
|
1126 |
]. |
|
1127 |
(limit <= 0) ifTrue:[ |
|
1128 |
aStream nextPutAll:'...etc...)'. |
|
1129 |
^ self |
|
1130 |
] ifFalse:[ |
|
92 | 1131 |
"/ old code, worked only on positionable streams |
1132 |
"/ |
|
1133 |
"/ pos1 := aStream position. |
|
1134 |
"/ element printOn:aStream. |
|
1135 |
"/ limit := limit - (aStream position - pos1) |
|
169 | 1136 |
|
1137 |
string := element printString. |
|
1138 |
aStream nextPutAll:string. |
|
1139 |
limit := limit - string size. |
|
155 | 1140 |
]. |
1 | 1141 |
]. |
1142 |
aStream nextPut:$) |
|
44 | 1143 |
|
88 | 1144 |
" |
1145 |
#(1 2 3 'hello' $a) printOn:Transcript |
|
169 | 1146 |
(Array new:100000) printOn:Transcript |
1147 |
(Array new:100000) printString size |
|
88 | 1148 |
(Dictionary new at:#hello put:'world'; yourself) printOn:Transcript |
169 | 1149 |
" |
1150 |
" |
|
88 | 1151 |
|a| |
1152 |
a := Array new:3. |
|
1153 |
a at:2 put:a. |
|
1154 |
a printOn:Transcript |
|
1155 |
" |
|
1 | 1156 |
! |
1157 |
||
602 | 1158 |
printOrDisplayStringUsing:aSelector |
1159 |
"common code for printString and displayString; they only differ in |
|
1160 |
the print-message sent to the elements" |
|
1161 |
||
1162 |
|thisString buffer count string noneYet total limit| |
|
1163 |
||
1164 |
thisContext isRecursive ifTrue:[ |
|
1165 |
Transcript showCr:'Error: print/storeString of self referencing collection.'. |
|
1166 |
^ '#("recursive")' |
|
1167 |
]. |
|
1168 |
||
1169 |
string := (self class name) , '('. |
|
1170 |
noneYet := true. |
|
1171 |
buffer := ''. |
|
1172 |
count := 0. |
|
1173 |
total := 0. |
|
1174 |
limit := self maxPrint. |
|
1175 |
||
1176 |
self do: [:element | |
|
1177 |
thisString := element perform:aSelector. |
|
1178 |
noneYet ifTrue:[ |
|
1179 |
noneYet := false. |
|
1180 |
buffer := buffer , thisString |
|
1181 |
] ifFalse:[ |
|
1182 |
buffer := buffer , (' ' , thisString) |
|
1183 |
]. |
|
1184 |
count := count + 1. |
|
1185 |
(count == 20) ifTrue:[ |
|
1186 |
string := string , buffer. |
|
1187 |
buffer := ''. |
|
1188 |
count := 0 |
|
1189 |
]. |
|
1190 |
total := total + 1. |
|
1191 |
(total > limit) ifTrue:[ |
|
1192 |
string := string , buffer , '... )'. |
|
1193 |
^ string |
|
1194 |
] |
|
1195 |
]. |
|
1196 |
string := string , buffer , ')'. |
|
1197 |
^ string |
|
1198 |
! |
|
1199 |
||
1 | 1200 |
storeOn:aStream |
88 | 1201 |
"output a printed representation onto the argument, aStream. |
1202 |
The text can be re-read to reconstruct (a copy of) the receiver. |
|
1203 |
Recursive (i.e. cyclic) collections cannot be stored correctly |
|
1204 |
(use storeBinaryOn: to handle those)." |
|
1 | 1205 |
|
1206 |
|isEmpty| |
|
1207 |
||
44 | 1208 |
thisContext isRecursive ifTrue:[ |
155 | 1209 |
Transcript showCr:'Error: storeOn: of self referencing collection.'. |
293 | 1210 |
aStream nextPutAll:'#recursive'. |
155 | 1211 |
^ self |
44 | 1212 |
]. |
1213 |
||
1214 |
aStream nextPutAll:'('. |
|
1 | 1215 |
aStream nextPutAll:(self class name). |
44 | 1216 |
aStream nextPutAll:' new'. |
1 | 1217 |
isEmpty := true. |
1218 |
self do:[:element | |
|
155 | 1219 |
aStream nextPutAll:' add:'. |
1220 |
element storeOn:aStream. |
|
1221 |
aStream nextPutAll:';'. |
|
1222 |
isEmpty := false |
|
1 | 1223 |
]. |
1224 |
isEmpty ifFalse:[aStream nextPutAll:' yourself']. |
|
1225 |
aStream nextPut:$) |
|
44 | 1226 |
|
88 | 1227 |
" |
1228 |
OrderedCollection new storeOn:Transcript |
|
1229 |
(1 to:10) storeOn:Transcript |
|
1230 |
(Set new add:1; add:'hello'; yourself) storeOn:Transcript |
|
1231 |
" |
|
293 | 1232 |
" |
1233 |
|s| |
|
1234 |
||
1235 |
s := Set new. |
|
1236 |
s add:1; add:'hello'; add:s. |
|
1237 |
s storeOn:Transcript |
|
1238 |
" |
|
1 | 1239 |
! ! |
602 | 1240 |
|
1241 |
!Collection methodsFor:'queries'! |
|
1242 |
||
1243 |
isCollection |
|
1244 |
"return true, if the receiver is some kind of collection; |
|
1245 |
true is returned here - the method is redefined from Object." |
|
1246 |
||
1247 |
^ true |
|
1248 |
! |
|
1249 |
||
1174 | 1250 |
isSorted |
1251 |
"return true, if the receiver is sorted. |
|
1252 |
Collections which hold their elements in sorted order |
|
1253 |
should return true. Some algorithms (quicksort) degenerate when |
|
1254 |
operating on sorted collections and can be avoided if this information |
|
1255 |
is given. The default returned here (false) should not hurt. |
|
1256 |
I.e. you should NEVER depend on that in your application." |
|
1257 |
||
1258 |
^ false |
|
1259 |
||
1260 |
"Created: 13.4.1996 / 12:35:55 / cg" |
|
1261 |
! |
|
1262 |
||
602 | 1263 |
longestCommonPrefix |
1246 | 1264 |
"return the longest common prefix of my elements. |
1265 |
Typically used with string collections." |
|
1266 |
||
602 | 1267 |
|try allMatching matchLen| |
1268 |
||
1269 |
" |
|
1270 |
find the longest common prefix |
|
1271 |
" |
|
1272 |
matchLen := 0. |
|
1273 |
self do:[:aName | |
|
1246 | 1274 |
aName size > matchLen ifTrue:[ |
1275 |
matchLen := aName size. |
|
1276 |
try := aName |
|
1277 |
] |
|
602 | 1278 |
]. |
1279 |
||
1280 |
allMatching := true. |
|
1281 |
||
1282 |
[true] whileTrue:[ |
|
1246 | 1283 |
allMatching := true. |
1284 |
self do:[:aName | |
|
1285 |
(aName startsWith:try) ifFalse:[ |
|
1286 |
allMatching := false |
|
1287 |
] |
|
1288 |
]. |
|
1289 |
allMatching ifTrue:[ |
|
1290 |
^ try |
|
1291 |
]. |
|
1292 |
matchLen := matchLen - 1. |
|
1293 |
try := try copyTo:matchLen. |
|
602 | 1294 |
]. |
1295 |
^ try |
|
1296 |
||
1297 |
" |
|
1298 |
#('Array' 'ArrayedCollection' 'ArrayOfFoo') longestCommonPrefix |
|
1299 |
#('Arra' 'ArrayedCollection' 'ArrayOfFoo') longestCommonPrefix |
|
1300 |
#('Arra' 'b' 'c') longestCommonPrefix |
|
1301 |
#( (1 2 3 4) (1 2 3) (1 2 3 7) (1 2 3 9 10 11)) longestCommonPrefix |
|
1302 |
" |
|
1246 | 1303 |
|
1304 |
"Modified: 22.4.1996 / 13:04:56 / cg" |
|
602 | 1305 |
! ! |
1306 |
||
1307 |
!Collection methodsFor:'testing'! |
|
1308 |
||
1309 |
capacity |
|
1310 |
"return the number of elements, that the receiver is |
|
1311 |
prepared to take. For most collections, this is the actual |
|
1312 |
size. However, some have more space preallocated to allow |
|
1313 |
for faster adding of elements. |
|
1314 |
Not used by the system; added for ST-80 compatibility." |
|
1315 |
||
1316 |
^ self size |
|
1317 |
! |
|
1318 |
||
1319 |
includes:anElement |
|
1320 |
"return true, if an object equal to the argument, anObject is in the list. |
|
1321 |
This compares using #= (i.e. it does not look for the object itself, |
|
1322 |
instead, one that compares equal). |
|
1323 |
See #includesIdentical: when identity is asked for." |
|
1324 |
||
1325 |
self do:[:element | |
|
1326 |
(anElement = element) ifTrue:[^ true]. |
|
1327 |
]. |
|
1328 |
^ false |
|
1329 |
! |
|
1330 |
||
1331 |
includesAll:aCollection |
|
1332 |
"return true, if the the receiver includes all elements of |
|
1333 |
the argument, aCollection; false if any is missing. |
|
1334 |
Notice: this method has O-square runtime behavior and may be |
|
1335 |
slow for big receivers/args. Think about using a Set, |
|
1336 |
or Dictionary." |
|
1337 |
||
1338 |
aCollection do:[:element | |
|
1339 |
(self includes:element) ifFalse:[^ false]. |
|
1340 |
]. |
|
1341 |
^ true |
|
1342 |
||
1343 |
" |
|
1344 |
#(1 2 3 4 5 6 7) includesAll:#(1 2 3) |
|
1345 |
#('hello' 'there' 'world') includesAll:#('hello' 'world') |
|
1346 |
#(1 2 3 4 5 6 7) includesAll:#(7 8 9) |
|
1347 |
" |
|
1348 |
! |
|
1349 |
||
1350 |
includesAny:aCollection |
|
1351 |
"return true, if the the receiver includes any elements of |
|
1352 |
the argument, aCollection; false if it includes none. |
|
1353 |
Notice: this method has O-square runtime behavior and may be |
|
1354 |
slow for big receivers/args. Think about using a Set, |
|
1355 |
or Dictionary. Speedup is possible, by arrangy highly |
|
1356 |
probable elements towards the beginning of aCollection, |
|
1357 |
to avoid useless searches." |
|
1358 |
||
1359 |
aCollection do:[:element | |
|
1360 |
(self includes:element) ifTrue:[^ true]. |
|
1361 |
]. |
|
1362 |
^ false |
|
1363 |
||
1364 |
" |
|
1365 |
#(1 2 3 4 5 6 7) includesAny:#(1 2 3) |
|
1366 |
#('hello' 'there' 'world') includesAny:#('hello' 'world') |
|
1367 |
#(1 2 3 4 5 6 7) includesAny:#(7 8 9) |
|
1368 |
#(1 2 3 4 5 6 7) includesAny:#(8 9 10) |
|
1369 |
" |
|
1370 |
! |
|
1371 |
||
1372 |
includesIdentical:anElement |
|
1373 |
"return true, if the argument, anObject is in the list. |
|
1374 |
This compares using #== (i.e. object identity). |
|
1375 |
See #includes: when equality is asked for." |
|
1376 |
||
1377 |
self do:[:element | |
|
1378 |
(anElement == element) ifTrue:[^ true]. |
|
1379 |
]. |
|
1380 |
^ false |
|
1381 |
! |
|
1382 |
||
1383 |
isEmpty |
|
1384 |
"return true, if the receiver is empty" |
|
1385 |
||
1386 |
^ self size == 0 |
|
1387 |
! |
|
1388 |
||
1389 |
max |
|
1390 |
"return the maximum value in the receiver collection" |
|
1391 |
||
1392 |
^ self inject:nil |
|
1393 |
into:[:maxSoFar :this | |
|
1394 |
(maxSoFar isNil |
|
1395 |
or:[this > maxSoFar]) ifTrue:[this] |
|
1396 |
ifFalse:[maxSoFar]] |
|
1397 |
||
1398 |
" |
|
1399 |
#(15 1 -9 10 5) max |
|
1400 |
(1 to:15) max |
|
1401 |
" |
|
1402 |
! |
|
1403 |
||
1404 |
min |
|
1405 |
"return the minimum value in the receiver collection" |
|
1406 |
||
1407 |
^ self inject:nil |
|
1408 |
into:[:minSoFar :this | |
|
1409 |
(minSoFar isNil |
|
1410 |
or:[this < minSoFar]) ifTrue:[this] |
|
1411 |
ifFalse:[minSoFar]] |
|
1412 |
||
1413 |
" |
|
1414 |
#(15 1 -9 10 5) min |
|
1415 |
(1 to:15) min |
|
1416 |
" |
|
1417 |
! |
|
1418 |
||
1419 |
notEmpty |
|
1420 |
"return true, if the receiver is not empty" |
|
1421 |
||
1422 |
^ self isEmpty not |
|
1423 |
! |
|
1424 |
||
1425 |
occurrencesOf:anElement |
|
1426 |
"return the number of occurrences of the argument, anElement in |
|
1427 |
the receiver" |
|
1428 |
||
1429 |
|count "{ Class: SmallInteger }" | |
|
1430 |
||
1431 |
count := 0. |
|
1432 |
self do:[:element | |
|
1433 |
(anElement = element) ifTrue:[ |
|
1434 |
count := count + 1 |
|
1435 |
]. |
|
1436 |
]. |
|
1437 |
^ count |
|
1438 |
! |
|
1439 |
||
1440 |
size |
|
1441 |
"return the number of elements in the receiver. |
|
1442 |
This is usually redefined in subclasses for more performance." |
|
1443 |
||
1444 |
|count "{ Class: SmallInteger }" | |
|
1445 |
||
1446 |
count := 0. |
|
1447 |
self do:[:element | |
|
1448 |
count := count + 1 |
|
1449 |
]. |
|
1450 |
^ count |
|
1451 |
! ! |
|
1452 |
||
628 | 1453 |
!Collection class methodsFor:'documentation'! |
1454 |
||
1455 |
version |
|
1289 | 1456 |
^ '$Header: /cvs/stx/stx/libbasic/Collection.st,v 1.52 1996-04-25 16:12:44 cg Exp $' |
628 | 1457 |
! ! |
602 | 1458 |
Collection initialize! |