diff options
author | Joe Crayne <joe@jerkface.net> | 2019-07-16 14:46:48 -0400 |
---|---|---|
committer | Joe Crayne <joe@jerkface.net> | 2019-07-16 14:46:48 -0400 |
commit | 46bbdd047e7dfba3fe95e8b8f5c9e729d4268862 (patch) | |
tree | 51689eb8be09300aeff512f50226d186a4141122 /lib | |
parent | 4ed990981e5bf0b3902364b6b09919d3d7d976cc (diff) |
Updates to IntMapClass.
Diffstat (limited to 'lib')
-rw-r--r-- | lib/IntMapClass.hs | 98 |
1 files changed, 53 insertions, 45 deletions
diff --git a/lib/IntMapClass.hs b/lib/IntMapClass.hs index 3d08e46..b44e05c 100644 --- a/lib/IntMapClass.hs +++ b/lib/IntMapClass.hs | |||
@@ -5,6 +5,7 @@ | |||
5 | DeriveDataTypeable #-} | 5 | DeriveDataTypeable #-} |
6 | module IntMapClass where | 6 | module IntMapClass where |
7 | 7 | ||
8 | import Control.Arrow (second) | ||
8 | import qualified Data.IntMap.Strict as IntMap | 9 | import qualified Data.IntMap.Strict as IntMap |
9 | import Data.IntMap.Strict ( IntMap ) | 10 | import Data.IntMap.Strict ( IntMap ) |
10 | import Data.Typeable ( Typeable ) | 11 | import Data.Typeable ( Typeable ) |
@@ -32,7 +33,13 @@ newtype IMap k a = IMap { intmap :: IntMap a } | |||
32 | , NFData | 33 | , NFData |
33 | ) | 34 | ) |
34 | 35 | ||
36 | adapt_k_a_m :: Coercible k1 Int => | ||
37 | (Int -> t -> IntMap a -> x) -> k1 -> t -> IMap k2 a -> x | ||
35 | adaptm_k_a_m f k a m = IMap $ adapt_k_a_m f k a m | 38 | adaptm_k_a_m f k a m = IMap $ adapt_k_a_m f k a m |
39 | |||
40 | adaptm_k_a_m :: Coercible k1 Int => | ||
41 | (Int -> t -> IntMap a1 -> IntMap a2) | ||
42 | -> k1 -> t -> IMap k2 a1 -> IMap k3 a2 | ||
36 | adapt_k_a_m f k a m = adapt_m (adapt_k f k a) m | 43 | adapt_k_a_m f k a m = adapt_m (adapt_k f k a) m |
37 | 44 | ||
38 | adapt_m_k :: Coercible k Int => (IntMap a -> Int -> x) -> IMap k a -> k -> x | 45 | adapt_m_k :: Coercible k Int => (IntMap a -> Int -> x) -> IMap k a -> k -> x |
@@ -60,17 +67,16 @@ adaptm_m_m f a b = IMap $ adapt_m_m f a b | |||
60 | adapt_m :: (IntMap a -> x) -> IMap k a -> x | 67 | adapt_m :: (IntMap a -> x) -> IMap k a -> x |
61 | adapt_m f (IMap m) = f m | 68 | adapt_m f (IMap m) = f m |
62 | 69 | ||
63 | first f (x,y) = (f x,y) | ||
64 | second f (x,y) = (x,f y) | ||
65 | |||
66 | |||
67 | (!) :: Coercible k Int => IMap k a -> k -> a | 70 | (!) :: Coercible k Int => IMap k a -> k -> a |
68 | (!) = adapt_m_k (IntMap.!) | 71 | (!) = adapt_m_k (IntMap.!) |
69 | 72 | ||
70 | (\\) :: IMap k a -> IMap k a -> IMap k a | 73 | (\\) :: IMap k a -> IMap k a -> IMap k a |
71 | (\\) a b = IMap $ adapt_m_m (IntMap.\\) a b | 74 | (\\) a b = IMap $ adapt_m_m (IntMap.\\) a b |
72 | 75 | ||
76 | null :: IMap k a -> Bool | ||
73 | null = adapt_m (IntMap.null) | 77 | null = adapt_m (IntMap.null) |
78 | |||
79 | size :: IMap k a -> Int | ||
74 | size = adapt_m (IntMap.size) | 80 | size = adapt_m (IntMap.size) |
75 | 81 | ||
76 | member :: Coercible k Int => k -> IMap k a -> Bool | 82 | member :: Coercible k Int => k -> IMap k a -> Bool |
@@ -85,16 +91,19 @@ lookup = adapt_k_m (IntMap.lookup) | |||
85 | findWithDefault :: Coercible k Int => x -> k -> IMap k x -> x | 91 | findWithDefault :: Coercible k Int => x -> k -> IMap k x -> x |
86 | findWithDefault a = adapt_k_m (IntMap.findWithDefault a) | 92 | findWithDefault a = adapt_k_m (IntMap.findWithDefault a) |
87 | 93 | ||
88 | -- FIXME: fmap (first coerce) probably incurs cost | 94 | lookupLT :: Coercible Int k => k -> IMap k a -> Maybe (k, a) |
89 | lookupLT :: ( Coercible Int k, Coercible k Int ) => k -> IMap k a -> Maybe (k, a) | 95 | lookupLT k (IMap m) = coerce $ IntMap.lookupLT (coerce k) m |
90 | lookupLT k m = fmap (first coerce) $ adapt_k_m (IntMap.lookupLT) k m | ||
91 | lookupGT :: ( Coercible Int k, Coercible k Int ) => k -> IMap k a -> Maybe (k, a) | ||
92 | lookupGT k m = fmap (first coerce) $ adapt_k_m (IntMap.lookupGT) k m | ||
93 | lookupLE :: ( Coercible Int k, Coercible k Int ) => k -> IMap k a -> Maybe (k, a) | ||
94 | lookupLE k m = fmap (first coerce) $ adapt_k_m (IntMap.lookupLE) k m | ||
95 | lookupGE :: ( Coercible Int k, Coercible k Int ) => k -> IMap k a -> Maybe (k, a) | ||
96 | lookupGE k m = fmap (first coerce) $ adapt_k_m (IntMap.lookupGE) k m | ||
97 | 96 | ||
97 | lookupGT :: Coercible Int k => k -> IMap k a -> Maybe (k, a) | ||
98 | lookupGT k (IMap m) = coerce $ IntMap.lookupGT (coerce k) m | ||
99 | |||
100 | lookupLE :: Coercible Int k => k -> IMap k a -> Maybe (k, a) | ||
101 | lookupLE k (IMap m) = coerce $ IntMap.lookupLE (coerce k) m | ||
102 | |||
103 | lookupGE :: Coercible Int k => k -> IMap k a -> Maybe (k, a) | ||
104 | lookupGE k (IMap m) = coerce $ IntMap.lookupGE (coerce k) m | ||
105 | |||
106 | empty :: IMap k a | ||
98 | empty = IMap IntMap.empty | 107 | empty = IMap IntMap.empty |
99 | 108 | ||
100 | singleton :: Coercible k Int => k -> a -> IMap k a | 109 | singleton :: Coercible k Int => k -> a -> IMap k a |
@@ -106,10 +115,10 @@ insert = adaptm_k_a_m IntMap.insert | |||
106 | insertWith :: Coercible k Int => (a -> a -> a) -> k -> a -> IMap k a -> IMap k a | 115 | insertWith :: Coercible k Int => (a -> a -> a) -> k -> a -> IMap k a -> IMap k a |
107 | insertWith f = adaptm_k_a_m (IntMap.insertWith f) | 116 | insertWith f = adaptm_k_a_m (IntMap.insertWith f) |
108 | 117 | ||
109 | insertWithKey :: (Coercible Int k, Coercible k Int) => (k -> a -> a -> a) -> k -> a -> IMap k a -> IMap k a | 118 | insertWithKey :: Coercible Int k => (k -> a -> a -> a) -> k -> a -> IMap k a -> IMap k a |
110 | insertWithKey f = adaptm_k_a_m (IntMap.insertWithKey $ f . coerce) | 119 | insertWithKey f = adaptm_k_a_m (IntMap.insertWithKey $ f . coerce) |
111 | 120 | ||
112 | insertLookupWithKey :: (Coercible Int k, Coercible k Int) => | 121 | insertLookupWithKey :: Coercible Int k => |
113 | (k -> a -> a -> a) -> k -> a -> IMap k a -> (Maybe a, IMap k a) | 122 | (k -> a -> a -> a) -> k -> a -> IMap k a -> (Maybe a, IMap k a) |
114 | insertLookupWithKey f k a m = second IMap $ adapt_k_a_m (IntMap.insertLookupWithKey $ f . coerce) k a m | 123 | insertLookupWithKey f k a m = second IMap $ adapt_k_a_m (IntMap.insertLookupWithKey $ f . coerce) k a m |
115 | 124 | ||
@@ -119,7 +128,7 @@ delete = adaptm_k_m IntMap.delete | |||
119 | adjust :: Coercible k Int => (a -> a) -> k -> IMap k a -> IMap k a | 128 | adjust :: Coercible k Int => (a -> a) -> k -> IMap k a -> IMap k a |
120 | adjust f = adaptm_k_m (IntMap.adjust f) | 129 | adjust f = adaptm_k_m (IntMap.adjust f) |
121 | 130 | ||
122 | adjustWithKey :: ( Coercible Int k, Coercible k Int ) => | 131 | adjustWithKey :: Coercible Int k => |
123 | (k -> a -> a) -> k -> IMap k a -> IMap k a | 132 | (k -> a -> a) -> k -> IMap k a -> IMap k a |
124 | adjustWithKey f = adaptm_k_m (IntMap.adjustWithKey $ f . coerce) | 133 | adjustWithKey f = adaptm_k_m (IntMap.adjustWithKey $ f . coerce) |
125 | 134 | ||
@@ -128,13 +137,11 @@ update | |||
128 | update f = adaptm_k_m (IntMap.update f) | 137 | update f = adaptm_k_m (IntMap.update f) |
129 | 138 | ||
130 | 139 | ||
131 | updateWithKey | 140 | updateWithKey :: Coercible Int k => |
132 | :: (Coercible k Int, Coercible Int k) => | ||
133 | (k -> a -> Maybe a) -> k -> IMap k a -> IMap k a | 141 | (k -> a -> Maybe a) -> k -> IMap k a -> IMap k a |
134 | updateWithKey f = adaptm_k_m (IntMap.updateWithKey $ f . coerce) | 142 | updateWithKey f = adaptm_k_m (IntMap.updateWithKey $ f . coerce) |
135 | 143 | ||
136 | updateLookupWithKey :: | 144 | updateLookupWithKey :: Coercible k Int => |
137 | (Coercible k Int, Coercible Int k) => | ||
138 | (k -> a -> Maybe a) -> k -> IMap k a -> (Maybe a, IMap k a) | 145 | (k -> a -> Maybe a) -> k -> IMap k a -> (Maybe a, IMap k a) |
139 | updateLookupWithKey f k m = | 146 | updateLookupWithKey f k m = |
140 | second IMap $ adapt_k_m (IntMap.updateLookupWithKey $ f . coerce) k m | 147 | second IMap $ adapt_k_m (IntMap.updateLookupWithKey $ f . coerce) k m |
@@ -158,8 +165,11 @@ unionWithKey f = adaptm_m_m (IntMap.unionWithKey $ f . coerce) | |||
158 | -- unionsWith :: Coercible [IMap k a] [IntMap a] => (a->a->a) -> [IMap k a] -> IMap k a | 165 | -- unionsWith :: Coercible [IMap k a] [IntMap a] => (a->a->a) -> [IMap k a] -> IMap k a |
159 | -- unionsWith f ms = IMap $ IntMap.unionsWith f (coerce ms) | 166 | -- unionsWith f ms = IMap $ IntMap.unionsWith f (coerce ms) |
160 | 167 | ||
168 | difference :: IMap k b -> IMap k b -> IMap k b | ||
161 | difference = adaptm_m_m IntMap.difference | 169 | difference = adaptm_m_m IntMap.difference |
162 | 170 | ||
171 | differenceWith :: (b -> b -> Maybe b) | ||
172 | -> IMap k b -> IMap k b -> IMap k b | ||
163 | differenceWith f = adaptm_m_m (IntMap.differenceWith f) | 173 | differenceWith f = adaptm_m_m (IntMap.differenceWith f) |
164 | 174 | ||
165 | differenceWithKey :: | 175 | differenceWithKey :: |
@@ -167,7 +177,10 @@ differenceWithKey :: | |||
167 | (k -> a -> a -> Maybe a) -> IMap k a -> IMap k a -> IMap k a | 177 | (k -> a -> a -> Maybe a) -> IMap k a -> IMap k a -> IMap k a |
168 | differenceWithKey f = adaptm_m_m (IntMap.differenceWithKey $ f . coerce) | 178 | differenceWithKey f = adaptm_m_m (IntMap.differenceWithKey $ f . coerce) |
169 | 179 | ||
180 | intersection :: IMap k b -> IMap k b -> IMap k b | ||
170 | intersection = adaptm_m_m IntMap.intersection | 181 | intersection = adaptm_m_m IntMap.intersection |
182 | |||
183 | intersectionWith :: (a -> a -> a) -> IMap k a -> IMap k a -> IMap k a | ||
171 | intersectionWith f = adaptm_m_m (IntMap.intersectionWith f) | 184 | intersectionWith f = adaptm_m_m (IntMap.intersectionWith f) |
172 | 185 | ||
173 | mergeWithKey :: | 186 | mergeWithKey :: |
@@ -211,15 +224,15 @@ mapAccumRWithKey :: Coercible Int k => | |||
211 | mapAccumRWithKey f a m = second IMap $ IntMap.mapAccumRWithKey f' a (intmap m) | 224 | mapAccumRWithKey f a m = second IMap $ IntMap.mapAccumRWithKey f' a (intmap m) |
212 | where f' a k b = f a (coerce k) b | 225 | where f' a k b = f a (coerce k) b |
213 | 226 | ||
214 | mapKeys :: (Coercible Int k1, Coercible k2 Int) => | 227 | mapKeys :: (Coercible Int k1, Coercible Int k2) => |
215 | (k1 -> k2) -> IMap k1 a -> IMap k2 a | 228 | (k1 -> k2) -> IMap k1 a -> IMap k2 a |
216 | mapKeys f = IMap . adapt_m (IntMap.mapKeys (coerce . f . coerce)) | 229 | mapKeys f = IMap . adapt_m (IntMap.mapKeys (coerce . f . coerce)) |
217 | 230 | ||
218 | mapKeysWith :: (Coercible Int k1, Coercible k2 Int) => | 231 | mapKeysWith :: (Coercible Int k1, Coercible Int k2) => |
219 | (a->a->a) -> (k1 -> k2) -> IMap k1 a -> IMap k2 a | 232 | (a->a->a) -> (k1 -> k2) -> IMap k1 a -> IMap k2 a |
220 | mapKeysWith c f = IMap . adapt_m (IntMap.mapKeysWith c (coerce . f . coerce)) | 233 | mapKeysWith c f = IMap . adapt_m (IntMap.mapKeysWith c (coerce . f . coerce)) |
221 | 234 | ||
222 | mapKeysMonotonic :: (Coercible Int k1, Coercible k2 Int) => | 235 | mapKeysMonotonic :: (Coercible Int k1, Coercible Int k2) => |
223 | (k1 -> k2) -> IMap k1 a -> IMap k2 a | 236 | (k1 -> k2) -> IMap k1 a -> IMap k2 a |
224 | mapKeysMonotonic f = IMap . adapt_m (IntMap.mapKeysMonotonic (coerce . f . coerce)) | 237 | mapKeysMonotonic f = IMap . adapt_m (IntMap.mapKeysMonotonic (coerce . f . coerce)) |
225 | 238 | ||
@@ -236,7 +249,7 @@ foldlWithKey :: | |||
236 | Coercible Int k => (x -> k -> a -> x) -> x -> IMap k a -> x | 249 | Coercible Int k => (x -> k -> a -> x) -> x -> IMap k a -> x |
237 | foldlWithKey f a = adapt_m (IntMap.foldlWithKey f' a) where f' a = f a . coerce | 250 | foldlWithKey f a = adapt_m (IntMap.foldlWithKey f' a) where f' a = f a . coerce |
238 | 251 | ||
239 | foldMapWithKey :: (Monoid m, Coercible k Int) => (k -> a -> m) -> IMap k a -> m | 252 | foldMapWithKey :: (Monoid m, Coercible Int k) => (k -> a -> m) -> IMap k a -> m |
240 | foldMapWithKey f = adapt_m (IntMap.foldMapWithKey $ f . coerce) | 253 | foldMapWithKey f = adapt_m (IntMap.foldMapWithKey $ f . coerce) |
241 | 254 | ||
242 | foldr' :: (a -> x -> x) -> x -> IMap k a -> x | 255 | foldr' :: (a -> x -> x) -> x -> IMap k a -> x |
@@ -255,50 +268,47 @@ foldlWithKey' f a = adapt_m (IntMap.foldlWithKey' f' a) where f' a = f a . coerc | |||
255 | elems :: IMap k a -> [a] | 268 | elems :: IMap k a -> [a] |
256 | elems = IntMap.elems . intmap | 269 | elems = IntMap.elems . intmap |
257 | 270 | ||
258 | keys :: Coercible [Int] [k] => IMap k a -> [k] | 271 | keys :: Coercible Int k => IMap k a -> [k] |
259 | keys = coerce . IntMap.keys . intmap | 272 | keys = coerce . IntMap.keys . intmap |
260 | 273 | ||
261 | assocs :: Coercible [(Int,a)] [(k,a)] => IMap k a -> [(k, a)] | 274 | assocs :: Coercible Int k => IMap k a -> [(k, a)] |
262 | assocs = coerce . IntMap.assocs . intmap | 275 | assocs = coerce . IntMap.assocs . intmap |
263 | 276 | ||
264 | -- Not implementing... (doing it right requires wrapping IntSet) | 277 | -- Not implementing... (doing it right requires wrapping IntSet) |
265 | -- keysSet :: IntMap a -> IntSet | 278 | -- keysSet :: IntMap a -> IntSet |
266 | -- fromSet :: (Key -> a) -> IntSet -> IntMap a | 279 | -- fromSet :: (Key -> a) -> IntSet -> IntMap a |
267 | 280 | ||
268 | toList :: Coercible [(Int,a)] [(k,a)] => IMap k a -> [(k, a)] | 281 | toList :: Coercible Int k => IMap k a -> [(k, a)] |
269 | toList = coerce . IntMap.toList . intmap | 282 | toList = coerce . IntMap.toList . intmap |
270 | 283 | ||
271 | fromList :: Coercible [(k,a)] [(Int,a)] => [(k, a)] -> IMap k a | 284 | fromList :: Coercible Int k => [(k, a)] -> IMap k a |
272 | fromList = IMap . IntMap.fromList . coerce | 285 | fromList = IMap . IntMap.fromList . coerce |
273 | 286 | ||
274 | fromListWith :: Coercible [(k,a)] [(Int,a)] => (a -> a -> a) -> [(k, a)] -> IMap k a | 287 | fromListWith :: Coercible Int k => (a -> a -> a) -> [(k, a)] -> IMap k a |
275 | fromListWith f = IMap . IntMap.fromListWith f . coerce | 288 | fromListWith f = IMap . IntMap.fromListWith f . coerce |
276 | 289 | ||
277 | fromListWithKey :: ( Coercible Int k | 290 | fromListWithKey :: Coercible Int k => |
278 | , Coercible [(k,a)] [(Int,a)] ) => | ||
279 | (k -> a -> a -> a) -> [(k, a)] -> IMap k a | 291 | (k -> a -> a -> a) -> [(k, a)] -> IMap k a |
280 | fromListWithKey f = IMap . IntMap.fromListWithKey (f . coerce) . coerce | 292 | fromListWithKey f = IMap . IntMap.fromListWithKey (f . coerce) . coerce |
281 | 293 | ||
282 | toAscList :: Coercible [(Int,a)] [(k,a)] => IMap k a -> [(k,a)] | 294 | toAscList :: Coercible Int k => IMap k a -> [(k,a)] |
283 | toAscList (IMap m) = coerce $ IntMap.toAscList m | 295 | toAscList (IMap m) = coerce $ IntMap.toAscList m |
284 | 296 | ||
285 | toDescList :: Coercible [(Int,a)] [(k,a)] => IMap k a -> [(k,a)] | 297 | toDescList :: Coercible Int k => IMap k a -> [(k,a)] |
286 | toDescList (IMap m) = coerce $ IntMap.toDescList m | 298 | toDescList (IMap m) = coerce $ IntMap.toDescList m |
287 | 299 | ||
288 | fromAscList :: Coercible [(k,a)] [(Int,a)] => [(k, a)] -> IMap k a | 300 | fromAscList :: Coercible Int k => [(k, a)] -> IMap k a |
289 | fromAscList = IMap . IntMap.fromAscList . coerce | 301 | fromAscList = IMap . IntMap.fromAscList . coerce |
290 | 302 | ||
291 | fromAscListWith :: | 303 | fromAscListWith :: Coercible Int k |
292 | Coercible [(k,a)] [(Int, a)] => | 304 | => (a -> a -> a) -> [(k,a)] -> IMap k a |
293 | (a -> a -> a) -> [(k,a)] -> IMap k a | ||
294 | fromAscListWith f = IMap . IntMap.fromAscListWith f . coerce | 305 | fromAscListWith f = IMap . IntMap.fromAscListWith f . coerce |
295 | 306 | ||
296 | fromAscListWithKey :: | 307 | fromAscListWithKey :: Coercible Int k |
297 | (Coercible Int k, Coercible [(k,a)] [(Int, a)]) => | 308 | => (k -> a -> a -> a) -> [(k,a)] -> IMap k a |
298 | (k -> a -> a -> a) -> [(k,a)] -> IMap k a | ||
299 | fromAscListWithKey f = IMap . IntMap.fromAscListWithKey (f . coerce) . coerce | 309 | fromAscListWithKey f = IMap . IntMap.fromAscListWithKey (f . coerce) . coerce |
300 | 310 | ||
301 | fromDistinctAscList :: Coercible [(k,a)] [(Int,a)] => [(k, a)] -> IMap k a | 311 | fromDistinctAscList :: Coercible Int k => [(k, a)] -> IMap k a |
302 | fromDistinctAscList = IMap . IntMap.fromDistinctAscList . coerce | 312 | fromDistinctAscList = IMap . IntMap.fromDistinctAscList . coerce |
303 | 313 | ||
304 | filter :: (a -> Bool) -> IMap k a -> IMap k a | 314 | filter :: (a -> Bool) -> IMap k a -> IMap k a |
@@ -307,13 +317,11 @@ filter f = IMap . adapt_m (IntMap.filter f) | |||
307 | filterWithKey :: Coercible Int k => (k -> a -> Bool) -> IMap k a -> IMap k a | 317 | filterWithKey :: Coercible Int k => (k -> a -> Bool) -> IMap k a -> IMap k a |
308 | filterWithKey f = IMap . adapt_m (IntMap.filterWithKey $ f . coerce) | 318 | filterWithKey f = IMap . adapt_m (IntMap.filterWithKey $ f . coerce) |
309 | 319 | ||
310 | partition :: Coercible (IntMap a,IntMap a) (IMap k a,IMap k a) | 320 | partition :: (a -> Bool) -> IMap k a -> (IMap k a, IMap k a) |
311 | => (a -> Bool) -> IMap k a -> (IMap k a, IMap k a) | ||
312 | partition f m = coerce $ IntMap.partition f (intmap m) | 321 | partition f m = coerce $ IntMap.partition f (intmap m) |
313 | 322 | ||
314 | 323 | ||
315 | partitionWithKey :: ( Coercible Int k | 324 | partitionWithKey :: Coercible Int k |
316 | , Coercible (IntMap a,IntMap a) (IMap k a,IMap k a) ) | ||
317 | => (k -> a -> Bool) -> IMap k a -> (IMap k a, IMap k a) | 325 | => (k -> a -> Bool) -> IMap k a -> (IMap k a, IMap k a) |
318 | partitionWithKey f m = coerce $ IntMap.partitionWithKey (f . coerce) (intmap m) | 326 | partitionWithKey f m = coerce $ IntMap.partitionWithKey (f . coerce) (intmap m) |
319 | 327 | ||