{-# 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 Cardano.Binary
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
toMap ::
(VG.Vector kv k, VG.Vector vv v) =>
KVVector kv vv (k, v) ->
Map.Map k v
toMap :: KVVector kv vv (k, v) -> Map k v
toMap = [(k, v)] -> Map k v
forall k a. [(k, a)] -> Map k a
Map.fromDistinctAscList ([(k, v)] -> Map k v)
-> (KVVector kv vv (k, v) -> [(k, v)])
-> KVVector kv vv (k, v)
-> Map k v
forall b c a. (b -> c) -> (a -> b) -> a -> c
. KVVector kv vv (k, v) -> [(k, v)]
forall (v :: * -> *) a. Vector v a => v a -> [a]
VG.toList
{-# INLINE toMap #-}
fromMap ::
(VG.Vector kv k, VG.Vector vv v) => Map.Map k v -> KVVector kv vv (k, v)
fromMap :: Map k v -> KVVector kv vv (k, v)
fromMap Map k v
m = Int -> [(k, v)] -> KVVector kv vv (k, v)
forall (kv :: * -> *) k (vv :: * -> *) v.
(Vector kv k, Vector vv v) =>
Int -> [(k, v)] -> KVVector kv vv (k, v)
fromDistinctAscListN (Map k v -> Int
forall k a. Map k a -> Int
Map.size Map k v
m) ([(k, v)] -> KVVector kv vv (k, v))
-> [(k, v)] -> KVVector kv vv (k, v)
forall a b. (a -> b) -> a -> b
$ Map k v -> [(k, v)]
forall k a. Map k a -> [(k, a)]
Map.toAscList Map k v
m
{-# INLINE fromMap #-}
fromList ::
(VG.Vector kv k, VG.Vector vv v, Ord k) =>
[(k, v)] ->
KVVector kv vv (k, v)
fromList :: [(k, v)] -> KVVector kv vv (k, v)
fromList [(k, v)]
xs = (forall s. ST s (Mutable (KVVector kv vv) s (k, v)))
-> KVVector kv vv (k, v)
forall (v :: * -> *) a.
Vector v a =>
(forall s. ST s (Mutable v s a)) -> v a
VG.create ((forall s. ST s (Mutable (KVVector kv vv) s (k, v)))
-> KVVector kv vv (k, v))
-> (forall s. ST s (Mutable (KVVector kv vv) s (k, v)))
-> KVVector kv vv (k, v)
forall a b. (a -> b) -> a -> b
$ do
KVMVector (Mutable kv) (Mutable vv) s (k, v)
mv <- Int
-> ST
s (KVMVector (Mutable kv) (Mutable vv) (PrimState (ST s)) (k, v))
forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
Int -> m (v (PrimState m) a)
VGM.unsafeNew ([(k, v)] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
Prelude.length [(k, v)]
xs)
[(Int, (k, v))] -> ((Int, (k, v)) -> ST s ()) -> ST s ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ ([Int] -> [(k, v)] -> [(Int, (k, v))]
forall a b. [a] -> [b] -> [(a, b)]
Prelude.zip [Int
0 ..] [(k, v)]
xs) ((Int -> (k, v) -> ST s ()) -> (Int, (k, v)) -> ST s ()
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry (KVMVector (Mutable kv) (Mutable vv) (PrimState (ST s)) (k, v)
-> Int -> (k, v) -> ST s ()
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)
KVMVector (Mutable kv) (Mutable vv) (PrimState (ST s)) (k, v)
mv))
KVMVector (Mutable kv) (Mutable vv) (PrimState (ST s)) (k, v)
-> ST s ()
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)
KVMVector (Mutable kv) (Mutable vv) (PrimState (ST s)) (k, v)
mv
KVMVector (Mutable kv) (Mutable vv) (PrimState (ST s)) (k, v)
-> ST
s (KVMVector (Mutable kv) (Mutable vv) (PrimState (ST s)) (k, v))
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)
KVMVector (Mutable kv) (Mutable vv) (PrimState (ST s)) (k, v)
mv
{-# INLINE fromList #-}
fromListN ::
(VG.Vector kv k, VG.Vector vv v, Ord k) =>
Int ->
[(k, v)] ->
KVVector kv vv (k, v)
fromListN :: Int -> [(k, v)] -> KVVector kv vv (k, v)
fromListN Int
n [(k, v)]
xs = (forall s. ST s (Mutable (KVVector kv vv) s (k, v)))
-> KVVector kv vv (k, v)
forall (v :: * -> *) a.
Vector v a =>
(forall s. ST s (Mutable v s a)) -> v a
VG.create ((forall s. ST s (Mutable (KVVector kv vv) s (k, v)))
-> KVVector kv vv (k, v))
-> (forall s. ST s (Mutable (KVVector kv vv) s (k, v)))
-> KVVector kv vv (k, v)
forall a b. (a -> b) -> a -> b
$ do
KVMVector (Mutable kv) (Mutable vv) s (k, v)
mv <- [(k, v)]
-> KVMVector (Mutable kv) (Mutable vv) (PrimState (ST s)) (k, v)
-> ST
s (KVMVector (Mutable kv) (Mutable vv) (PrimState (ST s)) (k, v))
forall (v :: * -> * -> *) a (m :: * -> *).
(MVector v a, PrimMonad m) =>
[a] -> v (PrimState m) a -> m (v (PrimState m) a)
fillWithList [(k, v)]
xs (KVMVector (Mutable kv) (Mutable vv) s (k, v)
-> ST s (KVMVector (Mutable kv) (Mutable vv) s (k, v)))
-> ST s (KVMVector (Mutable kv) (Mutable vv) s (k, v))
-> ST s (KVMVector (Mutable kv) (Mutable vv) s (k, v))
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Int
-> ST
s (KVMVector (Mutable kv) (Mutable vv) (PrimState (ST s)) (k, v))
forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
Int -> m (v (PrimState m) a)
VGM.unsafeNew Int
n
KVMVector (Mutable kv) (Mutable vv) (PrimState (ST s)) (k, v)
-> ST s ()
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)
KVMVector (Mutable kv) (Mutable vv) (PrimState (ST s)) (k, v)
mv
KVMVector (Mutable kv) (Mutable vv) (PrimState (ST s)) (k, v)
-> ST
s (KVMVector (Mutable kv) (Mutable vv) (PrimState (ST s)) (k, v))
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)
KVMVector (Mutable kv) (Mutable vv) (PrimState (ST s)) (k, v)
mv
{-# INLINE fromListN #-}
fromDistinctAscList ::
(VG.Vector kv k, VG.Vector vv v) =>
[(k, v)] ->
KVVector kv vv (k, v)
fromDistinctAscList :: [(k, v)] -> KVVector kv vv (k, v)
fromDistinctAscList [(k, v)]
xs = Int -> [(k, v)] -> KVVector kv vv (k, v)
forall (v :: * -> *) a. Vector v a => Int -> [a] -> v a
VG.fromListN ([(k, v)] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
Prelude.length [(k, v)]
xs) [(k, v)]
xs
{-# INLINE fromDistinctAscList #-}
fromDistinctAscListN ::
(VG.Vector kv k, VG.Vector vv v) =>
Int ->
[(k, v)] ->
KVVector kv vv (k, v)
fromDistinctAscListN :: Int -> [(k, v)] -> KVVector kv vv (k, v)
fromDistinctAscListN = Int -> [(k, v)] -> KVVector kv vv (k, v)
forall (v :: * -> *) a. Vector v a => Int -> [a] -> v a
VG.fromListN
{-# INLINE fromDistinctAscListN #-}
fromAscList ::
(Eq k, VG.Vector kv k, VG.Vector vv v) =>
[(k, v)] ->
KVVector kv vv (k, v)
fromAscList :: [(k, v)] -> KVVector kv vv (k, v)
fromAscList [(k, v)]
xs = Int -> [(k, v)] -> KVVector kv vv (k, v)
forall k (kv :: * -> *) (vv :: * -> *) v.
(Eq k, Vector kv k, Vector vv v) =>
Int -> [(k, v)] -> KVVector kv vv (k, v)
fromAscListN ([(k, v)] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
Prelude.length [(k, v)]
xs) [(k, v)]
xs
{-# INLINE fromAscList #-}
fromAscListN ::
(Eq k, VG.Vector kv k, VG.Vector vv v) =>
Int ->
[(k, v)] ->
KVVector kv vv (k, v)
fromAscListN :: Int -> [(k, v)] -> KVVector kv vv (k, v)
fromAscListN Int
n = Int -> (k -> v -> v -> v) -> [(k, v)] -> KVVector kv vv (k, v)
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
forall k v. k -> v -> v -> v
selectDuplicate
{-# INLINE fromAscListN #-}
fillWithList ::
(VGM.MVector v a, PrimMonad m) =>
[a] ->
v (PrimState m) a ->
m (v (PrimState m) a)
fillWithList :: [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 = v (PrimState m) a -> Int
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 Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
n = v (PrimState m) a -> m (v (PrimState m) a)
forall (f :: * -> *) a. Applicative f => a -> f a
pure v (PrimState m) a
mv
| a
x : [a]
xs <- [a]
ys = v (PrimState m) a -> Int -> a -> m ()
forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
v (PrimState m) a -> Int -> a -> m ()
VGM.write v (PrimState m) a
mv Int
i a
x m () -> m (v (PrimState m) a) -> m (v (PrimState m) a)
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Int -> [a] -> m (v (PrimState m) a)
go (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) [a]
xs
| Bool
otherwise = v (PrimState m) a -> m (v (PrimState m) a)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (v (PrimState m) a -> m (v (PrimState m) a))
-> v (PrimState m) a -> m (v (PrimState m) a)
forall a b. (a -> b) -> a -> b
$ Int -> Int -> v (PrimState m) a -> v (PrimState m) a
forall (v :: * -> * -> *) a s.
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 :: (k -> v -> v -> v) -> [(k, v)] -> KVVector kv vv (k, v)
fromAscListWithKey k -> v -> v -> v
f [(k, v)]
xs = Int -> (k -> v -> v -> v) -> [(k, v)] -> KVVector kv vv (k, v)
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 ([(k, v)] -> Int
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 :: 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 Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
0 = KVVector kv vv (k, v)
forall (v :: * -> *) a. Vector v a => v a
VG.empty
| Bool
otherwise = (forall s. ST s (Mutable (KVVector kv vv) s (k, v)))
-> KVVector kv vv (k, v)
forall (v :: * -> *) a.
Vector v a =>
(forall s. ST s (Mutable v s a)) -> v a
VG.create ((forall s. ST s (Mutable (KVVector kv vv) s (k, v)))
-> KVVector kv vv (k, v))
-> (forall s. ST s (Mutable (KVVector kv vv) s (k, v)))
-> KVVector kv vv (k, v)
forall a b. (a -> b) -> a -> b
$ Int
-> ST
s (KVMVector (Mutable kv) (Mutable vv) (PrimState (ST s)) (k, v))
forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
Int -> m (v (PrimState m) a)
VGM.unsafeNew Int
n ST s (KVMVector (Mutable kv) (Mutable vv) s (k, v))
-> (KVMVector (Mutable kv) (Mutable vv) s (k, v)
-> ST s (KVMVector (Mutable kv) (Mutable vv) s (k, v)))
-> ST s (KVMVector (Mutable kv) (Mutable vv) s (k, v))
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= [(k, v)]
-> KVMVector (Mutable kv) (Mutable vv) (PrimState (ST s)) (k, v)
-> ST
s (KVMVector (Mutable kv) (Mutable vv) (PrimState (ST s)) (k, v))
forall (v :: * -> * -> *) a (m :: * -> *).
(MVector v a, PrimMonad m) =>
[a] -> v (PrimState m) a -> m (v (PrimState m) a)
fillWithList [(k, v)]
xs ST s (KVMVector (Mutable kv) (Mutable vv) s (k, v))
-> (KVMVector (Mutable kv) (Mutable vv) s (k, v)
-> ST s (KVMVector (Mutable kv) (Mutable vv) s (k, v)))
-> ST s (KVMVector (Mutable kv) (Mutable vv) s (k, v))
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (k -> v -> v -> v)
-> KVMVector (Mutable kv) (Mutable vv) (PrimState (ST s)) (k, v)
-> ST
s (KVMVector (Mutable kv) (Mutable vv) (PrimState (ST s)) (k, v))
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 :: (a -> b) -> KVVector kv vv (k, a) -> KVVector kv vv (k, b)
mapValsKVVector a -> b
f KVVector kv vv (k, a)
vec =
KVVector :: forall (kv :: * -> *) (vv :: * -> *) a.
kv (Key a) -> vv (Value a) -> KVVector kv vv a
KVVector {keysVector :: kv (Key (k, b))
keysVector = KVVector kv vv (k, a) -> kv (Key (k, a))
forall (kv :: * -> *) (vv :: * -> *) a.
KVVector kv vv a -> kv (Key a)
keysVector KVVector kv vv (k, a)
vec, valsVector :: vv (Value (k, b))
valsVector = (a -> b) -> vv a -> vv b
forall (v :: * -> *) a b.
(Vector v a, Vector v b) =>
(a -> b) -> v a -> v b
VG.map a -> b
f (KVVector kv vv (k, a) -> vv (Value (k, a))
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 :: (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 :: forall (kv :: * -> *) (vv :: * -> *) a.
kv (Key a) -> vv (Value a) -> KVVector kv vv a
KVVector
{ keysVector :: kv (Key (k, b))
keysVector = kv (Key (k, a))
kv (Key (k, b))
keysVector,
valsVector :: vv (Value (k, b))
valsVector = (Int -> a -> b) -> vv a -> vv b
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 k
kv (Key (k, a))
keysVector kv k -> Int -> k
forall (v :: * -> *) a. Vector v a => v a -> Int -> a
VG.! Int
i)) vv a
vv (Value (k, a))
valsVector
}
{-# INLINE mapWithKeyKVVector #-}
internKVVectorMaybe :: (VG.Vector kv k, Ord k) => k -> KVVector kv vv (k, v) -> Maybe k
internKVVectorMaybe :: k -> KVVector kv vv (k, v) -> Maybe k
internKVVectorMaybe k
key (KVVector kv (Key (k, v))
keys vv (Value (k, v))
_values) =
kv k -> Int -> Maybe k
forall (v :: * -> *) a (m :: * -> *).
(Vector v a, Monad m) =>
v a -> Int -> m a
VG.indexM kv k
kv (Key (k, v))
keys (Int -> Maybe k) -> Maybe Int -> Maybe k
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< k -> kv k -> Maybe Int
forall (kv :: * -> *) k.
(Vector kv k, Ord k) =>
k -> kv k -> Maybe Int
lookupIxSortedVector k
key kv k
kv (Key (k, v))
keys
{-# INLINE internKVVectorMaybe #-}
lookupKVVector ::
(Ord k, VG.Vector kv k, VG.Vector vv v) => k -> KVVector kv vv (k, v) -> Maybe v
lookupKVVector :: k -> KVVector kv vv (k, v) -> Maybe v
lookupKVVector k
key (KVVector kv (Key (k, v))
keys vv (Value (k, v))
values) =
vv v -> Int -> Maybe v
forall (v :: * -> *) a (m :: * -> *).
(Vector v a, Monad m) =>
v a -> Int -> m a
VG.indexM vv v
vv (Value (k, v))
values (Int -> Maybe v) -> Maybe Int -> Maybe v
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< k -> kv k -> Maybe Int
forall (kv :: * -> *) k.
(Vector kv k, Ord k) =>
k -> kv k -> Maybe Int
lookupIxSortedVector k
key kv k
kv (Key (k, v))
keys
{-# INLINE lookupKVVector #-}
lookupDefaultKVVector ::
(Ord k, VG.Vector kv k, VG.Vector vv v) => v -> k -> KVVector kv vv (k, v) -> v
lookupDefaultKVVector :: v -> k -> KVVector kv vv (k, v) -> v
lookupDefaultKVVector v
v k
k = v -> Maybe v -> v
forall a. a -> Maybe a -> a
fromMaybe v
v (Maybe v -> v)
-> (KVVector kv vv (k, v) -> Maybe v) -> KVVector kv vv (k, v) -> v
forall b c a. (b -> c) -> (a -> b) -> a -> c
. k -> KVVector kv vv (k, v) -> Maybe v
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 :: k -> KVVector kv vv (k, v) -> Bool
memberKVVector k
k = Maybe Int -> Bool
forall a. Maybe a -> Bool
isJust (Maybe Int -> Bool)
-> (KVVector kv vv (k, v) -> Maybe Int)
-> KVVector kv vv (k, v)
-> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. k -> kv k -> Maybe Int
forall (kv :: * -> *) k.
(Vector kv k, Ord k) =>
k -> kv k -> Maybe Int
lookupIxSortedVector k
k (kv k -> Maybe Int)
-> (KVVector kv vv (k, v) -> kv k)
-> KVVector kv vv (k, v)
-> Maybe Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. KVVector kv vv (k, v) -> kv k
forall (kv :: * -> *) (vv :: * -> *) a.
KVVector kv vv a -> kv (Key a)
keysVector
{-# INLINE memberKVVector #-}
lookupIxSortedVector ::
(VG.Vector kv k, Ord k) => k -> kv k -> Maybe Int
lookupIxSortedVector :: k -> kv k -> Maybe Int
lookupIxSortedVector k
key kv k
keys = Int -> Int -> Maybe Int
go Int
0 (kv k -> Int
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
Bool -> Maybe ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard (Int
l Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
u)
let !i :: Int
i = ((Int
u Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
l) Int -> Int -> Int
forall a. Integral a => a -> a -> a
`div` Int
2) Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
l
case k -> k -> Ordering
forall a. Ord a => a -> a -> Ordering
compare k
key (kv k
keys kv k -> Int -> k
forall (v :: * -> *) a. 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 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) Int
u
Ordering
EQ -> Int -> Maybe Int
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 :: KVMVector kmv vmv (PrimState m) (k, v) -> m ()
sortAscKVMVector = Comparison (k, v) -> KVMVector kmv vmv (PrimState m) (k, v) -> m ()
forall (m :: * -> *) (v :: * -> * -> *) e.
(PrimMonad m, MVector v e) =>
Comparison e -> v (PrimState m) e -> m ()
sortBy (\(k
k1, v
_) (k
k2, v
_) -> k -> k -> Ordering
forall a. Ord a => a -> a -> Ordering
compare k
k1 k
k2)
{-# INLINE sortAscKVMVector #-}
selectDuplicate :: k -> v -> v -> v
selectDuplicate :: 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_ :: KVMVector kmv vmv (PrimState m) (k, v)
-> m (KVMVector kmv vmv (PrimState m) (k, v))
removeDuplicates_ = (k -> v -> v -> v)
-> KVMVector kmv vmv (PrimState m) (k, v)
-> m (KVMVector kmv vmv (PrimState m) (k, v))
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
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 :: (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
| KVMVector kmv vmv (PrimState m) (k, v) -> Bool
forall (v :: * -> * -> *) a s. MVector v a => v s a -> Bool
VGM.null KVMVector kmv vmv (PrimState m) (k, v)
mv = KVMVector kmv vmv (PrimState m) (k, v)
-> m (KVMVector kmv vmv (PrimState m) (k, v))
forall (f :: * -> *) a. Applicative f => a -> f a
pure KVMVector kmv vmv (PrimState m) (k, v)
mv
| Bool
otherwise = do
let n :: Int
n = KVMVector kmv vmv (PrimState m) (k, v) -> Int
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
KVMVector kmv vmv (PrimState m) (k, v) -> Int -> (k, v) -> m ()
forall (m :: * -> *) (v :: * -> * -> *) a.
(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 Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
n
then do
cur :: (k, v)
cur@(k
ck, v
cv) <- KVMVector kmv vmv (PrimState m) (k, v) -> Int -> m (k, v)
forall (m :: * -> *) (v :: * -> * -> *) a.
(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 k -> k -> Bool
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 Int -> Int -> Int
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 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) (k, v)
cur (Int
curIx Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)
else KVMVector kmv vmv (PrimState m) (k, v)
-> m (KVMVector kmv vmv (PrimState m) (k, v))
forall (f :: * -> *) a. Applicative f => a -> f a
pure (KVMVector kmv vmv (PrimState m) (k, v)
-> m (KVMVector kmv vmv (PrimState m) (k, v)))
-> KVMVector kmv vmv (PrimState m) (k, v)
-> m (KVMVector kmv vmv (PrimState m) (k, v))
forall a b. (a -> b) -> a -> b
$ Int
-> Int
-> KVMVector kmv vmv (PrimState m) (k, v)
-> KVMVector kmv vmv (PrimState m) (k, v)
forall (v :: * -> * -> *) a s.
MVector v a =>
Int -> Int -> v s a -> v s a
VGM.slice Int
0 (Int
lastIx Int -> Int -> Int
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 Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
n = do
cur :: (k, v)
cur@(k
ck, v
cv) <- KVMVector kmv vmv (PrimState m) (k, v) -> Int -> m (k, v)
forall (m :: * -> *) (v :: * -> * -> *) a.
(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 k -> k -> Bool
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 Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) (k
ck, k -> v -> v -> v
f k
ck v
cv v
pv) (Int
curIx Int -> Int -> Int
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 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)
| Bool
otherwise = KVMVector kmv vmv (PrimState m) (k, v)
-> m (KVMVector kmv vmv (PrimState m) (k, v))
forall (f :: * -> *) a. Applicative f => a -> f a
pure KVMVector kmv vmv (PrimState m) (k, v)
mv
(k, v)
x0 <- KVMVector kmv vmv (PrimState m) (k, v) -> Int -> m (k, v)
forall (m :: * -> *) (v :: * -> * -> *) a.
(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 :: KVVector kv vv (k, v) -> KVVector kv vv (k, v)
normalize KVVector kv vv (k, v)
v = (forall s. ST s (KVVector kv vv (k, v))) -> KVVector kv vv (k, v)
forall a. (forall s. ST s a) -> a
runST ((forall s. ST s (KVVector kv vv (k, v))) -> KVVector kv vv (k, v))
-> (forall s. ST s (KVVector kv vv (k, v)))
-> KVVector kv vv (k, v)
forall a b. (a -> b) -> a -> b
$ KVVector kv vv (k, v)
-> ST s (Mutable (KVVector kv vv) (PrimState (ST s)) (k, v))
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 ST s (KVMVector (Mutable kv) (Mutable vv) s (k, v))
-> (KVMVector (Mutable kv) (Mutable vv) s (k, v)
-> ST s (KVMVector (Mutable kv) (Mutable vv) s (k, v)))
-> ST s (KVMVector (Mutable kv) (Mutable vv) s (k, v))
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= KVMVector (Mutable kv) (Mutable vv) s (k, v)
-> ST s (KVMVector (Mutable kv) (Mutable vv) s (k, v))
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 ST s (KVMVector (Mutable kv) (Mutable vv) s (k, v))
-> (KVMVector (Mutable kv) (Mutable vv) s (k, v)
-> ST s (KVVector kv vv (k, v)))
-> ST s (KVVector kv vv (k, v))
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= KVMVector (Mutable kv) (Mutable vv) s (k, v)
-> ST s (KVVector kv vv (k, v))
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 :: 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 = KVMVector (Mutable kv) (Mutable vv) (PrimState m) (k, v) -> m ()
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 m ()
-> m (KVMVector (Mutable kv) (Mutable vv) (PrimState m) (k, v))
-> m (KVMVector (Mutable kv) (Mutable vv) (PrimState m) (k, v))
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> KVMVector (Mutable kv) (Mutable vv) (PrimState m) (k, v)
-> m (KVMVector (Mutable kv) (Mutable vv) (PrimState m) (k, v))
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 = KVVector kv vv (k, v) -> KVVector kv vv (k, v)
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 KVVector kv vv (k, v)
-> KVVector kv vv (k, v) -> KVVector kv vv (k, v)
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 = KVVector kv vv (k, v) -> KVVector kv vv (k, v)
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) -> KVVector kv vv (k, v))
-> (NonEmpty (KVVector kv vv (k, v)) -> KVVector kv vv (k, v))
-> NonEmpty (KVVector kv vv (k, v))
-> KVVector kv vv (k, v)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [KVVector kv vv (k, v)] -> KVVector kv vv (k, v)
forall (v :: * -> *) a. Vector v a => [v a] -> v a
VG.concat ([KVVector kv vv (k, v)] -> KVVector kv vv (k, v))
-> (NonEmpty (KVVector kv vv (k, v)) -> [KVVector kv vv (k, v)])
-> NonEmpty (KVVector kv vv (k, v))
-> KVVector kv vv (k, v)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. NonEmpty (KVVector kv vv (k, v)) -> [KVVector kv vv (k, v)]
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 = KVVector kv vv (k, v)
forall (v :: * -> *) a. Vector v a => v a
VG.empty
mconcat :: [KVVector kv vv (k, v)] -> KVVector kv vv (k, v)
mconcat = KVVector kv vv (k, v) -> KVVector kv vv (k, v)
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) -> KVVector kv vv (k, v))
-> ([KVVector kv vv (k, v)] -> KVVector kv vv (k, v))
-> [KVVector kv vv (k, v)]
-> KVVector kv vv (k, v)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [KVVector kv vv (k, v)] -> KVVector kv vv (k, v)
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
{ KVVector kv vv a -> kv (Key a)
keysVector :: !(kv (Key a)),
KVVector kv vv a -> vv (Value a)
valsVector :: !(vv (Value a))
}
deriving ((forall x. KVVector kv vv a -> Rep (KVVector kv vv a) x)
-> (forall x. Rep (KVVector kv vv a) x -> KVVector kv vv a)
-> Generic (KVVector kv vv a)
forall x. Rep (KVVector kv vv a) x -> KVVector kv vv a
forall x. KVVector kv vv a -> Rep (KVVector kv vv a) x
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 = [Item (KVVector kv vv (k, v))] -> KVVector kv vv (k, v)
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 = Int -> [Item (KVVector kv vv (k, v))] -> KVVector kv vv (k, v)
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 = KVVector kv vv (k, v) -> [Item (KVVector kv vv (k, v))]
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
{ KVMVector kmv vmv s a -> kmv s (Key a)
_keysMVector :: !(kmv s (Key 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 k
kv (Key (k, v))
keysVector kv k -> vv v -> vv v
forall a b. NFData a => a -> b -> b
`deepseq` vv v
vv (Value (k, v))
valsVector vv v -> () -> ()
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 :: Mutable (KVVector kv vv) (PrimState m) (k, v)
-> m (KVVector kv vv (k, v))
basicUnsafeFreeze (KVMVector kmv vmv) =
kv k -> vv v -> KVVector kv vv (k, v)
forall (kv :: * -> *) (vv :: * -> *) a.
kv (Key a) -> vv (Value a) -> KVVector kv vv a
KVVector (kv k -> vv v -> KVVector kv vv (k, v))
-> m (kv k) -> m (vv v -> KVVector kv vv (k, v))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Mutable kv (PrimState m) k -> m (kv k)
forall (v :: * -> *) a (m :: * -> *).
(Vector v a, PrimMonad m) =>
Mutable v (PrimState m) a -> m (v a)
VG.basicUnsafeFreeze Mutable kv (PrimState m) k
Mutable kv (PrimState m) (Key (k, v))
kmv m (vv v -> KVVector kv vv (k, v))
-> m (vv v) -> m (KVVector kv vv (k, v))
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Mutable vv (PrimState m) v -> m (vv v)
forall (v :: * -> *) a (m :: * -> *).
(Vector v a, PrimMonad m) =>
Mutable v (PrimState m) a -> m (v a)
VG.basicUnsafeFreeze Mutable vv (PrimState m) v
Mutable vv (PrimState m) (Value (k, v))
vmv
{-# INLINE basicUnsafeThaw #-}
basicUnsafeThaw :: KVVector kv vv (k, v)
-> m (Mutable (KVVector kv vv) (PrimState m) (k, v))
basicUnsafeThaw (KVVector kv (Key (k, v))
kv vv (Value (k, v))
vv) = Mutable kv (PrimState m) k
-> Mutable vv (PrimState m) v
-> KVMVector (Mutable kv) (Mutable vv) (PrimState m) (k, v)
forall (kmv :: * -> * -> *) (vmv :: * -> * -> *) s a.
kmv s (Key a) -> vmv s (Value a) -> KVMVector kmv vmv s a
KVMVector (Mutable kv (PrimState m) k
-> Mutable vv (PrimState m) v
-> KVMVector (Mutable kv) (Mutable vv) (PrimState m) (k, v))
-> m (Mutable kv (PrimState m) k)
-> m (Mutable vv (PrimState m) v
-> KVMVector (Mutable kv) (Mutable vv) (PrimState m) (k, v))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> kv k -> m (Mutable kv (PrimState m) k)
forall (v :: * -> *) a (m :: * -> *).
(Vector v a, PrimMonad m) =>
v a -> m (Mutable v (PrimState m) a)
VG.basicUnsafeThaw kv k
kv (Key (k, v))
kv m (Mutable vv (PrimState m) v
-> KVMVector (Mutable kv) (Mutable vv) (PrimState m) (k, v))
-> m (Mutable vv (PrimState m) v)
-> m (KVMVector (Mutable kv) (Mutable vv) (PrimState m) (k, v))
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> vv v -> m (Mutable vv (PrimState m) v)
forall (v :: * -> *) a (m :: * -> *).
(Vector v a, PrimMonad m) =>
v a -> m (Mutable v (PrimState m) a)
VG.basicUnsafeThaw vv v
vv (Value (k, v))
vv
{-# INLINE basicLength #-}
basicLength :: KVVector kv vv (k, v) -> Int
basicLength (KVVector kv (Key (k, v))
kv vv (Value (k, v))
_) = kv k -> Int
forall (v :: * -> *) a. Vector v a => v a -> Int
VG.basicLength kv k
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) =
kv (Key (k, v)) -> vv (Value (k, v)) -> KVVector kv vv (k, v)
forall (kv :: * -> *) (vv :: * -> *) a.
kv (Key a) -> vv (Value a) -> KVVector kv vv a
KVVector (Int -> Int -> kv k -> kv k
forall (v :: * -> *) a. Vector v a => Int -> Int -> v a -> v a
VG.basicUnsafeSlice Int
i Int
n kv k
kv (Key (k, v))
kv) (Int -> Int -> vv v -> vv v
forall (v :: * -> *) a. Vector v a => Int -> Int -> v a -> v a
VG.basicUnsafeSlice Int
i Int
n vv v
vv (Value (k, v))
vv)
{-# INLINE basicUnsafeIndexM #-}
basicUnsafeIndexM :: KVVector kv vv (k, v) -> Int -> m (k, v)
basicUnsafeIndexM (KVVector kv (Key (k, v))
kv vv (Value (k, v))
vv) Int
i = do
k
k <- kv k -> Int -> m k
forall (v :: * -> *) a (m :: * -> *).
(Vector v a, Monad m) =>
v a -> Int -> m a
VG.basicUnsafeIndexM kv k
kv (Key (k, v))
kv Int
i
v
v <- vv v -> Int -> m v
forall (v :: * -> *) a (m :: * -> *).
(Vector v a, Monad m) =>
v a -> Int -> m a
VG.basicUnsafeIndexM vv v
vv (Value (k, v))
vv Int
i
(k, v) -> m (k, v)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (k
k, v
v)
{-# INLINE basicUnsafeCopy #-}
basicUnsafeCopy :: Mutable (KVVector kv vv) (PrimState m) (k, v)
-> KVVector kv vv (k, v) -> m ()
basicUnsafeCopy (KVMVector kvDst vvDst) (KVVector kv (Key (k, v))
kvSrc vv (Value (k, v))
vvSrc) =
Mutable kv (PrimState m) k -> kv k -> m ()
forall (v :: * -> *) a (m :: * -> *).
(Vector v a, PrimMonad m) =>
Mutable v (PrimState m) a -> v a -> m ()
VG.basicUnsafeCopy Mutable kv (PrimState m) k
Mutable kv (PrimState m) (Key (k, v))
kvDst kv k
kv (Key (k, v))
kvSrc m () -> m () -> m ()
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Mutable vv (PrimState m) v -> vv v -> m ()
forall (v :: * -> *) a (m :: * -> *).
(Vector v a, PrimMonad m) =>
Mutable v (PrimState m) a -> v a -> m ()
VG.basicUnsafeCopy Mutable vv (PrimState m) v
Mutable vv (PrimState m) (Value (k, v))
vvDst vv v
vv (Value (k, v))
vvSrc
instance (VGM.MVector kmv k, VGM.MVector vmv v) => VGM.MVector (KVMVector kmv vmv) (k, v) where
{-# INLINE basicLength #-}
basicLength :: KVMVector kmv vmv s (k, v) -> Int
basicLength (KVMVector kmv s (Key (k, v))
kmv vmv s (Value (k, v))
_) = kmv s k -> Int
forall (v :: * -> * -> *) a s. MVector v a => v s a -> Int
VGM.basicLength kmv s k
kmv s (Key (k, v))
kmv
{-# INLINE basicUnsafeSlice #-}
basicUnsafeSlice :: 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) =
kmv s (Key (k, v))
-> vmv s (Value (k, v)) -> KVMVector kmv vmv s (k, v)
forall (kmv :: * -> * -> *) (vmv :: * -> * -> *) s a.
kmv s (Key a) -> vmv s (Value a) -> KVMVector kmv vmv s a
KVMVector (Int -> Int -> kmv s k -> kmv s k
forall (v :: * -> * -> *) a s.
MVector v a =>
Int -> Int -> v s a -> v s a
VGM.basicUnsafeSlice Int
j Int
m kmv s k
kmv s (Key (k, v))
kmv) (Int -> Int -> vmv s v -> vmv s v
forall (v :: * -> * -> *) a s.
MVector v a =>
Int -> Int -> v s a -> v s a
VGM.basicUnsafeSlice Int
j Int
m vmv s v
vmv s (Value (k, v))
vmv)
{-# INLINE basicOverlaps #-}
basicOverlaps :: 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) =
kmv s k -> kmv s k -> Bool
forall (v :: * -> * -> *) a s.
MVector v a =>
v s a -> v s a -> Bool
VGM.basicOverlaps kmv s k
kmv s (Key (k, v))
kmv1 kmv s k
kmv s (Key (k, v))
kmv2 Bool -> Bool -> Bool
&& vmv s v -> vmv s v -> Bool
forall (v :: * -> * -> *) a s.
MVector v a =>
v s a -> v s a -> Bool
VGM.basicOverlaps vmv s v
vmv s (Value (k, v))
vmv1 vmv s v
vmv s (Value (k, v))
vmv2
{-# INLINE basicUnsafeNew #-}
basicUnsafeNew :: Int -> m (KVMVector kmv vmv (PrimState m) (k, v))
basicUnsafeNew Int
n = do
kmv (PrimState m) k
kmv1 <- Int -> m (kmv (PrimState m) k)
forall (v :: * -> * -> *) a (m :: * -> *).
(MVector v a, PrimMonad m) =>
Int -> m (v (PrimState m) a)
VGM.basicUnsafeNew Int
n
vmv (PrimState m) v
vmv1 <- Int -> m (vmv (PrimState m) v)
forall (v :: * -> * -> *) a (m :: * -> *).
(MVector v a, PrimMonad m) =>
Int -> m (v (PrimState m) a)
VGM.basicUnsafeNew Int
n
KVMVector kmv vmv (PrimState m) (k, v)
-> m (KVMVector kmv vmv (PrimState m) (k, v))
forall (m :: * -> *) a. Monad m => a -> m a
return (kmv (PrimState m) (Key (k, v))
-> vmv (PrimState m) (Value (k, v))
-> KVMVector kmv vmv (PrimState m) (k, v)
forall (kmv :: * -> * -> *) (vmv :: * -> * -> *) s a.
kmv s (Key a) -> vmv s (Value a) -> KVMVector kmv vmv s a
KVMVector kmv (PrimState m) k
kmv (PrimState m) (Key (k, v))
kmv1 vmv (PrimState m) v
vmv (PrimState m) (Value (k, v))
vmv1)
{-# INLINE basicInitialize #-}
basicInitialize :: KVMVector kmv vmv (PrimState m) (k, v) -> m ()
basicInitialize (KVMVector kmv (PrimState m) (Key (k, v))
kmv vmv (PrimState m) (Value (k, v))
vmv) = kmv (PrimState m) k -> m ()
forall (v :: * -> * -> *) a (m :: * -> *).
(MVector v a, PrimMonad m) =>
v (PrimState m) a -> m ()
VGM.basicInitialize kmv (PrimState m) k
kmv (PrimState m) (Key (k, v))
kmv m () -> m () -> m ()
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> vmv (PrimState m) v -> m ()
forall (v :: * -> * -> *) a (m :: * -> *).
(MVector v a, PrimMonad m) =>
v (PrimState m) a -> m ()
VGM.basicInitialize vmv (PrimState m) v
vmv (PrimState m) (Value (k, v))
vmv
{-# INLINE basicUnsafeReplicate #-}
basicUnsafeReplicate :: Int -> (k, v) -> m (KVMVector kmv vmv (PrimState m) (k, v))
basicUnsafeReplicate Int
n !(!k
k, !v
v) =
kmv (PrimState m) k
-> vmv (PrimState m) v -> KVMVector kmv vmv (PrimState m) (k, v)
forall (kmv :: * -> * -> *) (vmv :: * -> * -> *) s a.
kmv s (Key a) -> vmv s (Value a) -> KVMVector kmv vmv s a
KVMVector (kmv (PrimState m) k
-> vmv (PrimState m) v -> KVMVector kmv vmv (PrimState m) (k, v))
-> m (kmv (PrimState m) k)
-> m (vmv (PrimState m) v
-> KVMVector kmv vmv (PrimState m) (k, v))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Int -> k -> m (kmv (PrimState m) k)
forall (v :: * -> * -> *) a (m :: * -> *).
(MVector v a, PrimMonad m) =>
Int -> a -> m (v (PrimState m) a)
VGM.basicUnsafeReplicate Int
n k
k m (vmv (PrimState m) v -> KVMVector kmv vmv (PrimState m) (k, v))
-> m (vmv (PrimState m) v)
-> m (KVMVector kmv vmv (PrimState m) (k, v))
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Int -> v -> m (vmv (PrimState m) v)
forall (v :: * -> * -> *) a (m :: * -> *).
(MVector v a, PrimMonad m) =>
Int -> a -> m (v (PrimState m) a)
VGM.basicUnsafeReplicate Int
n v
v
{-# INLINE basicUnsafeRead #-}
basicUnsafeRead :: KVMVector kmv vmv (PrimState m) (k, v) -> Int -> m (k, v)
basicUnsafeRead (KVMVector kmv (PrimState m) (Key (k, v))
kmv vmv (PrimState m) (Value (k, v))
vmv) Int
i = do
k
k <- kmv (PrimState m) k -> Int -> m k
forall (v :: * -> * -> *) a (m :: * -> *).
(MVector v a, PrimMonad m) =>
v (PrimState m) a -> Int -> m a
VGM.basicUnsafeRead kmv (PrimState m) k
kmv (PrimState m) (Key (k, v))
kmv Int
i
v
v <- vmv (PrimState m) v -> Int -> m v
forall (v :: * -> * -> *) a (m :: * -> *).
(MVector v a, PrimMonad m) =>
v (PrimState m) a -> Int -> m a
VGM.basicUnsafeRead vmv (PrimState m) v
vmv (PrimState m) (Value (k, v))
vmv Int
i
(k, v) -> m (k, v)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (k
k, v
v)
{-# INLINE basicUnsafeWrite #-}
basicUnsafeWrite :: KVMVector kmv vmv (PrimState m) (k, v) -> Int -> (k, v) -> m ()
basicUnsafeWrite (KVMVector kmv (PrimState m) (Key (k, v))
kmv vmv (PrimState m) (Value (k, v))
vmv) Int
i !(!k
k, !v
v) =
kmv (PrimState m) k -> Int -> k -> m ()
forall (v :: * -> * -> *) a (m :: * -> *).
(MVector v a, PrimMonad m) =>
v (PrimState m) a -> Int -> a -> m ()
VGM.basicUnsafeWrite kmv (PrimState m) k
kmv (PrimState m) (Key (k, v))
kmv Int
i k
k m () -> m () -> m ()
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> vmv (PrimState m) v -> Int -> v -> m ()
forall (v :: * -> * -> *) a (m :: * -> *).
(MVector v a, PrimMonad m) =>
v (PrimState m) a -> Int -> a -> m ()
VGM.basicUnsafeWrite vmv (PrimState m) v
vmv (PrimState m) (Value (k, v))
vmv Int
i v
v
{-# INLINE basicUnsafeCopy #-}
basicUnsafeCopy :: KVMVector kmv vmv (PrimState m) (k, v)
-> KVMVector kmv vmv (PrimState m) (k, v) -> m ()
basicUnsafeCopy (KVMVector kmv (PrimState m) (Key (k, v))
kmvDst vmv (PrimState m) (Value (k, v))
vmvDst) (KVMVector kmv (PrimState m) (Key (k, v))
kmvSrc vmv (PrimState m) (Value (k, v))
vmvSrc) =
kmv (PrimState m) k -> kmv (PrimState m) k -> m ()
forall (v :: * -> * -> *) a (m :: * -> *).
(MVector v a, PrimMonad m) =>
v (PrimState m) a -> v (PrimState m) a -> m ()
VGM.basicUnsafeCopy kmv (PrimState m) k
kmv (PrimState m) (Key (k, v))
kmvDst kmv (PrimState m) k
kmv (PrimState m) (Key (k, v))
kmvSrc m () -> m () -> m ()
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> vmv (PrimState m) v -> vmv (PrimState m) v -> m ()
forall (v :: * -> * -> *) a (m :: * -> *).
(MVector v a, PrimMonad m) =>
v (PrimState m) a -> v (PrimState m) a -> m ()
VGM.basicUnsafeCopy vmv (PrimState m) v
vmv (PrimState m) (Value (k, v))
vmvDst vmv (PrimState m) v
vmv (PrimState m) (Value (k, v))
vmvSrc
{-# INLINE basicUnsafeMove #-}
basicUnsafeMove :: KVMVector kmv vmv (PrimState m) (k, v)
-> KVMVector kmv vmv (PrimState m) (k, v) -> m ()
basicUnsafeMove (KVMVector kmv (PrimState m) (Key (k, v))
kmvDst vmv (PrimState m) (Value (k, v))
vmvDst) (KVMVector kmv (PrimState m) (Key (k, v))
kmvSrc vmv (PrimState m) (Value (k, v))
vmvSrc) =
kmv (PrimState m) k -> kmv (PrimState m) k -> m ()
forall (v :: * -> * -> *) a (m :: * -> *).
(MVector v a, PrimMonad m) =>
v (PrimState m) a -> v (PrimState m) a -> m ()
VGM.basicUnsafeMove kmv (PrimState m) k
kmv (PrimState m) (Key (k, v))
kmvDst kmv (PrimState m) k
kmv (PrimState m) (Key (k, v))
kmvSrc m () -> m () -> m ()
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> vmv (PrimState m) v -> vmv (PrimState m) v -> m ()
forall (v :: * -> * -> *) a (m :: * -> *).
(MVector v a, PrimMonad m) =>
v (PrimState m) a -> v (PrimState m) a -> m ()
VGM.basicUnsafeMove vmv (PrimState m) v
vmv (PrimState m) (Value (k, v))
vmvDst vmv (PrimState m) v
vmv (PrimState m) (Value (k, v))
vmvSrc
{-# INLINE basicClear #-}
basicClear :: KVMVector kmv vmv (PrimState m) (k, v) -> m ()
basicClear (KVMVector kmv (PrimState m) (Key (k, v))
kmv vmv (PrimState m) (Value (k, v))
vmv) = kmv (PrimState m) k -> m ()
forall (v :: * -> * -> *) a (m :: * -> *).
(MVector v a, PrimMonad m) =>
v (PrimState m) a -> m ()
VGM.basicClear kmv (PrimState m) k
kmv (PrimState m) (Key (k, v))
kmv m () -> m () -> m ()
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> vmv (PrimState m) v -> m ()
forall (v :: * -> * -> *) a (m :: * -> *).
(MVector v a, PrimMonad m) =>
v (PrimState m) a -> m ()
VGM.basicClear vmv (PrimState m) v
vmv (PrimState m) (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) = Maybe ThunkInfo -> Maybe ThunkInfo -> Maybe ThunkInfo
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
(<|>) (Maybe ThunkInfo -> Maybe ThunkInfo -> Maybe ThunkInfo)
-> IO (Maybe ThunkInfo) -> IO (Maybe ThunkInfo -> Maybe ThunkInfo)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Context -> kv k -> IO (Maybe ThunkInfo)
forall a. NoThunks a => Context -> a -> IO (Maybe ThunkInfo)
wNoThunks Context
c kv k
kv (Key (k, v))
kv IO (Maybe ThunkInfo -> Maybe ThunkInfo)
-> IO (Maybe ThunkInfo) -> IO (Maybe ThunkInfo)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Context -> vv v -> IO (Maybe ThunkInfo)
forall a. NoThunks a => Context -> a -> IO (Maybe ThunkInfo)
wNoThunks Context
c vv v
vv (Value (k, v))
vv
showTypeOf :: Proxy (KVVector kv vv (k, v)) -> String
showTypeOf Proxy (KVVector kv vv (k, v))
px = TypeRep -> ShowS
showsTypeRep (Proxy (KVVector kv vv (k, v)) -> TypeRep
forall k (proxy :: k -> *) (a :: k).
Typeable a =>
proxy a -> TypeRep
typeRep Proxy (KVVector kv vv (k, v))
px) String
""
instance
(ToCBOR k, ToCBOR v, Ord k, VG.Vector kv k, VG.Vector vv v, Typeable kv, Typeable vv) =>
ToCBOR (KVVector kv vv (k, v))
where
toCBOR :: KVVector kv vv (k, v) -> Encoding
toCBOR = Map k v -> Encoding
forall a. ToCBOR a => a -> Encoding
toCBOR (Map k v -> Encoding)
-> (KVVector kv vv (k, v) -> Map k v)
-> KVVector kv vv (k, v)
-> Encoding
forall b c a. (b -> c) -> (a -> b) -> a -> c
. KVVector kv vv (k, v) -> Map k v
forall (kv :: * -> *) k (vv :: * -> *) v.
(Vector kv k, Vector vv v) =>
KVVector kv vv (k, v) -> Map k v
toMap
instance
(FromCBOR k, FromCBOR v, Ord k, VG.Vector kv k, VG.Vector vv v, Typeable kv, Typeable vv) =>
FromCBOR (KVVector kv vv (k, v))
where
fromCBOR :: Decoder s (KVVector kv vv (k, v))
fromCBOR = Map k v -> KVVector kv vv (k, v)
forall (kv :: * -> *) k (vv :: * -> *) v.
(Vector kv k, Vector vv v) =>
Map k v -> KVVector kv vv (k, v)
fromMap (Map k v -> KVVector kv vv (k, v))
-> Decoder s (Map k v) -> Decoder s (KVVector kv vv (k, v))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Decoder s (Map k v)
forall a s. FromCBOR a => Decoder s a
fromCBOR
instance Typeable e => NoThunks (VS.Vector e) where
wNoThunks :: Context -> Vector e -> IO (Maybe ThunkInfo)
wNoThunks Context
_ !Vector e
_ = Maybe ThunkInfo -> IO (Maybe ThunkInfo)
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe ThunkInfo
forall a. Maybe a
Nothing
showTypeOf :: Proxy (Vector e) -> String
showTypeOf Proxy (Vector e)
px = TypeRep -> ShowS
showsTypeRep (Proxy (Vector e) -> TypeRep
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
_ = Maybe ThunkInfo -> IO (Maybe ThunkInfo)
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe ThunkInfo
forall a. Maybe a
Nothing
showTypeOf :: Proxy (Vector e) -> String
showTypeOf Proxy (Vector e)
px = TypeRep -> ShowS
showsTypeRep (Proxy (Vector e) -> TypeRep
forall k (proxy :: k -> *) (a :: k).
Typeable a =>
proxy a -> TypeRep
typeRep Proxy (Vector e)
px) String
""