Пример #1
0
int vectortest(void)
{
    	VECTOR test;
        VECTOR fusetest;
	int n = 10, a = 5, b = 0, cnt = 0;
	int* thrdVal;
	test = makeVECTOR(sizeof(int), 3); //Create VECTOR. Reserve space for 3 integer elements.
        fusetest = makeVECTOR(sizeof(int), 3); //Create VECTOR. Reserve space for 3 integer elements.
        
        vectorpush(&fusetest, (void*)&a);
        vectorpush(&fusetest, (void*)&b);
        vectorpush(&fusetest, (void*)&n);
        
	//Show the VECTOR size and allocated space.
	printf("VECTOR size: %i, max: %i\n", test.size, test.maxsize);

	//Push 10 values to the VECTOR
	do
	{
		if(vectorpush(&test, &n) < 1)
		{
			printf("Error pushing element %i\n", cnt);
			exit;
		}
		n += 2;
		cnt++;
	}while(cnt < 10);

	vectorinsert(&test, &a, 5);
	vectordelete(&test, 3);
	a += 12;
	vectorinsert(&test, &a, 3);
	vectorpop(&test, &b);
	printf("Pop: %i\n", b);
	vector_read(&test, &b, 8);
	printf("Read 8: %i\n", b);
	vector_reverse(&test);
	vector_resize(&test, 20);
	cnt = 0;
	do
	{
		if(vectorpush(&test, &n) < 1)
		{
			printf("Error pushing element %i\n", cnt);
			exit;
		}
		n += 2;
		cnt++;
	}while(cnt < 5);

	//Show the new VECTOR size and allocated space.
	printf("VECTOR size: %i, max: %i\n", test.size, test.maxsize);
	cnt = 0;
	//Show every value inside the VECTOR
	do
	{
		thrdVal = (int*)&test.data[sizeof(int)*cnt];
		printf("test[%i] = %i\n", cnt,*thrdVal);
		cnt++;
	}while(cnt < test.size);

	vector_shrink_to_fit(&test);
	vector_swap(&test, &a, 9);
	vector_swap_b(&test, 1, 13);

	printf("VECTOR size: %i, max: %i\n", test.size, test.maxsize);
	cnt = 0;
	//Show every value inside the VECTOR
	do
	{
		thrdVal = (int*)&test.data[sizeof(int)*cnt];
		printf("test[%i] = %i\n", cnt,*thrdVal);
		cnt++;
	}while(cnt < test.size);
        
        printf("\n- vector fuse -\n");
        printf("VECTOR size: %i, max: %i\n", fusetest.size, fusetest.maxsize);
        printf("fusetest[0] = %i\n", *thrdVal);
        vector_fuse(&fusetest, &test);
        cnt = 0;
        printf("VECTOR size after fuse: %i, max: %i\n", fusetest.size, fusetest.maxsize);
        do{
            vector_read(&fusetest, thrdVal, cnt);
            printf("%i: %i\n", cnt, *thrdVal);
            cnt++;
        }while(cnt < fusetest.size);
        
        //Vector copy
        printf("Before copy...\n");
        cnt = 0;
        do
	{
		thrdVal = (int*)&test.data[sizeof(int)*cnt];
		printf("test[%i] = %i\n", cnt,*thrdVal);
		cnt++;
	}while(cnt < test.size);
        
        vector_copy(&test, &fusetest, 1, 4, 2);
        printf("After copy...\n");
        cnt = 0;
	do
	{
		thrdVal = (int*)&test.data[sizeof(int)*cnt];
		printf("test[%i] = %i\n", cnt,*thrdVal);
		cnt++;
	}while(cnt < test.size);
        printf("VECTOR size: %i, max: %i\n", test.size, test.maxsize);
        
        //Clean the memory
	vectorclear(&test);
	printf("VECTOR size: %i, max: %i\n", test.size, test.maxsize);
        
	return 0;
}
Пример #2
0
double fill_trellis(int *in, int *out, double(*cost)(int, int), int mode) {
    int i, x, y, inlen, outlen;
    double left, down, diag, p;
    inlen = intseqlen(in);
    outlen = intseqlen(out);
    g_trellis[0][0] = g_zero;
    for (x = 1; x <= outlen; x++) {
		g_trellis[x][0] = g_trellis[x-1][0] + cost(0,out[x-1]);
		g_backptr[x][0] = LEFT;
    }
    for (y = 1; y <= inlen; y++) {
		g_trellis[0][y] = g_trellis[0][y-1] + cost(in[y-1], 0);
		g_backptr[0][y] = DOWN;
    }
    for (x = 1; x <= outlen; x++) {
		for (y = 1; y <= inlen; y++) {
			left = g_trellis[x-1][y] + cost(0,out[x-1]);
			down = g_trellis[x][y-1] + cost(in[y-1], 0);
			diag = g_trellis[x-1][y-1] + cost(in[y-1], out[x-1]);
	    
			if (mode == MATRIX_MODE_MED) {
				g_trellis[x][y] = MIN3(left, diag, down);
				g_backptr[x][y] = CMP3(left, diag, down);
			}
			else if (mode == MATRIX_MODE_GS) {
				g_trellis[x][y] = log_add(log_add(left, diag), down);
			}
		}
    }

    /* Resample a new "path" for the string pair <in:out> starting from upper right-hand corner
       in the matrix and moving left, down, or diagonally down/left until we reach [0,0]
       ..[B][A]   To choose the direction we do a weighted coin toss between choices A -> B, A -> C, A -> D:
       ..[C][D]   w(B) = p(B) * p(B->A) ; w(C) = p(C) * p(C->A) ; w(D) = p(D) * p(D -> A).
          .  .    and p(X->Y) = the probability of taking the transition (X->Y)               
          .  .    Since we've stored the probabilities in log space, we need to do some scaling
                  and conversion before doing the weighted toss.		   
    */                                          

    if (mode == MATRIX_MODE_GS) {
		for (y = inlen, x = outlen; x > 0 || y > 0 ; ) {
			if (x == 0) {
				y--;
			} else if (y == 0) {
				x--;
			} else {
				left = g_trellis[x-1][y] + cost(0,out[x-1]);
				down = g_trellis[x][y-1] + cost(in[y-1], 0);
				diag = g_trellis[x-1][y-1] + cost(in[y-1], out[x-1]);
				g_backptr[x][y] = random_3draw(left, diag, down);
				x--;
				y--;
			}
		}
    }

    for (i = 0, y = inlen, x = outlen; x > 0 || y > 0; i++) {
		if (g_backptr[x][y] == DIAG) {
			x--; 
			y--;
			g_in_result[i] = in[y];
			g_out_result[i] = out[x];
		} else if (g_backptr[x][y] == LEFT) {
			x--;
			g_in_result[i] = 0;
			g_out_result[i] = out[x];
		} else if (g_backptr[x][y] == DOWN) {
			y--;
			g_in_result[i] = in[y];
			g_out_result[i] = 0;
		}
    }

    g_in_result[i] = -1;
    g_out_result[i] = -1;

    vector_reverse(g_in_result, i);
    vector_reverse(g_out_result, i);
    p = g_trellis[outlen][inlen];
    return(p);
}