int-multimap 0.1.0.1 → 0.1.1
raw patch · 2 files changed
+103/−12 lines, 2 filesPVP: major bump suggested
API removals or changes: PVP suggests a major version bump
API changes (from Hackage documentation)
- IntMultiMap: IntMultiMap :: (IntMap (HashSet value)) -> IntMultiMap value
- IntMultiMap: newtype IntMultiMap value
+ IntMultiMap: data IntMultiMap a
+ IntMultiMap: elems :: IntMultiMap a -> [a]
+ IntMultiMap: empty :: IntMultiMap a
+ IntMultiMap: fromList :: (Eq a, Hashable a) => [(Int, a)] -> IntMultiMap a
+ IntMultiMap: instance (GHC.Classes.Eq a, Data.Hashable.Class.Hashable a) => GHC.Exts.IsList (IntMultiMap.IntMultiMap a)
+ IntMultiMap: keys :: IntMultiMap a -> [Int]
+ IntMultiMap: map :: (Eq b, Hashable b) => (a -> b) -> IntMultiMap a -> IntMultiMap b
+ IntMultiMap: member :: Int -> IntMultiMap a -> Bool
+ IntMultiMap: null :: IntMultiMap a -> Bool
+ IntMultiMap: singleton :: (Hashable a) => Int -> a -> IntMultiMap a
+ IntMultiMap: size :: IntMultiMap a -> Int
+ IntMultiMap: toList :: IntMultiMap b -> [(Int, b)]
- IntMultiMap: delete :: (Hashable value, Eq value) => Int -> value -> IntMultiMap value -> IntMultiMap value
+ IntMultiMap: delete :: (Hashable a, Eq a) => Int -> a -> IntMultiMap a -> IntMultiMap a
- IntMultiMap: insert :: (Hashable value, Ord value) => Int -> value -> IntMultiMap value -> IntMultiMap value
+ IntMultiMap: insert :: (Hashable a, Ord a) => Int -> a -> IntMultiMap a -> IntMultiMap a
- IntMultiMap: split :: Int -> IntMultiMap value -> (IntMultiMap value, IntMultiMap value)
+ IntMultiMap: split :: Int -> IntMultiMap a -> (IntMultiMap a, IntMultiMap a)
Files
- int-multimap.cabal +1/−1
- library/IntMultiMap.hs +102/−11
int-multimap.cabal view
@@ -1,7 +1,7 @@ name: int-multimap version:- 0.1.0.1+ 0.1.1 synopsis: A data structure that associates each Int key with a set of values category:
library/IntMultiMap.hs view
@@ -1,33 +1,124 @@ module IntMultiMap- where+(+ IntMultiMap, + -- * Construction+ empty,+ singleton,++ -- * List+ toList,+ fromList,++ -- * Transformations+ map,++ -- * Basic interface+ null,+ size,+ member,+ delete,+ insert,++ -- * Conversions+ elems,+ keys,++ -- * Filter+ split+)+where+ import qualified Data.IntMap.Strict as A import qualified Data.HashSet as B-import Prelude+import qualified Data.Foldable as C+import qualified GHC.Exts as G+import Prelude hiding (map, null) import Data.Int import Data.Hashable-import Data.Foldable+import Data.Functor+import Data.List(length) import Control.Monad {-|-+A multi map of integers to values a. -}-newtype IntMultiMap value =- IntMultiMap (A.IntMap (B.HashSet value))+newtype IntMultiMap a =+ IntMultiMap (A.IntMap (B.HashSet a)) deriving(Foldable) -delete :: (Hashable value, Eq value) => Int {-^ Key -} -> value -> IntMultiMap value -> IntMultiMap value+{--------------------------------------------------------------------+ Transformations+--------------------------------------------------------------------}+map :: (Eq b, Hashable b) => (a -> b) -> IntMultiMap a -> IntMultiMap b+map f (IntMultiMap intMap) = IntMultiMap $+ fmap (\hashSet -> B.map f hashSet) intMap+{-# INLINE map #-}++{--------------------------------------------------------------------+ Lists+--------------------------------------------------------------------}+instance (Eq a, Hashable a) => G.IsList (IntMultiMap a) where+ type Item (IntMultiMap a) = (Int, a)+ toList = toList+ fromList = fromList++toList :: IntMultiMap b -> [(Int, b)]+toList (IntMultiMap multiMap) = do+ (key, hashSet) <- A.toList multiMap+ fmap ((,) key) $ B.toList hashSet++fromList :: (Eq a, Hashable a) =>+ [(Int, a)] -> IntMultiMap a+fromList = IntMultiMap . A.fromListWith B.union . fmap (fmap B.singleton)++{--------------------------------------------------------------------+ Construction+--------------------------------------------------------------------}+empty :: IntMultiMap a+empty = IntMultiMap A.empty++singleton :: (Hashable a) => Int -> a -> IntMultiMap a+singleton k v = IntMultiMap $ A.singleton k $ B.singleton v+{-# INLINABLE singleton #-}++{--------------------------------------------------------------------+ Basic interface+--------------------------------------------------------------------}+null :: IntMultiMap a -> Bool+null (IntMultiMap intMap) = A.null intMap+{-# INLINE null #-}++size :: IntMultiMap a -> Int+size = length . toList++member :: Int -> IntMultiMap a -> Bool+member key (IntMultiMap intMap) = A.member key intMap++insert :: (Hashable a, Ord a) => Int -> a -> IntMultiMap a -> IntMultiMap a+insert key value (IntMultiMap intMap) =+ IntMultiMap $ A.update (\hash -> Just $ B.insert value hash) key intMap++delete :: (Hashable a, Eq a) => Int {-^ Key -} -> a -> IntMultiMap a -> IntMultiMap a delete key value (IntMultiMap intMap) = IntMultiMap $ A.update f key intMap where f hashSet = mfilter (not . B.null) . Just $ B.delete value hashSet -insert :: (Hashable value, Ord value) => Int -> value -> IntMultiMap value -> IntMultiMap value-insert key value (IntMultiMap intMap) =- IntMultiMap $ A.update (\hash -> Just $ B.insert value hash) key intMap+{--------------------------------------------------------------------+ Conversions+--------------------------------------------------------------------}+elems :: IntMultiMap a -> [a]+elems = foldr (:) [] -split :: Int -> IntMultiMap value -> (IntMultiMap value, IntMultiMap value)+keys :: IntMultiMap a -> [Int]+keys (IntMultiMap intMap) = A.keys intMap++{--------------------------------------------------------------------+ Filter+--------------------------------------------------------------------}+split :: Int -> IntMultiMap a -> (IntMultiMap a, IntMultiMap a) split key (IntMultiMap intMap) = (IntMultiMap oldMap, IntMultiMap newMap) where (oldMap, newMap) = A.split key intMap