Is there a Hash-like class that maintains insertion order and, ideally, allows 'retrieval' by either key or index? I googled around for this but can't seem to hit on a query string that is useful.
In a perfect implementation I'd be able to do something like:
> Is there a Hash-like class that maintains insertion order and, ideally, > allows 'retrieval' by either key or index? I googled around for this but > can't seem to hit on a query string that is useful.
> In a perfect implementation I'd be able to do something like:
but this only returns keys in order for methods like each - it does not allow look-up by number __or__ field.
hth.
-a -- =========================================================================== ==== | email :: ara [dot] t [dot] howard [at] noaa [dot] gov | phone :: 303.497.6469 | Your life dwells amoung the causes of death | Like a lamp standing in a strong breeze. --Nagarjuna =========================================================================== ====
> Is there a Hash-like class that maintains insertion order and, > ideally, allows 'retrieval' by either key or index? I googled > around for this but can't seem to hit on a query string that is > useful.
> In a perfect implementation I'd be able to do something like:
I can't think of one off the top of my head, but its not to hard to write
% cat orderedhash.rb class OrderedHash < Hash def initialize @key_list = [] super end def []=(key, value) if has_key?(key) super(key, value) else @key_list << key super(key, value) end end
def by_index(index) self[@key_list[index]] end
def each @key_list.each do |key| yield(key, self[key] ) end end
def delete(key) @key_list = @key_list.delete_if { |x| x == key } super(key) end end
Ara, your arrayfield is an excellent fit to what I need. Thanks! The semantics is slightly different (e.g. when you update a field you don't change it's position in the array) than what I had been thinking, but the difference is slight and is perfectly sensible. I've incorporated it into my project (it only took a moment or two) and all the unit tests I had pass.
Your alib also looks to be very interesting. Certainly worth looking at just to pick up some Ruby tricks (so is arrayfield for that matter).
I'd be more than happy to use it, but I don't know what the license terms are. What are they? Same question for alib.
>> Is there a Hash-like class that maintains insertion order and, >> ideally, allows 'retrieval' by either key or index? I googled >> around for this but can't seem to hit on a query string that is >> useful.
>> In a perfect implementation I'd be able to do something like:
> but this only returns keys in order for methods like each - it does > not allow > look-up by number __or__ field.
> hth.
> -a > -- > ====================================================================== > ========= > | email :: ara [dot] t [dot] howard [at] noaa [dot] gov > | phone :: 303.497.6469 > | Your life dwells amoung the causes of death > | Like a lamp standing in a strong breeze. --Nagarjuna > ====================================================================== > =========
> On Sep 25, 2005, at 10:21 PM, Bob Hutchison wrote:
>> Hi,
>> Is there a Hash-like class that maintains insertion order and, >> ideally, allows 'retrieval' by either key or index? I googled >> around for this but can't seem to hit on a query string that is >> useful.
>> In a perfect implementation I'd be able to do something like:
> Disadvantages include merge won't work properly, and delete is now O > (n) instead of O(1)
> I didn't change [] for it to have different semantics for integers > vs. "anything else" because what if you want to use an integer as a > key.
You are right it doesn't look too difficult to write. I had intended to do so if I couldn't find a lazy way out -- which I think I did with Ara's arrayfield class. I was a bit worried that something nasty would come up when implementing it.
On Mon, 26 Sep 2005, Bob Hutchison wrote: > Ara, your arrayfield is an excellent fit to what I need. Thanks! The > semantics is slightly different (e.g. when you update a field you don't > change it's position in the array) than what I had been thinking, but the > difference is slight and is perfectly sensible. I've incorporated it into my > project (it only took a moment or two) and all the unit tests I had pass.
> Your alib also looks to be very interesting. Certainly worth looking at just > to pick up some Ruby tricks (so is arrayfield for that matter).
> I'd be more than happy to use it, but I don't know what the license terms > are. What are they? Same question for alib.
all my projects use ruby's license - which is dual - so do what you like with any of my code.
glad it worked out for you.
kind regards.
-a -- =========================================================================== ==== | email :: ara [dot] t [dot] howard [at] noaa [dot] gov | phone :: 303.497.6469 | Your life dwells amoung the causes of death | Like a lamp standing in a strong breeze. --Nagarjuna =========================================================================== ====
On Mon, 2005-09-26 at 11:21 +0900, Bob Hutchison wrote: > Hi,
> Is there a Hash-like class that maintains insertion order and, > ideally, allows 'retrieval' by either key or index? I googled around > for this but can't seem to hit on a query string that is useful.
> In a perfect implementation I'd be able to do something like:
>> Ara, your arrayfield is an excellent fit to what I need. Thanks! The >> semantics is slightly different (e.g. when you update a field you >> don't >> change it's position in the array) than what I had been thinking, >> but the >> difference is slight and is perfectly sensible. I've incorporated >> it into my >> project (it only took a moment or two) and all the unit tests I >> had pass.
>> Your alib also looks to be very interesting. Certainly worth >> looking at just >> to pick up some Ruby tricks (so is arrayfield for that matter).
>> I'd be more than happy to use it, but I don't know what the >> license terms >> are. What are they? Same question for alib.
> all my projects use ruby's license - which is dual - so do what you > like with > any of my code.
> glad it worked out for you.
Works beautifully. If you are curious, here is a link to an article I have just published on my weblog about the project I'm working on: <http://recursive.ca/hutch/index.php?p=243>
> -a > -- > ====================================================================== > ========= > | email :: ara [dot] t [dot] howard [at] noaa [dot] gov > | phone :: 303.497.6469 > | Your life dwells amoung the causes of death > | Like a lamp standing in a strong breeze. --Nagarjuna > ====================================================================== > =========