// This is a library for array based integer lists.

// Each list is an array.
// The first element is the length.
// The rest of the elements are the contents.
// Because of this, indices are 1-based.

const int SIZE = 0;

void ArrayList_Clear(int list) {
  list[0] = 0;}

void ArrayList_Append(int list, int value) {
  list[SIZE]++;
  list[list[SIZE]] = value;
  //ArrayList_Print(list);
}

int ArrayList_IndexOf(int list, int value) {
  int size = list[0];
  for (int index = 1; index <= size; index++) {
    if (list[index] == value) {
      return index;}}
  return -1;}

// Returns removed value.
int ArrayList_Remove(int list, int index) {
  int size = list[SIZE];
  if (index > size) {return -1;}
  int value = list[index];
  for (; index < size; index++) {
    list[index] = list[index + 1];}
  list[0]--;
  //ArrayList_Print(list);
  return value;}

// Removes a random element.
int ArrayList_RemoveRandom(int list) {
  return ArrayList_Remove(list, Rand(list[SIZE]) + 1);}

void ArrayList_Print(int list) {
  int msg[256];
  int format[] = "List (%d):";
  sprintf(msg, format, list[SIZE]);
  TraceS(msg);
  for (int i = 1; i <= list[SIZE]; i++) {
    int format[] = " %d";
    int msg[32];
    sprintf(msg, format, list[i]);
    TraceS(msg);}
  TraceNL();}
