permutation-0.5.0: A library for permutations and combinations.

CopyrightCopyright (c) , Patrick Perry <patperry@stanford.edu>
LicenseBSD3
MaintainerPatrick Perry <patperry@stanford.edu>
Stabilityexperimental
Safe HaskellNone
LanguageHaskell98

Data.Choose.MChoose

Contents

Description

An overloaded interface to mutable combinations. For combination types which can be used with this interface, see Data.Choose.IO and Data.Choose.ST.

Synopsis

Class of mutable combination types

class Monad m => MChoose c m | c -> m, m -> c where Source

Class for representing a mutable combination. The type is parameterized over the type of the monad, m, in which the mutable combination will be manipulated.

Methods

getPossible :: c -> m Int Source

Get the number of possibilities, n in the combination.

getSize :: c -> m Int Source

Get the number of outcomes, k in the combination.

newChoose :: Int -> Int -> m c Source

newChoose n k creates a new combination of k outcomes from n possibilites initialized to the subset { 0, ..., k-1 }.

newChoose_ :: Int -> Int -> m c Source

newChoose n k allocates a new combination of k outcomes from n possibilities but does not initialize it.

unsafeGetElem :: c -> Int -> m Int Source

unsafeSetElem :: c -> Int -> Int -> m () Source

getElems :: c -> m [Int] Source

Get a lazy list of the combination elements. The laziness makes this function slightly dangerous if you are modifying the combination.

setElems :: c -> [Int] -> m () Source

Set all the values of a combination from a list of elements.

unsafeFreeze :: c -> m Choose Source

unsafeThaw :: Choose -> m c Source

Instances

Constructing mutable combinations

newListChoose :: MChoose c m => Int -> Int -> [Int] -> m c Source

Construct a combination from a list of elements. newListChoose n k is creates a combination of k outcomes from n possibilities initialized to have the ith element equal to is !! i. For the combination to be valid, the elements must all be unique, they must be in sorted order, and they all must be in the range 0 .. n-1.

newCopyChoose :: MChoose c m => c -> m c Source

Construct a new combination by copying another.

copyChoose :: MChoose c m => c -> c -> m () Source

copyChoose dst src copies the elements of the combination src into the combination dst. The two combinations must have the same size.

setFirst :: MChoose c m => c -> m () Source

Set a combination to be the first subset of its size.

Accessing combination elements

getElem :: MChoose c m => c -> Int -> m Int Source

getElem c i gets the value of the ith element of the combination c. The index i must be in the range 0..k-1, where n is the size of the combination.

setElem :: MChoose c m => c -> Int -> Int -> m () Source

setElem c i x sets the value of the ith element of the combination c. The index i must be in the range 0..k-1, where k is the size of the combination.

Combination properties

isValid :: MChoose c m => c -> m Bool Source

Returns whether or not the combination is valid. For it to be valid, the elements must all be unique, they must be in sorted order, and they all must be in the range 0 .. n-1, where n is the number of possibilies in the combination.

Combination functions

getComplement :: MChoose c m => c -> m c Source

Compute the complement of a combination

getComplElems :: MChoose c m => c -> m [Int] Source

Return a lazy list of the elements in the complement of a combination. If the combination is a subset of k outcomes from n possibilities, then the returned list will be sorted and of length n-k. Due to the laziness, you should be careful when using this function if you are also modifying the combination.

setNext :: MChoose c m => c -> m Bool Source

Advance a combination to the next in lexicogrphic order and return True. If no further combinations are available, return False and leave the combination unmodified. Starting with [ 0 .. k-1 ] and repeatedly calling setNext will iterate through all subsets of size k.

setPrev :: MChoose c m => c -> m Bool Source

Step backwards to the previous combination in lexicographic order and return True. If there is no previous combination, return False and leave the combination unmodified.

Converstions between mutable and immutable combinations

freeze :: MChoose c m => c -> m Choose Source

Convert a mutable combination to an immutable one.

thaw :: MChoose c m => Choose -> m c Source

Convert an immutable combination to a mutable one.

Unsafe operations

unsafeNewListChoose :: MChoose c m => Int -> Int -> [Int] -> m c Source