permutation-0.1: Permutations and combinations

MaintainerPatrick Perry <>






The Permutation type

data Permutation Source

Represents a permutation of the integers [0..n).

Creating permutations

permutation :: Int -> [Int] -> PermutationSource

Create a permutation from a list of values. The list must be of length n and contain each integer in {0, 1, ..., (n-1) } exactly once. The permutation that is returned will send the integer i to its index in the list.

identity :: Int -> PermutationSource

Create an identity permutation of the given size.

inverse :: Permutation -> PermutationSource

Get the inverse of a permutation.

Permutation properties

size :: Permutation -> IntSource

Get the size of the permutation.

apply :: Permutation -> Int -> IntSource

Apply a permutation to an integer. The integer must be in the range [0..n).

Applying permutations

applyWith :: Monad m => (Int -> Int -> m ()) -> Permutation -> m ()Source

applyWith swap perm applies the permutation as a sequence of swaps. After this function is applied, OUT[i] = IN[P[i]]

invertWith :: Monad m => (Int -> Int -> m ()) -> Permutation -> m ()Source

invertWith swap p applies the inverse of the permutation as a sequence of swaps. After this function is applied, OUT[P[i]] = IN[i]

Converstion to/from other types


fromForeignPtr :: Int -> ForeignPtr Int -> Int -> PermutationSource

Convert size and raw array to a permutation. No validation is performed on the arguments.

toForeignPtr :: Permutation -> (Int, ForeignPtr Int, Int)Source

Get the size, raw data array and offset


Unsafe operations

withPermutationPtr :: Permutation -> (Ptr Int -> IO a) -> IO aSource

Perform an operation, given a pointer to the start of the permutation data

unsafePermutation :: Int -> [Int] -> PermutationSource

Same as permutation, but does not check that the inputs are valid.

unsafeApply :: Permutation -> Int -> IntSource

Same as apply but does not range-check the argument.