Skip to content
This repository was archived by the owner on Jul 7, 2024. It is now read-only.

Commit

Permalink
Bitset fixes (#32)
Browse files Browse the repository at this point in the history
- Simplification and functionality fix for next-clear-bit and next-set-bit
- clear-bit! now ensures the existence of said bit.
  • Loading branch information
Scitoshi Nakayobro authored and SquidDev committed Jan 8, 2019
1 parent 025c24d commit 6e6717c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 11 deletions.
27 changes: 17 additions & 10 deletions lib/data/bitset.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@
out = «bitset: 00000000»
```"
(assert-type! bs bitset)
(ensure-bit-exists! bs bit)
(let* [(data (bitset-data bs))
(eindex (+ (bit->element bit) 1))]
(when (<= eindex (n data))
Expand Down Expand Up @@ -152,7 +153,7 @@
(set-idx! data eindex (bit-xor orig (shl 1 (bit->element-index bit))))))

(defun next-set-bit (bs start)
"Finds the next set bit in the bitset BS after the index START. If no set bit is found, -1 is returned
"Finds the next set bit in the bitset BS at or after the index START. If no set bit is found, -1 is returned
### Example:
```cl
Expand All @@ -162,24 +163,27 @@
out = nil
> (next-set-bit bs 2)
out = 5
> (set-bit! bs 0)
out = nil
> (next-set-bit bs 0)
out = 0
```"
(assert-type! bs bitset)
(let* [(data (bitset-data bs))
(len (n data))
(sindex (bit->element start))
(eindex sindex)
(eindex (bit->element start))
(result -1)]
(demand (<= sindex len))
(demand (< eindex len))
(while (and (= result -1) (< eindex len))
(for i 0 (- bits-per-int 1) 1
(with (j (+ (* eindex bits-per-int) i))
(when (and (= result -1) (get-bit bs j) (> j start))
(when (and (= result -1) (get-bit bs j) (>= j start))
(set! result j))))
(inc! eindex))
result))

(defun next-clear-bit (bs start)
"Finds the next clear bit in the bitset BS after the index START. If no clear bit is found, -1 is returned
"Finds the next clear bit in the bitset BS at or after the index START. If no clear bit is found, -1 is returned
### Example:
```cl
Expand All @@ -191,18 +195,21 @@
out = nil
> (next-clear-bit bs 0)
out = 2
> (clear-bit! bs 0)
out = nil
> (next-clear-bit bs 0)
out = 0
```"
(assert-type! bs bitset)
(let* [(data (bitset-data bs))
(len (n data))
(sindex (bit->element start))
(eindex sindex)
(eindex (bit->element start))
(result -1)]
(demand (<= sindex len))
(demand (< eindex len))
(while (and (= result -1) (< eindex len))
(for i 0 (- bits-per-int 1) 1
(with (j (+ (* eindex bits-per-int) i))
(when (and (= result -1) (not (get-bit bs j)) (> j start))
(when (and (= result -1) (not (get-bit bs j)) (>= j start))
(set! result j))))
(inc! eindex))
result))
Expand Down
2 changes: 1 addition & 1 deletion tests/lib/data/bitset.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@
(affirm (= (next-set-bit bs 2) 5))
(affirm (= (next-set-bit bs 6) 15))
(affirm (= (next-set-bit bs 30) 33))
(affirm (= (next-set-bit bs 33) -1))))
(affirm (= (next-set-bit bs 33) 33))))

(it "can find the next clear bit"
(with (bs (make-bitset))
Expand Down

0 comments on commit 6e6717c

Please sign in to comment.