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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
use core::mem::MaybeUninit;
use crate::types::{Move, Move32};
/// The largest legal shogi move set fits comfortably below this bound.
const MOVE_LIST_CAPACITY: usize = 600;
macro_rules! fixed_move_list {
($name:ident, $move:ty) => {
pub struct $name {
moves: [MaybeUninit<$move>; MOVE_LIST_CAPACITY],
len: usize,
}
impl Default for $name {
fn default() -> Self {
Self::new()
}
}
impl $name {
#[must_use]
pub const fn new() -> Self {
Self { moves: [const { MaybeUninit::uninit() }; MOVE_LIST_CAPACITY], len: 0 }
}
pub fn clear(&mut self) {
self.len = 0;
}
#[must_use]
pub const fn len(&self) -> usize {
self.len
}
#[must_use]
pub const fn is_empty(&self) -> bool {
self.len == 0
}
#[must_use]
pub const fn capacity(&self) -> usize {
MOVE_LIST_CAPACITY
}
pub fn push(&mut self, mv: $move) {
assert!(self.len < MOVE_LIST_CAPACITY, "move list capacity exceeded");
self.moves[self.len].write(mv);
self.len += 1;
}
pub(crate) fn ensure_additional_capacity(&self, additional: usize) {
assert!(additional <= MOVE_LIST_CAPACITY - self.len, "move list capacity exceeded");
}
/// # Safety
///
/// The caller must ensure that the active prefix has spare capacity.
pub(crate) unsafe fn push_unchecked(&mut self, mv: $move) {
debug_assert!(self.len < MOVE_LIST_CAPACITY);
self.moves[self.len].write(mv);
self.len += 1;
}
/// Keeps the moves for which `f` returns true, preserving their relative order.
pub fn retain<F>(&mut self, mut f: F)
where
F: FnMut(&$move) -> bool,
{
let mut write = 0;
for read in 0..self.len {
// SAFETY: every element in the active prefix was initialized by `push`.
let mv = unsafe { self.moves[read].assume_init_read() };
if f(&mv) {
self.moves[write].write(mv);
write += 1;
}
}
self.len = write;
}
/// Keeps the moves for which `f` returns true without preserving their order.
pub fn retain_unordered<F>(&mut self, mut f: F)
where
F: FnMut($move) -> bool,
{
let mut index = 0;
while index < self.len {
// SAFETY: every element in the active prefix was initialized by `push`.
let mv = unsafe { self.moves[index].assume_init_read() };
if f(mv) {
self.moves[index].write(mv);
index += 1;
} else {
self.len -= 1;
if index < self.len {
// SAFETY: the last element remains in the active initialized prefix.
let last = unsafe { self.moves[self.len].assume_init_read() };
self.moves[index].write(last);
}
}
}
}
#[must_use]
pub fn as_slice(&self) -> &[$move] {
// SAFETY: only the first `len` elements are exposed and every one is written by push.
unsafe {
core::slice::from_raw_parts(self.moves.as_ptr().cast::<$move>(), self.len)
}
}
#[must_use]
pub fn as_mut_slice(&mut self) -> &mut [$move] {
// SAFETY: `push` initializes every element in the exposed prefix.
unsafe {
core::slice::from_raw_parts_mut(
self.moves.as_mut_ptr().cast::<$move>(),
self.len,
)
}
}
pub fn iter(&self) -> core::slice::Iter<'_, $move> {
self.as_slice().iter()
}
}
impl Clone for $name {
fn clone(&self) -> Self {
let mut out = Self::new();
for &mv in self.iter() {
out.push(mv);
}
out
}
}
impl core::fmt::Debug for $name
where
$move: core::fmt::Debug,
{
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
self.as_slice().fmt(f)
}
}
impl AsRef<[$move]> for $name {
fn as_ref(&self) -> &[$move] {
self.as_slice()
}
}
impl core::ops::Deref for $name {
type Target = [$move];
fn deref(&self) -> &Self::Target {
self.as_slice()
}
}
impl<'a> IntoIterator for &'a $name {
type Item = &'a $move;
type IntoIter = core::slice::Iter<'a, $move>;
fn into_iter(self) -> Self::IntoIter {
self.iter()
}
}
};
}
fixed_move_list!(MoveList, Move);
fixed_move_list!(Move32List, Move32);