summaryrefslogtreecommitdiff
path: root/IntMapClass.hs
diff options
context:
space:
mode:
authorjoe <joe@jerkface.net>2014-08-12 18:14:44 -0400
committerjoe <joe@jerkface.net>2014-08-12 18:14:44 -0400
commitb86c7842e1899298a160157b0a021a6df64a890a (patch)
tree788975fcf674a1732927175cf573e48563c2d6fc /IntMapClass.hs
parent524dee47806e54c77db56755fc0fdac932b0e887 (diff)
more IntMap wrapping
Diffstat (limited to 'IntMapClass.hs')
-rw-r--r--IntMapClass.hs122
1 files changed, 120 insertions, 2 deletions
diff --git a/IntMapClass.hs b/IntMapClass.hs
index 28ae5ba..7c18de8 100644
--- a/IntMapClass.hs
+++ b/IntMapClass.hs
@@ -14,6 +14,7 @@ import Data.Foldable ( Foldable )
14import Data.Traversable ( Traversable ) 14import Data.Traversable ( Traversable )
15import Data.Monoid ( Monoid ) 15import Data.Monoid ( Monoid )
16import Control.DeepSeq ( NFData ) 16import Control.DeepSeq ( NFData )
17import Control.Applicative ( Applicative )
17#if MIN_VERSION_base(4,7,0) 18#if MIN_VERSION_base(4,7,0)
18import Data.Coerce 19import Data.Coerce
19#else 20#else
@@ -35,6 +36,9 @@ newtype IMap k a = IMap { intmap :: IntMap a }
35 , NFData 36 , NFData
36 ) 37 )
37 38
39adaptm_k_a_m f k a m = IMap $ adapt_k_a_m f k a m
40adapt_k_a_m f k a m = adapt_m (adapt_k f k a) m
41
38adapt_m_k :: Coercible k Int => (IntMap a -> Int -> x) -> IMap k a -> k -> x 42adapt_m_k :: Coercible k Int => (IntMap a -> Int -> x) -> IMap k a -> k -> x
39adapt_m_k f (IMap m) k = f m (coerce k) 43adapt_m_k f (IMap m) k = f m (coerce k)
40 44
@@ -42,16 +46,26 @@ adapt_k_m :: Coercible k Int => (Int -> IntMap a -> x) -> k -> IMap k a -> x
42adapt_k_m f k (IMap m) = f (coerce k) m 46adapt_k_m f k (IMap m) = f (coerce k) m
43-- adapt_k_m2 :: Coercible k Int => (Int -> IntMap a -> x) -> k -> IMap k a -> x 47-- adapt_k_m2 :: Coercible k Int => (Int -> IntMap a -> x) -> k -> IMap k a -> x
44-- adapt_k_m2 f k m = (adapt_k f) k (intmap m) 48-- adapt_k_m2 f k m = (adapt_k f) k (intmap m)
45-- adapt_k :: Coercible k Int => (Int -> x) -> k -> x 49
46-- adapt_k f k = f (coerce k) 50adaptm_k_m
51 :: Coercible k Int =>
52 (Int -> IntMap a -> IntMap a) -> k -> IMap k a -> IMap k a
53adaptm_k_m f k m = IMap $ adapt_k_m f k m
54
55adapt_k :: Coercible k Int => (Int -> x) -> k -> x
56adapt_k f k = f (coerce k)
47 57
48adapt_m_m :: (IntMap a -> IntMap a -> x) -> IMap k a -> IMap k a -> x 58adapt_m_m :: (IntMap a -> IntMap a -> x) -> IMap k a -> IMap k a -> x
49adapt_m_m f m = adapt_m (adapt_m f m) 59adapt_m_m f m = adapt_m (adapt_m f m)
50 60
61adaptm_m_m :: (IntMap a -> IntMap a -> IntMap a) -> IMap k a -> IMap k a -> IMap k a
62adaptm_m_m f a b = IMap $ adapt_m_m f a b
63
51adapt_m :: (IntMap a -> x) -> IMap k a -> x 64adapt_m :: (IntMap a -> x) -> IMap k a -> x
52adapt_m f (IMap m) = f m 65adapt_m f (IMap m) = f m
53 66
54first f (x,y) = (f x,y) 67first f (x,y) = (f x,y)
68second f (x,y) = (x,f y)
55 69
56 70
57(!) :: Coercible k Int => IMap k a -> k -> a 71(!) :: Coercible k Int => IMap k a -> k -> a
@@ -83,3 +97,107 @@ lookupLE :: ( Coercible Int k, Coercible k Int ) => k -> IMap k a -> Maybe (k, a
83lookupLE k m = fmap (first coerce) $ adapt_k_m (IntMap.lookupLE) k m 97lookupLE k m = fmap (first coerce) $ adapt_k_m (IntMap.lookupLE) k m
84lookupGE :: ( Coercible Int k, Coercible k Int ) => k -> IMap k a -> Maybe (k, a) 98lookupGE :: ( Coercible Int k, Coercible k Int ) => k -> IMap k a -> Maybe (k, a)
85lookupGE k m = fmap (first coerce) $ adapt_k_m (IntMap.lookupGE) k m 99lookupGE k m = fmap (first coerce) $ adapt_k_m (IntMap.lookupGE) k m
100
101empty = IMap IntMap.empty
102
103singleton :: Coercible k Int => k -> a -> IMap k a
104singleton = (IMap .) . adapt_k IntMap.singleton
105
106insert :: Coercible k Int => k -> a -> IMap k a -> IMap k a
107insert = adaptm_k_a_m IntMap.insert
108
109insertWith :: Coercible k Int => (a -> a -> a) -> k -> a -> IMap k a -> IMap k a
110insertWith f = adaptm_k_a_m (IntMap.insertWith f)
111
112insertWithKey :: (Coercible Int k, Coercible k Int) => (k -> a -> a -> a) -> k -> a -> IMap k a -> IMap k a
113insertWithKey f = adaptm_k_a_m (IntMap.insertWithKey $ f . coerce)
114
115insertLookupWithKey :: (Coercible Int k, Coercible k Int) =>
116 (k -> a -> a -> a) -> k -> a -> IMap k a -> (Maybe a, IMap k a)
117insertLookupWithKey f k a m = second IMap $ adapt_k_a_m (IntMap.insertLookupWithKey $ f . coerce) k a m
118
119delete :: Coercible k Int => k -> IMap k a -> IMap k a
120delete = adaptm_k_m IntMap.delete
121
122adjust :: Coercible k Int => (a -> a) -> k -> IMap k a -> IMap k a
123adjust f = adaptm_k_m (IntMap.adjust f)
124
125adjustWithKey :: ( Coercible Int k, Coercible k Int ) =>
126 (k -> a -> a) -> k -> IMap k a -> IMap k a
127adjustWithKey f = adaptm_k_m (IntMap.adjustWithKey $ f . coerce)
128
129update
130 :: Coercible k Int => (a -> Maybe a) -> k -> IMap k a -> IMap k a
131update f = adaptm_k_m (IntMap.update f)
132
133
134updateWithKey
135 :: (Coercible k Int, Coercible Int k) =>
136 (k -> a -> Maybe a) -> k -> IMap k a -> IMap k a
137updateWithKey f = adaptm_k_m (IntMap.updateWithKey $ f . coerce)
138
139updateLookupWithKey ::
140 (Coercible k Int, Coercible Int k) =>
141 (k -> a -> Maybe a) -> k -> IMap k a -> (Maybe a, IMap k a)
142updateLookupWithKey f k m =
143 second IMap $ adapt_k_m (IntMap.updateLookupWithKey $ f . coerce) k m
144
145alter :: Coercible k Int => (Maybe a -> Maybe a) -> k -> IMap k a -> IMap k a
146alter f = adaptm_k_m (IntMap.alter f)
147
148union :: IMap k a -> IMap k a -> IMap k a
149union = adaptm_m_m IntMap.union
150
151unionWith :: (a -> a -> a) -> IMap k a -> IMap k a -> IMap k a
152unionWith f = adaptm_m_m (IntMap.unionWith f)
153
154
155unionWithKey :: Coercible Int k => (k -> a -> a -> a) -> IMap k a -> IMap k a -> IMap k a
156unionWithKey f = adaptm_m_m (IntMap.unionWithKey $ f . coerce)
157
158unions :: Coercible [IMap k a] [IntMap a] => [IMap k a] -> IMap k a
159unions ms = IMap $ IntMap.unions (coerce ms)
160
161unionsWith :: Coercible [IMap k a] [IntMap a] => (a->a->a) -> [IMap k a] -> IMap k a
162unionsWith f ms = IMap $ IntMap.unionsWith f (coerce ms)
163
164difference = adaptm_m_m IntMap.difference
165
166differenceWith f = adaptm_m_m (IntMap.differenceWith f)
167
168differenceWithKey ::
169 Coercible Int k =>
170 (k -> a -> a -> Maybe a) -> IMap k a -> IMap k a -> IMap k a
171differenceWithKey f = adaptm_m_m (IntMap.differenceWithKey $ f . coerce)
172
173intersection = adaptm_m_m IntMap.intersection
174intersectionWith f = adaptm_m_m (IntMap.intersectionWith f)
175
176mergeWithKey ::
177 Coercible Int k =>
178 (k -> a -> b -> Maybe c)
179 -> (IMap k a -> IMap k c)
180 -> (IMap k b -> IMap k c)
181 -> IMap k a
182 -> IMap k b
183 -> IMap k c
184mergeWithKey f g1 g2 = adaptm_m_m (IntMap.mergeWithKey f' g1' g2')
185 where f' = f . coerce
186 g1' = intmap . g1 . IMap
187 g2' = intmap . g2 . IMap
188 adapt_m_m f m = adapt_m (adapt_m f m)
189 adaptm_m_m f a b = IMap $ adapt_m_m f a b
190
191map :: (a -> b) -> IMap k a -> IMap k b
192map f = IMap . adapt_m (IntMap.map f)
193
194mapWithKey :: Coercible Int k => (k -> a -> b) -> IMap k a -> IMap k b
195mapWithKey f = IMap . adapt_m (IntMap.mapWithKey $ f . coerce)
196
197traverseWithKey ::
198 (Applicative f, Coercible Int k) =>
199 (k -> a -> f b) -> IMap k a -> f (IMap k b)
200traverseWithKey f = fmap IMap . adapt_m (IntMap.traverseWithKey $ f . coerce)
201
202mapAccum :: (t -> b -> (t, a)) -> t -> IMap k b -> (t, IMap k a)
203mapAccum f a m = second IMap $ IntMap.mapAccum f a (intmap m)