예제 #1
0
int *PM1_INSERT(int log2size){
	int *hdVector = VectorCreate();
	int iter = (1 << log2size) - 2;

	int i;
//	print("\n");
	for(i = 0; i < iter; i++){
	//	putnum(i);
	//	print(" ");
		hdVector = VectorPushBack(hdVector, i);		
	//	putnum(hdVector);
	//	print("\n");
	}
	return hdVector;
}
예제 #2
0
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);
}
예제 #3
0
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);
}