9 Bit Vectors
(require data/bit-vector) | package: base |
A bit vector is a mutable sequence whose elements are booleans. A bit vector also acts as a dictionary (dict? from racket/dict), where the keys are zero-based indexes and the values are the elements of the bit-vector. A bit-vector has a fixed size.
Two bit-vectors are equal? if they contain the same number of elements and if they contain equal elements at each index.
procedure
(make-bit-vector size [fill]) → bit-vector?
size : exact-integer? fill : boolean? = #f
> (bit-vector-ref (make-bit-vector 3) 2) #f
> (bit-vector-ref (make-bit-vector 3 #t) 2) #t
procedure
(bit-vector elem ...) → bit-vector?
elem : boolean?
> (bit-vector-ref (bit-vector #f #t #f) 1) #t
procedure
(bit-vector? v) → boolean?
v : any/c
procedure
(bit-vector-ref bv index [default]) → any/c
bv : bit-vector? index : exact-nonnegative-integer? default : any/c = (error ....)
> (bit-vector-ref (bit-vector #f #t) 1) #t
> (bit-vector-ref (bit-vector #f #t) 5 'not-there) 'not-there
procedure
(bit-vector-set! bv index value) → void?
bv : bit-vector?
index :
(and/c exact-nonnegative-integer? (</c (+ 1 (bit-vector-length vv)))) value : boolean?
> (define bv (bit-vector #f #t)) > (bit-vector-ref bv 0) #f
> (bit-vector-set! bv 0 #t) > (bit-vector-ref bv 0) #t
procedure
bv : bit-vector?
procedure
bv : bit-vector?
> (bit-vector-popcount (bit-vector #f #t #t)) 2
procedure
(bit-vector-copy bv [start end]) → bit-vector?
bv : bit-vector? start : exact-nonnegative-integer? = 0 end : exact-nonnegative-integer? = (vector-length v)
procedure
(in-bit-vector bv) → sequence?
bv : bit-vector?
> (define bv (bit-vector #f #t #f)) > (for/list ([x (in-bit-vector bv)]) x) '(#f #t #f)
syntax
(for/bit-vector maybe-length (for-clause ...) body-or-break ... body)
maybe-length =
| #:length length-expr | #:length length-expr #:fill fill-expr
length-expr : exact-nonnegative-integer?
If the optional #:length clause is specified, the result of length-expr determines the length of the result bit-vector. In that case, the iteration can be performed more efficiently, and it terminates when the bit-vector is full or the requested number of iterations have been performed, whichever comes first. If length-expr specifies a length longer than the number of iterations, then the remaining slots of the vector are initialized to the value of fill-expr, which defaults to #f (i.e., the default argument of make-bit-vector).
> (bit-vector->list (for/bit-vector ([i '(1 2 3)]) (odd? i))) '(#t #f #t)
> (bit-vector->list (for/bit-vector #:length 2 ([i '(1 2 3)]) (odd? i))) '(#t #f)
> (bit-vector->list (for/bit-vector #:length 4 ([i '(1 2 3)]) (odd? i))) '(#t #f #t #f)
> (bit-vector->list (for/bit-vector #:length 4 #:fill #t ([i '(1 2 3)]) (odd? i))) '(#t #f #t #t)
The for/bit-vector form may allocate a bit-vector and mutate it after each iteration of body, which means that capturing a continuation during body and applying it multiple times may mutate a shared bit-vector.
syntax
(for*/bit-vector maybe-length (for-clause ...) body-or-break ... body)
procedure
(bit-vector->list bv) → (listof boolean?)
bv : bit-vector?
procedure
(list->bit-vector bits) → bit-vector?
bits : (listof boolean?)
procedure
(bit-vector->string bv) → (and/c string? #rx"^[01]*$")
bv : bit-vector?
procedure
s : (and/c string? #rx"^[01]*$")
> (bit-vector->list (string->bit-vector "100111")) '(#t #f #f #t #t #t)
> (bit-vector->string (list->bit-vector '(#t #f #t #t))) "1011"