From 93e0606a7992ace7d7cede3da4f0ceb7dd3d2eac Mon Sep 17 00:00:00 2001 From: Shawn Xu Date: Mon, 30 Dec 2024 23:00:03 -0800 Subject: [PATCH] Cleanup stats entry No functional change --- src/history.h | 56 ++++++++++++++++++++++++++++++++++++------------ src/movepick.cpp | 1 - 2 files changed, 42 insertions(+), 15 deletions(-) diff --git a/src/history.h b/src/history.h index 27571459..412c75e7 100644 --- a/src/history.h +++ b/src/history.h @@ -76,9 +76,12 @@ class StatsEntry { T entry; public: - void operator=(const T& v) { entry = v; } - T* operator&() { return &entry; } - T* operator->() { 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) { @@ -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 { diff --git a/src/movepick.cpp b/src/movepick.cpp index 77aacc9e..9e616860 100644 --- a/src/movepick.cpp +++ b/src/movepick.cpp @@ -18,7 +18,6 @@ #include "movepick.h" -#include #include #include