Copyright | Copyright (c) , Patrick Perry <patperry@stanford.edu> |
---|---|
License | BSD3 |
Maintainer | Patrick Perry <patperry@stanford.edu> |
Stability | experimental |
Safe Haskell | None |
Language | Haskell98 |
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 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.
getPossible :: c -> m Int Source
Get the number of possibilities, n
in the combination.
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
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 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 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.
Accessing combination elements
getElem :: MChoose c m => c -> Int -> m Int Source
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 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.