permutation-0.1: Permutations and combinations

Stability experimental Patrick Perry

Data.Permutation

Description

Synopsis

# The Permutation type

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

Instances

 Eq Permutation Show Permutation

# 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.

Create an identity permutation of the given size.

Get the inverse of a permutation.

# Permutation properties

Get the size of the permutation.

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

## `ForeignPtr`s

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

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.

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