-- | This module provides the isSorted function, which checks if a list of
--   elements is sorted in ascending order.
module Data.Tensort.Utils.Check (isSorted) where

import Data.Tensort.Utils.ComparisonFunctions (lessThanOrEqualBit, lessThanOrEqualRecord)
import Data.Tensort.Utils.Types (Sortable (..))

-- | Takes a Sortable list and returns True if the list is sorted in ascending
--   order and False otherwise.

-- | ==== __Examples__
-- >>> isSorted (SortBit [0, 1, 2, 3, 4])
-- True
--
-- >>> isSorted (SortBit [0, 1, 2, 4, 3])
-- False
isSorted :: Sortable -> Bool
isSorted :: Sortable -> Bool
isSorted (SortBit []) = Bool
True
isSorted (SortBit [Bit
_]) = Bool
True
isSorted (SortBit (Bit
x : Bit
y : [Bit]
remainingElements)) = Bit -> Bit -> Bool
lessThanOrEqualBit Bit
x Bit
y Bool -> Bool -> Bool
&& Sortable -> Bool
isSorted ([Bit] -> Sortable
SortBit (Bit
y Bit -> [Bit] -> [Bit]
forall a. a -> [a] -> [a]
: [Bit]
remainingElements))
isSorted (SortRec []) = Bool
True
isSorted (SortRec [Record
_]) = Bool
True
isSorted (SortRec (Record
x : Record
y : [Record]
remainingElements)) = Record -> Record -> Bool
lessThanOrEqualRecord Record
x Record
y Bool -> Bool -> Bool
&& Sortable -> Bool
isSorted ([Record] -> Sortable
SortRec (Record
y Record -> [Record] -> [Record]
forall a. a -> [a] -> [a]
: [Record]
remainingElements))