📄 tseries.head.hpp
字号:
#ifndef TSERIES_HPP#define TSERIES_HPP#include <string>#include <set>#include <map>#include <vector>#include <cmath>#include <ctime>#include <iostream>#include <fstream>#include <numeric>#include "mask.hpp"#include "rangeSpecifier.hpp"#include "array.hpp"// from tseries.io.cpp#include <sstream>#include <iomanip>#include "util.hpp"// clean up later//#define DGB_TSERIESusing namespace std;template<class DateT>class tseries { protected: DateT *dates; double *data; unsigned int rows; unsigned int cols; vector<string> colnames; bool local_data; public: ~tseries() { /* only destroy these arrays if the object owns its data if this constructor is used: tseries(double *data, double *dates, const int nrow, const int ncol) then the data could be from another application (R for instance) so we can't free it when the object gets whacked */#ifdef DGB_TSERIES cout << "destroy: " << this << endl; cout << "dates: " << dates << endl; cout << "data: " << data << endl; cout << "rows: " << rows << endl; cout << "cols: " << cols << endl;#endif if(local_data) {#ifdef DGB_TSERIES cout << "killing data" << cols << endl;#endif delete []dates; delete []data; } } tseries(double *external_data, DateT *external_dates, const unsigned int& nrows, const unsigned int& ncols, const bool& own_data=true) { if(external_data==NULL || external_dates==NULL || nrows==0 || ncols==0) { cerr << "ERROR: tseries(double *external_data, DateT *external_dates, const unsigned int nrows" ", const unsigned int ncols, bool own_data)" << endl; cerr << "problem with data used for tseries creation:" << endl; cerr << "external_data: " << external_data << endl;; cerr << "external_dates: " << external_dates << endl;; cerr << "nrows: " << nrows << endl; cerr << "ncols: " << ncols << endl; // set to NULL ts and return data = NULL; dates = NULL; cols = 0; rows = 0; local_data = true; return; } data = external_data; dates = external_dates; cols = ncols; rows = nrows; local_data = own_data; } tseries(double *external_data, DateT *external_dates, const unsigned int& nrows, const unsigned int& ncols, vector<string> colnms, const bool& own_data=true) { if(external_data==NULL || external_dates==NULL || nrows==0 || ncols==0) { cerr << "tseries(double *external_data," "DateT *external_dates," "const unsigned int nrows," "const unsigned int ncols," "vector<string> colnms," "bool own_data)" << endl; cerr << "problem with data used for tseries creation:" << endl; cerr << "external_data: " << external_data << endl;; cerr << "external_dates: " << external_dates << endl;; cerr << "nrows: " << nrows << endl; cerr << "ncols: " << ncols << endl; // set to NULL ts and return data = NULL; dates = NULL; cols = 0; rows = 0; local_data = true; return; } // only set if these match match // else colnames are null if(colnms.size()==cols) { colnames = colnms; } data = external_data; dates = external_dates; cols = ncols; rows = nrows; local_data = own_data; } // construct an empty tseries given dimentsions tseries(const unsigned int& nrow, const unsigned int& ncol) {#ifdef DGB_TSERIES cout << "tseries(const unsigned int nrow, const unsigned int ncol)" << this << endl; fakeDates();#endif init(nrow,ncol); } /* tseries(quick_TS x) { // new number of rows in ans unsigned int s = x.size(); // alloc memory for answer data = new double[s]; dates = new DateT[s]; // if we did not get the memory, then clear any we did get, set ptrs to NULL, set col and rows to 0, and return if(dates==NULL || data==NULL) { cerr << "tseries(quick_TS x): could not allocate memory." << endl; delete []dates; delete []data; dates=NULL; data=NULL; rows = 0; cols = 0; return; } // new dims rows = s; cols = 1; local_data = true; // fill ans w/ data unsigned int i = 0; for(quick_TSIter iter = x.begin(); iter != x.end(); iter++, i++) { dates[i] = (*iter).first; data[i] = (*iter).second; } } */ tseries(const unsigned int& nrow, const unsigned int& ncol, const double& fillVal) {#ifdef DGB_TSERIES cout << "tseries(const unsigned int nrow, const unsigned int ncol, const double fillVal)" << this << endl;#endif init(nrow,ncol); initConst(fillVal); fakeDates(); } tseries() { data = NULL; dates = NULL; rows = 0; cols = 0; local_data = true;#ifdef DGB_TSERIES cout << "tseries()" << this << endl; cout << "dates: " << dates << endl; cout << "data: " << data << endl;#endif } tseries(const tseries& t) {#ifdef DGB_TSERIES cout << "calling: tseries(const tseries &t)" << this << endl;#endif // alloc new storage data = new double[t.rows*t.cols]; dates = new DateT[t.rows]; // if we didn't get the memory, then clear what we did get, set ptrs to NULL, and dims to null, and return if(data==NULL||dates==NULL) { cerr << "tseries(const tseries &t): memory allocation error." << endl; delete []data; delete []dates; data = NULL; dates = NULL; rows = 0; cols = 0; return; }#ifdef DGB_TSERIES cout << "dates: " << dates << endl; cout << "t.dates: " << t.dates << endl; cout << "data: " << data << endl; cout << "t.data: " << t.data << endl;#endif // copy old data into new storage memcpy(dates,t.dates,sizeof(DateT)*t.rows); memcpy(data,t.data,sizeof(double)*t.rows*t.cols); // set new dims rows = t.rows; cols = t.cols; // set new colnames colnames = t.colnames; local_data = true; } bool valid() { if(rows!=0 && cols!=0 && data!=NULL && dates != NULL ) { return true; } return false; } bool isnull() { if(rows==0 || cols==0 || data==NULL || dates==NULL) { return true; } return false; } void init(const unsigned int& nrows, const unsigned int& ncols) { rows = nrows; cols = ncols; data = new double[rows*cols]; dates = new DateT[rows]; if(data==NULL || dates==NULL) { cerr << "void init(const unsigned int nrows, const unsigned int ncols) " "memory allocation error." << endl; } local_data = true; // this is here to make valgrind happy // take this out to run faster#ifdef VALGRIND_HAPPY for(unsigned int i=0; i < rows; i++) dates[i] = 0; for(unsigned int i=0; i < rows*cols; i++) data[i] = NAN;#endif } void fakeDates() { for(unsigned int i=0; i < rows; i++) { dates[i] = static_cast<DateT>(i*86400-19*3600); // offset for EST } } void fakeData() { for(unsigned int i=0; i < (rows*cols); i++) { data[i] = static_cast<double>(i); } } /* initialize all rows and cols of a tseries to a constant value */ void initConst(const double& fillVal) { for(unsigned int i=0; i < (rows*cols); i++) { data[i] = fillVal; } } void randomize() { /* initialize random generator */ srand ( time(NULL) ); for(unsigned int i=0; i < cols*rows; i++) { data[i] = rand(); } } vector<string> getColNames() const { return colnames; } /* print a tseries on the console */ void print() const { if(rows==0||cols==0||data==NULL||dates==NULL) { cout << "(null tseries)" << endl; return; } unsigned int cns = colnames.size(); // print colnames if(cns) { for(unsigned int i = 0; i < cns; i++) { cout << colnames[i] << " "; } cout << endl; } for(unsigned int row=0; row < rows; row++) { printDate(dates[row],"%Y-%m-%d %T"); cout << " "; for(unsigned int col=0; col < cols; col++) { cout << data[row+rows*col] << " "; } cout << endl; } } double *getCol(const int& col) const { if(col >= 0) { return getCol(static_cast<unsigned int>(col) ); } cerr << "ERROR: getCol(const int col)" << endl; cerr << "col is less than zero: " << col << endl; return NULL; } double *getCol(const unsigned int& col) const { // check that col is a sensible number 0..(N-1) if(col < ncol()) { return &data[rows*col]; } cerr << "ERROR: double *getCol(const int col): " "tried to reference column outside of range: " << col <<", nc: " << cols << endl; return NULL; } double *getCol(const string& s) const { int col = match_string(s,colnames); return getCol(col); } double *getCol(const char *colName) const { string s(colName); return getCol(s); } tseries getColTS(const string& s) const { int matchcol = match_string(s,colnames); // string not found /// return empty tseries if(matchcol == -1) { cerr << "ERROR: tseries getColTS(const string s)" << endl; cerr << "cannot find column: " << s << endl; return tseries(); } // alloc a 1 col tseries w/ the same number of rows // as this one tseries ans(rows,1); memcpy(ans.getData(),getCol(matchcol),sizeof(double)*rows); memcpy(ans.getDates(),dates,sizeof(DateT)*rows); // set colname of ans ans.colnames.push_back(s); return ans; } // returns a double pointer instead of the SEXP double* getData() const { // we want the address of the first double return data; } // returns a pointer instead of an SEXP DateT* getDates() const { return dates; } /* return an element from a tseries this is zero indexed*/ double getElement(const unsigned int& row, const unsigned int& col) const { if(col < cols && col >= 0 && row < rows && row >=0) { return data[row+rows*col]; } else { cerr << "ERROR: getElement(const unsigned int row, const unsigned int col)" << endl; cerr << "tried to access element outside of data range: r" << row << " " << "c" << col << endl; return NAN; } } /* set an element of a tseries object 0 indexed: valid rows: 0..(rows-1) valid cols: 0..(cols-1) */ void setElement(const unsigned int& row, const unsigned int& col, const double& value) { if(col < cols && col >= 0 && row < rows && row >=0) { data[row+rows*col]=value; } else { cerr << "ERROR: tsetElement(const unsigned int row, const unsigned int col, const double value)" << endl; cerr << "tried to set element outside of data range: r" << row << " " << "c" << col << endl; } } void setColNames(const vector<string>& new_colnames) { if(new_colnames.size()==0) { return; } if(new_colnames.size() == cols) { colnames = new_colnames; } else { cerr << "WARNING: setColNames(vector<string> new_colnames) " << endl; cerr << "size of colnames passed in is not equal to number of cols in tseries." << endl; } } void setColNames(const char* cname) { // check for NULL tseries if(cols==0) { cerr << "ERROR: void setColNames(const char* cname)" << endl; cerr << "can't set colname of NULL tseries." << endl; return; } // not ok of colnames is not == 1 if(cols!=1) { cerr << "ERROR: void setColNames(const char* cname)" << endl; cerr << "this tseries has more than one column." << endl; return; } // ok if colnames are empty if(colnames.size()==0) { colnames.push_back(cname); return; } // set colname to new value colnames[0] = cname; } // returns the number of cols of the tseries unsigned int ncol() const { return cols; } // returns the number of rows in the tseries unsigned int nrow() const {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -