GHC.Data.FlatBag

ghc-lib-parser-9.14.1.20251220: The GHC API, decoupled from GHC versions

Safe Haskell | Ignore
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 #

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

emptyFlatBag :: FlatBag a #

Create an empty `FlatBag`.

The empty `FlatBag` is shared over all instances.

unitFlatBag :: a -> FlatBag a #

Create a singleton `FlatBag`.

sizeFlatBag :: FlatBag a -> Word #

Calculate the size of

elemsFlatBag :: FlatBag a -> [a] #

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

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

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 #

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 #

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