ac-library-hs-0.1.0.0: Data structures and algorithms
Safe HaskellNone
LanguageGHC2021

AtCoder.Internal.Buffer

Description

Pushable vector with fixed size capacity. Stack. Internally it has the number of elements.

Example

Expand

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

Buffer

data Buffer s a #

Pushable vector with fixed size capacity. Stack.

Since: 1.0.0

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

capacity :: Unbox a => Buffer s a -> Int #

\(O(1)\) Returns the array size.

Since: 1.0.0

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

Conversions

freeze :: (PrimMonad m, Unbox a) => Buffer (PrimState m) a -> m (Vector a) #

\(O(n)\) Yields an immutable copy of the mutable vector.

Since: 1.0.0

unsafeFreeze :: (PrimMonad m, Unbox a) => Buffer (PrimState m) a -> m (Vector a) #

\(O(1)\) Unsafely converts a mutable vector to an immutable one without copying. The mutable vector may not be used after this operation.

Since: 1.0.0