rcpp_table.h
Go to the documentation of this file.
#ifndef RCPP_TABLE_H
#define RCPP_TABLE_H
#include <Rcpp.h>
#include <functional>
#include <tuple>
#include <set>
using namespace std;
using namespace Rcpp;
using std::get;
// TODO: re-write the DataFrameView class and adapt the Table class for it.
class Interpolate {
public:
vector<double> x, y, slope;
Interpolate() {
}
Interpolate(vector<double> inx, vector<double> iny) :
x(inx), y(iny) {
// calculate the slope between points
for (size_t i=0; i<x.size()-1; i++) {
slope.push_back((y[i+1]-y[i]) / (x[i+1]-x[i]));
}
}
double approx(double xfind) {
int i;
if (xfind<=x[0]) return y[0];
else if (xfind>=*(--x.end())) return *(--y.end());
else {
i = lower_bound(x.begin(), x.end(), xfind) - x.begin();
return y[i]+slope[i]*(xfind-x[i]);
}
}
double operator()(double xfind) {
if (xfind<=x[0]) return y[0];
int i = lower_bound(x.begin(), x.end(), xfind) - x.begin();
return y[--i];
}
};
class NumericInterpolate {
public:
NumericVector x, y, slope;
int n;
NumericInterpolate() : x(0), y(0), slope(0), n(0) {
}
NumericInterpolate(DataFrame df, int i0=0, int i1=1) {
// calculate the slope between points
x = df(i0);
y = df(i1);
n = x.size();
prepare();
}
void prepare() {
for (int i=0; i<n-1; i++) {
slope.push_back((y[i+1]-y[i]) / (x[i+1]-x[i]));
}
}
void push_back(pair<double,double> xy) {
x.push_back(xy.first);
y.push_back(xy.second);
n++;
}
double approx(double xfind) {
int i;
if (xfind<=x[0]) return y[0];
else if (xfind>=x[n-1]) return y[n-1]+slope[n-2]*(xfind-x[n-1]); // linear
else {
i = lower_bound(x.begin(), x.end(), xfind) - 1 - x.begin();
return y[i]+slope[i]*(xfind-x[i]);
}
}
double operator()(double xfind) {
if (xfind<=x[0]) return y[0];
int i = lower_bound(x.begin(), x.end(), xfind) - x.begin();
return y[--i];
}
double invert(double yfind) { // assumes that the function is increasing
int i;
if (yfind<=y[0]) return x[0];
else if (yfind>=y[n-1]) return x[n-1]+(yfind-y[n-1])/slope[n-2];
else {
i = lower_bound(y.begin(), y.end(), yfind) - 1 - y.begin();
return x[i]+(yfind-y[i])/slope[i];
}
}
double invert(double yfind, double xentry) { // assumes that the function is increasing
double yentry = approx(xentry);
return invert(yfind - yentry);
}
double invert_decreasing(double yfind) { // assumes that the function is decreasing
int i;
if (yfind>=y[0]) return x[0];
else if (yfind<y[n-1]) return x[n-1]+(yfind-y[n-1])/slope[n-2];
else {
i = lower_bound(y.begin(), y.end(), yfind, greater<double>()) - 1 - y.begin();
return x[i]+(yfind-y[i])/slope[i];
}
}
};
template<class T>
T set_lower_bound(set<T,greater<T> > aset, T value) {
return value<*aset.rbegin() ? *aset.rbegin() : *aset.lower_bound(value);
}
template <class T>
class DataFrameSelect {
public:
Vector<Rcpp::traits::r_sexptype_traits<T>::rtype> data;
DataFrameSelect(const DataFrame & df, int i = 0) {
data = df(i); // copy
}
DataFrameSelect(const DataFrame & df, string name) {
data = df[name];
}
T operator[](int i) {
return(data[i]);
}
int size() {
return data.size();
}
};
struct null_type {};
template<class I0 = null_type, class I1 = null_type, class I2 = null_type,
class I3 = null_type, class I4 = null_type, class Outcome = null_type>
class Table {
public:
Table() {}
typedef std::tuple<I0,I1,I2,I3,I4> key_type;
typedef Outcome mapped_type;
typedef std::tuple<
set<I0, greater<I0> >,
set<I1, greater<I1> >,
set<I2, greater<I2> >,
set<I3, greater<I3> >,
set<I4, greater<I4> >
> Axis;
void insert(I0 key0, I1 key1, I2 key2, I3 key3, I4 key4, Outcome outcome) {
key_type key = key_type(key0,key1,key2,key3,key4);
get<0>(axis).insert(key0);
get<1>(axis).insert(key1);
get<2>(axis).insert(key2);
get<3>(axis).insert(key3);
get<4>(axis).insert(key4);
data[key] = outcome;
}
virtual Outcome operator()(I0 i0, I1 i1, I2 i2, I3 i3, I4 i4) {
return data[key_type(set_lower_bound(get<0>(axis), i0),
set_lower_bound(get<1>(axis), i1),
set_lower_bound(get<2>(axis), i2),
set_lower_bound(get<3>(axis), i3),
set_lower_bound(get<4>(axis), i4))];
}
Table(const DataFrame & df, string s0, string s1, string s2, string s3, string s4, string s5) {
DataFrameSelect<I0> df0(df,s0);
DataFrameSelect<I1> df1(df,s1);
DataFrameSelect<I2> df2(df,s2);
DataFrameSelect<I3> df3(df,s3);
DataFrameSelect<I4> df4(df,s4);
DataFrameSelect<Outcome> df5(df,s5);
for (int i=0; i<df0.size(); i++) {
insert(df0[i],df1[i],df2[i],df3[i],df4[i], df5[i]);
}
}
private:
Axis axis;
map<key_type,mapped_type> data;
};
template<class I0, class I1, class I2, class I3, class Outcome>
class Table<I0,I1,I2,I3,Outcome> {
public:
typedef std::tuple<I0,I1,I2,I3> key_type;
typedef Outcome mapped_type;
typedef std::tuple<
set<I0, greater<I0> >,
set<I1, greater<I1> >,
set<I2, greater<I2> >,
set<I3, greater<I3> >
> Axis;
Table() {}
void insert(I0 key0, I1 key1, I2 key2, I3 key3, mapped_type outcome) {
key_type key = key_type(key0,key1,key2,key3);
get<0>(axis).insert(key0);
get<1>(axis).insert(key1);
get<2>(axis).insert(key2);
get<3>(axis).insert(key3);
data[key] = outcome;
}
virtual Outcome operator()(I0 i0, I1 i1, I2 i2, I3 i3) {
return data[key_type(set_lower_bound(get<0>(axis), i0),
set_lower_bound(get<1>(axis), i1),
set_lower_bound(get<2>(axis), i2),
set_lower_bound(get<3>(axis), i3))];
}
Table(const DataFrame & df, string s0, string s1, string s2, string s3, string s4) {
DataFrameSelect<I0> df0(df,s0);
DataFrameSelect<I1> df1(df,s1);
DataFrameSelect<I2> df2(df,s2);
DataFrameSelect<I3> df3(df,s3);
DataFrameSelect<Outcome> df4(df,s4);
for (int i=0; i<df0.size(); i++) {
insert(df0[i],df1[i],df2[i],df3[i], df4[i]);
}
}
private:
Axis axis;
map<key_type,mapped_type> data;
};
template<class I0, class I1, class I2, class Outcome>
class Table<I0,I1,I2,Outcome> {
public:
typedef std::tuple<I0,I1,I2> key_type;
typedef Outcome mapped_type;
typedef std::tuple<
set<I0, greater<I0> >,
set<I1, greater<I1> >,
set<I2, greater<I2> >
> Axis;
Table() {}
void insert(I0 key0, I1 key1, I2 key2, mapped_type outcome) {
key_type key = key_type(key0,key1,key2);
get<0>(axis).insert(key0);
get<1>(axis).insert(key1);
get<2>(axis).insert(key2);
data[key] = outcome;
}
virtual Outcome operator()(I0 i0, I1 i1, I2 i2) {
return data[key_type(set_lower_bound(get<0>(axis), i0),
set_lower_bound(get<1>(axis), i1),
set_lower_bound(get<2>(axis), i2))];
}
Table(const DataFrame & df, string s0, string s1, string s2, string s3) {
DataFrameSelect<I0> df0(df,s0);
DataFrameSelect<I1> df1(df,s1);
DataFrameSelect<I2> df2(df,s2);
DataFrameSelect<Outcome> df3(df,s3);
for (int i=0; i<df0.size(); i++) {
insert(df0[i],df1[i],df2[i], df3[i]);
}
}
private:
Axis axis;
map<key_type,mapped_type> data;
};
template<class I0, class I1, class Outcome>
class Table<I0,I1,Outcome> {
public:
typedef std::tuple<I0,I1> key_type;
typedef Outcome mapped_type;
typedef std::tuple<
set<I0, greater<I0> >,
set<I1, greater<I1> >
> Axis;
Table() {}
void insert(I0 key0, I1 key1, mapped_type outcome) {
key_type key = key_type(key0,key1);
get<0>(axis).insert(key0);
get<1>(axis).insert(key1);
data[key] = outcome;
}
virtual Outcome operator()(I0 i0, I1 i1) {
return data[key_type(set_lower_bound(get<0>(axis), i0),
set_lower_bound(get<1>(axis), i1))];
}
Table(const DataFrame & df, string s0, string s1, string s2) {
DataFrameSelect<I0> df0(df,s0);
DataFrameSelect<I1> df1(df,s1);
DataFrameSelect<Outcome> df2(df,s2);
for (int i=0; i<df0.size(); i++) {
insert(df0[i],df1[i], df2[i]);
}
}
private:
Axis axis;
map<key_type,mapped_type> data;
};
template<class key_type, class mapped_type>
class Table<key_type,mapped_type> {
public:
typedef set<key_type, greater<key_type> > Axis;
Table() {}
void insert(const key_type& key, const mapped_type& outcome) {
axis.insert(key);
data[key] = outcome;
}
virtual mapped_type operator()(key_type key) {
return data[set_lower_bound(axis,key)];
}
Table(const DataFrame & df, string s0, string s1) {
DataFrameSelect<key_type> df0(df,s0);
DataFrameSelect<mapped_type> df1(df,s1);
for (int i=0; i<df0.size(); i++) {
insert(key_type(df0[i]), mapped_type(df1[i]));
}
}
private:
Axis axis;
map<key_type,mapped_type> data;
};
#endif /* RCPP_TABLE_H */
DataFrameSelect::data
Vector< Rcpp::traits::r_sexptype_traits< T >::rtype > data
Definition: rcpp_table.h:122
Table::key_type
std::tuple< I0, I1, I2, I3, I4 > key_type
Definition: rcpp_table.h:150
Table< I0, I1, I2, Outcome >::Table
Table()
Definition: rcpp_table.h:242
Table< I0, I1, Outcome >::axis
Axis axis
Definition: rcpp_table.h:298
Table
Definition: rcpp_table.h:147
Table< I0, I1, I2, I3, Outcome >::axis
Axis axis
Definition: rcpp_table.h:228
Interpolate::Interpolate
Interpolate(vector< double > inx, vector< double > iny)
Definition: rcpp_table.h:21
Table< key_type, mapped_type >::operator()
virtual mapped_type operator()(key_type key)
Definition: rcpp_table.h:311
Table< I0, I1, Outcome >::key_type
std::tuple< I0, I1 > key_type
Definition: rcpp_table.h:272
Table< I0, I1, I2, Outcome >::Table
Table(const DataFrame &df, string s0, string s1, string s2, string s3)
Definition: rcpp_table.h:255
Table< I0, I1, Outcome >::insert
void insert(I0 key0, I1 key1, mapped_type outcome)
Definition: rcpp_table.h:279
NumericInterpolate::invert_decreasing
double invert_decreasing(double yfind)
Definition: rcpp_table.h:102
Table< I0, I1, Outcome >::Table
Table(const DataFrame &df, string s0, string s1, string s2)
Definition: rcpp_table.h:289
Table< key_type, mapped_type >::Axis
set< key_type, greater< key_type > > Axis
Definition: rcpp_table.h:305
DataFrameSelect::DataFrameSelect
DataFrameSelect(const DataFrame &df, int i=0)
Definition: rcpp_table.h:123
NumericInterpolate::NumericInterpolate
NumericInterpolate()
Definition: rcpp_table.h:56
Table< key_type, mapped_type >::data
map< key_type, mapped_type > data
Definition: rcpp_table.h:323
Table::mapped_type
Outcome mapped_type
Definition: rcpp_table.h:151
Table< I0, I1, I2, Outcome >::Axis
std::tuple< set< I0, greater< I0 > >, set< I1, greater< I1 > >, set< I2, greater< I2 > > > Axis
Definition: rcpp_table.h:241
NumericInterpolate::invert
double invert(double yfind, double xentry)
Definition: rcpp_table.h:98
Table< I0, I1, I2, Outcome >::key_type
std::tuple< I0, I1, I2 > key_type
Definition: rcpp_table.h:235
Table< key_type, mapped_type >::axis
Axis axis
Definition: rcpp_table.h:322
Table< key_type, mapped_type >::Table
Table()
Definition: rcpp_table.h:306
Table< I0, I1, I2, I3, Outcome >::operator()
virtual Outcome operator()(I0 i0, I1 i1, I2 i2, I3 i3)
Definition: rcpp_table.h:211
NumericInterpolate::y
NumericVector y
Definition: rcpp_table.h:54
Table< I0, I1, I2, I3, Outcome >::key_type
std::tuple< I0, I1, I2, I3 > key_type
Definition: rcpp_table.h:194
Table< I0, I1, I2, Outcome >::axis
Axis axis
Definition: rcpp_table.h:265
Table::Axis
std::tuple< set< I0, greater< I0 > >, set< I1, greater< I1 > >, set< I2, greater< I2 > >, set< I3, greater< I3 > >, set< I4, greater< I4 > > > Axis
Definition: rcpp_table.h:158
Table::data
map< key_type, mapped_type > data
Definition: rcpp_table.h:188
Table< I0, I1, I2, I3, Outcome >::mapped_type
Outcome mapped_type
Definition: rcpp_table.h:195
Table< key_type, mapped_type >::Table
Table(const DataFrame &df, string s0, string s1)
Definition: rcpp_table.h:314
DataFrameSelect::DataFrameSelect
DataFrameSelect(const DataFrame &df, string name)
Definition: rcpp_table.h:126
Table< I0, I1, I2, I3, Outcome >::data
map< key_type, mapped_type > data
Definition: rcpp_table.h:229
Table< I0, I1, I2, I3, Outcome >::Table
Table()
Definition: rcpp_table.h:202
Table< I0, I1, I2, Outcome >::insert
void insert(I0 key0, I1 key1, I2 key2, mapped_type outcome)
Definition: rcpp_table.h:243
NumericInterpolate::NumericInterpolate
NumericInterpolate(DataFrame df, int i0=0, int i1=1)
Definition: rcpp_table.h:58
NumericInterpolate::push_back
void push_back(pair< double, double > xy)
Definition: rcpp_table.h:70
Interpolate::approx
double approx(double xfind)
Definition: rcpp_table.h:28
NumericInterpolate
Definition: rcpp_table.h:52
Table< I0, I1, I2, Outcome >::operator()
virtual Outcome operator()(I0 i0, I1 i1, I2 i2)
Definition: rcpp_table.h:250
Table< I0, I1, I2, I3, Outcome >::insert
void insert(I0 key0, I1 key1, I2 key2, I3 key3, mapped_type outcome)
Definition: rcpp_table.h:203
Table< I0, I1, Outcome >::Table
Table()
Definition: rcpp_table.h:278
set_lower_bound
T set_lower_bound(set< T, greater< T > > aset, T value)
Definition: rcpp_table.h:115
Table< I0, I1, I2, Outcome >::mapped_type
Outcome mapped_type
Definition: rcpp_table.h:236
null_type
A table class for lookups. For the case of a single key, this is a small extension to std::map,...
Definition: rcpp_table.h:143
NumericInterpolate::n
int n
Definition: rcpp_table.h:55
Interpolate
Definition: rcpp_table.h:16
DataFrameSelect::operator[]
T operator[](int i)
Definition: rcpp_table.h:129
Table< I0, I1, Outcome >::Axis
std::tuple< set< I0, greater< I0 > >, set< I1, greater< I1 > > > Axis
Definition: rcpp_table.h:277
Table< I0, I1, I2, I3, Outcome >::Axis
std::tuple< set< I0, greater< I0 > >, set< I1, greater< I1 > >, set< I2, greater< I2 > >, set< I3, greater< I3 > > > Axis
Definition: rcpp_table.h:201
Interpolate::y
vector< double > y
Definition: rcpp_table.h:18
Table::Table
Table()
Definition: rcpp_table.h:149
NumericInterpolate::invert
double invert(double yfind)
Definition: rcpp_table.h:89
NumericInterpolate::prepare
void prepare()
Definition: rcpp_table.h:65
Table< key_type, mapped_type >::insert
void insert(const key_type &key, const mapped_type &outcome)
Definition: rcpp_table.h:307
Table::insert
void insert(I0 key0, I1 key1, I2 key2, I3 key3, I4 key4, Outcome outcome)
Definition: rcpp_table.h:159
Table::axis
Axis axis
Definition: rcpp_table.h:187
NumericInterpolate::operator()
double operator()(double xfind)
Definition: rcpp_table.h:84
Rcpp
Definition: microsimulation.h:101
Interpolate::operator()
double operator()(double xfind)
Definition: rcpp_table.h:37
Table::operator()
virtual Outcome operator()(I0 i0, I1 i1, I2 i2, I3 i3, I4 i4)
Definition: rcpp_table.h:168
DataFrameSelect
Definition: rcpp_table.h:120
Table< I0, I1, Outcome >::data
map< key_type, mapped_type > data
Definition: rcpp_table.h:299
Table::Table
Table(const DataFrame &df, string s0, string s1, string s2, string s3, string s4, string s5)
Definition: rcpp_table.h:175
Table< I0, I1, Outcome >::mapped_type
Outcome mapped_type
Definition: rcpp_table.h:273
NumericInterpolate::approx
double approx(double xfind)
Definition: rcpp_table.h:75
Table< I0, I1, Outcome >::operator()
virtual Outcome operator()(I0 i0, I1 i1)
Definition: rcpp_table.h:285
DataFrameSelect::size
int size()
Definition: rcpp_table.h:132
Table< I0, I1, I2, I3, Outcome >::Table
Table(const DataFrame &df, string s0, string s1, string s2, string s3, string s4)
Definition: rcpp_table.h:217
Table< I0, I1, I2, Outcome >::data
map< key_type, mapped_type > data
Definition: rcpp_table.h:266
Interpolate::Interpolate
Interpolate()
Definition: rcpp_table.h:19