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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
use connect6::{agent, game::Player, policy::Evaluator, BOARD_CAPACITY, BOARD_SIZE};
use cppbind::*;
#[no_mangle]
pub extern "C" fn test_new_raw_path() -> RawPath {
RawPath::new()
}
#[no_mangle]
pub extern "C" fn test_with_raw_path() -> RawPath {
let mut board = [[Player::None; BOARD_SIZE]; BOARD_SIZE];
for i in 0..BOARD_SIZE {
for j in 0..BOARD_SIZE {
let id = ((i * BOARD_SIZE + j) % 3) as i32 - 1;
board[i][j] = Player::from(id);
}
}
let path = agent::Path {
turn: Player::White,
board,
pos: (0, BOARD_SIZE % 5 + 1),
};
RawPath::with_path(&path)
}
#[no_mangle]
pub extern "C" fn test_echo_raw_path(
turn: CInt,
board_ptr: *mut CInt,
row: CInt,
col: CInt,
) -> RawPath {
let board_slice = unsafe { ::std::slice::from_raw_parts(board_ptr, BOARD_CAPACITY) };
let mut board = [[0; BOARD_SIZE]; BOARD_SIZE];
for i in 0..BOARD_SIZE {
for j in 0..BOARD_SIZE {
board[i][j] = board_slice[i * BOARD_SIZE + j];
}
}
RawPath {
turn,
board,
row,
col,
}
}
#[no_mangle]
pub extern "C" fn test_with_raw_play_result(allocator: AllocatorType<RawPath>) -> RawPlayResult {
let mut vec = Vec::new();
let mut player = Player::Black;
for i in 0..10 {
let mut board = [[Player::None; BOARD_SIZE]; BOARD_SIZE];
for j in 0..i + 1 {
board[j][j] = Player::from(((i + j) % 3) as i32 - 1);
}
vec.push(agent::Path {
turn: player,
board,
pos: (i, i + 1),
});
player.mut_switch();
}
let result = agent::PlayResult {
winner: Player::Black,
path: vec,
};
let alloc = Allocator::new(allocator);
RawPlayResult::with_result(&result, &alloc)
}
#[no_mangle]
pub extern "C" fn test_echo_raw_play_result(
winner: CInt,
path: *mut RawPath,
len: CInt,
allocator: AllocatorType<RawPath>,
) -> RawPlayResult {
let path_s = unsafe { ::std::slice::from_raw_parts(path, len as usize) };
let mut vec = Vec::new();
for i in 0..len as usize {
let mut board = [[Player::None; BOARD_SIZE]; BOARD_SIZE];
for j in 0..BOARD_SIZE {
for k in 0..BOARD_SIZE {
board[j][k] = Player::from(path_s[i].board[j][k]);
}
}
vec.push(agent::Path {
turn: Player::from(path_s[i].turn),
board,
pos: (path_s[i].row as usize, path_s[i].col as usize),
});
}
let result = agent::PlayResult {
winner: Player::from(winner),
path: vec,
};
let alloc = Allocator::new(allocator);
RawPlayResult::with_result(&result, &alloc)
}
#[no_mangle]
pub extern "C" fn test_with_raw_vec(allocator: AllocatorType<CInt>) -> RawVec<CInt> {
let vec = vec![0, 1, 2, 3, 4, 5];
let alloc = Allocator::new(allocator);
RawVec::with_vec(vec, &alloc)
}
#[no_mangle]
pub extern "C" fn test_echo_raw_vec(
ptr: *const CInt,
len: CInt,
allocator: AllocatorType<CInt>,
) -> RawVec<CInt> {
let raw_slice = unsafe { ::std::slice::from_raw_parts(ptr, len as usize) };
let mut vec = Vec::new();
for i in 0..len {
vec.push(raw_slice[i as usize]);
}
let alloc = Allocator::new(allocator);
RawVec::with_vec(vec, &alloc)
}
#[no_mangle]
pub extern "C" fn test_echo_cppeval(
turn: CInt,
boards: *const CInt,
len: CInt,
callback: Callback,
allocator: AllocatorType<CFloat>,
) -> RawVec<CFloat> {
let turn = Player::from(turn);
let len = len as usize;
let boards = unsafe { ::std::slice::from_raw_parts(boards, len * BOARD_CAPACITY) };
let mut cnt = 0;
let mut vec = Vec::new();
for _ in 0..len {
let mut board = [[Player::None; BOARD_SIZE]; BOARD_SIZE];
for r in 0..BOARD_SIZE {
for c in 0..BOARD_SIZE {
board[r][c] = Player::from(boards[cnt]);
cnt += 1;
}
}
vec.push(board);
}
let cppeval = CppEval::new(callback);
let res = cppeval.eval(turn, &vec);
assert!(res.is_some());
let (vals, policies) = res.unwrap();
assert_eq!(vals.len(), len);
assert_eq!(policies.len(), len);
let mut ret = Vec::new();
for val in vals {
ret.push(val);
}
for policy in policies {
for i in 0..BOARD_SIZE {
for j in 0..BOARD_SIZE {
ret.push(policy[i][j]);
}
}
}
let alloc = Allocator::new(allocator);
RawVec::with_vec(ret, &alloc)
}
#[no_mangle]
pub extern "C" fn test_cpp_policy(
board_ptr: *const [[CFloat; BOARD_SIZE]; BOARD_SIZE],
callback: PolicyCallback,
allocator: AllocatorType<CInt>,
) -> RawVec<CInt> {
let board_ref = unsafe { board_ptr.as_ref() }.unwrap();
let mut board = [[Player::None; BOARD_SIZE]; BOARD_SIZE];
for i in 0..BOARD_SIZE {
for j in 0..BOARD_SIZE {
board[i][j] = Player::from(board_ref[i][j] as i32);
}
}
let cpp_policy = CppPolicy::new(callback);
let res = if let Some((row, col)) = cpp_policy.callback(&board) {
vec![row as i32, col as i32]
} else {
Vec::new()
};
let alloc = Allocator::new(allocator);
RawVec::with_vec(res, &alloc)
}