Skip to content

Commit 3bb209c

Browse files
committed
Arrays: Prepare count pointer for the future matrix shapes
Ref: dss-extensions/dss_capi#113
1 parent 1e64689 commit 3bb209c

File tree

3 files changed

+43
-13
lines changed

3 files changed

+43
-13
lines changed

dss/IObj.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,7 @@ def _set_obj(self, idx: int, other):
404404

405405
def _get_obj_array(self, idx: int, pycls):
406406
ptr = self._ffi.new('void***')
407-
cnt = self._ffi.new('int32_t[2]')
407+
cnt = self._ffi.new('int32_t[4]')
408408
self._lib.Obj_GetObjectArray(ptr, cnt, self._ptr, idx)
409409
if not cnt[0]:
410410
self._lib.DSS_Dispose_PPointer(ptr)
@@ -453,7 +453,7 @@ def __init__(self, api_util, **kwargs):
453453
self._ffi = api_util.ffi
454454

455455
self.pointer = self._ffi.new('void***')
456-
self.count = self._ffi.new('int32_t[2]')
456+
self.count = self._ffi.new('int32_t[4]')
457457
if len(kwargs) == 0:
458458
self._lib.Batch_CreateByClass(self.pointer, self.count, self._cls_idx)
459459
self._check_for_error()

dss/_cffi_api_util.py

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -220,51 +220,81 @@ def get_string(self, b):
220220

221221
def get_float64_array(self, func, *args):
222222
ptr = self.ffi.new('double**')
223-
cnt = self.ffi.new('int32_t[2]')
223+
cnt = self.ffi.new('int32_t[4]')
224224
func(ptr, cnt, *args)
225225
res = np.frombuffer(self.ffi.buffer(ptr[0], cnt[0] * 8), dtype=np.float64).copy()
226226
self.lib.DSS_Dispose_PDouble(ptr)
227+
228+
if cnt[3] != 0:
229+
# If the last element is filled, we have a matrix. Otherwise, the
230+
# matrix feature is disabled or the result is indeed a vector
231+
return res.reshape((cnt[2], cnt[3]))
232+
227233
return res
228-
234+
235+
229236
def get_float64_gr_array(self):
230237
ptr, cnt = self.gr_float64_pointers
238+
if cnt[3] != 0:
239+
return np.frombuffer(self.ffi.buffer(ptr[0], cnt[0] * 8), dtype=np.float64).copy().reshape((cnt[2], cnt[3]))
240+
231241
return np.frombuffer(self.ffi.buffer(ptr[0], cnt[0] * 8), dtype=np.float64).copy()
232242

233243
def get_int32_array(self, func, *args):
234244
ptr = self.ffi.new('int32_t**')
235-
cnt = self.ffi.new('int32_t[2]')
245+
cnt = self.ffi.new('int32_t[4]')
236246
func(ptr, cnt, *args)
237247
res = np.frombuffer(self.ffi.buffer(ptr[0], cnt[0] * 4), dtype=np.int32).copy()
238248
self.lib.DSS_Dispose_PInteger(ptr)
249+
250+
if cnt[3] != 0:
251+
# If the last element is filled, we have a matrix. Otherwise, the
252+
# matrix feature is disabled or the result is indeed a vector
253+
return res.reshape((cnt[2], cnt[3]))
254+
239255
return res
240256

257+
241258
def get_ptr_array(self, func, *args):
242259
ptr = self.ffi.new('void***')
243-
cnt = self.ffi.new('int32_t[2]')
260+
cnt = self.ffi.new('int32_t[4]')
244261
func(ptr, cnt, *args)
245262
res = np.frombuffer(self.ffi.buffer(ptr[0], cnt[0] * np.dtype(np.uintp).itemsize), dtype=np.uintp).copy()
246263
self.lib.DSS_Dispose_PPointer(ptr)
247264
return res
248265

249266
def get_int32_gr_array(self):
250267
ptr, cnt = self.gr_int32_pointers
268+
if cnt[3] != 0:
269+
return np.frombuffer(self.ffi.buffer(ptr[0], cnt[0] * 4), dtype=np.int32).copy().reshape((cnt[2], cnt[3]))
270+
251271
return np.frombuffer(self.ffi.buffer(ptr[0], cnt[0] * 4), dtype=np.int32).copy()
252272

273+
253274
def get_int8_array(self, func, *args):
254275
ptr = self.ffi.new('int8_t**')
255-
cnt = self.ffi.new('int32_t[2]')
276+
cnt = self.ffi.new('int32_t[4]')
256277
func(ptr, cnt, *args)
257278
res = np.frombuffer(self.ffi.buffer(ptr[0], cnt[0] * 1), dtype=np.int8).copy()
258279
self.lib.DSS_Dispose_PByte(ptr)
280+
281+
if cnt[3] != 0:
282+
# If the last element is filled, we have a matrix. Otherwise, the
283+
# matrix feature is disabled or the result is indeed a vector
284+
return res.reshape((cnt[2], cnt[3]))
285+
259286
return res
260287

261288
def get_int8_gr_array(self):
262289
ptr, cnt = self.gr_int8_pointers
290+
if cnt[3] != 0:
291+
return np.frombuffer(self.ffi.buffer(ptr[0], cnt[0] * 1), dtype=np.int8).copy().reshape((cnt[2], cnt[3]))
292+
263293
return np.frombuffer(self.ffi.buffer(ptr[0], cnt[0] * 1), dtype=np.int8).copy()
264294

265295
def get_string_array(self, func, *args):
266296
ptr = self.ffi.new('char***')
267-
cnt = self.ffi.new('int32_t[2]')
297+
cnt = self.ffi.new('int32_t[4]')
268298
func(ptr, cnt, *args)
269299
if not cnt[0]:
270300
res = []
@@ -283,7 +313,7 @@ def get_string_array(self, func, *args):
283313

284314
def get_string_array2(self, func, *args): # for compatibility with OpenDSSDirect.py
285315
ptr = self.ffi.new('char***')
286-
cnt = self.ffi.new('int32_t[2]')
316+
cnt = self.ffi.new('int32_t[4]')
287317
func(ptr, cnt, *args)
288318

289319
if not cnt[0]:
@@ -308,7 +338,7 @@ def get_string_array2(self, func, *args): # for compatibility with OpenDSSDirect
308338

309339
def get_float64_array2(self, func, *args):
310340
ptr = self.ffi.new('double**')
311-
cnt = self.ffi.new('int32_t[2]')
341+
cnt = self.ffi.new('int32_t[4]')
312342
func(ptr, cnt, *args)
313343
if not cnt[0]:
314344
res = []
@@ -324,7 +354,7 @@ def get_float64_gr_array2(self):
324354

325355
def get_int32_array2(self, func, *args):
326356
ptr = self.ffi.new('int32_t**')
327-
cnt = self.ffi.new('int32_t[2]')
357+
cnt = self.ffi.new('int32_t[4]')
328358
func(ptr, cnt, *args)
329359
if not cnt[0]:
330360
res = None
@@ -340,7 +370,7 @@ def get_int32_gr_array2(self):
340370

341371
def get_int8_array2(self, func, *args):
342372
ptr = self.ffi.new('int8_t**')
343-
cnt = self.ffi.new('int32_t[2]')
373+
cnt = self.ffi.new('int32_t[4]')
344374
func(ptr, cnt, *args)
345375
if not cnt[0]:
346376
res = None

dss/dss_capi_ir/IZIP.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ def Extract(self, FileName):
5555
'''
5656
api_util = self._api_util
5757
ptr = api_util.ffi.new('int8_t**')
58-
cnt = api_util.ffi.new('int32_t[2]')
58+
cnt = api_util.ffi.new('int32_t[4]')
5959
if type(FileName) is not bytes:
6060
FileName = FileName.encode(api_util.codec)
6161

0 commit comments

Comments
 (0)