int main(void) { hash_t *hash; person_t p1 = {1, "FZ", 29}; person_t p2 = {8, "SB", 31}; person_t p3 = {39, "ZZ", 39}; person_t *p; hash = hash_alloc(1, _person_cmp, 1); if (!hash) { printf("alloc hash error\n"); return -1; } if (hash_insert(hash, p1.id, &p1)) { printf("insert hash failed\n"); hash_free(hash, NULL); return -1; } hash_print(hash, _person_print); if (hash_insert(hash, p2.id, &p2)) { printf("insert hash failed\n"); hash_free(hash, NULL); return -1; } hash_print(hash, _person_print); if (hash_insert(hash, p3.id, &p3)) { printf("insert hash failed\n"); hash_free(hash, NULL); return -1; } hash_print(hash, _person_print); p = hash_search(hash, 39, &p3); if (p) _person_print(p); else printf("not found %d\n", 39); hash_print(hash, _person_print); hash_delete(hash, p2.id, &p2); hash_delete(hash, p1.id, &p1); hash_delete(hash, p3.id, &p3); hash_print(hash, _person_print); hash_free(hash, NULL); return 0; }
int main(int argc,char **argv) { FILE *fp; huffman_tree *huff; char prefix_arr[1000],*prefix; prefix=prefix_arr; /*start to statistics the word count*/ fp=fopen("test.txt","r"); word_table=hash_create(HASH_TABLE); while(get_word(fp)!=EOF){ printf("-------%s--------\n",word); if(word[0]!='\0') word_count(word); } hash_print(word_table); /*start to generate the huffman code*/ huff=huffman_generate_code(word_table); print_huffman_tree(huff); printf("print the huffman code\n"); print_huffman_code(huff,""); /*start to write the huffman code into file*/ return 0; }
static boolean alias_build (kpathsea kpse, hash_table_type *table, const_string alias_filename) { string line, real, alias; unsigned count = 0; FILE *alias_file = fopen (alias_filename, FOPEN_R_MODE); if (alias_file) { while ((line = read_line (alias_file)) != NULL) { /* comments or empty */ if (*line == 0 || *line == '%' || *line == '#') { ; } else { /* Each line should have two fields: realname aliasname. */ real = line; while (*real && ISSPACE (*real)) real++; alias = real; while (*alias && !ISSPACE (*alias)) alias++; *alias++ = 0; while (*alias && ISSPACE (*alias)) alias++; /* Is the check for errors strong enough? Should we warn the user for potential errors? */ if (strlen (real) != 0 && strlen (alias) != 0) { /* Stuff in the alias file should be normalized. */ hash_insert_normalized (table, xstrdup (alias), xstrdup (real)); count++; } } free (line); } #ifdef KPSE_DEBUG if (KPATHSEA_DEBUG_P (KPSE_DEBUG_HASH)) { /* As with ls-R above ... */ boolean hash_summary_only = true; DEBUGF2 ("%s: %u aliases.\n", alias_filename, count); DEBUGF ("alias hash table:"); hash_print (*table, hash_summary_only); fflush (stderr); } #endif /* KPSE_DEBUG */ xfclose (alias_file, alias_filename); } return alias_file != NULL; }
int main(void) { int menu_select; int value; HASH_NODE* hash_table[HASH_BUCKET]; hash_init(hash_table); for(;;) { menu_select = print_menu(); switch(menu_select) { case 1: printf("Insert value>"); scanf("%d",&value); hash_insert(hash_table,value); break; case 2: printf("Can't select this menu\n"); break; case 3: printf("Insert value>"); scanf("%d",&value); HASH_NODE* node = hash_search(hash_table,value); printf("Result:"); if(node==NULL) { printf("Not Found:%d\n",value); } else { printf("Found:%d at %p\n",value,node); } break; case 4: printf("HASH>\n"); hash_print(hash_table); break; case 5: return 0; break; default: printf("Please select 1 to 5\n"); break; } } return 1; }
int main (int argc, char **argv) { // inicia a hash table initMe(); int token = yylex(); while (running) { printf ("token %d - linha %d\n", token, getLineNumber()); token = yylex(); } hash_print(); return 0; }
int alloc_pool(){ int i, size=mem_pool.size; trace(3,"alloc\n"); //top always =0 here! =) mrealloc(mem_pool.free,unsigned int,mem_pool.size+ALLOC_CELLS); mrealloc(mem_pool.mem,cell,mem_pool.size+ALLOC_CELLS); for(i=0;i<ALLOC_CELLS;i++){ //reverse mem_pool.free[i]=ALLOC_CELLS+size-1-i; } mem_pool.top=ALLOC_CELLS-1; mem_pool.size+=ALLOC_CELLS; pool_print(); hash_print(); return SUCCESS; }
static int write_document_hash(jbig2dec_params_t *params) { FILE *out; if (!strncmp(params->output_file, "-", 2)) { out = stderr; } else { out = stdout; } fprintf(out, "Hash of decoded document: "); hash_print(params, out); fprintf(out, "\n"); return 0; }
int main(void) { if(hash_init() != 0) { printf("initial hash failed\n"); return -1; } int val = 23; if(hash_add(0, &val, sizeof(int)) != 0) printf("add data into hash failed!\n"); int value = 230; if(hash_add(2, &value, sizeof(int)) != 0) printf("add data into hash failed!\n"); hash_print(); data_hash_t *p = NULL; if((p = hash_find(1)) != NULL) { printf("find it, key: %d, data: %d\n", p->key, *(int *)p->data); } else { printf("not find it\n"); } if((p = hash_deep_find(0, &val, sizeof(int))) != NULL) { printf("find it, key: %d, data: %d\n", p->key, *(int *)p->data); } else { printf("not find it\n"); } return 0; }
int main(void) { int menu_select; int value; HASH_NODE* hash_table[HASH_BUCKET]; hash_init(hash_table); for(;;) { menu_select = print_menu(); switch(menu_select) { case 1: printf("Insert value>"); scanf("%d",&value); hash_insert(hash_table,value); break; case 2: printf("Can't select this menu\n"); break; case 3: printf("Can't select this menu\n"); break; case 4: printf("HASH>\n"); hash_print(hash_table); break; case 5: return 0; break; default: printf("Please select 1 to 5\n"); break; } } return 1; }
void cndtab_print (struct cndtab *tab, char *head, FILE *stream, int indent) { cndnum_t i; int j; char *comma; fprintf (stream, "Condition table %s\n", head); for (i=0; i<tab->count_cnds; i++) { fprintf (stream, "C%d, lexhash=", i); hash_print (&tab->cnds [i].linehash, stream); fprintf (stream, ", vars_needed="); bitset_print (tab->cnds [i].vars_needed, "V", stream); comma = "\nexpression = "; for (j=0; j<tab->cnds [i].calclen; j++) { if (tab->cnds [i].calc [j] < 0) { fprintf (stream, "%s%c", comma, -tab->cnds [i].calc [j]); } else { fprintf (stream, "%sV%d", comma, tab->cnds [i].calc [j]); } comma = ","; } fprintf (stream, "\n"); } }
int main(int argc ,char ** argv) { Hash_table * myhash=hash_initialize (100,NULL,hash_fun,hash_str_cmp,free); char str[256]; struct tl * ptl=NULL ; FILE * fp=fopen("c:\\test.log","r"); while(NULL != fgets(str,256,fp)) { ptl=make_tl(str); hash_insert(myhash,ptl); // printf("%s 44444 %s ",ptl->s1,ptl->s2); } fclose(fp); hash_print(myhash); printf("-----------------------------------------------------\n"); struct tl dd ; struct tl* s; strcpy(dd.s1,"888033"); s=hash_lookup (myhash,&dd); printf("%s the value %s ",dd.s1,s->s2); hash_print_statistics(myhash,stdout); return 0; }
/** * the worker **/ void *getvocab( void *p ){ // get start & end for this thread Thread* thread = (Thread*)p; const long int start = thread->start(); const long int end = thread->end(); const long int nbop = (end-start)/100; // get output file name std::string output_file_name = std::string(c_vocab_file_name); // attach thread to CPU if (thread->id() != -1){ thread->set(); output_file_name += "-" + typeToString(thread->id()); if (verbose){ fprintf(stderr,"create pthread n°%ld, reading from position %ld to %ld\n",thread->id(), start, end-1); } } // create vocab vocab hash; // open input file std::string input_file_name = std::string(c_input_file_name); File input_file(input_file_name); input_file.open(); input_file.jump_to_position(start); long long ntokens=0; // read and store tokens char word[MAX_TOKEN]; long int position=input_file.position(); int itr=0; if (verbose) loadbar(thread->id(), itr, 100); while ( position<end ){ // get next word while (input_file.getword(word)){ hash[word]++; ++ntokens; } // get current position in stream position = input_file.position(); if (verbose){ if ( position-(start+(itr*nbop)) > nbop) loadbar(thread->id(), ++itr, 100); } } if (verbose) loadbar(thread->id(), 100, 100); // closing input file input_file.close(); // exit thread if ( thread->id()!= -1 ){ long long *ptr_ntokens = (long long *) thread->object; // increment total number of tokens (*ptr_ntokens) += ntokens; // write hash table hash_print(&hash, output_file_name.c_str()); // existing pthread pthread_exit( (void*)thread->id() ); }else{ if (verbose) fprintf(stderr, "\ndone after reading %lld tokens.\n", ntokens); // write out writevocab(&hash, output_file_name); } return 0; }
static boolean db_build (kpathsea kpse, hash_table_type *table, const_string db_filename) { string line; unsigned dir_count = 0, file_count = 0, ignore_dir_count = 0; unsigned len = strlen (db_filename) - sizeof (DB_NAME) + 1; /* Keep the /. */ string top_dir = (string)xmalloc (len + 1); string cur_dir = NULL; /* First thing in ls-R might be a filename. */ FILE *db_file = fopen (db_filename, FOPEN_R_MODE); #if defined(WIN32) string pp; #endif strncpy (top_dir, db_filename, len); top_dir[len] = 0; if (db_file) { while ((line = read_line (db_file)) != NULL) { len = strlen (line); #if defined(WIN32) for (pp = line; *pp; pp++) { if (IS_KANJI(pp)) pp++; else *pp = TRANSFORM(*pp); } #endif /* A line like `/foo:' = new dir foo. Allow both absolute (/...) and explicitly relative (./...) names here. It's a kludge to pass in the directory name with the trailing : still attached, but it doesn't actually hurt. */ if (len > 0 && line[len - 1] == ':' && kpathsea_absolute_p (kpse, line, true)) { /* New directory line. */ if (!ignore_dir_p (line)) { /* If they gave a relative name, prepend full directory name now. */ line[len - 1] = DIR_SEP; /* Skip over leading `./', it confuses `match' and is just a waste of space, anyway. This will lose on `../', but `match' won't work there, either, so it doesn't matter. */ cur_dir = *line == '.' ? concat (top_dir, line + 2) : xstrdup (line); dir_count++; } else { cur_dir = NULL; ignore_dir_count++; } /* Ignore blank, `.' and `..' lines. */ } else if (*line != 0 && cur_dir /* a file line? */ && !(*line == '.' && (line[1] == 0 || (line[1] == '.' && line[2] == 0)))) { /* Make a new hash table entry with a key of `line' and a data of `cur_dir'. An already-existing identical key is ok, since a file named `foo' can be in more than one directory. Share `cur_dir' among all its files (and hence never free it). Note that we assume that all names in the ls-R file have already been case-smashed to lowercase where appropriate. */ hash_insert_normalized (table, xstrdup (line), cur_dir); file_count++; } /* else ignore blank lines or top-level files or files in ignored directories*/ free (line); } xfclose (db_file, db_filename); if (file_count == 0) { WARNING1 ("kpathsea: %s: No usable entries in ls-R", db_filename); WARNING ("kpathsea: See the manual for how to generate ls-R"); db_file = NULL; } else { str_list_add (&(kpse->db_dir_list), xstrdup (top_dir)); } #ifdef KPSE_DEBUG if (KPATHSEA_DEBUG_P (KPSE_DEBUG_HASH)) { /* Don't make this a debugging bit, since the output is so voluminous, and being able to specify -1 is too useful. Instead, let people who want it run the program under a debugger and change the variable that way. */ boolean hash_summary_only = true; DEBUGF4 ("%s: %u entries in %d directories (%d hidden).\n", db_filename, file_count, dir_count, ignore_dir_count); DEBUGF ("ls-R hash table:"); hash_print (*table, hash_summary_only); fflush (stderr); } #endif /* KPSE_DEBUG */ } free (top_dir); return db_file != NULL; }
int main(int argc, char **argv) { FILE * pData; FILE * pCmds; char szCommand[STR_MAX]; hash_tbl * tbl; if(argc < 3) { fprintf(stderr, "Usage: %s [DATA FILE] [CMD FILE]\n", argv[0]); exit (1); } #ifdef LINEAR printf("Using linear collision resolution...\n\n"); #else printf("Using quadratic collision resolution...\n\n"); #endif pData = fopen(argv[1], "r"); pCmds = fopen(argv[2], "r"); if(!pData || !pCmds) { perror("fopen"); fclose(pData); fclose(pCmds); exit (1); } tbl = hash_init(); while(fgets(szCommand, STR_MAX, pData)) { int val = atoi(szCommand); if(val > 0) { hash_insert(tbl, val); } } fclose(pData); while(fgets(szCommand, STR_MAX, pCmds)) { char * tok = strtok(szCommand, " "); if(*tok == 'i') { int val; tok = strtok(NULL, " "); val = atoi(tok); if(val > 0) { printf("INSERTING %d\n", val); if(-1 == hash_insert(tbl, val) ) perror("hash_insert"); } } else if(*tok == 'd') { int val; tok = strtok(NULL, " "); val = atoi(tok); if(val > 0) { printf("REMOVING %d\n", val); if(-1 == hash_remove(tbl, val) ) perror("hash_remove"); } } else if(*tok == 'p') { printf("TABLE: "); hash_print(tbl); printf("\n"); } } fclose(pCmds); hash_print(tbl); printf("\n"); return 0; }