SetLite

typesince v1.12.134Edit
satisfies ICloneable ICollection ICounted IEditableCollection IEmptyableCollection IEquiv IFn IHash IIterable ILookup IMeta IPrintWithWriter ISeqable ISet ITransientCollection ITransientSet IWithMeta

(SetLite. meta hash-map __hash)

Source code @ clojurescript:src/main/cljs/cljs/core.cljs
(deftype SetLite [meta hash-map ^:mutable __hash]
  Object
  (toString [coll]
    (pr-str* coll))
  (keys [coll]
    (es6-iterator (-seq coll)))
  (entries [coll]
    (es6-set-entries-iterator (-seq coll)))
  (values [coll]
    (es6-iterator (-seq coll)))
  (has [coll k]
    (contains? coll k))
  (forEach [coll f]
    (let [xs (-seq hash-map)]
      (when (some? xs)
        (.forEach (.-arr xs)
          #(f (-val %) (-key %))))))

  IWithMeta
  (-with-meta [coll new-meta]
    (if (identical? new-meta meta)
      coll
      (SetLite. new-meta hash-map __hash)))

  IMeta
  (-meta [coll] meta)

  ICloneable
  (-clone [coll] (SetLite. meta hash-map __hash))

  ICollection
  (-conj [coll o]
    (let [new-hash-map (assoc hash-map o o)]
      (if (identical? new-hash-map hash-map)
        coll
        (SetLite. meta new-hash-map nil))))

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

  IEquiv
  (-equiv [coll other]
    (and
     (set? other)
     (= (-count coll) (count other))
     (every? #(contains? coll %)
             other)))

  IHash
  (-hash [coll] (caching-hash coll hash-unordered-coll __hash))

  ISeqable
  (-seq [coll]
    (let [xs (-seq hash-map)]
      (when (some? xs)
        (prim-seq (.map (.-arr xs) (fn [kv] (-key kv)))))))

  ICounted
  (-count [coll]
    (let [xs (-seq coll)]
      (if (some? xs)
        (-count xs)
        0)))

  ILookup
  (-lookup [coll v]
    (-lookup coll v nil))
  (-lookup [coll v not-found]
    (if (-contains-key? hash-map v)
      (-lookup hash-map v)
      not-found))

  ISet
  (-disjoin [coll v]
    (let [new-hash-map (-dissoc hash-map v)]
      (if (identical? new-hash-map hash-map)
        coll
        (SetLite. meta new-hash-map nil))))

  IEditableCollection
  (-as-transient [coll]
    coll)

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

  ITransientSet
  (-disjoin! [coll key]
    (-disjoin coll key))

  IFn
  (-invoke [coll k]
    (-lookup coll k))
  (-invoke [coll k not-found]
    (-lookup coll k not-found))

  IIterable
  (-iterator [coll]
    (let [xs (-seq coll)]
      (if (some? xs)
        (-iterator xs)
        (nil-iter))))

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