From fb20d1289ce87234bfae2cb8d784d9edf1d3ff16 Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Wed, 15 Nov 2017 21:22:40 +0000 Subject: Change history to boost multi-index map --- src/main/game/card.cpp | 11 +++++++++++ src/main/game/card.h | 1 + src/main/game/game_state.cpp | 37 +++++++++++++++++++++++++++++++++++-- src/main/game/game_state.h | 2 ++ src/main/game/pile.cpp | 7 +++++++ src/main/game/pile.h | 1 + src/main/solver/solver.cpp | 20 +++++++++----------- src/main/solver/solver.h | 17 +++++++++++++++-- 8 files changed, 81 insertions(+), 15 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 +#include + #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 hasher; + unsigned char raw_val = static_cast( + 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 #include +#include #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 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 const&); private: static std::vector 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 #include +#include + #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::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; diff --git a/src/main/solver/solver.cpp b/src/main/solver/solver.cpp index f412d03..a4933b2 100644 --- a/src/main/solver/solver.cpp +++ b/src/main/solver/solver.cpp @@ -9,8 +9,6 @@ #include "solver.h" #include "input-output/log_helper.h" -#include "game/game_state.h" -#include using namespace std; using namespace boost; @@ -21,13 +19,13 @@ solver::solver(const game_state gs, const sol_rules& sr) solver::node::node(const node* parent, const game_state& gs) : history(solver::node::gen_history(parent)), state(gs) {} -const vector solver::node::gen_history(const node* parent) { - vector hist; +const solver::node_history solver::node::gen_history(const node* parent) { + node_history hist; if (parent != NULL) { - hist = std::vector(parent->history); - hist.push_back(parent->state); - } else { - hist = vector(); + copy (begin(parent->history.get<0>()), + end(parent->history.get<0>()), + back_inserter(hist.get<0>())); + hist.get<0>().push_back(parent->state); } return hist; } @@ -51,8 +49,8 @@ const optional solver::run() { for (auto it = new_children.rbegin(); it != new_children.rend(); it++) { // If we have seen the state before, ignore it (loop detection)) - if (find(begin(current.history), end(current.history), *it) - != end(current.history)) { + if (current.history.get<1>().find(*it) + != current.history.get<1>().end()) { continue; } @@ -77,7 +75,7 @@ int solver::get_states_searched() const { } std::ostream& operator<< (std::ostream& stream, const solver::node& solution) { - for (auto state : solution.history) { + for (auto state : solution.history.get<0>()) { stream << state << "\n"; } stream << solution.state << "\n"; diff --git a/src/main/solver/solver.h b/src/main/solver/solver.h index 91c93d6..73e5e24 100644 --- a/src/main/solver/solver.h +++ b/src/main/solver/solver.h @@ -9,18 +9,30 @@ #include #include +#include +#include +#include +#include #include "game/game_state.h" #include "game/sol_rules.h" class solver { public: + typedef boost::multi_index::multi_index_container< + game_state, + boost::multi_index::indexed_by< + boost::multi_index::sequenced<>, + boost::multi_index::hashed_unique> + > + > node_history; + struct node { node(const node*, const game_state&); - std::vector history; + node_history history; game_state state; - static const std::vector gen_history(const node* parent); + static const node_history gen_history(const node* parent); }; solver(const game_state, const sol_rules&); @@ -33,6 +45,7 @@ public: friend std::ostream& operator<< (std::ostream&, const solver::node&); private: + const node root; const sol_rules rules; node solution; -- cgit v1.2.1