From 4c328ff6bd2fadbebb48753c28edee32a6695848 Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Thu, 18 Jan 2018 13:53:51 +0000 Subject: Revamp basic global cache class --- src/main/game/global_cache.cpp | 119 ++++++++++++++++++++++++++++------------- src/main/game/global_cache.h | 29 +++++----- 2 files changed, 99 insertions(+), 49 deletions(-) (limited to 'src/main/game') diff --git a/src/main/game/global_cache.cpp b/src/main/game/global_cache.cpp index a1d7585..83a9420 100644 --- a/src/main/game/global_cache.cpp +++ b/src/main/game/global_cache.cpp @@ -2,7 +2,7 @@ // Created by thecharlesblake on 1/10/18. // -#include +#include #include "global_cache.h" @@ -11,39 +11,35 @@ using namespace boost; typedef sol_rules::build_policy pol; typedef sol_rules::stock_deal_type sdt; -typedef global_cache cache; -global_cache::global_cache(const game_state init_game_state) { - global_cache::init_gs = init_game_state; +bool global_cache::insert(const game_state& gs) { + return u_set.insert(gs).second; } -bool global_cache::insert(const std::vector& pile_vec) { - return u_set.insert(pile_vec).second; +bool global_cache::contains(const game_state& gs) const { + return u_set.count(gs) > 0; } -bool global_cache::contains(const std::vector& pile_vec) { - return u_set.count(pile_vec) > 0; +void global_cache::clear() { + u_set.clear(); } size_t hash_value(card const& c) { - boost::hash hasher; + boost::hash hasher; - auto suit_val; - switch (cache::init_gs.rules.build_pol) { + uint8_t suit_val; + switch (game_state::rules.build_pol) { case pol::SAME_SUIT: - suit_val = static_cast>( - c.get_suit()); + suit_val = static_cast(c.get_suit()); break; case pol::RED_BLACK: - suit_val = static_cast>( - c.get_colour()); + suit_val = static_cast(c.get_colour()); break; default: suit_val = 0; - break; } - auto raw_val = static_cast(suit_val * 13 + c.get_rank()); + auto raw_val = static_cast(suit_val * 13 + c.get_rank()); return hasher(raw_val); } @@ -51,44 +47,95 @@ size_t hash_value(pile const& p) { return hash_range(begin(p.pile_vec), end(p.pile_vec)); } -size_t hash_value(std::vector const& piles) { +size_t hash_value(game_state const& gs) { + boost::hash pile_hasher; size_t seed = 0; // Using addition for commutative hash - for (game_state::pile_ref pr : cache::init_gs.tableau_piles) { - seed += hash_value(piles[pr]); + for (game_state::pile_ref pr : game_state::tableau_piles) { + seed += pile_hasher(gs.piles[pr]); } // Using hash_combine for order-dependent hash - for (game_state::pile_ref pr : cache::init_gs.foundations) { - hash_combine(seed, piles[pr]); + for (game_state::pile_ref pr : game_state::foundations) { + hash_combine(seed, gs.piles[pr]); } // Commutative - for (game_state::pile_ref pr : cache::init_gs.reserve) { - seed += hash_value(piles[pr]); - } - - if (cache::init_gs.rules.stock_size > 0) { - hash_combine(seed, piles[cache::init_gs.stock]); + for (game_state::pile_ref pr : game_state::reserve) { + seed += pile_hasher(gs.piles[pr]); } - if (cache::init_gs.rules.stock_deal_t == sdt::WASTE) { - hash_combine(seed, piles[cache::init_gs.waste]); + if (game_state::rules.stock_size > 0) { + hash_combine(seed, gs.piles[game_state::stock]); } - if (cache::init_gs.rules.stock_deal_t == sdt::WASTE) { - hash_combine(seed, piles[cache::init_gs.waste]); + if (game_state::rules.stock_deal_t == sdt::WASTE) { + hash_combine(seed, gs.piles[game_state::waste]); } - if (cache::init_gs.rules.hole) { - hash_combine(seed, piles[cache::init_gs.hole]); + if (game_state::rules.hole) { + hash_combine(seed, gs.piles[game_state::hole]); } // Commutative - for (game_state::pile_ref pr : cache::init_gs.cells) { - seed += hash_value(piles[pr]); + for (game_state::pile_ref pr : game_state::cells) { + seed += pile_hasher(gs.piles[pr]); } return seed; } + +bool global_cache::game_state_pred::comp_pile(const pile &x, const pile &y) { + // Largest piles first + if (x.size() != y.size()) { + return x.size() > y.size(); + } + // Larger card values first + for (pile::size_type pile_idx = 0; pile_idx < x.size(); pile_idx++) { + auto x_val = static_cast(x[pile_idx].get_suit()) * 13 + + x[pile_idx].get_rank(); + auto y_val = static_cast(y[pile_idx].get_suit()) * 13 + + y[pile_idx].get_rank(); + if (x_val != y_val) { + return x_val > y_val; + } + } + // It is a requirement of std::sort that equal values return false + return false; +} + +bool global_cache::game_state_pred::operator()(const game_state& x, + const game_state& y) const { + if (x.piles.size() != y.piles.size()) return false; + + vector x_piles = x.piles; + vector y_piles = y.piles; + + sort(begin(x_piles), end(x_piles), comp_pile); + sort(begin(y_piles), end(y_piles), comp_pile); + + for (vector::size_type pile_idx = 0; pile_idx < x_piles.size(); pile_idx++) { + if (x_piles[pile_idx].size() != y_piles[pile_idx].size()) return false; + + for (auto card_idx = 0; card_idx < x_piles[pile_idx].size(); card_idx++) { + card cx = x_piles[pile_idx][card_idx]; + card cy = y_piles[pile_idx][card_idx]; + + if (cx.get_rank() != cy.get_rank()) return false; + + switch (game_state::rules.build_pol) { + case pol::SAME_SUIT: + if (cx.get_suit() != cy.get_suit()) return false; + break; + case pol::RED_BLACK: + if (cx.get_colour() != cy.get_colour()) return false; + break; + default: + break; + } + } + } + + return true; +} diff --git a/src/main/game/global_cache.h b/src/main/game/global_cache.h index 73f0ce7..a3d1e48 100644 --- a/src/main/game/global_cache.h +++ b/src/main/game/global_cache.h @@ -8,24 +8,27 @@ #include #include -#include "../game/game_state.h" -#include "../game/sol_rules.h" +#include + +#include "sol_rules.h" +#include "game_state.h" class global_cache { public: - explicit global_cache(game_state); - - bool insert(const std::vector&); - bool contains(const std::vector&); - - friend std::size_t hash_value(card const&); - friend std::size_t hash_value(std::vector const&); + bool insert(const game_state&); + bool contains(const game_state&) const; + void clear(); + + struct game_state_pred { + static bool comp_pile(const pile& x, const pile& y); + bool operator() (const game_state& x, const game_state& y) + const; + }; private: - static game_state init_gs; - std::unordered_set< - std::vector, - boost::hash> + game_state, + boost::hash, + game_state_pred > u_set; }; -- cgit v1.2.1