// std_hash_functions.c - some standard hash functions

#include <stdio.h>
#include <stdlib.h>

#include "default_error_handling.h"

//------------------------------------------------------------
// very primitive string hash function - needs better
// implementation if you really need a good one.
unsigned stringHash(void *key,unsigned modulus,
                    void (*error_handler)(int error_type,
                                          void *special))
{
  unsigned hash = 0;
  unsigned char *current = (unsigned char*)key;

  if (!key)
  {
    error_handler(ILLEGAL_ARGUMENTS,"stringHash");
    return(0);
  }
  while(*current)
    hash = ((hash * 31) + *current++) % modulus;
  return(hash);
}

//------------------------------------------------------------
// helper function for string compare operations, if keys
// in a hashtable are strings
int stringCompare(void *key1, void *key2,
                  void (*error_handler)(int error_type,
                                        void *special))
{
  if ((!key1) || (!key2))
  {
    error_handler(ILLEGAL_ARGUMENTS,"stringCompare");
    return(ILLEGAL_ARGUMENTS); // must be discarded anyway
  }
  return(strcmp(key1,key2));
}
