--- a/OrdColl.st Wed May 17 14:17:43 1995 +0200
+++ b/OrdColl.st Thu May 18 17:10:35 1995 +0200
@@ -21,7 +21,7 @@
COPYRIGHT (c) 1989 by Claus Gittinger
All Rights Reserved
-$Header: /cvs/stx/stx/libbasic/Attic/OrdColl.st,v 1.24 1995-05-16 17:08:13 claus Exp $
+$Header: /cvs/stx/stx/libbasic/Attic/OrdColl.st,v 1.25 1995-05-18 15:09:50 claus Exp $
'!
!OrderedCollection class methodsFor:'documentation'!
@@ -42,7 +42,7 @@
version
"
-$Header: /cvs/stx/stx/libbasic/Attic/OrdColl.st,v 1.24 1995-05-16 17:08:13 claus Exp $
+$Header: /cvs/stx/stx/libbasic/Attic/OrdColl.st,v 1.25 1995-05-18 15:09:50 claus Exp $
"
!
@@ -200,9 +200,11 @@
].
^ anObject
- "(OrderedCollection withAll:#(1 2 3 4 5)) removeFirst; yourself"
- "(SortedCollection withAll:#(5 4 3 2 1)) removeFirst; yourself"
- "(SortedCollection new) removeFirst"
+ "
+ (OrderedCollection withAll:#(1 2 3 4 5)) removeFirst; yourself
+ OrderedCollection new removeFirst
+ (SortedCollection withAll:#(5 4 3 2 1)) removeFirst; yourself
+ "
!
removeLast
@@ -225,17 +227,98 @@
].
^ anObject
- "(OrderedCollection withAll:#(1 2 3 4 5)) removeLast; yourself"
- "(SortedCollection withAll:#(5 4 3 2 1)) removeLast; yourself"
+ "
+ (OrderedCollection withAll:#(1 2 3 4 5)) removeLast; yourself
+ OrderedCollection new removeLast
+ (SortedCollection withAll:#(5 4 3 2 1)) removeLast; yourself
+ "
+!
+
+removeFirst:n
+ "remove the first n elements from the collection;
+ return the elements in an Array"
+
+ |mySize ret|
+
+ mySize := self size.
+ mySize < n ifTrue:[
+ "error if collection has not enough elements"
+ ^ self notEnoughElementsError.
+ ].
+
+ ret := Array new:n.
+ ret replaceFrom:1 to:n with:contentsArray startingAt:firstIndex.
+ "/
+ "/ nil-out contents array, to not keep elements from being GC'd
+ "/
+ contentsArray from:firstIndex to:firstIndex + n - 1 put:nil.
+ firstIndex := firstIndex + n.
+
+ firstIndex > lastIndex ifTrue:[
+ "reset to avoid ever growing"
+ firstIndex := 1.
+ lastIndex := 0
+ ].
+ ^ ret
+
+ "
+ (OrderedCollection withAll:#(1 2 3 4 5)) removeFirst:2; yourself
+ (OrderedCollection withAll:#(1 2 3 4 5)) removeFirst:0; yourself
+ OrderedCollection new removeFirst:2
+ (OrderedCollection withAll:#(1 2 3 4 5)) removeFirst:6
+ (SortedCollection withAll:#(5 4 3 2 1)) removeFirst:2; yourself
+ "
+!
+
+removeLast:n
+ "remove the last n elements from the collection;
+ return the elements in an Array"
+
+ |mySize ret|
+
+ mySize := self size.
+ mySize < n ifTrue:[
+ "error if collection has not enough elements"
+ ^ self notEnoughElementsError.
+ ].
+
+ ret := Array new:n.
+ ret replaceFrom:1 to:n with:contentsArray startingAt:lastIndex - n + 1.
+ "/
+ "/ nil-out contents array, to not keep elements from being GC'd
+ "/
+ contentsArray from:lastIndex - n + 1 to:lastIndex put:nil.
+ lastIndex := lastIndex - n.
+
+ firstIndex > lastIndex ifTrue:[
+ "reset to avoid ever growing"
+ firstIndex := 1.
+ lastIndex := 0
+ ].
+ ^ ret
+
+ "
+ (OrderedCollection withAll:#(1 2 3 4 5)) removeLast:2; yourself
+ (OrderedCollection withAll:#(1 2 3 4 5)) removeLast:0; yourself
+ (OrderedCollection withAll:#(1 2 3 4 5)) removeLast:6; yourself
+ (SortedCollection withAll:#(5 4 3 2 1)) removeLast:2; yourself
+ "
!
removeFromIndex:startIndex toIndex:stopIndex
"remove the elements stored under startIndex up to and including
the elements under stopIndex.
- return the receiver."
+ Return the receiver.
+ Returning the receiver here is a historic leftover - it may change.
+ Please use yourself in a cascade, if you need the receivers value
+ when using this method."
|nDeleted|
+ (startIndex < firstIndex
+ or:[stopIndex > lastIndex]) ifTrue:[
+ ^ self notEnoughElementsError
+ ].
nDeleted := stopIndex - startIndex + 1.
contentsArray
replaceFrom:(firstIndex + startIndex - 1)
@@ -257,6 +340,7 @@
"
#(1 2 3 4 5 6 7 8 9) asOrderedCollection removeFromIndex:3 toIndex:6
+ #(1 2 3 4 5) asOrderedCollection removeFromIndex:3 toIndex:6
"
!
@@ -287,6 +371,11 @@
index := index + 1
].
^ exceptionBlock value
+
+ "
+ #(1 2 3 4 5 6 7 8 9) asOrderedCollection remove:3 ifAbsent:'oops'
+ #(1 2 3 4 5) asOrderedCollection remove:9 ifAbsent:'oops'
+ "
!
removeAll
@@ -295,6 +384,39 @@
self initContents:10
!
+removeAllSuchThat:aBlock
+ "remove all elements that meet a test criteria as specified in aBlock.
+ The argument, aBlock is evaluated for successive elements and all those,
+ for which it returns true, are removed."
+
+ "/ this is a q&d implementation (slow).
+ "/ it should be rewritten to
+
+ |runIndex removed element|
+
+ removed := self species new.
+
+ runIndex := 1.
+ [runIndex <= self size] whileTrue:[
+ element := self at:runIndex.
+ (aBlock value:element) ifTrue:[
+ removed add:element.
+ self removeAtIndex:runIndex
+ ] ifFalse:[
+ runIndex := runIndex + 1
+ ]
+ ].
+ ^ removed
+
+ "
+ |coll|
+
+ coll := OrderedCollection withAll:(1 to:10).
+ Transcript showCr:(coll removeAllSuchThat:[:el | el even]).
+ Transcript showCr:coll
+ "
+!
+
add:anObject
"add the argument, anObject to the end of the collection
Return the argument, anObject."
@@ -393,7 +515,7 @@
idx ~~ 0 ifTrue:[
^ self add:newObject beforeIndex:(idx + 1).
].
- self errorNotFound:oldObject
+ self errorValueNotFound:oldObject
"
|c|
@@ -424,7 +546,7 @@
idx ~~ 0 ifTrue:[
^ self add:newObject beforeIndex:idx.
].
- self errorNotFound:oldObject
+ self errorValueNotFound:oldObject
"
|c|
@@ -572,7 +694,7 @@
"return the element, after anObject.
If anObject is not in the receiver, report an error."
- ^ self after:anObject ifAbsent:[self errorNotFound:anObject]
+ ^ self after:anObject ifAbsent:[self errorValueNotFound:anObject]
"
#(4 3 2 1) asOrderedCollection after:3.
@@ -605,7 +727,7 @@
"return the element before the argument, anObject.
If anObject is not in the receiver, report an error."
- ^ self before:anObject ifAbsent:[self errorNotFound:anObject]
+ ^ self before:anObject ifAbsent:[self errorValueNotFound:anObject]
"
#(4 3 2 1) asOrderedCollection before:3.