/*
  Splits the function on NaN values for x, y or xy, depending on
  whether _sym_ is +:x+, +:y+ or +:xy+ (or, as a matter of fact,
  anything else than +:x+ or +:y+).

  This returns an array of new Function objects.

  This function will return empty Function objects between consecutive
  NaN values.
*/
static VALUE function_split_on_nan(VALUE self, VALUE sym)
{
  VALUE ret = rb_ary_new();
  VALUE cur_x = Dvector_Create();
  VALUE cur_y = Dvector_Create();
  int on_x = 1;
  int on_y = 1;
  long size = function_sanity_check(self);
  long cur_size = 0;
  long i;
  if(size < 2)
    rb_raise(rb_eRuntimeError, "Function needs to have at least 2 points");

  double *x = Dvector_Data_for_Read(get_x_vector(self),NULL);
  double *y = Dvector_Data_for_Read(get_y_vector(self),NULL);

  VALUE f;
  
  if(sym == ID2SYM(rb_intern("x")))
    on_y = 0;
  else if(sym == ID2SYM(rb_intern("y")))
    on_x = 0;


  for(i = 0; i < size; i++) {
    if((on_x && isnan(x[i])) ||
       (on_y && isnan(y[i]))) {
      /* We split */
      f = Function_Create(cur_x, cur_y);
      rb_ary_push(ret, f);
      cur_x = Dvector_Create();
      cur_y = Dvector_Create();
    }
    else {
      Dvector_Push_Double(cur_x, x[i]);
      Dvector_Push_Double(cur_y, y[i]);
    }
  }
  f = Function_Create(cur_x, cur_y);
  rb_ary_push(ret, f);
  return ret;
}