ghc-9.12: The GHC API
Safe HaskellNone
LanguageGHC2021

GHC.Data.FlatBag

Contents

Synopsis

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 #

(<$) :: a -> FlatBag b -> FlatBag a #

Foldable FlatBag Source # 
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 Source # 
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) 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 FlatBags.

The new FlatBag contains all elements from both FlatBags.

If one of the FlatBags 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.

fromSizedSeq :: SizedSeq 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.