{- |
   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 :: forall k v. SetMap k v
empty = Map k (Set v)
forall k a. Map k a
M.empty

fromList :: (Ord k, Ord v) => [(k,v)] -> SetMap k v
fromList :: forall k v. (Ord k, Ord v) => [(k, v)] -> SetMap k v
fromList = ((k, v) -> SetMap k v -> SetMap k v)
-> SetMap k v -> [(k, v)] -> SetMap k v
forall a b. (a -> b -> b) -> b -> [a] -> b
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 :: forall k v. (Ord k, Ord v) => 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 :: forall k v.
Ord k =>
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