LookupTable vs Dictionary

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

LookupTable vs Dictionary

Fernando-2
Hi,

I don't understand very well the difference between both and when
should either be used preferently. Can anybody help?
Thanks!


Reply | Threaded
Open this post in threaded view
|

Re: LookupTable vs Dictionary

Ian Bartholomew-19
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.


Reply | Threaded
Open this post in threaded view
|

Re: LookupTable vs Dictionary

Schwab,Wilhelm K
Ian,

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

I've hit that too.  IMHO, it's an avoidable defect in Dictionary.  It
uses nil as a cookie, forcing "the rest of us" (I hope the * for Dummies
guys don't come after me for that<g>) to create and use a cookie when we
mean nil.

Have a good one,

Bill


--
Wilhelm K. Schwab, Ph.D.
[hidden email]


Reply | Threaded
Open this post in threaded view
|

Re: LookupTable vs Dictionary

Warren Stanley
In reply to this post by Fernando-2
Fernando,

A Dictionary is just a collection of Associations.
A LookupTable has a collection of keys and a collection of values.

 From this, the first difference is that a LookupTable can not have nil
a  key value, a Dictionary can.  This is because an empty key value in a
LookupTables key array is nil, so you can't tell the difference between
an unused slot and one keyed by nil.  You should get an error if you try
LookupTable new at: nil put: 1.

The next major difference is in copying.  A copy of a dictionary will
share the associations, so changing the value (via #at:put: or directly
manipulating the association) will affect the original and the copy.  A
LookupTable does not have any associations so the copy is more like you
would expect.

Otherwise, I am not aware of any major differences between the two.

This should apply for all Smalltalks, but if it doesn't, this is how it
is in VAST.

Warren.

Fernando wrote:
> Hi,
>
> I don't understand very well the difference between both and when
> should either be used preferently. Can anybody help?
> Thanks!
>


--


Warren Stanley
Technical Consultant
R&D team
Application Solutions

Wizard Information Services


To send e-mail, use my name with a dot in the middle,
and the domain is wizardis dot com dot au


Reply | Threaded
Open this post in threaded view
|

Re: LookupTable vs Dictionary

OCIT
In reply to this post by Fernando-2
No such animal in VisualWorks.

-Charles

Fernando wrote:
> Hi,
>
> I don't understand very well the difference between both and when
> should either be used preferently. Can anybody help?
> Thanks!