From f12e2936fda51eb59016dd316ff9c73fba621fa2 Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Fri, 12 Jan 2018 16:31:16 +0000 Subject: Add basic global_cache interface --- src/main/game/global_cache.cpp | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 src/main/game/global_cache.cpp (limited to 'src/main/game/global_cache.cpp') diff --git a/src/main/game/global_cache.cpp b/src/main/game/global_cache.cpp new file mode 100644 index 0000000..cc95b3b --- /dev/null +++ b/src/main/game/global_cache.cpp @@ -0,0 +1,16 @@ +// +// Created by thecharlesblake on 1/10/18. +// + +#include "global_cache.h" + + +global_cache::global_cache(const sol_rules&) {} + +bool global_cache::insert(const std::vector& pile_vec) { + return u_set.insert(pile_vec).second; +} + +bool global_cache::contains(const std::vector& pile_vec) { + return u_set.count(pile_vec) > 0; +} -- cgit v1.2.1 From 01083ef011143eef36f8f83ecbdae8da6e948021 Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Fri, 12 Jan 2018 17:55:56 +0000 Subject: Implement symmetrical hash for cards --- src/main/game/global_cache.cpp | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) (limited to 'src/main/game/global_cache.cpp') diff --git a/src/main/game/global_cache.cpp b/src/main/game/global_cache.cpp index cc95b3b..f55a72c 100644 --- a/src/main/game/global_cache.cpp +++ b/src/main/game/global_cache.cpp @@ -5,7 +5,11 @@ #include "global_cache.h" -global_cache::global_cache(const sol_rules&) {} +typedef sol_rules::build_policy pol; + +global_cache::global_cache(const sol_rules& rules) { + global_cache::rules = rules; +} bool global_cache::insert(const std::vector& pile_vec) { return u_set.insert(pile_vec).second; @@ -14,3 +18,25 @@ bool global_cache::insert(const std::vector& pile_vec) { bool global_cache::contains(const std::vector& pile_vec) { return u_set.count(pile_vec) > 0; } + +size_t hash_value(card const& c) { + boost::hash hasher; + + auto suit_val; + switch (global_cache::rules.build_pol) { + case pol::SAME_SUIT: + suit_val = static_cast>( + c.get_suit()); + break; + case pol::RED_BLACK: + suit_val = static_cast>( + c.get_colour()); + break; + default: + suit_val = 0; + break; + } + + auto raw_val = static_cast(suit_val * 13 + c.get_rank()); + return hasher(raw_val); +} -- cgit v1.2.1 From 895bbca6c0c7233fe11f331a10135443970ec4c5 Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Fri, 12 Jan 2018 18:05:07 +0000 Subject: Implement hash for piles --- src/main/game/global_cache.cpp | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'src/main/game/global_cache.cpp') diff --git a/src/main/game/global_cache.cpp b/src/main/game/global_cache.cpp index f55a72c..e881408 100644 --- a/src/main/game/global_cache.cpp +++ b/src/main/game/global_cache.cpp @@ -2,8 +2,12 @@ // Created by thecharlesblake on 1/10/18. // +#include + #include "global_cache.h" +using namespace std; +using namespace boost; typedef sol_rules::build_policy pol; @@ -40,3 +44,7 @@ size_t hash_value(card const& c) { auto raw_val = static_cast(suit_val * 13 + c.get_rank()); return hasher(raw_val); } + +size_t hash_value(pile const& p) { + return hash_range(begin(p.pile_vec), end(p.pile_vec)); +} -- cgit v1.2.1 From add27468ed6ee09a9df6f885becc6b0428f666bf Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Fri, 12 Jan 2018 19:03:23 +0000 Subject: Implement symmetrical hash for pile vector --- src/main/game/global_cache.cpp | 50 +++++++++++++++++++++++++++++++++++++++--- 1 file changed, 47 insertions(+), 3 deletions(-) (limited to 'src/main/game/global_cache.cpp') diff --git a/src/main/game/global_cache.cpp b/src/main/game/global_cache.cpp index e881408..a1d7585 100644 --- a/src/main/game/global_cache.cpp +++ b/src/main/game/global_cache.cpp @@ -10,9 +10,11 @@ using namespace std; 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 sol_rules& rules) { - global_cache::rules = rules; +global_cache::global_cache(const game_state init_game_state) { + global_cache::init_gs = init_game_state; } bool global_cache::insert(const std::vector& pile_vec) { @@ -27,7 +29,7 @@ size_t hash_value(card const& c) { boost::hash hasher; auto suit_val; - switch (global_cache::rules.build_pol) { + switch (cache::init_gs.rules.build_pol) { case pol::SAME_SUIT: suit_val = static_cast>( c.get_suit()); @@ -48,3 +50,45 @@ size_t hash_value(card const& c) { 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 seed = 0; + + // Using addition for commutative hash + for (game_state::pile_ref pr : cache::init_gs.tableau_piles) { + seed += hash_value(piles[pr]); + } + + // Using hash_combine for order-dependent hash + for (game_state::pile_ref pr : cache::init_gs.foundations) { + hash_combine(seed, 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]); + } + + if (cache::init_gs.rules.stock_deal_t == sdt::WASTE) { + hash_combine(seed, piles[cache::init_gs.waste]); + } + + if (cache::init_gs.rules.stock_deal_t == sdt::WASTE) { + hash_combine(seed, piles[cache::init_gs.waste]); + } + + if (cache::init_gs.rules.hole) { + hash_combine(seed, piles[cache::init_gs.hole]); + } + + // Commutative + for (game_state::pile_ref pr : cache::init_gs.cells) { + seed += hash_value(piles[pr]); + } + + return seed; +} -- cgit v1.2.1 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 ++++++++++++++++++++++++++++------------- 1 file changed, 83 insertions(+), 36 deletions(-) (limited to 'src/main/game/global_cache.cpp') 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; +} -- cgit v1.2.1 From 60b86edcba4cd61a0b14aa5e1138a1dd1d7d8718 Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Thu, 18 Jan 2018 18:57:58 +0000 Subject: Tidy up game_state private access --- src/main/game/global_cache.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/main/game/global_cache.cpp') diff --git a/src/main/game/global_cache.cpp b/src/main/game/global_cache.cpp index 83a9420..7be491e 100644 --- a/src/main/game/global_cache.cpp +++ b/src/main/game/global_cache.cpp @@ -86,7 +86,7 @@ size_t hash_value(game_state const& gs) { return seed; } -bool global_cache::game_state_pred::comp_pile(const pile &x, const pile &y) { +bool game_state_pred::comp_pile(const pile &x, const pile &y) { // Largest piles first if (x.size() != y.size()) { return x.size() > y.size(); @@ -105,7 +105,7 @@ bool global_cache::game_state_pred::comp_pile(const pile &x, const pile &y) { return false; } -bool global_cache::game_state_pred::operator()(const game_state& x, +bool game_state_pred::operator()(const game_state& x, const game_state& y) const { if (x.piles.size() != y.piles.size()) return false; -- cgit v1.2.1 From 4bc355a0c61d043a6231889e8d902b6cf951096c Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Thu, 18 Jan 2018 23:11:16 +0000 Subject: Add SIMPLE_HASH debug flag to cache logic --- src/main/game/global_cache.cpp | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) (limited to 'src/main/game/global_cache.cpp') diff --git a/src/main/game/global_cache.cpp b/src/main/game/global_cache.cpp index 7be491e..8e3aa8b 100644 --- a/src/main/game/global_cache.cpp +++ b/src/main/game/global_cache.cpp @@ -27,6 +27,9 @@ void global_cache::clear() { size_t hash_value(card const& c) { boost::hash hasher; +#ifdef SIMPLE_HASH + uint8_t suit_val = static_cast(c.get_suit()); +#else uint8_t suit_val; switch (game_state::rules.build_pol) { case pol::SAME_SUIT: @@ -38,6 +41,7 @@ size_t hash_value(card const& c) { default: suit_val = 0; } +#endif auto raw_val = static_cast(suit_val * 13 + c.get_rank()); return hasher(raw_val); @@ -48,9 +52,15 @@ size_t hash_value(pile const& p) { } size_t hash_value(game_state const& gs) { - boost::hash pile_hasher; size_t seed = 0; +#ifdef SIMPLE_HASH + for (game_state::pile_ref pr = 0; pr < gs.piles.size(); pr++) { + hash_combine(seed, gs.piles[pr]); + } +#else + boost::hash pile_hasher; + // Using addition for commutative hash for (game_state::pile_ref pr : game_state::tableau_piles) { seed += pile_hasher(gs.piles[pr]); @@ -82,6 +92,7 @@ size_t hash_value(game_state const& gs) { for (game_state::pile_ref pr : game_state::cells) { seed += pile_hasher(gs.piles[pr]); } +#endif return seed; } @@ -112,9 +123,11 @@ bool game_state_pred::operator()(const game_state& x, vector x_piles = x.piles; vector y_piles = y.piles; + // Orders the piles from largest to smallest sort(begin(x_piles), end(x_piles), comp_pile); sort(begin(y_piles), end(y_piles), comp_pile); + // Compares the two for equality 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; @@ -124,6 +137,9 @@ bool game_state_pred::operator()(const game_state& x, if (cx.get_rank() != cy.get_rank()) return false; +#ifdef SIMPLE_HASH + if (cx.get_suit() != cy.get_suit()) return false; +#else switch (game_state::rules.build_pol) { case pol::SAME_SUIT: if (cx.get_suit() != cy.get_suit()) return false; @@ -134,6 +150,7 @@ bool game_state_pred::operator()(const game_state& x, default: break; } +#endif } } -- cgit v1.2.1