void main() { int bloom[20],i,k,l=20,n,m,a[20],h1,h2,b[20]; printf("Enter the number of terms to insert:\n"); scanf("%d",&m); printf("Enter %d values to insert:\n",m); for(i=0;i<m;i++) scanf("%d",&a[i]); for(i=0;i<l;i++) bloom[i]=0; for(i=0;i<m;i++) { k=a[i]; h1=hash1(k,l); h2=hash2(k,l); bloom[h1]=1; bloom[h2]=1; } printf("Enter the number of terms to search:\n"); scanf("%d",&n); printf("Enter %d values to search:\n",n); for(i=0;i<n;i++) scanf("%d",&b[i]); for(i=0;i<n;i++) { k=b[i]; h1=hash1(k,l); h2=hash2(k,l); bfilter(bloom,h1,h2,k); } }
int checkUniqueness(char *url){ int index, index2; index = hash1(url)%(MAX_URL_LENGTH); // printf("url being considered: %s\n", url); // printf("index: %d\n",index); if(dict->hash[index] == NULL){ // if there are no DNODES in the hash table at the gievn index, it is unique // printf("UNIQUE\n"); return 1; } else { DNODE *c = dict->hash[index]; // search through collision cluster (DNODES at the same hash index while(c != NULL){ // while still in the DNODE list // printf("CHECKING %s with %s\n", url, c->key); index2 = hash1(c->key)%(MAX_URL_LENGTH); // rehash next DNODE to test if still in collision cluster if(index != index2){ // exit if you've checked all DNODES in the cluster // printf("UNIQUE\n"); // printf(") return 1; } if(strcmp(url, c->key) == 0){ // if url is in current, it is not unique // printf("NOT UNIQUE\n"); return 0; } else { // go to next DNODE to check c = c->next; } } } // printf("UNIQUE BY DEFAULLT\n"); return 1; // if get to end of list and haven't found }
// Test case: TestLookUp:1 // This test calls lookUp() for the condition where // the query has an OR operator. int TestLookUp1() { START_TEST_CASE; INVERTED_INDEX* testIndex = NULL; int wordHash; int wordHash2; testIndex = initStructure(testIndex); wordHash = hash1("dog") % MAX_NUMBER_OF_SLOTS; DocumentNode* docNode = NULL; docNode = newDocNode(docNode, 15, 1); WordNode* wordNode = NULL; wordNode = newWordNode(wordNode, docNode, "dog"); testIndex->hash[wordHash] = wordNode; wordHash2 = hash1("cat") % MAX_NUMBER_OF_SLOTS; DocumentNode* docNode2 = NULL; docNode2 = newDocNode(docNode2, 20, 2); WordNode* wordNode2 = NULL; wordNode2 = newWordNode(wordNode2, docNode2, "cat"); testIndex->hash[wordHash2] = wordNode2; char query[1000] = "dog OR cat"; sanitize(query); char* temp[1000]; BZERO(temp, 1000); char* queryList[2]; BZERO(queryList, 2); curateWords(queryList, query); SHOULD_BE(strcmp(queryList[0],"dog") == 0); SHOULD_BE(strcmp(queryList[1],"OR") == 0); SHOULD_BE(strcmp(queryList[2],"cat") == 0); DocumentNode* saved[1000]; BZERO(saved, 1000); lookUp(saved, queryList, testIndex); SHOULD_BE(saved[0]->document_id == docNode->document_id); SHOULD_BE(saved[0]->page_word_frequency == docNode->page_word_frequency); SHOULD_BE(saved[1]->document_id == docNode2->document_id); SHOULD_BE(saved[1]->page_word_frequency == docNode2->page_word_frequency); cleanUpList(saved); cleanUpQueryList(queryList); BZERO(saved, 1000); cleanUpIndex(testIndex); END_TEST_CASE; }
void insertHashtable(Hash_item* table1, int htsize, int cid, char *item, int price){ static int cnt=0; int idx=hash1(item,htsize); int idx2=hash2(cid,CSIZE); node* newNode= (node*) malloc(sizeof(node)); newNode->cust_id=cid; newNode->item_cost=price; strcpy(newNode->item_code,item); newNode->next=NULL; if(table1->table[idx]) { if((table1->table[idx])->table[idx2]) { node* temp= table1->table[idx]->table[idx2]; table1->table[idx]->table[idx2]=newNode; newNode->next=temp; } else { table1->table[idx]->table[idx2]=newNode; } } else { table1->table[idx]=(Hash_id*) malloc(sizeof(Hash_id)); memset(table1->table[idx]->table,0,sizeof(table1->table[idx]->table)); table1->table[idx]->table[idx2]=newNode; newNode->next=NULL; } }
BOOLEAN EnvWriteRetrieve(OBJECT env, FILE_NUM fnum, int *offset, int *lnum) { unsigned int pos; OBJECT link, y, z; debug2(DET, DD, "EnvWriteRetrieve(env %d, %s)", (int) env, FileName(fnum)); debug1(DET, DDD, " %s", EchoObject(env)); stat_writes++; hash1(pos, env, fnum); if( tab[pos] != nilobj ) { for( link = Down(tab[pos]); link != tab[pos]; link = NextDown(link) ) { Child(y, link); Child(z, Down(y)); if( env_fnum(y) == fnum && z == env && !env_read(y) ) { MoveLink(LastUp(y), env_cache, PARENT); *offset = env_offset(y); *lnum = env_lnum(y); stat_write_hits++; debug2(DET, DD, "EnvWriteRetrieve returning TRUE (offset %d, lnum %d)", *offset, *lnum); return TRUE; } } } debug0(DET, DD, "EnvWriteRetrieve returning FALSE"); return FALSE; } /* end EnvWriteRetrieve */
void BloomFilter::del(const Key& key) { countDelete++; ////////////// Write your code below //////////////////////// unsigned long hash_out_1 = hash1(key); unsigned long hash_out_2 = hash2(key); }
int findEntry(int htsize,int cust_id, char* item_code) { int key1 = hash1(item_code,htsize); int key2 = hash2(cust_id,htsize); Item *temp = Ht[key1]->Hid[key2]->Head; if(temp!=NULL) { while(temp!=NULL) { if(strcmp(temp->item_code,item_code) == 0 && temp->cust_id == cust_id) { return temp->cost_item; } else { temp = temp->next; } } return -1; } else return -1; }
Hash_item * populateHashTable(char *filename) { Hash_item *ht; Node node; SIZE_T = 100; ht = malloc(sizeof(Hash_item )*SIZE_T); int h1,h2,i,j; FILE *fp; char c[9]; fp = fopen(filename,"r"); while(!feof(fp)) { fscanf(fp,"%d %s %d",&i,c,&j); node.cust_id = i; strcpy(node.item_code,c); node.item_cost = j; h1 = hash1(node.item_code,SIZE_T); h2 = hash2(node.cust_id,20); if(ht[h1].CSIZE!=20) { ht[h1].CSIZE = 20; ht[h1].hashId = malloc(sizeof(Hash_id)*20); } ht[h1].hashId[h2]=node; } return ht; }
int hash(int key, int i, int M){ int address; address = (hash1(key, M) + i * hash2(key, M)) % M; return address; }
void EnvWriteInsert(OBJECT env, FILE_NUM fnum, int offset, int lnum) { unsigned int pos; OBJECT loser, x; debug3(DET, DD, "EnvWriteInsert(env %d, %s, %d)", (int) env, FileName(fnum), offset); /* to limit the cache size, remove least recently used entry if full */ if( cache_count >= MAX_CACHE ) { Child(loser, Down(env_cache)); DeleteLink(Up(loser)); DisposeChild(Up(loser)); cache_count--; } /* insert the new entry */ hash1(pos, env, fnum); if( tab[pos] == nilobj ) New(tab[pos], ACAT); New(x, ACAT); env_fnum(x) = fnum; env_offset(x) = offset; env_lnum(x) = lnum; env_read(x) = FALSE; Link(tab[pos], x); Link(env_cache, x); Link(x, env); cache_count++; debug1(DET, DD, "EnvWriteInsert returning (cache_count = %d)", cache_count); } /* end EnvWriteInsert */
void v_macro(char *str, MNEMONIC *dummy) { STRLIST *base; int defined = 0; STRLIST **slp, *sl; MACRO *mac; /* slp, mac: might be used uninitialised */ MNEMONIC *mne; unsigned int i; char buf[MAXLINE]; int skipit = !(Ifstack->xtrue && Ifstack->acctrue); strlower(str); if (skipit) { defined = 1; } else { defined = (findmne(str) != NULL); if (F_listfile && ListMode) outlistfile(""); } if (!defined) { base = NULL; slp = &base; mac = (MACRO *)permalloc(sizeof(MACRO)); i = hash1(str); mac->next = (MACRO *)MHash[i]; mac->vect = v_execmac; mac->name = strcpy(permalloc(strlen(str)+1), str); mac->flags = MF_MACRO; MHash[i] = (MNEMONIC *)mac; } while (fgets(buf, MAXLINE, pIncfile->fi)) { const char *comment; if (Xdebug) printf("%08lx %s\n", (unsigned long) pIncfile, buf); ++pIncfile->lineno; comment = cleanup(buf, true); mne = parse(buf); if (Av[1][0]) { if (mne && mne->flags & MF_ENDM) { if (!defined) mac->strlist = base; return; } } if (!skipit && F_listfile && ListMode) outlistfile(comment); if (!defined) { sl = (STRLIST *)permalloc(STRLISTSIZE+1+strlen(buf)); strcpy(sl->buf, buf); *slp = sl; slp = &sl->next; } } asmerr( ERROR_PREMATURE_EOF, true, NULL ); }
static void make_forward_references_hash1(void) { int n; init_hash_table(); /* set up the forward references using the last_index hash table */ for (n = 0; n < Number_Of_Texts; n++) { struct text *txt = &Text[n]; size_t j; for ( /* all pos'ns in txt except the last Min_Run_Size-1 */ j = txt->tx_start; /* >= 1 */ j + Min_Run_Size - 1 < txt->tx_limit; j++ ) { if (May_Be_Start_Of_Run(Token_Array[j])) { size_t h = hash1(&Token_Array[j]); if (last_index[h]) { forward_reference[last_index[h]] = j; } last_index[h] = j; } } } Free((char *)last_index); #ifdef DB_FORW_REF db_forward_references("first hashing"); #endif /* DB_FORW_REF */ }
// inserts a page (DocumentNode) into the index int findWordData(char* word, char** list, INVERTED_INDEX* index) { int hashIndex = hash1(word)%MAX_HASH_SLOT; int pos = 0; // something is in there already if (((index->hash[hashIndex]) != NULL) && (!strcmp(word,(index->hash[hashIndex])->word))) { WordNode* node = index->hash[hashIndex]; // printf("\nThe following word is present: %s\n", word); DocumentNode* d; d = node->page; while (d != NULL) { list[pos] = (char*)malloc(sizeof(char)*1000); BZERO(list[pos], 1000); int id = d->document_id; int freq = d->page_word_frequency; sprintf(list[pos], "%d %d ", id, freq); // printf("%d: %s\n", pos, list[pos]); d = d->next; pos++; } return(1); } // nothing is in there already else { printf("\nNo results found for the following word: %s\n", word); return(0); } }
insertHashtable(hash_item ht, int htsize, int cust_id, char *item_code, int cost_item) { int h1 = hash1(item_code, htsize); int h2 = hash2(cust_id, htsize); Hash_id hi = ht.HT_id[h1]; node* n = hi[h2]; add(n, cust_id, item_code, cost_item); }
int insertDict(char* word, int pagenum){ int hash_val=hash1(word)%MAX_HASH_SLOT; DNODE* findnode=dict->hash[hash_val]; while(findnode){ if((hash1(findnode->key)%MAX_HASH_SLOT)!=hash_val) break; //find the node if(strcmp(findnode->key,word)==0) return insertDocNode(findnode,pagenum); findnode=findnode->next; } //The word not currently in the Dict DNODE* rv=insertWordNode(word,hash_val,findnode); return insertDocNode(rv,pagenum); }
// tweaked version of update index to insert an element into the index along with with the frequency int insertElementIntoIndex(char* word, int documentId, int frequency, INVERTED_INDEX* index) { int hashIndex = hash1(word)%MAX_HASH_SLOT; // something is in there already if ((index->hash[hashIndex]) != NULL) { DocumentNode* init; init = (index->hash[hashIndex])->page; if(init->document_id == documentId) { init->page_word_frequency = frequency; (index->hash[hashIndex])->page = init; } else { DocumentNode* d; if(index->hash[hashIndex]) { int flag = 0; d = (index->hash[hashIndex])->page; while ((d->next) != NULL) { d = d->next; if(d->document_id == documentId) { //we found the same documentId d->page_word_frequency = frequency; flag = 1; break; } } if (flag == 0) { // documentId not present DocumentNode* dd = (DocumentNode*)malloc(sizeof(DocumentNode)); MALLOC_CHECK(dd); dd->next = NULL; dd->document_id = documentId; dd->page_word_frequency = frequency; if (d->next == NULL) { d->next = dd; } } } } return 1; } // nothing is in there already else { DocumentNode* doc = malloc(sizeof(DocumentNode)); MALLOC_CHECK(doc); doc->document_id = documentId; doc->page_word_frequency = frequency; doc->next = NULL; // add element into the index IndexAdd(index, (void*)doc, word); return 1; } }
static int file_hash(void *key) { cm_handle_t f1 = (cm_handle_t ) key; int hash_value = 0, i; char *name = ((struct handle *)f1)->name; for (i = 0; i < strlen(name); i++) { hash_value += abs(hash1(name[i])); } return hash_value; }
bool BloomFilter::exist(const Key& key) { countFind++; ////////////// Write your code below //////////////////////// unsigned long hash_out_1 = hash1(key); unsigned long hash_out_2 = hash2(key); if (m_tickBook.at(hash_out_1) == 1) { return true; } return false; //you have to replace this line with your own. }
void BloomFilter::add(const Key& key) { countAdd++; unsigned long hash_out_1 = hash1(key); unsigned long hash_out_2 = hash2(key); unsigned long hash_at_1 = m_tickBook.at(hash_out_1); unsigned long hash_at_2 = m_tickBook.at(hash_out_1); m_tickBook.at(hash_at_1); }
void DoubleHashDict::rehash() { // 221 Students: DO NOT CHANGE OR DELETE THE NEXT FEW LINES!!! // And leave this at the beginning of the rehash() function. // We will use this code when marking to be able to watch what // your program is doing, so if you change things, we'll mark it wrong. #ifdef MARKING_TRACE std::cout << "*** REHASHING " << size; #endif // End of "DO NOT CHANGE" Block // TODO: [done] Your code goes here... std::cout << "rehash" << std::endl; // Keep a pointer to the old table. bucket *old = table; int x = size; // Get a bigger table if (primes[1 + size_index] == -1) return; size = primes[++size_index]; table = new bucket[size](); // Rehash all the data over int h, o; for (int i = 0; i < x; ++i) { if (old[i].key != 0) { // need to add it to new table h = hash1(old[i].key->getUniqId()); o = hash2(old[i].key->getUniqId()); for (int j = 0; j < size; ++j) { if (table[(h + (o * j) % size) % size].key == 0) { table[(h + (o * j) % size) % size].key = old[i].key; table[(h + (o * j) % size) % size].data = old[i].data; break; } } } } delete[] old; // No need to delete the data, as all copied into new table. // 221 Students: DO NOT CHANGE OR DELETE THE NEXT FEW LINES!!! // And leave this at the end of the rehash() function. // We will use this code when marking to be able to watch what // your program is doing, so if you change things, we'll mark it wrong. #ifdef MARKING_TRACE std::cout << " to " << size << " ***\n"; #endif // End of "DO NOT CHANGE" Block }
int checkUniqueness(char *word, INVERTED_INDEX *index) { int hashIndex, hashIndex2; hashIndex = hash1(word)%(MAX_WORD_LEN); if(index->hash[hashIndex] == NULL) { // if there are no DNODES in the hash table at the gievn index, it is unique return 1; } else { WORDNODE *c = index->hash[hashIndex]; // search through collision cluster (DNODES at the same hash index while(c != NULL) { // while still in the DNODE list hashIndex2 = hash1(c->word)%(MAX_WORD_LEN); // rehash next DNODE to test if still in collision cluster if(hashIndex != hashIndex2) { // exit if you've checked all DNODES in the cluster return 1; } if(strcmp(word, c->word) == 0) { // if url is in current, it is not unique return 0; } else { // go to next DNODE to check c = c->next; } } } return 1; // if get to end of list and haven't found }
DNODE* findNode(char* word){ int hash_val=hash1(word)%MAX_HASH_SLOT; DNODE* findnode=dict->hash[hash_val]; while(findnode){ //find the node if(strcmp(findnode->key,word)==0) return findnode; findnode=findnode->next; } return 0; }
bool BloomFilter::exist(const Key& key) { countFind++; unsigned long v1 = hash1(key); unsigned long v2 = hash2(key); int p1 = v1/m_pocketSize; int p2 = v2/m_pocketSize; unsigned long address1 = 1<<(v1 % m_pocketSize); unsigned long address2 = 1<<(v2 % m_pocketSize); return (m_tickBook[p1] & (address1)) && (m_tickBook[p2] & (address2)); }
void brute_hash1(){ //uint8_t csn[8] = {0,0,0,0,0xf7,0xff,0x12,0xe0}; uint8_t k[8]= {0,0,0,0,0,0,0,0}; uint16_t a,b,c,d; //uint8_t testcsn[8] ={0x00,0x0d,0x0f,0xfd,0xf7,0xff,0x12,0xe0} ; uint8_t csn[8] = {0x04,0x0f,0x0f,0xf7,0xf7,0xff,0x12,0xe0}; //uint8_t testkey2[8] = {0}; hash1(csn, k); printf("CSN\t%02x%02x%02x%02x%02x%02x%02x%02x\t%02x %02x %02x %02x %02x %02x %02x %02x\t" ,csn[0],csn[1],csn[2],csn[3],csn[4],csn[5],csn[6],csn[7] ,k[0],k[1],k[2],k[3],k[4],k[5],k[6],k[7] ); //uint8_t testkey[8] ={0x05 ,0x01 ,0x00 ,0x10 ,0x45 ,0x08 ,0x45,0x56} ; calc_score(csn,k); printf("Brute forcing hashones\n"); //exit(1); for(a=0;a < 256;a++) { //if(a > 0)printf("%d/256 done...\n", a); for(b=0;b < 256 ; b++) for(c=0;c < 256;c++) for(d=0;d < 256;d++) { csn[0] = a; csn[1] = b; csn[2] = c; csn[3] = d; csn[4] = 0xf7; csn[5] = 0xff; csn[6] = 0x12; csn[7] = 0xe0; hash1(csn, k); calc_score(csn,k); } } }
int findEntry(Hash_item ht, int htsize, int cust_id, char *item_code){ // This function searches for an entry corresponding to customer's id, cust_id, and item_code. This function returns the cost of the item purchased by the customer if found; else it returns -1. int idx=hash1(item_code,T_SIZE); int idx2=hash2(cust_id,CSIZE); node* temp=ht.table[idx]->table[idx2]; while(temp) { if(temp->cust_id==cust_id && strcmp(temp->item_code,item_code)==0) { return temp->item_cost; } temp=temp->next; } return -1; }
void VMACTest::CompareAccess(std::vector<byte> &Key, std::vector<byte> &Iv) { std::vector<byte> hash1(20); CEX::Mac::VMAC mac; mac.Initialize(Key, Iv); mac.BlockUpdate(m_input, 0, m_input.size()); mac.DoFinal(hash1, 0); std::vector<byte> hash2(20); mac.ComputeMac(m_input, hash2); if (hash1 != hash2) throw std::string("VMACTest: hash is not equal!"); }
static hashmapEntry* find(const hashmap* map, const char* key) { unsigned long index, step, initialIndex; hashmapEntry* freeEntry = 0; initialIndex = index = hash1(key) & map->size; /* first try */ if (map->array[index].key) { if (!strcmp(map->array[index].key, key)) return &map->array[index]; } else if (!map->array[index].data) { return &map->array[index]; } else { freeEntry = &map->array[index]; } /* collision */ step = (hash2(key) % map->size) + 1; do { index = (index + step) & map->size; if (map->array[index].key) { if (!strcmp(map->array[index].key, key)) return &map->array[index]; } else if (!map->array[index].data) { return (freeEntry) ? freeEntry : &map->array[index]; } else if (!freeEntry) { freeEntry = &map->array[index]; } } while (index != initialIndex); return freeEntry; }
void addhashtable(MNEMONIC *mne) { int i, j; unsigned int opcode[NUMOC]; for (; mne->vect; ++mne) { memcpy(opcode, mne->opcode, sizeof(mne->opcode)); for (i = j = 0; i < NUMOC; ++i) { mne->opcode[i] = 0; /* not really needed */ if (mne->okmask & (1L << i)) mne->opcode[i] = opcode[j++]; } i = hash1(mne->name); mne->next = MHash[i]; MHash[i] = mne; } }
void hash(int array[],int hashtable[]) { int i,first,second,count,off; int left = 0; for ( i = 0; i < 120; ++i) { first = hash1(array[i]); if(hashtable[first]==-1) { hashtable[first] = array[i]; printf("the first hashed index for %d is %d\n",array[i],first); } else { printf("occupied by %d\n",hashtable[first] ); count = 0; second = first; off = offset(array[i]); while(count<25) { second = second + off; if (second>172) second -= 173; if (hashtable[second]==-1) { hashtable[second] = array[i]; break; } else count++; } if (count>=25)//no space found { left++; printf("no space found for %d\n",array[i]); } else printf("hashed %d at %d in %d th attempt\n",array[i],second,count+1); } } printf("\nleft are %d \n",left); }
void BloomFilter::del(const Key& key) { countDelete++; unsigned long v1 = hash1(key); unsigned long v2 = hash2(key); unsigned int p1 = v1/m_pocketSize; unsigned int p2 = v2/m_pocketSize; unsigned long address1 = 1<<(v1 % m_pocketSize); unsigned long address2 = 1<<(v2 % m_pocketSize); m_tickBook[p1] &= ~address1; m_tickBook[p2] &= ~address2; }