void readfile(char op){ FILE* fp; char* string; char buf[256] = { 0 }; int line_cnt = 1; char* ignorestr= (char*)malloc(sizeof(2)); //공간이 있어야 strcpy 가능..! if (op == 'y'){ // Case Sensitive fp = fopen("./book_5MB.txt", "r"); if (fp == NULL){ printf("text file have to same location with this program\n"); return; } else{ while (1){ if (fgets(buf, 256, fp) == NULL){ break; } string = strtok(buf, " ,./<>?`1234567890-=|~\n!@#$%%^&*()_+:;\"'{}[]\\"); // 1.맨처음것을 strok 으로 자르고 while (string != NULL){ hash_chaining(hash_function(string), line_cnt); // 2.체이닝 string = strtok(NULL, " ,./<>?`1234567890-=|~\n!@#$%%^&*()_+:;\"'{}[]\\"); // 3.strok 다시 한 후 while loop으로..! } line_cnt++; printf("현재 %d 줄을 읽고 있습니다. \n", line_cnt); } fclose(fp); } } else if (op == 'n'){ // Case Ignore fp = fopen("C:\\sample_data\\book_50KB.txt", "r"); if (fp == NULL){ printf("파일이 안보이네요!\n"); return; } else{ while (1){ if (fgets(buf, 256, fp) == NULL){ break; } string = strtok(buf, " ,./<>?`1234567890-=|~\n!@#$%%^&*()_+:;\"'{}[]\\\""); // 1.맨처음것을 strok 으로 자르고 while (string != NULL){ strcpy(ignorestr, string); strcpy(ignorestr, strlwr(ignorestr)); hash_chaining(hash_function(ignorestr), line_cnt); // 2.체이닝 string = strtok(NULL, " ,./<>?`1234567890-=|~\n!@#$%%^&*()_+:;\"'{}[]\\\""); // 3.strok 다시 한 후 while loop으로..! } line_cnt++; printf("현재 %d 줄을 읽고 있습니다. \n", line_cnt); } fclose(fp); } } }
static unsigned int cassoclist_resize(cassoclist_t *cassoclist) { size_t element_counter = 0,array_size = cassoclist->array_size; void *temp = realloc(cassoclist->element_info_array ,sizeof(cassoclist_element_info_t)*array_size*2); cassoclist_element_info_t *element_info; if(!temp){ return CASSOCLIST_MEMORY_ALLOCATION_ERROR; } cassoclist->element_info_array = temp; temp = realloc(cassoclist->value_array ,cassoclist->element_size*array_size*2); if(!temp){ return CASSOCLIST_MEMORY_ALLOCATION_ERROR; } cassoclist->value_array = temp; while(element_counter != array_size){ element_info = get_element_info_by_offset(cassoclist,element_counter); if(get_used_flag(element_info) && (hash_function(get_key(element_info) ,get_hash_id(element_info))&array_size)){ *get_element_info_by_offset(cassoclist,element_counter+array_size) = *get_element_info_by_offset(cassoclist,element_counter); memcpy(get_value_by_offset(cassoclist,element_counter+array_size) ,get_value_by_offset(cassoclist,element_counter) ,cassoclist->element_size); get_used_flag(element_info) = 0; } else{ get_used_flag(element_info+array_size) = 0; } element_counter++; } cassoclist->array_size = array_size*2; return CASSOCLIST_SUCCESS; }
/** * Returns true if word is in dictionary else false. */ bool check(const char* word) { char* lower_case = to_lower((char*) word); // index into our hashtable int index = hash_function(lower_case); node* check = NULL; check = hashtable[index]; while(check != NULL) { if(strcmp((const char*) check->word, lower_case) == 0) { free(lower_case); return true; } else check = check->next; } free(lower_case); return false; }
static KEYWORD * FindIdentifier(const char *name) { KEYWORD *result = 0; #if USE_TSEARCH if (name != 0 && strlen(name) != 0) { KEYWORD find; void *pp; find.kw_name = (char *) name; if ((pp = tfind(&find, &(current_class->data), compare_data)) != 0) { result = *(KEYWORD **) pp; } } #else size_t size; if (name != 0 && (size = strlen(name)) != 0) { int Index = hash_function(name); result = my_table[Index]; while (result != NULL) { if (result->kw_size == size && strcmp(result->kw_name, name) == 0) { break; } result = result->kw_next; } } #endif /* TSEARCH */ return result; }
/* * Returns value from map by its key, or NULL if value assigned to this key * not found. */ void *hashmap_get(struct hashmap *map, const char *key) { unsigned int index; struct hashmap_item *p; if (key == NULL) return NULL; index = hash_function(key, map->size); p = map->table[index]; // Value was not setted. if (p == NULL) return NULL; #ifdef HASHMAP_THREAD_SAVE pthread_mutex_lock(&map->mutex); #endif /* HASHMAP_THREAD_SAVE */ do { if (strcmp(p->key, key) == 0) #ifdef HASHMAP_THREAD_SAVE pthread_mutex_unlock(&map->mutex); #endif /* HASHMAP_THREAD_SAVE */ return p->value; } while ((p = p->next) != NULL); #ifdef HASHMAP_THREAD_SAVE pthread_mutex_unlock(&map->mutex); #endif /* HASHMAP_THREAD_SAVE */ return NULL; }
iterator HashMap<Key, T, Hasher, EqualKey, Alloc>:: addNode(const ValueType& v){ NodePointer np = new Node(); np->hash_ = hash_function()(v.first); np->value_ = v; if (size_ + 1 > max_load_) { rehash(size_+1); } size_type index = bucketIndex(np->hash_, bucket_count_); if (bucket_list_[index] == nullptr) { NodePointer pStart = static_cast<NodePointer>(&bucket_start_node_); np->next_ = pStart->next_; pStart->next_ = np; bucket_list_[index] = pStart; if (np->next_ != nullptr) { bucket_list_[bucketIndex(np->next_->hash_, bucket_count_)] = np->next_; } } else{ np->next_ = bucket_list_[index]->next_; bucket_list_[index]->next_ = np; } ++size_; return iterator(np); }
/* * Remove item from hash table * * @param t reference on hash table * @param key string used as index for item */ void htab_remove(htab_t *t, const char *key) { // index and pointers unsigned index = hash_function(key, t->htab_size); htab_listitem* item_ptr = t->list[index]; htab_listitem* item = t->list[index]; while(item != NULL) { // lets search if(strcmp(item->key,key) == 0) { // found if(item_ptr == item) { // Found as first in list, so reset list t->list[index] = item->next; } item_ptr->next = item->next; free(item); break; } else { // Next.. item_ptr = item; item = item->next; } } }
Sub *first_in_hash(char *name) /****************************************************************** * Return Sub who's in string matches the first part of name ******************************************************************/ { return(first_in_list(global_hash[hash_function(name)], name) ); }
static KVPair * DictionaryGetInternal(Dictionary * d, const char * key) { if (key == NULL || key[0] == 0) { return NULL; } KVPair ** table = (KVPair **)d->data; return table[hash_function(key) % d->size]; }
/** * Returns true if word is in dictionary else false. */ bool check(const char* word) { //store the coming word to organize it to compotablie with the dictionary char*inword=malloc(LENGTH*sizeof(char)+1); int j=0; // declare a variable as counter to count througth the char of string coming while(word[j]!='\0') { inword[j]=tolower(word[j]); j++; } inword[j]='\0'; // this become the lowercase word as in the dictionary int index=hash_function(inword); //making a new cursor pointed to the head pointer of every bucket node*cursor=buckets[index]; while(cursor!=NULL) { //comparing the given word with the words in the bucket if((strcmp(cursor->word,inword))==0) // the coming word is equal to the word in the dictionary { free(inword); return true; } else if ((strcmp(cursor->word,inword))<0) // the word coming is short than the word in the dicionary { free(inword); return false; } cursor=cursor->next; } free(inword); return false; }
/** * Returns true if word is in dictionary else false. * i'm here ! */ bool check(const char* word) { char mot_tmp[LENGTH+1]; strcpy(mot_tmp, word); int hash_value = hash_function(mot_tmp); for(int i = 0, size = strlen(mot_tmp); i < size; i++) // Design OK, Style OK { mot_tmp[i] = tolower(mot_tmp[i]); // we lowercase everything } if(hash_table[hash_value] == NULL) { return false; } else { node* p = hash_table[hash_value]; while(p != NULL) { if(!strcmp(p->word,mot_tmp)) return true; else p = p->next; } } return false; }
typename HashtableOpen< T, Hasher >::iterator HashtableOpen< T, Hasher >::find( const key_type &key ) const { size_type index; size_type original_index; iterator result; // Initialize result to be the same as the end() iterator. result.table = this; result.current = nbuckets; // Compute the table index where this object might be. index = hash_function( key ); index %= nbuckets; original_index = index; while( freemap[index] == false ) { if( table[index] == key ) { result.current = index; break; } ++index; if( index == nbuckets ) index = 0; if( index == original_index ) break; } return result; }
/** * Loads dictionary into memory. Returns true if successful else false. */ bool load(const char* dictionary) { //as you know you should assign all pointers to NULL for(int k=0;k<26;k++){ buckets[k]=NULL;} //reading the words from dictionary FILE*memory=fopen(dictionary,"r"); //tempstorage to put the read word from the dictionary in it char* tempword= malloc( LENGTH*sizeof(char) +1); while(fscanf(memory,"%s",tempword)!=EOF) { int index=hash_function(tempword); //fuction to append the word in the relavent bucket append(tempword,index); tempword= malloc( LENGTH*sizeof(char) +1); // we create that for every new word ....yes yes we can make the origenal tempword and overwrite it //but this result that every node will have the latest value that overwritten because in this case all pointers will point to the same (tempword) // because the tempword is associated with another pointers like the poiners of (the function append and hash_function ) } free(tempword); // to free the last unused temp block named (tempword) when reached the end of file //check if the file opened or not if(ferror(memory)){ fclose(memory); return false; } // when the dictionary is loaded close the fie and return true fclose(memory); return true; }
static int find_within_node( const int planet_no, const double jd, const POSN_CACHE *cache) { int loc = hash_function( planet_no, jd); int n_probes = 1; while( cache[loc].planet_no) { if( cache[loc].planet_no == planet_no && cache[loc].jd == jd) break; n_probes++; loc = (loc + n_probes) % node_size; } #if 0 for( int i = 0; i < node_size; i++) if( i != loc) assert( cache[i].planet_no != planet_no || cache[i].jd != jd); #endif #ifdef TEST_PLANET_CACHING_HASH_FUNCTION if( max_probes_required < n_probes) max_probes_required = n_probes; total_n_searches++; total_n_probes += n_probes; #endif return( loc); }
/* insert a new key-value pair into an existing dictionary */ void DictInsert(Dict d, int key, int value, int dist) { struct elt *e; unsigned long h; e = malloc(sizeof(*e)); assert(e); e->key = key; e->value = value; e->dist = dist; h = hash_function(key, value) % d->size; e->next = d->table[h]; d->table[h] = e; d->n++; /* grow table if there is not enough room */ if(d->n >= d->size * MAX_LOAD_FACTOR) { grow(d); } }
int hash_resize(hcfg_t *cfg, unsigned int new_logsize) { unsigned int i, idx, orig_size, new_size; keyval_t *new_hash; if (new_logsize == cfg->logsize) return 0; orig_size = 1 << cfg->logsize; new_size = 1 << new_logsize; new_hash = (keyval_t *) malloc(sizeof(keyval_t) * new_size); if (new_hash == NULL) return -1; memset(new_hash, 0, sizeof(keyval_t) * new_size); for (i = 0; i < orig_size; ++i) { if (cfg->hash[i].key != NULL) { idx = hash_function(cfg->hash[i].key, new_logsize); while (new_hash[idx].key != NULL) idx = (idx + 1) % new_size; new_hash[idx].key = cfg->hash[i].key; new_hash[idx].val = cfg->hash[i].val; } } free(cfg->hash); cfg->hash = new_hash; cfg->logsize = new_logsize; if (cfg->logsize > 16) fprintf(stderr, "Warning: Internal hash grew beyond expectation.\n"); return 0; }
//------------------------------------------------------------------------------ // hash_generate_key function // // This function generates a hash key based on the string and the configuration // parameters given. // // Hashing is done as follows: // 1) For each character a number is generated using the supplied function // pointer. // 2) This value for each character is multiplied by the position in the // string. // 3) The obtained values are summed. If this sum overflows we ignore it. // 4) The remainder of the sum divided by the hash_table_size parameter is // the hash_key. // symtab_key_t hash_generate_key (slcc_hash_type type, const char* key_value) { hash_function_ptr hash_function; symtab_key_t hash_table_size; size_t max_char_value; if (type == HT_CXX) { hash_function = &hash_get_cxx_char_hash_value; hash_table_size = MaxCppHashTableEntries; max_char_value = MaxCppHashCharacterValue; } else { hash_function = &hash_get_pp_char_hash_value; hash_table_size = MaxPreHashTableEntries; max_char_value = MaxPreHashCharacterValue; } symtab_key_t key = 0; symtab_key_t pos = 0; while (*key_value != '\0') { pos++; key += hash_function (*key_value) * pos * max_char_value; key_value++; } return key % hash_table_size; }
static void _nc_make_hash_table(struct name_table_entry *table, short *hash_table) { short i; int hashvalue; int collisions = 0; for (i = 0; i < HASHTABSIZE; i++) { hash_table[i] = -1; } for (i = 0; i < CAPTABSIZE; i++) { hashvalue = hash_function(table[i].nte_name); if (hash_table[hashvalue] >= 0) collisions++; if (hash_table[hashvalue] != 0) table[i].nte_link = hash_table[hashvalue]; hash_table[hashvalue] = i; } DEBUG(4, ("Hash table complete: %d collisions out of %d entries", collisions, CAPTABSIZE)); }
void insert(int a[10]){ int res_array[5] = {1, 2, 3, 4}; for(int i = 0; i < 4; i++){ int index = hash_function(res_array[i]); a[index] = res_array[i]; } }
static hash_item_t *hash_internal_insert(hash_t *h, nx_field_iterator_t *key, int *error) { unsigned long idx = hash_function(key) & h->bucket_mask; hash_item_t *item = DEQ_HEAD(h->buckets[idx].items); *error = 0; while (item) { if (nx_field_iterator_equal(key, item->key)) break; item = item->next; } if (item) { *error = -1; return 0; } item = new_hash_item_t(); if (!item) { *error = -2; return 0; } DEQ_ITEM_INIT(item); item->key = nx_field_iterator_copy(key); DEQ_INSERT_TAIL(h->buckets[idx].items, item); h->size++; return item; }
void insertInHashTable(HashTable *hashtable, Tuple *tuple, long result) { int location_index = hash_function(tuple, hashtable->size); Node* new_head = malloc(sizeof(Node)); new_head->tuple = tuple; new_head->result = result; new_head->next = hashtable->array[location_index]; hashtable->array[location_index] = new_head; }
Item* find(Hashtable* table, const std::string& key) { int index = hash_function(key) % table->size(); for (auto entry: (*table)[index]) { if (key == entry->key) return entry; } return nullptr; }
/**************************************************************************** Description: This function will insert a new hash value into the hast table. output: none. ****************************************************************************/ void hash_table::insert_hash_value(char insert_key, double insert_value) { //hash function int hash_index = hash_function(insert_key); table[hash_index] = new hash_node; table[hash_index]->key = insert_key; table[hash_index]->value = insert_value; }
long getFromHashTable(HashTable *hashtable, Tuple* tuple) { int location_index = hash_function(tuple, hashtable->size); Node* location = hashtable->array[location_index]; while(!areEqualTuples(tuple, location->tuple)) { if(location->next==NULL) return -1; else location=location->next; } return location->result; }
bool inHashTable(Tuple *tuple, HashTable *hashtable) { int location_index = hash_function(tuple, hashtable->size); Node* location = hashtable->array[location_index]; while(location!=NULL) { if(areEqualTuples(tuple, location->tuple)) return true; location=location->next; } return false; }
static Dictionary * DictionaryInsertPair(Dictionary * d, KVPair * e) { if (e) { KVPair ** table = (KVPair **)d->data; table[hash_function(e->key) % d->size] = e; d->n++; } return d; }
//Funcia hlada dane slovo v zozname //ak nie je v tabulke, tak zaznam vytvori inline htab_listitem * htab_lookup(htab_t *t, const char *key) { //Odkaz na polozku tabulky htab_listitem *match; //Hash funckia nam vrati aky index v flexibilnom prvku struktury htab_t mame prehladavat match = t->ptr[ hash_function(key, t->htab_size ) ]; //Ak je zoznam na danom indexe prazdny //vytvorime prvok a dame ju na prve miesto if (match == NULL) { match = htab_listitem_create(key); if (match == NULL) { return NULL; } t->ptr[ hash_function(key, t->htab_size ) ] = match; return match; } else { //Cyklus na hladanie daneho zaznamu o danom slove do{ if( strcmp( match->key, key ) == 0 ) { return match; } //Posuntie na dalsi prvok if( match->next != NULL ) match=match->next; } while ( match->next != NULL ); //Ak sme ho nenasli vytvorime novy a pridame ho nakoniec //ak sa nepodari alokovat miesto tak sa vrati NULL htab_listitem *novy_prvok=htab_listitem_create(key); if (novy_prvok==NULL) { return NULL; } match->next=novy_prvok; return novy_prvok; } return NULL; }
/** * Loads dictionary into memory. Returns true if successful else false. */ bool load(const char* dictionary) { // open the dictionary file FILE* dict = fopen(dictionary, "r"); // if the file doesn't exist if(dict == NULL) { puts("Cannot open the dictionary file."); return false; } // create index for hashtable function int index = 0; // create an array for word to be put into char word[LENGTH+1]; // loop that places each word into hash table and linked list until the end of the file is reached while (fscanf(dict, "%s\n", word)!= EOF) { // create a new node in memory for each word node* new_node = malloc(sizeof(node)); new_node->next = NULL; // put word in the new node strcpy(new_node->word, word); // increase the word count count++; // initialize variable that uses hash_function to find the index of word index = hash_function(word); // case if hashtable index of does not yet have any words if(hashtable[index] == NULL) { hashtable[index] = new_node; } // case if hashtable index already has words else { new_node->next = hashtable[index]; hashtable[index] = new_node; } } // close the file fclose(dict); return true; }
int find_hash_in_chain (struct chain_context * chain, uint32_t a, uint32_t b, uint32_t c, uint32_t d) { double temp; unsigned char data[16]; int tmp; chain->step = 0; memset(chain->plaintext, 0, 128); temp = sin((double) chain->chain); memcpy(data, &temp, 8); temp = sin((double) chain->table); memcpy(&(data[8]), &temp, 8); memcpy(&(chain->plaintext), data, 16); tmp = chain->plaintext_length; chain->plaintext_length = 16; hash_function(chain); chain->plaintext_length = tmp; memset(chain->plaintext, 0, 128); while (chain->step < chain->chain_size) { reduce_function(chain); hash_function(chain); if ((chain->A == a) && (chain->B == b) && (chain->C == c) && (chain->D == d)) { printf("found plaintext :%s\n", chain->plaintext); return 1; } chain->step++; } return 0; }
/** * Returns true if word is in dictionary else false. */ bool check(const char* word) { // create temp variable that stores lowercase version of the word char temp[LENGTH + 1]; // set variable for length of char in word string int len = strlen(word); // create structure for case insensitivity for(int letter = 0; letter < len; letter++) { // case for the apostrophe if(temp[letter] == '\'') { temp[letter] = word[letter]; } else { // make letter lowercase temp[letter] = tolower(word[letter]); } } // add null terminator temp[len] = '\0'; // hash the word to see which bucket it's in int bucket = hash_function(temp); if(hashtable[bucket] == NULL) { return false; } // create cursor to compare the words node* cursor = hashtable[bucket]; // search the word in the hashtable's linked list using strcmp while(cursor != NULL) { if(strcmp(temp, cursor->word) == 0) { // return true is the word is in the linked list - proven by strcmp function return true; } // if the current cursor was not the same as "word", then move to the next node in linked list cursor = cursor->next; } return false; }