diff options
-rw-r--r-- | IntMapClass.hs | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/IntMapClass.hs b/IntMapClass.hs index ce544f9..77ce16b 100644 --- a/IntMapClass.hs +++ b/IntMapClass.hs | |||
@@ -89,6 +89,7 @@ lookup = adapt_k_m (IntMap.lookup) | |||
89 | findWithDefault :: Coercible k Int => x -> k -> IMap k x -> x | 89 | findWithDefault :: Coercible k Int => x -> k -> IMap k x -> x |
90 | findWithDefault a = adapt_k_m (IntMap.findWithDefault a) | 90 | findWithDefault a = adapt_k_m (IntMap.findWithDefault a) |
91 | 91 | ||
92 | -- FIXME: fmap (first coerce) probably incurs cost | ||
92 | lookupLT :: ( Coercible Int k, Coercible k Int ) => k -> IMap k a -> Maybe (k, a) | 93 | lookupLT :: ( Coercible Int k, Coercible k Int ) => k -> IMap k a -> Maybe (k, a) |
93 | lookupLT k m = fmap (first coerce) $ adapt_k_m (IntMap.lookupLT) k m | 94 | lookupLT k m = fmap (first coerce) $ adapt_k_m (IntMap.lookupLT) k m |
94 | lookupGT :: ( Coercible Int k, Coercible k Int ) => k -> IMap k a -> Maybe (k, a) | 95 | lookupGT :: ( Coercible Int k, Coercible k Int ) => k -> IMap k a -> Maybe (k, a) |
@@ -194,6 +195,7 @@ map f = IMap . adapt_m (IntMap.map f) | |||
194 | mapWithKey :: Coercible Int k => (k -> a -> b) -> IMap k a -> IMap k b | 195 | mapWithKey :: Coercible Int k => (k -> a -> b) -> IMap k a -> IMap k b |
195 | mapWithKey f = IMap . adapt_m (IntMap.mapWithKey $ f . coerce) | 196 | mapWithKey f = IMap . adapt_m (IntMap.mapWithKey $ f . coerce) |
196 | 197 | ||
198 | -- FIXME: fmap IMap ? | ||
197 | traverseWithKey :: | 199 | traverseWithKey :: |
198 | (Applicative f, Coercible Int k) => | 200 | (Applicative f, Coercible Int k) => |
199 | (k -> a -> f b) -> IMap k a -> f (IMap k b) | 201 | (k -> a -> f b) -> IMap k a -> f (IMap k b) |
@@ -283,4 +285,45 @@ fromListWithKey :: ( Coercible Int k | |||
283 | (k -> a -> a -> a) -> [(k, a)] -> IMap k a | 285 | (k -> a -> a -> a) -> [(k, a)] -> IMap k a |
284 | fromListWithKey f = IMap . IntMap.fromListWithKey (f . coerce) . coerce | 286 | fromListWithKey f = IMap . IntMap.fromListWithKey (f . coerce) . coerce |
285 | 287 | ||
288 | toAscList :: Coercible [(Int,a)] [(k,a)] => IMap k a -> [(k,a)] | ||
289 | toAscList (IMap m) = coerce $ IntMap.toAscList m | ||
290 | |||
291 | toDescList :: Coercible [(Int,a)] [(k,a)] => IMap k a -> [(k,a)] | ||
292 | toDescList (IMap m) = coerce $ IntMap.toDescList m | ||
293 | |||
294 | fromAscList :: Coercible [(k,a)] [(Int,a)] => [(k, a)] -> IMap k a | ||
295 | fromAscList = IMap . IntMap.fromAscList . coerce | ||
296 | |||
297 | fromAscListWith :: | ||
298 | Coercible [(k,a)] [(Int, a)] => | ||
299 | (a -> a -> a) -> [(k,a)] -> IMap k a | ||
300 | fromAscListWith f = IMap . IntMap.fromAscListWith f . coerce | ||
301 | |||
302 | fromAscListWithKey :: | ||
303 | (Coercible Int k, Coercible [(k,a)] [(Int, a)]) => | ||
304 | (k -> a -> a -> a) -> [(k,a)] -> IMap k a | ||
305 | fromAscListWithKey f = IMap . IntMap.fromAscListWithKey (f . coerce) . coerce | ||
306 | |||
307 | fromDistinctAscList :: Coercible [(k,a)] [(Int,a)] => [(k, a)] -> IMap k a | ||
308 | fromDistinctAscList = IMap . IntMap.fromDistinctAscList . coerce | ||
309 | |||
310 | filter :: (a -> Bool) -> IMap k a -> IMap k a | ||
311 | filter f = IMap . adapt_m (IntMap.filter f) | ||
312 | |||
313 | filterWithKey :: Coercible Int k => (k -> a -> Bool) -> IMap k a -> IMap k a | ||
314 | filterWithKey f = IMap . adapt_m (IntMap.filterWithKey $ f . coerce) | ||
315 | |||
316 | partition :: Coercible (IntMap a,IntMap a) (IMap k a,IMap k a) | ||
317 | => (a -> Bool) -> IMap k a -> (IMap k a, IMap k a) | ||
318 | partition f m = coerce $ IntMap.partition f (intmap m) | ||
319 | |||
320 | |||
321 | partitionWithKey :: ( Coercible Int k | ||
322 | , Coercible (IntMap a,IntMap a) (IMap k a,IMap k a) ) | ||
323 | => (k -> a -> Bool) -> IMap k a -> (IMap k a, IMap k a) | ||
324 | partitionWithKey f m = coerce $ IntMap.partitionWithKey (f . coerce) (intmap m) | ||
325 | |||
326 | mapMaybe :: (a -> Maybe b) -> IMap k a -> IMap k b | ||
327 | mapMaybe f = IMap . IntMap.mapMaybe f . intmap | ||
328 | |||
286 | 329 | ||