
int vectorp(int v) {
  if (MvectorpProper(v))
    return Mtrue;
  else
    return Mfalse;
}

int make_vector_core(int n, int vector_sub_type) {
  // used for vector subtypes in general
  int vector_loc;
  int next_loc;

  next_loc = curr_cell + n + 1;
  if (next_loc >= cell_max) {
    gc(0, 0, n);
    next_loc = curr_cell + n + 1;
  }
  vector_loc = curr_cell;
  curr_cell = next_loc;
  Mvecset(vector_loc, 0, n | vector_sub_type); // vector length and subtype
  // no need to init vector. gc zeros the cells.
  return VECTORTYPE | vector_loc;
}

int make_vector_no_init(int n) { // used in exec for speed
  int vector_loc;
  int next_loc;

  next_loc = curr_cell + n + 1;
  if (next_loc >= cell_max) {
    gc(0, 0, n);
    next_loc = curr_cell + n + 1;
  }
  vector_loc = curr_cell;
  curr_cell = next_loc;
  Mvecset(vector_loc, 0, n); // vector length. subtype = 0
  return VECTORTYPE | vector_loc;
}

int make_vector(int n) {
  return make_vector_core(n, VECTORSUBTYPE);
}

int make_vector_integer(int n) {
  return make_vector(get_integer_val(n));
}

int vector_length_integer(int v) {
  return make_integer(Mveclength(v));
}

int vector_ref(int v, int n) {
  if (n >= Mveclength(v)) {
    printf("vector reference out of range ");
    write_int(n);
    writeln();
    cleanup_();
  }
  return Mvecvecref(v, n);
}

int vector_ref_no_check(int v, int n) {
  return Mvecvecref(v, n);
}

int vector_set(int v, int n, int obj) {
  if (n >= Mveclength(v)) {
    printf("vector reference out of range ");
    write_int(n);
    writeln();
    cleanup_();
  }
  Mvecvecset(v, n, obj);
  return obj;
}

int vector_fill(int v, int obj) {
  int i;
  int size;

  size = Mveclength(v);
  for (i = 0; i < size; i++)
    vector_set(v, i, obj);
  return v;
}

int make_vector_integer_with_fill(int n, int obj) {
  int vec;
  vec = make_vector(get_integer_val(n));
  return vector_fill(vec, obj);
}

int list_to_vector(int l) {
  // this is really used in the scheme library. this version is used by
  // read.scm. It leaks!
  int v;
  int n;
  int size;

  size = list_length(l);
  v = make_vector_core(size, VECTORSUBTYPE);
  for (n = 0; n < size; n++) {
    vector_set(v, n, Mcar(l));
    l = kdr(l);
  }
  return v;
}
