module Data.Tensort.Utils.Compose
( createInitialTensors,
createTensor,
)
where
import Data.Tensort.Utils.SimplifyRegister
( applySortingFromSimplifiedRegister,
simplifyRegister,
)
import Data.Tensort.Utils.Split (splitEvery)
import Data.Tensort.Utils.Types
( Byte,
ByteR,
Memory (..),
MemoryR (..),
Record,
RecordR,
SBit (..),
SBytes (..),
SMemory (..),
SRecord (..),
STensor (..),
STensors (..),
SortAlg,
Sortable (..),
Tensor,
TensorR,
TensortProps (..),
fromSBitBit,
fromSBitRec,
fromSRecordArrayBit,
fromSRecordArrayRec,
fromSTensorBit,
fromSTensorRec,
fromSortRec,
)
createInitialTensors :: TensortProps -> SBytes -> STensors
createInitialTensors :: TensortProps -> SBytes -> STensors
createInitialTensors TensortProps
tsProps (SBytesBit [Byte]
bytes) =
[Tensor] -> STensors
STensorsBit (TensortProps -> [Byte] -> [Tensor]
createInitialTensorsBits TensortProps
tsProps [Byte]
bytes)
createInitialTensors TensortProps
tsProps (SBytesRec [ByteR]
recs) =
[TensorR] -> STensors
STensorsRec (TensortProps -> [ByteR] -> [TensorR]
createInitialTensorsRecs TensortProps
tsProps [ByteR]
recs)
createInitialTensorsBits :: TensortProps -> [Byte] -> [Tensor]
createInitialTensorsBits :: TensortProps -> [Byte] -> [Tensor]
createInitialTensorsBits TensortProps
tsProps [Byte]
bytes =
([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]
++ [ STensor -> Tensor
fromSTensorBit
(SortAlg -> SBytes -> STensor
getTensorFromBytes (TensortProps -> SortAlg
subAlgorithm TensortProps
tsProps) ([Byte] -> SBytes
SBytesBit [Byte]
byte))
]
createInitialTensorsRecs :: TensortProps -> [ByteR] -> [TensorR]
createInitialTensorsRecs :: TensortProps -> [ByteR] -> [TensorR]
createInitialTensorsRecs TensortProps
tsProps [ByteR]
bytesR =
([ByteR] -> [TensorR] -> [TensorR])
-> [TensorR] -> [[ByteR]] -> [TensorR]
forall a b. (a -> b -> b) -> b -> [a] -> b
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr [ByteR] -> [TensorR] -> [TensorR]
acc [] (Int -> [ByteR] -> [[ByteR]]
forall a. Int -> [a] -> [[a]]
splitEvery (TensortProps -> Int
bytesize TensortProps
tsProps) [ByteR]
bytesR)
where
acc :: [ByteR] -> [TensorR] -> [TensorR]
acc :: [ByteR] -> [TensorR] -> [TensorR]
acc [ByteR]
byteR [TensorR]
tensorStacks =
[TensorR]
tensorStacks
[TensorR] -> [TensorR] -> [TensorR]
forall a. [a] -> [a] -> [a]
++ [ STensor -> TensorR
fromSTensorRec
(SortAlg -> SBytes -> STensor
getTensorFromBytes (TensortProps -> SortAlg
subAlgorithm TensortProps
tsProps) ([ByteR] -> SBytes
SBytesRec [ByteR]
byteR))
]
createTensor :: SortAlg -> SMemory -> STensor
createTensor :: SortAlg -> SMemory -> STensor
createTensor SortAlg
subAlg (SMemoryBit Memory
memory) = SortAlg -> Memory -> STensor
createTensorB SortAlg
subAlg Memory
memory
createTensor SortAlg
subAlg (SMemoryRec MemoryR
memoryR) = SortAlg -> MemoryR -> STensor
createTensorR SortAlg
subAlg MemoryR
memoryR
createTensorB :: SortAlg -> Memory -> STensor
createTensorB :: SortAlg -> Memory -> STensor
createTensorB SortAlg
subAlg (ByteMem [Byte]
bytes) =
SortAlg -> SBytes -> STensor
getTensorFromBytes SortAlg
subAlg ([Byte] -> SBytes
SBytesBit [Byte]
bytes)
createTensorB SortAlg
subAlg (TensorMem [Tensor]
tensors) =
SortAlg -> STensors -> STensor
getTensorFromTensors SortAlg
subAlg ([Tensor] -> STensors
STensorsBit [Tensor]
tensors)
createTensorR :: SortAlg -> MemoryR -> STensor
createTensorR :: SortAlg -> MemoryR -> STensor
createTensorR SortAlg
subAlg (ByteMemR [ByteR]
bytesR) =
SortAlg -> SBytes -> STensor
getTensorFromBytes SortAlg
subAlg ([ByteR] -> SBytes
SBytesRec [ByteR]
bytesR)
createTensorR SortAlg
subAlg (TensorMemR [TensorR]
tensorsR) =
SortAlg -> STensors -> STensor
getTensorFromTensors SortAlg
subAlg ([TensorR] -> STensors
STensorsRec [TensorR]
tensorsR)
getTensorFromBytes :: SortAlg -> SBytes -> STensor
getTensorFromBytes :: SortAlg -> SBytes -> STensor
getTensorFromBytes SortAlg
subAlg (SBytesBit [Byte]
bytes) =
Tensor -> STensor
STensorBit (SortAlg -> [Byte] -> Tensor
getTensorFromBytesB SortAlg
subAlg [Byte]
bytes)
getTensorFromBytes SortAlg
subAlg (SBytesRec [ByteR]
recs) =
TensorR -> STensor
STensorRec (SortAlg -> [ByteR] -> TensorR
getTensorFromBytesR SortAlg
subAlg [ByteR]
recs)
getTensorFromBytesB :: SortAlg -> [Byte] -> Tensor
getTensorFromBytesB :: SortAlg -> [Byte] -> Tensor
getTensorFromBytesB SortAlg
subAlg [Byte]
bytes = do
let register :: ByteR
register = [Byte] -> ByteR -> Int -> ByteR
acc [Byte]
bytes [] Int
0
let register' :: ByteR
register' = Sortable -> ByteR
fromSortRec (SortAlg
subAlg (ByteR -> Sortable
SortRec ByteR
register))
(ByteR
register', [Byte] -> Memory
ByteMem [Byte]
bytes)
where
acc :: [Byte] -> [Record] -> Int -> [Record]
acc :: [Byte] -> ByteR -> Int -> ByteR
acc [] ByteR
register Int
_ = ByteR
register
acc ([] : [Byte]
remainingBytes) ByteR
register Int
i = [Byte] -> ByteR -> Int -> ByteR
acc [Byte]
remainingBytes ByteR
register (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)
acc (Byte
byte : [Byte]
remainingBytes) ByteR
register Int
i =
[Byte] -> ByteR -> Int -> ByteR
acc [Byte]
remainingBytes (ByteR
register ByteR -> ByteR -> ByteR
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)
getTensorFromBytesR :: SortAlg -> [ByteR] -> TensorR
getTensorFromBytesR :: SortAlg -> [ByteR] -> TensorR
getTensorFromBytesR SortAlg
subAlg [ByteR]
bytesR = do
let registerR :: [RecordR]
registerR = [ByteR] -> [RecordR] -> Int -> [RecordR]
acc [ByteR]
bytesR [] Int
0
let simplifiedRegiser :: ByteR
simplifiedRegiser = [RecordR] -> ByteR
simplifyRegister [RecordR]
registerR
let simplifiedRegiser' :: ByteR
simplifiedRegiser' = Sortable -> ByteR
fromSortRec (SortAlg
subAlg (ByteR -> Sortable
SortRec ByteR
simplifiedRegiser))
let registerR' :: [RecordR]
registerR' =
ByteR -> [RecordR] -> [RecordR]
applySortingFromSimplifiedRegister ByteR
simplifiedRegiser' [RecordR]
registerR
([RecordR]
registerR', [ByteR] -> MemoryR
ByteMemR [ByteR]
bytesR)
where
acc :: [ByteR] -> [RecordR] -> Int -> [RecordR]
acc :: [ByteR] -> [RecordR] -> Int -> [RecordR]
acc [] [RecordR]
register Int
_ = [RecordR]
register
acc ([] : [ByteR]
remainingBytesR) [RecordR]
registerR Int
i =
[ByteR] -> [RecordR] -> Int -> [RecordR]
acc [ByteR]
remainingBytesR [RecordR]
registerR (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)
acc (ByteR
byteR : [ByteR]
remainingBytesR) [RecordR]
registerR Int
i =
[ByteR] -> [RecordR] -> Int -> [RecordR]
acc [ByteR]
remainingBytesR ([RecordR]
registerR [RecordR] -> [RecordR] -> [RecordR]
forall a. [a] -> [a] -> [a]
++ [(Int
i, ByteR -> TopBitR
forall a. HasCallStack => [a] -> a
last ByteR
byteR)]) (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)
getTensorFromTensors :: SortAlg -> STensors -> STensor
getTensorFromTensors :: SortAlg -> STensors -> STensor
getTensorFromTensors SortAlg
subAlg (STensorsBit [Tensor]
tensors) =
Tensor -> STensor
STensorBit (SortAlg -> [Tensor] -> Tensor
getTensorFromTensorsB SortAlg
subAlg [Tensor]
tensors)
getTensorFromTensors SortAlg
subAlg (STensorsRec [TensorR]
tensors) =
TensorR -> STensor
STensorRec (SortAlg -> [TensorR] -> TensorR
getTensorFromTensorsR SortAlg
subAlg [TensorR]
tensors)
getTensorFromTensorsB :: SortAlg -> [Tensor] -> Tensor
getTensorFromTensorsB :: SortAlg -> [Tensor] -> Tensor
getTensorFromTensorsB SortAlg
subAlg [Tensor]
tensors =
( Sortable -> ByteR
fromSortRec
( SortAlg
subAlg
( ByteR -> Sortable
SortRec
( [SRecord] -> ByteR
fromSRecordArrayBit
(STensors -> [SRecord]
getRegisterFromTensors ([Tensor] -> STensors
STensorsBit [Tensor]
tensors))
)
)
),
[Tensor] -> Memory
TensorMem [Tensor]
tensors
)
getTensorFromTensorsR :: SortAlg -> [TensorR] -> TensorR
getTensorFromTensorsR :: SortAlg -> [TensorR] -> TensorR
getTensorFromTensorsR SortAlg
subAlg [TensorR]
tensorsR = do
let registerR :: [SRecord]
registerR = STensors -> [SRecord]
getRegisterFromTensors ([TensorR] -> STensors
STensorsRec [TensorR]
tensorsR)
let simplifiedRegiser :: ByteR
simplifiedRegiser = [RecordR] -> ByteR
simplifyRegister ([SRecord] -> [RecordR]
fromSRecordArrayRec [SRecord]
registerR)
let simplifiedRegiser' :: ByteR
simplifiedRegiser' = Sortable -> ByteR
fromSortRec (SortAlg
subAlg (ByteR -> Sortable
SortRec ByteR
simplifiedRegiser))
let registerR' :: [RecordR]
registerR' =
ByteR -> [RecordR] -> [RecordR]
applySortingFromSimplifiedRegister
ByteR
simplifiedRegiser'
([SRecord] -> [RecordR]
fromSRecordArrayRec [SRecord]
registerR)
([RecordR]
registerR', [TensorR] -> MemoryR
TensorMemR [TensorR]
tensorsR)
getRegisterFromTensors :: STensors -> [SRecord]
getRegisterFromTensors :: STensors -> [SRecord]
getRegisterFromTensors (STensorsBit [Tensor]
tensors) = [Tensor] -> [SRecord]
getRegisterFromTensorsB [Tensor]
tensors
getRegisterFromTensors (STensorsRec [TensorR]
tensors) = [TensorR] -> [SRecord]
getRegisterFromTensorsR [TensorR]
tensors
getRegisterFromTensorsB :: [Tensor] -> [SRecord]
getRegisterFromTensorsB :: [Tensor] -> [SRecord]
getRegisterFromTensorsB [Tensor]
tensors = [Tensor] -> [SRecord] -> [SRecord]
acc [Tensor]
tensors []
where
acc :: [Tensor] -> [SRecord] -> [SRecord]
acc :: [Tensor] -> [SRecord] -> [SRecord]
acc [] [SRecord]
records = [SRecord]
records
acc (([], Memory
_) : [Tensor]
remainingTensors) [SRecord]
records = [Tensor] -> [SRecord] -> [SRecord]
acc [Tensor]
remainingTensors [SRecord]
records
acc (Tensor
tensor : [Tensor]
remainingTensors) [SRecord]
records =
[Tensor] -> [SRecord] -> [SRecord]
acc
[Tensor]
remainingTensors
( [SRecord]
records
[SRecord] -> [SRecord] -> [SRecord]
forall a. [a] -> [a] -> [a]
++ [ TopBitR -> SRecord
SRecordBit
( Int
i,
SBit -> Int
fromSBitBit
(STensor -> SBit
getTopBitFromTensorStack (Tensor -> STensor
STensorBit Tensor
tensor))
)
]
)
where
i :: Int
i = [SRecord] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [SRecord]
records
getRegisterFromTensorsR :: [TensorR] -> [SRecord]
getRegisterFromTensorsR :: [TensorR] -> [SRecord]
getRegisterFromTensorsR [TensorR]
tensorsR = [TensorR] -> [SRecord] -> [SRecord]
acc [TensorR]
tensorsR []
where
acc :: [TensorR] -> [SRecord] -> [SRecord]
acc :: [TensorR] -> [SRecord] -> [SRecord]
acc [] [SRecord]
records = [SRecord]
records
acc (([], MemoryR
_) : [TensorR]
remainingTensorsR) [SRecord]
records = [TensorR] -> [SRecord] -> [SRecord]
acc [TensorR]
remainingTensorsR [SRecord]
records
acc (TensorR
tensorR : [TensorR]
remainingTensorsR) [SRecord]
records =
[TensorR] -> [SRecord] -> [SRecord]
acc
[TensorR]
remainingTensorsR
( [SRecord]
records
[SRecord] -> [SRecord] -> [SRecord]
forall a. [a] -> [a] -> [a]
++ [ RecordR -> SRecord
SRecordRec
( Int
i,
SBit -> TopBitR
fromSBitRec
(STensor -> SBit
getTopBitFromTensorStack (TensorR -> STensor
STensorRec TensorR
tensorR))
)
]
)
where
i :: Int
i = [SRecord] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [SRecord]
records
getTopBitFromTensorStack :: STensor -> SBit
getTopBitFromTensorStack :: STensor -> SBit
getTopBitFromTensorStack (STensorBit Tensor
tensor) =
Tensor -> SBit
getTopBitFromTensorStackB Tensor
tensor
getTopBitFromTensorStack (STensorRec TensorR
tensorR) =
TensorR -> SBit
getTopBitFromTensorStackR TensorR
tensorR
getTopBitFromTensorStackB :: Tensor -> SBit
getTopBitFromTensorStackB :: Tensor -> SBit
getTopBitFromTensorStackB (ByteR
register, Memory
_) = Int -> SBit
SBitBit (TopBitR -> Int
forall a b. (a, b) -> b
snd (ByteR -> TopBitR
forall a. HasCallStack => [a] -> a
last ByteR
register))
getTopBitFromTensorStackR :: TensorR -> SBit
getTopBitFromTensorStackR :: TensorR -> SBit
getTopBitFromTensorStackR ([RecordR]
registerR, MemoryR
_) = TopBitR -> SBit
SBitRec (RecordR -> TopBitR
forall a b. (a, b) -> b
snd ([RecordR] -> RecordR
forall a. HasCallStack => [a] -> a
last [RecordR]
registerR))