#ifndef _TABLE_H #define _TABLE_H #include "types.h" #include "vector_type.hpp" template class vector_type; template <> class vector_type; #ifdef _MSC_VER namespace types { enum Type_t; template struct Types; template struct Coercion; } #endif template class ColRef : public vector_type<_Ty> { public: const char* name; types::Type_t ty = types::ERROR; ColRef(uint32_t size, const char* name = "") : vector_type<_Ty>(size), name(name) {} ColRef(const char* name) : name(name) {} void init() { ty = types::Types<_Ty>::getType(); this->size = this->capacity = 0; this->container = 0; } ColRef(const char* name, types::Type_t ty) : name(name), ty(ty) {} template ColRef scast(); }; template template inline ColRef ColRef<_Ty>::scast() { this->ty = types::Types::getType(); return *(ColRef *)this; } using uColRef = ColRef; template struct TableInfo { const char* name; ColRef* colrefs; uint32_t n_cols, n_rows; //void print(); TableInfo(const char* name, uint32_t n_cols); }; template constexpr static inline bool is_vector(const ColRef&) { return true; } template constexpr static inline bool is_vector(const vector_type&) { return true; } template constexpr static inline bool is_vector(const T&) { return false; } template TableInfo::TableInfo(const char* name, uint32_t n_cols) : name(name), n_cols(n_cols) { this->colrefs = (ColRef*)malloc(sizeof(ColRef) * n_cols); } template ColRef::type> operator -(const ColRef& lhs, const ColRef& rhs) { auto ret = ColRef::type>(lhs.size, ""); for (int i = 0; i < lhs.size; ++i) ret.container[i] = lhs.container[i] - rhs.container[i]; return ret; } template ColRef::type> operator -(const ColRef& lhs, const T2& rhs) { auto ret = ColRef::type>(lhs.size, ""); for (int i = 0; i < lhs.size; ++i) ret.container[i] = lhs.container[i] - rhs; return ret; } template constexpr ColRef>>& get(TableInfo<_Types...>& table) noexcept { return *(ColRef< std::tuple_element_t<_Index, std::tuple<_Types...>>> *) &(table.colrefs[_Index]); } //void TableInfo::print() //{ // for (int i = 0; i < n_rows; ++i) { // for (int j = 0; j < n_cols; ++j) // TODO: Deal with date/time // printf(types::printf_str[colrefs[j].ty], colrefs[j].get(i, colrefs[j].ty)); // puts(""); // } //} template ColRef mins(const ColRef& arr) { const int& len = arr.size; std::deque> cache; ColRef ret(len); T min = std::numeric_limits::max(); for (int i = 0; i < len; ++i) { if (arr[i] < min) min = arr[i]; ret[i] = min; } return ret; } template ColRef maxs(const ColRef& arr) { const int& len = arr.size; ColRef ret(len); T max = std::numeric_limits::min(); for (int i = 0; i < len; ++i) { if (arr[i] > max) max = arr[i]; ret[i] = max; } return ret; } template ColRef minw(const ColRef& arr, uint32_t w) { const int& len = arr.size; ColRef ret(len); std::deque> cache; for (int i = 0; i < len; ++i) { if (!cache.empty() && cache.front().second == i - w) cache.pop_front(); while (!cache.empty() && cache.back().first > arr[i]) cache.pop_back(); cache.push_back({ arr[i], i }); ret[i] = cache.front().first; } return ret; } template ColRef maxw(const ColRef& arr, uint32_t w) { const int& len = arr.size; ColRef ret(len); std::deque> cache; for (int i = 0; i < len; ++i) { if (!cache.empty() && cache.front().second == i - w) cache.pop_front(); while (!cache.empty() && cache.back().first > arr[i]) cache.pop_back(); cache.push_back({ arr[i], i }); arr[i] = cache.front().first; } return ret; } template void print(const T& v) { printf(types::printf_str[types::Types::getType()], v); } template void print(const ColRef& v, const char* delimiter) { for (const auto& vi : v) { print(vi); puts(delimiter); } } #endif