/*
  Called by the marshalling mechanism to retrieve a permanent copy of a 
  Dtable. 
 */
VALUE dtable_load(VALUE klass, VALUE str)
{
  VALUE ret = Qnil;
  VALUE s = StringValue(str);
  unsigned char * buf = (unsigned char *) StringValuePtr(s);
  unsigned char * dest = buf + RSTRING_LEN(s);
  unsigned i; /* for GET_UNSIGNED */
  unsigned tmp = 0;
  long rows, cols;
  long x,y;
  double ** data;
  double * col;
  /*  depending on the first byte, the decoding will be different */
  switch(*(buf++)) 
    {
    case 1:
      GET_UNSIGNED(tmp, buf);
      rows = tmp;
      GET_UNSIGNED(tmp, buf);
      cols = tmp;
      /* create a new Dtable with the right size */
      ret = dtable_init(dtable_alloc(cDtable), cols, rows);
      data = Dtable_Ptr(ret, NULL, NULL);
      for(x = 0; x < rows; x++) 
        {
          col = data[x];
          for(y = 0; y< cols; y++)
            {
              if(buf + 8 > dest)
                {
                  rb_raise(rb_eRuntimeError, 
                           "corrupted data given to Dtable._load");
                  break;
                }     
              else 
                {
                  col[y] = get_double(buf);
                  buf += 8;
                }
            }
        }
      break;
    default:
      rb_raise(rb_eRuntimeError, "corrupted data given to Dtable._load");
    }
  return ret;
}