author | Claus Gittinger <cg@exept.de> |
Fri, 19 Apr 1996 12:47:25 +0200 | |
changeset 1221 | 46d72af387e9 |
parent 1212 | 4f05e716a6a7 |
child 1246 | 132a3f6c83bf |
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 |
826 |
|nextValue| |
|
827 |
||
828 |
nextValue := thisValue. |
|
829 |
self do: [:each | nextValue := binaryBlock value:nextValue value:each]. |
|
830 |
^ nextValue |
|
68 | 831 |
|
832 |
" |
|
833 |
#(1 2 3 4) inject:0 into:[:accu :element | accu + element] |
|
834 |
(1 to:10) inject:0 into:[:accu :element | accu + element] |
|
835 |
" |
|
1 | 836 |
! |
837 |
||
602 | 838 |
keysAndValuesDo:aTwoArgBlock |
839 |
"evaluate the argument, aBlock for every element in the collection, |
|
840 |
passing both index and element as arguments." |
|
841 |
||
842 |
^ self errorNotKeyed |
|
843 |
! |
|
844 |
||
845 |
pairsDo:aBlock |
|
846 |
"evaluate the argument, aBlock for every element in the collection, |
|
847 |
which is supposed to consist of 2-element collections. |
|
848 |
The block is called with 2 arguments for each collection in the receiver." |
|
849 |
||
850 |
self do:[:aPair | |
|
851 |
aBlock value:(aPair at:1) value:(aPair at:2). |
|
852 |
] |
|
853 |
" |
|
854 |
#( (1 one) (2 two) (3 three) (4 four) (5 five) (6 six)) |
|
855 |
pairsDo:[:num :sym | Transcript show:num; show:' is: '; showCr:sym] |
|
856 |
||
857 |
||
858 |
#( (1 1) (1 2) (1 3) (1 4) (1 5)) |
|
859 |
pairsDo:[:x :y | Transcript showCr:x@y] |
|
860 |
" |
|
861 |
! |
|
862 |
||
1 | 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 |
||
602 | 875 |
reverseDo:aBlock |
876 |
"evaluate the argument, aBlock for each element in reverse order." |
|
877 |
||
878 |
"it could be defined in terms of do: - but very inefficient. |
|
879 |
Better force programmer to define a better version ..." |
|
880 |
||
881 |
^ self subclassResponsibility |
|
882 |
! |
|
883 |
||
1 | 884 |
select:aBlock |
885 |
"return a new collection with all elements from the receiver, for which |
|
886 |
the argument aBlock evaluates to true" |
|
887 |
||
888 |
|newCollection| |
|
889 |
||
201 | 890 |
newCollection := self species new. |
1 | 891 |
self do:[:each | |
155 | 892 |
(aBlock value:each) ifTrue:[newCollection add:each]. |
1 | 893 |
]. |
894 |
^ newCollection |
|
68 | 895 |
|
896 |
" |
|
897 |
#(1 2 3 4) select:[:e | e odd] |
|
898 |
(1 to:10) select:[:e | e even] |
|
899 |
" |
|
900 |
! |
|
901 |
||
902 |
select:aBlock ifNone:exceptionBlock |
|
903 |
"try a new collection with all elements from the receiver, for which |
|
904 |
the argument aBlock evaluates to true. If none of the elements passes |
|
905 |
the check of aBlock, return the result of evaluating exceptionBlock." |
|
906 |
||
907 |
|newCollection| |
|
908 |
||
909 |
newCollection := self select:aBlock. |
|
910 |
newCollection isEmpty ifTrue:[^ exceptionBlock value]. |
|
911 |
^ newCollection |
|
912 |
||
913 |
" |
|
914 |
#(1 2 3 4) select:[:e | e > 10] ifNone:['sorry'] |
|
915 |
" |
|
12 | 916 |
! |
917 |
||
362 | 918 |
triplesDo:aBlock |
919 |
"evaluate the argument, aBlock for every element in the collection, |
|
920 |
which is supposed to consist of 3-element collections. |
|
921 |
The block is called with 3 arguments for each collection in the receiver." |
|
922 |
||
923 |
self do:[:aTriple | |
|
924 |
aBlock value:(aTriple at:1) value:(aTriple at:2) value:(aTriple at:3). |
|
925 |
] |
|
926 |
" |
|
927 |
#( (1 one eins) (2 two zwei) (3 three drei) (4 four vier) (5 five #'fünf') (6 six sechs)) |
|
928 |
triplesDo:[:num :sym1 :sym2 | Transcript show:num; space; show:sym1; space; showCr:sym2] |
|
929 |
" |
|
1 | 930 |
! |
931 |
||
602 | 932 |
with:aCollection collect:aTwoArgBlock |
933 |
"evaluate the argument, aBlock for successive elements from |
|
934 |
each the receiver and the argument, aSequenceableCollection; |
|
935 |
The second argument, aBlock must be a two-argument block, which is |
|
936 |
evaluated for each element-pair. |
|
937 |
Collect the results and return a collection containing them. |
|
938 |
This method fails if neither the receiver nor aCollection is |
|
939 |
a sequenceable collection (i.e. implements numeric key access)." |
|
260 | 940 |
|
602 | 941 |
|index "{ Class: SmallInteger }" |
942 |
newCollection| |
|
1 | 943 |
|
602 | 944 |
newCollection := self species new. |
945 |
index := 1. |
|
946 |
aCollection isSequenceable ifFalse:[ |
|
947 |
self isSequenceable ifFalse:[ |
|
948 |
^ self error:'neither collection is sequenceable'. |
|
949 |
]. |
|
950 |
aCollection do:[:element | |
|
951 |
newCollection add:(aTwoArgBlock value:(self at:index) |
|
952 |
value:element). |
|
953 |
index := index + 1 |
|
954 |
] |
|
955 |
] ifTrue:[ |
|
956 |
self do:[:element | |
|
957 |
newCollection add:(aTwoArgBlock value:element |
|
958 |
value:(aCollection at:index)). |
|
959 |
index := index + 1 |
|
960 |
] |
|
961 |
]. |
|
962 |
^ newCollection |
|
201 | 963 |
|
964 |
" |
|
602 | 965 |
(1 to:3) with:#(one two three) collect:[:num :sym | (num->sym)] |
966 |
#(1 2 3) with:#(10 20 30) collect:[:x :y | (x@y)] |
|
201 | 967 |
" |
2 | 968 |
! |
969 |
||
602 | 970 |
with:aCollection do:aTwoArgBlock |
971 |
"evaluate the argument, aBlock for successive elements from |
|
972 |
each the receiver and the argument, aSequenceableCollection. |
|
973 |
The second argument, aBlock must be a two-argument block. |
|
974 |
This method fails if neither the receiver nor aCollection is |
|
975 |
a sequenceable collection (i.e. implements numeric key access)." |
|
976 |
||
977 |
|index "{ Class: SmallInteger }"| |
|
2 | 978 |
|
602 | 979 |
index := 1. |
980 |
aCollection isSequenceable ifFalse:[ |
|
981 |
self isSequenceable ifFalse:[ |
|
982 |
^ self error:'neither collection is sequenceable'. |
|
983 |
]. |
|
984 |
aCollection do:[:element | |
|
985 |
aTwoArgBlock value:(self at:index) value:element. |
|
986 |
index := index + 1 |
|
987 |
] |
|
988 |
] ifTrue:[ |
|
989 |
self do:[:element | |
|
990 |
aTwoArgBlock value:element value:(aCollection at:index). |
|
991 |
index := index + 1 |
|
992 |
] |
|
993 |
] |
|
201 | 994 |
|
995 |
" |
|
602 | 996 |
(1 to:3) with:#(one two three) do:[:num :sym | |
997 |
Transcript showCr:(num->sym) |
|
998 |
] |
|
999 |
(1 to:3) with:#(one two three) asSet do:[:num :sym | |
|
1000 |
Transcript showCr:(num->sym) |
|
1001 |
]. |
|
1002 |
(1 to:3) asSet with:#(one two three) do:[:num :sym | |
|
1003 |
Transcript showCr:(num->sym) |
|
1004 |
] |
|
201 | 1005 |
" |
1 | 1006 |
! ! |
1007 |
||
602 | 1008 |
!Collection methodsFor:'error handling'! |
1009 |
||
1010 |
emptyCheck |
|
1011 |
"check if the receiver is empty; report an error if so" |
|
1012 |
||
1013 |
self size == 0 ifTrue:[ |
|
1014 |
^ self emptyCollectionError. |
|
1015 |
]. |
|
1016 |
! |
|
1017 |
||
1018 |
emptyCollectionError |
|
1019 |
"report an error that the operation is not allowed for |
|
1020 |
empty collections" |
|
1021 |
||
1022 |
^ EmptyCollectionSignal raise |
|
1023 |
! |
|
1024 |
||
1025 |
errorInvalidKey:aKey |
|
1026 |
"report an error that the given key was invalid" |
|
1027 |
||
1028 |
^ InvalidKeySignal raiseRequestWith:aKey |
|
1029 |
! |
|
1030 |
||
1031 |
errorNotKeyed |
|
1032 |
"report an error that keyed access methods are not allowed" |
|
1033 |
||
1034 |
^ self error:(self class name, 's do not respond to keyed accessing messages') |
|
1035 |
! |
|
1036 |
||
1037 |
errorValueNotFound:anObject |
|
1038 |
"report an error that an object was not found in the collection" |
|
1039 |
||
1040 |
^ ValueNotFoundSignal raiseRequestWith:anObject |
|
1041 |
! |
|
1042 |
||
1043 |
notEnoughElementsError |
|
1044 |
"report an error that the operation is not allowed, |
|
1045 |
since not enough elements are in the collection" |
|
1046 |
||
1047 |
^ NotEnoughElementsSignal raise |
|
1048 |
! ! |
|
1049 |
||
1050 |
!Collection methodsFor:'growing'! |
|
1051 |
||
1052 |
grow |
|
1053 |
"make the receiver larger" |
|
1054 |
||
1055 |
self grow:(self size + self growSize) |
|
1056 |
! |
|
1057 |
||
1058 |
grow:howBig |
|
1059 |
"change the receivers size" |
|
1060 |
||
1061 |
^ self subclassResponsibility |
|
1062 |
! |
|
1063 |
||
1064 |
growSize |
|
1065 |
"return a suitable size increment for growing. |
|
1066 |
The default returned here may be (and is) redefined in subclasses." |
|
1067 |
||
1068 |
^ self size max:2 |
|
1069 |
! ! |
|
1070 |
||
1 | 1071 |
!Collection methodsFor:'printing & storing'! |
1072 |
||
602 | 1073 |
displayString |
1074 |
"return a printed representation of the receiver for display in inspectors etc." |
|
1075 |
||
1076 |
^ self printOrDisplayStringUsing:#displayString |
|
1077 |
! |
|
1078 |
||
1 | 1079 |
maxPrint |
169 | 1080 |
"the print-limit; printOn: will try to not produce more output |
88 | 1081 |
than the limit defined here." |
1082 |
||
1 | 1083 |
^ 5000 |
1084 |
! |
|
1085 |
||
1086 |
printOn:aStream |
|
88 | 1087 |
"append a user readable representation of the receiver to aStream. |
1088 |
The text appended is not meant to be read back for reconstruction of |
|
169 | 1089 |
the receiver. Also, this method limits the size of generated string. |
88 | 1090 |
" |
1091 |
||
169 | 1092 |
|limit firstOne string| |
1 | 1093 |
|
44 | 1094 |
thisContext isRecursive ifTrue:[ |
155 | 1095 |
Transcript showCr:'Error: printOn: of self referencing collection.'. |
1096 |
aStream nextPutAll:'#("recursive")'. |
|
1097 |
^ self |
|
44 | 1098 |
]. |
1099 |
||
1100 |
limit := self maxPrint. |
|
1 | 1101 |
aStream nextPutAll:self class name. |
1102 |
aStream nextPut:$(. |
|
1103 |
firstOne := true. |
|
169 | 1104 |
|
1 | 1105 |
self do:[:element | |
169 | 1106 |
firstOne ifFalse:[ |
1107 |
aStream space |
|
1108 |
] ifTrue:[ |
|
1109 |
firstOne := false |
|
1110 |
]. |
|
1111 |
(limit <= 0) ifTrue:[ |
|
1112 |
aStream nextPutAll:'...etc...)'. |
|
1113 |
^ self |
|
1114 |
] ifFalse:[ |
|
92 | 1115 |
"/ old code, worked only on positionable streams |
1116 |
"/ |
|
1117 |
"/ pos1 := aStream position. |
|
1118 |
"/ element printOn:aStream. |
|
1119 |
"/ limit := limit - (aStream position - pos1) |
|
169 | 1120 |
|
1121 |
string := element printString. |
|
1122 |
aStream nextPutAll:string. |
|
1123 |
limit := limit - string size. |
|
155 | 1124 |
]. |
1 | 1125 |
]. |
1126 |
aStream nextPut:$) |
|
44 | 1127 |
|
88 | 1128 |
" |
1129 |
#(1 2 3 'hello' $a) printOn:Transcript |
|
169 | 1130 |
(Array new:100000) printOn:Transcript |
1131 |
(Array new:100000) printString size |
|
88 | 1132 |
(Dictionary new at:#hello put:'world'; yourself) printOn:Transcript |
169 | 1133 |
" |
1134 |
" |
|
88 | 1135 |
|a| |
1136 |
a := Array new:3. |
|
1137 |
a at:2 put:a. |
|
1138 |
a printOn:Transcript |
|
1139 |
" |
|
1 | 1140 |
! |
1141 |
||
602 | 1142 |
printOrDisplayStringUsing:aSelector |
1143 |
"common code for printString and displayString; they only differ in |
|
1144 |
the print-message sent to the elements" |
|
1145 |
||
1146 |
|thisString buffer count string noneYet total limit| |
|
1147 |
||
1148 |
thisContext isRecursive ifTrue:[ |
|
1149 |
Transcript showCr:'Error: print/storeString of self referencing collection.'. |
|
1150 |
^ '#("recursive")' |
|
1151 |
]. |
|
1152 |
||
1153 |
string := (self class name) , '('. |
|
1154 |
noneYet := true. |
|
1155 |
buffer := ''. |
|
1156 |
count := 0. |
|
1157 |
total := 0. |
|
1158 |
limit := self maxPrint. |
|
1159 |
||
1160 |
self do: [:element | |
|
1161 |
thisString := element perform:aSelector. |
|
1162 |
noneYet ifTrue:[ |
|
1163 |
noneYet := false. |
|
1164 |
buffer := buffer , thisString |
|
1165 |
] ifFalse:[ |
|
1166 |
buffer := buffer , (' ' , thisString) |
|
1167 |
]. |
|
1168 |
count := count + 1. |
|
1169 |
(count == 20) ifTrue:[ |
|
1170 |
string := string , buffer. |
|
1171 |
buffer := ''. |
|
1172 |
count := 0 |
|
1173 |
]. |
|
1174 |
total := total + 1. |
|
1175 |
(total > limit) ifTrue:[ |
|
1176 |
string := string , buffer , '... )'. |
|
1177 |
^ string |
|
1178 |
] |
|
1179 |
]. |
|
1180 |
string := string , buffer , ')'. |
|
1181 |
^ string |
|
1182 |
! |
|
1183 |
||
1 | 1184 |
storeOn:aStream |
88 | 1185 |
"output a printed representation onto the argument, aStream. |
1186 |
The text can be re-read to reconstruct (a copy of) the receiver. |
|
1187 |
Recursive (i.e. cyclic) collections cannot be stored correctly |
|
1188 |
(use storeBinaryOn: to handle those)." |
|
1 | 1189 |
|
1190 |
|isEmpty| |
|
1191 |
||
44 | 1192 |
thisContext isRecursive ifTrue:[ |
155 | 1193 |
Transcript showCr:'Error: storeOn: of self referencing collection.'. |
293 | 1194 |
aStream nextPutAll:'#recursive'. |
155 | 1195 |
^ self |
44 | 1196 |
]. |
1197 |
||
1198 |
aStream nextPutAll:'('. |
|
1 | 1199 |
aStream nextPutAll:(self class name). |
44 | 1200 |
aStream nextPutAll:' new'. |
1 | 1201 |
isEmpty := true. |
1202 |
self do:[:element | |
|
155 | 1203 |
aStream nextPutAll:' add:'. |
1204 |
element storeOn:aStream. |
|
1205 |
aStream nextPutAll:';'. |
|
1206 |
isEmpty := false |
|
1 | 1207 |
]. |
1208 |
isEmpty ifFalse:[aStream nextPutAll:' yourself']. |
|
1209 |
aStream nextPut:$) |
|
44 | 1210 |
|
88 | 1211 |
" |
1212 |
OrderedCollection new storeOn:Transcript |
|
1213 |
(1 to:10) storeOn:Transcript |
|
1214 |
(Set new add:1; add:'hello'; yourself) storeOn:Transcript |
|
1215 |
" |
|
293 | 1216 |
" |
1217 |
|s| |
|
1218 |
||
1219 |
s := Set new. |
|
1220 |
s add:1; add:'hello'; add:s. |
|
1221 |
s storeOn:Transcript |
|
1222 |
" |
|
1 | 1223 |
! ! |
602 | 1224 |
|
1225 |
!Collection methodsFor:'queries'! |
|
1226 |
||
1227 |
isCollection |
|
1228 |
"return true, if the receiver is some kind of collection; |
|
1229 |
true is returned here - the method is redefined from Object." |
|
1230 |
||
1231 |
^ true |
|
1232 |
! |
|
1233 |
||
1174 | 1234 |
isSorted |
1235 |
"return true, if the receiver is sorted. |
|
1236 |
Collections which hold their elements in sorted order |
|
1237 |
should return true. Some algorithms (quicksort) degenerate when |
|
1238 |
operating on sorted collections and can be avoided if this information |
|
1239 |
is given. The default returned here (false) should not hurt. |
|
1240 |
I.e. you should NEVER depend on that in your application." |
|
1241 |
||
1242 |
^ false |
|
1243 |
||
1244 |
"Created: 13.4.1996 / 12:35:55 / cg" |
|
1245 |
! |
|
1246 |
||
602 | 1247 |
longestCommonPrefix |
1248 |
|try allMatching matchLen| |
|
1249 |
||
1250 |
" |
|
1251 |
find the longest common prefix |
|
1252 |
" |
|
1253 |
matchLen := 0. |
|
1254 |
self do:[:aName | |
|
1255 |
aName size > matchLen ifTrue:[ |
|
1256 |
matchLen := aName size. |
|
1257 |
try := aName |
|
1258 |
] |
|
1259 |
]. |
|
1260 |
||
1261 |
allMatching := true. |
|
1262 |
||
1263 |
[true] whileTrue:[ |
|
1264 |
allMatching := true. |
|
1265 |
self do:[:aName | |
|
1266 |
(aName startsWith:try) ifFalse:[ |
|
1267 |
allMatching := false |
|
1268 |
] |
|
1269 |
]. |
|
1270 |
allMatching ifTrue:[ |
|
1271 |
^ try |
|
1272 |
]. |
|
1273 |
matchLen := matchLen - 1. |
|
1274 |
try := try copyTo:matchLen. |
|
1275 |
]. |
|
1276 |
^ try |
|
1277 |
||
1278 |
" |
|
1279 |
#('Array' 'ArrayedCollection' 'ArrayOfFoo') longestCommonPrefix |
|
1280 |
#('Arra' 'ArrayedCollection' 'ArrayOfFoo') longestCommonPrefix |
|
1281 |
#('Arra' 'b' 'c') longestCommonPrefix |
|
1282 |
#( (1 2 3 4) (1 2 3) (1 2 3 7) (1 2 3 9 10 11)) longestCommonPrefix |
|
1283 |
" |
|
1284 |
! ! |
|
1285 |
||
1286 |
!Collection methodsFor:'testing'! |
|
1287 |
||
1288 |
capacity |
|
1289 |
"return the number of elements, that the receiver is |
|
1290 |
prepared to take. For most collections, this is the actual |
|
1291 |
size. However, some have more space preallocated to allow |
|
1292 |
for faster adding of elements. |
|
1293 |
Not used by the system; added for ST-80 compatibility." |
|
1294 |
||
1295 |
^ self size |
|
1296 |
! |
|
1297 |
||
1298 |
includes:anElement |
|
1299 |
"return true, if an object equal to the argument, anObject is in the list. |
|
1300 |
This compares using #= (i.e. it does not look for the object itself, |
|
1301 |
instead, one that compares equal). |
|
1302 |
See #includesIdentical: when identity is asked for." |
|
1303 |
||
1304 |
self do:[:element | |
|
1305 |
(anElement = element) ifTrue:[^ true]. |
|
1306 |
]. |
|
1307 |
^ false |
|
1308 |
! |
|
1309 |
||
1310 |
includesAll:aCollection |
|
1311 |
"return true, if the the receiver includes all elements of |
|
1312 |
the argument, aCollection; false if any is missing. |
|
1313 |
Notice: this method has O-square runtime behavior and may be |
|
1314 |
slow for big receivers/args. Think about using a Set, |
|
1315 |
or Dictionary." |
|
1316 |
||
1317 |
aCollection do:[:element | |
|
1318 |
(self includes:element) ifFalse:[^ false]. |
|
1319 |
]. |
|
1320 |
^ true |
|
1321 |
||
1322 |
" |
|
1323 |
#(1 2 3 4 5 6 7) includesAll:#(1 2 3) |
|
1324 |
#('hello' 'there' 'world') includesAll:#('hello' 'world') |
|
1325 |
#(1 2 3 4 5 6 7) includesAll:#(7 8 9) |
|
1326 |
" |
|
1327 |
! |
|
1328 |
||
1329 |
includesAny:aCollection |
|
1330 |
"return true, if the the receiver includes any elements of |
|
1331 |
the argument, aCollection; false if it includes none. |
|
1332 |
Notice: this method has O-square runtime behavior and may be |
|
1333 |
slow for big receivers/args. Think about using a Set, |
|
1334 |
or Dictionary. Speedup is possible, by arrangy highly |
|
1335 |
probable elements towards the beginning of aCollection, |
|
1336 |
to avoid useless searches." |
|
1337 |
||
1338 |
aCollection do:[:element | |
|
1339 |
(self includes:element) ifTrue:[^ true]. |
|
1340 |
]. |
|
1341 |
^ false |
|
1342 |
||
1343 |
" |
|
1344 |
#(1 2 3 4 5 6 7) includesAny:#(1 2 3) |
|
1345 |
#('hello' 'there' 'world') includesAny:#('hello' 'world') |
|
1346 |
#(1 2 3 4 5 6 7) includesAny:#(7 8 9) |
|
1347 |
#(1 2 3 4 5 6 7) includesAny:#(8 9 10) |
|
1348 |
" |
|
1349 |
! |
|
1350 |
||
1351 |
includesIdentical:anElement |
|
1352 |
"return true, if the argument, anObject is in the list. |
|
1353 |
This compares using #== (i.e. object identity). |
|
1354 |
See #includes: when equality is asked for." |
|
1355 |
||
1356 |
self do:[:element | |
|
1357 |
(anElement == element) ifTrue:[^ true]. |
|
1358 |
]. |
|
1359 |
^ false |
|
1360 |
! |
|
1361 |
||
1362 |
isEmpty |
|
1363 |
"return true, if the receiver is empty" |
|
1364 |
||
1365 |
^ self size == 0 |
|
1366 |
! |
|
1367 |
||
1368 |
max |
|
1369 |
"return the maximum value in the receiver collection" |
|
1370 |
||
1371 |
^ self inject:nil |
|
1372 |
into:[:maxSoFar :this | |
|
1373 |
(maxSoFar isNil |
|
1374 |
or:[this > maxSoFar]) ifTrue:[this] |
|
1375 |
ifFalse:[maxSoFar]] |
|
1376 |
||
1377 |
" |
|
1378 |
#(15 1 -9 10 5) max |
|
1379 |
(1 to:15) max |
|
1380 |
" |
|
1381 |
! |
|
1382 |
||
1383 |
min |
|
1384 |
"return the minimum value in the receiver collection" |
|
1385 |
||
1386 |
^ self inject:nil |
|
1387 |
into:[:minSoFar :this | |
|
1388 |
(minSoFar isNil |
|
1389 |
or:[this < minSoFar]) ifTrue:[this] |
|
1390 |
ifFalse:[minSoFar]] |
|
1391 |
||
1392 |
" |
|
1393 |
#(15 1 -9 10 5) min |
|
1394 |
(1 to:15) min |
|
1395 |
" |
|
1396 |
! |
|
1397 |
||
1398 |
notEmpty |
|
1399 |
"return true, if the receiver is not empty" |
|
1400 |
||
1401 |
^ self isEmpty not |
|
1402 |
! |
|
1403 |
||
1404 |
occurrencesOf:anElement |
|
1405 |
"return the number of occurrences of the argument, anElement in |
|
1406 |
the receiver" |
|
1407 |
||
1408 |
|count "{ Class: SmallInteger }" | |
|
1409 |
||
1410 |
count := 0. |
|
1411 |
self do:[:element | |
|
1412 |
(anElement = element) ifTrue:[ |
|
1413 |
count := count + 1 |
|
1414 |
]. |
|
1415 |
]. |
|
1416 |
^ count |
|
1417 |
! |
|
1418 |
||
1419 |
size |
|
1420 |
"return the number of elements in the receiver. |
|
1421 |
This is usually redefined in subclasses for more performance." |
|
1422 |
||
1423 |
|count "{ Class: SmallInteger }" | |
|
1424 |
||
1425 |
count := 0. |
|
1426 |
self do:[:element | |
|
1427 |
count := count + 1 |
|
1428 |
]. |
|
1429 |
^ count |
|
1430 |
! ! |
|
1431 |
||
628 | 1432 |
!Collection class methodsFor:'documentation'! |
1433 |
||
1434 |
version |
|
1212 | 1435 |
^ '$Header: /cvs/stx/stx/libbasic/Attic/Coll.st,v 1.49 1996-04-18 12:18:53 cg Exp $' |
628 | 1436 |
! ! |
602 | 1437 |
Collection initialize! |