| Copyright | (c) Dominik Schrempf 2020 |
|---|---|
| License | GPL-3.0-or-later |
| Maintainer | dominik.schrempf@gmail.com |
| Stability | unstable |
| Portability | portable |
| Safe Haskell | None |
| Language | Haskell2010 |
Data.Stack.Circular
Description
Creation date: Thu Jun 18 10:00:28 2020.
Synopsis
- data CStack v a = CStack {}
- empty :: Vector v a => Int -> CStack v a
- unsafeEmpty :: Vector v a => Int -> CStack v a
- toVector :: Vector v a => CStack v a -> v a
- toVectorN :: Vector v a => Int -> CStack v a -> v a
- unsafeToVectorN :: Vector v a => Int -> CStack v a -> v a
- fromVector :: Vector v a => v a -> CStack v a
- unsafeFromVector :: Vector v a => v a -> CStack v a
- get :: Vector v a => CStack v a -> a
- pop :: Vector v a => CStack v a -> (a, CStack v a)
- push :: Vector v a => a -> CStack v a -> CStack v a
- unsafePush :: Vector v a => a -> CStack v a -> CStack v a
- reset :: CStack v a -> CStack v a
- isFull :: Vector v a => CStack v a -> Bool
- foldl :: (Vector v a, Vector v b) => (a -> b -> a) -> a -> CStack v b -> a
- foldl' :: (Vector v a, Vector v b) => (a -> b -> a) -> a -> CStack v b -> a
- foldl1' :: Vector v a => (a -> a -> a) -> CStack v a -> a
- sum :: (Num a, Vector v a) => CStack v a -> a
- product :: (Num a, Vector v a) => CStack v a -> a
Boxed circular stacks
Circular stacks with fxed maximum size are just normal vectors with a pointer to the last element.
Construction of CStacks is done with empty and subsequent pushes, or
the provided type conversion functions so that the index and bounds are
updated and checked consistently. The data constructor CStack is exported
only to enable creation of orphan instances such as Arbitrary (QuickCheck).
When denoting the efficiency of the functions m refers to the current size
of the stack, and n to the maximum size.
Instances
| (Eq (v a), Vector v a) => Eq (CStack v a) Source # | |
| (Show (v a), Vector v a) => Show (CStack v a) Source # | |
| (ToJSON a, ToJSON (v a), Vector v a) => ToJSON (CStack v a) Source # | We have |
Defined in Data.Stack.Circular | |
| (FromJSON a, FromJSON (v a), Vector v a) => FromJSON (CStack v a) Source # | |
Construction
empty :: Vector v a => Int -> CStack v a Source #
A circular stack without an element but of a given maximum size. At this state, it is not very useful :). O(n).
unsafeEmpty :: Vector v a => Int -> CStack v a Source #
See empty; do no check that length is strictly positive.
Conversion
toVector :: Vector v a => CStack v a -> v a Source #
Convert a circular stack to a vector. The first element of the returned vector is the deepest (oldest) element of the stack, the last element of the returned vector is the current (newest) element of the stack.
This is a relatively expensive operation. O(m).
toVectorN :: Vector v a => Int -> CStack v a -> v a Source #
Convert the last N elements of a circular stack to a vector. The first element of the returned vector is the deepest (oldest) element of the stack, the last element of the returned vector is the current (newest) element of the stack.
The size of the stack must be larger than N.
This is a relatively expensive operation. O(N).
unsafeToVectorN :: Vector v a => Int -> CStack v a -> v a Source #
See toVectorN but do not check that N is positive.
fromVector :: Vector v a => v a -> CStack v a Source #
Convert a vector to a circular stack. The first element of the vector is the deepest (oldest) element of the stack, the last element of the vector is the current (newest) element of the stack. O(n).
The vector must be non-empty.
unsafeFromVector :: Vector v a => v a -> CStack v a Source #
See fromVector but do not check for empty vector.
Accessors
pop :: Vector v a => CStack v a -> (a, CStack v a) Source #
Get the last element and remove it from the stack. O(1).
The stack must be non-empty.
push :: Vector v a => a -> CStack v a -> CStack v a Source #
Push an element on the stack. Slow! If possible, use unsafePush. O(n).
unsafePush :: Vector v a => a -> CStack v a -> CStack v a Source #
Push an element on the stack. O(1).
Be careful; the internal vector is mutated! The immutable circular stack may not be used after this operation.
Queries
Folding
foldl' :: (Vector v a, Vector v b) => (a -> b -> a) -> a -> CStack v b -> a Source #
Left fold with strict accumulator. O(m).
foldl1' :: Vector v a => (a -> a -> a) -> CStack v a -> a Source #
Left fold on non-empty vectors with strict accumulator. O(m).