-toHeaders ∷ [(CIAscii, Ascii)] → Headers
-{-# INLINE toHeaders #-}
-toHeaders = flip mkHeaders (∅)
-
-mkHeaders ∷ [(CIAscii, Ascii)] → Headers → Headers
-mkHeaders [] (Headers m) = Headers m
-mkHeaders ((key, val):xs) (Headers m)
- = mkHeaders xs $ Headers $
- case M.lookup key m of
- Nothing → M.insert key val m
- Just old → M.insert key (merge old val) m
+-- |@'insert' (key, val)@ merges @val@ with an existing one if any.
+instance Unfoldable Headers (CIAscii, Ascii) where
+ {-# INLINE insert #-}
+ insert (key, val) (Headers m)
+ = Headers $ insertWith merge key val m
+ {-# INLINE empty #-}
+ empty
+ = Headers empty
+ {-# INLINE singleton #-}
+ singleton v
+ = Headers $ singleton v
+ {-# INLINE insertMany #-}
+ insertMany f (Headers m)
+ = Headers $ insertMany f m
+ {-# INLINE insertManySorted #-}
+ insertManySorted f (Headers m)
+ = Headers $ insertManySorted f m
+
+instance Foldable Headers (CIAscii, Ascii) where
+ {-# INLINE foldr #-}
+ foldr f b (Headers m) = foldr f b m
+
+instance Collection Headers (CIAscii, Ascii) where
+ {-# INLINE filter #-}
+ filter f (Headers m) = Headers $ filter f m
+
+instance Indexed Headers CIAscii Ascii where
+ {-# INLINE index #-}
+ index k (Headers m) = index k m
+ {-# INLINE adjust #-}
+ adjust f k (Headers m) = Headers $ adjust f k m
+ {-# INLINE inDomain #-}
+ inDomain k (Headers m) = inDomain k m
+
+instance Monoid Headers where
+ {-# INLINE mempty #-}
+ mempty = empty
+ {-# INLINE mappend #-}
+ mappend (Headers α) (Headers β)
+ = Headers $ insertManySorted β α
+
+instance Map Headers CIAscii Ascii where
+ {-# INLINE lookup #-}
+ lookup k (Headers m) = lookup k m
+ {-# INLINE insertWith #-}
+ insertWith f k v (Headers m)
+ = Headers $ insertWith f k v m
+ {-# INLINE mapWithKey #-}
+ mapWithKey f (Headers m)
+ = Headers $ mapWithKey f m
+ {-# INLINE unionWith #-}
+ unionWith f (Headers α) (Headers β)
+ = Headers $ unionWith f α β
+ {-# INLINE intersectionWith #-}
+ intersectionWith f (Headers α) (Headers β)
+ = Headers $ intersectionWith f α β
+ {-# INLINE differenceWith #-}
+ differenceWith f (Headers α) (Headers β)
+ = Headers $ differenceWith f α β
+ {-# INLINE isSubmapBy #-}
+ isSubmapBy f (Headers α) (Headers β)
+ = isSubmapBy f α β
+ {-# INLINE isProperSubmapBy #-}
+ isProperSubmapBy f (Headers α) (Headers β)
+ = isProperSubmapBy f α β
+
+instance SortingCollection Headers (CIAscii, Ascii) where
+ {-# INLINE minView #-}
+ minView (Headers m) = second Headers <$> minView m
+
+merge ∷ Ascii → Ascii → Ascii
+{-# INLINE merge #-}
+merge a b
+ | nullA a ∧ nullA b = (∅)
+ | nullA a = b
+ | nullA b = a
+ | otherwise = a ⊕ ", " ⊕ b