diff --git a/src/history.h b/src/history.h index 17ab3481c3e..db9a79aef86 100644 --- a/src/history.h +++ b/src/history.h @@ -76,10 +76,13 @@ class StatsEntry { T entry; public: - void operator=(const T& v) { entry = v; } - T* operator&() { return &entry; } - T* operator->() { return &entry; } - operator const T&() const { return entry; } + StatsEntry& operator=(const T& v) { + entry = v; + return *this; + } + T* operator&() { return &entry; } + T* operator->() { return &entry; } + operator const T&() const { return entry; } void operator<<(int bonus) { static_assert(D <= std::numeric_limits::max(), "D overflows T"); @@ -92,28 +95,53 @@ class StatsEntry { } }; +template +struct StatsHelper; + // Stats is a generic N-dimensional array used to store various statistics. // The first template parameter T is the base type of the array, and the second // template parameter D limits the range of updates in [-D, D] when we update // values with the << operator, while the last parameters (Size and Sizes) // encode the dimensions of the array. -template -struct Stats: public std::array, Size> { - using stats = Stats; +template +class Stats { + using child_type = typename StatsHelper::child_type; + using array_type = std::array; + array_type data; - void fill(const T& v) { + public: + using size_type = typename array_type::size_type; + + auto& operator[](size_type index) { return data[index]; } + const auto& operator[](size_type index) const { return data[index]; } - // For standard-layout 'this' points to the first struct member - assert(std::is_standard_layout_v); + auto begin() { return data.begin(); } + auto end() { return data.end(); } + auto begin() const { return data.cbegin(); } + auto end() const { return data.cend(); } + auto cbegin() const { return data.cbegin(); } + auto cend() const { return data.cend(); } - using entry = StatsEntry; - entry* p = reinterpret_cast(this); - std::fill(p, p + sizeof(*this) / sizeof(entry), v); + void fill(const T& v) { + for (auto& ele : data) + { + if constexpr (sizeof...(Sizes) == 0) + ele = v; + else + ele.fill(v); + } } }; -template -struct Stats: public std::array, Size> {}; +template +struct StatsHelper { + using child_type = Stats; +}; + +template +struct StatsHelper { + using child_type = StatsEntry; +}; // In stats table, D=0 means that the template parameter is not used enum StatsParams {