#include <stdio.h>
#include <string.h>
#include "xl_hash.h"
#define HASH_TABLE_SIZE 1000
int str_calc_hash(XL_KEY_TYPE key){
unsigned int sum = 0;
int i;
for(i = 0; i < strlen((char *)key); i++){
sum = sum * 62423 + *((char*)key + i);
}
return sum % HASH_TABLE_SIZE;
}
void fe_print(XL_VALUE_TYPE v){
printf("%s\n", (char *)v);
}
int lu_test(XL_VALUE_TYPE v){
return (((char *)v)[strlen((char *)v) - 1] - '0') % 2;
}
int main(int argc, char *argv[]){
xl_hash_type type = {strcmp, str_calc_hash, 128, 128};
xl_hash *hash;
char key_buff[128], value_buff[128];
int i;
hash = xl_hash_init_with_size(&type, HASH_TABLE_SIZE);
for(i = 0; i < 20; i++){
sprintf(value_buff, "value:%d", i);
sprintf(key_buff, "key_%d", i);
xl_hash_add_first(hash, (XL_VALUE_TYPE)value_buff, (XL_KEY_TYPE)key_buff);
}
xl_hash_foreach(hash, fe_print);
xl_hash_foreach(xl_hash_lookup(hash, lu_test), fe_test);
xl_hash_free(hash);
return 0;
}