aboutsummaryrefslogtreecommitdiff
path: root/src/main/game/global_cache.cpp
diff options
context:
space:
mode:
authorthecharlesblake2018-01-12 17:55:56 +0000
committerthecharlesblake2018-01-12 17:55:56 +0000
commit01083ef011143eef36f8f83ecbdae8da6e948021 (patch)
tree8192d4c8e6e9a61c44c8f96be2dd7d7b3ff395b0 /src/main/game/global_cache.cpp
parentAdd unit tests for global cache (diff)
downloadmetasol-01083ef011143eef36f8f83ecbdae8da6e948021.tar.gz
metasol-01083ef011143eef36f8f83ecbdae8da6e948021.zip

Implement symmetrical hash for cards

Diffstat (limited to 'src/main/game/global_cache.cpp')
-rw-r--r--src/main/game/global_cache.cpp28
1 files changed, 27 insertions, 1 deletions
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>& pile_vec) {
return u_set.insert(pile_vec).second;
@@ -14,3 +18,25 @@ bool global_cache::insert(const std::vector<pile>& pile_vec) {
bool global_cache::contains(const std::vector<pile>& pile_vec) {
return u_set.count(pile_vec) > 0;
}
+
+size_t hash_value(card const& c) {
+ boost::hash<unsigned char> hasher;
+
+ auto suit_val;
+ switch (global_cache::rules.build_pol) {
+ case pol::SAME_SUIT:
+ suit_val = static_cast<std::underlying_type_t<card::suit_t>>(
+ c.get_suit());
+ break;
+ case pol::RED_BLACK:
+ suit_val = static_cast<std::underlying_type_t<card::colour_t >>(
+ c.get_colour());
+ break;
+ default:
+ suit_val = 0;
+ break;
+ }
+
+ auto raw_val = static_cast<unsigned char>(suit_val * 13 + c.get_rank());
+ return hasher(raw_val);
+}