static rc_t rgn_store_bio_or_adapter( Vector * stock, Vector * to, const int32_t * block, int32_t sra_read_type ) { rc_t rc = 0; if ( block[ pacbio_idx_start ] < block[ pacbio_idx_end ] ) { region * a_region; rc = rgn_get_or_make( stock, &a_region ); if ( rc == 0 ) { a_region->spot_id = block[ pacbio_idx_spot_id ]; a_region->type = sra_read_type; a_region->start = block[ pacbio_idx_start ]; a_region->end = block[ pacbio_idx_end ]; /* see every region shorter as MIN_BIOLOGICAL_LEN as a technical (adapter) region */ if ( ( a_region->end - a_region->start ) <= MIN_BIOLOGICAL_LEN ) a_region->type = SRA_READ_TYPE_TECHNICAL; a_region->filter = SRA_READ_FILTER_PASS; rc = VectorInsert ( to, a_region, NULL, rgn_sort_by_start ); } } return rc; }
static bool ng_add_node( p_ng generator, const uint64_t num_1, const uint64_t num_2 ) { bool res = false; if ( generator != NULL ) { p_ng_node node; if ( num_1 == num_2 ) { node = ng_make_node( num_1, 1 ); } else if ( num_1 < num_2 ) { node = ng_make_node( num_1, ( num_2 - num_1 ) + 1 ); } else { node = ng_make_node( num_2, ( num_1 - num_2 ) + 1 ); } if ( node != NULL ) { res = ( VectorInsert( &(generator->nodes), node, NULL, ng_insert_helper ) == 0 ); if ( res ) { generator->node_count++; } } } return res; }
/* helper function for range-check */ static void CC num_gen_copy_valid_nodes( void *item, void *data ) { p_num_gen_node node = ( p_num_gen_node )item; Vector *dest = ( Vector * )data; if ( ( node->start != 0 )&&( node->count != 0 ) ) VectorInsert ( dest, node, NULL, num_gen_insert_helper ); else free ( node ); }
/* helper callback to create a deep and conditional copy of a node-vector */ static void CC num_gen_copy_cb( void *item, void *data ) { p_num_gen_node node = ( p_num_gen_node )item; if ( node->count > 0 ) { Vector * dst = ( Vector *)data; p_num_gen_node new_node = num_gen_make_node( node->start, node->count ); if ( new_node != NULL ) VectorInsert( dst, new_node, NULL, num_gen_insert_helper ); } }
static void TestInsertDelete(vector *alphabet) { char ch = '-'; int i; for (i = 3; i < VectorLength(alphabet); i += 4) // Insert dash every 4th char VectorInsert(alphabet, &ch, i); fprintf(stdout, "\nAfter insert dashes: "); VectorMap(alphabet, PrintChar, stdout); for (i = 3; i < VectorLength(alphabet); i += 3) // Delete every 4th char VectorDelete(alphabet, i); fprintf(stdout, "\nAfter deleting dashes: "); VectorMap(alphabet, PrintChar, stdout); ch = '!'; VectorInsert(alphabet, &ch, VectorLength(alphabet)); VectorDelete(alphabet, VectorLength(alphabet) - 1); fprintf(stdout, "\nAfter adding and deleting to very end: "); VectorMap(alphabet, PrintChar, stdout); }
static rc_t add_ref_region_range( struct reference_region * self, const uint64_t start, const uint64_t end ) { rc_t rc = 0; struct reference_range *r = make_range( start, end ); if ( r == NULL ) rc = RC( rcApp, rcNoTarg, rcConstructing, rcMemory, rcExhausted ); else { rc = VectorInsert ( &self->ranges, r, NULL, cmp_range_wrapper ); if ( rc != 0 ) free( r ); } return rc; }
static void CC ng_copy_valid_nodes( void *item, void *data ) { p_ng_node the_node = ( p_ng_node )item; Vector *p_temp_nodes = ( Vector * )data; if ( ( the_node->start != 0 )&&( the_node->count != 0 ) ) { p_ng_node new_node = ng_make_node( the_node->start, the_node->count ); if ( new_node ) { VectorInsert( p_temp_nodes, new_node, NULL, ng_insert_helper ); } } }
/* inserts the generated region into the sort_Regions */ static rc_t rgn_generate( Vector * stock, Vector * dst, const int32_t spot_id, const uint32_t start, const uint32_t len ) { region * a_region; rc_t rc = rgn_get_or_make( stock, &a_region ); if ( rc == 0 ) { a_region->spot_id = spot_id; a_region->type = SRA_READ_TYPE_TECHNICAL; /*means "i dont know"*/ a_region->start = start; a_region->end = start + len; a_region->filter = SRA_READ_FILTER_CRITERIA; rc = VectorInsert ( dst, a_region, NULL, rgn_sort_by_start ); } return rc; }
/* helper function for the parse-function */ static rc_t num_gen_add_node( num_gen* self, const uint64_t from, const uint64_t to ) { p_num_gen_node node = NULL; int64_t count = ( to - from ); if ( self == NULL ) return RC( rcVDB, rcNoTarg, rcInserting, rcSelf, rcNull ); if ( count >= 0 ) node = num_gen_make_node( from, count + 1 ); else node = num_gen_make_node( to, -( count + 1 ) ); if ( node == NULL ) return RC( rcVDB, rcNoTarg, rcInserting, rcMemory, rcExhausted ); return VectorInsert( &(self->nodes), node, NULL, num_gen_insert_helper ); }
static void MemoryTest() { int i; const char * const kQuestionWords[] = {"who", "what", "where", "how", "why"}; const int kNumQuestionWords = sizeof(kQuestionWords) / sizeof(kQuestionWords[0]); vector questionWords; char *questionWord; fprintf(stdout, "\n\n------------------------- Starting the memory tests...\n"); fprintf(stdout, "Creating a vector designed to store dynamically allocated C-strings.\n"); VectorNew(&questionWords, sizeof(char *), FreeString, kNumQuestionWords); fprintf(stdout, "Populating the char * vector with the question words.\n"); for (i = 0; i < kNumQuestionWords; i++) { questionWord = malloc(strlen(kQuestionWords[i]) + 1); strcpy(questionWord, kQuestionWords[i]); VectorInsert(&questionWords, &questionWord, 0); // why the ampersand? isn't questionWord already a pointer? } fprintf(stdout, "Mapping over the char * vector (ask yourself: why are char **'s passed to PrintString?!!)\n"); VectorMap(&questionWords, PrintString, stdout); fprintf(stdout, "Finally, destroying the char * vector.\n"); VectorDispose(&questionWords); }
static rc_t parse_and_add_matepair_dist( Vector * dist_vector, const char * s ) { rc_t rc = 0; uint64_t start = 0, end = 0; range *r; if ( cmp_pchar( s, "unknown" ) != 0 ) parse_matepair_definition( s, &start, &end ); r = make_range( start, end ); if ( r == NULL ) { rc = RC( rcExe, rcNoTarg, rcValidating, rcMemory, rcExhausted ); (void)LOGERR( klogErr, rc, "error storing matepair-distance" ); } else { rc = VectorInsert ( dist_vector, r, NULL, cmp_range_wrapper ); if ( rc != 0 ) free( r ); } return rc; }
static bool vdn_add_node( num_gen* generator, const uint64_t num_1, const uint64_t num_2 ) { rc_t res; p_num_gen_node node = NULL; if ( generator == NULL ) return false; if ( num_1 == num_2 ) { node = vdn_make_node( num_1, 1 ); } else if ( num_1 < num_2 ) { node = vdn_make_node( num_1, ( num_2 - num_1 ) + 1 ); } else { node = vdn_make_node( num_2, ( num_1 - num_2 ) + 1 ); } if ( node == NULL ) return false; res = VectorInsert( &(generator->nodes), node, NULL, vdn_insert_helper ); if ( res == 0 ) generator->node_count++; return ( res == 0 ); }
/* ************************************************************* tries to merge overlapping adapter-regions... ( uses rgn->read_Regions as scratch-pad ) INTPUT : rgn->sort_Regions OUTPUT : rgn->sort_Regions ************************************************************* */ static rc_t rgn_merge_consecutive_regions( Vector * stock, Vector * from, Vector * to ) { rc_t rc = 0; uint32_t i, count = VectorLength ( from ); region * a_region = NULL; region * prev_region = NULL; for ( i = 0; i < count && rc == 0; ++ i ) { /* take the region out of rgn->sort_Regions*/ rc = VectorRemove ( from, 0, (void**)&a_region ); if ( rc == 0 ) { bool copy = true; if ( prev_region != NULL ) { if ( ( a_region->start <= prev_region->end ) && ( a_region->type == prev_region->type ) && ( a_region->filter == prev_region->filter ) ) { prev_region->end = a_region->end; /* put the now unused region back into the stock */ VectorAppend ( stock, NULL, a_region ); /* and keep prev-region! */ copy = false; } } if ( copy ) { prev_region = a_region; rc = VectorInsert ( to, a_region, NULL, rgn_sort_by_start ); } } } return rc; }
void ManipulateObjects() { /* We should initialize the container before any operations. */ Vector* vector = VectorInit(DEFAULT_CAPACITY); VectorSetClean(vector, CleanObject); /* Push the object elements. */ Tuple* tuple = (Tuple*)malloc(sizeof(Tuple)); tuple->first = 3; tuple->second = -3; VectorPushBack(vector, tuple); tuple = (Tuple*)malloc(sizeof(Tuple)); tuple->first = 4; tuple->second = -4; VectorPushBack(vector, tuple); /* Insert the elements at the specified indexes. */ tuple = (Tuple*)malloc(sizeof(Tuple)); tuple->first = 1; tuple->second = -1; VectorInsert(vector, 0, tuple); tuple = (Tuple*)malloc(sizeof(Tuple)); tuple->first = 2; tuple->second = -2; VectorInsert(vector, 1, tuple); /*---------------------------------------------------------------* * Now the vector should be: [1] | [2] | [3] | [4] * *---------------------------------------------------------------*/ /* Iterate through the vector. */ int num = 1; void* elem; VectorFirst(vector, false); while (VectorNext(vector, &elem)) { assert(((Tuple*)elem)->first == num); ++num; } /* Reversely iterate through the vector. */ num = 4; VectorFirst(vector, true); while (VectorReverseNext(vector, &elem)) { assert(((Tuple*)elem)->first == num); --num; } /* Get the elements from the specified indexes. */ VectorGet(vector, 0, &elem); assert(((Tuple*)elem)->first == 1); VectorGet(vector, 3, &elem); assert(((Tuple*)elem)->first == 4); /* Replace the elements at the specified indexes. */ tuple = (Tuple*)malloc(sizeof(Tuple)); tuple->first = 10; tuple->second = -10; VectorSet(vector, 0, tuple); tuple = (Tuple*)malloc(sizeof(Tuple)); tuple->first = 40; tuple->second = -40; VectorSet(vector, 3, tuple); /*---------------------------------------------------------------* * Now the vector should be: [10] | [2] | [3] | [40] * *---------------------------------------------------------------*/ /* Get the number of stored elements. */ unsigned size = VectorSize(vector); assert(size == 4); /* Sort the integer elements. */ VectorSort(vector, CompareObject); /*---------------------------------------------------------------* * Now the vector should be: [2] | [3] | [10] | [40] * *---------------------------------------------------------------*/ /* Remove the elements at the specified indexes. */ VectorRemove(vector, 3); VectorRemove(vector, 0); /*---------------------------------------------------------------* * Now the vector should be: [3] | [10] * *---------------------------------------------------------------*/ VectorGet(vector, 0, &elem); assert(((Tuple*)elem)->first == 3); VectorGet(vector, 1, &elem); assert(((Tuple*)elem)->first == 10); /* Pop the elements. */ VectorPopBack(vector); VectorPopBack(vector); assert(VectorSize(vector) == 0); VectorDeinit(vector); }
void VectorAppend(vector *v, const void *elemAddr) { assert (v!=NULL); VectorInsert(v,elemAddr,v->realSize); }
/* ************************************************************* if gap is 1, expand previous region by 1 (correct off-by-1) fill in gaps > 1 ( regions are not consecutive ) correct overlapping regions fill in a gap at the end, if the last region does not reach spotlen INTPUT : rgn->read_Regions OUTPUT : rgn->sort_Regions ************************************************************* */ static rc_t rgn_correct( Vector * stock, Vector * from, Vector * to, const uint32_t spot_id, const uint32_t spot_len, regions_stat * stats ) { rc_t rc; int32_t start, expands_a = 0, expands_i = 0, inserts = 0; uint32_t i, count = VectorLength ( from ); for ( rc = 0, start = 0, i = 0; i < count && rc == 0; ++ i ) { region * a_region; /* take the region out of rgn->read_Regions*/ rc = VectorRemove ( from, 0, (void**)&a_region ); if ( rc == 0 ) { int32_t gap_len = ( a_region->start - start ); if ( gap_len == 1 ) { /* the gap-length is one, try to expand the previous region */ if ( !rgn_expand_last_rgn_by_1( to, &expands_a, &expands_i ) ) { /* there is no previous region ! */ rc = rgn_generate( stock, to, spot_id, start, gap_len ); } } else if ( gap_len > 1 ) { /* generate a artificial gap in the middle or the start of the spot */ rc = rgn_generate( stock, to, spot_id, start, gap_len ); inserts++; } else if ( gap_len < 0 ) { /* a negative gap would be an error in the sorting of the regions, or an overlapp of regions */ if ( ( a_region->start - gap_len ) > a_region->end ) { rc = RC ( rcExe, rcNoTarg, rcLoading, rcData, rcInconsistent ); LOGERR( klogErr, rc, "((a_region->start-gap_len)>a_region->end) in rgn_correct()'" ); } else /** move the start point ***/ { a_region->start -= gap_len; stats->overlapps++; } } if ( rc == 0 ) { rc = VectorInsert ( to, a_region, NULL, rgn_sort_by_start ); if ( rc != 0 ) LOGERR( klogErr, rc, "VectorInsert(rgn_sort_by_start) in rgn_correct()'" ); start = a_region->end; } else { rc_t rc1 = VectorInsert( stock, a_region, NULL, NULL ); if ( rc1 != 0 ) LOGERR( klogErr, rc, "VectorInsert(NULL) in rgn_correct()'" ); } } } if ( rc == 0 ) { /* if the last region does not reach to the end of the spot */ if ( start < spot_len ) { int32_t gap_len = ( spot_len - start ); if ( gap_len == 1 ) { /* !!! this can also happen if spot_len == 1 !!! */ /* the gap-length is one, try to expand the previous region */ if ( ! rgn_expand_last_rgn_by_1( to, &expands_a, &expands_i ) ) { /* there is no previous region ! */ rc = rgn_generate( stock, to, spot_id, start, gap_len ); stats->end_gap++; } } else if ( gap_len > 0 ) { /* fill the gap to the end... */ rc = rgn_generate( stock, to, spot_id, start, gap_len ); stats->end_gap++; } else if ( gap_len < 0 ) { /* a negative gap would be an error in the sorting of the regions, or an overlapp of regions */ rc = RC ( rcExe, rcNoTarg, rcLoading, rcData, rcInconsistent ); LOGERR( klogErr, rc, "(gap_len<0) in rgn_correct()'" ); } } } /* do some statistics */ if ( ( expands_i + expands_a ) > 0 ) { stats->expands_a += expands_a; stats->expands_i += expands_i; stats->expands_spots++; } if ( inserts > 0 ) { stats->inserts += inserts; stats->inserts_spots++; } return rc; }
static rc_t list_action (const KDirectory * dir, const char * path, void * _adata) { rc_t rc = 0; list_adata * data = _adata; list_item * item = NULL; KPathType type = KDirectoryPathType (dir, "%s", path); size_t pathlen = strlen (path); size_t linklen = 0; char link [2 * 4096]; /* we'll truncate? */ if (type & kptAlias) { rc = KDirectoryVResolveAlias (dir, false, link, sizeof (link), path, NULL); if (rc == 0) linklen = strlen (link); } if (rc == 0) { item = calloc (sizeof (*item) + pathlen + linklen + 2, 1); /* usually one too many */ if (item == NULL) { rc = RC (rcExe, rcNoTarg, rcAllocating, rcMemory, rcExhausted); } else { do { item->path = (char *)(item+1); strcpy (item->path, path); item->type = type; rc = KDirectoryAccess (dir, &item->access, "%s", path); if (rc) break; rc = KDirectoryDate (dir, &item->mtime, "%s", path); if (rc) break; if (type & kptAlias) { item->link = item->path + pathlen + 1; strcpy (item->link, link); } else switch (type & ~kptAlias) { case kptNotFound: rc = RC (rcExe, rcDirectory, rcAccessing, rcPath, rcNotFound); break; case kptBadPath: rc = RC (rcExe, rcDirectory, rcAccessing, rcPath, rcInvalid); break; case kptZombieFile: data->has_zombies = true; case kptFile: rc = KDirectoryFileSize (dir, &item->size, "%s", path); if (rc == 0) rc = KDirectoryFileLocator (dir, &item->loc, "%s", path); DBGMSG (DBG_APP, 1, ("%s: found file %s size %lu at %lu\n", __func__, item->path, item->size, item->loc)); break; case kptDir: DBGMSG (DBG_APP, 1, ("%s: found directory %s\n", __func__, item->path)); break; default: DBGMSG (DBG_APP, 1, ("%s: found unknown %s\n", __func__, item->path)); break; } } while (0); } } if (rc == 0) { VectorAppend (&data->list, NULL, item); VectorInsert (&data->sort, item, NULL, list_item_cmp); if (type == kptDir) rc = step_through_dir (dir, path, list_action, data); } return rc; }
void VectorAppend(cvector *v, const void *elemAddr) { VectorInsert(v,elemAddr,v->ItemsCount); }
void ManipulateNumerics() { /* We should initialize the container before any operations. */ Vector* vector = VectorInit(DEFAULT_CAPACITY); /* Push the integer elements. */ VectorPushBack(vector, (void*)(intptr_t)3); VectorPushBack(vector, (void*)(intptr_t)4); /* Insert the elements at the specified indexes. */ VectorInsert(vector, 0, (void*)(intptr_t)1); VectorInsert(vector, 1, (void*)(intptr_t)2); /*---------------------------------------------------------------* * Now the vector should be: [1] | [2] | [3] | [4] * *---------------------------------------------------------------*/ /* Iterate through the vector. */ int num = 1; void* elem; VectorFirst(vector, false); while (VectorNext(vector, &elem)) { assert((intptr_t)(void*)elem == num); ++num; } /* Reversely iterate through the vector. */ num = 4; VectorFirst(vector, true); while (VectorReverseNext(vector, &elem)) { assert((intptr_t)(void*)elem == num); --num; } /* Get the elements from the specified indexes. */ VectorGet(vector, 0, &elem); assert((intptr_t)(void*)elem == 1); VectorGet(vector, 3, &elem); assert((intptr_t)(void*)elem == 4); /* Replace the elements at the specified indexes. */ VectorSet(vector, 0, (void*)(intptr_t)10); VectorSet(vector, 3, (void*)(intptr_t)40); /*---------------------------------------------------------------* * Now the vector should be: [10] | [2] | [3] | [40] * *---------------------------------------------------------------*/ /* Get the number of stored elements. */ unsigned size = VectorSize(vector); assert(size == 4); /* Sort the integer elements. */ VectorSort(vector, CompareNumber); /*---------------------------------------------------------------* * Now the vector should be: [2] | [3] | [10] | [40] * *---------------------------------------------------------------*/ /* Remove the elements at the specified indexes. */ VectorRemove(vector, 3); VectorRemove(vector, 0); /*---------------------------------------------------------------* * Now the vector should be: [3] | [10] * *---------------------------------------------------------------*/ VectorGet(vector, 0, &elem); assert((int)(intptr_t)elem == 3); VectorGet(vector, 1, &elem); assert((int)(intptr_t)elem == 10); /* Pop the elements. */ VectorPopBack(vector); VectorPopBack(vector); assert(VectorSize(vector) == 0); VectorDeinit(vector); }