summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChouser <chouser@n01se.net>2010-02-12 23:40:21 -0500
committerRich Hickey <richhickey@gmail.com>2010-03-11 07:26:54 -0500
commitdf17f32858eb0b700d21a2a2364a54596f139e89 (patch)
treebb8b16ec791a7ef9b99eac6b784707b09542204c
parentc97e974c492218609e38a6497b953e38e30b1d83 (diff)
gvec: implement Iterable, j.u.Collection, and j.u.List Fixes #268 and #269
Signed-off-by: Rich Hickey <richhickey@gmail.com>
-rw-r--r--src/clj/clojure/gvec.clj61
1 files changed, 61 insertions, 0 deletions
diff --git a/src/clj/clojure/gvec.clj b/src/clj/clojure/gvec.clj
index 229394b6..c2dd2e5e 100644
--- a/src/clj/clojure/gvec.clj
+++ b/src/clj/clojure/gvec.clj
@@ -281,6 +281,67 @@
subidx (bit-and (bit-shift-right i level) (int 0x1f))]
(aset arr subidx (.doAssoc this (- level (int 5)) (aget arr subidx) i val))
(VecNode (:edit node) arr))))
+
+ java.lang.Iterable
+ (iterator []
+ (let [i (java.util.concurrent.atomic.AtomicInteger. 0)]
+ (reify java.util.Iterator
+ (hasNext [] (< (.get i) cnt))
+ (next [] (.nth this (dec (.incrementAndGet i))))
+ (remove [] (throw (UnsupportedOperationException.))))))
+
+ java.util.Collection
+ (contains [o] (boolean (some #(= % o) this)))
+ (containsAll [c] (every? #(.contains this %) c))
+ (isEmpty [] (zero? cnt))
+ (toArray [] (into-array Object this))
+ (toArray [arr]
+ (if (>= (count arr) cnt)
+ (do
+ (dotimes [i cnt]
+ (aset arr i (.nth this i)))
+ arr)
+ (into-array Object this)))
+ (size [] cnt)
+ (add [o] (throw (UnsupportedOperationException.)))
+ (addAll [c] (throw (UnsupportedOperationException.)))
+ (clear [] (throw (UnsupportedOperationException.)))
+ (#^boolean remove [o] (throw (UnsupportedOperationException.)))
+ (removeAll [c] (throw (UnsupportedOperationException.)))
+ (retainAll [c] (throw (UnsupportedOperationException.)))
+
+ java.util.List
+ (get [i] (.nth this i))
+ (indexOf [o]
+ (loop [i (int 0)]
+ (cond
+ (== i cnt) -1
+ (= o (.nth this i)) i
+ :else (recur (inc i)))))
+ (lastIndexOf [o]
+ (loop [i (dec cnt)]
+ (cond
+ (< i 0) -1
+ (= o (.nth this i)) i
+ :else (recur (dec i)))))
+ (listIterator [] (.listIterator this 0))
+ (listIterator [i]
+ (let [i (java.util.concurrent.atomic.AtomicInteger. i)]
+ (reify java.util.ListIterator
+ (hasNext [] (< (.get i) cnt))
+ (hasPrevious [] (pos? i))
+ (next [] (.nth this (dec (.incrementAndGet i))))
+ (nextIndex [] (.get i))
+ (previous [] (.nth this (.decrementAndGet i)))
+ (previousIndex [] (dec (.get i)))
+ (add [e] (throw (UnsupportedOperationException.)))
+ (remove [] (throw (UnsupportedOperationException.)))
+ (set [e] (throw (UnsupportedOperationException.))))))
+ (subList [a z] (subvec this a z))
+ (add [i o] (throw (UnsupportedOperationException.)))
+ (addAll [i c] (throw (UnsupportedOperationException.)))
+ (#^Object remove [#^int i] (throw (UnsupportedOperationException.)))
+ (set [i e] (throw (UnsupportedOperationException.)))
)
(defmethod print-method ::Vec [v w]