Safe Haskell | None |
---|---|
Language | Haskell2010 |
A contention-free STM hash map. "Contention-free" means that the map will never cause spurious conflicts. A transaction operating on the map will only ever have to retry if another transaction is operating on the same key at the same time.
- data Map k v
- empty :: STM (Map k v)
- insert :: (Eq k, Hashable k) => k -> v -> Map k v -> STM ()
- delete :: (Eq k, Hashable k) => k -> Map k v -> STM ()
- unsafeDelete :: (Eq k, Hashable k) => k -> Map k v -> IO ()
- lookup :: (Eq k, Hashable k) => k -> Map k v -> STM (Maybe v)
- phantomLookup :: (Eq k, Hashable k) => k -> Map k v -> STM (Maybe v)
Documentation
Construction
Modification
insert :: (Eq k, Hashable k) => k -> v -> Map k v -> STM () Source
O(log n). Associate the given value with the given key. If the key is already present in the map, the old value is replaced.
delete :: (Eq k, Hashable k) => k -> Map k v -> STM () Source
O(log n). Remove the value associated with a given key from the map, if present.
Note: This does not actually remove the key from the map.
In fact, it might actually increase the map's memory consumption
by putting the key into the map.
To completely delete an entry, including its key, use unsafeDelete
.
unsafeDelete :: (Eq k, Hashable k) => k -> Map k v -> IO () Source
O(log n). This will completely remove a given key and its associated value from the map, if present. This is not an atomic operation, however. Use with caution!
Query
lookup :: (Eq k, Hashable k) => k -> Map k v -> STM (Maybe v) Source
O(log n). Return the value associated with the given key, or Nothing
.
Note: This might increase the map's memory consumption
by putting the key into the map.
If that is not acceptable, use phantomLookup
.
phantomLookup :: (Eq k, Hashable k) => k -> Map k v -> STM (Maybe v) Source
O(log n). Return the value associated with the given key, or Nothing
.
In contrast to lookup
, this will never increase the map's memory consumption.
However, it might allow phantom reads to occur.
Consider the following situation:
f = atomically $ do v1 <- phantomLookup k m v2 <- phantomLookup k m return (v1 == v2)
Under certain circumstances f
might actually return False
, in particular
if the first phantomLookup
happens on an empty map
and some other transaction inserts a value for k
before the second call
to phantomLookup
.