#include "psLib.h"
#include "psArray.h"

typedef struct {
    int x, y;
} psXY;

psXY *psXYAlloc(void)
{
    return psAlloc(sizeof(psXY));
}

void psXYFree(psXY *xy)
{
    psFree(xy);
}

PS_DECLARE_ARRAY_TYPE(psXY);
PS_CREATE_ARRAY_TYPE(psXY);

PS_DECLARE_ARRAY_PTR_TYPE(psXY);
PS_CREATE_ARRAY_PTR_TYPE(psXY);

int main(void)
{
    psXYArray *t = psXYArrayAlloc(10);
    psXYPtrArray *pt = psXYPtrArrayAlloc(10);

    for (int i = 0; i < t->n; i++) {
	t->arr[i].x = i;
	pt->arr[i]->y = 10*i;
    }

    t = psXYArrayRealloc(t, 5);
    t = psXYArrayRealloc(t, 8);

    for (int i = 0; i < t->n; i++) {
	printf("%d %d  ", t->arr[i].x, pt->arr[i]->y);
    }
    printf("\n");
    
    psXYArrayFree(t);

    psXY *xy = psMemDecrRefCounter(pt->arr[0]);
    pt->arr[0] = NULL;
    psXYFree(xy);    
    
    psXYPtrArrayFree(pt);

    psMemCheckLeaks(0, NULL, stderr);

    return 0;
}
