{-# LANGUAGE TemplateHaskell #-}---------------------------------------------------------------------------------- |-- Module : Data.Permutation-- Copyright : (C) Frank Staals-- License : see the LICENSE file-- Maintainer : Frank Staals---- Data type for representing a Permutation----------------------------------------------------------------------------------module Data.Permutation whereimport Control.DeepSeqimport Control.Lensimport Control.Monad (forM)import Control.Monad.ST (runST)import qualified Data.Foldable as Fimport Data.Maybe (catMaybes)import qualified Data.Traversable as Timport qualified Data.Vector as Vimport qualified Data.Vector.Generic as GVimport qualified Data.Vector.Unboxed as UVimport qualified Data.Vector.Unboxed.Mutable as UMVimport GHC.Generics (Generic)---------------------------------------------------------------------------------- | Orbits (Cycles) are represented by vectorstype Orbit a = V.Vector a-- | Cyclic representation of a permutation.data Permutation a = Permutation { _orbits :: V.Vector (Orbit a) , _indexes :: UV.Vector (Int,Int) -- ^ idxes (fromEnum a) = (i,j) -- implies that a is the j^th -- item in the i^th orbit } deriving (Show,Eq,Generic)makeLenses ''Permutationinstance NFData a => NFData (Permutation a)instance Functor Permutation where fmap = T.fmapDefaultinstance F.Foldable Permutation where foldMap = T.foldMapDefaultinstance T.Traversable Permutation where traverse f (Permutation os is) = flip Permutation is <$> T.traverse (T.traverse f) oselems :: Permutation a -> V.Vector aelems = GV.concat . GV.toList . _orbitssize :: Permutation a -> Intsize perm = GV.length (perm^.indexes)-- | The cycle containing a given itemcycleOf :: Enum a => Permutation a -> a -> Orbit acycleOf perm x = perm^?!orbits.ix (perm^?!indexes.ix (fromEnum x)._1)-- | Next item in a cyclic permutationnext :: GV.Vector v a => v a -> Int -> anext v i = let n = GV.length v in v GV.! ((i+1) `mod` n)-- | Previous item in a cyclic permutationprevious :: GV.Vector v a => v a -> Int -> aprevious v i = let n = GV.length v in v GV.! ((i-1) `mod` n)-- | Lookup the indices of an element, i.e. in which orbit the item is, and the-- index within the orbit.---- runnign time: \(O(1)\)lookupIdx :: Enum a => Permutation a -> a -> (Int,Int)lookupIdx perm x = perm^?!indexes.ix (fromEnum x)-- | Apply the permutation, i.e. consider the permutation as a function.apply :: Enum a => Permutation a -> a -> aapply perm x = let (c,i) = lookupIdx perm x in next (perm^?!orbits.ix c) i-- | Find the cycle in the permutation starting at element sorbitFrom :: Eq a => a -> (a -> a) -> [a]orbitFrom s p = s : (takeWhile (/= s) . tail $ iterate p s)-- | Given a vector with items in the permutation, and a permutation (by its-- functional representation) construct the cyclic representation of the-- permutation.cycleRep :: (GV.Vector v a, Enum a, Eq a) => v a -> (a -> a) -> Permutation acycleRep v perm = toCycleRep n $ runST $ do bv <- UMV.replicate n False -- bit vector of marks morbs <- forM [0..(n - 1)] $ \i -> do m <- UMV.read bv (fromEnum $ v GV.! i) if m then pure Nothing -- already visited else do let xs = orbitFrom (v GV.! i) perm markAll bv $ map fromEnum xs pure . Just $ xs pure . catMaybes $ morbs where n = GV.length v mark bv i = UMV.write bv i True markAll bv = mapM_ (mark bv)-- | Given the size n, and a list of Cycles, turns the cycles into a-- cyclic representation of the Permutation.toCycleRep :: Enum a => Int -> [[a]] -> Permutation atoCycleRep n os = Permutation (V.fromList . map V.fromList $ os) (genIndexes n os)genIndexes :: Enum a => Int -> [[a]] -> UV.Vector (Int,Int)genIndexes n os = UV.create $ do v <- UMV.new n mapM_ (uncurry $ UMV.write v) ixes' pure v where f i c = zipWith (\x j -> (fromEnum x,(i,j))) c [0..] ixes' = concat $ zipWith f [0..] os