1 | module Data.LRUCache1
3 | import public Data.HashPSQ
4 | import public Data.LRUCache.Internal
5 | import public Data.LRUCache1.Internal
11 | import Data.Zippable
12 | import Data.Linear.Ref1
22 | empty : (capacity : Nat)
23 | -> F1 s (LRUCache1 s k v)
25 | let lrucache # t := ref1 ( MkLRUCache capacity
30 | in (MkLRUCache1 lrucache) # t
37 | compress : List (k, Priority, v)
38 | -> List (k, Priority, v)
40 | let sortedq = sortBy compareByPriority q
41 | in zipWith (\(k, _, v), p => (k, p, v))
43 | [1..(length sortedq)]
45 | compareByPriority : (k, Priority, v)
48 | compareByPriority (_, p, _) (_, p', _) =
58 | -> F1 s (LRUCache1 s k v)
59 | insert key val (MkLRUCache1 cache) t =
61 | (\(MkLRUCache c s tc q) =>
62 | let (mboldval, queue) := HashPSQ.insertView key tc val q
63 | s1 := case isNothing mboldval of
66 | cache1 := MkLRUCache c s1 (tc `plus` 1) queue
69 | ( MkLRUCache c (s1 `minus` 1) (tc `plus` 1) (deleteMin queue)
86 | insertView : Hashable k
91 | -> F1 s (Maybe (k, v), LRUCache1 s k v)
92 | insertView key val (MkLRUCache1 cache) t =
94 | (\(MkLRUCache c s tc q) =>
95 | let (mboldval, queue) := HashPSQ.insertView key tc val q
96 | s1 := case isNothing mboldval of
101 | case HashPSQ.findMin queue of
103 | (assert_total $
idris_crash "Data.LRUCache1.insertView: internal error")
105 | ( MkLRUCache c (s1 `minus` 1) (tc `plus` 1) (deleteMin queue)
106 | , (Just (k, v), MkLRUCache1 cache)
109 | ( MkLRUCache c s1 (tc `plus` 1) queue
110 | , (Nothing, MkLRUCache1 cache)
120 | lookup : Hashable k
124 | -> F1 s (Maybe (v, LRUCache1 s k v))
125 | lookup k (MkLRUCache1 cache) t =
127 | (\(MkLRUCache c s tc q) =>
128 | case HashPSQ.alter (\x =>
133 | (Just x', Just (tc, x'))
136 | ( MkLRUCache c s tc q
140 | let tc1 = tc `plus` 1
143 | ( MkLRUCache c (s `minus` 1) tc1 (deleteMin queue)
144 | , Just (x, MkLRUCache1 cache)
147 | ( MkLRUCache c s tc1 queue
148 | , Just (x, MkLRUCache1 cache)
158 | toList : Hashable k
161 | -> F1 s (List (k, v))
162 | toList (MkLRUCache1 cache) t =
163 | let (MkLRUCache _ _ _ q) # t := read1 cache t
164 | lst := map (\(k, _, v) => (k, v)) (HashPSQ.toList q)