diff --git a/.gitignore b/.gitignore index 864be8e..1f44a9e 100644 --- a/.gitignore +++ b/.gitignore @@ -7,5 +7,7 @@ pom.xml.asc *.class /.lein-* !/.lein-classpath -/.nrepl-port -*.DS_Store \ No newline at end of file +/.nrepl-* +*.DS_Store +project.clj +*.iml \ No newline at end of file diff --git a/src/linked/map.cljc b/src/linked/map.cljc index 2661a58..2284b24 100644 --- a/src/linked/map.cljc +++ b/src/linked/map.cljc @@ -1,23 +1,24 @@ (ns linked.map (:require [clojure.string :as string] - #?(:cljs [cljs.reader :as reader])) - #?(:clj (:import (clojure.lang Associative - Counted - IObj - IFn - IHashEq - ILookup - IPersistentCollection - IPersistentVector - IPersistentMap - MapEntry - MapEquivalence - Reversible - Seqable - SeqIterator) - (java.util Map - Map$Entry) - (java.lang Iterable)))) + #?(:cljs [cljs.reader :as reader])) + #?(:clj + (:import (clojure.lang Associative + Counted + IObj + IFn + IHashEq + ILookup + IPersistentCollection + IPersistentVector + IPersistentMap + MapEntry + MapEquivalence + Reversible + Seqable + SeqIterator) + (java.util Map + Map$Entry) + (java.lang Iterable)))) (declare empty-linked-map) @@ -30,192 +31,192 @@ (deftype LinkedMap [head delegate-map] #?@(:clj - [IPersistentMap - (assoc [this k v] - (assoc* this k v)) - (assocEx [this k v] - (if (.containsKey this k) - (throw (RuntimeException. "Key already present")) - (assoc this k v))) - (without [this k] - (dissoc* this k)) - - MapEquivalence - - Map - (get [this k] - (.valAt this k)) - (containsValue [this v] - (boolean (seq (filter #(= % v) (.values this))))) - (values [this] - (map val (.seq this))) - (size [_] - (count delegate-map)) - - Counted - - IPersistentCollection - (count [this] - (.size this)) - (cons [this o] - (condp instance? o - Map$Entry (let [^Map$Entry e o] - (.assoc this (.getKey e) (.getValue e))) - IPersistentVector (if (= 2 (count o)) - (.assoc this (nth o 0) (nth o 1)) - (throw (IllegalArgumentException. "Vector arg to map conj must be a pair"))) - ;; TODO support for transient to speed up multiple assoc? - (reduce (fn [^IPersistentMap m ^Map$Entry e] - (.assoc m (.getKey e) (.getValue e))) - this - o))) - (empty [_] - (with-meta empty-linked-map (meta delegate-map))) - (equiv [this o] - (and (instance? Map o) - (= (.count this) (count o)) - (every? (fn [[k v :as kv]] - (= kv (find o k))) - (.seq this)))) - - Seqable - (seq [this] - (seq* this)) - - Reversible - (rseq [this] - (rseq* this)) - - Iterable - (iterator [this] - (SeqIterator. (.seq this))) - - Associative - (containsKey [_ k] - (contains? delegate-map k)) - (entryAt [this k] - (when (.containsKey this k) - (MapEntry. k (.valAt this k)))) - - ILookup - (valAt [this k] - (.valAt this k nil)) - (valAt [_ k not-found] - (if-let [entry (find delegate-map k)] - (-> entry val :value) - not-found)) - - IFn - (invoke [this k] - (.valAt this k)) - (invoke [this k not-found] - (.valAt this k not-found)) - - IObj - (meta [this] - (.meta ^IObj delegate-map)) - (withMeta [this m] - (LinkedMap. head (.withMeta ^IObj delegate-map m))) - - ;; IEditableCollection - - IHashEq - (hasheq [this] (.hasheq (into {} this))) - - Object - (toString [this] - (str "{" (string/join ", " (for [[k v] this] (str k " " v))) "}")) - (equals [this other] - (.equiv this other)) - (hashCode [this] - (.hashCode (into {} this)))] - :cljs - [Object - (toString [coll] - (str "{" (string/join ", " (for [[k v] coll] (str k " " v))) "}")) - (equiv [this other] - (-equiv this other)) - - ICloneable - (-clone [_] - (LinkedMap. head delegate-map)) - - IWithMeta - (-with-meta [coll meta] - (LinkedMap. head (with-meta delegate-map meta))) - - IMeta - (-meta [coll] (meta delegate-map)) - - ICollection - (-conj [coll entry] - (if (vector? entry) - (-assoc coll (-nth entry 0) (-nth entry 1)) - (loop [ret coll es (seq entry)] - (if (nil? es) - ret - (let [e (first es)] - (if (vector? e) - (recur (-assoc ret (-nth e 0) (-nth e 1)) - (next es)) - (throw (js/Error. "conj on a map takes map entries or seqables of map entries")))))))) - - IEmptyableCollection - (-empty [coll] (-with-meta empty-linked-map (meta delegate-map))) - - IEquiv - (-equiv [coll other] (equiv-map coll other)) - - IHash - (-hash [coll] (hash (into {} coll))) - - ISequential - - ISeqable - (-seq [coll] (seq* coll)) - - IReversible - (-rseq [coll] (rseq* coll)) - - ICounted - (-count [coll] - (count delegate-map)) - - ILookup - (-lookup [coll k] - (-lookup coll k nil)) - - (-lookup [coll k not-found] - (if-let [entry (find delegate-map k)] - (-> entry val :value) - not-found)) - - IAssociative - (-assoc [coll k v] - (assoc* coll k v)) - - (-contains-key? [coll k] - (contains? delegate-map k)) - - IMap - (-dissoc [coll k] - (dissoc* coll k)) - - IKVReduce - (-kv-reduce [coll f init] - (reduce (seq coll) f init)) - - IFn - (-invoke [coll k] - (-lookup coll k)) - - (-invoke [coll k not-found] - (-lookup coll k not-found)) - - ;; IEditableCollection - - IPrintWithWriter - (-pr-writer [coll writer opts] (-write writer (str "#linked/map " (into [] coll))))])) + [IPersistentMap + (assoc [this k v] + (assoc* this k v)) + (assocEx [this k v] + (if (.containsKey this k) + (throw (RuntimeException. "Key already present")) + (assoc this k v))) + (without [this k] + (dissoc* this k)) + + MapEquivalence + + Map + (get [this k] + (.valAt this k)) + (containsValue [this v] + (boolean (seq (filter #(= % v) (.values this))))) + (values [this] + (map val (.seq this))) + (size [_] + (count delegate-map)) + + Counted + + IPersistentCollection + (count [this] + (.size this)) + (cons [this o] + (condp instance? o + Map$Entry (let [^Map$Entry e o] + (.assoc this (.getKey e) (.getValue e))) + IPersistentVector (if (= 2 (count o)) + (.assoc this (nth o 0) (nth o 1)) + (throw (IllegalArgumentException. "Vector arg to map conj must be a pair"))) + ;; TODO support for transient to speed up multiple assoc? + (reduce (fn [^IPersistentMap m ^Map$Entry e] + (.assoc m (.getKey e) (.getValue e))) + this + o))) + (empty [_] + (with-meta empty-linked-map (meta delegate-map))) + (equiv [this o] + (and (instance? Map o) + (= (.count this) (count o)) + (every? (fn [[k v :as kv]] + (= kv (find o k))) + (.seq this)))) + + Seqable + (seq [this] + (seq* this)) + + Reversible + (rseq [this] + (rseq* this)) + + Iterable + (iterator [this] + (SeqIterator. (.seq this))) + + Associative + (containsKey [_ k] + (contains? delegate-map k)) + (entryAt [this k] + (when (.containsKey this k) + (MapEntry. k (.valAt this k)))) + + ILookup + (valAt [this k] + (.valAt this k nil)) + (valAt [_ k not-found] + (if-let [entry (find delegate-map k)] + (-> entry val :value) + not-found)) + + IFn + (invoke [this k] + (.valAt this k)) + (invoke [this k not-found] + (.valAt this k not-found)) + + IObj + (meta [this] + (.meta ^IObj delegate-map)) + (withMeta [this m] + (LinkedMap. head (.withMeta ^IObj delegate-map m))) + + ;; IEditableCollection + + IHashEq + (hasheq [this] (.hasheq ^IHashEq (into {} this))) + + Object + (toString [this] + (str "{" (string/join ", " (for [[k v] this] (str k " " v))) "}")) + (equals [this other] + (.equiv this other)) + (hashCode [this] + (.hashCode ^Object (into {} this)))] + :cljs + [Object + (toString [coll] + (str "{" (string/join ", " (for [[k v] coll] (str k " " v))) "}")) + (equiv [this other] + (-equiv this other)) + + ICloneable + (-clone [_] + (LinkedMap. head delegate-map)) + + IWithMeta + (-with-meta [coll meta] + (LinkedMap. head (with-meta delegate-map meta))) + + IMeta + (-meta [coll] (meta delegate-map)) + + ICollection + (-conj [coll entry] + (if (vector? entry) + (-assoc coll (-nth entry 0) (-nth entry 1)) + (loop [ret coll es (seq entry)] + (if (nil? es) + ret + (let [e (first es)] + (if (vector? e) + (recur (-assoc ret (-nth e 0) (-nth e 1)) + (next es)) + (throw (js/Error. "conj on a map takes map entries or seqables of map entries")))))))) + + IEmptyableCollection + (-empty [coll] (-with-meta empty-linked-map (meta delegate-map))) + + IEquiv + (-equiv [coll other] (equiv-map coll other)) + + IHash + (-hash [coll] (hash (into {} coll))) + + ISequential + + ISeqable + (-seq [coll] (seq* coll)) + + IReversible + (-rseq [coll] (rseq* coll)) + + ICounted + (-count [coll] + (count delegate-map)) + + ILookup + (-lookup [coll k] + (-lookup coll k nil)) + + (-lookup [coll k not-found] + (if-let [entry (find delegate-map k)] + (-> entry val :value) + not-found)) + + IAssociative + (-assoc [coll k v] + (assoc* coll k v)) + + (-contains-key? [coll k] + (contains? delegate-map k)) + + IMap + (-dissoc [coll k] + (dissoc* coll k)) + + IKVReduce + (-kv-reduce [coll f init] + (reduce (seq coll) f init)) + + IFn + (-invoke [coll k] + (-lookup coll k)) + + (-invoke [coll k not-found] + (-lookup coll k not-found)) + + ;; IEditableCollection + + IPrintWithWriter + (-pr-writer [coll writer opts] (-write writer (str "#linked/map " (into [] coll))))])) #?(:clj (defmethod print-method LinkedMap [o ^java.io.Writer w] @@ -254,7 +255,7 @@ ;;;; seq and rseq impl (defn- map-entry [k v] - #?(:clj (MapEntry. k v) + #?(:clj (MapEntry. k v) :cljs (vector k v))) (defn- visit-node [delegate-map current last direction] diff --git a/src/linked/set.cljc b/src/linked/set.cljc index 31b023b..84a52f0 100644 --- a/src/linked/set.cljc +++ b/src/linked/set.cljc @@ -1,154 +1,155 @@ (ns linked.set (:require [linked.map :refer [empty-linked-map]] [clojure.string :as string] - #?(:cljs [cljs.reader :as reader])) - #?(:clj (:import (clojure.lang Counted - IObj - IFn - IHashEq - ILookup - IPersistentCollection - IPersistentSet - IPersistentVector - Reversible - Seqable - SeqIterator) - (java.util Set) - (java.lang Iterable)))) + #?(:cljs [cljs.reader :as reader])) + #?(:clj + (:import (clojure.lang Counted + IObj + IFn + IHashEq + ILookup + IPersistentCollection + IPersistentSet + IPersistentVector + Reversible + Seqable + SeqIterator) + (java.util Set) + (java.lang Iterable)))) (declare empty-linked-set) (deftype LinkedSet [linked-map] #?@(:clj - [IPersistentSet - (disjoin [_ k] - (LinkedSet. (dissoc linked-map k))) - (contains [_ k] - (contains? linked-map k)) - (get [this k] - (when (.contains this k) k)) - - Set - (size [this] - (.count this)) - - Iterable - (iterator [this] - (SeqIterator. (.seq this))) - - Counted - - IPersistentCollection - (count [_] - (count linked-map)) - (cons [this o] - (if (contains? linked-map o) - this - (LinkedSet. (assoc linked-map o nil)))) - (empty [_] - empty-linked-set) - (equiv [this other] - (or (identical? this other) - (and (instance? Set other) - (let [^Set s other] - (and (= (.size this) (.size s)) - (every? #(.contains s %) (.seq this))))))) - Seqable - (seq [_] - (when-let [s (seq linked-map)] (map key s))) - - Reversible - (rseq [_] - (when-let [s (rseq linked-map)] (map key s))) - - IFn - (invoke [this k] - (get this k)) - - IObj - (meta [this] - (.meta ^IObj linked-map)) - (withMeta [this m] - (LinkedSet. (.withMeta ^IObj linked-map m))) - - IHashEq - (hasheq [this] (.hasheq (into #{} this))) - - Object - (toString [this] - (str "[" (string/join " " (map str this)) "]")) - (hashCode [this] - (.hashCode (into #{} this))) - (equals [this other] - (.equiv this other))] - :cljs - [Object - (toString [this] - (str "[" (string/join " " (map str this)) "]")) - (equiv [this other] - (-equiv this other)) - - ICloneable - (-clone [_] (LinkedSet. linked-map)) - - IWithMeta - (-with-meta [coll meta] (LinkedSet. (with-meta linked-map meta))) - - IMeta - (-meta [coll] (meta linked-map)) - - ICollection - (-conj [coll o] - (LinkedSet. (assoc linked-map o nil))) - - IEmptyableCollection - (-empty [coll] (with-meta empty-linked-set meta)) - - IEquiv - (-equiv [coll other] - (and + [IPersistentSet + (disjoin [_ k] + (LinkedSet. (dissoc linked-map k))) + (contains [_ k] + (contains? linked-map k)) + (get [this k] + (when (.contains this k) k)) + + Set + (size [this] + (.count this)) + + Iterable + (iterator [this] + (SeqIterator. (.seq this))) + + Counted + + IPersistentCollection + (count [_] + (count linked-map)) + (cons [this o] + (if (contains? linked-map o) + this + (LinkedSet. (assoc linked-map o nil)))) + (empty [_] + empty-linked-set) + (equiv [this other] + (or (identical? this other) + (and (instance? Set other) + (let [^Set s other] + (and (= (.size this) (.size s)) + (every? #(.contains s %) (.seq this))))))) + Seqable + (seq [_] + (when-let [s (seq linked-map)] (map key s))) + + Reversible + (rseq [_] + (when-let [s (rseq linked-map)] (map key s))) + + IFn + (invoke [this k] + (get this k)) + + IObj + (meta [this] + (.meta ^IObj linked-map)) + (withMeta [this m] + (LinkedSet. (.withMeta ^IObj linked-map m))) + + IHashEq + (hasheq [this] (.hasheq ^IHashEq (into #{} this))) + + Object + (toString [this] + (str "[" (string/join " " (map str this)) "]")) + (hashCode [this] + (.hashCode ^Object (into #{} this))) + (equals [this other] + (.equiv this other))] + :cljs + [Object + (toString [this] + (str "[" (string/join " " (map str this)) "]")) + (equiv [this other] + (-equiv this other)) + + ICloneable + (-clone [_] (LinkedSet. linked-map)) + + IWithMeta + (-with-meta [coll meta] (LinkedSet. (with-meta linked-map meta))) + + IMeta + (-meta [coll] (meta linked-map)) + + ICollection + (-conj [coll o] + (LinkedSet. (assoc linked-map o nil))) + + IEmptyableCollection + (-empty [coll] (with-meta empty-linked-set meta)) + + IEquiv + (-equiv [coll other] + (and (set? other) (== (count coll) (count other)) (every? #(contains? coll %) other))) - IHash - (-hash [coll] (hash (into #{} coll))) + IHash + (-hash [coll] (hash (into #{} coll))) - ISeqable - (-seq [coll] (when-let [s (seq linked-map)] (map key s))) + ISeqable + (-seq [coll] (when-let [s (seq linked-map)] (map key s))) - IReversible - (-rseq [coll] (when-let [s (rseq linked-map)] (map key s))) + IReversible + (-rseq [coll] (when-let [s (rseq linked-map)] (map key s))) - ISequential + ISequential - ICounted - (-count [coll] (-count linked-map)) + ICounted + (-count [coll] (-count linked-map)) - ILookup - (-lookup [coll v] - (-lookup coll v nil)) - (-lookup [coll v not-found] - (if (-contains-key? linked-map v) - v - not-found)) + ILookup + (-lookup [coll v] + (-lookup coll v nil)) + (-lookup [coll v not-found] + (if (-contains-key? linked-map v) + v + not-found)) - ISet - (-disjoin [coll v] - (LinkedSet. (-dissoc linked-map v))) + ISet + (-disjoin [coll v] + (LinkedSet. (-dissoc linked-map v))) - IFn - (-invoke [coll k] - (-lookup coll k)) - (-invoke [coll k not-found] - (-lookup coll k not-found)) + IFn + (-invoke [coll k] + (-lookup coll k)) + (-invoke [coll k not-found] + (-lookup coll k not-found)) - ;; IEditableCollection + ;; IEditableCollection - IPrintWithWriter - (-pr-writer [coll writer opts] - (-write writer (str "#linked/set " (into [] coll))))])) + IPrintWithWriter + (-pr-writer [coll writer opts] + (-write writer (str "#linked/set " (into [] coll))))])) #?(:clj (defmethod print-method LinkedSet [o ^java.io.Writer w]