int main(void) { for(int i = 0; i < SIZE; i++) { head[i] = NULL; } char* input = "apple"; char* input2 = "avocado"; int box = hashfunc(input); insertlist(input, box); box = hashfunc(input2); insertlist(input2, box); node* ptr = head[0]; while( ptr != NULL) { printf("%s\n", ptr->s); ptr = ptr->next; } }
// calculate the string for the use by map. // format: 0/1/23/34/34 string Itemset::calcKeyStr(int level) { if(getSize() == 0 || level == 0) return ""; string key = itoa(level) + ":" + itoa(hashfunc(is_items[0].getId())); for(int i = 1; i < level; i++) { key += "/" + itoa(hashfunc(is_items[i].getId())); } return key; }
void initialize_hash(int hashfunc(color_item *), color_item *hashbuckets[], int *initialized) /* initialize color lookup by given hash function */ { if (!*initialized) { FILE *fp; int red, green, blue; char line[BUFSIZ], namebuf[BUFSIZ]; (*initialized)++; if ((fp = fopen(RGBTXT, "r")) == NULL) fatal("RGB database %s is missing.", RGBTXT); else { int st; color_item sc; for (;;) { fgets(line, sizeof(line) - 1, fp); st = sscanf(line, "%d %d %d %[^\n]\n", &red, &green, &blue, namebuf); if (feof(fp)) break; else if (st == 4) { color_item *op, *newcolor, **hashbucket; #ifdef HASHDEBUG printf("* Caching %s = (%u, %u, %u) => %d\n", namebuf, red, green, blue, hashfunc(&sc)); #endif /* HASHDEBUG */ sc.r = (unsigned char)red; sc.g = (unsigned char)green; sc.b = (unsigned char)blue; sc.name = namebuf; hashbucket = &hashbuckets[hashfunc(&sc)]; newcolor = xalloc(sizeof(color_item)); memcpy(newcolor, &sc, sizeof(color_item)); newcolor->name = xstrdup(namebuf); op = *hashbucket; *hashbucket = newcolor; newcolor->next = op; } } fclose(fp); } } }
/*remove this entry from the hashtable and return the value associated with it*/ void* remove_hashtable_entry(hashtable* const hash, const char* const name, const int len) { pre(name != NULL); uint32_t index = hashfunc(name, len); index = index & hash->mask; bin* iter = NULL; bin* bin = NULL; if(hash->bins[index] != NULL){ for(iter = hash->bins[index];iter; iter = iter->next){ if(strncmp(iter->name, name, len) == 0){ bin = iter; break; } } } void* val = NULL; if(bin){ val = bin->val; slremove(&hash->bins[index], bin); ckfree(bin->name); ckfree(bin); hash->elcount--; } return val; }
/* 线性探测法 */ int linehash(int key) { int pos; //位置变量 int temp; //调用散列函数计算位置 pos = hashfunc(key); temp = pos; //保留开始位置 while(hashtable[temp] != key && //线性探测循环 hashtable[temp] != BLANK) { //使用余数将数组变成环状 temp = (temp + 1) % MAX; //下一个位置 if(pos == temp) //查询结束 { return -1; //没有找到 } } if(hashtable[temp] == BLANK) //是否是空白 return -1; else return temp; //找到了 }
/*add the following substring of length len to the hashtable. Return the * bin corresponding to it*/ bin* add_hashtable(hashtable* const hash, /*the hashtable*/ const char* const name, /*the string*/ const int length, /*use 'length' characters of name*/ void* val) /*the value*/ { pre(name != NULL); uint32_t index = hashfunc(name, length); index = index & hash->mask; char* hname = ckalloc(length+1); memcpy(hname, name, length); hname[length] = '\0'; bin* bin = ckallocz(sizeof(struct bin_st)); bin->name = hname; bin->val = val; if(hash->bins[index]){ hash->collisions++; } bin->next = hash->bins[index]; hash->bins[index] = bin; hash->elcount++; post(bin != NULL); return bin; }
identifier *SymbolTable::ChainSearch(const identifier& x, int (*hashfunc) (identifier) ) // Search the chained hash table @ht@ for @x@. On termination, return a pointer // to the identifier in the hash table. If the identifier does not exist, then return 0 { int j = hashfunc(x); // compute headnode address // search the chain starting at @ht[j]@ for (ListPtr l = ht[j]; l; l = l->link) if (l->ident == x) return &l->ident; return 0; }
int INSERISCIHASH (char * key, hdata_t * elem, hash_t hashTable) { int i; posizione p; i = hashfunc(key); if ( CERCAHASH(key, hashTable) == NULL ) { p = hashTable[i]; INSLISTA((void *)elem, &p); return 1; } return 0; }
static unsigned int lookup_table(struct key *key) { unsigned int pos = hashfunc(key) % MAXGLYPHS; while (1) { if (!table[pos].key.font) /* empty slot */ return pos; if (!memcmp(key, &table[pos].key, sizeof(struct key))) /* matching slot */ return pos; pos = (pos + 1) % MAXGLYPHS; } }
bool contains(HASHTABLE<KEY,VALUE> *ht, KEY k){ //tmp point to the root HASHTABLE<KEY,VALUE> *tmp; for (tmp = ht[hashfunc(ht,k)].next; tmp != NULL; tmp=tmp->next) { if(isequal(k,tmp->key)) return true; } return false; }
enum e_fgtype getEdgeType(int first, int second) { int bucket= hashfunc(first, second); EDGE *p = edgeHash[bucket]; while (p) { if (p->first == first && p->second == second) return p->edgetype; p = p->next; } return F_NONE; }
void main(){ unsigned int size; unsigned int k; int h; size = nondetpos(); k = nondetpos(); h = hashfunc(size, k); csolve_assert(h >= 0); csolve_assert(h < size); }
bool load(const char* dictionary) { for(int i = 0; i < SIZE; i++) { hashTable[i] = NULL; } FILE* dictFile = fopen(dictionary, "r"); if (dictFile == NULL) { printf("Could not open dictionary file"); return 1; } while(!feof(dictFile)) { node* new_node = malloc(sizeof(node)); if (new_node != NULL) { if (fscanf(dictFile, "%s", new_node->word) == 1) { if (feof(dictFile)) { break; }else { new_node->next = NULL; int slot = hashfunc(new_node->word); //strcpy (new_node->word, newWord); if(hashTable[slot] == NULL) { hashTable[slot] = new_node; count++; } else { new_node->next = hashTable[slot]; hashTable[slot] = new_node; count++; } } }else free(new_node); } }; fclose(dictFile); return true; }
/* find a service status entry */ servicestatus *find_servicestatus(char *host_name,char *svc_desc){ servicestatus *temp_servicestatus=NULL; if(host_name==NULL || svc_desc==NULL || servicestatus_hashlist==NULL) return NULL; for(temp_servicestatus=servicestatus_hashlist[hashfunc(host_name,svc_desc,SERVICESTATUS_HASHSLOTS)];temp_servicestatus && compare_hashdata(temp_servicestatus->host_name,temp_servicestatus->description,host_name,svc_desc)<0;temp_servicestatus=temp_servicestatus->nexthash); if(temp_servicestatus && (compare_hashdata(temp_servicestatus->host_name,temp_servicestatus->description,host_name,svc_desc)==0)) return temp_servicestatus; return NULL; }
/* find a host status entry */ hoststatus *find_hoststatus(char *host_name){ hoststatus *temp_hoststatus=NULL; if(host_name==NULL || hoststatus_hashlist==NULL) return NULL; for(temp_hoststatus=hoststatus_hashlist[hashfunc(host_name,NULL,HOSTSTATUS_HASHSLOTS)];temp_hoststatus && compare_hashdata(temp_hoststatus->host_name,NULL,host_name,NULL)<0;temp_hoststatus=temp_hoststatus->nexthash); if(temp_hoststatus && (compare_hashdata(temp_hoststatus->host_name,NULL,host_name,NULL)==0)) return temp_hoststatus; return NULL; }
size_t hash_get(hash_t *const hash, char const *const key) { size_t const x = hashfunc(hash, key); if(HASH_NOTFOUND == x) return x; size_t i = x; for(;;) { char const *const k = HASH_KEY(hash, i); if(0 == nulcmp(k, hash->keylen)) break; if(0 == memcmp(k, key, hash->keylen)) return i; i = (i + 1) % hash->count; if(x == i) return HASH_NOTFOUND; } return HASH_NOTFOUND; }
int scene::pushObjToFront(std::string Id) { std::hash<std::string> hashfunc; std::size_t Ohash = hashfunc(Id); for (std::vector<graphObject *>::iterator it = this->drawPipeline.begin(); it != this->drawPipeline.end(); ++it) { if ((*it)->getHash() == Ohash) { std::rotate(it,it,this->drawPipeline.end()); break; }; } return 0; }
void SymbolTable::Insert(identifier x, int (*hashfunc) (identifier)) { int j = hashfunc(x); ListNode *prev = 0; for (ListPtr ptr = ht[j]; ptr && !(ptr->ident == x); ptr = ptr->link) prev = ptr; if (ptr->ident == x) return; // already there ListNode *newnode = new ListNode; newnode->ident = x; newnode->link = 0; if (ht[j] == 0) ht[j] = newnode; // already there else prev->link = newnode; }
// ht is a array of address, defined in buildhashtable; // *ht points to the first node of HASHTABLE // *(ht+index) points to the next one that is pushed in the HASHTABLE; // so index linked with the pushed element void put(HASHTABLE<KEY,VALUE> *ht, KEY k, VALUE v){ if (!contains(ht,k)) { HASHTABLE<KEY,VALUE> *tmp; int index = hashfunc(ht,k); //need to be defined tmp = ht[index].next; //the ht store the beginning address of HASHTABLE //it will let ht+index store the ht[index].next = new HASHTABLE<KEY,VALUE>; //directly allocate ht[].next as a new hash table ht[index].next->key = k; //equals to *(ht+index); ht is a array point to different node of HASHTABLE ht[index].next->value = v; ht[index].next->next = tmp; } }
unsigned int HashTableCounter::get_count(const unsigned int k_item[]) const { unsigned int temp_k_item[m_dimension]; for (unsigned int i = 0; i < m_dimension; i++) { temp_k_item[i] = k_item[i]; } if (quicksort<unsigned int>(temp_k_item, m_dimension, true, true) != m_dimension) return 0; unsigned int hashcode = hashfunc(temp_k_item); if (NULL != m_hash_table[hashcode]) return m_hash_table[hashcode][m_dimension]; else return 0; }
static int gatherEdges(enum e_fgtype type, BLOCK *parent, BLOCK *in) { if (parent) { EDGE *edge = oAlloc(sizeof(EDGE)); int bucket = hashfunc(parent->blocknum, in->blocknum); edge->first = parent->blocknum; edge->second = in->blocknum; edge->edgetype = type; edge->next = edgeHash[bucket]; edgeHash[bucket] = edge; } return TRUE; }
list *find_key(const char *key, hash *h) { list *p; list *l; l = h->listnode[hashfunc(key, h->count)]; p = l->next; while (p != NULL && strcmp(p->data.name, key) != 0) p = p->next; if (p == NULL) printf("can't find this data\n"); return p; }
vector<vector<string>> groupStrings(vector<string>& strings) { vector<vector<string>> result; unordered_map<string, vector<string>> list; for(auto s:strings) { string hv = hashfunc(s); if(list.find(hv)==list.end()) list[hv] = vector<string>(); list[hv].push_back(s); } for(auto item:list) { sort(item.second.begin(), item.second.end()); result.push_back(item.second); } return result; }
size_t hash_del_internal(hash_t *const hash, size_t const x) { if(x >= hash->count) return 0; size_t i = x; for(;;) { size_t const next = (i + 1) % hash->count; if(x == next) break; char const *const k = HASH_KEY(hash, next); if(0 == nulcmp(k, hash->keylen)) break; size_t const alt = hashfunc(hash, k); if(next == alt) break; memcpy(HASH_KEY(hash, i), k, hash->keylen); i = next; } memset(HASH_KEY(hash, i), 0, hash->keylen); return (hash->count + i - x) % hash->count; }
/** * * double_hash_func: This function is used for double hashing, based on another hash function. * * This functions uses the hash function contained in hparam to compute a first hash value, then use it to * compute a second value like this: h = ( firsthash + ( 8 - ( firsthash % 8 ) ) ) % hparam.index_size * This operation just changes the last 3 bits, but it can be demonstrated that this produced a more * efficient and better balanced hash function (See 'Algorithm in C', Robert Sedjewick for more detail on this). * * @param hparam the parameter structure that was used to define the hashtable * @param buffclef the key to compute the hash value on * * @return the hash value * * @see double_hash_func */ unsigned long double_hash_func(hash_parameter_t * p_hparam, hash_buffer_t * buffclef) { unsigned long firsthash = 0; unsigned long h = 0; hash_function_t hashfunc = simple_hash_func; /* first, we find the intial value for simple hashing */ firsthash = hashfunc(p_hparam, buffclef); /* A second value is computed * a second a value is added to the first one, then the modulo is kept * For the second hash, we choose to change the last 3 bit, which is usually a good compromise */ h = (firsthash + (8 - (firsthash % 8))) % p_hparam->index_size; return h; } /* double_hash_func */
HASH_ENTRY* CHash_Map::find(int nkey) { int nhash_value; HASH_ENTRY* phash_entry; nhash_value = hashfunc(nkey); phash_entry = mpentry_list[nhash_value].next; while(phash_entry!=NULL) { if(phash_entry->key==nkey) break; phash_entry = phash_entry->next; } return phash_entry; }
/** * Returns true if word is in dictionary else false. */ bool check(const char* words) { int slot = hashfunc(words); node* ptr = hashTable[slot]; while( ptr != NULL) { if(strcasecmp(ptr->word, words) == 0) { return 1; } ptr = ptr->next; } return 0; }
VALUE get(HASHTABLE<KEY,VALUE> *ht, KEY k){ HASHTABLE<KEY,VALUE> *tmp = ht[hashfunc(ht,k)].next; if (contains(ht,k)) { while(tmp!=NULL){ if (isequal(tmp->key,k)) return tmp->value; tmp = tmp->next; } }else{ cout <<"Cannot be found!\n"; VALUE z_v; return z_v; } }
void LoadGraph::insertNeighborhoods() { NeighborhoodRec r; r.graphID=gid; EdgeMap::iterator mp, mp2; for(int v1=0; v1<G->n(); v1++) { r.degree=G->degree(v1); r.nbConnection=0; r.nodeID=v1; memset((char*)r.nbArray, 0, sizeof(uint16_t)*96/16); debug(1, "after memset\n"); EdgeMap* m=G->getNeighbors(v1); for(mp=m->begin(); mp!=m->end(); mp++) { UINT v2=mp->first; for(unsigned int i=0; i<(*pOrthinfolist)[v2].size(); i++) { unsigned int pos=hashfunc((*pOrthinfolist)[v2][i]) % 96; setBit(r.nbArray, pos); debug(1, "set bit: "<<pos<<endl); } mp2=mp; mp2++; for(; mp2!=m->end(); mp2++) { UINT v3=mp2->first; if(G->isEdge(v2,v3)) r.nbConnection++; debug(1, "checked edge ("<<v2<<", "<<v3<<")\n"); } } for(unsigned int i=0; i<(*pOrthinfolist)[v1].size(); i++) { r.orthologID=(*pOrthinfolist)[v1][i]; debug(1, "before writeNeighborhoodRec\n"); wtf->writeNeighborhoodRec(r); debug(1, "after writeNeighborhoodRec\n"); } } }
nagios_comment *get_next_comment_by_host(char *host_name, nagios_comment *start) { nagios_comment *temp_comment = NULL; if(host_name == NULL || comment_hashlist == NULL) return NULL; if(start == NULL) temp_comment = comment_hashlist[hashfunc(host_name, NULL, COMMENT_HASHSLOTS)]; else temp_comment = start->nexthash; for(; temp_comment && compare_hashdata(temp_comment->host_name, NULL, host_name, NULL) < 0; temp_comment = temp_comment->nexthash); if(temp_comment && compare_hashdata(temp_comment->host_name, NULL, host_name, NULL) == 0) return temp_comment; return NULL; }