|
| 1 | +import array as host |
1 | 2 | from .library import * |
| 3 | +from .util import dim4 |
| 4 | + |
| 5 | +def create_array(buf, numdims, idims, dtype): |
| 6 | + out_arr = c_longlong(0) |
| 7 | + c_dims = dim4(idims[0], idims[1], idims[2], idims[3]) |
| 8 | + clib.af_create_array(pointer(out_arr), c_longlong(buf), numdims, pointer(c_dims), dtype) |
| 9 | + return out_arr |
2 | 10 |
|
3 | 11 | class array(object): |
4 | 12 |
|
5 | | - def __init__(self): |
| 13 | + def __init__(self, src=None, dims=(0,)): |
| 14 | + |
6 | 15 | self.arr = c_longlong(0) |
7 | 16 |
|
| 17 | + buf=None |
| 18 | + buf_len=0 |
| 19 | + type_char='f' |
| 20 | + dtype = f32 |
| 21 | + |
| 22 | + if src is not None: |
| 23 | + |
| 24 | + if isinstance(src, host.array): |
| 25 | + buf,buf_len = src.buffer_info() |
| 26 | + type_char = src.typecode |
| 27 | + elif isinstance(src, list): |
| 28 | + tmp = host.array('f', src) |
| 29 | + buf,buf_len = tmp.buffer_info() |
| 30 | + type_char = tmp.typecode |
| 31 | + else: |
| 32 | + raise TypeError("src is an object of unsupported class") |
| 33 | + |
| 34 | + elements = 1 |
| 35 | + numdims = len(dims) |
| 36 | + idims = [1]*4 |
| 37 | + |
| 38 | + for i in range(numdims): |
| 39 | + elements *= dims[i] |
| 40 | + idims[i] = dims[i] |
| 41 | + |
| 42 | + if (elements == 0): |
| 43 | + idims = [buf_len, 1, 1, 1] |
| 44 | + numdims = 1 |
| 45 | + |
| 46 | + if type_char == 'f': |
| 47 | + dtype = f32 |
| 48 | + elif type_char == 'd': |
| 49 | + dtype = f64 |
| 50 | + elif type_char == 'b': |
| 51 | + dtype = b8 |
| 52 | + elif type_char == 'B': |
| 53 | + dtype = u8 |
| 54 | + elif type_char == 'i': |
| 55 | + dtype = s32 |
| 56 | + elif type_char == 'I': |
| 57 | + dtype = u32 |
| 58 | + elif type_char == 'l': |
| 59 | + dtype = s64 |
| 60 | + elif type_char == 'L': |
| 61 | + dtype = u64 |
| 62 | + |
| 63 | + self.arr = create_array(buf, numdims, idims, dtype) |
| 64 | + |
8 | 65 | def __del__(self): |
9 | 66 | if (self.arr.value != 0): |
10 | 67 | clib.af_release_array(self.arr) |
0 commit comments