-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: re-implement bit bector properly
Signed-off-by: Rudi Grinberg <[email protected]>
- Loading branch information
Showing
3 changed files
with
43 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,47 @@ | ||
type t = bool array | ||
type t = | ||
{ len : int | ||
; bits : Bytes.t | ||
} | ||
let byte s i = Char.code (Bytes.unsafe_get s i) | ||
|
||
let singleton v = [| v |] | ||
let low_mask = Array.init 9 (fun i -> (1 lsl i) - 1) | ||
let set_byte s i x = Bytes.unsafe_set s i (Char.chr x) | ||
|
||
let length t = Array.length t | ||
let create len b = | ||
let initv = if b then 255 else 0 in | ||
let q = len lsr 3 in | ||
let r = len land 7 in | ||
if r = 0 then | ||
{ len ; bits = Bytes.make q (Char.chr initv) } | ||
else begin | ||
let s = Bytes.make (q + 1) (Char.chr initv) in | ||
set_byte s q (initv land low_mask.(r)); | ||
{ len ; bits = s } | ||
end | ||
|
||
let set t i a = t.(i) <- a | ||
let singleton v = create 1 v | ||
|
||
let get t i = t.(i) | ||
let length t = t.len | ||
|
||
let create len v = Array.make len v | ||
let unsafe_set v n b = | ||
let i = n lsr 3 in | ||
let c = byte v.bits i in | ||
let mask = 1 lsl (n land 7) in | ||
set_byte v.bits i (if b then c lor mask else c land (lnot mask)) | ||
|
||
let set_all t v = | ||
Array.fill t 0 (length t) v | ||
let set v n b = | ||
if n < 0 || n >= v.len then invalid_arg "Bitv.set"; | ||
unsafe_set v n b | ||
|
||
let unsafe_get v n = | ||
let i = n lsr 3 in | ||
(byte v.bits i) land (1 lsl (n land 7)) > 0 | ||
|
||
let get v n = | ||
if n < 0 || n >= v.len then invalid_arg "Bitv.get"; | ||
unsafe_get v n | ||
|
||
let reset_zero t = | ||
for i = 0 to Bytes.length t.bits - 1 do | ||
Bytes.set t.bits i '\000' | ||
done |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters