SeqColl.st
changeset 32 ee1a621c696c
parent 13 62303f84ff5f
child 42 e33491f6f260
--- a/SeqColl.st	Sat Jan 08 17:18:40 1994 +0100
+++ b/SeqColl.st	Sat Jan 08 17:24:16 1994 +0100
@@ -26,7 +26,7 @@
 an index. SequenceableCollection is an abstract class - there are no
 instances of it in the system.
 
-$Header: /cvs/stx/stx/libbasic/Attic/SeqColl.st,v 1.5 1993-12-11 00:56:02 claus Exp $
+$Header: /cvs/stx/stx/libbasic/Attic/SeqColl.st,v 1.6 1994-01-08 16:23:06 claus Exp $
 
 written spring 89 by claus
 '!
@@ -56,6 +56,18 @@
     "return the last element"
 
     ^ self at:(self size)
+!
+
+at:index ifAbsent:exceptionBlock
+    "return the element at index if valid. If the index is invalid,
+     return the result of evaluating the exceptionblock"
+
+    ((index < 1) or:[index > self size]) ifTrue:[
+        ^ exceptionBlock value
+    ].
+    ^ self at:index
+
+    "#(1 2 3) at:4 ifAbsent:['no such index']"
 ! !
 
 !SequenceableCollection methodsFor:'queries'!
@@ -286,7 +298,7 @@
 atAllPut:anObject
     "replace all elements of the collection by the argument, anObject"
 
-    self from:(self firstIndex) to:(self lastIndex) put:anObject
+    self from:1 to:(self size) put:anObject
 !
 
 atAll:indexCollection put:anObject
@@ -301,7 +313,7 @@
 replaceAll:oldObject by:newObject
     "replace all oldObjects by newObject in the receiver"
 
-    self firstIndex to:self lastIndex do:[:index |
+    1 to:self size do:[:index |
         (self at:index) = oldObject ifTrue:[
             self at:index put:newObject
         ]
@@ -461,18 +473,15 @@
 
     reimplemented here for speed"
 
-    |index "{ Class: SmallInteger }"
-     stop  "{ Class: SmallInteger }"
+    |stop  "{ Class: SmallInteger }"
      element|
 
-    stop := self lastIndex.
-    index := self firstIndex.
-    [index <= stop] whileTrue:[
+    stop := self size.
+    1 to:stop do:[:index |
         element := self at:index.
         (aBlock value:element) ifTrue:[
             ^ element
         ].
-        index := index + 1
     ].
     ^ exceptionBlock value
 !
@@ -482,7 +491,7 @@
      if found, return the index otherwise return 0.
      The comparison is done using = (i.e. equality test)."
 
-    ^ self indexOf:anElement startingAt:self firstIndex
+    ^ self indexOf:anElement startingAt:1
 !
 
 indexOf:anElement ifAbsent:exceptionBlock
@@ -503,14 +512,13 @@
      if found, return the index otherwise return 0.
      The comparison is done using = (i.e. equality test)."
 
-    |index "{ Class: SmallInteger }"
-     stop  "{ Class: SmallInteger }" |
+    |startIndex "{ Class: SmallInteger }"
+     stop       "{ Class: SmallInteger }" |
 
-    index := start.
-    stop := self lastIndex.
-    [index <= stop] whileTrue:[
+    startIndex := start.
+    stop := self size.
+    startIndex to:stop do:[:index |
         anElement = (self at:index) ifTrue:[^ index].
-        index := index + 1
     ].
     ^ 0
 !
@@ -532,7 +540,7 @@
     "search the collection for anElement using identity compare (i.e. ==);
      if found, return the index otherwise return 0."
 
-    ^ self identityIndexOf:anElement startingAt:self firstIndex
+    ^ self identityIndexOf:anElement startingAt:1
 !
 
 identityIndexOf:anElement ifAbsent:exceptionBlock
@@ -542,7 +550,7 @@
 
     |index|
 
-    index := self identityIndexOf:anElement startingAt:self firstIndex.
+    index := self identityIndexOf:anElement startingAt:1.
     (index == 0) ifTrue:[^ exceptionBlock value].
     ^ index
 !
@@ -552,14 +560,13 @@
      using identity compare  (i.e. ==);
      if found, return the index otherwise return 0."
 
-    |index "{ Class: SmallInteger }"
-     stop  "{ Class: SmallInteger }" |
+    |startIndex "{ Class: SmallInteger }"
+     stop       "{ Class: SmallInteger }" |
 
-    index := start.
-    stop := self lastIndex.
-    [index <= stop] whileTrue:[
+    startIndex := start.
+    stop := self size.
+    startIndex to:stop do:[:index |
         anElement == (self at:index) ifTrue:[^ index].
-        index := index + 1
     ].
     ^ 0
 !
@@ -581,14 +588,11 @@
     "find the first element, for which evaluation of the argument, aBlock
      return true; return its index or 0 if none detected."
 
-    |index "{ Class: SmallInteger }"
-     stop  "{ Class: SmallInteger }" |
+    |stop  "{ Class: SmallInteger }" |
 
-    stop := self lastIndex.
-    index := self firstIndex.
-    [index <= stop] whileTrue:[
+    stop := self size.
+    1 to:stop do:[:index |
         (aBlock value:(self at:index)) ifTrue:[^ index].
-        index := index + 1
     ].
     ^ 0
 
@@ -601,14 +605,11 @@
     "find the last element, for which evaluation of the argument, aBlock
      return true; return its index or 0 if none detected."
 
-    |index "{ Class: SmallInteger }"
-     stop  "{ Class: SmallInteger }" |
+    |start "{ Class: SmallInteger }"|
 
-    index := self lastIndex.
-    stop := self firstIndex.
-    [index >= stop] whileTrue:[
+    start := self size.
+    start to:1 by:-1 do:[:index |
         (aBlock value:(self at:index)) ifTrue:[^ index].
-        index := index - 1
     ].
     ^ 0
 
@@ -620,7 +621,7 @@
     "return true if the collection contains anElement; false otherwise.
      Comparison is done using equality compare (i.e. =)."
 
-    ((self indexOf:anElement startingAt:self firstIndex) == 0) ifTrue:[^ false].
+    ((self indexOf:anElement startingAt:1) == 0) ifTrue:[^ false].
     ^ true
 ! !
 
@@ -633,8 +634,8 @@
      hiIndex  "{ Class: SmallInteger }"
      t|
 
-    hiIndex := self lastIndex.
-    lowIndex := self firstIndex.
+    hiIndex := self size.
+    lowIndex := 1.
     [lowIndex < hiIndex] whileTrue:[
         t := self at:lowIndex.
         self at:lowIndex put:(self at:hiIndex). 
@@ -778,8 +779,8 @@
      end    "{ Class: SmallInteger }"
      smallest smallestIndex thisOne|
 
-    end := self lastIndex.
-    index := self firstIndex.
+    end := self size.
+    index := 1.
     [index <= end] whileTrue:[
         smallest := self at:index.
         smallestIndex := index.
@@ -806,12 +807,11 @@
     "sort the collection inplace. The elements are compared using
      > and < i.e. they should offer a magnitude-like protocol."
 
-    |start stop|
+    |stop|
 
-    stop := self lastIndex.
-    start := self firstIndex.
-    (stop > start) ifTrue:[
-        self quickSortFrom:start to:stop
+    stop := self size.
+    (stop > 1) ifTrue:[
+        self quickSortFrom:1 to:stop
     ]
 
     "#(1 16 7 98 3 19 4 0) sort"
@@ -821,12 +821,11 @@
     "sort the receiver collection inplace, also sort aCollection with it.
      Use, when you have a key collection to sort another collection with."
 
-    |start stop|
+    |stop|
 
-    stop := self lastIndex.
-    start := self firstIndex.
-    (stop > start) ifTrue:[
-        self quickSortFrom:start to:stop with:aCollection
+    stop := self size.
+    (stop > 1) ifTrue:[
+        self quickSortFrom:1 to:stop with:aCollection
     ]
 
     "|c1 c2|
@@ -841,12 +840,11 @@
     "sort the collection inplace using the 2-arg block sortBlock
      for comparison. This allows any sort criteria to be implemented."
 
-    |start stop|
+    |stop|
 
-    stop := self lastIndex.
-    start := self firstIndex.
-    (stop > start) ifTrue:[
-        self quickSortFrom:start to:stop sortBlock:sortBlock
+    stop := self size.
+    (stop > 1) ifTrue:[
+        self quickSortFrom:1 to:stop sortBlock:sortBlock
     ]
 
     "#(1 16 7 98 3 19 4 0) sort:[:a :b | a < b]"
@@ -857,12 +855,11 @@
     "sort the collection inplace using the 2-arg block sortBlock
      for comparison. Also reorder the elements in aCollection"
 
-    |start stop|
+    |stop|
 
-    stop := self lastIndex.
-    start := self firstIndex.
-    (stop > start) ifTrue:[
-        self quickSortFrom:start to:stop sortBlock:sortBlock with:aCollection
+    stop := self size.
+    (stop > 1) ifTrue:[
+        self quickSortFrom:1 to:stop sortBlock:sortBlock with:aCollection
     ]
 
     "|c1 c2|
@@ -878,14 +875,11 @@
 do:aBlock
     "evaluate the argument, aBlock for every element in the collection."
 
-    |index  "{ Class:SmallInteger }"
-     length "{ Class:SmallInteger }"|
+    |stop "{ Class:SmallInteger }"|
 
-    index := self firstIndex.
-    length := self lastIndex.
-    [index <= length] whileTrue:[
+    stop := self size.
+    1 to:stop do:[:index |
         aBlock value:(self at:index).
-        index := index + 1
     ]
 !
 
@@ -893,14 +887,11 @@
     "evaluate the argument, aBlock for every element in the collection,
      passing both index and element as arguments."
 
-    |index  "{ Class:SmallInteger }"
-     length "{ Class:SmallInteger }"|
+    |stop  "{ Class:SmallInteger }"|
 
-    index := self firstIndex.
-    length := self lastIndex.
-    [index <= length] whileTrue:[
+    stop := self size.
+    1 to:stop do:[:index |
         aTwoArgBlock value:index value:(self at:index).
-        index := index + 1
     ]
 !
 
@@ -908,30 +899,26 @@
     "evaluate the argument, aBlock for the elements with index index1 to
      index2 in the collection"
 
-    |index "{ Class:SmallInteger }"
+    |start "{ Class:SmallInteger }"
      stop  "{ Class:SmallInteger }" |
 
-    index := index1.
+    start := index1.
     stop := index2.
-    [index <= stop] whileTrue:[
+    start to:stop do:[:index |
         aBlock value:(self at:index).
-        index := index + 1
     ]
 !
 
-with:aCollection do:aBlock
+with:aCollection do:aTwoArgBlock
     "evaluate the argument, aBlock for successive elements from
      each of the two collections self and aCollection.
      aBlock must be a two-argument block"
 
-    |index "{ Class: SmallInteger }"
-     stop  "{ Class: SmallInteger }" |
+    |stop  "{ Class: SmallInteger }" |
 
-    index := self firstIndex.
-    stop := self lastIndex.
-    [index <= stop] whileTrue:[
-        aBlock value:(self at:index) value:(aCollection at:index).
-        index := index + 1
+    stop := self size.
+    1 to:stop do:[:index |
+        aTwoArgBlock value:(self at:index) value:(aCollection at:index).
     ]
 !
 
@@ -939,14 +926,11 @@
     "evaluate the argument, aBlock for every element in the collection
      in reverse order"
 
-    |index "{ Class:SmallInteger }" 
-     stop  "{ Class:SmallInteger }"|
+    |sz  "{ Class:SmallInteger }"|
 
-    index := self lastIndex.
-    stop := self firstIndex.
-    [index >= stop] whileTrue:[
+    sz := self size.
+    sz to:1 by:-1 do:[:index |
         aBlock value:(self at:index).
-        index := index - 1
     ]
 !
 
@@ -955,15 +939,12 @@
      and return a collection of the results"
 
     |newCollection
-     index  "{ Class:SmallInteger }"
-     length "{ Class:SmallInteger }" |
+     sz  "{ Class:SmallInteger }"|
 
-    length := self lastIndex.
-    newCollection := self species new:length.
-    index := self firstIndex.
-    [index <= length] whileTrue:[
+    sz := self size.
+    newCollection := self species new:sz.
+    1 to:sz do:[:index |
         newCollection at:index put:(aBlock value:(self at:index)).
-        index := index + 1
     ].
     ^ newCollection
 !
@@ -974,18 +955,15 @@
      true"
 
     |element newColl
-     index  "{ Class:SmallInteger }"
-     length "{ Class:SmallInteger }" |
+     sz  "{ Class:SmallInteger }"|
 
-    length := self lastIndex.
-    newColl := OrderedCollection new:length.
-    index := self firstIndex.
-    [index <= length] whileTrue:[
+    sz := self size.
+    newColl := OrderedCollection new:sz.
+    1 to:sz do:[:index |
         element := self at:index.
         (aBlock value:element) ifTrue:[
             newColl add:element
         ].
-        index := index + 1
     ].
     ^ newColl
 ! !