Collection.st
changeset 348 5ac1b6b43600
parent 345 cf2301210c47
child 360 90c3608b92a3
equal deleted inserted replaced
347:0d4c08ca9da3 348:5ac1b6b43600
    11 "
    11 "
    12 
    12 
    13 Object subclass:#Collection
    13 Object subclass:#Collection
    14        instanceVariableNames:''
    14        instanceVariableNames:''
    15        classVariableNames:'InvalidKeySignal EmptyCollectionSignal
    15        classVariableNames:'InvalidKeySignal EmptyCollectionSignal
    16 			   ValueNotFoundSignal'
    16 			   ValueNotFoundSignal NotEnoughElementsSignal'
    17        poolDictionaries:''
    17        poolDictionaries:''
    18        category:'Collections-Abstract'
    18        category:'Collections-Abstract'
    19 !
    19 !
    20 
    20 
    21 Collection comment:'
    21 Collection comment:'
    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 $Header: /cvs/stx/stx/libbasic/Collection.st,v 1.33 1995-05-16 17:06:27 claus Exp $
    25 $Header: /cvs/stx/stx/libbasic/Collection.st,v 1.34 1995-05-18 15:09:00 claus Exp $
    26 '!
    26 '!
    27 
    27 
    28 !Collection class methodsFor:'documentation'!
    28 !Collection class methodsFor:'documentation'!
    29 
    29 
    30 copyright
    30 copyright
    41 "
    41 "
    42 !
    42 !
    43 
    43 
    44 version
    44 version
    45 "
    45 "
    46 $Header: /cvs/stx/stx/libbasic/Collection.st,v 1.33 1995-05-16 17:06:27 claus Exp $
    46 $Header: /cvs/stx/stx/libbasic/Collection.st,v 1.34 1995-05-18 15:09:00 claus Exp $
    47 "
    47 "
    48 !
    48 !
    49 
    49 
    50 documentation
    50 documentation
    51 "
    51 "
    71 
    71 
    72 	ValueNotFoundSignal := ErrorSignal newSignalMayProceed:true.
    72 	ValueNotFoundSignal := ErrorSignal newSignalMayProceed:true.
    73 	ValueNotFoundSignal nameClass:self message:#valueNotFoundSignal.
    73 	ValueNotFoundSignal nameClass:self message:#valueNotFoundSignal.
    74 	ValueNotFoundSignal notifierString:'value not found:'.
    74 	ValueNotFoundSignal notifierString:'value not found:'.
    75 
    75 
    76 	EmptyCollectionSignal := ErrorSignal newSignalMayProceed:true.
    76 	NotEnoughElementsSignal := ErrorSignal newSignalMayProceed:true.
       
    77 	NotEnoughElementsSignal nameClass:self message:#notEnoughElementsSignal.
       
    78 	NotEnoughElementsSignal notifierString:'not enough elements in collection'.
       
    79 
       
    80 	EmptyCollectionSignal := NotEnoughElementsSignal newSignalMayProceed:true.
    77 	EmptyCollectionSignal nameClass:self message:#emptyCollectionSignal.
    81 	EmptyCollectionSignal nameClass:self message:#emptyCollectionSignal.
    78 	EmptyCollectionSignal notifierString:'operation not allowed for empty collections'.
    82 	EmptyCollectionSignal notifierString:'operation not allowed for empty collections'.
    79     ]
    83     ]
    80 ! !
    84 ! !
    81 
    85 
    95 
    99 
    96 emptyCollectionSignal
   100 emptyCollectionSignal
    97     "return the signal used to report non-allowed operation on empty collections"
   101     "return the signal used to report non-allowed operation on empty collections"
    98 
   102 
    99     ^ EmptyCollectionSignal
   103     ^ EmptyCollectionSignal
       
   104 ! 
       
   105 
       
   106 notEnoughElementsSignal
       
   107     "return the signal used to report attempts for an operation, for which
       
   108      there are not enough elements in the collection"
       
   109 
       
   110     ^ NotEnoughElementsSignal
   100 ! !
   111 ! !
   101 
   112 
   102 !Collection class methodsFor:'queries'!
   113 !Collection class methodsFor:'queries'!
   103 
   114 
   104 growIsCheap
   115 growIsCheap
   216 emptyCollectionError
   227 emptyCollectionError
   217     "report an error that the operation is not allowed for
   228     "report an error that the operation is not allowed for
   218      empty collections"
   229      empty collections"
   219 
   230 
   220     ^ EmptyCollectionSignal raise
   231     ^ EmptyCollectionSignal raise
       
   232 !
       
   233 
       
   234 notEnoughElementsError
       
   235     "report an error that the operation is not allowed,  
       
   236      since not enough elements are in the collection"
       
   237 
       
   238     ^ NotEnoughElementsSignal raise
   221 !
   239 !
   222 
   240 
   223 emptyCheck 
   241 emptyCheck 
   224     "check if the receiver is empty; report an error if so"
   242     "check if the receiver is empty; report an error if so"
   225 
   243 
   387      c addAllFirst:#(9 8 7 6 5)
   405      c addAllFirst:#(9 8 7 6 5)
   388     "
   406     "
   389 !
   407 !
   390 
   408 
   391 remove:anObject ifAbsent:exceptionBlock
   409 remove:anObject ifAbsent:exceptionBlock
   392     "remove the argument, anObject from the receiver - if it was not
   410     "remove the (first occurrence of) argument, anObject from the receiver,
   393      in the collection returns the the value of the exceptionBlock"
   411      and return it.
       
   412      If it was not in the collection, return the the value of the exceptionBlock"
   394 
   413 
   395     ^ self subclassResponsibility
   414     ^ self subclassResponsibility
   396 !
   415 !
   397 
   416 
   398 remove:anObject
   417 remove:anObject
   399     "remove the argument, anObject from the receiver"
   418     "remove the (first occurrence of) argument, anObject from the receiver,
       
   419      and return it.
       
   420      If it was not in the collection, raise an error."
   400 
   421 
   401     self remove:anObject ifAbsent:[self errorValueNotFound:anObject]
   422     self remove:anObject ifAbsent:[self errorValueNotFound:anObject]
   402 !
   423 !
   403 
   424 
   404 removeAll
   425 removeAll
   414 removeAll:aCollection
   435 removeAll:aCollection
   415     "remove all elements of the argument, aCollection from the receiver"
   436     "remove all elements of the argument, aCollection from the receiver"
   416 
   437 
   417     aCollection do:[:element | self remove:element].
   438     aCollection do:[:element | self remove:element].
   418     ^ aCollection
   439     ^ aCollection
       
   440 !
       
   441 
       
   442 removeFirst
       
   443     "remove the first element from the receiver.
       
   444      Return the removed element."
       
   445 
       
   446     ^ self subclassResponsibility
       
   447 !
       
   448 
       
   449 removeLast
       
   450     "remove the last element from the receiver.
       
   451      Return the removed element."
       
   452 
       
   453     ^ self subclassResponsibility
       
   454 !
       
   455 
       
   456 removeFirst:n
       
   457     "remove the first n elements from the receiver.
       
   458      Return an array filled with the removed elements."
       
   459 
       
   460     |ret|
       
   461 
       
   462     self size < n ifTrue:[
       
   463 	^ self notEnoughElementsError
       
   464     ].
       
   465     ret := Array new:n.
       
   466     1 to:n do:[:i |
       
   467 	ret at:i put:(self removeFirst).
       
   468     ].
       
   469     ^ ret
       
   470 !
       
   471 
       
   472 removeLast:n
       
   473     "remove the last n elements from the receiver.
       
   474      Return an array filled with the removed elements."
       
   475 
       
   476     |ret|
       
   477 
       
   478     self size < n ifTrue:[
       
   479 	^ self notEnoughElementsError
       
   480     ].
       
   481     ret := Array new:n.
       
   482     n to:1 by:-1 do:[:i |
       
   483 	ret at:i put:(self removeLast).
       
   484     ].
       
   485     ^ ret
   419 ! !
   486 ! !
   420 
   487 
   421 !Collection methodsFor:'growing'!
   488 !Collection methodsFor:'growing'!
   422 
   489 
   423 growSize
   490 growSize