[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

ordered hash ?

Its Me

12/2/2004 12:34:00 AM

Is there a pure-ruby ordered hash? I'm looking for something that will
preserve the order in which key/value pairs were entered. The one on RAA is
buggy http://raa.ruby-lang.org/project/or...

Will 2.0 come with a good ordered hash?

Thanks


42 Answers

Austin Ziegler

12/2/2004 3:30:00 AM

0

On Thu, 2 Dec 2004 09:37:44 +0900, itsme213 <itsme213@hotmail.com> wrote:
> Is there a pure-ruby ordered hash? I'm looking for something that will
> preserve the order in which key/value pairs were entered. The one on RAA is
> buggy http://raa.ruby-lang.org/project/or...

What's buggy about it? I have an ohash in the code to PDF::Writer that
you're welcome to use.

> Will 2.0 come with a good ordered hash?

The native hashing mechanism has been changed to preserve that order, I think.

-austin
--
Austin Ziegler * halostatue@gmail.com
* Alternate: austin@halostatue.ca


Its Me

12/2/2004 4:12:00 AM

0

> What's buggy about it?

It threw an error on '==', I did not dig much deeper after that.

> I have an ohash in the code to PDF::Writer that
> you're welcome to use.

I'll take a look, thanks.

> > Will 2.0 come with a good ordered hash?
>
> The native hashing mechanism has been changed to preserve that order, I
think.

That's good to know. Is this also in the 1.8 stream, or just 1.9 ?



Gavin Sinclair

12/2/2004 6:33:00 AM

0

On Thursday, December 2, 2004, 2:30:00 PM, Austin wrote:

>> Will 2.0 come with a good ordered hash?

> The native hashing mechanism has been changed to preserve that order, I think.

That sounds a bit silly. Wouldn't there be a performance overhead
associated with that? Shouldn't that price be paid only by those
people who want that functionality, given that it can be easily
implemented in C or Ruby?

A C-based extension providing an InsertOrderHash, distributed with
Ruby, would be very handy.

Gavin



nobu.nokada

12/2/2004 6:47:00 AM

0

Hi,

At Thu, 2 Dec 2004 12:30:00 +0900,
Austin Ziegler wrote in [ruby-talk:122119]:
> The native hashing mechanism has been changed to preserve that order, I think.

No.

--
Nobu Nakada


Robert Klemme

12/2/2004 10:44:00 AM

0


"itsme213" <itsme213@hotmail.com> schrieb im Newsbeitrag
news:Mptrd.59198$KQ2.38410@fe2.texas.rr.com...
> Is there a pure-ruby ordered hash? I'm looking for something that will
> preserve the order in which key/value pairs were entered. The one on RAA
is
> buggy http://raa.ruby-lang.org/project/or...

Whenever I hear "ordered hash" then I think of a hash whose keys are
ordered according to a definable order - not necessarily insertion order.
In fact, insertion order seems to me a very special case. Is this really
widely used?

> Will 2.0 come with a good ordered hash?

Dunno.

Kind regards

robert

Michael Neumann

12/2/2004 1:24:00 PM

0

itsme213 wrote:
> Is there a pure-ruby ordered hash? I'm looking for something that will
> preserve the order in which key/value pairs were entered. The one on RAA is
> buggy http://raa.ruby-lang.org/project/or...
>
> Will 2.0 come with a good ordered hash?

This works fine for me:

class OrderedHash < Hash
def initialize
@order_of_keys = []
super
end

def []=(key, value)
@order_of_keys.delete(key) if has_key?(key)
@order_of_keys << key
super
end

def each
@order_of_keys.each do |key|
yield key, self[key]
end
end
end


Regards,

Michael


gabriele renzi

12/2/2004 2:06:00 PM

0

Michael Neumann ha scritto:
> itsme213 wrote:
>
>> Is there a pure-ruby ordered hash? I'm looking for something that will
>> preserve the order in which key/value pairs were entered. The one on
>> RAA is
>> buggy http://raa.ruby-lang.org/project/or...
>>
>> Will 2.0 come with a good ordered hash?
>
>
> This works fine for me:
>

<snipcode>
well, but it works just unless you use a different method to access it
(i.e. shift). We definitely need an Association/Map mixin, imo.

Nikolai Weibull

12/2/2004 2:38:00 PM

0

* itsme213 <itsme213@hotmail.com> [Dec 02, 2004 14:00]:
> Is there a pure-ruby ordered hash? I'm looking for something that will
> preserve the order in which key/value pairs were entered. The one on
> RAA is buggy http://raa.ruby-lang.org/project/or...

Having an ordered Hash is like saying that arrays should be indexed by
strings--it's simply not what they are meant to be. My RDSL library
(http://rdsly.ruby...) contains an implementation of the Treap
ADT. It works like a Hash, but preserves order. It is implemented much
like a Tree but with Heap-like abilities--hence the name. Anyway, the
items are ordered, not by insertion but by comparison. It does not use
a two-level scheme like OrderedHash seems to use, so there's no wasted
memory on keeping ordering information. Also, benchmarks have shown
that it works very fast, even though it's purely ruby.

If a Treap isn't your style, RDSL also includes a SkipList
implementation, that has similar capabilities to that of a Treap; it's
yet another sorted ADT.

Please, if you haven't already commited to some solution, give RDSL a
try. It needs some testing. I'm going to get back to working on it
when I get time, but until then, some love from other users wouldn't
hurt.

End of plug. Thanks,
nikolai


--
::: name: Nikolai Weibull :: aliases: pcp / lone-star / aka :::
::: born: Chicago, IL USA :: loc atm: Gothenburg, Sweden :::
::: page: www.pcppopper.org :: fun atm: gf,lps,ruby,lisp,war3 :::
main(){printf(&linux["\021%six\012\0"],(linux)["have"]+"fun"-97);}


Austin Ziegler

12/2/2004 2:50:00 PM

0

On Thu, 2 Dec 2004 23:37:34 +0900, Nikolai Weibull
<mailing-lists.ruby-talk@rawuncut.elitemail.org> wrote:
> * itsme213 <itsme213@hotmail.com> [Dec 02, 2004 14:00]:
> > Is there a pure-ruby ordered hash? I'm looking for something that will
> > preserve the order in which key/value pairs were entered. The one on
> > RAA is buggy http://raa.ruby-lang.org/project/or...
>
> Having an ordered Hash is like saying that arrays should be indexed by
> strings--it's simply not what they are meant to be.

Be that as it may, it is still necessary to have an insertion-ordered
hash-like object.

I use it in PDF::Writer for page objects that can be referred to
meaningfully -- but still render in the order in which they were
inserted.

-austin
--
Austin Ziegler * halostatue@gmail.com
* Alternate: austin@halostatue.ca


Hugh Sasse

12/2/2004 3:18:00 PM

0