From 44b0f302146d66f1a04be81cb23c5659c1214cd5 Mon Sep 17 00:00:00 2001 From: Edoardo Lolletti Date: Fri, 5 Jan 2024 16:53:38 +0100 Subject: [PATCH] More minor c++17 updates --- bit.h | 4 ++-- field.cpp | 2 -- field.h | 9 ++++++++- interpreter.cpp | 18 +++++++++--------- 4 files changed, 19 insertions(+), 14 deletions(-) diff --git a/bit.h b/bit.h index 45703062..efd3db11 100644 --- a/bit.h +++ b/bit.h @@ -11,7 +11,7 @@ namespace bit { template -inline uint8_t popcnt(T val) { +constexpr inline uint8_t popcnt(T val) { uint8_t ans = 0; while(val) { val &= val - 1; @@ -21,7 +21,7 @@ inline uint8_t popcnt(T val) { } template -inline bool has_invalid_bits(T value, T2 allowed) { +constexpr inline bool has_invalid_bits(T value, T2 allowed) { return (value & (~allowed)) != 0; } diff --git a/field.cpp b/field.cpp index f0cefe87..896dbccc 100644 --- a/field.cpp +++ b/field.cpp @@ -15,8 +15,6 @@ #include "group.h" #include "interpreter.h" -int32_t field::field_used_count[32] = {0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4, 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5}; - bool chain::chain_operation_sort(const chain& c1, const chain& c2) { return c1.triggering_effect->id < c2.triggering_effect->id; } diff --git a/field.h b/field.h index d929029b..fe178151 100644 --- a/field.h +++ b/field.h @@ -15,6 +15,7 @@ #include #include //std::forward #include +#include "bit.h" #include "card.h" #include "common.h" #include "containers_fwd.h" @@ -417,7 +418,13 @@ class field { return_card_code return_card_codes; tevent nil_event; - static int32_t field_used_count[32]; + static constexpr auto field_used_count = ([]() constexpr { + std::array ret{}; + for(size_t i = 0; i < ret.size(); ++i) { + ret[i] = bit::popcnt(i); + } + return ret; + })(); explicit field(duel* pduel, const OCG_DuelOptions& options); ~field() = default; void reload_field_info(); diff --git a/interpreter.cpp b/interpreter.cpp index fa6dc66d..254f1e69 100644 --- a/interpreter.cpp +++ b/interpreter.cpp @@ -31,7 +31,7 @@ interpreter::interpreter(duel* pd, const OCG_DuelOptions& options): coroutines(2 pduel = pd; no_action = 0; call_depth = 0; - memcpy(lua_getextraspace(lua_state), &pd, sizeof(duel*)); + std::memcpy(lua_getextraspace(lua_state), &pd, sizeof(duel*)); // Open basic and used functionality auto open_lib = [L=lua_state](const char* libname, lua_CFunction openf) { luaL_requiref(L, libname, openf, 1); @@ -229,27 +229,27 @@ bool interpreter::load_card_script(uint32_t code) { void interpreter::push_param(lua_State* L, bool is_coroutine) { int32_t pushed = 0; luaL_checkstack(L, static_cast(params.size()), nullptr); - for (const auto& it : params) { - switch(it.second) { + for(const auto& [param, type] : params) { + switch(type) { case LuaParam::INT: - lua_pushinteger(L, it.first.integer); + lua_pushinteger(L, param.integer); break; case LuaParam::STRING: - lua_pushstring(L, static_cast(it.first.ptr)); + lua_pushstring(L, static_cast(param.ptr)); break; case LuaParam::BOOLEAN: - lua_pushboolean(L, static_cast(it.first.integer)); + lua_pushboolean(L, static_cast(param.integer)); break; case LuaParam::CARD: case LuaParam::EFFECT: case LuaParam::GROUP: - pushobject(L, static_cast(it.first.ptr)); + pushobject(L, static_cast(param.ptr)); break; case LuaParam::FUNCTION: - pushobject(L, static_cast(it.first.integer)); + pushobject(L, static_cast(param.integer)); break; case LuaParam::INDEX: { - auto index = static_cast(it.first.integer); + auto index = static_cast(param.integer); if(index > 0) lua_pushvalue(L, index); else if(is_coroutine) {