forked from arrayfire/arrayfire-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.py
More file actions
162 lines (129 loc) · 4.26 KB
/
Copy pathindex.py
File metadata and controls
162 lines (129 loc) · 4.26 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
#######################################################
# Copyright (c) 2015, ArrayFire
# All rights reserved.
#
# This file is distributed under 3-clause BSD license.
# The complete license agreement can be obtained at:
# http://arrayfire.com/licenses/BSD-3-Clause
########################################################
from .library import *
from .util import *
from .base import *
from .broadcast import *
class seq(ct.Structure):
_fields_ = [("begin", ct.c_double),
("end" , ct.c_double),
("step" , ct.c_double)]
def __init__ (self, S):
num = __import__("numbers")
self.begin = ct.c_double( 0)
self.end = ct.c_double(-1)
self.step = ct.c_double( 1)
if is_number(S):
self.begin = ct.c_double(S)
self.end = ct.c_double(S)
elif isinstance(S, slice):
if (S.start is not None):
self.begin = ct.c_double(S.start)
if (S.stop is not None):
self.end = ct.c_double(S.stop - 1)
if (S.step is not None):
self.step = ct.c_double(S.step)
else:
raise IndexError("Invalid type while indexing arrayfire.array")
class parallel_range(seq):
def __init__(self, start, stop=None, step=None):
if (stop is None):
stop = start
start = 0
self.S = slice(start, stop, step)
super(parallel_range, self).__init__(self.S)
def __iter__(self):
return self
def next(self):
if bcast.get() is True:
bcast.toggle()
raise StopIteration
else:
bcast.toggle()
return self
def __next__(self):
return self.next()
def slice_to_length(key, dim):
tkey = [key.start, key.stop, key.step]
if tkey[0] is None:
tkey[0] = 0
elif tkey[0] < 0:
tkey[0] = dim - tkey[0]
if tkey[1] is None:
tkey[1] = dim
elif tkey[1] < 0:
tkey[1] = dim - tkey[1]
if tkey[2] is None:
tkey[2] = 1
return int(((tkey[1] - tkey[0] - 1) / tkey[2]) + 1)
class uidx(ct.Union):
_fields_ = [("arr", ct.c_longlong),
("seq", seq)]
class index(ct.Structure):
_fields_ = [("idx", uidx),
("isSeq", ct.c_bool),
("isBatch", ct.c_bool)]
def __init__ (self, idx):
self.idx = uidx()
self.isBatch = False
self.isSeq = True
if isinstance(idx, base_array):
self.idx.arr = idx.arr
self.isSeq = False
elif isinstance(idx, parallel_range):
self.idx.seq = idx
self.isBatch = True
else:
self.idx.seq = seq(idx)
def get_indices(key, n_dims):
index_vec = index * n_dims
inds = index_vec()
for n in range(n_dims):
inds[n] = index(slice(None))
if isinstance(key, tuple):
n_idx = len(key)
for n in range(n_idx):
inds[n] = index(key[n])
else:
inds[0] = index(key)
return inds
def get_assign_dims(key, idims):
dims = [1]*4
for n in range(len(idims)):
dims[n] = idims[n]
if is_number(key):
dims[0] = 1
return dims
elif isinstance(key, slice):
dims[0] = slice_to_length(key, idims[0])
return dims
elif isinstance(key, parallel_range):
dims[0] = slice_to_length(key.S, idims[0])
return dims
elif isinstance(key, base_array):
dims[0] = key.elements()
return dims
elif isinstance(key, tuple):
n_inds = len(key)
if (n_inds > len(idims)):
raise IndexError("Number of indices greater than array dimensions")
for n in range(n_inds):
if (is_number(key[n])):
dims[n] = 1
elif (isinstance(key[n], base_array)):
dims[n] = key[n].elements()
elif (isinstance(key[n], slice)):
dims[n] = slice_to_length(key[n], idims[n])
elif (isinstance(key[n], parallel_range)):
dims[n] = slice_to_length(key[n].S, idims[n])
else:
raise IndexError("Invalid type while assigning to arrayfire.array")
return dims
else:
raise IndexError("Invalid type while assigning to arrayfire.array")