module Data.Tensort.Utils.Compose
( createInitialTensors,
createTensor,
)
where
import Data.Tensort.Utils.Split (splitEvery)
import Data.Tensort.Utils.Types (Byte, Memory (..), Record, SortAlg, Sortable (..), Tensor, TensortProps (..), fromSortRec, Bit)
createInitialTensors :: [Byte] -> TensortProps -> [Tensor]
createInitialTensors :: [Byte] -> TensortProps -> [Tensor]
createInitialTensors [Byte]
bytes TensortProps
tsProps = ([Byte] -> [Tensor] -> [Tensor])
-> [Tensor] -> [[Byte]] -> [Tensor]
forall a b. (a -> b -> b) -> b -> [a] -> b
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr [Byte] -> [Tensor] -> [Tensor]
acc [] (Int -> [Byte] -> [[Byte]]
forall a. Int -> [a] -> [[a]]
splitEvery (TensortProps -> Int
bytesize TensortProps
tsProps) [Byte]
bytes)
where
acc :: [Byte] -> [Tensor] -> [Tensor]
acc :: [Byte] -> [Tensor] -> [Tensor]
acc [Byte]
byte [Tensor]
tensorStacks = [Tensor]
tensorStacks [Tensor] -> [Tensor] -> [Tensor]
forall a. [a] -> [a] -> [a]
++ [[Byte] -> SortAlg -> Tensor
getTensorFromBytes [Byte]
byte (TensortProps -> SortAlg
subAlgorithm TensortProps
tsProps)]
createTensor :: Memory -> SortAlg -> Tensor
createTensor :: Memory -> SortAlg -> Tensor
createTensor (ByteMem [Byte]
bytes) SortAlg
subAlg = [Byte] -> SortAlg -> Tensor
getTensorFromBytes [Byte]
bytes SortAlg
subAlg
createTensor (TensorMem [Tensor]
tensors) SortAlg
subAlg = [Tensor] -> SortAlg -> Tensor
getTensorFromTensors [Tensor]
tensors SortAlg
subAlg
getTensorFromBytes :: [Byte] -> SortAlg -> Tensor
getTensorFromBytes :: [Byte] -> SortAlg -> Tensor
getTensorFromBytes [Byte]
bytes SortAlg
subAlg = do
let register :: [Record]
register = [Byte] -> [Record] -> Int -> [Record]
acc [Byte]
bytes [] Int
0
let register' :: [Record]
register' = Sortable -> [Record]
fromSortRec (SortAlg
subAlg ([Record] -> Sortable
SortRec [Record]
register))
([Record]
register', [Byte] -> Memory
ByteMem [Byte]
bytes)
where
acc :: [Byte] -> [Record] -> Int -> [Record]
acc :: [Byte] -> [Record] -> Int -> [Record]
acc [] [Record]
register Int
_ = [Record]
register
acc ([] : [Byte]
remainingBytes) [Record]
register Int
i = [Byte] -> [Record] -> Int -> [Record]
acc [Byte]
remainingBytes [Record]
register (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)
acc (Byte
byte : [Byte]
remainingBytes) [Record]
register Int
i = [Byte] -> [Record] -> Int -> [Record]
acc [Byte]
remainingBytes ([Record]
register [Record] -> [Record] -> [Record]
forall a. [a] -> [a] -> [a]
++ [(Int
i, Byte -> Int
forall a. HasCallStack => [a] -> a
last Byte
byte)]) (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)
getTensorFromTensors :: [Tensor] -> SortAlg -> Tensor
getTensorFromTensors :: [Tensor] -> SortAlg -> Tensor
getTensorFromTensors [Tensor]
tensors SortAlg
subAlg = (Sortable -> [Record]
fromSortRec (SortAlg
subAlg ([Record] -> Sortable
SortRec ([Tensor] -> [Record]
getRegisterFromTensors [Tensor]
tensors))), [Tensor] -> Memory
TensorMem [Tensor]
tensors)
getRegisterFromTensors :: [Tensor] -> [Record]
getRegisterFromTensors :: [Tensor] -> [Record]
getRegisterFromTensors [Tensor]
tensors = [Tensor] -> [Record] -> [Record]
acc [Tensor]
tensors []
where
acc :: [Tensor] -> [Record] -> [Record]
acc :: [Tensor] -> [Record] -> [Record]
acc [] [Record]
records = [Record]
records
acc (([], Memory
_) : [Tensor]
remainingTensors) [Record]
records = [Tensor] -> [Record] -> [Record]
acc [Tensor]
remainingTensors [Record]
records
acc (Tensor
tensor : [Tensor]
remainingTensors) [Record]
records = [Tensor] -> [Record] -> [Record]
acc [Tensor]
remainingTensors ([Record]
records [Record] -> [Record] -> [Record]
forall a. [a] -> [a] -> [a]
++ [(Int
i, Tensor -> Int
getTopBitFromTensorStack Tensor
tensor)])
where
i :: Int
i = [Record] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [Record]
records
getTopBitFromTensorStack :: Tensor -> Bit
getTopBitFromTensorStack :: Tensor -> Int
getTopBitFromTensorStack ([Record]
register, Memory
_) = Record -> Int
forall a b. (a, b) -> b
snd ([Record] -> Record
forall a. HasCallStack => [a] -> a
last [Record]
register)