Convert ByteString to Vector

I have ByteStringone that contains a view of Floats. Each Floatis represented by 3 bytes per ByteString.

I need to do some processing of values Float, so I would like to do this processing with Vectorvalues Float. What would be the best way to do this?

I have a function toFloat :: [Word8] -> Floatthat converts 3 bytes ByteStringto Float. So I was thinking of iterating over ByteStringwith a 3 byte step and converting each step to Floatfor Vector.

I looked through the library functions for Vector, but I can not find anything that is suitable for this purpose. Data.Vector.Storable.ByteString.byteStringToVectorIt looked promising, but it converts every byte (instead of every 3 bytes) and gives me no control over how the conversion ByteStringto should happen Float.

+4
source share
2 answers

Just use Data.Vector.generate:

V.generate (BS.length bs `div` 3) $ \i ->
  myToFloat (bs BS.! 3*i) (bs BS.! 3*i+1) (bs BS.! 3*i+2)

It will select the vector all at once and fill it. Data.ByteString.!- O (1), therefore it is quite effective.

+2
source

Try using

splitAt :: Int -> ByteString -> (ByteString, ByteString)

ByteString : 3 , - . , 3 ( Data.List.Split.chunksOf), unpack , [Word8], . toFloat Vector.fromList.

, , , , , , , , , , /fromList. ByteString O (1), , . , .

+2

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


All Articles