How do I convert C++ 2 dimensional array to Common Lisp/CFFI

Discussion of Common Lisp

How do I convert C++ 2 dimensional array to Common Lisp/CFFI

Postby joeish80829 » Mon Mar 10, 2014 9:21 am

Here is what I'm trying to convert to Lisp with CFFI

Code: Select all
float trainingData[4][2] = { {501, 10}, {255, 10}, {501, 255}, {10, 501} };


I was told by a CFFI developer that the only way to do it was with this undocumented CFFI function

Code: Select all
(defun foreign-array-alloc (array array-type)
  "Allocate a foreign array containing the elements of lisp array.
The foreign array must be freed with foreign-array-free."
  (check-type array array)
  (let* ((type (follow-typedefs (parse-type array-type)))
         (ptr (foreign-alloc (element-type type)
                             :count (reduce #'* (dimensions type)))))
    (lisp-array-to-foreign array ptr array-type)
    ptr))


The thing is he didn't tell me how and I think it would be better if I heard how to use it from others mouth other than piecing together a hack that might break in code I distribute. So if anyone could throw me a quick how to...I'd be forever grateful=) If this breaks the rules I apologize but I do like to be safe whenever possible. I will edit my code with attempts if neccessary though.
joeish80829
 
Posts: 153
Joined: Tue Sep 03, 2013 5:32 am

Re: How do I convert C++ 2 dimensional array to Common Lisp/

Postby pjstirling » Mon Mar 10, 2014 5:10 pm

You asked this question in November and I answered it then.

viewtopic.php?f=2&t=4226
pjstirling
 
Posts: 84
Joined: Sun Nov 28, 2010 4:21 pm

Re: How do I convert C++ 2 dimensional array to Common Lisp/

Postby joeish80829 » Thu Mar 13, 2014 9:17 pm

I couldn't find anything on that page maybe you can show me where...that's why I ended up asking again. I figured out I can run
Code: Select all
(foreign-array-alloc #2A((8 7) (6 5) (4 3) (2 1)) '(:array :int 4 2))
to create a 2 dimensional array. A cffi developer told me that but was unclear so I googled "foreign-array-alloc" with quotes and found 2 incomplete examples and just figured it out...Thank you for all your help with this. Its nice to know there are people out there that care about the code
joeish80829
 
Posts: 153
Joined: Tue Sep 03, 2013 5:32 am

Re: How do I convert C++ 2 dimensional array to Common Lisp/

Postby Goheeca » Fri Mar 14, 2014 6:00 am

By row-major-order was meant to do something like this:
Code: Select all
(defvar *2d-array* #2A((1 2 3) (4 5 6)))
(foreign-alloc :int
               :initial-contents (make-array (reduce #'* (array-dimensions *2d-array*))
                                             :displaced-to *2d-array*))

But your aferomentioned solution is neater.
cl-2dsyntax is my attempt to create a Python-like reader. My mirror of CLHS (and the dark themed version).
User avatar
Goheeca
 
Posts: 220
Joined: Thu May 10, 2012 12:54 pm


Return to Common Lisp

Who is online

Users browsing this forum: Yahoo [Bot] and 3 guests

cron