OrderedCollection.st
changeset 42 e33491f6f260
parent 40 a1defe2846d6
child 56 be0ed17e6f85
--- a/OrderedCollection.st	Sun Jan 09 22:25:58 1994 +0100
+++ b/OrderedCollection.st	Wed Jan 12 20:11:58 1994 +0100
@@ -22,7 +22,7 @@
 COPYRIGHT (c) 1989 by Claus Gittinger
               All Rights Reserved
 
-$Header: /cvs/stx/stx/libbasic/OrderedCollection.st,v 1.8 1994-01-09 21:18:57 claus Exp $
+$Header: /cvs/stx/stx/libbasic/OrderedCollection.st,v 1.9 1994-01-12 19:09:43 claus Exp $
 written spring 89 by claus
 '!
 
@@ -120,6 +120,11 @@
 
     |anObject |
 
+    firstIndex > lastIndex ifTrue:[
+        "error if collection is empty"
+        self subscriptBoundsError.
+        ^ nil
+    ].
     anObject := contentsArray at:firstIndex.
     contentsArray at:firstIndex put:nil.
     firstIndex := firstIndex + 1.
@@ -129,6 +134,10 @@
         lastIndex := 0 
     ].
     ^ anObject
+
+    "(OrderedCollection withAll:#(1 2 3 4 5)) removeFirst; yourself"
+    "(SortedCollection withAll:#(5 4 3 2 1)) removeFirst; yourself"
+    "(SortedCollection new) removeFirst"
 !
 
 removeLast
@@ -136,6 +145,11 @@
 
     |anObject |
 
+    firstIndex > lastIndex ifTrue:[
+        "error if collection is empty"
+        self subscriptBoundsError.
+        ^ nil
+    ].
     anObject := contentsArray at:lastIndex.
     contentsArray at:lastIndex put:nil.
     lastIndex := lastIndex - 1.
@@ -145,6 +159,9 @@
         lastIndex := 0 
     ].
     ^ anObject
+
+    "(OrderedCollection withAll:#(1 2 3 4 5)) removeLast; yourself"
+    "(SortedCollection withAll:#(5 4 3 2 1)) removeLast; yourself"
 !
 
 remove:anObject ifAbsent:exceptionBlock
@@ -313,6 +330,32 @@
     ] ifFalse:[
         ^ contentsArray at:idx put:anObject
     ]
+!
+
+first
+    "return the first element"
+
+    firstIndex <= lastIndex ifTrue:[
+        ^ contentsArray at:firstIndex
+    ].
+    "error if collection is empty"
+    self subscriptBoundsError
+
+    "(OrderedCollection withAll:#(1 2 3 4 5)) first"
+    "(SortedCollection withAll:#(5 4 3 2 1)) first"
+!
+
+last
+    "return the last element"
+
+    firstIndex <= lastIndex ifTrue:[
+        ^ contentsArray at:lastIndex
+    ].
+    "error if collection is empty"
+    self subscriptBoundsError
+
+    "(OrderedCollection withAll:#(1 2 3 4 5)) last"
+    "(SortedCollection withAll:#(5 4 3 2 1)) last"
 ! !
 
 !OrderedCollection methodsFor:'filing & replacing'!
@@ -531,6 +574,13 @@
     contentsArray from:firstIndex to:lastIndex do:aBlock
 !
 
+reverseDo:aBlock
+    "evaluate the argument, aBlock for every element in the collection
+     procesing elements in reverse direction (i.e. starting with the last)"
+
+    contentsArray from:firstIndex to:lastIndex reverseDo:aBlock
+!
+
 keysAndValuesDo:aTwoArgBlock
     "evaluate the argument, aBlock for every element in the collection,
      passing both index and element as arguments."