build permutation map

This commit is contained in:
Victor Martinez 2025-03-18 12:05:24 +01:00
commit b56388a670
3 changed files with 53 additions and 0 deletions

View file

@ -28,6 +28,7 @@ library
Data.AssocMap
Graph
Lib
PermutationMap
other-modules:
Paths_ladder
autogen-modules:

View file

@ -15,6 +15,7 @@ import Data.Maybe (fromMaybe, isJust)
import Prelude hiding (lookup)
newtype AssocMap k v = AssocMap [(k, v)]
deriving (Show)
empty :: AssocMap k v
empty = AssocMap []

51
src/PermutationMap.hs Normal file
View file

@ -0,0 +1,51 @@
module PermutationMap
( PermutationMap,
empty,
member,
alter,
delete,
insert,
lookup,
findWithDefault,
createPermutationMap,
)
where
import qualified Data.AssocMap as M
import Data.List (nub, sort)
import Data.Maybe (fromMaybe)
import Prelude hiding (lookup)
type PermutationMap = M.AssocMap String [String]
empty :: PermutationMap
empty = M.empty
member :: String -> PermutationMap -> Bool
member key = M.member (sort key)
alter :: (Maybe [String] -> Maybe [String]) -> String -> PermutationMap -> PermutationMap
alter f key = M.alter f (sort key)
delete :: String -> PermutationMap -> PermutationMap
delete key = M.delete (sort key)
insert :: String -> [String] -> PermutationMap -> PermutationMap
insert key = M.insert (sort key)
lookup :: String -> PermutationMap -> Maybe [String]
lookup key = M.lookup (sort key)
findWithDefault :: [String] -> String -> PermutationMap -> [String]
findWithDefault defaultValue key pmap = fromMaybe defaultValue (PermutationMap.lookup key pmap)
createPermutationMap :: [String] -> PermutationMap
createPermutationMap = aux empty
where
aux permMap [] = permMap
aux permMap (x : xs) = aux (insertPermutation x permMap) xs
insertPermutation word = alter (insertList word) word
insertList word Nothing = Just [word]
insertList word (Just xs) = Just $ nub (word : xs)