module Data.CircularSeq( CSeq
, cseq
, singleton
, fromNonEmpty
, fromList
, focus
, index, adjust
, item
, rotateL
, rotateR
, rotateNL, rotateNR
, rightElements
, leftElements
, asSeq
, withIndices
, reverseDirection
, allRotations
, findRotateTo
, rotateTo
, zipLWith, zipL
, zip3LWith
, insertOrd, insertOrdBy
, isShiftOf
) where
import Algorithms.StringSearch.KMP (isSubStringOf)
import Control.DeepSeq
import Control.Lens (Lens', bimap, lens)
import Data.Ext
import qualified Data.Foldable as F
import qualified Data.List as L
import qualified Data.List.NonEmpty as NonEmpty
import Data.Maybe (isJust)
import Data.Semigroup.Foldable
import Data.Sequence (Seq, ViewL (..), ViewR (..), (<|),
(|>))
import qualified Data.Sequence as S
import qualified Data.Traversable as T
import Data.Tuple (swap)
import GHC.Generics (Generic)
import Test.QuickCheck (Arbitrary (..))
import Test.QuickCheck.Instances ()
data CSeq a = CSeq !(Seq a) !a !(Seq a)
deriving ((forall x. CSeq a -> Rep (CSeq a) x)
-> (forall x. Rep (CSeq a) x -> CSeq a) -> Generic (CSeq a)
forall x. Rep (CSeq a) x -> CSeq a
forall x. CSeq a -> Rep (CSeq a) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall a x. Rep (CSeq a) x -> CSeq a
forall a x. CSeq a -> Rep (CSeq a) x
$cto :: forall a x. Rep (CSeq a) x -> CSeq a
$cfrom :: forall a x. CSeq a -> Rep (CSeq a) x
Generic)
instance NFData a => NFData (CSeq a)
instance Eq a => Eq (CSeq a) where
CSeq a
a == :: CSeq a -> CSeq a -> Bool
== CSeq a
b = CSeq a -> Seq a
forall a. CSeq a -> Seq a
asSeq CSeq a
a Seq a -> Seq a -> Bool
forall a. Eq a => a -> a -> Bool
== CSeq a -> Seq a
forall a. CSeq a -> Seq a
asSeq CSeq a
b
instance Show a => Show (CSeq a) where
showsPrec :: Int -> CSeq a -> ShowS
showsPrec Int
d CSeq a
s = Bool -> ShowS -> ShowS
showParen (Int
d Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
app_prec) (ShowS -> ShowS) -> ShowS -> ShowS
forall a b. (a -> b) -> a -> b
$
String -> ShowS
showString ((String
"CSeq " String -> ShowS
forall a. Semigroup a => a -> a -> a
<>) ShowS -> (CSeq a -> String) -> CSeq a -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [a] -> String
forall a. Show a => a -> String
show ([a] -> String) -> (CSeq a -> [a]) -> CSeq a -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Seq a -> [a]
forall (t :: * -> *) a. Foldable t => t a -> [a]
F.toList (Seq a -> [a]) -> (CSeq a -> Seq a) -> CSeq a -> [a]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. CSeq a -> Seq a
forall a. CSeq a -> Seq a
rightElements (CSeq a -> String) -> CSeq a -> String
forall a b. (a -> b) -> a -> b
$ CSeq a
s)
where app_prec :: Int
app_prec = Int
10
instance Read a => Read (CSeq a) where
readsPrec :: Int -> ReadS (CSeq a)
readsPrec Int
d = Bool -> ReadS (CSeq a) -> ReadS (CSeq a)
forall a. Bool -> ReadS a -> ReadS a
readParen (Int
d Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
app_prec) (ReadS (CSeq a) -> ReadS (CSeq a))
-> ReadS (CSeq a) -> ReadS (CSeq a)
forall a b. (a -> b) -> a -> b
$ \String
r ->
[ ([a] -> CSeq a
forall a. [a] -> CSeq a
fromList [a]
lst, String
t) | (String
"CSeq", String
s) <- ReadS String
lex String
r, ([a]
lst, String
t) <- ReadS [a]
forall a. Read a => ReadS a
reads String
s ]
where app_prec :: Int
app_prec = Int
10
instance T.Traversable CSeq where
traverse :: (a -> f b) -> CSeq a -> f (CSeq b)
traverse a -> f b
f (CSeq Seq a
l a
x Seq a
r) = (\b
x' Seq b
r' Seq b
l' -> Seq b -> b -> Seq b -> CSeq b
forall a. Seq a -> a -> Seq a -> CSeq a
CSeq Seq b
l' b
x' Seq b
r')
(b -> Seq b -> Seq b -> CSeq b)
-> f b -> f (Seq b -> Seq b -> CSeq b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a -> f b
f a
x f (Seq b -> Seq b -> CSeq b) -> f (Seq b) -> f (Seq b -> CSeq b)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> (a -> f b) -> Seq a -> f (Seq b)
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse a -> f b
f Seq a
r f (Seq b -> CSeq b) -> f (Seq b) -> f (CSeq b)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> (a -> f b) -> Seq a -> f (Seq b)
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse a -> f b
f Seq a
l
instance Foldable1 CSeq
instance F.Foldable CSeq where
foldMap :: (a -> m) -> CSeq a -> m
foldMap = (a -> m) -> CSeq a -> m
forall (t :: * -> *) m a.
(Traversable t, Monoid m) =>
(a -> m) -> t a -> m
T.foldMapDefault
length :: CSeq a -> Int
length (CSeq Seq a
l a
_ Seq a
r) = Int
1 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Seq a -> Int
forall a. Seq a -> Int
S.length Seq a
l Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Seq a -> Int
forall a. Seq a -> Int
S.length Seq a
r
instance Functor CSeq where
fmap :: (a -> b) -> CSeq a -> CSeq b
fmap = (a -> b) -> CSeq a -> CSeq b
forall (t :: * -> *) a b. Traversable t => (a -> b) -> t a -> t b
T.fmapDefault
instance Arbitrary a => Arbitrary (CSeq a) where
arbitrary :: Gen (CSeq a)
arbitrary = Seq a -> a -> Seq a -> CSeq a
forall a. Seq a -> a -> Seq a -> CSeq a
CSeq (Seq a -> a -> Seq a -> CSeq a)
-> Gen (Seq a) -> Gen (a -> Seq a -> CSeq a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Gen (Seq a)
forall a. Arbitrary a => Gen a
arbitrary Gen (a -> Seq a -> CSeq a) -> Gen a -> Gen (Seq a -> CSeq a)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Gen a
forall a. Arbitrary a => Gen a
arbitrary Gen (Seq a -> CSeq a) -> Gen (Seq a) -> Gen (CSeq a)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Gen (Seq a)
forall a. Arbitrary a => Gen a
arbitrary
singleton :: a -> CSeq a
singleton :: a -> CSeq a
singleton a
x = Seq a -> a -> Seq a -> CSeq a
forall a. Seq a -> a -> Seq a -> CSeq a
CSeq Seq a
forall a. Seq a
S.empty a
x Seq a
forall a. Seq a
S.empty
focus :: CSeq a -> a
focus :: CSeq a -> a
focus (CSeq Seq a
_ a
x Seq a
_) = a
x
index :: CSeq a -> Int -> a
index :: CSeq a -> Int -> a
index s :: CSeq a
s@(CSeq Seq a
l a
x Seq a
r) Int
i' = let i :: Int
i = Int
i' Int -> Int -> Int
forall a. Integral a => a -> a -> a
`mod` CSeq a -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length CSeq a
s
rn :: Int
rn = Seq a -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length Seq a
r
in if Int
i Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0 then a
x
else if Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1 Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
rn then Seq a -> Int -> a
forall a. Seq a -> Int -> a
S.index Seq a
r (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1)
else Seq a -> Int -> a
forall a. Seq a -> Int -> a
S.index Seq a
l (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
rn Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1)
withIndices :: CSeq a -> CSeq (Int :+ a)
withIndices :: CSeq a -> CSeq (Int :+ a)
withIndices (CSeq Seq a
l a
x Seq a
r) = let s :: Int
s = Int
1 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Seq a -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length Seq a
r in
Seq (Int :+ a) -> (Int :+ a) -> Seq (Int :+ a) -> CSeq (Int :+ a)
forall a. Seq a -> a -> Seq a -> CSeq a
CSeq ((Int -> a -> Int :+ a) -> Seq a -> Seq (Int :+ a)
forall a b. (Int -> a -> b) -> Seq a -> Seq b
S.mapWithIndex (\Int
i a
y -> Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
s Int -> a -> Int :+ a
forall core extra. core -> extra -> core :+ extra
:+ a
y) Seq a
l) (Int
0 Int -> a -> Int :+ a
forall core extra. core -> extra -> core :+ extra
:+ a
x) ((Int -> a -> Int :+ a) -> Seq a -> Seq (Int :+ a)
forall a b. (Int -> a -> b) -> Seq a -> Seq b
S.mapWithIndex (\Int
i a
y -> Int
iInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
1 Int -> a -> Int :+ a
forall core extra. core -> extra -> core :+ extra
:+ a
y) Seq a
r)
adjust :: (a -> a) -> Int -> CSeq a -> CSeq a
adjust :: (a -> a) -> Int -> CSeq a -> CSeq a
adjust a -> a
f Int
i' s :: CSeq a
s@(CSeq Seq a
l a
x Seq a
r) = let i :: Int
i = Int
i' Int -> Int -> Int
forall a. Integral a => a -> a -> a
`mod` CSeq a -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length CSeq a
s
rn :: Int
rn = Seq a -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length Seq a
r
in if Int
i Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0 then Seq a -> a -> Seq a -> CSeq a
forall a. Seq a -> a -> Seq a -> CSeq a
CSeq Seq a
l (a -> a
f a
x) Seq a
r
else if Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1 Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
rn
then Seq a -> a -> Seq a -> CSeq a
forall a. Seq a -> a -> Seq a -> CSeq a
CSeq Seq a
l a
x ((a -> a) -> Int -> Seq a -> Seq a
forall a. (a -> a) -> Int -> Seq a -> Seq a
S.adjust a -> a
f (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) Seq a
r)
else Seq a -> a -> Seq a -> CSeq a
forall a. Seq a -> a -> Seq a -> CSeq a
CSeq ((a -> a) -> Int -> Seq a -> Seq a
forall a. (a -> a) -> Int -> Seq a -> Seq a
S.adjust a -> a
f (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
rn Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) Seq a
l) a
x Seq a
r
item :: Int -> Lens' (CSeq a) a
item :: Int -> Lens' (CSeq a) a
item Int
i = (CSeq a -> a) -> (CSeq a -> a -> CSeq a) -> Lens' (CSeq a) a
forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
lens (CSeq a -> Int -> a
forall a. CSeq a -> Int -> a
`index` Int
i) (\CSeq a
s a
x -> (a -> a) -> Int -> CSeq a -> CSeq a
forall a. (a -> a) -> Int -> CSeq a -> CSeq a
adjust (a -> a -> a
forall a b. a -> b -> a
const a
x) Int
i CSeq a
s)
resplit :: Seq a -> (Seq a, Seq a)
resplit :: Seq a -> (Seq a, Seq a)
resplit Seq a
s = (Seq a, Seq a) -> (Seq a, Seq a)
forall a b. (a, b) -> (b, a)
swap ((Seq a, Seq a) -> (Seq a, Seq a))
-> (Seq a, Seq a) -> (Seq a, Seq a)
forall a b. (a -> b) -> a -> b
$ Int -> Seq a -> (Seq a, Seq a)
forall a. Int -> Seq a -> (Seq a, Seq a)
S.splitAt (Seq a -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length Seq a
s Int -> Int -> Int
forall a. Integral a => a -> a -> a
`div` Int
2) Seq a
s
cseq :: Seq a -> a -> Seq a -> CSeq a
cseq :: Seq a -> a -> Seq a -> CSeq a
cseq Seq a
l a
x Seq a
r
| Int
ln Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
1 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
2Int -> Int -> Int
forall a. Num a => a -> a -> a
*Int
rn = a -> Seq a -> CSeq a
forall a. a -> Seq a -> CSeq a
withFocus a
x (Seq a
r Seq a -> Seq a -> Seq a
forall a. Semigroup a => a -> a -> a
<> Seq a
l)
| Int
ln Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
rn Int -> Int -> Int
forall a. Integral a => a -> a -> a
`div` Int
2 = a -> Seq a -> CSeq a
forall a. a -> Seq a -> CSeq a
withFocus a
x (Seq a
r Seq a -> Seq a -> Seq a
forall a. Semigroup a => a -> a -> a
<> Seq a
l)
| Bool
otherwise = Seq a -> a -> Seq a -> CSeq a
forall a. Seq a -> a -> Seq a -> CSeq a
CSeq Seq a
l a
x Seq a
r
where
rn :: Int
rn = Seq a -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length Seq a
r
ln :: Int
ln = Seq a -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length Seq a
l
cseq' :: Seq a -> Seq a -> CSeq a
cseq' :: Seq a -> Seq a -> CSeq a
cseq' Seq a
l Seq a
r = case Seq a -> ViewL a
forall a. Seq a -> ViewL a
S.viewl Seq a
r of
(a
x :< Seq a
r') -> Seq a -> a -> Seq a -> CSeq a
forall a. Seq a -> a -> Seq a -> CSeq a
cseq Seq a
l a
x Seq a
r'
ViewL a
EmptyL -> let (a
x :< Seq a
l') = Seq a -> ViewL a
forall a. Seq a -> ViewL a
S.viewl Seq a
l in Seq a -> a -> Seq a -> CSeq a
forall a. Seq a -> a -> Seq a -> CSeq a
cseq Seq a
l' a
x Seq a
r
withFocus :: a -> Seq a -> CSeq a
withFocus :: a -> Seq a -> CSeq a
withFocus a
x Seq a
s = let (Seq a
l,Seq a
r) = Seq a -> (Seq a, Seq a)
forall a. Seq a -> (Seq a, Seq a)
resplit Seq a
s in Seq a -> a -> Seq a -> CSeq a
forall a. Seq a -> a -> Seq a -> CSeq a
CSeq Seq a
l a
x Seq a
r
rotateR :: CSeq a -> CSeq a
rotateR :: CSeq a -> CSeq a
rotateR s :: CSeq a
s@(CSeq Seq a
l a
x Seq a
r) = case Seq a -> ViewL a
forall a. Seq a -> ViewL a
S.viewl Seq a
r of
ViewL a
EmptyL -> case Seq a -> ViewL a
forall a. Seq a -> ViewL a
S.viewl Seq a
l of
ViewL a
EmptyL -> CSeq a
s
(a
y :< Seq a
l') -> Seq a -> a -> Seq a -> CSeq a
forall a. Seq a -> a -> Seq a -> CSeq a
cseq (a -> Seq a
forall a. a -> Seq a
S.singleton a
x) a
y Seq a
l'
(a
y :< Seq a
r') -> Seq a -> a -> Seq a -> CSeq a
forall a. Seq a -> a -> Seq a -> CSeq a
cseq (Seq a
l Seq a -> a -> Seq a
forall a. Seq a -> a -> Seq a
|> a
x) a
y Seq a
r'
rotateL :: CSeq a -> CSeq a
rotateL :: CSeq a -> CSeq a
rotateL s :: CSeq a
s@(CSeq Seq a
l a
x Seq a
r) = case Seq a -> ViewR a
forall a. Seq a -> ViewR a
S.viewr Seq a
l of
ViewR a
EmptyR -> case Seq a -> ViewR a
forall a. Seq a -> ViewR a
S.viewr Seq a
r of
ViewR a
EmptyR -> CSeq a
s
(Seq a
r' :> a
y) -> Seq a -> a -> Seq a -> CSeq a
forall a. Seq a -> a -> Seq a -> CSeq a
cseq Seq a
r' a
y (a -> Seq a
forall a. a -> Seq a
S.singleton a
x)
(Seq a
l' :> a
y) -> Seq a -> a -> Seq a -> CSeq a
forall a. Seq a -> a -> Seq a -> CSeq a
cseq Seq a
l' a
y (a
x a -> Seq a -> Seq a
forall a. a -> Seq a -> Seq a
<| Seq a
r)
asSeq :: CSeq a -> Seq a
asSeq :: CSeq a -> Seq a
asSeq = CSeq a -> Seq a
forall a. CSeq a -> Seq a
rightElements
rightElements :: CSeq a -> Seq a
rightElements :: CSeq a -> Seq a
rightElements (CSeq Seq a
l a
x Seq a
r) = a
x a -> Seq a -> Seq a
forall a. a -> Seq a -> Seq a
<| Seq a
r Seq a -> Seq a -> Seq a
forall a. Semigroup a => a -> a -> a
<> Seq a
l
leftElements :: CSeq a -> Seq a
leftElements :: CSeq a -> Seq a
leftElements (CSeq Seq a
l a
x Seq a
r) = a
x a -> Seq a -> Seq a
forall a. a -> Seq a -> Seq a
<| Seq a -> Seq a
forall a. Seq a -> Seq a
S.reverse Seq a
l Seq a -> Seq a -> Seq a
forall a. Semigroup a => a -> a -> a
<> Seq a -> Seq a
forall a. Seq a -> Seq a
S.reverse Seq a
r
fromNonEmpty :: NonEmpty.NonEmpty a -> CSeq a
fromNonEmpty :: NonEmpty a -> CSeq a
fromNonEmpty (a
x NonEmpty.:| [a]
xs) = a -> Seq a -> CSeq a
forall a. a -> Seq a -> CSeq a
withFocus a
x (Seq a -> CSeq a) -> Seq a -> CSeq a
forall a b. (a -> b) -> a -> b
$ [a] -> Seq a
forall a. [a] -> Seq a
S.fromList [a]
xs
fromList :: [a] -> CSeq a
fromList :: [a] -> CSeq a
fromList (a
x:[a]
xs) = a -> Seq a -> CSeq a
forall a. a -> Seq a -> CSeq a
withFocus a
x (Seq a -> CSeq a) -> Seq a -> CSeq a
forall a b. (a -> b) -> a -> b
$ [a] -> Seq a
forall a. [a] -> Seq a
S.fromList [a]
xs
fromList [] = String -> CSeq a
forall a. HasCallStack => String -> a
error String
"fromList: Empty list"
rotateNR :: Int -> CSeq a -> CSeq a
rotateNR :: Int -> CSeq a -> CSeq a
rotateNR Int
i = (Seq a -> Seq a -> CSeq a) -> (Seq a, Seq a) -> CSeq a
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry Seq a -> Seq a -> CSeq a
forall a. Seq a -> Seq a -> CSeq a
cseq' ((Seq a, Seq a) -> CSeq a)
-> (CSeq a -> (Seq a, Seq a)) -> CSeq a -> CSeq a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Seq a -> (Seq a, Seq a)
forall a. Int -> Seq a -> (Seq a, Seq a)
S.splitAt Int
i (Seq a -> (Seq a, Seq a))
-> (CSeq a -> Seq a) -> CSeq a -> (Seq a, Seq a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. CSeq a -> Seq a
forall a. CSeq a -> Seq a
rightElements
rotateNL :: Int -> CSeq a -> CSeq a
rotateNL :: Int -> CSeq a -> CSeq a
rotateNL Int
i CSeq a
s = let (a
x :< Seq a
xs) = Seq a -> ViewL a
forall a. Seq a -> ViewL a
S.viewl (Seq a -> ViewL a) -> Seq a -> ViewL a
forall a b. (a -> b) -> a -> b
$ CSeq a -> Seq a
forall a. CSeq a -> Seq a
rightElements CSeq a
s
(Seq a
l',Seq a
r) = Int -> Seq a -> (Seq a, Seq a)
forall a. Int -> Seq a -> (Seq a, Seq a)
S.splitAt (CSeq a -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length CSeq a
s Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
i) (Seq a -> (Seq a, Seq a)) -> Seq a -> (Seq a, Seq a)
forall a b. (a -> b) -> a -> b
$ Seq a
xs Seq a -> a -> Seq a
forall a. Seq a -> a -> Seq a
|> a
x
in case Seq a -> ViewR a
forall a. Seq a -> ViewR a
S.viewr Seq a
l' of
Seq a
l :> a
y -> Seq a -> a -> Seq a -> CSeq a
forall a. Seq a -> a -> Seq a -> CSeq a
cseq Seq a
l a
y Seq a
r
ViewR a
S.EmptyR -> let (a
y :< Seq a
r') = Seq a -> ViewL a
forall a. Seq a -> ViewL a
S.viewl Seq a
r in Seq a -> a -> Seq a -> CSeq a
forall a. Seq a -> a -> Seq a -> CSeq a
cseq Seq a
l' a
y Seq a
r'
reverseDirection :: CSeq a -> CSeq a
reverseDirection :: CSeq a -> CSeq a
reverseDirection (CSeq Seq a
l a
x Seq a
r) = Seq a -> a -> Seq a -> CSeq a
forall a. Seq a -> a -> Seq a -> CSeq a
CSeq (Seq a -> Seq a
forall a. Seq a -> Seq a
S.reverse Seq a
r) a
x (Seq a -> Seq a
forall a. Seq a -> Seq a
S.reverse Seq a
l)
findRotateTo :: (a -> Bool) -> CSeq a -> Maybe (CSeq a)
findRotateTo :: (a -> Bool) -> CSeq a -> Maybe (CSeq a)
findRotateTo a -> Bool
p = (CSeq a -> Bool) -> [CSeq a] -> Maybe (CSeq a)
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Maybe a
L.find (a -> Bool
p (a -> Bool) -> (CSeq a -> a) -> CSeq a -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. CSeq a -> a
forall a. CSeq a -> a
focus) ([CSeq a] -> Maybe (CSeq a))
-> (CSeq a -> [CSeq a]) -> CSeq a -> Maybe (CSeq a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. CSeq a -> [CSeq a]
forall a. CSeq a -> [CSeq a]
allRotations'
rotateTo :: Eq a => a -> CSeq a -> Maybe (CSeq a)
rotateTo :: a -> CSeq a -> Maybe (CSeq a)
rotateTo a
x = (a -> Bool) -> CSeq a -> Maybe (CSeq a)
forall a. (a -> Bool) -> CSeq a -> Maybe (CSeq a)
findRotateTo (a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
x)
allRotations :: CSeq a -> CSeq (CSeq a)
allRotations :: CSeq a -> CSeq (CSeq a)
allRotations = [CSeq a] -> CSeq (CSeq a)
forall a. [a] -> CSeq a
fromList ([CSeq a] -> CSeq (CSeq a))
-> (CSeq a -> [CSeq a]) -> CSeq a -> CSeq (CSeq a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. CSeq a -> [CSeq a]
forall a. CSeq a -> [CSeq a]
allRotations'
allRotations' :: CSeq a -> [CSeq a]
allRotations' :: CSeq a -> [CSeq a]
allRotations' CSeq a
s = Int -> [CSeq a] -> [CSeq a]
forall a. Int -> [a] -> [a]
take (CSeq a -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length CSeq a
s) ([CSeq a] -> [CSeq a])
-> (CSeq a -> [CSeq a]) -> CSeq a -> [CSeq a]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (CSeq a -> CSeq a) -> CSeq a -> [CSeq a]
forall a. (a -> a) -> a -> [a]
iterate CSeq a -> CSeq a
forall a. CSeq a -> CSeq a
rotateR (CSeq a -> [CSeq a]) -> CSeq a -> [CSeq a]
forall a b. (a -> b) -> a -> b
$ CSeq a
s
zipLWith :: (a -> b -> c) -> CSeq a -> CSeq b -> CSeq c
zipLWith :: (a -> b -> c) -> CSeq a -> CSeq b -> CSeq c
zipLWith a -> b -> c
f CSeq a
as CSeq b
bs = [c] -> CSeq c
forall a. [a] -> CSeq a
fromList ([c] -> CSeq c) -> [c] -> CSeq c
forall a b. (a -> b) -> a -> b
$ (a -> b -> c) -> [a] -> [b] -> [c]
forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
zipWith a -> b -> c
f (CSeq a -> [a]
forall (t :: * -> *) a. Foldable t => t a -> [a]
F.toList CSeq a
as) (CSeq b -> [b]
forall (t :: * -> *) a. Foldable t => t a -> [a]
F.toList CSeq b
bs)
zipL :: CSeq a -> CSeq b -> CSeq (a, b)
zipL :: CSeq a -> CSeq b -> CSeq (a, b)
zipL = (a -> b -> (a, b)) -> CSeq a -> CSeq b -> CSeq (a, b)
forall a b c. (a -> b -> c) -> CSeq a -> CSeq b -> CSeq c
zipLWith (,)
zip3LWith :: (a -> b -> c -> d) -> CSeq a -> CSeq b -> CSeq c -> CSeq d
zip3LWith :: (a -> b -> c -> d) -> CSeq a -> CSeq b -> CSeq c -> CSeq d
zip3LWith a -> b -> c -> d
f CSeq a
as CSeq b
bs CSeq c
cs = [d] -> CSeq d
forall a. [a] -> CSeq a
fromList ([d] -> CSeq d) -> [d] -> CSeq d
forall a b. (a -> b) -> a -> b
$ (a -> b -> c -> d) -> [a] -> [b] -> [c] -> [d]
forall a b c d. (a -> b -> c -> d) -> [a] -> [b] -> [c] -> [d]
zipWith3 a -> b -> c -> d
f (CSeq a -> [a]
forall (t :: * -> *) a. Foldable t => t a -> [a]
F.toList CSeq a
as) (CSeq b -> [b]
forall (t :: * -> *) a. Foldable t => t a -> [a]
F.toList CSeq b
bs) (CSeq c -> [c]
forall (t :: * -> *) a. Foldable t => t a -> [a]
F.toList CSeq c
cs)
insertOrd :: Ord a => a -> CSeq a -> CSeq a
insertOrd :: a -> CSeq a -> CSeq a
insertOrd = (a -> a -> Ordering) -> a -> CSeq a -> CSeq a
forall a. (a -> a -> Ordering) -> a -> CSeq a -> CSeq a
insertOrdBy a -> a -> Ordering
forall a. Ord a => a -> a -> Ordering
compare
insertOrdBy :: (a -> a -> Ordering) -> a -> CSeq a -> CSeq a
insertOrdBy :: (a -> a -> Ordering) -> a -> CSeq a -> CSeq a
insertOrdBy a -> a -> Ordering
cmp a
x = [a] -> CSeq a
forall a. [a] -> CSeq a
fromList ([a] -> CSeq a) -> (CSeq a -> [a]) -> CSeq a -> CSeq a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> a -> Ordering) -> a -> [a] -> [a]
forall a. (a -> a -> Ordering) -> a -> [a] -> [a]
insertOrdBy' a -> a -> Ordering
cmp a
x ([a] -> [a]) -> (CSeq a -> [a]) -> CSeq a -> [a]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Seq a -> [a]
forall (t :: * -> *) a. Foldable t => t a -> [a]
F.toList (Seq a -> [a]) -> (CSeq a -> Seq a) -> CSeq a -> [a]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. CSeq a -> Seq a
forall a. CSeq a -> Seq a
rightElements
insertOrdBy' :: (a -> a -> Ordering) -> a -> [a] -> [a]
insertOrdBy' :: (a -> a -> Ordering) -> a -> [a] -> [a]
insertOrdBy' a -> a -> Ordering
cmp a
x [a]
xs = case ([a]
rest, a
x a -> a -> Ordering
`cmp` [a] -> a
forall a. [a] -> a
head [a]
rest) of
([], Ordering
_) -> (a -> a -> Ordering) -> a -> [a] -> [a]
forall a. (a -> a -> Ordering) -> a -> [a] -> [a]
L.insertBy a -> a -> Ordering
cmp a
x [a]
pref
(a
z:[a]
zs, Ordering
GT) -> a
z a -> [a] -> [a]
forall a. a -> [a] -> [a]
: (a -> a -> Ordering) -> a -> [a] -> [a]
forall a. (a -> a -> Ordering) -> a -> [a] -> [a]
L.insertBy a -> a -> Ordering
cmp a
x [a]
zs [a] -> [a] -> [a]
forall a. [a] -> [a] -> [a]
++ [a]
pref
(a
_:[a]
_, Ordering
EQ) -> a
x a -> [a] -> [a]
forall a. a -> [a] -> [a]
: [a]
xs
(a
_:[a]
_, Ordering
LT) -> [a]
rest [a] -> [a] -> [a]
forall a. [a] -> [a] -> [a]
++ (a -> a -> Ordering) -> a -> [a] -> [a]
forall a. (a -> a -> Ordering) -> a -> [a] -> [a]
L.insertBy a -> a -> Ordering
cmp a
x [a]
pref
where
([a]
pref,[a]
rest) = (a -> a -> Ordering) -> [a] -> ([a], [a])
forall a. (a -> a -> Ordering) -> [a] -> ([a], [a])
splitIncr a -> a -> Ordering
cmp [a]
xs
splitIncr :: (a -> a -> Ordering) -> [a] -> ([a],[a])
splitIncr :: (a -> a -> Ordering) -> [a] -> ([a], [a])
splitIncr a -> a -> Ordering
_ [] = ([],[])
splitIncr a -> a -> Ordering
cmp xs :: [a]
xs@(a
x:[a]
_) = ([a], [a]) -> ([a], [a])
forall a b. (a, b) -> (b, a)
swap (([a], [a]) -> ([a], [a]))
-> ([(a, a)] -> ([a], [a])) -> [(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])
forall (p :: * -> * -> *) a b c d.
Bifunctor p =>
(a -> b) -> (c -> d) -> p a c -> p b d
bimap (((a, a) -> a) -> [(a, a)] -> [a]
forall a b. (a -> b) -> [a] -> [b]
map (a, a) -> a
forall a b. (a, b) -> b
snd) (((a, a) -> a) -> [(a, a)] -> [a]
forall a b. (a -> b) -> [a] -> [b]
map (a, a) -> a
forall a b. (a, b) -> b
snd)
(([(a, a)], [(a, a)]) -> ([a], [a]))
-> ([(a, a)] -> ([(a, a)], [(a, a)])) -> [(a, a)] -> ([a], [a])
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((a, a) -> Bool) -> [(a, a)] -> ([(a, a)], [(a, a)])
forall a. (a -> Bool) -> [a] -> ([a], [a])
L.break (\(a
a,a
b) -> (a
a a -> a -> Ordering
`cmp` a
b) Ordering -> Ordering -> Bool
forall a. Eq a => a -> a -> Bool
== Ordering
GT) ([(a, a)] -> ([a], [a])) -> [(a, a)] -> ([a], [a])
forall a b. (a -> b) -> a -> b
$ [a] -> [a] -> [(a, a)]
forall a b. [a] -> [b] -> [(a, b)]
zip (a
xa -> [a] -> [a]
forall a. a -> [a] -> [a]
:[a]
xs) [a]
xs
isShiftOf :: Eq a => CSeq a -> CSeq a -> Bool
CSeq a
xs isShiftOf :: CSeq a -> CSeq a -> Bool
`isShiftOf` CSeq a
ys = let twice :: CSeq a -> Seq a
twice CSeq a
zs = let zs' :: Seq a
zs' = CSeq a -> Seq a
forall a. CSeq a -> Seq a
leftElements CSeq a
zs in Seq a
zs' Seq a -> Seq a -> Seq a
forall a. Semigroup a => a -> a -> a
<> Seq a
zs'
once :: CSeq a -> Seq a
once = CSeq a -> Seq a
forall a. CSeq a -> Seq a
leftElements
check :: CSeq a -> CSeq a -> Bool
check CSeq a
as CSeq a
bs = Maybe Int -> Bool
forall a. Maybe a -> Bool
isJust (Maybe Int -> Bool) -> Maybe Int -> Bool
forall a b. (a -> b) -> a -> b
$ CSeq a -> Seq a
forall a. CSeq a -> Seq a
once CSeq a
as Seq a -> Seq a -> Maybe Int
forall a (p :: * -> *) (t :: * -> *).
(Eq a, Foldable p, Foldable t) =>
p a -> t a -> Maybe Int
`isSubStringOf` CSeq a -> Seq a
forall a. CSeq a -> Seq a
twice CSeq a
bs
in CSeq a -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length CSeq a
xs Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== CSeq a -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length CSeq a
ys Bool -> Bool -> Bool
&& CSeq a -> CSeq a -> Bool
forall a. Eq a => CSeq a -> CSeq a -> Bool
check CSeq a
xs CSeq a
ys