Beispiel #1
0
int hmap_remove(struct hmap *mp, void *key)
{
	long h = hash_calc(mp->keytype, key) % (mp->hsize);
	struct hmap_e *e = mp->em[h];
    if (e == NULL)
        return 0;
    if ((*(mp->cpa))(key, e->key) == 0)
    {
        mp->em[h] = e->next;
		free(e->key);
        (*(mp->fr))(e->value);
		free(e);
        mp->size--;
        return 1;
    }
       
    struct hmap_e *pre = e;
    e = e->next; 
	while (e)
	{
        if ((*(mp->cpa))(key, e->key) == 0)
        {
            pre->next = e->next;
            free(e->key);
            (*(mp->fr))(e->value);
            free(e);
            mp->size--;
            return 1;
        }
        pre = pre->next;
        e = e->next;
    }
    return 0;
}
Beispiel #2
0
int hash_put(HashTable *ht, char *key, char *value)
{
    int   hash_val;
    Cell *cell, *newcell;

    /* replace value if (key,value) pair already exists */
    cell = hash_get_cell(ht, key);
    if (cell) {
        strncpy(cell->value, value, VALSIZE);
    /*    printf("put item, key:%s, value:%s\n", cell->key, cell->value);*/
        return 0;
    }

    /* create new cell */
    newcell = (Cell*)malloc(sizeof(Cell));
    if (newcell == NULL) {
        return -1;
    }
    strncpy(newcell->key, key, KEYSIZE);
    strncpy(newcell->value, value, VALSIZE);
    newcell->next = NULL;

    hash_val = hash_calc(key);
    cell     = ht->table[hash_val];

    if (cell) {
        while (cell->next) cell = cell->next;
        cell->next              = newcell;
    } else {
        ht->table[hash_val]     = newcell;
    }

   /* printf("put item, key: %s, value: %s\n", newcell->key, newcell->value);*/
    return 0;
}
Beispiel #3
0
//删除目标key的节点
void *hmap_del(struct hmap *mp, void *key)
{
	if (key == NULL)
        return NULL;
    long h = hash_calc(mp->keytype, key) % (mp->hsize);
	struct hmap_e *e = mp->em[h];
    struct hmap_e *pre = hmap_e_create(NULL,NULL);
    pre->next = e;
	while (e)
	{
		if((*(mp->cpa))(key, e->key) == 0){
            pre->next = e->next;
            mp->size--;
            e->next = NULL;
            free(pre);
            //如果此槽中只剩下一个元素,pre的方式会不能删除掉
            if (mp->em[h] == e)
                mp->em[h] = NULL;
            void *r = e->value;
            free(e->key);
            free(e);
            return r;
        }
        pre = e;
        e = e->next;
	}
    free(pre);
	return NULL;
}
Beispiel #4
0
int set_insert(set_t *set, int new_val)
{
  unsigned int key = hash_calc(set, new_val);
  struct entry_t **entry;
  struct entry_t *new_entry;
  
  entry = &set->table[key];
  
  while (*entry && (*entry)->value < new_val){
    entry = &((*entry)->next);
  }
  /* this is a duplicate entry */
  if (*entry && (*entry)->value == new_val) {
    return 0;
  }
  
  new_entry = safe_malloc(sizeof(entry_t));
  new_entry->key = key;
  new_entry->value = new_val;
  new_entry->next = *entry;
  
  *entry = new_entry;

  set->items++;

  return 1;
}
Beispiel #5
0
unsigned int
isc_hash_calc(const unsigned char *key, unsigned int keylen,
	      isc_boolean_t case_sensitive)
{
	INSIST(hash != NULL && VALID_HASH(hash));
	REQUIRE(keylen <= hash->limit);

	return (hash_calc(hash, key, keylen, case_sensitive));
}
Beispiel #6
0
unsigned int
isc_hash_ctxcalc(isc_hash_t *hctx, const unsigned char *key,
		 unsigned int keylen, isc_boolean_t case_sensitive)
{
	REQUIRE(hctx != NULL && VALID_HASH(hctx));
	REQUIRE(keylen <= hctx->limit);

	return (hash_calc(hctx, key, keylen, case_sensitive));
}
Beispiel #7
0
/* 
 * function : 获得一个hmap的元素,hashmap内部使用
 * input    : hashmap, key
 * output   : hashmap元素
 */
struct hmap_e *hmap_get_e(struct hmap *mp, void *key)
{
	if(key == NULL)
        return NULL;
	long h = hash_calc(mp->keytype, key) % (mp->hsize);
	struct hmap_e *e = mp->em[h];
	while (e)
	{
		if((*(mp->cpa))(key, e->key) == 0)
			return e;
        e = e->next;
	}
	return NULL;
}
Beispiel #8
0
static Cell *hash_get_cell(HashTable *ht, char *key)
{
    int   hash_val;
    Cell *cell;

    hash_val                                            = hash_calc(key);
    cell                                                = ht->table[hash_val];
    while (cell) {
        if (strncmp(cell->key, key, strlen(cell->key)) == 0) {
            return cell;
        }
        cell = cell->next;
    }
    return NULL;
}
Beispiel #9
0
int set_delete(set_t *set, int del_val)
{
  unsigned int key = hash_calc(set, del_val);
  struct entry_t **entry;
  
  entry = &set->table[key];
  while (*entry && (*entry)->value < del_val) {
    entry = &((*entry)->next);
  }
  if (*entry && (*entry)->value == del_val) {
    struct entry_t *del_entry = *entry;
    *entry = (*entry)->next;
    free(del_entry);
    set->items--;
    return 1;
  }
  return 0;
}
Beispiel #10
0
int set_search(set_t *set, int search_val)
{
  unsigned int key = hash_calc(set, search_val);
  struct entry_t *entry;

  if(set->table[key] == NULL)
    return false;
  
  entry = set->table[key];
  
  while(entry && entry->value < search_val) {
    entry = entry->next;
  }
  if (entry && entry->value == search_val) {
    return 1;
  }
  
  return 0;	
}
Beispiel #11
0
struct hmap_e *hmap_put(struct hmap *mp, void *key, void *value)
{
	if(key == NULL)
        return NULL;
    //判断是否已经放入hashmap中,如果放入则替换内容,否则添加
	struct hmap_e *e  = hmap_get_e(mp, key);
	if (e != NULL)
	{   
        (*(mp->fr))(e->value);
        e->value = value;
        free(key);
		return e;
	}
    long h = hash_calc(mp->keytype, key) % (mp->hsize);
    //没有放入,则添加
	e = hmap_e_create(key, value);
    //把新元素放到每个key队列的头部
    e->next = mp->em[h];
    mp->em[h] = e;
    mp->size++;
    return e;
}
Beispiel #12
0
int hash_delete(HashTable *ht, char *key)
{
    int   hash_val;
    Cell *cell, *tmp;

    hash_val = hash_calc(key);
    cell     = ht->table[hash_val];
    tmp      = NULL;
    while (cell) {
        if (strncmp(cell->key, key, strlen(cell->key)) == 0) {
            if (tmp == NULL) {
                ht->table[hash_val] = cell->next;
            } else {
                tmp->next           = cell->next;
            }
         /*   printf("delete item, key: %s, value: %s\n", cell->key, cell->value);*/
            free(cell);
            return 0;
        }
        tmp  = cell;
        cell = cell->next;
    }
    return -1;                  /* not exists */
}