One wants to store objects, allowing them to be retrieved via a numerical key. These keys can range from 0 to an arbitrary size (~100K, for instance), but not every natural number in the range has a corresponding object.
One might have the following:
structure[0] => some_obj_a
structure[3] => some_obj_b
structure[7] => some_obj_c
...
structure[100103] => some_obj_z
But all other keys (1, 2, 4, 5, 6, …) do not have an associated object. The numerical keys are used for retrieval, such that an “ID” is provided to return an object associated to that ID:
ID = get_input_id
my_obj = structure[ID]
What is the most efficient data structure for this scenario in Ruby? And for what reasons? (So far, I can see it being a hash or an array.)
I define “efficient” in terms of:
- Least memory used
- Fastest lookup times
- Fastest entry creation/updates (at arbitrary keys)
An initialization for this structure might be
hsh = Hash.new # or Array.new
hsh[0] = {:id => 0, :var => "a", :count => 45}
hsh[3] = {:id => 3, :var => "k", :count => 32}
hsh[7] = {:id => 7, :var => "e", :count => 2}
You’ve essentially described a sparse array or a hash.
Hashes are fast and only use the memory they have to use. They are also memory efficient. There is no “magic” data structure for this that’ll be any faster. Use a hash.