static void quickhash_insert_key(Hash * hash, Int i) { Int ind; ind = data_hash(&hash->keys->el[i]) % hash->hashtab_size; hash->links[i] = hash->hashtab[ind]; hash->hashtab[ind] = i; }
data_t * _any_hash(data_t *self, char *name, arguments_t *args) { data_t *obj; (void) name; obj = (args && arguments_args_size(args)) ? arguments_get_arg(args, 0) : self; return (data_t *) int_create(data_hash(obj)); }
static void insert_key(cDict *dict, Int i) { Int ind; ind = data_hash(&dict->keys->el[i]) % dict->hashtab_size; dict->links[i] = dict->hashtab[ind]; dict->hashtab[ind] = i; }
INTERNAL void insert_key(Hash * hash, Int i) { Int ind; ind = data_hash(&hash->keys->el[i]) % hash->hashtab_size; hash->links[i] = hash->hashtab[ind]; hash->hashtab[ind] = i; }
Int hash_find(Hash * hash, cData *key) { Int ind, i; ind = data_hash(key) % hash->hashtab_size; for (i = hash->hashtab[ind]; i != -1; i = hash->links[i]) { if (data_cmp(&hash->keys->el[i], key) == 0) return i; } return F_FAILURE; }
static Int search(cDict *dict, cData *key) { Int ind, i; ind = data_hash(key) % dict->hashtab_size; for (i = dict->hashtab[ind]; i != -1; i = dict->links[i]) { if (data_cmp(&dict->keys->el[i], key) == 0) return i; } return F_FAILURE; }
/* Error-checking is the caller's responsibility; this routine assumes that it * will find the key in the dictionary. */ cDict *dict_del(cDict *dict, cData *key) { Int ind, *ip, i = -1, j; dict = dict_prep(dict); /* Search for a pointer to the key, either in the hash table entry or in * the chain links. */ ind = data_hash(key) % dict->hashtab_size; for (ip = &dict->hashtab[ind];; ip = &dict->links[*ip]) { i = *ip; if (data_cmp(&dict->keys->el[i], key) == 0) break; } /* Delete the element from the keys and values lists. */ dict->keys = list_delete(dict->keys, i); dict->values = list_delete(dict->values, i); /* Replace the pointer to the key index with the next link. */ *ip = dict->links[i]; /* Copy the links beyond i backward. */ MEMMOVE(dict->links + i, dict->links + i + 1, dict->keys->len - i); dict->links[dict->keys->len] = -1; /* Since we've renumbered all the elements beyond i, we have to check * all the links and hash table entries. If they're greater than i, * decrement them. Skip this step if the element we removed was the last * one. */ if (i < dict->keys->len) { for (j = 0; j < dict->keys->len; j++) { if (dict->links[j] > i) dict->links[j]--; } for (j = 0; j < dict->hashtab_size; j++) { if (dict->hashtab[j] > i) dict->hashtab[j]--; } } return dict; }
int hash_handled (cData *d) { HandledFrob *h = HANDLED_FROB(d); return h->cclass + h->handler + data_hash(&h->rep); }
int main() { init_util(); cList *keys = list_new(2); cList *values = list_new(2); cData key1, key2; cData val1, val2; key1.type = STRING; key2.type = INTEGER; key1.u.str = string_from_chars("ab", 2); key2.u.val = 1106; val1.type = val2.type = INTEGER; val1.u.val = 10; val2.u.val = 20; list_add(keys, &key1); list_add(keys, &key2); values = list_add(values, &val1); values = list_add(values, &val2); cData keys_data, values_data; keys_data.type = values_data.type = LIST; keys_data.u.list = keys; values_data.u.list = values; cDict *mydict = dict_new(keys, values); cStr *lit = string_new(0); dict_add_literal_to_str(lit, mydict, 0); printf("%s\n", lit->s); printf("%s -> %d\n", key1.u.str->s, data_hash(&key1)); printf("%d -> %d\n", key2.u.val, data_hash(&key2)); puts("Testing dict!"); int i; for (i = 0; i < mydict->hashtab_size; i++) { printf("dict->links[%d] = %d\n", i, mydict->links[i]); } puts(""); for (i = 0; i < mydict->hashtab_size; i++) { printf("dict->hashtab[%d] = %d\n", i, mydict->hashtab[i]); } puts(""); for (i = 0; i < list_length(values); i++) { printf("dict->values[%d] = %s\n", i, data_to_literal(list_elem(mydict->values, i), 0)->s); //printf("dict->values[%d] = %d\n", i, list_elem(values, i)->u.val); } /*puts("Find some valz!"); cData ret; cData ret2; long res = dict_find(mydict, &key1, &ret); printf("%s => %d (%d)\n", key1.u.str->s, ret.u.val, res == keynf_id); res = dict_find(mydict, &key2, &ret2); printf("%s => %d (%d)\n", key2.u.str->s, ret2.u.val, res == keynf_id); */ return 0; }