GHC.Data.FlatBag

ghc-9.14.0.20251028: The GHC API

Safe Haskell | None
Language | GHC2021

GHC.Data.FlatBag

Contents

- Construction

Synopsis

- data FlatBag a
  - = EmptyFlatBag
  - | UnitFlatBag !a
  - | TupleFlatBag !a !a
- emptyFlatBag :: FlatBag a
- unitFlatBag :: a -> FlatBag a
- sizeFlatBag :: FlatBag a -> Word
- elemsFlatBag :: FlatBag a -> [a]
- mappendFlatBag :: FlatBag a -> FlatBag a -> FlatBag a
- fromList :: Word -> [a] -> FlatBag a
- fromSmallArray :: SmallArray a -> FlatBag a

# Documentation

data FlatBag a Source #

Store elements in a flattened representation.

A `FlatBag` is a data structure that stores an ordered list of elements in a flat structure, avoiding the overhead of a linked list. Use this data structure, if the code requires the following properties:

- Elements are stored in a long-lived object, and benefit from a flattened representation.
- The `FlatBag` will be traversed but not extended or filtered.
- The number of elements should be known.
- Sharing of the empty case improves memory behaviour.

A `FlagBag` aims to have as little overhead as possible to store its elements. To achieve that, it distinguishes between the empty case, singleton, tuple and general case. Thus, we only pay for the additional three words of an `Array` if we have at least three elements.

Constructors

EmptyFlatBag |
UnitFlatBag !a |
TupleFlatBag !a !a |

#### Instances

Instances details

Functor FlatBag Source # |
Instance details Defined in GHC.Data.FlatBag Methods fmap :: (a -> b) -> FlatBag a -> FlatBag b Source # (<$) :: a -> FlatBag b -> FlatBag a Source #
Foldable FlatBag Source # |
Instance details Defined in GHC.Data.FlatBag Methods fold :: Monoid m => FlatBag m -> m Source # foldMap :: Monoid m => (a -> m) -> FlatBag a -> m Source # foldMap' :: Monoid m => (a -> m) -> FlatBag a -> m Source # foldr :: (a -> b -> b) -> b -> FlatBag a -> b Source # foldr' :: (a -> b -> b) -> b -> FlatBag a -> b Source # foldl :: (b -> a -> b) -> b -> FlatBag a -> b Source # foldl' :: (b -> a -> b) -> b -> FlatBag a -> b Source # foldr1 :: (a -> a -> a) -> FlatBag a -> a Source # foldl1 :: (a -> a -> a) -> FlatBag a -> a Source # toList :: FlatBag a -> [a] Source # null :: FlatBag a -> Bool Source # length :: FlatBag a -> Int Source # elem :: Eq a => a -> FlatBag a -> Bool Source # maximum :: Ord a => FlatBag a -> a Source # minimum :: Ord a => FlatBag a -> a Source # sum :: Num a => FlatBag a -> a Source # product :: Num a => FlatBag a -> a Source #
Traversable FlatBag Source # |
Instance details Defined in GHC.Data.FlatBag Methods traverse :: Applicative f => (a -> f b) -> FlatBag a -> f (FlatBag b) Source # sequenceA :: Applicative f => FlatBag (f a) -> f (FlatBag a) Source # mapM :: Monad m => (a -> m b) -> FlatBag a -> m (FlatBag b) Source # sequence :: Monad m => FlatBag (m a) -> m (FlatBag a) Source #
NFData a => NFData (FlatBag a) Source # |
Instance details Defined in GHC.Data.FlatBag Methods rnf :: FlatBag a -> () Source #

emptyFlatBag :: FlatBag a Source #

Create an empty `FlatBag`.

The empty `FlatBag` is shared over all instances.

unitFlatBag :: a -> FlatBag a Source #

Create a singleton `FlatBag`.

sizeFlatBag :: FlatBag a -> Word Source #

Calculate the size of

elemsFlatBag :: FlatBag a -> [a] Source #

Get all elements that are stored in the `FlatBag`.

mappendFlatBag :: FlatBag a -> FlatBag a -> FlatBag a Source #

Combine two `FlatBag`s.

The new `FlatBag` contains all elements from both `FlatBag`s.

If one of the `FlatBag`s is empty, the old `FlatBag` is reused.

# Construction

fromList :: Word -> [a] -> FlatBag a Source #

Store the list in a flattened memory representation, avoiding the memory overhead of a linked list.

The size `n` needs to be smaller or equal to the length of the list. If it is smaller than the length of the list, overflowing elements are discarded. It is undefined behaviour to set `n` to be bigger than the length of the list.

fromSmallArray :: SmallArray a -> FlatBag a Source #

Convert a `SizedSeq` into its flattened representation. A 'FlatBag a' is more memory efficient than '[a]', if no further modification is necessary.