#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <limits.h>

#include "dtypes.h"
#include "arraylist.h"

#ifdef __cplusplus
extern "C" {
#endif

arraylist_t *arraylist_new(arraylist_t *a, size_t size)
{
    a->len = 0;
    if (size <= AL_N_INLINE) {
        a->items = &a->_space[0];
        a->max = AL_N_INLINE;
    }
    else {
        a->items = (void**)LLT_ALLOC(size*sizeof(void*));
        a->max = size;
    }
    if (a->items == NULL) return NULL;
    return a;
}

void arraylist_free(arraylist_t *a)
{
    if (a->items != &a->_space[0])
        LLT_FREE(a->items);
    a->len = 0;
    a->max = AL_N_INLINE;
    a->items = &a->_space[0];
}

static void al_grow(arraylist_t *a, size_t n)
{
    if (a->len+n > a->max) {
        if (a->items == &a->_space[0]) {
            void **p = (void**)LLT_ALLOC((a->len+n)*sizeof(void*));
            if (p == NULL) return;
            memcpy(p, a->items, a->len*sizeof(void*));
            a->items = p;
            a->max = a->len+n;
        }
        else {
            size_t nm = a->max*2;
            if (nm == 0) nm = 1;
            while (a->len+n > nm) nm*=2;
            void **p = (void**)LLT_REALLOC(a->items, nm*sizeof(void*));
            if (p == NULL) return;
            a->items = p;
            a->max = nm;
        }
    }
    a->len += n;
}

void arraylist_push(arraylist_t *a, void *elt)
{
    al_grow(a, 1);
    a->items[a->len-1] = elt;
}

void *arraylist_pop(arraylist_t *a)
{
    if (a->len == 0) return NULL;
    void *p = a->items[--a->len];
    a->items[a->len] = NULL;
    return p;
}

#ifdef __cplusplus
}
#endif
