/* count all the 1-nucleotide and 2-nucleotide sequences, and write the * code and percentage frequency, sorted */ static void do_freq(const struct buf *seq, unsigned len, char *dst) { struct ht t; htinit(&t, HT_BINS, MIN(dna_combo(len), MAX_ENTRIES)); unsigned long total = freq_build(&t, seq, len); { struct htentry *e = ht2vec(&t); qsort(e, htsize(&t), sizeof *e, freq_cmp); freq_print(e, htsize(&t), total, dst); free(e); } htfree(&t); }
/* count all 'buf' substrings of length 'len', return count for buf[0..len-1] */ static void do_cnt(const struct buf *seq, unsigned len, char *buf) { const char *Match = "GGTATTTTAATTTATAGT"; unsigned long cnt = 0; struct ht t; htinit(&t, HT_BINS, MIN(dna_combo(len), MAX_ENTRIES)); freq_build(&t, seq, len); struct htentry *e = htfind(&t, Match, len, index(Match, len)); if (e) cnt = e->val.cnt; htfree(&t); sprintf(buf, "%lu\t%.*s\n", cnt, len, Match); }
/* Usefile format: * U<pkgname> * T<pickled-type> * R<picled-trait> * I<pickled-impl> * D<picled-decl> * G<pickled-decl><pickled-initializer> */ int loaduse(char *path, FILE *f, Stab *st, Vis vis) { intptr_t tid; size_t i; int v; char *pkg; Node *dcl, *impl, *init; Stab *s; Type *ty; Trait *tr; char *lib; int c; pushstab(file->file.globls); if (!tydedup) tydedup = mkht(tyhash, tyeq); if (fgetc(f) != 'U') return 0; v = rdint(f); if (v != Abiversion) { fprintf(stderr, "%s: abi version %d, expected %d\n", path, v, Abiversion); return 0; } pkg = rdstr(f); /* if the package names match up, or the usefile has no declared * package, then we simply add to the current stab. Otherwise, * we add a new stab under the current one */ if (st->name) { if (pkg && !strcmp(pkg, st->name)) { s = st; } else { s = findstab(st, pkg); } } else { if (pkg) { s = findstab(st, pkg); } else { s = st; } } if (!streq(st->name, pkg)) vis = Visintern; if (!s) { printf("could not find matching package for merge: %s in %s\n", st->name, path); exit(1); } tidmap = mkht(ptrhash, ptreq); trmap = mkht(ptrhash, ptreq); if (!initmap) initmap = mkht(namehash, nameeq); /* builtin traits */ for (i = 0; i < Ntraits; i++) htput(trmap, itop(i), traittab[i]); while ((c = fgetc(f)) != EOF) { switch(c) { case 'L': lib = rdstr(f); for (i = 0; i < file->file.nlibdeps; i++) if (!strcmp(file->file.libdeps[i], lib)) /* break out of both loop and switch */ goto foundlib; lappend(&file->file.libdeps, &file->file.nlibdeps, lib); foundlib: break; case 'X': lib = rdstr(f); for (i = 0; i < file->file.nextlibs; i++) if (!strcmp(file->file.extlibs[i], lib)) /* break out of both loop and switch */ goto foundextlib; lappend(&file->file.extlibs, &file->file.nextlibs, lib); foundextlib: break; case 'F': lappend(&file->file.files, &file->file.nfiles, rdstr(f)); break; case 'G': case 'D': dcl = rdsym(f, NULL); dcl->decl.vis = vis; dcl->decl.isglobl = 1; putdcl(s, dcl); break; case 'S': init = unpickle(f); if (!hthas(initmap, init)) { htput(initmap, init, init); lappend(&file->file.init, &file->file.ninit, init); } break; case 'R': tr = traitunpickle(f); tr->vis = vis; puttrait(s, tr->name, tr); for (i = 0; i < tr->nfuncs; i++) putdcl(s, tr->funcs[i]); break; case 'T': tid = rdint(f); ty = tyunpickle(f); if(!ty->ishidden) ty->vis = vis; htput(tidmap, itop(tid), ty); /* fix up types */ if (ty->type == Tyname || ty->type == Tygeneric) { if (ty->issynth) break; if (!streq(s->name, ty->name->name.ns)) ty->ishidden = 1; if (!gettype(s, ty->name) && !ty->ishidden) puttype(s, ty->name, ty); } else if (ty->type == Tyunion) { for (i = 0; i < ty->nmemb; i++) if (!getucon(s, ty->udecls[i]->name) && !ty->udecls[i]->synth) putucon(s, ty->udecls[i]); } break; case 'I': impl = unpickle(f); putimpl(s, impl); /* specialized declarations always go into the global stab */ for (i = 0; i < impl->impl.ndecls; i++) putdcl(file->file.globls, impl->impl.decls[i]); break; case EOF: break; } } fixtypemappings(s); fixtraitmappings(s); htfree(tidmap); popstab(); return 1; }
main(void) { ht.arr = 0; puts("Hash tabulka"); puts("----------"); puts(""); menu: puts("1 - HashTable_Init()"); puts("2 - HashTable_Destruct()"); puts("3 - HashTable_Insert()"); puts("4 - HashTable_Delete()"); puts("5 - HashTable_Find()"); puts("6 - HashTable_Get_Count()"); puts("7 - HashTable_Clear()"); puts("8 - HashTable_Process()"); puts("M - zobraz toto menu"); puts("K - konec"); puts("Pro ukonceni stiskni CTRL+D (Linux) nebo CTRL+Z (Windows)."); in: if(!gets(t)) goto out; switch(t[0]) { case '2': case '3': case '4': case '5': case '6': case '7': case '8': if(ht.arr) break; puts("Tabulka neni inicializovana."); goto in; } switch(t[0]) { case '1': puts("Zadejte velikost tabulky (napr. 1000):"); htfree(ht); ht = htinit(readint()); goto in; case '3': if(!readperson()) break; printf("hash: Key: %s\n", k); printf("-> Hash index: %d\n", htset(&ht, k, p)); goto in; case '6': puts("Velikost tabulky:"); printf("%d\n", ht.len); goto in; case '8': htwalk(ht, printperson); goto in; case 'M': case 'm': goto menu; } out: htfree(ht); return 0; }