Commits

Moritz Heidkamp  committed f907128

Some persistent vector fixes

  • Participants
  • Parent commits 95634c4
  • Branches pv

Comments (0)

Files changed (2)

File clojurian-data-structure-helpers.scm

+(define (arithmetic-shift-right/zero-fill n x)
+  (arithmetic-shift (bitwise-and n #xFFFFFFFF) (- x)))
+
+;; (defn bit-count
+;;   "Counts the number of bits set in n"
+;;   [v]
+;;   (let [v (- v (bit-and (bit-shift-right v 1) 0x55555555))
+;;         v (+ (bit-and v 0x33333333) (bit-and (bit-shift-right v 2) 0x33333333))]
+;;     (bit-shift-right (* (bit-and (+ v (bit-shift-right v 4)) 0xF0F0F0F) 0x1010101) 24)))
+
+;; TODO: 64bit compat by doubling hex digits and -56 instead of -24
+
+(define (bit-count v)
+  (let* ((v (- v (bitwise-and (arithmetic-shift v -1) #x55555555)))
+         (v (+ (bitwise-and v #x33333333) (bitwise-and (arithmetic-shift v -2) #x33333333))))
+    (arithmetic-shift (* (bitwise-and (+ v (arithmetic-shift v -4)) #xF0F0F0F) #x1010101) -24)))
+
+(define (vector-copy vec #!optional (count (vector-length vec)) copy)
+  (let* ((count (if (positive? count)
+                    count
+                    (+ (vector-length vec) count 1)))
+         (copy (or copy (make-vector count))))
+    (vector-copy! vec copy count)
+    copy))
+
+
+;; (defn- array-copy
+;;   ([from i to j len]
+;;      (loop [i i j j len len]
+;;        (if (zero? len)
+;;          to
+;;          (do (aset to j (aget from i))
+;;              (recur (inc i) (inc j) (dec len)))))))
+
+(define (vector-copy* from i to j len)
+  (let loop ((i i) (j j) (len len))
+    (if (zero? len)
+        to
+        (begin
+          (vector-set! to j (vector-ref from i))
+          (loop (+ i 1) (+ j 1) (- len 1))))))
+
+
+;; (defn- array-copy-downward
+;;   ([from i to j len]
+;;      (loop [i (+ i (dec len)) j (+ j (dec len)) len len]
+;;        (if (zero? len)
+;;          to
+;;          (do (aset to j (aget from i))
+;;              (recur (dec i) (dec j) (dec len)))))))
+
+(define (vector-copy-downward from i to j len)
+  (let loop ((i (+ i (- len 1))) (j (+ j (- len 1))) (len len))
+    (if (zero? len)
+        to
+        (begin
+          (vector-set! to j (vector-ref from i))
+          (loop (- i 1) (- j 1) (- len 1))))))

File clojurian-vector.scm

 (use clojurian-syntax)
 
-(define (arithmetic-shift-right/zero-fill n x)
-  (arithmetic-shift (bitwise-and n #xFFFFFFFF) (- x)))
-
-(define (vector-copy vec #!optional (count (vector-length vec)) copy)
-  (let* ((count (if (positive? count)
-                    count
-                    (+ (vector-length vec) count 1)))
-         (copy (or copy (make-vector count))))
-    (vector-copy! vec copy count)
-    copy))
+(include "clojurian-data-structure-helpers.scm")
 
 ;; (deftype VectorNode [edit arr])
 
 ;;   (VectorNode. (.-edit node) (aclone (.-arr node))))
 (define (pv-clone-node node)
   (make-pv-node (pv-node-edit node)
-                    (vector-copy (pv-node-vec node))))
+                (vector-copy (pv-node-vec node))))
 
 
 ;; (defn- tail-off [pv]
      (else
       (let* ((new-tail (vector-for pv (- cnt 2)))
              (nr (pop-tail pv (pv-shift pv) (pv-root pv)))
-             (new-root (or nr +empty-vector+))
+             (new-root (or nr +empty-node+))
              (cnt-1 (- cnt 1)))
         (if (and (< 5 (pv-shift pv)) (not (pv-aget new-root 1)))
             (make-pv cnt-1 (- (pv-shift pv) 5) (pv-aget new-root 0) new-tail)