void ht_set(t_hashtable *hashtable, char *key, void *value) { const int bin = ht_hash(hashtable, key); t_entry *newpair; t_entry *next; t_entry *last; ht_fln(hashtable->table[bin], &next, &last, key); if (next != NULL && next->key != NULL && ft_strcmp(key, next->key) == 0) { free(next->value); next->value = value; } else { newpair = ht_newpair(key, value); if (next == hashtable->table[bin]) { newpair->next = next; hashtable->table[bin] = newpair; return ; } last->next = newpair; if (next) newpair->next = next; } }
t_value *ht_update(t_hash_table *h, t_key key) { int hash = ht_hash(key, h->size); t_bucket *b = ht_find_bucket(h, key, hash); t_value value; value.ull = 0ULL; if (b == NULL) { if (h->num_keys == h->size) { ht_resize(h); hash = ht_hash(key, h->size); } b = ht_create_bucket(h, key, value, h->bucket_ptrs[hash]); h->bucket_ptrs[hash] = b; } return &(b->value); }
/* Delete the student info item that matches the given id. If missing, don't do anything. */ void ht_delete(hashtable ht, int id) { int index = ht_hash(ht, id); if ( ! ht->buckets[index] ) { // empty list, not present return; } // check head first looking for matching id ht_node *cur = ht->buckets[index]; if ( (cur->item).id == id ) { ht_node *next = cur->next; ht->buckets[index] = next; free(cur); } else { ht_node *previous = cur; cur = cur->next; // walk along collision list looking for matching id while (cur) { if ( (cur->item).id == id ) { previous->next = cur->next; free(cur); break; } // no match, more to look at, move on previous = cur; cur = cur->next; } } }
void ht_remove(t_hashtable *hashtable, char *key) { int bin; t_entry *pair; t_entry *old; bin = ht_hash(hashtable, key); pair = hashtable->table[bin]; old = NULL; while (pair != NULL && pair->key != NULL && ft_strcmp(key, pair->key) > 0) { old = pair; pair = pair->next; } if (pair == NULL || pair->key == NULL || ft_strcmp(key, pair->key) != 0) return ; else { if (old) old->next = pair->next; free(pair->key); free(pair->value); free(pair); if (!old) hashtable->table[bin] = NULL; } }
int ht_get_count(hashtable_t *hashtable, char *key) { int bin = 0; entry_t *pair; bin = ht_hash(hashtable, key); /* Step through the bin, looking for our value. */ pair = hashtable->table[bin]; while (pair != NULL && pair->key != NULL && strcmp(key, pair->key) > 0) { pair = pair->next; } /* Did we actually find anything? */ //printf("%d\n\n",pair->value); if (pair == NULL || pair->key == NULL || strcmp(key, pair->key) != 0) { return -1; } else { return pair->count; } }
void ht_append(hashtable *hashtable , char *key){ int bin = NULL; entry *lastPair = NULL; entry *next = NULL; entry *newpair=NULL; bin = ht_hash(hashtable , key); next = hashtable->table[ bin ]; while (next != NULL && next->lastName != NULL && strcmp(next->lastName , key) >0){ lastPair = next ; next = next->pNext; } newpair = ht_newpair(key); if( next == hashtable->table[ bin ]){ newpair-> pNext = next; hashtable->table[ bin ] = newpair; } else if (next == NULL){ lastPair->pNext = newpair; } else{ newpair->pNext = next ; lastPair->pNext = newpair; } }
/* * Adds an item to the given list. * Return 1 if successful or 0 otherwise. */ int ht_add(hash_table_t *ht, int item) { int position = ht_hash(item); int inserted = 0; // Allocate memory for the next element in hash table entry_t *new_entry = malloc(sizeof(entry_t)); new_entry->key = item; new_entry->next = NULL; pthread_rwlock_wrlock(&(ht->locks[position])); // Check if position is empty if(ht->table[position] == NULL) { ht->table[position] = new_entry; inserted = 1; } else { // Pass all elements in chain to add new entry entry_t *next_entry; next_entry = ht->table[position]; while(next_entry->next != NULL) next_entry = next_entry->next; next_entry->next = new_entry; inserted = 1; } pthread_rwlock_unlock(&(ht->locks[position])); ht->total_elements++; return inserted; }
/* delete the given key from the hashtable */ void ht_remove (hashtab_t * hashtable, void *key, size_t keylen) { hashtab_node_t *last_node, *next_node; int index = ht_hash (key, keylen, hashtable->size); next_node = hashtable->arr[index]; last_node = NULL; while (next_node != NULL) { if (next_node->keylen == keylen) { if (memcmp (key, next_node->key, keylen) == 0) { /* adjust the list pointers */ if (last_node != NULL) last_node->next = next_node->next; else hashtable->arr[index] = next_node->next; /* free the node */ xfree (next_node); break; } } last_node = next_node; next_node = next_node->next; } }
/* Insert a key-value pair into a hash table. */ void ht_set( hashtable_t *hashtable, void* key, int value ) { entry_t *newpair = NULL; entry_t *next = NULL; entry_t *last = NULL; int bin = ht_hash( hashtable, key ); next = hashtable->table[ bin ]; while( next != NULL && next->key != NULL && key != next->key ) { last = next; next = next->next; } /* There's already a pair. Let's replace that string. */ if( next != NULL && next->key != NULL && key == next->key ) { next->value = value; /* Nope, could't find it. Time to grow a pair. */ } else { newpair = ht_newpair( key, value ); /* We're at the start of the linked list in this bin. */ if( next == hashtable->table[ bin ] ) { newpair->next = next; hashtable->table[ bin ] = newpair; /* We're at the end of the linked list in this bin. */ } else if ( next == NULL ) { last->next = newpair; /* We're in the middle of the list. */ } else { newpair->next = next; last->next = newpair; } } }
void ht_remove(Hashtable *hashtable, char *key) { int bin = 0; entry_t* current = NULL; entry_t* prev = NULL; bin = ht_hash(hashtable, key); /* Step through the bin, looking for our value. */ current = hashtable->table[bin]; while (current != NULL && strcmp(key, current->key) != 0) { prev = current; current = current->next; } /* Did we actually find anything? */ if (current == NULL) { return; } else { /* We're at the start of the linked list in this bin. */ if (prev == NULL) { hashtable->table[bin] = current->next; } /* Otherwise */ else { prev->next = current->next; } // free(current->key); free(current); } }
void ht_put(Hashtable* hashtable, char* key, void* value) { entry_t* newpair = NULL; entry_t* current = NULL; entry_t* prev = NULL; int bin = ht_hash(hashtable, key); current = hashtable->table[bin]; while (current != NULL && strcmp(key, current->key) != 0) { prev = current; current = current->next; } /* There's already a pair. Let's replace that string. */ if (current != NULL) { current->value = value; } /* Nope, could't find it. Time to grow a pair. */ else { newpair = ht_newpair(key, value); /* We're at the start of the linked list in this bin. */ if (prev == NULL) { hashtable->table[bin] = newpair; } /* We're at the end of the linked list in this bin. */ else { prev->next = newpair; } } }
void ht_insert(hashtable **table, void *key, size_t key_size, void *value, size_t value_size) { *table = ht_resize_if_needed(*table); int hash = ht_hash(key, key_size, (*table)->max_size); hash = ht_linear_probe_if_needed(*table, hash, key, key_size); // Free old entry if we are replacing if ((*table)->entries[hash] != NULL && memcmp((*table)->entries[hash]->key, key, key_size) == 0) { free((*table)->entries[hash]); } size_t entry_size = sizeof(struct _htable_entry) + key_size + value_size; htable_entry *entry = (htable_entry *)malloc(entry_size); memcpy(entry->data, key, key_size); entry->key = entry->data; entry->key_size = key_size; memcpy(entry->data+key_size, value, value_size); entry->value = entry->data+key_size; entry->value_size = value_size; (*table)->entries[hash] = entry; (*table)->entry_count++; }
int exmpp_ht_store(struct exmpp_hashtable *ht, const char *key, int key_len, void *value) { unsigned int index; struct exmpp_ht_entry *entry; if (ht == NULL || ht->entries == NULL) return (-1); /* Allocate the new entry. */ entry = driver_alloc(sizeof(*entry)); if (entry == NULL) return (-1); if (key_len == -1) { entry->hash = ht_hash(key); entry->key = exmpp_strdup(key); if (entry->key == NULL) return (-1); } else { entry->hash = ht_hash_len(key, key_len); entry->key = driver_alloc(key_len + 1); if (entry->key == NULL) return (-1); memcpy(entry->key, key, key_len); entry->key[key_len] = '\0'; } entry->key_len = key_len; entry->value = value; #if defined(USE_RWLOCK) erl_drv_rwlock_rwlock(ht->lock); #endif /* Expand the table is necessary. */ if (++(ht->entries_count) > ht->load_limit) { /* * Ignore the return value. If expand fails, we should * still try cramming just this value into the existing * table -- we may not have memory for a larger table, * but one more element may be ok. Next time we insert, * we'll try expanding again. */ ht_expand(ht); } /* Wire the new entry. */ index = entry->hash % ht->length; entry->next = ht->entries[index]; ht->entries[index] = entry; #if defined(USE_RWLOCK) erl_drv_rwlock_rwunlock(ht->lock); #endif return (0); }
void ht_remove(hashtable *table, void *key, size_t key_size) { int hash = ht_hash(key, key_size, table->max_size); int index = ht_correct_index_if_needed(table, hash, key, key_size); assert(index >= 0 && index < table->max_size); free(table->entries[index]); table->entries[index] = NULL; }
/* Retrieve a key-value pair from a hash table. */ char *ht_get( hashtable_t *hashtable, char *key ) { entry_t *pair = hashtable->table[ht_hash( hashtable, key )]; while( pair && pair->key && strcmp(key, pair->key) > 0 ) pair = pair->next; if (!pair || !pair->key) return NULL; else return pair->value; }
void *ht_get(hashtable *table, void *key, size_t key_size) { int hash = ht_hash(key, key_size, table->max_size); int index = ht_correct_index_if_needed(table, hash, key, key_size); if (index < 0 || index >= table->max_size || table->entries[index]->value == NULL) { // fprintf(stderr, "NULL returned from table\n"); return NULL; } return table->entries[index]->value; }
/** * @brief Sets the Key and Value into the hashtable. * * @param hashtable A pointer to the hash table. * @param key The string that stores the key. * @param value The string that stores the value. * @return No return value. */ int ht_set( HashTable *hashtable, char *key, char *value ) { int bin = 0; Entry *newpair = NULL; Entry *next = NULL; Entry *last = NULL; bin = ht_hash( hashtable, key ); next = hashtable->table[ bin ]; while( next != NULL && next->key != NULL && strcmp( key, next->key ) > 0 ) { last = next; next = next->next; } /* There's already a pair. Let's replace that string. */ if( next != NULL && next->key != NULL && strcmp( key, next->key ) == 0 ) { free( next->value ); next->value = myStrDup( value ); next->metadata += 1; return HASH_SET_UPDATE; /* Nope, could't find it. Time to grow a pair. */ } else { newpair = ht_newpair( key, value ); /* Ensure we have not run out of memory */ if (newpair == NULL) return HASH_SET_FAIL; /* We're at the start of the linked list in this bin. */ if( next == hashtable->table[ bin ] ) { newpair->next = next; hashtable->table[ bin ] = newpair; /* We're at the end of the linked list in this bin. */ } else if ( next == NULL ) { last->next = newpair; /* We're in the middle of the list. */ } else { newpair->next = next; last->next = newpair; } /* We have successfully inserted */ return HASH_SET_INSERT; } return 0; // success }
t_entry *ht_get_pair(t_hashtable *hashtable, char *key) { int bin; t_entry *pair; bin = ht_hash(hashtable, key); pair = hashtable->table[bin]; while (pair != NULL && pair->key != NULL && ft_strcmp(key, pair->key) > 0) pair = pair->next; if (pair == NULL || pair->key == NULL || ft_strcmp(key, pair->key) != 0) return (NULL); else return (pair); }
/** * @brief Removes an item given a specific key * * @param hashtable A pointer to the hash table. * @param Key A pointer to the key to delete. * @return Returns the status of the removal, whether it was successful or not. */ int ht_removeItem ( HashTable *hashtable, char *key ){ Entry *curr = NULL; Entry *last = NULL; int bin = ht_hash( hashtable, key ); curr = hashtable->table[ bin ]; /* Key does not exist */ if (curr == NULL) return KEY_NOT_FOUND; /* Iterate through until we hit the end or find the key */ while( curr->next != NULL && strcmp( key, curr->key ) != 0 ) { last = curr; curr = curr->next; } /* We hit the end and yet no key was found */ if (strcmp (key, curr->key) != 0) return KEY_NOT_FOUND; /* Item was found! */ /* We're at the start of the linked list in this bin. */ if (curr == hashtable->table[bin]){ Entry * temp = curr; curr = curr -> next; free (temp->key); free (temp->value); temp->next = NULL; free (temp); hashtable->table[bin] = curr; return HASH_SET_DELETE; /* We are at the end of the list in this bin */ } else if ( curr->next == NULL ){ free (curr->key); free (curr->value); free (curr); last->next = NULL; return HASH_SET_DELETE; /* We're in the middle of the list. */ } else { last->next = curr-> next; free (curr->key); free (curr->value); free (curr); return HASH_SET_DELETE; } }
void ht_remove(t_hash_table *h, t_key key, void (*function)(t_value *value, void *data), void *data) { int hash = ht_hash(key, h->size); t_bucket *b, **prevnextb = &(h->bucket_ptrs[hash]); for (b = *prevnextb; b; b = *prevnextb) { if (b->key == key) { *prevnextb = b->next; if (function) function(&(b->value), data); ht_delete_bucket(h, b); } else prevnextb = &(b->next); } }
void *ht_search (hashtab_t * hashtable, void *key, size_t keylen) { int index = ht_hash (key, keylen, hashtable->size); if (hashtable->arr[index] == NULL) return NULL; hashtab_node_t *last_node = hashtable->arr[index]; while (last_node != NULL) { if (last_node->keylen == keylen) if (memcmp (key, last_node->key, keylen) == 0) return last_node->value; last_node = last_node->next; } return NULL; }
void ht_findName(hashtable *hashtable ,char *key){ int bin = 0; entry *pair; bin = ht_hash(hashtable , key); pair = hashtable->table[ bin ]; while( strcmp(pair->lastName,key) >0 ){ pair = pair->pNext; } if( strcmp(pair->lastName,key)!=0){ printf("%s is not found\n",key); } else{ printf("%s is found\n",key); } }
/* Retrieve a key-value pair from a hash table. */ int ht_get( hashtable_t *hashtable, void* key ) { entry_t *pair; int bin = ht_hash( hashtable, key ); /* Step through the bin, looking for our value. */ pair = hashtable->table[ bin ]; while( pair != NULL && pair->key != NULL && strcmp( key, pair->key ) > 0 ) { pair = pair->next; } /* Did we actually find anything? */ if( pair == NULL || pair->key == NULL || strcmp( key, pair->key ) != 0 ) { return -1; } else { return pair->value; } }
static void ht_resize(t_hash_table *h) { int i, new_hash, new_size = 2 * h->size; t_bucket *b, *nextb; t_bucket **new_bucket_ptrs = (t_bucket **)calloc(new_size, sizeof(t_bucket *)); for (i = h->size - 1; i >= 0; i--) { for (b = h->bucket_ptrs[i]; b; b = nextb) { new_hash = ht_hash(b->key, new_size); nextb = b->next; b->next = new_bucket_ptrs[new_hash]; new_bucket_ptrs[new_hash] = b; } } free(h->bucket_ptrs); h->bucket_ptrs = new_bucket_ptrs; h->size = new_size; }
void *ht_insert (hashtab_t * hashtable, void *key, size_t keylen, void *value, size_t vallen) { int index = ht_hash (key, keylen, hashtable->size); hashtab_node_t *next_node, *last_node; next_node = hashtable->arr[index]; last_node = NULL; /* Search for an existing key. */ while (next_node != NULL) { if (next_node->keylen == keylen) { if (memcmp (key, next_node->key, keylen) == 0) { next_node->value = value; next_node->vallen = vallen; return next_node->value; } } last_node = next_node; next_node = next_node->next; } /* create a new node */ hashtab_node_t *new_node; new_node = (hashtab_node_t *) xmalloc (sizeof (hashtab_node_t)); new_node->key = key; new_node->value = value; new_node->keylen = keylen; new_node->vallen = vallen; new_node->next = NULL; /* Tack the new node on the end or right on the table. */ if (last_node != NULL) last_node->next = new_node; else hashtable->arr[index] = new_node; hashtable->count++; return new_node->value; }
/* Insert a key-value pair into a hash table. */ void ht_set( hashtable_t *hashtable, char *key, char *value ) { int bin = ht_hash( hashtable, key ); entry_t *next = hashtable->table[bin]; entry_t *last, *newpair; while(next && next->key && strcmp(key, next->key) > 0) { last = next; next = next->next; } if (!hashtable->table[bin]) hashtable->table[bin] = ht_newpair(key, value, NULL); else if (!next || !next->key || strcmp(key, next->key)==0) last->next = ht_newpair(key, value, next); else { free (next->value); next->value = strdup(value); } }
/** * @brief Retrieve a value corresponding to a given Key. * * @param hashtable A pointer to the hash table. * @param key The string that stores the key. * @param value The string that stores the value. * @returns an entry pointer. */ Entry *ht_get( HashTable *hashtable, char *key ) { int bin = 0; Entry *pair; bin = ht_hash( hashtable, key ); /* Step through the bin, looking for our value. */ pair = hashtable->table[ bin ]; while( pair != NULL && pair->key != NULL && strcmp( key, pair->key ) > 0 ) { pair = pair->next; } /* Did we actually find anything? */ if( pair == NULL || pair->key == NULL || strcmp( key, pair->key ) != 0 ) { return NULL; } else { return pair; } }
/* * Removes an item from the given list. * Return 1 if successful or 0 otherwise. */ int ht_remove(hash_table_t *ht, int item) { int position = ht_hash(item); int removed = 0; pthread_rwlock_wrlock(&(ht->locks[position])); if(ht->table[position] != NULL) { entry_t *next_entry, *previous; // Check if item to be removed is the first of a chain list if(ht->table[position]->key == item) { // Remove the first element of chain next_entry = ht->table[position]; ht->table[position] = next_entry->next; free(next_entry); removed = 1; } else { // Remove the others elements of chain previous = ht->table[position]; next_entry = previous->next; while(next_entry != NULL) { if(next_entry->key == item) { previous->next = next_entry->next; free(next_entry); removed = 1; break; } previous = next_entry; next_entry = next_entry->next; } } } pthread_rwlock_unlock(&(ht->locks[position])); if(removed) ht->total_elements--; return removed; }
void * exmpp_ht_fetch(struct exmpp_hashtable *ht, const char *key, int key_len) { struct exmpp_ht_entry *entry; unsigned int hash, index; if (ht == NULL || ht->entries == NULL) return (NULL); hash = key_len == -1 ? ht_hash(key) : ht_hash_len(key, key_len); #if defined(USE_RWLOCK) erl_drv_rwlock_rlock(ht->lock); #endif index = hash % ht->length; entry = ht->entries[index]; while (entry != NULL) { /* Check hash value to short circuit heavier comparison. */ if (entry->hash == hash && ((key_len == -1 && strcmp(entry->key, key) == 0) || (entry->key_len == key_len && strncmp(entry->key, key, key_len) == 0))) { #if defined(USE_RWLOCK) erl_drv_rwlock_runlock(ht->lock); #endif return (entry->value); } entry = entry->next; } #if defined(USE_RWLOCK) erl_drv_rwlock_runlock(ht->lock); #endif return (NULL); }
/* Insert a key-value pair into a hash table. */ void ht_set( hashtable_t *hashtable, char *key, struct symbol_s *value ) { int bin = 0; entry_t *newpair = NULL; entry_t *next = NULL; entry_t *last = NULL; bin = ht_hash( hashtable, key ); next = hashtable->table[ bin ]; while( next != NULL && next->key != NULL && strcmp( key, next->key ) > 0 ) { last = next; next = next->next; } /* There's already a pair. Let's replace that string. */ if( next != NULL && next->key != NULL && strcmp( key, next->key ) == 0 ) { //TODO yyerror ("redefinition"); free( next->value ); next->value = value; /* Nope, could't find it. Time to grow a pair. */ } else { newpair = ht_newpair( key, value ); /* We're at the start of the linked list in this bin. */ if( next == hashtable->table[ bin ] ) { newpair->next = next; hashtable->table[ bin ] = newpair; /* We're at the end of the linked list in this bin. */ } else if ( next == NULL ) { last->next = newpair; /* We're in the middle of the list. */ } else { newpair->next = next; last->next = newpair; } } }