<commit_before>;;;; © 2016-2018 <NAME> - licensed under AGPLv3, see the file COPYING     -*- coding: utf-8 -*-

(in-package :petalisp-reference-backend)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; Generic Functions

(defgeneric make-intermediate-result (shape value-fn))

(defgeneric iref (immediate index))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; Classes

(defclass intermediate-result ()
  ((%shape :initarg :shape :reader shape)
   (%table :initarg :table :reader table)))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; Methods

(defmethod make-intermediate-result ((shape shape) (value-fn function))
  (let ((table (make-hash-table :test #'equal)))
    (loop for index in (set-elements shape) do
      (setf (gethash index table)
            (funcall value-fn index)))
    (make-instance 'intermediate-result
      :shape shape
      :table table)))

(defmethod iref ((intermediate-result intermediate-result) (index list))
  (multiple-value-bind (value present-p)
      (gethash index (table intermediate-result))
    (unless present-p
      (error "Invalid index ~S for the strided array ~S."
             index intermediate-result))
    value))

(defun immediate-from-intermediate-result (intermediate-result)
  (let ((shape (shape intermediate-result))
        (table (table intermediate-result)))
    (if (zerop (dimension shape))
        (make-instance 'scalar-immediate
          :shape shape
          :storage (gethash '() table))
        (let ((storage (make-array (mapcar #'set-size (ranges shape)))))
          (loop for index in (set-elements shape) do
            (setf (apply #'aref storage index)
                  (gethash index table)))
          (make-instance 'array-immediate
            :shape (shape intermediate-result)
            :storage storage)))))<commit_msg>Simplify reference backend.<commit_after>;;;; © 2016-2018 <NAME> - licensed under AGPLv3, see the file COPYING     -*- coding: utf-8 -*-

(in-package :petalisp-reference-backend)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; Classes

(defclass intermediate-result ()
  ((%shape :initarg :shape :reader shape)
   (%table :initarg :table :reader table)))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; Functions

(defun make-intermediate-result (shape value-fn)
  (let ((table (make-hash-table :test #'equal)))
    (loop for index in (set-elements shape) do
      (setf (gethash index table)
            (funcall value-fn index)))
    (make-instance 'intermediate-result
      :shape shape
      :table table)))

(defun iref (intermediate-result index)
  (multiple-value-bind (value present-p)
      (gethash index (table intermediate-result))
    (unless present-p
      (error "Invalid index ~S for the strided array ~S."
             index intermediate-result))
    value))

(defun immediate-from-intermediate-result (intermediate-result)
  (let ((shape (shape intermediate-result))
        (table (table intermediate-result)))
    (if (zerop (dimension shape))
        (make-instance 'scalar-immediate
          :shape shape
          :storage (gethash '() table))
        (let ((storage (make-array (mapcar #'set-size (ranges shape)))))
          (loop for index in (set-elements shape) do
            (setf (apply #'aref storage index)
                  (gethash index table)))
          (make-instance 'array-immediate
            :shape (shape intermediate-result)
            :storage storage)))))
{'fraction_non_alphanumeric': 0.22940851299060255, 'fraction_numerical': 0.00552791597567717, 'mean_word_length': 3.3707729468599035, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6342468', 'n_tokens_mistral': 1164, 'n_tokens_neox': 1049, 'n_words': 281}