Login  Register

Re: LookupTable vs Dictionary

Posted by Ian Bartholomew-19 on Feb 12, 2005; 5:24pm
URL: https://forum.world.st/LookupTable-vs-Dictionary-tp3373025p3373026.html

Fernando,

> I don't understand very well the difference between both

The difference is in the implementation, their interface is more or less
identical.

Dictionary stores it's entries as a collection of Associations.

LookupTable stores it's entries in two parallel Arrays, one for the keys
and one for the values.

>  and when
> should either be used preferently. Can anybody help?

As far as I recall there's not a great difference in speed, although
that might not hold for large instances.

Dictionaries have to create more objects, which might make a small
difference in GC times.

If you want to add/remove associations from the table, and maintain each
ones identity, then Dictionary is the one to use.

d := Dictionary new.
a := Association key: 1 value: 2.
d add: a.
(d associationAt: a key) == a    answers true

d := LookupTable new.
a := Association key: 1 value: 2.
d add: a.
(d associationAt: a key) == a     answers  false

Dictionaries can get a bit confused about nil (which can occasionally
cause a problem).

d := Dictionary new.
d add: (nil -> 1).
d at: nil    answers 1
d at: nil put: 2    throws an error

FWIW, I tend to use LookupTables unless there is a pressing reason not to.

--
Ian

Use the Reply-To address to contact me.
Mail sent to the From address is ignored.