Esempio n. 1
0
int main(int argc, char ** argv) {
  int iline=0, i, iarg=1, nfields=1, jmax=0, writetxt=0, writemat=0, grpsize=1;
  int membuf=1048576;
  char * here, *linebuf, *readbuf;
  string ifname = "", ofname = "", mfname = "", ffname = "", matfname = "", fdelim="\t", suffix="";
  if (argc < 2) {
    printf("%s", usage);
    return 1;
  }
  while (iarg < argc) {
    if (strncmp(argv[iarg], "-d", 2) == 0) {
      fdelim = argv[++iarg];
    } else if (strncmp(argv[iarg], "-c", 2) == 0) {
      suffix=".gz";
    } else if (strncmp(argv[iarg], "-f", 2) == 0) {
      ffname = argv[++iarg];
    } else if (strncmp(argv[iarg], "-i", 2) == 0) {
      ifname = argv[++iarg];
    } else if (strncmp(argv[iarg], "-m", 2) == 0) {
      mfname = argv[++iarg];
    } else if (strncmp(argv[iarg], "-o", 2) == 0) {
      ofname = argv[++iarg];
    } else if (strncmp(argv[iarg], "-s", 2) == 0) {
      membuf = strtol(argv[++iarg],NULL,10);
    } else if (strncmp(argv[iarg], "-?", 2) == 0) {
      printf("%s", usage);
      return 1;
    } else if (strncmp(argv[iarg], "-h", 2) == 0) {
      printf("%s", usage);
      return 1;
    } else {
      cout << "Unknown option " << argv[iarg] << endl;
      exit(1);
    }
    iarg++;
  }
  if (mfname.size() == 0) mfname = ofname;
  ivector tvec(0);
  svector delims(0);
  svector dnames(0);
  nfields = parseFormat(ffname, tvec, dnames, delims, &grpsize);
  srivector srv(nfields);
  ftvector ftv(nfields);

  istream * ifstr;
  linebuf = new char[membuf];
  readbuf = new char[membuf];

  ifstr = open_in_buf(ifname, readbuf, membuf);

  while (!ifstr->bad() && !ifstr->eof()) {
    ifstr->getline(linebuf, membuf-1);
    linebuf[membuf-1] = 0;
    if (ifstr->fail()) {
      ifstr->clear();
      ifstr->ignore(LONG_MAX,'\n');
    }
    if (strlen(linebuf) > 0) {
      jmax++;
      try {
        parseLine(linebuf, membuf, ++iline, fdelim.c_str(), tvec, delims, srv, ftv, grpsize);
      } catch (int e) {
        cerr << "Continuing" << endl;
      }
    }
    if ((jmax % 100000) == 0) {
      cout<<"\r"<<jmax<<" lines processed";
      cout.flush();
    }
  }
  if (ifstr) delete ifstr;
  cout<<"\r"<<jmax<<" lines processed";
  cout.flush();

  for (i = 0; i < nfields; i++) {
    switch (tvec[i]) {
    case ftype_int: case ftype_dt: case ftype_mdt: case ftype_date: case ftype_mdate: case ftype_cmdate:
      ftv[i].writeInts(ofname + dnames[i] + ".imat" + suffix);
      break;
    case ftype_dint:
      ftv[i].writeDInts(ofname + dnames[i] + ".dimat" + suffix);
      break;
    case ftype_qhex:
      ftv[i].writeQInts(ofname + dnames[i] + ".imat" + suffix);
      break;
    case ftype_float:
      ftv[i].writeFloats(ofname + dnames[i] + ".fmat" + suffix);
      break;
    case ftype_double:
      ftv[i].writeDoubles(ofname + dnames[i] + ".dmat" + suffix);
      break;
    case ftype_word:
      ftv[i].writeInts(ofname + dnames[i] + ".imat" + suffix);
      srv[i].writeMap(mfname + dnames[i], suffix);
      break;
    case ftype_string: case ftype_group: 
      ftv[i].writeIVecs(ofname + dnames[i] + ".imat" + suffix);
      srv[i].writeMap(mfname + dnames[i], suffix);
      break;
    case ftype_igroup:
      ftv[i].writeIVecs(ofname + dnames[i] + ".imat" + suffix);
      break;
    case ftype_digroup:
      ftv[i].writeDIVecs(ofname + dnames[i]);
      break;
    default:
      break;
    }    
  }
  printf("\n");
  if (linebuf) delete [] linebuf;
  return 0;
}
Esempio n. 2
0
//[[Rcpp::export]]
Rcpp::NumericMatrix SUPERMATRIX(Rcpp::List a,bool keep_names) {

  size_t i,j,k;
  Rcpp::NumericMatrix mat;

  int tot=0;

  Rcpp::CharacterVector rnam;
  Rcpp::CharacterVector cnam;

  Rcpp::CharacterVector this_nam;
  Rcpp::List dnames(2);
  bool any_rnames=false;
  bool any_cnames=false;

  for(i=0; i < a.size(); i++) {
    mat = Rcpp::as<Rcpp::NumericMatrix>(a[i]);
    if(mat.nrow() ==0) continue;
    if(mat.nrow() != mat.ncol()) Rcpp::stop("Not all matrices are square");

    tot = tot + mat.nrow();

    if(!keep_names) continue;

    dnames = mat.attr("dimnames");

    if(dnames.size()==0) {
      for(j=0; j < mat.nrow(); j++) {
	rnam.push_back(".");
	cnam.push_back(".");
      }
      continue;
    }

    if(!Rf_isNull(dnames[0])) {
      any_rnames=true;
      this_nam = dnames[0];
      for(j=0; j< this_nam.size(); j++) rnam.push_back(this_nam[j]);
    } else {
      for(j=0; j < mat.nrow(); j++) rnam.push_back(".");
    }
    if(!Rf_isNull(dnames[1])) {
      any_cnames=true;
      this_nam = dnames[1];
      for(j=0; j< this_nam.size(); j++) cnam.push_back(this_nam[j]);
    } else {
      for(j=0; j < mat.ncol(); j++) cnam.push_back(".");
    }
  }


  int totrow = 0;
  int totcol = 0;

  Rcpp::NumericMatrix ret(tot,tot);
  for(i=0; i < a.size(); i++) {
    mat = Rcpp::as<Rcpp::NumericMatrix>(a[i]);

    for(j=0; j < mat.nrow(); j++) {
      for(k=0; k < mat.ncol(); k++) {
	ret(totrow+j,totcol+k) = mat(j,k);
      }
    }
    totrow = totrow + mat.nrow();
    totcol = totcol + mat.ncol();
  }

  if(keep_names) {
    Rcpp::List dn = Rcpp::List::create(rnam,cnam);
    ret.attr("dimnames") = dn;
  }

  return(ret);
}