Esempio n. 1
0
uint ModHashFromName(const char* Module)
{
    //
    // return MODINFO.hash (based on the name)
    //
    if(!Module || Module[0] == '\0')
        return 0;

    return murmurhash(Module, (int)strlen(Module));
}
Esempio n. 2
0
bool add_dict(dict *d, char *key, int type, ...)
{
    uint32_t key_index = 0;
    int count = 0;
    dictEntry *de = NULL;
    va_list value_ptr;
    char *string_value_ptr = NULL;

    va_start(value_ptr, type);
    if (key == NULL || strlen(key) == 0)
    {
	    printf("Enpty key.\n");
	    return false;
    }
    if (exist_key(d, key) == true)
    {
	printf("Exist a same key.\n");
	return false;
    }
    if (d->rehash_index == -1)
    {
	if(d->hash_table[0].size <= d->hash_table[0].used )
	{
	    d->hash_table[1].table = (dictEntry **)calloc(growth_size(d->hash_table[0].used ), sizeof(dictEntry *));
	    for (count = 0; count < growth_size(d->hash_table[0].used); count ++)
	    {
		d->hash_table[1].table[count] = NULL;                           //pointer
	    }
	    d->hash_table[1].size = growth_size(d->hash_table[0].used);
	    d->hash_table[1].size_mask = d->hash_table[1].size - 1;
	    d->hash_table[1].used = 0;
	    d->rehash_index = 0;
	    single_rehash_dict(d);
	    switch (type) {
	    case INTTYPE:
	    {
		    add_dict(d, key, type, va_arg(value_ptr, long));
	    }
		    break;
	    case DECIMALTYPE:
	    {
		    add_dict(d, key, type, va_arg(value_ptr, double));
	    }
		    break;
	    case STRINGTYPE:
	    {
		    add_dict(d, key, type, va_arg(value_ptr, char *));
	    }
		    break;
	    case OBJECTTYPE:
	    {
		    add_dict(d, key, type, va_arg(value_ptr, void *));
	    }
		    break;
	    default:
	    {
		    printf("value type is error.\n");
		    return false;
	    }
		    break;
	    }
	}
	else
	{
	    key_index = murmurhash(key, (uint32_t )strlen(key), MMHASH_SEED);
	    key_index = key_index & d->hash_table[0].size_mask;
	    de = (dictEntry *)calloc(1, sizeof(dictEntry));
	    de->key = (char *)calloc(strlen(key) + 1, sizeof(char));
	    memcpy(de->key, key, strlen(key));
	    switch (type) {
	    case INTTYPE:
	    {
		    de->value_type = INTTYPE;
		    de->value.num_value = va_arg(value_ptr, long);
	    }
		break;
	    case DECIMALTYPE:
	    {
		    de->value_type = DECIMALTYPE;
		    de->value.decimal_value = va_arg(value_ptr, double);
	    }
		break;
	    case STRINGTYPE:
	    {
		    de->value_type = STRINGTYPE;
		    string_value_ptr = va_arg(value_ptr, char *);
		    de->value.string_value = (char *)calloc (strlen(string_value_ptr) + 1, sizeof(char));
		    memcpy(de->value.string_value, string_value_ptr, strlen(string_value_ptr));
	    }
		break;
	    case OBJECTTYPE:
	    {
		    de->value_type = OBJECTTYPE;
		    de->value.object_value = va_arg(value_ptr, void *);
	    }
		break;
	    default:
	    {
		printf("value type is error.\n");
		return false;
	    }
		break;
	    }
	    de->next = d->hash_table[0].table[key_index];
	    d->hash_table[0].table[key_index] = de;
	    d->hash_table[0].used++;
	}
    }
    if (exist_key(d, key) == false)
    {
	    if (d->rehash_index != -1)
	    {
		key_index = murmurhash(key, (uint32_t )strlen(key), MMHASH_SEED);
		key_index = key_index & d->hash_table[1].size_mask;
		de = (dictEntry *)calloc(1, sizeof(dictEntry));
		de->key = (char *)calloc(strlen(key) + 1, sizeof(char));
		memcpy(de->key, key, strlen(key));
		switch (type) {
		case INTTYPE:
		{
			de->value_type = INTTYPE;
			de->value.num_value = va_arg(value_ptr, long);
		}
		    break;
		case DECIMALTYPE:
		{
			de->value_type = DECIMALTYPE;
			de->value.decimal_value = va_arg(value_ptr, double);
		}
		    break;
		case STRINGTYPE:
		{
			de->value_type = STRINGTYPE;
			string_value_ptr = va_arg(value_ptr, char *);
			de->value.string_value = (char *)calloc (strlen(string_value_ptr) + 1, sizeof(char));
			memcpy(de->value.string_value, string_value_ptr, strlen(string_value_ptr));
		}
		    break;
		case OBJECTTYPE:
		{
			de->value_type = OBJECTTYPE;
			de->value.object_value = va_arg(value_ptr, void *);
		    //can't delete value
		}
		    break;
		default:
		{
		    printf("value type is error.\n");
		    return false;
		}
		    break;
		}
		de->next = d->hash_table[1].table[key_index];
		d->hash_table[1].table[key_index] = de;
		d->hash_table[1].used++;
	    }

    }