blob: ec0ae734b9ed21b2b0cca9f6544294a7ce2bea0e (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
module Much.MappedSets (invert, mk) where
import Control.Arrow
import Data.Map.Strict (Map)
import qualified Data.Map.Strict as Map
import Data.Maybe
import Data.Set (Set)
import qualified Data.Set as Set
mk :: (Ord a, Ord b) => [(a, [b])] -> Map a (Set b)
mk =
Map.fromList . map (second Set.fromList)
invert :: (Ord a, Ord b) => Map a (Set b) -> Map b (Set a)
invert =
Map.foldrWithKey invert1 Map.empty
invert1 :: (Ord a, Ord b) => a -> Set b -> Map b (Set a) -> Map b (Set a)
invert1 k v a =
Set.foldr (upsert k) a v
upsert :: (Ord a, Ord b) => a -> b -> Map b (Set a) -> Map b (Set a)
upsert k =
Map.alter (Just . Set.insert k . fromMaybe Set.empty)
|