-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelper.h
More file actions
42 lines (34 loc) · 1.18 KB
/
helper.h
File metadata and controls
42 lines (34 loc) · 1.18 KB
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
#pragma once
#include <stdexcept>
namespace
{
inline int xy_to_index(int x, int y, int yL) { return y * yL + x; }
inline int positive_modulo(int i, int n) { return (i % n + n) % n; }
}
void generate_2D_NNList(int xL, int yL, uint32_t* nnList)
{
if (xL * yL * 4 > UINT32_MAX)
throw std::runtime_error("xL * yL * 4 > UINT32_MAX");
for (int y = 0; y < yL; y++)
{
for (int x = 0; x < xL; x++)
{
size_t index = xy_to_index(x, y, yL) * 4;
nnList[index + 0] = xy_to_index(positive_modulo(x + 1, xL), y, yL); //right
nnList[index + 1] = xy_to_index(positive_modulo(x - 1, xL), y, yL); //left
nnList[index + 2] = xy_to_index(x , positive_modulo(y + 1, yL), yL); //bottom
nnList[index + 3] = xy_to_index(x , positive_modulo(y - 1, yL), yL); //top
}
}
}
void randomize_state(int8_t* state, int n)
{
for (int i = 0; i < n; i++)
{
double rnd = double(rand()) / RAND_MAX;
uint8_t spin = 1;
if (rnd < 0.5)
spin = -1;
state[i] = spin;
}
}