module Data.Tensort.Tensort
( tensort,
tensortB4,
tensortBN,
tensortBL,
mkTSProps,
)
where
import Data.Tensort.Subalgorithms.Bubblesort (bubblesort)
import Data.Tensort.Utils.Compose (createInitialTensors)
import Data.Tensort.Utils.Convert (rawBitsToBytes)
import Data.Tensort.Utils.RandomizeList (randomizeList)
import Data.Tensort.Utils.Reduce (reduceTensorStacks)
import Data.Tensort.Utils.Render (getSortedBitsFromTensor)
import Data.Tensort.Utils.Types (Bit, SortAlg, Sortable (..), TensortProps (..), fromSortBit)
tensort :: [Bit] -> TensortProps -> [Bit]
tensort :: [Int] -> TensortProps -> [Int]
tensort [] TensortProps
_ = []
tensort [Int]
xs TensortProps
tsProps = do
let bits :: Sortable
bits = Sortable -> Int -> Sortable
randomizeList ([Int] -> Sortable
SortBit [Int]
xs) Int
143
let bytes :: [[Int]]
bytes = [Int] -> TensortProps -> [[Int]]
rawBitsToBytes (Sortable -> [Int]
fromSortBit Sortable
bits) TensortProps
tsProps
let tensorStacks :: [Tensor]
tensorStacks = [[Int]] -> TensortProps -> [Tensor]
createInitialTensors [[Int]]
bytes TensortProps
tsProps
let topTensor :: Tensor
topTensor = [Tensor] -> TensortProps -> Tensor
reduceTensorStacks [Tensor]
tensorStacks TensortProps
tsProps
Tensor -> SortAlg -> [Int]
getSortedBitsFromTensor Tensor
topTensor (TensortProps -> SortAlg
subAlgorithm TensortProps
tsProps)
mkTSProps :: Int -> SortAlg -> TensortProps
mkTSProps :: Int -> SortAlg -> TensortProps
mkTSProps Int
bSize SortAlg
subAlg = TensortProps {bytesize :: Int
bytesize = Int
bSize, subAlgorithm :: SortAlg
subAlgorithm = SortAlg
subAlg}
tensortB4 :: [Bit] -> [Bit]
tensortB4 :: [Int] -> [Int]
tensortB4 [Int]
xs = [Int] -> TensortProps -> [Int]
tensort [Int]
xs (Int -> SortAlg -> TensortProps
mkTSProps Int
4 SortAlg
bubblesort)
tensortBN :: Int -> [Bit] -> [Bit]
tensortBN :: Int -> [Int] -> [Int]
tensortBN Int
n [Int]
xs = [Int] -> TensortProps -> [Int]
tensort [Int]
xs (Int -> SortAlg -> TensortProps
mkTSProps Int
n SortAlg
bubblesort)
tensortBL :: [Bit] -> [Bit]
tensortBL :: [Int] -> [Int]
tensortBL [] = []
tensortBL [Int
x] = [Int
x]
tensortBL [Int
x, Int
y] = if Int
x Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
y then [Int
x, Int
y] else [Int
y, Int
x]
tensortBL [Int]
xs = [Int] -> TensortProps -> [Int]
tensort [Int]
xs (Int -> SortAlg -> TensortProps
mkTSProps ([Int] -> Int
calculateBytesize [Int]
xs) SortAlg
bubblesort)
calculateBytesize :: [Bit] -> Int
calculateBytesize :: [Int] -> Int
calculateBytesize [Int]
xs = Double -> Int
forall b. Integral b => Double -> b
forall a b. (RealFrac a, Integral b) => a -> b
ceiling (Double -> Double
forall a. Floating a => a -> a
log (Int -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral ([Int] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [Int]
xs)) :: Double)