diff options
Diffstat (limited to 'IntMapClass.hs')
-rw-r--r-- | IntMapClass.hs | 122 |
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 ) | |||
14 | import Data.Traversable ( Traversable ) | 14 | import Data.Traversable ( Traversable ) |
15 | import Data.Monoid ( Monoid ) | 15 | import Data.Monoid ( Monoid ) |
16 | import Control.DeepSeq ( NFData ) | 16 | import Control.DeepSeq ( NFData ) |
17 | import Control.Applicative ( Applicative ) | ||
17 | #if MIN_VERSION_base(4,7,0) | 18 | #if MIN_VERSION_base(4,7,0) |
18 | import Data.Coerce | 19 | import 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 | ||
39 | adaptm_k_a_m f k a m = IMap $ adapt_k_a_m f k a m | ||
40 | adapt_k_a_m f k a m = adapt_m (adapt_k f k a) m | ||
41 | |||
38 | adapt_m_k :: Coercible k Int => (IntMap a -> Int -> x) -> IMap k a -> k -> x | 42 | adapt_m_k :: Coercible k Int => (IntMap a -> Int -> x) -> IMap k a -> k -> x |
39 | adapt_m_k f (IMap m) k = f m (coerce k) | 43 | adapt_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 | |||
42 | adapt_k_m f k (IMap m) = f (coerce k) m | 46 | adapt_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) | 50 | adaptm_k_m |
51 | :: Coercible k Int => | ||
52 | (Int -> IntMap a -> IntMap a) -> k -> IMap k a -> IMap k a | ||
53 | adaptm_k_m f k m = IMap $ adapt_k_m f k m | ||
54 | |||
55 | adapt_k :: Coercible k Int => (Int -> x) -> k -> x | ||
56 | adapt_k f k = f (coerce k) | ||
47 | 57 | ||
48 | adapt_m_m :: (IntMap a -> IntMap a -> x) -> IMap k a -> IMap k a -> x | 58 | adapt_m_m :: (IntMap a -> IntMap a -> x) -> IMap k a -> IMap k a -> x |
49 | adapt_m_m f m = adapt_m (adapt_m f m) | 59 | adapt_m_m f m = adapt_m (adapt_m f m) |
50 | 60 | ||
61 | adaptm_m_m :: (IntMap a -> IntMap a -> IntMap a) -> IMap k a -> IMap k a -> IMap k a | ||
62 | adaptm_m_m f a b = IMap $ adapt_m_m f a b | ||
63 | |||
51 | adapt_m :: (IntMap a -> x) -> IMap k a -> x | 64 | adapt_m :: (IntMap a -> x) -> IMap k a -> x |
52 | adapt_m f (IMap m) = f m | 65 | adapt_m f (IMap m) = f m |
53 | 66 | ||
54 | first f (x,y) = (f x,y) | 67 | first f (x,y) = (f x,y) |
68 | second 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 | |||
83 | lookupLE k m = fmap (first coerce) $ adapt_k_m (IntMap.lookupLE) k m | 97 | lookupLE k m = fmap (first coerce) $ adapt_k_m (IntMap.lookupLE) k m |
84 | lookupGE :: ( Coercible Int k, Coercible k Int ) => k -> IMap k a -> Maybe (k, a) | 98 | lookupGE :: ( Coercible Int k, Coercible k Int ) => k -> IMap k a -> Maybe (k, a) |
85 | lookupGE k m = fmap (first coerce) $ adapt_k_m (IntMap.lookupGE) k m | 99 | lookupGE k m = fmap (first coerce) $ adapt_k_m (IntMap.lookupGE) k m |
100 | |||
101 | empty = IMap IntMap.empty | ||
102 | |||
103 | singleton :: Coercible k Int => k -> a -> IMap k a | ||
104 | singleton = (IMap .) . adapt_k IntMap.singleton | ||
105 | |||
106 | insert :: Coercible k Int => k -> a -> IMap k a -> IMap k a | ||
107 | insert = adaptm_k_a_m IntMap.insert | ||
108 | |||
109 | insertWith :: Coercible k Int => (a -> a -> a) -> k -> a -> IMap k a -> IMap k a | ||
110 | insertWith f = adaptm_k_a_m (IntMap.insertWith f) | ||
111 | |||
112 | insertWithKey :: (Coercible Int k, Coercible k Int) => (k -> a -> a -> a) -> k -> a -> IMap k a -> IMap k a | ||
113 | insertWithKey f = adaptm_k_a_m (IntMap.insertWithKey $ f . coerce) | ||
114 | |||
115 | insertLookupWithKey :: (Coercible Int k, Coercible k Int) => | ||
116 | (k -> a -> a -> a) -> k -> a -> IMap k a -> (Maybe a, IMap k a) | ||
117 | insertLookupWithKey f k a m = second IMap $ adapt_k_a_m (IntMap.insertLookupWithKey $ f . coerce) k a m | ||
118 | |||
119 | delete :: Coercible k Int => k -> IMap k a -> IMap k a | ||
120 | delete = adaptm_k_m IntMap.delete | ||
121 | |||
122 | adjust :: Coercible k Int => (a -> a) -> k -> IMap k a -> IMap k a | ||
123 | adjust f = adaptm_k_m (IntMap.adjust f) | ||
124 | |||
125 | adjustWithKey :: ( Coercible Int k, Coercible k Int ) => | ||
126 | (k -> a -> a) -> k -> IMap k a -> IMap k a | ||
127 | adjustWithKey f = adaptm_k_m (IntMap.adjustWithKey $ f . coerce) | ||
128 | |||
129 | update | ||
130 | :: Coercible k Int => (a -> Maybe a) -> k -> IMap k a -> IMap k a | ||
131 | update f = adaptm_k_m (IntMap.update f) | ||
132 | |||
133 | |||
134 | updateWithKey | ||
135 | :: (Coercible k Int, Coercible Int k) => | ||
136 | (k -> a -> Maybe a) -> k -> IMap k a -> IMap k a | ||
137 | updateWithKey f = adaptm_k_m (IntMap.updateWithKey $ f . coerce) | ||
138 | |||
139 | updateLookupWithKey :: | ||
140 | (Coercible k Int, Coercible Int k) => | ||
141 | (k -> a -> Maybe a) -> k -> IMap k a -> (Maybe a, IMap k a) | ||
142 | updateLookupWithKey f k m = | ||
143 | second IMap $ adapt_k_m (IntMap.updateLookupWithKey $ f . coerce) k m | ||
144 | |||
145 | alter :: Coercible k Int => (Maybe a -> Maybe a) -> k -> IMap k a -> IMap k a | ||
146 | alter f = adaptm_k_m (IntMap.alter f) | ||
147 | |||
148 | union :: IMap k a -> IMap k a -> IMap k a | ||
149 | union = adaptm_m_m IntMap.union | ||
150 | |||
151 | unionWith :: (a -> a -> a) -> IMap k a -> IMap k a -> IMap k a | ||
152 | unionWith f = adaptm_m_m (IntMap.unionWith f) | ||
153 | |||
154 | |||
155 | unionWithKey :: Coercible Int k => (k -> a -> a -> a) -> IMap k a -> IMap k a -> IMap k a | ||
156 | unionWithKey f = adaptm_m_m (IntMap.unionWithKey $ f . coerce) | ||
157 | |||
158 | unions :: Coercible [IMap k a] [IntMap a] => [IMap k a] -> IMap k a | ||
159 | unions ms = IMap $ IntMap.unions (coerce ms) | ||
160 | |||
161 | unionsWith :: Coercible [IMap k a] [IntMap a] => (a->a->a) -> [IMap k a] -> IMap k a | ||
162 | unionsWith f ms = IMap $ IntMap.unionsWith f (coerce ms) | ||
163 | |||
164 | difference = adaptm_m_m IntMap.difference | ||
165 | |||
166 | differenceWith f = adaptm_m_m (IntMap.differenceWith f) | ||
167 | |||
168 | differenceWithKey :: | ||
169 | Coercible Int k => | ||
170 | (k -> a -> a -> Maybe a) -> IMap k a -> IMap k a -> IMap k a | ||
171 | differenceWithKey f = adaptm_m_m (IntMap.differenceWithKey $ f . coerce) | ||
172 | |||
173 | intersection = adaptm_m_m IntMap.intersection | ||
174 | intersectionWith f = adaptm_m_m (IntMap.intersectionWith f) | ||
175 | |||
176 | mergeWithKey :: | ||
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 | ||
184 | mergeWithKey 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 | |||
191 | map :: (a -> b) -> IMap k a -> IMap k b | ||
192 | map f = IMap . adapt_m (IntMap.map f) | ||
193 | |||
194 | mapWithKey :: Coercible Int k => (k -> a -> b) -> IMap k a -> IMap k b | ||
195 | mapWithKey f = IMap . adapt_m (IntMap.mapWithKey $ f . coerce) | ||
196 | |||
197 | traverseWithKey :: | ||
198 | (Applicative f, Coercible Int k) => | ||
199 | (k -> a -> f b) -> IMap k a -> f (IMap k b) | ||
200 | traverseWithKey f = fmap IMap . adapt_m (IntMap.traverseWithKey $ f . coerce) | ||
201 | |||
202 | mapAccum :: (t -> b -> (t, a)) -> t -> IMap k b -> (t, IMap k a) | ||
203 | mapAccum f a m = second IMap $ IntMap.mapAccum f a (intmap m) | ||