{- |
   Module      : Text.Pandoc.Readers.Odt.Generic.SetMap
   Copyright   : Copyright (C) 2015 Martin Linnemann
   License     : GNU GPL, version 2 or above

   Maintainer  : Martin Linnemann <theCodingMarlin@googlemail.com>
   Stability   : alpha
   Portability : portable

A map of values to sets of values.
-}

module Text.Pandoc.Readers.Odt.Generic.SetMap where

import qualified Data.Map as M
import qualified Data.Set as S

type SetMap k v = M.Map k (S.Set v)

empty :: SetMap k v
empty :: SetMap k v
empty = SetMap k v
forall k a. Map k a
M.empty

fromList :: (Ord k, Ord v) => [(k,v)] -> SetMap k v
fromList :: [(k, v)] -> SetMap k v
fromList = ((k, v) -> SetMap k v -> SetMap k v)
-> SetMap k v -> [(k, v)] -> SetMap k v
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr ((k -> v -> SetMap k v -> SetMap k v)
-> (k, v) -> SetMap k v -> SetMap k v
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry k -> v -> SetMap k v -> SetMap k v
forall k v. (Ord k, Ord v) => k -> v -> SetMap k v -> SetMap k v
insert) SetMap k v
forall k v. SetMap k v
empty

insert :: (Ord k, Ord v) => k -> v -> SetMap k v -> SetMap k v
insert :: k -> v -> SetMap k v -> SetMap k v
insert k
key v
value SetMap k v
setMap = (Set v -> Set v -> Set v) -> k -> Set v -> SetMap k v -> SetMap k v
forall k a. Ord k => (a -> a -> a) -> k -> a -> Map k a -> Map k a
M.insertWith Set v -> Set v -> Set v
forall a. Ord a => Set a -> Set a -> Set a
S.union k
key (v -> Set v
forall a. a -> Set a
S.singleton v
value) SetMap k v
setMap

union3 :: (Ord k) => SetMap k v -> SetMap k v -> SetMap k v -> SetMap k v
union3 :: SetMap k v -> SetMap k v -> SetMap k v -> SetMap k v
union3 SetMap k v
sm1 SetMap k v
sm2 SetMap k v
sm3 = SetMap k v
sm1 SetMap k v -> SetMap k v -> SetMap k v
forall k a. Ord k => Map k a -> Map k a -> Map k a
`M.union` SetMap k v
sm2 SetMap k v -> SetMap k v -> SetMap k v
forall k a. Ord k => Map k a -> Map k a -> Map k a
`M.union` SetMap k v
sm3