Safe Haskell | None |
---|---|
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
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 ()
- 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 ()
- 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
- freeze :: (PrimMonad m, Unbox a) => GrowVec (PrimState m) a -> m (Vector a)
- unsafeFreeze :: (PrimMonad m, Unbox a) => GrowVec (PrimState m) a -> m (Vector a)
Growable vector
Constructions
Initialization
new :: (PrimMonad m, Unbox a) => Int -> m (GrowVec (PrimState m) a) #
\(O(n)\) Creates GrowVec
with initial capacity \(n\).
Since: 1.0.0
build :: (PrimMonad m, Unbox a) => Vector a -> m (GrowVec (PrimState m) a) #
\(O(n)\) Creates GrowVec
with initial values.
Since: 1.0.0
Reserving
reserve :: (HasCallStack, PrimMonad m, Unbox a) => GrowVec (PrimState m) a -> Int -> m () #
\(O(n)\) Reserves the internal storage capacity.
Since: 1.0.0
Accessing individual elements
read :: (HasCallStack, PrimMonad m, Unbox a) => GrowVec (PrimState m) a -> Int -> m a #
\(O(1)\) Yields the element at the given position. Will throw an exception if the index is out of range.
Since: 1.0.0
write :: (HasCallStack, PrimMonad m, Unbox a) => GrowVec (PrimState m) a -> Int -> a -> m () #
\(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
Modifying the buffer
pushBack :: (PrimMonad m, Unbox a) => GrowVec (PrimState m) a -> a -> m () #
Amortized \(O(1)\). Grow the capacity twice
Since: 1.0.0
popBack :: (PrimMonad m, Unbox a) => GrowVec (PrimState m) a -> m (Maybe a) #
\(O(1)\) Removes the last element from the buffer and returns it, or Nothing
if it is empty.
Since: 1.0.0
popBack_ :: (PrimMonad m, Unbox a) => GrowVec (PrimState m) a -> m () #
\(O(1)\) popBack
with return value discarded.
Since: 1.0.0
Accessors
length :: (PrimMonad m, Unbox a) => GrowVec (PrimState m) a -> m Int #
\(O(1)\) Returns the number of elements in the vector.
Since: 1.0.0
capacity :: (PrimMonad m, Unbox a) => GrowVec (PrimState m) a -> m Int #
\(O(1)\) Returns the capacity of the internal the vector.
Since: 1.0.0
null :: (PrimMonad m, Unbox a) => GrowVec (PrimState m) a -> m Bool #
\(O(1)\) Returns True
if the vector is empty.
Since: 1.0.0