tBitmapIndex PiggyFindBitmap (const char * pszName, int bD1Data) { tBitmapIndex bi; int i; const char *t; char name [FILENAME_LEN], alias [FILENAME_LEN]; bi.index = 0; strcpy (name, pszName); if ((t = strchr (pszName, '#'))) name [t - pszName] = '\0'; for (i = 0; i < gameData.pig.tex.nAliases; i++) if (!stricmp (name, gameData.pig.tex.aliases [i].aliasname)) { if (t) { //this is a frame of an animated texture, so add the frame number _splitpath (gameData.pig.tex.aliases [i].filename, NULL, NULL, alias, NULL); strcat (alias, "#"); strcat (alias, t + 1); pszName = alias; } else pszName = gameData.pig.tex.aliases [i].filename; break; } i = HashTableSearch (bitmapNames + bD1Data, pszName); Assert (i != 0); bi.index = i; return bi; }
/*----------------------------------------------------------------------------- * 设置IndexNode的状态信息 如cFlags, dwPosition等 *-----------------------------------------------------------------------------*/ int SetIndexNodeStatus(HashTable *pstHashTable, uint32_t dwUin, IndexNode *pstNewIndexNode) { IndexNode stIndexNode= {0}; IndexNode *pstIndexNode = NULL; if(pstHashTable == NULL || dwUin == 0 || pstNewIndexNode == NULL) { return -1; } DD("SetIndexNodeStatus. \n"); stIndexNode.dwUin = dwUin; pstIndexNode = HashTableSearch(pstHashTable,(void *)&stIndexNode, dwUin); /*存在该key的indexnode节点 但是无法使用*/ if(pstIndexNode != NULL) { DD("pstIndexNode->cFlags = 0x%x\n", pstIndexNode->cFlags); if(pstIndexNode->cFlags == INODE_LOCKED) { printf("dwUin = %d is locked.\n", dwUin); return -3; } else if(pstIndexNode->cFlags == INODE_UNUSED) { return 0; } pstIndexNode->cFlags = pstNewIndexNode->cFlags; if(pstNewIndexNode->dwPosition > 0) { pstIndexNode->dwPosition = pstNewIndexNode->dwPosition; } if(pstNewIndexNode->ddwLastPublicTime > 0) { pstIndexNode->ddwLastPublicTime = pstNewIndexNode->ddwLastPublicTime; } } /*不存在该key的indexnode节点 */ return -4; }
/* * Removes all tags of specific types (hashed in h, or all if h == NULL) * from a specified contig. * * Returns 0 on success * -1 on failure */ static int delete_tag_single_contig(GapIO *io, tg_rec crec, HashTable *h, int verbose) { contig_iterator *ci; rangec_t *r; contig_t *c; int ret = -1; ci = contig_iter_new_by_type(io, crec, 1, CITER_FIRST, CITER_CSTART, CITER_CEND, GRANGE_FLAG_ISANNO); if (!ci) return -1; if (!(c = cache_search(io, GT_Contig, crec))) { contig_iter_del(ci); return -1; } cache_incr(io, c); while (NULL != (r = contig_iter_next(io, ci))) { char t[5]; (void)type2str(r->mqual, t); if (!h || HashTableSearch(h, t, 4)) { anno_ele_t *e; if (verbose) vmessage("Removing anno %s #%"PRIrec"\tContig %s\t%d..%d\n", t, r->rec, c->name, r->start, r->end); if (bin_remove_item(io, &c, GT_AnnoEle, r->rec)) goto fail; /* FIXME: Need to reclaim the GT_AnnoEle record itself */ } } ret = 0; fail: contig_iter_del(ci); cache_decr(io, c); return ret; }
/*----------------------------------------------------------------------------- * 根据dwUin获取node信息 *-----------------------------------------------------------------------------*/ int GetIndexNode(HashTable *pstHashTable, uint32_t dwUin, IndexNode **ppstInode) { IndexNode stIndexNode= {0}; if(pstHashTable == NULL || ppstInode == NULL || dwUin == 0) { return -1; } DD("GetIndexNode\n"); stIndexNode.dwUin = dwUin; if((*ppstInode = HashTableSearch(pstHashTable, (void *)&stIndexNode, dwUin)) == NULL) { DD("dwUin = %d not found. \n", dwUin); return -2; } if((*ppstInode)->cFlags != INODE_USED) { printf("dwUin = %d is not used.\n", dwUin); return -3; } return 0; }
void cram_stats_del(cram_stats *st, int32_t val) { st->nsamp--; //assert(val >= 0); if (val < MAX_STAT_VAL && val >= 0) { st->freqs[val]--; assert(st->freqs[val] >= 0); } else if (st->h) { HashItem *hi; if ((hi = HashTableSearch(st->h, (char *)(size_t)val, 4))) { if (--hi->data.i == 0) HashTableDel(st->h, hi, 0); } else { fprintf(stderr, "Failed to remove val %d from cram_stats\n", val); st->nsamp++; } } else { fprintf(stderr, "Failed to remove val %d from cram_stats\n", val); st->nsamp++; } }
void cram_stats_add(cram_stats *st, int32_t val) { st->nsamp++; //assert(val >= 0); if (val < MAX_STAT_VAL && val >= 0) { st->freqs[val]++; } else { HashItem *hi; if (!st->h) { st->h = HashTableCreate(2048, HASH_DYNAMIC_SIZE|HASH_NONVOLATILE_KEYS|HASH_INT_KEYS); } if ((hi = HashTableSearch(st->h, (char *)(size_t)val, 4))) { hi->data.i++; } else { HashData hd; hd.i = 1; HashTableAdd(st->h, (char *)(size_t)val, 4, hd, NULL); } } }
int main(int argc, char *argv[]) { char *fname; // name of dictionary file FILE *wordf; // handle for dictionary file Item word; // current word from file int size = 7919; // default size of hash table HashTable htab; // the hash table int nwords; // # words read and stored int nfound; // # words found during search tests // set up parameters switch (argc) { case 2: fname = argv[1]; break; case 3: fname = argv[1]; size = atoi(argv[2]); break; default: fname = NULL; usage(argv[0]); break; } // access the word file if (eq(fname,"-")) { wordf = stdin; printf("Reading words from stdin\n"); } else { wordf = fopen(fname,"r"); if (wordf == NULL) { printf("Can't open %s\n",fname); exit(1); } printf("Reading words from %s\n",fname); } // build hash table, containing all words from file nwords = 0; nfound = 0; htab = newHashTable(size); while ((word = ItemGet(wordf)) != NULL) { if (eq(word,"")) { dropItem(word); continue; } HashTableInsert(htab,word); nwords++; if (HashTableSearch(htab,word) != NULL) nfound++; dropItem(word); } // examine hash table HashTableStats(htab); // tests // warning: we are assuming that "!aaaaaa!" etc. // do not occur in the input; this is not guaranteed assert(nfound == nwords); assert(HashTableSearch(htab,"!aaaaaa!") == NULL) ; assert(HashTableSearch(htab,"!xxxxxx!") == NULL) ; assert(HashTableSearch(htab,"!yyyyyy!") == NULL) ; assert(HashTableSearch(htab,"!zzzzzz!") == NULL) ; printf("Testing completed OK\n"); // clean up fclose(wordf); dropHashTable(htab); return 0; }
/* * Adds tar index data to the global files[] array. * * Returns 0 on success * -1 on failure */ int accumulate(HashFile *hf, FILE *fp, char *archive, options_t *opt) { tar_block blk; char member[256]; int LongLink = 0; size_t size, extra; size_t offset = 0; /* Add to HashFile archives list */ if (archive) { hf->narchives++; hf->archives = realloc(hf->archives, hf->narchives * sizeof(char *)); hf->archives[hf->narchives-1] = strdup(archive); } /* Fill out the files[] array with the offsets, size and names */ while(fread(&blk, sizeof(blk), 1, fp) == 1) { /* * If a directory is too large to fit in the name (>100) but short * enough to fit in the prefix the name field will be empty, this is * not the cas for ordinary files where the name field is always * non-empty */ if (!blk.header.name[0] && !blk.header.prefix[0]) break; /* get size of member, rounded to a multiple of TBLOCK */ size = strtoul(blk.header.size, NULL, 8); extra = TBLOCK*((size+TBLOCK-1)/TBLOCK) - size; /* skip directories unless requested */ if (opt->directories || blk.header.typeflag != DIRTYPE) { /* * extract member name (prefix + name), unless last member * was ././@LongLink */ if (LongLink == 0) { (void) strncpy(member, blk.header.prefix, 155); if (strlen(blk.header.prefix) > 0 && blk.header.name[0]) (void) strcat(member, "/"); (void) strncat(member, blk.header.name, 100); } /* account for gtar ././@LongLink */ if (strcmp(member, "././@LongLink") == 0) { /* still expect filenames to fit into 256 bytes */ if (size > 256) { fread(member, 1, size > 256 ? 256 : size, fp); fprintf(stderr,"././@LongLink too long size=%ld\n", (long)size); fprintf(stderr,"%s...\n", member); exit(1); } /* * extract full name of next member then rewind to start * of header */ fread(member, 1, size > 256 ? 256 : size, fp); fseek(fp, -size, SEEK_CUR); LongLink = 1; } else { /* output offset, member name */ /* printf("%lu %.256s\n", (long)offset, member); */ LongLink = 0; if (nfiles >= files_alloc) { if (files_alloc) files_alloc *= 2; else files_alloc = 1024; files = (tar_file *)realloc(files, files_alloc*sizeof(tar_file)); } if (opt->basename) { char *cp = strrchr(member, '/'); if (cp) memmove(member, cp+1, strlen(cp+1)+1); } if (opt->map) { HashItem *hi = HashTableSearch(opt->map, member, strlen(member)); if (hi) { //fprintf(stderr, "Mapped %s to %s\n", // member, hi->data.p); strcpy(files[nfiles].member, hi->data.p); } else { //fprintf(stderr, "No map for %s\n", // member); strcpy(files[nfiles].member, member); } } else { strcpy(files[nfiles].member, member); } files[nfiles].archive = hf->narchives-1; files[nfiles].pos = offset+sizeof(blk); files[nfiles].size = size; if (opt->verbose) fprintf(stderr, "File %d: pos %010ld+%06d: %s\n", nfiles, (long)files[nfiles].pos, files[nfiles].size, files[nfiles].member); nfiles++; } } /* increment offset */ size += extra; seek_forward(fp, size); offset += sizeof(blk) + size; } }
/* * Complements a scaffold; both complementing each contig within it and * reversing the order of contigs in the scaffold. * * Returns 0 on success * -1 on failure */ int complement_scaffold(GapIO *io, tg_rec srec) { scaffold_t *f; int i, j, nc = ArrayMax(io->contig_order); scaffold_member_t *contigs; tg_rec *crecs; HashTable *h; reg_order ro; reg_buffer_start rs; reg_buffer_end re; if (!(f = cache_search(io, GT_Scaffold, srec))) return -1; if (!(f = cache_rw(io, f))) return -1; cache_incr(io, f); /* Complement contigs */ contigs = ArrayBase(scaffold_member_t, f->contig); for (i = 0; i < ArrayMax(f->contig); i++) { complement_contig(io, contigs[i].rec); } /* Reverse the order of the contigs in the scaffold array */ for (i = 0, j = ArrayMax(f->contig)-1; i < j; i++, j--) { scaffold_member_t cr1 = contigs[i]; contigs[i] = contigs[j]; contigs[j] = cr1; } /* * Reverse the order of contigs in the contig_order array too. * This is the part that really matters. It's also hard as the contigs * in the contig order array could be in any order and not adjacent. * For our purposes we'll just ensure the contigs in this scaffold in * the contig order array match our freshly complemented scaffold * ordering. * * We initially build a hash table of contigs in this scaffold, and * then iterate through contig_order copying out the new contigs whenever * one matches. */ h = HashTableCreate(nc, 0); for (i = 0; i < ArrayMax(f->contig); i++) { HashData hd; hd.i = 0; HashTableAdd(h, (char *)&contigs[i].rec, sizeof(tg_rec), hd, NULL); } /* Replace any contig matching the scaffold with the new order */ crecs = ArrayBase(tg_rec, io->contig_order); for (i = j = 0; i < nc; i++) { HashItem *hi; if (!(hi = HashTableSearch(h, (char *)&crecs[i], sizeof(tg_rec)))) continue; crecs[i] = contigs[j++].rec; } /* Send event messages around */ rs.job = REG_BUFFER_START; for (i = 0; i < nc; i++) { HashItem *hi; if (!(hi = HashTableSearch(h, (char *)&crecs[i], sizeof(tg_rec)))) continue; contig_notify(io, crecs[i], (reg_data *)&rs); } ro.job = REG_ORDER; for (i = 0; i < nc; i++) { HashItem *hi; if (!(hi = HashTableSearch(h, (char *)&crecs[i], sizeof(tg_rec)))) continue; ro.pos = i+1; contig_notify(io, crecs[i], (reg_data *)&ro); } /* Notify the end of our updates */ re.job = REG_BUFFER_END; for (i = 0; i < nc; i++) { HashItem *hi; if (!(hi = HashTableSearch(h, (char *)&crecs[i], sizeof(tg_rec)))) continue; contig_notify(io, crecs[i], (reg_data *)&re); } HashTableDestroy(h, 0); cache_decr(io, f); return 0; }
/* * Parse the REGN chunk, add to regn HASH * * Returns corresponding HashItem * from regn Hash */ HashItem *parse_regn(ztr_t *z, ztr_chunk_t *chunk, HashTable *regn_hash) { char key[1024]; char *name; HashItem *hi; regn_t *regn; size_t l; uncompress_chunk(z, chunk); /* the hash key is a combination of the region names and boundaries */ name = ztr_lookup_mdata_value(z, chunk, "NAME"); l = snprintf(key, sizeof(key), "names=%s", name); if( chunk->dlength ){ int nbndy = (chunk->dlength-1)/4; uint4 *bndy = (uint4 *)(chunk->data+1); int ibndy; for (ibndy=0; ibndy<nbndy; ibndy++) { if( ibndy ) l += snprintf(key + l, sizeof(key) - l, ";%d", be_int4(bndy[ibndy])); else l += snprintf(key + l, sizeof(key) - l, " boundaries=%d", be_int4(bndy[ibndy])); } } if (NULL == (hi = (HashTableSearch(regn_hash, key, strlen(key))))) { int iregion, nregions = 0; char *coord; char *cp1; uint4 bndy[MAX_REGIONS]; int ibndy, nbndy = 0; HashData hd; if( NULL == (regn = (regn_t *)malloc(sizeof(regn_t)))) { return NULL; } coord = ztr_lookup_mdata_value(z, chunk, "COORD"); regn->coord = (NULL == coord ? 'B' : *coord ); regn->region_names = strdup(name); cp1 = strtok (regn->region_names,";"); while(cp1) { char *cp2; if(NULL == (cp2 = strchr(cp1,':'))) { fprintf(stderr, "Invalid region name/code pair %s\n", cp1); return NULL; } *cp2++ = '\0'; regn->name[nregions] = cp1; regn->code[nregions] = *cp2; nregions++; cp1 = strtok (NULL, ";"); } regn->nregions = nregions; if( chunk->dlength ) { nbndy = (chunk->dlength-1)/4; memcpy(bndy, chunk->data+1, chunk->dlength-1); } for( iregion=0, ibndy=0; iregion<nregions; iregion++) { /* start = (start + length of previous region) or 0 if no previous region */ /* length = (next boundary - start of region) or -1 if no next boundary */ if( regn->code[iregion] == 'E' ){ /* no sequence, length = 0 */ regn->start[iregion] = (iregion ? (regn->start[iregion-1] + regn->length[iregion-1]) : 0); regn->length[iregion] = 0; }else{ if( ibndy > nbndy ){ fprintf(stderr, "More name/code pairs than boundaries\n"); return NULL; } regn->start[iregion] = (iregion ? (regn->start[iregion-1] + regn->length[iregion-1]) : 0); regn->length[iregion] = (ibndy == nbndy ? -1 : (be_int4(bndy[ibndy])-regn->start[iregion])); ibndy++; } } regn->count = 1; hd.p = regn; if (NULL == (hi = HashTableAdd(regn_hash, key, strlen(key), hd, NULL))) { free(regn->region_names); free(regn); return NULL; } } else { regn = (regn_t *)(hi->data.p); regn->count++; } return hi; }