Exemplo n.º 1
0
/**
 * hashmap_put:
 * puts key/value pair into hashmap if any bucket available
 * NOTE: overwrites any bucket with same key
 */
int hashmap_put(hashmap_ptr map, const any_key_t* key, const any_value_t* value) {
    if (map) {
        int index;
        hashmap_map* _map = (hashmap_map*) map;

        /* find a suitable bucket, if any */
        index = hashmap_hash(_map, key);
        while (HASHMAP_FULL == index) {
            int status = hashmap_rehash(_map);
            if (HASHMAP_OK == status) {
                index = hashmap_hash(_map, key);
            } else {
                return HASHMAP_FULL;
            }
        }

        /* fill the bucket */
        _map->_buckets[index]._key = (any_key_t*) key;
        _map->_buckets[index]._value = (any_value_t*) value;
        _map->_buckets[index]._in_use = 1;
        _map->_cur_size++;
        _map->_changed = 1;

        return HASHMAP_OK;
    }
    return HASHMAP_ERR;
}
Exemplo n.º 2
0
END_TEST

START_TEST(test_hashmap_hash) {
  ck_assert_int_eq(hashmap_hash("a"), 177670);
  // Consistency check..
  ck_assert_int_eq(hashmap_hash("a"), 177670);
}
Exemplo n.º 3
0
void
hashmap_add(hashmap *map, const char *key, void *data, boolean is_alias)
{
	hashnode *node;
	uint16_t bucket;

	assert(map);
	assert(key);
	assert(data);

	if ((node = malloc(sizeof(hashnode))) == NULL)
	{
		quit_error(POUTOFMEM);
	}

	node->key = key;
	node->data = data;
	node->hash = hashmap_hash(key);
	node->is_alias = is_alias;

	bucket = node->hash % map->buckets;

	if (!map->data[bucket])
	{
		map->data[bucket] = darray_create();
		darray_push(map->data[bucket], node);
	}
	else
	{
		darray_push(map->data[bucket], node);
	}
}
Exemplo n.º 4
0
//Add a pointer to the hashmap with some key
int hashmap_insert(hashmap_t hashmap, int key, hashmap_item_t value)
{
	int index = hashmap_hash(hashmap, key);

	while(index == -1)
	{
		if (hashmap_rehash(hashmap) == -1) 
			return -1;
		index = hashmap_hash(hashmap, key);
	}

	hashmap->data[index]->data = value;
	hashmap->data[index]->key = key;
	hashmap->data[index]->in_use = 1;
	hashmap->size++; 

	return 0;
}
Exemplo n.º 5
0
void hashmap_add_list(hashmap_t* hashmap, list_value_t* lt) {
    hashmap_node_t* tmp_node;
    while(lt!=NULL) {
        tmp_node = hashmap_node_create(hashmap_hash(lt->key), lt);
        hashmap_set(hashmap, tmp_node);
        lt=lt->next;
    }

}
Exemplo n.º 6
0
/*
 * Add a pointer to the hashmap with some key
 */
int hashmap_put(map_t in, char* key, any_t value){
	int index;
	hashmap_map* m;

	/* Cast the hashmap */
	m = (hashmap_map *) in;

	/* Find a place to put our value */
	index = hashmap_hash(in, key);
	while(index == MAP_FULL){
		if (hashmap_rehash(in) == MAP_OMEM) {
			return MAP_OMEM;
		}
		index = hashmap_hash(in, key);
	}

	/* Set the data */
        m->data[index].data = value;
	m->data[index].key = key;
	m->data[index].in_use = 1;
	m->size++; 

	return MAP_OK;
}
Exemplo n.º 7
0
hashmap_node_t* hashmap_find(hashmap_t* hashmap,char *string) {
    char* key;
    list_value_t* find=JSON_parse(string);
    key=find->key;
    int create=1;
    hashmap_node_t* result=NULL;
    if(strlen(key)==0) {
        int i;
        for(i=0; i<BUCKET_NUMBER; i++) {
            hashmap_node_t* tmp=hashmap->map[i];
            while (tmp!=NULL) {
                if(create) {
                    result=hashmap_node_create(0, tmp->lt);
                    create=0;
                } else
                    hashmap_node_append_node(result, hashmap_node_create(0, tmp->lt));
                tmp=tmp->next;
            }
        }
        return result;
    } else {
        uint32_t hash=hashmap_hash(key);
        hashmap_node_t* bucket = *hashmap_get_bucket(hashmap, hash);
        if(bucket==NULL)
            return bucket;
        else {
            while(bucket!=NULL) {
                if(bucket->hash==hash && strcmp(key,bucket->lt->key)==0 && value_compare(find, bucket->lt)==0) {
                    if(create) {
                        result=hashmap_node_create(hash, bucket->lt);
                        create=0;
                    } else
                        hashmap_node_append_node(result, hashmap_node_create(hash, bucket->lt));
                }
                bucket=bucket->next;
            }
        }
        return result;
    }
}