{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE UndecidableInstances #-}
{-# OPTIONS_GHC -fno-warn-orphans #-}

module Data.VMap.KVVector (
  VG.Vector,
  VGM.MVector,
  KVVector (..),
  KVMVector,
  toMap,
  fromMap,
  fromAscList,
  fromAscListN,
  fromAscListWithKey,
  fromAscListWithKeyN,
  fromDistinctAscList,
  fromDistinctAscListN,
  fromList,
  fromListN,
  mapValsKVVector,
  mapWithKeyKVVector,
  memberKVVector,
  lookupKVVector,
  lookupDefaultKVVector,
  sortAscKVMVector,
  internKVVectorMaybe,
  normalize,
  normalizeM,
)
where

import Control.Applicative
import Control.DeepSeq
import Control.Monad
import Control.Monad.Primitive
import Control.Monad.ST
import Data.Kind
import qualified Data.List.NonEmpty as NE
import qualified Data.Map.Strict as Map
import Data.Maybe as Maybe
import Data.Semigroup
import Data.Typeable
import Data.Vector.Algorithms.Merge
import qualified Data.Vector.Generic as VG
import qualified Data.Vector.Generic.Mutable as VGM
import qualified Data.Vector.Primitive as VP
import qualified Data.Vector.Storable as VS
import qualified GHC.Exts as Exts
import GHC.Generics
import NoThunks.Class

-- | Convert a __sorted__ key/value vector into a `Map.Map`
toMap ::
  (VG.Vector kv k, VG.Vector vv v) =>
  KVVector kv vv (k, v) ->
  Map.Map k v
toMap :: forall (kv :: * -> *) k (vv :: * -> *) v.
(Vector kv k, Vector vv v) =>
KVVector kv vv (k, v) -> Map k v
toMap = forall k a. [(k, a)] -> Map k a
Map.fromDistinctAscList forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a -> b) -> [a] -> [b]
map forall {a} {b}. (a, b) -> (a, b)
evalSecond forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (v :: * -> *) a. Vector v a => v a -> [a]
VG.toList
  where
    evalSecond :: (a, b) -> (a, b)
evalSecond kv :: (a, b)
kv@(a
_, !b
_) = (a, b)
kv
{-# INLINE toMap #-}

-- | Convert a `Map.Map` into a sorted key/value vector.
fromMap ::
  (VG.Vector kv k, VG.Vector vv v) => Map.Map k v -> KVVector kv vv (k, v)
fromMap :: forall (kv :: * -> *) k (vv :: * -> *) v.
(Vector kv k, Vector vv v) =>
Map k v -> KVVector kv vv (k, v)
fromMap Map k v
m = forall (kv :: * -> *) k (vv :: * -> *) v.
(Vector kv k, Vector vv v) =>
Int -> [(k, v)] -> KVVector kv vv (k, v)
fromDistinctAscListN (forall k a. Map k a -> Int
Map.size Map k v
m) forall a b. (a -> b) -> a -> b
$ forall k a. Map k a -> [(k, a)]
Map.toAscList Map k v
m
{-# INLINE fromMap #-}

-- | Convert a possibly unsorted assoc list into a KVVector.
fromList ::
  (VG.Vector kv k, VG.Vector vv v, Ord k) =>
  [(k, v)] ->
  KVVector kv vv (k, v)
fromList :: forall (kv :: * -> *) k (vv :: * -> *) v.
(Vector kv k, Vector vv v, Ord k) =>
[(k, v)] -> KVVector kv vv (k, v)
fromList [(k, v)]
xs = forall (v :: * -> *) a.
Vector v a =>
(forall s. ST s (Mutable v s a)) -> v a
VG.create forall a b. (a -> b) -> a -> b
$ do
  KVMVector (Mutable kv) (Mutable vv) s (k, v)
mv <- forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
Int -> m (v (PrimState m) a)
VGM.unsafeNew (forall (t :: * -> *) a. Foldable t => t a -> Int
Prelude.length [(k, v)]
xs)
  forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ (forall a b. [a] -> [b] -> [(a, b)]
Prelude.zip [Int
0 ..] [(k, v)]
xs) (forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry (forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
v (PrimState m) a -> Int -> a -> m ()
VGM.unsafeWrite KVMVector (Mutable kv) (Mutable vv) s (k, v)
mv))
  forall (kmv :: * -> * -> *) k (vmv :: * -> * -> *) v (m :: * -> *).
(MVector kmv k, MVector vmv v, Ord k, PrimMonad m) =>
KVMVector kmv vmv (PrimState m) (k, v) -> m ()
sortAscKVMVector KVMVector (Mutable kv) (Mutable vv) s (k, v)
mv
  forall (kmv :: * -> * -> *) k (vmv :: * -> * -> *) v (m :: * -> *).
(MVector kmv k, MVector vmv v, Eq k, PrimMonad m) =>
KVMVector kmv vmv (PrimState m) (k, v)
-> m (KVMVector kmv vmv (PrimState m) (k, v))
removeDuplicates_ KVMVector (Mutable kv) (Mutable vv) s (k, v)
mv
{-# INLINE fromList #-}

-- | Convert a possibly unsorted assoc list into a KVVector.
fromListN ::
  (VG.Vector kv k, VG.Vector vv v, Ord k) =>
  Int ->
  [(k, v)] ->
  KVVector kv vv (k, v)
fromListN :: forall (kv :: * -> *) k (vv :: * -> *) v.
(Vector kv k, Vector vv v, Ord k) =>
Int -> [(k, v)] -> KVVector kv vv (k, v)
fromListN Int
n [(k, v)]
xs = forall (v :: * -> *) a.
Vector v a =>
(forall s. ST s (Mutable v s a)) -> v a
VG.create forall a b. (a -> b) -> a -> b
$ do
  KVMVector (Mutable kv) (Mutable vv) s (k, v)
mv <- forall (v :: * -> * -> *) a (m :: * -> *).
(MVector v a, PrimMonad m) =>
[a] -> v (PrimState m) a -> m (v (PrimState m) a)
fillWithList [(k, v)]
xs forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
Int -> m (v (PrimState m) a)
VGM.unsafeNew Int
n
  forall (kmv :: * -> * -> *) k (vmv :: * -> * -> *) v (m :: * -> *).
(MVector kmv k, MVector vmv v, Ord k, PrimMonad m) =>
KVMVector kmv vmv (PrimState m) (k, v) -> m ()
sortAscKVMVector KVMVector (Mutable kv) (Mutable vv) s (k, v)
mv
  forall (kmv :: * -> * -> *) k (vmv :: * -> * -> *) v (m :: * -> *).
(MVector kmv k, MVector vmv v, Eq k, PrimMonad m) =>
KVMVector kmv vmv (PrimState m) (k, v)
-> m (KVMVector kmv vmv (PrimState m) (k, v))
removeDuplicates_ KVMVector (Mutable kv) (Mutable vv) s (k, v)
mv
{-# INLINE fromListN #-}

-- | Convert a sorted assoc list with distionct keys into a KVVector
fromDistinctAscList ::
  (VG.Vector kv k, VG.Vector vv v) =>
  [(k, v)] ->
  KVVector kv vv (k, v)
fromDistinctAscList :: forall (kv :: * -> *) k (vv :: * -> *) v.
(Vector kv k, Vector vv v) =>
[(k, v)] -> KVVector kv vv (k, v)
fromDistinctAscList [(k, v)]
xs = forall (v :: * -> *) a. Vector v a => Int -> [a] -> v a
VG.fromListN (forall (t :: * -> *) a. Foldable t => t a -> Int
Prelude.length [(k, v)]
xs) [(k, v)]
xs
-- We do not use `VG.fromList`, because we need guarantees for minimal memory
-- consumption by the vector, which growing conversion algorithm implemented in
-- vector does not provide
{-# INLINE fromDistinctAscList #-}

-- | Convert a sorted assoc list with distionct keys into a KVVector. Length
-- must be supplied.
fromDistinctAscListN ::
  (VG.Vector kv k, VG.Vector vv v) =>
  Int ->
  [(k, v)] ->
  KVVector kv vv (k, v)
fromDistinctAscListN :: forall (kv :: * -> *) k (vv :: * -> *) v.
(Vector kv k, Vector vv v) =>
Int -> [(k, v)] -> KVVector kv vv (k, v)
fromDistinctAscListN = forall (v :: * -> *) a. Vector v a => Int -> [a] -> v a
VG.fromListN
{-# INLINE fromDistinctAscListN #-}

-- | Convert a sorted assoc list into a KVVector
fromAscList ::
  (Eq k, VG.Vector kv k, VG.Vector vv v) =>
  [(k, v)] ->
  KVVector kv vv (k, v)
fromAscList :: forall k (kv :: * -> *) (vv :: * -> *) v.
(Eq k, Vector kv k, Vector vv v) =>
[(k, v)] -> KVVector kv vv (k, v)
fromAscList [(k, v)]
xs = forall k (kv :: * -> *) (vv :: * -> *) v.
(Eq k, Vector kv k, Vector vv v) =>
Int -> [(k, v)] -> KVVector kv vv (k, v)
fromAscListN (forall (t :: * -> *) a. Foldable t => t a -> Int
Prelude.length [(k, v)]
xs) [(k, v)]
xs
{-# INLINE fromAscList #-}

-- | Convert a sorted assoc list into a KVVector
fromAscListN ::
  (Eq k, VG.Vector kv k, VG.Vector vv v) =>
  Int ->
  [(k, v)] ->
  KVVector kv vv (k, v)
fromAscListN :: forall k (kv :: * -> *) (vv :: * -> *) v.
(Eq k, Vector kv k, Vector vv v) =>
Int -> [(k, v)] -> KVVector kv vv (k, v)
fromAscListN Int
n = forall k (kv :: * -> *) (vv :: * -> *) v.
(Eq k, Vector kv k, Vector vv v) =>
Int -> (k -> v -> v -> v) -> [(k, v)] -> KVVector kv vv (k, v)
fromAscListWithKeyN Int
n forall k v. k -> v -> v -> v
selectDuplicate
{-# INLINE fromAscListN #-}

-- | Fill a mutable vector with elements from the list, slicing the vector if
-- the list too short.
fillWithList ::
  (VGM.MVector v a, PrimMonad m) =>
  [a] ->
  v (PrimState m) a ->
  m (v (PrimState m) a)
fillWithList :: forall (v :: * -> * -> *) a (m :: * -> *).
(MVector v a, PrimMonad m) =>
[a] -> v (PrimState m) a -> m (v (PrimState m) a)
fillWithList [a]
zs v (PrimState m) a
mv = Int -> [a] -> m (v (PrimState m) a)
go Int
0 [a]
zs
  where
    n :: Int
n = forall (v :: * -> * -> *) a s. MVector v a => v s a -> Int
VGM.length v (PrimState m) a
mv
    go :: Int -> [a] -> m (v (PrimState m) a)
go Int
i [a]
ys
      | Int
i forall a. Eq a => a -> a -> Bool
== Int
n = forall (f :: * -> *) a. Applicative f => a -> f a
pure v (PrimState m) a
mv
      | a
x : [a]
xs <- [a]
ys = forall (m :: * -> *) (v :: * -> * -> *) a.
(HasCallStack, PrimMonad m, MVector v a) =>
v (PrimState m) a -> Int -> a -> m ()
VGM.write v (PrimState m) a
mv Int
i a
x forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Int -> [a] -> m (v (PrimState m) a)
go (Int
i forall a. Num a => a -> a -> a
+ Int
1) [a]
xs
      | Bool
otherwise = forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ forall (v :: * -> * -> *) a s.
(HasCallStack, MVector v a) =>
Int -> Int -> v s a -> v s a
VGM.slice Int
0 Int
i v (PrimState m) a
mv
{-# INLINE fillWithList #-}

fromAscListWithKey ::
  (Eq k, VG.Vector kv k, VG.Vector vv v) =>
  (k -> v -> v -> v) ->
  [(k, v)] ->
  KVVector kv vv (k, v)
fromAscListWithKey :: forall k (kv :: * -> *) (vv :: * -> *) v.
(Eq k, Vector kv k, Vector vv v) =>
(k -> v -> v -> v) -> [(k, v)] -> KVVector kv vv (k, v)
fromAscListWithKey k -> v -> v -> v
f [(k, v)]
xs = forall k (kv :: * -> *) (vv :: * -> *) v.
(Eq k, Vector kv k, Vector vv v) =>
Int -> (k -> v -> v -> v) -> [(k, v)] -> KVVector kv vv (k, v)
fromAscListWithKeyN (forall (t :: * -> *) a. Foldable t => t a -> Int
length [(k, v)]
xs) k -> v -> v -> v
f [(k, v)]
xs
{-# INLINE fromAscListWithKey #-}

fromAscListWithKeyN ::
  (Eq k, VG.Vector kv k, VG.Vector vv v) =>
  Int ->
  (k -> v -> v -> v) ->
  [(k, v)] ->
  KVVector kv vv (k, v)
fromAscListWithKeyN :: forall k (kv :: * -> *) (vv :: * -> *) v.
(Eq k, Vector kv k, Vector vv v) =>
Int -> (k -> v -> v -> v) -> [(k, v)] -> KVVector kv vv (k, v)
fromAscListWithKeyN Int
n k -> v -> v -> v
f [(k, v)]
xs
  | Int
n forall a. Ord a => a -> a -> Bool
<= Int
0 = forall (v :: * -> *) a. Vector v a => v a
VG.empty
  | Bool
otherwise = forall (v :: * -> *) a.
Vector v a =>
(forall s. ST s (Mutable v s a)) -> v a
VG.create forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
Int -> m (v (PrimState m) a)
VGM.unsafeNew Int
n forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= forall (v :: * -> * -> *) a (m :: * -> *).
(MVector v a, PrimMonad m) =>
[a] -> v (PrimState m) a -> m (v (PrimState m) a)
fillWithList [(k, v)]
xs forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= forall (kmv :: * -> * -> *) k (vmv :: * -> * -> *) v (m :: * -> *).
(MVector kmv k, MVector vmv v, Eq k, PrimMonad m) =>
(k -> v -> v -> v)
-> KVMVector kmv vmv (PrimState m) (k, v)
-> m (KVMVector kmv vmv (PrimState m) (k, v))
removeDuplicates k -> v -> v -> v
f
{-# INLINE fromAscListWithKeyN #-}

mapValsKVVector ::
  (VG.Vector vv a, VG.Vector vv b) =>
  (a -> b) ->
  KVVector kv vv (k, a) ->
  KVVector kv vv (k, b)
mapValsKVVector :: forall (vv :: * -> *) a b (kv :: * -> *) k.
(Vector vv a, Vector vv b) =>
(a -> b) -> KVVector kv vv (k, a) -> KVVector kv vv (k, b)
mapValsKVVector a -> b
f KVVector kv vv (k, a)
vec =
  KVVector {keysVector :: kv (Key (k, b))
keysVector = forall (kv :: * -> *) (vv :: * -> *) a.
KVVector kv vv a -> kv (Key a)
keysVector KVVector kv vv (k, a)
vec, valsVector :: vv (Value (k, b))
valsVector = forall (v :: * -> *) a b.
(Vector v a, Vector v b) =>
(a -> b) -> v a -> v b
VG.map a -> b
f (forall (kv :: * -> *) (vv :: * -> *) a.
KVVector kv vv a -> vv (Value a)
valsVector KVVector kv vv (k, a)
vec)}
{-# INLINE mapValsKVVector #-}

mapWithKeyKVVector ::
  (VG.Vector kv k, VG.Vector vv a, VG.Vector vv b) =>
  (k -> a -> b) ->
  KVVector kv vv (k, a) ->
  KVVector kv vv (k, b)
mapWithKeyKVVector :: forall (kv :: * -> *) k (vv :: * -> *) a b.
(Vector kv k, Vector vv a, Vector vv b) =>
(k -> a -> b) -> KVVector kv vv (k, a) -> KVVector kv vv (k, b)
mapWithKeyKVVector k -> a -> b
f KVVector {kv (Key (k, a))
vv (Value (k, a))
valsVector :: vv (Value (k, a))
keysVector :: kv (Key (k, a))
valsVector :: forall (kv :: * -> *) (vv :: * -> *) a.
KVVector kv vv a -> vv (Value a)
keysVector :: forall (kv :: * -> *) (vv :: * -> *) a.
KVVector kv vv a -> kv (Key a)
..} =
  KVVector
    { keysVector :: kv (Key (k, b))
keysVector = kv (Key (k, a))
keysVector
    , valsVector :: vv (Value (k, b))
valsVector = forall (v :: * -> *) a b.
(Vector v a, Vector v b) =>
(Int -> a -> b) -> v a -> v b
VG.imap (\Int
i -> k -> a -> b
f (kv (Key (k, a))
keysVector forall (v :: * -> *) a.
(HasCallStack, Vector v a) =>
v a -> Int -> a
VG.! Int
i)) vv (Value (k, a))
valsVector
    }
{-# INLINE mapWithKeyKVVector #-}

internKVVectorMaybe :: (VG.Vector kv k, Ord k) => k -> KVVector kv vv (k, v) -> Maybe k
internKVVectorMaybe :: forall (kv :: * -> *) k (vv :: * -> *) v.
(Vector kv k, Ord k) =>
k -> KVVector kv vv (k, v) -> Maybe k
internKVVectorMaybe k
key (KVVector kv (Key (k, v))
keys vv (Value (k, v))
_values) =
  forall (v :: * -> *) a (m :: * -> *).
(HasCallStack, Vector v a, Monad m) =>
v a -> Int -> m a
VG.indexM kv (Key (k, v))
keys forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< forall (kv :: * -> *) k.
(Vector kv k, Ord k) =>
k -> kv k -> Maybe Int
lookupIxSortedVector k
key kv (Key (k, v))
keys
{-# INLINE internKVVectorMaybe #-}

-- | Look up a value by the key in a __sorted__ key/value vector. Ensure it is
-- sorted otherwise terrible things happen.
lookupKVVector ::
  (Ord k, VG.Vector kv k, VG.Vector vv v) => k -> KVVector kv vv (k, v) -> Maybe v
lookupKVVector :: forall k (kv :: * -> *) (vv :: * -> *) v.
(Ord k, Vector kv k, Vector vv v) =>
k -> KVVector kv vv (k, v) -> Maybe v
lookupKVVector k
key (KVVector kv (Key (k, v))
keys vv (Value (k, v))
values) =
  forall (v :: * -> *) a (m :: * -> *).
(HasCallStack, Vector v a, Monad m) =>
v a -> Int -> m a
VG.indexM vv (Value (k, v))
values forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< forall (kv :: * -> *) k.
(Vector kv k, Ord k) =>
k -> kv k -> Maybe Int
lookupIxSortedVector k
key kv (Key (k, v))
keys
{-# INLINE lookupKVVector #-}

-- | Look up a value by the key in a __sorted__ key/value vector. Ensure it is
-- sorted otherwise terrible things happen.
lookupDefaultKVVector ::
  (Ord k, VG.Vector kv k, VG.Vector vv v) => v -> k -> KVVector kv vv (k, v) -> v
lookupDefaultKVVector :: forall k (kv :: * -> *) (vv :: * -> *) v.
(Ord k, Vector kv k, Vector vv v) =>
v -> k -> KVVector kv vv (k, v) -> v
lookupDefaultKVVector v
v k
k = forall a. a -> Maybe a -> a
fromMaybe v
v forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall k (kv :: * -> *) (vv :: * -> *) v.
(Ord k, Vector kv k, Vector vv v) =>
k -> KVVector kv vv (k, v) -> Maybe v
lookupKVVector k
k
{-# INLINE lookupDefaultKVVector #-}

memberKVVector :: (Ord k, VG.Vector kv k) => k -> KVVector kv vv (k, v) -> Bool
memberKVVector :: forall k (kv :: * -> *) (vv :: * -> *) v.
(Ord k, Vector kv k) =>
k -> KVVector kv vv (k, v) -> Bool
memberKVVector k
k = forall a. Maybe a -> Bool
isJust forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (kv :: * -> *) k.
(Vector kv k, Ord k) =>
k -> kv k -> Maybe Int
lookupIxSortedVector k
k forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (kv :: * -> *) (vv :: * -> *) a.
KVVector kv vv a -> kv (Key a)
keysVector
{-# INLINE memberKVVector #-}

-- | Perform a binary search on a sorted vector
lookupIxSortedVector ::
  (VG.Vector kv k, Ord k) => k -> kv k -> Maybe Int
lookupIxSortedVector :: forall (kv :: * -> *) k.
(Vector kv k, Ord k) =>
k -> kv k -> Maybe Int
lookupIxSortedVector k
key kv k
keys = Int -> Int -> Maybe Int
go Int
0 (forall (v :: * -> *) a. Vector v a => v a -> Int
VG.length kv k
keys)
  where
    go :: Int -> Int -> Maybe Int
go !Int
l !Int
u = do
      forall (f :: * -> *). Alternative f => Bool -> f ()
guard (Int
l forall a. Ord a => a -> a -> Bool
< Int
u)
      let !i :: Int
i = ((Int
u forall a. Num a => a -> a -> a
- Int
l) forall a. Integral a => a -> a -> a
`div` Int
2) forall a. Num a => a -> a -> a
+ Int
l
      case forall a. Ord a => a -> a -> Ordering
compare k
key (kv k
keys forall (v :: * -> *) a.
(HasCallStack, Vector v a) =>
v a -> Int -> a
VG.! Int
i) of
        Ordering
LT -> Int -> Int -> Maybe Int
go Int
l Int
i
        Ordering
GT -> Int -> Int -> Maybe Int
go (Int
i forall a. Num a => a -> a -> a
+ Int
1) Int
u
        Ordering
EQ -> forall a. a -> Maybe a
Just Int
i
{-# INLINE lookupIxSortedVector #-}

sortAscKVMVector ::
  (VGM.MVector kmv k, VGM.MVector vmv v, Ord k, PrimMonad m) =>
  KVMVector kmv vmv (PrimState m) (k, v) ->
  m ()
sortAscKVMVector :: forall (kmv :: * -> * -> *) k (vmv :: * -> * -> *) v (m :: * -> *).
(MVector kmv k, MVector vmv v, Ord k, PrimMonad m) =>
KVMVector kmv vmv (PrimState m) (k, v) -> m ()
sortAscKVMVector = forall (m :: * -> *) (v :: * -> * -> *) e.
(PrimMonad m, MVector v e) =>
Comparison e -> v (PrimState m) e -> m ()
sortBy (\(k
k1, v
_) (k
k2, v
_) -> forall a. Ord a => a -> a -> Ordering
compare k
k1 k
k2)
{-# INLINE sortAscKVMVector #-}

selectDuplicate :: k -> v -> v -> v
selectDuplicate :: forall k v. k -> v -> v -> v
selectDuplicate k
_ v
v v
_ = v
v

removeDuplicates_ ::
  (VGM.MVector kmv k, VGM.MVector vmv v, Eq k, PrimMonad m) =>
  KVMVector kmv vmv (PrimState m) (k, v) ->
  m (KVMVector kmv vmv (PrimState m) (k, v))
removeDuplicates_ :: forall (kmv :: * -> * -> *) k (vmv :: * -> * -> *) v (m :: * -> *).
(MVector kmv k, MVector vmv v, Eq k, PrimMonad m) =>
KVMVector kmv vmv (PrimState m) (k, v)
-> m (KVMVector kmv vmv (PrimState m) (k, v))
removeDuplicates_ = forall (kmv :: * -> * -> *) k (vmv :: * -> * -> *) v (m :: * -> *).
(MVector kmv k, MVector vmv v, Eq k, PrimMonad m) =>
(k -> v -> v -> v)
-> KVMVector kmv vmv (PrimState m) (k, v)
-> m (KVMVector kmv vmv (PrimState m) (k, v))
removeDuplicates forall k v. k -> v -> v -> v
selectDuplicate
{-# INLINE removeDuplicates_ #-}

removeDuplicates ::
  (VGM.MVector kmv k, VGM.MVector vmv v, Eq k, PrimMonad m) =>
  (k -> v -> v -> v) ->
  KVMVector kmv vmv (PrimState m) (k, v) ->
  m (KVMVector kmv vmv (PrimState m) (k, v))
removeDuplicates :: forall (kmv :: * -> * -> *) k (vmv :: * -> * -> *) v (m :: * -> *).
(MVector kmv k, MVector vmv v, Eq k, PrimMonad m) =>
(k -> v -> v -> v)
-> KVMVector kmv vmv (PrimState m) (k, v)
-> m (KVMVector kmv vmv (PrimState m) (k, v))
removeDuplicates k -> v -> v -> v
f KVMVector kmv vmv (PrimState m) (k, v)
mv
  | forall (v :: * -> * -> *) a s. MVector v a => v s a -> Bool
VGM.null KVMVector kmv vmv (PrimState m) (k, v)
mv = forall (f :: * -> *) a. Applicative f => a -> f a
pure KVMVector kmv vmv (PrimState m) (k, v)
mv
  | Bool
otherwise = do
      let n :: Int
n = forall (v :: * -> * -> *) a s. MVector v a => v s a -> Int
VGM.length KVMVector kmv vmv (PrimState m) (k, v)
mv
          goMoved :: Int -> (k, v) -> Int -> m (KVMVector kmv vmv (PrimState m) (k, v))
goMoved Int
lastIx prev :: (k, v)
prev@(k
pk, v
pv) Int
curIx = do
            forall (m :: * -> *) (v :: * -> * -> *) a.
(HasCallStack, PrimMonad m, MVector v a) =>
v (PrimState m) a -> Int -> a -> m ()
VGM.write KVMVector kmv vmv (PrimState m) (k, v)
mv Int
lastIx (k, v)
prev
            if Int
curIx forall a. Ord a => a -> a -> Bool
< Int
n
              then do
                cur :: (k, v)
cur@(k
ck, v
cv) <- forall (m :: * -> *) (v :: * -> * -> *) a.
(HasCallStack, PrimMonad m, MVector v a) =>
v (PrimState m) a -> Int -> m a
VGM.read KVMVector kmv vmv (PrimState m) (k, v)
mv Int
curIx
                if k
ck forall a. Eq a => a -> a -> Bool
== k
pk
                  then Int -> (k, v) -> Int -> m (KVMVector kmv vmv (PrimState m) (k, v))
goMoved Int
lastIx (k
ck, k -> v -> v -> v
f k
ck v
cv v
pv) (Int
curIx forall a. Num a => a -> a -> a
+ Int
1)
                  else Int -> (k, v) -> Int -> m (KVMVector kmv vmv (PrimState m) (k, v))
goMoved (Int
lastIx forall a. Num a => a -> a -> a
+ Int
1) (k, v)
cur (Int
curIx forall a. Num a => a -> a -> a
+ Int
1)
              else forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ forall (v :: * -> * -> *) a s.
(HasCallStack, MVector v a) =>
Int -> Int -> v s a -> v s a
VGM.slice Int
0 (Int
lastIx forall a. Num a => a -> a -> a
+ Int
1) KVMVector kmv vmv (PrimState m) (k, v)
mv
          goUnmoved :: (k, v) -> Int -> m (KVMVector kmv vmv (PrimState m) (k, v))
goUnmoved (k
pk, v
pv) Int
curIx
            | Int
curIx forall a. Ord a => a -> a -> Bool
< Int
n = do
                cur :: (k, v)
cur@(k
ck, v
cv) <- forall (m :: * -> *) (v :: * -> * -> *) a.
(HasCallStack, PrimMonad m, MVector v a) =>
v (PrimState m) a -> Int -> m a
VGM.read KVMVector kmv vmv (PrimState m) (k, v)
mv Int
curIx
                if k
ck forall a. Eq a => a -> a -> Bool
== k
pk
                  then Int -> (k, v) -> Int -> m (KVMVector kmv vmv (PrimState m) (k, v))
goMoved (Int
curIx forall a. Num a => a -> a -> a
- Int
1) (k
ck, k -> v -> v -> v
f k
ck v
cv v
pv) (Int
curIx forall a. Num a => a -> a -> a
+ Int
1)
                  else (k, v) -> Int -> m (KVMVector kmv vmv (PrimState m) (k, v))
goUnmoved (k, v)
cur (Int
curIx forall a. Num a => a -> a -> a
+ Int
1)
            | Bool
otherwise = forall (f :: * -> *) a. Applicative f => a -> f a
pure KVMVector kmv vmv (PrimState m) (k, v)
mv
      (k, v)
x0 <- forall (m :: * -> *) (v :: * -> * -> *) a.
(HasCallStack, PrimMonad m, MVector v a) =>
v (PrimState m) a -> Int -> m a
VGM.read KVMVector kmv vmv (PrimState m) (k, v)
mv Int
0
      (k, v) -> Int -> m (KVMVector kmv vmv (PrimState m) (k, v))
goUnmoved (k, v)
x0 Int
1
{-# INLINE removeDuplicates #-}

normalize ::
  (VG.Vector kv k, VG.Vector vv v, Ord k) =>
  KVVector kv vv (k, v) ->
  KVVector kv vv (k, v)
normalize :: forall (kv :: * -> *) k (vv :: * -> *) v.
(Vector kv k, Vector vv v, Ord k) =>
KVVector kv vv (k, v) -> KVVector kv vv (k, v)
normalize KVVector kv vv (k, v)
v = forall a. (forall s. ST s a) -> a
runST forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) (v :: * -> *) a.
(PrimMonad m, Vector v a) =>
v a -> m (Mutable v (PrimState m) a)
VG.thaw KVVector kv vv (k, v)
v forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= forall k (m :: * -> *) (kv :: * -> *) (vv :: * -> *) v.
(Ord k, PrimMonad m, Vector kv k, Vector vv v) =>
KVMVector (Mutable kv) (Mutable vv) (PrimState m) (k, v)
-> m (KVMVector (Mutable kv) (Mutable vv) (PrimState m) (k, v))
normalizeM forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= forall (m :: * -> *) (v :: * -> *) a.
(PrimMonad m, Vector v a) =>
Mutable v (PrimState m) a -> m (v a)
VG.unsafeFreeze
{-# INLINE normalize #-}

normalizeM ::
  (Ord k, PrimMonad m, VG.Vector kv k, VG.Vector vv v) =>
  KVMVector (VG.Mutable kv) (VG.Mutable vv) (PrimState m) (k, v) ->
  m (KVMVector (VG.Mutable kv) (VG.Mutable vv) (PrimState m) (k, v))
normalizeM :: forall k (m :: * -> *) (kv :: * -> *) (vv :: * -> *) v.
(Ord k, PrimMonad m, Vector kv k, Vector vv v) =>
KVMVector (Mutable kv) (Mutable vv) (PrimState m) (k, v)
-> m (KVMVector (Mutable kv) (Mutable vv) (PrimState m) (k, v))
normalizeM KVMVector (Mutable kv) (Mutable vv) (PrimState m) (k, v)
mv = forall (kmv :: * -> * -> *) k (vmv :: * -> * -> *) v (m :: * -> *).
(MVector kmv k, MVector vmv v, Ord k, PrimMonad m) =>
KVMVector kmv vmv (PrimState m) (k, v) -> m ()
sortAscKVMVector KVMVector (Mutable kv) (Mutable vv) (PrimState m) (k, v)
mv forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> forall (kmv :: * -> * -> *) k (vmv :: * -> * -> *) v (m :: * -> *).
(MVector kmv k, MVector vmv v, Eq k, PrimMonad m) =>
KVMVector kmv vmv (PrimState m) (k, v)
-> m (KVMVector kmv vmv (PrimState m) (k, v))
removeDuplicates_ KVMVector (Mutable kv) (Mutable vv) (PrimState m) (k, v)
mv
{-# INLINE normalizeM #-}

instance (VG.Vector kv k, VG.Vector vv v, Ord k) => Semigroup (KVVector kv vv (k, v)) where
  <> :: KVVector kv vv (k, v)
-> KVVector kv vv (k, v) -> KVVector kv vv (k, v)
(<>) KVVector kv vv (k, v)
v1 KVVector kv vv (k, v)
v2 = forall (kv :: * -> *) k (vv :: * -> *) v.
(Vector kv k, Vector vv v, Ord k) =>
KVVector kv vv (k, v) -> KVVector kv vv (k, v)
normalize (KVVector kv vv (k, v)
v1 forall (v :: * -> *) a. Vector v a => v a -> v a -> v a
VG.++ KVVector kv vv (k, v)
v2)
  {-# INLINE (<>) #-}
  sconcat :: NonEmpty (KVVector kv vv (k, v)) -> KVVector kv vv (k, v)
sconcat = forall (kv :: * -> *) k (vv :: * -> *) v.
(Vector kv k, Vector vv v, Ord k) =>
KVVector kv vv (k, v) -> KVVector kv vv (k, v)
normalize forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (v :: * -> *) a. Vector v a => [v a] -> v a
VG.concat forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. NonEmpty a -> [a]
NE.toList
  {-# INLINE sconcat #-}

instance (VG.Vector kv k, VG.Vector vv v, Ord k) => Monoid (KVVector kv vv (k, v)) where
  mempty :: KVVector kv vv (k, v)
mempty = forall (v :: * -> *) a. Vector v a => v a
VG.empty
  mconcat :: [KVVector kv vv (k, v)] -> KVVector kv vv (k, v)
mconcat = forall (kv :: * -> *) k (vv :: * -> *) v.
(Vector kv k, Vector vv v, Ord k) =>
KVVector kv vv (k, v) -> KVVector kv vv (k, v)
normalize forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (v :: * -> *) a. Vector v a => [v a] -> v a
VG.concat
  {-# INLINE mconcat #-}

type family Key e :: Type where
  Key (k, v) = k

type family Value e :: Type where
  Value (k, v) = v

data KVVector kv vv a = KVVector
  { forall (kv :: * -> *) (vv :: * -> *) a.
KVVector kv vv a -> kv (Key a)
keysVector :: !(kv (Key a))
  , forall (kv :: * -> *) (vv :: * -> *) a.
KVVector kv vv a -> vv (Value a)
valsVector :: !(vv (Value a))
  }
  deriving (forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall (kv :: * -> *) (vv :: * -> *) a x.
Rep (KVVector kv vv a) x -> KVVector kv vv a
forall (kv :: * -> *) (vv :: * -> *) a x.
KVVector kv vv a -> Rep (KVVector kv vv a) x
$cto :: forall (kv :: * -> *) (vv :: * -> *) a x.
Rep (KVVector kv vv a) x -> KVVector kv vv a
$cfrom :: forall (kv :: * -> *) (vv :: * -> *) a x.
KVVector kv vv a -> Rep (KVVector kv vv a) x
Generic)

instance (VG.Vector kv k, VG.Vector vv v, Ord k) => Exts.IsList (KVVector kv vv (k, v)) where
  type Item (KVVector kv vv (k, v)) = (k, v)
  fromList :: [Item (KVVector kv vv (k, v))] -> KVVector kv vv (k, v)
fromList = forall (kv :: * -> *) k (vv :: * -> *) v.
(Vector kv k, Vector vv v, Ord k) =>
[(k, v)] -> KVVector kv vv (k, v)
fromList
  {-# INLINE fromList #-}
  fromListN :: Int -> [Item (KVVector kv vv (k, v))] -> KVVector kv vv (k, v)
fromListN = forall (kv :: * -> *) k (vv :: * -> *) v.
(Vector kv k, Vector vv v, Ord k) =>
Int -> [(k, v)] -> KVVector kv vv (k, v)
fromListN
  {-# INLINE fromListN #-}
  toList :: KVVector kv vv (k, v) -> [Item (KVVector kv vv (k, v))]
toList = forall (v :: * -> *) a. Vector v a => v a -> [a]
VG.toList
  {-# INLINE toList #-}

deriving instance (Eq (kv k), Eq (vv v)) => Eq (KVVector kv vv (k, v))

deriving instance (Show (kv k), Show (vv v)) => Show (KVVector kv vv (k, v))

data KVMVector kmv vmv s a = KVMVector
  { forall (kmv :: * -> * -> *) (vmv :: * -> * -> *) s a.
KVMVector kmv vmv s a -> kmv s (Key a)
_keysMVector :: !(kmv s (Key a))
  , forall (kmv :: * -> * -> *) (vmv :: * -> * -> *) s a.
KVMVector kmv vmv s a -> vmv s (Value a)
_valsMVector :: !(vmv s (Value a))
  }

type instance VG.Mutable (KVVector kv vv) = KVMVector (VG.Mutable kv) (VG.Mutable vv)

instance (NFData (kv k), (NFData (vv v))) => NFData (KVVector kv vv (k, v)) where
  rnf :: KVVector kv vv (k, v) -> ()
rnf KVVector {kv (Key (k, v))
vv (Value (k, v))
valsVector :: vv (Value (k, v))
keysVector :: kv (Key (k, v))
valsVector :: forall (kv :: * -> *) (vv :: * -> *) a.
KVVector kv vv a -> vv (Value a)
keysVector :: forall (kv :: * -> *) (vv :: * -> *) a.
KVVector kv vv a -> kv (Key a)
..} = kv (Key (k, v))
keysVector forall a b. NFData a => a -> b -> b
`deepseq` vv (Value (k, v))
valsVector forall a b. NFData a => a -> b -> b
`deepseq` ()

instance (VG.Vector kv k, VG.Vector vv v) => VG.Vector (KVVector kv vv) (k, v) where
  {-# INLINE basicUnsafeFreeze #-}
  basicUnsafeFreeze :: forall s.
Mutable (KVVector kv vv) s (k, v) -> ST s (KVVector kv vv (k, v))
basicUnsafeFreeze (KVMVector Mutable kv s (Key (k, v))
kmv Mutable vv s (Value (k, v))
vmv) =
    forall (kv :: * -> *) (vv :: * -> *) a.
kv (Key a) -> vv (Value a) -> KVVector kv vv a
KVVector forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (v :: * -> *) a s. Vector v a => Mutable v s a -> ST s (v a)
VG.basicUnsafeFreeze Mutable kv s (Key (k, v))
kmv forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall (v :: * -> *) a s. Vector v a => Mutable v s a -> ST s (v a)
VG.basicUnsafeFreeze Mutable vv s (Value (k, v))
vmv

  {-# INLINE basicUnsafeThaw #-}
  basicUnsafeThaw :: forall s.
KVVector kv vv (k, v) -> ST s (Mutable (KVVector kv vv) s (k, v))
basicUnsafeThaw (KVVector kv (Key (k, v))
kv vv (Value (k, v))
vv) = forall (kmv :: * -> * -> *) (vmv :: * -> * -> *) s a.
kmv s (Key a) -> vmv s (Value a) -> KVMVector kmv vmv s a
KVMVector forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (v :: * -> *) a s. Vector v a => v a -> ST s (Mutable v s a)
VG.basicUnsafeThaw kv (Key (k, v))
kv forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall (v :: * -> *) a s. Vector v a => v a -> ST s (Mutable v s a)
VG.basicUnsafeThaw vv (Value (k, v))
vv

  {-# INLINE basicLength #-}
  -- ignore length on values, it assumed to match vector with keys
  basicLength :: KVVector kv vv (k, v) -> Int
basicLength (KVVector kv (Key (k, v))
kv vv (Value (k, v))
_) = forall (v :: * -> *) a. Vector v a => v a -> Int
VG.basicLength kv (Key (k, v))
kv

  {-# INLINE basicUnsafeSlice #-}
  basicUnsafeSlice :: Int -> Int -> KVVector kv vv (k, v) -> KVVector kv vv (k, v)
basicUnsafeSlice Int
i Int
n (KVVector kv (Key (k, v))
kv vv (Value (k, v))
vv) =
    forall (kv :: * -> *) (vv :: * -> *) a.
kv (Key a) -> vv (Value a) -> KVVector kv vv a
KVVector (forall (v :: * -> *) a. Vector v a => Int -> Int -> v a -> v a
VG.basicUnsafeSlice Int
i Int
n kv (Key (k, v))
kv) (forall (v :: * -> *) a. Vector v a => Int -> Int -> v a -> v a
VG.basicUnsafeSlice Int
i Int
n vv (Value (k, v))
vv)

  {-# INLINE basicUnsafeIndexM #-}
  basicUnsafeIndexM :: KVVector kv vv (k, v) -> Int -> Box (k, v)
basicUnsafeIndexM (KVVector kv (Key (k, v))
kv vv (Value (k, v))
vv) Int
i = do
    k
k <- forall (v :: * -> *) a. Vector v a => v a -> Int -> Box a
VG.basicUnsafeIndexM kv (Key (k, v))
kv Int
i
    v
v <- forall (v :: * -> *) a. Vector v a => v a -> Int -> Box a
VG.basicUnsafeIndexM vv (Value (k, v))
vv Int
i
    forall (f :: * -> *) a. Applicative f => a -> f a
pure (k
k, v
v)

  {-# INLINE basicUnsafeCopy #-}
  basicUnsafeCopy :: forall s.
Mutable (KVVector kv vv) s (k, v)
-> KVVector kv vv (k, v) -> ST s ()
basicUnsafeCopy (KVMVector Mutable kv s (Key (k, v))
kvDst Mutable vv s (Value (k, v))
vvDst) (KVVector kv (Key (k, v))
kvSrc vv (Value (k, v))
vvSrc) =
    forall (v :: * -> *) a s.
Vector v a =>
Mutable v s a -> v a -> ST s ()
VG.basicUnsafeCopy Mutable kv s (Key (k, v))
kvDst kv (Key (k, v))
kvSrc forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> forall (v :: * -> *) a s.
Vector v a =>
Mutable v s a -> v a -> ST s ()
VG.basicUnsafeCopy Mutable vv s (Value (k, v))
vvDst vv (Value (k, v))
vvSrc

instance (VGM.MVector kmv k, VGM.MVector vmv v) => VGM.MVector (KVMVector kmv vmv) (k, v) where
  {-# INLINE basicLength #-}
  -- ignore length on values, it assumed to match vector with keys
  basicLength :: forall s. KVMVector kmv vmv s (k, v) -> Int
basicLength (KVMVector kmv s (Key (k, v))
kmv vmv s (Value (k, v))
_) = forall (v :: * -> * -> *) a s. MVector v a => v s a -> Int
VGM.basicLength kmv s (Key (k, v))
kmv

  {-# INLINE basicUnsafeSlice #-}
  basicUnsafeSlice :: forall s.
Int
-> Int -> KVMVector kmv vmv s (k, v) -> KVMVector kmv vmv s (k, v)
basicUnsafeSlice Int
j Int
m (KVMVector kmv s (Key (k, v))
kmv vmv s (Value (k, v))
vmv) =
    forall (kmv :: * -> * -> *) (vmv :: * -> * -> *) s a.
kmv s (Key a) -> vmv s (Value a) -> KVMVector kmv vmv s a
KVMVector (forall (v :: * -> * -> *) a s.
MVector v a =>
Int -> Int -> v s a -> v s a
VGM.basicUnsafeSlice Int
j Int
m kmv s (Key (k, v))
kmv) (forall (v :: * -> * -> *) a s.
MVector v a =>
Int -> Int -> v s a -> v s a
VGM.basicUnsafeSlice Int
j Int
m vmv s (Value (k, v))
vmv)

  {-# INLINE basicOverlaps #-}
  basicOverlaps :: forall s.
KVMVector kmv vmv s (k, v) -> KVMVector kmv vmv s (k, v) -> Bool
basicOverlaps (KVMVector kmv s (Key (k, v))
kmv1 vmv s (Value (k, v))
vmv1) (KVMVector kmv s (Key (k, v))
kmv2 vmv s (Value (k, v))
vmv2) =
    forall (v :: * -> * -> *) a s.
MVector v a =>
v s a -> v s a -> Bool
VGM.basicOverlaps kmv s (Key (k, v))
kmv1 kmv s (Key (k, v))
kmv2 Bool -> Bool -> Bool
&& forall (v :: * -> * -> *) a s.
MVector v a =>
v s a -> v s a -> Bool
VGM.basicOverlaps vmv s (Value (k, v))
vmv1 vmv s (Value (k, v))
vmv2

  {-# INLINE basicUnsafeNew #-}
  basicUnsafeNew :: forall s. Int -> ST s (KVMVector kmv vmv s (k, v))
basicUnsafeNew Int
n = do
    kmv s k
kmv1 <- forall (v :: * -> * -> *) a s. MVector v a => Int -> ST s (v s a)
VGM.basicUnsafeNew Int
n
    vmv s v
vmv1 <- forall (v :: * -> * -> *) a s. MVector v a => Int -> ST s (v s a)
VGM.basicUnsafeNew Int
n
    forall (m :: * -> *) a. Monad m => a -> m a
return (forall (kmv :: * -> * -> *) (vmv :: * -> * -> *) s a.
kmv s (Key a) -> vmv s (Value a) -> KVMVector kmv vmv s a
KVMVector kmv s k
kmv1 vmv s v
vmv1)

  {-# INLINE basicInitialize #-}
  basicInitialize :: forall s. KVMVector kmv vmv s (k, v) -> ST s ()
basicInitialize (KVMVector kmv s (Key (k, v))
kmv vmv s (Value (k, v))
vmv) = forall (v :: * -> * -> *) a s. MVector v a => v s a -> ST s ()
VGM.basicInitialize kmv s (Key (k, v))
kmv forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> forall (v :: * -> * -> *) a s. MVector v a => v s a -> ST s ()
VGM.basicInitialize vmv s (Value (k, v))
vmv

  {-# INLINE basicUnsafeReplicate #-}
  basicUnsafeReplicate :: forall s. Int -> (k, v) -> ST s (KVMVector kmv vmv s (k, v))
basicUnsafeReplicate Int
n !(!k
k, !v
v) =
    forall (kmv :: * -> * -> *) (vmv :: * -> * -> *) s a.
kmv s (Key a) -> vmv s (Value a) -> KVMVector kmv vmv s a
KVMVector forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (v :: * -> * -> *) a s.
MVector v a =>
Int -> a -> ST s (v s a)
VGM.basicUnsafeReplicate Int
n k
k forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall (v :: * -> * -> *) a s.
MVector v a =>
Int -> a -> ST s (v s a)
VGM.basicUnsafeReplicate Int
n v
v

  {-# INLINE basicUnsafeRead #-}
  basicUnsafeRead :: forall s. KVMVector kmv vmv s (k, v) -> Int -> ST s (k, v)
basicUnsafeRead (KVMVector kmv s (Key (k, v))
kmv vmv s (Value (k, v))
vmv) Int
i = do
    k
k <- forall (v :: * -> * -> *) a s.
MVector v a =>
v s a -> Int -> ST s a
VGM.basicUnsafeRead kmv s (Key (k, v))
kmv Int
i
    v
v <- forall (v :: * -> * -> *) a s.
MVector v a =>
v s a -> Int -> ST s a
VGM.basicUnsafeRead vmv s (Value (k, v))
vmv Int
i
    forall (f :: * -> *) a. Applicative f => a -> f a
pure (k
k, v
v)

  {-# INLINE basicUnsafeWrite #-}
  basicUnsafeWrite :: forall s. KVMVector kmv vmv s (k, v) -> Int -> (k, v) -> ST s ()
basicUnsafeWrite (KVMVector kmv s (Key (k, v))
kmv vmv s (Value (k, v))
vmv) Int
i !(!k
k, !v
v) =
    forall (v :: * -> * -> *) a s.
MVector v a =>
v s a -> Int -> a -> ST s ()
VGM.basicUnsafeWrite kmv s (Key (k, v))
kmv Int
i k
k forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> forall (v :: * -> * -> *) a s.
MVector v a =>
v s a -> Int -> a -> ST s ()
VGM.basicUnsafeWrite vmv s (Value (k, v))
vmv Int
i v
v

  {-# INLINE basicUnsafeCopy #-}
  basicUnsafeCopy :: forall s.
KVMVector kmv vmv s (k, v) -> KVMVector kmv vmv s (k, v) -> ST s ()
basicUnsafeCopy (KVMVector kmv s (Key (k, v))
kmvDst vmv s (Value (k, v))
vmvDst) (KVMVector kmv s (Key (k, v))
kmvSrc vmv s (Value (k, v))
vmvSrc) =
    forall (v :: * -> * -> *) a s.
MVector v a =>
v s a -> v s a -> ST s ()
VGM.basicUnsafeCopy kmv s (Key (k, v))
kmvDst kmv s (Key (k, v))
kmvSrc forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> forall (v :: * -> * -> *) a s.
MVector v a =>
v s a -> v s a -> ST s ()
VGM.basicUnsafeCopy vmv s (Value (k, v))
vmvDst vmv s (Value (k, v))
vmvSrc

  {-# INLINE basicUnsafeMove #-}
  basicUnsafeMove :: forall s.
KVMVector kmv vmv s (k, v) -> KVMVector kmv vmv s (k, v) -> ST s ()
basicUnsafeMove (KVMVector kmv s (Key (k, v))
kmvDst vmv s (Value (k, v))
vmvDst) (KVMVector kmv s (Key (k, v))
kmvSrc vmv s (Value (k, v))
vmvSrc) =
    forall (v :: * -> * -> *) a s.
MVector v a =>
v s a -> v s a -> ST s ()
VGM.basicUnsafeMove kmv s (Key (k, v))
kmvDst kmv s (Key (k, v))
kmvSrc forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> forall (v :: * -> * -> *) a s.
MVector v a =>
v s a -> v s a -> ST s ()
VGM.basicUnsafeMove vmv s (Value (k, v))
vmvDst vmv s (Value (k, v))
vmvSrc

  {-# INLINE basicClear #-}
  basicClear :: forall s. KVMVector kmv vmv s (k, v) -> ST s ()
basicClear (KVMVector kmv s (Key (k, v))
kmv vmv s (Value (k, v))
vmv) = forall (v :: * -> * -> *) a s. MVector v a => v s a -> ST s ()
VGM.basicClear kmv s (Key (k, v))
kmv forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> forall (v :: * -> * -> *) a s. MVector v a => v s a -> ST s ()
VGM.basicClear vmv s (Value (k, v))
vmv

instance
  ( NoThunks (kv k)
  , NoThunks (vv v)
  , Typeable kv
  , Typeable vv
  , Typeable k
  , Typeable v
  ) =>
  NoThunks (KVVector kv vv (k, v))
  where
  wNoThunks :: Context -> KVVector kv vv (k, v) -> IO (Maybe ThunkInfo)
wNoThunks Context
c (KVVector kv (Key (k, v))
kv vv (Value (k, v))
vv) = forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
(<|>) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a. NoThunks a => Context -> a -> IO (Maybe ThunkInfo)
wNoThunks Context
c kv (Key (k, v))
kv forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall a. NoThunks a => Context -> a -> IO (Maybe ThunkInfo)
wNoThunks Context
c vv (Value (k, v))
vv
  showTypeOf :: Proxy (KVVector kv vv (k, v)) -> String
showTypeOf Proxy (KVVector kv vv (k, v))
px = TypeRep -> ShowS
showsTypeRep (forall {k} (proxy :: k -> *) (a :: k).
Typeable a =>
proxy a -> TypeRep
typeRep Proxy (KVVector kv vv (k, v))
px) String
""

instance Typeable e => NoThunks (VS.Vector e) where
  wNoThunks :: Context -> Vector e -> IO (Maybe ThunkInfo)
wNoThunks Context
_ !Vector e
_ = forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a. Maybe a
Nothing
  showTypeOf :: Proxy (Vector e) -> String
showTypeOf Proxy (Vector e)
px = TypeRep -> ShowS
showsTypeRep (forall {k} (proxy :: k -> *) (a :: k).
Typeable a =>
proxy a -> TypeRep
typeRep Proxy (Vector e)
px) String
""

instance Typeable e => NoThunks (VP.Vector e) where
  wNoThunks :: Context -> Vector e -> IO (Maybe ThunkInfo)
wNoThunks Context
_ !Vector e
_ = forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a. Maybe a
Nothing
  showTypeOf :: Proxy (Vector e) -> String
showTypeOf Proxy (Vector e)
px = TypeRep -> ShowS
showsTypeRep (forall {k} (proxy :: k -> *) (a :: k).
Typeable a =>
proxy a -> TypeRep
typeRep Proxy (Vector e)
px) String
""