Commits

Meikel Brandmeyer committed f4d07f5

Implemented LazyMap via genclass

Comments (0)

Files changed (3)

 ; THE SOFTWARE.
 
 (let [classes ["LazyMapEntry"
-               "LazyMapSeq"]]
+               "LazyMapSeq"
+               "LazyMapClass"]]
   (doseq cf classes
     (load-file (str "src/de/kotka/lazymap/" cf ".gen.clj"))))

src/de/kotka/lazymap/LazyMapClass.clj

+;-
+; Copyright 2008 (c) Meikel Brandmeyer.
+; All rights reserved.
+;
+; Permission is hereby granted, free of charge, to any person obtaining a copy
+; of this software and associated documentation files (the "Software"), to deal
+; in the Software without restriction, including without limitation the rights
+; to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+; copies of the Software, and to permit persons to whom the Software is
+; furnished to do so, subject to the following conditions:
+;
+; The above copyright notice and this permission notice shall be included in
+; all copies or substantial portions of the Software.
+;
+; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+; IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+; FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+; AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+; LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+; OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+; THE SOFTWARE.
+
+(clojure/in-ns 'de.kotka.lazymap)
+(clojure/refer 'clojure)
+
+(import '(clojure.lang SeqIterator IMapEntry IPersistentVector)
+        '(de.kotka.lazymap LazyMapClass LazyMapSeq))
+
+(defn- LazyMapClass-init
+  [theMap]
+  [[] theMap])
+
+(defn- LazyMapClass-lazyAssoc
+  [this k v]
+  (new LazyMapClass (.. this theMap (assoc k v))))
+
+; IPersistentMap
+(defn- LazyMapClass-assoc
+  [this k v]
+  (new LazyMapClass (.. this theMap (assoc k (delay v)))))
+
+(defn- LazyMapClass-assocEx
+  [this k v]
+  (when (. this containsKey k)
+    (throw (new Exception (str "Key alread present: " k))))
+  (. this assoc k v))
+
+(defn- LazyMapClass-without
+  [this k]
+  (new LazyMapClass (.. this theMap (without k))))
+
+; Associative
+(defn- LazyMapClass-containsKey
+  [this k]
+  (.. this theMap (containsKey k)))
+
+(defn- LazyMapClass-entryAt
+  [this k]
+  (new LazyMapEntry k (.. this theMap (valAt k))))
+
+(defn- LazyMapClass-valAt
+  ([this k]
+   (. this valAt k nil))
+  ([this k nf]
+   (if (.. this theMap (containsKey k))
+     (force (.. this theMap (valAt k)))
+     nf)))
+
+; Iterable
+(defn- LazyMapClass-iterator
+  [this]
+  (new SeqIterator (. this seq)))
+
+; IPersistentCollection
+(defn- LazyMapClass-count
+  [this]
+  (.. this theMap (count)))
+
+(defn- LazyMapClass-seq
+  [this]
+  (LazyMapSeq-createSeq (.. this theMap (seq))))
+
+(defn- LazyMapClass-cons
+  [this o]
+  (cond
+    (instance? LazyMapEntry o) (let [k (. o getKey)
+                                     v (. o getRawValue)]
+                                 (. this lazyAssoc k v))
+    (instance? IMapEntry o)    (let [k (. o getKey)
+                                     v (. o getValue)]
+                                 (. this assoc k (delay v)))
+    (instance? IPersistentVector o)
+                               (if (= (count o) 2)
+                                 (let [k (o 0)
+                                       v (o 1)]
+                                   (. this assoc k (delay v)))
+                                 (throw (new IllegalArgumentException
+                                             "Vector arg to map conj must be a pair")))
+    :else                      (reduce #(. %1 cons %2) this o)))
+
+(defn- LazyMapClass-empty
+  [this]
+  (new LazyMapClass (.. this theMap (empty))))
+
+; IFn
+(defn- LazyMapClass-invoke
+  [this & ks]
+  (. this valAt (first ks)))
+
+(defn- LazyMapClass-applyTo
+  [this ks]
+  (. this valAt (first ks)))

src/de/kotka/lazymap/LazyMapClass.gen.clj

+;-
+; Copyright 2008 (c) Meikel Brandmeyer.
+; All rights reserved.
+;
+; Permission is hereby granted, free of charge, to any person obtaining a copy
+; of this software and associated documentation files (the "Software"), to deal
+; in the Software without restriction, including without limitation the rights
+; to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+; copies of the Software, and to permit persons to whom the Software is
+; furnished to do so, subject to the following conditions:
+;
+; The above copyright notice and this permission notice shall be included in
+; all copies or substantial portions of the Software.
+;
+; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+; IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+; FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+; AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+; LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+; OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+; THE SOFTWARE.
+
+(gen-and-save-class "dist" 'de.kotka.lazymap.LazyMapClass
+  :init         'init
+  :state        'theMap
+  :methods      [['lazyAssoc [Object Object] de.kotka.lazymap.LazyMapClass]]
+  :implements   [clojure.lang.IPersistentMap clojure.lang.IFn]
+  :constructors {[clojure.lang.IPersistentMap] []})