Source

clojurian / 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))))))
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.