static void GrowAtomTable(void) { CACHE_REGS UInt size = LOCAL_ExportAtomHashTableSize; export_atom_hash_entry_t *p, *newt, *oldt = LOCAL_ExportAtomHashChain; UInt new_size = size + (size > 1024 ? size : 1024); UInt i; newt = (export_atom_hash_entry_t *)calloc(new_size,sizeof(export_atom_hash_entry_t)); if (!newt) { return; } p = oldt; for (i = 0 ; i < size ; p++,i++) { Atom a = p->val; export_atom_hash_entry_t *newp; CELL hash; const unsigned char *apt; if (!a) continue; apt = RepAtom(a)->UStrOfAE; hash = HashFunction(apt)/(2*sizeof(CELL)) % new_size; newp = newt+hash; while (newp->val) { newp++; if (newp == newt+new_size) newp = newt; } newp->val = a; } LOCAL_ExportAtomHashChain = newt; LOCAL_ExportAtomHashTableSize = new_size; free(oldt); }
/* hash table */ VALUE HashGet(VALUE key_identifier, HASH_TABLE* table) { unsigned int index; KEY_VALUE_PAIR* table_entries; index = HashFunction(key_identifier); index = index % table->capacity; table_entries = table->table; if (key_identifier.type == VAL_STRING) { while (table_entries[index].key.type != VAL_NIL && !(table_entries[index].key.type == VAL_STRING && strcmp(table_entries[index].key.data.string, key_identifier.data.string) == 0)) { index = (index+1) % table->capacity; } } else { while (table_entries[index].key.type != VAL_NIL && !(key_identifier.type == table_entries[index].key.type && key_identifier.data.primitive == table_entries[index].key.data.primitive)) { index = (index+1) % table->capacity; } } if (table_entries[index].key.type == VAL_NIL) { VALUE null; null.type = VAL_NIL; null.data.primitive = 0; return null; } return table_entries[index].value; }
/* * If present, get an Inode from the InodeTable. * * PRE: * <none> * * POST: * <none> * * IN: * Super : Superblock of the file system the inode resides in * InodeNo : Inode nr of the Inode * * OUT * <none> * * RETURNS * pointer to TInode if found, otherwise 0. */ static TInode* InodeGetFromTable(TSuperBlock *Super, ULONG InodeNo) { UINT hash; TInode *inode, *hashfirst; hash = HashFunction((ULONG) Super->Device, InodeNo); VxdDebugPrint(D_INODE, "InodeGetFromTable: dev=%s inono=%lu, hash=%u", Super->DeviceName, InodeNo, hash); inode = hashfirst = sInodeTable[hash]; if(!inode) return 0; /* * loop over the (circular) list to see if it's * there */ do { if (inode->Super->Device == Super->Device && inode->InodeNo == InodeNo) goto getfromtable_done; } while ( (inode = inode->InodeNext) != hashfirst); inode = 0; getfromtable_done: VxdDebugPrint(D_INODE, "InodeGetFromTable: inode %s", inode ? "found":"not found"); return inode; }
static void LookupAtom(Atom at) { CACHE_REGS const unsigned char *p = RepAtom(at)->UStrOfAE; CELL hash = HashFunction(p) % LOCAL_ExportAtomHashTableSize; export_atom_hash_entry_t *a; a = LOCAL_ExportAtomHashChain+hash; while (a->val) { if (a->val == at) { return; } a++; if (a == LOCAL_ExportAtomHashChain+LOCAL_ExportAtomHashTableSize) a = LOCAL_ExportAtomHashChain; } a->val = at; LOCAL_ExportAtomHashTableNum++; if (LOCAL_ExportAtomHashTableNum > LOCAL_ExportAtomHashTableSize/2 ) { GrowAtomTable(); if (!LOCAL_ExportAtomHashChain) { return; } } }
bool HashClass::findWord(string word) { int index; int tries = 0; index = HashFunction(word); bool nameFound = false; int line; int column; int frequency; Node* nodePtr; nodePtr = HashTable[index]; //cout << "word at index is " << nodePtr->word << endl; while (nodePtr != NULL) { if (nodePtr->word == word) { nameFound = true; line = nodePtr->line; column = nodePtr->column; frequency = nodePtr->frequency; } nodePtr = nodePtr->next; tries++; } //cout << "It took " << tries << " until word was found" << endl; return nameFound; // if found, return true }
typename Utilities::Map< EntryType >::TableEntry* Utilities::Map< EntryType >::LookupTableEntry( const char* entryName, List** tableEntryList /*= 0*/, int* tableEntryHash /*= 0*/ ) { List* list = 0; if( !tableEntryList ) tableEntryList = &list; int hash = HashFunction( entryName ); if( tableEntryHash ) *tableEntryHash = hash; int index = hash % hashTableSize; *tableEntryList = &hashTable[ index ]; TableEntry* tableEntry = ( TableEntry* )( *tableEntryList )->LeftMost(); while( tableEntry ) { if( tableEntry->hash == hash ) { // My hash function isn't very good. Further qualify // the match by making sure that the entry names match. if( 0 == strcmp( tableEntry->entryName, entryName ) ) break; } tableEntry = ( TableEntry* )tableEntry->Right(); } return tableEntry; }
void DictPutNode(Dict* dict, DictNode* node) { if(dict->used >= (int)(dict->capacity * (2.0f / 3.0f))) DictResize(dict, dict->capacity * 2); node->activeIndex = dict->active.length; unsigned long hash = HashFunction(node->key) % dict->capacity; if(!dict->buckets[hash]) { dict->buckets[hash] = node; dict->used += 1; while(dict->active.length + 1 >= dict->active.capacity) { dict->active.capacity *= 2; dict->active.data = erealloc(dict->active.data, sizeof(int) * dict->active.capacity); } dict->active.data[dict->active.length++] = hash; } else { node->next = dict->buckets[hash]; dict->buckets[hash] = node; } ++dict->numEntries; }
void HashClass::add(string word, int line, int column) { int index; index = HashFunction(word); // if bucket is empty if (HashTable[index]->word == "empty") { HashTable[index]->word = word; HashTable[index]->line = line; HashTable[index]->column = column; HashTable[index]->frequency = 1; }// end if else // if bucket is not empty { Node* nodePtr = HashTable[index]; // node pointing to head Node* newNode = new Node; Node* prev = NULL; newNode->word = word; newNode->line = line; newNode->column = column; newNode->frequency = 1; newNode->next = NULL; while (nodePtr != NULL) { prev = nodePtr; nodePtr = nodePtr->next; } // end while prev->next = newNode; } // end else }
void Yap_LookupAtomWithAddress(const char *atom, AtomEntry *ae) { /* lookup atom in atom table */ register CELL hash; register const unsigned char *p; Atom a; /* compute hash */ p = (const unsigned char *)atom; hash = HashFunction(p) % AtomHashTableSize; /* ask for a WRITE lock because it is highly unlikely we shall find anything */ WRITE_LOCK(HashChain[hash].AERWLock); a = HashChain[hash].Entry; /* search atom in chain */ if (SearchAtom(p, a) != NIL) { Yap_Error(SYSTEM_ERROR_INTERNAL, TermNil, "repeated initialization for atom %s", ae); WRITE_UNLOCK(HashChain[hash].AERWLock); return; } /* add new atom to start of chain */ NOfAtoms++; ae->NextOfAE = a; HashChain[hash].Entry = AbsAtom(ae); ae->PropsOfAE = NIL; strcpy((char *)ae->StrOfAE, (char *)atom); INIT_RWLOCK(ae->ARWLock); WRITE_UNLOCK(HashChain[hash].AERWLock); }
void Yap_ReleaseAtom(Atom atom) { /* Releases an atom from the hash chain */ register Int hash; register const unsigned char *p; AtomEntry *inChain; AtomEntry *ap = RepAtom(atom); char unsigned *name = ap->UStrOfAE; /* compute hash */ p = name; hash = HashFunction(p) % AtomHashTableSize; WRITE_LOCK(HashChain[hash].AERWLock); if (HashChain[hash].Entry == atom) { NOfAtoms--; HashChain[hash].Entry = ap->NextOfAE; WRITE_UNLOCK(HashChain[hash].AERWLock); return; } /* else */ inChain = RepAtom(HashChain[hash].Entry); while (inChain->NextOfAE != atom) inChain = RepAtom(inChain->NextOfAE); WRITE_LOCK(inChain->ARWLock); inChain->NextOfAE = ap->NextOfAE; WRITE_UNLOCK(inChain->ARWLock); WRITE_UNLOCK(HashChain[hash].AERWLock); }
void HashClass::printWord(string word) { int index; index = HashFunction(word); bool nameFound = false; int line; int column; int frequency; Node* nodePtr; nodePtr = HashTable[index]; while (nodePtr != NULL) { if (nodePtr->word == word) { nameFound = true; line = nodePtr->line; column = nodePtr->column; frequency = nodePtr->frequency; } nodePtr = nodePtr->next; } if (nameFound) { cout << "The word <" << word << "> was found." << endl; cout << "Line #: " << line << endl; cout << "Column # " << column << endl; cout << "Frequency: " << frequency << endl; } else { cout << "The word <" << word << "> cannot be found in hash table." << endl; } }
void ResizeHashTable(HASH_TABLE* table) { // double size int new_capacity; KEY_VALUE_PAIR* new_table; new_capacity = table->capacity * HT_RESIZE_FACTOR; new_table = (KEY_VALUE_PAIR*)malloc(new_capacity * sizeof(KEY_VALUE_PAIR)); memset((void*)new_table, 0, new_capacity * sizeof(KEY_VALUE_PAIR)); int itr; int cap; unsigned int index; KEY_VALUE_PAIR pair; cap = table->capacity; // copy table for (itr = 0; itr < cap; itr++) { if (table->table[itr].key.type != VAL_NIL) { pair = table->table[itr]; index = HashFunction(pair.key) % new_capacity; while (new_table[index].key.type != VAL_NIL) { index = (index + 1) % new_capacity; } new_table[index] = pair; } } table->capacity = new_capacity; // free old array free(table->table); table->table = new_table; }
void hstring::Init(const char *str) { if(!str) { mId=0; return; } int hash=HashFunction(str); int id=HashHelper().FindFirst(hash); while (id) { assert(id>0&&id<ThePool().mNextStringId); if (!strcmp(str,gCharPtrs[id])) { mId=id; return; } id=HashHelper().FindNext(); } char *raw=ThePool().Alloc(strlen(str),mId); strcpy(raw,str); HashHelper().Add(hash,mId); #ifdef _DEBUG int test; raw=TheDebugPool().Alloc(strlen(str),test); assert(test==mId); strcpy(raw,str); #endif }
VarEntry * Yap_LookupVar(char *var) /* lookup variable in variables table */ { CACHE_REGS VarEntry *p; #if DEBUG if (GLOBAL_Option[4]) fprintf(stderr,"[LookupVar %s]", var); #endif if (var[0] != '_' || var[1] != '\0') { VarEntry **op = &LOCAL_VarTable; unsigned char *vp = (unsigned char *)var; UInt hv; p = LOCAL_VarTable; hv = HashFunction(vp) % AtomHashTableSize; while (p != NULL) { CELL hpv = p->hv; if (hv == hpv) { Int scmp; if ((scmp = strcmp(var, p->VarRep)) == 0) { p->refs++; return(p); } else if (scmp < 0) { op = &(p->VarLeft); p = p->VarLeft; } else { op = &(p->VarRight); p = p->VarRight; } } else if (hv < hpv) { op = &(p->VarLeft); p = p->VarLeft; } else { op = &(p->VarRight); p = p->VarRight; } } p = (VarEntry *) Yap_AllocScannerMemory(strlen(var) + sizeof(VarEntry)); *op = p; p->VarLeft = p->VarRight = NULL; p->hv = hv; p->refs = 1L; strcpy(p->VarRep, var); } else { /* anon var */ p = (VarEntry *) Yap_AllocScannerMemory(sizeof(VarEntry) + 2); p->VarLeft = LOCAL_AnonVarTable; LOCAL_AnonVarTable = p; p->VarRight = NULL; p->refs = 0L; p->hv = 1L; p->VarRep[0] = '_'; p->VarRep[1] = '\0'; } p->VarAdr = TermNil; return (p); }
static Atom LookupAtom(const unsigned char *atom) { /* lookup atom in atom table */ uint64_t hash; const unsigned char *p; Atom a, na; AtomEntry *ae; size_t sz = AtomHashTableSize; /* compute hash */ p = atom; hash = HashFunction(p); hash = hash % sz ; /* we'll start by holding a read lock in order to avoid contention */ READ_LOCK(HashChain[hash].AERWLock); a = HashChain[hash].Entry; /* search atom in chain */ na = SearchAtom(atom, a); if (na != NIL) { READ_UNLOCK(HashChain[hash].AERWLock); return (na); } READ_UNLOCK(HashChain[hash].AERWLock); /* we need a write lock */ WRITE_LOCK(HashChain[hash].AERWLock); /* concurrent version of Yap, need to take care */ #if defined(YAPOR) || defined(THREADS) if (a != HashChain[hash].Entry) { a = HashChain[hash].Entry; na = SearchAtom(atom, a); if (na != NIL) { WRITE_UNLOCK(HashChain[hash].AERWLock); return (na); } } #endif /* add new atom to start of chain */ ae = (AtomEntry *)Yap_AllocAtomSpace((sizeof *ae) + strlen((const char *)atom) + 1); if (ae == NULL) { WRITE_UNLOCK(HashChain[hash].AERWLock); return NIL; } NOfAtoms++; na = AbsAtom(ae); ae->PropsOfAE = NIL; if (ae->UStrOfAE != atom) strcpy((char *)ae->StrOfAE, (const char *)atom); ae->NextOfAE = a; HashChain[hash].Entry = na; INIT_RWLOCK(ae->ARWLock); WRITE_UNLOCK(HashChain[hash].AERWLock); if (NOfAtoms > 2 * AtomHashTableSize) { Yap_signal(YAP_CDOVF_SIGNAL); } return na; }
//-------------------------------------------------------------------------- // Add a page to the hash table. If the page is already in the hash table // it is updated and a new time stamp is given. If the page is new then // it is added to the table. The data in buf is put in the page and the // Space location number is returned. // // portnum the port number of the port being used for the // 1-Wire Network. // SNum the serial number for the part that the read is // to be done on. // pg the page to add // buf the buffer of the page data // len len of data for the page // // return the space number for the new page // uchar AddPage(int portnum, uchar *SNum, PAGE_TYPE pg, uchar *buf, int len) { uchar hs,p=0xFF; short i=0,m; PAGE_TYPE page; int tlen; uchar cache_page[32]; page = pg; // attempt to see if page already there if(!FindPage(portnum,SNum,&page,(uchar)(len & 0x80),FALSE, &cache_page[0],&tlen,&p)) return FALSE; if (p == 0xFF) { // page not found so add one hs = HashFunction(SNum,page); p = FindNew(hs); // attach the device to the chain (if there is one) // no other page in hash location if (Hash[hs] == 0xFF) { Hash[hs] = p; // hash p to new page Space[p].Fptr = 0xFF; // np front p to nothing Space[p].Bptr = 0xFF; // np back p to nothing Space[p].Hptr = hs; // np hash p to hash location } // some other page already there else { // insert as first page Space[p].Fptr = Hash[hs]; // np front p to old first page Space[Hash[hs]].Bptr = p; // old first page back p to np Hash[hs] = p; // hash p to np Space[p].Hptr = hs; // np hash p to hash location } // set the page number Space[p].Page = page; // set the rom for (i = 0; i < 8; i++) Space[p].ROM[i] = SNum[i]; } // set the data Space[p].Data[0] = (uchar)len; m = ((len & 0x1F) <= 0x1D) ? (len & 0x1F) : 0; for (i = 0; i < m; i++) Space[p].Data[i+1] = buf[i]; // set the time stamp limit of X seconds Space[p].Tstamp = msGettick() + CACHE_TIMEOUT; // (3.10) // return the Space number of the new page return p; }
bool HashTable::Insert_item(string val) { int index=HashFunction(val); array[index].InsertNodesAtFront(val); size++; return true; }
unsigned int RHTable::RemoteHash( unsigned char *key, unsigned int size ) { mdk::mdk_assert(NULL != HashFunction); unsigned char hashKey[256]; unsigned int hashSize; HashFunction( hashKey, hashSize, key, size ); return DJBHash( hashKey, hashSize ); }
Data* Find(HashTable* table, char key[]){ int index = HashFunction(key); Bucket* ptr = table->buckets[index].next; while(ptr){ if(strcmp(ptr->data.key, key) == 0) return &(ptr->data); ptr = ptr->next; } return NULL; }
void Push(HashTable* table, Data* data){ int index = HashFunction(data.key); Bucket* ptr = &(table->buckets[index]); while(ptr && ptr->next){ ptr = ptr->next; } ptr->next = (Bucket*) malloc(sizeof(Bucket)); ptr = ptr->next; ptr->next = NULL; ptr->data.value = data->value; strcpy(ptr->data.key, data->key); }
void const * FFHeaderI::FindFile(char const * name, size_t * lengthP) const { for (CLIF<FFDataI> i_file(&files[HashFunction(name)]); !i_file.done(); i_file.next()) { if (i_file()==name) { if (lengthP) *lengthP = i_file().GetLength(); return i_file().GetDataPointer(); } } return 0; }
void* DictGet(Dict* dict, const char* key) { unsigned long hash = HashFunction(key) % dict->capacity; DictNode* node = dict->buckets[hash]; while(node) { if(strcmp(node->key, key) == 0) return node->value; node = node->next; } return NULL; }
ReducePrePhase(Context& ctx, size_t dia_id, size_t num_partitions, KeyExtractor key_extractor, ReduceFunction reduce_function, std::vector<BlockWriter>& emit, const ReduceConfig& config = ReduceConfig(), const IndexFunction& index_function = IndexFunction(), const EqualToFunction& equal_to_function = EqualToFunction(), const HashFunction hash_function = HashFunction()) : Super(ctx, dia_id, num_partitions, key_extractor, reduce_function, emit, config, index_function, equal_to_function, hash_function, /*duplicates*/ true), hash_function_(hash_function) { }
int main(int argc, char *argv[]) { char buf[LINE_LEN]; /* word string buffer */ char fileName[LINE_LEN]; /* file name buffer */ int howManyBins; /* number of bins to create */ TABLE *hashTable; /* pointer to hash table */ FILE *fp; /* Read file name from command line */ if (argc < MIN_ARGS || sscanf(argv[FILE_ARG_IX], BUFFMT "s", fileName) != 1) { fprintf(stderr, "No file name specified on command line\n"); return EXIT_FAILURE; } fp = OpenFile(fileName); /* Read bin count from command line */ if (sscanf(argv[BINS_ARG_IX], "%d", &howManyBins) != 1) { fprintf(stderr, "No bin count specified on command line\n"); return EXIT_FAILURE; } hashTable = CreateTable((size_t)howManyBins); /* allocate table array */ /* * The following loop will read one string at a time from stdin until * EOF is reached. For each string read the BuildList function will * be called to update the hash table. */ while (fscanf(fp, BUFFMT "s", buf) != EOF) /* get string from file */ { /* find appropriate bin */ BIN *bin = &hashTable->firstBin[HashFunction(buf, (size_t)howManyBins)]; NODE *newStart = BuildTree(bin->firstNode, buf, bin); /* put string in list */ bin->firstNode = newStart; } fclose(fp); PrintTable(hashTable); /* print all strings */ FreeTable(hashTable); /* free the table */ return(EXIT_SUCCESS); }
int FillTheHashTable ( ListHead** HashTable, int Size, char** Words, int (* HashFunction ) ( char* ) ) { long int i = 0; while ( Words[i] != NULL ) { unsigned UnCode = ( unsigned ) HashFunction ( Words[i] ); if ( UnCode >= Size ) UnCode = UnCode % Size; if ( IsElementInList ( HashTable[UnCode], Words[i] ) == 0 ) AddElement ( HashTable[UnCode], Words[i] ); i++; } return 0; }
/* * Create an TInode structure * * PRE: * The inode is not yet present in the InodeTable, that is, * InodeGetFromTable returned 0. * * POST: * New TInode structure is created and (if succesfull) is chained * in InodeTable. TInode fields initialised: * inode->nrClients = 0 * inode->InodePrev and InodeNext --> used to chain the inode in Table * inode->Super = Super * inode=>ParentInode = ParentInode * * IN: * Super : Superblock of the file system the inode resides in * ParentInode : pointer to TInode of parent inode (may be zero) * InodeNo : Inode nr of the Inode * * OUT * <none> * * RETURNS * pointer to TInode if created, otherwise 0. */ static TInode* InodeCreate(TSuperBlock *Super, TInode *ParentInode, ULONG InodeNo) { UINT hash; TInode *inode, *hashfirst; hash = HashFunction((ULONG) Super->Device, InodeNo); VxdDebugPrint(D_INODE, "InodeCreate: dev=%s, inono=%lu, hash=%u", Super->DeviceName, InodeNo, hash); inode = (TInode *) calloc(1, sizeof(TInode)); if (!inode) { VxdDebugPrint(D_ERROR, "InodeCreate: could not alloc TInode"); goto inocreate_err_1; } /* * Set fields */ inode->ParentInode = ParentInode; inode->InodeNo = InodeNo; inode->Super = Super; inode->nrClients = 0; /* * Link inode in InodeTable, put it first in list. * Special treatment for empty linked list */ if ((hashfirst = sInodeTable[hash])) { hashfirst->InodePrev->InodeNext = inode; inode->InodePrev = hashfirst->InodePrev; inode->InodeNext = hashfirst; hashfirst->InodePrev = inode; sInodeTable[hash] = inode; } else { inode->InodePrev = inode->InodeNext = inode; sInodeTable[hash] = inode; } inocreate_err_1: VxdDebugPrint(D_INODE, "InodeCreate: done"); return inode; }
void ConsolePrintPokedexEntryInPokedex(Pokedex *obj, char *name) { unsigned int hashKey = HashFunction(name); printf("obj->mem->table of %u points to %p\n", hashKey, obj->mem->table[hashKey]); PokedexEntry *pokePtr = obj->mem->table[hashKey]; printf("pokedex points to %p\n", obj); printf("name is %s\n", name); printf("hash function is %u\n", hashKey); printf("pokePtr points to %p\n", pokePtr); while(pokePtr != 0 && NameComparison(pokePtr->name, name)) pokePtr = pokePtr->next; if(pokePtr == 0) printf("ENTRY DOES NOT EXIST\n"); else ConsolePrintPokedexEntry(pokePtr); }
void HashClass::addFrequency(string word) { // search for word and modify frequency int index; index = HashFunction(word); Node* nodePtr; nodePtr = HashTable[index]; while (nodePtr != NULL) { if (nodePtr->word == word) { cout << "adding fre?" << endl; nodePtr->frequency++; } nodePtr = nodePtr->next; }// end while }
void HashClass::removeItem(string word) { int index; index = HashFunction(word); // Case 1: Bucket is empty if (HashTable[index]->word == "empty") { cout << word << " not found in hash table" << endl; }// Case 2: Match is in the first item and it is the only item in the bucket else if (HashTable[index]->word == word && HashTable[index]->next == NULL) { HashTable[index]->word = "empty"; // erase items HashTable[index]->line = 0; HashTable[index]->column = 0; }// Case 3: Match is in first item but more than one item is in the bucket else if (HashTable[index]->word == word) { Node* delPtr; delPtr = HashTable[index]; // set to first item in item in the bucket HashTable[index] = HashTable[index]->next; // second item in list is now the first item delete delPtr; // delPtr still pointing to first item in bucket, delete it }// Case 4: Bucket contains more than one item and bucket may or may not contain match else { Node* nodePtr; nodePtr = HashTable[index]->next; Node* prev; prev = HashTable[index]; while (nodePtr != NULL && nodePtr->word != word) { prev = nodePtr; nodePtr = prev->next; } if (nodePtr == NULL) { cout << word << " was not found in hash table" << endl; } else { Node* delPtr = nodePtr; nodePtr = nodePtr->next; prev->next = nodePtr; delete delPtr; } } }
/** Doesn't include checking if the entry already exists. Will add another if it does. **/ void SetPokedexEntryInPokedex(Pokedex* original, PokedexEntry* obj) { unsigned int hashKey = HashFunction(obj->name); printf("AFTER HASH: hashKey is %d\n\n", hashKey); if(original->mem->table[hashKey] == 0) original->mem->table[hashKey] = CopyPokedexEntry(original, obj); else { PokedexEntry *entryPtr = original->mem->table[hashKey]; printf("INITIALIZE entryPtr\n\n"); while(entryPtr->next != 0) entryPtr = entryPtr->next; printf("Pokedex address is %p\n", original); entryPtr->next = CopyPokedexEntry(original, obj); printf("entryPtr in SetEntry is %p\n", entryPtr); } }