{-# LANGUAGE FlexibleContexts #-}
module Simulation.Aivika.Vector.Unboxed
(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
import Simulation.Aivika.Unboxed
data Vector a = Vector { Vector a -> IORef (IOUArray Int a)
vectorArrayRef :: IORef (IOUArray Int a),
Vector a -> IORef Int
vectorCountRef :: IORef Int,
Vector a -> IORef Int
vectorCapacityRef :: IORef Int }
newVector :: Unboxed a => IO (Vector a)
newVector :: IO (Vector a)
newVector =
do IOUArray Int a
array <- (Int, Int) -> IO (IOUArray Int a)
forall e i. (Unboxed e, Ix i) => (i, i) -> IO (IOUArray i e)
newUnboxedArray_ (Int
0, Int
4 Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1)
IORef (IOUArray Int a)
arrayRef <- IOUArray Int a -> IO (IORef (IOUArray Int a))
forall a. a -> IO (IORef a)
newIORef IOUArray Int a
array
IORef Int
countRef <- Int -> IO (IORef Int)
forall a. a -> IO (IORef a)
newIORef Int
0
IORef Int
capacityRef <- Int -> IO (IORef Int)
forall a. a -> IO (IORef a)
newIORef Int
4
Vector a -> IO (Vector a)
forall (m :: * -> *) a. Monad m => a -> m a
return Vector :: forall a.
IORef (IOUArray Int a) -> IORef Int -> IORef Int -> Vector a
Vector { vectorArrayRef :: IORef (IOUArray Int a)
vectorArrayRef = IORef (IOUArray Int a)
arrayRef,
vectorCountRef :: IORef Int
vectorCountRef = IORef Int
countRef,
vectorCapacityRef :: IORef Int
vectorCapacityRef = IORef Int
capacityRef }
copyVector :: Unboxed a => Vector a -> IO (Vector a)
copyVector :: Vector a -> IO (Vector a)
copyVector Vector a
vector =
do IOUArray Int a
array <- IORef (IOUArray Int a) -> IO (IOUArray Int a)
forall a. IORef a -> IO a
readIORef (Vector a -> IORef (IOUArray Int a)
forall a. Vector a -> IORef (IOUArray Int a)
vectorArrayRef Vector a
vector)
Int
count <- IORef Int -> IO Int
forall a. IORef a -> IO a
readIORef (Vector a -> IORef Int
forall a. Vector a -> IORef Int
vectorCountRef Vector a
vector)
IOUArray Int a
array' <- (Int, Int) -> IO (IOUArray Int a)
forall e i. (Unboxed e, Ix i) => (i, i) -> IO (IOUArray i e)
newUnboxedArray_ (Int
0, Int
count Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1)
IORef (IOUArray Int a)
arrayRef' <- IOUArray Int a -> IO (IORef (IOUArray Int a))
forall a. a -> IO (IORef a)
newIORef IOUArray Int a
array'
IORef Int
countRef' <- Int -> IO (IORef Int)
forall a. a -> IO (IORef a)
newIORef Int
count
IORef Int
capacityRef' <- Int -> IO (IORef Int)
forall a. a -> IO (IORef a)
newIORef Int
count
[Int] -> (Int -> IO ()) -> IO ()
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 -> IO ()) -> IO ()) -> (Int -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \Int
i ->
do a
x <- IOUArray Int a -> Int -> IO a
forall (a :: * -> * -> *) e (m :: * -> *) i.
(MArray a e m, Ix i) =>
a i e -> i -> m e
readArray IOUArray Int a
array Int
i
IOUArray Int a -> Int -> a -> IO ()
forall (a :: * -> * -> *) e (m :: * -> *) i.
(MArray a e m, Ix i) =>
a i e -> i -> e -> m ()
writeArray IOUArray Int a
array' Int
i a
x
Vector a -> IO (Vector a)
forall (m :: * -> *) a. Monad m => a -> m a
return Vector :: forall a.
IORef (IOUArray Int a) -> IORef Int -> IORef Int -> Vector a
Vector { vectorArrayRef :: IORef (IOUArray Int a)
vectorArrayRef = IORef (IOUArray Int a)
arrayRef',
vectorCountRef :: IORef Int
vectorCountRef = IORef Int
countRef',
vectorCapacityRef :: IORef Int
vectorCapacityRef = IORef Int
capacityRef' }
vectorEnsureCapacity :: Unboxed a => Vector a -> Int -> IO ()
vectorEnsureCapacity :: Vector a -> Int -> IO ()
vectorEnsureCapacity Vector a
vector Int
capacity =
do Int
capacity' <- IORef Int -> IO Int
forall a. IORef a -> IO a
readIORef (Vector a -> IORef Int
forall a. Vector a -> IORef Int
vectorCapacityRef Vector a
vector)
Bool -> IO () -> IO ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Int
capacity' Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
capacity) (IO () -> IO ()) -> IO () -> IO ()
forall a b. (a -> b) -> a -> b
$
do IOUArray Int a
array' <- IORef (IOUArray Int a) -> IO (IOUArray Int a)
forall a. IORef a -> IO a
readIORef (Vector a -> IORef (IOUArray Int a)
forall a. Vector a -> IORef (IOUArray Int a)
vectorArrayRef Vector a
vector)
Int
count' <- IORef Int -> IO Int
forall a. IORef a -> IO a
readIORef (Vector a -> IORef Int
forall a. Vector a -> IORef Int
vectorCountRef Vector 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
IOUArray Int a
array'' <- (Int, Int) -> IO (IOUArray Int a)
forall e i. (Unboxed e, Ix i) => (i, i) -> IO (IOUArray i e)
newUnboxedArray_ (Int
0, Int
capacity'' Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1)
[Int] -> (Int -> IO ()) -> IO ()
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 -> IO ()) -> IO ()) -> (Int -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \Int
i ->
do a
x <- IOUArray Int a -> Int -> IO a
forall (a :: * -> * -> *) e (m :: * -> *) i.
(MArray a e m, Ix i) =>
a i e -> i -> m e
readArray IOUArray Int a
array' Int
i
IOUArray Int a -> Int -> a -> IO ()
forall (a :: * -> * -> *) e (m :: * -> *) i.
(MArray a e m, Ix i) =>
a i e -> i -> e -> m ()
writeArray IOUArray Int a
array'' Int
i a
x
IORef (IOUArray Int a) -> IOUArray Int a -> IO ()
forall a. IORef a -> a -> IO ()
writeIORef (Vector a -> IORef (IOUArray Int a)
forall a. Vector a -> IORef (IOUArray Int a)
vectorArrayRef Vector a
vector) IOUArray Int a
array''
IORef Int -> Int -> IO ()
forall a. IORef a -> a -> IO ()
writeIORef (Vector a -> IORef Int
forall a. Vector a -> IORef Int
vectorCapacityRef Vector a
vector) Int
capacity''
vectorCount :: Unboxed a => Vector a -> IO Int
vectorCount :: Vector a -> IO Int
vectorCount Vector a
vector = IORef Int -> IO Int
forall a. IORef a -> IO a
readIORef (Vector a -> IORef Int
forall a. Vector a -> IORef Int
vectorCountRef Vector a
vector)
appendVector :: Unboxed a => Vector a -> a -> IO ()
appendVector :: Vector a -> a -> IO ()
appendVector Vector a
vector a
item =
do Int
count <- IORef Int -> IO Int
forall a. IORef a -> IO a
readIORef (Vector a -> IORef Int
forall a. Vector a -> IORef Int
vectorCountRef Vector a
vector)
Vector a -> Int -> IO ()
forall a. Unboxed a => Vector a -> Int -> IO ()
vectorEnsureCapacity Vector a
vector (Int
count Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)
IOUArray Int a
array <- IORef (IOUArray Int a) -> IO (IOUArray Int a)
forall a. IORef a -> IO a
readIORef (Vector a -> IORef (IOUArray Int a)
forall a. Vector a -> IORef (IOUArray Int a)
vectorArrayRef Vector a
vector)
IOUArray Int a -> Int -> a -> IO ()
forall (a :: * -> * -> *) e (m :: * -> *) i.
(MArray a e m, Ix i) =>
a i e -> i -> e -> m ()
writeArray IOUArray Int a
array Int
count a
item
IORef Int -> Int -> IO ()
forall a. IORef a -> a -> IO ()
writeIORef (Vector a -> IORef Int
forall a. Vector a -> IORef Int
vectorCountRef Vector a
vector) (Int
count Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)
readVector :: Unboxed a => Vector a -> Int -> IO a
readVector :: Vector a -> Int -> IO a
readVector Vector a
vector Int
index =
do IOUArray Int a
array <- IORef (IOUArray Int a) -> IO (IOUArray Int a)
forall a. IORef a -> IO a
readIORef (Vector a -> IORef (IOUArray Int a)
forall a. Vector a -> IORef (IOUArray Int a)
vectorArrayRef Vector a
vector)
IOUArray Int a -> Int -> IO a
forall (a :: * -> * -> *) e (m :: * -> *) i.
(MArray a e m, Ix i) =>
a i e -> i -> m e
readArray IOUArray Int a
array Int
index
writeVector :: Unboxed a => Vector a -> Int -> a -> IO ()
writeVector :: Vector a -> Int -> a -> IO ()
writeVector Vector a
vector Int
index a
item =
do IOUArray Int a
array <- IORef (IOUArray Int a) -> IO (IOUArray Int a)
forall a. IORef a -> IO a
readIORef (Vector a -> IORef (IOUArray Int a)
forall a. Vector a -> IORef (IOUArray Int a)
vectorArrayRef Vector a
vector)
IOUArray Int a -> Int -> a -> IO ()
forall (a :: * -> * -> *) e (m :: * -> *) i.
(MArray a e m, Ix i) =>
a i e -> i -> e -> m ()
writeArray IOUArray Int a
array Int
index a
item
vectorBinarySearch' :: (Unboxed a, Ord a) => IOUArray Int a -> a -> Int -> Int -> IO Int
vectorBinarySearch' :: IOUArray Int a -> a -> Int -> Int -> IO Int
vectorBinarySearch' IOUArray Int a
array a
item Int
left Int
right =
if Int
left Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
right
then Int -> IO Int
forall (m :: * -> *) a. Monad m => a -> m a
return (Int -> IO Int) -> Int -> IO 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 <- IOUArray Int a -> Int -> IO a
forall (a :: * -> * -> *) e (m :: * -> *) i.
(MArray a e m, Ix i) =>
a i e -> i -> m e
readArray IOUArray Int a
array Int
index
if a
item a -> a -> Bool
forall a. Ord a => a -> a -> Bool
< a
curr
then IOUArray Int a -> a -> Int -> Int -> IO Int
forall a.
(Unboxed a, Ord a) =>
IOUArray Int a -> a -> Int -> Int -> IO Int
vectorBinarySearch' IOUArray Int a
array 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 -> IO Int
forall (m :: * -> *) a. Monad m => a -> m a
return Int
index
else IOUArray Int a -> a -> Int -> Int -> IO Int
forall a.
(Unboxed a, Ord a) =>
IOUArray Int a -> a -> Int -> Int -> IO Int
vectorBinarySearch' IOUArray Int a
array a
item (Int
index Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) Int
right
vectorBinarySearch :: (Unboxed a, Ord a) => Vector a -> a -> IO Int
vectorBinarySearch :: Vector a -> a -> IO Int
vectorBinarySearch Vector a
vector a
item =
do IOUArray Int a
array <- IORef (IOUArray Int a) -> IO (IOUArray Int a)
forall a. IORef a -> IO a
readIORef (Vector a -> IORef (IOUArray Int a)
forall a. Vector a -> IORef (IOUArray Int a)
vectorArrayRef Vector a
vector)
Int
count <- IORef Int -> IO Int
forall a. IORef a -> IO a
readIORef (Vector a -> IORef Int
forall a. Vector a -> IORef Int
vectorCountRef Vector a
vector)
IOUArray Int a -> a -> Int -> Int -> IO Int
forall a.
(Unboxed a, Ord a) =>
IOUArray Int a -> a -> Int -> Int -> IO Int
vectorBinarySearch' IOUArray Int a
array a
item Int
0 (Int
count Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1)
freezeVector :: Unboxed a => Vector a -> IO (Array Int a)
freezeVector :: Vector a -> IO (Array Int a)
freezeVector Vector a
vector =
do Vector a
vector' <- Vector a -> IO (Vector a)
forall a. Unboxed a => Vector a -> IO (Vector a)
copyVector Vector a
vector
IOUArray Int a
array <- IORef (IOUArray Int a) -> IO (IOUArray Int a)
forall a. IORef a -> IO a
readIORef (Vector a -> IORef (IOUArray Int a)
forall a. Vector a -> IORef (IOUArray Int a)
vectorArrayRef Vector a
vector')
IOUArray Int a -> IO (Array Int a)
forall i (a :: * -> * -> *) e (m :: * -> *) (b :: * -> * -> *).
(Ix i, MArray a e m, IArray b e) =>
a i e -> m (b i e)
freeze IOUArray Int a
array
vectorInsert :: Unboxed a => Vector a -> Int -> a -> IO ()
vectorInsert :: Vector a -> Int -> a -> IO ()
vectorInsert Vector a
vector Int
index a
item =
do Int
count <- IORef Int -> IO Int
forall a. IORef a -> IO a
readIORef (Vector a -> IORef Int
forall a. Vector a -> IORef Int
vectorCountRef Vector a
vector)
Bool -> IO () -> IO ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Int
index Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
0) (IO () -> IO ()) -> IO () -> IO ()
forall a b. (a -> b) -> a -> b
$
[Char] -> IO ()
forall a. HasCallStack => [Char] -> a
error ([Char] -> IO ()) -> [Char] -> IO ()
forall a b. (a -> b) -> a -> b
$
[Char]
"Index cannot be " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++
[Char]
"negative: vectorInsert."
Bool -> IO () -> IO ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Int
index Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
count) (IO () -> IO ()) -> IO () -> IO ()
forall a b. (a -> b) -> a -> b
$
[Char] -> IO ()
forall a. HasCallStack => [Char] -> a
error ([Char] -> IO ()) -> [Char] -> IO ()
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 a -> Int -> IO ()
forall a. Unboxed a => Vector a -> Int -> IO ()
vectorEnsureCapacity Vector a
vector (Int
count Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)
IOUArray Int a
array <- IORef (IOUArray Int a) -> IO (IOUArray Int a)
forall a. IORef a -> IO a
readIORef (Vector a -> IORef (IOUArray Int a)
forall a. Vector a -> IORef (IOUArray Int a)
vectorArrayRef Vector a
vector)
[Int] -> (Int -> IO ()) -> IO ()
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 -> IO ()) -> IO ()) -> (Int -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \Int
i ->
do a
x <- IOUArray Int a -> Int -> IO a
forall (a :: * -> * -> *) e (m :: * -> *) i.
(MArray a e m, Ix i) =>
a i e -> i -> m e
readArray IOUArray Int a
array (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1)
IOUArray Int a -> Int -> a -> IO ()
forall (a :: * -> * -> *) e (m :: * -> *) i.
(MArray a e m, Ix i) =>
a i e -> i -> e -> m ()
writeArray IOUArray Int a
array Int
i a
x
IOUArray Int a -> Int -> a -> IO ()
forall (a :: * -> * -> *) e (m :: * -> *) i.
(MArray a e m, Ix i) =>
a i e -> i -> e -> m ()
writeArray IOUArray Int a
array Int
index a
item
IORef Int -> Int -> IO ()
forall a. IORef a -> a -> IO ()
writeIORef (Vector a -> IORef Int
forall a. Vector a -> IORef Int
vectorCountRef Vector a
vector) (Int
count Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)
vectorDeleteAt :: Unboxed a => Vector a -> Int -> IO ()
vectorDeleteAt :: Vector a -> Int -> IO ()
vectorDeleteAt Vector a
vector Int
index =
do Int
count <- IORef Int -> IO Int
forall a. IORef a -> IO a
readIORef (Vector a -> IORef Int
forall a. Vector a -> IORef Int
vectorCountRef Vector a
vector)
Bool -> IO () -> IO ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Int
index Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
0) (IO () -> IO ()) -> IO () -> IO ()
forall a b. (a -> b) -> a -> b
$
[Char] -> IO ()
forall a. HasCallStack => [Char] -> a
error ([Char] -> IO ()) -> [Char] -> IO ()
forall a b. (a -> b) -> a -> b
$
[Char]
"Index cannot be " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++
[Char]
"negative: vectorDeleteAt."
Bool -> IO () -> IO ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Int
index Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
count) (IO () -> IO ()) -> IO () -> IO ()
forall a b. (a -> b) -> a -> b
$
[Char] -> IO ()
forall a. HasCallStack => [Char] -> a
error ([Char] -> IO ()) -> [Char] -> IO ()
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."
IOUArray Int a
array <- IORef (IOUArray Int a) -> IO (IOUArray Int a)
forall a. IORef a -> IO a
readIORef (Vector a -> IORef (IOUArray Int a)
forall a. Vector a -> IORef (IOUArray Int a)
vectorArrayRef Vector a
vector)
[Int] -> (Int -> IO ()) -> IO ()
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 -> IO ()) -> IO ()) -> (Int -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \Int
i ->
do a
x <- IOUArray Int a -> Int -> IO a
forall (a :: * -> * -> *) e (m :: * -> *) i.
(MArray a e m, Ix i) =>
a i e -> i -> m e
readArray IOUArray Int a
array (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)
IOUArray Int a -> Int -> a -> IO ()
forall (a :: * -> * -> *) e (m :: * -> *) i.
(MArray a e m, Ix i) =>
a i e -> i -> e -> m ()
writeArray IOUArray Int a
array Int
i a
x
IORef Int -> Int -> IO ()
forall a. IORef a -> a -> IO ()
writeIORef (Vector a -> IORef Int
forall a. Vector a -> IORef Int
vectorCountRef Vector a
vector) (Int
count Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1)
vectorDeleteRange :: Unboxed a
=> Vector a
-> Int
-> Int
-> IO ()
vectorDeleteRange :: Vector a -> Int -> Int -> IO ()
vectorDeleteRange Vector a
vector Int
index Int
len =
do Int
count <- IORef Int -> IO Int
forall a. IORef a -> IO a
readIORef (Vector a -> IORef Int
forall a. Vector a -> IORef Int
vectorCountRef Vector a
vector)
Bool -> IO () -> IO ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Int
index Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
0) (IO () -> IO ()) -> IO () -> IO ()
forall a b. (a -> b) -> a -> b
$
[Char] -> IO ()
forall a. HasCallStack => [Char] -> a
error ([Char] -> IO ()) -> [Char] -> IO ()
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 -> IO () -> IO ()
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) (IO () -> IO ()) -> IO () -> IO ()
forall a b. (a -> b) -> a -> b
$
[Char] -> IO ()
forall a. HasCallStack => [Char] -> a
error ([Char] -> IO ()) -> [Char] -> IO ()
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 -> IO () -> IO ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Int
len Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
0) (IO () -> IO ()) -> IO () -> IO ()
forall a b. (a -> b) -> a -> b
$
[Char] -> IO ()
forall a. HasCallStack => [Char] -> a
error [Char]
"Negative range length: vectorDeleteRange."
IOUArray Int a
array <- IORef (IOUArray Int a) -> IO (IOUArray Int a)
forall a. IORef a -> IO a
readIORef (Vector a -> IORef (IOUArray Int a)
forall a. Vector a -> IORef (IOUArray Int a)
vectorArrayRef Vector a
vector)
[Int] -> (Int -> IO ()) -> IO ()
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 -> IO ()) -> IO ()) -> (Int -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \Int
i ->
do a
x <- IOUArray Int a -> Int -> IO a
forall (a :: * -> * -> *) e (m :: * -> *) i.
(MArray a e m, Ix i) =>
a i e -> i -> m e
readArray IOUArray Int a
array (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
len)
IOUArray Int a -> Int -> a -> IO ()
forall (a :: * -> * -> *) e (m :: * -> *) i.
(MArray a e m, Ix i) =>
a i e -> i -> e -> m ()
writeArray IOUArray Int a
array Int
i a
x
IORef Int -> Int -> IO ()
forall a. IORef a -> a -> IO ()
writeIORef (Vector a -> IORef Int
forall a. Vector a -> IORef Int
vectorCountRef Vector a
vector) (Int
count Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
len)
vectorIndex :: (Unboxed a, Eq a) => Vector a -> a -> IO Int
vectorIndex :: Vector a -> a -> IO Int
vectorIndex Vector a
vector a
item =
do Int
count <- IORef Int -> IO Int
forall a. IORef a -> IO a
readIORef (Vector a -> IORef Int
forall a. Vector a -> IORef Int
vectorCountRef Vector a
vector)
IOUArray Int a
array <- IORef (IOUArray Int a) -> IO (IOUArray Int a)
forall a. IORef a -> IO a
readIORef (Vector a -> IORef (IOUArray Int a)
forall a. Vector a -> IORef (IOUArray Int a)
vectorArrayRef Vector a
vector)
let loop :: Int -> m Int
loop Int
index =
if Int
index Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
count
then Int -> m Int
forall (m :: * -> *) a. Monad m => a -> m a
return (Int -> m Int) -> Int -> m Int
forall a b. (a -> b) -> a -> b
$ -Int
1
else do a
x <- IOUArray Int a -> Int -> m a
forall (a :: * -> * -> *) e (m :: * -> *) i.
(MArray a e m, Ix i) =>
a i e -> i -> m e
readArray IOUArray Int a
array Int
index
if a
item a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
x
then Int -> m Int
forall (m :: * -> *) a. Monad m => a -> m a
return Int
index
else Int -> m Int
loop (Int -> m Int) -> Int -> m Int
forall a b. (a -> b) -> a -> b
$ Int
index Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1
Int -> IO Int
forall (m :: * -> *). MArray IOUArray a m => Int -> m Int
loop Int
0
vectorIndexBy :: Unboxed a => Vector a -> (a -> Bool) -> IO Int
vectorIndexBy :: Vector a -> (a -> Bool) -> IO Int
vectorIndexBy Vector a
vector a -> Bool
pred =
do Int
count <- IORef Int -> IO Int
forall a. IORef a -> IO a
readIORef (Vector a -> IORef Int
forall a. Vector a -> IORef Int
vectorCountRef Vector a
vector)
IOUArray Int a
array <- IORef (IOUArray Int a) -> IO (IOUArray Int a)
forall a. IORef a -> IO a
readIORef (Vector a -> IORef (IOUArray Int a)
forall a. Vector a -> IORef (IOUArray Int a)
vectorArrayRef Vector a
vector)
let loop :: Int -> m Int
loop Int
index =
if Int
index Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
count
then Int -> m Int
forall (m :: * -> *) a. Monad m => a -> m a
return (Int -> m Int) -> Int -> m Int
forall a b. (a -> b) -> a -> b
$ -Int
1
else do a
x <- IOUArray Int a -> Int -> m a
forall (a :: * -> * -> *) e (m :: * -> *) i.
(MArray a e m, Ix i) =>
a i e -> i -> m e
readArray IOUArray Int a
array Int
index
if a -> Bool
pred a
x
then Int -> m Int
forall (m :: * -> *) a. Monad m => a -> m a
return Int
index
else Int -> m Int
loop (Int -> m Int) -> Int -> m Int
forall a b. (a -> b) -> a -> b
$ Int
index Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1
Int -> IO Int
forall (m :: * -> *). MArray IOUArray a m => Int -> m Int
loop Int
0
vectorDelete :: (Unboxed a, Eq a) => Vector a -> a -> IO Bool
vectorDelete :: Vector a -> a -> IO Bool
vectorDelete Vector a
vector a
item =
do Int
index <- Vector a -> a -> IO Int
forall a. (Unboxed a, Eq a) => Vector a -> a -> IO Int
vectorIndex Vector a
vector a
item
if Int
index Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
0
then do Vector a -> Int -> IO ()
forall a. Unboxed a => Vector a -> Int -> IO ()
vectorDeleteAt Vector a
vector Int
index
Bool -> IO Bool
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
True
else Bool -> IO Bool
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
False
vectorDeleteBy :: Unboxed a => Vector a -> (a -> Bool) -> IO Bool
vectorDeleteBy :: Vector a -> (a -> Bool) -> IO Bool
vectorDeleteBy Vector a
vector a -> Bool
pred =
do Int
index <- Vector a -> (a -> Bool) -> IO Int
forall a. Unboxed a => Vector a -> (a -> Bool) -> IO Int
vectorIndexBy Vector a
vector a -> Bool
pred
if Int
index Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
0
then do Vector a -> Int -> IO ()
forall a. Unboxed a => Vector a -> Int -> IO ()
vectorDeleteAt Vector a
vector Int
index
Bool -> IO Bool
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
True
else Bool -> IO Bool
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
False
vectorContains :: (Unboxed a, Eq a) => Vector a -> a -> IO Bool
vectorContains :: Vector a -> a -> IO Bool
vectorContains Vector a
vector a
item =
do Int
index <- Vector a -> a -> IO Int
forall a. (Unboxed a, Eq a) => Vector a -> a -> IO Int
vectorIndex Vector a
vector a
item
Bool -> IO Bool
forall (m :: * -> *) a. Monad m => a -> m a
return (Int
index Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
0)
vectorContainsBy :: Unboxed a => Vector a -> (a -> Bool) -> IO (Maybe a)
vectorContainsBy :: Vector a -> (a -> Bool) -> IO (Maybe a)
vectorContainsBy Vector a
vector a -> Bool
pred =
do Int
index <- Vector a -> (a -> Bool) -> IO Int
forall a. Unboxed a => Vector a -> (a -> Bool) -> IO Int
vectorIndexBy Vector 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 a -> Int -> IO a
forall a. Unboxed a => Vector a -> Int -> IO a
readVector Vector a
vector Int
index
Maybe a -> IO (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 -> IO (Maybe a)
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe a
forall a. Maybe a
Nothing