From c06c22547e59c62d6c32b62915c771a0f3737a5d Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Thu, 1 Feb 2018 12:57:50 +0000 Subject: Add default constructor for card & 'divider' card --- src/main/game/card.cpp | 4 ++++ src/main/game/card.h | 3 +++ 2 files changed, 7 insertions(+) (limited to 'src') diff --git a/src/main/game/card.cpp b/src/main/game/card.cpp index 25a981f..271bf73 100644 --- a/src/main/game/card.cpp +++ b/src/main/game/card.cpp @@ -18,6 +18,8 @@ card::card(suit_t s, rank_t r) : suit(s), rank(r) {} card::card(const char* c) : suit(suit_from_str(c)), rank(rank_from_str(c)) {} +card::card() : card(0, 0) {} + card::suit_t card::suit_from_str(const char* c) { switch(tolower(c[strlen(c) - 1])) { case 'c': return suit::Clubs; @@ -75,6 +77,8 @@ std::string card::to_string() const { return s; } +const card card::divider = card(0, 0); + bool operator==(const card& l, const card& r) { return l.get_rank() == r.get_rank() && l.get_suit() == r.get_suit(); } diff --git a/src/main/game/card.h b/src/main/game/card.h index 318cd12..74f513d 100644 --- a/src/main/game/card.h +++ b/src/main/game/card.h @@ -27,10 +27,13 @@ public: static const colour_t Red = 1; }; + const static card divider; + // A card can either be created using two int values (rank & suit), or // using a string (e.g. "AS") card(suit_t, rank_t); explicit card(const char*); + card(); suit_t get_suit() const; colour_t get_colour() const; -- cgit v1.2.1 From 712d6b4bde9016b9b8fa45e3c82c364255965d92 Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Thu, 1 Feb 2018 12:58:32 +0000 Subject: Change pile comp ops to assess size first --- src/main/game/pile.cpp | 24 ++++++++++++++++++++---- src/main/game/pile.h | 1 + 2 files changed, 21 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/main/game/pile.cpp b/src/main/game/pile.cpp index 6d52335..367f7a0 100644 --- a/src/main/game/pile.cpp +++ b/src/main/game/pile.cpp @@ -52,17 +52,33 @@ bool operator==(const pile& a, const pile& b) { } bool operator<(const pile& a, const pile& b) { - return a.pile_vec < b.pile_vec; + if (a.size() == b.size()) { + return a.pile_vec < b.pile_vec; + } else { + return a.size() < b.size(); + } } bool operator>(const pile& a, const pile& b) { - return a.pile_vec > b.pile_vec; + if (a.size() == b.size()) { + return a.pile_vec > b.pile_vec; + } else { + return a.size() > b.size(); + } } bool operator<=(const pile& a, const pile& b) { - return a.pile_vec <= b.pile_vec; + if (a.size() == b.size()) { + return a.pile_vec <= b.pile_vec; + } else { + return a.size() <= b.size(); + } } bool operator>=(const pile& a, const pile& b) { - return a.pile_vec >= b.pile_vec; + if (a.size() == b.size()) { + return a.pile_vec >= b.pile_vec; + } else { + return a.size() >= b.size(); + } } diff --git a/src/main/game/pile.h b/src/main/game/pile.h index 4256b02..7257738 100644 --- a/src/main/game/pile.h +++ b/src/main/game/pile.h @@ -14,6 +14,7 @@ class pile { friend class hasher; friend class game_state; + friend class cached_game_state; public: typedef uint8_t size_type; -- cgit v1.2.1 From fff4e0472b422e09f176c1deffdf4f0a30b15f4c Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Thu, 1 Feb 2018 12:59:29 +0000 Subject: Remove order on cache flag from game state --- src/main/game/game_state.cpp | 6 ------ src/main/game/game_state.h | 3 +-- 2 files changed, 1 insertion(+), 8 deletions(-) (limited to 'src') diff --git a/src/main/game/game_state.cpp b/src/main/game/game_state.cpp index faab429..ad2ac3f 100644 --- a/src/main/game/game_state.cpp +++ b/src/main/game/game_state.cpp @@ -277,21 +277,16 @@ void game_state::undo_move(const move m) { // reorders the pile refs so that the largest pile is first void game_state::place_card(pile_ref pr, card c) { piles[pr].place(c); -#ifndef ORDER_ON_CACHE eval_pile_order(pr, true); -#endif } // Same as above but for taking cards card game_state::take_card(pile_ref pr) { card c = piles[pr].take(); -#ifndef ORDER_ON_CACHE eval_pile_order(pr, false); -#endif return c; } -#ifndef ORDER_ON_CACHE // Assesses whether the pile ref that was modified was a tableau, cell or // reserve pile, and if so makes the relevant function call void game_state::eval_pile_order(pile_ref pr, bool is_place) { @@ -361,7 +356,6 @@ void game_state::eval_pile_order(list& pile_lst, pile_ref changed_pr, } #endif } -#endif //////////////////////// diff --git a/src/main/game/game_state.h b/src/main/game/game_state.h index 532eefc..27cb2c0 100644 --- a/src/main/game/game_state.h +++ b/src/main/game/game_state.h @@ -22,6 +22,7 @@ class game_state { friend class predicate; friend class hasher; friend class global_cache; + friend class cached_game_state; public: // Defines types typedef uint8_t pile_ref; @@ -58,11 +59,9 @@ private: // Private constructor explicit game_state(const sol_rules&); -#ifndef ORDER_ON_CACHE // Used to maintain pile order invariant void eval_pile_order(pile_ref, bool); void eval_pile_order(std::list&, pile_ref, bool); -#endif // Used in get_legal_moves() move get_stock_tableau_move() const; -- cgit v1.2.1 From a2665d5958ec351a79246f95596280bc3874537b Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Thu, 1 Feb 2018 12:59:45 +0000 Subject: Add game state reduction to cache --- src/main/game/global_cache.cpp | 243 ++++++++++++++++++++--------------------- src/main/game/global_cache.h | 31 +++++- 2 files changed, 146 insertions(+), 128 deletions(-) (limited to 'src') diff --git a/src/main/game/global_cache.cpp b/src/main/game/global_cache.cpp index bf55f67..976dbd7 100644 --- a/src/main/game/global_cache.cpp +++ b/src/main/game/global_cache.cpp @@ -15,11 +15,11 @@ typedef sol_rules::build_policy pol; typedef sol_rules::stock_deal_type sdt; bool global_cache::insert(const game_state& gs) { - return u_set.emplace(get_ordered_vec(gs)).second; + return u_set.emplace(gs).second; } bool global_cache::contains(const game_state& gs) const { - return u_set.count(get_ordered_vec(gs)) > 0; + return u_set.count(cached_game_state(gs)) > 0; } void global_cache::clear() { @@ -30,121 +30,21 @@ global_cache::global_cache(const game_state& gs) : u_set(0, hasher(gs), predicate(gs)) { } -std::vector global_cache::get_ordered_vec(const game_state& gs) { -#ifdef ORDER_ON_CACHE - vector ordered_vec = gs.get_data(); - if (gs.rules.cells > 0) { - sort(begin(ordered_vec) + gs.original_cells[0], - begin(ordered_vec) + gs.original_cells[0] + gs.original_cells.size()); - } - if (gs.rules.reserve_size > 0) { - sort(begin(ordered_vec) + gs.original_reserve[0], - begin(ordered_vec) + gs.original_reserve[0] + gs.original_reserve.size()); - } - if (gs.rules.tableau_pile_count > 0) { - sort(begin(ordered_vec) + gs.original_tableau_piles[0], - begin(ordered_vec) + gs.original_tableau_piles[0] + gs.original_tableau_piles.size()); - } -#else - const vector& gs_vec = gs.get_data(); - vector ordered_vec; - ordered_vec.reserve(gs_vec.size()); - - if (gs.rules.hole) - ordered_vec.push_back(gs_vec[gs.hole]); - - for (game_state::pile_ref pr : gs.foundations) - ordered_vec.push_back(gs_vec[pr]); - - for (game_state::pile_ref pr : gs.cells) - ordered_vec.push_back(gs_vec[pr]); - - if (gs.rules.stock_size > 0) { - ordered_vec.push_back(gs_vec[gs.stock]); - - if (gs.rules.stock_deal_t == sdt::WASTE) { - ordered_vec.push_back(gs_vec[gs.waste]); - } - } - - for (game_state::pile_ref pr : gs.reserve) - ordered_vec.push_back(gs_vec[pr]); - - for (game_state::pile_ref pr : gs.tableau_piles) - ordered_vec.push_back(gs_vec[pr]); - -#ifndef NDEBUG - // Makes sure the piles are in order - if (gs.original_cells.size() > 1) { - for (game_state::pile_ref pr : gs.original_cells) { - assert(ordered_vec[pr] >= ordered_vec[pr+1]); - if (pr == gs.original_cells[gs.original_cells.size()-2]) break; - } - } - if (gs.original_reserve.size() > 1) { - for (game_state::pile_ref pr : gs.original_reserve) { - assert(ordered_vec[pr] >= ordered_vec[pr+1]); - if (pr == gs.original_reserve[gs.original_reserve.size()-2]) break; - } - } - if (gs.original_tableau_piles.size() > 1) { - for (game_state::pile_ref pr : gs.original_tableau_piles) { - assert(ordered_vec[pr] >= ordered_vec[pr+1]); - if (pr == gs.original_tableau_piles[gs.original_tableau_piles.size()-2]) break; - } - } -#endif -#endif - - return ordered_vec; -} - hasher::hasher(const game_state& gs) : init_gs(gs) { } -size_t hasher::operator()(const vector& piles) const { +size_t hasher::operator()(const cached_game_state& cgs) const { size_t seed = 0; -#ifdef SIMPLE_HASH - for (game_state::pile_ref pr = 0; pr < piles.size(); pr++) { - seed += combine(seed, hash_value(piles[pr])); - } +#ifdef NO_REDUCED_STATE + for (const pile& d : cgs.data) { #else - // Using addition for commutative hash - for (game_state::pile_ref pr : init_gs.tableau_piles) { - combine_commutative(seed, hash_value(piles[pr])); - } - - // Using hash_combine for order-dependent hash - for (game_state::pile_ref pr : init_gs.foundations) { - combine(seed, hash_value(piles[pr])); - } - - // Commutative - for (game_state::pile_ref pr : init_gs.reserve) { - combine_commutative(seed, hash_value(piles[pr])); - } - - if (init_gs.rules.stock_size > 0) { - combine(seed, hash_value(piles[init_gs.stock])); - - if (init_gs.rules.stock_deal_t == sdt::WASTE) { - combine(seed, hash_value(piles[init_gs.waste])); - } - } - - if (init_gs.rules.hole) { - combine(seed, hash_value(piles[init_gs.hole])); - } - - // Commutative - for (game_state::pile_ref pr : init_gs.cells) { - combine_commutative(seed, hash_value(piles[pr])); - } + for (card d : cgs.data) { #endif - + combine(seed, hash_value(d)); + } return seed; } @@ -161,10 +61,6 @@ std::size_t hasher::combine(std::size_t& seed, std::size_t value) const { return seed ^= value + 0x9e3779b9 + (seed<<6) + (seed>>2); } -std::size_t hasher::combine_commutative(std::size_t& seed, std::size_t value) const { - return seed += value; -} - size_t hasher::hash_value(card const& c) const { boost::hash boost_hasher; @@ -191,20 +87,21 @@ size_t hasher::hash_value(card const& c) const { predicate::predicate(const game_state& gs) : init_gs(gs) { } -bool predicate::operator()(const vector& x_piles, - const vector& y_piles) const { - 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; +bool predicate::operator()(const cached_game_state& a, + const cached_game_state& b) const { +#ifdef NO_REDUCED_STATE + for (vector::size_type pile_idx = 0; pile_idx < a.data.size(); pile_idx++) { + if (a.data[pile_idx].size() != b.data[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]; + for (auto card_idx = 0; card_idx < a.data[pile_idx].size(); card_idx++) { + card cx = a.data[pile_idx][card_idx]; + card cy = b.data[pile_idx][card_idx]; if (cx.get_rank() != cy.get_rank()) return false; -#ifdef SIMPLE_HASH +# ifdef SIMPLE_HASH if (cx.get_suit() != cy.get_suit()) return false; -#else +# else switch (init_gs.rules.build_pol) { case pol::SAME_SUIT: if (cx.get_suit() != cy.get_suit()) return false; @@ -215,9 +112,111 @@ bool predicate::operator()(const vector& x_piles, default: break; } -#endif +# endif } } +#else + for (cached_game_state::size_type i = 0; i < a.data.size(); i++) { + if (a.data[i] != b.data[i]) return false; + } +#endif return true; } + +cached_game_state::cached_game_state(const game_state& gs) { +#ifdef NO_REDUCED_STATE + const vector& gs_vec = gs.get_data(); + + if (gs.rules.hole) + data.push_back(gs_vec[gs.hole]); + + for (game_state::pile_ref pr : gs.foundations) + data.push_back(gs_vec[pr]); + + for (game_state::pile_ref pr : gs.cells) + data.push_back(gs_vec[pr]); + + if (gs.rules.stock_size > 0) { + data.push_back(gs_vec[gs.stock]); + + if (gs.rules.stock_deal_t == sdt::WASTE) { + data.push_back(gs_vec[gs.waste]); + } + } + + for (game_state::pile_ref pr : gs.reserve) + data.push_back(gs_vec[pr]); + + for (game_state::pile_ref pr : gs.tableau_piles) + data.push_back(gs_vec[pr]); +#else + if (gs.rules.hole) { + add_card(gs.piles[gs.hole].top_card(), gs); + } + + // No need to add foundations, as they are implicit in the other piles + + for (game_state::pile_ref pr : gs.cells) { + add_pile(gs.piles[pr], gs); + } + if (gs.rules.cells > 0) { + add_card_divider(); + } + + if (gs.rules.stock_size > 0) { + add_pile(gs.piles[gs.stock], gs); + add_card_divider(); + + if (gs.rules.stock_deal_t == sdt::WASTE) { + add_pile(gs.piles[gs.waste], gs); + add_card_divider(); + } + } + + for (game_state::pile_ref pr : gs.reserve) { + add_pile(gs.piles[pr], gs); + } + if (gs.rules.reserve_size > 0) { + add_card_divider(); + } + + for (game_state::pile_ref pr : gs.tableau_piles) { + add_pile(gs.piles[pr], gs); + add_card_divider(); + } +#endif +} + + +#ifndef NO_REDUCED_STATE +void cached_game_state::add_pile(const pile& p, const game_state& gs) { + for (card c : p.pile_vec) { + add_card(c, gs); + } +} + +void cached_game_state::add_card(card c, const game_state& gs) { + switch (gs.rules.build_pol) { +# ifdef SIMPLE_HASH + default: + data.emplace_back(c); + break; +# else + case pol::SAME_SUIT: + data.emplace_back(c); + break; + case pol::RED_BLACK: + data.emplace_back(c.get_colour(), c.get_rank()); + break; + default: + data.emplace_back(0, c.get_rank()); + break; +# endif + } +} + +void cached_game_state::add_card_divider() { + data.emplace_back(0, 0); +} +#endif diff --git a/src/main/game/global_cache.h b/src/main/game/global_cache.h index 17585ed..14691af 100644 --- a/src/main/game/global_cache.h +++ b/src/main/game/global_cache.h @@ -7,26 +7,47 @@ #include #include +#include +#include #include "sol_rules.h" #include "game_state.h" +struct cached_game_state { + typedef std::vector< +#ifdef NO_REDUCED_STATE + pile +#else + card +#endif + > state_data; + typedef state_data::size_type size_type; + + explicit cached_game_state(const game_state&); +#ifndef NO_REDUCED_STATE + void add_pile(const pile&, const game_state&); + void add_card(card, const game_state&); + void add_card_divider(); +#endif + + state_data data; +}; + class predicate { public: explicit predicate(const game_state&); - bool operator() (const std::vector&, const std::vector&) const; + bool operator() (const cached_game_state&, const cached_game_state&) const; private: const game_state& init_gs; }; struct hasher { explicit hasher(const game_state&); - std::size_t operator() (const std::vector&) const; + std::size_t operator() (const cached_game_state&) const; std::size_t hash_value(const card&) const; std::size_t hash_value(const pile&) const; std::size_t combine(std::size_t&, std::size_t) const; - std::size_t combine_commutative(std::size_t&, std::size_t) const; const game_state& init_gs; }; @@ -39,10 +60,8 @@ public: void clear(); private: - static std::vector get_ordered_vec(const game_state&); - std::unordered_set< - std::vector, + cached_game_state, hasher, predicate > u_set; -- cgit v1.2.1 From d70981a3ab3fe37928f39c3d822ad5f775292a78 Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Thu, 1 Feb 2018 13:00:16 +0000 Subject: Update tests to use new reduced game state --- src/test/test_helper.cpp | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/test/test_helper.cpp b/src/test/test_helper.cpp index 22499de..9ee1233 100644 --- a/src/test/test_helper.cpp +++ b/src/test/test_helper.cpp @@ -52,21 +52,24 @@ void test_helper::run_card_cache_test(sol_rules::build_policy bp, ASSERT_TRUE(cache.contains(s)) << "Contains self"; - ASSERT_(assert_dif_col, hasher(s)(s.get_data()) == hasher(s)(s_dif_suit.get_data())) + ASSERT_(assert_dif_col, hasher(s)(cached_game_state(s)) + == hasher(s)(cached_game_state(s_dif_suit))) << "Insert: " << c.to_string() << ", Comp: " << c_dif_col.to_string(); ASSERT_(assert_dif_col, cache.contains(s_dif_suit)) << "Insert: " << c.to_string() << ", Comp: " << c_dif_col.to_string(); - ASSERT_(assert_same_col, hasher(s)(s.get_data()) == hasher(s)(s_same_col.get_data())) + ASSERT_(assert_same_col, hasher(s)(cached_game_state(s)) + == hasher(s)(cached_game_state(s_same_col))) << "Insert: " << c.to_string() << ", Comp: " << c_same_col.to_string(); ASSERT_(assert_same_col, cache.contains(s_same_col)) << "Insert: " << c.to_string() << ", Comp: " << c_same_col.to_string(); - ASSERT_FALSE(hasher(s)(s.get_data()) == hasher(s)(s_dif_rank.get_data())) + ASSERT_FALSE(hasher(s)(cached_game_state(s)) + == hasher(s)(cached_game_state(s_dif_rank))) << "Insert: " << c.to_string() << ", Comp: " << c_dif_rank.to_string(); ASSERT_FALSE(cache.contains(s_dif_rank)) -- cgit v1.2.1 From 009b1baee478243a9158d4fbd6110ba19d5a0a4d Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Fri, 2 Feb 2018 13:02:23 +0000 Subject: Remove unnecessary get_data() from game_state --- src/main/game/game_state.cpp | 4 ---- src/main/game/game_state.h | 1 - 2 files changed, 5 deletions(-) (limited to 'src') diff --git a/src/main/game/game_state.cpp b/src/main/game/game_state.cpp index ad2ac3f..097c744 100644 --- a/src/main/game/game_state.cpp +++ b/src/main/game/game_state.cpp @@ -404,10 +404,6 @@ bool game_state::is_solved() const { return solved; } -const vector& game_state::get_data() const { - return piles; -} - /////////// // PRINT // diff --git a/src/main/game/game_state.h b/src/main/game/game_state.h index 27cb2c0..9b0d7b5 100644 --- a/src/main/game/game_state.h +++ b/src/main/game/game_state.h @@ -49,7 +49,6 @@ public: // Inspects state bool is_solved() const; - const std::vector& get_data() const; friend std::ostream& operator<< (std::ostream&, const game_state&); -- cgit v1.2.1 From ac01aa9a755874e538fe5aadbdaa670f5d292ebc Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Fri, 2 Feb 2018 13:02:46 +0000 Subject: Simplify flagged versions of cached state --- src/main/game/global_cache.cpp | 247 ++++++++++++++++++----------------------- src/main/game/global_cache.h | 18 +-- 2 files changed, 115 insertions(+), 150 deletions(-) (limited to 'src') diff --git a/src/main/game/global_cache.cpp b/src/main/game/global_cache.cpp index 976dbd7..37738f2 100644 --- a/src/main/game/global_cache.cpp +++ b/src/main/game/global_cache.cpp @@ -14,24 +14,110 @@ using namespace boost; typedef sol_rules::build_policy pol; typedef sol_rules::stock_deal_type sdt; -bool global_cache::insert(const game_state& gs) { - return u_set.emplace(gs).second; + +/////////////////////// +// CACHED GAME STATE // +/////////////////////// + +cached_game_state::cached_game_state(const game_state& gs) { + if (gs.rules.hole) { +#ifdef NO_REDUCED_STATE + add_pile(gs.hole, gs); +#else + add_card(gs.piles[gs.hole].top_card(), gs); +#endif + } + + // No need to add foundations for reduced state, as they are implicit in the + // other piles +#ifdef NO_REDUCED_STATE + for (game_state::pile_ref pr : gs.foundations) + add_pile(pr, gs); +#endif + + for (game_state::pile_ref pr : gs.cells) { + add_pile(pr, gs); + } + if (gs.rules.cells > 0) { + add_card_divider(); + } + + if (gs.rules.stock_size > 0) { + add_pile(gs.stock, gs); + add_card_divider(); + + if (gs.rules.stock_deal_t == sdt::WASTE) { + add_pile(gs.waste, gs); + add_card_divider(); + } + } + + for (game_state::pile_ref pr : gs.reserve) { + add_pile(pr, gs); + } + if (gs.rules.reserve_size > 0) { + add_card_divider(); + } + + for (game_state::pile_ref pr : gs.tableau_piles) { + add_pile(pr, gs); + add_card_divider(); + } } -bool global_cache::contains(const game_state& gs) const { - return u_set.count(cached_game_state(gs)) > 0; + +void cached_game_state::add_pile(game_state::pile_ref pr, const game_state& gs) { +#ifdef NO_REDUCED_STATE + data.emplace_back(pile()); +#endif + for (card c : gs.piles[pr].pile_vec) { + add_card(c, gs); + } } -void global_cache::clear() { - u_set.clear(); +void cached_game_state::add_card(card c, const game_state& gs) { + +#ifdef NO_REDUCED_STATE + auto& target = data.back().pile_vec; +#else + auto& target = data; +#endif + + switch (gs.rules.build_pol) { +#ifdef SIMPLE_HASH + default: + target.emplace_back(c); + break; +#else + case pol::SAME_SUIT: + target.emplace_back(c); + break; + case pol::RED_BLACK: + target.emplace_back(c.get_colour(), c.get_rank()); + break; + default: + target.emplace_back(0, c.get_rank()); + break; +#endif + } } -global_cache::global_cache(const game_state& gs) - : u_set(0, hasher(gs), predicate(gs)) { +void cached_game_state::add_card_divider() { +#ifndef NO_REDUCED_STATE + data.emplace_back(0, 0); +#endif } +bool operator==(const cached_game_state& a, const cached_game_state& b) { + return a.data == b.data; +} + +////////////////// +// STATE HASHER // +////////////////// + hasher::hasher(const game_state& gs) : init_gs(gs) { } @@ -48,6 +134,7 @@ size_t hasher::operator()(const cached_game_state& cgs) const { return seed; } +#ifdef NO_REDUCED_STATE size_t hasher::hash_value(pile const& p) const { std::size_t seed = 0; @@ -56,6 +143,7 @@ size_t hasher::hash_value(pile const& p) const { } return seed; } +#endif std::size_t hasher::combine(std::size_t& seed, std::size_t value) const { return seed ^= value + 0x9e3779b9 + (seed<<6) + (seed>>2); @@ -65,15 +153,15 @@ size_t hasher::hash_value(card const& c) const { boost::hash boost_hasher; #ifdef SIMPLE_HASH - uint8_t suit_val = static_cast(c.get_suit()); + uint8_t suit_val = c.get_suit(); #else uint8_t suit_val; switch (init_gs.rules.build_pol) { case pol::SAME_SUIT: - suit_val = static_cast(c.get_suit()); + suit_val = c.get_suit(); break; case pol::RED_BLACK: - suit_val = static_cast(c.get_colour()); + suit_val = c.get_colour(); break; default: suit_val = 0; @@ -84,139 +172,24 @@ size_t hasher::hash_value(card const& c) const { return boost_hasher(raw_val); } -predicate::predicate(const game_state& gs) : init_gs(gs) { -} - -bool predicate::operator()(const cached_game_state& a, - const cached_game_state& b) const { -#ifdef NO_REDUCED_STATE - for (vector::size_type pile_idx = 0; pile_idx < a.data.size(); pile_idx++) { - if (a.data[pile_idx].size() != b.data[pile_idx].size()) return false; - - for (auto card_idx = 0; card_idx < a.data[pile_idx].size(); card_idx++) { - card cx = a.data[pile_idx][card_idx]; - card cy = b.data[pile_idx][card_idx]; - - if (cx.get_rank() != cy.get_rank()) return false; - -# ifdef SIMPLE_HASH - if (cx.get_suit() != cy.get_suit()) return false; -# else - switch (init_gs.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; - } -# endif - } - } -#else - for (cached_game_state::size_type i = 0; i < a.data.size(); i++) { - if (a.data[i] != b.data[i]) return false; - } -#endif - - return true; -} -cached_game_state::cached_game_state(const game_state& gs) { -#ifdef NO_REDUCED_STATE - const vector& gs_vec = gs.get_data(); - - if (gs.rules.hole) - data.push_back(gs_vec[gs.hole]); - - for (game_state::pile_ref pr : gs.foundations) - data.push_back(gs_vec[pr]); - for (game_state::pile_ref pr : gs.cells) - data.push_back(gs_vec[pr]); +////////////////// +// GLOBAL CACHE // +////////////////// - if (gs.rules.stock_size > 0) { - data.push_back(gs_vec[gs.stock]); - - if (gs.rules.stock_deal_t == sdt::WASTE) { - data.push_back(gs_vec[gs.waste]); - } - } - - for (game_state::pile_ref pr : gs.reserve) - data.push_back(gs_vec[pr]); - - for (game_state::pile_ref pr : gs.tableau_piles) - data.push_back(gs_vec[pr]); -#else - if (gs.rules.hole) { - add_card(gs.piles[gs.hole].top_card(), gs); - } - - // No need to add foundations, as they are implicit in the other piles - - for (game_state::pile_ref pr : gs.cells) { - add_pile(gs.piles[pr], gs); - } - if (gs.rules.cells > 0) { - add_card_divider(); - } - - if (gs.rules.stock_size > 0) { - add_pile(gs.piles[gs.stock], gs); - add_card_divider(); - - if (gs.rules.stock_deal_t == sdt::WASTE) { - add_pile(gs.piles[gs.waste], gs); - add_card_divider(); - } - } - - for (game_state::pile_ref pr : gs.reserve) { - add_pile(gs.piles[pr], gs); - } - if (gs.rules.reserve_size > 0) { - add_card_divider(); - } - - for (game_state::pile_ref pr : gs.tableau_piles) { - add_pile(gs.piles[pr], gs); - add_card_divider(); - } -#endif +bool global_cache::insert(const game_state& gs) { + return u_set.emplace(gs).second; } - -#ifndef NO_REDUCED_STATE -void cached_game_state::add_pile(const pile& p, const game_state& gs) { - for (card c : p.pile_vec) { - add_card(c, gs); - } +bool global_cache::contains(const game_state& gs) const { + return u_set.count(cached_game_state(gs)) > 0; } -void cached_game_state::add_card(card c, const game_state& gs) { - switch (gs.rules.build_pol) { -# ifdef SIMPLE_HASH - default: - data.emplace_back(c); - break; -# else - case pol::SAME_SUIT: - data.emplace_back(c); - break; - case pol::RED_BLACK: - data.emplace_back(c.get_colour(), c.get_rank()); - break; - default: - data.emplace_back(0, c.get_rank()); - break; -# endif - } +void global_cache::clear() { + u_set.clear(); } -void cached_game_state::add_card_divider() { - data.emplace_back(0, 0); +global_cache::global_cache(const game_state& gs) + : u_set(0, hasher(gs)) { } -#endif diff --git a/src/main/game/global_cache.h b/src/main/game/global_cache.h index 14691af..4658e52 100644 --- a/src/main/game/global_cache.h +++ b/src/main/game/global_cache.h @@ -24,29 +24,22 @@ struct cached_game_state { typedef state_data::size_type size_type; explicit cached_game_state(const game_state&); -#ifndef NO_REDUCED_STATE - void add_pile(const pile&, const game_state&); + void add_pile(game_state::pile_ref, const game_state&); void add_card(card, const game_state&); void add_card_divider(); -#endif state_data data; }; - -class predicate { -public: - explicit predicate(const game_state&); - bool operator() (const cached_game_state&, const cached_game_state&) const; -private: - const game_state& init_gs; -}; +bool operator==(const cached_game_state&, const cached_game_state&); struct hasher { explicit hasher(const game_state&); std::size_t operator() (const cached_game_state&) const; std::size_t hash_value(const card&) const; +#ifdef NO_REDUCED_STATE std::size_t hash_value(const pile&) const; +#endif std::size_t combine(std::size_t&, std::size_t) const; const game_state& init_gs; @@ -62,8 +55,7 @@ public: private: std::unordered_set< cached_game_state, - hasher, - predicate + hasher > u_set; }; -- cgit v1.2.1