What is the data structure for an array of bit flags?

I am porting some imperative code to Haskell. My goal is to analyze the executable file, so each byte of the text section is assigned several flags that will all fit into bytes (more precisely, 6 bits).

In a language like C, I would just allocate an array of bytes, nullify them, and update when I go. How can I do this efficiently in Haskell?

In other words: I'm looking for a ByteString with bit access and constant time updates when I parse more of a text section.

Edit: Of course, any other data structure would work if it were just as efficient.

+5
source share
2 answers

You can use a vector with any data type that is an instance of the Bits typeclass , for example, Word64 for 64 bits per element in the vector. Use unboxed vector if you want the array to be contiguous in memory.

+6
source

The implementation for unpacked Bool -s arrays in an array is a packed bitarray. You can perform mutable updates on such arrays in ST Monad (this is essentially the same runtime behavior as in C).

+9
source

Source: https://habr.com/ru/post/1204466/


All Articles