-
-
Notifications
You must be signed in to change notification settings - Fork 80
/
Map.hs
321 lines (258 loc) · 7.05 KB
/
Map.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
{-# LANGUAGE CPP #-}
{-# LANGUAGE Trustworthy #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
{- |
Module : Relude.Extra.Map
Copyright : (c) 2018-2023 Kowainik
SPDX-License-Identifier : MIT
Maintainer : Kowainik <xrom.xkov@gmail.com>
Stability : Stable
Portability : Portable
Contains implementation of polymorphic type classes for data types 'Set' and
'Map'.
-}
module Relude.Extra.Map
( StaticMap (..)
, DynamicMap (..)
, (!?)
, notMember
, lookupDefault
-- * To pairs
, toPairs
, keys
, elems
) where
#if __GLASGOW_HASKELL__ >= 904
import Data.Type.Equality (type (~))
#endif
import GHC.Exts (IsList (Item, toList))
#if MIN_VERSION_hashable(1,4,0)
import Relude.Base (Ord, Type)
#else
import Relude.Base (Eq, Ord, Type)
#endif
import Relude.Bool (Bool, guard, not)
import Relude.Container.Reexport (HashMap, HashSet, Hashable, IntMap, IntSet, Map, Set, fst, snd)
import Relude.Function ((.))
import Relude.Functor.Reexport (($>))
import Relude.List (map)
import Relude.Monad.Reexport (Maybe (..), fromMaybe)
import Relude.Numeric (Int)
import qualified Data.HashMap.Strict as HM
import qualified Data.HashSet as HS
import qualified Data.IntMap as IM
import qualified Data.IntSet as IS
import qualified Data.Map.Strict as M
import qualified Data.Set as S
----------------------------------------------------------------------------
-- Static Map
----------------------------------------------------------------------------
{- | Read-only map or set. Contains polymorphic functions which work for both
sets and maps.
@since 0.1.0
-}
class StaticMap t where
type Key t :: Type
type Val t :: Type
size :: t -> Int
lookup :: Key t -> t -> Maybe (Val t)
member :: Key t -> t -> Bool
{- |
@since 0.1.0
-}
instance Ord k => StaticMap (Map k v) where
type Key (Map k v) = k
type Val (Map k v) = v
size = M.size
{-# INLINE size #-}
lookup = M.lookup
{-# INLINE lookup #-}
member = M.member
{-# INLINE member #-}
{- |
@since 0.1.0
-}
#if MIN_VERSION_hashable(1,4,0)
instance (Hashable k) => StaticMap (HashMap k v) where
#else
instance (Eq k, Hashable k) => StaticMap (HashMap k v) where
#endif
type Key (HashMap k v) = k
type Val (HashMap k v) = v
size = HM.size
{-# INLINE size #-}
lookup = HM.lookup
{-# INLINE lookup #-}
member = HM.member
{-# INLINE member #-}
{- |
@since 0.1.0
-}
instance StaticMap (IntMap v) where
type Key (IntMap v) = Int
type Val (IntMap v) = v
size = IM.size
{-# INLINE size #-}
lookup = IM.lookup
{-# INLINE lookup #-}
member = IM.member
{-# INLINE member #-}
{- |
@since 0.1.0
-}
instance Ord a => StaticMap (Set a) where
type Key (Set a) = a
type Val (Set a) = a
size = S.size
{-# INLINE size #-}
member = S.member
{-# INLINE member #-}
lookup k m = guard (member k m) $> k
{-# INLINE lookup #-}
{- |
@since 0.1.0
-}
#if MIN_VERSION_hashable(1,4,0)
instance (Hashable a) => StaticMap (HashSet a) where
#else
instance (Eq a, Hashable a) => StaticMap (HashSet a) where
#endif
type Key (HashSet a) = a
type Val (HashSet a) = a
size = HS.size
{-# INLINE size #-}
member = HS.member
{-# INLINE member #-}
lookup k m = guard (member k m) $> k
{-# INLINE lookup #-}
{- |
@since 0.1.0
-}
instance StaticMap IntSet where
type Key IntSet = Int
type Val IntSet = Int
size = IS.size
{-# INLINE size #-}
member = IS.member
{-# INLINE member #-}
lookup k m = guard (member k m) $> k
{-# INLINE lookup #-}
{- | Operator version of 'lookup' function.
>>> let myHashMap = HashMap.fromList [('a', "xxx"), ('b', "yyy")]
>>> myHashMap !? 'b'
Just "yyy"
>>> myHashMap !? 'd'
Nothing
@since 0.1.0
-}
infixl 9 !?
(!?) :: StaticMap t => t -> Key t -> Maybe (Val t)
(!?) m k = lookup k m
{-# INLINE (!?) #-}
{- | Inverse of 'member' function.
>>> let myHashMap = HashMap.fromList [('a', "xxx"), ('b', "yyy")]
>>> notMember 'b' myHashMap
False
>>> notMember 'c' myHashMap
True
@since 0.1.0
-}
notMember :: StaticMap t => Key t -> t -> Bool
notMember k = not . member k
{-# INLINE notMember #-}
{- | Return the value to which the specified key is mapped, or the default value
if this map contains no mapping for the key.
>>> let myHashMap = HashMap.fromList [('a', "xxx"), ('b', "yyy")]
>>> lookupDefault "zzz" 'b' myHashMap
"yyy"
>>> lookupDefault "zzz" 'c' myHashMap
"zzz"
@since 0.1.0
-}
lookupDefault :: StaticMap t
=> Val t -- ^ Default value to return.
-> Key t -- ^ Key to search
-> t -- ^ Container to search
-> Val t
lookupDefault def k = fromMaybe def . lookup k
{-# INLINE lookupDefault #-}
----------------------------------------------------------------------------
-- Dynamic Map
----------------------------------------------------------------------------
{- | Modifiable Map.
@since 0.1.0
-}
class StaticMap t => DynamicMap t where
-- insertions
insert :: Key t -> Val t -> t -> t
insertWith :: (Val t -> Val t -> Val t) -> Key t -> Val t -> t -> t
-- deletions
delete :: Key t -> t -> t
alter :: (Maybe (Val t) -> Maybe (Val t)) -> Key t -> t -> t
{- |
@since 0.1.0
-}
instance Ord k => DynamicMap (Map k v) where
insert = M.insert
{-# INLINE insert #-}
insertWith = M.insertWith
{-# INLINE insertWith #-}
delete = M.delete
{-# INLINE delete #-}
alter = M.alter
{-# INLINE alter #-}
{- |
@since 0.1.0
-}
#if MIN_VERSION_hashable(1,4,0)
instance (Hashable k) => DynamicMap (HashMap k v) where
#else
instance (Eq k, Hashable k) => DynamicMap (HashMap k v) where
#endif
insert = HM.insert
{-# INLINE insert #-}
insertWith = HM.insertWith
{-# INLINE insertWith #-}
delete = HM.delete
{-# INLINE delete #-}
alter = HM.alter
{-# INLINE alter #-}
{- |
@since 0.1.0
-}
instance DynamicMap (IntMap v) where
insert = IM.insert
{-# INLINE insert #-}
insertWith = IM.insertWith
{-# INLINE insertWith #-}
delete = IM.delete
{-# INLINE delete #-}
alter = IM.alter
{-# INLINE alter #-}
----------------------------------------------------------------------------
-- ToPairs
----------------------------------------------------------------------------
-- $setup
-- >>> import qualified Data.HashMap.Strict as HashMap
{- | Converts the structure to the list of the key-value pairs.
>>> toPairs (HashMap.fromList [('a', "xxx"), ('b', "yyy")])
[('a',"xxx"),('b',"yyy")]
@since 0.1.0
-}
toPairs :: (IsList t, Item t ~ (a, b)) => t -> [(a, b)]
toPairs = toList
{- | Converts the structure to the list of the keys.
>>> keys (HashMap.fromList [('a', "xxx"), ('b', "yyy")])
"ab"
@since 0.1.0
-}
keys :: (IsList t, Item t ~ (a, b)) => t -> [a]
keys = map fst . toList
{- | Converts the structure to the list of the values.
>>> elems (HashMap.fromList [('a', "xxx"), ('b', "yyy")])
["xxx","yyy"]
@since 0.1.0
-}
elems :: (IsList t, Item t ~ (a, b)) => t -> [b]
elems = map snd . toList