l0n0lc 0.4.0__py3-none-any.whl → 0.5.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
l0n0lc/StdList.py ADDED
@@ -0,0 +1,24 @@
1
+ from .c基础处理 import c变量, cpp类型, list初始化列表
2
+
3
+
4
+ class StdList(c变量):
5
+ def __init__(
6
+ self,
7
+ 初始化列表: list初始化列表,
8
+ 名字: str, 是参数: bool) -> None:
9
+ self.初始化列表 = 初始化列表
10
+ if 初始化列表.类型 == cpp类型.ANY:
11
+ super().__init__(f'std::vector<{初始化列表.类型}>', 名字, 是参数)
12
+ else:
13
+ super().__init__(f'{初始化列表.类型}', 名字, 是参数)
14
+
15
+ def __getitem__(self, key):
16
+ return f'{self}[{key}]'
17
+
18
+ def __setitem__(self, key, value):
19
+ return f'{self}[{key}] = {value};'
20
+
21
+ def 初始化(self, 初始值):
22
+ if self.初始化列表.类型 == cpp类型.ANY:
23
+ return super().初始化(初始值)
24
+ return f'{self.类型} {self.名字}[] = {初始值};'
l0n0lc/StdMap.py ADDED
@@ -0,0 +1,21 @@
1
+ from .c基础处理 import py类型转c类型, dict初始化列表, c变量, cpp类型
2
+ from typing import Tuple, Union, List
3
+ from .通用 import toCString
4
+
5
+
6
+ class StdUnorderedMap(c变量):
7
+ def __init__(
8
+ self,
9
+ 初始化列表: dict初始化列表,
10
+ 名字: str, 是参数: bool) -> None:
11
+ self.初始化列表 = 初始化列表
12
+ super().__init__(
13
+ f'std::unordered_map<{初始化列表.key类型}, {初始化列表.value类型}>', 名字, 是参数)
14
+
15
+ def __getitem__(self, key):
16
+ return f'{self}[{toCString(key)}]'
17
+
18
+ def __setitem__(self, key, value):
19
+ 左 = f'{self}[{toCString(key)}]'
20
+ 右 = toCString(value)
21
+ return f'{左} = {右};'
l0n0lc/__init__.py CHANGED
@@ -1,6 +1,6 @@
1
- from .输出 import 配置输出函数
2
- from .c类型基础 import c类型基础, c_void, c_void_p, c函数, c结构体, c数组
3
- from .c域 import 花括号, 如果, 否则, 否则如果, 循环, 输出
4
- from .c基础类型 import *
5
- from .c语句 import *
6
- from .编译 import 编译
1
+ from .通用 import *
2
+ from .c基础处理 import *
3
+ from .StdList import *
4
+ from .编译 import *
5
+ from .StdMap import *
6
+ from .jit import *
@@ -0,0 +1,307 @@
1
+ from typing import Union, List, get_origin, get_args
2
+ from .通用 import 生成变量Id, toCString
3
+ import numpy as np
4
+ import ctypes
5
+
6
+
7
+ def cpp获取变量类型(v):
8
+ return f'decltype({v})'
9
+
10
+
11
+ class cpp类型:
12
+ INT8_T = 'int8_t'
13
+ INT16_T = 'int16_t'
14
+ INT32_T = 'int32_t'
15
+ INT64_T = 'int64_t'
16
+ UINT8_T = 'uint8_t'
17
+ UINT16_T = 'uint16_t'
18
+ UINT32_T = 'uint32_t'
19
+ UINT64_T = 'uint64_t'
20
+ HALF = 'half'
21
+ FLOAT = 'float'
22
+ STRING = 'std::string'
23
+ BOOL = 'bool'
24
+ ANY = 'std::any'
25
+ AUTO = 'auto'
26
+ VOID_P = 'void*'
27
+
28
+
29
+ class 指针:
30
+ def __init__(self, 类型) -> None:
31
+ self.基础类型 = 类型
32
+
33
+ def __str__(self) -> str:
34
+ return f'{self.基础类型}*'
35
+
36
+
37
+ def 执行额外函数(函数列表: List, *args):
38
+ for fn in 函数列表:
39
+ ret = fn(*args)
40
+ if ret is not None:
41
+ return ret
42
+
43
+
44
+ 额外py转ctypes函数 = []
45
+
46
+
47
+ def py类型转ctypes类型(类型):
48
+ ret = 执行额外函数(额外py转ctypes函数, 类型)
49
+ if ret is not None:
50
+ return ret
51
+
52
+ # 基础类型
53
+ if 类型 is int:
54
+ return ctypes.c_int64
55
+ if 类型 is float:
56
+ return ctypes.c_float
57
+ if 类型 is str:
58
+ return ctypes.c_char_p
59
+ if 类型 is bool:
60
+ return ctypes.c_bool
61
+ if 类型 is 指针:
62
+ return ctypes.c_void_p
63
+
64
+ # numpy dtype 转换
65
+ return numpy类型转ctypes类型(类型)
66
+
67
+
68
+ 额外py转c函数 = []
69
+
70
+
71
+ def py类型转c类型(类型):
72
+ ret = 执行额外函数(额外py转c函数, 类型)
73
+ if ret is not None:
74
+ return ret
75
+
76
+ # 基础类型
77
+ if 类型 is int:
78
+ return cpp类型.INT64_T
79
+ if 类型 is float:
80
+ return cpp类型.FLOAT
81
+ if 类型 is str:
82
+ return cpp类型.STRING
83
+ if 类型 is bool:
84
+ return cpp类型.BOOL
85
+ if 类型 is 指针:
86
+ return cpp类型.VOID_P
87
+
88
+ origin = get_origin(类型)
89
+ args = get_args(类型)
90
+
91
+ if origin is Union:
92
+ return cpp类型.ANY
93
+
94
+ # List[...] → 指针(...)
95
+ if origin is list:
96
+ if args:
97
+ elem_type = args[0]
98
+ return 指针(py类型转c类型(elem_type))
99
+
100
+ # Dict[K, V] → std::unordered_map<K, V>
101
+ if origin is dict:
102
+ if len(args) == 2:
103
+ key_type = py类型转c类型(args[0])
104
+ val_type = py类型转c类型(args[1])
105
+ return f"std::unordered_map<{key_type}, {val_type}>&"
106
+
107
+ # numpy dtype 转换
108
+ ret = numpy类型转c类型(类型)
109
+ if ret is not None:
110
+ return ret
111
+
112
+ # 直接传字符串类型名
113
+ if isinstance(类型, str):
114
+ return 类型
115
+
116
+ return cpp类型.ANY
117
+
118
+
119
+ 额外numpy转ctypes函数 = []
120
+
121
+
122
+ def numpy类型转ctypes类型(npdtype):
123
+ ret = 执行额外函数(额外numpy转ctypes函数, npdtype)
124
+ if ret is not None:
125
+ return ret
126
+
127
+ if npdtype == np.int8:
128
+ return ctypes.c_int8
129
+ if npdtype == np.int16:
130
+ return ctypes.c_int16
131
+ if npdtype == np.int32:
132
+ return ctypes.c_int32
133
+ if npdtype == np.int64:
134
+ return ctypes.c_int64
135
+ if npdtype == np.uint8:
136
+ return ctypes.c_uint8
137
+ if npdtype == np.uint16:
138
+ return ctypes.c_uint16
139
+ if npdtype == np.uint32:
140
+ return ctypes.c_uint32
141
+ if npdtype == np.uint64:
142
+ return ctypes.c_uint64
143
+ try:
144
+ if npdtype == np.bool_:
145
+ return ctypes.c_bool
146
+ except:
147
+ pass
148
+ try:
149
+ if npdtype == np.bool: # type: ignore
150
+ return ctypes.c_bool
151
+ except:
152
+ pass
153
+ if npdtype == np.float32:
154
+ return ctypes.c_float
155
+ if npdtype == np.float64:
156
+ return ctypes.c_double
157
+
158
+
159
+ 额外numpy转c函数 = []
160
+
161
+
162
+ def numpy类型转c类型(npdtype):
163
+ ret = 执行额外函数(额外numpy转c函数, npdtype)
164
+ if ret is not None:
165
+ return ret
166
+
167
+ if npdtype == np.int8:
168
+ return cpp类型.INT8_T
169
+ if npdtype == np.int16:
170
+ return cpp类型.INT16_T
171
+ if npdtype == np.int32:
172
+ return cpp类型.INT32_T
173
+ if npdtype == np.int64:
174
+ return cpp类型.INT64_T
175
+ if npdtype == np.uint8:
176
+ return cpp类型.UINT8_T
177
+ if npdtype == np.uint16:
178
+ return cpp类型.UINT16_T
179
+ if npdtype == np.uint32:
180
+ return cpp类型.UINT32_T
181
+ if npdtype == np.uint64:
182
+ return cpp类型.UINT64_T
183
+ try:
184
+ if npdtype == np.bool_:
185
+ return cpp类型.BOOL
186
+ except:
187
+ pass
188
+ try:
189
+ if npdtype == np.bool: # type: ignore
190
+ return cpp类型.BOOL
191
+ except:
192
+ pass
193
+ if npdtype == np.float16:
194
+ return cpp类型.HALF
195
+ if npdtype == np.float32:
196
+ return cpp类型.FLOAT
197
+
198
+
199
+ def cpp类型检查(类型, 支持提示: str):
200
+ if 类型 not in [int, float, str, bool]:
201
+ raise Exception(f'{支持提示} 仅支持 [int, float, str, bool]')
202
+
203
+
204
+ class list初始化列表:
205
+ def __init__(
206
+ self,
207
+ 代码: str,
208
+ 类型列表: Union[List[Union[int, float, bool, str]],
209
+ int, float, bool, str],
210
+ 长度: int) -> None:
211
+ self.代码 = 代码
212
+ self.类型列表 = 类型列表
213
+ self.类型 = py类型转c类型(类型列表)
214
+ self.长度 = 长度
215
+
216
+ def __str__(self) -> str:
217
+ return self.代码
218
+
219
+
220
+ def 从list构建初始化列表(value: List):
221
+ 数据类型列表 = []
222
+ 初始化列表 = []
223
+ for v in value:
224
+ dtype = type(v)
225
+ cpp类型检查(dtype, 'List')
226
+ 数据类型列表.append(dtype)
227
+ 初始化列表.append(toCString(v))
228
+ # 构建初始化列表
229
+ 初始化列表 = '{' + ','.join(初始化列表) + '}'
230
+
231
+ # 构建类型列表
232
+ if all(类型 == 数据类型列表[0] for 类型 in 数据类型列表):
233
+ 数据类型列表 = 数据类型列表[0]
234
+
235
+ return list初始化列表(初始化列表, 数据类型列表, len(value))
236
+
237
+
238
+ def 从ndarray构建初始化列表(value: np.ndarray):
239
+ value = value.reshape(value.size)
240
+ 类型 = numpy类型转c类型(value.dtype)
241
+ if 类型 is None:
242
+ raise Exception(f'暂不支持{value.dtype}')
243
+ 初始化列表 = '{' + ','.join(value) + '}'
244
+ return list初始化列表(初始化列表, 类型, value.size)
245
+
246
+
247
+ class dict初始化列表:
248
+ def __init__(
249
+ self,
250
+ 代码: str,
251
+ key类型列表: Union[List[Union[int, float, bool, str]],
252
+ int, float, bool, str],
253
+ value类型列表: Union[List[Union[int, float, bool, str]],
254
+ int, float, bool, str]) -> None:
255
+ self.代码 = 代码
256
+ self.key类型列表 = key类型列表
257
+ self.value类型列表 = value类型列表
258
+ self.key类型 = py类型转c类型(key类型列表)
259
+ self.value类型 = py类型转c类型(value类型列表)
260
+
261
+ def __str__(self) -> str:
262
+ return self.代码
263
+
264
+
265
+ def 从dict构建初始化列表(value: dict):
266
+ code = []
267
+ key类型列表 = []
268
+ value类型列表 = []
269
+ for k, v in value.items():
270
+ key类型 = type(k)
271
+ cpp类型检查(key类型, 'Map')
272
+ value类型 = type(v)
273
+ cpp类型检查(value类型, 'Map')
274
+ key类型列表.append(key类型)
275
+ value类型列表.append(value类型)
276
+ code.append(f'{{ {toCString(k)}, {v} }}')
277
+
278
+ # 构建类型列表
279
+ if all(类型 == key类型列表[0] for 类型 in key类型列表):
280
+ key类型列表 = key类型列表[0]
281
+
282
+ if all(类型 == value类型列表[0] for 类型 in value类型列表):
283
+ value类型列表 = value类型列表[0]
284
+
285
+ # 构建初始化列表
286
+ 初始化列表 = '{' + ','.join(code) + '}'
287
+
288
+ return dict初始化列表(初始化列表, key类型列表, value类型列表)
289
+
290
+
291
+ class c变量:
292
+ def __init__(self, 类型: str, 名字: str, 是参数: bool, 默认值=None) -> None:
293
+ self.类型 = 类型
294
+ self.名字 = 名字
295
+ self.c名字 = 生成变量Id(名字)
296
+ self.是参数 = 是参数
297
+ self.默认值 = 默认值
298
+
299
+ def __str__(self):
300
+ return self.c名字
301
+
302
+ @property
303
+ def c类型(self):
304
+ return cpp获取变量类型(self)
305
+
306
+ def 初始化(self, 初始值):
307
+ return f'{self.类型} {self.c名字} = {初始值};'