diff options
| author | thecharlesblake | 2018-02-02 13:02:46 +0000 |
|---|---|---|
| committer | thecharlesblake | 2018-02-02 13:02:46 +0000 |
| commit | ac01aa9a755874e538fe5aadbdaa670f5d292ebc (patch) | |
| tree | 9cdad089f7e2fee94cbb75505c711b25111bf49a /src/main | |
| parent | Remove unnecessary get_data() from game_state (diff) | |
| download | metasol-ac01aa9a755874e538fe5aadbdaa670f5d292ebc.tar.gz metasol-ac01aa9a755874e538fe5aadbdaa670f5d292ebc.zip | |
Simplify flagged versions of cached state
Diffstat (limited to 'src/main')
| -rw-r--r-- | src/main/game/global_cache.cpp | 247 | ||||
| -rw-r--r-- | src/main/game/global_cache.h | 18 |
2 files changed, 115 insertions, 150 deletions
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<uint8_t> boost_hasher; #ifdef SIMPLE_HASH - uint8_t suit_val = static_cast<uint8_t>(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<uint8_t>(c.get_suit()); + suit_val = c.get_suit(); break; case pol::RED_BLACK: - suit_val = static_cast<uint8_t>(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<pile>::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<pile>& 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; }; |
