Matthew Fioravante
Matthew Fioravante

Reputation: 1498

Multi-directional hash table

I apologize in advance if this has been asked before. If it has I have no idea what this data structure would be called.

I have a collection of N (approx ~300 or less) widgets. Each widget has M (around ~10) different names. This collection will be populated once and then looked up many times. I will be implementing this in C++.

An example of this might be a collection of 200 people and storing their names in 7 different languages.

The lookup function would basically look like this: lookup("name", A, B), which will return the translation of the name "name" from language A to language B, (only if name is in the collection).

Is there any known data structure in the literature for doing this sort of thing efficiently? The most obvious solution is to create a bunch of hash tables for the lookups, but having MxM hash tables for all the possible pairs quickly gets unwieldy and memory inefficient. I'd also be willing to consider sorted arrays (binary search) or even trees. Since the collection is not huge, log(N) lookups are just fine.

Thank you everyone!

Upvotes: 1

Views: 212

Answers (2)

James Waldby - jwpat7
James Waldby - jwpat7

Reputation: 8711

Create an N-by-M array D, such that D[u,v] is the word in language v for widget u.

Also create M hash tables, H₀...Hₘ (where m is M-1) such that Hᵥ(w).data is u if w is the word for widget u in language v.

To perform lookup(w, r, s),
(1) set u = Hᵣ(w).data
(2) if D[u,r] equals w, return D[u,s], else return not-found.

Upvotes: 1

user2608621
user2608621

Reputation:

Based on your description of the desired lookup function, it sounds like you could use a single hash table where the key is tuple<string, Language, Language> and the value is the result of the translation. The two languages in the key identify the source language of the string and the language of the desired translation, respectively.

Upvotes: 1

Related Questions