[BUG] OrderedCollection>>insert:before:
Boris Gaertner
Boris.Gaertner at gmx.net
Thu Jan 8 20:29:13 UTC 2004
Alain Plantec <alain.plantec at univ-brest.fr> wrote:
> Hi all,
>
> Squeak 3.6
> Latest update: #5429
>
>
> With one insert, it's OK :
> | l |
> l := #(1 2 3 4) asOrderedCollection.
> l insert: 88 before: 1.
> l.
> an OrderedCollection(88 1 2 3 4) (OK)
>
> With two, KO
> | l |
> l := #(1 2 3 4) asOrderedCollection.
> l insert: 88 before: 1.
> l insert: 99 before: 2.
> l.
> an OrderedCollection(nil 88 1 2 3 4) (KO)
>
When you inspect l, you see that the 99 is
innserted into the instance variable array, but
at position 2, which is not within the range
firstIndex .. lastindex.
I confess, that on first reading, I thought that this is
a serious bug. Now I think it is not:
#insert:before: is a *private* method. It is called
by other methods of OrderedCollection that compute
an internal insertion index, which
is an integer in the range firstIndex .. lastIndex.
( The external indices run from 1 .. collection size)
So, I think that #insert:before: should not be used
in the way you used it. The public methods are:
#add:before:
#add:after:
#add:afterIndex:
Here are your examples rewritten for #add:before:
| l |
l := #(1 2 3 4) asOrderedCollection.
l add: 88 before: 1.
l. an OrderedCollection(88 1 2 3 4)
an OrderedCollection(88 1 2 3 4)
| l |
l := #(1 2 3 4) asOrderedCollection.
l add: 88 before: 1.
l add: 99 before: 2.
l.
an OrderedCollection(88 1 99 2 3 4)
--------------------------
Note that 1, 2 are *not* indices, but objects. You
can also write:
| l |
l := #('Jim' 'Mary' 'John' 'Andrew' ) asOrderedCollection.
l add: 'James' before: 'Jim'.
l.
an OrderedCollection('James' 'Jim' 'Mary' 'John' 'Andrew')
| l |
l := #('Jim' 'Mary' 'John' 'Andrew' ) asOrderedCollection.
l add: 'James' before: 'Jim'.
l add: 'Margaret' before: 'Andrew'.
l.
an OrderedCollection('James' 'Jim' 'Mary' 'John' 'Margaret' 'Andrew')
To add after a position, you use #add:afterIndex:
| l |
l := #('Jim' 'Mary' 'John' 'Andrew' ) asOrderedCollection.
l add: 'James' afterIndex: 2.
l. an OrderedCollection('Jim' 'Mary' 'James' 'John' 'Andrew')
A method #add:beforeIndex: is not currently defined.
It should read:
add: newObject beforeIndex: index
"Add the argument, newObject, as an element of the receiver. Put it in
the sequence just after index. Answer newObject."
self insert: newObject before: firstIndex + index - 1.
^ newObject
Is this a helpful answer?
Greetings, Boris
More information about the Squeak-dev
mailing list
|