So I want to build a barebones static BitArray and I can’t seem to use the type system properly. Here’s my first attempt at a definition:
struct SBitVector{N} <: AbstractVector{Bool}
chunks::SVector{M,UInt64}
len::Int
end
I’m following the BitArray code, only using a static vector for the bit data. I want N to be len, and M to be div(N,64,RoundingUp)
. But this doesn’t work because M hasn’t been defined yet.
I guess I need to include M
in the signature and then write an inner constructor. But even this is beyond me and nothing I’ve tried compiles.
struct SBitVector{N,M} <: AbstractVector{Bool} where {N,M} # am I using where properly?
chunks::SVector{M,UInt64}
len::Int
function SBitVector(Vector{UInt64},len)
# not sure how to do arithmetic on type parameters to enforce M = div(N,64,RoundingUp)
# len = N etc.
end
end
But it’s weird needing two parameters when the second is strictly determined by the first (never mind that I would like to drop len as well, since it is itself is just equal to N!). So any ideas on how to define this kind of thing idiomatically and/or elegantly whichever comes first?
P.S. Use case is these will become “genomes” of genetic algorithms, length a few hundred bits
and I need them small so they fit on the cache, and SVector{Bool,len}
is 8bits per bit.
For this reason I’d also like to drop len
, and use the type parameter itself as function parameter where needed to save space for another UInt64 chunk
.
Thanks for all the help.