module Data.Tensort.Utils.Convert (rawBitsToBytes) where

import Data.Tensort.Utils.Split (splitEvery)
import Data.Tensort.Utils.Types (Byte, Sortable (..), TensortProps (..), fromSortBit, Bit)

-- | Convert a list of Bits to a list of Bytes of given bytesize, bubblesorting
--   each byte.

-- | ==== __Examples__
--   >>> rawBitsToBytes [5,1,3,7,8,2,4,6] 4
--   [[2,4,6,8],[1,3,5,7]]
-- rawBitsToBytes :: [Bit] -> Int -> [Byte]
-- rawBitsToBytes bits bytesize = foldr acc [] (splitEvery bytesize bits)
--   where
--     acc :: [Bit] -> [Byte] -> [Byte]
--     acc byte bytes = bytes ++ [fromSortBit (bubblesort (SortBit byte))]
rawBitsToBytes :: [Bit] -> TensortProps -> [Byte]
rawBitsToBytes :: [Bit] -> TensortProps -> [[Bit]]
rawBitsToBytes [Bit]
bits TensortProps
tsProps = ([Bit] -> [[Bit]] -> [[Bit]]) -> [[Bit]] -> [[Bit]] -> [[Bit]]
forall a b. (a -> b -> b) -> b -> [a] -> b
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr [Bit] -> [[Bit]] -> [[Bit]]
acc [] (Bit -> [Bit] -> [[Bit]]
forall a. Bit -> [a] -> [[a]]
splitEvery (TensortProps -> Bit
bytesize TensortProps
tsProps) [Bit]
bits)
  where
    acc :: [Bit] -> [Byte] -> [Byte]
    acc :: [Bit] -> [[Bit]] -> [[Bit]]
acc [Bit]
byte [[Bit]]
bytes = [[Bit]]
bytes [[Bit]] -> [[Bit]] -> [[Bit]]
forall a. [a] -> [a] -> [a]
++ [Sortable -> [Bit]
fromSortBit (TensortProps -> SortAlg
subAlgorithm TensortProps
tsProps ([Bit] -> Sortable
SortBit [Bit]
byte))]