-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.lisp
76 lines (62 loc) · 2.73 KB
/
utils.lisp
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
66
67
68
69
70
71
72
73
74
75
76
(in-package :nlopt)
(defmacro ensure-success (&body body)
`(assert (eql :nlopt_success (progn ,@body))))
(deftype doubles ()
"Array of double floats"
'(array double-float *))
(defun doubles (list)
"Create an array of double floats from given `list' of double floats"
(make-array (length list) :element-type 'double-float
:initial-contents list))
(defun doubles* (list)
"Create an array of double floats from given `list' (coerces numbers to double-float)"
(make-array (length list) :element-type 'double-float
:initial-contents (loop for x in list
collect (coerce x 'double-float))))
(defun doubles-array (size initial-element)
"Create oan array of double floats of `size' with `initial-element'"
(make-array size :element-type 'double-float
:initial-element initial-element))
(defun doubles-array* (size initial-element)
"Create oan array of double floats of `size' with `initial-element' (coerces `initial-element' to double-float)"
(make-array size :element-type 'double-float
:initial-element (coerce initial-element 'double-float)))
(declaim (inline dref))
(defun dref (ptr index)
"Return element of foreign double float array (`ptr') at `index' position"
(cffi:mem-aref ptr :double index))
(defun (setf dref) (val ptr index)
"Set element of foreign double float array `ptr' at `index' to value `val'"
(setf (cffi:mem-aref ptr :double index) val))
(defun setf-doubles2 (ptr data)
"Set elements of a foreign array of doubles"
(loop for d double-float in data
for i integer from 0 do
(setf (cffi:mem-aref ptr :double i) d)))
(defun setf-doubles (ptr &rest data)
"Set elements of a foreign array of doubles"
(loop for d double-float in data
for i integer from 0 do
(setf (cffi:mem-aref ptr :double i) d)))
(defun %dreffing@ (var expr)
(cond ((atom expr)
(if (and (symbolp expr)
(char-equal #\@ (aref (symbol-name expr) 0)))
`(dref ,var ,(parse-integer (symbol-name expr) :start 1))
expr))
((listp expr)
(cons (first expr) (mapcar (lambda (e) (%dreffing@ var e)) (rest expr))))))
(defmacro dreffing@ (x &body body)
"replace occurance of @n with (dref x n) in body
Usefull for avoiding "
`(progn ,@(mapcar (lambda (e) (%dreffing@ x e))
body)))
(defun foreign-darray-to-lisp (ptr)
(cffi:foreign-array-to-lisp ptr 'double-float))
(defmacro with-vector-ptr-to (vector &body body)
(assert (symbolp vector))
`(if ,vector
(cffi:with-pointer-to-vector-data (,vector ,vector)
,@body)
(let ((,vector (cffi:null-pointer)))
,@body)))