/* method:  where2
   idx_true, idx_false = narray.where2 */
static VALUE
 na_where2(volatile VALUE obj)
{
  VALUE v1, v0;
  int  n, i, n1, n0;
  char *c;
  int32_t *idx1, *idx0;
  struct NARRAY *ary, *a1, *a0; /* a1=true, a0=false */

  GetNArray(obj,ary);
  /* Convert to NA_BYTE by calling "obj.ne(0)", if needed */
  if(ary->type != NA_BYTE) {
    obj = rb_funcall(obj, na_id_ne, 1, INT2FIX(0));
    GetNArray(obj,ary);
  }
  n = ary->total;

  /* Count true */
  c = ary->ptr;
  n1 = 0;
  for (i=0; i<n; ++i)
    if (*(c++)) ++n1;

  n0 = n-n1;

  /* partially true and false */
  v1 = na_make_object( NA_LINT, 1, &n1, cNArray );
  GetNArray(v1,a1);
  idx1 = (int32_t*) a1->ptr;
  v0 = na_make_object( NA_LINT, 1, &n0, cNArray );
  GetNArray(v0,a0);
  idx0 = (int32_t*) a0->ptr;

  /* Get Indices */
  c = ary->ptr;
  for ( i=0; i<n; ++i ) {
    if (*(c++))
      *(idx1++) = i;
    else
      *(idx0++) = i;
  }

  return rb_assoc_new( v1, v0 );
}