module StmContainers.Bimap
( Bimap,
new,
newIO,
null,
size,
focusLeft,
focusRight,
lookupLeft,
lookupRight,
insertLeft,
insertRight,
deleteLeft,
deleteRight,
reset,
unfoldlM,
listT,
)
where
import qualified Focus as B
import qualified StmContainers.Map as A
import StmContainers.Prelude hiding (delete, empty, foldM, insert, lookup, null, toList)
data Bimap leftKey rightKey
= Bimap !(A.Map leftKey rightKey) !(A.Map rightKey leftKey)
deriving (Typeable)
{-# INLINE new #-}
new :: STM (Bimap leftKey rightKey)
new :: forall leftKey rightKey. STM (Bimap leftKey rightKey)
new =
forall leftKey rightKey.
Map leftKey rightKey
-> Map rightKey leftKey -> Bimap leftKey rightKey
Bimap forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall key value. STM (Map key value)
A.new forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall key value. STM (Map key value)
A.new
{-# INLINE newIO #-}
newIO :: IO (Bimap leftKey rightKey)
newIO :: forall leftKey rightKey. IO (Bimap leftKey rightKey)
newIO =
forall leftKey rightKey.
Map leftKey rightKey
-> Map rightKey leftKey -> Bimap leftKey rightKey
Bimap forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall key value. IO (Map key value)
A.newIO forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall key value. IO (Map key value)
A.newIO
{-# INLINE null #-}
null :: Bimap leftKey rightKey -> STM Bool
null :: forall leftKey rightKey. Bimap leftKey rightKey -> STM Bool
null (Bimap Map leftKey rightKey
leftMap Map rightKey leftKey
_) =
forall key value. Map key value -> STM Bool
A.null Map leftKey rightKey
leftMap
{-# INLINE size #-}
size :: Bimap leftKey rightKey -> STM Int
size :: forall leftKey rightKey. Bimap leftKey rightKey -> STM Int
size (Bimap Map leftKey rightKey
leftMap Map rightKey leftKey
_) =
forall key value. Map key value -> STM Int
A.size Map leftKey rightKey
leftMap
{-# INLINE focusLeft #-}
focusLeft :: (Hashable leftKey, Hashable rightKey) => B.Focus rightKey STM result -> leftKey -> Bimap leftKey rightKey -> STM result
focusLeft :: forall leftKey rightKey result.
(Hashable leftKey, Hashable rightKey) =>
Focus rightKey STM result
-> leftKey -> Bimap leftKey rightKey -> STM result
focusLeft Focus rightKey STM result
rightFocus leftKey
leftKey (Bimap Map leftKey rightKey
leftMap Map rightKey leftKey
rightMap) =
do
((result
output, Change rightKey
change), Maybe rightKey
maybeRightKey) <- forall key value result.
Hashable key =>
Focus value STM result -> key -> Map key value -> STM result
A.focus (forall (m :: * -> *) a b.
Monad m =>
Focus a m b -> Focus a m (b, Maybe a)
B.extractingInput (forall (m :: * -> *) a b.
Monad m =>
Focus a m b -> Focus a m (b, Change a)
B.extractingChange Focus rightKey STM result
rightFocus)) leftKey
leftKey Map leftKey rightKey
leftMap
case Change rightKey
change of
Change rightKey
B.Leave ->
forall (m :: * -> *) a. Monad m => a -> m a
return ()
Change rightKey
B.Remove ->
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ Maybe rightKey
maybeRightKey forall a b. (a -> b) -> a -> b
$ \rightKey
oldRightKey -> forall key value. Hashable key => key -> Map key value -> STM ()
A.delete rightKey
oldRightKey Map rightKey leftKey
rightMap
B.Set rightKey
newRightKey ->
do
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ Maybe rightKey
maybeRightKey forall a b. (a -> b) -> a -> b
$ \rightKey
rightKey -> forall key value. Hashable key => key -> Map key value -> STM ()
A.delete rightKey
rightKey Map rightKey leftKey
rightMap
Maybe leftKey
maybeReplacedLeftKey <- forall key value result.
Hashable key =>
Focus value STM result -> key -> Map key value -> STM result
A.focus (forall (m :: * -> *) a. Monad m => Focus a m (Maybe a)
B.lookup forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* forall (m :: * -> *) a. Monad m => a -> Focus a m ()
B.insert leftKey
leftKey) rightKey
newRightKey Map rightKey leftKey
rightMap
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ Maybe leftKey
maybeReplacedLeftKey forall a b. (a -> b) -> a -> b
$ \leftKey
replacedLeftKey -> forall key value. Hashable key => key -> Map key value -> STM ()
A.delete leftKey
replacedLeftKey Map leftKey rightKey
leftMap
forall (m :: * -> *) a. Monad m => a -> m a
return result
output
{-# INLINE focusRight #-}
focusRight :: (Hashable leftKey, Hashable rightKey) => B.Focus leftKey STM result -> rightKey -> Bimap leftKey rightKey -> STM result
focusRight :: forall leftKey rightKey result.
(Hashable leftKey, Hashable rightKey) =>
Focus leftKey STM result
-> rightKey -> Bimap leftKey rightKey -> STM result
focusRight Focus leftKey STM result
valueFocus2 rightKey
rightKey (Bimap Map leftKey rightKey
leftMap Map rightKey leftKey
rightMap) =
forall leftKey rightKey result.
(Hashable leftKey, Hashable rightKey) =>
Focus rightKey STM result
-> leftKey -> Bimap leftKey rightKey -> STM result
focusLeft Focus leftKey STM result
valueFocus2 rightKey
rightKey (forall leftKey rightKey.
Map leftKey rightKey
-> Map rightKey leftKey -> Bimap leftKey rightKey
Bimap Map rightKey leftKey
rightMap Map leftKey rightKey
leftMap)
{-# INLINE lookupLeft #-}
lookupLeft :: (Hashable leftKey) => leftKey -> Bimap leftKey rightKey -> STM (Maybe rightKey)
lookupLeft :: forall leftKey rightKey.
Hashable leftKey =>
leftKey -> Bimap leftKey rightKey -> STM (Maybe rightKey)
lookupLeft leftKey
leftKey (Bimap Map leftKey rightKey
leftMap Map rightKey leftKey
_) =
forall key value.
Hashable key =>
key -> Map key value -> STM (Maybe value)
A.lookup leftKey
leftKey Map leftKey rightKey
leftMap
{-# INLINE lookupRight #-}
lookupRight :: (Hashable rightKey) => rightKey -> Bimap leftKey rightKey -> STM (Maybe leftKey)
lookupRight :: forall rightKey leftKey.
Hashable rightKey =>
rightKey -> Bimap leftKey rightKey -> STM (Maybe leftKey)
lookupRight rightKey
rightKey (Bimap Map leftKey rightKey
_ Map rightKey leftKey
rightMap) =
forall key value.
Hashable key =>
key -> Map key value -> STM (Maybe value)
A.lookup rightKey
rightKey Map rightKey leftKey
rightMap
{-# INLINE insertLeft #-}
insertLeft :: (Hashable leftKey, Hashable rightKey) => rightKey -> leftKey -> Bimap leftKey rightKey -> STM ()
insertLeft :: forall leftKey rightKey.
(Hashable leftKey, Hashable rightKey) =>
rightKey -> leftKey -> Bimap leftKey rightKey -> STM ()
insertLeft rightKey
rightKey =
forall leftKey rightKey result.
(Hashable leftKey, Hashable rightKey) =>
Focus rightKey STM result
-> leftKey -> Bimap leftKey rightKey -> STM result
focusLeft (forall (m :: * -> *) a. Monad m => a -> Focus a m ()
B.insert rightKey
rightKey)
{-# INLINE insertRight #-}
insertRight :: (Hashable leftKey, Hashable rightKey) => leftKey -> rightKey -> Bimap leftKey rightKey -> STM ()
insertRight :: forall leftKey rightKey.
(Hashable leftKey, Hashable rightKey) =>
leftKey -> rightKey -> Bimap leftKey rightKey -> STM ()
insertRight leftKey
leftKey rightKey
rightKey (Bimap Map leftKey rightKey
leftMap Map rightKey leftKey
rightMap) =
forall leftKey rightKey.
(Hashable leftKey, Hashable rightKey) =>
rightKey -> leftKey -> Bimap leftKey rightKey -> STM ()
insertLeft leftKey
leftKey rightKey
rightKey (forall leftKey rightKey.
Map leftKey rightKey
-> Map rightKey leftKey -> Bimap leftKey rightKey
Bimap Map rightKey leftKey
rightMap Map leftKey rightKey
leftMap)
{-# INLINE deleteLeft #-}
deleteLeft :: (Hashable leftKey, Hashable rightKey) => leftKey -> Bimap leftKey rightKey -> STM ()
deleteLeft :: forall leftKey rightKey.
(Hashable leftKey, Hashable rightKey) =>
leftKey -> Bimap leftKey rightKey -> STM ()
deleteLeft leftKey
leftKey (Bimap Map leftKey rightKey
leftMap Map rightKey leftKey
rightMap) =
forall key value result.
Hashable key =>
Focus value STM result -> key -> Map key value -> STM result
A.focus forall (m :: * -> *) a. Monad m => Focus a m (Maybe a)
B.lookupAndDelete leftKey
leftKey Map leftKey rightKey
leftMap
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ (\rightKey
rightKey -> forall key value. Hashable key => key -> Map key value -> STM ()
A.delete rightKey
rightKey Map rightKey leftKey
rightMap)
{-# INLINE deleteRight #-}
deleteRight :: (Hashable leftKey, Hashable rightKey) => rightKey -> Bimap leftKey rightKey -> STM ()
deleteRight :: forall leftKey rightKey.
(Hashable leftKey, Hashable rightKey) =>
rightKey -> Bimap leftKey rightKey -> STM ()
deleteRight rightKey
rightKey (Bimap Map leftKey rightKey
leftMap Map rightKey leftKey
rightMap) =
forall leftKey rightKey.
(Hashable leftKey, Hashable rightKey) =>
leftKey -> Bimap leftKey rightKey -> STM ()
deleteLeft rightKey
rightKey (forall leftKey rightKey.
Map leftKey rightKey
-> Map rightKey leftKey -> Bimap leftKey rightKey
Bimap Map rightKey leftKey
rightMap Map leftKey rightKey
leftMap)
{-# INLINE reset #-}
reset :: Bimap leftKey rightKey -> STM ()
reset :: forall leftKey rightKey. Bimap leftKey rightKey -> STM ()
reset (Bimap Map leftKey rightKey
leftMap Map rightKey leftKey
rightMap) =
do
forall key value. Map key value -> STM ()
A.reset Map leftKey rightKey
leftMap
forall key value. Map key value -> STM ()
A.reset Map rightKey leftKey
rightMap
{-# INLINE unfoldlM #-}
unfoldlM :: Bimap leftKey rightKey -> UnfoldlM STM (leftKey, rightKey)
unfoldlM :: forall leftKey rightKey.
Bimap leftKey rightKey -> UnfoldlM STM (leftKey, rightKey)
unfoldlM (Bimap Map leftKey rightKey
leftMap Map rightKey leftKey
_) =
forall key value. Map key value -> UnfoldlM STM (key, value)
A.unfoldlM Map leftKey rightKey
leftMap
{-# INLINE listT #-}
listT :: Bimap key value -> ListT STM (key, value)
listT :: forall key value. Bimap key value -> ListT STM (key, value)
listT (Bimap Map key value
leftMap Map value key
_) =
forall key value. Map key value -> ListT STM (key, value)
A.listT Map key value
leftMap