packages feed

expiring-containers (empty) → 0.1.0.2

raw patch · 5 files changed

+152/−0 lines, 5 filesdep +basedep +containersdep +hashablesetup-changed

Dependencies added: base, containers, hashable, int-multimap, time, timestamp, unordered-containers

Files

+ LICENSE view
@@ -0,0 +1,22 @@+Copyright (c) 2018, Metrix.AI++Permission is hereby granted, free of charge, to any person+obtaining a copy of this software and associated documentation+files (the "Software"), to deal in the Software without+restriction, including without limitation the rights to use,+copy, modify, merge, publish, distribute, sublicense, and/or sell+copies of the Software, and to permit persons to whom the+Software is furnished to do so, subject to the following+conditions:++The above copyright notice and this permission notice shall be+included in all copies or substantial portions of the Software.++THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES+OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT+HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,+WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR+OTHER DEALINGS IN THE SOFTWARE.
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ expiring-containers.cabal view
@@ -0,0 +1,53 @@+name:+  expiring-containers+version:+  0.1.0.2+synopsis:+  Expiring containers+category:+  Time, Containers+homepage:+  https://github.com/metrix-ai/expiring-containers+bug-reports:+  https://github.com/metrix-ai/expiring-containers/issues+author:+  Oleg Shevchenko <shevchenko.cmc@gmail.com>+maintainer:+  Metrix.AI Ninjas <ninjas@metrix.ai>+copyright:+  (c) 2018, Metrix.AI+license:+  MIT+license-file:+  LICENSE+build-type:+  Simple+cabal-version:+  >=1.24++source-repository head+  type:+    git+  location:+    https://github.com/metrix-ai/expiring-containers.git++library+  hs-source-dirs:+    library+  default-extensions:+    Arrows, BangPatterns, ConstraintKinds, DataKinds, DefaultSignatures, DeriveDataTypeable, DeriveFoldable, DeriveFunctor, DeriveGeneric, DeriveTraversable, EmptyDataDecls, FlexibleContexts, FlexibleInstances, FunctionalDependencies, GADTs, GeneralizedNewtypeDeriving, LambdaCase, LiberalTypeSynonyms, MagicHash, MultiParamTypeClasses, MultiWayIf, NoImplicitPrelude, NoMonomorphismRestriction, OverloadedStrings, PatternGuards, ParallelListComp, QuasiQuotes, RankNTypes, RecordWildCards, ScopedTypeVariables, StandaloneDeriving, TemplateHaskell, TupleSections, TypeFamilies, TypeOperators, UnboxedTuples, StrictData+  ghc-options: -funbox-strict-fields+  default-language:+    Haskell2010+  exposed-modules:+    ExpiringContainers.ExpiringMap+    ExpiringContainers.ExpiringSet+  other-modules:+  build-depends:+    base >=4.7 && <5,+    time >=1.8 && <2,+    hashable >=1.2 && <2,+    timestamp >=0.2 && <0.3,+    containers >=0.5.10 && <0.5.15,+    int-multimap >=0.1 && <0.2,+    unordered-containers >=0.2.8.0 && <0.3
+ library/ExpiringContainers/ExpiringMap.hs view
@@ -0,0 +1,33 @@+module ExpiringContainers.ExpiringMap+  where++import ExpiringContainers.ExpiringSet as A+import Data.HashMap.Strict as B+import Data.Time+import Data.Maybe+import Prelude+import Data.Hashable+import Data.List++{-|++-}+data ExpiringMap key value =+  ExpiringMap+    (ExpiringSet key)+    (HashMap key value)++insert :: (Eq key, Ord key, Hashable key) => UTCTime {-^ Expiry time -} -> key -> value -> ExpiringMap key value -> ExpiringMap key value+insert time key value (ExpiringMap expSet hashMap) =+  ExpiringMap (A.insert time key expSet) (B.insert key value hashMap)++lookup :: (Eq key, Hashable key) => key -> ExpiringMap key value -> Maybe value+lookup key (ExpiringMap expSet hashMap) =+  B.lookup key hashMap++setCurrentTime :: (Eq key, Ord key, Hashable key) => UTCTime -> ExpiringMap key value -> ExpiringMap key value+setCurrentTime time (ExpiringMap expSet hashMap) =+  ExpiringMap newExpSet newHashMap+    where+      (keys, newExpSet) = A.clean time expSet+      newHashMap = Data.List.foldl' (flip B.delete) hashMap keys
+ library/ExpiringContainers/ExpiringSet.hs view
@@ -0,0 +1,42 @@+module ExpiringContainers.ExpiringSet+  where++import Data.HashMap.Strict as A+import IntMultiMap as B+import Data.HashSet as C+import Data.Time+import Data.Int+import Prelude+import Timestamp+import Data.Hashable+import Data.Foldable++{-|++-}+data ExpiringSet element =+  ExpiringSet+    {-| Elements indexed by timestamps -}+    (IntMultiMap element)+    {-| Timestamps indexed by elements -}+    (HashMap element Int)+++clean :: (Hashable element, Ord element) => UTCTime -> ExpiringSet element -> ([element], ExpiringSet element)+clean time (ExpiringSet intMultiMap hashMap) =+  (listElem, ExpiringSet newMultiMap newHash)+  where+    key = fromIntegral $ (timestampMicroSecondsInt64 . utcTimeTimestamp) time+    newHash = A.filterWithKey (\_ k -> k >= key) hashMap+    (oldMultiMap, newMultiMap) = (B.split key intMultiMap)+    listElem = Data.Foldable.toList oldMultiMap++insert :: (Hashable element, Ord element) => UTCTime {-^ Expiry time -} -> element -> ExpiringSet element -> ExpiringSet element+insert time value (ExpiringSet intMultiMap hashMap) =+  ExpiringSet newMultiMap (A.insert value key hashMap)+  where+    key = fromIntegral $ (timestampMicroSecondsInt64 . utcTimeTimestamp) time+    startKey = A.lookup value hashMap+    newMultiMap = B.insert key value $ case startKey of+      Just k -> B.delete k value intMultiMap+      Nothing -> intMultiMap