aboutsummaryrefslogtreecommitdiff
path: root/src/main/game/global_cache.h
blob: 4658e5206c7137446f4801744a9c728f2e0cc374 (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
//
// Created by thecharlesblake on 1/10/18.
//

#ifndef SOLVITAIRE_GLOBAL_CACHE_H
#define SOLVITAIRE_GLOBAL_CACHE_H

#include <vector>
#include <unordered_set>
#include <boost/pool/pool.hpp>
#include <boost/pool/pool_alloc.hpp>

#include "sol_rules.h"
#include "game_state.h"

struct cached_game_state {
    typedef std::vector<
#ifdef NO_REDUCED_STATE
            pile
#else
            card
#endif
    > state_data;
    typedef state_data::size_type size_type;

    explicit cached_game_state(const game_state&);
    void add_pile(game_state::pile_ref, const game_state&);
    void add_card(card, const game_state&);
    void add_card_divider();

    state_data data;
};
bool operator==(const cached_game_state&, const cached_game_state&);

struct hasher {
    explicit hasher(const game_state&);
    std::size_t operator() (const cached_game_state&) const;

    std::size_t hash_value(const card&) const;
#ifdef NO_REDUCED_STATE
    std::size_t hash_value(const pile&) const;
#endif
    std::size_t combine(std::size_t&, std::size_t) const;

    const game_state& init_gs;
};

class global_cache {
public:
    explicit global_cache(const game_state&);
    bool insert(const game_state&);
    bool contains(const game_state&) const;
    void clear();

private:
    std::unordered_set<
            cached_game_state,
            hasher
    > u_set;
};

#endif //SOLVITAIRE_GLOBAL_CACHE_H