indexing
Richard A. O'Keefe
ok at cs.otago.ac.nz
Wed Jun 12 00:36:08 UTC 2002
Niko Schwarz <niko.schwarz at gmx.net> wrote:
Is there a way to index OrderedCollections beginning at 0?
I can think of several approaches, starting with defining your own
version of OrderedCollection.
While I personally like to start indices at 0, since every existing
"array-like" collection in Smalltalk starts at 1, I think it would
be unwise to introduce something different. I am a bear of very
little brain. I can handle rules like "In C, arrays start at 0.
In Smalltalk, arrays start at 1. In the Algols, each array starts
wherever you want." I have real trouble with rules like "in this
copy of Squeak, most collections start at 1, except for a few that
don't."
Collection subclass: #OriginShiftedSequence
instanceVariables: 'sequence delta'
&c &c
at: index
^sequence at: index + delta
at: index put: element
^sequence at: index + delta put: element
includesKey: index
^index + delta between: 1 and: sequence size
origin
^1 - delta
origin:
delta := 1 - origin
on: aCollection
self assert: [aCollection isSequenceable].
sequence := aCollection
do: aBlock
sequence do: aBlock
"class methods"
on: aCollection origin: origin
^self new on: aCollection; origin: origin; yourself
Now you can do
os0 := OriginShiftedSequence on: anOrderedCollection origin: 0.
If you go this route, you will have to write a lot of "forwarding"
methods. You might like to base it on my EnumerationAdapter class,
which will define many of those methods for you.
More information about the Squeak-dev
mailing list
|