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
|
from suitesparse_graphblas import check_status, exceptions, ffi, lib
def free(v):
"""Free a scalar."""
check_status(v, lib.GxB_Scalar_free(v))
def new(T, *, free=free):
"""Create a new `GxB_Scalar` of type `T` and initialize it.
The `free` argument is called when the object is garbage
collected, the default is `scalar.free()`. If `free` is None then
there is no automatic garbage collection and it is up to the user
to free the scalar.
>>> S = new(lib.GrB_UINT8)
"""
s = ffi.new("GxB_Scalar*")
check_status(s, lib.GxB_Scalar_new(s, T))
if free:
return ffi.gc(s, free)
return s
def type(s):
"""Return the GraphBLAS type of the scalar.
>>> S = new(lib.GrB_UINT8)
>>> type(S) == lib.GrB_UINT8
True
"""
T = ffi.new("GrB_Type*")
check_status(s, lib.GxB_Scalar_type(T, s[0]))
return T[0]
def set_bool(s, value):
"""Set a boolean value to the scalar.
>>> s = new(lib.GrB_BOOL)
>>> set_bool(s, True)
>>> bool(s) == True
True
"""
check_status(s, lib.GxB_Scalar_setElement_BOOL(s[0], value))
def bool(s):
"""Get a boolean value from the scalar.
>>> s = new(lib.GrB_BOOL)
>>> set_bool(s, True)
>>> bool(s) == True
True
"""
value = ffi.new("bool*")
res = check_status(s, lib.GxB_Scalar_extractElement_BOOL(value, s[0]))
if res == exceptions.NoValue:
return None
return value[0]
|