module Data.PrimitiveArray.Sparse.BinSearch where
import Control.Monad.Primitive (PrimState,PrimMonad)
import Control.Monad.ST (ST)
import Debug.Trace (traceShow)
import qualified Control.Monad.State.Strict as SS
import qualified Data.HashMap.Strict as HMS
import qualified Data.Vector.Algorithms.Intro as VAI
import qualified Data.Vector.Algorithms.Search as VAS
import qualified Data.Vector as V
import qualified Data.Vector.Fusion.Stream.Monadic as SM
import qualified Data.Vector.Generic as VG
import qualified Data.Vector.Generic.Mutable as VGM
import qualified Data.Vector.Storable as VS
import qualified Data.Vector.Unboxed as VU
import Data.PrimitiveArray.Class
import Data.PrimitiveArray.Index.Class
import Data.PrimitiveArray.Index
data Sparse w v sh e = Sparse
{ Sparse w v sh e -> LimitType sh
sparseUpperBound :: !(LimitType sh)
, Sparse w v sh e -> v e
sparseData :: !(v e)
, Sparse w v sh e -> w sh
sparseIndices :: !(w sh)
, Sparse w v sh e -> Vector Int
manhattanStart :: !(VU.Vector Int)
}
type Unboxed w sh e = Sparse w VU.Vector sh e
type Storable w sh e = Sparse w VS.Vector sh e
type Boxed w sh e = Sparse w V.Vector sh e
data instance MutArr m (Sparse w v sh e) = MSparse
{ MutArr m (Sparse w v sh e) -> LimitType sh
msparseUpperBound :: !(LimitType sh)
, MutArr m (Sparse w v sh e) -> Mutable v (PrimState m) e
msparseData :: !(VG.Mutable v (PrimState m) e)
, MutArr m (Sparse w v sh e) -> w sh
msparseIndices :: !(w sh)
, MutArr m (Sparse w v sh e) -> Vector Int
mmanhattanStart :: !(VU.Vector Int)
}
type instance FillStruc (Sparse w v sh e) = (w sh)
instance
( Index sh, SparseBucket sh, Eq sh, Ord sh
, VG.Vector w sh , VG.Vector w (Int,sh), VG.Vector w (Int,(Int,sh))
, VG.Vector v e
#if ADPFUSION_DEBUGOUTPUT
, Show sh, Show (LimitType sh), Show e
#endif
) => PrimArrayOps (Sparse w v) sh e where
{-# Inline upperBound #-}
upperBound :: Sparse w v sh e -> LimitType sh
upperBound Sparse{w sh
v e
Vector Int
LimitType sh
manhattanStart :: Vector Int
sparseIndices :: w sh
sparseData :: v e
sparseUpperBound :: LimitType sh
manhattanStart :: forall (w :: * -> *) k (v :: k -> *) sh (e :: k).
Sparse w v sh e -> Vector Int
sparseIndices :: forall (w :: * -> *) k (v :: k -> *) sh (e :: k).
Sparse w v sh e -> w sh
sparseData :: forall (w :: * -> *) k (v :: k -> *) sh (e :: k).
Sparse w v sh e -> v e
sparseUpperBound :: forall (w :: * -> *) k (v :: k -> *) sh (e :: k).
Sparse w v sh e -> LimitType sh
..} = LimitType sh
sparseUpperBound
{-# Inline unsafeIndex #-}
unsafeIndex :: Sparse w v sh e -> sh -> e
unsafeIndex Sparse{w sh
v e
Vector Int
LimitType sh
manhattanStart :: Vector Int
sparseIndices :: w sh
sparseData :: v e
sparseUpperBound :: LimitType sh
manhattanStart :: forall (w :: * -> *) k (v :: k -> *) sh (e :: k).
Sparse w v sh e -> Vector Int
sparseIndices :: forall (w :: * -> *) k (v :: k -> *) sh (e :: k).
Sparse w v sh e -> w sh
sparseData :: forall (w :: * -> *) k (v :: k -> *) sh (e :: k).
Sparse w v sh e -> v e
sparseUpperBound :: forall (w :: * -> *) k (v :: k -> *) sh (e :: k).
Sparse w v sh e -> LimitType sh
..} sh
idx = case LimitType sh -> Vector Int -> w sh -> sh -> Maybe Int
forall sh (v :: * -> *).
(SparseBucket sh, Vector v sh, Eq sh, Ord sh) =>
LimitType sh -> Vector Int -> v sh -> sh -> Maybe Int
manhattanIndex LimitType sh
sparseUpperBound Vector Int
manhattanStart w sh
sparseIndices sh
idx of
Maybe Int
Nothing -> [Char] -> e
forall a. HasCallStack => [Char] -> a
error [Char]
"unsafeIndex of non-existing index"
Just Int
v -> v e -> Int -> e
forall (v :: * -> *) a. Vector v a => v a -> Int -> a
VG.unsafeIndex v e
sparseData Int
v
{-# Inline safeIndex #-}
safeIndex :: Sparse w v sh e -> sh -> Maybe e
safeIndex Sparse{w sh
v e
Vector Int
LimitType sh
manhattanStart :: Vector Int
sparseIndices :: w sh
sparseData :: v e
sparseUpperBound :: LimitType sh
manhattanStart :: forall (w :: * -> *) k (v :: k -> *) sh (e :: k).
Sparse w v sh e -> Vector Int
sparseIndices :: forall (w :: * -> *) k (v :: k -> *) sh (e :: k).
Sparse w v sh e -> w sh
sparseData :: forall (w :: * -> *) k (v :: k -> *) sh (e :: k).
Sparse w v sh e -> v e
sparseUpperBound :: forall (w :: * -> *) k (v :: k -> *) sh (e :: k).
Sparse w v sh e -> LimitType sh
..} = (Int -> e) -> Maybe Int -> Maybe e
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (v e -> Int -> e
forall (v :: * -> *) a. Vector v a => v a -> Int -> a
VG.unsafeIndex v e
sparseData) (Maybe Int -> Maybe e) -> (sh -> Maybe Int) -> sh -> Maybe e
forall b c a. (b -> c) -> (a -> b) -> a -> c
. LimitType sh -> Vector Int -> w sh -> sh -> Maybe Int
forall sh (v :: * -> *).
(SparseBucket sh, Vector v sh, Eq sh, Ord sh) =>
LimitType sh -> Vector Int -> v sh -> sh -> Maybe Int
manhattanIndex LimitType sh
sparseUpperBound Vector Int
manhattanStart w sh
sparseIndices
{-# Inline unsafeFreezeM #-}
unsafeFreezeM :: MutArr m (Sparse w v sh e) -> m (Sparse w v sh e)
unsafeFreezeM MSparse{..} = do
let sparseUpperBound :: LimitType sh
sparseUpperBound = LimitType sh
msparseUpperBound
sparseIndices :: w sh
sparseIndices = w sh
msparseIndices
manhattanStart :: Vector Int
manhattanStart = Vector Int
mmanhattanStart
v e
sparseData <- Mutable v (PrimState m) e -> m (v e)
forall (m :: * -> *) (v :: * -> *) a.
(PrimMonad m, Vector v a) =>
Mutable v (PrimState m) a -> m (v a)
VG.unsafeFreeze Mutable v (PrimState m) e
msparseData
Sparse w v sh e -> m (Sparse w v sh e)
forall (m :: * -> *) a. Monad m => a -> m a
return Sparse :: forall k (w :: * -> *) (v :: k -> *) sh (e :: k).
LimitType sh -> v e -> w sh -> Vector Int -> Sparse w v sh e
Sparse{w sh
v e
Vector Int
LimitType sh
sparseData :: v e
manhattanStart :: Vector Int
sparseIndices :: w sh
sparseUpperBound :: LimitType sh
manhattanStart :: Vector Int
sparseIndices :: w sh
sparseData :: v e
sparseUpperBound :: LimitType sh
..}
{-# Inline unsafeThawM #-}
unsafeThawM :: Sparse w v sh e -> m (MutArr m (Sparse w v sh e))
unsafeThawM Sparse{w sh
v e
Vector Int
LimitType sh
manhattanStart :: Vector Int
sparseIndices :: w sh
sparseData :: v e
sparseUpperBound :: LimitType sh
manhattanStart :: forall (w :: * -> *) k (v :: k -> *) sh (e :: k).
Sparse w v sh e -> Vector Int
sparseIndices :: forall (w :: * -> *) k (v :: k -> *) sh (e :: k).
Sparse w v sh e -> w sh
sparseData :: forall (w :: * -> *) k (v :: k -> *) sh (e :: k).
Sparse w v sh e -> v e
sparseUpperBound :: forall (w :: * -> *) k (v :: k -> *) sh (e :: k).
Sparse w v sh e -> LimitType sh
..} = do
let msparseUpperBound :: LimitType sh
msparseUpperBound = LimitType sh
sparseUpperBound
msparseIndices :: w sh
msparseIndices = w sh
sparseIndices
mmanhattanStart :: Vector Int
mmanhattanStart = Vector Int
manhattanStart
Mutable v (PrimState m) e
msparseData <- v e -> m (Mutable v (PrimState m) e)
forall (m :: * -> *) (v :: * -> *) a.
(PrimMonad m, Vector v a) =>
v a -> m (Mutable v (PrimState m) a)
VG.unsafeThaw v e
sparseData
MutArr m (Sparse w v sh e) -> m (MutArr m (Sparse w v sh e))
forall (m :: * -> *) a. Monad m => a -> m a
return MSparse :: forall (m :: * -> *) (w :: * -> *) (v :: * -> *) sh e.
LimitType sh
-> Mutable v (PrimState m) e
-> w sh
-> Vector Int
-> MutArr m (Sparse w v sh e)
MSparse{w sh
Vector Int
Mutable v (PrimState m) e
LimitType sh
msparseData :: Mutable v (PrimState m) e
mmanhattanStart :: Vector Int
msparseIndices :: w sh
msparseUpperBound :: LimitType sh
mmanhattanStart :: Vector Int
msparseIndices :: w sh
msparseData :: Mutable v (PrimState m) e
msparseUpperBound :: LimitType sh
..}
{-# Inline upperBoundM #-}
upperBoundM :: MutArr m (Sparse w v sh e) -> LimitType sh
upperBoundM MSparse{..} = LimitType sh
msparseUpperBound
{-# Inline newM #-}
newM :: LimitType sh -> m (MutArr m (Sparse w v sh e))
newM = [Char] -> LimitType sh -> m (MutArr m (Sparse w v sh e))
forall a. HasCallStack => [Char] -> a
error [Char]
"not implemented, use newSM"
{-# Inline newWithM #-}
newWithM :: LimitType sh -> e -> m (MutArr m (Sparse w v sh e))
newWithM = [Char] -> LimitType sh -> e -> m (MutArr m (Sparse w v sh e))
forall a. HasCallStack => [Char] -> a
error [Char]
"not implemented, use newWithSM"
{-# Inline readM #-}
readM :: MutArr m (Sparse w v sh e) -> sh -> m e
readM MSparse{..} sh
idx = do
case LimitType sh -> Vector Int -> w sh -> sh -> Maybe Int
forall sh (v :: * -> *).
(SparseBucket sh, Vector v sh, Eq sh, Ord sh) =>
LimitType sh -> Vector Int -> v sh -> sh -> Maybe Int
manhattanIndex LimitType sh
msparseUpperBound Vector Int
mmanhattanStart w sh
msparseIndices sh
idx of
Maybe Int
Nothing -> [Char] -> m e
forall a. HasCallStack => [Char] -> a
error [Char]
"read of non-existing element"
Just Int
v -> Mutable v (PrimState m) e -> Int -> m e
forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
v (PrimState m) a -> Int -> m a
VGM.unsafeRead Mutable v (PrimState m) e
msparseData Int
v
{-# Inline writeM #-}
writeM :: MutArr m (Sparse w v sh e) -> sh -> e -> m ()
writeM MSparse{..} sh
idx e
elm = do
case LimitType sh -> Vector Int -> w sh -> sh -> Maybe Int
forall sh (v :: * -> *).
(SparseBucket sh, Vector v sh, Eq sh, Ord sh) =>
LimitType sh -> Vector Int -> v sh -> sh -> Maybe Int
manhattanIndex LimitType sh
msparseUpperBound Vector Int
mmanhattanStart w sh
msparseIndices sh
idx of
Maybe Int
Nothing -> [Char] -> m ()
forall a. HasCallStack => [Char] -> a
error [Char]
"read of non-existing element"
Just Int
v -> Mutable v (PrimState m) e -> Int -> e -> m ()
forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
v (PrimState m) a -> Int -> a -> m ()
VGM.unsafeWrite Mutable v (PrimState m) e
msparseData Int
v e
elm
{-# Inline newSM #-}
newSM :: LimitType sh
-> FillStruc (Sparse w v sh e) -> m (MutArr m (Sparse w v sh e))
newSM LimitType sh
h FillStruc (Sparse w v sh e)
fs' = do
w (Int, sh)
fs <- w (Int, sh) -> m (Mutable w (PrimState m) (Int, sh))
forall (m :: * -> *) (v :: * -> *) a.
(PrimMonad m, Vector v a) =>
v a -> m (Mutable v (PrimState m) a)
VG.thaw ((sh -> (Int, sh)) -> w sh -> w (Int, sh)
forall (v :: * -> *) a b.
(Vector v a, Vector v b) =>
(a -> b) -> v a -> v b
VG.map (\sh
i -> (LimitType sh -> sh -> Int
forall sh. SparseBucket sh => LimitType sh -> sh -> Int
manhattan LimitType sh
h sh
i, sh
i)) w sh
FillStruc (Sparse w v sh e)
fs') m (Mutable w (PrimState m) (Int, sh))
-> (Mutable w (PrimState m) (Int, sh) -> m (w (Int, sh)))
-> m (w (Int, sh))
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \Mutable w (PrimState m) (Int, sh)
v -> Mutable w (PrimState m) (Int, sh) -> m ()
forall (m :: * -> *) (v :: * -> * -> *) e.
(PrimMonad m, MVector v e, Ord e) =>
v (PrimState m) e -> m ()
VAI.sort Mutable w (PrimState m) (Int, sh)
v m () -> m (w (Int, sh)) -> m (w (Int, sh))
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Mutable w (PrimState m) (Int, sh) -> m (w (Int, sh))
forall (m :: * -> *) (v :: * -> *) a.
(PrimMonad m, Vector v a) =>
Mutable v (PrimState m) a -> m (v a)
VG.unsafeFreeze Mutable w (PrimState m) (Int, sh)
v
let msparseUpperBound :: LimitType sh
msparseUpperBound = LimitType sh
h
msparseIndices :: w sh
msparseIndices = w sh -> w sh
forall (v :: * -> *) a. Vector v a => v a -> v a
VG.force (w sh -> w sh) -> w sh -> w sh
forall a b. (a -> b) -> a -> b
$ ((Int, sh) -> sh) -> w (Int, sh) -> w sh
forall (v :: * -> *) a b.
(Vector v a, Vector v b) =>
(a -> b) -> v a -> v b
VG.map (Int, sh) -> sh
forall a b. (a, b) -> b
snd w (Int, sh)
fs
go :: VU.MVector s Int -> ST s ()
go :: MVector s Int -> ST s ()
go MVector s Int
mv = do
w (Int, (Int, sh)) -> ((Int, (Int, sh)) -> ST s ()) -> ST s ()
forall (m :: * -> *) (v :: * -> *) a b.
(Monad m, Vector v a) =>
v a -> (a -> m b) -> m ()
VG.forM_ (w (Int, (Int, sh)) -> w (Int, (Int, sh))
forall (v :: * -> *) a. Vector v a => v a -> v a
VG.reverse (w (Int, (Int, sh)) -> w (Int, (Int, sh)))
-> w (Int, (Int, sh)) -> w (Int, (Int, sh))
forall a b. (a -> b) -> a -> b
$ w (Int, sh) -> w (Int, (Int, sh))
forall (v :: * -> *) a.
(Vector v a, Vector v (Int, a)) =>
v a -> v (Int, a)
VG.indexed w (Int, sh)
fs) (((Int, (Int, sh)) -> ST s ()) -> ST s ())
-> ((Int, (Int, sh)) -> ST s ()) -> ST s ()
forall a b. (a -> b) -> a -> b
$ \(Int
i,(mh,_)) -> MVector (PrimState (ST s)) Int -> Int -> Int -> ST s ()
forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
v (PrimState m) a -> Int -> a -> m ()
VGM.write MVector s Int
MVector (PrimState (ST s)) Int
mv Int
mh Int
i
mmanhattanStart :: Vector Int
mmanhattanStart = (forall s. Mutable Vector s Int -> ST s ())
-> Vector Int -> Vector Int
forall (v :: * -> *) a.
Vector v a =>
(forall s. Mutable v s a -> ST s ()) -> v a -> v a
VG.modify forall s. MVector s Int -> ST s ()
forall s. Mutable Vector s Int -> ST s ()
go (Vector Int -> Vector Int) -> Vector Int -> Vector Int
forall a b. (a -> b) -> a -> b
$ Int -> Int -> Vector Int
forall (v :: * -> *) a. Vector v a => Int -> a -> v a
VG.replicate (LimitType sh -> Int
forall sh. SparseBucket sh => LimitType sh -> Int
manhattanMax LimitType sh
h Int -> Int -> Int
forall a. Num a => a -> a -> a
+Int
1) (w (Int, sh) -> Int
forall (v :: * -> *) a. Vector v a => v a -> Int
VG.length w (Int, sh)
fs)
Mutable v (PrimState m) e
msparseData <- Int -> m (Mutable v (PrimState m) e)
forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
Int -> m (v (PrimState m) a)
VGM.new (Int -> m (Mutable v (PrimState m) e))
-> Int -> m (Mutable v (PrimState m) e)
forall a b. (a -> b) -> a -> b
$ w sh -> Int
forall (v :: * -> *) a. Vector v a => v a -> Int
VG.length w sh
msparseIndices
MutArr m (Sparse w v sh e) -> m (MutArr m (Sparse w v sh e))
forall (m :: * -> *) a. Monad m => a -> m a
return (MutArr m (Sparse w v sh e) -> m (MutArr m (Sparse w v sh e)))
-> MutArr m (Sparse w v sh e) -> m (MutArr m (Sparse w v sh e))
forall a b. (a -> b) -> a -> b
$ MSparse :: forall (m :: * -> *) (w :: * -> *) (v :: * -> *) sh e.
LimitType sh
-> Mutable v (PrimState m) e
-> w sh
-> Vector Int
-> MutArr m (Sparse w v sh e)
MSparse {w sh
Vector Int
Mutable v (PrimState m) e
LimitType sh
msparseData :: Mutable v (PrimState m) e
mmanhattanStart :: Vector Int
msparseIndices :: w sh
msparseUpperBound :: LimitType sh
mmanhattanStart :: Vector Int
msparseIndices :: w sh
msparseData :: Mutable v (PrimState m) e
msparseUpperBound :: LimitType sh
..}
{-# Inline newWithSM #-}
newWithSM :: LimitType sh
-> FillStruc (Sparse w v sh e)
-> e
-> m (MutArr m (Sparse w v sh e))
newWithSM LimitType sh
h FillStruc (Sparse w v sh e)
fs' e
e = do
MutArr m (Sparse w v sh e)
mv <- LimitType sh
-> FillStruc (Sparse w v sh e) -> m (MutArr m (Sparse w v sh e))
forall (arr :: * -> * -> *) sh elm (m :: * -> *).
(PrimArrayOps arr sh elm, Monad m, PrimMonad m) =>
LimitType sh -> FillStruc (arr sh elm) -> m (MutArr m (arr sh elm))
newSM LimitType sh
h FillStruc (Sparse w v sh e)
fs'
Mutable v (PrimState m) e -> e -> m ()
forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
v (PrimState m) a -> a -> m ()
VGM.set (MutArr m (Sparse w v sh e) -> Mutable v (PrimState m) e
forall (m :: * -> *) (w :: * -> *) (v :: * -> *) sh e.
MutArr m (Sparse w v sh e) -> Mutable v (PrimState m) e
msparseData MutArr m (Sparse w v sh e)
mv) e
e
MutArr m (Sparse w v sh e) -> m (MutArr m (Sparse w v sh e))
forall (m :: * -> *) a. Monad m => a -> m a
return MutArr m (Sparse w v sh e)
mv
{-# Inline safeWriteM #-}
safeWriteM :: MutArr m (Sparse w v sh e) -> sh -> e -> m ()
safeWriteM MSparse{..} sh
sh e
e = case LimitType sh -> Vector Int -> w sh -> sh -> Maybe Int
forall sh (v :: * -> *).
(SparseBucket sh, Vector v sh, Eq sh, Ord sh) =>
LimitType sh -> Vector Int -> v sh -> sh -> Maybe Int
manhattanIndex LimitType sh
msparseUpperBound Vector Int
mmanhattanStart w sh
msparseIndices sh
sh of
Maybe Int
Nothing -> () -> m ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
Just Int
v -> Mutable v (PrimState m) e -> Int -> e -> m ()
forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
v (PrimState m) a -> Int -> a -> m ()
VGM.unsafeWrite Mutable v (PrimState m) e
msparseData Int
v e
e
{-# Inline safeReadM #-}
safeReadM :: MutArr m (Sparse w v sh e) -> sh -> m (Maybe e)
safeReadM MSparse{..} sh
sh = case LimitType sh -> Vector Int -> w sh -> sh -> Maybe Int
forall sh (v :: * -> *).
(SparseBucket sh, Vector v sh, Eq sh, Ord sh) =>
LimitType sh -> Vector Int -> v sh -> sh -> Maybe Int
manhattanIndex LimitType sh
msparseUpperBound Vector Int
mmanhattanStart w sh
msparseIndices sh
sh of
Maybe Int
Nothing -> Maybe e -> m (Maybe e)
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe e
forall a. Maybe a
Nothing
Just Int
v -> e -> Maybe e
forall a. a -> Maybe a
Just (e -> Maybe e) -> m e -> m (Maybe e)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Mutable v (PrimState m) e -> Int -> m e
forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
v (PrimState m) a -> Int -> m a
VGM.unsafeRead Mutable v (PrimState m) e
msparseData Int
v
transformShape :: (LimitType sh -> LimitType sh')
-> Sparse w v sh e -> Sparse w v sh' e
transformShape = [Char]
-> (LimitType sh -> LimitType sh')
-> Sparse w v sh e
-> Sparse w v sh' e
forall a. HasCallStack => [Char] -> a
error [Char]
"implement me"
fromListM :: LimitType sh -> [e] -> m (MutArr m (Sparse w v sh e))
fromListM = [Char] -> LimitType sh -> [e] -> m (MutArr m (Sparse w v sh e))
forall a. HasCallStack => [Char] -> a
error [Char]
"implement me"
manhattanIndex
:: (SparseBucket sh, VG.Vector v sh, Eq sh, Ord sh)
=> LimitType sh -> Vector Int -> v sh -> sh -> Maybe Int
{-# Inline manhattanIndex #-}
manhattanIndex :: LimitType sh -> Vector Int -> v sh -> sh -> Maybe Int
manhattanIndex LimitType sh
ub Vector Int
mstart v sh
sixs sh
idx = (Int -> Int) -> Maybe Int -> Maybe Int
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Int -> Int -> Int
forall a. Num a => a -> a -> a
+Int
l) (Maybe Int -> Maybe Int)
-> (v sh -> Maybe Int) -> v sh -> Maybe Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. sh -> v sh -> Maybe Int
forall sh (v :: * -> *).
(Eq sh, Ord sh, Vector v sh) =>
sh -> v sh -> Maybe Int
binarySearch sh
idx (v sh -> Maybe Int) -> v sh -> Maybe Int
forall a b. (a -> b) -> a -> b
$ Int -> Int -> v sh -> v sh
forall (v :: * -> *) a. Vector v a => Int -> Int -> v a -> v a
VG.unsafeSlice Int
l (Int
hInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
lInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
1) v sh
sixs
where
b :: Int
b = LimitType sh -> sh -> Int
forall sh. SparseBucket sh => LimitType sh -> sh -> Int
manhattan LimitType sh
ub sh
idx
l :: Int
l = Vector Int
mstart Vector Int -> Int -> Int
forall a. Unbox a => Vector a -> Int -> a
`VU.unsafeIndex` Int
b
h :: Int
h = Vector Int
mstart Vector Int -> Int -> Int
forall a. Unbox a => Vector a -> Int -> a
`VU.unsafeIndex` (Int
bInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
1)
binarySearch :: (Eq sh, Ord sh, VG.Vector v sh) => sh -> v sh -> Maybe Int
{-# Inline binarySearch #-}
binarySearch :: sh -> v sh -> Maybe Int
binarySearch sh
e v sh
v = Int -> Int -> Maybe Int
go Int
0 (v sh -> Int
forall (v :: * -> *) a. Vector v a => v a -> Int
VG.length v sh
v Int -> Int -> Int
forall a. Num a => a -> a -> a
-Int
1)
where
go :: Int -> Int -> Maybe Int
go :: Int -> Int -> Maybe Int
go !Int
l !Int
r =
let !m :: Int
m = (Int
rInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
l) Int -> Int -> Int
forall a. Integral a => a -> a -> a
`div` Int
2
!x :: sh
x = v sh -> Int -> sh
forall (v :: * -> *) a. Vector v a => v a -> Int -> a
VG.unsafeIndex v sh
v Int
m
in if Int
rInt -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<Int
l then Maybe Int
forall a. Maybe a
Nothing else case sh -> sh -> Ordering
forall a. Ord a => a -> a -> Ordering
compare sh
e sh
x of
Ordering
LT -> Int -> Int -> Maybe Int
go Int
l (Int
mInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
1)
Ordering
EQ -> Int -> Maybe Int
forall a. a -> Maybe a
Just Int
m
Ordering
GT -> Int -> Int -> Maybe Int
go (Int
mInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
1) Int
r
mergeIndexVectors :: (Eq sh, Ord sh, VG.Vector w sh) => w sh -> w sh -> w sh
{-# Inlinable mergeIndexVectors #-}
mergeIndexVectors :: w sh -> w sh -> w sh
mergeIndexVectors w sh
xs w sh
ys = (forall s. ST s (Mutable w s sh)) -> w sh
forall (v :: * -> *) a.
Vector v a =>
(forall s. ST s (Mutable v s a)) -> v a
VG.create ((forall s. ST s (Mutable w s sh)) -> w sh)
-> (forall s. ST s (Mutable w s sh)) -> w sh
forall a b. (a -> b) -> a -> b
$ do
let lxs :: Int
lxs = w sh -> Int
forall (v :: * -> *) a. Vector v a => v a -> Int
VG.length w sh
xs
lys :: Int
lys = w sh -> Int
forall (v :: * -> *) a. Vector v a => v a -> Int
VG.length w sh
ys
Mutable w s sh
mv <- Int -> ST s (Mutable w (PrimState (ST s)) sh)
forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
Int -> m (v (PrimState m) a)
VGM.new (Int -> ST s (Mutable w (PrimState (ST s)) sh))
-> Int -> ST s (Mutable w (PrimState (ST s)) sh)
forall a b. (a -> b) -> a -> b
$ Int
lxs Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
lys
let go :: Int -> Int -> Int -> ST s Int
go !Int
n !Int
i !Int
j
| Int
iInt -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>=Int
lxs Bool -> Bool -> Bool
&& Int
jInt -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>=Int
lys = Int -> ST s Int
forall (m :: * -> *) a. Monad m => a -> m a
return Int
n
| Int
jInt -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>=Int
lys = w sh -> Int -> ST s sh
forall (v :: * -> *) a (m :: * -> *).
(Vector v a, Monad m) =>
v a -> Int -> m a
VG.unsafeIndexM w sh
xs Int
i ST s sh -> (sh -> ST s ()) -> ST s ()
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Mutable w (PrimState (ST s)) sh -> Int -> sh -> ST s ()
forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
v (PrimState m) a -> Int -> a -> m ()
VGM.unsafeWrite Mutable w s sh
Mutable w (PrimState (ST s)) sh
mv Int
n ST s () -> ST s Int -> ST s Int
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Int -> Int -> Int -> ST s Int
go (Int
nInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
1) (Int
iInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
1) Int
j
| Int
iInt -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>=Int
lxs = w sh -> Int -> ST s sh
forall (v :: * -> *) a (m :: * -> *).
(Vector v a, Monad m) =>
v a -> Int -> m a
VG.unsafeIndexM w sh
ys Int
j ST s sh -> (sh -> ST s ()) -> ST s ()
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Mutable w (PrimState (ST s)) sh -> Int -> sh -> ST s ()
forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
v (PrimState m) a -> Int -> a -> m ()
VGM.unsafeWrite Mutable w s sh
Mutable w (PrimState (ST s)) sh
mv Int
n ST s () -> ST s Int -> ST s Int
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Int -> Int -> Int -> ST s Int
go (Int
nInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
1) Int
i (Int
jInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
1)
| Bool
otherwise = do
sh
x <- w sh -> Int -> ST s sh
forall (v :: * -> *) a (m :: * -> *).
(Vector v a, Monad m) =>
v a -> Int -> m a
VG.unsafeIndexM w sh
xs Int
i
sh
y <- w sh -> Int -> ST s sh
forall (v :: * -> *) a (m :: * -> *).
(Vector v a, Monad m) =>
v a -> Int -> m a
VG.unsafeIndexM w sh
ys Int
j
if | sh
xsh -> sh -> Bool
forall a. Eq a => a -> a -> Bool
==sh
y -> Mutable w (PrimState (ST s)) sh -> Int -> sh -> ST s ()
forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
v (PrimState m) a -> Int -> a -> m ()
VGM.unsafeWrite Mutable w s sh
Mutable w (PrimState (ST s)) sh
mv Int
n sh
x ST s () -> ST s Int -> ST s Int
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Int -> Int -> Int -> ST s Int
go (Int
nInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
1) (Int
iInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
1) (Int
jInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
1)
| sh
xsh -> sh -> Bool
forall a. Ord a => a -> a -> Bool
< sh
y -> Mutable w (PrimState (ST s)) sh -> Int -> sh -> ST s ()
forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
v (PrimState m) a -> Int -> a -> m ()
VGM.unsafeWrite Mutable w s sh
Mutable w (PrimState (ST s)) sh
mv Int
n sh
x ST s () -> ST s Int -> ST s Int
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Int -> Int -> Int -> ST s Int
go (Int
nInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
1) (Int
iInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
1) Int
j
| sh
xsh -> sh -> Bool
forall a. Ord a => a -> a -> Bool
> sh
y -> Mutable w (PrimState (ST s)) sh -> Int -> sh -> ST s ()
forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
v (PrimState m) a -> Int -> a -> m ()
VGM.unsafeWrite Mutable w s sh
Mutable w (PrimState (ST s)) sh
mv Int
n sh
y ST s () -> ST s Int -> ST s Int
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Int -> Int -> Int -> ST s Int
go (Int
nInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
1) Int
i (Int
jInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
1)
Int
n <- Int -> Int -> Int -> ST s Int
go Int
0 Int
0 Int
0
Mutable w s sh -> ST s (Mutable w s sh)
forall (m :: * -> *) a. Monad m => a -> m a
return (Mutable w s sh -> ST s (Mutable w s sh))
-> Mutable w s sh -> ST s (Mutable w s sh)
forall a b. (a -> b) -> a -> b
$ Int -> Mutable w s sh -> Mutable w s sh
forall (v :: * -> * -> *) a s. MVector v a => Int -> v s a -> v s a
VGM.unsafeTake Int
n Mutable w s sh
mv