rio-0.1.22.0: A standard library for Haskell

Safe Haskell | None
Language | Haskell2010

RIO.Deque

Contents

- Types
- Operations
- Inference helpers

Synopsis

- data Deque (v :: Type -> Type -> Type) s a
- type UDeque = Deque MVector
- type SDeque = Deque MVector
- type BDeque = Deque MVector
- newDeque :: forall (v :: Type -> Type -> Type) a m. (MVector v a, PrimMonad m) => m (Deque v (PrimState m) a)
- getDequeSize :: forall m (v :: Type -> Type -> Type) a. PrimMonad m => Deque v (PrimState m) a -> m Int
- popFrontDeque :: forall (v :: Type -> Type -> Type) a m. (MVector v a, PrimMonad m) => Deque v (PrimState m) a -> m (Maybe a)
- popBackDeque :: forall (v :: Type -> Type -> Type) a m. (MVector v a, PrimMonad m) => Deque v (PrimState m) a -> m (Maybe a)
- pushFrontDeque :: forall (v :: Type -> Type -> Type) a m. (MVector v a, PrimMonad m) => Deque v (PrimState m) a -> a -> m ()
- pushBackDeque :: forall (v :: Type -> Type -> Type) a m. (MVector v a, PrimMonad m) => Deque v (PrimState m) a -> a -> m ()
- foldlDeque :: forall (v :: Type -> Type -> Type) a m acc. (MVector v a, PrimMonad m) => (acc -> a -> m acc) -> acc -> Deque v (PrimState m) a -> m acc
- foldrDeque :: forall (v :: Type -> Type -> Type) a m acc. (MVector v a, PrimMonad m) => (a -> acc -> m acc) -> acc -> Deque v (PrimState m) a -> m acc
- dequeToList :: forall (v :: Type -> Type -> Type) a m. (MVector v a, PrimMonad m) => Deque v (PrimState m) a -> m [a]
- dequeToVector :: forall v' a (v :: Type -> Type -> Type) m. (Vector v' a, MVector v a, PrimMonad m) => Deque v (PrimState m) a -> m (v' a)
- freezeDeque :: (Vector v a, PrimMonad m) => Deque (Mutable v) (PrimState m) a -> m (v a)
- asUDeque :: UDeque s a -> UDeque s a
- asSDeque :: SDeque s a -> SDeque s a
- asBDeque :: BDeque s a -> BDeque s a

# Types

data Deque (v :: Type -> Type -> Type) s a #

A double-ended queue supporting any underlying vector type and any monad.

This implements a circular double-ended queue with exponential growth.

Since: 0.1.9.0

type UDeque = Deque MVector #

A `Deque` specialized to unboxed vectors.

Since: 0.1.9.0

type SDeque = Deque MVector #

A `Deque` specialized to storable vectors.

Since: 0.1.9.0

type BDeque = Deque MVector #

A `Deque` specialized to boxed vectors.

Since: 0.1.9.0

# Operations

newDeque :: forall (v :: Type -> Type -> Type) a m. (MVector v a, PrimMonad m) => m (Deque v (PrimState m) a) #

Create a new, empty `Deque`

Since: 0.1.9.0

getDequeSize :: forall m (v :: Type -> Type -> Type) a. PrimMonad m => Deque v (PrimState m) a -> m Int #

O(1) - Get the number of elements that is currently in the `Deque`

Since: 0.1.9.0

popFrontDeque :: forall (v :: Type -> Type -> Type) a m. (MVector v a, PrimMonad m) => Deque v (PrimState m) a -> m (Maybe a) #

Pop the first value from the beginning of the `Deque`

Since: 0.1.9.0

popBackDeque :: forall (v :: Type -> Type -> Type) a m. (MVector v a, PrimMonad m) => Deque v (PrimState m) a -> m (Maybe a) #

Pop the first value from the end of the `Deque`

Since: 0.1.9.0

pushFrontDeque :: forall (v :: Type -> Type -> Type) a m. (MVector v a, PrimMonad m) => Deque v (PrimState m) a -> a -> m () #

Push a new value to the beginning of the `Deque`

Since: 0.1.9.0

pushBackDeque :: forall (v :: Type -> Type -> Type) a m. (MVector v a, PrimMonad m) => Deque v (PrimState m) a -> a -> m () #

Push a new value to the end of the `Deque`

Since: 0.1.9.0

foldlDeque :: forall (v :: Type -> Type -> Type) a m acc. (MVector v a, PrimMonad m) => (acc -> a -> m acc) -> acc -> Deque v (PrimState m) a -> m acc #

Fold over a `Deque`, starting at the beginning. Does not modify the `Deque`.

Since: 0.1.9.0

foldrDeque :: forall (v :: Type -> Type -> Type) a m acc. (MVector v a, PrimMonad m) => (a -> acc -> m acc) -> acc -> Deque v (PrimState m) a -> m acc #

Fold over a `Deque`, starting at the end. Does not modify the `Deque`.

Since: 0.1.9.0

dequeToList :: forall (v :: Type -> Type -> Type) a m. (MVector v a, PrimMonad m) => Deque v (PrimState m) a -> m [a] #

Convert a `Deque` into a list. Does not modify the `Deque`.

Since: 0.1.9.0

dequeToVector :: forall v' a (v :: Type -> Type -> Type) m. (Vector v' a, MVector v a, PrimMonad m) => Deque v (PrimState m) a -> m (v' a) #

Convert to an immutable vector of any type. If resulting pure vector corresponds to the mutable one used by the `Deque`, it will be more efficient to use `freezeDeque` instead.

#### Example

Expand

    >>> :set -XTypeApplications
    >>> import qualified RIO.Vector.Unboxed as U
    >>> import qualified RIO.Vector.Storable as S
    >>> d <- newDeque @U.MVector @Int
    >>> mapM_ (pushFrontDeque d) [0..10]
    >>> dequeToVector @S.Vector d
    [10,9,8,7,6,5,4,3,2,1,0]

Since: 0.1.9.0

freezeDeque :: (Vector v a, PrimMonad m) => Deque (Mutable v) (PrimState m) a -> m (v a) #

Yield an immutable copy of the underlying mutable vector. The difference from `dequeToVector` is that the the copy will be performed with a more efficient `memcpy`, rather than element by element. The downside is that the resulting vector type must be the one that corresponds to the mutable one that is used in the `Deque`.

#### Example

Expand

    >>> :set -XTypeApplications
    >>> import qualified RIO.Vector.Unboxed as U
    >>> d <- newDeque @U.MVector @Int
    >>> mapM_ (pushFrontDeque d) [0..10]
    >>> freezeDeque @U.Vector d
    [10,9,8,7,6,5,4,3,2,1,0]

Since: 0.1.9.0

# Inference helpers

asUDeque :: UDeque s a -> UDeque s a #

Helper function to assist with type inference, forcing usage of an unboxed vector.

Since: 0.1.9.0

asSDeque :: SDeque s a -> SDeque s a #

Helper function to assist with type inference, forcing usage of a storable vector.

Since: 0.1.9.0

asBDeque :: BDeque s a -> BDeque s a #

Helper function to assist with type inference, forcing usage of a boxed vector.

Since: 0.1.9.0