From 8d36234bbf94ae74334908500cca5ce16717683a Mon Sep 17 00:00:00 2001 From: Michael Chirico Date: Thu, 11 Jul 2019 18:46:45 +0800 Subject: [PATCH 01/10] fwrite supports complex-valued columns, part of #3690 --- NEWS.md | 2 ++ inst/tests/tests.Rraw | 10 ++++++++-- src/fwrite.c | 24 ++++++++++++++++++++++-- src/fwrite.h | 4 ++++ src/fwriteR.c | 3 +++ 5 files changed, 39 insertions(+), 4 deletions(-) diff --git a/NEWS.md b/NEWS.md index 691647cba1..7988a84494 100644 --- a/NEWS.md +++ b/NEWS.md @@ -35,6 +35,8 @@ * Gains `yaml` argument matching that of `fread`, [#3534](https://github.com/Rdatatable/data.table/issues/3534). See the item in `fread` for a bit more detail; here, we'd like to reiterate that feedback is appreciated in the initial phase of rollout for this feature. * Gains `bom` argument to add a *byte order mark* (BOM) at the beginning of the file to signal that the file is encoded in UTF-8, [#3488](https://github.com/Rdatatable/data.table/issues/3488). Thanks to Stefan Fleck for requesting and Philippe Chataignon for implementing. + + * Supports writing complex-valued columns, part of [#3690](https://github.com/Rdatatable/data.table/issues/3690). 4. Assigning to one item of a list column no longer requires the RHS to be wrapped with `list` or `.()`, [#950](https://github.com/Rdatatable/data.table/issues/950). diff --git a/inst/tests/tests.Rraw b/inst/tests/tests.Rraw index 0c498e9f76..50ed746af1 100644 --- a/inst/tests/tests.Rraw +++ b/inst/tests/tests.Rraw @@ -9566,8 +9566,6 @@ test(1658.44, file.info(f3)$size, file.info(f1)$size) unlink(c(f1,f2,f3)) DT = data.table(a=1:3, b=list(1:4, c(3.14, 100e10), c("foo", "bar", "baz"))) test(1658.45, fwrite(DT), output=c("a,b","1,1|2|3|4","2,3.14|1e+12","3,foo|bar|baz")) -DT[3,b:=c(3i,4i,5i)] -test(1658.46, fwrite(DT), error="Row 3 of list column is type 'complex'") DT[3,b:=factor(letters[1:3])] test(1658.47, fwrite(DT), error="Row 3 of list column is type 'factor'") @@ -9595,6 +9593,14 @@ test(1658.53, fwrite(DT, file=f<-tempfile(fileext=".gz"), bom=TRUE, col.names=FA if (test_R.utils) test(1658.54, fread(f), setnames(DT,c("V1","V2"))) unlink(f) +# complex column support for fwrite, part of #3690 +DT = data.table(a = 1:3, z = 0:2 - (2:0)*1i) +test(1658.54, fwrite(DT), output = '0-2i.*2,1-1i.*3,2+0i') +test(1658.55, fwrite(data.table(exp(1) - pi*1i)), output = '2.718[0-9]*-3.141[0-9]*i') +## formerly 1658.46 +DT = data.table(a=1:3, b=list(1:4, c(3.14, 100e10), c(3i,4i,5i))) +test(1658.56, fwrite(DT), output='0+3i|0+4i|0+5i') + ## End fwrite tests # tests for #679, inrange(), FR #707 diff --git a/src/fwrite.c b/src/fwrite.c index 949f82f6db..d2ff9574ec 100644 --- a/src/fwrite.c +++ b/src/fwrite.c @@ -7,6 +7,7 @@ #include // isfinite, isnan #include // abs #include // strlen, strerror +#include #ifdef WIN32 #include @@ -168,7 +169,7 @@ void genLookups() { } */ -void writeFloat64(double *col, int64_t row, char **pch) +void writeFloat64Scalar(double x, char **pch) { // hand-rolled / specialized for speed // *pch is safely the output destination with enough space (ensured via calculating maxLineLen up front) @@ -178,7 +179,6 @@ void writeFloat64(double *col, int64_t row, char **pch) // ii) no C libary calls such as sprintf() where the fmt string has to be interpretted over and over // iii) no need to return variables or flags. Just writes. // iv) shorter, easier to read and reason with in one self contained place. - double x = col[row]; char *ch = *pch; if (!isfinite(x)) { if (isnan(x)) { @@ -292,6 +292,26 @@ void writeFloat64(double *col, int64_t row, char **pch) *pch = ch; } +void writeFloat64(double *col, int64_t row, char **pch) +{ + double x = col[row]; + char *ch = *pch; + writeFloat64Scalar(x, &ch); + *pch = ch; +} + +void writeComplex(double complex *col, int64_t row, char **pch) +{ + double complex x = col[row]; + char *ch = *pch; + writeFloat64Scalar(creal(x), &ch); + // let writeFloat64 handle the - sign for negative imaginary part + if (cimag(x) >= 0.0) *ch++ = '+'; + writeFloat64Scalar(cimag(x), &ch); + *ch++ = 'i'; + *pch = ch; +} + // DATE/TIME static inline void write_time(int32_t x, char **pch) diff --git a/src/fwrite.h b/src/fwrite.h index fb6dad9373..efcee5e2d8 100644 --- a/src/fwrite.h +++ b/src/fwrite.h @@ -9,12 +9,14 @@ typedef void (*writer_fun_t)(void *, int64_t, char **); +// in the order of writer_fun_t in fwriteR.c void writeBool8(); void writeBool32(); void writeBool32AsString(); void writeInt32(); void writeInt64(); void writeFloat64(); +void writeComplex(); void writeITime(); void writeDateInt32(); void writeDateFloat64(); @@ -33,6 +35,7 @@ typedef enum { // same order as fun[] above WF_Int32, WF_Int64, WF_Float64, + WF_Complex, WF_ITime, WF_DateInt32, WF_DateFloat64, @@ -50,6 +53,7 @@ static const int writerMaxLen[] = { // same order as fun[] and WFs above; max f 11, //&writeInt32 "-2147483647" 20, //&writeInt64 "-9223372036854775807" 29, //&writeFloat64 "-3.141592653589793115998E-123" [max sf 22 consistent with options()$digits] + 60, //&writeComplex "-3.141592653589793115998E-123+2.7182818284590450907956i" [3x writeFloat64,+,i] 32, //&writeITime 16, //&writeDateInt32 16, //&writeDateFloat64 diff --git a/src/fwriteR.c b/src/fwriteR.c index 9979f96ef9..407978ade9 100644 --- a/src/fwriteR.c +++ b/src/fwriteR.c @@ -55,6 +55,7 @@ writer_fun_t funs[] = { &writeInt32, &writeInt64, &writeFloat64, + &writeComplex, &writeITime, &writeDateInt32, &writeDateFloat64, @@ -130,6 +131,8 @@ static int32_t whichWriter(SEXP column) { if (INHERITS(column, char_Date)) return WF_DateFloat64; if (INHERITS(column, char_POSIXct)) return WF_POSIXct; return WF_Float64; + case CPLXSXP: + return WF_Complex; case STRSXP: return WF_String; case VECSXP: From 0a347bed19b57ee8e1d0292b82422b5d72370ccb Mon Sep 17 00:00:00 2001 From: Michael Chirico Date: Thu, 11 Jul 2019 19:16:28 +0800 Subject: [PATCH 02/10] test numbering (missed b/c dont have R.utils in current lib) --- inst/tests/tests.Rraw | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/inst/tests/tests.Rraw b/inst/tests/tests.Rraw index 50ed746af1..ab9ead65ee 100644 --- a/inst/tests/tests.Rraw +++ b/inst/tests/tests.Rraw @@ -9595,11 +9595,11 @@ unlink(f) # complex column support for fwrite, part of #3690 DT = data.table(a = 1:3, z = 0:2 - (2:0)*1i) -test(1658.54, fwrite(DT), output = '0-2i.*2,1-1i.*3,2+0i') -test(1658.55, fwrite(data.table(exp(1) - pi*1i)), output = '2.718[0-9]*-3.141[0-9]*i') +test(1658.55, fwrite(DT), output = '0-2i.*2,1-1i.*3,2+0i') +test(1658.56, fwrite(data.table(exp(1) - pi*1i)), output = '2.718[0-9]*-3.141[0-9]*i') ## formerly 1658.46 DT = data.table(a=1:3, b=list(1:4, c(3.14, 100e10), c(3i,4i,5i))) -test(1658.56, fwrite(DT), output='0+3i|0+4i|0+5i') +test(1658.57, fwrite(DT), output='0+3i|0+4i|0+5i') ## End fwrite tests From 44138e2f788ea24aa9b7ec2ba73cca778bc97e29 Mon Sep 17 00:00:00 2001 From: Michael Chirico Date: Thu, 11 Jul 2019 19:28:42 +0800 Subject: [PATCH 03/10] not sure, try being more explicit --- inst/tests/tests.Rraw | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/inst/tests/tests.Rraw b/inst/tests/tests.Rraw index ab9ead65ee..3989f2e739 100644 --- a/inst/tests/tests.Rraw +++ b/inst/tests/tests.Rraw @@ -9595,7 +9595,7 @@ unlink(f) # complex column support for fwrite, part of #3690 DT = data.table(a = 1:3, z = 0:2 - (2:0)*1i) -test(1658.55, fwrite(DT), output = '0-2i.*2,1-1i.*3,2+0i') +test(1658.55, fwrite(DT), output = 'a,z\n1,0-2i\n2,1-1i\n3,2+0i') test(1658.56, fwrite(data.table(exp(1) - pi*1i)), output = '2.718[0-9]*-3.141[0-9]*i') ## formerly 1658.46 DT = data.table(a=1:3, b=list(1:4, c(3.14, 100e10), c(3i,4i,5i))) From d7103bd5ba7c0ec8effdba69ca7658cc826846ce Mon Sep 17 00:00:00 2001 From: Michael Chirico Date: Thu, 11 Jul 2019 19:49:28 +0800 Subject: [PATCH 04/10] missed handling NA output; added test --- inst/tests/tests.Rraw | 8 +++++--- src/fwrite.c | 12 ++++++++---- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/inst/tests/tests.Rraw b/inst/tests/tests.Rraw index 3989f2e739..f3582ee290 100644 --- a/inst/tests/tests.Rraw +++ b/inst/tests/tests.Rraw @@ -9594,12 +9594,14 @@ if (test_R.utils) test(1658.54, fread(f), setnames(DT,c("V1","V2"))) unlink(f) # complex column support for fwrite, part of #3690 -DT = data.table(a = 1:3, z = 0:2 - (2:0)*1i) -test(1658.55, fwrite(DT), output = 'a,z\n1,0-2i\n2,1-1i\n3,2+0i') -test(1658.56, fwrite(data.table(exp(1) - pi*1i)), output = '2.718[0-9]*-3.141[0-9]*i') +DT = data.table(a=1:3, z=0:2 - (2:0)*1i) +test(1658.55, fwrite(DT), output='a,z\n1,0-2i\n2,1-1i\n3,2+0i') +test(1658.56, fwrite(data.table(exp(1) - pi*1i)), output='2.718[0-9]*-3.141[0-9]*i') ## formerly 1658.46 DT = data.table(a=1:3, b=list(1:4, c(3.14, 100e10), c(3i,4i,5i))) test(1658.57, fwrite(DT), output='0+3i|0+4i|0+5i') +DT[ , b := c(1i, -1-1i, NA)] +test(1658.58, fwrite(DT), output='a,b\n1,0\\+1i\n2,-1-1i\n3,$') ## End fwrite tests diff --git a/src/fwrite.c b/src/fwrite.c index d2ff9574ec..a36fc19d2c 100644 --- a/src/fwrite.c +++ b/src/fwrite.c @@ -303,12 +303,16 @@ void writeFloat64(double *col, int64_t row, char **pch) void writeComplex(double complex *col, int64_t row, char **pch) { double complex x = col[row]; + double im = cimag(x); char *ch = *pch; writeFloat64Scalar(creal(x), &ch); - // let writeFloat64 handle the - sign for negative imaginary part - if (cimag(x) >= 0.0) *ch++ = '+'; - writeFloat64Scalar(cimag(x), &ch); - *ch++ = 'i'; + // writeFloat64Scalar handled NA for the real part + if (!ISNA(im)) { + // let writeFloat64 handle the - sign for negative imaginary part + if (im >= 0.0) *ch++ = '+'; + writeFloat64Scalar(im, &ch); + *ch++ = 'i'; + } *pch = ch; } From b52a1398e96beec569499e2ec6fa9f8cca3de247 Mon Sep 17 00:00:00 2001 From: Michael Chirico Date: Thu, 11 Jul 2019 21:16:10 +0800 Subject: [PATCH 05/10] recover lost coverage, update error message --- inst/tests/tests.Rraw | 2 ++ src/fwriteR.c | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/inst/tests/tests.Rraw b/inst/tests/tests.Rraw index f3582ee290..b9e8657041 100644 --- a/inst/tests/tests.Rraw +++ b/inst/tests/tests.Rraw @@ -9566,6 +9566,8 @@ test(1658.44, file.info(f3)$size, file.info(f1)$size) unlink(c(f1,f2,f3)) DT = data.table(a=1:3, b=list(1:4, c(3.14, 100e10), c("foo", "bar", "baz"))) test(1658.45, fwrite(DT), output=c("a,b","1,1|2|3|4","2,3.14|1e+12","3,foo|bar|baz")) +DT[3,b:=as.raw(0:2)] +test(1658.46, fwrite(DT), error="Row 3 of list column is type 'raw'") DT[3,b:=factor(letters[1:3])] test(1658.47, fwrite(DT), error="Row 3 of list column is type 'factor'") diff --git a/src/fwriteR.c b/src/fwriteR.c index 407978ade9..7fab0fe059 100644 --- a/src/fwriteR.c +++ b/src/fwriteR.c @@ -96,7 +96,7 @@ const int getMaxListItemLen(const SEXP *col, const int64_t n) { int32_t wf = whichWriter(this); if (TYPEOF(this)==VECSXP || wf==INT32_MIN || isFactor(this)) { error("Row %d of list column is type '%s' - not yet implemented. fwrite() can write list columns containing items which are atomic vectors of" \ - " type logical, integer, integer64, double and character.", i+1, isFactor(this) ? "factor" : type2char(TYPEOF(this))); + " type logical, integer, integer64, double, complex and character.", i+1, isFactor(this) ? "factor" : type2char(TYPEOF(this))); } int width = writerMaxLen[wf]; if (width==0) { From 938c4f788d5b4fc5ee1b83da904706d91e669475 Mon Sep 17 00:00:00 2001 From: Michael Chirico Date: Thu, 11 Jul 2019 21:20:02 +0800 Subject: [PATCH 06/10] update manual (dated reference) --- man/fwrite.Rd | 2 -- 1 file changed, 2 deletions(-) diff --git a/man/fwrite.Rd b/man/fwrite.Rd index c8acd80db4..2b7a008d58 100644 --- a/man/fwrite.Rd +++ b/man/fwrite.Rd @@ -3,8 +3,6 @@ \title{Fast CSV writer} \description{ As \code{write.csv} but much faster (e.g. 2 seconds versus 1 minute) and just as flexible. Modern machines almost surely have more than one CPU so \code{fwrite} uses them; on all operating systems including Linux, Mac and Windows. - -This is new functionality as of Nov 2016. We may need to refine argument names and defaults. } \usage{ fwrite(x, file = "", append = FALSE, quote = "auto", From f4a994a8d105e181841c974fe6fe3f612c778a33 Mon Sep 17 00:00:00 2001 From: Michael Chirico Date: Fri, 12 Jul 2019 09:54:27 +0800 Subject: [PATCH 07/10] complete fwriteR.c coverage --- inst/tests/tests.Rraw | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/inst/tests/tests.Rraw b/inst/tests/tests.Rraw index b9e8657041..0aaabcc3b9 100644 --- a/inst/tests/tests.Rraw +++ b/inst/tests/tests.Rraw @@ -9605,6 +9605,11 @@ test(1658.57, fwrite(DT), output='0+3i|0+4i|0+5i') DT[ , b := c(1i, -1-1i, NA)] test(1658.58, fwrite(DT), output='a,b\n1,0\\+1i\n2,-1-1i\n3,$') +# more coverage +test(1658.59, fwrite(data.table(a=list('a')), verbose=TRUE), + output='fields will be quoted if the field contains either sep.*sep2.*list column') +test(1658.60, fwrite(data.table(r=as.raw(0))), error = "'raw' - not yet implemented") + ## End fwrite tests # tests for #679, inrange(), FR #707 From 5e1f045a84664bbb3ef7d05171f70790dd82ff63 Mon Sep 17 00:00:00 2001 From: Michael Chirico Date: Fri, 12 Jul 2019 10:02:51 +0800 Subject: [PATCH 08/10] nocov unused writeBool8 --- src/fwrite.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/fwrite.c b/src/fwrite.c index a36fc19d2c..cb4e4225a9 100644 --- a/src/fwrite.c +++ b/src/fwrite.c @@ -52,6 +52,7 @@ inline void write_chars(const char *x, char **pch) *pch = ch; } +// #nocov start void writeBool8(int8_t *col, int64_t row, char **pch) { int8_t x = col[row]; @@ -59,6 +60,7 @@ void writeBool8(int8_t *col, int64_t row, char **pch) *ch++ = '0'+(x==1); *pch = ch-(x==INT8_MIN); // if NA then step back, to save a branch } +// #nocov end void writeBool32(int32_t *col, int64_t row, char **pch) { From 74db2ce90052db43642533bbfb537bafce9d4352 Mon Sep 17 00:00:00 2001 From: Michael Chirico Date: Sun, 14 Jul 2019 04:30:41 +0800 Subject: [PATCH 09/10] move to Rcomplex API --- src/fwrite.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/fwrite.c b/src/fwrite.c index cb4e4225a9..af7df52c34 100644 --- a/src/fwrite.c +++ b/src/fwrite.c @@ -7,7 +7,6 @@ #include // isfinite, isnan #include // abs #include // strlen, strerror -#include #ifdef WIN32 #include @@ -302,12 +301,12 @@ void writeFloat64(double *col, int64_t row, char **pch) *pch = ch; } -void writeComplex(double complex *col, int64_t row, char **pch) +void writeComplex(Rcomplex *col, int64_t row, char **pch) { - double complex x = col[row]; - double im = cimag(x); + Rcomplex x = col[row]; + double im = x.i; char *ch = *pch; - writeFloat64Scalar(creal(x), &ch); + writeFloat64Scalar(x.r, &ch); // writeFloat64Scalar handled NA for the real part if (!ISNA(im)) { // let writeFloat64 handle the - sign for negative imaginary part From e12bec5c52ac0b459fbdce589c791c715acc2043 Mon Sep 17 00:00:00 2001 From: mattdowle Date: Wed, 17 Jul 2019 15:20:52 -0700 Subject: [PATCH 10/10] simplified so writeComplex can just be added with no extra hop/change to writeFloat64, and no nocov for writeBool since that will be covered by type raw one day --- NEWS.md | 2 +- src/fwrite.c | 24 ++++++------------------ 2 files changed, 7 insertions(+), 19 deletions(-) diff --git a/NEWS.md b/NEWS.md index 8f98c39212..f86bb3a7a4 100644 --- a/NEWS.md +++ b/NEWS.md @@ -36,7 +36,7 @@ * Gains `bom` argument to add a *byte order mark* (BOM) at the beginning of the file to signal that the file is encoded in UTF-8, [#3488](https://github.com/Rdatatable/data.table/issues/3488). Thanks to Stefan Fleck for requesting and Philippe Chataignon for implementing. - * Supports writing complex-valued columns, part of [#3690](https://github.com/Rdatatable/data.table/issues/3690). + * Now supports type `complex`, [#3690](https://github.com/Rdatatable/data.table/issues/3690). 4. Assigning to one item of a list column no longer requires the RHS to be wrapped with `list` or `.()`, [#950](https://github.com/Rdatatable/data.table/issues/950). diff --git a/src/fwrite.c b/src/fwrite.c index af7df52c34..512b789780 100644 --- a/src/fwrite.c +++ b/src/fwrite.c @@ -51,7 +51,6 @@ inline void write_chars(const char *x, char **pch) *pch = ch; } -// #nocov start void writeBool8(int8_t *col, int64_t row, char **pch) { int8_t x = col[row]; @@ -59,7 +58,6 @@ void writeBool8(int8_t *col, int64_t row, char **pch) *ch++ = '0'+(x==1); *pch = ch-(x==INT8_MIN); // if NA then step back, to save a branch } -// #nocov end void writeBool32(int32_t *col, int64_t row, char **pch) { @@ -170,7 +168,7 @@ void genLookups() { } */ -void writeFloat64Scalar(double x, char **pch) +void writeFloat64(double *col, int64_t row, char **pch) { // hand-rolled / specialized for speed // *pch is safely the output destination with enough space (ensured via calculating maxLineLen up front) @@ -180,6 +178,7 @@ void writeFloat64Scalar(double x, char **pch) // ii) no C libary calls such as sprintf() where the fmt string has to be interpretted over and over // iii) no need to return variables or flags. Just writes. // iv) shorter, easier to read and reason with in one self contained place. + double x = col[row]; char *ch = *pch; if (!isfinite(x)) { if (isnan(x)) { @@ -293,25 +292,14 @@ void writeFloat64Scalar(double x, char **pch) *pch = ch; } -void writeFloat64(double *col, int64_t row, char **pch) -{ - double x = col[row]; - char *ch = *pch; - writeFloat64Scalar(x, &ch); - *pch = ch; -} - void writeComplex(Rcomplex *col, int64_t row, char **pch) { Rcomplex x = col[row]; - double im = x.i; char *ch = *pch; - writeFloat64Scalar(x.r, &ch); - // writeFloat64Scalar handled NA for the real part - if (!ISNA(im)) { - // let writeFloat64 handle the - sign for negative imaginary part - if (im >= 0.0) *ch++ = '+'; - writeFloat64Scalar(im, &ch); + writeFloat64(&x.r, 0, &ch); + if (!ISNAN(x.i)) { + if (x.i >= 0.0) *ch++ = '+'; // else writeFloat64 writes the - sign + writeFloat64(&x.i, 0, &ch); *ch++ = 'i'; } *pch = ch;