(almost) Non-colliding simple hash function for use in a switch

I am writing an advanced calculator in C. As you can guess, it currently has many functions, and I use the switch to perform the correct operation for each function name. This happens something like this:

switch(hash_of(function_name_currently_being_parsed))
{
  case HASH_COS:
    // do something
    break;

  case HASH_SIN:
    // do something else
    break;

  // etc.
}

So far, I have used this simple function that I found somewhere on the Internet to do hashing:

#define NHASH 29989
#define MULT 31

unsigned int simple_hash(char *p)
{
  unsigned int h = 0;
  for(; *p; p++)
    h = MULT * h + tolower(*p);
  return h % NHASH;
}

He did this job just fine and really fast. However, now that the calculator is expanding more and more, as well as users can define their own functions and variables, collisions become very visible - for example, condand dotpthe two hash to 612.

- , , ? , , , , .

.

+3
2

-, Paul Hseih 'Page ( ) , Murmur2 ( ), , ( - ) 32- ( , , ) 64- ( 't 16 , ).

, , , , (, )

+3

. .

0

Source: https://habr.com/ru/post/1759867/


All Articles