-
Notifications
You must be signed in to change notification settings - Fork 14
/
build.py
146 lines (94 loc) · 3.24 KB
/
build.py
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
import itertools
import numpy as np
# byteset_lengths = [4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 48, 52, 56, 60, 64]
byteset_lengths = [8, 16, 32, 64, 128, 256]
key_types = [
'u4',
'u8',
'i4',
'i8',
*[f'S{i}' for i in byteset_lengths],
]
value_types = [
'u1',
'u2',
'u4',
'u8',
'i1',
'i2',
'i4',
'i8',
'f4',
'f8',
*[f'S{i}' for i in byteset_lengths]
]
cpp_types = {
'i1' : 'int8_t',
'i2' : 'int16_t',
'i4' : 'int32_t',
'i8' : 'int64_t',
'u1' : 'uint8_t',
'u2' : 'uint16_t',
'u4' : 'uint32_t',
'u8' : 'uint64_t',
'f4' : 'float',
'f8' : 'double',
**{f'S{i}' : f'std::array<char,{i}>' for i in byteset_lengths},
}
def write_dict_types_dict(getpy_file, key_type, value_type):
getpy_file.write(f" (np.dtype('{key_type}'), np.dtype('{value_type}')) : _gp.Dict_{key_type}_{value_type},\n")
def write_set_types_dict(getpy_file, key_type):
getpy_file.write(f" np.dtype('{key_type}') : _gp.Set_{key_type},\n")
def write_multidict_types_dict(getpy_file, key_type, value_type):
getpy_file.write(f" (np.dtype('{key_type}'), np.dtype('{value_type}')) : _gp.MultiDict_{key_type}_{value_type},\n")
def write_declare_dict(getpy_file, key_type, value_type):
getpy_file.write(f' declare_dict<{cpp_types[key_type]}, {cpp_types[value_type]}>(m, "Dict_{key_type}_{value_type}");\n')
def write_declare_set(getpy_file, key_type):
getpy_file.write(f' declare_set<{cpp_types[key_type]}>(m, "Set_{key_type}");\n')
def write_declare_multidict(getpy_file, key_type, value_type):
getpy_file.write(f' declare_multidict<{cpp_types[key_type]}, {cpp_types[value_type]}>(m, "MultiDict_{key_type}_{value_type}");\n')
def write_getpy_cpp(key_types, value_types):
with open('src/getpy_types.cpp', 'w') as getpy_file:
getpy_file.write("""\
#include "getpy.cpp"
#include <pybind11/pybind11.h>
PYBIND11_MODULE(_getpy, m) {
""")
for key_type, value_type in itertools.product(key_types, value_types):
write_declare_dict(getpy_file, key_type, value_type)
getpy_file.write('\n')
for key_type in key_types:
write_declare_set(getpy_file, key_type)
getpy_file.write('\n')
for key_type, value_type in itertools.product(key_types, key_types):
write_declare_multidict(getpy_file, key_type, value_type)
getpy_file.write("""\
}
""")
def write_getpy_py(key_types, value_types):
with open('getpy/getpy_types.py', 'w') as getpy_file:
getpy_file.write("""\
import numpy as np
import _getpy as _gp
dict_types = {
""")
for key_type, value_type in itertools.product(key_types, value_types):
write_dict_types_dict(getpy_file, key_type, value_type)
getpy_file.write("""\
}
set_types = {
""")
for key_type in key_types:
write_set_types_dict(getpy_file, key_type)
getpy_file.write("""\
}
multidict_types = {
""")
for key_type, value_type in itertools.product(key_types, key_types):
write_multidict_types_dict(getpy_file, key_type, value_type)
getpy_file.write("""\
}
""")
if __name__ == '__main__':
write_getpy_cpp(key_types, value_types)
write_getpy_py(key_types, value_types)