/* Example: custom type dynamic array, separate vector type header, custom vector type */
/* Part of genvector examples (http://repo.hu/projects/genvector) */

/* The generic engine is instantiated by vtf0.h and vtf0.c. The new instance,
   vtf0_* represents a dynamic array of floats, new elements initialized to 0.
*/

#include <stdio.h>
#include <string.h>
#include "vtf0.h"

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

int main()
{
	int n;
	vtf0_t a, a2;
	float farr[] = {2.01, 4, 8, 16};
	float farr2[] = {7, 6, 5.55, 4};

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


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

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

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

	/* Concat two vectors: sort of "s += s2" */
	vtf0_init(&a2);
	vtf0_append_len(&a, farr2, sizeof(farr2)/sizeof(int));
	vtf0_concat(&a, &a2);
	dump("concat: ", &a);

	/* Array is still just an int * array */
	printf("[3] is:      %.2f\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 */
	vtf0_set(&a, 15, 77);
	dump("set: ", &a);

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

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

	return 0;
}
