tmapchan 0.0.1 → 0.0.2
raw patch · 3 files changed
+29/−18 lines, 3 files
Files
- src/Control/Concurrent/STM/TMapChan.hs +15/−8
- src/Control/Concurrent/STM/TMapChan/Hash.hs +12/−8
- tmapchan.cabal +2/−2
src/Control/Concurrent/STM/TMapChan.hs view
@@ -7,18 +7,19 @@ , delete, deleteAll , -- * Utils getTChan+ , setTChan , broadcast , cloneAt, cloneAll, cloneAllUniquely ) where import Prelude hiding (lookup) import Data.Map.Lazy (Map)-import qualified Data.Map.Lazy as Map+import qualified Data.Map.Strict as Map import qualified Data.Set as Set import Control.Monad (void, forM_) import Control.Concurrent.STM (STM)-import Control.Concurrent.STM.TChan (TChan, newTChan, writeTChan, readTChan, tryReadTChan, peekTChan, tryPeekTChan, unGetTChan, cloneTChan)-import Control.Concurrent.STM.TVar (TVar, newTVar, readTVar, writeTVar, modifyTVar)+import Control.Concurrent.STM.TChan (TChan, newTChan, writeTChan, readTChan, tryReadTChan, peekTChan, tryPeekTChan, unGetTChan)+import Control.Concurrent.STM.TVar (TVar, newTVar, readTVar, writeTVar, modifyTVar') newtype TMapChan k a = TMapChan {runTMapChan :: TVar (Map k (TChan a))}@@ -74,7 +75,7 @@ mNext <- tryObserve t k case mNext of Nothing -> pure []- Just next -> ((:) next) <$> observeAll t k+ Just next -> (:) next <$> observeAll t k -- | Deletes the /next/ element in the map, if it exists. Doesn't block.@@ -107,6 +108,12 @@ pure c' Just c' -> pure c' ++setTChan :: Ord k => TMapChan k a -> k -> TChan a -> STM ()+setTChan (TMapChan t) k c = modifyTVar' t (Map.insert k c)+++ cloneAt :: Ord k => TMapChan k a -> k -- ^ key to clone /from/@@ -116,7 +123,7 @@ as <- observeAll t kFrom c <- newTChan forM_ as (writeTChan c)- modifyTVar xs (Map.insert kTo c)+ modifyTVar' xs (Map.insert kTo c) -- | Clones all the content for every key, by the key. cloneAll :: Ord k => TMapChan k a -> k -> STM ()@@ -126,7 +133,7 @@ forM_ (Map.keysSet as) $ \k' -> do as' <- observeAll t k' forM_ as' (writeTChan c)- modifyTVar xs (Map.insert k c)+ modifyTVar' xs (Map.insert k c) -- | Clones all the content from every channel, and inserts the unique subset of them to `k`, in the order of their Ord instance cloneAllUniquely :: ( Ord k@@ -141,7 +148,7 @@ as' <- newTVar Set.empty forM_ (Map.keysSet as) $ \k' -> do as'More <- Set.fromList <$> observeAll t k'- modifyTVar as' (Set.union as'More)+ modifyTVar' as' (Set.union as'More) as'All <- Set.toAscList <$> readTVar as' forM_ as'All (writeTChan c)- modifyTVar xs (Map.insert k c)+ modifyTVar' xs (Map.insert k c)
src/Control/Concurrent/STM/TMapChan/Hash.hs view
@@ -3,12 +3,12 @@ import Prelude hiding (lookup) import Data.Hashable (Hashable) import Data.HashMap.Lazy (HashMap)-import qualified Data.HashMap.Lazy as HashMap+import qualified Data.HashMap.Strict as HashMap import qualified Data.HashSet as HashSet import Control.Monad (void, forM_) import Control.Concurrent.STM (STM)-import Control.Concurrent.STM.TChan (TChan, newTChan, writeTChan, readTChan, tryReadTChan, peekTChan, tryPeekTChan, unGetTChan, cloneTChan)-import Control.Concurrent.STM.TVar (TVar, newTVar, readTVar, writeTVar, modifyTVar)+import Control.Concurrent.STM.TChan (TChan, newTChan, writeTChan, readTChan, tryReadTChan, peekTChan, tryPeekTChan, unGetTChan)+import Control.Concurrent.STM.TVar (TVar, newTVar, readTVar, writeTVar, modifyTVar') newtype TMapChan k a = TMapChan {runTMapChan :: TVar (HashMap k (TChan a))}@@ -65,7 +65,7 @@ mNext <- tryObserve t k case mNext of Nothing -> pure []- Just next -> ((:) next) <$> observeAll t k+ Just next -> (:) next <$> observeAll t k -- | Deletes the /next/ element in the map, if it exists. Doesn't block.@@ -91,6 +91,10 @@ pure c' Just c' -> pure c' +setTChan :: (Eq k, Hashable k) => TMapChan k a -> k -> TChan a -> STM ()+setTChan (TMapChan t) k c = modifyTVar' t (HashMap.insert k c)++ broadcast :: (Eq k, Hashable k) => TMapChan k a -> a -> STM () broadcast t@(TMapChan xs) a = do ks <- HashMap.keys <$> readTVar xs@@ -106,7 +110,7 @@ as <- observeAll t kFrom c <- newTChan forM_ as (writeTChan c)- modifyTVar xs (HashMap.insert kTo c)+ modifyTVar' xs (HashMap.insert kTo c) -- | Clones all the content for every key, by the key. cloneAll :: (Eq k, Hashable k) => TMapChan k a -> k -> STM ()@@ -116,7 +120,7 @@ forM_ (HashMap.keys as) $ \k' -> do as' <- observeAll t k' forM_ as' (writeTChan c)- modifyTVar xs (HashMap.insert k c)+ modifyTVar' xs (HashMap.insert k c) -- | Clones all the content from every channel, and inserts the unique subset of them to `k`, in an unspecified order. cloneAllUniquely :: ( Eq k, Hashable k@@ -131,7 +135,7 @@ as' <- newTVar HashSet.empty forM_ (HashMap.keys as) $ \k' -> do as'More <- HashSet.fromList <$> observeAll t k'- modifyTVar as' (HashSet.union as'More)+ modifyTVar' as' (HashSet.union as'More) as'All <- HashSet.toList <$> readTVar as' forM_ as'All (writeTChan c)- modifyTVar xs (HashMap.insert k c)+ modifyTVar' xs (HashMap.insert k c)
tmapchan.cabal view
@@ -1,6 +1,6 @@ name: tmapchan-version: 0.0.1-synopsis: A time-ordered multimap which consumes values as you lookup+version: 0.0.2+synopsis: An insert-ordered multimap (indexed FIFO) which consumes values as you lookup -- description: homepage: https://github.com/athanclark/tmapchan#readme license: BSD3