{-# LANGUAGE FlexibleContexts #-}

-- |
-- Module     : Simulation.Aivika.Trans.Vector
-- Copyright  : Copyright (c) 2009-2017, David Sorokin <david.sorokin@gmail.com>
-- License    : BSD3
-- Maintainer : David Sorokin <david.sorokin@gmail.com>
-- Stability  : experimental
-- Tested with: GHC 8.0.1
--
-- An imperative vector.
--
module Simulation.Aivika.Trans.Vector
       (Vector, 
        newVector, 
        copyVector,
        vectorCount, 
        appendVector, 
        readVector, 
        writeVector,
        vectorBinarySearch,
        vectorInsert,
        vectorDeleteAt,
        vectorDeleteRange,
        vectorDelete,
        vectorDeleteBy,
        vectorIndex,
        vectorIndexBy,
        vectorContains,
        vectorContainsBy,
        freezeVector) where 

import Data.Array

import Control.Monad

import Simulation.Aivika.Trans.Simulation
import Simulation.Aivika.Trans.Event
import Simulation.Aivika.Trans.Ref.Base.Lazy

-- | Represents a resizable vector.
data Vector m a = Vector { Vector m a -> Ref m (Array Int (Ref m a))
vectorArrayRef :: Ref m (Array Int (Ref m a)),
                           Vector m a -> Ref m Int
vectorCountRef :: Ref m Int, 
                           Vector m a -> Ref m Int
vectorCapacityRef :: Ref m Int }

-- | Create a new vector.
newVector :: MonadRef m => Simulation m (Vector m a)
{-# INLINABLE newVector #-}
newVector :: Simulation m (Vector m a)
newVector =
  do [Ref m a]
xs <- [Integer]
-> (Integer -> Simulation m (Ref m a)) -> Simulation m [Ref m a]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
t a -> (a -> m b) -> m (t b)
forM [Integer
0 .. Integer
4 Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
- Integer
1] ((Integer -> Simulation m (Ref m a)) -> Simulation m [Ref m a])
-> (Integer -> Simulation m (Ref m a)) -> Simulation m [Ref m a]
forall a b. (a -> b) -> a -> b
$ \Integer
i -> a -> Simulation m (Ref m a)
forall (m :: * -> *) a. MonadRef m => a -> Simulation m (Ref m a)
newRef a
forall a. HasCallStack => a
undefined
     let arr :: Array Int (Ref m a)
arr = (Int, Int) -> [(Int, Ref m a)] -> Array Int (Ref m a)
forall i e. Ix i => (i, i) -> [(i, e)] -> Array i e
array (Int
0, Int
4 Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) ([(Int, Ref m a)] -> Array Int (Ref m a))
-> [(Int, Ref m a)] -> Array Int (Ref m a)
forall a b. (a -> b) -> a -> b
$ [Int] -> [Ref m a] -> [(Int, Ref m a)]
forall a b. [a] -> [b] -> [(a, b)]
zip [Int
0..] [Ref m a]
xs
     Ref m (Array Int (Ref m a))
arrRef   <- Array Int (Ref m a) -> Simulation m (Ref m (Array Int (Ref m a)))
forall (m :: * -> *) a. MonadRef m => a -> Simulation m (Ref m a)
newRef (Array Int (Ref m a) -> Simulation m (Ref m (Array Int (Ref m a))))
-> Array Int (Ref m a)
-> Simulation m (Ref m (Array Int (Ref m a)))
forall a b. (a -> b) -> a -> b
$! Array Int (Ref m a)
arr
     Ref m Int
countRef <- Int -> Simulation m (Ref m Int)
forall (m :: * -> *) a. MonadRef m => a -> Simulation m (Ref m a)
newRef (Int -> Simulation m (Ref m Int))
-> Int -> Simulation m (Ref m Int)
forall a b. (a -> b) -> a -> b
$! Int
0
     Ref m Int
capacityRef <- Int -> Simulation m (Ref m Int)
forall (m :: * -> *) a. MonadRef m => a -> Simulation m (Ref m a)
newRef (Int -> Simulation m (Ref m Int))
-> Int -> Simulation m (Ref m Int)
forall a b. (a -> b) -> a -> b
$! Int
4
     Vector m a -> Simulation m (Vector m a)
forall (m :: * -> *) a. Monad m => a -> m a
return Vector :: forall (m :: * -> *) a.
Ref m (Array Int (Ref m a)) -> Ref m Int -> Ref m Int -> Vector m a
Vector { vectorArrayRef :: Ref m (Array Int (Ref m a))
vectorArrayRef = Ref m (Array Int (Ref m a))
arrRef,
                     vectorCountRef :: Ref m Int
vectorCountRef = Ref m Int
countRef,
                     vectorCapacityRef :: Ref m Int
vectorCapacityRef = Ref m Int
capacityRef }

-- | Copy the vector.
copyVector :: MonadRef m => Vector m a -> Event m (Vector m a)
{-# INLINABLE copyVector #-}
copyVector :: Vector m a -> Event m (Vector m a)
copyVector Vector m a
vector =
  do Array Int (Ref m a)
arr   <- Ref m (Array Int (Ref m a)) -> Event m (Array Int (Ref m a))
forall (m :: * -> *) a. MonadRef m => Ref m a -> Event m a
readRef (Vector m a -> Ref m (Array Int (Ref m a))
forall (m :: * -> *) a. Vector m a -> Ref m (Array Int (Ref m a))
vectorArrayRef Vector m a
vector)
     Int
count <- Ref m Int -> Event m Int
forall (m :: * -> *) a. MonadRef m => Ref m a -> Event m a
readRef (Vector m a -> Ref m Int
forall (m :: * -> *) a. Vector m a -> Ref m Int
vectorCountRef Vector m a
vector)
     [Ref m a]
xs' <-
       [Int] -> (Int -> Event m (Ref m a)) -> Event m [Ref m a]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
t a -> (a -> m b) -> m (t b)
forM [Int
0 .. Int
count Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1] ((Int -> Event m (Ref m a)) -> Event m [Ref m a])
-> (Int -> Event m (Ref m a)) -> Event m [Ref m a]
forall a b. (a -> b) -> a -> b
$ \Int
i ->
       do a
x <- Ref m a -> Event m a
forall (m :: * -> *) a. MonadRef m => Ref m a -> Event m a
readRef (Array Int (Ref m a)
arr Array Int (Ref m a) -> Int -> Ref m a
forall i e. Ix i => Array i e -> i -> e
! Int
i)
          Simulation m (Ref m a) -> Event m (Ref m a)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
SimulationLift t m =>
Simulation m a -> t m a
liftSimulation (Simulation m (Ref m a) -> Event m (Ref m a))
-> Simulation m (Ref m a) -> Event m (Ref m a)
forall a b. (a -> b) -> a -> b
$ a -> Simulation m (Ref m a)
forall (m :: * -> *) a. MonadRef m => a -> Simulation m (Ref m a)
newRef a
x
     let arr' :: Array Int (Ref m a)
arr' = (Int, Int) -> [(Int, Ref m a)] -> Array Int (Ref m a)
forall i e. Ix i => (i, i) -> [(i, e)] -> Array i e
array (Int
0, Int
count Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) ([(Int, Ref m a)] -> Array Int (Ref m a))
-> [(Int, Ref m a)] -> Array Int (Ref m a)
forall a b. (a -> b) -> a -> b
$ [Int] -> [Ref m a] -> [(Int, Ref m a)]
forall a b. [a] -> [b] -> [(a, b)]
zip [Int
0..] [Ref m a]
xs'
     Ref m (Array Int (Ref m a))
arrRef'   <- Simulation m (Ref m (Array Int (Ref m a)))
-> Event m (Ref m (Array Int (Ref m a)))
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
SimulationLift t m =>
Simulation m a -> t m a
liftSimulation (Simulation m (Ref m (Array Int (Ref m a)))
 -> Event m (Ref m (Array Int (Ref m a))))
-> Simulation m (Ref m (Array Int (Ref m a)))
-> Event m (Ref m (Array Int (Ref m a)))
forall a b. (a -> b) -> a -> b
$ Array Int (Ref m a) -> Simulation m (Ref m (Array Int (Ref m a)))
forall (m :: * -> *) a. MonadRef m => a -> Simulation m (Ref m a)
newRef (Array Int (Ref m a) -> Simulation m (Ref m (Array Int (Ref m a))))
-> Array Int (Ref m a)
-> Simulation m (Ref m (Array Int (Ref m a)))
forall a b. (a -> b) -> a -> b
$! Array Int (Ref m a)
arr'
     Ref m Int
countRef' <- Simulation m (Ref m Int) -> Event m (Ref m Int)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
SimulationLift t m =>
Simulation m a -> t m a
liftSimulation (Simulation m (Ref m Int) -> Event m (Ref m Int))
-> Simulation m (Ref m Int) -> Event m (Ref m Int)
forall a b. (a -> b) -> a -> b
$ Int -> Simulation m (Ref m Int)
forall (m :: * -> *) a. MonadRef m => a -> Simulation m (Ref m a)
newRef (Int -> Simulation m (Ref m Int))
-> Int -> Simulation m (Ref m Int)
forall a b. (a -> b) -> a -> b
$! Int
count
     Ref m Int
capacityRef' <- Simulation m (Ref m Int) -> Event m (Ref m Int)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
SimulationLift t m =>
Simulation m a -> t m a
liftSimulation (Simulation m (Ref m Int) -> Event m (Ref m Int))
-> Simulation m (Ref m Int) -> Event m (Ref m Int)
forall a b. (a -> b) -> a -> b
$ Int -> Simulation m (Ref m Int)
forall (m :: * -> *) a. MonadRef m => a -> Simulation m (Ref m a)
newRef (Int -> Simulation m (Ref m Int))
-> Int -> Simulation m (Ref m Int)
forall a b. (a -> b) -> a -> b
$! Int
count
     Vector m a -> Event m (Vector m a)
forall (m :: * -> *) a. Monad m => a -> m a
return Vector :: forall (m :: * -> *) a.
Ref m (Array Int (Ref m a)) -> Ref m Int -> Ref m Int -> Vector m a
Vector { vectorArrayRef :: Ref m (Array Int (Ref m a))
vectorArrayRef = Ref m (Array Int (Ref m a))
arrRef',
                     vectorCountRef :: Ref m Int
vectorCountRef = Ref m Int
countRef',
                     vectorCapacityRef :: Ref m Int
vectorCapacityRef = Ref m Int
capacityRef' }

-- | Ensure that the vector has the specified capacity.
vectorEnsureCapacity :: MonadRef m => Vector m a -> Int -> Event m ()
{-# INLINABLE vectorEnsureCapacity #-}
vectorEnsureCapacity :: Vector m a -> Int -> Event m ()
vectorEnsureCapacity Vector m a
vector Int
capacity =
  do Int
capacity' <- Ref m Int -> Event m Int
forall (m :: * -> *) a. MonadRef m => Ref m a -> Event m a
readRef (Vector m a -> Ref m Int
forall (m :: * -> *) a. Vector m a -> Ref m Int
vectorCapacityRef Vector m a
vector)
     Bool -> Event m () -> Event m ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Int
capacity' Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
capacity) (Event m () -> Event m ()) -> Event m () -> Event m ()
forall a b. (a -> b) -> a -> b
$
       do Array Int (Ref m a)
arr'   <- Ref m (Array Int (Ref m a)) -> Event m (Array Int (Ref m a))
forall (m :: * -> *) a. MonadRef m => Ref m a -> Event m a
readRef (Vector m a -> Ref m (Array Int (Ref m a))
forall (m :: * -> *) a. Vector m a -> Ref m (Array Int (Ref m a))
vectorArrayRef Vector m a
vector)
          Int
count' <- Ref m Int -> Event m Int
forall (m :: * -> *) a. MonadRef m => Ref m a -> Event m a
readRef (Vector m a -> Ref m Int
forall (m :: * -> *) a. Vector m a -> Ref m Int
vectorCountRef Vector m a
vector)
          let capacity'' :: Int
capacity'' = Int -> Int -> Int
forall a. Ord a => a -> a -> a
max (Int
2 Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
capacity') Int
capacity
          [Ref m a]
xs'' <-
            [Int] -> (Int -> Event m (Ref m a)) -> Event m [Ref m a]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
t a -> (a -> m b) -> m (t b)
forM [Int
0 .. Int
capacity'' Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1] ((Int -> Event m (Ref m a)) -> Event m [Ref m a])
-> (Int -> Event m (Ref m a)) -> Event m [Ref m a]
forall a b. (a -> b) -> a -> b
$ \Int
i ->
            Simulation m (Ref m a) -> Event m (Ref m a)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
SimulationLift t m =>
Simulation m a -> t m a
liftSimulation (Simulation m (Ref m a) -> Event m (Ref m a))
-> Simulation m (Ref m a) -> Event m (Ref m a)
forall a b. (a -> b) -> a -> b
$ a -> Simulation m (Ref m a)
forall (m :: * -> *) a. MonadRef m => a -> Simulation m (Ref m a)
newRef a
forall a. HasCallStack => a
undefined
          let arr'' :: Array Int (Ref m a)
arr'' = (Int, Int) -> [(Int, Ref m a)] -> Array Int (Ref m a)
forall i e. Ix i => (i, i) -> [(i, e)] -> Array i e
array (Int
0, Int
capacity'' Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) ([(Int, Ref m a)] -> Array Int (Ref m a))
-> [(Int, Ref m a)] -> Array Int (Ref m a)
forall a b. (a -> b) -> a -> b
$ [Int] -> [Ref m a] -> [(Int, Ref m a)]
forall a b. [a] -> [b] -> [(a, b)]
zip [Int
0..] [Ref m a]
xs''
          [Int] -> (Int -> Event m ()) -> Event m ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [Int
0 .. Int
count' Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1] ((Int -> Event m ()) -> Event m ())
-> (Int -> Event m ()) -> Event m ()
forall a b. (a -> b) -> a -> b
$ \Int
i ->
            do a
x <- Ref m a -> Event m a
forall (m :: * -> *) a. MonadRef m => Ref m a -> Event m a
readRef (Array Int (Ref m a)
arr' Array Int (Ref m a) -> Int -> Ref m a
forall i e. Ix i => Array i e -> i -> e
! Int
i)
               Ref m a -> a -> Event m ()
forall (m :: * -> *) a. MonadRef m => Ref m a -> a -> Event m ()
writeRef (Array Int (Ref m a)
arr'' Array Int (Ref m a) -> Int -> Ref m a
forall i e. Ix i => Array i e -> i -> e
! Int
i) a
x
          Ref m (Array Int (Ref m a)) -> Array Int (Ref m a) -> Event m ()
forall (m :: * -> *) a. MonadRef m => Ref m a -> a -> Event m ()
writeRef (Vector m a -> Ref m (Array Int (Ref m a))
forall (m :: * -> *) a. Vector m a -> Ref m (Array Int (Ref m a))
vectorArrayRef Vector m a
vector) (Array Int (Ref m a) -> Event m ())
-> Array Int (Ref m a) -> Event m ()
forall a b. (a -> b) -> a -> b
$! Array Int (Ref m a)
arr''
          Ref m Int -> Int -> Event m ()
forall (m :: * -> *) a. MonadRef m => Ref m a -> a -> Event m ()
writeRef (Vector m a -> Ref m Int
forall (m :: * -> *) a. Vector m a -> Ref m Int
vectorCapacityRef Vector m a
vector) (Int -> Event m ()) -> Int -> Event m ()
forall a b. (a -> b) -> a -> b
$! Int
capacity''
          
-- | Return the element count.
vectorCount :: MonadRef m => Vector m a -> Event m Int
{-# INLINABLE vectorCount #-}
vectorCount :: Vector m a -> Event m Int
vectorCount Vector m a
vector = Ref m Int -> Event m Int
forall (m :: * -> *) a. MonadRef m => Ref m a -> Event m a
readRef (Vector m a -> Ref m Int
forall (m :: * -> *) a. Vector m a -> Ref m Int
vectorCountRef Vector m a
vector)
          
-- | Add the specified element to the end of the vector.
appendVector :: MonadRef m => Vector m a -> a -> Event m ()          
{-# INLINABLE appendVector #-}
appendVector :: Vector m a -> a -> Event m ()
appendVector Vector m a
vector a
item =
  do Int
count <- Ref m Int -> Event m Int
forall (m :: * -> *) a. MonadRef m => Ref m a -> Event m a
readRef (Vector m a -> Ref m Int
forall (m :: * -> *) a. Vector m a -> Ref m Int
vectorCountRef Vector m a
vector)
     Vector m a -> Int -> Event m ()
forall (m :: * -> *) a.
MonadRef m =>
Vector m a -> Int -> Event m ()
vectorEnsureCapacity Vector m a
vector (Int
count Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)
     Array Int (Ref m a)
arr   <- Ref m (Array Int (Ref m a)) -> Event m (Array Int (Ref m a))
forall (m :: * -> *) a. MonadRef m => Ref m a -> Event m a
readRef (Vector m a -> Ref m (Array Int (Ref m a))
forall (m :: * -> *) a. Vector m a -> Ref m (Array Int (Ref m a))
vectorArrayRef Vector m a
vector)
     Ref m a -> a -> Event m ()
forall (m :: * -> *) a. MonadRef m => Ref m a -> a -> Event m ()
writeRef (Array Int (Ref m a)
arr Array Int (Ref m a) -> Int -> Ref m a
forall i e. Ix i => Array i e -> i -> e
! Int
count) (a -> Event m ()) -> a -> Event m ()
forall a b. (a -> b) -> a -> b
$! a
item
     Ref m Int -> Int -> Event m ()
forall (m :: * -> *) a. MonadRef m => Ref m a -> a -> Event m ()
writeRef (Vector m a -> Ref m Int
forall (m :: * -> *) a. Vector m a -> Ref m Int
vectorCountRef Vector m a
vector) (Int -> Event m ()) -> Int -> Event m ()
forall a b. (a -> b) -> a -> b
$! (Int
count Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)
     
-- | Read a value from the vector, where indices are started from 0.
readVector :: MonadRef m => Vector m a -> Int -> Event m a
{-# INLINABLE readVector #-}
readVector :: Vector m a -> Int -> Event m a
readVector Vector m a
vector Int
index =
  do Array Int (Ref m a)
arr <- Ref m (Array Int (Ref m a)) -> Event m (Array Int (Ref m a))
forall (m :: * -> *) a. MonadRef m => Ref m a -> Event m a
readRef (Vector m a -> Ref m (Array Int (Ref m a))
forall (m :: * -> *) a. Vector m a -> Ref m (Array Int (Ref m a))
vectorArrayRef Vector m a
vector)
     Ref m a -> Event m a
forall (m :: * -> *) a. MonadRef m => Ref m a -> Event m a
readRef (Array Int (Ref m a)
arr Array Int (Ref m a) -> Int -> Ref m a
forall i e. Ix i => Array i e -> i -> e
! Int
index)
          
-- | Set an array item at the specified index which is started from 0.
writeVector :: MonadRef m => Vector m a -> Int -> a -> Event m ()
{-# INLINABLE writeVector #-}
writeVector :: Vector m a -> Int -> a -> Event m ()
writeVector Vector m a
vector Int
index a
item =
  do Array Int (Ref m a)
arr <- Ref m (Array Int (Ref m a)) -> Event m (Array Int (Ref m a))
forall (m :: * -> *) a. MonadRef m => Ref m a -> Event m a
readRef (Vector m a -> Ref m (Array Int (Ref m a))
forall (m :: * -> *) a. Vector m a -> Ref m (Array Int (Ref m a))
vectorArrayRef Vector m a
vector)
     Ref m a -> a -> Event m ()
forall (m :: * -> *) a. MonadRef m => Ref m a -> a -> Event m ()
writeRef (Array Int (Ref m a)
arr Array Int (Ref m a) -> Int -> Ref m a
forall i e. Ix i => Array i e -> i -> e
! Int
index) (a -> Event m ()) -> a -> Event m ()
forall a b. (a -> b) -> a -> b
$! a
item

vectorBinarySearch' :: (MonadRef m, Ord a) => Array Int (Ref m a) -> a -> Int -> Int -> Event m Int
{-# INLINABLE vectorBinarySearch' #-}
vectorBinarySearch' :: Array Int (Ref m a) -> a -> Int -> Int -> Event m Int
vectorBinarySearch' Array Int (Ref m a)
arr a
item Int
left Int
right =
  if Int
left Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
right 
  then Int -> Event m Int
forall (m :: * -> *) a. Monad m => a -> m a
return (Int -> Event m Int) -> Int -> Event m Int
forall a b. (a -> b) -> a -> b
$ - (Int
right Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1
  else
    do let index :: Int
index = (Int
left Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
right) Int -> Int -> Int
forall a. Integral a => a -> a -> a
`div` Int
2
       a
curr <- Ref m a -> Event m a
forall (m :: * -> *) a. MonadRef m => Ref m a -> Event m a
readRef (Array Int (Ref m a)
arr Array Int (Ref m a) -> Int -> Ref m a
forall i e. Ix i => Array i e -> i -> e
! Int
index)
       if a
item a -> a -> Bool
forall a. Ord a => a -> a -> Bool
< a
curr 
         then Array Int (Ref m a) -> a -> Int -> Int -> Event m Int
forall (m :: * -> *) a.
(MonadRef m, Ord a) =>
Array Int (Ref m a) -> a -> Int -> Int -> Event m Int
vectorBinarySearch' Array Int (Ref m a)
arr a
item Int
left (Int
index Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1)
         else if a
item a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
curr
              then Int -> Event m Int
forall (m :: * -> *) a. Monad m => a -> m a
return Int
index
              else Array Int (Ref m a) -> a -> Int -> Int -> Event m Int
forall (m :: * -> *) a.
(MonadRef m, Ord a) =>
Array Int (Ref m a) -> a -> Int -> Int -> Event m Int
vectorBinarySearch' Array Int (Ref m a)
arr a
item (Int
index Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) Int
right
                   
-- | Return the index of the specified element using binary search; otherwise, 
-- a negated insertion index minus one: 0 -> -0 - 1, ..., i -> -i - 1, ....
vectorBinarySearch :: (MonadRef m, Ord a) => Vector m a -> a -> Event m Int
{-# INLINABLE vectorBinarySearch #-}
vectorBinarySearch :: Vector m a -> a -> Event m Int
vectorBinarySearch Vector m a
vector a
item =
  do Array Int (Ref m a)
arr   <- Ref m (Array Int (Ref m a)) -> Event m (Array Int (Ref m a))
forall (m :: * -> *) a. MonadRef m => Ref m a -> Event m a
readRef (Vector m a -> Ref m (Array Int (Ref m a))
forall (m :: * -> *) a. Vector m a -> Ref m (Array Int (Ref m a))
vectorArrayRef Vector m a
vector)
     Int
count <- Ref m Int -> Event m Int
forall (m :: * -> *) a. MonadRef m => Ref m a -> Event m a
readRef (Vector m a -> Ref m Int
forall (m :: * -> *) a. Vector m a -> Ref m Int
vectorCountRef Vector m a
vector)
     Array Int (Ref m a) -> a -> Int -> Int -> Event m Int
forall (m :: * -> *) a.
(MonadRef m, Ord a) =>
Array Int (Ref m a) -> a -> Int -> Int -> Event m Int
vectorBinarySearch' Array Int (Ref m a)
arr a
item Int
0 (Int
count Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1)

-- | Return the elements of the vector in an immutable array.
freezeVector :: MonadRef m => Vector m a -> Event m (Array Int a)
{-# INLINABLE freezeVector #-}
freezeVector :: Vector m a -> Event m (Array Int a)
freezeVector Vector m a
vector = 
  do Array Int (Ref m a)
arr   <- Ref m (Array Int (Ref m a)) -> Event m (Array Int (Ref m a))
forall (m :: * -> *) a. MonadRef m => Ref m a -> Event m a
readRef (Vector m a -> Ref m (Array Int (Ref m a))
forall (m :: * -> *) a. Vector m a -> Ref m (Array Int (Ref m a))
vectorArrayRef Vector m a
vector)
     Int
count <- Ref m Int -> Event m Int
forall (m :: * -> *) a. MonadRef m => Ref m a -> Event m a
readRef (Vector m a -> Ref m Int
forall (m :: * -> *) a. Vector m a -> Ref m Int
vectorCountRef Vector m a
vector)
     [a]
xs' <-
       [Int] -> (Int -> Event m a) -> Event m [a]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
t a -> (a -> m b) -> m (t b)
forM [Int
0 .. Int
count Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1] ((Int -> Event m a) -> Event m [a])
-> (Int -> Event m a) -> Event m [a]
forall a b. (a -> b) -> a -> b
$ \Int
i ->
       Ref m a -> Event m a
forall (m :: * -> *) a. MonadRef m => Ref m a -> Event m a
readRef (Array Int (Ref m a)
arr Array Int (Ref m a) -> Int -> Ref m a
forall i e. Ix i => Array i e -> i -> e
! Int
i)
     let arr' :: Array Int a
arr' = (Int, Int) -> [(Int, a)] -> Array Int a
forall i e. Ix i => (i, i) -> [(i, e)] -> Array i e
array (Int
0, Int
count Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) ([(Int, a)] -> Array Int a) -> [(Int, a)] -> Array Int a
forall a b. (a -> b) -> a -> b
$ [Int] -> [a] -> [(Int, a)]
forall a b. [a] -> [b] -> [(a, b)]
zip [Int
0..] [a]
xs'
     Array Int a -> Event m (Array Int a)
forall (m :: * -> *) a. Monad m => a -> m a
return Array Int a
arr'
     
-- | Insert the element in the vector at the specified index.
vectorInsert :: MonadRef m => Vector m a -> Int -> a -> Event m ()          
{-# INLINABLE vectorInsert #-}
vectorInsert :: Vector m a -> Int -> a -> Event m ()
vectorInsert Vector m a
vector Int
index a
item =
  do Int
count <- Ref m Int -> Event m Int
forall (m :: * -> *) a. MonadRef m => Ref m a -> Event m a
readRef (Vector m a -> Ref m Int
forall (m :: * -> *) a. Vector m a -> Ref m Int
vectorCountRef Vector m a
vector)
     Bool -> Event m () -> Event m ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Int
index Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
0) (Event m () -> Event m ()) -> Event m () -> Event m ()
forall a b. (a -> b) -> a -> b
$
       [Char] -> Event m ()
forall a. HasCallStack => [Char] -> a
error ([Char] -> Event m ()) -> [Char] -> Event m ()
forall a b. (a -> b) -> a -> b
$
       [Char]
"Index cannot be " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++
       [Char]
"negative: vectorInsert."
     Bool -> Event m () -> Event m ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Int
index Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
count) (Event m () -> Event m ()) -> Event m () -> Event m ()
forall a b. (a -> b) -> a -> b
$
       [Char] -> Event m ()
forall a. HasCallStack => [Char] -> a
error ([Char] -> Event m ()) -> [Char] -> Event m ()
forall a b. (a -> b) -> a -> b
$
       [Char]
"Index cannot be greater " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++
       [Char]
"than the count: vectorInsert."
     Vector m a -> Int -> Event m ()
forall (m :: * -> *) a.
MonadRef m =>
Vector m a -> Int -> Event m ()
vectorEnsureCapacity Vector m a
vector (Int
count Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)
     Array Int (Ref m a)
arr <- Ref m (Array Int (Ref m a)) -> Event m (Array Int (Ref m a))
forall (m :: * -> *) a. MonadRef m => Ref m a -> Event m a
readRef (Vector m a -> Ref m (Array Int (Ref m a))
forall (m :: * -> *) a. Vector m a -> Ref m (Array Int (Ref m a))
vectorArrayRef Vector m a
vector)
     [Int] -> (Int -> Event m ()) -> Event m ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [Int
count, Int
count Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1 .. Int
index Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1] ((Int -> Event m ()) -> Event m ())
-> (Int -> Event m ()) -> Event m ()
forall a b. (a -> b) -> a -> b
$ \Int
i ->
       do a
x <- Ref m a -> Event m a
forall (m :: * -> *) a. MonadRef m => Ref m a -> Event m a
readRef (Array Int (Ref m a)
arr Array Int (Ref m a) -> Int -> Ref m a
forall i e. Ix i => Array i e -> i -> e
! (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1))
          Ref m a -> a -> Event m ()
forall (m :: * -> *) a. MonadRef m => Ref m a -> a -> Event m ()
writeRef (Array Int (Ref m a)
arr Array Int (Ref m a) -> Int -> Ref m a
forall i e. Ix i => Array i e -> i -> e
! Int
i) a
x
     Ref m a -> a -> Event m ()
forall (m :: * -> *) a. MonadRef m => Ref m a -> a -> Event m ()
writeRef (Array Int (Ref m a)
arr Array Int (Ref m a) -> Int -> Ref m a
forall i e. Ix i => Array i e -> i -> e
! Int
index) (a -> Event m ()) -> a -> Event m ()
forall a b. (a -> b) -> a -> b
$! a
item
     Ref m Int -> Int -> Event m ()
forall (m :: * -> *) a. MonadRef m => Ref m a -> a -> Event m ()
writeRef (Vector m a -> Ref m Int
forall (m :: * -> *) a. Vector m a -> Ref m Int
vectorCountRef Vector m a
vector) (Int -> Event m ()) -> Int -> Event m ()
forall a b. (a -> b) -> a -> b
$! (Int
count Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)
     
-- | Delete the element at the specified index.
vectorDeleteAt :: MonadRef m => Vector m a -> Int -> Event m ()
{-# INLINABLE vectorDeleteAt #-}
vectorDeleteAt :: Vector m a -> Int -> Event m ()
vectorDeleteAt Vector m a
vector Int
index =
  do Int
count <- Ref m Int -> Event m Int
forall (m :: * -> *) a. MonadRef m => Ref m a -> Event m a
readRef (Vector m a -> Ref m Int
forall (m :: * -> *) a. Vector m a -> Ref m Int
vectorCountRef Vector m a
vector)
     Bool -> Event m () -> Event m ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Int
index Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
0) (Event m () -> Event m ()) -> Event m () -> Event m ()
forall a b. (a -> b) -> a -> b
$
       [Char] -> Event m ()
forall a. HasCallStack => [Char] -> a
error ([Char] -> Event m ()) -> [Char] -> Event m ()
forall a b. (a -> b) -> a -> b
$
       [Char]
"Index cannot be " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++
       [Char]
"negative: vectorDeleteAt."
     Bool -> Event m () -> Event m ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Int
index Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
count) (Event m () -> Event m ()) -> Event m () -> Event m ()
forall a b. (a -> b) -> a -> b
$
       [Char] -> Event m ()
forall a. HasCallStack => [Char] -> a
error ([Char] -> Event m ()) -> [Char] -> Event m ()
forall a b. (a -> b) -> a -> b
$
       [Char]
"Index must be less " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++
       [Char]
"than the count: vectorDeleteAt."
     Array Int (Ref m a)
arr <- Ref m (Array Int (Ref m a)) -> Event m (Array Int (Ref m a))
forall (m :: * -> *) a. MonadRef m => Ref m a -> Event m a
readRef (Vector m a -> Ref m (Array Int (Ref m a))
forall (m :: * -> *) a. Vector m a -> Ref m (Array Int (Ref m a))
vectorArrayRef Vector m a
vector)
     [Int] -> (Int -> Event m ()) -> Event m ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [Int
index, Int
index Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1 .. Int
count Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
2] ((Int -> Event m ()) -> Event m ())
-> (Int -> Event m ()) -> Event m ()
forall a b. (a -> b) -> a -> b
$ \Int
i ->
       do a
x <- Ref m a -> Event m a
forall (m :: * -> *) a. MonadRef m => Ref m a -> Event m a
readRef (Array Int (Ref m a)
arr Array Int (Ref m a) -> Int -> Ref m a
forall i e. Ix i => Array i e -> i -> e
! (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1))
          Ref m a -> a -> Event m ()
forall (m :: * -> *) a. MonadRef m => Ref m a -> a -> Event m ()
writeRef (Array Int (Ref m a)
arr Array Int (Ref m a) -> Int -> Ref m a
forall i e. Ix i => Array i e -> i -> e
! Int
i) a
x
     Ref m a -> a -> Event m ()
forall (m :: * -> *) a. MonadRef m => Ref m a -> a -> Event m ()
writeRef (Array Int (Ref m a)
arr Array Int (Ref m a) -> Int -> Ref m a
forall i e. Ix i => Array i e -> i -> e
! (Int
count Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1)) a
forall a. HasCallStack => a
undefined
     Ref m Int -> Int -> Event m ()
forall (m :: * -> *) a. MonadRef m => Ref m a -> a -> Event m ()
writeRef (Vector m a -> Ref m Int
forall (m :: * -> *) a. Vector m a -> Ref m Int
vectorCountRef Vector m a
vector) (Int -> Event m ()) -> Int -> Event m ()
forall a b. (a -> b) -> a -> b
$! (Int
count Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1)

-- | Delete the specified range of elements.
vectorDeleteRange :: MonadRef m
                     => Vector m a
                     -- ^ the vector
                     -> Int
                     -- ^ the start index
                     -> Int
                     -- ^ the count of items to be removed
                     -> Event m ()
{-# INLINABLE vectorDeleteRange #-}
vectorDeleteRange :: Vector m a -> Int -> Int -> Event m ()
vectorDeleteRange Vector m a
vector Int
index Int
len =
  do Int
count <- Ref m Int -> Event m Int
forall (m :: * -> *) a. MonadRef m => Ref m a -> Event m a
readRef (Vector m a -> Ref m Int
forall (m :: * -> *) a. Vector m a -> Ref m Int
vectorCountRef Vector m a
vector)
     Bool -> Event m () -> Event m ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Int
index Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
0) (Event m () -> Event m ()) -> Event m () -> Event m ()
forall a b. (a -> b) -> a -> b
$
       [Char] -> Event m ()
forall a. HasCallStack => [Char] -> a
error ([Char] -> Event m ()) -> [Char] -> Event m ()
forall a b. (a -> b) -> a -> b
$
       [Char]
"The first index cannot be " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++
       [Char]
"negative: vectorDeleteRange."
     Bool -> Event m () -> Event m ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Int
index Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
len Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1 Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
count) (Event m () -> Event m ()) -> Event m () -> Event m ()
forall a b. (a -> b) -> a -> b
$
       [Char] -> Event m ()
forall a. HasCallStack => [Char] -> a
error ([Char] -> Event m ()) -> [Char] -> Event m ()
forall a b. (a -> b) -> a -> b
$
       [Char]
"The last index must be less " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++
       [Char]
"than the count: vectorDeleteRange."
     Bool -> Event m () -> Event m ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Int
len Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
0) (Event m () -> Event m ()) -> Event m () -> Event m ()
forall a b. (a -> b) -> a -> b
$
       [Char] -> Event m ()
forall a. HasCallStack => [Char] -> a
error [Char]
"Negative range length: vectorDeleteRange." 
     Array Int (Ref m a)
arr <- Ref m (Array Int (Ref m a)) -> Event m (Array Int (Ref m a))
forall (m :: * -> *) a. MonadRef m => Ref m a -> Event m a
readRef (Vector m a -> Ref m (Array Int (Ref m a))
forall (m :: * -> *) a. Vector m a -> Ref m (Array Int (Ref m a))
vectorArrayRef Vector m a
vector)
     [Int] -> (Int -> Event m ()) -> Event m ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [Int
index, Int
index Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1 .. (Int
count Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
len) Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1] ((Int -> Event m ()) -> Event m ())
-> (Int -> Event m ()) -> Event m ()
forall a b. (a -> b) -> a -> b
$ \Int
i ->
       do a
x <- Ref m a -> Event m a
forall (m :: * -> *) a. MonadRef m => Ref m a -> Event m a
readRef (Array Int (Ref m a)
arr Array Int (Ref m a) -> Int -> Ref m a
forall i e. Ix i => Array i e -> i -> e
! (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
len))
          Ref m a -> a -> Event m ()
forall (m :: * -> *) a. MonadRef m => Ref m a -> a -> Event m ()
writeRef (Array Int (Ref m a)
arr Array Int (Ref m a) -> Int -> Ref m a
forall i e. Ix i => Array i e -> i -> e
! Int
i) a
x
     [Int] -> (Int -> Event m ()) -> Event m ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [(Int
count Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
len) .. Int
count Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1] ((Int -> Event m ()) -> Event m ())
-> (Int -> Event m ()) -> Event m ()
forall a b. (a -> b) -> a -> b
$ \Int
i ->
       Ref m a -> a -> Event m ()
forall (m :: * -> *) a. MonadRef m => Ref m a -> a -> Event m ()
writeRef (Array Int (Ref m a)
arr Array Int (Ref m a) -> Int -> Ref m a
forall i e. Ix i => Array i e -> i -> e
! Int
i) a
forall a. HasCallStack => a
undefined
     Ref m Int -> Int -> Event m ()
forall (m :: * -> *) a. MonadRef m => Ref m a -> a -> Event m ()
writeRef (Vector m a -> Ref m Int
forall (m :: * -> *) a. Vector m a -> Ref m Int
vectorCountRef Vector m a
vector) (Int -> Event m ()) -> Int -> Event m ()
forall a b. (a -> b) -> a -> b
$! (Int
count Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
len)
     
-- | Return the index of the item or -1.     
vectorIndex :: (MonadRef m, Eq a) => Vector m a -> a -> Event m Int
{-# INLINABLE vectorIndex #-}
vectorIndex :: Vector m a -> a -> Event m Int
vectorIndex Vector m a
vector a
item =
  do Int
count <- Ref m Int -> Event m Int
forall (m :: * -> *) a. MonadRef m => Ref m a -> Event m a
readRef (Vector m a -> Ref m Int
forall (m :: * -> *) a. Vector m a -> Ref m Int
vectorCountRef Vector m a
vector)
     Array Int (Ref m a)
arr   <- Ref m (Array Int (Ref m a)) -> Event m (Array Int (Ref m a))
forall (m :: * -> *) a. MonadRef m => Ref m a -> Event m a
readRef (Vector m a -> Ref m (Array Int (Ref m a))
forall (m :: * -> *) a. Vector m a -> Ref m (Array Int (Ref m a))
vectorArrayRef Vector m a
vector)
     let loop :: Int -> Event m Int
loop Int
index =
           if Int
index Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
count
           then Int -> Event m Int
forall (m :: * -> *) a. Monad m => a -> m a
return (Int -> Event m Int) -> Int -> Event m Int
forall a b. (a -> b) -> a -> b
$ -Int
1
           else do a
x <- Ref m a -> Event m a
forall (m :: * -> *) a. MonadRef m => Ref m a -> Event m a
readRef (Array Int (Ref m a)
arr Array Int (Ref m a) -> Int -> Ref m a
forall i e. Ix i => Array i e -> i -> e
! Int
index)
                   if a
item a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
x
                     then Int -> Event m Int
forall (m :: * -> *) a. Monad m => a -> m a
return Int
index
                     else Int -> Event m Int
loop (Int -> Event m Int) -> Int -> Event m Int
forall a b. (a -> b) -> a -> b
$ Int
index Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1
     Int -> Event m Int
loop Int
0
     
-- | Return an index of the item satisfying the predicate or -1.     
vectorIndexBy :: MonadRef m => Vector m a -> (a -> Bool) -> Event m Int
{-# INLINABLE vectorIndexBy #-}
vectorIndexBy :: Vector m a -> (a -> Bool) -> Event m Int
vectorIndexBy Vector m a
vector a -> Bool
pred =
  do Int
count <- Ref m Int -> Event m Int
forall (m :: * -> *) a. MonadRef m => Ref m a -> Event m a
readRef (Vector m a -> Ref m Int
forall (m :: * -> *) a. Vector m a -> Ref m Int
vectorCountRef Vector m a
vector)
     Array Int (Ref m a)
arr   <- Ref m (Array Int (Ref m a)) -> Event m (Array Int (Ref m a))
forall (m :: * -> *) a. MonadRef m => Ref m a -> Event m a
readRef (Vector m a -> Ref m (Array Int (Ref m a))
forall (m :: * -> *) a. Vector m a -> Ref m (Array Int (Ref m a))
vectorArrayRef Vector m a
vector)
     let loop :: Int -> Event m Int
loop Int
index =
           if Int
index Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
count
           then Int -> Event m Int
forall (m :: * -> *) a. Monad m => a -> m a
return (Int -> Event m Int) -> Int -> Event m Int
forall a b. (a -> b) -> a -> b
$ -Int
1
           else do a
x <- Ref m a -> Event m a
forall (m :: * -> *) a. MonadRef m => Ref m a -> Event m a
readRef (Array Int (Ref m a)
arr Array Int (Ref m a) -> Int -> Ref m a
forall i e. Ix i => Array i e -> i -> e
! Int
index)
                   if a -> Bool
pred a
x
                     then Int -> Event m Int
forall (m :: * -> *) a. Monad m => a -> m a
return Int
index
                     else Int -> Event m Int
loop (Int -> Event m Int) -> Int -> Event m Int
forall a b. (a -> b) -> a -> b
$ Int
index Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1
     Int -> Event m Int
loop Int
0

-- | Remove the specified element and return a flag indicating
-- whether the element was found and removed.
vectorDelete :: (MonadRef m, Eq a) => Vector m a -> a -> Event m Bool
{-# INLINABLE vectorDelete #-}
vectorDelete :: Vector m a -> a -> Event m Bool
vectorDelete Vector m a
vector a
item =
  do Int
index <- Vector m a -> a -> Event m Int
forall (m :: * -> *) a.
(MonadRef m, Eq a) =>
Vector m a -> a -> Event m Int
vectorIndex Vector m a
vector a
item
     if Int
index Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
0
       then do Vector m a -> Int -> Event m ()
forall (m :: * -> *) a.
MonadRef m =>
Vector m a -> Int -> Event m ()
vectorDeleteAt Vector m a
vector Int
index
               Bool -> Event m Bool
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
True
       else Bool -> Event m Bool
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
False
            
-- | Remove an element by the specified predicate and return the element if found.
vectorDeleteBy :: MonadRef m => Vector m a -> (a -> Bool) -> Event m (Maybe a)
{-# INLINABLE vectorDeleteBy #-}
vectorDeleteBy :: Vector m a -> (a -> Bool) -> Event m (Maybe a)
vectorDeleteBy Vector m a
vector a -> Bool
pred =
  do Int
index <- Vector m a -> (a -> Bool) -> Event m Int
forall (m :: * -> *) a.
MonadRef m =>
Vector m a -> (a -> Bool) -> Event m Int
vectorIndexBy Vector m a
vector a -> Bool
pred
     if Int
index Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
0
       then do a
a <- Vector m a -> Int -> Event m a
forall (m :: * -> *) a.
MonadRef m =>
Vector m a -> Int -> Event m a
readVector Vector m a
vector Int
index
               Vector m a -> Int -> Event m ()
forall (m :: * -> *) a.
MonadRef m =>
Vector m a -> Int -> Event m ()
vectorDeleteAt Vector m a
vector Int
index
               Maybe a -> Event m (Maybe a)
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> Maybe a
forall a. a -> Maybe a
Just a
a)
       else Maybe a -> Event m (Maybe a)
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe a
forall a. Maybe a
Nothing

-- | Detect whether the specified element is contained in the vector.
vectorContains :: (MonadRef m, Eq a) => Vector m a -> a -> Event m Bool
{-# INLINABLE vectorContains #-}
vectorContains :: Vector m a -> a -> Event m Bool
vectorContains Vector m a
vector a
item =
  do Int
index <- Vector m a -> a -> Event m Int
forall (m :: * -> *) a.
(MonadRef m, Eq a) =>
Vector m a -> a -> Event m Int
vectorIndex Vector m a
vector a
item
     Bool -> Event m Bool
forall (m :: * -> *) a. Monad m => a -> m a
return (Int
index Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
0)
            
-- | Detect whether an element satisfying the specified predicate is contained in the vector.
vectorContainsBy :: MonadRef m => Vector m a -> (a -> Bool) -> Event m (Maybe a)
{-# INLINABLE vectorContainsBy #-}
vectorContainsBy :: Vector m a -> (a -> Bool) -> Event m (Maybe a)
vectorContainsBy Vector m a
vector a -> Bool
pred =
  do Int
index <- Vector m a -> (a -> Bool) -> Event m Int
forall (m :: * -> *) a.
MonadRef m =>
Vector m a -> (a -> Bool) -> Event m Int
vectorIndexBy Vector m a
vector a -> Bool
pred
     if Int
index Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
0
       then do a
a <- Vector m a -> Int -> Event m a
forall (m :: * -> *) a.
MonadRef m =>
Vector m a -> Int -> Event m a
readVector Vector m a
vector Int
index
               Maybe a -> Event m (Maybe a)
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> Maybe a
forall a. a -> Maybe a
Just a
a)
       else Maybe a -> Event m (Maybe a)
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe a
forall a. Maybe a
Nothing