1 ! Copyright (C) 2017 John Benediktsson
2 ! See http://factorcode.org/license.txt for BSD license
4 USING: accessors assocs deques dlists kernel linked-assocs
5 linked-assocs.private math sequences.private ;
9 TUPLE: lru-cache < linked-assoc max-size ;
11 : <lru-cache> ( max-size exemplar -- assoc )
12 dupd new-assoc <dlist> rot lru-cache boa ;
14 : <lru-hash> ( max-size -- assoc )
18 [ assoc>> at* ] [ dlist>> dup ] bi '[
22 [ obj>> second-unsafe ] tri
27 [ call-next-method ] keep dup max-size>> [
28 over assoc>> assoc-size < [
29 [ dlist>> pop-front first-unsafe ]
31 [ dlist>> ] tri (delete-at)
36 [ assoc>> clone ] [ dlist>> clone ] [ max-size>> ] tri
39 TUPLE: fifo-cache < linked-assoc max-size ;
41 : <fifo-cache> ( max-size exemplar -- assoc )
42 dupd new-assoc <dlist> rot fifo-cache boa ;
44 : <fifo-hash> ( max-size -- assoc )
48 [ call-next-method ] keep dup max-size>> [
49 over assoc>> assoc-size < [
50 [ dlist>> pop-front first-unsafe ]
52 [ dlist>> ] tri (delete-at)
57 [ assoc>> clone ] [ dlist>> clone ] [ max-size>> ] tri
60 TUPLE: lifo-cache < linked-assoc max-size ;
62 : <lifo-cache> ( max-size exemplar -- assoc )
63 dupd new-assoc <dlist> rot lifo-cache boa ;
65 : <lifo-hash> ( max-size -- assoc )
70 over assoc>> assoc-size <= [
72 [ dlist>> pop-back first-unsafe ]
74 [ dlist>> ] tri (delete-at)
76 ] when* call-next-method ;
79 [ assoc>> clone ] [ dlist>> clone ] [ max-size>> ] tri