int find_place(Hash_Table *t, char *name) { int size = t->size; Label *table = t->table; int key = hashf(name, size); if (table[key].name != NULL && strcmp(table[key].name, name)) { int step = hashf(name, size - 1) + 1; int probe = (key + step) % size; while (table[probe].name != NULL) { if (!strcmp(table[probe].name, name)) { break; } probe = (probe + step) % size; } key = probe; } return key; }
/* * std::hash specialization for ChaTIN::ConferenceId. * Produces hash by xoring hash of each ConferenceId's component. * MBO: Method uses strings which are created from ConferenceId's components. It could be more efficient if there is own implementation of hash function, without using hash<std::string> (which needs creatinion of std::string from Glib::ustring). * @param const ChaTIN::ConferenceId& Reference to ConferenceId to be hash evaluated of. * @return size_t Evaluated hash. */ size_t operator()(const ChaTIN::ConferenceId& v) const { hash<std::string> hashf; size_t resultHash; std::string owneripString = v.ownerip; std::string nameString = v.name; resultHash = hashf(owneripString) ^ hashf(nameString); return resultHash; }
bool primitivePut(pHashmap self, pHashmapElem elem, size_t hashv) { if(!attemptPut(self, elem, hashf(self, hashv))) { // no free slot found - at least double the size of the elems array and // rehash // Note: the slot has to be re-computed SEND(self, rehash, nextPrimeOrNumber(self->size * 2 + 1)); return attemptPut(self, elem, hashf(self, hashv)); } return true; }
/* * find the most recent hash node for name name (ending with first * non-identifier char) installed by install * * If LEN is >= 0, it is the length of the name. * Otherwise, compute the length by scanning the entire name. * * If HASH is >= 0, it is the precomputed hash code. * Otherwise, compute the hash code. */ HASHNODE * cpp_lookup(const char *name, int len, int hash) { const char *bp; HASHNODE *bucket; if (len < 0) { for (bp = name; IS_IDCHAR(*bp); bp++) ; len = bp - name; } if (hash < 0) hash = hashf(name, len, HASHSIZE); bucket = hashtab[hash]; while (bucket) { if (bucket->length == len && strncmp((const char *)bucket->name, name, len) == 0) return bucket; bucket = bucket->next; } return (HASHNODE *) 0; }
void inserthash(Hash *hash, Avl *data) { unsigned long cur = hashf(data, hash->order); Avl *oldp; if (hash->map[cur] == NULL) hash->map[cur] = mkavltree(&avlcmp); insertavl(hash->map[cur], data, &oldp); }
int do_search( s_history *history, s_cubies cubies[48], char hash_table[48][6912], int depth ) { int h = hashf(history, cubies); int i, k; int q = (history->cur_phase / 2 * 19 + 8) << 7; if ((depth < hash_table[history->cur_phase + 0][h % q] | depth < hash_table[history->cur_phase + 4][h / q]) ^ history->search_mode) { if (history->search_mode) { // std::cout << "H: " << static_cast<int>(h) << std::endl; if (depth <= history->depth_to_go[h]) return !h; else history->depth_to_go[h] = depth; } hash_table[history->cur_phase + 0][h % q] = std::min(hash_table[history->cur_phase + 0][h % q], static_cast<char>(depth)); hash_table[history->cur_phase + 4][h / q] = std::min(hash_table[history->cur_phase + 4][h / q], static_cast<char>(depth)); for (k = 0; k < 6; ++k) { for (i = 0; i < 4; ++i) { rotate(k, cubies); if (k < history->cur_phase * 2 & i != 1 || i > 2) continue; history->moves[history->index + 0] = k; history->rotpt[history->index + 0] = i; ++history->index; if (do_search(history, cubies, hash_table, depth - history->search_mode * 2 + 1)) return 1; --history->index; } } } return 0; }
size_t operator()(const ChaTIN::IPv6& v) const { hash<std::string> hashf; size_t resultHash; std::string ipv6String = v.c_str(); //MBO @see hash<ChaTIN::ConferenceId>::operator() resultHash = hashf(ipv6String); return resultHash; }
double HashTable::get( int key) { int hash = hashf( key ); if ( entryTable[hash] == NULL ) return -1; else return entryTable[hash]->getValue(); }
void HashTable::add( int key, double v ) { int hash = hashf( key ); if ( entryTable[hash] != NULL ) // key is already in use: do not add a new value. return; else entryTable[hash] = new HashTableEntry( key, v ); }
static int nhash_find_insert(int *set, int setsize) { int j, found, *currlist; struct nhash_list *tableptr; unsigned int hashval; hashval = hashf(set, setsize); if ((table+hashval)->size == 0) { return(nhash_insert(hashval, set, setsize)); } else { for (tableptr=(table+hashval); tableptr != NULL; tableptr = tableptr->next) { if ((tableptr)->size != setsize) { continue; } else { /* Compare the list at hashval position */ /* to the current set by looking at etable */ /* entries */ for (j=0, found = 1, currlist= set_table+tableptr->set_offset; j < setsize; j++) { if (*(e_table+(*(currlist+j))) != (mainloop-1)) { found = 0; break; } } if (op == SUBSET_TEST_STAR_FREE && found == 1) { for (j=0, currlist= set_table+tableptr->set_offset; j < setsize; j++) { if (*(set+j) != *(currlist+j)) { /* Set mark */ star_free_mark = 1; } } } if (found == 1) { return(tableptr->setnum); } } } if (nhash_load / NHASH_LOAD_LIMIT > nhash_tablesize) { nhash_rebuild_table(); hashval = hashf(set, setsize); } return(nhash_insert(hashval, set, setsize)); } }
hash_node_type * hash_node_alloc_new(const char *key, node_data_type * data, hashf_type *hashf , uint32_t table_size) { hash_node_type *node; node = (hash_node_type*) util_malloc(sizeof *node ); node->key = util_alloc_string_copy( key ); node->data = data; node->next_node = NULL; node->global_index = hashf(node->key , strlen(node->key)); hash_node_set_table_index(node , table_size); return node; }
void hash_interface(){ int end = 0, unit = 100; char input, string[MAX_LINE_LENGTH]; while(!end){ if(PPRINT){ printf("\n \033[1;31mHash Table commands:\033[0m \n %3s \t %s \n %3s \t %s \n %3s \t %s \n %3s \t %s \n %3s \t %s \n \n: ", "\033[1;32mh\033[0m", "calculate the hash of a string", "\033[1;32ml\033[0m", "print the load factor of the authors' hash table", "\033[1;32md\033[0m", "print a histogram of the collisions in the hash table", "\033[1;32mc\033[0m", "print the number of keys in the hash table", "\033[1;32mq\033[0m", "return to main menu"); } else{ printf("\n Hash Table commands: \n %3s \t %s \n %3s \t %s \n %3s \t %s \n %3s \t %s \n %3s \t %s \n \n: ", "h", "calculate the hash of a string", "l", "print the load factor of the authors' hash table", "d", "print a histogram of the collisions in the hash table", "c", "print the number of authors in the hash table", "q", "return to main menu"); } scanf(" %c", &input); switch (input) { case 'h': printf("string to hash: "); flush_input_buffer(); fgets(string, MAX_LINE_LENGTH, stdin); printf("\n %ld\n", hashf(string, AUTHORS_HASH_DIM)); break; case 'l' : printf("load factor: %f", (float)authors_dict->n_keys / (float) authors_dict->modulo); break; case 'd': printf("set the number of authors a dot in the histogram corresponds to (default %d): ", unit); flush_input_buffer(); fgets(string, MAX_LINE_LENGTH, stdin); if(strcmp(string, "\n") != 0) unit = atoi(string); print_hash_histogram(authors_dict, unit); break; case 'c': printf("\n %d keys \n", authors_dict->n_keys); break; case 'q': end = 1; break; } } }
void _Hashmap_rehash(void* _self, size_t newSize) { pHashmap self = (pHashmap)_self; size_t oldSize = self->size; pHashmapElem* oldElems = self->elems; self->size = newSize; self->elems = (pHashmapElem*)internal_allocate( self->size * sizeof(pHashmapElem)); SEND(self, clear); for(size_t i = 0; i < oldSize; i++) if(oldElems[i]) { if(!attemptPut(self, oldElems[i], hashf(self, SEND(oldElems[i], key_hash)))) // not good, can't help Universe_error_exit("Hashmap overflow while rehashing.\n"); else oldElems[i] = NULL; } internal_free(oldElems); }
static void nhash_rebuild_table () { int i, oldsize; struct nhash_list *oldtable, *tableptr, *ntableptr, *newptr; unsigned int hashval; oldtable = table; oldsize = nhash_tablesize; nhash_load = 0; for (i=0; primes[i] < nhash_tablesize; i++) { } nhash_tablesize = primes[(i+1)]; table = xxcalloc(nhash_tablesize,sizeof(struct nhash_list)); for (i=0; i < oldsize;i++) { if ((oldtable+i)->size == 0) { continue; } tableptr = oldtable+i; for ( ; tableptr != NULL; (tableptr = tableptr->next)) { /* rehash */ hashval = hashf(set_table+tableptr->set_offset,tableptr->size); ntableptr = table+hashval; if (ntableptr->size == 0) { nhash_load++; ntableptr->size = tableptr->size; ntableptr->set_offset = tableptr->set_offset; ntableptr->setnum = tableptr->setnum; ntableptr->next = NULL; } else { newptr = xxmalloc(sizeof(struct nhash_list)); newptr->next = ntableptr->next; ntableptr->next = newptr; newptr->setnum = tableptr->setnum; newptr->size = tableptr->size; newptr->set_offset = tableptr->set_offset; } } } nhash_free(oldtable, oldsize); }
/* * install a name in the main hash table, even if it is already there. * name stops with first non alphanumeric, except leading '#'. * caller must check against redefinition if that is desired. * delete_macro () removes things installed by install () in fifo order. * this is important because of the `defined' special symbol used * in #if, and also if pushdef/popdef directives are ever implemented. * * If LEN is >= 0, it is the length of the name. * Otherwise, compute the length by scanning the entire name. * * If HASH is >= 0, it is the precomputed hash code. * Otherwise, compute the hash code. */ HASHNODE * install(const char *name, int len, enum node_type type, int ivalue, char *value, int hash) { HASHNODE *hp; int i, bucket; const char *p; if (len < 0) { p = name; while (IS_IDCHAR(*p)) p++; len = p - name; } if (hash < 0) hash = hashf(name, len, HASHSIZE); i = sizeof(HASHNODE) + len + 1; hp = (HASHNODE *) xmalloc(i); bucket = hash; hp->bucket_hdr = &hashtab[bucket]; hp->next = hashtab[bucket]; hashtab[bucket] = hp; hp->prev = NULL; if (hp->next) hp->next->prev = hp; hp->type = type; hp->length = len; if (hp->type == T_CONST) hp->value.ival = ivalue; else hp->value.cpval = value; hp->name = ((char *)hp) + sizeof(HASHNODE); memcpy(hp->name, name, len); hp->name[len] = 0; return hp; }
Avl *lookuphash(Hash *hash, Avl *data) { unsigned long cur = hashf(data, hash->order); return (lookupavl(hash->map[cur], data)); }
int lizard_compress(const byte *in, uns in_len, byte *out) /* Requires out being allocated for at least in_len * LIZARD_MAX_MULTIPLY + * LIZARD_MAX_ADD. There must be at least LIZARD_NEEDS_CHARS characters * allocated after in. Returns the actual compressed length. */ { hash_ptr_t hash_tab[HASH_SIZE]; struct hash_record hash_rec[HASH_RECORDS]; const byte *in_end = in + in_len; byte *out_start = out; const byte *copy_start = in; uns head = 1; /* 0 in unused */ uns to_delete = 0, bof = 1; bzero(hash_tab, sizeof(hash_tab)); /* init the hash-table */ while (in < in_end) { uns hash = hashf(in); byte *best = NULL; uns len = find_match(hash_tab[hash], hash_rec, in, in_end, &best, head); if (len < 3) #if 0 // TODO: now, our routine does not detect matches of length 2 if (len == 2 && (in - best->string - 1) < (1<<10)) { /* pass-thru */ } else #endif { literal: head = hash_string(hash_tab, hash, hash_rec, head, &to_delete); in++; /* add a literal */ continue; } if (in + len > in_end) /* crop EOF */ { len = in_end - in; if (len < 3) goto literal; } /* Record the match. */ uns copy_len = in - copy_start; uns is_in_copy_mode = bof || copy_len >= 4; uns shift = in - best - 1; /* Try to use a 2-byte sequence. */ #if 0 if (len == 2) { if (is_in_copy_mode || !copy_len) /* cannot use with 0 copied characters, because this bit pattern is reserved for copy mode */ goto literal; else goto dump_2sequence; } else #endif /* now, len >= 3 */ if (shift < (1<<11) && len <= 8) { shift |= (len-3 + 2)<<11; dump_2sequence: if (copy_len) out = flush_copy_command(bof, out, copy_start, copy_len); *out++ = (shift>>6) & ~3; /* shift fits into 10 bits */ *out++ = shift & 0xff; } else if (len == 3 && is_in_copy_mode)
/** * Get an element from a hash map */ void* _Hashmap_get(void* _self, void* key) { pHashmap self = (pHashmap)_self; return primitiveGet(self, key, hashf(self, ((pOOObject)key)->hash)); }