diff options
Diffstat (limited to 'Holumbus/Data')
-rw-r--r-- | Holumbus/Data/MultiMap.hs | 190 |
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 | |||
24 | module 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 | ) | ||
47 | where | ||
48 | |||
49 | import Prelude hiding (null, lookup) | ||
50 | |||
51 | import qualified Data.Map as Map | ||
52 | import qualified Data.Set as Set | ||
53 | |||
54 | |||
55 | -- | A MultiMap, it can hold more (different!!!) Elements for one key. | ||
56 | data MultiMap k a = MM (Map.Map k (Set.Set a)) | ||
57 | deriving (Show, Eq, Ord) | ||
58 | |||
59 | {- | ||
60 | instance (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. | ||
69 | empty :: (Ord k, Ord a) => MultiMap k a | ||
70 | empty = MM Map.empty | ||
71 | |||
72 | |||
73 | -- | Test, if the MultiMap is empty. | ||
74 | null :: (Ord k, Ord a) => MultiMap k a -> Bool | ||
75 | null (MM m) = Map.null m | ||
76 | |||
77 | |||
78 | -- | Inserts an element in the MultiMap. | ||
79 | insert :: (Ord k, Ord a) => k -> a -> MultiMap k a -> MultiMap k a | ||
80 | insert 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. | ||
87 | insertSet :: (Ord k, Ord a) => k -> Set.Set a -> MultiMap k a -> MultiMap k a | ||
88 | insertSet 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. | ||
96 | insertKeys :: (Ord k, Ord a) => [k] -> Set.Set a -> MultiMap k a -> MultiMap k a | ||
97 | insertKeys 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. | ||
101 | lookup :: (Ord k, Ord a) => k -> MultiMap k a -> Set.Set a | ||
102 | lookup k (MM m) = maybe (Set.empty) (id) (Map.lookup k m) | ||
103 | |||
104 | |||
105 | -- | Get all different elements from a list of keys. | ||
106 | lookupKeys :: (Ord k, Ord a) => [k] -> MultiMap k a -> Set.Set a | ||
107 | lookupKeys ks m = Set.unions $ map (\k -> lookup k m) ks | ||
108 | |||
109 | |||
110 | -- | Get all different keys from the map. | ||
111 | keys :: (Ord k, Ord a) => MultiMap k a -> Set.Set k | ||
112 | keys (MM m) = Set.fromList $ Map.keys m | ||
113 | |||
114 | |||
115 | -- | Get all different values in the map without regarding their keys. | ||
116 | elems :: (Ord k, Ord a) => MultiMap k a -> Set.Set a | ||
117 | elems (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. | ||
122 | filterElements :: (Ord k, Ord a) => [k] -> MultiMap k a -> Set.Set a | ||
123 | filterElements [] m = elems m -- get all | ||
124 | filterElements ks m = lookupKeys ks m | ||
125 | |||
126 | |||
127 | -- | Test, if a key is in the Map. | ||
128 | member :: (Ord k, Ord a) => k -> MultiMap k a -> Bool | ||
129 | member 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. | ||
134 | delete :: (Ord k, Ord a) => k -> Maybe a -> MultiMap k a -> MultiMap k a | ||
135 | delete k Nothing m = deleteKey k m | ||
136 | delete k (Just a) m = deleteElem k a m | ||
137 | |||
138 | |||
139 | -- | Deletes a whole key from the map. | ||
140 | deleteKey :: (Ord k, Ord a) => k -> MultiMap k a -> MultiMap k a | ||
141 | deleteKey k (MM m) = MM $ Map.delete k m | ||
142 | |||
143 | |||
144 | -- | Deletes a single Element from the map. | ||
145 | deleteElem :: (Ord k, Ord a) => k -> a -> MultiMap k a -> MultiMap k a | ||
146 | deleteElem 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. | ||
155 | deleteElemIf :: (Ord k, Ord a) => k -> (a -> Bool) -> MultiMap k a -> MultiMap k a | ||
156 | deleteElemIf 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!!!). | ||
166 | deleteAllElems :: (Ord k, Ord a) => a -> MultiMap k a -> MultiMap k a | ||
167 | deleteAllElems 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). | ||
173 | fromList :: (Ord k, Ord a) => [(k,Set.Set a)] -> MultiMap k a | ||
174 | fromList ks = foldl (\m (k,as) -> insertSet k as m) empty ks | ||
175 | |||
176 | |||
177 | -- | Creates a MultiMap from a list of tuples. | ||
178 | fromTupleList :: (Ord k, Ord a) => [(k,a)] -> MultiMap k a | ||
179 | fromTupleList 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). | ||
183 | toList :: (Ord k, Ord a) => MultiMap k a -> [(k,Set.Set a)] | ||
184 | toList (MM m) = Map.toList m | ||
185 | |||
186 | |||
187 | -- | The same as toList, but the keys are in ascending order. | ||
188 | toAscList :: (Ord k, Ord a) => MultiMap k a -> [(k,Set.Set a)] | ||
189 | toAscList (MM m) = Map.toAscList m | ||
190 | |||