-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.lisp
64 lines (55 loc) · 2.51 KB
/
common.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
(in-package #:xdb2)
(defun slot-val (instance slot-name)
(if (and instance
(slot-boundp instance slot-name))
(slot-value instance slot-name)))
(defgeneric get-val (object element &key data-type)
(:documentation "Returns the value in a object based on the supplied element name and possible type hints."))
(defgeneric (setf get-val) (new-value object element &key data-type)
(:documentation "Set the value in a object based on the supplied element name and possible type hints."))
(defmethod get-val (object element &key data-type)
(when object
(typecase (or data-type object)
(hash-table
(gethash element object))
(standard-object
(slot-val object element))
(t
(if data-type
(cond
((equal 'alist data-type)
(second (assoc element object :test #'equal)))
((equal 'plist data-type)
(get object element))
(t
(error "Does not handle this type of object. Implement your own get-val method.")))
(if (listp object)
(second (assoc element object :test #'equal))
(error "Does not handle this type of object. Implement your own get-val method.")))))))
(defmethod (setf get-val) (new-value object element &key data-type)
(typecase (or data-type object)
(hash-table (setf (gethash element object) new-value))
(standard-object (setf (slot-value object element) new-value))
(t
(if data-type
(cond ((equal 'alist data-type)
(replace object (list (list element new-value))))
((equal 'plist data-type)
;;TODO: Implement this properly.
(get object element ))
(t
(error "Does not handle this type of object. Implement your own get-val method.")))
(if (listp object)
(replace object (list (list element new-value)))
(error "Does not handle this type of object. Implement your own get-val method."))))))
(defun copy-array (array)
(let ((new-array
(make-array (array-dimensions array)
:element-type (array-element-type array)
:adjustable (adjustable-array-p array)
:fill-pointer (and (array-has-fill-pointer-p array)
(fill-pointer array)))))
(loop for i below (array-total-size array)
do (setf (row-major-aref new-array i)
(row-major-aref array i)))
new-array))