#ifndef _TABLE_H #define _TABLE_H #include "types.h" #include "vector_type.hpp" #include 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 ColView; template class ColRef : public vector_type<_Ty> { public: const char* name; types::Type_t ty = types::ERROR; ColRef(const 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) {} using vector_type<_Ty>::operator[]; using vector_type<_Ty>::operator=; ColView<_Ty> operator [](const vector_type&idxs) const { return ColView<_Ty>(*this, idxs); } template ColRef scast(); }; template class ColView { public: const vector_type& idxs; const ColRef<_Ty>& orig; const uint32_t& size; ColView(const ColRef<_Ty>& orig, const vector_type& idxs) : orig(orig), idxs(idxs), size(idxs.size) {} ColView(const ColView<_Ty>& orig, const vector_type& idxs) : orig(orig.orig), idxs(idxs), size(idxs.size) { for (uint32_t i = 0; i < size; ++i) idxs[i] = orig.idxs[idxs[i]]; } _Ty& operator [](const uint32_t& i) const { return orig[idxs[i]]; } struct Iterator_t { const uint32_t* val; const ColRef<_Ty>& orig; constexpr Iterator_t(const uint32_t* val, const ColRef<_Ty>& orig) noexcept : val(val), orig(orig) {} _Ty& operator*() { return orig[*val]; } bool operator != (const Iterator_t& rhs) { return rhs.val != val; } Iterator_t& operator++ () { ++val; return *this; } Iterator_t operator++ (int) { Iterator_t tmp = *this; ++val; return tmp; } }; Iterator_t begin() const { return Iterator_t(idxs.begin(), orig); } Iterator_t end() const { return Iterator_t(idxs.end(), orig); } }; 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; void print(const char* __restrict sep, const char* __restrict end) const; typedef std::tuple tuple_type; template typename std::enable_if::type print_impl(const uint32_t& i, const char* __restrict sep = " ") const; template typename std::enable_if::type print_impl(const uint32_t& i, const char* __restrict sep = " ") const; template struct GetTypes { typedef typename std::tuple::type ...> type; }; template using getRecordType = typename GetTypes::type; TableInfo(const char* name, uint32_t n_cols); //template //struct Iterator_t { // uint32_t val; // const TableInfo* info; // constexpr Iterator_t(const uint32_t* val, const TableInfo* info) noexcept : val(val), info(info) {} // getRecordType operator*() { // return getRecordType(info->colrefs[Idxs].operator[](*val)...); // } // bool operator != (const Iterator_t& rhs) { return rhs.val != val; } // Iterator_t& operator++ () { // ++val; // return *this; // } // Iterator_t operator++ (int) { // Iterator_t tmp = *this; // ++val; // return tmp; // } //}; //template //Iterator_t begin() const { // //} //template //Iterator_t end() const { // //} // //template //order_by() { // vector_type order(colrefs[0].size); // std::sort(this->begin) //} }; template constexpr inline ColRef>>& get(const TableInfo<_Types...>& table) noexcept { return *(ColRef>> *) & (table.colrefs[_Index]); } 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 template inline typename std::enable_if::type TableInfo::print_impl(const uint32_t& i, const char* __restrict sep) const { std::cout << (get(*this))[i]; } template template inline typename std::enable_if::type TableInfo::print_impl(const uint32_t& i, const char* __restrict sep) const { std::cout << (get(*this))[i] << sep; print_impl(i, sep); } template inline void TableInfo::print(const char* __restrict sep, const char* __restrict end) const { int n_rows = 0; if (n_cols > 0 && colrefs[0].size > 0) n_rows = colrefs[0].size; for (int i = 0; i < n_rows; ++i) { print_impl(i); std::cout << end; } } template class VT> VT::type> operator -(const VT& lhs, const VT& rhs) { auto ret = VT::type>(lhs.size, ""); for (int i = 0; i < lhs.size; ++i) ret.container[i] = lhs.container[i] - rhs.container[i]; return ret; } template class VT> VT::type> operator -(const VT& lhs, const T2& rhs) { auto ret = VT::type>(lhs.size, ""); for (int i = 0; i < lhs.size; ++i) ret.container[i] = lhs.container[i] - rhs; return ret; } template class VT> VT::type> operator +(const VT& lhs, const VT& rhs) { auto ret = VT::type>(lhs.size, ""); for (int i = 0; i < lhs.size; ++i) ret.container[i] = lhs.container[i] + rhs.container[i]; return ret; } template class VT> VT::type> operator +(const VT& lhs, const T2& rhs) { auto ret = VT::type>(lhs.size, ""); for (int i = 0; i < lhs.size; ++i) ret.container[i] = lhs.container[i] + rhs; return ret; } template class VT> VT::type> operator *(const VT& lhs, const VT& rhs) { auto ret = VT::type>(lhs.size, ""); for (int i = 0; i < lhs.size; ++i) ret.container[i] = lhs.container[i] * rhs.container[i]; return ret; } template class VT> VT::type> operator *(const VT& lhs, const T2& rhs) { auto ret = VT::type>(lhs.size, ""); for (int i = 0; i < lhs.size; ++i) ret.container[i] = lhs.container[i] * rhs; return ret; } template class VT> VT::type> operator /(const VT& lhs, const VT& rhs) { auto ret = VT::type>(lhs.size, ""); for (int i = 0; i < lhs.size; ++i) ret.container[i] = lhs.container[i] / rhs.container[i]; return ret; } template class VT> VT::type> operator /(const VT& lhs, const T2& rhs) { auto ret = VT::type>(lhs.size, ""); for (int i = 0; i < lhs.size; ++i) ret.container[i] = lhs.container[i] / rhs; return ret; } template void print(const TableInfo& v, const char* delimiter = " ", const char* endline = "\n") { v.print(delimiter, endline); } template void print(const T& v, const char* delimiter = " ") { printf(types::printf_str[types::Types::getType()], v); } template void inline print_impl(const T& v, const char* delimiter, const char* endline) { for (const auto& vi : v) { print(vi); printf("%s", delimiter); } printf("%s", endline); } template class VT> typename std::enable_if, TableInfo>::value>::type print(const VT& v, const char* delimiter = " ", const char* endline = "\n") { print_impl(v, delimiter, endline); } #endif