Converting a c++ array to Lisp vector faster
Posted: Sun May 11, 2014 4:57 am
I have this function that can convert a C++ vector to a Lisp vector, I would like to make faster. First I would like to show it to you and then explain how the functions involved work. The closer I can get to a 0(1) op the better.
I have this function c-arr-to-vec-int which converts a c array of integers created by foreign-alloc to a C++ vector<int>. It is used like this:
the 5 at the end is a length parameter. To convert the newly formed vector<int>, A, just created back to a c array which is the same as a (foreign-alloc :int :initial-contents '(1 2 3 4 5)), I would use my other function vec-int-to-c-arr like this:
The output of vec-int-to-c-arr is mem-aref-able since it is the same as:
So I can do this:
The vec-int-length function just retrieve a C++ vector length.
Code: Select all
(defmacro vec-int-to-lisp-vec (&rest args)
(let ((x (gensym))
(y (gensym))
(z (gensym)))
`(let* ((,x (first (list ,@args)))
(,y (vec-int-length ,x))
(,z (make-array ,y :element-type t :fill-pointer 0
:initial-element
nil)))
(dotimes (i ,y)
(vector-push (mem-aref (vec-int-to-c-arr ,x) :int i) ,z))
,z)))
I have this function c-arr-to-vec-int which converts a c array of integers created by foreign-alloc to a C++ vector<int>. It is used like this:
Code: Select all
(defparameter a (c-arr-to-vec-int (foreign-alloc :int :initial-contents '(1 2 3 4 5)) 5))
Code: Select all
CV> (vec-int-to-c-arr a)
#.(SB-SYS:INT-SAP #X7FFF965C3040) <--This is analagous to (foreign-alloc :int :initial-contents '(1 2 3 4 5))
Code: Select all
(foreign-alloc :int :initial-contents '(1 2 3 4 5))
Code: Select all
CV> (mem-aref (vec-int-to-c-arr a) :int)
1
CV> (mem-aref (vec-int-to-c-arr a) :int 1)
2
The vec-int-length function just retrieve a C++ vector length.