/* Example: int * dynamic array, 0-initialized (vti0_), standard vector type */
/* Part of genvector examples (http://repo.hu/projects/genvector) */
#include <stdio.h>
#include <genvector/vti0.h>

/* print the array with a prefix */
static void dump(const char *prefix, vti0_t *v)
{
	if (v->array != NULL) {
		int n;
		printf("%-12s", prefix);
		for(n = 0; n < vti0_len(v); n++)
			printf(" %d", v->array[n]);
		printf("\n");
	}
	else
		printf("%-12s <NULL>\n", prefix);
}

int main()
{
	int n;
	vti0_t a, a2;
	int carr[] = {2, 4, 8, 16};
	int carr2[] = {7, 6, 5, 4};

	/* Initialize the vector (to empty, which means the array is not allocated) */
	vti0_init(&a);
	dump("empty: ", &a);


	/* append 100..109, int by int */
	for(n = 0; n < 10; n++)
		vti0_append(&a, 100+n);
	dump("append: ", &a);

	/* append a static array */
	vti0_append_len(&a, carr, sizeof(carr)/sizeof(int));
	dump("append_len: ", &a);

	/* truncate after 8 elements */
	vti0_truncate(&a, 8);
	dump("truncate: ", &a);

	/* Concat two vectors: sort of "s += s2" */
	vti0_init(&a2);
	vti0_append_len(&a, carr2, sizeof(carr2)/sizeof(int));
	vti0_concat(&a, &a2);
	dump("concat: ", &a);

	/* Array is still just an int * array */
	printf("[3] is:      %d\n", a.array[3]);

	/* It can be read or written as an int * array, within bounds */
	a.array[5] = 99;
	dump("array asg: ", &a);

	/* Initializing an element beyond current end will create multiple
	   new elements, all initialized to 0 */
	vti0_set(&a, 15, 77);
	dump("set: ", &a);

	/* Remove a mid-section of the array, 3 elements starting from [2] */
	vti0_remove(&a, 2, 3);
	dump("remove: ", &a);

	/* Free memory */
	vti0_uninit(&a);
	vti0_uninit(&a2);

	return 0;
}
