示例#1
0
/*
 * Return the integer of the location in data
 * to store the point to the item, or MAP_FULL.
 */
int hashmap_hash(map_t in, char* key){
	int curr;
	int i;

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

	/* If full, return immediately */
	if(m->size >= (m->table_size/2)) return MAP_FULL;

	/* Find the best index */
	curr = hashmap_hash_int(m, key);

	/* Linear probing */
	for(i = 0; i< MAX_CHAIN_LENGTH; i++){
		if(m->data[curr].in_use == 0)
			return curr;

		if(m->data[curr].in_use == 1 && (strcmp(m->data[curr].key,key)==0))
			return curr;

		curr = (curr + 1) % m->table_size;
	}

	return MAP_FULL;
}
示例#2
0
/*
 * Remove an element with that key from the map
 */
int hashmap_remove(map_t in, char* key){
	int i;
	int curr;
	hashmap_map* m;

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

	/* Find key */
	curr = hashmap_hash_int(m, key);

	/* Linear probing, if necessary */
	for(i = 0; i<MAX_CHAIN_LENGTH; i++){

        int in_use = m->data[curr].in_use;
        if (in_use == 1){
            if (strcmp(m->data[curr].key,key)==0){
                /* Blank out the fields */
                m->data[curr].in_use = 0;
                m->data[curr].data = 0;
                m->data[curr].key = NULL;

                /* Reduce the size */
                m->size--;
                return MAP_OK;
            }
		}
		curr = (curr + 1) % m->table_size;
	}

	/* Data not found */
	return MAP_MISSING;
}
示例#3
0
/*
 * Get your pointer out of the hashmap with a key
 */
int hashmap_get(map_t in, char* key, any_t *arg){
	int curr;
	int i;
	hashmap_map* m;

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

	/* Find data location */
	curr = hashmap_hash_int(m, key);

	/* Linear probing, if necessary */
	for(i = 0; i<MAX_CHAIN_LENGTH; i++){

        int in_use = m->data[curr].in_use;
        if (in_use == 1){
            if (strcmp(m->data[curr].key,key)==0){
                *arg = (m->data[curr].data);
                return MAP_OK;
            }
		}

		curr = (curr + 1) % m->table_size;
	}

        *arg = 0;

	/* Not found */
	return MAP_MISSING;
}
示例#4
0
//Return the integer of the location in data to store the point to the item, or -1.
int hashmap_hash(hashmap_t hashmap, int key)
{
	int curr;
	int i;

	if (hashmap->size == hashmap->table_size) 
		return -1;

	curr = hashmap_hash_int(hashmap, key);

	for (i = 0; i< hashmap->table_size; i++)
	{
		if (hashmap->data[curr]->in_use == 0)
			return curr;

		if (hashmap->data[curr]->key == key && hashmap->data[curr]->in_use == 1)
			return curr;

		curr = (curr + 1) % hashmap->table_size;
	}

	return -1;
}
示例#5
0
//Remove an element with that key from the map, 0 on success, -1 on fail
int hashmap_delete(hashmap_t hashmap, int key)
{
	int i;
	int curr;

	curr = hashmap_hash_int(hashmap, key);

	for (i = 0; i < hashmap->table_size; i++)
	{
		if(hashmap->data[curr]->key == key && hashmap->data[curr]->in_use == 1)
		{
			hashmap->data[curr]->in_use = 0;
			hashmap->data[curr]->data = NULL;
			hashmap->data[curr]->key = 0;

			hashmap->size--;
			return 0;
		}
		curr = (curr + 1) % hashmap->table_size;
	}

	return -1;
}
示例#6
0
//Get your pointer out of the hashmap with a key
int hashmap_get(hashmap_t hashmap, int key, hashmap_item_t *arg)
{
	int curr;
	int i;

	curr = hashmap_hash_int(hashmap, key);

	for (i = 0; i< hashmap->table_size; i++)
	{
		if (hashmap->data[curr]->key == key && hashmap->data[curr]->in_use == 1)
		{
			*arg = (int *) (hashmap->data[curr]->data);
			return 0;
		}

		curr = (curr + 1) % hashmap->table_size;
	}

	*arg = NULL;

	//Not found
	return -1;
}