{-# LANGUAGE FlexibleInstances          #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE StandaloneDeriving         #-}

module HaskellWorks.Data.RankSelect.Base.Rank0
    ( Rank0(..)
    ) where

import Data.Word
import HaskellWorks.Data.AtIndex
import HaskellWorks.Data.Bits.BitShown
import HaskellWorks.Data.Bits.ElemFixedBitSize
import HaskellWorks.Data.Bits.PopCount.PopCount0
import HaskellWorks.Data.Positioning
import HaskellWorks.Data.RankSelect.Base.Rank1   as X
import Prelude                                   as P

import qualified Data.Vector          as DV
import qualified Data.Vector.Storable as DVS

{-# ANN module ("HLint: Ignore Reduce duplication"  :: String) #-}

class Rank0 v where
  rank0 :: v -> Count -> Count

deriving instance Rank0 a => Rank0 (BitShown a)

instance Rank0 Word8 where
  rank0 :: Word8 -> Count -> Count
rank0 Word8
v Count
s0 = Count
s0 Count -> Count -> Count
forall a. Num a => a -> a -> a
- Word8 -> Count -> Count
forall v. Rank1 v => v -> Count -> Count
rank1 Word8
v Count
s0
  {-# INLINE rank0 #-}

instance Rank0 Word16 where
  rank0 :: Word16 -> Count -> Count
rank0 Word16
v Count
s0 = Count
s0 Count -> Count -> Count
forall a. Num a => a -> a -> a
- Word16 -> Count -> Count
forall v. Rank1 v => v -> Count -> Count
rank1 Word16
v Count
s0
  {-# INLINE rank0 #-}

instance Rank0 Word32 where
  rank0 :: Word32 -> Count -> Count
rank0 Word32
v Count
s0 = Count
s0 Count -> Count -> Count
forall a. Num a => a -> a -> a
- Word32 -> Count -> Count
forall v. Rank1 v => v -> Count -> Count
rank1 Word32
v Count
s0
  {-# INLINE rank0 #-}

instance Rank0 Word64 where
  rank0 :: Count -> Count -> Count
rank0 Count
v Count
s0 = Count
s0 Count -> Count -> Count
forall a. Num a => a -> a -> a
- Count -> Count -> Count
forall v. Rank1 v => v -> Count -> Count
rank1 Count
v Count
s0
  {-# INLINE rank0 #-}

instance Rank0 [Bool] where
  rank0 :: [Bool] -> Count -> Count
rank0 = Count -> [Bool] -> Count -> Count
forall a t. (Eq a, Num a, Num t) => t -> [Bool] -> a -> t
go Count
0
    where go :: t -> [Bool] -> a -> t
go t
r [Bool]
_ a
0          = t
r
          go t
r (Bool
False:[Bool]
bs) a
p = t -> [Bool] -> a -> t
go (t
r t -> t -> t
forall a. Num a => a -> a -> a
+ t
1) [Bool]
bs (a
p a -> a -> a
forall a. Num a => a -> a -> a
- a
1)
          go t
r (Bool
True:[Bool]
bs) a
p  = t -> [Bool] -> a -> t
go  t
r      [Bool]
bs (a
p a -> a -> a
forall a. Num a => a -> a -> a
- a
1)
          go t
_ [] a
_         = [Char] -> t
forall a. HasCallStack => [Char] -> a
error [Char]
"Out of range"
  {-# INLINE rank0 #-}

instance Rank0 [Word8] where
  rank0 :: [Word8] -> Count -> Count
rank0 [Word8]
v Count
p = [Word8] -> Count
forall v. PopCount0 v => v -> Count
popCount0 [Word8]
prefix Count -> Count -> Count
forall a. Num a => a -> a -> a
+ if Count
r Count -> Count -> Bool
forall a. Eq a => a -> a -> Bool
== Count
0 then Count
0 else (Word8 -> Count -> Count
forall v. Rank0 v => v -> Count -> Count
`rank0` Count
r) Word8
maybeElem
    where (Count
q, Count
r)    = if Count
p Count -> Count -> Bool
forall a. Ord a => a -> a -> Bool
< Count
1 then (Count
0, Count
0) else ((Count
p Count -> Count -> Count
forall a. Num a => a -> a -> a
- Count
1) Count -> Count -> Count
forall a. Integral a => a -> a -> a
`quot` [Word8] -> Count
forall v. ElemFixedBitSize v => v -> Count
elemFixedBitSize [Word8]
v, ((Count
p Count -> Count -> Count
forall a. Num a => a -> a -> a
- Count
1) Count -> Count -> Count
forall a. Integral a => a -> a -> a
`rem` [Word8] -> Count
forall v. ElemFixedBitSize v => v -> Count
elemFixedBitSize [Word8]
v) Count -> Count -> Count
forall a. Num a => a -> a -> a
+ Count
1)
          prefix :: [Word8]
prefix    = Int -> [Word8] -> [Word8]
forall a. Int -> [a] -> [a]
take (Count -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Count
q) [Word8]
v
          maybeElem :: Word8
maybeElem = [Word8]
v [Word8] -> Int -> Word8
forall a. [a] -> Int -> a
!! Count -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Count
q
  {-# INLINE rank0 #-}

instance Rank0 [Word16] where
  rank0 :: [Word16] -> Count -> Count
rank0 [Word16]
v Count
p = [Word16] -> Count
forall v. PopCount0 v => v -> Count
popCount0 [Word16]
prefix Count -> Count -> Count
forall a. Num a => a -> a -> a
+ if Count
r Count -> Count -> Bool
forall a. Eq a => a -> a -> Bool
== Count
0 then Count
0 else (Word16 -> Count -> Count
forall v. Rank0 v => v -> Count -> Count
`rank0` Count
r) Word16
maybeElem
    where (Count
q, Count
r)    = if Count
p Count -> Count -> Bool
forall a. Ord a => a -> a -> Bool
< Count
1 then (Count
0, Count
0) else ((Count
p Count -> Count -> Count
forall a. Num a => a -> a -> a
- Count
1) Count -> Count -> Count
forall a. Integral a => a -> a -> a
`quot` [Word16] -> Count
forall v. ElemFixedBitSize v => v -> Count
elemFixedBitSize [Word16]
v, ((Count
p Count -> Count -> Count
forall a. Num a => a -> a -> a
- Count
1) Count -> Count -> Count
forall a. Integral a => a -> a -> a
`rem` [Word16] -> Count
forall v. ElemFixedBitSize v => v -> Count
elemFixedBitSize [Word16]
v) Count -> Count -> Count
forall a. Num a => a -> a -> a
+ Count
1)
          prefix :: [Word16]
prefix    = Int -> [Word16] -> [Word16]
forall a. Int -> [a] -> [a]
take (Count -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Count
q) [Word16]
v
          maybeElem :: Word16
maybeElem = [Word16]
v [Word16] -> Int -> Word16
forall a. [a] -> Int -> a
!! Count -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Count
q
  {-# INLINE rank0 #-}

instance Rank0 [Word32] where
  rank0 :: [Word32] -> Count -> Count
rank0 [Word32]
v Count
p = [Word32] -> Count
forall v. PopCount0 v => v -> Count
popCount0 [Word32]
prefix Count -> Count -> Count
forall a. Num a => a -> a -> a
+ if Count
r Count -> Count -> Bool
forall a. Eq a => a -> a -> Bool
== Count
0 then Count
0 else (Word32 -> Count -> Count
forall v. Rank0 v => v -> Count -> Count
`rank0` Count
r) Word32
maybeElem
    where (Count
q, Count
r)    = if Count
p Count -> Count -> Bool
forall a. Ord a => a -> a -> Bool
< Count
1 then (Count
0, Count
0) else ((Count
p Count -> Count -> Count
forall a. Num a => a -> a -> a
- Count
1) Count -> Count -> Count
forall a. Integral a => a -> a -> a
`quot` [Word32] -> Count
forall v. ElemFixedBitSize v => v -> Count
elemFixedBitSize [Word32]
v, ((Count
p Count -> Count -> Count
forall a. Num a => a -> a -> a
- Count
1) Count -> Count -> Count
forall a. Integral a => a -> a -> a
`rem` [Word32] -> Count
forall v. ElemFixedBitSize v => v -> Count
elemFixedBitSize [Word32]
v) Count -> Count -> Count
forall a. Num a => a -> a -> a
+ Count
1)
          prefix :: [Word32]
prefix    = Int -> [Word32] -> [Word32]
forall a. Int -> [a] -> [a]
take (Count -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Count
q) [Word32]
v
          maybeElem :: Word32
maybeElem = [Word32]
v [Word32] -> Int -> Word32
forall a. [a] -> Int -> a
!! Count -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Count
q
  {-# INLINE rank0 #-}

instance Rank0 [Word64] where
  rank0 :: [Count] -> Count -> Count
rank0 [Count]
v Count
p = [Count] -> Count
forall v. PopCount0 v => v -> Count
popCount0 [Count]
prefix Count -> Count -> Count
forall a. Num a => a -> a -> a
+ if Count
r Count -> Count -> Bool
forall a. Eq a => a -> a -> Bool
== Count
0 then Count
0 else (Count -> Count -> Count
forall v. Rank0 v => v -> Count -> Count
`rank0` Count
r) Count
maybeElem
    where (Count
q, Count
r)    = if Count
p Count -> Count -> Bool
forall a. Ord a => a -> a -> Bool
< Count
1 then (Count
0, Count
0) else ((Count
p Count -> Count -> Count
forall a. Num a => a -> a -> a
- Count
1) Count -> Count -> Count
forall a. Integral a => a -> a -> a
`quot` [Count] -> Count
forall v. ElemFixedBitSize v => v -> Count
elemFixedBitSize [Count]
v, ((Count
p Count -> Count -> Count
forall a. Num a => a -> a -> a
- Count
1) Count -> Count -> Count
forall a. Integral a => a -> a -> a
`rem` [Count] -> Count
forall v. ElemFixedBitSize v => v -> Count
elemFixedBitSize [Count]
v) Count -> Count -> Count
forall a. Num a => a -> a -> a
+ Count
1)
          prefix :: [Count]
prefix    = Int -> [Count] -> [Count]
forall a. Int -> [a] -> [a]
take (Count -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Count
q) [Count]
v
          maybeElem :: Count
maybeElem = [Count]
v [Count] -> Int -> Count
forall a. [a] -> Int -> a
!! Count -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Count
q
  {-# INLINE rank0 #-}

instance Rank0 (DV.Vector Word8) where
  rank0 :: Vector Word8 -> Count -> Count
rank0 Vector Word8
v Count
p = Vector Word8 -> Count
forall v. PopCount0 v => v -> Count
popCount0 Vector Word8
prefix Count -> Count -> Count
forall a. Num a => a -> a -> a
+ if Count
r Count -> Count -> Bool
forall a. Eq a => a -> a -> Bool
== Count
0 then Count
0 else (Word8 -> Count -> Count
forall v. Rank0 v => v -> Count -> Count
`rank0` Count
r) Word8
maybeElem
    where (Count
q, Count
r)    = if Count
p Count -> Count -> Bool
forall a. Ord a => a -> a -> Bool
< Count
1 then (Count
0, Count
0) else ((Count
p Count -> Count -> Count
forall a. Num a => a -> a -> a
- Count
1) Count -> Count -> Count
forall a. Integral a => a -> a -> a
`quot` Vector Word8 -> Count
forall v. ElemFixedBitSize v => v -> Count
elemFixedBitSize Vector Word8
v, ((Count
p Count -> Count -> Count
forall a. Num a => a -> a -> a
- Count
1) Count -> Count -> Count
forall a. Integral a => a -> a -> a
`rem` Vector Word8 -> Count
forall v. ElemFixedBitSize v => v -> Count
elemFixedBitSize Vector Word8
v) Count -> Count -> Count
forall a. Num a => a -> a -> a
+ Count
1)
          prefix :: Vector Word8
prefix    = Int -> Vector Word8 -> Vector Word8
forall a. Int -> Vector a -> Vector a
DV.take (Count -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Count
q) Vector Word8
v
          maybeElem :: Elem (Vector Word8)
maybeElem = Vector Word8
v Vector Word8 -> Position -> Elem (Vector Word8)
forall v. AtIndex v => v -> Position -> Elem v
!!! Count -> Position
forall a b. (Integral a, Num b) => a -> b
fromIntegral Count
q
  {-# INLINE rank0 #-}

instance Rank0 (DV.Vector Word16) where
  rank0 :: Vector Word16 -> Count -> Count
rank0 Vector Word16
v Count
p = Vector Word16 -> Count
forall v. PopCount0 v => v -> Count
popCount0 Vector Word16
prefix Count -> Count -> Count
forall a. Num a => a -> a -> a
+ if Count
r Count -> Count -> Bool
forall a. Eq a => a -> a -> Bool
== Count
0 then Count
0 else (Word16 -> Count -> Count
forall v. Rank0 v => v -> Count -> Count
`rank0` Count
r) Word16
maybeElem
    where (Count
q, Count
r)    = if Count
p Count -> Count -> Bool
forall a. Ord a => a -> a -> Bool
< Count
1 then (Count
0, Count
0) else ((Count
p Count -> Count -> Count
forall a. Num a => a -> a -> a
- Count
1) Count -> Count -> Count
forall a. Integral a => a -> a -> a
`quot` Vector Word16 -> Count
forall v. ElemFixedBitSize v => v -> Count
elemFixedBitSize Vector Word16
v, ((Count
p Count -> Count -> Count
forall a. Num a => a -> a -> a
- Count
1) Count -> Count -> Count
forall a. Integral a => a -> a -> a
`rem` Vector Word16 -> Count
forall v. ElemFixedBitSize v => v -> Count
elemFixedBitSize Vector Word16
v) Count -> Count -> Count
forall a. Num a => a -> a -> a
+ Count
1)
          prefix :: Vector Word16
prefix    = Int -> Vector Word16 -> Vector Word16
forall a. Int -> Vector a -> Vector a
DV.take (Count -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Count
q) Vector Word16
v
          maybeElem :: Elem (Vector Word16)
maybeElem = Vector Word16
v Vector Word16 -> Position -> Elem (Vector Word16)
forall v. AtIndex v => v -> Position -> Elem v
!!! Count -> Position
forall a b. (Integral a, Num b) => a -> b
fromIntegral Count
q
  {-# INLINE rank0 #-}

instance Rank0 (DV.Vector Word32) where
  rank0 :: Vector Word32 -> Count -> Count
rank0 Vector Word32
v Count
p = Vector Word32 -> Count
forall v. PopCount0 v => v -> Count
popCount0 Vector Word32
prefix Count -> Count -> Count
forall a. Num a => a -> a -> a
+ if Count
r Count -> Count -> Bool
forall a. Eq a => a -> a -> Bool
== Count
0 then Count
0 else (Word32 -> Count -> Count
forall v. Rank0 v => v -> Count -> Count
`rank0` Count
r) Word32
maybeElem
    where (Count
q, Count
r)    = if Count
p Count -> Count -> Bool
forall a. Ord a => a -> a -> Bool
< Count
1 then (Count
0, Count
0) else ((Count
p Count -> Count -> Count
forall a. Num a => a -> a -> a
- Count
1) Count -> Count -> Count
forall a. Integral a => a -> a -> a
`quot` Vector Word32 -> Count
forall v. ElemFixedBitSize v => v -> Count
elemFixedBitSize Vector Word32
v, ((Count
p Count -> Count -> Count
forall a. Num a => a -> a -> a
- Count
1) Count -> Count -> Count
forall a. Integral a => a -> a -> a
`rem` Vector Word32 -> Count
forall v. ElemFixedBitSize v => v -> Count
elemFixedBitSize Vector Word32
v) Count -> Count -> Count
forall a. Num a => a -> a -> a
+ Count
1)
          prefix :: Vector Word32
prefix    = Int -> Vector Word32 -> Vector Word32
forall a. Int -> Vector a -> Vector a
DV.take (Count -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Count
q) Vector Word32
v
          maybeElem :: Elem (Vector Word32)
maybeElem = Vector Word32
v Vector Word32 -> Position -> Elem (Vector Word32)
forall v. AtIndex v => v -> Position -> Elem v
!!! Count -> Position
forall a b. (Integral a, Num b) => a -> b
fromIntegral Count
q
  {-# INLINE rank0 #-}

instance Rank0 (DV.Vector Word64) where
  rank0 :: Vector Count -> Count -> Count
rank0 Vector Count
v Count
p = Vector Count -> Count
forall v. PopCount0 v => v -> Count
popCount0 Vector Count
prefix Count -> Count -> Count
forall a. Num a => a -> a -> a
+ if Count
r Count -> Count -> Bool
forall a. Eq a => a -> a -> Bool
== Count
0 then Count
0 else (Count -> Count -> Count
forall v. Rank0 v => v -> Count -> Count
`rank0` Count
r) Count
maybeElem
    where (Count
q, Count
r)    = if Count
p Count -> Count -> Bool
forall a. Ord a => a -> a -> Bool
< Count
1 then (Count
0, Count
0) else ((Count
p Count -> Count -> Count
forall a. Num a => a -> a -> a
- Count
1) Count -> Count -> Count
forall a. Integral a => a -> a -> a
`quot` Vector Count -> Count
forall v. ElemFixedBitSize v => v -> Count
elemFixedBitSize Vector Count
v, ((Count
p Count -> Count -> Count
forall a. Num a => a -> a -> a
- Count
1) Count -> Count -> Count
forall a. Integral a => a -> a -> a
`rem` Vector Count -> Count
forall v. ElemFixedBitSize v => v -> Count
elemFixedBitSize Vector Count
v) Count -> Count -> Count
forall a. Num a => a -> a -> a
+ Count
1)
          prefix :: Vector Count
prefix    = Int -> Vector Count -> Vector Count
forall a. Int -> Vector a -> Vector a
DV.take (Count -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Count
q) Vector Count
v
          maybeElem :: Elem (Vector Count)
maybeElem = Vector Count
v Vector Count -> Position -> Elem (Vector Count)
forall v. AtIndex v => v -> Position -> Elem v
!!! Count -> Position
forall a b. (Integral a, Num b) => a -> b
fromIntegral Count
q
  {-# INLINE rank0 #-}

instance Rank0 (DVS.Vector Word8) where
  rank0 :: Vector Word8 -> Count -> Count
rank0 Vector Word8
v Count
p = Vector Word8 -> Count
forall v. PopCount0 v => v -> Count
popCount0 Vector Word8
prefix Count -> Count -> Count
forall a. Num a => a -> a -> a
+ if Count
r Count -> Count -> Bool
forall a. Eq a => a -> a -> Bool
== Count
0 then Count
0 else (Word8 -> Count -> Count
forall v. Rank0 v => v -> Count -> Count
`rank0` Count
r) Word8
maybeElem
    where (Count
q, Count
r)    = if Count
p Count -> Count -> Bool
forall a. Ord a => a -> a -> Bool
< Count
1 then (Count
0, Count
0) else ((Count
p Count -> Count -> Count
forall a. Num a => a -> a -> a
- Count
1) Count -> Count -> Count
forall a. Integral a => a -> a -> a
`quot` Vector Word8 -> Count
forall v. ElemFixedBitSize v => v -> Count
elemFixedBitSize Vector Word8
v, ((Count
p Count -> Count -> Count
forall a. Num a => a -> a -> a
- Count
1) Count -> Count -> Count
forall a. Integral a => a -> a -> a
`rem` Vector Word8 -> Count
forall v. ElemFixedBitSize v => v -> Count
elemFixedBitSize Vector Word8
v) Count -> Count -> Count
forall a. Num a => a -> a -> a
+ Count
1)
          prefix :: Vector Word8
prefix    = Int -> Vector Word8 -> Vector Word8
forall a. Storable a => Int -> Vector a -> Vector a
DVS.take (Count -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Count
q) Vector Word8
v
          maybeElem :: Elem (Vector Word8)
maybeElem = Vector Word8
v Vector Word8 -> Position -> Elem (Vector Word8)
forall v. AtIndex v => v -> Position -> Elem v
!!! Count -> Position
forall a b. (Integral a, Num b) => a -> b
fromIntegral Count
q
  {-# INLINE rank0 #-}

instance Rank0 (DVS.Vector Word16) where
  rank0 :: Vector Word16 -> Count -> Count
rank0 Vector Word16
v Count
p = Vector Word16 -> Count
forall v. PopCount0 v => v -> Count
popCount0 Vector Word16
prefix Count -> Count -> Count
forall a. Num a => a -> a -> a
+ if Count
r Count -> Count -> Bool
forall a. Eq a => a -> a -> Bool
== Count
0 then Count
0 else (Word16 -> Count -> Count
forall v. Rank0 v => v -> Count -> Count
`rank0` Count
r) Word16
maybeElem
    where (Count
q, Count
r)    = if Count
p Count -> Count -> Bool
forall a. Ord a => a -> a -> Bool
< Count
1 then (Count
0, Count
0) else ((Count
p Count -> Count -> Count
forall a. Num a => a -> a -> a
- Count
1) Count -> Count -> Count
forall a. Integral a => a -> a -> a
`quot` Vector Word16 -> Count
forall v. ElemFixedBitSize v => v -> Count
elemFixedBitSize Vector Word16
v, ((Count
p Count -> Count -> Count
forall a. Num a => a -> a -> a
- Count
1) Count -> Count -> Count
forall a. Integral a => a -> a -> a
`rem` Vector Word16 -> Count
forall v. ElemFixedBitSize v => v -> Count
elemFixedBitSize Vector Word16
v) Count -> Count -> Count
forall a. Num a => a -> a -> a
+ Count
1)
          prefix :: Vector Word16
prefix    = Int -> Vector Word16 -> Vector Word16
forall a. Storable a => Int -> Vector a -> Vector a
DVS.take (Count -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Count
q) Vector Word16
v
          maybeElem :: Elem (Vector Word16)
maybeElem = Vector Word16
v Vector Word16 -> Position -> Elem (Vector Word16)
forall v. AtIndex v => v -> Position -> Elem v
!!! Count -> Position
forall a b. (Integral a, Num b) => a -> b
fromIntegral Count
q
  {-# INLINE rank0 #-}

instance Rank0 (DVS.Vector Word32) where
  rank0 :: Vector Word32 -> Count -> Count
rank0 Vector Word32
v Count
p = Vector Word32 -> Count
forall v. PopCount0 v => v -> Count
popCount0 Vector Word32
prefix Count -> Count -> Count
forall a. Num a => a -> a -> a
+ if Count
r Count -> Count -> Bool
forall a. Eq a => a -> a -> Bool
== Count
0 then Count
0 else (Word32 -> Count -> Count
forall v. Rank0 v => v -> Count -> Count
`rank0` Count
r) Word32
maybeElem
    where (Count
q, Count
r)    = if Count
p Count -> Count -> Bool
forall a. Ord a => a -> a -> Bool
< Count
1 then (Count
0, Count
0) else ((Count
p Count -> Count -> Count
forall a. Num a => a -> a -> a
- Count
1) Count -> Count -> Count
forall a. Integral a => a -> a -> a
`quot` Vector Word32 -> Count
forall v. ElemFixedBitSize v => v -> Count
elemFixedBitSize Vector Word32
v, ((Count
p Count -> Count -> Count
forall a. Num a => a -> a -> a
- Count
1) Count -> Count -> Count
forall a. Integral a => a -> a -> a
`rem` Vector Word32 -> Count
forall v. ElemFixedBitSize v => v -> Count
elemFixedBitSize Vector Word32
v) Count -> Count -> Count
forall a. Num a => a -> a -> a
+ Count
1)
          prefix :: Vector Word32
prefix    = Int -> Vector Word32 -> Vector Word32
forall a. Storable a => Int -> Vector a -> Vector a
DVS.take (Count -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Count
q) Vector Word32
v
          maybeElem :: Elem (Vector Word32)
maybeElem = Vector Word32
v Vector Word32 -> Position -> Elem (Vector Word32)
forall v. AtIndex v => v -> Position -> Elem v
!!! Count -> Position
forall a b. (Integral a, Num b) => a -> b
fromIntegral Count
q
  {-# INLINE rank0 #-}

instance Rank0 (DVS.Vector Word64) where
  rank0 :: Vector Count -> Count -> Count
rank0 Vector Count
v Count
p = Vector Count -> Count
forall v. PopCount0 v => v -> Count
popCount0 Vector Count
prefix Count -> Count -> Count
forall a. Num a => a -> a -> a
+ if Count
r Count -> Count -> Bool
forall a. Eq a => a -> a -> Bool
== Count
0 then Count
0 else (Count -> Count -> Count
forall v. Rank0 v => v -> Count -> Count
`rank0` Count
r) Count
maybeElem
    where (Count
q, Count
r)    = if Count
p Count -> Count -> Bool
forall a. Ord a => a -> a -> Bool
< Count
1 then (Count
0, Count
0) else ((Count
p Count -> Count -> Count
forall a. Num a => a -> a -> a
- Count
1) Count -> Count -> Count
forall a. Integral a => a -> a -> a
`quot` Vector Count -> Count
forall v. ElemFixedBitSize v => v -> Count
elemFixedBitSize Vector Count
v, ((Count
p Count -> Count -> Count
forall a. Num a => a -> a -> a
- Count
1) Count -> Count -> Count
forall a. Integral a => a -> a -> a
`rem` Vector Count -> Count
forall v. ElemFixedBitSize v => v -> Count
elemFixedBitSize Vector Count
v) Count -> Count -> Count
forall a. Num a => a -> a -> a
+ Count
1)
          prefix :: Vector Count
prefix    = Int -> Vector Count -> Vector Count
forall a. Storable a => Int -> Vector a -> Vector a
DVS.take (Count -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Count
q) Vector Count
v
          maybeElem :: Elem (Vector Count)
maybeElem = Vector Count
v Vector Count -> Position -> Elem (Vector Count)
forall v. AtIndex v => v -> Position -> Elem v
!!! Count -> Position
forall a b. (Integral a, Num b) => a -> b
fromIntegral Count
q
  {-# INLINE rank0 #-}