A high performance Nim implementation of BitVector with base (int, int64, uint32, uint16, or uint8), and with support for slices and other seq supported operations. BitVector format order is little endian, where Least Significant Byte has the lowest address. BitVector is an in-memory bit vector, no mmap option is available at the moment.
Usage
import bitvector block: var ba = newBitVector[uint](64) # Create a BitVector with uint64 base echo ba # Prints capacity in bits = 64, and number of elements = 1 ba[0] = 1 # Assign `true` to bit index `0` ba[2] = 1 ba[7] = 1 # ba[0..7] now is `10000101` == 133 assert(ba[0..7] == 133, "incorrect result: " & $ba[0..7]) assert(ba[0..4] == 5, "incorrect result: " & $ba[0..4]) assert(ba[1..4] == 2, "incorrect result: " & $ba[1..4]) var bitvectorA = newBitVector[uint](2e9) bitvectorA[0] = 1 bitvectorA[1] = 1 bitvectorA[2] = 1 # Test range lookups/inserts bitvectorA[65] = 1 doAssert bitvectorA[65] == 1 bitvectorA[131] = 1 bitvectorA[194] = 1 assert bitvectorA[2..66] == bitvectorA[131..194] let sliceValue = bitvectorA[131..194] bitvectorA[270..333] = sliceValue bitvectorA[400..463] = uint(-9223372036854775807) assert bitvectorA[131..194] == bitvectorA[270..333] assert bitvectorA[131..194] == bitvectorA[400..463]
Funcs
func newBitVector[T](size: int; init = 0): BitVector[T] {...}{.inline.}
- Create new in-memory BitVector of type T and number of elements is size rounded up to the nearest byte. You can initialize the bitvector to 1 by passing any value other than zero to init.
func `[]`[T](b: BitVector[T]; i: int): Bit {...}{.inline.}
func `[]`[T](x: BitVector[T]; i: Slice[int]): T {...}{.inline.}
func cap[T](b: BitVector[T]): int {...}{.inline.}
- Returns capacity, i.e number of bits
func len[T](b: BitVector[T]): int {...}{.inline.}
- Returns length, i.e number of elements
func toBitVector[T](x: openArray[int]): BitVector[T]
func toBitVector[T](x: int): BitVector[T]
func `==`(x, y: BitVector): bool
func `$`[T](b: BitVector[T]): string {...}{.inline.}
- Prints number of bits and elements the BitVector is capable of handling. It also prints out a slice if specified in little endian format.