#pragma once #include "types.h" #include #include #include #include #undef max #undef min template class VT> size_t count(const VT& v) { return v.size; } template constexpr static inline size_t count(const T&) { return 1; } // TODO: Specializations for dt/str/none template class VT> types::GetLongType sum(const VT& v) { types::GetLongType ret = 0; for (const auto& _v : v) ret += _v; return ret; } template class VT> types::GetFPType avg(const VT& v) { return static_cast>( sum(v) / static_cast(v.size)); } template class VT> VT sqrt(const VT& v) { VT ret {v.size}; for (uint32_t i = 0; i < v.size; ++i){ ret[i] = sqrt(v[i]); } return ret; } template class VT> T max(const VT& v) { T max_v = std::numeric_limits::min(); for (const auto& _v : v) max_v = max_v > _v ? max_v : _v; return max_v; } template class VT> T min(const VT& v) { T min_v = std::numeric_limits::max(); for (const auto& _v : v) min_v = min_v < _v ? min_v : _v; return min_v; } template class VT> decayed_t mins(const VT& arr) { const uint32_t& len = arr.size; std::deque> cache; decayed_t 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 class VT> decayed_t maxs(const VT& arr) { const uint32_t& len = arr.size; decayed_t 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 class VT> decayed_t minw(uint32_t w, const VT& arr) { const uint32_t& len = arr.size; decayed_t 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 class VT> decayed_t maxw(uint32_t w, const VT& arr) { const uint32_t& len = arr.size; decayed_t 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 class VT> decayed_t> sums(const VT& arr) { const uint32_t& len = arr.size; decayed_t> ret(len); uint32_t i = 0; if(len) ret[i++] = arr[0]; for (; i < len; ++i) ret[i] = ret[i-1] + arr[i]; return ret; } template class VT> decayed_t> avgs(const VT& arr) { const uint32_t& len = arr.size; typedef types::GetFPType FPType; decayed_t ret(len); uint32_t i = 0; types::GetLongType s; if(len) s = ret[i++] = arr[0]; for (; i < len; ++i) ret[i] = (s+=arr[i])/(FPType)(i+1); return ret; } template class VT> decayed_t> sumw(uint32_t w, const VT& arr) { const uint32_t& len = arr.size; decayed_t> ret(len); uint32_t i = 0; w = w > len ? len : w; if(len) ret[i++] = arr[0]; for (; i < w; ++i) ret[i] = ret[i-1] + arr[i]; for (; i < len; ++i) ret[i] = ret[i-1] + arr[i] - arr[i-w]; return ret; } template class VT> decayed_t>> avgw(uint32_t w, const VT& arr) { typedef types::GetFPType> FPType; const uint32_t& len = arr.size; decayed_t ret(len); uint32_t i = 0; types::GetLongType s{}; w = w > len ? len : w; if(len) s = ret[i++] = arr[0]; for (; i < w; ++i) ret[i] = (s += arr[i])/(FPType)(i+1); for (; i < len; ++i) ret[i] = ret[i-1] + (arr[i] - arr[i-w])/(FPType)w; return ret; } template constexpr inline T count(const T& v) { return 1; } template constexpr inline T max(const T& v) { return v; } template constexpr inline T min(const T& v) { return v; } template constexpr inline T avg(const T& v) { return v; } template constexpr inline T sum(const T& v) { return v; } template constexpr inline T maxw(uint32_t, const T& v) { return v; } template constexpr inline T minw(uint32_t, const T& v) { return v; } template constexpr inline T avgw(uint32_t, const T& v) { return v; } template constexpr inline T sumw(uint32_t, const T& v) { return v; } template constexpr inline T maxs(const T& v) { return v; } template constexpr inline T mins(const T& v) { return v; } template constexpr inline T avgs(const T& v) { return v; } template constexpr inline T sums(const T& v) { return v; }