<commit_before>(in-package :rtg-math.vectors)

;;----------------------------------------------------------------

(define-compiler-macro swizzle (&whole form vec pattern)
  (if (keywordp pattern)
      (let* ((name (cl:symbol-name pattern))
             (len (cl:length name))
             (arr (gensym "arr")))
        (if (or (> len 4) (< len 2))
            (error "Vectors: swizzle: Cepl vectors cannot have a length less that 2 or greater than 4")
            `(cl:let ((,arr ,vec))
               (cl:make-array
                ,len :element-type 'single-float :initial-contents
                (list ,@(loop :for char :across name
                           :collect `(aref ,arr ,(or (position char '(#\X #\Y #\Z #\W))
						     (error "Vectors: swizzle: Pattern component was not X, Y, Z or W: ~a" char)))))))))
      form))

(defun swizzle (vec pattern)
  (let* ((name (cl:symbol-name pattern))
         (len (cl:length name))
         (result (cl:make-array (cl:length name) :element-type 'single-float)))
    (if (or (> len 4) (< len 2))
        (error "Vectors: swizzle: Cepl vectors cannot have a length less that 2 or greater than 4")
        (loop :for char :across name :for i :from 0 :do
           (setf (aref result i)
                 (aref vec (or (position char '(#\X #\Y #\Z #\W))
			       (error "Vectors: swizzle: Pattern component was not X, Y, Z or W: ~a" char))))))
    result))

(defun s~ (vec pattern) (swizzle vec pattern))
(define-compiler-macro s~ (vec pattern) `(swizzle ,vec ,pattern))

(defmacro dvec (var-list expression &body body)
  (when (or (not (listp var-list))
	    (not (every #'symbolp var-list))
	    (< (cl:length var-list) 1)
	    (> (cl:length var-list) 4))
    (error "dvec: invalid vector destructuring pattern ~s"
	   var-list))
  (let ((e (gensym "expression")))
    `(let ((,e ,expression))
       (let ,(loop :for v :in var-list :for i :from 0 :collect<commit_msg>add setf of swizzle<commit_after>(in-package :rtg-math.vectors)

;;----------------------------------------------------------------

(define-compiler-macro swizzle (&whole form vec pattern)
  (if (keywordp pattern)
      (let* ((name (cl:symbol-name pattern))
             (len (cl:length name))
             (arr (gensym "arr")))
        (if (or (> len 4) (< len 2))
            (error "Vectors: swizzle: Cepl vectors cannot have a length less that 2 or greater than 4")
            `(cl:let ((,arr ,vec))
               (cl:make-array
                ,len :element-type 'single-float :initial-contents
                (list ,@(loop :for char :across name
                           :collect `(aref ,arr ,(or (position char "XYZW")
						     (error "Vectors: swizzle: Pattern component was not X, Y, Z or W: ~a" char)))))))))
      form))

(defun swizzle (vec pattern)
  (let* ((name (cl:symbol-name pattern))
         (len (cl:length name))
         (result (cl:make-array (cl:length name) :element-type 'single-float)))
    (if (or (> len 4) (< len 2))
        (error "Vectors: swizzle: Cepl vectors cannot have a length less that 2 or greater than 4")
        (loop :for char :across name :for i :from 0 :do
           (setf (aref result i)
                 (aref vec (or (position char "XYZW")
			       (error "Vectors: swizzle: Pattern component was not X, Y, Z or W: ~a" char))))))
    result))

(defun (setf swizzle) (value vec pattern)
  (let* ((name (cl:symbol-name pattern))
         (len (cl:length name)))
    (if (or (> len 4) (< len 2))
        (error "Vectors: swizzle: Cepl vectors cannot have a length less that 2 or greater than 4")
        (loop :for char :across name :for i :from 0 :do
           (setf (aref vec (position char "XYZW"))
                 (aref value i))))
    vec))

(define-compiler-macro (setf swizzle) (&whole form value vec pattern)
  (labels ((get-pos (char)
             (or (position char "XYZW")
                 (error "Vectors: swizzle: Pattern component was not X, Y, Z or W: ~a" char))))
    (if (keywordp pattern)
        (let* ((name (cl:symbol-name pattern))
               (len (cl:length name))
               (val (gensym "val"))
               (dst (gensym "vec")))
          (if (or (> len 4) (< len 2))
              (error "Vectors: swizzle: Cepl vectors cannot have a length less that 2 or greater than 4")
              `(let ((,dst ,vec)
                     (,val ,value))
                 ,@(loop :for char :across name :for i :from 0
                      :collect `(setf (aref ,dst ,(get-pos char))
                                      (aref ,val ,i)))
                 ,dst)))
        form)))

(defun s~ (vec pattern)
  (swizzle vec pattern))

(define-compiler-macro s~ (vec pattern)
  `(swizzle ,vec ,pattern))

(defun (setf s~) (value vec pattern)
  (setf (swizzle vec pattern) value))

(define-compiler-macro (setf s~) (value vec pattern)
  `(setf (swizzle ,vec ,pattern) ,value))

;;----------------------------------------------------------------

(defmacro dvec (var-list expression &body body)
  (when (or (not (listp var-list))
	    (not (every #'symbolp var-list))
	    (< (cl:length var-list) 1)
	    (> (cl:length var-list) 4))
    (error "dvec: invalid vector destructuring pattern ~s"
	   var-list))
  (let ((e (gensym "expression")))
    `(let ((,e ,expression))
       (let ,(loop :for v :in var-list :for i :from 0 :collect
{'fraction_non_alphanumeric': 0.18113632102006375, 'fraction_numerical': 0.0063753984624039, 'mean_word_length': 2.015262860373092, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1961559', 'n_tokens_mistral': 1787, 'n_tokens_neox': 1688, 'n_words': 611}