Vector

typeremoved v1.12.134added v1.12.116removed v0.0-1798added v0.0-927Edit
satisfies IAssociative ICloneable ICollection IComparable ICounted IDrop IEditableCollection IEmptyableCollection IEquiv IFn IHash IIndexed IIterable IKVReduce ILookup IMeta IPrintWithWriter IReduce IReversible ISeqable ISequential IStack ITransientAssociative ITransientCollection ITransientVector IVector IWithMeta

(Vector. meta array __hash)

Source code @ clojurescript:src/main/cljs/cljs/core.cljs
(deftype Vector [meta array ^:mutable __hash]
  Object
  (toString [coll]
    (pr-str* coll))
  (equiv [coll other]
    (-equiv coll other))
  (indexOf [coll x start]
    (let [start (if (nil? start) 0 start)
          len   (-count coll)]
      (if (>= start len)
        -1
        (loop [idx (cond
                     (pos? start) start
                     (neg? start) (unchecked-max 0 (+ start len))
                     :else start)]
          (if (< idx len)
            (if (= (-nth coll idx) x)
              idx
              (recur (inc idx)))
            -1)))))
  (lastIndexOf [coll x start]
    (let [start (if (nil? start) (alength array) start)
          len   (-count coll)]
      (if (zero? len)
        -1
        (loop [idx (cond
                     (pos? start) (unchecked-min (dec len) start)
                     (neg? start) (+ len start)
                     :else start)]
          (if (>= idx 0)
            (if (= (-nth coll idx) x)
              idx
              (recur (dec idx)))
            -1)))))

  IWithMeta
  (-with-meta [coll new-meta]
    (if (identical? new-meta meta)
      coll
      (Vector. new-meta array __hash)))

  ICloneable
  (-clone [coll] (Vector. meta array __hash))

  IMeta
  (-meta [coll] meta)

  IStack
  (-peek [coll]
    (let [count (alength array)]
      (when (> count 0)
        (aget array (dec count)))))
  (-pop [coll]
    (if (> (alength array) 0)
      (let [new-array (aclone array)]
        (. new-array (pop))
        (Vector. meta new-array nil))
      (throw (js/Error. "Can't pop empty vector"))))

  ICollection
  (-conj [coll o]
    (let [new-array (aclone array)]
      (.push new-array o)
      (Vector. meta new-array nil)))

  IEmptyableCollection
  (-empty [coll] (with-meta (. Vector -EMPTY) meta))

  ISequential
  IEquiv
  (-equiv [coll other] (equiv-sequential coll other))

  IHash
  (-hash [coll] (hash-ordered-coll coll))

  ISeqable
  (-seq [coll]
    (when (> (alength array) 0)
      (prim-seq array)))

  ICounted
  (-count [coll] (alength array))

  IIndexed
  (-nth [coll n]
    (if (and (<= 0 n) (< n (alength array)))
      (aget array (int n))
      (throw (js/Error. (str "No item " n " in vector of length " (alength array))))))
  (-nth [coll n not-found]
    (if (and (<= 0 n) (< n (alength array)))
      (aget array (int n))
      not-found))

  ILookup
  (-lookup [coll k]
    (when (number? k)
      (-nth coll k nil)))
  (-lookup [coll k not-found]
    (if (number? k)
      (-nth coll k not-found)
      not-found))

  IAssociative
  (-assoc [coll k v]
    (if (number? k)
      (let [new-array (aclone array)]
        (aset new-array k v)
        (Vector. meta new-array nil))
      (throw (js/Error. "Vector's key for assoc must be a number."))))
  (-contains-key? [coll k]
    (if (integer? k)
      (and (<= 0 k) (< k (alength array)))
      false))

  IVector
  (-assoc-n [coll n val] (-assoc coll n val))

  IReversible
  (-rseq [coll]
    (let [cnt (alength array)]
      (when (pos? cnt)
        (RSeq. coll (dec cnt) nil))))

  IReduce
  (-reduce [v f]
    (array-reduce array f))
  (-reduce [v f start]
    (array-reduce array f start))

  IKVReduce
  (-kv-reduce [v f init]
    (let [len (alength array)]
      (loop [i 0 init init]
        (if (< i len)
          (let [init (f init i (aget array i))]
            (if (reduced? init)
              @init
              (recur (inc i) init)))
          init))))

  IDrop
  (-drop [v n]
    (let [cnt (alength array)]
      (if (< n cnt)
        (prim-seq array n)
        nil)))

  IComparable
  (-compare [x y]
    (if (vector? y)
      (compare-indexed x y)
      (throw (js/Error. "Cannot compare with Vector"))))

  IFn
  (-invoke [coll k]
    (if (number? k)
      (-nth coll k)
      (throw (js/Error. "Key must be integer"))))

  IEditableCollection
  (-as-transient [coll]
    coll)

  ITransientCollection
  (-conj! [coll val]
    (-conj coll val))
  (-persistent! [coll]
    coll)

  ITransientAssociative
  (-assoc! [tcoll key val]
    (-assoc-n! tcoll key val))

  ITransientVector
  (-assoc-n! [tcoll key val]
    (if (number? key)
      (-assoc-n tcoll key val)
      (throw (js/Error. "Vector's key for assoc! must be a number."))))

  (-pop! [tcoll]
    (-pop tcoll))

  IIterable
  (-iterator [coll]
    (VectorIterator. array 0))

  IPrintWithWriter
  (-pr-writer [coll writer opts] (pr-sequential-writer writer pr-writer "[" " " "]" opts coll)))