![]() |
fuse_kafka
|
00001 #include <string.h> 00002 #include <stdio.h> 00003 #include <stdlib.h> 00004 #define FK_HASH_SIZE 100 00005 typedef struct _fk_hash_list { 00006 void* key; 00007 void* value; 00008 struct _fk_hash_list* next; 00009 } fk_hash_list; 00010 typedef fk_hash_list** fk_hash; 00011 fk_hash fk_hash_new() 00012 { 00013 fk_hash hash = (fk_hash) malloc(sizeof(fk_hash_list*) * FK_HASH_SIZE); 00014 memset(hash, 0, sizeof(fk_hash_list*) * FK_HASH_SIZE); 00015 return hash; 00016 } 00021 int fk_hash_hash(void* key, int key_is_string) 00022 { 00023 char* k = (char*) key; 00024 long int i = (long int) key; 00025 if(key_is_string && key == NULL) return 0; 00026 return ((key_is_string? (k[0] + k[strlen(k) - 1]): (i < 0? -i : i)) % FK_HASH_SIZE); 00027 } 00031 fk_hash_list* fk_hash_list_new(void* key, void* value) 00032 { 00033 fk_hash_list* list = malloc(sizeof(struct _fk_hash_list)); 00034 list->key = key; 00035 list->value = value; 00036 list->next = 0; 00037 return list; 00038 } 00039 void fk_hash_list_free(fk_hash_list* list, int delete_keys, int delete_value) 00040 { 00041 if(delete_keys) free(list->key); 00042 if(delete_value) free(list->value); 00043 free(list); 00044 } 00045 void fk_hash_list_delete(fk_hash_list* list, int delete_keys, int delete_value) 00046 { 00047 if(list == NULL) return; 00048 fk_hash_list_delete(list->next, delete_keys, delete_value); 00049 fk_hash_list_free(list, delete_keys, delete_value); 00050 } 00055 void fk_hash_put(fk_hash hash, void* key, void* value, int key_is_string) 00056 { 00057 if(hash == NULL) return; 00058 int h = fk_hash_hash(key, key_is_string); 00059 if(hash[h] == NULL) 00060 { 00061 hash[h] = fk_hash_list_new(key, value); 00062 } 00063 else 00064 { 00065 fk_hash_list* current; 00066 for(current = hash[h]; current != NULL; current = current->next) 00067 { 00068 if((key_is_string && strcmp(current->key, key) == 0) || (!key_is_string && current->key == key)) 00069 { 00070 current->value = value; 00071 return; 00072 } 00073 } 00074 fk_hash_list* new = fk_hash_list_new(key, value); 00075 new->next = hash[h]; 00076 hash[h] = new; 00077 } 00078 } 00083 void* fk_hash_get(fk_hash hash, void* key, int key_is_string) 00084 { 00085 if (hash == NULL) return (void*) -1; 00086 int h = fk_hash_hash(key, key_is_string); 00087 fk_hash_list* current; 00088 for(current = hash[h]; current != NULL; current = current->next) 00089 { 00090 if((key_is_string && strcmp(current->key, key) == 0) || (!key_is_string && current->key == key)) 00091 { 00092 return current->value; 00093 } 00094 } 00095 return (void*) -1; 00096 } 00097 void fk_hash_remove(fk_hash hash, void* key, int key_is_string, int delete_keys, int delete_value) 00098 { 00099 if(hash == NULL) return; 00100 int h = fk_hash_hash(key, key_is_string); 00101 fk_hash_list* current, *previous; 00102 previous = NULL; 00103 for(current = hash[h]; current != NULL; current = current->next) 00104 { 00105 if((key_is_string && strcmp(current->key, key) == 0) || (!key_is_string && current->key == key)) 00106 { 00107 if(previous == NULL) hash[h] = NULL; 00108 else previous->next = current->next; 00109 fk_hash_list_free(current, delete_keys, delete_value); 00110 return; 00111 } 00112 previous = current; 00113 } 00114 } 00115 void fk_hash_delete(fk_hash hash, int delete_keys, int delete_value) 00116 { 00117 int i; 00118 if(hash == NULL) return; 00119 for(i = 0; i < FK_HASH_SIZE; i++) 00120 { 00121 fk_hash_list_delete(hash[i], delete_keys, delete_value); 00122 hash[i] = NULL; 00123 } 00124 free(hash); 00125 }