i had a look into facets dictionary but that does
only preserve the ordering of insertion which
is not what i want.
The dictionary class in Facets/Calibre is basically the ordered hash
written by jan molic. I've been wanting to improve on it. This looks
like the oppornunity. What is it that you need? I'd be happy to work in
your critera.
is there anywhere a class that does soemthing
akin to java.util.SortedMap? that is sorting
and iterating over the hash in the order of its
keys.
i had a look into facets dictionary but that does
only preserve the ordering of insertion which
is not what i want.
any other suggestions?
ciao robertj
---
first : 42
second : forty-two
-a
--
ara [dot] t [dot] howard [at] noaa [dot] gov
all happiness comes from the desire for others to be happy. all misery
comes from the desire for oneself to be happy.
-- bodhicaryavatara
On Thursday 08 December 2005 12:37 pm, robertj wrote:
> hi,
>
> is there anywhere a class that does soemthing
> akin to java.util.SortedMap? that is sorting
> and iterating over the hash in the order of its
> keys.
When I tried it, it seemed that Ruby automatically sorted the keys. I'm not
sure if this is what you want, but see this:
> is there anywhere a class that does soemthing
> akin to java.util.SortedMap? that is sorting
> and iterating over the hash in the order of its
> keys.
>
> i had a look into facets dictionary but that does
> only preserve the ordering of insertion which
> is not what i want.
>
> any other suggestions?
>
> ciao robertj
another idea would be to be able to define
a comperator block that does the actual
comparisson.
I see. So you want a way to tell the object itself how it's to order
the elements. The default wprobably should stay insertion order, but
you'd like to specify an alternative like alpahnumeric key order. Is
that right? If so, I can put in an optional parameter for that no
problem. Although you may have to set it post instantiation, something
like
d = Dictionary.new.sort_on { |a,b| a.key <=> b.key }
As a shorthand:
d = Dictionary.new.sort_on(:key)
While I would like to add this as a block/parameter of the initialize
method itself, it may be a problem b/c this should probably be used for
a default block like hash has.
ara [dot] t [dot] howard [at] noaa [dot] gov
all happiness comes from the desire for others to be happy. all misery
comes from the desire for oneself to be happy.
-- bodhicaryavatara
Okay, I have preliminary implementation of Dictionary class with
built-in ordering. Its important to note that the implementation isn't
as efficient as sorting externally b/c the class sorts the pairs *every
time* #each is called (if order_by is set). There are ways to improve
the efficiency, but that's a task for another day.
The basic way to do it:
d = Dictionary.new.order_by{ |k,v| k }
This creates a dictionary orderd by the key. The block allows you to
define almost any order mechisim you like. Since alphanumeric key order
is likely the most common (after the default of insertion order) I
created a special constructor method just for it:
d = Dictionary.alpha
This does the exact same thing is the last example. The only thing I'm
not so sure about is the name of this method. Is this good or would
something else be better?
BTW, Ara, you inspired me:
d = Dictionary.auto
will do the same as OrderedAutoHash::new. Thanks for that idea. Of
course, I have the same question about my choice of method name here
too.
By the way have you seen calibre/association? That's kind of neat. It
isn't quite like a regular hash, but you can use it do order hash-like
strucutures.
You could easily wrap a Hash and provide an each() that sorted before yielding.
I've been discussing this off and on for years. What you say is true,
and it's also true that we can easily write a class that acts like an
ordered hash.
The problem is literals or constants. Nothing I do will ensure that
the hash { x=>a, y=>b, z=>c } will be iterated over in that
original specified order.