int hash_add_item(hash_t *h, hash_item_t *newItem) { unsigned int index = hash_index(h, newItem->key->ptr, newItem->key->len); hash_item_t *i = (h->buckets[index]).items; int isAdd = 1; while(i) { if(string_compare(i->key, newItem->key) == 0) { isAdd = 0; i->type = newItem->type; i->value = newItem->value; break; } i = i->next; } if(isAdd) { (h->buckets[index]).count += 1; h->count += 1; newItem->next = h->buckets[index].items; h->buckets[index].items = newItem; } return 0; }
int hash_table_remove(hash_table_t * hash, const char *key) { int ndx = hash_index(hash, key); hash_entry_t *hash_entry = hash->entries + ndx; hash_entry_t *next_entry = NULL, *last_entry = NULL; while (hash_entry) { if (hash_entry->key) { if (strcmp(key, hash_entry->key) == 0) { free(hash_entry->key); if (last_entry) { last_entry->next = hash_entry->next; free(hash_entry); } else { next_entry = hash_entry->next; if (next_entry) { memmove(hash_entry, next_entry, sizeof(hash_entry_t)); free(next_entry); } else { memset(hash_entry, 0, sizeof(hash_entry_t)); } } return 1; } } last_entry = hash_entry; hash_entry = hash_entry->next; } return 0; }
void * ADhash_add (ADhash_t hash, void *key, void *data) { struct hash_t *h; struct hash_element_t *p, *e; unsigned int i; /* Check hash structure validity */ if ((h = check_magic (hash, ADMAGIC_HASH)) == NULL) return (NULL); /* First check for duplicate key */ p = hash_lookup (h, key); if (p != NULL) return (NULL); /* Create new hash element */ NEW_ROF (e, NULL); e->key = key; e->data = data; /* Calculate hash index and add element to bucket */ i = hash_index (h, key); e->bucket = i; if ((p = h->hash[i]) == NULL) { h->hash[i] = e; } else { while (p->next != NULL) p = p->next; p->next = e; } h->size++; /* Return new element */ return (data); }
void remove_all(struct hash *hash, struct node *n) { struct tree *t; struct tree_node *h, *s; if (n->neededby->root || n->providedby->root) return; h = tree_first(n->provide->root); while (h) { s = tree_search_tree_node(h->n->providedby, n); tree_node_free(h->n->providedby, s); remove_all(hash, h->n); h = tree_next(h); } h = tree_first(n->need->root); while (h) { s = tree_search_tree_node(h->n->neededby, n); tree_node_free(h->n->neededby, s); remove_all(hash, h->n); h = tree_next(h); } t = hash->tbl[hash_index(n->name)]; s = tree_search_tree_node(t, n); tree_node_free(t, s); node_free(n); }
int hash_del_item(hash_t *h, const char *key, int keyLen) { int index = hash_index(h, key, keyLen); hash_item_t *pre, *cur ; pre = NULL; cur = h->buckets[index].items; if(h->buckets[index].count == 0 || cur == NULL) { return 0; } while(cur) { if(keyLen == cur->key->len && strncmp(key, cur->key->ptr, keyLen) == 0) { break; } pre = cur; cur = cur->next; } if(cur != NULL) { if(pre == NULL) { h->buckets[index].items = cur->next; } else { pre->next = cur->next; } h->buckets[index].count--; } return 0; }
void hash_setpos (Hash *hash, void *key, uint32_t pos, uint32_t keylen) { int index = hash_index(hash, key, keylen); if (hash->keys[index] == 0) { hash->keys[index] = key; hash->lens[index] = keylen; } hash->bits[index] |= 1 << (pos % 64); }
float Noise2:: operator()(float x, float y) const { float floorx=std::floor(x), floory=std::floor(y); int i=(int)floorx, j=(int)floory; const Vec2f &n00=basis[hash_index(i,j)]; const Vec2f &n10=basis[hash_index(i+1,j)]; const Vec2f &n01=basis[hash_index(i,j+1)]; const Vec2f &n11=basis[hash_index(i+1,j+1)]; float fx=x-floorx, fy=y-floory; float sx=fx*fx*fx*(10-fx*(15-fx*6)), sy=fy*fy*fy*(10-fy*(15-fy*6)); return bilerp( fx*n00[0] + fy*n00[1], (fx-1)*n10[0] + fy*n10[1], fx*n01[0] + (fy-1)*n01[1], (fx-1)*n11[0] + (fy-1)*n11[1], sx, sy); }
struct node *hash_safe_insert(struct hash *hash, struct node *n) { unsigned long index = hash_index(n->name); struct node *r; r = tree_search_node(hash->tbl[index], n->name); if (r) return r; tree_insert(hash->tbl[index], n); return n; }
void store_hash(int depth, int flag, int score, move m) { ttable *tt = tables + hash_index(board->hash); if (depth < tt->depth) return; tt->hash = board->hash; tt->depth = depth; tt->score = score; tt->flag = flag; tt->m = m; }
static struct hash_element_t * hash_lookup (struct hash_t *h, void *key) { unsigned int idx; struct hash_element_t *p; /* Calculate hash index and check for empty bucket */ idx = hash_index (h, key); if ((p = h->hash[idx]) == NULL) return (NULL); /* Check elements in bucket */ while ((p != NULL) && (h->match (key, p->key) == 0)) p = p->next; return (p); }
void *hash_table_get(hash_table_t * hash, const char *key) { int ndx = hash_index(hash, key); hash_entry_t *hash_entry = hash->entries + ndx; while (hash_entry) { if (hash_entry->key) { if (strcmp(key, hash_entry->key) == 0) { hash->n_hits++; return hash_entry->data; } } hash_entry = hash_entry->next; } hash->n_misses++; return NULL; }
hash_item_t * get_hash_item(hash_t *h, const char *key, int keyLen) { unsigned int index = hash_index(h, key, keyLen); hash_item_t * item = h->buckets[index].items; if(h->buckets[index].count == 0 || item == NULL) { return NULL; } while(item ) { if(strncmp(key, item->key->ptr, keyLen) == 0) { return item; } item = item->next; } return NULL; }
int hash_table_insert(hash_table_t * hash, const char *key, void *value) { unsigned int bucket_len = 0; int ndx = hash_index(hash, key); hash_entry_t *hash_entry = hash->entries + ndx; if (hash_entry->key) { if (strcmp(hash_entry->key, key) == 0) { /* alread in table, update the value */ hash_entry->data = value; return 0; } else { /* * if this is a collision, we have to go to the end of the ll, * then add a new entry * before we can hook up the value */ while (hash_entry->next) { hash_entry = hash_entry->next; if (strcmp(hash_entry->key, key) == 0) { hash_entry->data = value; return 0; } bucket_len++; } hash_entry->next = xcalloc(1, sizeof(hash_entry_t)); hash_entry = hash_entry->next; hash_entry->next = NULL; hash->n_collisions++; if (++bucket_len > hash->max_bucket_len) hash->max_bucket_len = bucket_len; } } else hash->n_used_buckets++; hash->n_elements++; hash_entry->key = xstrdup(key); hash_entry->data = value; return 0; }
Hash* find_free_hash_entry(Hash* hTable, Block* bTable, const char* key, unsigned long table_size) { Hash* hashEnd = hTable + table_size; Hash* hash0; Hash* hash; unsigned long index = hash_index(table_size, key); unsigned long keyA = hash_keyA(key); unsigned long keyB = hash_keyB(key); unsigned long blockIndex = 0xFFFFFFFF; hash = hash0 = hTable + index; while (hash->blockIndex < HASH_ENTRY_DELETED) { if (++hash >= hashEnd) hash = hTable; if (hash == hash0) return NULL; } hash->keyA = keyA; hash->keyB = keyB; for (index = 0; index < table_size; index++) { Block * block = bTable + index; if ((block->flags & FLAG_BLOCK_EXSIT) == 0) { blockIndex = index; break; } } if (blockIndex == 0xFFFFFFFF) blockIndex = table_size; hash->blockIndex = blockIndex; return hash; }
int retrieve_hash(int depth, int *score, move *m) { ttable *tt = tables + hash_index(board->hash); if (tt->hash == board->hash) { if (tt->score<LOSE+100) { *score = tt->score + (ply - search_info.startply) + tt->depth; } else if (tt->score>WIN-100) { *score = tt->score - (ply - search_info.startply) - tt->depth; } else { *score = tt->score; } *m = tt->m; if (tt->depth > depth) { tablehits[0] ++; return tt->flag; } else if (tt->depth == depth) { tablehits[1] ++; return tt->flag; } else { tablehits[2] ++; return LOWER_DEPTH; } } return NO_HIT; }
static int logfs_write_dir(struct inode *dir, struct dentry *dentry, struct inode *inode) { struct page *page; struct logfs_disk_dentry *dd; u32 hash = hash_32(dentry->d_name.name, dentry->d_name.len, 0); pgoff_t index; int round, err; for (round = 0; round < 20; round++) { index = hash_index(hash, round); if (logfs_exist_block(dir, index)) continue; page = find_or_create_page(dir->i_mapping, index, GFP_KERNEL); if (!page) return -ENOMEM; dd = kmap_atomic(page, KM_USER0); memset(dd, 0, sizeof(*dd)); dd->ino = cpu_to_be64(inode->i_ino); dd->type = logfs_type(inode); logfs_set_name(dd, &dentry->d_name); kunmap_atomic(dd, KM_USER0); err = logfs_write_buf(dir, page, WF_LOCK); unlock_page(page); page_cache_release(page); if (!err) grow_dir(dir, index); return err; } /* FIXME: Is there a better return value? In most cases neither * the filesystem nor the directory are full. But we have had * too many collisions for this particular hash and no fallback. */ return -ENOSPC; }
static struct page *logfs_get_dd_page(struct inode *dir, struct dentry *dentry) { struct qstr *name = &dentry->d_name; struct page *page; struct logfs_disk_dentry *dd; u32 hash = hash_32(name->name, name->len, 0); pgoff_t index; int round; if (name->len > LOGFS_MAX_NAMELEN) return ERR_PTR(-ENAMETOOLONG); for (round = 0; round < 20; round++) { index = hash_index(hash, round); if (beyond_eof(dir, index)) return NULL; if (!logfs_exist_block(dir, index)) continue; page = read_cache_page(dir->i_mapping, index, (filler_t *)logfs_readpage, NULL); if (IS_ERR(page)) return page; dd = kmap_atomic(page, KM_USER0); BUG_ON(dd->namelen == 0); if (name->len != be16_to_cpu(dd->namelen) || memcmp(name->name, dd->name, name->len)) { kunmap_atomic(dd, KM_USER0); page_cache_release(page); continue; } kunmap_atomic(dd, KM_USER0); return page; } return NULL; }
Hash* get_hash_entry(Hash* table, const char* key, unsigned long table_size) { Hash* hashEnd = table + table_size; Hash* hash0; Hash* hash; unsigned long index = (unsigned long)key; unsigned long keyA; unsigned long keyB; if(index <= table_size) { for(hash = table;hash<hashEnd;hash++) { if(hash->index == index) return hash; } return NULL; } index = hash_index(table_size,key); keyA = hash_keyA(key); keyB = hash_keyB(key); hash = hash0 = table + index; while(hash->index != HASH_ENTRY_FREE) { if(hash->keyA == keyA && hash->keyB == keyB && hash->index != HASH_ENTRY_DELETED) return hash; if(++hash >= hashEnd) hash = table; if(hash == hash0) break; } return NULL; }
void hash_insert(struct hash *hash, struct node *n) { unsigned long index = hash_index(n->name); tree_insert(hash->tbl[index], n); }
struct node *hash_search(struct hash *hash, char *key) { return tree_search_node(hash->tbl[hash_index(key)], key); }
uint64_t hash_getpos (Hash *hash, void *key, uint32_t keylen) { int index = hash_index(hash, key, keylen); return hash->bits[index]; }
int main () { printf ("apg_data_structures test\n"); printf ("sizeof llist_node_t %i\nsizeof dllist_node_t %i\n", (int)sizeof (llist_node_t), (int)sizeof (dllist_node_t)); { printf ("\n0) linked list:\n"); llist_node_t* llist_list_ptr = NULL; // add nodes char data = 'a'; llist_add_to_front (&llist_list_ptr, &data, 1); data = 'b'; llist_add_to_front (&llist_list_ptr, &data, 1); data = 'c'; llist_add_to_front (&llist_list_ptr, &data, 1); data = 'd'; llist_node_t* d_ptr = llist_add_to_front (&llist_list_ptr, &data, 1); data = 'e'; llist_add_to_front (&llist_list_ptr, &data, 1); data = 'f'; llist_add_to_front (&llist_list_ptr, &data, 1); // add g 'after' d data = 'g'; llist_node_t* g_ptr = llist_insert_after (d_ptr, &data, 1); printf ("contents of list:\n"); llist_node_t* p = llist_list_ptr; while (p) { printf (" %c\n", *(char*)p->data); p = p->next; } print_mem_allocd (); // remove a node from front, end, middle // delete 'f' assert (llist_delete_node (&llist_list_ptr, llist_list_ptr)); // delete 'a' llist_node_t* end_ptr = llist_find_end_node (llist_list_ptr); assert (llist_delete_node (&llist_list_ptr, end_ptr)); // delete 'd' assert (llist_delete_node (&llist_list_ptr, d_ptr)); printf ("contents of list:\n"); p = llist_list_ptr; while (p) { printf (" %c\n", *(char*)p->data); p = p->next; } print_mem_allocd (); // delete the entire list assert (llist_recursive_delete (&llist_list_ptr)); printf ("contents of list:\n"); p = llist_list_ptr; while (p) { printf (" %c\n", *(char*)p->data); p = p->next; } print_mem_allocd (); } /////////////////////////////////////////////////////////////////////////// { printf ("\n1) doubly-linked list:\n"); dllist_node_t* dllist_list_ptr = NULL; // add nodes char data = 'a'; dllist_add_to_front (&dllist_list_ptr, &data, 1); data = 'b'; dllist_add_to_front (&dllist_list_ptr, &data, 1); data = 'c'; dllist_add_to_front (&dllist_list_ptr, &data, 1); data = 'd'; dllist_node_t* d_ptr = dllist_add_to_front (&dllist_list_ptr, &data, 1); data = 'e'; dllist_add_to_front (&dllist_list_ptr, &data, 1); data = 'f'; dllist_add_to_front (&dllist_list_ptr, &data, 1); // add g 'after' d data = 'g'; dllist_node_t* g_ptr = dllist_insert_after (d_ptr, &data, 1); printf ("contents of list:\n"); dllist_node_t* p = dllist_list_ptr; while (p) { printf (" %c\n", *(char*)p->data); p = p->next; } print_mem_allocd (); // remove a node from front, end, middle // delete 'f' assert (dllist_delete_node (&dllist_list_ptr, dllist_list_ptr)); // delete 'a' dllist_node_t* end_ptr = dllist_find_end_node (dllist_list_ptr); assert (dllist_delete_node (&dllist_list_ptr, end_ptr)); // delete 'd' assert (dllist_delete_node (&dllist_list_ptr, d_ptr)); printf ("contents of list:\n"); p = dllist_list_ptr; while (p) { printf (" %c\n", *(char*)p->data); p = p->next; } print_mem_allocd (); // delete the entire list assert (dllist_recursive_delete (&dllist_list_ptr)); printf ("contents of list:\n"); p = dllist_list_ptr; while (p) { printf (" %c\n", *(char*)p->data); p = p->next; } print_mem_allocd (); } /////////////////////////////////////////////////////////////////////////// #if 0 { printf ("\n2) hash table length 16:\n"); int k = 16; printf ("dog = %i\n", hash_index ("dog", k)); printf ("god = %i\n", hash_index ("god", k)); printf ("cat = %i\n", hash_index ("cat", k)); printf ("hamster = %i\n", hash_index ("hamster", k)); printf ("albatross = %i\n", hash_index ("albatross", k)); printf ("unfavourable stare = %i\n", hash_index ("unfavourable stare", k)); printf ("anton = %i\n", hash_index ("anton", k)); printf ("is = %i\n", hash_index ("is", k)); printf ("and outstanding gentleman, and incredibly humble = %i\n", hash_index ("and outstanding gentleman, and incredibly humble", k)); printf ("suddenly = %i\n", hash_index ("suddenly", k)); printf ("it = %i\n", hash_index ("it", k)); printf ("sprang = %i\n", hash_index ("sprang", k)); printf ("from = %i\n", hash_index ("from", k)); } #endif { int u = 1000, v = 250; reduce_frac( &u, &v ); printf("1000/250 reduces to %i/%i\n", u, v); } return 0; }