aboutsummaryrefslogtreecommitdiff
path: root/src/main/game/global_cache.cpp
blob: a1d7585a26ebc3b8ab9b0326653dc2a90ab5a02c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
//
// Created by thecharlesblake on 1/10/18.
//

#include <boost/functional/hash.hpp>

#include "global_cache.h"

using namespace std;
using namespace boost;

typedef sol_rules::build_policy pol;
typedef sol_rules::stock_deal_type sdt;
typedef global_cache cache;

global_cache::global_cache(const game_state init_game_state) {
    global_cache::init_gs = init_game_state;
}

bool global_cache::insert(const std::vector<pile>& pile_vec) {
    return u_set.insert(pile_vec).second;
}

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 (cache::init_gs.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);
}

size_t hash_value(pile const& p) {
    return hash_range(begin(p.pile_vec), end(p.pile_vec));
}

size_t hash_value(std::vector<pile> const& piles) {
    size_t seed = 0;

    // Using addition for commutative hash
    for (game_state::pile_ref pr : cache::init_gs.tableau_piles) {
        seed += hash_value(piles[pr]);
    }

    // Using hash_combine for order-dependent hash
    for (game_state::pile_ref pr : cache::init_gs.foundations) {
        hash_combine(seed, piles[pr]);
    }

    // Commutative
    for (game_state::pile_ref pr : cache::init_gs.reserve) {
        seed += hash_value(piles[pr]);
    }

    if (cache::init_gs.rules.stock_size > 0) {
        hash_combine(seed, piles[cache::init_gs.stock]);
    }

    if (cache::init_gs.rules.stock_deal_t == sdt::WASTE) {
        hash_combine(seed, piles[cache::init_gs.waste]);
    }

    if (cache::init_gs.rules.stock_deal_t == sdt::WASTE) {
        hash_combine(seed, piles[cache::init_gs.waste]);
    }

    if (cache::init_gs.rules.hole) {
        hash_combine(seed, piles[cache::init_gs.hole]);
    }

    // Commutative
    for (game_state::pile_ref pr : cache::init_gs.cells) {
        seed += hash_value(piles[pr]);
    }

    return seed;
}