Safe Haskell | Safe-Inferred |
---|---|
Language | GHC2021 |
Growable vector with some runtime overhead (by MutVar
).
Example
>>>
import AtCoder.Internal.GrowVec qualified as GV
>>>
growVec <- GV.new @_ @Int 0
>>>
GV.null growVec
True
>>>
GV.pushBack growVec 10
>>>
GV.pushBack growVec 11
>>>
GV.pushBack growVec 12
>>>
GV.freeze growVec
[10,11,12]
>>>
GV.length growVec
3
>>>
GV.capacity growVec
4
>>>
GV.write growVec 1 20
>>>
GV.read growVec 1
20
>>>
GV.popBack growVec
Just 12
>>>
GV.popBack growVec
Just 20
>>>
GV.reserve growVec 20
>>>
GV.capacity growVec
20
>>>
GV.unsafeFreeze growVec
[10]
Since: 1.0.0.0
Synopsis
- data GrowVec s a
- new :: (PrimMonad m, Unbox a) => Int -> m (GrowVec (PrimState m) a)
- build :: (PrimMonad m, Unbox a) => Vector a -> m (GrowVec (PrimState m) a)
- reserve :: (HasCallStack, PrimMonad m, Unbox a) => GrowVec (PrimState m) a -> Int -> m ()
- length :: (PrimMonad m, Unbox a) => GrowVec (PrimState m) a -> m Int
- capacity :: (PrimMonad m, Unbox a) => GrowVec (PrimState m) a -> m Int
- null :: (PrimMonad m, Unbox a) => GrowVec (PrimState m) a -> m Bool
- read :: (HasCallStack, PrimMonad m, Unbox a) => GrowVec (PrimState m) a -> Int -> m a
- write :: (HasCallStack, PrimMonad m, Unbox a) => GrowVec (PrimState m) a -> Int -> a -> m ()
- pushBack :: (PrimMonad m, Unbox a) => GrowVec (PrimState m) a -> a -> m ()
- popBack :: (PrimMonad m, Unbox a) => GrowVec (PrimState m) a -> m (Maybe a)
- popBack_ :: (PrimMonad m, Unbox a) => GrowVec (PrimState m) a -> m ()
- freeze :: (PrimMonad m, Unbox a) => GrowVec (PrimState m) a -> m (Vector a)
- unsafeFreeze :: (PrimMonad m, Unbox a) => GrowVec (PrimState m) a -> m (Vector a)
GrowVec
Constructors
new :: (PrimMonad m, Unbox a) => Int -> m (GrowVec (PrimState m) a) Source #
\(O(n)\) Creates a GrowVec
with initial capacity \(n\).
Since: 1.0.0.0
build :: (PrimMonad m, Unbox a) => Vector a -> m (GrowVec (PrimState m) a) Source #
\(O(n)\) Creates a GrowVec
with initial values.
Since: 1.0.0.0
reserve :: (HasCallStack, PrimMonad m, Unbox a) => GrowVec (PrimState m) a -> Int -> m () Source #
\(O(n)\) Reserves the internal storage capacity.
Since: 1.0.0.0
Metadata
length :: (PrimMonad m, Unbox a) => GrowVec (PrimState m) a -> m Int Source #
\(O(1)\) Returns the number of elements in the vector.
Since: 1.0.0.0
capacity :: (PrimMonad m, Unbox a) => GrowVec (PrimState m) a -> m Int Source #
\(O(1)\) Returns the capacity of the internal the vector.
Since: 1.0.0.0
null :: (PrimMonad m, Unbox a) => GrowVec (PrimState m) a -> m Bool Source #
\(O(1)\) Returns True
if the vector is empty.
Since: 1.0.0.0
Readings
read :: (HasCallStack, PrimMonad m, Unbox a) => GrowVec (PrimState m) a -> Int -> m a Source #
\(O(1)\) Yields the element at the given position. Will throw an exception if the index is out of range.
Since: 1.0.0.0
Modifications
Writing
write :: (HasCallStack, PrimMonad m, Unbox a) => GrowVec (PrimState m) a -> Int -> a -> m () Source #
\(O(1)\) Writes to the element at the given position. Will throw an exception if the index is out of range.
Since: 1.0.0.0
Push/pop
pushBack :: (PrimMonad m, Unbox a) => GrowVec (PrimState m) a -> a -> m () Source #
Amortized \(O(1)\). Grow the capacity twice
Since: 1.0.0.0
popBack :: (PrimMonad m, Unbox a) => GrowVec (PrimState m) a -> m (Maybe a) Source #
\(O(1)\) Removes the last element from the buffer and returns it, or Nothing
if it is empty.
Since: 1.0.0.0
popBack_ :: (PrimMonad m, Unbox a) => GrowVec (PrimState m) a -> m () Source #
\(O(1)\) popBack
with the return value discarded.
Since: 1.0.0.0