haskell-list-builder-0.1.0.0: Fast Sequencing and Building Lists using Unsafe Primitives
Safe HaskellNone
LanguageHaskell2010

Data.ListBuilder

Description

Mutable List Builder.

A ListBuilder s a is like a wrapper over ST s [a], but uses unsafe mutation to achieve constant time append as well as prepend.

As the builder is backed with a standard List, it is light-weight and cheap to return to a list.

Code from this module is derived from Scala's ListBuffer module, using the unsafe set field technique described by Twan van Laarhoven.

Synopsis

Mutable list builder

data ListBuilder s a Source #

A List Builder.

This builder is backed by a standard haskell List. It offers predictable (and fast) operations, and doesn't pause for grow operations as an array based builder might.

Construction

newBuilder :: ST s (ListBuilder s a) Source #

Create a new, empty ListBuilder

Mutations

append :: a -> ListBuilder s a -> ST s () Source #

Append an item to the back of the ListBuilder

O(1)

prepend :: a -> ListBuilder s a -> ST s () Source #

Prepend an item to the front of the ListBuilder

O(1)

insert :: Int -> a -> ListBuilder s a -> ST s () Source #

Insert into a location in a ListBuilder.

This function doesn't create a new spine across the list builder, and only allocates the new cons cell itself.

O(N)

filterInPlace :: (a -> Bool) -> ListBuilder s a -> ST s () Source #

Filter the ListBuilder with the supplied predicate

O(N)

clear :: ListBuilder s a -> ST s () Source #

Empty the ListBuilder of all values.

O(1)

Accessors

readLength :: ListBuilder s a -> ST s Int Source #

The current length of the ListBuilder.

O(1)

readFirst :: ListBuilder s a -> ST s (Maybe a) Source #

Return the current first element in the ListBuilder

O(1)

readLast :: ListBuilder s a -> ST s (Maybe a) Source #

Return the current last element in the ListBuilder

O(1)

readAt :: ListBuilder s a -> Int -> ST s (Maybe a) Source #

Return the current element at a particular index for the ListBuilder

O(N)

Conversions

freeze :: ListBuilder s a -> ST s [a] Source #

Freeze the result and return the list.

This function strictly copies the spine of the list.

O(n)

unsafeFreeze :: ListBuilder s a -> ST s [a] Source #

Return the List backing the ListBuilder.

This does not stop mutations made to the builder from affecting the resultant list. So one must not continue to call the mutating functions.

This function is safe in tail position within a call to runST.

O(1)