@@ -57,9 +57,12 @@ module Data.HashMap.Strict.Refined
57 , insertLookupWithKey
57 , insertLookupWithKey
58 -- * Deletion/Update
58 -- * Deletion/Update
59 , Common.delete
59 , Common.delete
61 , adjustWithKey
62 , adjustWithKey
63 , updateLookupWithKey
66 , updateLookupWithKey
64 -- * Query
67 -- * Query
65 , Common.lookup
68 , Common.lookup
@@ -239,6 +242,10 @@ insertWithKey f k v (HashMap m) = SomeHashMapWith
240-- | Overwrite a key-value pair that is known to already be in the map. The set
243-- | Overwrite a key-value pair that is known to already be in the map. The set
241-- of keys remains the same.
244-- of keys remains the same.
247+-- 'reinsert' k v = 'adjust (const v) k'
243 :: forall s k a. Hashable k
250 :: forall s k a. Hashable k
244 => Key s k -> a -> HashMap s k a -> HashMap s k a
251 => Key s k -> a -> HashMap s k a -> HashMap s k a
@@ -260,12 +267,18 @@ insertLookupWithKey f k v (HashMap m) =
260 $ InsertProof (unsafeKey k) unsafeSubset
267 $ InsertProof (unsafeKey k) unsafeSubset
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.
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
263-- | Update the value at a specific key known the be in the map using the given
276-- | Update the value at a specific key known the be in the map using the given
264-- function. The set of keys remains the same.
277-- function. The set of keys remains the same.
266 :: forall s k a. Hashable k
279 :: forall s k a. Hashable k
267 => (a -> a) -> Key s k -> HashMap s k a -> HashMap s k a
280 => (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
270-- | If the given key is in the map, update the associated value using the given
283-- | If the given key is in the map, update the associated value using the given
271-- function with a proof that the key was in the map; otherwise return the map
284-- function with a proof that the key was in the map; otherwise return the map
@@ -275,6 +288,17 @@ adjustWithKey
275 => (Key s k -> a -> a) -> k -> HashMap s k a -> HashMap s k a
288 => (Key s k -> a -> a) -> k -> HashMap s k a -> HashMap s k a
276adjustWithKey f k (HashMap m) = HashMap $ HashMap.adjust (f $ unsafeKey k) k m
289adjustWithKey f k (HashMap m) = HashMap $ HashMap.adjust (f $ unsafeKey k) k m
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.
294+ :: forall s k a. Hashable k
298+ -> SomeHashMapWith (SupersetProof 'Hashed s) k a
299+update' f k (HashMap m) = SomeHashMapWith (HashMap $ HashMap.update f k m)
300+ $ SupersetProof unsafeSubset
278-- | Update or delete a key known to be in the map using the given function,
302-- | Update or delete a key known to be in the map using the given function,
279-- returning a potentially smaller map.
303-- returning a potentially smaller map.
@@ -283,9 +307,20 @@ update
283 -> Key s k
307 -> Key s k
284 -> HashMap s k a
308 -> HashMap s k a
285 -> SomeHashMapWith (SupersetProof 'Hashed s) k a
309 -> SomeHashMapWith (SupersetProof 'Hashed s) k a
286−update f k (HashMap m)
310+update = gcoerceWith (unsafeCastKey @s @k) $ coerce $ update' @s @k @a
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.
316+ :: forall s k a. Hashable k
317+ => (Key s k -> a -> Maybe a)
320+ -> SomeHashMapWith (SupersetProof 'Hashed s) k a
321+updateWithKey f k (HashMap m) = SomeHashMapWith
287− = SomeHashMapWith (HashMap $ HashMap.update f (unrefine k) m)
322+ (HashMap $ HashMap.update (f $ unsafeKey k) k m)
288− $ SupersetProof unsafeSubset
323+ $ SupersetProof unsafeSubset
290-- | If the given key is in the map, update or delete it using the given
325-- | If the given key is in the map, update or delete it using the given
291-- function with a proof that the key was in the map; otherwise the map is
326-- function with a proof that the key was in the map; otherwise the map is