mirror of
https://github.com/opelly27/Stockfish.git
synced 2026-05-20 09:47:46 +00:00
Cleanup stats entry
Prevents potential issue caused by publicly inheriting from STL container types closes https://github.com/official-stockfish/Stockfish/pull/5746 No functional change
This commit is contained in:
+41
-13
@@ -76,7 +76,10 @@ class StatsEntry {
|
|||||||
T entry;
|
T entry;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
void operator=(const T& v) { entry = v; }
|
StatsEntry& operator=(const T& v) {
|
||||||
|
entry = v;
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
T* operator&() { return &entry; }
|
T* operator&() { return &entry; }
|
||||||
T* operator->() { return &entry; }
|
T* operator->() { return &entry; }
|
||||||
operator const T&() const { return entry; }
|
operator const T&() const { return entry; }
|
||||||
@@ -92,28 +95,53 @@ class StatsEntry {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
template<typename T, int D, std::size_t Size, std::size_t... Sizes>
|
||||||
|
struct StatsHelper;
|
||||||
|
|
||||||
// Stats is a generic N-dimensional array used to store various statistics.
|
// 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
|
// 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
|
// 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)
|
// values with the << operator, while the last parameters (Size and Sizes)
|
||||||
// encode the dimensions of the array.
|
// encode the dimensions of the array.
|
||||||
template<typename T, int D, int Size, int... Sizes>
|
template<typename T, int D, std::size_t Size, std::size_t... Sizes>
|
||||||
struct Stats: public std::array<Stats<T, D, Sizes...>, Size> {
|
class Stats {
|
||||||
using stats = Stats<T, D, Size, Sizes...>;
|
using child_type = typename StatsHelper<T, D, Size, Sizes...>::child_type;
|
||||||
|
using array_type = std::array<child_type, Size>;
|
||||||
|
array_type data;
|
||||||
|
|
||||||
|
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]; }
|
||||||
|
|
||||||
|
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(); }
|
||||||
|
|
||||||
void fill(const T& v) {
|
void fill(const T& v) {
|
||||||
|
for (auto& ele : data)
|
||||||
// For standard-layout 'this' points to the first struct member
|
{
|
||||||
assert(std::is_standard_layout_v<stats>);
|
if constexpr (sizeof...(Sizes) == 0)
|
||||||
|
ele = v;
|
||||||
using entry = StatsEntry<T, D>;
|
else
|
||||||
entry* p = reinterpret_cast<entry*>(this);
|
ele.fill(v);
|
||||||
std::fill(p, p + sizeof(*this) / sizeof(entry), v);
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
template<typename T, int D, int Size>
|
template<typename T, int D, std::size_t Size, std::size_t... Sizes>
|
||||||
struct Stats<T, D, Size>: public std::array<StatsEntry<T, D>, Size> {};
|
struct StatsHelper {
|
||||||
|
using child_type = Stats<T, D, Sizes...>;
|
||||||
|
};
|
||||||
|
|
||||||
|
template<typename T, int D, std::size_t Size>
|
||||||
|
struct StatsHelper<T, D, Size> {
|
||||||
|
using child_type = StatsEntry<T, D>;
|
||||||
|
};
|
||||||
|
|
||||||
// In stats table, D=0 means that the template parameter is not used
|
// In stats table, D=0 means that the template parameter is not used
|
||||||
enum StatsParams {
|
enum StatsParams {
|
||||||
|
|||||||
@@ -18,7 +18,6 @@
|
|||||||
|
|
||||||
#include "movepick.h"
|
#include "movepick.h"
|
||||||
|
|
||||||
#include <array>
|
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
#include <limits>
|
#include <limits>
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user