Coll.st
changeset 348 5ac1b6b43600
parent 345 cf2301210c47
child 360 90c3608b92a3
--- a/Coll.st	Wed May 17 14:17:43 1995 +0200
+++ b/Coll.st	Thu May 18 17:10:35 1995 +0200
@@ -13,7 +13,7 @@
 Object subclass:#Collection
        instanceVariableNames:''
        classVariableNames:'InvalidKeySignal EmptyCollectionSignal
-			   ValueNotFoundSignal'
+			   ValueNotFoundSignal NotEnoughElementsSignal'
        poolDictionaries:''
        category:'Collections-Abstract'
 !
@@ -22,7 +22,7 @@
 COPYRIGHT (c) 1989 by Claus Gittinger
 	      All Rights Reserved
 
-$Header: /cvs/stx/stx/libbasic/Attic/Coll.st,v 1.33 1995-05-16 17:06:27 claus Exp $
+$Header: /cvs/stx/stx/libbasic/Attic/Coll.st,v 1.34 1995-05-18 15:09:00 claus Exp $
 '!
 
 !Collection class methodsFor:'documentation'!
@@ -43,7 +43,7 @@
 
 version
 "
-$Header: /cvs/stx/stx/libbasic/Attic/Coll.st,v 1.33 1995-05-16 17:06:27 claus Exp $
+$Header: /cvs/stx/stx/libbasic/Attic/Coll.st,v 1.34 1995-05-18 15:09:00 claus Exp $
 "
 !
 
@@ -73,7 +73,11 @@
 	ValueNotFoundSignal nameClass:self message:#valueNotFoundSignal.
 	ValueNotFoundSignal notifierString:'value not found:'.
 
-	EmptyCollectionSignal := ErrorSignal newSignalMayProceed:true.
+	NotEnoughElementsSignal := ErrorSignal newSignalMayProceed:true.
+	NotEnoughElementsSignal nameClass:self message:#notEnoughElementsSignal.
+	NotEnoughElementsSignal notifierString:'not enough elements in collection'.
+
+	EmptyCollectionSignal := NotEnoughElementsSignal newSignalMayProceed:true.
 	EmptyCollectionSignal nameClass:self message:#emptyCollectionSignal.
 	EmptyCollectionSignal notifierString:'operation not allowed for empty collections'.
     ]
@@ -97,6 +101,13 @@
     "return the signal used to report non-allowed operation on empty collections"
 
     ^ EmptyCollectionSignal
+! 
+
+notEnoughElementsSignal
+    "return the signal used to report attempts for an operation, for which
+     there are not enough elements in the collection"
+
+    ^ NotEnoughElementsSignal
 ! !
 
 !Collection class methodsFor:'queries'!
@@ -220,6 +231,13 @@
     ^ EmptyCollectionSignal raise
 !
 
+notEnoughElementsError
+    "report an error that the operation is not allowed,  
+     since not enough elements are in the collection"
+
+    ^ NotEnoughElementsSignal raise
+!
+
 emptyCheck 
     "check if the receiver is empty; report an error if so"
 
@@ -389,14 +407,17 @@
 !
 
 remove:anObject ifAbsent:exceptionBlock
-    "remove the argument, anObject from the receiver - if it was not
-     in the collection returns the the value of the exceptionBlock"
+    "remove the (first occurrence of) argument, anObject from the receiver,
+     and return it.
+     If it was not in the collection, return the the value of the exceptionBlock"
 
     ^ self subclassResponsibility
 !
 
 remove:anObject
-    "remove the argument, anObject from the receiver"
+    "remove the (first occurrence of) argument, anObject from the receiver,
+     and return it.
+     If it was not in the collection, raise an error."
 
     self remove:anObject ifAbsent:[self errorValueNotFound:anObject]
 !
@@ -416,6 +437,52 @@
 
     aCollection do:[:element | self remove:element].
     ^ aCollection
+!
+
+removeFirst
+    "remove the first element from the receiver.
+     Return the removed element."
+
+    ^ self subclassResponsibility
+!
+
+removeLast
+    "remove the last element from the receiver.
+     Return the removed element."
+
+    ^ self subclassResponsibility
+!
+
+removeFirst:n
+    "remove the first n elements from the receiver.
+     Return an array filled with the removed elements."
+
+    |ret|
+
+    self size < n ifTrue:[
+	^ self notEnoughElementsError
+    ].
+    ret := Array new:n.
+    1 to:n do:[:i |
+	ret at:i put:(self removeFirst).
+    ].
+    ^ ret
+!
+
+removeLast:n
+    "remove the last n elements from the receiver.
+     Return an array filled with the removed elements."
+
+    |ret|
+
+    self size < n ifTrue:[
+	^ self notEnoughElementsError
+    ].
+    ret := Array new:n.
+    n to:1 by:-1 do:[:i |
+	ret at:i put:(self removeLast).
+    ].
+    ^ ret
 ! !
 
 !Collection methodsFor:'growing'!