summaryrefslogtreecommitdiff
path: root/Holumbus/Data/MultiMap.hs
diff options
context:
space:
mode:
authorjoe <joe@jerkface.net>2017-11-04 17:04:16 -0400
committerjoe <joe@jerkface.net>2017-11-04 17:04:16 -0400
commit366d793c9f9acc9b228675f1af23c01ef02dd3b1 (patch)
tree0970725923144707cb5dfb479c9d1289ec0be4c7 /Holumbus/Data/MultiMap.hs
parent67afff3c6e2ce009e7fff83669e8c381e27166f2 (diff)
Deleted obsolete files.
Diffstat (limited to 'Holumbus/Data/MultiMap.hs')
-rw-r--r--Holumbus/Data/MultiMap.hs190
1 files changed, 0 insertions, 190 deletions
diff --git a/Holumbus/Data/MultiMap.hs b/Holumbus/Data/MultiMap.hs
deleted file mode 100644
index 07db808e..00000000
--- a/Holumbus/Data/MultiMap.hs
+++ /dev/null
@@ -1,190 +0,0 @@
1-- ----------------------------------------------------------------------------
2{- |
3 Module : Holumbus.Data.MultiMap
4 Copyright : Copyright (C) 2008 Stefan Schmidt
5 License : MIT
6
7 Maintainer : Stefan Schmidt (stefanschmidt@web.de)
8 Stability : experimental
9 Portability: portable
10 Version : 0.1
11
12 This module provides a MultiMap, that means a Map, which can hold
13 multiple values for one key, but every distinct value is only stores once.
14 So adding the same key-value-pair twice will only create one new entry in
15 the map.
16
17 This Map is helpfull to examine how many different key-values-pairs you
18 have in your application.
19
20 Most of the functions are borrowed from Data.Map
21-}
22-- ----------------------------------------------------------------------------
23
24module Holumbus.Data.MultiMap
25(
26 MultiMap
27, empty
28, null
29, insert
30, insertSet
31, insertKeys
32, lookup
33, keys
34, elems
35, filterElements
36, member
37, delete
38, deleteKey
39, deleteElem
40, deleteElemIf
41, deleteAllElems
42, fromList
43, fromTupleList
44, toList
45, toAscList
46)
47where
48
49import Prelude hiding (null, lookup)
50
51import qualified Data.Map as Map
52import qualified Data.Set as Set
53
54
55-- | A MultiMap, it can hold more (different!!!) Elements for one key.
56data MultiMap k a = MM (Map.Map k (Set.Set a))
57 deriving (Show, Eq, Ord)
58
59{-
60instance (Show k, Show a) => Show (MultiMap k a) where
61 show (MM m) = msShow
62 where
63 ms = map (\(k,s) -> (k, Set.toList s)) (Map.toList m)
64 msShow = concat $ map (\(k,s) -> (show k) ++ "\n" ++ (showL s)) ms
65 showL ls = concat $ map (\s -> show s ++ "\n") ls
66-}
67
68-- | The empty MultiMap.
69empty :: (Ord k, Ord a) => MultiMap k a
70empty = MM Map.empty
71
72
73-- | Test, if the MultiMap is empty.
74null :: (Ord k, Ord a) => MultiMap k a -> Bool
75null (MM m) = Map.null m
76
77
78-- | Inserts an element in the MultiMap.
79insert :: (Ord k, Ord a) => k -> a -> MultiMap k a -> MultiMap k a
80insert k a (MM m) = MM $ Map.alter altering k m
81 where
82 altering Nothing = Just $ Set.singleton a
83 altering (Just s) = Just $ Set.insert a s
84
85
86-- | Inserts multiple elements in a set to the MultiMap.
87insertSet :: (Ord k, Ord a) => k -> Set.Set a -> MultiMap k a -> MultiMap k a
88insertSet k newSet mm@(MM m) =
89 if (Set.null newSet) then mm else MM $ Map.alter altering k m
90 where
91 altering Nothing = Just newSet
92 altering (Just s) = Just $ Set.union newSet s
93
94
95-- | Inserts multiple keys with the same values.
96insertKeys :: (Ord k, Ord a) => [k] -> Set.Set a -> MultiMap k a -> MultiMap k a
97insertKeys ks a m = foldl (\m' k -> insertSet k a m') m ks
98
99
100-- | Gets all different elements for one key or an empty set.
101lookup :: (Ord k, Ord a) => k -> MultiMap k a -> Set.Set a
102lookup k (MM m) = maybe (Set.empty) (id) (Map.lookup k m)
103
104
105-- | Get all different elements from a list of keys.
106lookupKeys :: (Ord k, Ord a) => [k] -> MultiMap k a -> Set.Set a
107lookupKeys ks m = Set.unions $ map (\k -> lookup k m) ks
108
109
110-- | Get all different keys from the map.
111keys :: (Ord k, Ord a) => MultiMap k a -> Set.Set k
112keys (MM m) = Set.fromList $ Map.keys m
113
114
115-- | Get all different values in the map without regarding their keys.
116elems :: (Ord k, Ord a) => MultiMap k a -> Set.Set a
117elems (MM m) = Set.unions $ Map.elems m
118
119
120-- | Like lookup keys, but an empty input list will give all elements back,
121-- not the empty set.
122filterElements :: (Ord k, Ord a) => [k] -> MultiMap k a -> Set.Set a
123filterElements [] m = elems m -- get all
124filterElements ks m = lookupKeys ks m
125
126
127-- | Test, if a key is in the Map.
128member :: (Ord k, Ord a) => k -> MultiMap k a -> Bool
129member k m = Set.empty /= lookup k m
130
131
132-- | Deletes an Element from the Map, if the data in Nothing, the whole key is
133-- deleted.
134delete :: (Ord k, Ord a) => k -> Maybe a -> MultiMap k a -> MultiMap k a
135delete k Nothing m = deleteKey k m
136delete k (Just a) m = deleteElem k a m
137
138
139-- | Deletes a whole key from the map.
140deleteKey :: (Ord k, Ord a) => k -> MultiMap k a -> MultiMap k a
141deleteKey k (MM m) = MM $ Map.delete k m
142
143
144-- | Deletes a single Element from the map.
145deleteElem :: (Ord k, Ord a) => k -> a -> MultiMap k a -> MultiMap k a
146deleteElem k a (MM m) = MM $ Map.alter delSet k m
147 where
148 delSet Nothing = Nothing
149 delSet (Just set) = filterEmpty $ Set.delete a set
150 filterEmpty set
151 | set == Set.empty = Nothing
152 | otherwise = Just set
153
154-- | Deletes a single Element from the map.
155deleteElemIf :: (Ord k, Ord a) => k -> (a -> Bool) -> MultiMap k a -> MultiMap k a
156deleteElemIf k pred (MM m) = MM $ Map.alter delSet k m
157 where
158 delSet Nothing = Nothing
159 delSet (Just set) = filterEmpty $ Set.filter (not . pred) set
160 filterEmpty set
161 | set == Set.empty = Nothing
162 | otherwise = Just set
163
164
165-- | Deletes all Elements (*,a) (slow!!!).
166deleteAllElems :: (Ord k, Ord a) => a -> MultiMap k a -> MultiMap k a
167deleteAllElems a m = foldl (\m'' k -> deleteElem k a m'') m ks
168 where
169 ks = Set.toList $ keys m
170
171
172-- | Creates a MultiMap from a list of pairs (key,set value).
173fromList :: (Ord k, Ord a) => [(k,Set.Set a)] -> MultiMap k a
174fromList ks = foldl (\m (k,as) -> insertSet k as m) empty ks
175
176
177-- | Creates a MultiMap from a list of tuples.
178fromTupleList :: (Ord k, Ord a) => [(k,a)] -> MultiMap k a
179fromTupleList ks = foldl (\m (k,a) -> insert k a m) empty ks
180
181
182-- | Transforms a MultiMap to a list of pairs (key,set value).
183toList :: (Ord k, Ord a) => MultiMap k a -> [(k,Set.Set a)]
184toList (MM m) = Map.toList m
185
186
187-- | The same as toList, but the keys are in ascending order.
188toAscList :: (Ord k, Ord a) => MultiMap k a -> [(k,Set.Set a)]
189toAscList (MM m) = Map.toAscList m
190