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