<commit_before>module Data.Multimap
  ( Multimap
  , empty
  , null
  , insert
  , deleteAll
  , delete
  , lookup
  , fromList
  , elems
  ) where

import           Prelude    hiding (lookup, null)

import qualified Data.Map   as Map
import qualified Data.Maybe as Maybe
import qualified Data.Set   as Set


newtype Multimap k v = Multimap { unMultimap :: Map.Map k (Set.Set v) }
  deriving (Eq, Ord, Show)

empty :: Multimap k v
empty = Multimap Map.empty

null :: Multimap k v -> Bool
null (Multimap m) = Map.null m

inserter :: Ord v => v -> Maybe (Set.Set v) -> Maybe (Set.Set v)
inserter v Nothing  = Just (Set.singleton v)
inserter v (Just s) = Just (Set.insert v s)

insert :: (Ord k, Ord v) => k -> v -> Multimap k v -> Multimap k v
insert k v (Multimap m) = Multimap $ Map.alter (inserter v) k m

deleteAll :: Ord k => k -> Multimap k v -> Multimap k v
deleteAll k (Multimap m) = Multimap $ Map.delete k m

deleter :: Ord v => v -> Maybe (Set.Set v) -> Maybe (Set.Set v)
deleter _ Nothing  = Nothing
deleter v (Just s) =
  let
    updated = Set.delete v s
  in
    if Set.null updated
    then Nothing
    else Just updated

delete :: (Ord k, Ord v) => k -> v -> Multimap k v -> Multimap k v
delete k v (Multimap m) = Multimap $ Map.alter (deleter v) k m

lookup :: Ord k => k -> Multimap k v -> Set.Set v
lookup k (Multimap m) = Maybe.fromMaybe Set.empty (Map.lookup k m)

fromList :: (Ord k, Ord v) => [(k, v)] -> Multimap k v
fromList = foldr f empty
  where
    f (k, v) = insert k v

elems :: Multimap k v -> [v]
elems (Multimap m) = Map.elems m >>= Set.elems<commit_msg>Data.Multimap: tweak imports<commit_after>module Data.Multimap
  ( Multimap
  , empty
  , null
  , insert
  , deleteAll
  , delete
  , lookup
  , fromList
  , elems
  ) where

import           Prelude    hiding (lookup, null)

import           Data.Map   (Map)
import qualified Data.Map   as Map
import qualified Data.Maybe as Maybe
import           Data.Set   (Set)
import qualified Data.Set   as Set


newtype Multimap k v = Multimap { unMultimap :: Map k (Set v) }
  deriving (Eq, Ord, Show)

empty :: Multimap k v
empty = Multimap Map.empty

null :: Multimap k v -> Bool
null (Multimap m) = Map.null m

inserter :: Ord v => v -> Maybe (Set v) -> Maybe (Set v)
inserter v Nothing  = Just (Set.singleton v)
inserter v (Just s) = Just (Set.insert v s)

insert :: (Ord k, Ord v) => k -> v -> Multimap k v -> Multimap k v
insert k v (Multimap m) = Multimap $ Map.alter (inserter v) k m

deleteAll :: Ord k => k -> Multimap k v -> Multimap k v
deleteAll k (Multimap m) = Multimap $ Map.delete k m

deleter :: Ord v => v -> Maybe (Set v) -> Maybe (Set v)
deleter _ Nothing  = Nothing
deleter v (Just s) =
  let
    updated = Set.delete v s
  in
    if Set.null updated
    then Nothing
    else Just updated

delete :: (Ord k, Ord v) => k -> v -> Multimap k v -> Multimap k v
delete k v (Multimap m) = Multimap $ Map.alter (deleter v) k m

lookup :: Ord k => k -> Multimap k v -> Set v
lookup k (Multimap m) = Maybe.fromMaybe Set.empty (Map.lookup k m)

fromList :: (Ord k, Ord v) => [(k, v)] -> Multimap k v
fromList = foldr f empty
  where
    f (k, v) = insert k v

elems :: Multimap k v -> [v]
elems (Multimap m) = Map.elems m >>= Set.elems
{'fraction_non_alphanumeric': 0.1199256275178184, 'fraction_numerical': 0.0, 'mean_word_length': 2.689142857142857, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 55, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2601456', 'n_tokens_mistral': 1203, 'n_tokens_neox': 1197, 'n_words': 475}