kudzu-forest/elm-random-tree/1.0.0

NOTE: the latest version is 3.0.1

RandomTree

This module provides data structure that allows random picking up from a collection of data in O(log(N)) time. You can set weight of each element with WideFloat from kudzu-forest/elm-wide-float package.

Types

type RandomTree a

Type that represents binary tree with weighted elements for random picking up.

type alias Weighted a =
{ weight : WideFloat
, content : a
}

Type alias that represents each weighted contents in RandomTree.

type alias WideFloat =
WideFloat

Type that represent floating point number with wider range than Float in core package. This is introduced for preventing overflow of exponentially changing value. If you need to directly manipulate values of WideFloat, please hit elm install kudzu-forest/elm-wide-float in your terminal.

Operation around weighted datum

adjustWeight : Float -> Weighted a -> Weighted a

Returns weighted datum whose weight is multiplied with the given Float.

adjustContent : (a -> b) -> Weighted a -> Weighted b

Maps content in Element.

Creation

singleton : Weighted a -> RandomTree a

Returns a RandomTree that has only one element.

fromList : Weighted a -> List (Weighted a) -> RandomTree a

Returns a RandomTree that has all element in given list.

fromPairs : ( Float, a ) -> List ( Float, a ) -> RandomTree a

Creates RandomTree from list of tuples of a Float value corresponding to its weight as the first component, and a datum as the second component. the weighter it is, the higher probability it is chosen in.

Insertion

insert : Weighted a -> RandomTree a -> RandomTree a

Inserts a Weighted data to an already-existing RandomTree.

Inserts an element into the tree. The first parameter denotes the relative weight(1 means the whole weight of the original tree).

insertListWithRelativeWeight : List ( Float, a ) -> RandomTree a -> RandomTree a

Inserts all elements of given list into the tree of second parameter. The first components of the tuple denotes the relative weight(1 means the whole weight of the original tree).

Modification

delete : a -> RandomTree a -> Maybe (RandomTree a)

Removes any number of elements which is the same as the first parameter from the second parameter. The returned value is wrapped in Maybe.

map : (a -> b) -> RandomTree a -> RandomTree b

Maps all the content of the tree, with the weights unchanged.

filter : (WideFloat -> a -> Bool) -> RandomTree a -> Maybe (RandomTree a)

Removes any number of elements which passes the test function given as the first parameter from the second parameter. The returned value is wrapped in Maybe.

Random Pick Up

get : RandomTree a -> Generator a

Random generator that generates one of the elements conteined in RandomTree given as parameter. The time complexity is O(log(N)) where N denotes the size of the tree.

take : RandomTree a -> Generator ( Weighted a, Maybe (RandomTree a) )

Random generator that generates one of the elements conteined in RandomTree, paired with the rest part of the tree. If the RandomTree has only one element, then Nothing is returned as the second component. The time complexity is O(log(N)) where N denotes the size of the tree.

Weighted a
-> Generator ( Weighted a, RandomTree a )

Random generator that replaces one weighted data from the tree and generates a pair consisting of the removed data and resultant tree.

Query

count : RandomTree a -> Int

Returns how many elements the tree has.

totalWeight : RandomTree a -> WideFloat

Returns the sum of weight of all the elements in the tree.