Safe Haskell | None |
---|---|
Language | Haskell2010 |
Auto-growing ArrayList
type that over-allocates memory to optimize frequent resizes.
This is similar to std::vector
from C++ or list
from Python.
When an ArrayList
is requested to grow and current allocated capacity can't hold the
required number of items, capacity is multiplied by a constant factor of 1.5
, instead
of allocating just the required number of items. This ensures that frequent grow requests
will result in a much lower number of actual allocations and memory moves.
All functions in this module are marked INLINEABLE
, so that they may be specialized to concrete
monad and vector types at the use-site.
Synopsis
- data ArrayList v s a
- new :: (PrimMonad m, MVector v a) => Int -> m (ArrayList v (PrimState m) a)
- fromVector :: (PrimMonad m, Vector v a) => v a -> m (ArrayList (Mutable v) (PrimState m) a)
- vector :: (PrimMonad m, MVector v a) => ArrayList v (PrimState m) a -> m (v (PrimState m) a)
- size :: (PrimMonad m, MVector v a) => ArrayList v (PrimState m) a -> m Int
- unsafeVector :: (PrimMonad m, MVector v a) => ArrayList v (PrimState m) a -> m (v (PrimState m) a)
- push :: (PrimMonad m, MVector v a) => ArrayList v (PrimState m) a -> a -> m ()
- grow :: (PrimMonad m, MVector v a) => ArrayList v (PrimState m) a -> Int -> m ()
- freeze :: (PrimMonad m, Vector v a) => ArrayList (Mutable v) (PrimState m) a -> m (v a)
Creation of ArrayList
s
fromVector :: (PrimMonad m, Vector v a) => v a -> m (ArrayList (Mutable v) (PrimState m) a) Source #
Create a new ArrayList
from immutable vector. Capacity will be equal to the length of vector.
Memory will be copied, so that input vector is safe to use afterwards.
Accessing the underlying vector
vector :: (PrimMonad m, MVector v a) => ArrayList v (PrimState m) a -> m (v (PrimState m) a) Source #
Get underlying mutable vector that stores ArrayList
's data. Length of this vector will be set
to the actually used length of ArrayList
, not its capacity.
size :: (PrimMonad m, MVector v a) => ArrayList v (PrimState m) a -> m Int Source #
Get currently used size of the ArrayList
.
unsafeVector :: (PrimMonad m, MVector v a) => ArrayList v (PrimState m) a -> m (v (PrimState m) a) Source #
Extending the ArrayList
push :: (PrimMonad m, MVector v a) => ArrayList v (PrimState m) a -> a -> m () Source #
Append an element to the end of the ArrayList
.
grow :: (PrimMonad m, MVector v a) => ArrayList v (PrimState m) a -> Int -> m () Source #
Request to add more items to the vector. Used size will be set to current size + extra requested size.
NOTE: Do not use the result of vector
after calling this function, as it may refer to discarded
memory.
This function defines two Cost Centres: "ArrayList.grow.toNewSize
" when vector will grow to fit
precisely the number of required elements, and "ArrayList.grow.toNewCapacity
" when vector will
over-allocate memory.
These may be useful when profiling your program to check how much allocations and moves ArrayList
does actually save.
This function is specialized to IO
and ST
monads, as well to simple MVector
and unboxed
MVector
types.