Exemplo n.º 1
0
boolean vector_push( lc_vector_t* p_vector, void *data )
{
	byte* dst;
	assert( p_vector );

	/* grow the array if needed */
	if( vector_size(p_vector) >= vector_array_size(p_vector) )
	{
		size_t new_size      = VECTOR_GROWTH_FACTOR * p_vector->array_size + 1;
		p_vector->array_size = new_size;
		p_vector->array      = realloc( p_vector->array, p_vector->element_size * vector_array_size(p_vector) );
		assert( p_vector->array );
	}

	#ifdef _DEBUG_VECTOR
	memset( vector_array(p_vector) + vector_size(p_vector) * vector_element_size(p_vector), 0, vector_element_size(p_vector) );
	#endif

	dst = vector_array(p_vector) + (vector_size(p_vector) * vector_element_size(p_vector));

	memcpy( dst, data, vector_element_size(p_vector) );
	p_vector->size++;

	return p_vector->array != NULL;
}
Exemplo n.º 2
0
boolean vector_pop( lc_vector_t* p_vector )
{
	boolean result = TRUE;
	assert( p_vector );

	DESTROY_CHECK(
		void *element = vector_array(p_vector) + (vector_size(p_vector) - 1) * vector_element_size(p_vector);
		result  = p_vector->destroy( element );
	);
Exemplo n.º 3
0
void *vector_pushx( lc_vector_t* p_vector )
{
	void *result;

	assert( p_vector );

	/* grow the array if needed */
	if( vector_size(p_vector) >= vector_array_size(p_vector) )
	{
		size_t new_size      = VECTOR_GROWTH_FACTOR * p_vector->array_size + 1;
		p_vector->array_size = new_size;
		p_vector->array      = realloc( p_vector->array, p_vector->element_size * vector_array_size(p_vector) );
		assert( p_vector->array );
	}

	#ifdef _DEBUG_VECTOR
	memset( vector_array(p_vector) + vector_size(p_vector) * vector_element_size(p_vector), 0, vector_element_size(p_vector) );
	#endif

	result = (void *)( vector_array(p_vector) + vector_size(p_vector) * vector_element_size(p_vector));

	p_vector->size++;
	return result;
}
Exemplo n.º 4
0
TEST(VectorTest, AccessorsManipulators) {
	status_t err = NO_ERROR;
	vector_handle_t vector = NULL;
	size_t count = 0;
	size_t capacity = 0;
	char element = 0;
	char* p_element = NULL;

	err = vector_create(1, 1, &vector);
	ASSERT_EQ(err, NO_ERROR);
	ASSERT_TRUE(vector != NULL);

	err = vector_capacity(vector, &capacity);
	ASSERT_EQ(err, NO_ERROR);
	ASSERT_EQ(capacity, (size_t)1);


	err = vector_count(vector, &count);
	ASSERT_EQ(err, NO_ERROR);
	ASSERT_EQ(count, (size_t)0);

	err = vector_append(vector, &element);
	ASSERT_EQ(err, NO_ERROR);

	err = vector_count(vector, &count);
	ASSERT_EQ(err, NO_ERROR);
	ASSERT_EQ(count, (size_t)1);

	err = vector_pop(vector);
	ASSERT_EQ(err, NO_ERROR);

	err = vector_count(vector, &count);
	ASSERT_EQ(err, NO_ERROR);
	ASSERT_EQ(count, (size_t)0);

	for (element = 0; element < 10; element++) {
		err = vector_append(vector, &element);
		ASSERT_EQ(err, NO_ERROR);
	}

	err = vector_count(vector, &count);
	ASSERT_EQ(err, NO_ERROR);
	ASSERT_EQ(count, (size_t)10);

	err = vector_capacity(vector, &capacity);
	ASSERT_EQ(err, NO_ERROR);
	ASSERT_GT(capacity, (size_t)1);

	err = vector_element_address(vector, 5, (void**)&p_element);
	ASSERT_EQ(err, NO_ERROR);
	ASSERT_TRUE(NULL != p_element);
	ASSERT_EQ(*p_element, (char)5);

	err = vector_remove(vector, 5);
	ASSERT_EQ(err, NO_ERROR);

	err = vector_element_address(vector, 5, (void**)&p_element);
	ASSERT_EQ(err, NO_ERROR);
	ASSERT_TRUE(NULL != p_element);
	ASSERT_EQ(*p_element, (char)6);

	err = vector_count(vector, &count);
	while ((err == NO_ERROR) && (count > 4)) {
		err = vector_pop(vector);
		ASSERT_EQ(err, NO_ERROR);
		err = vector_count(vector, &count);
	}

	ASSERT_EQ(err, NO_ERROR);
	ASSERT_EQ(count, (size_t)4);

	err = vector_remove(vector, 6);
	ASSERT_NE(err, NO_ERROR);

	err = vector_array(vector, (void**)&p_element);
	ASSERT_EQ(err, NO_ERROR);
	ASSERT_TRUE(NULL != p_element);

	element = 0;
	while (element < 4) {
		ASSERT_EQ(*p_element, element);
		p_element++;
		element++;
	}

	err = vector_clear(vector);
	ASSERT_EQ(err, NO_ERROR);

	err = vector_count(vector, &count);
	ASSERT_EQ(err, NO_ERROR);
	ASSERT_EQ(count, (size_t)0);

	err = vector_pop(vector);
	ASSERT_NE(err, NO_ERROR);

	vector_release(vector);
}
Exemplo n.º 5
0
	return p_vector->array != NULL;
}

boolean vector_pop( lc_vector_t* p_vector )
{
	boolean result = TRUE;
	assert( p_vector );

	DESTROY_CHECK(
		void *element = vector_array(p_vector) + (vector_size(p_vector) - 1) * vector_element_size(p_vector);
		result  = p_vector->destroy( element );
	);

	#ifdef _DEBUG_VECTOR
	memset( vector_array(p_vector) + (vector_size(p_vector) - 1) * vector_element_size(p_vector), 0, vector_element_size(p_vector) );
	#endif

	p_vector->size--;
	return result;
}

boolean vector_resize( lc_vector_t* p_vector, size_t new_size )
{
	if( vector_size(p_vector) > new_size )
	{
		while( vector_size(p_vector) > new_size )
		{
			vector_pop( p_vector );
		}
	}