{-# LANGUAGE CPP
            ,MultiParamTypeClasses
            ,FlexibleContexts
            ,FlexibleInstances
            ,TypeFamilies
            ,UndecidableInstances #-}
{-# LANGUAGE TypeOperators #-}  -- for GHC >= 9.4
#if __GLASGOW_HASKELL__ < 710
{-# LANGUAGE OverlappingInstances #-}
#endif
{-# OPTIONS -fno-warn-orphans #-}

-- | ListLike instance for any type supporting the @Data.Vector.Generic@
-- interface.  To avoid collisions with other Vector instances, this module
-- must be imported directly.
module Data.ListLike.Vector.Generic ()

where

import           Prelude as P
import qualified Data.Vector.Generic as V
import           Data.Vector.Generic ((!))
import           Data.ListLike.Base
import           Data.ListLike.FoldableLL
import           Data.ListLike.String
import           Data.String (IsString)
import           GHC.Exts (IsList(..))

instance {-# OVERLAPPABLE #-} V.Vector v a => FoldableLL (v a) a where
    foldl :: forall a. (a -> a -> a) -> a -> v a -> a
foldl = (a -> a -> a) -> a -> v a -> a
forall (v :: * -> *) b a.
Vector v b =>
(a -> b -> a) -> a -> v b -> a
V.foldl
    foldl' :: forall a. (a -> a -> a) -> a -> v a -> a
foldl' = (a -> a -> a) -> a -> v a -> a
forall (v :: * -> *) b a.
Vector v b =>
(a -> b -> a) -> a -> v b -> a
V.foldl'
    foldl1 :: (a -> a -> a) -> v a -> a
foldl1 = (a -> a -> a) -> v a -> a
forall (v :: * -> *) a. Vector v a => (a -> a -> a) -> v a -> a
V.foldl1
    foldr :: forall b. (a -> b -> b) -> b -> v a -> b
foldr = (a -> b -> b) -> b -> v a -> b
forall (v :: * -> *) a b.
Vector v a =>
(a -> b -> b) -> b -> v a -> b
V.foldr
    foldr' :: forall b. (a -> b -> b) -> b -> v a -> b
foldr' = (a -> b -> b) -> b -> v a -> b
forall (v :: * -> *) a b.
Vector v a =>
(a -> b -> b) -> b -> v a -> b
V.foldr'
    foldr1 :: (a -> a -> a) -> v a -> a
foldr1 = (a -> a -> a) -> v a -> a
forall (v :: * -> *) a. Vector v a => (a -> a -> a) -> v a -> a
V.foldr1

#if 0
instance {-# OVERLAPPABLE #-} (Monoid (v a), Eq (v a), V.Vector v a) => IsList (v a) where
    type Item (v a) = a
    toList = V.toList
    fromList = V.fromList
#endif

instance {-# OVERLAPPABLE #-} (IsList (v a), Item (v a) ~ a, Monoid (v a), Eq (v a), V.Vector v a) => ListLike (v a) a where
    empty :: v a
empty = v a
forall (v :: * -> *) a. Vector v a => v a
V.empty
    singleton :: a -> v a
singleton = a -> v a
forall (v :: * -> *) a. Vector v a => a -> v a
V.singleton
    cons :: a -> v a -> v a
cons = a -> v a -> v a
forall (v :: * -> *) a. Vector v a => a -> v a -> v a
V.cons
    snoc :: v a -> a -> v a
snoc = v a -> a -> v a
forall (v :: * -> *) a. Vector v a => v a -> a -> v a
V.snoc
    append :: v a -> v a -> v a
append = v a -> v a -> v a
forall a. Monoid a => a -> a -> a
mappend
    head :: v a -> a
head = v a -> a
forall (v :: * -> *) a. Vector v a => v a -> a
V.head
    last :: v a -> a
last = v a -> a
forall (v :: * -> *) a. Vector v a => v a -> a
V.last
    tail :: v a -> v a
tail = v a -> v a
forall (v :: * -> *) a. Vector v a => v a -> v a
V.tail
    init :: v a -> v a
init = v a -> v a
forall (v :: * -> *) a. Vector v a => v a -> v a
V.init
    null :: v a -> Bool
null = v a -> Bool
forall (v :: * -> *) a. Vector v a => v a -> Bool
V.null
    length :: v a -> Int
length = v a -> Int
forall (v :: * -> *) a. Vector v a => v a -> Int
V.length
    rigidMap :: (a -> a) -> v a -> v a
rigidMap = (a -> a) -> v a -> v a
forall (v :: * -> *) a b.
(Vector v a, Vector v b) =>
(a -> b) -> v a -> v b
V.map
    reverse :: v a -> v a
reverse = v a -> v a
forall (v :: * -> *) a. Vector v a => v a -> v a
V.reverse
    --intersperse =
    concat :: forall full'. ListLike full' (v a) => full' -> v a
concat = [v a] -> v a
forall (v :: * -> *) a. Vector v a => [v a] -> v a
V.concat ([v a] -> v a) -> (full' -> [v a]) -> full' -> v a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. full' -> [v a]
full' -> [Item full']
forall l. IsList l => l -> [Item l]
toList
    rigidConcatMap :: (a -> v a) -> v a -> v a
rigidConcatMap = (a -> v a) -> v a -> v a
forall (v :: * -> *) a b.
(Vector v a, Vector v b) =>
(a -> v b) -> v a -> v b
V.concatMap
    any :: (a -> Bool) -> v a -> Bool
any = (a -> Bool) -> v a -> Bool
forall (v :: * -> *) a. Vector v a => (a -> Bool) -> v a -> Bool
V.any
    all :: (a -> Bool) -> v a -> Bool
all = (a -> Bool) -> v a -> Bool
forall (v :: * -> *) a. Vector v a => (a -> Bool) -> v a -> Bool
V.all
    maximum :: Ord a => v a -> a
maximum = v a -> a
forall (v :: * -> *) a. (Vector v a, Ord a) => v a -> a
V.maximum
    minimum :: Ord a => v a -> a
minimum = v a -> a
forall (v :: * -> *) a. (Vector v a, Ord a) => v a -> a
V.minimum
    replicate :: Int -> a -> v a
replicate = Int -> a -> v a
forall (v :: * -> *) a. Vector v a => Int -> a -> v a
V.replicate
    take :: Int -> v a -> v a
take = Int -> v a -> v a
forall (v :: * -> *) a. Vector v a => Int -> v a -> v a
V.take
    drop :: Int -> v a -> v a
drop = Int -> v a -> v a
forall (v :: * -> *) a. Vector v a => Int -> v a -> v a
V.drop
    --splitAt =
    takeWhile :: (a -> Bool) -> v a -> v a
takeWhile = (a -> Bool) -> v a -> v a
forall (v :: * -> *) a. Vector v a => (a -> Bool) -> v a -> v a
V.takeWhile
    dropWhile :: (a -> Bool) -> v a -> v a
dropWhile = (a -> Bool) -> v a -> v a
forall (v :: * -> *) a. Vector v a => (a -> Bool) -> v a -> v a
V.dropWhile
    span :: (a -> Bool) -> v a -> (v a, v a)
span = (a -> Bool) -> v a -> (v a, v a)
forall (v :: * -> *) a.
Vector v a =>
(a -> Bool) -> v a -> (v a, v a)
V.span
    break :: (a -> Bool) -> v a -> (v a, v a)
break = (a -> Bool) -> v a -> (v a, v a)
forall (v :: * -> *) a.
Vector v a =>
(a -> Bool) -> v a -> (v a, v a)
V.break
    --group =
    --inits =
    --tails =
    isPrefixOf :: Eq a => v a -> v a -> Bool
isPrefixOf = v a -> v a -> Bool
forall (v :: * -> *) a.
(Vector v a, Eq (v a)) =>
v a -> v a -> Bool
isPrefixOf'
    isSuffixOf :: Eq a => v a -> v a -> Bool
isSuffixOf = v a -> v a -> Bool
forall (v :: * -> *) a.
(Vector v a, Eq (v a)) =>
v a -> v a -> Bool
isSuffixOf'
    elem :: Eq a => a -> v a -> Bool
elem = a -> v a -> Bool
forall (v :: * -> *) a. (Vector v a, Eq a) => a -> v a -> Bool
V.elem
    find :: (a -> Bool) -> v a -> Maybe a
find = (a -> Bool) -> v a -> Maybe a
forall (v :: * -> *) a. Vector v a => (a -> Bool) -> v a -> Maybe a
V.find
    filter :: (a -> Bool) -> v a -> v a
filter = (a -> Bool) -> v a -> v a
forall (v :: * -> *) a. Vector v a => (a -> Bool) -> v a -> v a
V.filter
    index :: v a -> Int -> a
index = v a -> Int -> a
forall (v :: * -> *) a. Vector v a => v a -> Int -> a
(!)
    findIndex :: (a -> Bool) -> v a -> Maybe Int
findIndex = (a -> Bool) -> v a -> Maybe Int
forall (v :: * -> *) a.
Vector v a =>
(a -> Bool) -> v a -> Maybe Int
V.findIndex
    --toList = V.toList
    --fromList = V.fromList
    --fromListLike = fromList . toList
    --groupBy f =
    genericLength :: forall a. Num a => v a -> a
genericLength = Integer -> a
forall a. Num a => Integer -> a
fromInteger (Integer -> a) -> (v a -> Integer) -> v a -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Integer
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> Integer) -> (v a -> Int) -> v a -> Integer
forall b c a. (b -> c) -> (a -> b) -> a -> c
. v a -> Int
forall (v :: * -> *) a. Vector v a => v a -> Int
V.length
    genericTake :: forall a. Integral a => a -> v a -> v a
genericTake a
i = Int -> v a -> v a
forall (v :: * -> *) a. Vector v a => Int -> v a -> v a
V.take (a -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral a
i)
    genericDrop :: forall a. Integral a => a -> v a -> v a
genericDrop a
i = Int -> v a -> v a
forall (v :: * -> *) a. Vector v a => Int -> v a -> v a
V.drop (a -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral a
i)
    --genericSplitAt i =
    genericReplicate :: forall a. Integral a => a -> a -> v a
genericReplicate a
i = Int -> a -> v a
forall (v :: * -> *) a. Vector v a => Int -> a -> v a
V.replicate (a -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral a
i)

    sequence :: forall (m :: * -> *) fullinp.
(Applicative m, ListLike fullinp (m a)) =>
fullinp -> m (v a)
sequence  = ([a] -> v a) -> m [a] -> m (v a)
forall a b. (a -> b) -> m a -> m b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap [a] -> v a
[Item (v a)] -> v a
forall l. IsList l => [Item l] -> l
fromList (m [a] -> m (v a)) -> (fullinp -> m [a]) -> fullinp -> m (v a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [m a] -> m [a]
forall (t :: * -> *) (f :: * -> *) a.
(Traversable t, Applicative f) =>
t (f a) -> f (t a)
forall (f :: * -> *) a. Applicative f => [f a] -> f [a]
P.sequenceA  ([m a] -> m [a]) -> (fullinp -> [m a]) -> fullinp -> m [a]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. fullinp -> [m a]
fullinp -> [Item fullinp]
forall l. IsList l => l -> [Item l]
toList
    mapM :: forall (m :: * -> *) full' item'.
(Applicative m, ListLike full' item') =>
(a -> m item') -> v a -> m full'
mapM a -> m item'
func = ([item'] -> full') -> m [item'] -> m full'
forall a b. (a -> b) -> m a -> m b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap [item'] -> full'
[Item full'] -> full'
forall l. IsList l => [Item l] -> l
fromList (m [item'] -> m full') -> (v a -> m [item']) -> v a -> m full'
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> m item') -> [a] -> m [item']
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> [a] -> f [b]
P.traverse a -> m item'
func ([a] -> m [item']) -> (v a -> [a]) -> v a -> m [item']
forall b c a. (b -> c) -> (a -> b) -> a -> c
. v a -> [a]
v a -> [Item (v a)]
forall l. IsList l => l -> [Item l]
toList

instance (Eq (v Char), V.Vector v Char) => IsString (v Char) where
    fromString :: String -> v Char
fromString = String -> v Char
forall (v :: * -> *) a. Vector v a => [a] -> v a
V.fromList

instance (Eq (v Char), V.Vector v Char) => StringLike (v Char) where
    toString :: v Char -> String
toString = v Char -> String
forall (v :: * -> *) a. Vector v a => v a -> [a]
V.toList
    --words =
    --lines =
    unwords :: forall full. ListLike full (v Char) => full -> v Char
unwords = let sp :: v Char
sp = Char -> v Char
forall (v :: * -> *) a. Vector v a => a -> v a
V.singleton Char
' ' in [v Char] -> v Char
forall (v :: * -> *) a. Vector v a => [v a] -> v a
V.concat ([v Char] -> v Char) -> (full -> [v Char]) -> full -> v Char
forall b c a. (b -> c) -> (a -> b) -> a -> c
. v Char -> [v Char] -> [v Char]
forall full item. ListLike full item => item -> full -> full
intersperse v Char
sp ([v Char] -> [v Char]) -> (full -> [v Char]) -> full -> [v Char]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. full -> [v Char]
full -> [Item full]
forall l. IsList l => l -> [Item l]
toList
    unlines :: forall full. ListLike full (v Char) => full -> v Char
unlines = let eol :: v Char
eol = Char -> v Char
forall (v :: * -> *) a. Vector v a => a -> v a
V.singleton Char
'\n' in [v Char] -> v Char
forall (v :: * -> *) a. Vector v a => [v a] -> v a
V.concat ([v Char] -> v Char) -> (full -> [v Char]) -> full -> v Char
forall b c a. (b -> c) -> (a -> b) -> a -> c
. v Char -> [v Char] -> [v Char]
forall full item. ListLike full item => item -> full -> full
intersperse v Char
eol ([v Char] -> [v Char]) -> (full -> [v Char]) -> full -> [v Char]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. full -> [v Char]
full -> [Item full]
forall l. IsList l => l -> [Item l]
toList

isPrefixOf' :: (V.Vector v a, Eq (v a)) => v a -> v a -> Bool
isPrefixOf' :: forall (v :: * -> *) a.
(Vector v a, Eq (v a)) =>
v a -> v a -> Bool
isPrefixOf' v a
needle v a
haystack
  | v a -> Bool
forall (v :: * -> *) a. Vector v a => v a -> Bool
V.null v a
needle = Bool
True
  | v a -> Int
forall (v :: * -> *) a. Vector v a => v a -> Int
V.length v a
needle Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< v a -> Int
forall (v :: * -> *) a. Vector v a => v a -> Int
V.length v a
haystack =
            v a
needle v a -> v a -> Bool
forall a. Eq a => a -> a -> Bool
== Int -> Int -> v a -> v a
forall (v :: * -> *) a. Vector v a => Int -> Int -> v a -> v a
V.slice Int
0 (v a -> Int
forall (v :: * -> *) a. Vector v a => v a -> Int
V.length v a
needle) v a
haystack
  | v a -> Int
forall (v :: * -> *) a. Vector v a => v a -> Int
V.length v a
needle Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== v a -> Int
forall (v :: * -> *) a. Vector v a => v a -> Int
V.length v a
haystack = v a
needle v a -> v a -> Bool
forall a. Eq a => a -> a -> Bool
== v a
haystack
  | Bool
otherwise = Bool
False
isSuffixOf' :: (V.Vector v a, Eq (v a)) => v a -> v a -> Bool
isSuffixOf' :: forall (v :: * -> *) a.
(Vector v a, Eq (v a)) =>
v a -> v a -> Bool
isSuffixOf' v a
needle v a
haystack
  | v a -> Bool
forall (v :: * -> *) a. Vector v a => v a -> Bool
V.null v a
needle = Bool
True
  | v a -> Int
forall (v :: * -> *) a. Vector v a => v a -> Int
V.length v a
needle Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< v a -> Int
forall (v :: * -> *) a. Vector v a => v a -> Int
V.length v a
haystack =
        v a
needle v a -> v a -> Bool
forall a. Eq a => a -> a -> Bool
== Int -> Int -> v a -> v a
forall (v :: * -> *) a. Vector v a => Int -> Int -> v a -> v a
V.slice (v a -> Int
forall (v :: * -> *) a. Vector v a => v a -> Int
V.length v a
haystack Int -> Int -> Int
forall a. Num a => a -> a -> a
- v a -> Int
forall (v :: * -> *) a. Vector v a => v a -> Int
V.length v a
needle)
                          (v a -> Int
forall (v :: * -> *) a. Vector v a => v a -> Int
V.length v a
needle)
                          v a
haystack
  | v a -> Int
forall (v :: * -> *) a. Vector v a => v a -> Int
V.length v a
needle Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== v a -> Int
forall (v :: * -> *) a. Vector v a => v a -> Int
V.length v a
haystack = v a
needle v a -> v a -> Bool
forall a. Eq a => a -> a -> Bool
== v a
haystack
  | Bool
otherwise = Bool
False