Bitset Type

Bitsets are arrays of bits.

They were originally introduced to provide testable state for SRFI-14 char-sets: “is this code point Lowercase?”

Reader Form

The reader form for bitsets is

#B{ size [bits ...] }.

size indicates the absolute size of the bitset with indexing starting at 0. Referencing a bit beyond this range is an error.

All bits are initially cleared.

bits defines the bits that are initially set (or clear) with a sequence of 1s and 0s in blocks of up to 8 bits. The first 0 or 1 sets the first bit, the second 0 or 1 the second bit etc.. The next block of eight bits starts at the ninth bit and continues.

Setting the nth bit in a large bitset should not involve explicitly indicating that all the previous bits were 0 so bits can take the form offset:bits where offset is a hexadecimal number and is a multiple of 8. Intervening blocks are skipped. Subsequent bits continue with the next block.

Whole blocks of bits can be set with the form first-last where first and last are hexadecimal numbers (and multiples of 8) representing the first block of all 1s through to the last block inclusive. To set a single block use first-first.

By way of example:

  • #B{ 32 001 } is much like hexadecimal 0x0004

  • #B{ 32 8:1 } is much like hexadecimal 0x0100

  • #B{ 32 1 10-18 } is much like hexadecimal 0xFF01

  • #B{ 32 01 10-10 00000001 } is much like hexadecimal 0x8F02

  • #B{ 65536 30-30 11000000 01111110 60:01111110 } is the bitset of Unicode’s ASCII_Hex_Digit Property in Plane 0, this it, bits 0x30-0x39, 0x41-0x46 and 0x61-0x66.

  • try typing SRFI-14/char-set:lower-case at the Idio prompt to see a richer example

Bitset Predicates

function bitset? o

test if o is an bitset

Param o:

object to test

Return:

#t if o is an bitset, #f otherwise

Bitset Constructors

function make-bitset size

create an bitset with a size of size

Param size:

initial bitset size

Type size:

integer

Rtype:

bitset

Bitset Attributes

function bitset-size bs

return the size of bitset bs

Rtype:

integer

function bitset-set! bs bit

set bit bit in bitset bs

Param bs:

bitset

Type bs:

bitset

Param bit:

bit

Type bit:

unicode or integer

Return:

#<unspec>

function bitset-clear! bs bit

clear bit bit in bitset bs

Param bs:

bitset

Type bs:

bitset

Param bit:

bit

Type bit:

unicode or integer

Return:

#<unspec>

function bitset-ref bs bit

get bit bit in bitset bs

Param bs:

bitset

Type bs:

bitset

Param bit:

bit

Type bit:

unicode or integer

Rtype:

#<unspec>

Bitset Functions

function copy-bitset bs

copy the bitset

Param args:

bitset to be copied

Type args:

bitset

Rtype:

bitset

function merge-bitset [bs ...]

merge the bitsets

Param args:

bitsets to be merged

Type args:

list

Rtype:

bitset or #n if no bitsets supplied

function and-bitset [bs ...]

logical AND the bitsets

Param args:

bitsets to be operated on

Type args:

list

Rtype:

bitset or #n if no bitsets supplied

function ior-bitset [bs ...]

logical Inclusive OR the bitsets

Param args:

bitsets to be operated on

Type args:

list

Rtype:

bitset or #n if no bitsets supplied

function xor-bitset [bs ...]

logical eXclusive OR the bitsets

Param args:

bitsets to be operated on

Type args:

list

Rtype:

bitset or #n if no bitsets supplied

function not-bitset bs

logical complement of the bitset

Param args:

bitset to be operated on

Type args:

bitset

Rtype:

bitset

function subtract-bitset [bs ...]

subtract the bitsets

Param args:

bitsets to be operated on

Type args:

list

Rtype:

bitset or #n if no bitsets supplied

function equal-bitset? [bs ...]

are the bitsets equal

Param args:

bitsets to be operated on

Type args:

list

Rtype:

bitset or #f if no bitsets supplied

function bitset-for-each-set bs f

invoke f on each bit in bitset bs that is set

Param bs:

bitset to be operated on

Type bs:

bitset

Param f:

function to invoke on each set bit

Type f:

function of 1 arg

Rtype:

#<unspec>

The argument to f will be the index of the bit

function fold-bitset bs f v

invoke f on each bit in bitset bs that is set accumulating the result in v

Param bs:

bitset to be operated on

Type bs:

bitset

Param f:

function to invoke on each set bit

Type f:

function of 2 args

Param v:

accumulated value

Type v:

any

Return:

the accumulated value

Rtype:

any

For each set bit, the arguments to f will be the index of the bit and v and v is subsequently set to the result of f.

Last built at 2024-05-17T06:10:57Z+0000 from 62cca4c (dev) for Idio 0.3.b.6