module Data.IOHashMap.STM
( IOHashMap
, newIOHashMap
, readIOHashMap
, modifyIOHashMap
, empty
, singleton
, null
, size
, member
, lookup
, (!?)
, findWithDefault
, (!)
, insert
, insertWith
, delete
, adjust
, update
, alter
, foldMapWithKey
, foldr
, foldl
, foldr'
, foldl'
, foldrWithKey'
, foldlWithKey'
, foldrWithKey
, foldlWithKey
, keys
, elems
, toList
, fromList
) where
import Control.Concurrent.STM (STM, TVar, modifyTVar', newTVar,
readTVar)
import Data.HashMap.Strict (HashMap)
import qualified Data.HashMap.Strict as HM
import Data.Hashable
import Prelude hiding (foldl, foldr, lookup, null)
newtype IOHashMap k v = IOHashMap (TVar (HashMap k v))
newIOHashMap :: HashMap k v -> STM (IOHashMap k v)
newIOHashMap :: HashMap k v -> STM (IOHashMap k v)
newIOHashMap HashMap k v
h = TVar (HashMap k v) -> IOHashMap k v
forall k v. TVar (HashMap k v) -> IOHashMap k v
IOHashMap (TVar (HashMap k v) -> IOHashMap k v)
-> STM (TVar (HashMap k v)) -> STM (IOHashMap k v)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> HashMap k v -> STM (TVar (HashMap k v))
forall a. a -> STM (TVar a)
newTVar HashMap k v
h
readIOHashMap :: (HashMap k v -> a) -> IOHashMap k v -> STM a
readIOHashMap :: (HashMap k v -> a) -> IOHashMap k v -> STM a
readIOHashMap HashMap k v -> a
f (IOHashMap TVar (HashMap k v)
h) = HashMap k v -> a
f (HashMap k v -> a) -> STM (HashMap k v) -> STM a
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> TVar (HashMap k v) -> STM (HashMap k v)
forall a. TVar a -> STM a
readTVar TVar (HashMap k v)
h
modifyIOHashMap :: (HashMap k v -> HashMap k v) -> IOHashMap k v -> STM ()
modifyIOHashMap :: (HashMap k v -> HashMap k v) -> IOHashMap k v -> STM ()
modifyIOHashMap HashMap k v -> HashMap k v
f (IOHashMap TVar (HashMap k v)
h) = TVar (HashMap k v) -> (HashMap k v -> HashMap k v) -> STM ()
forall a. TVar a -> (a -> a) -> STM ()
modifyTVar' TVar (HashMap k v)
h HashMap k v -> HashMap k v
f
empty :: STM (IOHashMap k v)
empty :: STM (IOHashMap k v)
empty = HashMap k v -> STM (IOHashMap k v)
forall k v. HashMap k v -> STM (IOHashMap k v)
newIOHashMap HashMap k v
forall k v. HashMap k v
HM.empty
singleton :: Hashable k => k -> v -> STM (IOHashMap k v)
singleton :: k -> v -> STM (IOHashMap k v)
singleton k
k = HashMap k v -> STM (IOHashMap k v)
forall k v. HashMap k v -> STM (IOHashMap k v)
newIOHashMap (HashMap k v -> STM (IOHashMap k v))
-> (v -> HashMap k v) -> v -> STM (IOHashMap k v)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. k -> v -> HashMap k v
forall k v. Hashable k => k -> v -> HashMap k v
HM.singleton k
k
null :: IOHashMap k v -> STM Bool
null :: IOHashMap k v -> STM Bool
null = (HashMap k v -> Bool) -> IOHashMap k v -> STM Bool
forall k v a. (HashMap k v -> a) -> IOHashMap k v -> STM a
readIOHashMap HashMap k v -> Bool
forall k v. HashMap k v -> Bool
HM.null
size :: IOHashMap k v -> STM Int
size :: IOHashMap k v -> STM Int
size = (HashMap k v -> Int) -> IOHashMap k v -> STM Int
forall k v a. (HashMap k v -> a) -> IOHashMap k v -> STM a
readIOHashMap HashMap k v -> Int
forall k v. HashMap k v -> Int
HM.size
member :: (Eq k, Hashable k) => k -> IOHashMap k a -> STM Bool
member :: k -> IOHashMap k a -> STM Bool
member = (HashMap k a -> Bool) -> IOHashMap k a -> STM Bool
forall k v a. (HashMap k v -> a) -> IOHashMap k v -> STM a
readIOHashMap ((HashMap k a -> Bool) -> IOHashMap k a -> STM Bool)
-> (k -> HashMap k a -> Bool) -> k -> IOHashMap k a -> STM Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. k -> HashMap k a -> Bool
forall k a. (Eq k, Hashable k) => k -> HashMap k a -> Bool
HM.member
lookup :: (Eq k, Hashable k) => k -> IOHashMap k v -> STM (Maybe v)
lookup :: k -> IOHashMap k v -> STM (Maybe v)
lookup = (HashMap k v -> Maybe v) -> IOHashMap k v -> STM (Maybe v)
forall k v a. (HashMap k v -> a) -> IOHashMap k v -> STM a
readIOHashMap ((HashMap k v -> Maybe v) -> IOHashMap k v -> STM (Maybe v))
-> (k -> HashMap k v -> Maybe v)
-> k
-> IOHashMap k v
-> STM (Maybe v)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. k -> HashMap k v -> Maybe v
forall k v. (Eq k, Hashable k) => k -> HashMap k v -> Maybe v
HM.lookup
(!?) :: (Eq k, Hashable k) => IOHashMap k v -> k -> STM (Maybe v)
!? :: IOHashMap k v -> k -> STM (Maybe v)
(!?) IOHashMap k v
m k
k = k -> IOHashMap k v -> STM (Maybe v)
forall k v.
(Eq k, Hashable k) =>
k -> IOHashMap k v -> STM (Maybe v)
lookup k
k IOHashMap k v
m
findWithDefault :: (Eq k, Hashable k)
=> v
-> k -> IOHashMap k v -> STM v
findWithDefault :: v -> k -> IOHashMap k v -> STM v
findWithDefault v
v = (HashMap k v -> v) -> IOHashMap k v -> STM v
forall k v a. (HashMap k v -> a) -> IOHashMap k v -> STM a
readIOHashMap ((HashMap k v -> v) -> IOHashMap k v -> STM v)
-> (k -> HashMap k v -> v) -> k -> IOHashMap k v -> STM v
forall b c a. (b -> c) -> (a -> b) -> a -> c
. v -> k -> HashMap k v -> v
forall k v. (Eq k, Hashable k) => v -> k -> HashMap k v -> v
HM.findWithDefault v
v
(!) :: (Eq k, Hashable k) => IOHashMap k v -> k -> STM v
(!) IOHashMap k v
m k
k = (HashMap k v -> v) -> IOHashMap k v -> STM v
forall k v a. (HashMap k v -> a) -> IOHashMap k v -> STM a
readIOHashMap ((HashMap k v -> k -> v) -> k -> HashMap k v -> v
forall a b c. (a -> b -> c) -> b -> a -> c
flip HashMap k v -> k -> v
forall k v.
(Eq k, Hashable k, HasCallStack) =>
HashMap k v -> k -> v
(HM.!) k
k) IOHashMap k v
m
infixl 9 !
insert :: (Eq k, Hashable k) => k -> v -> IOHashMap k v -> STM ()
insert :: k -> v -> IOHashMap k v -> STM ()
insert k
k = (HashMap k v -> HashMap k v) -> IOHashMap k v -> STM ()
forall k v. (HashMap k v -> HashMap k v) -> IOHashMap k v -> STM ()
modifyIOHashMap ((HashMap k v -> HashMap k v) -> IOHashMap k v -> STM ())
-> (v -> HashMap k v -> HashMap k v)
-> v
-> IOHashMap k v
-> STM ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. k -> v -> HashMap k v -> HashMap k v
forall k v.
(Eq k, Hashable k) =>
k -> v -> HashMap k v -> HashMap k v
HM.insert k
k
insertWith :: (Eq k, Hashable k) => (v -> v -> v) -> k -> v -> IOHashMap k v
-> STM ()
insertWith :: (v -> v -> v) -> k -> v -> IOHashMap k v -> STM ()
insertWith v -> v -> v
f k
k = (HashMap k v -> HashMap k v) -> IOHashMap k v -> STM ()
forall k v. (HashMap k v -> HashMap k v) -> IOHashMap k v -> STM ()
modifyIOHashMap ((HashMap k v -> HashMap k v) -> IOHashMap k v -> STM ())
-> (v -> HashMap k v -> HashMap k v)
-> v
-> IOHashMap k v
-> STM ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (v -> v -> v) -> k -> v -> HashMap k v -> HashMap k v
forall k v.
(Eq k, Hashable k) =>
(v -> v -> v) -> k -> v -> HashMap k v -> HashMap k v
HM.insertWith v -> v -> v
f k
k
delete :: (Eq k, Hashable k) => k -> IOHashMap k v -> STM ()
delete :: k -> IOHashMap k v -> STM ()
delete = (HashMap k v -> HashMap k v) -> IOHashMap k v -> STM ()
forall k v. (HashMap k v -> HashMap k v) -> IOHashMap k v -> STM ()
modifyIOHashMap ((HashMap k v -> HashMap k v) -> IOHashMap k v -> STM ())
-> (k -> HashMap k v -> HashMap k v)
-> k
-> IOHashMap k v
-> STM ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. k -> HashMap k v -> HashMap k v
forall k v. (Eq k, Hashable k) => k -> HashMap k v -> HashMap k v
HM.delete
adjust :: (Eq k, Hashable k) => (v -> v) -> k -> IOHashMap k v -> STM ()
adjust :: (v -> v) -> k -> IOHashMap k v -> STM ()
adjust v -> v
f = (HashMap k v -> HashMap k v) -> IOHashMap k v -> STM ()
forall k v. (HashMap k v -> HashMap k v) -> IOHashMap k v -> STM ()
modifyIOHashMap ((HashMap k v -> HashMap k v) -> IOHashMap k v -> STM ())
-> (k -> HashMap k v -> HashMap k v)
-> k
-> IOHashMap k v
-> STM ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (v -> v) -> k -> HashMap k v -> HashMap k v
forall k v.
(Eq k, Hashable k) =>
(v -> v) -> k -> HashMap k v -> HashMap k v
HM.adjust v -> v
f
update :: (Eq k, Hashable k) => (a -> Maybe a) -> k -> IOHashMap k a -> STM ()
update :: (a -> Maybe a) -> k -> IOHashMap k a -> STM ()
update a -> Maybe a
f = (HashMap k a -> HashMap k a) -> IOHashMap k a -> STM ()
forall k v. (HashMap k v -> HashMap k v) -> IOHashMap k v -> STM ()
modifyIOHashMap ((HashMap k a -> HashMap k a) -> IOHashMap k a -> STM ())
-> (k -> HashMap k a -> HashMap k a)
-> k
-> IOHashMap k a
-> STM ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> Maybe a) -> k -> HashMap k a -> HashMap k a
forall k a.
(Eq k, Hashable k) =>
(a -> Maybe a) -> k -> HashMap k a -> HashMap k a
HM.update a -> Maybe a
f
alter :: (Eq k, Hashable k) => (Maybe v -> Maybe v) -> k -> IOHashMap k v -> STM ()
alter :: (Maybe v -> Maybe v) -> k -> IOHashMap k v -> STM ()
alter Maybe v -> Maybe v
f = (HashMap k v -> HashMap k v) -> IOHashMap k v -> STM ()
forall k v. (HashMap k v -> HashMap k v) -> IOHashMap k v -> STM ()
modifyIOHashMap ((HashMap k v -> HashMap k v) -> IOHashMap k v -> STM ())
-> (k -> HashMap k v -> HashMap k v)
-> k
-> IOHashMap k v
-> STM ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Maybe v -> Maybe v) -> k -> HashMap k v -> HashMap k v
forall k v.
(Eq k, Hashable k) =>
(Maybe v -> Maybe v) -> k -> HashMap k v -> HashMap k v
HM.alter Maybe v -> Maybe v
f
foldMapWithKey :: Monoid m => (k -> v -> m) -> IOHashMap k v -> STM m
foldMapWithKey :: (k -> v -> m) -> IOHashMap k v -> STM m
foldMapWithKey = (HashMap k v -> m) -> IOHashMap k v -> STM m
forall k v a. (HashMap k v -> a) -> IOHashMap k v -> STM a
readIOHashMap ((HashMap k v -> m) -> IOHashMap k v -> STM m)
-> ((k -> v -> m) -> HashMap k v -> m)
-> (k -> v -> m)
-> IOHashMap k v
-> STM m
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (k -> v -> m) -> HashMap k v -> m
forall m k v. Monoid m => (k -> v -> m) -> HashMap k v -> m
HM.foldMapWithKey
foldr :: (v -> a -> a) -> a -> IOHashMap k v -> STM a
foldr :: (v -> a -> a) -> a -> IOHashMap k v -> STM a
foldr v -> a -> a
f = (HashMap k v -> a) -> IOHashMap k v -> STM a
forall k v a. (HashMap k v -> a) -> IOHashMap k v -> STM a
readIOHashMap ((HashMap k v -> a) -> IOHashMap k v -> STM a)
-> (a -> HashMap k v -> a) -> a -> IOHashMap k v -> STM a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (v -> a -> a) -> a -> HashMap k v -> a
forall v a k. (v -> a -> a) -> a -> HashMap k v -> a
HM.foldr v -> a -> a
f
foldl :: (a -> v -> a) -> a -> IOHashMap k v -> STM a
foldl :: (a -> v -> a) -> a -> IOHashMap k v -> STM a
foldl a -> v -> a
f = (HashMap k v -> a) -> IOHashMap k v -> STM a
forall k v a. (HashMap k v -> a) -> IOHashMap k v -> STM a
readIOHashMap ((HashMap k v -> a) -> IOHashMap k v -> STM a)
-> (a -> HashMap k v -> a) -> a -> IOHashMap k v -> STM a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> v -> a) -> a -> HashMap k v -> a
forall a v k. (a -> v -> a) -> a -> HashMap k v -> a
HM.foldl a -> v -> a
f
foldr' :: (v -> a -> a) -> a -> IOHashMap k v -> STM a
foldr' :: (v -> a -> a) -> a -> IOHashMap k v -> STM a
foldr' v -> a -> a
f = (HashMap k v -> a) -> IOHashMap k v -> STM a
forall k v a. (HashMap k v -> a) -> IOHashMap k v -> STM a
readIOHashMap ((HashMap k v -> a) -> IOHashMap k v -> STM a)
-> (a -> HashMap k v -> a) -> a -> IOHashMap k v -> STM a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (v -> a -> a) -> a -> HashMap k v -> a
forall v a k. (v -> a -> a) -> a -> HashMap k v -> a
HM.foldr' v -> a -> a
f
foldl' :: (a -> v -> a) -> a -> IOHashMap k v -> STM a
foldl' :: (a -> v -> a) -> a -> IOHashMap k v -> STM a
foldl' a -> v -> a
f = (HashMap k v -> a) -> IOHashMap k v -> STM a
forall k v a. (HashMap k v -> a) -> IOHashMap k v -> STM a
readIOHashMap ((HashMap k v -> a) -> IOHashMap k v -> STM a)
-> (a -> HashMap k v -> a) -> a -> IOHashMap k v -> STM a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> v -> a) -> a -> HashMap k v -> a
forall a v k. (a -> v -> a) -> a -> HashMap k v -> a
HM.foldl' a -> v -> a
f
foldrWithKey' :: (k -> v -> a -> a) -> a -> IOHashMap k v -> STM a
foldrWithKey' :: (k -> v -> a -> a) -> a -> IOHashMap k v -> STM a
foldrWithKey' k -> v -> a -> a
f = (HashMap k v -> a) -> IOHashMap k v -> STM a
forall k v a. (HashMap k v -> a) -> IOHashMap k v -> STM a
readIOHashMap ((HashMap k v -> a) -> IOHashMap k v -> STM a)
-> (a -> HashMap k v -> a) -> a -> IOHashMap k v -> STM a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (k -> v -> a -> a) -> a -> HashMap k v -> a
forall k v a. (k -> v -> a -> a) -> a -> HashMap k v -> a
HM.foldrWithKey' k -> v -> a -> a
f
foldlWithKey' :: (a -> k -> v -> a) -> a -> IOHashMap k v -> STM a
foldlWithKey' :: (a -> k -> v -> a) -> a -> IOHashMap k v -> STM a
foldlWithKey' a -> k -> v -> a
f = (HashMap k v -> a) -> IOHashMap k v -> STM a
forall k v a. (HashMap k v -> a) -> IOHashMap k v -> STM a
readIOHashMap ((HashMap k v -> a) -> IOHashMap k v -> STM a)
-> (a -> HashMap k v -> a) -> a -> IOHashMap k v -> STM a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> k -> v -> a) -> a -> HashMap k v -> a
forall a k v. (a -> k -> v -> a) -> a -> HashMap k v -> a
HM.foldlWithKey' a -> k -> v -> a
f
foldrWithKey :: (k -> v -> a -> a) -> a -> IOHashMap k v -> STM a
foldrWithKey :: (k -> v -> a -> a) -> a -> IOHashMap k v -> STM a
foldrWithKey k -> v -> a -> a
f = (HashMap k v -> a) -> IOHashMap k v -> STM a
forall k v a. (HashMap k v -> a) -> IOHashMap k v -> STM a
readIOHashMap ((HashMap k v -> a) -> IOHashMap k v -> STM a)
-> (a -> HashMap k v -> a) -> a -> IOHashMap k v -> STM a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (k -> v -> a -> a) -> a -> HashMap k v -> a
forall k v a. (k -> v -> a -> a) -> a -> HashMap k v -> a
HM.foldrWithKey k -> v -> a -> a
f
foldlWithKey :: (a -> k -> v -> a) -> a -> IOHashMap k v -> STM a
foldlWithKey :: (a -> k -> v -> a) -> a -> IOHashMap k v -> STM a
foldlWithKey a -> k -> v -> a
f = (HashMap k v -> a) -> IOHashMap k v -> STM a
forall k v a. (HashMap k v -> a) -> IOHashMap k v -> STM a
readIOHashMap ((HashMap k v -> a) -> IOHashMap k v -> STM a)
-> (a -> HashMap k v -> a) -> a -> IOHashMap k v -> STM a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> k -> v -> a) -> a -> HashMap k v -> a
forall a k v. (a -> k -> v -> a) -> a -> HashMap k v -> a
HM.foldlWithKey a -> k -> v -> a
f
keys :: IOHashMap k v -> STM [k]
keys :: IOHashMap k v -> STM [k]
keys = (HashMap k v -> [k]) -> IOHashMap k v -> STM [k]
forall k v a. (HashMap k v -> a) -> IOHashMap k v -> STM a
readIOHashMap HashMap k v -> [k]
forall k v. HashMap k v -> [k]
HM.keys
elems :: IOHashMap k v -> STM [v]
elems :: IOHashMap k v -> STM [v]
elems = (HashMap k v -> [v]) -> IOHashMap k v -> STM [v]
forall k v a. (HashMap k v -> a) -> IOHashMap k v -> STM a
readIOHashMap HashMap k v -> [v]
forall k v. HashMap k v -> [v]
HM.elems
toList :: IOHashMap k v -> STM [(k, v)]
toList :: IOHashMap k v -> STM [(k, v)]
toList = (HashMap k v -> [(k, v)]) -> IOHashMap k v -> STM [(k, v)]
forall k v a. (HashMap k v -> a) -> IOHashMap k v -> STM a
readIOHashMap HashMap k v -> [(k, v)]
forall k v. HashMap k v -> [(k, v)]
HM.toList
fromList :: (Eq k, Hashable k) => [(k, v)] -> STM (IOHashMap k v)
fromList :: [(k, v)] -> STM (IOHashMap k v)
fromList = HashMap k v -> STM (IOHashMap k v)
forall k v. HashMap k v -> STM (IOHashMap k v)
newIOHashMap (HashMap k v -> STM (IOHashMap k v))
-> ([(k, v)] -> HashMap k v) -> [(k, v)] -> STM (IOHashMap k v)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [(k, v)] -> HashMap k v
forall k v. (Eq k, Hashable k) => [(k, v)] -> HashMap k v
HM.fromList