fix popcount bug in bit-vector

This commit is contained in:
Ryan Culpepper 2012-12-16 18:52:24 -05:00
parent 28f1d4ff5d
commit e4da3edeec
3 changed files with 12 additions and 8 deletions

View File

@ -21,7 +21,7 @@
(define (make-bit-vector size [fill #f])
(define-values (q r) (quotient/remainder size bits-in-a-word))
(define word-size (add1 q))
(define word-size (+ q (if (zero? r) 0 1)))
(define words (make-fxvector word-size (if fill largest-fixnum 0)))
(when (and fill (not (zero? r)))
(fxvector-set! words q (- (expt 2 r) 1)))

View File

@ -1,6 +1,6 @@
#lang racket
#lang racket/base
(require racket/unsafe/ops
(for-syntax racket/fixnum racket/vector))
(for-syntax racket/base racket/fixnum racket/vector))
(provide fxpopcount)
;; Count set bits for 30 bit number in 5 steps.
;; for 62 bit number in 6.

View File

@ -100,13 +100,17 @@
(test-case "bit-vector-popcount"
(let ()
(define (test)
(define (test len)
(define fill (odd? (random 2)))
(define bv (make-bit-vector 1000 fill))
(define ns (list->set (build-list 100 (λ (_) (random 1000)))))
(define bv (make-bit-vector len fill))
(define ns (list->set (build-list 100 (λ (_) (random len)))))
(for ([n (in-set ns)]) (bit-vector-set! bv n (not fill)))
(define count
(if fill (- 1000 (set-count ns)) (set-count ns)))
(if fill (- len (set-count ns)) (set-count ns)))
(check-equal? (bit-vector-popcount bv) count))
(for ([i (in-range 100)])
(test))))
(test 1000))
;; test multiples of possible word sizes
(for ([ws (in-list '(8 30 62))])
(for ([i (in-range 10)])
(test (* ws 10))))))