aboutsummaryrefslogtreecommitdiff
path: root/src/main/game
diff options
context:
space:
mode:
authorCharlie Blake2017-12-25 22:39:46 +0000
committerGitHub2017-12-25 22:39:46 +0000
commit846b3d141352a492a5b8e0f61a2707b8feacbcc6 (patch)
tree3e10925da099bac8deb71e90d111fad2cd7abb2b /src/main/game
parentMerge pull request #23 from thecharlesblake/benchmark-script (diff)
parentAdd necessary features for profiler (diff)
downloadmetasol-846b3d141352a492a5b8e0f61a2707b8feacbcc6.tar.gz
metasol-846b3d141352a492a5b8e0f61a2707b8feacbcc6.zip

Merge pull request #24 from thecharlesblake/basic-optimisations

Basic optimisations

Diffstat (limited to 'src/main/game')
-rw-r--r--src/main/game/card.cpp11
-rw-r--r--src/main/game/card.h1
-rw-r--r--src/main/game/game_state.cpp37
-rw-r--r--src/main/game/game_state.h2
-rw-r--r--src/main/game/pile.cpp7
-rw-r--r--src/main/game/pile.h1
6 files changed, 57 insertions, 2 deletions
diff --git a/src/main/game/card.cpp b/src/main/game/card.cpp
index 9f7dbdf..f5e9acb 100644
--- a/src/main/game/card.cpp
+++ b/src/main/game/card.cpp
@@ -4,9 +4,12 @@
#include <sstream>
+#include <boost/functional/hash.hpp>
+
#include "card.h"
using namespace std;
+using namespace boost;
// A card can either be created using two int values (rank & suit), or
// using a string (e.g. "AS")
@@ -89,4 +92,12 @@ std::ostream & operator<<(std::ostream & s, card const & c) {
s << c.get_suit();
return s;
+}
+
+size_t hash_value(card const& c) {
+ boost::hash<unsigned char> hasher;
+ unsigned char raw_val = static_cast<unsigned char>(
+ c.get_suit_val() * 13 + c.get_rank());
+
+ return hasher(raw_val);
} \ No newline at end of file
diff --git a/src/main/game/card.h b/src/main/game/card.h
index 1cac344..b518485 100644
--- a/src/main/game/card.h
+++ b/src/main/game/card.h
@@ -26,6 +26,7 @@ private:
std::ostream & operator<<(std::ostream &, card const &);
bool operator==(const card&, const card&);
+std::size_t hash_value(card const&);
#endif //SOLITAIRE_PIP_CARD_H
diff --git a/src/main/game/game_state.cpp b/src/main/game/game_state.cpp
index 1814a41..2b77fec 100644
--- a/src/main/game/game_state.cpp
+++ b/src/main/game/game_state.cpp
@@ -9,11 +9,13 @@
#include <functional>
#include <rapidjson/document.h>
+#include <boost/functional/hash.hpp>
#include "game_state.h"
-using namespace rapidjson;
using namespace std;
+using namespace rapidjson;
+using namespace boost;
typedef sol_rules::build_order ord;
typedef sol_rules::build_policy pol;
@@ -324,4 +326,35 @@ bool operator==(const game_state& a, const game_state& b) {
ostream& operator <<(ostream& stream, const game_state& gs) {
return gs.print(stream);
-} \ No newline at end of file
+}
+
+size_t hash_value(game_state const& gs) {
+ size_t seed = 0;
+
+ if (gs.rules.foundations) {
+ hash_combine(seed, gs.foundations);
+ }
+ if (gs.rules.cells) {
+ hash_combine(seed, gs.cells);
+ }
+ if (gs.rules.tableau_pile_count > 0) {
+ hash_combine(seed, gs.tableau_piles);
+ }
+ if (gs.rules.reserve_size > 0) {
+ hash_combine(seed, gs.reserve);
+ }
+ if (gs.rules.stock_size > 0) {
+ hash_combine(seed, gs.stock);
+ hash_combine(seed, gs.waste);
+ }
+ if (gs.rules.hole) {
+ hash_combine(seed, gs.hole);
+ }
+
+ return seed;
+}
+
+size_t hash_value(vector<pile> const& vp) {
+ return hash_range(begin(vp), end(vp));
+}
+
diff --git a/src/main/game/game_state.h b/src/main/game/game_state.h
index 12dcda9..873ca6f 100644
--- a/src/main/game/game_state.h
+++ b/src/main/game/game_state.h
@@ -29,6 +29,8 @@ public:
friend bool operator==(const game_state&, const game_state&);
friend std::ostream& operator<< (std::ostream&, const game_state&);
+ friend std::size_t hash_value(game_state const&);
+ friend std::size_t hash_value(std::vector<pile> const&);
private:
static std::vector<card> shuffled_deck(int, int);
diff --git a/src/main/game/pile.cpp b/src/main/game/pile.cpp
index bebd1f6..1ce7c01 100644
--- a/src/main/game/pile.cpp
+++ b/src/main/game/pile.cpp
@@ -5,9 +5,12 @@
#include <vector>
#include <ostream>
+#include <boost/functional/hash.hpp>
+
#include "pile.h"
using namespace std;
+using namespace boost;
typedef sol_rules::build_order ord;
typedef sol_rules::build_policy pol;
@@ -143,3 +146,7 @@ void pile::clear() {
bool operator==(const pile& a, const pile& b) {
return a.pile_vec == b.pile_vec;
}
+
+size_t hash_value(pile const& p) {
+ return hash_range(begin(p.pile_vec), end(p.pile_vec));
+}
diff --git a/src/main/game/pile.h b/src/main/game/pile.h
index 6482e0c..1922c73 100644
--- a/src/main/game/pile.h
+++ b/src/main/game/pile.h
@@ -38,6 +38,7 @@ public:
card operator[] (std::vector<card>::size_type) const;
friend bool operator==(const pile&, const pile&);
+ friend std::size_t hash_value(pile const&);
private:
bool one_lt(int, int) const;