3 | import public Data.HashPSQ
4 | import public Data.LRUCache.Internal
22 | case capacity < 1 of
24 | assert_total $
idris_crash "Data.LRUCache.empty: capacity < 1"
36 | compress : List (k, Priority, v)
37 | -> List (k, Priority, v)
39 | let sortedq = sortBy compareByPriority q
40 | in zipWith (\(k, _, v), p => (k, p, v))
42 | [1..(length sortedq)]
44 | compareByPriority : (k, Priority, v)
47 | compareByPriority (_, p, _) (_, p', _) =
57 | trim cache@(MkLRUCache c s t q) =
75 | insert key val (MkLRUCache c s t q) =
76 | let (mboldval, queue) = HashPSQ.insertView key t val q
77 | in case isNothing mboldval of
100 | -> (Maybe (k, v), LRUCache k v)
101 | trim' cache@(MkLRUCache c s t q) =
104 | case HashPSQ.findMin q of
106 | assert_total $
idris_crash "Data.LRUCache.trim': internal error"
108 | let c' = MkLRUCache c
111 | (HashPSQ.deleteMin q)
112 | in (Just (k, v), c')
120 | insertView : Hashable k
125 | -> (Maybe (k, v), LRUCache k v)
126 | insertView key val (MkLRUCache c s t q) =
127 | let (mboldval, queue) = HashPSQ.insertView key t val q
128 | in case isNothing mboldval of
148 | lookup : Hashable k
152 | -> Maybe (v, LRUCache k v)
153 | lookup k cache@(MkLRUCache c s t q) =
154 | case HashPSQ.alter lookupAndBump k q of
165 | lookupAndBump : Maybe (a, b)
166 | -> (Maybe b, Maybe (Priority, b))
167 | lookupAndBump Nothing =
169 | lookupAndBump (Just (_, x)) =
170 | (Just x, Just (t, x))