<commit_before>(in-package :erlang-term)

;;;;
;;;; Erlang tuple
;;;;

(defclass erlang-tuple (erlang-object)
  ((elements :reader elements :initarg :elements))
  (:documentation "Erlang tuple."))


;;;
;;; Methods
;;;

(defmethod print-object ((object erlang-tuple) stream)
  (print-unreadable-object (object stream :type t)
    (format stream "{~{~s~^ ~}}" (coerce (elements object) 'list))))

(defun tuple (&rest erlang-translatable-objects)
  "Create an Erlang tuple"
  (make-instance 'erlang-tuple
                 :elements (coerce erlang-translatable-objects 'vector)))

(defun tuple-ref (tuple pos)
  (svref (elements tuple) pos))

(defun tuple-arity (tuple)
  (length (elements tuple)))

(defmethod arity ((x erlang-tuple))
  "The number of elements of Erlang tuple X."
  (tuple-arity x))

(defmethod size ((x erlang-tuple))
  "The number of elements of Erlang tuple X."
  (tuple-arity x))

(defmethod match-p ((a erlang-tuple) (b erlang-tuple))
  (and (= (arity a) (arity b))
       (every #'match-p (elements a) (elements b))))


;;;
;;; Encode/Decode
;;;

(defmethod encode ((x erlang-tuple) &key atom-cache-entries &allow-other-keys)
  (if (> 256 (tuple-arity x))
      (encode-external-small-tuple x atom-cache-entries)
      (encode-external-large-tuple x atom-cache-entries)))

(defun decode-erlang-tuple (bytes &key (start 0) (version-tag nil))
  (when (integerp version-tag)
    (let ((version (aref bytes start)))
      (unless (= version version-tag)
        (error 'unexpected-message-tag-error
               :received-tag version
               :expected-tags (list version-tag))))
    (incf start))
  (let ((tag (aref bytes start)))
    (case tag
      (#.+small-tuple-ext+ (decode-external-small-tuple bytes (1+ start)))
      (#.+large-tuple-ext+ (decode-external-large-tuple bytes (1+ start)))
      (#.+compressed-term+
       (decode-compressed-erlang-term bytes (1+ start)))
      (otherwise
       (error 'unexpected-message-tag-error
              :received-tag tag
              :expected-tags (list +small-tuple-ext+
                                   +large-tuple-ext+
                                   +compressed-term+))) )))


;; SMALL_TUPLE_EXT
;; +-----+-------+----------+
;; |  1  |   1   |     N    |
;; +-----+-------+----------+
;; | 104 | Arity | Elements |
;; +-----+-------+----------+
;;

(defun encode-external-small-tuple (tuple atom-cache-entries)
  (concatenate 'nibbles:simple-octet-vector<commit_msg>Removed the unused function DECODE-ERLANG-TUPLE.<commit_after>(in-package :erlang-term)

;;;;
;;;; Erlang tuple
;;;;

(defclass erlang-tuple (erlang-object)
  ((elements :reader elements :initarg :elements))
  (:documentation "Erlang tuple."))


;;;
;;; Methods
;;;

(defmethod print-object ((object erlang-tuple) stream)
  (print-unreadable-object (object stream :type t)
    (format stream "{~{~s~^ ~}}" (coerce (elements object) 'list))))

(defun tuple (&rest erlang-translatable-objects)
  "Create an Erlang tuple"
  (make-instance 'erlang-tuple
                 :elements (coerce erlang-translatable-objects 'vector)))

(defun tuple-ref (tuple pos)
  (svref (elements tuple) pos))

(defun tuple-arity (tuple)
  (length (elements tuple)))

(defmethod arity ((x erlang-tuple))
  "The number of elements of Erlang tuple X."
  (tuple-arity x))

(defmethod size ((x erlang-tuple))
  "The number of elements of Erlang tuple X."
  (tuple-arity x))

(defmethod match-p ((a erlang-tuple) (b erlang-tuple))
  (and (= (arity a) (arity b))
       (every #'match-p (elements a) (elements b))))


;;;
;;; Encode/Decode
;;;

(defmethod encode ((x erlang-tuple) &key atom-cache-entries &allow-other-keys)
  (if (> 256 (tuple-arity x))
      (encode-external-small-tuple x atom-cache-entries)
      (encode-external-large-tuple x atom-cache-entries)))


;; SMALL_TUPLE_EXT
;; +-----+-------+----------+
;; |  1  |   1   |     N    |
;; +-----+-------+----------+
;; | 104 | Arity | Elements |
;; +-----+-------+----------+
;;

(defun encode-external-small-tuple (tuple atom-cache-entries)
  (concatenate 'nibbles:simple-octet-vector
{'fraction_non_alphanumeric': 0.1950441609421001, 'fraction_numerical': 0.004906771344455349, 'mean_word_length': 2.8209934395501404, 'pattern_counts': {'":': 0, '<': 3, '<?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': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '772392', 'n_tokens_mistral': 1398, 'n_tokens_neox': 1339, 'n_words': 337}