-- |
-- Module      :  Data.MinMax
-- Copyright   :  (c) OleksandrZhabenko 2020-2023
-- License     :  MIT
-- Stability   :  Experimental
-- Maintainer  :  oleksandr.zhabenko@yahoo.com
--
-- Functions to find both minimum and maximum elements of the 'F.Foldable' structure of the 'Ord'ered elements.

{-# LANGUAGE NoImplicitPrelude #-}

module Data.MinMax where

import GHC.Base
import Data.SubG
import qualified Data.Foldable as F
import qualified Data.List as L (sortBy)

-- | Returns a pair where the first element is the minimum element from the two given ones and the second one is the maximum. If the arguments are
-- equal then the tuple contains equal elements.
minmaxP :: (Ord a) => a -> a -> (a,a)
minmaxP :: forall a. Ord a => a -> a -> (a, a)
minmaxP = forall a. Ord a => (a -> a -> Ordering) -> a -> a -> (a, a)
minmaxPBy forall a. Ord a => a -> a -> Ordering
compare
{-# INLINE minmaxP #-}

-- | A variant of the 'minmaxP' where you can specify your own comparison function.
minmaxPBy :: (Ord a) => (a -> a -> Ordering) -> a -> a -> (a,a)
minmaxPBy :: forall a. Ord a => (a -> a -> Ordering) -> a -> a -> (a, a)
minmaxPBy a -> a -> Ordering
g a
x a
y
 | a -> a -> Ordering
g a
x a
y forall a. Eq a => a -> a -> Bool
== Ordering
LT = (a
x,a
y)
 | Bool
otherwise = (a
y,a
x)

-- | A ternary predicate to check whether the third argument lies between the first two unequal ones or whether they are all equal.
betweenNX :: (Ord a) => a -> a -> a -> Bool
betweenNX :: forall a. Ord a => a -> a -> a -> Bool
betweenNX = forall a. Ord a => (a -> a -> Ordering) -> a -> a -> a -> Bool
betweenNXBy forall a. Ord a => a -> a -> Ordering
compare
{-# INLINE betweenNX #-}

-- | A variant of the 'betweenNX' where you can specify your own comparison function.
betweenNXBy :: (Ord a) => (a -> a -> Ordering) -> a -> a -> a -> Bool
betweenNXBy :: forall a. Ord a => (a -> a -> Ordering) -> a -> a -> a -> Bool
betweenNXBy a -> a -> Ordering
g a
x a
y a
z
 | a
x forall a. Eq a => a -> a -> Bool
== a
y = a
x forall a. Eq a => a -> a -> Bool
== a
z
 | a -> a -> Ordering
g a
z a
k forall a. Eq a => a -> a -> Bool
== Ordering
LT Bool -> Bool -> Bool
&& a -> a -> Ordering
g a
z a
t forall a. Eq a => a -> a -> Bool
== Ordering
GT = Bool
True
 | Bool
otherwise = Bool
False
      where (a
t,a
k) = forall a. Ord a => (a -> a -> Ordering) -> a -> a -> (a, a)
minmaxPBy a -> a -> Ordering
g a
x a
y

-- | Finds out the minimum and maximum values of the finite structure that has not less than two elements. Otherwise returns 'Nothing'.
minMax11 :: (Ord a, InsertLeft t a, Monoid (t a)) => t a -> Maybe (a, a)
minMax11 :: forall a (t :: * -> *).
(Ord a, InsertLeft t a, Monoid (t a)) =>
t a -> Maybe (a, a)
minMax11 = forall a (t :: * -> *).
(Ord a, InsertLeft t a, Monoid (t a)) =>
(a -> a -> Ordering) -> t a -> Maybe (a, a)
minMax11By forall a. Ord a => a -> a -> Ordering
compare
{-# INLINE minMax11 #-}

-- | A generalized variant of the 'minMax11' where you can specify your own comparison function.
minMax11By :: (Ord a, InsertLeft t a, Monoid (t a)) => (a -> a -> Ordering) -> t a -> Maybe (a, a)
minMax11By :: forall a (t :: * -> *).
(Ord a, InsertLeft t a, Monoid (t a)) =>
(a -> a -> Ordering) -> t a -> Maybe (a, a)
minMax11By a -> a -> Ordering
g t a
xs
 | forall (t :: * -> *) a. Foldable t => t a -> Int
F.length t a
xs forall a. Ord a => a -> a -> Bool
< Int
2 = forall a. Maybe a
Nothing
 | Bool
otherwise = forall a. a -> Maybe a
Just forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
F.foldr a -> (a, a) -> (a, a)
f (a
t,a
u) forall a b. (a -> b) -> a -> b
$ t a
str1
      where (t a
str1,t a
str2) = forall b (t :: * -> *) a.
(Integral b, InsertLeft t a, Monoid (t a)) =>
b -> t a -> (t a, t a)
splitAtEndG Integer
2 forall a b. (a -> b) -> a -> b
$ t a
xs
            [a
t,a
u] = forall a. (a -> a -> Ordering) -> [a] -> [a]
L.sortBy a -> a -> Ordering
g forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (t :: * -> *) a. Foldable t => t a -> [a]
F.toList forall a b. (a -> b) -> a -> b
$ t a
str2
            f :: a -> (a, a) -> (a, a)
f a
z (a
x,a
y)
              | a -> a -> Ordering
g a
z a
x forall a. Eq a => a -> a -> Bool
== Ordering
LT = (a
z,a
y)
              | a -> a -> Ordering
g a
z a
y forall a. Eq a => a -> a -> Bool
== Ordering
GT = (a
x,a
z)
              | Bool
otherwise = (a
x,a
y)

-- | Given a finite structure with at least 3 elements returns a tuple with the two most minimum elements
-- (the first one is less than the second one) and the maximum element. If the structure has less elements, returns 'Nothing'.
-- Uses just three passes through the structure, so may be more efficient than some other approaches.
minMax21 :: (Ord a, InsertLeft t a, Monoid (t a)) => t a -> Maybe ((a,a), a)
minMax21 :: forall a (t :: * -> *).
(Ord a, InsertLeft t a, Monoid (t a)) =>
t a -> Maybe ((a, a), a)
minMax21 = forall a (t :: * -> *).
(Ord a, InsertLeft t a, Monoid (t a)) =>
(a -> a -> Ordering) -> t a -> Maybe ((a, a), a)
minMax21By forall a. Ord a => a -> a -> Ordering
compare
{-# INLINE minMax21 #-}

-- | A variant of the 'minMax21' where you can specify your own comparison function.
minMax21By :: (Ord a, InsertLeft t a, Monoid (t a)) => (a -> a -> Ordering) -> t a -> Maybe ((a,a), a)
minMax21By :: forall a (t :: * -> *).
(Ord a, InsertLeft t a, Monoid (t a)) =>
(a -> a -> Ordering) -> t a -> Maybe ((a, a), a)
minMax21By a -> a -> Ordering
g t a
xs
 | forall (t :: * -> *) a. Foldable t => t a -> Int
F.length t a
xs forall a. Ord a => a -> a -> Bool
< Int
3 = forall a. Maybe a
Nothing
 | Bool
otherwise = forall a. a -> Maybe a
Just forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
F.foldr a -> ((a, a), a) -> ((a, a), a)
f ((a
n,a
p),a
q) forall a b. (a -> b) -> a -> b
$ t a
str1
      where (t a
str1,t a
str2) = forall b (t :: * -> *) a.
(Integral b, InsertLeft t a, Monoid (t a)) =>
b -> t a -> (t a, t a)
splitAtEndG Integer
3 t a
xs
            [a
n,a
p,a
q] = forall a. (a -> a -> Ordering) -> [a] -> [a]
L.sortBy a -> a -> Ordering
g forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (t :: * -> *) a. Foldable t => t a -> [a]
F.toList forall a b. (a -> b) -> a -> b
$ t a
str2
            f :: a -> ((a, a), a) -> ((a, a), a)
f a
z ((a
x,a
y),a
t)
              | a -> a -> Ordering
g a
z a
t forall a. Eq a => a -> a -> Bool
== Ordering
GT = ((a
x,a
y),a
z)
              | a -> a -> Ordering
g a
z a
y forall a. Eq a => a -> a -> Bool
== Ordering
LT = if a -> a -> Ordering
g a
z a
x forall a. Eq a => a -> a -> Bool
== Ordering
GT then ((a
x,a
z),a
t) else ((a
z,a
x),a
t)
              | Bool
otherwise = ((a
x,a
y),a
t)

-- | Given a finite structure with at least 3 elements returns a tuple with the minimum element
-- and two maximum elements (the first one is less than the second one). If the structure has less elements, returns 'Nothing'.
-- Uses just three passes through the structure, so may be more efficient than some other approaches.
minMax12 :: (Ord a, InsertLeft t a, Monoid (t a)) => t a -> Maybe (a, (a,a))
minMax12 :: forall a (t :: * -> *).
(Ord a, InsertLeft t a, Monoid (t a)) =>
t a -> Maybe (a, (a, a))
minMax12 = forall a (t :: * -> *).
(Ord a, InsertLeft t a, Monoid (t a)) =>
(a -> a -> Ordering) -> t a -> Maybe (a, (a, a))
minMax12By forall a. Ord a => a -> a -> Ordering
compare
{-# INLINE minMax12 #-}

-- | A variant of the 'minMax12' where you can specify your own comparison function.
minMax12By :: (Ord a, InsertLeft t a, Monoid (t a)) => (a -> a -> Ordering) -> t a -> Maybe (a, (a,a))
minMax12By :: forall a (t :: * -> *).
(Ord a, InsertLeft t a, Monoid (t a)) =>
(a -> a -> Ordering) -> t a -> Maybe (a, (a, a))
minMax12By a -> a -> Ordering
g t a
xs
 | forall (t :: * -> *) a. Foldable t => t a -> Int
F.length t a
xs forall a. Ord a => a -> a -> Bool
< Int
3 = forall a. Maybe a
Nothing
 | Bool
otherwise = forall a. a -> Maybe a
Just forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
F.foldr a -> (a, (a, a)) -> (a, (a, a))
f (a
n,(a
p,a
q)) forall a b. (a -> b) -> a -> b
$ t a
str1
      where (t a
str1,t a
str2) = forall b (t :: * -> *) a.
(Integral b, InsertLeft t a, Monoid (t a)) =>
b -> t a -> (t a, t a)
splitAtEndG Integer
3 t a
xs
            [a
n,a
p,a
q] = forall a. (a -> a -> Ordering) -> [a] -> [a]
L.sortBy a -> a -> Ordering
g forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (t :: * -> *) a. Foldable t => t a -> [a]
F.toList forall a b. (a -> b) -> a -> b
$ t a
str2
            f :: a -> (a, (a, a)) -> (a, (a, a))
f a
z (a
x,(a
y,a
t))
              | a -> a -> Ordering
g a
z a
x forall a. Eq a => a -> a -> Bool
== Ordering
LT = (a
z,(a
y,a
t))
              | a -> a -> Ordering
g a
z a
y forall a. Eq a => a -> a -> Bool
== Ordering
GT = if a -> a -> Ordering
g a
z a
t forall a. Eq a => a -> a -> Bool
== Ordering
LT then (a
x,(a
z,a
t)) else (a
x,(a
t,a
z))
              | Bool
otherwise = (a
x,(a
y,a
t))

-- | Given a finite structure with at least 4 elements returns a tuple with two minimum elements
-- and two maximum elements. If the structure has less elements, returns 'Nothing'.
-- Uses just three passes through the structure, so may be more efficient than some other approaches.
minMax22 :: (Ord a, InsertLeft t a, Monoid (t a)) => t a -> Maybe ((a,a), (a,a))
minMax22 :: forall a (t :: * -> *).
(Ord a, InsertLeft t a, Monoid (t a)) =>
t a -> Maybe ((a, a), (a, a))
minMax22 = forall a (t :: * -> *).
(Ord a, InsertLeft t a, Monoid (t a)) =>
(a -> a -> Ordering) -> t a -> Maybe ((a, a), (a, a))
minMax22By forall a. Ord a => a -> a -> Ordering
compare
{-# INLINE minMax22 #-}

-- | A variant of the 'minMax22' where you can specify your own comparison function.
minMax22By :: (Ord a, InsertLeft t a, Monoid (t a)) => (a -> a -> Ordering) -> t a -> Maybe ((a,a), (a,a))
minMax22By :: forall a (t :: * -> *).
(Ord a, InsertLeft t a, Monoid (t a)) =>
(a -> a -> Ordering) -> t a -> Maybe ((a, a), (a, a))
minMax22By a -> a -> Ordering
g t a
xs
 | forall (t :: * -> *) a. Foldable t => t a -> Int
F.length t a
xs forall a. Ord a => a -> a -> Bool
< Int
4 = forall a. Maybe a
Nothing
 | Bool
otherwise = forall a. a -> Maybe a
Just forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
F.foldr a -> ((a, a), (a, a)) -> ((a, a), (a, a))
f ((a
n,a
p),(a
q,a
r)) forall a b. (a -> b) -> a -> b
$ t a
str1
      where (t a
str1,t a
str2) = forall b (t :: * -> *) a.
(Integral b, InsertLeft t a, Monoid (t a)) =>
b -> t a -> (t a, t a)
splitAtEndG Integer
4 t a
xs
            [a
n,a
p,a
q,a
r] = forall a. (a -> a -> Ordering) -> [a] -> [a]
L.sortBy a -> a -> Ordering
g forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (t :: * -> *) a. Foldable t => t a -> [a]
F.toList forall a b. (a -> b) -> a -> b
$ t a
str2
            f :: a -> ((a, a), (a, a)) -> ((a, a), (a, a))
f a
z ((a
x,a
y),(a
t,a
w))
              | a -> a -> Ordering
g a
z a
y forall a. Eq a => a -> a -> Bool
== Ordering
LT = if a -> a -> Ordering
g a
z a
x forall a. Eq a => a -> a -> Bool
== Ordering
GT then ((a
x,a
z),(a
t,a
w)) else ((a
z,a
x),(a
t,a
w))
              | a -> a -> Ordering
g a
z a
t forall a. Eq a => a -> a -> Bool
== Ordering
GT = if a -> a -> Ordering
g a
z a
w forall a. Eq a => a -> a -> Bool
== Ordering
LT then ((a
x,a
y),(a
z,a
w)) else ((a
x,a
y),(a
w,a
z))
              | Bool
otherwise = ((a
x,a
y),(a
t,a
w))