mirror of
https://codeberg.org/JasterV/word-ladder.hs.git
synced 2026-04-27 02:15:44 +00:00
build permutation map
This commit is contained in:
parent
87db09ec1f
commit
b56388a670
3 changed files with 53 additions and 0 deletions
|
|
@ -28,6 +28,7 @@ library
|
||||||
Data.AssocMap
|
Data.AssocMap
|
||||||
Graph
|
Graph
|
||||||
Lib
|
Lib
|
||||||
|
PermutationMap
|
||||||
other-modules:
|
other-modules:
|
||||||
Paths_ladder
|
Paths_ladder
|
||||||
autogen-modules:
|
autogen-modules:
|
||||||
|
|
|
||||||
|
|
@ -15,6 +15,7 @@ import Data.Maybe (fromMaybe, isJust)
|
||||||
import Prelude hiding (lookup)
|
import Prelude hiding (lookup)
|
||||||
|
|
||||||
newtype AssocMap k v = AssocMap [(k, v)]
|
newtype AssocMap k v = AssocMap [(k, v)]
|
||||||
|
deriving (Show)
|
||||||
|
|
||||||
empty :: AssocMap k v
|
empty :: AssocMap k v
|
||||||
empty = AssocMap []
|
empty = AssocMap []
|
||||||
|
|
|
||||||
51
src/PermutationMap.hs
Normal file
51
src/PermutationMap.hs
Normal 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)
|
||||||
Loading…
Add table
Add a link
Reference in a new issue