{-# LANGUAGE CPP
            ,MultiParamTypeClasses
            ,FlexibleContexts
            ,FlexibleInstances
            ,TypeFamilies
            ,UndecidableInstances #-}
#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           Control.Monad
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 :: (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' :: (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 :: (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' :: (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 :: 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]
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 :: v a -> a
maximum = v a -> a
forall (v :: * -> *) a. (Vector v a, Ord a) => v a -> a
V.maximum
    minimum :: 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 :: 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 :: 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 :: 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 = (!)
    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 :: 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 :: 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 :: 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 :: 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 :: fullinp -> m (v a)
sequence  = ([a] -> v a) -> m [a] -> m (v a)
forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM [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 :: * -> *) (m :: * -> *) a.
(Traversable t, Monad m) =>
t (m a) -> m (t a)
P.sequence  ([m a] -> m [a]) -> (fullinp -> [m a]) -> fullinp -> m [a]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. fullinp -> [m a]
forall l. IsList l => l -> [Item l]
toList
    mapM :: (a -> m item') -> v a -> m full'
mapM a -> m item'
func = ([item'] -> full') -> m [item'] -> m full'
forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM [item'] -> 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 :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
P.mapM 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]
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 :: 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]
forall l. IsList l => l -> [Item l]
toList
    unlines :: 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]
forall l. IsList l => l -> [Item l]
toList

isPrefixOf' :: (V.Vector v a, Eq (v a)) => v a -> v a -> Bool
isPrefixOf' :: 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' :: 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