-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmod.rs
138 lines (122 loc) · 4.35 KB
/
mod.rs
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
//! transposition tables!
use std::sync::Arc;
use std::sync::RwLock;
use crate::search::SearchResult;
use crate::setup::depth::Depth;
use crate::transposition_table::vl::VL;
use crate::transposition_table::vl::VlShare;
pub mod empty_table;
pub mod entry;
pub mod vl;
/// The default size of a transposition table, in bytes [release mode]
#[cfg(not(debug_assertions))]
pub const DEFAULT_TABLE_SIZE: usize = 16 * 1024 * 1024;
/// The default size of a transposition table, in bytes [debug mode]
#[cfg(debug_assertions)]
pub const DEFAULT_TABLE_SIZE: usize = 256;
/// A key for a transposition table
/// - FromType: the type of the position's identifier (e.g. the board state, or
/// [`chess::Board`] object)
pub trait TKey: Sync + Sized + Copy + Clone {
/// the type of the position's identifier
type FromType;
/// create a new key from the position's identifier
/// (e.g. the board state, or [`chess::Board`] object)
fn hash(from: &Self::FromType) -> Self;
/// a full match between keys, used to determine equality
/// (slower than [`matches`])
fn equals(&self, other: &Self) -> bool;
}
/// A transposition table entry
pub trait TEntry: Sync {
/// the type of the hash used to identify this entry
type Key: TKey;
/// the hash-key of this entry
fn key(&self) -> Self::Key;
/// create a new empty entry
fn new_empty() -> Self;
/// create a new entry to store a search result
fn new_from_result(hash: u64, depth: Depth, result: &SearchResult, bound: EvalBound) -> Self;
/// the depth of the search that created this entry
fn depth(&self) -> Depth;
/// the relative evaluation of the entry
fn bound(&self) -> EvalBound;
/// a [`SearchResult`] from this entry
fn search_result(&self) -> SearchResult;
/// do the entry's values make sense?
fn is_valid(&self) -> bool;
// ...
}
/// A transposition table of hashes with type `K` and entries of type `E`
pub trait TranspositionTable<Key: TKey, Entry: TEntry> {
/// create a new transposition table, with a size of `bytes` bytes
fn new(bytes: usize) -> Self;
/// resize the transposition table to `bytes` bytes.
/// return the number of entries in the new table
fn resize(&mut self, bytes: usize) -> usize;
/// get the entry for a hash, if it exists
fn get(&self, hash: Key) -> Option<Entry>;
/// insert an entry for a hash.
///
/// intended as a wrapper around [`entry`]
fn insert(&mut self, hash: Key, entry: Entry);
/// empty the transposition table
fn clear(&mut self);
/// the number of entries in the table
fn entry_count(&self) -> usize;
/// capacity in number of entries
fn capacity(&self) -> usize;
/// UCI hashfull
fn hashfull(&self) -> usize;
}
/// A model for concurrent access to the transposition table
pub trait TableAccess<K: TKey, E: TEntry, T: Send + Sync + TranspositionTable<K, E>> {
/// increment number of successful reads were made in this transposition
/// table
fn hit(&self);
/// access the table. the implementation must ensure that this is
/// - safe
/// - fast
/// - calling &mut self functions on the returned table is safe & updates
/// the _same_ table
fn share(&self) -> Self;
}
/// the type of the currently used transposition table
pub type TableImpl = VL;
/// the type for the currently used thread-sharing implementation
pub type ShareImpl = VlShare;
/// the actual transposition table struct that's passed to the search threads
#[derive(Debug)]
pub struct TT {
/// the table access struct, defined for whatever the currently used
/// implementation is
table: ShareImpl,
}
impl TT {
/// create a new table access point
pub fn new() -> Self {
// let table: EmptyTable<EmptyHash, EmptyEntry> =
// EmptyTable::new(DEFAULT_TABLE_SIZE);
Self {
table: Arc::new(RwLock::new(VL::new(DEFAULT_TABLE_SIZE))),
}
}
/// get a reference to the table
pub fn get(&self) -> VlShare {
self.table.share()
}
/// get a mutable reference to the table
pub fn get_mut(&mut self) -> VlShare {
self.table.share()
}
}
/// The bound of an evaluation
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum EvalBound {
/// The evaluation is precise
Exact,
/// The evaluation is a lower bound
LowerBound,
/// The evaluation is an upper bound
UpperBound,
}