Ejemplo n.º 1
0
void dict_replace(dict *d, void *k, void *v) {
  unsigned int hash, idx;
  dict_entry *entry;
  setting *s = get_setting();
  if(d->size >= d->cap) dict_rehash(d, d->cap * 2);
  hash = DICT_HASH(d, k);
  idx = hash % d->cap;
  
  entry = _dict_find(d, idx, k);
  if(entry) {
    DICT_VALUE_FREE(d, entry->value);
    DICT_KEY_FREE(d, entry->key);
    entry->key = k;
    entry->value = v;
    return;
  }

  entry = s->malloc(sizeof(struct dict_entry));
  memset(entry, 0, sizeof(struct dict_entry));
  entry->bulk_next = d->entries[idx];
  d->entries[idx] = entry;
  entry->key = k;
  entry->value = v;
  if(d->head == NULL) {
    d->tail = d->head = entry;
  } else {
    d->tail->next = entry;
    entry->prev = d->tail;
    d->tail = entry;
  }
  d->size++;
}
Ejemplo n.º 2
0
/* add an item into the dictionary */
void
dict_add(struct dict *d, char *k, size_t sz, void *v) {

	struct bucket *b;
	char *k_dup = k;
	unsigned long h = d->key_hash(k, sz);

	/* possibly duplicate key */
	k_dup = d->key_dup(k, sz);

	/* check for important load and resize if need be. */
	if((float)d->count / (float)d->ht->sz > DICT_MAX_LOAD) {
		/* expand and replace HT */
		d->ht_old = d->ht;
		d->ht = ht_new(d->ht->sz + 1); /* will select next prime */
	}

	if((b = ht_insert(d->ht, h, k_dup, sz, v))) {
		d->count++;
		ht_record_used_bucket(d->ht, b);
	}
	
	dict_rehash(d);
}