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
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).
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!
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!
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
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.
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
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.
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
my alib has an orderedhash which maintains insertion order.
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.
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.
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…
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.
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.