SequenceableCollection.st
branchjv
changeset 18011 deb0c3355881
parent 17966 8b5df02e171f
parent 14621 7f004e0e8998
child 18017 7fef9e17913f
--- a/SequenceableCollection.st	Thu Dec 20 11:48:59 2012 +0000
+++ b/SequenceableCollection.st	Sat Jan 19 01:30:00 2013 +0000
@@ -346,18 +346,6 @@
     "Modified: / 09-01-2011 / 10:37:35 / cg"
 !
 
-writeStreamClass
-    "the type of stream used in writeStream"
-
-    ^ WriteStream
-
-    "
-     OrderedCollection writeStreamClass
-    "
-
-    "Created: / 09-01-2011 / 10:37:15 / cg"
-!
-
 writeStreamWithInitialSize:l
     "create a write-stream on an instance of the receiver-class"
 
@@ -474,7 +462,7 @@
 
 atWrap:index
     "Return the index'th element of me if possible.
-     Wrap the index modulu the receivers size if it is out of bounds."
+     Wrap the index modulu the receiver's size if it is out of bounds."
 
     ^ self at:((index - 1) \\ (self size)) + 1
 
@@ -1351,8 +1339,8 @@
 !SequenceableCollection methodsFor:'combinatoric'!
 
 combinationsDo: aBlock
-    "Repeatly evaluate aBlock with all combinations of elements from the receivers elements. 
-     The receivers elements must be collections of the individuals to be taken for the combinations"
+    "Repeatly evaluate aBlock with all combinations of elements from the receiver's elements. 
+     The receiver's elements must be collections of the individuals to be taken for the combinations"
 
     self combinationsStartingAt:1 prefix:#() do:aBlock
 
@@ -1859,7 +1847,7 @@
 !
 
 endsWith:aCollection
-    "return true, if the receivers last elements match those
+    "return true, if the receiver's last elements match those
      of aCollection"
 
     |index1 "{ Class: SmallInteger }"
@@ -1877,9 +1865,9 @@
     index1 := sz.
     index2 := stop.
     [index2 > 0] whileTrue:[
-	(self at:index1) = (aCollection at:index2) ifFalse:[^ false].
-	index1 := index1 - 1.
-	index2 := index2 - 1
+        (self at:index1) = (aCollection at:index2) ifFalse:[^ false].
+        index1 := index1 - 1.
+        index2 := index2 - 1
     ].
     ^ true
 
@@ -2078,7 +2066,7 @@
 !
 
 startsWith:aCollection
-    "return true, if the receivers first elements match those
+    "return true, if the receiver's first elements match those
      of aCollection
      If the argument is empty, true is returned."
 
@@ -2093,8 +2081,8 @@
 
     index := 1.
     [index <= stop] whileTrue:[
-	(self at:index) = (aCollection at:index) ifFalse:[^false].
-	index := index + 1
+        (self at:index) = (aCollection at:index) ifFalse:[^false].
+        index := index + 1
     ].
     ^ true
 
@@ -2153,7 +2141,7 @@
 
 asCollectionOfSubCollectionsOfSize:pieceSize
     "return a collection containing pieces of size pieceSite from the receiver.
-     The last piece may be smaller, if the receivers size is not a multiple of pieceSize."
+     The last piece may be smaller, if the receiver's size is not a multiple of pieceSize."
 
     |pieces
      start  "{ Class:SmallInteger }"
@@ -3047,7 +3035,7 @@
 
 from:startIndex to:endIndex by:step
     "Create a ReindexedCollection from the receiver.
-     The new collection represents the receivers elements up to endIndex.
+     The new collection represents the receiver's elements up to endIndex.
      (i.e. logically it represents the receiver copyTo:endIndex,
      however, physically, no copy is made).
      Warning:
@@ -3071,7 +3059,7 @@
 
 to:endIndex
     "Create a ReindexedCollection from the receiver.
-     The new collection represents the receivers elements up to endIndex.
+     The new collection represents the receiver's elements up to endIndex.
      (i.e. logically it represents the receiver copyTo:endIndex,
      however, physically, no copy is made).
      Warning:
@@ -3094,7 +3082,7 @@
 
 to:endIndex by:step
     "Create a ReindexedCollection from the receiver.
-     The new collection represents the receivers elements up to endIndex.
+     The new collection represents the receiver's elements up to endIndex.
      (i.e. logically it represents the receiver copyTo:endIndex,
      however, physically, no copy is made).
      Warning:
@@ -3123,7 +3111,7 @@
     "return a new collection formed from concatenating the receiver with
      the argument. The class of the new collection is determined by the
      receivers class, so mixing classes is possible, if the second collections
-     elements can be stored into instances of the receivers class.
+     elements can be stored into instances of the receiver's class.
      This is just syntactic sugar for javascript"
 
     ^ self , aCollection
@@ -3189,7 +3177,7 @@
 !
 
 copyButFirst:count
-    "return a new collection consisting of the receivers elements
+    "return a new collection consisting of the receiver's elements
      except for the first count elements - for convenience."
 
     ^ self copyFrom:(count + 1)
@@ -3201,7 +3189,7 @@
 !
 
 copyButLast:count
-    "return a new collection consisting of the receivers elements
+    "return a new collection consisting of the receiver's elements
      except for the last count elements - for convenience."
 
     ^ self copyTo:(self size - count)
@@ -3213,7 +3201,7 @@
 !
 
 copyFirst:count
-    "return a new collection consisting of the receivers first count
+    "return a new collection consisting of the receiver's first count
      elements - this is just a rename of copyTo: - for compatibility."
 
     ^ self copyFrom:1 to:count
@@ -3225,7 +3213,7 @@
 !
 
 copyFrom:startIndex
-    "return a new collection consisting of receivers elements from startIndex to the end of the collection.
+    "return a new collection consisting of receiver's elements from startIndex to the end of the collection.
      Return an empty collection, if startIndex is beyond the receivers size."
 
     ^ self copyFrom:startIndex to:(self size)
@@ -3241,12 +3229,12 @@
     "return a new collection consisting of numberOfElements elements
      extracted from the receiver starting at index start
      (i.e. extract a slice).
-     Returns an empty collection if startIndex is beyond the receivers size.
+     Returns an empty collection if startIndex is beyond the receiver's size.
      Raise an error, if stopIndex is out of range."
 
     ^ self
-	copyFrom:startIndex
-	to:(startIndex + numberOfElements - 1)
+        copyFrom:startIndex
+        to:(startIndex + numberOfElements - 1)
 
     "
      #($a $b $c $d $e $f $g) copyFrom:2 count:3
@@ -3257,16 +3245,16 @@
 !
 
 copyFrom:startIndex to:stopIndex
-    "return a new collection consisting of receivers elements
+    "return a new collection consisting of receiver's elements
      between start and stop.
-     Returns an empty collection if startIndex is beyond the receivers size.
+     Returns an empty collection if startIndex is beyond the receiver's size.
      Raise an error, if stopIndex is out of range."
 
     |newCollection newSize|
 
     newSize := stopIndex - startIndex + 1.
     newSize < 0 ifTrue:[
-	newSize := 0
+        newSize := 0
     ].
     newCollection := self copyEmptyAndGrow:newSize.
     newCollection replaceFrom:1 to:newSize with:self startingAt:startIndex.
@@ -3283,8 +3271,7 @@
 !
 
 copyLast:count
-    "return a new collection consisting of the receivers last count
-     elements."
+    "return a new collection consisting of the receiver's last count elements."
 
     |sz|
 
@@ -3458,7 +3445,7 @@
 !
 
 copyTo:stopIndex
-    "Return a new collection consisting of receivers elements from 1 up to (including) stopIndex.
+    "Return a new collection consisting of receiver's elements from 1 up to (including) stopIndex.
      Raise an error, if stopIndex is out of range."
 
     ^ self copyFrom:1 to:stopIndex
@@ -3470,7 +3457,7 @@
 !
 
 copyToMax:stop
-    "return a new collection consisting of receivers elements
+    "return a new collection consisting of receiver's elements
      from 1 up to (including) index stop, or up to the receivers end,
      whichever is smaller (i.e. like copyTo:, but do not err if receiver is smaller"
 
@@ -3720,7 +3707,7 @@
 !
 
 copyWith:newElement
-    "return a new collection containing the receivers elements
+    "return a new collection containing the receiver's elements
      and the single new element, newElement.
      This is different from concatentation, which expects another collection
      as argument, but equivalent to copy-and-addLast."
@@ -3743,7 +3730,7 @@
 !
 
 copyWithFirst:newFirstElement
-    "return a new collection containing the receivers elements
+    "return a new collection containing the receiver's elements
      and the single new element, newElement up front.
      This is different from concatentation, which expects two collections
      as argument, but equivalent to copy-and-addFirst."
@@ -3819,7 +3806,7 @@
 !
 
 copyWithoutFirst:elementToSkip
-    "return a new collection consisting of a copy of the receivers elements
+    "return a new collection consisting of a copy of the receiver's elements
      without the first elementToSkip, if it was present.
      No error is reported, if elementToSkip is not in the collection.
      Do not confuse this with copyButFirst:"
@@ -3890,7 +3877,7 @@
 !
 
 copyWithoutIndex:omitIndex
-    "return a new collection consisting of receivers elements
+    "return a new collection consisting of receiver's elements
      without the argument stored at omitIndex"
 
     |copy sz|
@@ -3908,7 +3895,7 @@
 !
 
 copyWithoutLast:count
-    "return a new collection consisting of the receivers elements
+    "return a new collection consisting of the receiver's elements
      except the last count elements.
      That is the same as copyButLast: and badly named (for compatibility),
      because the name may confuse users with the copyWithoutFirst: functionality.
@@ -3945,7 +3932,7 @@
 !
 
 restAfter:anElement
-    "return a new collection consisting of the receivers elements after
+    "return a new collection consisting of the receiver's elements after
      (but excluding) anElement.
      If anElement is not in the receiver, the returned collection
      will be empty.
@@ -4002,7 +3989,7 @@
 !
 
 upTo:anElement
-    "return a new collection consisting of the receivers elements upTo
+    "return a new collection consisting of the receiver's elements upTo
      (but excluding) anElement.
      If anElement is not in the receiver, the returned collection
      will consist of all elements of the receiver.
@@ -4036,7 +4023,7 @@
 !
 
 upTo:anElement count:count
-    "return a new collection consisting of the receivers elements upTo
+    "return a new collection consisting of the receiver's elements upTo
      (but excluding) count found anElements, i.e. the first found count-1
      elements are included in the returned collection; if count < 0 the
      procedure is done from the end of the collection backwards.
@@ -4046,7 +4033,7 @@
     |startPos endPos pos found|
 
     (count == 0 or: [count == 1]) ifTrue:[
-	^ self upTo:anElement
+        ^ self upTo:anElement
     ].
 
     startPos := 1.
@@ -4054,21 +4041,21 @@
     found    := 0.
 
     count > 1 ifTrue:[
-	pos := 0.
-	[pos < self size and: [found < count]]
-	    whileTrue: [pos := pos + 1. (self at: pos) == anElement ifTrue: [found := found + 1]].
-	found == count ifTrue:[
-	    endPos   := pos - 1
-	]
+        pos := 0.
+        [pos < self size and: [found < count]]
+            whileTrue: [pos := pos + 1. (self at: pos) == anElement ifTrue: [found := found + 1]].
+        found == count ifTrue:[
+            endPos   := pos - 1
+        ]
     ].
 
     count < 0 ifTrue:[
-	pos := self size + 1.
-	[pos > 1 and: [found < count abs]]
-	    whileTrue: [pos := pos - 1. (self at: pos) == anElement ifTrue: [found := found + 1]].
-	found == count abs ifTrue:[
-	    startPos := pos + 1
-	]
+        pos := self size + 1.
+        [pos > 1 and: [found < count abs]]
+            whileTrue: [pos := pos - 1. (self at: pos) == anElement ifTrue: [found := found + 1]].
+        found == count abs ifTrue:[
+            startPos := pos + 1
+        ]
     ].
 
     ^ self copyFrom: startPos to: endPos
@@ -4083,7 +4070,7 @@
 !
 
 upToAny:aCollectionOfObjects
-    "return a new collection consisting of the receivers elements upTo
+    "return a new collection consisting of the receiver's elements upTo
      (but excluding) any in aCollectionOfObjects.
      If none of aCollectionOfObjects is found in the receiver, the returned collection
      will consist of all elements of the receiver.
@@ -4104,7 +4091,7 @@
 !
 
 withoutLeadingForWhich:aCheckBlock
-    "return a copy of myself without leading eleents for which aCheckBlock returns true.
+    "return a copy of myself without leading elements for which aCheckBlock returns true.
      Returns an empty collection, if the receiver consist only of matching elements.
      Normally, this is mostly used with string receivers."
 
@@ -5320,33 +5307,38 @@
      dstIndex "{ Class: SmallInteger }"
      end      "{ Class: SmallInteger }" |
 
-    (repStartIndex < startIndex) ifTrue:[
-	" must do reverse copy "
-	srcIndex := repStartIndex + (stopIndex - startIndex).
-	dstIndex := stopIndex.
-	end := startIndex.
-	[dstIndex >= end] whileTrue:[
-	    self at:dstIndex put:(replacementCollection at:srcIndex).
-	    srcIndex := srcIndex - 1.
-	    dstIndex := dstIndex - 1
-	].
-	^ self
+    replacementCollection == self ifTrue:[
+        repStartIndex == startIndex ifTrue:[ "noting to copy" ^ self ].
+
+        "beware the overlapping copy"
+        (repStartIndex < startIndex) ifTrue:[
+            " must do reverse copy "
+            srcIndex := repStartIndex + (stopIndex - startIndex).
+            dstIndex := stopIndex.
+            end := startIndex.
+            [dstIndex >= end] whileTrue:[
+                self at:dstIndex put:(replacementCollection at:srcIndex).
+                srcIndex := srcIndex - 1.
+                dstIndex := dstIndex - 1
+            ].
+            ^ self
+        ].
     ].
 
     srcIndex := repStartIndex.
     dstIndex := startIndex.
     end := stopIndex.
     [dstIndex <= end] whileTrue:[
-	self at:dstIndex put:(replacementCollection at:srcIndex).
-	srcIndex := srcIndex + 1.
-	dstIndex := dstIndex + 1
+        self at:dstIndex put:(replacementCollection at:srcIndex).
+        srcIndex := srcIndex + 1.
+        dstIndex := dstIndex + 1
     ]
 
     "
      args:    startIndex            : <integer>
-	      stopIndex             : <integer>
-	      replacementCollection : <colleciton of <object> >
-	      repStartIndex         : <integer>
+              stopIndex             : <integer>
+              replacementCollection : <colleciton of <object> >
+              repStartIndex         : <integer>
 
      returns: self
     "
@@ -5365,7 +5357,7 @@
      c replaceFrom:4 to:5 with:c startingAt:2
     "
 
-    "Modified: / 20.5.1998 / 15:23:21 / cg"
+    "Modified: / 08-05-2012 / 13:23:51 / cg"
 !
 
 replaceFrom:startIndex with:replacementCollection
@@ -5427,6 +5419,39 @@
     "Modified: / 20.5.1998 / 15:25:18 / cg"
 !
 
+replaceLast:count with:replacementCollection
+    "replace the last count elements elements in the receiver
+     with elements taken from replacementCollection.
+     Return the receiver.
+
+     Notice: This operation modifies the receiver, NOT a copy;
+     therefore the change may affect all others referencing the receiver."
+
+    self replaceLast:count with:replacementCollection startingAt:1
+
+    "
+     '1234567890' replaceLast:5 with:'abcdef'
+    "
+!
+
+replaceLast:count with:replacementCollection startingAt:repStartIndex
+    "replace the last count elements elements in the receiver
+     with elements taken from replacementCollection starting at repStartIndex.
+     Return the receiver.
+
+     Notice: This operation modifies the receiver, NOT a copy;
+     therefore the change may affect all others referencing the receiver."
+
+    |sz|
+
+    sz := self size.
+    self replaceFrom:(sz - count + 1) to:sz with:replacementCollection startingAt:repStartIndex
+
+    "
+     '1234567890' replaceLast:5 with:'abcdef' startingAt:1
+    "
+!
+
 startingAt:sourceStart replaceElementsIn:destColl from:destStartIndex to:destEndIndex
     "replace elements in destColl with elements from the receiver.
 
@@ -5691,16 +5716,16 @@
     "return a new collection of length size, which contains the receiver
      right-adjusted (i.e. padded on the left).
      Elements on the left are filled with padElement.
-     If the receivers size is equal or greater than the length argument,
+     If the receiver's size is equal or greater than the length argument,
      the original receiver is returned unchanged."
 
     |len s|
 
     len := self size.
     (len < size) ifTrue:[
-	s := self species new:size withAll:padElement.
-	s replaceFrom:(size - len + 1) with:self.
-	^ s
+        s := self species new:size withAll:padElement.
+        s replaceFrom:(size - len + 1) with:self.
+        ^ s
     ]
 
     "
@@ -5720,16 +5745,16 @@
 paddedTo:newSize with:padElement
     "return a new collection consisting of the receivers elements,
      plus pad elements up to length.
-     If the receivers size is equal or greater than the length argument,
+     If the receiver's size is equal or greater than the length argument,
      the original receiver is returned unchanged."
 
     |s len|
 
     len := self size.
     len < newSize ifTrue:[
-	s := self species new:newSize withAll:padElement.
-	s replaceFrom:1 to:len with:self.
-	^ s
+        s := self species new:newSize withAll:padElement.
+        s replaceFrom:1 to:len with:self.
+        ^ s
     ]
 
     "
@@ -8708,15 +8733,12 @@
 !SequenceableCollection class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libbasic/SequenceableCollection.st,v 1.325 2012/08/23 13:25:43 cg Exp $'
+    ^ '$Header: /cvs/stx/stx/libbasic/SequenceableCollection.st,v 1.328 2013-01-01 11:30:33 cg Exp $'
 !
 
 version_CVS
-    ^ '§Header: /cvs/stx/stx/libbasic/SequenceableCollection.st,v 1.325 2012/08/23 13:25:43 cg Exp §'
-!
-
-version_SVN
-    ^ '$Id: SequenceableCollection.st 10844 2012-09-07 16:24:32Z vranyj1 $'
+    ^ '$Header: /cvs/stx/stx/libbasic/SequenceableCollection.st,v 1.328 2013-01-01 11:30:33 cg Exp $'
 ! !
 
+
 SequenceableCollection initialize!