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