{-# LANGUAGE FlexibleContexts #-}
module Simulation.Aivika.Vector
(Vector,
newVector,
copyVector,
vectorCount,
appendVector,
readVector,
writeVector,
vectorBinarySearch,
vectorInsert,
vectorDeleteAt,
vectorDeleteRange,
vectorDelete,
vectorDeleteBy,
vectorIndex,
vectorIndexBy,
vectorContains,
vectorContainsBy,
freezeVector) where
import Data.Array
import Data.Array.MArray.Safe
import Data.Array.IO.Safe
import Data.IORef
import Control.Monad
data Vector a = Vector { forall a. Vector a -> IORef (IOArray Int a)
vectorArrayRef :: IORef (IOArray Int a),
forall a. Vector a -> IORef Int
vectorCountRef :: IORef Int,
forall a. Vector a -> IORef Int
vectorCapacityRef :: IORef Int }
newVector :: IO (Vector a)
newVector :: forall a. IO (Vector a)
newVector =
do IOArray Int a
array <- forall (a :: * -> * -> *) e (m :: * -> *) i.
(MArray a e m, Ix i) =>
(i, i) -> m (a i e)
newArray_ (Int
0, Int
4 forall a. Num a => a -> a -> a
- Int
1)
IORef (IOArray Int a)
arrayRef <- forall a. a -> IO (IORef a)
newIORef IOArray Int a
array
IORef Int
countRef <- forall a. a -> IO (IORef a)
newIORef Int
0
IORef Int
capacityRef <- forall a. a -> IO (IORef a)
newIORef Int
4
forall (m :: * -> *) a. Monad m => a -> m a
return Vector { vectorArrayRef :: IORef (IOArray Int a)
vectorArrayRef = IORef (IOArray Int a)
arrayRef,
vectorCountRef :: IORef Int
vectorCountRef = IORef Int
countRef,
vectorCapacityRef :: IORef Int
vectorCapacityRef = IORef Int
capacityRef }
copyVector :: Vector a -> IO (Vector a)
copyVector :: forall a. Vector a -> IO (Vector a)
copyVector Vector a
vector =
do IOArray Int a
array <- forall a. IORef a -> IO a
readIORef (forall a. Vector a -> IORef (IOArray Int a)
vectorArrayRef Vector a
vector)
Int
count <- forall a. IORef a -> IO a
readIORef (forall a. Vector a -> IORef Int
vectorCountRef Vector a
vector)
IOArray Int a
array' <- forall (a :: * -> * -> *) e (m :: * -> *) i.
(MArray a e m, Ix i) =>
(i, i) -> m (a i e)
newArray_ (Int
0, Int
count forall a. Num a => a -> a -> a
- Int
1)
IORef (IOArray Int a)
arrayRef' <- forall a. a -> IO (IORef a)
newIORef IOArray Int a
array'
IORef Int
countRef' <- forall a. a -> IO (IORef a)
newIORef Int
count
IORef Int
capacityRef' <- forall a. a -> IO (IORef a)
newIORef Int
count
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [Int
0 .. Int
count forall a. Num a => a -> a -> a
- Int
1] forall a b. (a -> b) -> a -> b
$ \Int
i ->
do a
x <- forall (a :: * -> * -> *) e (m :: * -> *) i.
(MArray a e m, Ix i) =>
a i e -> i -> m e
readArray IOArray Int a
array Int
i
forall (a :: * -> * -> *) e (m :: * -> *) i.
(MArray a e m, Ix i) =>
a i e -> i -> e -> m ()
writeArray IOArray Int a
array' Int
i a
x
forall (m :: * -> *) a. Monad m => a -> m a
return Vector { vectorArrayRef :: IORef (IOArray Int a)
vectorArrayRef = IORef (IOArray Int a)
arrayRef',
vectorCountRef :: IORef Int
vectorCountRef = IORef Int
countRef',
vectorCapacityRef :: IORef Int
vectorCapacityRef = IORef Int
capacityRef' }
vectorEnsureCapacity :: Vector a -> Int -> IO ()
vectorEnsureCapacity :: forall a. Vector a -> Int -> IO ()
vectorEnsureCapacity Vector a
vector Int
capacity =
do Int
capacity' <- forall a. IORef a -> IO a
readIORef (forall a. Vector a -> IORef Int
vectorCapacityRef Vector a
vector)
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Int
capacity' forall a. Ord a => a -> a -> Bool
< Int
capacity) forall a b. (a -> b) -> a -> b
$
do IOArray Int a
array' <- forall a. IORef a -> IO a
readIORef (forall a. Vector a -> IORef (IOArray Int a)
vectorArrayRef Vector a
vector)
Int
count' <- forall a. IORef a -> IO a
readIORef (forall a. Vector a -> IORef Int
vectorCountRef Vector a
vector)
let capacity'' :: Int
capacity'' = forall a. Ord a => a -> a -> a
max (Int
2 forall a. Num a => a -> a -> a
* Int
capacity') Int
capacity
IOArray Int a
array'' <- forall (a :: * -> * -> *) e (m :: * -> *) i.
(MArray a e m, Ix i) =>
(i, i) -> m (a i e)
newArray_ (Int
0, Int
capacity'' forall a. Num a => a -> a -> a
- Int
1)
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [Int
0 .. Int
count' forall a. Num a => a -> a -> a
- Int
1] forall a b. (a -> b) -> a -> b
$ \Int
i ->
do a
x <- forall (a :: * -> * -> *) e (m :: * -> *) i.
(MArray a e m, Ix i) =>
a i e -> i -> m e
readArray IOArray Int a
array' Int
i
forall (a :: * -> * -> *) e (m :: * -> *) i.
(MArray a e m, Ix i) =>
a i e -> i -> e -> m ()
writeArray IOArray Int a
array'' Int
i a
x
forall a. IORef a -> a -> IO ()
writeIORef (forall a. Vector a -> IORef (IOArray Int a)
vectorArrayRef Vector a
vector) IOArray Int a
array''
forall a. IORef a -> a -> IO ()
writeIORef (forall a. Vector a -> IORef Int
vectorCapacityRef Vector a
vector) Int
capacity''
vectorCount :: Vector a -> IO Int
vectorCount :: forall a. Vector a -> IO Int
vectorCount Vector a
vector = forall a. IORef a -> IO a
readIORef (forall a. Vector a -> IORef Int
vectorCountRef Vector a
vector)
appendVector :: Vector a -> a -> IO ()
appendVector :: forall a. Vector a -> a -> IO ()
appendVector Vector a
vector a
item =
do Int
count <- forall a. IORef a -> IO a
readIORef (forall a. Vector a -> IORef Int
vectorCountRef Vector a
vector)
forall a. Vector a -> Int -> IO ()
vectorEnsureCapacity Vector a
vector (Int
count forall a. Num a => a -> a -> a
+ Int
1)
IOArray Int a
array <- forall a. IORef a -> IO a
readIORef (forall a. Vector a -> IORef (IOArray Int a)
vectorArrayRef Vector a
vector)
forall (a :: * -> * -> *) e (m :: * -> *) i.
(MArray a e m, Ix i) =>
a i e -> i -> e -> m ()
writeArray IOArray Int a
array Int
count a
item
forall a. IORef a -> a -> IO ()
writeIORef (forall a. Vector a -> IORef Int
vectorCountRef Vector a
vector) (Int
count forall a. Num a => a -> a -> a
+ Int
1)
readVector :: Vector a -> Int -> IO a
readVector :: forall a. Vector a -> Int -> IO a
readVector Vector a
vector Int
index =
do IOArray Int a
array <- forall a. IORef a -> IO a
readIORef (forall a. Vector a -> IORef (IOArray Int a)
vectorArrayRef Vector a
vector)
forall (a :: * -> * -> *) e (m :: * -> *) i.
(MArray a e m, Ix i) =>
a i e -> i -> m e
readArray IOArray Int a
array Int
index
writeVector :: Vector a -> Int -> a -> IO ()
writeVector :: forall a. Vector a -> Int -> a -> IO ()
writeVector Vector a
vector Int
index a
item =
do IOArray Int a
array <- forall a. IORef a -> IO a
readIORef (forall a. Vector a -> IORef (IOArray Int a)
vectorArrayRef Vector a
vector)
forall (a :: * -> * -> *) e (m :: * -> *) i.
(MArray a e m, Ix i) =>
a i e -> i -> e -> m ()
writeArray IOArray Int a
array Int
index a
item
vectorBinarySearch' :: Ord a => IOArray Int a -> a -> Int -> Int -> IO Int
vectorBinarySearch' :: forall a. Ord a => IOArray Int a -> a -> Int -> Int -> IO Int
vectorBinarySearch' IOArray Int a
array a
item Int
left Int
right =
if Int
left forall a. Ord a => a -> a -> Bool
> Int
right
then forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ - (Int
right forall a. Num a => a -> a -> a
+ Int
1) forall a. Num a => a -> a -> a
- Int
1
else
do let index :: Int
index = (Int
left forall a. Num a => a -> a -> a
+ Int
right) forall a. Integral a => a -> a -> a
`div` Int
2
a
curr <- forall (a :: * -> * -> *) e (m :: * -> *) i.
(MArray a e m, Ix i) =>
a i e -> i -> m e
readArray IOArray Int a
array Int
index
if a
item forall a. Ord a => a -> a -> Bool
< a
curr
then forall a. Ord a => IOArray Int a -> a -> Int -> Int -> IO Int
vectorBinarySearch' IOArray Int a
array a
item Int
left (Int
index forall a. Num a => a -> a -> a
- Int
1)
else if a
item forall a. Eq a => a -> a -> Bool
== a
curr
then forall (m :: * -> *) a. Monad m => a -> m a
return Int
index
else forall a. Ord a => IOArray Int a -> a -> Int -> Int -> IO Int
vectorBinarySearch' IOArray Int a
array a
item (Int
index forall a. Num a => a -> a -> a
+ Int
1) Int
right
vectorBinarySearch :: Ord a => Vector a -> a -> IO Int
vectorBinarySearch :: forall a. Ord a => Vector a -> a -> IO Int
vectorBinarySearch Vector a
vector a
item =
do IOArray Int a
array <- forall a. IORef a -> IO a
readIORef (forall a. Vector a -> IORef (IOArray Int a)
vectorArrayRef Vector a
vector)
Int
count <- forall a. IORef a -> IO a
readIORef (forall a. Vector a -> IORef Int
vectorCountRef Vector a
vector)
forall a. Ord a => IOArray Int a -> a -> Int -> Int -> IO Int
vectorBinarySearch' IOArray Int a
array a
item Int
0 (Int
count forall a. Num a => a -> a -> a
- Int
1)
freezeVector :: Vector a -> IO (Array Int a)
freezeVector :: forall a. Vector a -> IO (Array Int a)
freezeVector Vector a
vector =
do Vector a
vector' <- forall a. Vector a -> IO (Vector a)
copyVector Vector a
vector
IOArray Int a
array <- forall a. IORef a -> IO a
readIORef (forall a. Vector a -> IORef (IOArray Int a)
vectorArrayRef Vector a
vector')
forall i (a :: * -> * -> *) e (m :: * -> *) (b :: * -> * -> *).
(Ix i, MArray a e m, IArray b e) =>
a i e -> m (b i e)
freeze IOArray Int a
array
vectorInsert :: Vector a -> Int -> a -> IO ()
vectorInsert :: forall a. Vector a -> Int -> a -> IO ()
vectorInsert Vector a
vector Int
index a
item =
do Int
count <- forall a. IORef a -> IO a
readIORef (forall a. Vector a -> IORef Int
vectorCountRef Vector a
vector)
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Int
index forall a. Ord a => a -> a -> Bool
< Int
0) forall a b. (a -> b) -> a -> b
$
forall a. HasCallStack => [Char] -> a
error forall a b. (a -> b) -> a -> b
$
[Char]
"Index cannot be " forall a. [a] -> [a] -> [a]
++
[Char]
"negative: vectorInsert."
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Int
index forall a. Ord a => a -> a -> Bool
> Int
count) forall a b. (a -> b) -> a -> b
$
forall a. HasCallStack => [Char] -> a
error forall a b. (a -> b) -> a -> b
$
[Char]
"Index cannot be greater " forall a. [a] -> [a] -> [a]
++
[Char]
"than the count: vectorInsert."
forall a. Vector a -> Int -> IO ()
vectorEnsureCapacity Vector a
vector (Int
count forall a. Num a => a -> a -> a
+ Int
1)
IOArray Int a
array <- forall a. IORef a -> IO a
readIORef (forall a. Vector a -> IORef (IOArray Int a)
vectorArrayRef Vector a
vector)
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [Int
count, Int
count forall a. Num a => a -> a -> a
- Int
1 .. Int
index forall a. Num a => a -> a -> a
+ Int
1] forall a b. (a -> b) -> a -> b
$ \Int
i ->
do a
x <- forall (a :: * -> * -> *) e (m :: * -> *) i.
(MArray a e m, Ix i) =>
a i e -> i -> m e
readArray IOArray Int a
array (Int
i forall a. Num a => a -> a -> a
- Int
1)
forall (a :: * -> * -> *) e (m :: * -> *) i.
(MArray a e m, Ix i) =>
a i e -> i -> e -> m ()
writeArray IOArray Int a
array Int
i a
x
forall (a :: * -> * -> *) e (m :: * -> *) i.
(MArray a e m, Ix i) =>
a i e -> i -> e -> m ()
writeArray IOArray Int a
array Int
index a
item
forall a. IORef a -> a -> IO ()
writeIORef (forall a. Vector a -> IORef Int
vectorCountRef Vector a
vector) (Int
count forall a. Num a => a -> a -> a
+ Int
1)
vectorDeleteAt :: Vector a -> Int -> IO ()
vectorDeleteAt :: forall a. Vector a -> Int -> IO ()
vectorDeleteAt Vector a
vector Int
index =
do Int
count <- forall a. IORef a -> IO a
readIORef (forall a. Vector a -> IORef Int
vectorCountRef Vector a
vector)
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Int
index forall a. Ord a => a -> a -> Bool
< Int
0) forall a b. (a -> b) -> a -> b
$
forall a. HasCallStack => [Char] -> a
error forall a b. (a -> b) -> a -> b
$
[Char]
"Index cannot be " forall a. [a] -> [a] -> [a]
++
[Char]
"negative: vectorDeleteAt."
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Int
index forall a. Ord a => a -> a -> Bool
>= Int
count) forall a b. (a -> b) -> a -> b
$
forall a. HasCallStack => [Char] -> a
error forall a b. (a -> b) -> a -> b
$
[Char]
"Index must be less " forall a. [a] -> [a] -> [a]
++
[Char]
"than the count: vectorDeleteAt."
IOArray Int a
array <- forall a. IORef a -> IO a
readIORef (forall a. Vector a -> IORef (IOArray Int a)
vectorArrayRef Vector a
vector)
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [Int
index, Int
index forall a. Num a => a -> a -> a
+ Int
1 .. Int
count forall a. Num a => a -> a -> a
- Int
2] forall a b. (a -> b) -> a -> b
$ \Int
i ->
do a
x <- forall (a :: * -> * -> *) e (m :: * -> *) i.
(MArray a e m, Ix i) =>
a i e -> i -> m e
readArray IOArray Int a
array (Int
i forall a. Num a => a -> a -> a
+ Int
1)
forall (a :: * -> * -> *) e (m :: * -> *) i.
(MArray a e m, Ix i) =>
a i e -> i -> e -> m ()
writeArray IOArray Int a
array Int
i a
x
forall (a :: * -> * -> *) e (m :: * -> *) i.
(MArray a e m, Ix i) =>
a i e -> i -> e -> m ()
writeArray IOArray Int a
array (Int
count forall a. Num a => a -> a -> a
- Int
1) forall a. HasCallStack => a
undefined
forall a. IORef a -> a -> IO ()
writeIORef (forall a. Vector a -> IORef Int
vectorCountRef Vector a
vector) (Int
count forall a. Num a => a -> a -> a
- Int
1)
vectorDeleteRange :: Vector a
-> Int
-> Int
-> IO ()
vectorDeleteRange :: forall a. Vector a -> Int -> Int -> IO ()
vectorDeleteRange Vector a
vector Int
index Int
len =
do Int
count <- forall a. IORef a -> IO a
readIORef (forall a. Vector a -> IORef Int
vectorCountRef Vector a
vector)
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Int
index forall a. Ord a => a -> a -> Bool
< Int
0) forall a b. (a -> b) -> a -> b
$
forall a. HasCallStack => [Char] -> a
error forall a b. (a -> b) -> a -> b
$
[Char]
"The first index cannot be " forall a. [a] -> [a] -> [a]
++
[Char]
"negative: vectorDeleteRange."
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Int
index forall a. Num a => a -> a -> a
+ Int
len forall a. Num a => a -> a -> a
- Int
1 forall a. Ord a => a -> a -> Bool
>= Int
count) forall a b. (a -> b) -> a -> b
$
forall a. HasCallStack => [Char] -> a
error forall a b. (a -> b) -> a -> b
$
[Char]
"The last index must be less " forall a. [a] -> [a] -> [a]
++
[Char]
"than the count: vectorDeleteRange."
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Int
len forall a. Ord a => a -> a -> Bool
< Int
0) forall a b. (a -> b) -> a -> b
$
forall a. HasCallStack => [Char] -> a
error [Char]
"Negative range length: vectorDeleteRange."
IOArray Int a
array <- forall a. IORef a -> IO a
readIORef (forall a. Vector a -> IORef (IOArray Int a)
vectorArrayRef Vector a
vector)
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [Int
index, Int
index forall a. Num a => a -> a -> a
+ Int
1 .. (Int
count forall a. Num a => a -> a -> a
- Int
len) forall a. Num a => a -> a -> a
- Int
1] forall a b. (a -> b) -> a -> b
$ \Int
i ->
do a
x <- forall (a :: * -> * -> *) e (m :: * -> *) i.
(MArray a e m, Ix i) =>
a i e -> i -> m e
readArray IOArray Int a
array (Int
i forall a. Num a => a -> a -> a
+ Int
len)
forall (a :: * -> * -> *) e (m :: * -> *) i.
(MArray a e m, Ix i) =>
a i e -> i -> e -> m ()
writeArray IOArray Int a
array Int
i a
x
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [(Int
count forall a. Num a => a -> a -> a
- Int
len) .. Int
count forall a. Num a => a -> a -> a
- Int
1] forall a b. (a -> b) -> a -> b
$ \Int
i ->
forall (a :: * -> * -> *) e (m :: * -> *) i.
(MArray a e m, Ix i) =>
a i e -> i -> e -> m ()
writeArray IOArray Int a
array Int
i forall a. HasCallStack => a
undefined
forall a. IORef a -> a -> IO ()
writeIORef (forall a. Vector a -> IORef Int
vectorCountRef Vector a
vector) (Int
count forall a. Num a => a -> a -> a
- Int
len)
vectorIndex :: Eq a => Vector a -> a -> IO Int
vectorIndex :: forall a. Eq a => Vector a -> a -> IO Int
vectorIndex Vector a
vector a
item =
do Int
count <- forall a. IORef a -> IO a
readIORef (forall a. Vector a -> IORef Int
vectorCountRef Vector a
vector)
IOArray Int a
array <- forall a. IORef a -> IO a
readIORef (forall a. Vector a -> IORef (IOArray Int a)
vectorArrayRef Vector a
vector)
let loop :: Int -> m Int
loop Int
index =
if Int
index forall a. Ord a => a -> a -> Bool
>= Int
count
then forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ -Int
1
else do a
x <- forall (a :: * -> * -> *) e (m :: * -> *) i.
(MArray a e m, Ix i) =>
a i e -> i -> m e
readArray IOArray Int a
array Int
index
if a
item forall a. Eq a => a -> a -> Bool
== a
x
then forall (m :: * -> *) a. Monad m => a -> m a
return Int
index
else Int -> m Int
loop forall a b. (a -> b) -> a -> b
$ Int
index forall a. Num a => a -> a -> a
+ Int
1
forall {m :: * -> *}. MArray IOArray a m => Int -> m Int
loop Int
0
vectorIndexBy :: Vector a -> (a -> Bool) -> IO Int
vectorIndexBy :: forall a. Vector a -> (a -> Bool) -> IO Int
vectorIndexBy Vector a
vector a -> Bool
pred =
do Int
count <- forall a. IORef a -> IO a
readIORef (forall a. Vector a -> IORef Int
vectorCountRef Vector a
vector)
IOArray Int a
array <- forall a. IORef a -> IO a
readIORef (forall a. Vector a -> IORef (IOArray Int a)
vectorArrayRef Vector a
vector)
let loop :: Int -> m Int
loop Int
index =
if Int
index forall a. Ord a => a -> a -> Bool
>= Int
count
then forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ -Int
1
else do a
x <- forall (a :: * -> * -> *) e (m :: * -> *) i.
(MArray a e m, Ix i) =>
a i e -> i -> m e
readArray IOArray Int a
array Int
index
if a -> Bool
pred a
x
then forall (m :: * -> *) a. Monad m => a -> m a
return Int
index
else Int -> m Int
loop forall a b. (a -> b) -> a -> b
$ Int
index forall a. Num a => a -> a -> a
+ Int
1
forall {m :: * -> *}. MArray IOArray a m => Int -> m Int
loop Int
0
vectorDelete :: Eq a => Vector a -> a -> IO Bool
vectorDelete :: forall a. Eq a => Vector a -> a -> IO Bool
vectorDelete Vector a
vector a
item =
do Int
index <- forall a. Eq a => Vector a -> a -> IO Int
vectorIndex Vector a
vector a
item
if Int
index forall a. Ord a => a -> a -> Bool
>= Int
0
then do forall a. Vector a -> Int -> IO ()
vectorDeleteAt Vector a
vector Int
index
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
True
else forall (m :: * -> *) a. Monad m => a -> m a
return Bool
False
vectorDeleteBy :: Vector a -> (a -> Bool) -> IO (Maybe a)
vectorDeleteBy :: forall a. Vector a -> (a -> Bool) -> IO (Maybe a)
vectorDeleteBy Vector a
vector a -> Bool
pred =
do Int
index <- forall a. Vector a -> (a -> Bool) -> IO Int
vectorIndexBy Vector a
vector a -> Bool
pred
if Int
index forall a. Ord a => a -> a -> Bool
>= Int
0
then do a
a <- forall a. Vector a -> Int -> IO a
readVector Vector a
vector Int
index
forall a. Vector a -> Int -> IO ()
vectorDeleteAt Vector a
vector Int
index
forall (m :: * -> *) a. Monad m => a -> m a
return (forall a. a -> Maybe a
Just a
a)
else forall (m :: * -> *) a. Monad m => a -> m a
return forall a. Maybe a
Nothing
vectorContains :: Eq a => Vector a -> a -> IO Bool
vectorContains :: forall a. Eq a => Vector a -> a -> IO Bool
vectorContains Vector a
vector a
item =
do Int
index <- forall a. Eq a => Vector a -> a -> IO Int
vectorIndex Vector a
vector a
item
forall (m :: * -> *) a. Monad m => a -> m a
return (Int
index forall a. Ord a => a -> a -> Bool
>= Int
0)
vectorContainsBy :: Vector a -> (a -> Bool) -> IO (Maybe a)
vectorContainsBy :: forall a. Vector a -> (a -> Bool) -> IO (Maybe a)
vectorContainsBy Vector a
vector a -> Bool
pred =
do Int
index <- forall a. Vector a -> (a -> Bool) -> IO Int
vectorIndexBy Vector a
vector a -> Bool
pred
if Int
index forall a. Ord a => a -> a -> Bool
>= Int
0
then do a
a <- forall a. Vector a -> Int -> IO a
readVector Vector a
vector Int
index
forall (m :: * -> *) a. Monad m => a -> m a
return (forall a. a -> Maybe a
Just a
a)
else forall (m :: * -> *) a. Monad m => a -> m a
return forall a. Maybe a
Nothing