{-# OPTIONS_HADDOCK show-extensions #-}

-- |
-- Module      :  Phonetic.Languages.Simplified.Lists.UniquenessPeriodsG.Base
-- Copyright   :  (c) OleksandrZhabenko 2020-2021
-- License     :  MIT
-- Stability   :  Experimental
-- Maintainer  :  olexandr543@yahoo.com
--
-- Generalization of the uniqueness-periods and uniqueness-periods-general
-- packages functionality. Uses less dependencies.
--

{-# LANGUAGE BangPatterns #-}

module Phonetic.Languages.Simplified.Lists.UniquenessPeriodsG.Base (
  -- * List functions
  uniquenessPeriodsGG
  , uniquenessPeriodsG
  , uniquenessPeriodsGI8
  , diverse2GGL
  , diverse2GL
  , diverse2GLInt8
)where

import GHC.Int
import Data.List
import Data.Maybe (mapMaybe)

diverse2GGL :: (Foldable t, Ord a) => a -> [a] -> t a -> Int16
diverse2GGL :: a -> [a] -> t a -> Int16
diverse2GGL a
delim [a]
whspss = [Int16] -> Int16
forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum ([Int16] -> Int16) -> (t a -> [Int16]) -> t a -> Int16
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> [a] -> t a -> [Int16]
forall (t :: * -> *) a.
(Foldable t, Ord a) =>
a -> [a] -> t a -> [Int16]
uniquenessPeriodsGG a
delim [a]
whspss
{-# INLINE diverse2GGL #-}

-- | A generalization of the uniquenessPeriods function of the @uniqueness-periods@ package.
uniquenessPeriodsGG :: (Foldable t, Ord a) => a -> [a] -> t a -> [Int16]
uniquenessPeriodsGG :: a -> [a] -> t a -> [Int16]
uniquenessPeriodsGG a
delim [a]
whspss t a
ws
 | t a -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null t a
ws = []
 | Bool
otherwise = (([Int16], a) -> Maybe Int16) -> [([Int16], a)] -> [Int16]
forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe (([Int16] -> Int16) -> [a] -> ([Int16], a) -> Maybe Int16
forall a b. Eq a => ([b] -> b) -> [a] -> ([b], a) -> Maybe b
helpG [Int16] -> Int16
forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum [a]
whspss) ([([Int16], a)] -> [Int16])
-> ([(Int16, a)] -> [([Int16], a)]) -> [(Int16, a)] -> [Int16]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ([(Int16, a)] -> Maybe (([Int16], a), [(Int16, a)]))
-> [(Int16, a)] -> [([Int16], a)]
forall b a. (b -> Maybe (a, b)) -> b -> [a]
unfoldr [(Int16, a)] -> Maybe (([Int16], a), [(Int16, a)])
forall b.
Eq b =>
[(Int16, b)] -> Maybe (([Int16], b), [(Int16, b)])
f ([(Int16, a)] -> [Int16]) -> [(Int16, a)] -> [Int16]
forall a b. (a -> b) -> a -> b
$ [(Int16, a)]
ks
     where !ks :: [(Int16, a)]
ks = a -> t a -> [(Int16, a)]
forall (t :: * -> *) b. Foldable t => b -> t b -> [(Int16, b)]
indexedL a
delim t a
ws
           !vs :: [Int16]
vs = ((Int16, a) -> Maybe Int16) -> [(Int16, a)] -> [Int16]
forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe (Int16, a) -> Maybe Int16
forall a. (a, a) -> Maybe a
g [(Int16, a)]
ks
           g :: (a, a) -> Maybe a
g (a, a)
x
            | (a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [a]
whspss) (a -> Bool) -> ((a, a) -> a) -> (a, a) -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a, a) -> a
forall a b. (a, b) -> b
snd ((a, a) -> Bool) -> (a, a) -> Bool
forall a b. (a -> b) -> a -> b
$ (a, a)
x = a -> Maybe a
forall a. a -> Maybe a
Just ((a, a) -> a
forall a b. (a, b) -> a
fst (a, a)
x)
            | Bool
otherwise = Maybe a
forall a. Maybe a
Nothing
           {-# INLINE g #-}
           f :: [(Int16, b)] -> Maybe (([Int16], b), [(Int16, b)])
f ![(Int16, b)]
x
             | [(Int16, b)] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [(Int16, b)]
x = Maybe (([Int16], b), [(Int16, b)])
forall a. Maybe a
Nothing
             | Bool
otherwise = let !idX0 :: b
idX0 = (Int16, b) -> b
forall a b. (a, b) -> b
snd ((Int16, b) -> b)
-> ([(Int16, b)] -> (Int16, b)) -> [(Int16, b)] -> b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [(Int16, b)] -> (Int16, b)
forall a. [a] -> a
head ([(Int16, b)] -> b) -> [(Int16, b)] -> b
forall a b. (a -> b) -> a -> b
$ [(Int16, b)]
x in (([Int16], b), [(Int16, b)]) -> Maybe (([Int16], b), [(Int16, b)])
forall a. a -> Maybe a
Just ((([Int16], b), [(Int16, b)])
 -> Maybe (([Int16], b), [(Int16, b)]))
-> ([(Int16, b)] -> (([Int16], b), [(Int16, b)]))
-> [(Int16, b)]
-> Maybe (([Int16], b), [(Int16, b)])
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (\[Int16]
vws ([(Int16, b)]
v2,[(Int16, b)]
v3) -> (([Int16] -> [Int16] -> [Int16] -> [Int16]
helpUPV3 [Int16]
vws [] ([Int16] -> [Int16])
-> ([(Int16, b)] -> [Int16]) -> [(Int16, b)] -> [Int16]
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
                    ((Int16, b) -> Int16) -> [(Int16, b)] -> [Int16]
forall a b. (a -> b) -> [a] -> [b]
map (Int16, b) -> Int16
forall a b. (a, b) -> a
fst ([(Int16, b)] -> [Int16]) -> [(Int16, b)] -> [Int16]
forall a b. (a -> b) -> a -> b
$ [(Int16, b)]
v2,(Int16, b) -> b
forall a b. (a, b) -> b
snd ((Int16, b) -> b)
-> ([(Int16, b)] -> (Int16, b)) -> [(Int16, b)] -> b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [(Int16, b)] -> (Int16, b)
forall a. [a] -> a
head ([(Int16, b)] -> b) -> [(Int16, b)] -> b
forall a b. (a -> b) -> a -> b
$ [(Int16, b)]
v2),[(Int16, b)]
v3)) [Int16]
vs (([(Int16, b)], [(Int16, b)]) -> (([Int16], b), [(Int16, b)]))
-> ([(Int16, b)] -> ([(Int16, b)], [(Int16, b)]))
-> [(Int16, b)]
-> (([Int16], b), [(Int16, b)])
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((Int16, b) -> Bool)
-> [(Int16, b)] -> ([(Int16, b)], [(Int16, b)])
forall a. (a -> Bool) -> [a] -> ([a], [a])
partition (\(Int16
_,b
xs) -> b
xs b -> b -> Bool
forall a. Eq a => a -> a -> Bool
== b
idX0) ([(Int16, b)] -> Maybe (([Int16], b), [(Int16, b)]))
-> [(Int16, b)] -> Maybe (([Int16], b), [(Int16, b)])
forall a b. (a -> b) -> a -> b
$ [(Int16, b)]
x
{-# INLINE uniquenessPeriodsGG #-}

-- | A variant of the 'diverse2GGL' function for 'Char'.
diverse2GL :: Foldable t => String -> t Char -> Int16
diverse2GL :: String -> t Char -> Int16
diverse2GL = Char -> String -> t Char -> Int16
forall (t :: * -> *) a.
(Foldable t, Ord a) =>
a -> [a] -> t a -> Int16
diverse2GGL Char
'\00'
{-# INLINE diverse2GL #-}

-- | A variant for the 'uniquenessPeriodsGG' function for 'Char'.
uniquenessPeriodsG :: Foldable t => String -> t Char -> [Int16]
uniquenessPeriodsG :: String -> t Char -> [Int16]
uniquenessPeriodsG = Char -> String -> t Char -> [Int16]
forall (t :: * -> *) a.
(Foldable t, Ord a) =>
a -> [a] -> t a -> [Int16]
uniquenessPeriodsGG Char
'\00'
{-# INLINE uniquenessPeriodsG #-}

-- | A variant of the 'diverse2GGL' function for 'Int8'.
diverse2GLInt8 :: Foldable t => [Int8] -> t Int8 -> Int16
diverse2GLInt8 :: [Int8] -> t Int8 -> Int16
diverse2GLInt8 = Int8 -> [Int8] -> t Int8 -> Int16
forall (t :: * -> *) a.
(Foldable t, Ord a) =>
a -> [a] -> t a -> Int16
diverse2GGL (-Int8
1::Int8)
{-# INLINE diverse2GLInt8 #-}

-- | A variant for the 'uniquenessPeriodsGG' function for 'Int8'.
uniquenessPeriodsGI8 :: Foldable t => [Int8] -> t Int8 -> [Int16]
uniquenessPeriodsGI8 :: [Int8] -> t Int8 -> [Int16]
uniquenessPeriodsGI8 = Int8 -> [Int8] -> t Int8 -> [Int16]
forall (t :: * -> *) a.
(Foldable t, Ord a) =>
a -> [a] -> t a -> [Int16]
uniquenessPeriodsGG (-Int8
1::Int8)
{-# INLINE uniquenessPeriodsGI8 #-}

-- | The first and the third list arguments of numbers (if not empty) must be sorted in the ascending order.
helpUPV3 :: [Int16] -> [Int16] -> [Int16] -> [Int16]
helpUPV3 :: [Int16] -> [Int16] -> [Int16] -> [Int16]
helpUPV3 (Int16
z:[Int16]
zs) ![Int16]
acc (Int16
x:Int16
y:[Int16]
xs)
 | Int16 -> Int16 -> Ordering
forall a. Ord a => a -> a -> Ordering
compare ((Int16
x Int16 -> Int16 -> Int16
forall a. Num a => a -> a -> a
- Int16
z) Int16 -> Int16 -> Int16
forall a. Num a => a -> a -> a
* (Int16
y Int16 -> Int16 -> Int16
forall a. Num a => a -> a -> a
- Int16
z)) Int16
0 Ordering -> Ordering -> Bool
forall a. Eq a => a -> a -> Bool
== Ordering
LT = [Int16] -> [Int16] -> [Int16] -> [Int16]
helpUPV3 [Int16]
zs ((Int16
y Int16 -> Int16 -> Int16
forall a. Num a => a -> a -> a
- Int16
x)Int16 -> [Int16] -> [Int16]
forall a. a -> [a] -> [a]
:[Int16]
acc) (Int16
yInt16 -> [Int16] -> [Int16]
forall a. a -> [a] -> [a]
:[Int16]
xs)
 | Int16 -> Int16 -> Ordering
forall a. Ord a => a -> a -> Ordering
compare Int16
y Int16
z Ordering -> Ordering -> Bool
forall a. Eq a => a -> a -> Bool
== Ordering
GT = [Int16] -> [Int16] -> [Int16] -> [Int16]
helpUPV3 [Int16]
zs [Int16]
acc (Int16
xInt16 -> [Int16] -> [Int16]
forall a. a -> [a] -> [a]
:Int16
yInt16 -> [Int16] -> [Int16]
forall a. a -> [a] -> [a]
:[Int16]
xs)
 | Bool
otherwise = [Int16] -> [Int16] -> [Int16] -> [Int16]
helpUPV3 (Int16
zInt16 -> [Int16] -> [Int16]
forall a. a -> [a] -> [a]
:[Int16]
zs) [Int16]
acc (Int16
yInt16 -> [Int16] -> [Int16]
forall a. a -> [a] -> [a]
:[Int16]
xs)
helpUPV3 [Int16]
_ ![Int16]
acc [Int16]
_ = [Int16]
acc

indexedL :: Foldable t => b -> t b -> [(Int16, b)]
indexedL :: b -> t b -> [(Int16, b)]
indexedL b
y = (b -> [(Int16, b)] -> [(Int16, b)])
-> [(Int16, b)] -> t b -> [(Int16, b)]
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr b -> [(Int16, b)] -> [(Int16, b)]
forall a b. Num a => b -> [(a, b)] -> [(a, b)]
f [(Int16, b)]
v
  where v :: [(Int16, b)]
v = [(Int16
1::Int16,b
y)]
        f :: b -> [(a, b)] -> [(a, b)]
f b
x ((a
j,b
z):[(a, b)]
ys) = (a
ja -> a -> a
forall a. Num a => a -> a -> a
-a
1,b
x)(a, b) -> [(a, b)] -> [(a, b)]
forall a. a -> [a] -> [a]
:(a
j,b
z)(a, b) -> [(a, b)] -> [(a, b)]
forall a. a -> [a] -> [a]
:[(a, b)]
ys

helpG :: (Eq a) => ([b] -> b) -> [a] -> ([b], a) -> Maybe b
helpG :: ([b] -> b) -> [a] -> ([b], a) -> Maybe b
helpG [b] -> b
h [a]
xs ([b]
ts, a
x)
  | [b] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [b]
ts = Maybe b
forall a. Maybe a
Nothing
  | a
x a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [a]
xs = Maybe b
forall a. Maybe a
Nothing
  | Bool
otherwise = b -> Maybe b
forall a. a -> Maybe a
Just ([b] -> b
h [b]
ts)
{-# INLINE helpG #-}