static void test_vector(void) { MemoryManager memory = create_standard_memory_manager(); { Vector v; Vector_init(&v); TEST(Vector_empty(&v)); TEST(Vector_size(&v) == 0); Vector_free(&v, memory.deallocator); } { Vector v; Vector_init(&v); TEST(Vector_reserve(&v, 100, memory.allocator)); TEST(Vector_reserve(&v, 200, memory.allocator)); TEST(Vector_reserve(&v, 50, memory.allocator)); TEST(Vector_reserve(&v, 0, memory.allocator)); Vector_free(&v, memory.deallocator); } { Vector v; Vector_init(&v); TEST(Vector_resize(&v, 100, memory.allocator)); TEST(Vector_resize(&v, 200, memory.allocator)); TEST(Vector_resize(&v, 50, memory.allocator)); TEST(Vector_resize(&v, 0, memory.allocator)); Vector_free(&v, memory.deallocator); } { size_t i; size_t const count = 1000; Vector v; Vector_init(&v); for (i = 0; i < count; ++i) { char c = (char)('0' + (i % 10)); ASSERT(Vector_push_back(&v, &c, 1, memory.allocator)); } for (i = 0; i < count; ++i) { char c = (char)('0' + (i % 10)); ASSERT(Vector_data(&v)[i] == c); } Vector_free(&v, memory.deallocator); } }
int DBEntryAdaptor_fetchAllByGene(DBEntryAdaptor *dbea, Gene *gene) { char qStr[512]; StatementHandle *sth; ResultRow *row; sprintf(qStr, "SELECT t.transcript_id, t.canonical_translation_id" " FROM transcript t" " WHERE t.gene_id = " IDFMTSTR, Gene_getDbID(gene)); sth = dbea->prepare((BaseAdaptor *)dbea,qStr,strlen(qStr)); sth->execute(sth); while ((row = sth->fetchRow(sth))) { IDType transcriptId = row->getLongLongAt(row,0); int i; Vector *transLinks; if (row->col(row,1)) { IDType translationId = row->getLongLongAt(row,1); Vector *translatLinks = DBEntryAdaptor_fetchByObjectType(dbea, translationId,"Translation"); for (i=0;i<Vector_getNumElement(translatLinks); i++) { Gene_addDBLink(gene,Vector_getElementAt(translatLinks,i)); } Vector_free(translatLinks); } transLinks = DBEntryAdaptor_fetchByObjectType(dbea, transcriptId,"Transcript"); for (i=0;i<Vector_getNumElement(transLinks); i++) { Gene_addDBLink(gene, Vector_getElementAt(transLinks,i)); } Vector_free(transLinks); } /* NIY This is wrong so I'm not going to implement it! if($gene->stable_id){ my $genelinks = $self->_fetch_by_object_type( $gene->stable_id, 'Gene' ); foreach my $genelink ( @$genelinks ) { $gene->add_DBLink( $genelink ); } } */ return 1; }
/* * carsonl, jan 8,98 * * parameter : * * returns : MIME_OK if successful */ int mimeInfo_free( mimeInfo_t *p ) { if ( p == NULL ) { return MIME_ERR_INVALIDPARAM; } if ( p->szName != NULL ) { free( p->szName ); p->szName = NULL; } if ( p->szValue != NULL ) { free( p->szValue ); p->szValue = NULL; } if ( p->pVectorParam != NULL ) { Vector_free( p->pVectorParam ); p->pVectorParam = NULL; } if ( p != NULL ) free( p ); return MIME_OK; }
SeqFeature *BaseAdaptor_uncachedFetchByDbID(BaseAdaptor *ba, IDType id) { SeqFeature *feat = NULL; char constraint[1024]; //construct a constraint like 't1.table1_id = 123' NameTableType *tables = ba->getTables(); char **t = (*tables)[0]; sprintf(constraint, "%s.%s_id = "IDFMTSTR, t[SYN], t[NAME], id); //Should only be one Vector *vec = BaseAdaptor_genericFetch(ba, constraint, NULL, NULL); if (Vector_getNumElement(vec) > 1) { fprintf(stderr, "Error: Got more than one feature back in fetch ID call\n"); } else { if (Vector_getNumElement(vec) == 1) { feat = Vector_getElementAt(vec, 0); Object_incRefCount(feat); } } // NIY May want to set a free func??? Vector_free(vec); return feat; }
void part2(){ int r[4] = {0, 0, 1, 0}; Vector v = read_input(); run(v, r); printf("%d\n", r['a' - 'a']); Vector_free(v); }
char *Translation_modifyTranslation(Translation *translation, char *seq) { Vector *seqEds = Translation_getAllSeqEdits(translation); // Sort in reverse order to avoid complication of adjusting // downstream edits. // HACK: The translation ENSP00000420939 somehow makes the next line // bomb out ($a or $b becomes undef) if the start() method // is used. I haven't been able to find out why. It has 10 // Selenocysteine seqedits that looks correct. // /Andreas (release 59) if (Vector_getNumElement(seqEds)) { Vector_sort(seqEds, SeqEdit_reverseStartCompFunc); // @seqeds = sort { $b->{'start'} <=> $a->{'start'} } @seqeds; // Apply all edits. // Not particularly efficient currently, could improve by precalculating maximum size of new seq prior to applying edits int i; for (i=0; i<Vector_getNumElement(seqEds); i++) { SeqEdit *se = Vector_getElementAt(seqEds, i); seq = SeqEdit_applyEdit(se, seq); } //$seq->seq($peptide); } Vector_free(seqEds); return seq; }
int main(int argc, char *argv[]) { Vector *pieces; char *str; char *reverse; int testResult = 0; initEnsC(argc, argv); pieces = CigarStrUtil_getPieces(cigar1); testResult += ok(1, Vector_getNumElement(pieces) == 6); str = Vector_getElementAt(pieces,0); testResult += ok(2, !strcmp(str,"6M")); Vector_free(pieces); StrUtil_copyString(&str,cigar1,0); testResult += ok(3, !strcmp(str,cigar1)); reverse = CigarStrUtil_reverse(str,strlen(str)); free(str); pieces = CigarStrUtil_getPieces(reverse); testResult += ok(4, Vector_getNumElement(pieces) == 6); str = Vector_getElementAt(pieces,0); testResult += ok(5, !strcmp(str,"M")); return testResult; }
Mapper *PredictionTranscript_getcDNACoordMapper(PredictionTranscript *trans) { Mapper *mapper; int start = 1; int i; Vector *translateable; if (trans->exonCoordMapper) { return trans->exonCoordMapper; } // // the mapper is loaded with OBJECTS in place of the IDs !!!! // the objects are the contigs in the exons // // NIY: What should coordsystems be? mapper = Mapper_new( "cdna", "genomic", NULL, NULL ); translateable = PredictionTranscript_getAllTranslateableExons(trans); for (i=0; i<Vector_getNumElement(translateable); i++) { PredictionExon *exon = Vector_getElementAt(translateable,i); PredictionExon_loadGenomicMapper((Exon*)exon, mapper, (IDType)trans, start); start += PredictionExon_getLength(exon); } trans->exonCoordMapper = mapper; Vector_free(translateable); return mapper; }
// New Vector *Translation_getAllSeqEdits(Translation *translation) { char *edits[] = { "initial_met", "_selenocysteine", "amino_acid_sub", NULL }; Vector *seqEds = Vector_new(); char **editP = edits; while (*editP) { char *edit = *editP; Vector *attribs = Translation_getAllAttributes(translation, edit); // convert attributes to SeqEdit objects int i; for (i=0; i<Vector_getNumElement(attribs); i++) { Attribute *attrib = Vector_getElementAt(attribs, i); SeqEdit *seqEd = SeqEdit_newFromAttribute(attrib); Vector_addElement(seqEds, seqEd); } Vector_free(attribs); editP++; } return seqEds; }
void PredictionTranscript_free(PredictionTranscript *trans) { Object_decRefCount(trans); if (Object_getRefCount(trans) > 0) { return; } else if (Object_getRefCount(trans) < 0) { fprintf(stderr,"Error: Negative reference count for PredictionTranscript\n" " Freeing it anyway\n"); } if (trans->exonCoordMapper) Mapper_free(trans->exonCoordMapper); if (trans->translateableExons) Vector_free(trans->translateableExons); if (trans->exons) Vector_free(trans->exons); free(trans); }
void ImageManager_free(ImageManager *im, Deallocator images_deallocator) { for_each(Vector_begin(&im->images), Vector_end(&im->images), sizeof(Image), free_image, &images_deallocator); Vector_free(&im->images, images_deallocator); Deallocator_free(images_deallocator, im->directory); }
int DBEntryAdaptor_fetchAllByTranscript(DBEntryAdaptor *dbea, Transcript *trans) { char qStr[512]; StatementHandle *sth; ResultRow *row; Vector *transLinks; int i; sprintf(qStr, "SELECT t.canonical_translation_id" " FROM transcript t" " WHERE t.transcript_id = " IDFMTSTR, Transcript_getDbID(trans)); sth = dbea->prepare((BaseAdaptor *)dbea,qStr,strlen(qStr)); sth->execute(sth); // // Did this to be consistent with fetch_by_Gene, but don't like // it (filling in the object). I think returning the array would // be better. Oh well. EB // while ((row = sth->fetchRow(sth))) { IDType translationId = row->getLongLongAt(row,0); Vector *translatLinks = DBEntryAdaptor_fetchByObjectType(dbea, translationId,"Translation"); for (i=0;i<Vector_getNumElement(translatLinks); i++) { Transcript_addDBLink(trans,Vector_getElementAt(translatLinks,i)); } Vector_free(translatLinks); } sth->finish(sth); transLinks = DBEntryAdaptor_fetchByObjectType(dbea, Transcript_getDbID(trans),"Transcript"); fprintf(stderr,"transLinks\n"); for (i=0;i<Vector_getNumElement(transLinks); i++) { Transcript_addDBLink(trans,Vector_getElementAt(transLinks,i)); } Vector_free(transLinks); return 1; }
PredictionTranscript *PredictionTranscript_shallowCopy(PredictionTranscript *trans) { PredictionTranscript *newTranscript = PredictionTranscript_new(); // Exons will come in mem copy so free vector just created in PredictionTranscript_new Vector_free(newTranscript->exons); memcpy(newTranscript,trans,sizeof(PredictionTranscript)); return newTranscript; }
void part2(){ char c; Vector in = Vector_create_char(); long long count = 0; while(scanf("%c", &c) != EOF){ Vector_push_char(in, c); } Vector_push_char(in, 0); count = count_letters(in, 0, -1); printf("%lld\n", count); Vector_free(in); }
void PredictionTranscript_flushExons(PredictionTranscript *trans) { PredictionTranscript_setCodingRegionStartIsSet(trans, 0); PredictionTranscript_setCodingRegionEndIsSet(trans, 0); PredictionTranscript_setStartIsSet(trans, 0); PredictionTranscript_setEndIsSet(trans, 0); if (trans->translateableExons) { Vector_free(trans->translateableExons); trans->translateableExons = NULL; } // NIY Transcript_removeAllExons(trans); // NIY caches }
void part1(){ HashMap h = HM_create(Node_hash, Node_equals, NULL, free); Vector stack = Vector_create(sizeof(struct Node*)); struct Node *n; while(readline(h, stack)); solve(stack); HM_foreach(h, part1_finder, &n); printf("%d\n", n->id); HM_destroy(&h); Vector_free(stack); }
RepeatConsensus *RepeatConsensusAdaptor_fetchByName(RepeatConsensusAdaptor *rca, char *name) { char constraintStr[256]; Vector *rcVector; RepeatConsensus *rc; sprintf(constraintStr,"repeat_name = \'%s\' limit 1", name); rcVector = RepeatConsensusAdaptor_genericFetch(rca, constraintStr); rc = Vector_getElementAt(rcVector,0); Vector_free(rcVector); return rc; }
RepeatConsensus *RepeatConsensusAdaptor_fetchByDbID(RepeatConsensusAdaptor *rca, IDType dbID) { char constraintStr[256]; Vector *rcVector; RepeatConsensus *rc; sprintf(constraintStr,"repeat_consensus_id = " IDFMTSTR " limit 1", dbID); rcVector = RepeatConsensusAdaptor_genericFetch(rca, constraintStr); rc = Vector_getElementAt(rcVector,0); Vector_free(rcVector); return rc; }
void part1(){ char c; Vector in = Vector_create_char(); long long count = 0; while(scanf("%c", &c) != EOF){ Vector_push_char(in, c); } Vector_push_char(in, 0); for(int pos = 0; Vector_data_char(in)[pos];){ long long length; pos = rdp(in, pos, &length, 0); count += length; } printf("%lld\n", count); Vector_free(in); }
Vector *HomologyAdaptor_fetchHomologuesOfGene(HomologyAdaptor *ha, char *sp, char *gene) { char qStr[1024]; char *species; int nRelationship; IDType *relationshipIds; Vector *genes; int i; species = StrUtil_copyString(&species,sp,0); species = StrUtil_strReplChr(species,'_',' '); sprintf(qStr, "select grm.gene_relationship_id " " from gene_relationship_member grm, " " genome_db gd " " where gd.genome_db_id = grm.genome_db_id " " and gd.name = '%s' " " and grm.member_stable_id = '%s' " " group by grm.gene_relationship_id", species, gene); nRelationship = HomologyAdaptor_getRelationships(ha, qStr,&relationshipIds); genes = Vector_new(); for (i=0;i<nRelationship;i++) { Vector *homols; sprintf(qStr, "select grm.member_stable_id," " gd.name," " grm.chromosome," " grm.chrom_start," " grm.chrom_end" " from gene_relationship_member grm," " genome_db gd" " where grm.gene_relationship_id = " IDFMTSTR " and grm.genome_db_id = gd.genome_db_id " " and NOT (grm.member_stable_id = '%s')", relationshipIds[i], gene); homols = HomologyAdaptor_getHomologues(ha, qStr); Vector_append(genes,homols); Vector_free(homols); } free(relationshipIds); free(species); return genes; }
void part2(){ HashMap h = HM_create(Node_hash, Node_equals, NULL, free); Vector stack = Vector_create(sizeof(struct Node*)); struct Node *n; int ans = 1; while(readline(h, stack)); solve(stack); for(int i = 0; i < 3; ++i){ n = get_node(h, Output, i); ans *= n->inputs[0]; } HM_destroy(&h); Vector_free(stack); printf("%d\n", ans); }
MapperRangeSet *PredictionTranscript_genomic2cDNA(PredictionTranscript *trans, int start, int end, int strand, BaseContig *contig) { Mapper *mapper; // "ids" in mapper are contigs of exons, so use the same contig that should // be attached to all of the exons... if (!contig) { Vector *translateable = PredictionTranscript_getAllTranslateableExons(trans); PredictionExon *firstExon; if (!Vector_getNumElement(translateable)) { return MapperRangeSet_new(); } firstExon = Vector_getElementAt(translateable, 0); contig = (BaseContig*)PredictionExon_getSlice(firstExon); Vector_free(translateable); } mapper = PredictionTranscript_getcDNACoordMapper(trans); return Mapper_mapCoordinates(mapper,(IDType)contig, start, end, strand, "genomic"); }
PredictionTranscript *PredictionTranscriptAdaptor_fetchByStableId(PredictionTranscriptAdaptor *pta, char *stableId) { if (stableId == NULL) { fprintf(stderr,"Error: Stable_id argument expected in PredictionTranscriptAdaptor_fetchByStableId\n"); exit(1); } NameTableType *tables = pta->getTables(); char **primTab = (*tables)[0]; char *tableSynonym = primTab[SYN]; char constraint[1024]; sprintf(constraint, "%s.display_label = '%s'", tableSynonym, stableId); Vector *pts = PredictionTranscriptAdaptor_genericFetch(pta, constraint, NULL, NULL); PredictionTranscript *pt = Vector_getElementAt(pts, 0); Vector_free(pts); // NIY: Free pts if there are more than 1 // NIY: Perl seemed to allow no pts so undef return, but I've not done that in other places eg. TranscriptAdaptor so didn't here either return pt; }
Vector *HomologyAdaptor_fetchHomologuesOfGeneInSpecies(HomologyAdaptor *ha, char *sp, char *gene, char *hSp) { char qStr[1024]; Vector *genes; IDType *relationshipIds; int nRelationship; int i; char *hSpecies; char *species; species = StrUtil_copyString(&species,sp,0); species = StrUtil_strReplChr(species,'_',' '); hSpecies = StrUtil_copyString(&hSpecies,hSp,0); hSpecies = StrUtil_strReplChr(hSpecies,'_',' '); sprintf(qStr, "select grm.gene_relationship_id " " from gene_relationship_member grm, " " genome_db gd " " where gd.genome_db_id = grm.genome_db_id " " and gd.name = '%s' " " and grm.member_stable_id = '%s' " " group by grm.gene_relationship_id", species, gene); nRelationship = HomologyAdaptor_getRelationships(ha,qStr,&relationshipIds); genes = Vector_new(); for (i=0;i<nRelationship;i++) { Vector *homols = HomologyAdaptor_fetchHomologuesBySpeciesRelationshipId(ha,hSpecies,relationshipIds[i]); Vector_append(genes, homols); Vector_free(homols); } free(relationshipIds); free(species); free(hSpecies); return genes; }
Vector *AssemblyMapper_listSeqRegionsImpl(AssemblyMapper *am, char *frmSeqRegionName, long frmStart, long frmEnd, CoordSystem *frmCs) { //retrieve the seq_region names Vector *seqRegs = AssemblyMapper_listIds(am, frmSeqRegionName, frmStart, frmEnd, frmCs); // SMJS toCs doesn't seem to be used // CoordSystem *toCs; // if (!CoordSystem_compare(frmCs, AssemblyMapper_getAssembledCoordSystem(am))) { // toCs = AssemblyMapper_getComponentCoordSystem(am); // } else { // toCs = AssemblyMapper_getAssembledCoordSystem(am); // } // convert them to names AssemblyMapperAdaptor *adaptor = AssemblyMapper_getAdaptor(am); Vector *regions = AssemblyMapperAdaptor_seqIdsToRegions(adaptor, seqRegs); // Need to tidy up seqRegs; Vector_free(seqRegs); return regions; }
/* =head2 fetch_all_by_Transcript Arg[1] : Bio::EnsEMBL::Transcript Transcript to search with Example : my $ises = $isea->fetch_all_by_Transcript($transcript); Description : Uses the given Transcript to search for all instances of IntronSupportingEvidence linked to the transcript in the database Returntype : ArrayRef of IntronSupportingEvidence objects Exceptions : Thrown if arguments are not as stated and for DB errors =cut */ Vector *IntronSupportingEvidenceAdaptor_fetchAllByTranscript(IntronSupportingEvidenceAdaptor *isea, Transcript *transcript) { char qStr[1024]; sprintf(qStr,"SELECT intron_supporting_evidence_id " "FROM transcript_intron_supporting_evidence " "WHERE transcript_id = "IDFMTSTR, Transcript_getDbID(transcript)); StatementHandle *sth = isea->prepare((BaseAdaptor *)isea,qStr,strlen(qStr)); sth->execute(sth); Vector *idVec = Vector_new(); ResultRow *row; while ((row = sth->fetchRow(sth))) { IDType id = row->getLongLongAt(row, 0); IDType *idP; if ((idP = calloc(1,sizeof(IDType))) == NULL) { fprintf(stderr, "Failed allocating space for a id\n"); exit(1); } *idP = id; Vector_addElement(idVec, idP); } sth->finish(sth); Vector *out; if (Vector_getNumElement(idVec) > 0) { out = IntronSupportingEvidenceAdaptor_fetchAllByDbIDList(isea, idVec, NULL); } else { out = Vector_new(); } // Free ids vector Vector_setFreeFunc(idVec, free); Vector_free(idVec); return out; }
void Value_free(Value* val) { if(!val) return; switch(val->type) { case VAL_EXPR: BinOp_free(val->expr); break; case VAL_UNARY: UnOp_free(val->term); break; case VAL_CALL: FuncCall_free(val->call); break; case VAL_FRAC: Fraction_free(val->frac); break; case VAL_VAR: free(val->name); break; case VAL_VEC: Vector_free(val->vec); break; case VAL_ERR: Error_free(val->err); break; default: /* The rest don't need to be freed */ break; } free(val); }
/*! * @function pgm_load * Loads the pgm image at [filename] into a [width] x [height] unsigned char matrix, * and stores the pointer into [img]. * @param img * A pointer to an unsigned char pointer where the image will be stored. * @param height * A pointer to an int where the n of rows of the image will be stored. * @param width * A pointer to an int where the n of cols of the image will be stored. * @param filename * The name of the file to be read. * @return * 0 on success, non-0 on failure. */ int pgm_load(unsigned char ** const img, int * const height, int * const width, const char * const filename) { const char * const fname = "pgm_load"; if (NULL == img) { Debug_out(DEBUG_PGM, "%s: NULL pointer argument.\n", fname); goto error1; } char **lines = parseLines(filename); int i = 0, j; int l_width, l_height; unsigned int tmp; /* parse all file lines */ if (NULL == lines) { Debug_out(DEBUG_PGM, "%s: unable to read file '%s'.\n", fname, filename); goto error1; } /* check that first line says P2 */ if (NULL == lines[i]) { Debug_out(DEBUG_PGM, "%s: image ended too early.\n", fname); goto error2; } if (!StringUtils_startsWith(lines[i], "P2")) { Debug_out(DEBUG_PGM, "%s: illegal image start: %s.\n", fname, lines[i]); goto error2; } ++i; /* skip all comment lines */ while ((NULL != lines[i]) && (StringUtils_startsWith(lines[i], "#"))) { /* Comment line */ Debug_out(DEBUG_PGM, "%s: %s.\n", fname, lines[i]); ++i; } if (NULL == lines[i]) { Debug_out(DEBUG_PGM, "%s: image ended too early.\n", fname); goto error2; } /* parse width and height */ if (2 != sscanf(lines[i], "%d %d", &l_width, &l_height)) { Debug_out(DEBUG_PGM, "%s: error scanning line %d: %s.\n", fname, i, lines[i]); goto error2; } Debug_out(DEBUG_PGM, "%s: image size is %d x %d.\n", fname, l_width, l_height); ++i; if (NULL == lines[i]) { Debug_out(DEBUG_PGM, "%s: image ended too early.\n", fname); goto error2; } /* skip image max value */ Debug_out(DEBUG_PGM, "%s: image max value is %s.\n", fname, lines[i]); ++i; if (NULL == lines[i]) { Debug_out(DEBUG_PGM, "%s: image ended too early.\n", fname); goto error2; } /* allocate image and parse it */ *img = calloc(l_width * l_height, sizeof(unsigned char)); if (NULL == img) { Debug_out(DEBUG_PGM, "%s: calloc failed.\n", fname); goto error2; } Debug_out(DEBUG_PGM, "%s: Result image buffer allocated.\n", fname); for (j = 0; j < l_width * l_height; ++j) { if (NULL == lines[i+j]) { Debug_out(DEBUG_PGM, "%s: image ended too early.\n", fname); goto error3; } else if (1 != sscanf(lines[i+j], "%u", &tmp)) { Debug_out(DEBUG_PGM, "%s: error scanning line %d: %s.\n", fname, i+j, lines[i+j]); goto error3; } Debug_out(DEBUG_PGM, "%s: Pixel %d is %d.\n", fname, j, tmp); (*img)[j] = tmp; } /* save width and height, and free parsed lines */ if (NULL != width) { *width = l_width; } if (NULL != height) { *height = l_height; } Vector_free((void **) lines); return 0; error3: free(img); error2: Vector_free((void **) lines); error1: return 1; }
void Node_free(Node* node) { //析构函数 Vector_free(&((*node)->m_edges)); free(*node); *node = NULL; }
void Fauna_free(Fauna *fauna, Deallocator deallocator) { Vector_free(&fauna->npcs, deallocator); }