summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorchessai <>2018-06-13 16:15:00 (GMT)
committerhdiff <hdiff@hdiff.luite.com>2018-06-13 16:15:00 (GMT)
commit8f253adf5ef6ba6205c8e8627923ef5eea685613 (patch)
treed9da1e782d282099da6874d729dbd47427370e5c
version 0.1.0.0HEAD0.1.0.0master
-rw-r--r--ChangeLog.md5
-rw-r--r--Data/MultiHashMap.hs146
-rw-r--r--LICENSE30
-rw-r--r--Setup.hs2
-rw-r--r--multihashmap.cabal19
5 files changed, 202 insertions, 0 deletions
diff --git a/ChangeLog.md b/ChangeLog.md
new file mode 100644
index 0000000..e5c1c96
--- /dev/null
+++ b/ChangeLog.md
@@ -0,0 +1,5 @@
+# Revision history for multihashmap
+
+## 0.1.0.0 -- YYYY-mm-dd
+
+* First version. Released on an unsuspecting world.
diff --git a/Data/MultiHashMap.hs b/Data/MultiHashMap.hs
new file mode 100644
index 0000000..872d25a
--- /dev/null
+++ b/Data/MultiHashMap.hs
@@ -0,0 +1,146 @@
+module Data.MultiHashMap where
+
+import Data.Bifunctor
+import Data.Foldable
+import Data.HashMap.Lazy(HashMap)
+import Data.HashSet(HashSet)
+import Data.Hashable
+import Data.Semigroup
+import qualified Data.HashMap.Lazy as HashMap
+import qualified Data.HashSet as HashSet
+import qualified Data.Maybe as Maybe
+
+newtype MultiHashMap k v = MultiHashMap { toMap :: HashMap k (HashSet v) }
+ deriving (Eq, Show)
+
+instance (Eq k, Hashable k, Eq v, Hashable v) => Semigroup (MultiHashMap k v) where
+ (<>) = union
+
+instance (Eq k, Hashable k, Eq v, Hashable v) => Monoid (MultiHashMap k v) where
+ mempty = MultiHashMap mempty
+ mappend = union
+
+insert
+ :: (Eq k, Hashable k, Eq v, Hashable v)
+ => k
+ -> v
+ -> MultiHashMap k v
+ -> MultiHashMap k v
+insert k = inserts k . HashSet.singleton
+
+inserts
+ :: (Eq k, Hashable k, Eq v, Hashable v)
+ => k
+ -> HashSet v
+ -> MultiHashMap k v
+ -> MultiHashMap k v
+inserts k vs (MultiHashMap m)
+ = MultiHashMap
+ $ HashMap.insertWith HashSet.union k vs m
+
+lookup
+ :: (Eq k, Hashable k, Eq v, Hashable v)
+ => k
+ -> MultiHashMap k v
+ -> HashSet v
+lookup k (MultiHashMap m) = HashMap.lookupDefault mempty k m
+
+lookupDefault
+ :: (Eq k, Hashable k, Eq v, Hashable v)
+ => HashSet v
+ -> k
+ -> MultiHashMap k v
+ -> HashSet v
+lookupDefault d k (MultiHashMap m) = case HashMap.lookup k m of
+ Nothing -> d
+ Just s
+ | HashSet.null s -> d
+ | otherwise -> s
+
+union
+ :: (Eq k, Hashable k, Eq v, Hashable v)
+ => MultiHashMap k v
+ -> MultiHashMap k v
+ -> MultiHashMap k v
+union (MultiHashMap m1) (MultiHashMap m2)
+ = MultiHashMap
+ $ HashMap.unionWith HashSet.union m1 m2
+
+unions
+ :: (Eq k, Hashable k, Eq v, Hashable v)
+ => [MultiHashMap k v]
+ -> MultiHashMap k v
+unions = foldl' union mempty
+
+intersection
+ :: (Eq k, Hashable k, Eq v, Hashable v)
+ => MultiHashMap k v
+ -> MultiHashMap k w
+ -> MultiHashMap k v
+intersection (MultiHashMap m1) (MultiHashMap m2) = MultiHashMap $ HashMap.intersection m1 m2
+
+setIntersection
+ :: (Eq k, Hashable k, Eq v, Hashable v)
+ => MultiHashMap k v
+ -> HashSet k
+ -> MultiHashMap k v
+setIntersection (MultiHashMap m1) s = MultiHashMap $ HashMap.intersection m1 $ HashSet.toMap s
+
+fromList
+ :: (Eq k, Hashable k, Eq v, Hashable v)
+ => [(k, v)]
+ -> MultiHashMap k v
+fromList = foldr (uncurry insert) mempty
+
+toList
+ :: (Eq k, Hashable k, Eq v, Hashable v)
+ => MultiHashMap k v
+ -> [(k, v)]
+toList m = [(k, v) | (k, s) <- toMultiList m, v <- HashSet.toList s]
+
+fromMultiList
+ :: (Eq k, Hashable k, Eq v, Hashable v)
+ => [(k, HashSet v)]
+ -> MultiHashMap k v
+fromMultiList = foldr (uncurry inserts) mempty
+
+toMultiList
+ :: (Eq k, Hashable k, Eq v, Hashable v)
+ => MultiHashMap k v
+ -> [(k, HashSet v)]
+toMultiList (MultiHashMap m) = HashMap.toList m
+
+map
+ :: (Eq k, Hashable k, Eq v, Hashable v, Eq v', Hashable v')
+ => (v -> v')
+ -> MultiHashMap k v
+ -> MultiHashMap k v'
+map f (MultiHashMap m)
+ = MultiHashMap
+ $ fmap (HashSet.map f) m
+
+mapMaybe
+ :: (Eq k, Hashable k, Eq v, Hashable v, Eq v', Hashable v')
+ => (v -> Maybe v')
+ -> MultiHashMap k v
+ -> MultiHashMap k v'
+mapMaybe p (MultiHashMap m)
+ = MultiHashMap
+ $ HashMap.mapMaybe (nothingWhenEmpty . Maybe.mapMaybe p . HashSet.toList) m
+ where
+ nothingWhenEmpty [] = Nothing
+ nothingWhenEmpty xs = Just $ HashSet.fromList xs
+
+mapKeys
+ :: (Eq k, Hashable k, Eq k', Hashable k', Eq v, Hashable v)
+ => (k -> k')
+ -> MultiHashMap k v
+ -> MultiHashMap k' v
+mapKeys f = fromMultiList . Prelude.map (first f) . toMultiList
+
+mapWithKey
+ :: (Eq v', Hashable v')
+ => (k -> v -> v')
+ -> MultiHashMap k v
+ -> MultiHashMap k v'
+mapWithKey f (MultiHashMap m) = MultiHashMap $ HashMap.mapWithKey (HashSet.map . f) m
diff --git a/LICENSE b/LICENSE
new file mode 100644
index 0000000..8f3f232
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,30 @@
+Copyright (c) 2018, chessai
+
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+
+ * Redistributions of source code must retain the above copyright
+ notice, this list of conditions and the following disclaimer.
+
+ * Redistributions in binary form must reproduce the above
+ copyright notice, this list of conditions and the following
+ disclaimer in the documentation and/or other materials provided
+ with the distribution.
+
+ * Neither the name of chessai nor the names of other
+ contributors may be used to endorse or promote products derived
+ from this software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
diff --git a/Setup.hs b/Setup.hs
new file mode 100644
index 0000000..9a994af
--- /dev/null
+++ b/Setup.hs
@@ -0,0 +1,2 @@
+import Distribution.Simple
+main = defaultMain
diff --git a/multihashmap.cabal b/multihashmap.cabal
new file mode 100644
index 0000000..1fe6887
--- /dev/null
+++ b/multihashmap.cabal
@@ -0,0 +1,19 @@
+name: multihashmap
+version: 0.1.0.0
+synopsis: hashmap from keys to hashsets
+description: hashmap from keys to hashsets, yeah
+homepage: https://github.com/chessai/multihash
+license: BSD3
+license-file: LICENSE
+author: chessai
+maintainer: chessai1996@gmail.com
+copyright: (c) 2018 chessai
+category: Data
+build-type: Simple
+extra-source-files: ChangeLog.md
+cabal-version: >=1.10
+
+library
+ exposed-modules: Data.MultiHashMap
+ build-depends: base >=4.7 && <5.0, hashable, unordered-containers
+ default-language: Haskell2010