void TestFunction()
{
	char puzzle_board_data[] =
	{
		't', 'h', 'i', 's',
		'w', 'a', 't', 's',
		'o', 'a', 'h', 'g',
		'f', 'g', 'd', 't'
	};
	char* dictionary[] =
	{
		"this", "two", "fat", "that"
	};

	Array words, ret;
	int i;
	size_t index, count;
	PuzzleBoard puzzle_board = { puzzle_board_data, 4, 4 };

	ArrayInit(&words, 4, sizeof(char*));
	for (i = 0; i < 4; ++i)
	{
		ArrayPush(&words, &dictionary[i]);
	}

	// solution1
	ret = solution1_Function(&puzzle_board, &words);
	count = ArrayUsed(&ret);
	MLOG("solution 1:\n");
	for (index = 0; index < count; ++index)
	{
		RetWord* word = (RetWord*)ArrayGet(&ret, index);
		MLOG("%s (%d, %d) - (%d, %d)\n", word->word, word->start_x, word->start_y, word->end_x, word->end_y);
	}
	ArrayDestroy(&ret);

	// solution2
	ret = solution2_Function(&puzzle_board, &words);
	count = ArrayUsed(&ret);
	MLOG("solution 2:\n");
	for (index = 0; index < count; ++index)
	{
		RetWord* word = (RetWord*)ArrayGet(&ret, index);
		MLOG("%s (%d, %d) - (%d, %d)\n", word->word, word->start_x, word->start_y, word->end_x, word->end_y);
	}
	ArrayDestroy(&ret);

	ArrayDestroy(&words);
}
Exemplo n.º 2
0
void g_free_gdb(GDB *gdb)
/*
 * free gdb structure
 */
{
    if (gdb != NULL) {
	if (gdb->gfile != NULL) {
	    g_close_file(gdb->gfile);
	    gdb->gfile = NULL;
	}
	if (gdb->client != NULL) { ArrayDestroy(gdb->client); gdb->client = NULL; }
	if (gdb->view != NULL) { ArrayDestroy(gdb->view); gdb->view = NULL; }
	xfree(gdb);
    }
}
Exemplo n.º 3
0
static void g_destroy_index(GFile *gfile)
/*
 * destroy gfile index
 */
{
    if (gfile!=NULL && gfile->idx!=NULL) {
	ArrayDestroy(gfile->idx);
	gfile->idx=NULL;
    }
}
Exemplo n.º 4
0
int track_set_data(GapIO *io, track_t **t, Array value) {
    track_t *n;
    if (!(n = cache_rw(io, *t)))
        return -1;

    if (n->data)
        ArrayDestroy(n->data);

    n->data = value;
    *t = n;

    return 0;
}
Exemplo n.º 5
0
PetscErrorCode FluidFieldDestroy(FluidField f)
{
  PetscErrorCode ierr;

  PetscFunctionBegin;
  ierr = DMDestroy(&f->daV); CHKERRQ(ierr);
  ierr = MatDestroy(&f->mat); CHKERRQ(ierr);
  ierr = KSPDestroy(&f->ksp); CHKERRQ(ierr);
  ierr = VecDestroy(&f->rhs); CHKERRQ(ierr);
  ierr = VecDestroy(&f->vel); CHKERRQ(ierr);
  ierr = VecDestroy(&f->vel0); CHKERRQ(ierr);
  ierr = ArrayDestroy(f->dirichletBC); CHKERRQ(ierr);
  GA_Destroy(f->ga);

  ierr = DMDestroy(&f->daE); CHKERRQ(ierr);
//  ierr = VecDestroy(f->E); CHKERRQ(ierr);

  ierr = DMDestroy(&f->daB); CHKERRQ(ierr);
  ierr = VecDestroy(&f->buf); CHKERRQ(ierr);
  ierr = PetscFree(f); CHKERRQ(ierr);
  PetscFunctionReturn(0);
}
void TestPerformance()
{
	// puzzle board
	char puzzle_board_data[] =
	{
		't', 'h', 'i', 's',
		'w', 'a', 't', 's',
		'o', 'a', 'h', 'g',
		'f', 'g', 'd', 't'
	};
	PuzzleBoard puzzle_board = { puzzle_board_data, 4, 4 };

	// dictionary
	char* buf;
	long num;
	if (!File_Read("res/wordsEn/wordsEn.txt", &buf, &num))
	{
		MASSERT_MSG(0, "File (res/wordsEn/wordsEn.txt) not exist!\n");
		MLOG("File (res/wordsEn/wordsEn.txt) not exist!\n");
		return;
	}

	if (num == 0)
	{
		MLOG("dictionary is empty\n");
		return;
	}

	Array dictionary;
	ArrayInit(&dictionary, 8, sizeof(char*));

	char* p = buf;
	char* q = p;
	while (p)
	{
		if (*p == '\r' || *p == '\n')
		{
			*p = '\0';
			if (q != p)
			{
				ArrayPush(&dictionary, &q);
			}
			++p;
			q = p;
			continue;
		}

		if (*p == '\0')
		{
			if (q != p)
			{
				ArrayPush(&dictionary, &q);
			}
			break;
		}

		++p;
	}

	// performance test
	PerformanceTest test;
	size_t piece_size = dictionary.used / 128;
	for (int i = 1; i <= 32; ++i)
	{
		size_t num = i * piece_size;
		size_t count;
		Array ret;

		Array cur_dictionary;
		ArrayInit(&cur_dictionary, 8, sizeof(char*));
		for (size_t j = 0; j < num; ++j)
		{
			ArrayPush(&cur_dictionary, ArrayGet(&dictionary, j));
		}

		// solution 1
		PERFORMANCE_TEST_ADD(test, "puzzle - solution 1", (int)num, ret = solution1_Function(&puzzle_board, &cur_dictionary));
		count = ArrayUsed(&ret);
		MLOG("solution 1:\n");
		for (size_t index = 0; index < count; ++index)
		{
			RetWord* word = (RetWord*)ArrayGet(&ret, index);
			MLOG("%s (%d, %d) - (%d, %d)\n", word->word, word->start_x, word->start_y, word->end_x, word->end_y);
		}
		ArrayDestroy(&ret);

		// solution 2
		PERFORMANCE_TEST_ADD(test, "puzzle - solution 2", (int)num, ret = solution2_Function(&puzzle_board, &cur_dictionary));
		count = ArrayUsed(&ret);
		MLOG("solution 1:\n");
		for (size_t index = 0; index < count; ++index)
		{
			RetWord* word = (RetWord*)ArrayGet(&ret, index);
			MLOG("%s (%d, %d) - (%d, %d)\n", word->word, word->start_x, word->start_y, word->end_x, word->end_y);
		}
		ArrayDestroy(&ret);

		ArrayDestroy(&cur_dictionary);
	}
	test.WriteCompareToFile("puzzle.txt");

	// destroy
	ArrayDestroy(&dictionary);
	free(buf);
}
Exemplo n.º 7
0
int main(int argc, char* argv[])
{
    Array d;
    //int i;
    //char a = 96;
    //int code = 0;
    int pused = 2;
    int MAXBITS = 12;
    //int pref;
    // e;
    if (argc >= 2){
        MAXBITS = atoi(argv[1]);
    }
    d = internalArrayCreate((1<<MAXBITS) -2);

    FILE *ip = fopen(argv[2], "r");

    import(d, ip);
    ArrayInsert(d, 0, 0, 0);
    ArrayPrint(d);

    /*for(i = 0; i < 8; i++) {
        a++;
        pref =(int) a + i;
        if(d->n >= d->size) {
            printf("%s\n", "Table filled, grow");
            //exit(1);
            Arraygrow(d);
        }       
        ArrayInsert(d, pref, a, i);
        e = ArraySearchAll(d, i);
        e->used++;
        if (i % 2 == 0){
            e->used++;
        }
        printf("Found (%d, %d) at code %d, used %d\n", e->p, e->k, i, e->used);

    }*/

    ArrayPrune(d, pused);
    //ArrayInsert(d, 105, 'e', 4);
    ArrayPrint(d);
    /*for(i = 0; i < 10; i++) {
        a++;
        pref =(int) a + i;
        if(d->n >= d->size) {
            printf("%s\n", "Table filled, grow");
            Arraygrow(d);
        }       
        ArrayInsert(d, pref, a, i);
        e = ArraySearchAll(d, i);
        e->used++;
        printf("Found (%d, %d) at code %d, used %d\n", e->p, e->k, i, e->used);

    }
    FILE *fp = fopen("table", "w");
    ArraytoFile(d, fp);

    ArrayDelete(d, 4);
    assert(ArraySearch(d, 4) == -1);
    a=96;
    for(i = 0; i < 10; i++) {
        a++;
        pref =(int) a + i;
        e = ArraySearchAll(d, i);
        if (ArraySearch(d, i) == 0){
            printf("Found (%d, %d) at code %d, used %d\n", e->p, e->k, i, e->used);
        }     
        else{
            printf("Did not find any thing at %d\n", i);
        }  
    }

    ArrayInsert(d, 105, 'e', 4);
    if (ArraySearchAll(d, 4) != 0){
            printf("Found newly inserted (%d, %d) at code %d\n", ArraySearchP(d, 4), ArraySearchK(d, 4), 4);
    }*/
    ArrayDestroy(d);
    printf("%s", "Array is empty\n");


    return 0;
}
Exemplo n.º 8
0
int shuffle_contigs_io(GapIO *io, int ncontigs, contig_list_t *contigs,
		       int band, int flush) {
    int i; //, start;
    Array indels;
    
    set_malign_lookup(5);
    /* set_alignment_matrix("/tmp/nuc_matrix", "ACGTURYMWSKDHVB-*"); */

    indels = ArrayCreate(sizeof(con_indel_t), 0);

    for (i = 0; i < ncontigs; i++) {
	tg_rec cnum = contigs[i].contig;
	int64_t old_score, new_score, tot_score, orig_score;
	//for (start = 0; start < 1000000; start += 1000) {
	//  MALIGN *malign = build_malign(io, cnum, start, start + 1000);
	MALIGN *malign;
	int c_start, c_shift;

	vmessage("Shuffling pads for contig %s\n", get_contig_name(io, cnum));

	/*
	 * The shuffle pads code (malign) comes from gap4 and has lots of
	 * assumptions that the contig goes from base 1 to base N.
	 * Fixing these assumptions is a lot of work, so for now we will take
	 * the cheat route of moving the contig to ensure the assumption
	 * is valid.
	 */
	if (-1 == consensus_valid_range(io, cnum, &c_start, NULL)) {
	    verror(ERR_WARN, "shuffle_contigs_io",
		   "Failure in consensus_valid_range()");
	    return -1;
	}
	//printf("Contig starts at base %d\n", c_start);
	c_shift = 1-c_start;
	if (c_shift != 0) {
	    if (move_contig(io, cnum, c_shift) != 0)
		return -1;
	}

	//printf("Shuffle #%"PRIrec" from %d..%d, shift %d\n",
	//       contigs[i].contig, contigs[i].start, contigs[i].end, c_shift);

	malign = build_malign(io, cnum,
			      contigs[i].start + c_shift,
			      contigs[i].end   + c_shift);
	resort_contigl(malign);

	malign_add_region(malign,
			  contigs[i].start + c_shift,
			  contigs[i].end + c_shift);

	ArrayMax(indels) = 0;
	orig_score = new_score = malign_diffs(malign, &tot_score);
	vmessage("Initial score %.2f%% mismatches (%"PRId64" mismatches)\n",
		 (100.0 * orig_score)/tot_score, orig_score/128);
	if (flush)
	    UpdateTextOutput();
	//print_malign(malign);
	do {
	    old_score = new_score;
	    malign = realign_seqs(cnum, malign, band, indels);
	    //print_malign(malign);
	    new_score = malign_diffs(malign, &tot_score);
	    vmessage("  Consensus difference score: %"PRId64"\n", new_score);
	    if (flush)
		UpdateTextOutput();
	} while (new_score < old_score);

	if (new_score < orig_score) {
	    //print_malign(malign);
	    update_io(io, cnum, malign, indels);

	    /*
	     * It's possible the contig ends could move if a sequence that
	     * was previously the end of a contig has been moved such that
	     * it's no longer the contig end. This can lead to tags off the
	     * end of the contig, so trim them (reusing break_contig
	     * code).
	     */
	     contig_visible_start(io, cnum, CITER_CSTART);
	     contig_visible_end(io, cnum, CITER_CEND);
	} else {
	    vmessage("Could not reduce number of consensus differences.\n");
	}

	/* Remove pad columns */
	//printf("New score=%d, orig_score=%d\n", new_score, orig_score);
	if (new_score < orig_score) {
	    contigs[i].start += c_shift;
	    contigs[i].end += c_shift;
	    remove_pad_columns(io, 1, &contigs[i], 100, 1);

	    //contig_t *c;
	    //c = cache_search(io, GT_Contig, cnum);
	    //cache_incr(io, c);
	    //remove_pads(io, malign, c, contigs[i].start, contigs[i].end);
	    //cache_decr(io, c);
	}

	destroy_malign(malign, 1);

	vmessage("Final score %.2f%% mismatches\n",
		 (100.0 * new_score)/tot_score);

	/*
	 * Sequences like
	 *   AGCT**GATGC
	 *             TGGATCGA
	 * can end up causing holes. We break the contig in this case to
	 * avoid minor database inconsistencies.
	 */
	// remove_contig_holes(io, cnum);

	/* reassign_confidence_values(io, cnum); */
      //}

	/* Shift contig back */
	if (c_shift != 0) {
	    if (move_contig(io, cnum, -c_shift) != 0)
		return -1;
	}

	if (flush)
	    cache_flush(io);
    }

    ArrayDestroy(indels);

    return 0;
}