#include <stdio.h>#ifdef __STDC__#include <stdlib.h>#include <string.h>#else#include <malloc.h>#endif#include "cc.h"/** Two functions:* char * set_entry(int namespace, char * name, void * value);* returns a pointer to the copy of the name;** void * read_entry(int namespace, char * name);* returns the value;*/struct hashentry{struct hashentry * next;void * value;int namespace;char word[1];};struct hashentry ** hashtable;int hashsize = 0xFF; /* 2^X -1 */int hashcount = 0;static int hashvalue P((int namespace, char * word));void *read_entry(namespace, word)int namespace;char * word;{int hash_val;struct hashentry * hashline;if( hashtable == 0 ) return 0;hash_val = hashvalue(namespace, word);hashline = hashtable[hash_val];for(; hashline; hashline = hashline->next){if(namespace != hashline->namespace) continue;if(word[0] != hashline->word[0]) continue;if(strcmp(word, hashline->word) ) continue;return hashline->value;}return 0;}char *set_entry(namespace, word, value)int namespace;char * word;void * value;{int hash_val, i;struct hashentry * hashline, *prev;hash_val = hashvalue(namespace, word);if( hashtable ){hashline = hashtable[hash_val];for(prev=0; hashline; prev=hashline, hashline = hashline->next){if(namespace != hashline->namespace) continue;if(word[0] != hashline->word[0]) continue;if(strcmp(word, hashline->word) ) continue;if( value ) hashline->value = value;else{if( prev == 0 ) hashtable[hash_val] = hashline->next;else prev->next = hashline->next;free(hashline);return 0;}return hashline->word;}}if( value == 0 ) return 0;if( hashtable == 0 ){hashtable = malloc((hashsize+1)*sizeof(char*));if( hashtable == 0 ) cfatal("Out of memory");for(i=0; i<=hashsize; i++) hashtable[i] = 0;}/* Add record */hashline = malloc(sizeof(struct hashentry)+strlen(word));if( hashline == 0 ) cfatal("Out of memory");else{hashline->next = hashtable[hash_val];hashline->namespace = namespace;hashline->value = value;strcpy(hashline->word, word);hashtable[hash_val] = hashline;}return hashline->word;}static int hashvalue(namespace, word)int namespace;char * word;{int val = namespace;char *p = word;while(*p){val = ((val<<4)^((val>>12)&0xF)^((*p++)&0xFF));}val &= hashsize;return val;}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。