Guest User

Untitled

a guest
Nov 22nd, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.58 KB | None | 0 0
  1. (defpackage aligned-structure
  2. (:use :cl :alexandria :iterate :trivia))
  3.  
  4. (in-package :aligned-structure)
  5.  
  6. (defvar *default-allocation-size* 1024)
  7. (defmacro define-aligned-structure (name-and-options &body slots)
  8. (setf name-and-options (ensure-list name-and-options))
  9. `(progn (defstruct ,name-and-options
  10. ,@(iter (for s in slots)
  11. (collecting
  12. (ematch s
  13. ((and name (symbol))
  14. `(,name (make-array *default-allocation-size* :adjustable t) :type array))
  15. ((list* name default (and (property :type type '*)
  16. (property :read-only ro nil)))
  17. `(,name (make-array *default-allocation-size* :adjustable t
  18. :element-type ',type
  19. :initial-element ,default)
  20. :type (array ,type)
  21. :read-only ,ro))))))
  22.  
  23. ,@(iter (for s in slots)
  24. (ematch s
  25. ((or (and name (symbol)
  26. (<> type '*)
  27. (<> ro nil))
  28. (list* name _ (plist :type type :read-only ro)))
  29. (let* ((accessor (symbolicate (first name-and-options) '- name))
  30. (aref (symbolicate accessor '-aref))
  31. (struct (first name-and-options)))
  32. (collecting
  33. `(declaim (inline ,aref)))
  34. (collecting
  35. `(declaim (ftype (function (,struct (integer 0 ,array-dimension-limit)) ,type) ,aref)))
  36. (collecting
  37. `(defun ,aref (objects index)
  38. (aref (,accessor objects)
  39. index)))
  40. (unless ro
  41. (collecting
  42. `(declaim (inline (setf ,aref))))
  43. (collecting
  44. `(declaim (ftype (function (,type ,struct (integer 0 ,array-dimension-limit)) ,type) (setf ,aref))))
  45. (collecting
  46. `(defun (setf ,aref) (newval objects index)
  47. (setf (aref (,accessor objects)
  48. index)
  49. newval))))))))))
  50.  
  51. (define-aligned-structure points
  52. (x 0.0d0 :type double-float :read-only t)
  53. (y 0.0d0 :type double-float)
  54. (z 0.0d0 :read-only t))
Add Comment
Please, Sign In to add comment