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