--- a/SortedCollection.st Sat Jan 08 17:18:40 1994 +0100
+++ b/SortedCollection.st Sat Jan 08 17:24:16 1994 +0100
@@ -30,7 +30,7 @@
while [:a :b | a > b] defines descening order.
The default sortBlock for SortedCollections is the first one.
-$Header: /cvs/stx/stx/libbasic/SortedCollection.st,v 1.4 1993-12-11 00:58:06 claus Exp $
+$Header: /cvs/stx/stx/libbasic/SortedCollection.st,v 1.5 1994-01-08 16:23:33 claus Exp $
'!
!SortedCollection class methodsFor:'initialization'!
@@ -138,10 +138,9 @@
"add the argument, anObject at the proper place in the
receiver. Returns the argument, anObject."
- |sz index|
+ |index|
- sz := self size.
- sz == 0 ifTrue:[
+ lastIndex < firstIndex "i.e. self size == 0" ifTrue:[
super add:anObject
] ifFalse:[
index := self findIndexFor:anObject.
@@ -154,7 +153,7 @@
!SortedCollection methodsFor:'converting'!
asSortedCollection
- "retrun the receiver as a sorted collection"
+ "return the receiver as a sorted collection"
"could be an instance of a subclass..."
self class == SortedCollection ifTrue:[
@@ -170,10 +169,11 @@
Redefined, since due to beeing sorted, the inclusion check can
be done with log-n compares i.e. much faster."
- |index|
+ |index "{ Class: SmallInteger }"|
index := self findIndexFor:anObject.
- ^ (index <= self size) and:[(contentsArray at:index) = anObject]
+ ((index < firstIndex) or:[index > lastIndex]) ifTrue:[^ false].
+ ^ (contentsArray at:index) = anObject
"#(7 3 9 10 99) asSortedCollection includes:50"
"#(7 3 9 10 99) asSortedCollection includes:10"
@@ -189,10 +189,10 @@
tally "{ Class: SmallInteger }" |
index := self findIndexFor:anObject.
- mySize := self size.
- index > mySize ifTrue:[^ 0].
+ ((index < firstIndex) or:[index > lastIndex]) ifTrue:[^ 0].
+
tally := 0.
- [(index <= mySize) and:[(contentsArray at:index) = anObject]] whileTrue:[
+ [(index <= lastIndex) and:[(contentsArray at:index) = anObject]] whileTrue:[
tally := tally + 1.
index := index + 1
].
@@ -240,7 +240,8 @@
findIndexFor:anObject
"search the index at which to insert anObject. Can also be used
to search for an existing element by checking if the element at
- the returned index is the one we look for."
+ the returned index is the one we look for.
+ The returned index is a physical one, for accessing contentsArray."
|low "{ Class: SmallInteger}"
high "{ Class: SmallInteger}"
@@ -251,7 +252,7 @@
high := lastIndex.
[low <= high] whileTrue:[
middle := (low + high) // 2.
- element := super at:middle.
+ element := contentsArray at:middle.
(sortBlock value:element value:anObject) ifTrue:[
"middleelement is smaller than object"
low := middle + 1
@@ -262,5 +263,4 @@
^ low
"#(1 2 3 4 7 99 1313 981989 898989898) asSortedCollection findIndexFor:50"
-
! !