[ruby-talk:02433] Wider need for Array.lookup

From: mrilu <mrilu@...>
Date: 2000-04-10 23:27:44 UTC
List: ruby-talk #2433
Uh I forgot to mention the start of my adventure. I need functionality of
the following method every now and then. So I thought I could be nice to
incorporate into standard distribution if others feel it's good to have.

I know there's Array.index, but it's a little bit different than using hash.
Additionally I believe it performs O(n) while hash should be O(c).
Btw. is there need for SortedArray with Array.index O(n log n)?

# Return hash which is the original array transformed into lookuptable (hash).
class Array
  def lookup
    ret = {}
    self.each_index{|c| ret[self[c]] = c }
    return ret
  end
end

p [1,'abc',2].lookup    # => {1=>0, 2=>2, "abc"=>1}






In This Thread

Prev Next