Stability | experimental |
---|---|

Maintainer | Patrick Perry <patperry@stanford.edu> |

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

- class Monad m => MChoose c m | c -> m, m -> c where
- getPossible :: c -> m Int
- getSize :: c -> m Int
- newChoose :: Int -> Int -> m c
- newChoose_ :: Int -> Int -> m c
- unsafeGetElem :: c -> Int -> m Int
- unsafeSetElem :: c -> Int -> Int -> m ()
- getElems :: c -> m [Int]
- setElems :: c -> [Int] -> m ()
- unsafeFreeze :: c -> m Choose
- unsafeThaw :: Choose -> m c

- newListChoose :: MChoose c m => Int -> Int -> [Int] -> m c
- newCopyChoose :: MChoose c m => c -> m c
- copyChoose :: MChoose c m => c -> c -> m ()
- setFirst :: MChoose c m => c -> m ()
- getElem :: MChoose c m => c -> Int -> m Int
- setElem :: MChoose c m => c -> Int -> Int -> m ()
- isValid :: MChoose c m => c -> m Bool
- getComplement :: MChoose c m => c -> m c
- getComplElems :: MChoose c m => c -> m [Int]
- setNext :: MChoose c m => c -> m Bool
- setPrev :: MChoose c m => c -> m Bool
- freeze :: MChoose c m => c -> m Choose
- thaw :: MChoose c m => Choose -> m c
- unsafeNewListChoose :: MChoose c m => Int -> Int -> [Int] -> m c

# Class of mutable combination types

class Monad m => MChoose c m | c -> m, m -> c whereSource

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.

getPossible :: c -> m IntSource

Get the number of possibilities, `n`

in the combination.

Get the number of outcomes, `k`

in the combination.

newChoose :: Int -> Int -> m cSource

`newChoose n k`

creates a new combination of `k`

outcomes from `n`

possibilites initialized to the subset `{ 0, ..., k-1 }`

.

newChoose_ :: Int -> Int -> m cSource

`newChoose n k`

allocates a new combination of `k`

outcomes from
`n`

possibilities but does not initialize it.

unsafeGetElem :: c -> Int -> m IntSource

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 ChooseSource

unsafeThaw :: Choose -> m cSource

# Constructing mutable combinations

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

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 `i`

th 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 cSource

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.

# Accessing combination elements

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

`getElem c i`

gets the value of the `i`

th 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 `i`

th 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 BoolSource

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 cSource

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 BoolSource

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 BoolSource

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.