-
Notifications
You must be signed in to change notification settings - Fork 67
Expand file tree
/
Copy pathdatatype.py
More file actions
executable file
·358 lines (278 loc) · 10.1 KB
/
datatype.py
File metadata and controls
executable file
·358 lines (278 loc) · 10.1 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
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
"""
The follwing constructor classes exists here:
+------------------------------------------+---------------------------------------+
| Class | Description |
+==========================================+=======================================+
| :py:class:`~matlab2cpp.datatype.Type` | Frontend for the datatype string |
+------------------------------------------+---------------------------------------+
| :py:class:`~matlab2cpp.datatype.Dim` | Reference to the number of dimensions |
+------------------------------------------+---------------------------------------+
| :py:class:`~matlab2cpp.datatype.Mem` | Reference to the memory type |
+------------------------------------------+---------------------------------------+
| :py:class:`~matlab2cpp.datatype.Num` | Numerical value indicator |
+------------------------------------------+---------------------------------------+
| :py:class:`~matlab2cpp.datatype.Suggest` | Frontend for suggested datatype |
+------------------------------------------+---------------------------------------+
"""
from . import supplement
dim0 = {"int", "float", "uword", "double", "cx_double", "size_t"}
dim1 = {"ivec", "fvec", "uvec", "vec", "cx_vec"}
dim2 = {"irowvec", "frowvec", "urowvec", "rowvec", "cx_rowvec"}
dim3 = {"imat", "fmat", "umat", "mat", "cx_mat"}
dim4 = {"icube", "fcube", "ucube", "cube", "cx_cube"}
dims = [dim0, dim1, dim2, dim3, dim4]
mem0 = {"uword", "uvec", "urowvec", "umat", "ucube"}
mem1 = {"int", "ivec", "irowvec", "imat", "icube"}
mem2 = {"float", "fvec", "frowvec", "fmat", "fcube"}
mem3 = {"double", "vec", "rowvec", "mat", "cube"}
mem4 = {"cx_double", "cx_vec", "cx_rowvec", "cx_mat", "cx_cube"}
mems = [mem0, mem1, mem2, mem3, mem4]
others = {"char", "string", "TYPE", "func_lambda", "struct", "structs", "cell",
"wall_clock", "SPlot"}
def common_loose(vals):
"""Common denominator among several names.
Loose enforcment"""
if not isinstance(vals, (tuple, list)) or \
isinstance(vals[0], int):
vals = [vals]
vals = list(vals)
for i in range(len(vals)):
if isinstance(vals[i], str):
continue
if isinstance(vals[i][0], int):
vals[i] = get_name(*vals[i])
vals = set(vals)
if len(vals) == 1:
return vals.pop()
vals.discard("TYPE")
if len(vals) == 1:
return vals.pop()
for other in others:
vals.discard(other)
if len(vals) == 0:
return "TYPE"
elif len(vals) == 1:
return vals.pop()
dims_ = map(get_dim, vals)
if dims_:
dim = max(*dims_)
else:
return "TYPE"
if dim == 2 and 1 in dims_:
dim = 3
types = map(get_mem, vals)
type = max(*types)
val = get_name(dim, type)
return val
def common_strict(vals):
"""Common denominator among several names.
Strict enforcment"""
if not isinstance(vals, (tuple, list)) \
or isinstance(vals[0], int):
vals = [vals]
vals = list(vals)
for i in range(len(vals)):
if isinstance(vals[i], str):
continue
if isinstance(vals[i][0], int):
vals[i] = get_name(*vals[i])
vals = set(vals)
if len(vals) == 1:
return vals.pop()
for other in others:
if other in vals:
return "TYPE"
dims_ = map(get_dim, vals)
dim = max(*dims_)
if dim == 2 and 1 in dims_:
return "TYPE"
types = map(get_mem, vals)
type = max(*types)
val = get_name(dim, type)
return val
def pointer_split(name):
p = name.count("*")
if not p:
return 0, name
return p, name[:-p]
def get_dim(val):
while val[-1] == "*":
val = val[:-1]
if val in dim0: dim = 0
elif val in dim1: dim = 1
elif val in dim2: dim = 2
elif val in dim3: dim = 3
elif val in dim4: dim = 4
elif val in others: dim = None
else:
raise ValueError("Datatype '%s' not recognized" % val)
return dim
def get_mem(val):
while val[-1] == "*":
val = val[:-1]
if val in mem0: mem = 0
elif val in mem1: mem = 1
elif val in mem2: mem = 2
elif val in mem3: mem = 3
elif val in mem4: mem = 4
elif val in others: mem = None
else:
raise ValueError("Datatype '%s' not recognized" % val)
return mem
def get_num(val):
while val[-1] == "*":
val = val[:-1]
if val in others: num = False
else: num = True
return num
def get_name(dim, mem):
return dims[dim].intersection(mems[mem]).pop()
def get_type(instance):
if instance.prop["type"] == "TYPE":
instance = instance.declare
return instance.prop["type"]
class Dim(object):
"""
The `node.dim` is a help variable for handling numerical datatype.
It represents the number of dimension a numerical object represents:
+-------+--------------+
| *dim* | Description |
+=======+==============+
| 0 | scalar |
+-------+--------------+
| 1 | (col-)vector |
+-------+--------------+
| 2 | row-vector |
+-------+--------------+
| 3 | matrix |
+-------+--------------+
| 4 | cube |
+-------+--------------+
| None | Other |
+-------+--------------+
The variable can be both read and set in real time:
>>> node = collection.Var(None, "name")
>>> node.type="float"
>>> print(node.dim)
0
>>> node.dim = 3
>>> print(node.type)
fmat
"""
def __get__(self, instance, owner):
if instance is None:
return self
return get_dim(get_type(instance))
def __set__(self, instance, value):
mem = get_mem(get_type(instance))
instance.prop["type"] = get_name(value, mem)
class Mem(object):
"""
The `node.mem` is a help variable for handling numerical datatype.
It represents the internal basic datatype represented in memory:
+-------+-------------+
| *mem* | Description |
+=======+=============+
| 0 | unsiged int |
+-------+-------------+
| 1 | integer |
+-------+-------------+
| 2 | float |
+-------+-------------+
| 3 | double |
+-------+-------------+
| 4 | complex |
+-------+-------------+
| None | Other |
+-------+-------------+
The variable can be both read and set in real time:
>>> node = collection.Var(None, "name")
>>> node.type="float"
>>> print(node.mem)
2
>>> node.mem = 3
>>> print(node.type)
double
"""
def __get__(self, instance, owner):
if instance is None:
return self
return get_mem(get_type(instance))
def __set__(self, instance, value):
dim = get_dim(get_type(instance))
instance.prop["type"] = get_name(dim, value)
class Num(object):
"""
The `node.num` is a help variable for handling numerical datatype. It is
a boolean values which is true given that the datatype is of numerical type.
"""
def __get__(self, instance, owner):
if instance is None:
return self
return get_num(get_type(instance))
def __set__(self, instance, value):
if not value:
instance.prop["type"] = "TYPE"
else:
raise AttributeError("num can not be set True consistently")
class Type(object):
"""
Datatypes can be roughly split into two groups: **numerical** and
**non-numerical** types. The numerical types are as follows:
+-------------+--------------+-----------+-----------+----------+-------------+
| | unsigned int | int | float | double | complex |
+=============+==============+===========+===========+==========+=============+
| scalar | *uword* | *int* | *float* | *double* | *cx_double* |
+-------------+--------------+-----------+-----------+----------+-------------+
| vector | *uvec* | *ivec* | *fvec* | *vec* | *cx_vec* |
+-------------+--------------+-----------+-----------+----------+-------------+
| row\-vector | *urowvec* | *irowvec* | *frowvec* | *rowvec* | *cx_rowvec* |
+-------------+--------------+-----------+-----------+----------+-------------+
| matrix | *umat* | *imat* | *fmat* | *mat* | *cx_mat* |
+-------------+--------------+-----------+-----------+----------+-------------+
| cube | *ucube* | *icube* | *fcube* | *cube* | *cx_cube* |
+-------------+--------------+-----------+-----------+----------+-------------+
Values along the horizontal axis represents the amount of memory reserved per
element, and the along the vertical axis represents the various number of
dimensions. The names are equivalent to the ones in the Armadillo package.
The non-numerical types are as follows:
+---------------+------------------------+
| Name | Description |
+===============+========================+
| *char* | Single text character |
+---------------+------------------------+
| *string* | Text string |
+---------------+------------------------+
| *struct* | Struct container |
+---------------+------------------------+
| *structs* | Struct array container |
+---------------+------------------------+
| *func_lambda* | Anonymous function |
+---------------+------------------------+
The node datatype can be referenced by any node through `node.type` and can be
inserted as placeholder through `%(type)s`.
"""
def __get__(self, instance, owner):
if instance is None:
return self
return get_type(instance)
def __set__(self, instance, value):
value = value or "TYPE"
if isinstance(value, str):
p, value = pointer_split(value)
instance.pointer = p
else:
value = common_strict(value)
instance.prop["type"] = value
class Suggest(object):
"""Same as Type, but for suggested value.
"""
def __set__(self, instance, value):
if value == "TYPE":
return
instance.declare.prop["suggest"] = value
def __get__(self, instance, owner):
return supplement.suggests.get(instance)
if __name__ == "__main__":
import doctest
doctest.testmod()