-
Notifications
You must be signed in to change notification settings - Fork 0
/
genArrayModule.zig
116 lines (100 loc) · 2.98 KB
/
genArrayModule.zig
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
const py = @cImport({
@cDefine("PY_SSIZE_T_CLEAN", {});
@cInclude("Python.h");
});
const gen = @import("src/main.zig");
const std = @import("std");
const math = std.math;
const print = @import("std").debug.print;
const PyObject = py.PyObject;
const PyMethodDef = py.PyMethodDef;
const PyModuleDef = py.PyModuleDef;
const PyModuleDef_Base = py.PyModuleDef_Base;
const Py_BuildValue = py.Py_BuildValue;
const PyModule_Create = py.PyModule_Create;
const METH_NOARGS = py.METH_NOARGS;
const PyArg_ParseTuple = py.PyArg_ParseTuple;
const PyLong_FromLong = py.PyLong_FromLong;
const PyList_Append = py.PyList_Append;
const PyTuple_Append = py.PyTuple_Append;
fn returnArray(self: [*c]PyObject, args: [*c]PyObject) callconv(.C) [*]PyObject {
_ = self;
_ = args;
var list: [*c]PyObject = py.PyList_New(0);
const sample = gen.gen();
for (sample) |item| {
_ = PyList_Append(list, PyLong_FromLong(item));
}
return list;
}
fn returnArrayWithInput(self: [*c]PyObject, args: [*c]PyObject) callconv(.C) [*]PyObject {
_ = self;
var N: u32 = undefined;
if (!(py._PyArg_ParseTuple_SizeT(args, "l", &N) != 0)) return Py_BuildValue("");
var tuple: [*c]PyObject = py.PyTuple_New(N);
var a: i16 = undefined;
var b: i16 = undefined;
var c: i16 = undefined;
var d: i16 = undefined;
var e: i16 = undefined;
var f: i16 = undefined;
var g: i16 = undefined;
var y: i64 = undefined;
var rndGen = std.rand.DefaultPrng.init(42);
var i: u32 = 0;
while (i < N) : (i += 1) {
a = rndGen.random().int(i16);
b = rndGen.random().int(i16);
c = rndGen.random().int(i16);
d = rndGen.random().int(i16);
e = rndGen.random().int(i16);
f = rndGen.random().int(i16);
g = rndGen.random().int(i16);
y = @as(i64, a) + 2 * b - 12 * c + 155 + 5 * d + @floatToInt(i64, math.tan(@intToFloat(f16, e))) + (f - g) * 9;
const python_int: [*c]PyObject = Py_BuildValue("i", y);
_ = py.PyTuple_SetItem(tuple, i, python_int);
}
return tuple;
}
var Methods = [_]PyMethodDef{
PyMethodDef{
.ml_name = "returnArray",
.ml_meth = returnArray,
.ml_flags = METH_NOARGS,
.ml_doc = null,
},
PyMethodDef{
.ml_name = "returnArrayWithInput",
.ml_meth = returnArrayWithInput,
.ml_flags = @as(c_int, 1),
.ml_doc = null,
},
PyMethodDef{
.ml_name = null,
.ml_meth = null,
.ml_flags = 0,
.ml_doc = null,
},
};
var module = PyModuleDef{
.m_base = PyModuleDef_Base{
.ob_base = PyObject{
.ob_refcnt = 1,
.ob_type = null,
},
.m_init = null,
.m_index = 0,
.m_copy = null,
},
.m_name = "genArray",
.m_doc = null,
.m_size = -1,
.m_methods = &Methods,
.m_slots = null,
.m_traverse = null,
.m_clear = null,
.m_free = null,
};
pub export fn PyInit_genArray() [*]PyObject {
return PyModule_Create(&module);
}