{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE LinearTypes #-}
{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE MagicHash #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE StrictData #-}
{-# LANGUAGE UnboxedTuples #-}
{-# OPTIONS_GHC -Wno-unbanged-strict-patterns #-}
module Data.Vector.Mutable.Linear
(
Vector,
empty,
constant,
fromList,
set,
unsafeSet,
modify,
modify_,
push,
pop,
filter,
mapMaybe,
slice,
shrinkToFit,
get,
unsafeGet,
size,
capacity,
toList,
freeze,
read,
unsafeRead,
write,
unsafeWrite
)
where
import GHC.Stack
import Prelude.Linear hiding (read, filter, mapMaybe)
import Data.Array.Mutable.Linear (Array)
import qualified Prelude
import Data.Monoid.Linear
import qualified Data.Array.Mutable.Linear as Array
import qualified Data.Functor.Linear as Data
import qualified Unsafe.Linear as Unsafe
import qualified Data.Vector as Vector
constGrowthFactor :: Int
constGrowthFactor :: Int
constGrowthFactor = Int
2
data Vector a where
Vec ::
Int ->
Array a %1->
Vector a
fromArray :: HasCallStack => Array a %1-> Vector a
fromArray :: forall a. HasCallStack => Array a %1 -> Vector a
fromArray Array a
arr =
Array a %1 -> (Ur Int, Array a)
forall a. Array a %1 -> (Ur Int, Array a)
Array.size Array a
arr
(Ur Int, Array a)
%1 -> ((Ur Int, Array a) %1 -> Vector a) %1 -> Vector a
forall a b. a %1 -> (a %1 -> b) %1 -> b
& \(Ur Int
size', Array a
arr') -> Int -> Array a %1 -> Vector a
forall a. Int -> Array a -> Vector a
Vec Int
size' Array a
arr'
empty :: (Vector a %1-> Ur b) %1-> Ur b
empty :: forall a b. (Vector a %1 -> Ur b) %1 -> Ur b
empty Vector a %1 -> Ur b
f = [a] -> (Array a %1 -> Ur b) %1 -> Ur b
forall a b. HasCallStack => [a] -> (Array a %1 -> Ur b) %1 -> Ur b
Array.fromList [] (Vector a %1 -> Ur b
f (Vector a %1 -> Ur b)
%1 -> (Array a %1 -> Vector a) %1 -> Array a %1 -> Ur b
forall b c a. (b %1 -> c) %1 -> (a %1 -> b) %1 -> a %1 -> c
. Array a %1 -> Vector a
forall a. HasCallStack => Array a %1 -> Vector a
fromArray)
constant :: HasCallStack =>
Int -> a -> (Vector a %1-> Ur b) %1-> Ur b
constant :: forall a b.
HasCallStack =>
Int -> a -> (Vector a %1 -> Ur b) %1 -> Ur b
constant Int
size' a
x Vector a %1 -> Ur b
f
| Int
size' Int %1 -> Int %1 -> Bool
forall a. Ord a => a %1 -> a %1 -> Bool
< Int
0 =
([Char] -> x %1 -> x
forall a. HasCallStack => [Char] -> a
error ([Char]
"Trying to construct a vector of size " [Char] %1 -> [Char] %1 -> [Char]
forall a. [a] %1 -> [a] %1 -> [a]
++ Int -> [Char]
forall a. Show a => a -> [Char]
show Int
size') :: x %1-> x)
(Vector a %1 -> Ur b
f Vector a
forall a. HasCallStack => a
undefined)
| Bool
otherwise = Int -> a -> (Array a %1 -> Ur b) %1 -> Ur b
forall a b.
HasCallStack =>
Int -> a -> (Array a %1 -> Ur b) %1 -> Ur b
Array.alloc Int
size' a
x (Vector a %1 -> Ur b
f (Vector a %1 -> Ur b)
%1 -> (Array a %1 -> Vector a) %1 -> Array a %1 -> Ur b
forall b c a. (b %1 -> c) %1 -> (a %1 -> b) %1 -> a %1 -> c
. Array a %1 -> Vector a
forall a. HasCallStack => Array a %1 -> Vector a
fromArray)
fromList :: HasCallStack => [a] -> (Vector a %1-> Ur b) %1-> Ur b
fromList :: forall a b. HasCallStack => [a] -> (Vector a %1 -> Ur b) %1 -> Ur b
fromList [a]
xs Vector a %1 -> Ur b
f = [a] -> (Array a %1 -> Ur b) %1 -> Ur b
forall a b. HasCallStack => [a] -> (Array a %1 -> Ur b) %1 -> Ur b
Array.fromList [a]
xs (Vector a %1 -> Ur b
f (Vector a %1 -> Ur b)
%1 -> (Array a %1 -> Vector a) %1 -> Array a %1 -> Ur b
forall b c a. (b %1 -> c) %1 -> (a %1 -> b) %1 -> a %1 -> c
. Array a %1 -> Vector a
forall a. HasCallStack => Array a %1 -> Vector a
fromArray)
size :: Vector a %1-> (Ur Int, Vector a)
size :: forall a. Vector a %1 -> (Ur Int, Vector a)
size (Vec Int
size' Array a
arr) = (Int -> Ur Int
forall a. a -> Ur a
Ur Int
size', Int -> Array a %1 -> Vector a
forall a. Int -> Array a -> Vector a
Vec Int
size' Array a
arr)
capacity :: Vector a %1-> (Ur Int, Vector a)
capacity :: forall a. Vector a %1 -> (Ur Int, Vector a)
capacity (Vec Int
s Array a
arr) =
Array a %1 -> (Ur Int, Array a)
forall a. Array a %1 -> (Ur Int, Array a)
Array.size Array a
arr (Ur Int, Array a)
%1 -> ((Ur Int, Array a) %1 -> (Ur Int, Vector a))
%1 -> (Ur Int, Vector a)
forall a b. a %1 -> (a %1 -> b) %1 -> b
& \(Ur Int
cap, Array a
arr') -> (Ur Int
cap, Int -> Array a %1 -> Vector a
forall a. Int -> Array a -> Vector a
Vec Int
s Array a
arr')
push :: a -> Vector a %1-> Vector a
push :: forall a. a -> Vector a %1 -> Vector a
push a
x Vector a
vec =
Int -> Vector a %1 -> Vector a
forall a. HasCallStack => Int -> Vector a %1 -> Vector a
growToFit Int
1 Vector a
vec Vector a %1 -> (Vector a %1 -> Vector a) %1 -> Vector a
forall a b. a %1 -> (a %1 -> b) %1 -> b
& \(Vec Int
s Array a
arr) ->
Int -> a -> Vector a %1 -> Vector a
forall a. HasCallStack => Int -> a -> Vector a %1 -> Vector a
unsafeSet Int
s a
x (Int -> Array a %1 -> Vector a
forall a. Int -> Array a -> Vector a
Vec (Int
s Int %1 -> Int %1 -> Int
forall a. Additive a => a %1 -> a %1 -> a
+ Int
1) Array a
arr)
pop :: Vector a %1-> (Ur (Maybe a), Vector a)
pop :: forall a. Vector a %1 -> (Ur (Maybe a), Vector a)
pop Vector a
vec =
Vector a %1 -> (Ur Int, Vector a)
forall a. Vector a %1 -> (Ur Int, Vector a)
size Vector a
vec (Ur Int, Vector a)
%1 -> ((Ur Int, Vector a) %1 -> (Ur (Maybe a), Vector a))
%1 -> (Ur (Maybe a), Vector a)
forall a b. a %1 -> (a %1 -> b) %1 -> b
& \case
(Ur Int
0, Vector a
vec') ->
(Maybe a -> Ur (Maybe a)
forall a. a -> Ur a
Ur Maybe a
forall a. Maybe a
Nothing, Vector a
vec')
(Ur Int
s, Vector a
vec') ->
Int -> Vector a %1 -> (Ur a, Vector a)
forall a. HasCallStack => Int -> Vector a %1 -> (Ur a, Vector a)
get (Int
sInt %1 -> Int %1 -> Int
forall a. AdditiveGroup a => a %1 -> a %1 -> a
-Int
1) Vector a
vec' (Ur a, Vector a)
%1 -> ((Ur a, Vector a) %1 -> (Ur (Maybe a), Vector a))
%1 -> (Ur (Maybe a), Vector a)
forall a b. a %1 -> (a %1 -> b) %1 -> b
& \(Ur a
a, Vec Int
_ Array a
arr) ->
( Maybe a -> Ur (Maybe a)
forall a. a -> Ur a
Ur (a -> Maybe a
forall a. a -> Maybe a
Just a
a)
, Int -> Array a %1 -> Vector a
forall a. Int -> Array a -> Vector a
Vec (Int
sInt %1 -> Int %1 -> Int
forall a. AdditiveGroup a => a %1 -> a %1 -> a
-Int
1) Array a
arr
)
set :: HasCallStack => Int -> a -> Vector a %1-> Vector a
set :: forall a. HasCallStack => Int -> a -> Vector a %1 -> Vector a
set Int
ix a
val Vector a
vec =
Int -> a -> Vector a %1 -> Vector a
forall a. HasCallStack => Int -> a -> Vector a %1 -> Vector a
unsafeSet Int
ix a
val (Int -> Vector a %1 -> Vector a
forall a. HasCallStack => Int -> Vector a %1 -> Vector a
assertIndexInRange Int
ix Vector a
vec)
unsafeSet :: HasCallStack => Int -> a -> Vector a %1-> Vector a
unsafeSet :: forall a. HasCallStack => Int -> a -> Vector a %1 -> Vector a
unsafeSet Int
ix a
val (Vec Int
size' Array a
arr) =
Int -> Array a %1 -> Vector a
forall a. Int -> Array a -> Vector a
Vec Int
size' (Int -> a -> Array a %1 -> Array a
forall a. Int -> a -> Array a %1 -> Array a
Array.unsafeSet Int
ix a
val Array a
arr)
get :: HasCallStack => Int -> Vector a %1-> (Ur a, Vector a)
get :: forall a. HasCallStack => Int -> Vector a %1 -> (Ur a, Vector a)
get Int
ix Vector a
vec =
Int -> Vector a %1 -> (Ur a, Vector a)
forall a. HasCallStack => Int -> Vector a %1 -> (Ur a, Vector a)
unsafeGet Int
ix (Int -> Vector a %1 -> Vector a
forall a. HasCallStack => Int -> Vector a %1 -> Vector a
assertIndexInRange Int
ix Vector a
vec)
unsafeGet :: HasCallStack => Int -> Vector a %1-> (Ur a, Vector a)
unsafeGet :: forall a. HasCallStack => Int -> Vector a %1 -> (Ur a, Vector a)
unsafeGet Int
ix (Vec Int
size' Array a
arr) =
Int -> Array a %1 -> (Ur a, Array a)
forall a. Int -> Array a %1 -> (Ur a, Array a)
Array.unsafeGet Int
ix Array a
arr
(Ur a, Array a)
%1 -> ((Ur a, Array a) %1 -> (Ur a, Vector a))
%1 -> (Ur a, Vector a)
forall a b. a %1 -> (a %1 -> b) %1 -> b
& \(Ur a
val, Array a
arr') -> (Ur a
val, Int -> Array a %1 -> Vector a
forall a. Int -> Array a -> Vector a
Vec Int
size' Array a
arr')
unsafeModify :: HasCallStack => (a -> (a, b)) -> Int
-> Vector a %1-> (Ur b, Vector a)
unsafeModify :: forall a b.
HasCallStack =>
(a -> (a, b)) -> Int -> Vector a %1 -> (Ur b, Vector a)
unsafeModify a -> (a, b)
f Int
ix (Vec Int
size' Array a
arr) =
Int -> Array a %1 -> (Ur a, Array a)
forall a. Int -> Array a %1 -> (Ur a, Array a)
Array.unsafeGet Int
ix Array a
arr (Ur a, Array a)
%1 -> ((Ur a, Array a) %1 -> (Ur b, Vector a))
%1 -> (Ur b, Vector a)
forall a b. a %1 -> (a %1 -> b) %1 -> b
& \(Ur a
old, Array a
arr') ->
case a -> (a, b)
f a
old of
(a
a, b
b) -> Int -> a -> Array a %1 -> Array a
forall a. Int -> a -> Array a %1 -> Array a
Array.unsafeSet Int
ix a
a Array a
arr' Array a
%1 -> (Array a %1 -> (Ur b, Vector a)) %1 -> (Ur b, Vector a)
forall a b. a %1 -> (a %1 -> b) %1 -> b
& \Array a
arr'' ->
(b -> Ur b
forall a. a -> Ur a
Ur b
b, Int -> Array a %1 -> Vector a
forall a. Int -> Array a -> Vector a
Vec Int
size' Array a
arr'')
modify :: HasCallStack => (a -> (a, b)) -> Int
-> Vector a %1-> (Ur b, Vector a)
modify :: forall a b.
HasCallStack =>
(a -> (a, b)) -> Int -> Vector a %1 -> (Ur b, Vector a)
modify a -> (a, b)
f Int
ix Vector a
vec = (a -> (a, b)) -> Int -> Vector a %1 -> (Ur b, Vector a)
forall a b.
HasCallStack =>
(a -> (a, b)) -> Int -> Vector a %1 -> (Ur b, Vector a)
unsafeModify a -> (a, b)
f Int
ix (Int -> Vector a %1 -> Vector a
forall a. HasCallStack => Int -> Vector a %1 -> Vector a
assertIndexInRange Int
ix Vector a
vec)
modify_ :: HasCallStack => (a -> a) -> Int -> Vector a %1-> Vector a
modify_ :: forall a.
HasCallStack =>
(a -> a) -> Int -> Vector a %1 -> Vector a
modify_ a -> a
f Int
ix Vector a
vec =
(a -> (a, ())) -> Int -> Vector a %1 -> (Ur (), Vector a)
forall a b.
HasCallStack =>
(a -> (a, b)) -> Int -> Vector a %1 -> (Ur b, Vector a)
modify (\a
a -> (a -> a
f a
a, ())) Int
ix Vector a
vec
(Ur (), Vector a)
%1 -> ((Ur (), Vector a) %1 -> Vector a) %1 -> Vector a
forall a b. a %1 -> (a %1 -> b) %1 -> b
& \(Ur (), Vector a
vec') -> Vector a
vec'
toList :: Vector a %1-> Ur [a]
toList :: forall a. Vector a %1 -> Ur [a]
toList (Vec Int
s Array a
arr) =
Array a %1 -> Ur [a]
forall a. Array a %1 -> Ur [a]
Array.toList Array a
arr Ur [a] %1 -> (Ur [a] %1 -> Ur [a]) %1 -> Ur [a]
forall a b. a %1 -> (a %1 -> b) %1 -> b
& \(Ur [a]
xs) ->
[a] -> Ur [a]
forall a. a -> Ur a
Ur (Int -> [a] -> [a]
forall a. Int -> [a] -> [a]
Prelude.take Int
s [a]
xs)
filter :: Vector a %1-> (a -> Bool) -> Vector a
filter :: forall a. Vector a %1 -> (a -> Bool) -> Vector a
filter Vector a
v a -> Bool
f = Vector a %1 -> (a -> Maybe a) -> Vector a
forall a b. Vector a %1 -> (a -> Maybe b) -> Vector b
mapMaybe Vector a
v (\a
a -> if a -> Bool
f a
a then a -> Maybe a
forall a. a -> Maybe a
Just a
a else Maybe a
forall a. Maybe a
Nothing)
mapMaybe :: Vector a %1-> (a -> Maybe b) -> Vector b
mapMaybe :: forall a b. Vector a %1 -> (a -> Maybe b) -> Vector b
mapMaybe Vector a
vec (a -> Maybe b
f :: a -> Maybe b) =
Vector a %1 -> (Ur Int, Vector a)
forall a. Vector a %1 -> (Ur Int, Vector a)
size Vector a
vec (Ur Int, Vector a)
%1 -> ((Ur Int, Vector a) %1 -> Vector b) %1 -> Vector b
forall a b. a %1 -> (a %1 -> b) %1 -> b
& \(Ur Int
s, Vector a
vec') -> Int -> Int -> Int -> Vector a %1 -> Vector b
go Int
0 Int
0 Int
s Vector a
vec'
where
go :: Int
-> Int
-> Int
-> Vector a %1-> Vector b
go :: Int -> Int -> Int -> Vector a %1 -> Vector b
go Int
r Int
w Int
s Vector a
vec'
| Int
r Int %1 -> Int %1 -> Bool
forall a. Eq a => a %1 -> a %1 -> Bool
== Int
s =
Vector a
vec' Vector a %1 -> (Vector a %1 -> Vector b) %1 -> Vector b
forall a b. a %1 -> (a %1 -> b) %1 -> b
& \(Vec Int
_ Array a
arr) ->
Int -> Array b %1 -> Vector b
forall a. Int -> Array a -> Vector a
Vec Int
w (Array a %1 -> Array b
forall a b. a %1 -> b
Unsafe.coerce Array a
arr)
| Bool
otherwise =
Int -> Vector a %1 -> (Ur a, Vector a)
forall a. HasCallStack => Int -> Vector a %1 -> (Ur a, Vector a)
unsafeGet Int
r Vector a
vec' (Ur a, Vector a)
%1 -> ((Ur a, Vector a) %1 -> Vector b) %1 -> Vector b
forall a b. a %1 -> (a %1 -> b) %1 -> b
& \case
(Ur a
a, Vector a
vec'')
| Just b
b <- a -> Maybe b
f a
a ->
Int -> Int -> Int -> Vector a %1 -> Vector b
go (Int
rInt %1 -> Int %1 -> Int
forall a. Additive a => a %1 -> a %1 -> a
+Int
1) (Int
wInt %1 -> Int %1 -> Int
forall a. Additive a => a %1 -> a %1 -> a
+Int
1) Int
s (Int -> a -> Vector a %1 -> Vector a
forall a. HasCallStack => Int -> a -> Vector a %1 -> Vector a
unsafeSet Int
w (b %1 -> a
forall a b. a %1 -> b
Unsafe.coerce b
b) Vector a
vec'')
| Bool
otherwise ->
Int -> Int -> Int -> Vector a %1 -> Vector b
go (Int
rInt %1 -> Int %1 -> Int
forall a. Additive a => a %1 -> a %1 -> a
+Int
1) Int
w Int
s Vector a
vec''
shrinkToFit :: Vector a %1-> Vector a
shrinkToFit :: forall a. Vector a %1 -> Vector a
shrinkToFit Vector a
vec =
Vector a %1 -> (Ur Int, Vector a)
forall a. Vector a %1 -> (Ur Int, Vector a)
capacity Vector a
vec (Ur Int, Vector a)
%1 -> ((Ur Int, Vector a) %1 -> Vector a) %1 -> Vector a
forall a b. a %1 -> (a %1 -> b) %1 -> b
& \(Ur Int
cap, Vector a
vec') ->
Vector a %1 -> (Ur Int, Vector a)
forall a. Vector a %1 -> (Ur Int, Vector a)
size Vector a
vec' (Ur Int, Vector a)
%1 -> ((Ur Int, Vector a) %1 -> Vector a) %1 -> Vector a
forall a b. a %1 -> (a %1 -> b) %1 -> b
& \(Ur Int
s', Vector a
vec'') ->
if Int
cap Int %1 -> Int %1 -> Bool
forall a. Ord a => a %1 -> a %1 -> Bool
> Int
s'
then Int -> Vector a %1 -> Vector a
forall a. HasCallStack => Int -> Vector a %1 -> Vector a
unsafeResize Int
s' Vector a
vec''
else Vector a
vec''
slice :: Int -> Int -> Vector a %1-> Vector a
slice :: forall a. Int -> Int -> Vector a %1 -> Vector a
slice Int
from Int
newSize (Vec Int
oldSize Array a
arr) =
if Int
oldSize Int %1 -> Int %1 -> Bool
forall a. Ord a => a %1 -> a %1 -> Bool
< Int
from Int %1 -> Int %1 -> Int
forall a. Additive a => a %1 -> a %1 -> a
+ Int
newSize
then Array a
arr Array a %1 -> Vector a %1 -> Vector a
forall a b. Consumable a => a %1 -> b %1 -> b
`lseq` [Char] -> Vector a
forall a. HasCallStack => [Char] -> a
error [Char]
"Slice index out of bounds"
else if Int
from Int %1 -> Int %1 -> Bool
forall a. Eq a => a %1 -> a %1 -> Bool
== Int
0
then Int -> Array a %1 -> Vector a
forall a. Int -> Array a -> Vector a
Vec Int
newSize Array a
arr
else Int -> Int -> Array a %1 -> (Array a, Array a)
forall a.
HasCallStack =>
Int -> Int -> Array a %1 -> (Array a, Array a)
Array.slice Int
from Int
newSize Array a
arr (Array a, Array a)
%1 -> ((Array a, Array a) %1 -> Vector a) %1 -> Vector a
forall a b. a %1 -> (a %1 -> b) %1 -> b
& \(Array a
oldArr, Array a
newArr) ->
Array a
oldArr Array a %1 -> Vector a %1 -> Vector a
forall a b. Consumable a => a %1 -> b %1 -> b
`lseq` Array a %1 -> Vector a
forall a. HasCallStack => Array a %1 -> Vector a
fromArray Array a
newArr
freeze :: Vector a %1-> Ur (Vector.Vector a)
freeze :: forall a. Vector a %1 -> Ur (Vector a)
freeze (Vec Int
sz Array a
arr) =
Array a %1 -> Ur (Vector a)
forall a. Array a %1 -> Ur (Vector a)
Array.freeze Array a
arr
Ur (Vector a)
%1 -> (Ur (Vector a) %1 -> Ur (Vector a)) %1 -> Ur (Vector a)
forall a b. a %1 -> (a %1 -> b) %1 -> b
& \(Ur Vector a
vec) -> Vector a -> Ur (Vector a)
forall a. a -> Ur a
Ur (Int -> Vector a -> Vector a
forall a. Int -> Vector a -> Vector a
Vector.take Int
sz Vector a
vec)
write :: HasCallStack => Vector a %1-> Int -> a -> Vector a
write :: forall a. HasCallStack => Vector a %1 -> Int -> a -> Vector a
write Vector a
arr Int
i a
a = Int -> a -> Vector a %1 -> Vector a
forall a. HasCallStack => Int -> a -> Vector a %1 -> Vector a
set Int
i a
a Vector a
arr
unsafeWrite :: Vector a %1-> Int -> a -> Vector a
unsafeWrite :: forall a. Vector a %1 -> Int -> a -> Vector a
unsafeWrite Vector a
arr Int
i a
a = Int -> a -> Vector a %1 -> Vector a
forall a. HasCallStack => Int -> a -> Vector a %1 -> Vector a
unsafeSet Int
i a
a Vector a
arr
read :: HasCallStack => Vector a %1-> Int -> (Ur a, Vector a)
read :: forall a. HasCallStack => Vector a %1 -> Int -> (Ur a, Vector a)
read Vector a
arr Int
i = Int -> Vector a %1 -> (Ur a, Vector a)
forall a. HasCallStack => Int -> Vector a %1 -> (Ur a, Vector a)
get Int
i Vector a
arr
unsafeRead :: Vector a %1-> Int -> (Ur a, Vector a)
unsafeRead :: forall a. Vector a %1 -> Int -> (Ur a, Vector a)
unsafeRead Vector a
arr Int
i = Int -> Vector a %1 -> (Ur a, Vector a)
forall a. HasCallStack => Int -> Vector a %1 -> (Ur a, Vector a)
unsafeGet Int
i Vector a
arr
instance Consumable (Vector a) where
consume :: Vector a %1 -> ()
consume (Vec Int
_ Array a
arr) = Array a %1 -> ()
forall a. Consumable a => a %1 -> ()
consume Array a
arr
instance Dupable (Vector a) where
dup2 :: Vector a %1 -> (Vector a, Vector a)
dup2 (Vec Int
i Array a
arr) = Array a %1 -> (Array a, Array a)
forall a. Dupable a => a %1 -> (a, a)
dup2 Array a
arr (Array a, Array a)
%1 -> ((Array a, Array a) %1 -> (Vector a, Vector a))
%1 -> (Vector a, Vector a)
forall a b. a %1 -> (a %1 -> b) %1 -> b
& \(Array a
a1, Array a
a2) ->
(Int -> Array a %1 -> Vector a
forall a. Int -> Array a -> Vector a
Vec Int
i Array a
a1, Int -> Array a %1 -> Vector a
forall a. Int -> Array a -> Vector a
Vec Int
i Array a
a2)
instance Prelude.Semigroup (Vector a) where
Vector a
v1 <> :: Vector a -> Vector a -> Vector a
<> Vector a
v2 = Vector a
v1 Vector a %1 -> Vector a %1 -> Vector a
forall a. Semigroup a => a %1 -> a %1 -> a
Data.Monoid.Linear.<> Vector a
v2
instance Semigroup (Vector a) where
Vector a
v1 <> :: Vector a %1 -> Vector a %1 -> Vector a
<> Vector a
v2 =
Vector a %1 -> (Ur Int, Vector a)
forall a. Vector a %1 -> (Ur Int, Vector a)
size Vector a
v2 (Ur Int, Vector a)
%1 -> ((Ur Int, Vector a) %1 -> Vector a) %1 -> Vector a
forall a b. a %1 -> (a %1 -> b) %1 -> b
& \(Ur Int
s2, Vector a
v2') ->
Int -> Vector a %1 -> Vector a
forall a. HasCallStack => Int -> Vector a %1 -> Vector a
growToFit Int
s2 Vector a
v1 Vector a %1 -> (Vector a %1 -> Vector a) %1 -> Vector a
forall a b. a %1 -> (a %1 -> b) %1 -> b
& \Vector a
v1' ->
Vector a %1 -> Ur [a]
forall a. Vector a %1 -> Ur [a]
toList Vector a
v2' Ur [a] %1 -> (Ur [a] %1 -> Vector a) %1 -> Vector a
forall a b. a %1 -> (a %1 -> b) %1 -> b
& \(Ur [a]
xs) ->
[a] -> Vector a %1 -> Vector a
go [a]
xs Vector a
v1'
where
go :: [a] -> Vector a %1-> Vector a
go :: [a] -> Vector a %1 -> Vector a
go [] Vector a
vec = Vector a
vec
go (a
x:[a]
xs) (Vec Int
sz Array a
arr) =
Int -> a -> Vector a %1 -> Vector a
forall a. HasCallStack => Int -> a -> Vector a %1 -> Vector a
unsafeSet Int
sz a
x (Int -> Array a %1 -> Vector a
forall a. Int -> Array a -> Vector a
Vec (Int
szInt %1 -> Int %1 -> Int
forall a. Additive a => a %1 -> a %1 -> a
+Int
1) Array a
arr)
Vector a %1 -> (Vector a %1 -> Vector a) %1 -> Vector a
forall a b. a %1 -> (a %1 -> b) %1 -> b
& [a] -> Vector a %1 -> Vector a
go [a]
xs
instance Data.Functor Vector where
fmap :: forall a b. (a %1 -> b) -> Vector a %1 -> Vector b
fmap a %1 -> b
f Vector a
vec = Vector a %1 -> (a -> Maybe b) -> Vector b
forall a b. Vector a %1 -> (a -> Maybe b) -> Vector b
mapMaybe Vector a
vec (\a
a -> b -> Maybe b
forall a. a -> Maybe a
Just (a %1 -> b
f a
a))
growToFit :: HasCallStack => Int -> Vector a %1-> Vector a
growToFit :: forall a. HasCallStack => Int -> Vector a %1 -> Vector a
growToFit Int
n Vector a
vec =
Vector a %1 -> (Ur Int, Vector a)
forall a. Vector a %1 -> (Ur Int, Vector a)
capacity Vector a
vec (Ur Int, Vector a)
%1 -> ((Ur Int, Vector a) %1 -> Vector a) %1 -> Vector a
forall a b. a %1 -> (a %1 -> b) %1 -> b
& \(Ur Int
cap, Vector a
vec') ->
Vector a %1 -> (Ur Int, Vector a)
forall a. Vector a %1 -> (Ur Int, Vector a)
size Vector a
vec' (Ur Int, Vector a)
%1 -> ((Ur Int, Vector a) %1 -> Vector a) %1 -> Vector a
forall a b. a %1 -> (a %1 -> b) %1 -> b
& \(Ur Int
s', Vector a
vec'') ->
if Int
s' Int %1 -> Int %1 -> Int
forall a. Additive a => a %1 -> a %1 -> a
+ Int
n Int %1 -> Int %1 -> Bool
forall a. Ord a => a %1 -> a %1 -> Bool
<= Int
cap
then Vector a
vec''
else
let
newSize :: Int
newSize =
Int
constGrowthFactor
Int -> Int -> Int
forall a b. (Num a, Integral b) => a -> b -> a
^ (Double -> Int
forall a b. (RealFrac a, Integral b) => a -> b
ceiling :: Double -> Int)
(Double -> Double -> Double
forall a. Floating a => a -> a -> a
logBase
(Int -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
constGrowthFactor)
(Int -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int
s' Int %1 -> Int %1 -> Int
forall a. Additive a => a %1 -> a %1 -> a
+ Int
n)))
in Int -> Vector a %1 -> Vector a
forall a. HasCallStack => Int -> Vector a %1 -> Vector a
unsafeResize
Int
newSize
Vector a
vec''
unsafeResize :: HasCallStack => Int -> Vector a %1-> Vector a
unsafeResize :: forall a. HasCallStack => Int -> Vector a %1 -> Vector a
unsafeResize Int
newSize (Vec Int
size' Array a
ma) =
Int -> Array a %1 -> Vector a
forall a. Int -> Array a -> Vector a
Vec
(Int %1 -> Int %1 -> Int
forall a. (Dupable a, Ord a) => a %1 -> a %1 -> a
min Int
size' Int
newSize)
(Int -> a -> Array a %1 -> Array a
forall a. HasCallStack => Int -> a -> Array a %1 -> Array a
Array.resize
Int
newSize
([Char] -> a
forall a. HasCallStack => [Char] -> a
error [Char]
"access to uninitialized vector index")
Array a
ma
)
assertIndexInRange :: HasCallStack => Int -> Vector a %1-> Vector a
assertIndexInRange :: forall a. HasCallStack => Int -> Vector a %1 -> Vector a
assertIndexInRange Int
i Vector a
vec =
Vector a %1 -> (Ur Int, Vector a)
forall a. Vector a %1 -> (Ur Int, Vector a)
size Vector a
vec (Ur Int, Vector a)
%1 -> ((Ur Int, Vector a) %1 -> Vector a) %1 -> Vector a
forall a b. a %1 -> (a %1 -> b) %1 -> b
& \(Ur Int
s, Vector a
vec') ->
if Int
0 Int %1 -> Int %1 -> Bool
forall a. Ord a => a %1 -> a %1 -> Bool
<= Int
i Bool %1 -> Bool %1 -> Bool
&& Int
i Int %1 -> Int %1 -> Bool
forall a. Ord a => a %1 -> a %1 -> Bool
< Int
s
then Vector a
vec'
else Vector a
vec' Vector a %1 -> Vector a %1 -> Vector a
forall a b. Consumable a => a %1 -> b %1 -> b
`lseq` [Char] -> Vector a
forall a. HasCallStack => [Char] -> a
error [Char]
"Vector: index out of bounds"