This one is related to my previous question on the performance of Arrays and Hashes in Ruby.
Prerequisites
I know that using Hashes to store lots of Objects leads to significant performance increase because of the O(1) lookup.
Now let's assume I had two classes, namely A
and B
and they can be related to each other, but only if a third class C
exists (it's sort of a relationship class). To give a practical example, let's say I have Document
, Query
and the relationship class Judgement
(this is from information retrieval, so basically the judgement tells you whether the document is relevant for a query or not).
(I hope I got this right)
The Problem
In most cases you'd like to find out how many Judgements
there are for a combination of Document
and Query
or if there are any.
In order to find out the latter, I'll iterate over each Jugdement
...
@judgements.each { |j| return true if j.document == document and j.query == query }
Now, this brings us back to a linear search again, which isn't that helpful.
How to solve it?
I was thinking about a way to have double Hashes - if there's such a thing - so that I could just look up Judgements
using the Document
and Query
I already have.
Or is there any other way of quickly finding out whether a Judgement exists for a given pair of Document and Query?