A use case for an ordered hash

Phillip H. wrote:

person’s benefit would kill the speed and jack up memory usage.

a[‘key 1’]

=> 1

p a

=> [[‘key 1’,‘1’],[‘key 2’, ‘2’],[‘key 3’,3]]

a = [‘key 1’,‘1’, ‘key 2’, ‘2’, ‘key 3’,3].to_assoc

=> [[“key 1”, “1”], [“key 2”, “2”], [“key 3”, 3]]

a.assoc(‘key 2’)

=> [“key 2”, “2”]

a.set ‘key 2’, 99

=> [“key 2”, 99]

a

=> [[“key 1”, “1”], [“key 2”, 99], [“key 3”, 3]]

class Array
def to_assoc
f=nil ; partition{f=!f}.transpose
end
def set k, v
(pair = assoc(k)) ? self[ index(pair) ] = [k,v] : push( [k,v] )
end
def rm k
(pair = assoc( k )) && slice!( index( pair ) )
end
end

On 8/15/06, Hal F. [email protected] wrote:

insertion order.

OHash lookup should be as fast as Hash lookup.

Insertion would be slightly slower,

Not necessarily
that was why I suggested an
OrderedHash (where insertion would not be slower )
and a
SortedHash (where indeed insertion would be slower)

and iteration (and to_a) would

be slowed down more.

again iteration would be slowed down - possibly a lot - in an OrdedHash
but
would
not be slowed down in a SortedHash (with a price to pay on memory
consumption and update times).

Cheers
Robert

Hal

Christian N. wrote:

Fun history fact:

#524<5|>lilith:~/src$ grep Dict ruby-0.49/dict.c

C_Dict = rb_define_class(“Dict”, C_Object);
rb_name_class(C_Dict, rb_intern(“Hash”)); /* alias */

Now, that’s funny.

At least that seems to show that the name Dict isn’t outside
the realm of possibility.

Although it adds to the bad joke potential. “I Dict around
and made a Hash of everything. Array!!”

I’m trying to remember: What is wrong with the name Map?

Hal

Phillip H. wrote:

Using an ordered hash for one
person’s benefit would kill the speed and jack up memory usage.

[…] but replacing the Hash is not the way to go.

I don’t think anyone is suggesting replacing the
existing Hash. As I see it, the ordered hash would
merely be an alternative to the standard hash.

Regards,

On 8/21/06, Bil K. [email protected] wrote:

Phillip H. wrote:

Using an ordered hash for one
person’s benefit would kill the speed and jack up memory usage.

[…] but replacing the Hash is not the way to go.

I don’t think anyone is suggesting replacing the
existing Hash. As I see it, the ordered hash would
merely be an alternative to the standard hash.

Which, as has been shown can be provided without changes to the base
specification/implementation of Ruby.

The fly in the ointment, is that a few of the proponents of
OrderedHash seem to strongly require a literal syntax for the beast.
That’s the one thing which seems to require divine intervention .

Personally, I don’t feel a burning need for it, and not knowing the
implementation cost, I’m not sure if the idea “pulls it’s own weight.”
But that’s just a personal opinion.

Heck, the old Smalltalk equivalent of hash Dictionary, gave you the
keys as a Set, and the values as a Bag when you asked for them!


Rick DeNatale

My blog on Ruby
http://talklikeaduck.denhaven2.com/

Robert D. wrote:

SortedHash (where indeed insertion would be slower)

and iteration (and to_a) would

be slowed down more.

I meant an ordered hash would be slowed down simply
because you are storing an extra piece of information.

Hal

Rick DeNatale wrote:

merely be an alternative to the standard hash.

Which, as has been shown can be provided without changes to the base
specification/implementation of Ruby.

Sure. And if you didn’t have Hash, you could implenent
your own.

The fly in the ointment, is that a few of the proponents of
OrderedHash seem to strongly require a literal syntax for the beast.
That’s the one thing which seems to require divine intervention .

Yes. That’s the thing which makes hashes in Ruby bearable.
(How do they work in Java? I forgot.)

After all, C has arrays. But even if they were “heterogeneous”
like Ruby arrays, they still wouldn’t be as fun and convenient
as the ones in Ruby. And OO is part of that, but syntax is also
part. Notation does matter.

Personally, I don’t feel a burning need for it, and not knowing the
implementation cost, I’m not sure if the idea “pulls it’s own weight.”
But that’s just a personal opinion.

Heck, the old Smalltalk equivalent of hash Dictionary, gave you the
keys as a Set, and the values as a Bag when you asked for them!

That would give me a headache. :slight_smile:

Hal

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Rick DeNatale wrote:

Which, as has been shown can be provided without changes to the base
specification/implementation of Ruby.

The fly in the ointment, is that a few of the proponents of
OrderedHash seem to strongly require a literal syntax for the beast.
That’s the one thing which seems to require divine intervention .

This functionality is not part of Hash as traditionally
understood, including those new to Ruby. Adding it to the
core Hash in the language will add a space and time cost
to all programs currently using Hash.

If you need a hash with this functionality, create a
mixin module containing the extra code (several examples
have appeared in these threads) and include it where
you need it.

I often have a hankering for this kind of functionality
in random access data structures (database tables more
than in-memory structures), so I can see the use of it.

Doesn’t strike me as a must-have addition (or subtraction)
for the core Hash class…

And I wouldn’t call it an OrderedHash (suggests sorting
by the data rather than by insertion order). Call the
mixin something like InsertionOrderMemory and you could
conceivably use it for other structures…


regards

fergal byrne
technical director

Adnet Web D. - Building for Business since 1995
http://www.adnet.ie/ t:+353 1 676 4262 aim/skype:FergByrne

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.3 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFE6gFqd3SE35LsMPcRAlnwAJwN7FcUim+5ljQ+fliYBc3HVii7RgCghVNO
hveor+GT98zkVDHTW9FNuYc=
=b63O
-----END PGP SIGNATURE-----

On 8/22/06, Robert D. [email protected] wrote:

First there would be the SortedHash and all you sayed was true about it
(save that iteration would be slower, which seems wrong to me).
And then there might be the OrderedHash, which is a Hash without any
overhead, just offering different access methods,
which would implement order this might be a bad idea, as the access sorting
can so easily be done ourselfs.

The thing is, there’s no way to preserve insertion order in a hash
without using some extra storage, and taking a bit of extra time on
insertion and deletion. Lookup and iteration would not have their
speed affected, but insertion and deletion definitely would because
you’d have to make sure the insertion order was preserved too.

martin

On 8/21/06, Martin DeMello [email protected] wrote:

The thing is, there’s no way to preserve insertion order in a hash

You did it, thank you, I was not talking about InsertionOrder in Hash,
which is of course a
very important one, so I want three now!!!
SortedHash
OrderableHash (maybe better naming)
HistoricalHash (the name’s a joke of course :wink:

without using some extra storage, and taking a bit of extra time on

insertion and deletion. Lookup and iteration would not have their
speed affected, but insertion and deletion definitely would because
you’d have to make sure the insertion order was preserved too.

You summed it up nicely.

ok everything sorted out now.

martin

Robert


Deux choses sont infinies : l’univers et la bêtise humaine ; en ce qui
concerne l’univers, je n’en ai pas acquis la certitude absolue.

  • Albert Einstein

On Tue, 22 Aug 2006, Robert D. wrote:

can so easily be done ourselfs.

The thing is, there’s no way to preserve insertion order in a hash

You did it, thank you, I was not talking about InsertionOrder in Hash,
which is of course a
very important one, so I want three now!!!
SortedHash
OrderableHash (maybe better naming)
HistoricalHash (the name’s a joke of course :wink:

my alib has an orderedhash which maintains insertion order.

fyi.

-a

On 8/21/06, Hal F. [email protected] wrote:

OrderedHash (where insertion would not be slower )
and a
SortedHash (where indeed insertion would be slower)

and iteration (and to_a) would

be slowed down more.

I meant an ordered hash would be slowed down simply
because you are storing an extra piece of information.

I know but I would not be storing that extra piece of information in one
of
the variants of the two new Hashes I was dreaming of.

I think I know where there might be a different conception and you are
probably rightnot to worry about the OrderedHash.

First there would be the SortedHash and all you sayed was true about it
(save that iteration would be slower, which seems wrong to me).
And then there might be the OrderedHash, which is a Hash without any
overhead, just offering different access methods,
which would implement order this might be a bad idea, as the access
sorting
can so easily be done ourselfs.
I wanted to talk about the concept anyway as it might be interesting to
folks thinking about their own implementations.

Cheers
Robert

Hal


Deux choses sont infinies : l’univers et la bêtise humaine ; en ce qui
concerne l’univers, je n’en ai pas acquis la certitude absolue.

  • Albert Einstein

On 8/21/06, Chad P. [email protected] wrote:

On Tue, Aug 22, 2006 at 05:32:52AM +0900, Robert D. wrote:

OrderableHash (maybe better naming)

OrderHash sounds good to me.

Well when I OrderHash, I usually order CornedBeefHash with a
fried egg on top


Rick DeNatale

My blog on Ruby
http://talklikeaduck.denhaven2.com/

On Tue, Aug 22, 2006 at 05:32:52AM +0900, Robert D. wrote:

OrderableHash (maybe better naming)

OrderHash sounds good to me.

On Tue, Aug 22, 2006 at 06:18:36AM +0900, Rick DeNatale wrote:

On 8/21/06, Chad P. [email protected] wrote:

On Tue, Aug 22, 2006 at 05:32:52AM +0900, Robert D. wrote:

OrderableHash (maybe better naming)

OrderHash sounds good to me.

Well when I OrderHash, I usually order CornedBeefHash with a
fried egg on top

I was thinking of hash browns, actually.

On Tue, 22 Aug 2006, Rick DeNatale wrote:

On 8/21/06, Chad P. [email protected] wrote:

On Tue, Aug 22, 2006 at 05:32:52AM +0900, Robert D. wrote:

OrderableHash (maybe better naming)

OrderHash sounds good to me.

Well when I OrderHash, I usually order CornedBeefHash with a
fried egg on top

and i have a good time in amsterdam. :wink:

-a

On Aug 21, 2006, at 2:15 PM, Rick DeNatale wrote:

Which, as has been shown can be provided without changes to the base
specification/implementation of Ruby.

A ordered hash implementation in the standard library would be better
than nothing. Similar to the way that Set is not in Core but is in
the standard library.

A literal syntax would be a bonus but of course would require a change
to core.

Gary W.

“Hal F.” [email protected] wrote in message
news:[email protected]

existing Hash. As I see it, the ordered hash would
OrderedHash seem to strongly require a literal syntax for the beast.
That’s the one thing which seems to require divine intervention .

Yes. That’s the thing which makes hashes in Ruby bearable.
(How do they work in Java? I forgot.)

I disagree.  I dislike the hash literal syntax and I can't believe 

that
this is the only useful thing about hashes to some people. If the lack
of
pretty syntax for hash literals is your problem then life has been
pretty
good to you…

Just Another Victim of the Ambient M. wrote:

I disagree.  I dislike the hash literal syntax and I can't believe that 

this is the only useful thing about hashes to some people. If the lack of
pretty syntax for hash literals is your problem then life has been pretty
good to you…

Haha… I don’t think anyone said it’s the only useful thing. :slight_smile:
And granted, I dislike it because of the shift key I have to hit,
but I dislike it less than an array of arrays, or a flat list of
ordered pairs.

And yes, life has been good to me… :wink:

Hal

On Tue, 22 Aug 2006 [email protected] wrote:

to core.
not if

require ‘orderedhash’

replaces the builtin Hash

-a