module Data.MinMax where
import Prelude hiding (takeWhile,dropWhile,span)
import Data.SubG
import qualified Data.Foldable as F
import qualified Data.List as L (sortBy)
minmaxP :: (Ord a) => a -> a -> (a,a)
minmaxP :: a -> a -> (a, a)
minmaxP = (a -> a -> Ordering) -> a -> a -> (a, a)
forall a. Ord a => (a -> a -> Ordering) -> a -> a -> (a, a)
minmaxPBy a -> a -> Ordering
forall a. Ord a => a -> a -> Ordering
compare
{-# INLINE minmaxP #-}
minmaxPBy :: (Ord a) => (a -> a -> Ordering) -> a -> a -> (a,a)
minmaxPBy :: (a -> a -> Ordering) -> a -> a -> (a, a)
minmaxPBy a -> a -> Ordering
g a
x a
y
| a -> a -> Ordering
g a
x a
y Ordering -> Ordering -> Bool
forall a. Eq a => a -> a -> Bool
== Ordering
LT = (a
x,a
y)
| Bool
otherwise = (a
y,a
x)
betweenNX :: (Ord a) => a -> a -> a -> Bool
betweenNX :: a -> a -> a -> Bool
betweenNX = (a -> a -> Ordering) -> a -> a -> a -> Bool
forall a. Ord a => (a -> a -> Ordering) -> a -> a -> a -> Bool
betweenNXBy a -> a -> Ordering
forall a. Ord a => a -> a -> Ordering
compare
{-# INLINE betweenNX #-}
betweenNXBy :: (Ord a) => (a -> a -> Ordering) -> a -> a -> a -> Bool
betweenNXBy :: (a -> a -> Ordering) -> a -> a -> a -> Bool
betweenNXBy a -> a -> Ordering
g a
x a
y a
z
| a
x a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
y = a
x a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
z
| a -> a -> Ordering
g a
z a
k Ordering -> Ordering -> Bool
forall a. Eq a => a -> a -> Bool
== Ordering
LT Bool -> Bool -> Bool
&& a -> a -> Ordering
g a
z a
t Ordering -> Ordering -> Bool
forall a. Eq a => a -> a -> Bool
== Ordering
GT = Bool
True
| Bool
otherwise = Bool
False
where (a
t,a
k) = (a -> a -> Ordering) -> a -> a -> (a, a)
forall a. Ord a => (a -> a -> Ordering) -> a -> a -> (a, a)
minmaxPBy a -> a -> Ordering
g a
x a
y
minMax11 :: (Ord a, InsertLeft t a, Monoid (t a)) => t a -> Maybe (a, a)
minMax11 :: t a -> Maybe (a, a)
minMax11 = (a -> a -> Ordering) -> t a -> Maybe (a, a)
forall a (t :: * -> *).
(Ord a, InsertLeft t a, Monoid (t a)) =>
(a -> a -> Ordering) -> t a -> Maybe (a, a)
minMax11By a -> a -> Ordering
forall a. Ord a => a -> a -> Ordering
compare
{-# INLINE minMax11 #-}
minMax11By :: (Ord a, InsertLeft t a, Monoid (t a)) => (a -> a -> Ordering) -> t a -> Maybe (a, a)
minMax11By :: (a -> a -> Ordering) -> t a -> Maybe (a, a)
minMax11By a -> a -> Ordering
g t a
xs
| t a -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
F.length t a
xs Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
2 = Maybe (a, a)
forall a. Maybe a
Nothing
| Bool
otherwise = (a, a) -> Maybe (a, a)
forall a. a -> Maybe a
Just ((a, a) -> Maybe (a, a)) -> (t a -> (a, a)) -> t a -> Maybe (a, a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> (a, a) -> (a, a)) -> (a, a) -> t a -> (a, a)
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) (t a -> Maybe (a, a)) -> t a -> Maybe (a, a)
forall a b. (a -> b) -> a -> b
$ t a
str1
where (t a
str1,t a
str2) = Integer -> t a -> (t a, t a)
forall b (t :: * -> *) a.
(Integral b, InsertLeft t a, Monoid (t a)) =>
b -> t a -> (t a, t a)
splitAtEndG Integer
2 (t a -> (t a, t a)) -> t a -> (t a, t a)
forall a b. (a -> b) -> a -> b
$ t a
xs
[a
t,a
u] = (a -> a -> Ordering) -> [a] -> [a]
forall a. (a -> a -> Ordering) -> [a] -> [a]
L.sortBy a -> a -> Ordering
g ([a] -> [a]) -> (t a -> [a]) -> t a -> [a]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. t a -> [a]
forall (t :: * -> *) a. Foldable t => t a -> [a]
F.toList (t a -> [a]) -> t a -> [a]
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 Ordering -> Ordering -> Bool
forall a. Eq a => a -> a -> Bool
== Ordering
LT = (a
z,a
y)
| a -> a -> Ordering
g a
z a
y Ordering -> Ordering -> Bool
forall a. Eq a => a -> a -> Bool
== Ordering
GT = (a
x,a
z)
| Bool
otherwise = (a
x,a
y)
minMax21 :: (Ord a, InsertLeft t a, Monoid (t a)) => t a -> Maybe ((a,a), a)
minMax21 :: t a -> Maybe ((a, a), a)
minMax21 = (a -> a -> Ordering) -> t a -> Maybe ((a, a), a)
forall a (t :: * -> *).
(Ord a, InsertLeft t a, Monoid (t a)) =>
(a -> a -> Ordering) -> t a -> Maybe ((a, a), a)
minMax21By a -> a -> Ordering
forall a. Ord a => a -> a -> Ordering
compare
{-# INLINE minMax21 #-}
minMax21By :: (Ord a, InsertLeft t a, Monoid (t a)) => (a -> a -> Ordering) -> t a -> Maybe ((a,a), a)
minMax21By :: (a -> a -> Ordering) -> t a -> Maybe ((a, a), a)
minMax21By a -> a -> Ordering
g t a
xs
| t a -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
F.length t a
xs Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
3 = Maybe ((a, a), a)
forall a. Maybe a
Nothing
| Bool
otherwise = ((a, a), a) -> Maybe ((a, a), a)
forall a. a -> Maybe a
Just (((a, a), a) -> Maybe ((a, a), a))
-> (t a -> ((a, a), a)) -> t a -> Maybe ((a, a), a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> ((a, a), a) -> ((a, a), a))
-> ((a, a), a) -> t a -> ((a, a), a)
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) (t a -> Maybe ((a, a), a)) -> t a -> Maybe ((a, a), a)
forall a b. (a -> b) -> a -> b
$ t a
str1
where (t a
str1,t a
str2) = Integer -> t a -> (t a, t a)
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] = (a -> a -> Ordering) -> [a] -> [a]
forall a. (a -> a -> Ordering) -> [a] -> [a]
L.sortBy a -> a -> Ordering
g ([a] -> [a]) -> (t a -> [a]) -> t a -> [a]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. t a -> [a]
forall (t :: * -> *) a. Foldable t => t a -> [a]
F.toList (t a -> [a]) -> t a -> [a]
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 Ordering -> Ordering -> Bool
forall a. Eq a => a -> a -> Bool
== Ordering
GT = ((a
x,a
y),a
z)
| a -> a -> Ordering
g a
z a
y Ordering -> Ordering -> Bool
forall a. Eq a => a -> a -> Bool
== Ordering
LT = if a -> a -> Ordering
g a
z a
x Ordering -> Ordering -> Bool
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)
minMax12 :: (Ord a, InsertLeft t a, Monoid (t a)) => t a -> Maybe (a, (a,a))
minMax12 :: t a -> Maybe (a, (a, a))
minMax12 = (a -> a -> Ordering) -> t a -> Maybe (a, (a, a))
forall a (t :: * -> *).
(Ord a, InsertLeft t a, Monoid (t a)) =>
(a -> a -> Ordering) -> t a -> Maybe (a, (a, a))
minMax12By a -> a -> Ordering
forall a. Ord a => a -> a -> Ordering
compare
{-# INLINE minMax12 #-}
minMax12By :: (Ord a, InsertLeft t a, Monoid (t a)) => (a -> a -> Ordering) -> t a -> Maybe (a, (a,a))
minMax12By :: (a -> a -> Ordering) -> t a -> Maybe (a, (a, a))
minMax12By a -> a -> Ordering
g t a
xs
| t a -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
F.length t a
xs Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
3 = Maybe (a, (a, a))
forall a. Maybe a
Nothing
| Bool
otherwise = (a, (a, a)) -> Maybe (a, (a, a))
forall a. a -> Maybe a
Just ((a, (a, a)) -> Maybe (a, (a, a)))
-> (t a -> (a, (a, a))) -> t a -> Maybe (a, (a, a))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> (a, (a, a)) -> (a, (a, a)))
-> (a, (a, a)) -> t a -> (a, (a, a))
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)) (t a -> Maybe (a, (a, a))) -> t a -> Maybe (a, (a, a))
forall a b. (a -> b) -> a -> b
$ t a
str1
where (t a
str1,t a
str2) = Integer -> t a -> (t a, t a)
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] = (a -> a -> Ordering) -> [a] -> [a]
forall a. (a -> a -> Ordering) -> [a] -> [a]
L.sortBy a -> a -> Ordering
g ([a] -> [a]) -> (t a -> [a]) -> t a -> [a]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. t a -> [a]
forall (t :: * -> *) a. Foldable t => t a -> [a]
F.toList (t a -> [a]) -> t a -> [a]
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 Ordering -> Ordering -> Bool
forall a. Eq a => a -> a -> Bool
== Ordering
LT = (a
z,(a
y,a
t))
| a -> a -> Ordering
g a
z a
y Ordering -> Ordering -> Bool
forall a. Eq a => a -> a -> Bool
== Ordering
GT = if a -> a -> Ordering
g a
z a
t Ordering -> Ordering -> Bool
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))
minMax22 :: (Ord a, InsertLeft t a, Monoid (t a)) => t a -> Maybe ((a,a), (a,a))
minMax22 :: t a -> Maybe ((a, a), (a, a))
minMax22 = (a -> a -> Ordering) -> t a -> Maybe ((a, a), (a, a))
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
forall a. Ord a => a -> a -> Ordering
compare
{-# INLINE minMax22 #-}
minMax22By :: (Ord a, InsertLeft t a, Monoid (t a)) => (a -> a -> Ordering) -> t a -> Maybe ((a,a), (a,a))
minMax22By :: (a -> a -> Ordering) -> t a -> Maybe ((a, a), (a, a))
minMax22By a -> a -> Ordering
g t a
xs
| t a -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
F.length t a
xs Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
4 = Maybe ((a, a), (a, a))
forall a. Maybe a
Nothing
| Bool
otherwise = ((a, a), (a, a)) -> Maybe ((a, a), (a, a))
forall a. a -> Maybe a
Just (((a, a), (a, a)) -> Maybe ((a, a), (a, a)))
-> (t a -> ((a, a), (a, a))) -> t a -> Maybe ((a, a), (a, a))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> ((a, a), (a, a)) -> ((a, a), (a, a)))
-> ((a, a), (a, a)) -> t a -> ((a, a), (a, a))
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)) (t a -> Maybe ((a, a), (a, a))) -> t a -> Maybe ((a, a), (a, a))
forall a b. (a -> b) -> a -> b
$ t a
str1
where (t a
str1,t a
str2) = Integer -> t a -> (t a, t a)
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] = (a -> a -> Ordering) -> [a] -> [a]
forall a. (a -> a -> Ordering) -> [a] -> [a]
L.sortBy a -> a -> Ordering
g ([a] -> [a]) -> (t a -> [a]) -> t a -> [a]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. t a -> [a]
forall (t :: * -> *) a. Foldable t => t a -> [a]
F.toList (t a -> [a]) -> t a -> [a]
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 Ordering -> Ordering -> Bool
forall a. Eq a => a -> a -> Bool
== Ordering
LT = if a -> a -> Ordering
g a
z a
x Ordering -> Ordering -> Bool
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 Ordering -> Ordering -> Bool
forall a. Eq a => a -> a -> Bool
== Ordering
GT = if a -> a -> Ordering
g a
z a
w Ordering -> Ordering -> Bool
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))