Skip to content

Commit

Permalink
Added max_recs param to write() + fixed compiler debug warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
hosseinmoein committed Oct 14, 2022
1 parent 4d55494 commit 606e46b
Show file tree
Hide file tree
Showing 13 changed files with 244 additions and 145 deletions.
13 changes: 9 additions & 4 deletions docs/HTML/write.html
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@
write(S &o,
io_format iof = io_format::csv,
std::streamsize precision = 12,
bool columns_only = false) const; </font>
bool columns_only = false,
long max_recs = std::numeric_limits<long>::max()) const; </font>
</B></PRE>
</td>
<td>
Expand Down Expand Up @@ -113,6 +114,7 @@
<B>iof</B>: Specifies the I/O format. The default is CSV
<B>precision</B>: Specifies the precision for floating point numbers
<B>columns_only</B>: If true, the index columns is not written into the stream
<B>max_recs</B>: Max number of rows to write. If it is positive, it will write max_recs from the beginning of DataFrame. If it is negative, it will write max_recs from the end of DataFrame
</PRE>
</td>
</tr>
Expand All @@ -125,7 +127,8 @@
write(const char *file_name,
io_format iof = io_format::csv,
std::streamsize precision = 12,
bool columns_only = false) const; </font>
bool columns_only = false,
long max_recs = std::numeric_limits<long>::max()) const; </font>
</B></PRE>
</td>
<td>
Expand All @@ -143,7 +146,8 @@
write_async(S &amp;o,
io_format iof = io_format::csv,
std::streamsize precision = 12,
bool columns_only = false) const; </font>
bool columns_only = false,
long max_recs = std::numeric_limits<long>::max()) const; </font>
</B></PRE>
</td>
<td>
Expand All @@ -161,7 +165,8 @@
write_async(const char *file_name,
io_format iof = io_format::csv,
std::streamsize precision = 12,
bool columns_only = false) const; </font>
bool columns_only = false,
long max_recs = std::numeric_limits<long>::max()) const; </font>
</B></PRE>
</td>
<td>
Expand Down
34 changes: 30 additions & 4 deletions include/DataFrame/DataFrame.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include <functional>
#include <future>
#include <ios>
#include <limits>
#include <string>
#include <tuple>
#include <type_traits>
Expand Down Expand Up @@ -2209,6 +2210,23 @@ class DataFrame : public ThreadGranularity {
[[nodiscard]] IndexVecType &
get_index();

/*
// It prints to stdout n rows of all columns either from the beginning
// or end of the DataFrame. Print will be in csv2 format.
// If n is positive, n rows from the beginning of DataFrame are printed.
// If negative, n rows from the end of DataFrame are printed.
//
// Ts:
// List all the types of all data columns. A type should be specified in
// the list only once.
// n:
// Number of columns to print. It could be positive or negative
//
template<typename ... Ts>
void
head(long n) const;
*/

// It creates and returns a new DataFrame which has the col_to_be_index
// column as the index. If old_index_name is not null, it will be loaded
// as a regular column in the result under the name old_index_name.
Expand Down Expand Up @@ -3450,20 +3468,26 @@ class DataFrame : public ThreadGranularity {
// Specifies the precision for floating point numbers
// columns_only:
// If true, it won't write the index column
// max_recs:
// Max number of rows to write. If it is positive, it will write max_recs
// from the beginning of DataFrame. If it is negative, it will write
// max_recs from the end of DataFrame
//
template<typename S, typename ... Ts>
bool
write(S &o,
io_format iof = io_format::csv,
std::streamsize precision = 12,
bool columns_only = false) const;
bool columns_only = false,
long max_recs = std::numeric_limits<long>::max()) const;

template<typename ... Ts>
bool
write(const char *file_name,
io_format iof = io_format::csv,
std::streamsize precision = 12,
bool columns_only = false) const;
bool columns_only = false,
long max_recs = std::numeric_limits<long>::max()) const;

// Same as write() above, but executed asynchronously
//
Expand All @@ -3472,14 +3496,16 @@ class DataFrame : public ThreadGranularity {
write_async(S &o,
io_format iof = io_format::csv,
std::streamsize precision = 12,
bool columns_only = false) const;
bool columns_only = false,
long max_recs = std::numeric_limits<long>::max()) const;

template<typename ... Ts>
[[nodiscard]] std::future<bool>
write_async(const char *file_name,
io_format iof = io_format::csv,
std::streamsize precision = 12,
bool columns_only = false) const;
bool columns_only = false,
long max_recs = std::numeric_limits<long>::max()) const;

// This is a convenient function (simple implementation) to convert a
// DataFrame into a string that could be restored later by calling
Expand Down
10 changes: 5 additions & 5 deletions include/DataFrame/DataFrameFinancialVisitors.h
Original file line number Diff line number Diff line change
Expand Up @@ -949,13 +949,13 @@ struct SharpeRatioVisitor {
if (vec_s != b_s || vec_s < 3) {
char err[512];

sprintf (err,
snprintf (err, sizeof(err) - 1,
#ifdef _MSC_VER
"SharpeRatioVisitor: Size of asset = %zu and "
"benchmark = %zu time-series are not feasible.",
"SharpeRatioVisitor: Size of asset = %zu and "
"benchmark = %zu time-series are not feasible.",
#else
"SharpeRatioVisitor: Size of asset = %lu and "
"benchmark = %lu time-series are not feasible.",
"SharpeRatioVisitor: Size of asset = %lu and "
"benchmark = %lu time-series are not feasible.",
#endif // _MSC_VER
vec_s, b_s);
throw NotFeasible (err);
Expand Down
8 changes: 4 additions & 4 deletions include/DataFrame/DataFrameStatsVisitors.h
Original file line number Diff line number Diff line change
Expand Up @@ -2127,12 +2127,12 @@ struct QuantileVisitor {
if (qt_ < 0.0 || qt_ > 1.0 || col_s == 0) {
char buffer [512];

sprintf (buffer,
"QuantileVisitor{}: unable to do quantile: "
snprintf (buffer, sizeof(buffer) - 1,
"QuantileVisitor{}: unable to do quantile: "
#ifdef _MSC_VER
"qt: %f, Column Len: %zu",
"qt: %f, Column Len: %zu",
#else
"qt: %f, Column Len: %lu",
"qt: %f, Column Len: %lu",
#endif // _MSC_VER
qt_, col_s);
throw NotFeasible(buffer);
Expand Down
10 changes: 5 additions & 5 deletions include/DataFrame/Internals/DataFrame.tcc
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,9 @@ DataFrame<I, H>::shuffle(const std::vector<const char *> &col_names,
if (citer == column_tb_.end()) {
char buffer [512];

sprintf(buffer,
"DataFrame::shuffle(): ERROR: Cannot find column '%s'",
name_citer);
snprintf(buffer, sizeof(buffer) - 1,
"DataFrame::shuffle(): ERROR: Cannot find column '%s'",
name_citer);
throw ColNotFound(buffer);
}

Expand Down Expand Up @@ -474,8 +474,8 @@ fill_missing(const std::vector<const char *> &col_names,
else if (fp == fill_policy::linear_extrapolate) {
char buffer [512];

sprintf (
buffer,
snprintf (
buffer, sizeof(buffer) - 1,
"DataFrame::fill_missing(): fill_policy %d is not implemented",
static_cast<int>(fp));
throw NotImplemented(buffer);
Expand Down
27 changes: 20 additions & 7 deletions include/DataFrame/Internals/DataFrame_functors.h
Original file line number Diff line number Diff line change
Expand Up @@ -168,11 +168,16 @@ struct add_col_functor_ : DataVec::template visitor_base<Ts ...> {
template<typename ... Ts>
struct print_csv_functor_ : DataVec::template visitor_base<Ts ...> {

inline print_csv_functor_ (const char *n, std::ostream &o)
: name(n), os(o) { }
inline print_csv_functor_ (const char *n,
std::ostream &o,
long sr,
long er)
: name(n), os(o), start_row(sr), end_row(er) { }

const char *name;
std::ostream &os;
const long start_row;
const long end_row;

template<typename T>
void operator() (const T &vec);
Expand All @@ -184,11 +189,17 @@ struct print_csv_functor_ : DataVec::template visitor_base<Ts ...> {
template<typename ... Ts>
struct print_json_functor_ : DataVec::template visitor_base<Ts ...> {

inline print_json_functor_ (const char *n, bool npc, std::ostream &o)
: name(n), need_pre_comma(npc), os(o) { }
inline print_json_functor_ (const char *n,
bool npc,
std::ostream &o,
long sr,
long er)
: name(n), need_pre_comma(npc), start_row(sr), end_row(er), os(o) { }

const char *name;
const bool need_pre_comma;
const long start_row;
const long end_row;
std::ostream &os;

template<typename T>
Expand All @@ -200,11 +211,12 @@ struct print_json_functor_ : DataVec::template visitor_base<Ts ...> {
template<typename S, typename ... Ts>
struct print_csv2_header_functor_ : DataVec::template visitor_base<Ts ...> {

inline print_csv2_header_functor_ (const char *n, S &o)
: name(n), os(o) { }
inline print_csv2_header_functor_ (const char *n, S &o, long cs)
: name(n), os(o), col_size(cs) { }

const char *name;
S &os;
const long col_size;

template<typename T>
void operator() (const T &vec);
Expand All @@ -215,7 +227,8 @@ struct print_csv2_header_functor_ : DataVec::template visitor_base<Ts ...> {
template<typename S, typename ... Ts>
struct print_csv2_data_functor_ : DataVec::template visitor_base<Ts ...> {

inline print_csv2_data_functor_ (std::size_t i, S &o) : index(i), os(o) { }
inline print_csv2_data_functor_ (std::size_t i, S &o)
: index(i), os(o) { }

const std::size_t index;
S &os;
Expand Down
Loading

0 comments on commit 606e46b

Please sign in to comment.