Commits

Michał Marczyk committed 5b2ddf4

0.0.6: direct access to PV and SubVector internals (requires Clojure 1.5-RC5)

Comments (0)

Files changed (4)

-(defproject flexvec "0.0.5"
+(defproject flexvec "0.0.6"
   :description "RRB-Trees for Clojure(Script) -- see Bagwell & Rompf"
   :url "https://github.com/michalmarczyk/flexvec"
   :license {:name "Eclipse Public License"
             :url "http://www.eclipse.org/legal/epl-v10.html"}
-  :dependencies [[org.clojure/clojure "1.5.0-RC4"]]
+  :dependencies [[org.clojure/clojure "1.5.0-RC5"]]
   :warn-on-reflection true)

src/flexvec/debug.clj

 (ns flexvec.debug
   (:require flexvec.rrbt
-            [flexvec.nodes :refer [ranges object-nm primitive-nm
-                                   pv-root pv-shift pv-tail]]
+            [flexvec.nodes :refer [ranges object-nm primitive-nm]]
             [flexvec.core :as fv])
   (:import (clojure.lang PersistentVector)
            (clojure.core Vec)
 (defn dbg-vec [v]
   (let [[extract-root extract-shift extract-tail ^NodeManager nm]
         (condp identical? (class v)
-          PersistentVector [pv-root pv-shift pv-tail object-nm]
+          PersistentVector [#(.-root ^PersistentVector %)
+                            #(.-shift ^PersistentVector %)
+                            #(.-tail ^PersistentVector %)
+                            object-nm]
           Vec              [#(.-root ^Vec %)
                             #(.-shift ^Vec %)
                             #(.-tail ^Vec %)

src/flexvec/nodes.clj

 
 ;;; empty nodes
 
-(let [empty-node-field (.getDeclaredField PersistentVector "EMPTY_NODE")]
-  (.setAccessible empty-node-field true)
-  (def empty-pv-node (.get empty-node-field nil)))
+(def empty-pv-node PersistentVector/EMPTY_NODE)
 
 (def empty-gvec-node clojure.core/EMPTY-NODE)
 
 ;;; node managers
 
-(let [root-field (doto (.getDeclaredField PersistentVector "root")
-                   (.setAccessible true))]
-  (defn ^clojure.lang.PersistentVector$Node pv-root [^PersistentVector v]
-    (.get root-field v)))
-
-(let [tail-field (doto (.getDeclaredField PersistentVector "tail")
-                   (.setAccessible true))]
-  (defn pv-tail [^PersistentVector v]
-    (.get tail-field v)))
-
-(let [shift-field (doto (.getDeclaredField PersistentVector "shift")
-                    (.setAccessible true))]
-  (defn pv-shift [^PersistentVector v]
-    (.get shift-field v)))
-
-(let [array-field (doto (.getDeclaredField PersistentVector$Node "array")
-                    (.setAccessible true))]
-  (defn pv-node-array [^PersistentVector$Node node]
-    (.get array-field node)))
-
-(let [edit-field (doto (.getDeclaredField PersistentVector$Node "edit")
-                   (.setAccessible true))]
-  (defn ^AtomicReference pv-node-edit [^PersistentVector$Node node]
-    (.get edit-field node)))
-
-(let [node-ctor (.getDeclaredConstructor
-                 PersistentVector$Node
-                 (into-array Class [AtomicReference (class (object-array 0))]))]
-  (.setAccessible node-ctor true)
-  (defn make-pv-node [^AtomicReference edit ^objects arr]
-    (.newInstance node-ctor (object-array (list edit arr)))))
-
 (definterface NodeManager
   (node [^java.util.concurrent.atomic.AtomicReference edit arr])
   (empty [])
 (def object-nm
   (reify NodeManager
     (node [_ edit arr]
-      (make-pv-node edit arr))
+      (PersistentVector$Node. edit arr))
     (empty [_]
       empty-pv-node)
     (array [_ node]
-      (pv-node-array node))
+      (.-array ^PersistentVector$Node node))
     (edit [_ node]
-      (pv-node-edit node))
+      (.-edit ^PersistentVector$Node node))
     (regular [_ node]
-      (not (== (alength ^objects (pv-node-array node)) (int 33))))
+      (not (== (alength ^objects (.-array ^PersistentVector$Node node)) (int 33))))
     (clone [_ am shift node]
-      (make-pv-node (pv-node-edit node)
-                    (aclone ^objects (pv-node-array node))))))
+      (PersistentVector$Node.
+       (.-edit ^PersistentVector$Node node)
+       (aclone ^objects (.-array ^PersistentVector$Node node))))))
 
 (def primitive-nm
   (reify NodeManager

src/flexvec/rrbt.clj

                                    replace-leftmost-child
                                    replace-rightmost-child
                                    fold-tail new-path index-of-nil
-                                   object-am object-nm primitive-nm
-                                   pv-shift pv-root pv-tail]]
+                                   object-am object-nm primitive-nm]]
             [clojure.core.protocols :refer [IKVReduce]])
   (:import (clojure.core ArrayManager Vec VecSeq)
            (clojure.lang Util Box PersistentVector APersistentVector$SubVector)
 (defmethod print-method ::Vector [v w]
   ((get (methods print-method) clojure.lang.IPersistentVector) v w))
 
-(let [vector-field (.getDeclaredField APersistentVector$SubVector "v")]
-  (.setAccessible vector-field true)
-  (defn sv-vector [^APersistentVector$SubVector sv]
-    (.get vector-field sv)))
-
-(let [start-field (.getDeclaredField APersistentVector$SubVector "start")]
-  (.setAccessible start-field true)
-  (defn sv-start [^APersistentVector$SubVector sv]
-    (.get start-field sv)))
-
-(let [end-field (.getDeclaredField APersistentVector$SubVector "end")]
-  (.setAccessible end-field true)
-  (defn sv-end [^APersistentVector$SubVector sv]
-    (.get end-field sv)))
-
 (extend-protocol AsRRBT
   Vec
   (as-rrbt [^Vec this]
   PersistentVector
   (as-rrbt [^PersistentVector this]
     (Vector. object-nm object-am
-             (count this) (pv-shift this) (pv-root this) (pv-tail this)
+             (count this) (.-shift this) (.-root this) (.-tail this)
              (meta this) -1 -1))
 
   APersistentVector$SubVector
   (as-rrbt [^APersistentVector$SubVector this]
-    (let [v     (sv-vector this)
-          start (sv-start this)
-          end   (sv-end this)]
+    (let [v     (.-v this)
+          start (.-start this)
+          end   (.-end this)]
       (slicev (as-rrbt v) start end))))
 
 (defn shift-from-to [^NodeManager nm node from to]
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.