#include <stdio.h>
#include "array.h"
#include "base10.h"

int sizeof_str(char in[]) {
  int i = 0;
  for (i = 0; in[i] != '\0'; ++i)
    ;
  return i;
}

void print_str_array(char in[]) {
  int i;
  printf("{ ");
  for (i = 0; i < sizeof_str(in); ++i) {
    printf("%c", in[i]);
    if (i == sizeof_str(in) - 1) {
      printf(" }\n");
    } else {
      printf(", ");
    }
  }
}

void print_int_array(int_array *in) {
  int i;
  printf("{ ");
  for (i = 0; i < in->length; ++i) {
    printf("%d", in->data[i]);
    if (i == in->length - 1) {
      printf(" }\n");
    } else {
      printf(", ");
    }
  }
}

int check_int_array_sorted(int_array *in) {
  int i = 0;
  while(i < in->length - 1) {
    if (in->data[i] > in->data[i + 1])
      return 0;
    ++i;
  }
  
  return 1;
}

/* Convert an integer array into a combined integer */
int int_array_to_int(int_array *in) {
  int i;
  int out = 0;
  int current_digit;
  for (i = 0, current_digit = in->length; i <= in->length; ++i, --current_digit)
    out += in->data[i] * int_expand_digits(current_digit);
  return out;
}

/* Split an integer into an integer array */
int_array int_to_int_array(int in) {
  int in_length = int_count_digits(in);
  int out_data[in_length];
  int_array out;
  out.data = out_data;
  out.length = in_length; 
  int i;
  int j;
  for(i = in_length, j = 0; i >= 1; --i, ++j) {
    out.data[j] = in / int_expand_digits(i);
    in -= int_expand_digits(i) * out.data[j];
  }

  return out;
}
