timemap 0.0.1 → 0.0.2
raw patch · 2 files changed
+24/−1 lines, 2 files
Files
- src/Data/TimeMap.hs +23/−0
- timemap.cabal +1/−1
src/Data/TimeMap.hs view
@@ -21,6 +21,7 @@ , -- * Construction newTimeMap , insert+ , update , adjust , delete , -- * Query@@ -29,6 +30,7 @@ , ageOf , keys , elems+ , size , null , -- * Filter filter@@ -100,6 +102,8 @@ elems :: TimeMap k a -> STM [a] elems xs = L.toList $ (snd . snd) <$> HT.stream (keysMap xs) +size :: TimeMap k a -> STM Int+size xs = length <$> elems xs null :: TimeMap k a -> STM Bool null xs = HT.null (keysMap xs)@@ -118,6 +122,25 @@ now <- getCurrentTime mt <- atomically (timeOf k xs) pure (diffUTCTime now <$> mt)+++-- | Updates or deletes the value at @k@, while updating its time.+update :: ( Hashable k+ , Eq k+ ) => (a -> Maybe a) -> k -> TimeMap k a -> IO ()+update p k xs = do+ now <- getCurrentTime+ atomically $ HT.focus (go now) k (keysMap xs)+ where+ go _ Nothing = pure ((), F.Keep)+ go now (Just (oldTime, y)) =+ let (action,minsert) =+ case p y of+ Nothing -> (F.Remove , MM.remove oldTime k)+ Just y' -> (F.Replace (now, y'), id)+ in do modifyTVar (timeMap xs) (MM.insert now k . minsert)+ pure ((), action)+ -- | Adjusts the value at @k@, while updating its time.
timemap.cabal view
@@ -1,5 +1,5 @@ Name: timemap-Version: 0.0.1+Version: 0.0.2 Author: Athan Clark <athan.clark@gmail.com> Maintainer: Athan Clark <athan.clark@gmail.com> License: BSD3