11require (' arrayfire.lib'  )
22require (' arrayfire.defines'  )
3+ require (' arrayfire.dim4'  )
34local  ffi   =  require ( " ffi"   )
45
56local  funcs  =  {}
@@ -67,15 +68,22 @@ Array.__index = Array
6768
6869local  c_dim4_t  =  af .ffi .c_dim4_t 
6970local  c_uint_t  =  af .ffi .c_uint_t 
70- local  c_array_p  =  af .ffi .c_array_p 
71+ local  c_ptr_t  =  af .ffi .c_ptr_t 
72+ local  Dim4  =  af .Dim4 
7173
72- local  add_finalizer  =  function (arr_ptr )
73-    return  ffi .gc (arr_ptr [0 ], af .clib .af_release_array )
74+ local  c_array_p  =  function (ptr )
75+    local  arr_ptr  =  ffi .new (' void *[1]'  , ptr )
76+    arr_ptr [0 ] =  ffi .gc (arr_ptr [0 ], af .clib .af_release_array )
77+    return  arr_ptr 
7478end 
7579
76- Array . __init   =  function (data ,  dims ,  dtype ,  source )
80+ local   init   =  function (ptr )
7781   local  self  =  setmetatable ({}, Array )
82+    self ._array  =  ptr 
83+    return  self 
84+ end 
7885
86+ Array .__init  =  function (data , dims , dtype , source )
7987   if  data  then 
8088      assert (af .istable (data ))
8189   end 
@@ -87,32 +95,109 @@ Array.__init = function(data, dims, dtype, source)
8795   c_dims  =  c_dim4_t (dims  or  (data  and  {# data } or  {}))
8896   c_ndims  =  c_uint_t (dims  and  # dims  or  (data  and  1  or  0 ))
8997
90-    nelement  =  1 
98+    count  =  1 
9199   for  i  =  1 ,tonumber (c_ndims ) do 
92-       nelement  =  nelement  *  c_dims [i  -  1 ]
100+       count  =  count  *  c_dims [i  -  1 ]
93101   end 
94-    nelement  =  tonumber (nelement )
102+    count  =  tonumber (count )
95103
96104   local  atype  =  dtype  or  af .dtype .f32 
97105   local  res  =  c_array_p ()
98106   if  not  data  then 
99107      af .clib .af_create_handle (res , c_ndims , c_dims , atype )
100108   else 
101-       c_data  =  ffi . new (af .dtype_names [atype  +  1 ]  ..   ' [?] ' ,  nelement , data )
109+       c_data  =  c_ptr_t (af .dtype_names [atype  +  1 ],  count , data )
102110      af .clib .af_create_array (res , c_data , c_ndims , c_dims , atype )
103111   end 
104-    self .__arr  =  add_finalizer (res )
105-    return  self 
112+    return  Array .init (res [0 ])
106113end 
107114
108115Array .__tostring  =  function (self )
109116   return  ' arrayfire.Array\n ' 
110117end 
111118
112119Array .get  =  function (self )
113-    return  self .__arr 
120+    return  self ._array 
121+ end 
122+ 
123+ --  TODO: implement Array.write
124+ 
125+ Array .copy  =  function (self )
126+    local  res  =  c_array_p ()
127+    af .clib .af_copy_array (res , self ._array )
128+    return  Array .init (res [0 ])
129+ end 
130+ 
131+ Array .softCopy  =  function (self )
132+    local  res  =  c_array_p ()
133+    af .clib .af_copy_array (res , self ._array )
134+    return  Array .init (res [0 ])
135+ end 
136+ 
137+ Array .elements  =  function (self )
138+    local  res  =  c_ptr_t (' dim_t'  )
139+    af .clib .af_get_elements (res , self ._array )
140+    return  tonumber (res [0 ])
114141end 
115142
143+ Array .type  =  function (self )
144+    local  res  =  c_ptr_t (' af_dtype'  )
145+    af .clib .af_get_type (res , self ._array )
146+    return  tonumber (res [0 ])
147+ end 
148+ 
149+ Array .typeName  =  function (self )
150+    local  res  =  c_ptr_t (' af_dtype'  )
151+    af .clib .af_get_type (res , self ._array )
152+    return  af .dtype_names [tonumber (res [0 ])]
153+ end 
154+ 
155+ Array .dims  =  function (self )
156+    local  res  =  c_dim4_t ()
157+    af .clib .af_get_dims (res  +  0 , res  +  1 , res  +  2 , res  +  3 , self ._array )
158+    return  Dim4 (tonumber (res [0 ]), tonumber (res [1 ]),
159+                tonumber (res [2 ]), tonumber (res [3 ]))
160+ end 
161+ 
162+ Array .numdims  =  function (self )
163+    local  res  =  c_ptr_t (' unsigned int'  )
164+    af .clib .af_get_numdims (res , self ._array )
165+    return  tonumber (res [0 ])
166+ end 
167+ 
168+ local  funcs  =  {
169+    isEmpty         =  ' is_empty'  ,
170+    isScalar        =  ' is_scalar'  ,
171+    isRow           =  ' is_row'  ,
172+    isColumn        =  ' is_column'  ,
173+    isVector        =  ' is_vector'  ,
174+    isComplex       =  ' is_complex'  ,
175+    isReal          =  ' is_real'  ,
176+    isDouble        =  ' is_double'  ,
177+    isSingle        =  ' is_single'  ,
178+    isRealFloating  =  ' is_realfloating'  ,
179+    isFloating      =  ' is_floating'  ,
180+    isInteger       =  ' is_integer'  ,
181+    isBool          =  ' is_bool'  ,
182+ }
183+ 
184+ for  name , cname  in  pairs (funcs ) do 
185+    Array [name ] =  function (self )
186+       local  res  =  c_ptr_t (' bool'  )
187+       af .clib [' af_'  ..  cname ](res , self ._array )
188+       return  res [0 ]
189+    end 
190+ end 
191+ 
192+ Array .eval  =  function (self )
193+    af .clib .af_eval (self ._array )
194+ end 
195+ 
196+ --  Useful aliases
197+ Array .ndims  =  Array .numdims 
198+ Array .nElement  =  Array .elements 
199+ Array .clone  =  Array .copy 
200+ 
116201setmetatable (
117202   Array ,
118203   {
@@ -124,3 +209,5 @@ setmetatable(
124209
125210af .Array  =  Array 
126211af .ffi .add_finalizer  =  add_finalizer 
212+ af .ffi .c_array_p  =  c_array_p 
213+ af .Array .init  =  init 
0 commit comments