module RandomCycle.List
  ( uniformPartition,
    uniformPartitionThin,
    uniformCyclePartition,
    uniformCyclePartitionThin,
    partitionLengths,
    partitionFromBits,
  )
where

import Control.Monad.Primitive (PrimMonad)
import qualified Data.Vector as V
import RandomCycle.List.Partition
import qualified RandomCycle.Vector as RV
import System.Random.Stateful (StatefulGen)

-- | Sample a cycle graph partition of '[0.. n-1]',
-- uniformly over the \(n!\) possibilities. The list implementation
-- is a convenenience wrapper around 'RandomCycle.Vector.uniformCyclePartition'.
uniformCyclePartition :: (PrimMonad m, StatefulGen g m) => Int -> g -> m [(Int, Int)]
uniformCyclePartition :: forall (m :: * -> *) g.
(PrimMonad m, StatefulGen g m) =>
Int -> g -> m [(Int, Int)]
uniformCyclePartition Int
n g
g = do
  Vector (Int, Int)
v <- forall g (m :: * -> *).
(StatefulGen g m, PrimMonad m) =>
Int -> g -> m (Vector (Int, Int))
RV.uniformCyclePartition Int
n g
g
  forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ forall a. Vector a -> [a]
V.toList Vector (Int, Int)
v

-- | Sample a cycle graph partition of '[0.. n-1]',
-- uniformly over the set satisfying the conditions.
-- The list implementation is a convenenience wrapper around
-- 'RandomCycle.Vector.uniformCyclePartitionThin'.
uniformCyclePartitionThin ::
  (PrimMonad m, StatefulGen g m) =>
  Int ->
  ((Int, Int) -> Bool) ->
  Int ->
  g ->
  m (Maybe [(Int, Int)])
uniformCyclePartitionThin :: forall (m :: * -> *) g.
(PrimMonad m, StatefulGen g m) =>
Int -> ((Int, Int) -> Bool) -> Int -> g -> m (Maybe [(Int, Int)])
uniformCyclePartitionThin Int
maxit (Int, Int) -> Bool
r Int
n g
g = do
  Maybe (Vector (Int, Int))
v <- forall g (m :: * -> *).
(StatefulGen g m, PrimMonad m) =>
Int
-> ((Int, Int) -> Bool)
-> Int
-> g
-> m (Maybe (Vector (Int, Int)))
RV.uniformCyclePartitionThin Int
maxit (Int, Int) -> Bool
r Int
n g
g
  forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ forall a. Vector a -> [a]
V.toList forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe (Vector (Int, Int))
v