module STMContainers.Map
(
Map,
Key,
new,
newIO,
insert,
delete,
deleteAll,
lookup,
focus,
null,
size,
stream,
)
where
import STMContainers.Prelude hiding (insert, delete, lookup, alter, foldM, toList, empty, null)
import qualified STMContainers.HAMT as HAMT
import qualified STMContainers.HAMT.Nodes as HAMTNodes
import qualified Focus
newtype Map k v = Map (HAMT.HAMT (k, v))
deriving (Typeable)
type Key a = (Eq a, Hashable a)
instance (Eq k) => HAMTNodes.Element (k, v) where
type ElementKey (k, v) = k
elementKey (k, v) = k
associationValue :: (k, v) -> v
associationValue (_, v) = v
lookup :: (Key k) => k -> Map k v -> STM (Maybe v)
lookup k = focus Focus.lookupM k
insert :: (Key k) => v -> k -> Map k v -> STM ()
insert !v !k (Map h) = HAMT.insert (k, v) h
delete :: (Key k) => k -> Map k v -> STM ()
delete k (Map h) = HAMT.focus Focus.deleteM k h
deleteAll :: Map k v -> STM ()
deleteAll (Map h) = HAMT.deleteAll h
focus :: (Key k) => Focus.StrategyM STM v r -> k -> Map k v -> STM r
focus f k (Map h) = HAMT.focus f' k h
where
f' = (fmap . fmap . fmap) (\v -> k `seq` v `seq` (k, v)) . f . fmap associationValue
new :: STM (Map k v)
new = Map <$> HAMT.new
newIO :: IO (Map k v)
newIO = Map <$> HAMT.newIO
null :: Map k v -> STM Bool
null (Map h) = HAMT.null h
size :: Map k v -> STM Int
size (Map h) = HAMTNodes.size h
stream :: Map k v -> ListT STM (k, v)
stream (Map h) = HAMT.stream h