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); } }
static void make_forward_references_hash2(void) { size_t i; /* Clean out spurious matches, by a quadratic algorithm. Note that we do not want to eliminate overlapping sequences in this stage, since we might be removing the wrong copy. */ for (i = 0; i+Min_Run_Size < Text_Length(); i++) { size_t j = i; size_t h2 = hash2(&Token_Array[i]); /* Find the first token sequence in the chain with same secondary hash code. */ while ( /* there is still a forward reference */ (j = forward_reference[j]) && /* its hash code does not match */ hash2(&Token_Array[j]) != h2 ) { /* continue searching */ } /* short-circuit forward reference to it, or to zero */ forward_reference[i] = j; } #ifdef DB_FORW_REF db_forward_references("second hashing"); #endif /* DB_FORW_REF */ }
void func(struct Packet pkt) { pkt.filter1_idx = hash2(pkt.sport, pkt.dport) % NUM_ENTRIES; pkt.filter2_idx = hash2(pkt.sport, pkt.dport) % NUM_ENTRIES; pkt.filter3_idx = hash2(pkt.sport, pkt.dport) % NUM_ENTRIES; pkt.member = (filter1[pkt.filter1_idx] && filter2[pkt.filter2_idx] && filter3[pkt.filter3_idx]); filter1[pkt.filter1_idx] = 1; filter2[pkt.filter2_idx] = 1; filter3[pkt.filter3_idx] = 1; }
int main(void) { FILE *fin = fopen("ride.in", "r"); FILE *fout = fopen("ride.out", "w"); char a[7], b[7]; fscanf(fin, "%s", a); fscanf(fin, "%s", b); if (hash2(a) == hash2(b)) fprintf(fout, "GO\n"); else fprintf(fout, "STAY\n"); exit(0); }
void EnvReadInsert(FILE_NUM fnum, int offset, OBJECT env) { int pos; OBJECT x, loser; debug3(DET, DD, "EnvReadInsert(%s, %d, env %d)", FileName(fnum), offset, (int) env); /* 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 */ hash2(pos, fnum, offset); if( tab[pos] == nilobj ) New(tab[pos], ACAT); New(x, ACAT); env_fnum(x) = fnum; env_offset(x) = offset; env_read(x) = TRUE; Link(tab[pos], x); Link(env_cache, x); Link(x, env); cache_count++; debug1(DET, DD, "EnvReadInsert returning (cache_count = %d)", cache_count); } /* end EnvReadInsert */
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 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; }
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; } }
void test_big( const std::string& expected ) { typename H::encoder enc; for (char c : TEST6) { enc.put(c); } for (int i = 0; i < 16777215; i++) { enc.write( TEST6.c_str(), TEST6.size() ); } H hash = enc.result(); BOOST_CHECK_EQUAL( expected, (std::string) hash ); enc.reset(); enc.write( TEST1.c_str(), TEST1.size() ); hash = enc.result(); BOOST_CHECK( hash >= H::hash( TEST1 ) ); test<H>( TEST1, (std::string) hash ); hash = hash ^ hash; hash.data()[hash.data_size() - 1] = 1; for (int i = hash.data_size() * 8 - 1; i > 0; i--) { H other = hash << i; BOOST_CHECK( other != hash ); BOOST_CHECK( other > hash ); BOOST_CHECK( hash < other ); } H hash2( expected ); fc::variant v; to_variant( hash2, v ); from_variant( v, hash ); BOOST_CHECK( hash == hash2 ); H hash3( expected.substr(15) + "000000000000000" ); BOOST_CHECK( hash3 == hash2 << 60 ); }
int hash(int key, int i, int M){ int address; address = (hash1(key, M) + i * hash2(key, M)) % M; return address; }
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); }
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); }
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); }
entry *findName(char lastName[], entry *pHead) { unsigned int index = hash2(lastName); entry* tmp = hash_table[index]; while (tmp != NULL) { if (strcasecmp(lastName, tmp->lastName) == 0) return tmp; tmp = tmp->pNext; } return NULL; }
int main() { int i; for (i = 0; i < 0xffff; ++i) { struct values *v = hash2(i); if (v) { printf("0x%03x 0x%03x, %s, \"%s\"\n", i, i, v->name, v->desc); } } return 0; }
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 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 }
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)); }
entry* findNameHash(char *key, hashTable *ht) { entry *e; hashIndex index = hash2(key, ht); for(e = ht->list[index]; e != NULL; e = e->pNext){ if ( strcasecmp(key, e->lastName) == 0 ){ printf("%s\n", "found it!"); return e; } } printf("%s\n", "not found"); return NULL; }
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!"); }
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; }
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 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; }
entry *append(char lastName[], entry *e) { /* allocate memory for the new entry and put lastName */ entry* t = (entry *) malloc(sizeof(entry)); strcpy(t->lastName, lastName); t->pNext = NULL; unsigned int index = hash2(lastName); if(hash_table[index] == NULL){ hash_table[index] = t; }else{ //printf("F**K it: %s\n", pHead->lastName); hash_tail[index]->pNext = t; } hash_tail[index] = t; return e; }
int purchasedby(Hash_item ht, int htsize, int cust_id){ // This function traverses through the two hash tables and computes the total cost of the items purchased by the customer, cust_id. int i; int idx2=hash2(cust_id,CSIZE); int sum=0; for(i=0;i<T_SIZE;i++) { if(ht.table[i]) { if(ht.table[i]->table[idx2]) { node* temp= ht.table[i]->table[idx2]; if(temp->cust_id==cust_id) sum+= temp->item_cost; } } } return sum; }
void insert(int key, int val) { if (isFull()) return; int idx = hash1(key); if (m_hash[idx] == -1) { m_hash[idx] = val; } else { int idx2 = hash2(key); int i = 1; while (true) { int nidx = (idx + i * idx2) % m_hash.size(); if (m_hash[nidx] == -1) { m_hash[nidx] = val; break; } ++i; } } m_used++; }
TEST(ConsistentHashMapTest, TestRemove) { TestMap test_map; const string node1("node1"); SHA1Digest hash1(node1); size_t hash_value1; memcpy(&hash_value1, hash1.digest(), sizeof(size_t)); test_map.insert(hash_value1, node1); const string node2("node2"); SHA1Digest hash2(node2); size_t hash_value2; memcpy(&hash_value2, hash2.digest(), sizeof(size_t)); test_map.insert(hash_value2, node2); test_map.remove(hash_value2); EXPECT_EQ(4u, test_map.num_partitions()); test_map.remove(hash_value1); EXPECT_EQ(0u, test_map.num_partitions()); }
void insertHashtable(int htsize, int cust_id, char *item_code,int cost_item) { int key1 = hash1(item_code,htsize); //printf("%d\n",key1); int key2 = hash2(cust_id,htsize); //printf("%d\n",key2); //Ht[key1]->Hid[key2] = (Hash_id*)malloc(sizeof(Hash_id)); Item *temp; temp = (Item*)malloc(sizeof(Item)); temp->item_code = (char*)malloc(sizeof(char)*10); strcpy(temp->item_code,item_code); temp->cust_id = cust_id; temp->cost_item = cost_item; //printf("%s\n",temp->item_code); temp->next = Ht[key1]->Hid[key2]->Head; Ht[key1]->Hid[key2]->Head = temp; //printf("%s\n",Ht[key1]->Hid[key2]->Head->item_code); //printf("%d %d %d %s %d\n",key1,key2,Ht[key1]->Hid[key2]->Head->cust_id,Ht[key1]->Hid[key2]->Head->item_code,Ht[key1]->Hid[key2]->Head->cost_item); //no error till here. }
void HashTable::insert(int entry) { int index, increment; index = hash(entry); //Sets index value to hash function value if (data[index] != 0) { //Checks if value is already at index data[index] = entry; //Puts new entry into table used++; //Increments size counter } else { increment = hash2(entry); //Calculates increment index value for (int k = 0; k < CAPACITY; k++) { //Loops through table index += increment; //Increments index appropriately index = index % CAPACITY; if (data[index] != 0) { //If theres no value data[index] = entry; //Puts value into table k = CAPACITY; //Exits loop used++; //Increments size counter } } } }
void BloomFilter::add(const Key& key) { countAdd++; ////////////// Write your code below //////////////////////// unsigned long hash_value_1 = hash1(key); unsigned long hash_value_2 = hash2(key); // to find the index of the pockets // takes the hash value / bits in each pocket unsigned long pocket1 = hash_value_1 / m_pocketSize; unsigned long pocket2 = hash_value_2 / m_pocketSize; // sets the position of 1 in each signature unsigned long signature1 = 1 << (hash_value_1 % m_pocketSize); unsigned long signature2 = 1 << (hash_value_2 % m_pocketSize); // updates the bloom filter m_tickBook[pocket1] = m_tickBook[pocket1] | signature1; m_tickBook[pocket2] = m_tickBook[pocket2] | signature2; return; }