impure-containers-0.5.1: Mutable containers in Haskell.

Safe HaskellNone
LanguageHaskell2010

Data.ArrayList.Generic

Contents

Description

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

Creation of ArrayLists

data ArrayList v s a Source #

new :: (PrimMonad m, MVector v a) => Int -> m (ArrayList v (PrimState m) a) Source #

Create a new ArrayList with requested capacity. The length of ArrayList as reported by size will be 0.

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 #

Like vector, but do not set the length of resulting vector. Its length will be equal to the capacity of ArrayList. This means that some elements of the vector will be uninitialized.

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.

Deconstruction of ArrayLists

freeze :: (PrimMonad m, Vector v a) => ArrayList (Mutable v) (PrimState m) a -> m (v a) Source #

Make a copy of currently used part of ArrayList and return it as an immutable vector.