22 COPYRIGHT (c) 1989 by Claus Gittinger |
22 COPYRIGHT (c) 1989 by Claus Gittinger |
23 All Rights Reserved |
23 All Rights Reserved |
24 |
24 |
25 Abstract superclass for all collections |
25 Abstract superclass for all collections |
26 |
26 |
27 $Header: /cvs/stx/stx/libbasic/Collection.st,v 1.5 1993-11-08 02:29:51 claus Exp $ |
27 $Header: /cvs/stx/stx/libbasic/Collection.st,v 1.6 1993-12-11 00:45:11 claus Exp $ |
28 '! |
28 '! |
29 |
29 |
30 !Collection class methodsFor:'initialization'! |
30 !Collection class methodsFor:'initialization'! |
31 |
31 |
32 initialize |
32 initialize |
148 "add the argument, anObject to the receiver" |
148 "add the argument, anObject to the receiver" |
149 |
149 |
150 ^ self add:anObject |
150 ^ self add:anObject |
151 ! |
151 ! |
152 |
152 |
|
153 addFirst:anObject |
|
154 "add the argument, anObject to the receiver" |
|
155 |
|
156 ^ self subclassResponsibility |
|
157 ! |
|
158 |
153 addAll:aCollection |
159 addAll:aCollection |
154 "add all elements of the argument, aCollection to the receiver" |
160 "add all elements of the argument, aCollection to the receiver" |
155 |
161 |
156 aCollection do:[:element | |
162 aCollection do:[:element | |
157 self add:element |
163 self add:element |
158 ]. |
164 ]. |
159 ^ aCollection |
165 ^ aCollection |
|
166 ! |
|
167 |
|
168 addAllLast:aCollection |
|
169 "add all elements of the argument, aCollection to the receiver" |
|
170 |
|
171 ^ self addAll:aCollection |
|
172 ! |
|
173 |
|
174 addAllFirst:aCollection |
|
175 "insert all elements of the argument, aCollection at the beginning |
|
176 of the receiver" |
|
177 |
|
178 aCollection reverseDo:[:element | |
|
179 self addFirst: element |
|
180 ]. |
|
181 ^ aCollection |
|
182 |
|
183 " |
|
184 |c| |
|
185 c := #(4 3 2 1) asOrderedCollection. |
|
186 c addAllFirst:#(9 8 7 6 5) |
|
187 " |
160 ! |
188 ! |
161 |
189 |
162 remove:anObject ifAbsent:exceptionBlock |
190 remove:anObject ifAbsent:exceptionBlock |
163 "remove the argument, anObject from the receiver - if it was not |
191 "remove the argument, anObject from the receiver - if it was not |
164 in the collection returns the the value of the exceptionBlock" |
192 in the collection returns the the value of the exceptionBlock" |
403 ! |
439 ! |
404 |
440 |
405 asBag |
441 asBag |
406 "return a new Bag with the receiver collections elements" |
442 "return a new Bag with the receiver collections elements" |
407 |
443 |
408 |aBag| |
444 ^ self addAllTo:(Bag new) |
409 |
|
410 aBag := Bag new. |
|
411 self do:[:each | aBag add:each]. |
|
412 ^ aBag |
|
413 ! |
445 ! |
414 |
446 |
415 asOrderedCollection |
447 asOrderedCollection |
416 "return a new OrderedCollection with the receiver collections elements" |
448 "return a new OrderedCollection with the receiver collections elements" |
417 |
449 |
418 |anOrderedCollection| |
450 ^ self addAllTo:(OrderedCollection new:self size) |
419 |
|
420 anOrderedCollection := OrderedCollection new:self size. |
|
421 self do:[:each | anOrderedCollection addLast:each]. |
|
422 ^ anOrderedCollection |
|
423 ! |
451 ! |
424 |
452 |
425 asSet |
453 asSet |
426 "return a new Set with the receiver collections elements" |
454 "return a new Set with the receiver collections elements" |
427 |
455 |
428 |aSet| |
456 ^ self addAllTo:(Set new:self size) |
429 |
457 ! |
430 aSet := Set new: self size. |
458 |
431 self do:[:each | aSet add:each]. |
459 asIdentitySet |
432 ^ aSet |
460 "return a new IdentitySet with the receiver collections elements" |
|
461 |
|
462 ^ self addAllTo:(IdentitySet new:self size) |
433 ! |
463 ! |
434 |
464 |
435 asSortedCollection |
465 asSortedCollection |
436 "return a new SortedCollection with the receiver collections elements" |
466 "return a new SortedCollection with the receiver collections elements" |
437 |
467 |
469 |
499 |
470 maxPrint |
500 maxPrint |
471 ^ 5000 |
501 ^ 5000 |
472 ! |
502 ! |
473 |
503 |
474 printString |
504 printOrDisplayStringUsing:aSelector |
475 "return the printString of a big collection can take a long time |
505 "common code for printString and displayString; they only differ in |
476 due to long temporary strings - I use a buffer here collecting some |
506 the print-message sent to the elements" |
477 elements to reduce the GC overhead ... |
|
478 " |
|
479 |
507 |
480 |thisString buffer count string noneYet total| |
508 |thisString buffer count string noneYet total| |
481 |
509 |
482 string := (self class name) , '('. |
510 string := (self class name) , '('. |
483 noneYet := true. |
511 noneYet := true. |
484 buffer := ''. |
512 buffer := ''. |
485 count := 0. |
513 count := 0. |
486 total := 0. |
514 total := 0. |
487 self do: [:element | |
515 self do: [:element | |
488 thisString := element printString. |
516 thisString := element perform:aSelector. |
489 noneYet ifTrue:[ |
517 noneYet ifTrue:[ |
490 noneYet := false. |
518 noneYet := false. |
491 buffer := buffer , thisString |
519 buffer := buffer , thisString |
492 ] ifFalse:[ |
520 ] ifFalse:[ |
493 buffer := buffer , (' ' , thisString) |
521 buffer := buffer , (' ' , thisString) |
506 ]. |
534 ]. |
507 string := string , buffer , ')'. |
535 string := string , buffer , ')'. |
508 ^string |
536 ^string |
509 ! |
537 ! |
510 |
538 |
|
539 printString |
|
540 "return a printed representation of the receiver" |
|
541 |
|
542 ^ self printOrDisplayStringUsing:#printString |
|
543 ! |
|
544 |
511 displayString |
545 displayString |
512 "return the printString of a big collection can take a long time |
546 "return a printed representation of the receiver for display in inspectors etc." |
513 due to long temporary strings - I use a buffer here collecting some |
547 |
514 elements to reduce the GC overhead ... |
548 ^ self printOrDisplayStringUsing:#displayString |
515 " |
|
516 |
|
517 |thisString buffer count string noneYet total| |
|
518 |
|
519 string := (self class name) , '('. |
|
520 noneYet := true. |
|
521 buffer := ''. |
|
522 count := 0. |
|
523 total := 0. |
|
524 self do: [:element | |
|
525 thisString := element displayString. |
|
526 noneYet ifTrue:[ |
|
527 noneYet := false. |
|
528 buffer := buffer , thisString |
|
529 ] ifFalse:[ |
|
530 buffer := buffer , (' ' , thisString) |
|
531 ]. |
|
532 count := count + 1. |
|
533 (count == 20) ifTrue:[ |
|
534 string := string , buffer. |
|
535 buffer := ''. |
|
536 count := 0 |
|
537 ]. |
|
538 total := total + 1. |
|
539 (total > 5000) ifTrue:[ |
|
540 string := string , buffer , '... )'. |
|
541 ^string |
|
542 ] |
|
543 ]. |
|
544 string := string , buffer , ')'. |
|
545 ^string |
|
546 ! |
549 ! |
547 |
550 |
548 printOn:aStream |
551 printOn:aStream |
549 |tooMany firstOne noMore| |
552 |tooMany firstOne noMore| |
550 |
553 |