{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE TypeFamilies      #-}

module HaskellWorks.Data.Length
    ( Container(..)
    , Length(..)
    ) where

import Data.Int
import Data.Word
import HaskellWorks.Data.Container
import HaskellWorks.Data.Positioning
import Prelude                       hiding (length)

import qualified Data.ByteString      as BS
import qualified Data.List            as L
import qualified Data.Vector          as DV
import qualified Data.Vector.Storable as DVS

class Container v => Length v where
  length :: v -> Count
  end :: v -> Position
  end = Count -> Position
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Count -> Position) -> (v -> Count) -> v -> Position
forall b c a. (b -> c) -> (a -> b) -> a -> c
. v -> Count
forall v. Length v => v -> Count
length
  {-# INLINE end #-}

instance Length [a] where
  length :: [a] -> Count
length = Int -> Count
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> Count) -> ([a] -> Int) -> [a] -> Count
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
L.length
  {-# INLINE length #-}

instance Length BS.ByteString where
  length :: ByteString -> Count
length = Int -> Count
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> Count) -> (ByteString -> Int) -> ByteString -> Count
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Int
BS.length
  {-# INLINE length #-}

instance Length (DV.Vector Word8) where
  length :: Vector Word8 -> Count
length = Int -> Count
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> Count) -> (Vector Word8 -> Int) -> Vector Word8 -> Count
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Vector Word8 -> Int
forall a. Vector a -> Int
DV.length
  {-# INLINE length #-}

instance Length (DV.Vector Word16) where
  length :: Vector Word16 -> Count
length = Int -> Count
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> Count) -> (Vector Word16 -> Int) -> Vector Word16 -> Count
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Vector Word16 -> Int
forall a. Vector a -> Int
DV.length
  {-# INLINE length #-}

instance Length (DV.Vector Word32) where
  length :: Vector Word32 -> Count
length = Int -> Count
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> Count) -> (Vector Word32 -> Int) -> Vector Word32 -> Count
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Vector Word32 -> Int
forall a. Vector a -> Int
DV.length
  {-# INLINE length #-}

instance Length (DV.Vector Word64) where
  length :: Vector Count -> Count
length = Int -> Count
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> Count) -> (Vector Count -> Int) -> Vector Count -> Count
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Vector Count -> Int
forall a. Vector a -> Int
DV.length
  {-# INLINE length #-}

instance Length (DVS.Vector Word8) where
  length :: Vector Word8 -> Count
length = Int -> Count
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> Count) -> (Vector Word8 -> Int) -> Vector Word8 -> Count
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Vector Word8 -> Int
forall a. Storable a => Vector a -> Int
DVS.length
  {-# INLINE length #-}

instance Length (DVS.Vector Word16) where
  length :: Vector Word16 -> Count
length = Int -> Count
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> Count) -> (Vector Word16 -> Int) -> Vector Word16 -> Count
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Vector Word16 -> Int
forall a. Storable a => Vector a -> Int
DVS.length
  {-# INLINE length #-}

instance Length (DVS.Vector Word32) where
  length :: Vector Word32 -> Count
length = Int -> Count
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> Count) -> (Vector Word32 -> Int) -> Vector Word32 -> Count
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Vector Word32 -> Int
forall a. Storable a => Vector a -> Int
DVS.length
  {-# INLINE length #-}

instance Length (DVS.Vector Word64) where
  length :: Vector Count -> Count
length = Int -> Count
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> Count) -> (Vector Count -> Int) -> Vector Count -> Count
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Vector Count -> Int
forall a. Storable a => Vector a -> Int
DVS.length
  {-# INLINE length #-}

instance Length (DV.Vector Int8) where
  length :: Vector Int8 -> Count
length = Int -> Count
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> Count) -> (Vector Int8 -> Int) -> Vector Int8 -> Count
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Vector Int8 -> Int
forall a. Vector a -> Int
DV.length
  {-# INLINE length #-}

instance Length (DV.Vector Int16) where
  length :: Vector Int16 -> Count
length = Int -> Count
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> Count) -> (Vector Int16 -> Int) -> Vector Int16 -> Count
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Vector Int16 -> Int
forall a. Vector a -> Int
DV.length
  {-# INLINE length #-}

instance Length (DV.Vector Int32) where
  length :: Vector Int32 -> Count
length = Int -> Count
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> Count) -> (Vector Int32 -> Int) -> Vector Int32 -> Count
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Vector Int32 -> Int
forall a. Vector a -> Int
DV.length
  {-# INLINE length #-}

instance Length (DV.Vector Int64) where
  length :: Vector Position -> Count
length = Int -> Count
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> Count)
-> (Vector Position -> Int) -> Vector Position -> Count
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Vector Position -> Int
forall a. Vector a -> Int
DV.length
  {-# INLINE length #-}

instance Length (DVS.Vector Int8) where
  length :: Vector Int8 -> Count
length = Int -> Count
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> Count) -> (Vector Int8 -> Int) -> Vector Int8 -> Count
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Vector Int8 -> Int
forall a. Storable a => Vector a -> Int
DVS.length
  {-# INLINE length #-}

instance Length (DVS.Vector Int16) where
  length :: Vector Int16 -> Count
length = Int -> Count
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> Count) -> (Vector Int16 -> Int) -> Vector Int16 -> Count
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Vector Int16 -> Int
forall a. Storable a => Vector a -> Int
DVS.length
  {-# INLINE length #-}

instance Length (DVS.Vector Int32) where
  length :: Vector Int32 -> Count
length = Int -> Count
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> Count) -> (Vector Int32 -> Int) -> Vector Int32 -> Count
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Vector Int32 -> Int
forall a. Storable a => Vector a -> Int
DVS.length
  {-# INLINE length #-}

instance Length (DVS.Vector Int64) where
  length :: Vector Position -> Count
length = Int -> Count
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> Count)
-> (Vector Position -> Int) -> Vector Position -> Count
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Vector Position -> Int
forall a. Storable a => Vector a -> Int
DVS.length
  {-# INLINE length #-}

instance Length (DVS.Vector Int) where
  length :: Vector Int -> Count
length = Int -> Count
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> Count) -> (Vector Int -> Int) -> Vector Int -> Count
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Vector Int -> Int
forall a. Storable a => Vector a -> Int
DVS.length
  {-# INLINE length #-}