git.iko.soy/typeable/refined-containers
on main help sign in
Add adjust', update', updateWithKey
commit3fab77c5a3eac4a5da900ae3d68ded6d8c65e6ee authormniip <mniip@mniip.com> dateWed 5 Mar 2025, 12:00 (2 years ago) parent7788f4a
6 files changed+222−16
src/Data/HashMap/Refined.hs+39−4 src/Data/HashMap/Strict/Refined.hs+39−4 src/Data/IntMap/Refined.hs+35−2 src/Data/IntMap/Strict/Refined.hs+35−2 src/Data/Map/Refined.hs+37−2 src/Data/Map/Strict/Refined.hs+37−2
src/Data/HashMap/Refined.hs+39−4
@@ -57,9 +57,12 @@ module Data.HashMap.Refined
5757 , insertLookupWithKey
5858 -- * Deletion/Update
5959 , Common.delete
60+ , adjust'
6061 , adjust
6162 , adjustWithKey
63+ , update'
6264 , update
65+ , updateWithKey
6366 , updateLookupWithKey
6467 -- * Query
6568 , Common.lookup
@@ -252,12 +255,22 @@ insertLookupWithKey f k v (HashMap m) =
252255 $ InsertProof (unsafeKey k) unsafeSubset
253256 )
254257
258+-- | If the given key is in the map, update the value at that key using the
259+-- given function. In any case, the set of keys remains the same.
260+adjust'
261+ :: forall s k a. Hashable k => (a -> a) -> k -> HashMap s k a -> HashMap s k a
262+adjust' = coerce $ HashMap.adjust @k @a
263+
255264-- | Update the value at a specific key known the be in the map using the given
256265-- function. The set of keys remains the same.
266+--
267+-- @
268+-- 'reinsert' k v = 'adjust (const v) k'
269+-- @
257270adjust
258271 :: forall s k a. Hashable k
259272 => (a -> a) -> Key s k -> HashMap s k a -> HashMap s k a
260−adjust = gcoerceWith (unsafeCastKey @s @k) $ coerce $ HashMap.adjust @k @a
273+adjust = gcoerceWith (unsafeCastKey @s @k) $ coerce $ adjust' @s @k @a
261274
262275-- | If the given key is in the map, update the associated value using the given
263276-- function with a proof that the key was in the map; otherwise return the map
@@ -267,6 +280,17 @@ adjustWithKey
267280 => (Key s k -> a -> a) -> k -> HashMap s k a -> HashMap s k a
268281adjustWithKey f k (HashMap m) = HashMap $ HashMap.adjust (f $ unsafeKey k) k m
269282
283+-- | If a key is present in the map, update its value or delete it using the
284+-- given function, returning a potentially smaller map.
285+update'
286+ :: forall s k a. Hashable k
287+ => (a -> Maybe a)
288+ -> k
289+ -> HashMap s k a
290+ -> SomeHashMapWith (SupersetProof 'Hashed s) k a
291+update' f k (HashMap m) = SomeHashMapWith (HashMap $ HashMap.update f k m)
292+ $ SupersetProof unsafeSubset
293+
270294-- | Update or delete a key known to be in the map using the given function,
271295-- returning a potentially smaller map.
272296update
@@ -275,9 +299,20 @@ update
275299 -> Key s k
276300 -> HashMap s k a
277301 -> SomeHashMapWith (SupersetProof 'Hashed s) k a
278−update f k (HashMap m)
279− = SomeHashMapWith (HashMap $ HashMap.update f (unrefine k) m)
280− $ SupersetProof unsafeSubset
302+update = gcoerceWith (unsafeCastKey @s @k) $ coerce $ update' @s @k @a
303+
304+-- | If a key is present in the map, update its value or delete it using the
305+-- given function with a proof that the key was in the map, returning a
306+-- potentially smaller map.
307+updateWithKey
308+ :: forall s k a. Hashable k
309+ => (Key s k -> a -> Maybe a)
310+ -> k
311+ -> HashMap s k a
312+ -> SomeHashMapWith (SupersetProof 'Hashed s) k a
313+updateWithKey f k (HashMap m) = SomeHashMapWith
314+ (HashMap $ HashMap.update (f $ unsafeKey k) k m)
315+ $ SupersetProof unsafeSubset
281316
282317-- | If the given key is in the map, update or delete it using the given
283318-- function with a proof that the key was in the map; otherwise the map is
src/Data/HashMap/Strict/Refined.hs+39−4
@@ -57,9 +57,12 @@ module Data.HashMap.Strict.Refined
5757 , insertLookupWithKey
5858 -- * Deletion/Update
5959 , Common.delete
60+ , adjust'
6061 , adjust
6162 , adjustWithKey
63+ , update'
6264 , update
65+ , updateWithKey
6366 , updateLookupWithKey
6467 -- * Query
6568 , Common.lookup
@@ -239,6 +242,10 @@ insertWithKey f k v (HashMap m) = SomeHashMapWith
239242
240243-- | Overwrite a key-value pair that is known to already be in the map. The set
241244-- of keys remains the same.
245+--
246+-- @
247+-- 'reinsert' k v = 'adjust (const v) k'
248+-- @
242249reinsert
243250 :: forall s k a. Hashable k
244251 => Key s k -> a -> HashMap s k a -> HashMap s k a
@@ -260,12 +267,18 @@ insertLookupWithKey f k v (HashMap m) =
260267 $ InsertProof (unsafeKey k) unsafeSubset
261268 )
262269
270+-- | If the given key is in the map, update the value at that key using the
271+-- given function. In any case, the set of keys remains the same.
272+adjust'
273+ :: forall s k a. Hashable k => (a -> a) -> k -> HashMap s k a -> HashMap s k a
274+adjust' = coerce $ HashMap.adjust @k @a
275+
263276-- | Update the value at a specific key known the be in the map using the given
264277-- function. The set of keys remains the same.
265278adjust
266279 :: forall s k a. Hashable k
267280 => (a -> a) -> Key s k -> HashMap s k a -> HashMap s k a
268−adjust = gcoerceWith (unsafeCastKey @s @k) $ coerce $ HashMap.adjust @k @a
281+adjust = gcoerceWith (unsafeCastKey @s @k) $ coerce $ adjust' @s @k @a
269282
270283-- | If the given key is in the map, update the associated value using the given
271284-- function with a proof that the key was in the map; otherwise return the map
@@ -275,6 +288,17 @@ adjustWithKey
275288 => (Key s k -> a -> a) -> k -> HashMap s k a -> HashMap s k a
276289adjustWithKey f k (HashMap m) = HashMap $ HashMap.adjust (f $ unsafeKey k) k m
277290
291+-- | If a key is present in the map, update its value or delete it using the
292+-- given function, returning a potentially smaller map.
293+update'
294+ :: forall s k a. Hashable k
295+ => (a -> Maybe a)
296+ -> k
297+ -> HashMap s k a
298+ -> SomeHashMapWith (SupersetProof 'Hashed s) k a
299+update' f k (HashMap m) = SomeHashMapWith (HashMap $ HashMap.update f k m)
300+ $ SupersetProof unsafeSubset
301+
278302-- | Update or delete a key known to be in the map using the given function,
279303-- returning a potentially smaller map.
280304update
@@ -283,9 +307,20 @@ update
283307 -> Key s k
284308 -> HashMap s k a
285309 -> SomeHashMapWith (SupersetProof 'Hashed s) k a
286−update f k (HashMap m)
287− = SomeHashMapWith (HashMap $ HashMap.update f (unrefine k) m)
288− $ SupersetProof unsafeSubset
310+update = gcoerceWith (unsafeCastKey @s @k) $ coerce $ update' @s @k @a
311+
312+-- | If a key is present in the map, update its value or delete it using the
313+-- given function with a proof that the key was in the map, returning a
314+-- potentially smaller map.
315+updateWithKey
316+ :: forall s k a. Hashable k
317+ => (Key s k -> a -> Maybe a)
318+ -> k
319+ -> HashMap s k a
320+ -> SomeHashMapWith (SupersetProof 'Hashed s) k a
321+updateWithKey f k (HashMap m) = SomeHashMapWith
322+ (HashMap $ HashMap.update (f $ unsafeKey k) k m)
323+ $ SupersetProof unsafeSubset
289324
290325-- | If the given key is in the map, update or delete it using the given
291326-- function with a proof that the key was in the map; otherwise the map is
src/Data/IntMap/Refined.hs+35−2
@@ -48,9 +48,12 @@ module Data.IntMap.Refined
4848 , insertLookupWithKey
4949 -- * Deletion/Update
5050 , Common.delete
51+ , adjust'
5152 , adjust
5253 , adjustWithKey
54+ , update'
5355 , update
56+ , updateWithKey
5457 , updateLookupWithKey
5558 -- * Query
5659 , Common.lookup
@@ -251,10 +254,19 @@ insertLookupWithKey f k v (IntMap m)
251254 (v', !m') -> ((unsafeKey k,) <$> v',)
252255 $ SomeIntMapWith (IntMap m') $ InsertProof (unsafeKey k) unsafeSubset
253256
257+-- | If the given key is in the map, update the value at that key using the
258+-- given function. In any case, the set of keys remains the same.
259+adjust' :: forall s a. (a -> a) -> Int -> IntMap s a -> IntMap s a
260+adjust' = coerce $ IntMap.adjust @a
261+
254262-- | Update the value at a specific key known the be in the map using the given
255263-- function. The set of keys remains the same.
264+--
265+-- @
266+-- 'reinsert' k v = 'adjust (const v) k'
267+-- @
256268adjust :: forall s a. (a -> a) -> Key s -> IntMap s a -> IntMap s a
257−adjust = gcoerceWith (unsafeCastKey @s) $ coerce $ IntMap.adjust @a
269+adjust = gcoerceWith (unsafeCastKey @s) $ coerce $ adjust' @s @a
258270
259271-- | If the given key is in the map, update the associated value using the given
260272-- function with a proof that the key was in the map; otherwise return the map
@@ -264,6 +276,16 @@ adjustWithKey
264276adjustWithKey = gcoerceWith (unsafeCastKey @s) $ coerce
265277 $ IntMap.adjustWithKey @a
266278
279+-- | If a key is present in the map, update its value or delete it using the
280+-- given function, returning a potentially smaller map.
281+update'
282+ :: forall s a. (a -> Maybe a)
283+ -> Int
284+ -> IntMap s a
285+ -> SomeIntMapWith (SupersetProof 'Int s) a
286+update' f k (IntMap m) = SomeIntMapWith (IntMap $ IntMap.update f k m)
287+ $ SupersetProof unsafeSubset
288+
267289-- | Update or delete a key known to be in the map using the given function,
268290-- returning a potentially smaller map.
269291update
@@ -271,7 +293,18 @@ update
271293 -> Key s
272294 -> IntMap s a
273295 -> SomeIntMapWith (SupersetProof 'Int s) a
274−update f k (IntMap m) = SomeIntMapWith (IntMap $ IntMap.update f (unrefine k) m)
296+update = gcoerceWith (unsafeCastKey @s) $ coerce $ update' @s @a
297+
298+-- | If a key is present in the map, update its value or delete it using the
299+-- given function with a proof that the key was in the map, returning a
300+-- potentially smaller map.
301+updateWithKey
302+ :: forall s a. (Key s -> a -> Maybe a)
303+ -> Int
304+ -> IntMap s a
305+ -> SomeIntMapWith (SupersetProof 'Int s) a
306+updateWithKey f k (IntMap m) = SomeIntMapWith
307+ (IntMap $ IntMap.updateWithKey (f . unsafeKey) k m)
275308 $ SupersetProof unsafeSubset
276309
277310-- | If the given key is in the map, update or delete it using the given
src/Data/IntMap/Strict/Refined.hs+35−2
@@ -48,9 +48,12 @@ module Data.IntMap.Strict.Refined
4848 , insertLookupWithKey
4949 -- * Deletion/Update
5050 , Common.delete
51+ , adjust'
5152 , adjust
5253 , adjustWithKey
54+ , update'
5355 , update
56+ , updateWithKey
5457 , updateLookupWithKey
5558 -- * Query
5659 , Common.lookup
@@ -239,6 +242,10 @@ insertWithKey f k v (IntMap m) = SomeIntMapWith
239242
240243-- | Overwrite a key-value pair that is known to already be in the map. The set
241244-- of keys remains the same.
245+--
246+-- @
247+-- 'reinsert' k v = 'adjust (const v) k'
248+-- @
242249reinsert
243250 :: forall s a. Key s -> a -> IntMap s a -> IntMap s a
244251reinsert = gcoerceWith (unsafeCastKey @s) $ coerce $ IntMap.insert @a
@@ -257,10 +264,15 @@ insertLookupWithKey f k v (IntMap m)
257264 (v', !m') -> ((unsafeKey k,) <$> v',)
258265 $ SomeIntMapWith (IntMap m') $ InsertProof (unsafeKey k) unsafeSubset
259266
267+-- | If the given key is in the map, update the value at that key using the
268+-- given function. In any case, the set of keys remains the same.
269+adjust' :: forall s a. (a -> a) -> Int -> IntMap s a -> IntMap s a
270+adjust' = coerce $ IntMap.adjust @a
271+
260272-- | Update the value at a specific key known the be in the map using the given
261273-- function. The set of keys remains the same.
262274adjust :: forall s a. (a -> a) -> Key s -> IntMap s a -> IntMap s a
263−adjust = gcoerceWith (unsafeCastKey @s) $ coerce $ IntMap.adjust @a
275+adjust = gcoerceWith (unsafeCastKey @s) $ coerce $ adjust' @s @a
264276
265277-- | If the given key is in the map, update the associated value using the given
266278-- function with a proof that the key was in the map; otherwise return the map
@@ -270,6 +282,16 @@ adjustWithKey
270282adjustWithKey = gcoerceWith (unsafeCastKey @s) $ coerce
271283 $ IntMap.adjustWithKey @a
272284
285+-- | If a key is present in the map, update its value or delete it using the
286+-- given function, returning a potentially smaller map.
287+update'
288+ :: forall s a. (a -> Maybe a)
289+ -> Int
290+ -> IntMap s a
291+ -> SomeIntMapWith (SupersetProof 'Int s) a
292+update' f k (IntMap m) = SomeIntMapWith (IntMap $ IntMap.update f k m)
293+ $ SupersetProof unsafeSubset
294+
273295-- | Update or delete a key known to be in the map using the given function,
274296-- returning a potentially smaller map.
275297update
@@ -277,7 +299,18 @@ update
277299 -> Key s
278300 -> IntMap s a
279301 -> SomeIntMapWith (SupersetProof 'Int s) a
280−update f k (IntMap m) = SomeIntMapWith (IntMap $ IntMap.update f (unrefine k) m)
302+update = gcoerceWith (unsafeCastKey @s) $ coerce $ update' @s @a
303+
304+-- | If a key is present in the map, update its value or delete it using the
305+-- given function with a proof that the key was in the map, returning a
306+-- potentially smaller map.
307+updateWithKey
308+ :: forall s a. (Key s -> a -> Maybe a)
309+ -> Int
310+ -> IntMap s a
311+ -> SomeIntMapWith (SupersetProof 'Int s) a
312+updateWithKey f k (IntMap m) = SomeIntMapWith
313+ (IntMap $ IntMap.updateWithKey (f . unsafeKey) k m)
281314 $ SupersetProof unsafeSubset
282315
283316-- | If the given key is in the map, update or delete it using the given
src/Data/Map/Refined.hs+37−2
@@ -57,9 +57,12 @@ module Data.Map.Refined
5757 , insertLookupWithKey
5858 -- * Deletion/Update
5959 , Common.delete
60+ , adjust'
6061 , adjust
6162 , adjustWithKey
63+ , update'
6264 , update
65+ , updateWithKey
6366 , updateLookupWithKey
6467 -- * Query
6568 , Common.lookup
@@ -238,6 +241,10 @@ insertWithKey f k v (Map m) = SomeMapWith
238241
239242-- | Overwrite a key-value pair that is known to already be in the map. The set
240243-- of keys remains the same.
244+--
245+-- @
246+-- 'reinsert' k v = 'adjust (const v) k'
247+-- @
241248reinsert
242249 :: forall s k a. Ord k
243250 => Key s k -> a -> Map s k a -> Map s k a
@@ -258,10 +265,15 @@ insertLookupWithKey f k v (Map m)
258265 (v', !m') -> ((unsafeKey k,) <$> v',)
259266 $ SomeMapWith (Map m') $ InsertProof (unsafeKey k) unsafeSubset
260267
268+-- | If the given key is in the map, update the value at that key using the
269+-- given function. In any case, the set of keys remains the same.
270+adjust' :: forall s k a. Ord k => (a -> a) -> k -> Map s k a -> Map s k a
271+adjust' = coerce $ Map.adjust @k @a
272+
261273-- | Update the value at a specific key known the be in the map using the given
262274-- function. The set of keys remains the same.
263275adjust :: forall s k a. Ord k => (a -> a) -> Key s k -> Map s k a -> Map s k a
264−adjust = gcoerceWith (unsafeCastKey @s @k) $ coerce $ Map.adjust @k @a
276+adjust = gcoerceWith (unsafeCastKey @s @k) $ coerce $ adjust' @s @k @a
265277
266278-- | If the given key is in the map, update the associated value using the given
267279-- function with a proof that the key was in the map; otherwise return the map
@@ -271,6 +283,17 @@ adjustWithKey
271283adjustWithKey = gcoerceWith (unsafeCastKey @s @k) $ coerce
272284 $ Map.adjustWithKey @k @a
273285
286+-- | If a key is present in the map, update its value or delete it using the
287+-- given function, returning a potentially smaller map.
288+update'
289+ :: forall s k a. Ord k
290+ => (a -> Maybe a)
291+ -> k
292+ -> Map s k a
293+ -> SomeMapWith (SupersetProof 'Regular s) k a
294+update' f k (Map m) = SomeMapWith (Map $ Map.update f k m)
295+ $ SupersetProof unsafeSubset
296+
274297-- | Update or delete a key known to be in the map using the given function,
275298-- returning a potentially smaller map.
276299update
@@ -279,7 +302,19 @@ update
279302 -> Key s k
280303 -> Map s k a
281304 -> SomeMapWith (SupersetProof 'Regular s) k a
282−update f k (Map m) = SomeMapWith (Map $ Map.update f (unrefine k) m)
305+update = gcoerceWith (unsafeCastKey @s @k) $ coerce $ update' @s @k @a
306+
307+-- | If a key is present in the map, update its value or delete it using the
308+-- given function with a proof that the key was in the map, returning a
309+-- potentially smaller map.
310+updateWithKey
311+ :: forall s k a. Ord k
312+ => (Key s k -> a -> Maybe a)
313+ -> k
314+ -> Map s k a
315+ -> SomeMapWith (SupersetProof 'Regular s) k a
316+updateWithKey f k (Map m) = SomeMapWith
317+ (Map $ Map.updateWithKey (f . unsafeKey) k m)
283318 $ SupersetProof unsafeSubset
284319
285320-- | If the given key is in the map, update or delete it using the given
src/Data/Map/Strict/Refined.hs+37−2
@@ -57,9 +57,12 @@ module Data.Map.Strict.Refined
5757 , insertLookupWithKey
5858 -- * Deletion/Update
5959 , Common.delete
60+ , adjust'
6061 , adjust
6162 , adjustWithKey
63+ , update'
6264 , update
65+ , updateWithKey
6366 , updateLookupWithKey
6467 -- * Query
6568 , Common.lookup
@@ -245,6 +248,10 @@ insertWithKey f k v (Map m) = SomeMapWith
245248
246249-- | Overwrite a key-value pair that is known to already be in the map. The set
247250-- of keys remains the same.
251+--
252+-- @
253+-- 'reinsert' k v = 'adjust (const v) k'
254+-- @
248255reinsert
249256 :: forall s k a. Ord k
250257 => Key s k -> a -> Map s k a -> Map s k a
@@ -265,10 +272,15 @@ insertLookupWithKey f k v (Map m)
265272 (v', !m') -> ((unsafeKey k,) <$> v',)
266273 $ SomeMapWith (Map m') $ InsertProof (unsafeKey k) unsafeSubset
267274
275+-- | If the given key is in the map, update the value at that key using the
276+-- given function. In any case, the set of keys remains the same.
277+adjust' :: forall s k a. Ord k => (a -> a) -> k -> Map s k a -> Map s k a
278+adjust' = coerce $ Map.adjust @k @a
279+
268280-- | Update the value at a specific key known the be in the map using the given
269281-- function. The set of keys remains the same.
270282adjust :: forall s k a. Ord k => (a -> a) -> Key s k -> Map s k a -> Map s k a
271−adjust = gcoerceWith (unsafeCastKey @s @k) $ coerce $ Map.adjust @k @a
283+adjust = gcoerceWith (unsafeCastKey @s @k) $ coerce $ adjust' @s @k @a
272284
273285-- | If the given key is in the map, update the associated value using the given
274286-- function with a proof that the key was in the map; otherwise return the map
@@ -278,6 +290,17 @@ adjustWithKey
278290adjustWithKey = gcoerceWith (unsafeCastKey @s @k) $ coerce
279291 $ Map.adjustWithKey @k @a
280292
293+-- | If a key is present in the map, update its value or delete it using the
294+-- given function, returning a potentially smaller map.
295+update'
296+ :: forall s k a. Ord k
297+ => (a -> Maybe a)
298+ -> k
299+ -> Map s k a
300+ -> SomeMapWith (SupersetProof 'Regular s) k a
301+update' f k (Map m) = SomeMapWith (Map $ Map.update f k m)
302+ $ SupersetProof unsafeSubset
303+
281304-- | Update or delete a key known to be in the map using the given function,
282305-- returning a potentially smaller map.
283306update
@@ -286,7 +309,19 @@ update
286309 -> Key s k
287310 -> Map s k a
288311 -> SomeMapWith (SupersetProof 'Regular s) k a
289−update f k (Map m) = SomeMapWith (Map $ Map.update f (unrefine k) m)
312+update = gcoerceWith (unsafeCastKey @s @k) $ coerce $ update' @s @k @a
313+
314+-- | If a key is present in the map, update its value or delete it using the
315+-- given function with a proof that the key was in the map, returning a
316+-- potentially smaller map.
317+updateWithKey
318+ :: forall s k a. Ord k
319+ => (Key s k -> a -> Maybe a)
320+ -> k
321+ -> Map s k a
322+ -> SomeMapWith (SupersetProof 'Regular s) k a
323+updateWithKey f k (Map m) = SomeMapWith
324+ (Map $ Map.updateWithKey (f . unsafeKey) k m)
290325 $ SupersetProof unsafeSubset
291326
292327-- | If the given key is in the map, update or delete it using the given