Safe Haskell | None |
---|---|
Language | GHC2021 |
Pushable vector with fixed size capacity. Stack. Internally it has 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
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)
- pushBack :: (HasCallStack, PrimMonad m, Unbox a) => Buffer (PrimState m) a -> a -> m ()
- popBack :: (PrimMonad m, Unbox a) => Buffer (PrimState m) a -> m (Maybe a)
- 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
- 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 ()
- 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
- 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
Constructors
new :: (PrimMonad m, Unbox a) => Int -> m (Buffer (PrimState m) a) #
\(O(n)\) Creates a buffer with capacity \(n\).
Since: 1.0.0
build :: (PrimMonad m, Unbox a) => Vector a -> m (Buffer (PrimState m) a) #
\(O(n)\) Creates a buffer with capacity \(n\) with initial values.
Since: 1.0.0
Push/pop
pushBack :: (HasCallStack, PrimMonad m, Unbox a) => Buffer (PrimState m) a -> a -> m () #
\(O(1)\) Appends an element to the back.
Since: 1.0.0
popBack :: (PrimMonad m, Unbox a) => Buffer (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
Inspection
back :: (PrimMonad m, Unbox a) => Buffer (PrimState m) a -> m (Maybe a) #
\(O(1)\) Returns the last value in the buffer, or Nothing
if it is empty.
Since: 1.0.0
Accessing individual elements
read :: (HasCallStack, PrimMonad m, Unbox a) => Buffer (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) => Buffer (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
modify :: (HasCallStack, PrimMonad m, Unbox a) => Buffer (PrimState m) a -> (a -> a) -> Int -> 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
modifyM :: (HasCallStack, PrimMonad m, Unbox a) => Buffer (PrimState m) a -> (a -> m a) -> Int -> 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
Accesssors
length :: (PrimMonad m, Unbox a) => Buffer (PrimState m) a -> m Int #
\(O(1)\) Returns the number of elements in the buffer.
Since: 1.0.0
null :: (PrimMonad m, Unbox a) => Buffer (PrimState m) a -> m Bool #
\(O(1)\) Returns True
if the buffer is empty.
Since: 1.0.0
Clearing
clear :: (PrimMonad m, Unbox a) => Buffer (PrimState m) a -> m () #
\(O(1)\) Sets the length
to zero.
Since: 1.0.0