Question

I have a requirement to do a lookup based on a large number. The number could fall in the range 1 - 2^32. Based on the input, i need to return some other data structure. My question is that what data structure should i use to effectively hold this?

I would have used an array giving me O(1) lookup if the numbers were in the range say, 1 to 5000. But when my input number goes large, it becomes unrealistic to use an array as the memory requirements would be huge.

I am hence trying to look at a data structure that yields the result fast and is not very heavy.

Any clues anybody?

EDIT:

It would not make sense to use an array since i may have only 100 or 200 indices to store.

Abhishek

No correct solution

OTHER TIPS

unordered_map or map, depending on what version of C++ you are using.

http://www.cplusplus.com/reference/unordered_map/unordered_map/

http://www.cplusplus.com/reference/map/map/


A simple solution in C, given you've stated at most 200 elements is just an array of structs with an index and a data pointer (or two arrays, one of indices and one of data pointers, where index[i] corresponds to data[i]). Linearly search the array looking for the index you want. With a small number of elements, (200), that will be very fast.

One possibility is a Judy Array, which is a sparse associative array. There is a C Implementation available. I don't have any direct experience of these, although they look interesting and could be worth experimenting with if you have the time.

Another (probably more orthodox) choice is a hash table. Hash tables are data structures which map keys to values, and provide fast lookup and insertion times (provided a good hash function is chosen). One thing they do not provide, however, is ordered traversal.

There are many C implementations. A quick Google search turned up uthash which appears to be suitable, particularly because it allows you to use any value type as the key (many implementations assume a string as the key). In your case you want to use an integer as the key.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top