types.py 1.36 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import sympy as sp
from sympy.core.cache import cacheit


class TypedSymbol(sp.Symbol):

    def __new__(cls, name, *args, **kwds):
        obj = TypedSymbol.__xnew_cached_(cls, name, *args, **kwds)
        return obj

    def __new_stage2__(cls, name, dtype):
        obj = super(TypedSymbol, cls).__xnew__(cls, name)
        obj._dtype = dtype
        return obj

    __xnew__ = staticmethod(__new_stage2__)
    __xnew_cached_ = staticmethod(cacheit(__new_stage2__))

    @property
    def dtype(self):
        return self._dtype

    def _hashable_content(self):
        superClassContents = list(super(TypedSymbol, self)._hashable_content())
25
        t = tuple(superClassContents + [hash(repr(self._dtype))])
26
        return t
27
28
29
30

    def __getnewargs__(self):
        return self.name, self.dtype

31
32
33
34
35
36
37
38
39

_c_dtype_dict = {0: 'int', 1: 'double', 2: 'float'}
_dtype_dict = {'int': 0, 'double': 1, 'float': 2}


class DataType(object):
    def __init__(self, dtype):
        self.alias = True
        self.const = False
40
        self.ptr = False
41
42
43
44
45
46
        if isinstance(dtype, str):
            self.dtype = _dtype_dict[dtype]
        else:
            self.dtype = dtype

    def __repr__(self):
47
48
        return "{!s} {!s}{!s} {!s}".format("const" if self.const else "", _c_dtype_dict[self.dtype],
                                           "*" if self.ptr else "", "__restrict__" if not self.alias else "")