Monday, July 27, 2009

Using C is it possible to create an O(1) container if the keys of the container are not pointers?

By creating a hash map of pointers to data one has created a O(1) container but if the hash map is of a C data type there must be a search for the key before this key can be resolved into a value. This search cannot run in constant time in any manner I've been able to figure out. Help would be appreciated.

Using C is it possible to create an O(1) container if the keys of the container are not pointers?
Yes, it's possible, but you have to choose an algorithm based on your data and how you want to store it. For instance, you can have a hash function that instead of calculating a pointer, calculates the indice of an array, and store all the data in an array, ie:





f("John Doe") = 7, f("Jane Doe") = 14





These aren't pointers, but are instead indices of an array. Just watch out for collisions. Check out the diagram on wikipedia.


No comments:

Post a Comment