// TODO: Replace `cout, printf` with sprintf&fputs and custom buffers #ifndef _TABLE_H #define _TABLE_H #include "types.h" #include "vector_type.hpp" #include #include #include "io.h" 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: typedef ColRef<_Ty> Decayed_t; const char* name; types::Type_t ty = types::ERROR; ColRef() : vector_type<_Ty>(0), name("") {} ColRef(const uint32_t& size, const char* name = "") : vector_type<_Ty>(size), name(name) {} ColRef(const char* name) : name(name) {} void init(const char* name = "") { ty = types::Types<_Ty>::getType(); this->size = this->capacity = 0; this->container = 0; this->name = name; } 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); } void out(uint32_t n = 4, const char* sep = " ") const { n = n > this->size ? this->size : n; std::cout << '('; for (uint32_t i = 0; i < n; ++i) std::cout << this->operator[](i) << sep; std::cout << ')'; } template ColRef scast(); }; template class ColView { public: typedef ColRef<_Ty> Decayed_t; 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); } void out(uint32_t n = 4, const char* sep = " ") const { n = n > size ? size : n; std::cout<<'('; for (uint32_t i = 0; i < n; ++i) std::cout << this->operator[](i)<< sep; std::cout << ')'; } operator ColRef<_Ty>() { auto ret = ColRef<_Ty>(size); for (uint32_t i = 0; i < size; ++i) ret[i] = orig[idxs[i]]; return ret; } }; template