Vita
cache.cc
Go to the documentation of this file.
1
13#include <mutex>
14
15#include "kernel/cache.h"
16
17namespace vita
18{
24cache::cache(unsigned bits) : mutex_(), k_mask((1ull << bits) - 1),
25 table_(1ull << bits), seal_(1)
26{
27 Expects(bits);
28 Ensures(is_valid());
29}
30
35inline std::size_t cache::index(const hash_t &h) const
36{
37 return h.data[0] & k_mask;
38}
39
46{
47 std::unique_lock lock(mutex_);
48
49 ++seal_;
50
51 // for (auto &s : table_)
52 // {
53 // s.hash = hash_t();
54 // s.fitness = {};
55 // }
56}
57
63void cache::clear(const hash_t &h)
64{
65 std::unique_lock lock(mutex_);
66
67 table_[index(h)].hash = hash_t();
68
69 // An alternative to invalidate the slot:
70 // table_[index(h)].seal = 0;
71 // It works because the first valid seal is 1.
72}
73
81const fitness_t &cache::find(const hash_t &h) const
82{
83 std::shared_lock lock(mutex_);
84
85 const slot &s(table_[index(h)]);
86 const bool ret(seal_ == s.seal && h == s.hash);
87
88 if (ret)
89 return s.fitness;
90
91 static const fitness_t empty{};
92 return empty;
93}
94
102void cache::insert(const hash_t &h, const fitness_t &fitness)
103{
104 std::unique_lock lock(mutex_);
105
106 slot s;
107 s.hash = h;
108 s.fitness = fitness;
109 s.seal = seal_;
110
111 table_[index(s.hash)] = s;
112}
113
121bool cache::load(std::istream &in)
122{
123 std::unique_lock lock(mutex_);
124
125 decltype(seal_) t_seal;
126 if (!(in >> t_seal))
127 return false;
128
129 std::size_t n;
130 if (!(in >> n))
131 return false;
132
133 for (decltype(n) i(0); i < n; ++i)
134 {
135 slot s;
136 s.seal = t_seal;
137
138 if (!s.hash.load(in))
139 return false;
140 if (!s.fitness.load(in))
141 return false;
142
143 table_[index(s.hash)] = s;
144 }
145
146 seal_ = t_seal;
147
148 return true;
149}
150
155bool cache::save(std::ostream &out) const
156{
157 std::shared_lock lock(mutex_);
158
159 out << seal_ << ' ' << '\n';
160
161 std::size_t num(0);
162 for (const auto &s : table_)
163 if (!s.hash.empty())
164 ++num;
165 out << num << '\n';
166
167 for (const auto &s : table_)
168 if (s.seal == seal_ && !s.hash.empty())
169 {
170 s.hash.save(out);
171 s.fitness.save(out);
172 }
173
174 return out.good();
175}
176
180bool cache::is_valid() const
181{
182 return true;
183}
184
185} // namespace vita
cache(unsigned)
Creates a new hash table.
Definition: cache.cc:24
void clear()
Clears the content and the statistical informations of the table.
Definition: cache.cc:45
const fitness_t & find(const hash_t &) const
Looks for the fitness of an individual in the transposition table.
Definition: cache.cc:81
bool is_valid() const
Definition: cache.cc:180
void insert(const hash_t &, const fitness_t &)
Stores fitness information in the transposition table.
Definition: cache.cc:102
bool save(std::ostream &) const
Definition: cache.cc:155
bool load(std::istream &)
Definition: cache.cc:121
Contains flags and manipulators to control the output format of individuals.
The main namespace for the project.
A 128bit unsigned integer used as individual's signature / hash table look-up key.
Definition: cache_hash.h:27