Skip to content

Commit

Permalink
Adds insertIfAbsent to SLMap
Browse files Browse the repository at this point in the history
The `Data.LVar.SLMap.insert` function throws the runtime error if
multiple insertion attempts at the same key index is attempted. This
commits adds `insertIfAbsent`, which silently ignores subsequent
insertion attempts after a first successful attempt for a given key.

Relates to iu-parfunc#124 .
  • Loading branch information
robstewart57 committed Sep 15, 2015
1 parent 64e38e2 commit 98e958e
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion haskell/lvish/Data/LVar/SLMap.hs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ module Data.LVar.SLMap
-- * The type and its basic operations
IMap,
newEmptyMap, newMap, newFromList,
insert,
insert,insertIfAbsent,
getKey, waitSize, waitValue,
modify,

Expand Down Expand Up @@ -238,6 +238,18 @@ insert !key !elm (IMap (WrapLVar lv)) = WrapPar$ putLV lv putter
Added _ -> return $ Just (key, elm)
Found _ -> throw$ ConflictingPutExn$ "Multiple puts to one entry in an IMap!"

-- | Put a single entry into the map if the key is not present,
-- otherwise the insertion request is silently ignored.
-- (WHNF) Strict in the key and value.
insertIfAbsent :: (Ord k, Eq v, HasPut e) =>
k -> v -> IMap k s v -> Par e s ()
insertIfAbsent !key !elm (IMap (WrapLVar lv)) = WrapPar$ putLV lv putter
where putter slm = do
putRes <- SLM.putIfAbsent slm key $ return elm
case putRes of
Added _ -> return $ Just (key, elm)
Found _ -> return Nothing

-- | `IMap`s containing other LVars have some additional capabilities compared to
-- those containing regular Haskell data. In particular, it is possible to modify
-- existing entries (monotonically). Further, this `modify` function implicitly
Expand Down

0 comments on commit 98e958e

Please sign in to comment.