aboutsummaryrefslogtreecommitdiff
path: root/src/main/game/global_cache.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/game/global_cache.cpp')
-rw-r--r--src/main/game/global_cache.cpp197
1 files changed, 131 insertions, 66 deletions
diff --git a/src/main/game/global_cache.cpp b/src/main/game/global_cache.cpp
index 8e3aa8b..bf55f67 100644
--- a/src/main/game/global_cache.cpp
+++ b/src/main/game/global_cache.cpp
@@ -4,6 +4,8 @@
#include <algorithm>
+#include <boost/functional/hash.hpp>
+
#include "global_cache.h"
using namespace std;
@@ -13,121 +15,184 @@ typedef sol_rules::build_policy pol;
typedef sol_rules::stock_deal_type sdt;
bool global_cache::insert(const game_state& gs) {
- return u_set.insert(gs).second;
+ return u_set.emplace(get_ordered_vec(gs)).second;
}
bool global_cache::contains(const game_state& gs) const {
- return u_set.count(gs) > 0;
+ return u_set.count(get_ordered_vec(gs)) > 0;
}
void global_cache::clear() {
u_set.clear();
}
-size_t hash_value(card const& c) {
- boost::hash<uint8_t> hasher;
+global_cache::global_cache(const game_state& gs)
+ : u_set(0, hasher(gs), predicate(gs)) {
+}
-#ifdef SIMPLE_HASH
- uint8_t suit_val = static_cast<uint8_t>(c.get_suit());
+std::vector<pile> global_cache::get_ordered_vec(const game_state& gs) {
+#ifdef ORDER_ON_CACHE
+ vector<pile> 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
- uint8_t suit_val;
- switch (game_state::rules.build_pol) {
- case pol::SAME_SUIT:
- suit_val = static_cast<uint8_t>(c.get_suit());
- break;
- case pol::RED_BLACK:
- suit_val = static_cast<uint8_t>(c.get_colour());
- break;
- default:
- suit_val = 0;
+ const vector<pile>& gs_vec = gs.get_data();
+ vector<pile> 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
- auto raw_val = static_cast<uint8_t>(suit_val * 13 + c.get_rank());
- return hasher(raw_val);
+ return ordered_vec;
}
-size_t hash_value(pile const& p) {
- return hash_range(begin(p.pile_vec), end(p.pile_vec));
+
+
+hasher::hasher(const game_state& gs) : init_gs(gs) {
}
-size_t hash_value(game_state const& gs) {
+size_t hasher::operator()(const vector<pile>& piles) const {
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]);
+ for (game_state::pile_ref pr = 0; pr < piles.size(); pr++) {
+ seed += combine(seed, hash_value(piles[pr]));
}
#else
- boost::hash<pile> pile_hasher;
-
// Using addition for commutative hash
- for (game_state::pile_ref pr : game_state::tableau_piles) {
- seed += pile_hasher(gs.piles[pr]);
+ 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 : game_state::foundations) {
- hash_combine(seed, gs.piles[pr]);
+ for (game_state::pile_ref pr : init_gs.foundations) {
+ combine(seed, hash_value(piles[pr]));
}
// Commutative
- for (game_state::pile_ref pr : game_state::reserve) {
- seed += pile_hasher(gs.piles[pr]);
+ for (game_state::pile_ref pr : init_gs.reserve) {
+ combine_commutative(seed, hash_value(piles[pr]));
}
- if (game_state::rules.stock_size > 0) {
- hash_combine(seed, gs.piles[game_state::stock]);
- }
+ if (init_gs.rules.stock_size > 0) {
+ combine(seed, hash_value(piles[init_gs.stock]));
- if (game_state::rules.stock_deal_t == sdt::WASTE) {
- hash_combine(seed, gs.piles[game_state::waste]);
+ if (init_gs.rules.stock_deal_t == sdt::WASTE) {
+ combine(seed, hash_value(piles[init_gs.waste]));
+ }
}
- if (game_state::rules.hole) {
- hash_combine(seed, gs.piles[game_state::hole]);
+ if (init_gs.rules.hole) {
+ combine(seed, hash_value(piles[init_gs.hole]));
}
// Commutative
- for (game_state::pile_ref pr : game_state::cells) {
- seed += pile_hasher(gs.piles[pr]);
+ for (game_state::pile_ref pr : init_gs.cells) {
+ combine_commutative(seed, hash_value(piles[pr]));
}
#endif
return seed;
}
-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();
- }
- // Larger card values first
- for (pile::size_type pile_idx = 0; pile_idx < x.size(); pile_idx++) {
- auto x_val = static_cast<uint8_t>(x[pile_idx].get_suit()) * 13
- + x[pile_idx].get_rank();
- auto y_val = static_cast<uint8_t>(y[pile_idx].get_suit()) * 13
- + y[pile_idx].get_rank();
- if (x_val != y_val) {
- return x_val > y_val;
- }
+size_t hasher::hash_value(pile const& p) const {
+ std::size_t seed = 0;
+
+ for(auto first = begin(p.pile_vec); first != end(p.pile_vec); ++first) {
+ combine(seed, hash_value(*first));
}
- // It is a requirement of std::sort that equal values return false
- return false;
+ return seed;
+}
+
+std::size_t hasher::combine(std::size_t& seed, std::size_t value) const {
+ return seed ^= value + 0x9e3779b9 + (seed<<6) + (seed>>2);
}
-bool game_state_pred::operator()(const game_state& x,
- const game_state& y) const {
- if (x.piles.size() != y.piles.size()) return false;
+std::size_t hasher::combine_commutative(std::size_t& seed, std::size_t value) const {
+ return seed += value;
+}
- vector<pile> x_piles = x.piles;
- vector<pile> y_piles = y.piles;
+size_t hasher::hash_value(card const& c) const {
+ boost::hash<uint8_t> boost_hasher;
- // 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);
+#ifdef SIMPLE_HASH
+ uint8_t suit_val = static_cast<uint8_t>(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());
+ break;
+ case pol::RED_BLACK:
+ suit_val = static_cast<uint8_t>(c.get_colour());
+ break;
+ default:
+ suit_val = 0;
+ }
+#endif
+
+ auto raw_val = static_cast<uint8_t>(suit_val * 13 + c.get_rank());
+ return boost_hasher(raw_val);
+}
+
+predicate::predicate(const game_state& gs) : init_gs(gs) {
+}
- // Compares the two for equality
+bool predicate::operator()(const vector<pile>& x_piles,
+ const vector<pile>& y_piles) const {
for (vector<pile>::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;
@@ -140,7 +205,7 @@ bool game_state_pred::operator()(const game_state& x,
#ifdef SIMPLE_HASH
if (cx.get_suit() != cy.get_suit()) return false;
#else
- switch (game_state::rules.build_pol) {
+ switch (init_gs.rules.build_pol) {
case pol::SAME_SUIT:
if (cx.get_suit() != cy.get_suit()) return false;
break;