/**
 * Copyright (c) 2021 Dominic Masters
 * 
 * This software is released under the MIT License.
 * https://opensource.org/licenses/MIT
 */

#include "dynarray.h"

void dynArrayInit(dynarray_t *array, size_t size, int32_t total) {
  array->total = total;
  array->size = size;
  array->length = 0;
  array->data = malloc(size * total);
}

void * dynArrayGet(dynarray_t *array, int32_t i) {
  return arrayGet(array->size, array->data, i);
}

void dynArraySet(dynarray_t *array, int32_t i, void *data) {
  array->length = mathMax(array->length, i+1);
  arraySet(array->size, array->data, i, data);
}

int32_t dynArrayPush(dynarray_t *array, void *data) {
  int32_t i = array->length;
  dynArraySet(array, i, data);
  return i;
}

int32_t dynArrayAdd(dynarray_t *array) {
  int32_t i = array->length;
  array->length++;
  return i;
}

void dynArrayPop(dynarray_t *array) {
  array->length -= 1;
}

void dynArraySplice(dynarray_t *array, int32_t start, int32_t count) {
  arraySplice(array->size, array->data, start, count, array->length);
  array->length -= count;
}

void dynArrayDispose(dynarray_t *array) {
  free(array->data);
}