[Glass] OrderedDictionary

Martin McClure via Glass glass at lists.gemtalksystems.com
Thu Jan 29 16:45:28 PST 2015


On 01/29/2015 04:28 PM, Richard Sargent via Glass wrote:
> 
>> Is there a association based Dictionary in Gemstone that preserves order?
> 
> I've never seen one with associations and order preserving. Do you really
> need both?

Sebastian, I assume you mean a dictionary where values can be looked up
by keys, and enumeration is sorted by key order?

I agree with Richard that Smalltalk does not traditionally offer that
combination in a single collection. But I've also wanted such a thing
occasionally. Fortunately, it's easy to implement your own, made out of
a regular Dictionary (in GemStone, use a KeyValueDictionary instead of a
Dictionary for somewhat better performance) and a SortedCollection of
the keys.

This approach uses more space than the theoretical minimum. Since it
stores two references to each key the space used is proportional to 3N
instead of the minimum of 2N. It would be possible to create a
tree-based dictionary (which some other languages have) that would
perform better, but that would be more work. The simple approach is
likely to work well for you, unless your dictionaries will be very large
(millions or more).

Regards,

-Martin


More information about the Glass mailing list