Safe Haskell | Safe-Inferred |
---|---|
Language | GHC2021 |
A pushable vector with fixed capacity \(n\). Internally, it tracks the number of elements.
Example
Create a buffer with capacity 4
:
>>>
import AtCoder.Internal.Buffer qualified as B
>>>
buf <- B.new @_ @Int 4
>>>
B.capacity buf
4
>>>
B.null buf -- [_ _ _ _]
True
Append elements with pushBack
:
>>>
B.pushBack buf 10 -- [10 _ _ _]
>>>
B.pushBack buf 11 -- [10, 11 _ _]
>>>
length buf
2
Access each elements with read
, write
, modify
or modifyM
:
>>>
B.read buf 0
10
>>>
B.write buf 1 0 -- [10, 0, _ _]
Remove elements with pushBack
:
>>>
B.popBack buf -- [10 _ _ _]
Just 0
Inspect the internal vector with freeze
:
>>>
B.freeze buf
[10]
>>>
B.clear buf -- []
>>>
B.null buf
True
>>>
B.unsafeFreeze buf
[]
Since: 1.0.0.0
Synopsis
- data Buffer s a
- new :: (PrimMonad m, Unbox a) => Int -> m (Buffer (PrimState m) a)
- build :: (PrimMonad m, Unbox a) => Vector a -> m (Buffer (PrimState m) a)
- capacity :: Unbox a => Buffer s a -> Int
- length :: (PrimMonad m, Unbox a) => Buffer (PrimState m) a -> m Int
- null :: (PrimMonad m, Unbox a) => Buffer (PrimState m) a -> m Bool
- back :: (PrimMonad m, Unbox a) => Buffer (PrimState m) a -> m (Maybe a)
- read :: (HasCallStack, PrimMonad m, Unbox a) => Buffer (PrimState m) a -> Int -> m a
- pushBack :: (HasCallStack, PrimMonad m, Unbox a) => Buffer (PrimState m) a -> a -> m ()
- popBack :: (PrimMonad m, Unbox a) => Buffer (PrimState m) a -> m (Maybe a)
- write :: (HasCallStack, PrimMonad m, Unbox a) => Buffer (PrimState m) a -> Int -> a -> m ()
- modify :: (HasCallStack, PrimMonad m, Unbox a) => Buffer (PrimState m) a -> (a -> a) -> Int -> m ()
- modifyM :: (HasCallStack, PrimMonad m, Unbox a) => Buffer (PrimState m) a -> (a -> m a) -> Int -> m ()
- clear :: (PrimMonad m, Unbox a) => Buffer (PrimState m) a -> m ()
- freeze :: (PrimMonad m, Unbox a) => Buffer (PrimState m) a -> m (Vector a)
- unsafeFreeze :: (PrimMonad m, Unbox a) => Buffer (PrimState m) a -> m (Vector a)
Buffer
A pushable vector with fixed capacity \(n\). Internally, it tracks the number of elements.
Since: 1.0.0.0
Constructors
new :: (PrimMonad m, Unbox a) => Int -> m (Buffer (PrimState m) a) Source #
\(O(n)\) Creates a buffer with capacity \(n\).
Since: 1.0.0.0
build :: (PrimMonad m, Unbox a) => Vector a -> m (Buffer (PrimState m) a) Source #
\(O(n)\) Creates a buffer with capacity \(n\) with initial values.
Since: 1.0.0.0
Metadata
length :: (PrimMonad m, Unbox a) => Buffer (PrimState m) a -> m Int Source #
\(O(1)\) Returns the number of elements in the buffer.
Since: 1.0.0.0
null :: (PrimMonad m, Unbox a) => Buffer (PrimState m) a -> m Bool Source #
\(O(1)\) Returns True
if the buffer is empty.
Since: 1.0.0.0
Reading
back :: (PrimMonad m, Unbox a) => Buffer (PrimState m) a -> m (Maybe a) Source #
\(O(1)\) Returns the last value in the buffer, or Nothing
if it is empty.
Since: 1.0.0.0
read :: (HasCallStack, PrimMonad m, Unbox a) => Buffer (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
pushBack :: (HasCallStack, PrimMonad m, Unbox a) => Buffer (PrimState m) a -> a -> m () Source #
\(O(1)\) Appends an element to the back.
Since: 1.0.0.0
popBack :: (PrimMonad m, Unbox a) => Buffer (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
write :: (HasCallStack, PrimMonad m, Unbox a) => Buffer (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 bounds.
Since: 1.0.0.0
modify :: (HasCallStack, PrimMonad m, Unbox a) => Buffer (PrimState m) a -> (a -> a) -> Int -> m () Source #
\(O(1)\) Writes to the element at the given position. Will throw an exception if the index is out of bounds.
Since: 1.0.0.0
modifyM :: (HasCallStack, PrimMonad m, Unbox a) => Buffer (PrimState m) a -> (a -> m a) -> Int -> m () Source #
\(O(1)\) Writes to the element at the given position. Will throw an exception if the index is out of bounds.
Since: 1.0.0.0
clear :: (PrimMonad m, Unbox a) => Buffer (PrimState m) a -> m () Source #
\(O(1)\) Sets the length
to zero.
Since: 1.0.0.0